summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/net/fragment.c
blob: 781b9bc60c79d17246d3b7e6c0ce1f67594473af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ipxe/retry.h>
#include <ipxe/timer.h>
#include <ipxe/ipstat.h>
#include <ipxe/fragment.h>

/** @file
 *
 * Fragment reassembly
 *
 */

/**
 * Expire fragment reassembly buffer
 *
 * @v timer		Retry timer
 * @v fail		Failure indicator
 */
static void fragment_expired ( struct retry_timer *timer, int fail __unused ) {
	struct fragment *fragment =
		container_of ( timer, struct fragment, timer );

	DBGC ( fragment, "FRAG %p expired\n", fragment );
	free_iob ( fragment->iobuf );
	list_del ( &fragment->list );
	fragment->fragments->stats->reasm_fails++;
	free ( fragment );
}

/**
 * Find fragment reassembly buffer
 *
 * @v fragments		Fragment reassembler
 * @v iobuf		I/O buffer
 * @v hdrlen		Length of non-fragmentable potion of I/O buffer
 * @ret fragment	Fragment reassembly buffer, or NULL if not found
 */
static struct fragment * fragment_find ( struct fragment_reassembler *fragments,
					 struct io_buffer *iobuf,
					 size_t hdrlen ) {
	struct fragment *fragment;

	list_for_each_entry ( fragment, &fragments->list, list ) {
		if ( fragments->is_fragment ( fragment, iobuf, hdrlen ) )
			return fragment;
	}
	return NULL;
}

/**
 * Reassemble packet
 *
 * @v fragments		Fragment reassembler
 * @v iobuf		I/O buffer
 * @v hdrlen		Length of non-fragmentable potion of I/O buffer
 * @ret iobuf		Reassembled packet, or NULL
 *
 * This function takes ownership of the I/O buffer.  Note that the
 * length of the non-fragmentable portion may be modified.
 */
struct io_buffer * fragment_reassemble ( struct fragment_reassembler *fragments,
					 struct io_buffer *iobuf,
					 size_t *hdrlen ) {
	struct fragment *fragment;
	struct io_buffer *new_iobuf;
	size_t new_len;
	size_t offset;
	size_t expected_offset;
	int more_frags;

	/* Update statistics */
	fragments->stats->reasm_reqds++;

	/* Find matching fragment reassembly buffer, if any */
	fragment = fragment_find ( fragments, iobuf, *hdrlen );

	/* Drop out-of-order fragments */
	offset = fragments->fragment_offset ( iobuf, *hdrlen );
	expected_offset = ( fragment ? ( iob_len ( fragment->iobuf ) -
					 fragment->hdrlen ) : 0 );
	if ( offset != expected_offset ) {
		DBGC ( fragment, "FRAG %p dropping out-of-sequence fragment "
		       "[%zd,%zd), expected [%zd,...)\n", fragment, offset,
		       ( offset + iob_len ( iobuf ) - *hdrlen ),
		       expected_offset );
		goto drop;
	}

	/* Create or extend fragment reassembly buffer as applicable */
	if ( ! fragment ) {

		/* Create new fragment reassembly buffer */
		fragment = zalloc ( sizeof ( *fragment ) );
		if ( ! fragment )
			goto drop;
		list_add ( &fragment->list, &fragments->list );
		fragment->iobuf = iobuf;
		fragment->hdrlen = *hdrlen;
		timer_init ( &fragment->timer, fragment_expired, NULL );
		fragment->fragments = fragments;
		DBGC ( fragment, "FRAG %p [0,%zd)\n", fragment,
		       ( iob_len ( iobuf ) - *hdrlen ) );

	} else {

		/* Check if this is the final fragment */
		more_frags = fragments->more_fragments ( iobuf, *hdrlen );
		DBGC ( fragment, "FRAG %p [%zd,%zd)%s\n", fragment,
		       offset, ( offset + iob_len ( iobuf ) - *hdrlen ),
		       ( more_frags ? "" : " complete" ) );

		/* Extend fragment reassembly buffer.  Preserve I/O
		 * buffer headroom to allow for code which modifies
		 * and resends the buffer (e.g. ICMP echo responses).
		 */
		iob_pull ( iobuf, *hdrlen );
		new_len = ( iob_headroom ( fragment->iobuf ) +
			    iob_len ( fragment->iobuf ) + iob_len ( iobuf ) );
		new_iobuf = alloc_iob ( new_len );
		if ( ! new_iobuf ) {
			DBGC ( fragment, "FRAG %p could not extend reassembly "
			       "buffer to %zd bytes\n", fragment, new_len );
			goto drop;
		}
		iob_reserve ( new_iobuf, iob_headroom ( fragment->iobuf ) );
		memcpy ( iob_put ( new_iobuf, iob_len ( fragment->iobuf ) ),
			 fragment->iobuf->data, iob_len ( fragment->iobuf ) );
		memcpy ( iob_put ( new_iobuf, iob_len ( iobuf ) ),
			 iobuf->data, iob_len ( iobuf ) );
		free_iob ( fragment->iobuf );
		fragment->iobuf = new_iobuf;
		free_iob ( iobuf );

		/* Stop fragment reassembly timer */
		stop_timer ( &fragment->timer );

		/* If this is the final fragment, return it */
		if ( ! more_frags ) {
			iobuf = fragment->iobuf;
			*hdrlen = fragment->hdrlen;
			list_del ( &fragment->list );
			free ( fragment );
			fragments->stats->reasm_oks++;
			return iobuf;
		}
	}

	/* (Re)start fragment reassembly timer */
	start_timer_fixed ( &fragment->timer, FRAGMENT_TIMEOUT );

	return NULL;

 drop:
	fragments->stats->reasm_fails++;
	free_iob ( iobuf );
	return NULL;
}