2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/retry.h>
26 #include <ipxe/timer.h>
27 #include <ipxe/ipstat.h>
28 #include <ipxe/fragment.h>
37 * Expire fragment reassembly buffer
39 * @v timer Retry timer
40 * @v fail Failure indicator
42 static void fragment_expired ( struct retry_timer *timer, int fail __unused ) {
43 struct fragment *fragment =
44 container_of ( timer, struct fragment, timer );
46 DBGC ( fragment, "FRAG %p expired\n", fragment );
47 free_iob ( fragment->iobuf );
48 list_del ( &fragment->list );
49 fragment->fragments->stats->reasm_fails++;
54 * Find fragment reassembly buffer
56 * @v fragments Fragment reassembler
58 * @v hdrlen Length of non-fragmentable potion of I/O buffer
59 * @ret fragment Fragment reassembly buffer, or NULL if not found
61 static struct fragment * fragment_find ( struct fragment_reassembler *fragments,
62 struct io_buffer *iobuf,
64 struct fragment *fragment;
66 list_for_each_entry ( fragment, &fragments->list, list ) {
67 if ( fragments->is_fragment ( fragment, iobuf, hdrlen ) )
76 * @v fragments Fragment reassembler
78 * @v hdrlen Length of non-fragmentable potion of I/O buffer
79 * @ret iobuf Reassembled packet, or NULL
81 * This function takes ownership of the I/O buffer. Note that the
82 * length of the non-fragmentable portion may be modified.
84 struct io_buffer * fragment_reassemble ( struct fragment_reassembler *fragments,
85 struct io_buffer *iobuf,
87 struct fragment *fragment;
88 struct io_buffer *new_iobuf;
91 size_t expected_offset;
94 /* Update statistics */
95 fragments->stats->reasm_reqds++;
97 /* Find matching fragment reassembly buffer, if any */
98 fragment = fragment_find ( fragments, iobuf, *hdrlen );
100 /* Drop out-of-order fragments */
101 offset = fragments->fragment_offset ( iobuf, *hdrlen );
102 expected_offset = ( fragment ? ( iob_len ( fragment->iobuf ) -
103 fragment->hdrlen ) : 0 );
104 if ( offset != expected_offset ) {
105 DBGC ( fragment, "FRAG %p dropping out-of-sequence fragment "
106 "[%zd,%zd), expected [%zd,...)\n", fragment, offset,
107 ( offset + iob_len ( iobuf ) - *hdrlen ),
112 /* Create or extend fragment reassembly buffer as applicable */
115 /* Create new fragment reassembly buffer */
116 fragment = zalloc ( sizeof ( *fragment ) );
119 list_add ( &fragment->list, &fragments->list );
120 fragment->iobuf = iobuf;
121 fragment->hdrlen = *hdrlen;
122 timer_init ( &fragment->timer, fragment_expired, NULL );
123 fragment->fragments = fragments;
124 DBGC ( fragment, "FRAG %p [0,%zd)\n", fragment,
125 ( iob_len ( iobuf ) - *hdrlen ) );
129 /* Check if this is the final fragment */
130 more_frags = fragments->more_fragments ( iobuf, *hdrlen );
131 DBGC ( fragment, "FRAG %p [%zd,%zd)%s\n", fragment,
132 offset, ( offset + iob_len ( iobuf ) - *hdrlen ),
133 ( more_frags ? "" : " complete" ) );
135 /* Extend fragment reassembly buffer. Preserve I/O
136 * buffer headroom to allow for code which modifies
137 * and resends the buffer (e.g. ICMP echo responses).
139 iob_pull ( iobuf, *hdrlen );
140 new_len = ( iob_headroom ( fragment->iobuf ) +
141 iob_len ( fragment->iobuf ) + iob_len ( iobuf ) );
142 new_iobuf = alloc_iob ( new_len );
144 DBGC ( fragment, "FRAG %p could not extend reassembly "
145 "buffer to %zd bytes\n", fragment, new_len );
148 iob_reserve ( new_iobuf, iob_headroom ( fragment->iobuf ) );
149 memcpy ( iob_put ( new_iobuf, iob_len ( fragment->iobuf ) ),
150 fragment->iobuf->data, iob_len ( fragment->iobuf ) );
151 memcpy ( iob_put ( new_iobuf, iob_len ( iobuf ) ),
152 iobuf->data, iob_len ( iobuf ) );
153 free_iob ( fragment->iobuf );
154 fragment->iobuf = new_iobuf;
157 /* Stop fragment reassembly timer */
158 stop_timer ( &fragment->timer );
160 /* If this is the final fragment, return it */
161 if ( ! more_frags ) {
162 iobuf = fragment->iobuf;
163 *hdrlen = fragment->hdrlen;
164 list_del ( &fragment->list );
166 fragments->stats->reasm_oks++;
171 /* (Re)start fragment reassembly timer */
172 start_timer_fixed ( &fragment->timer, FRAGMENT_TIMEOUT );
177 fragments->stats->reasm_fails++;