Deleted Added
full compact
frag6.c (196019) frag6.c (207369)
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
30 */
31
32#include <sys/cdefs.h>
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet6/frag6.c 196019 2009-08-01 19:26:27Z rwatson $");
33__FBSDID("$FreeBSD: head/sys/netinet6/frag6.c 207369 2010-04-29 11:52:42Z bz $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/malloc.h>
38#include <sys/mbuf.h>
39#include <sys/domain.h>
40#include <sys/protosw.h>
41#include <sys/socket.h>
42#include <sys/errno.h>
43#include <sys/time.h>
44#include <sys/kernel.h>
45#include <sys/syslog.h>
46
47#include <net/if.h>
48#include <net/route.h>
49#include <net/vnet.h>
50
51#include <netinet/in.h>
52#include <netinet/in_var.h>
53#include <netinet/ip6.h>
54#include <netinet6/ip6_var.h>
55#include <netinet/icmp6.h>
56#include <netinet/in_systm.h> /* for ECN definitions */
57#include <netinet/ip.h> /* for ECN definitions */
58
59#include <security/mac/mac_framework.h>
60
61/*
62 * Define it to get a correct behavior on per-interface statistics.
63 * You will need to perform an extra routing table lookup, per fragment,
64 * to do it. This may, or may not be, a performance hit.
65 */
66#define IN6_IFSTAT_STRICT
67
68static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
69static void frag6_deq(struct ip6asfrag *);
70static void frag6_insque(struct ip6q *, struct ip6q *);
71static void frag6_remque(struct ip6q *);
72static void frag6_freef(struct ip6q *);
73
74static struct mtx ip6qlock;
75/*
76 * These fields all protected by ip6qlock.
77 */
78static VNET_DEFINE(u_int, frag6_nfragpackets);
79static VNET_DEFINE(u_int, frag6_nfrags);
80static VNET_DEFINE(struct ip6q, ip6q); /* ip6 reassemble queue */
81
82#define V_frag6_nfragpackets VNET(frag6_nfragpackets)
83#define V_frag6_nfrags VNET(frag6_nfrags)
84#define V_ip6q VNET(ip6q)
85
86#define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
87#define IP6Q_LOCK() mtx_lock(&ip6qlock)
88#define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock)
89#define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED)
90#define IP6Q_UNLOCK() mtx_unlock(&ip6qlock)
91
92static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
93
94/*
95 * Initialise reassembly queue and fragment identifier.
96 */
97static void
98frag6_change(void *tag)
99{
100
101 V_ip6_maxfragpackets = nmbclusters / 4;
102 V_ip6_maxfrags = nmbclusters / 4;
103}
104
105void
106frag6_init(void)
107{
108
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/malloc.h>
38#include <sys/mbuf.h>
39#include <sys/domain.h>
40#include <sys/protosw.h>
41#include <sys/socket.h>
42#include <sys/errno.h>
43#include <sys/time.h>
44#include <sys/kernel.h>
45#include <sys/syslog.h>
46
47#include <net/if.h>
48#include <net/route.h>
49#include <net/vnet.h>
50
51#include <netinet/in.h>
52#include <netinet/in_var.h>
53#include <netinet/ip6.h>
54#include <netinet6/ip6_var.h>
55#include <netinet/icmp6.h>
56#include <netinet/in_systm.h> /* for ECN definitions */
57#include <netinet/ip.h> /* for ECN definitions */
58
59#include <security/mac/mac_framework.h>
60
61/*
62 * Define it to get a correct behavior on per-interface statistics.
63 * You will need to perform an extra routing table lookup, per fragment,
64 * to do it. This may, or may not be, a performance hit.
65 */
66#define IN6_IFSTAT_STRICT
67
68static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
69static void frag6_deq(struct ip6asfrag *);
70static void frag6_insque(struct ip6q *, struct ip6q *);
71static void frag6_remque(struct ip6q *);
72static void frag6_freef(struct ip6q *);
73
74static struct mtx ip6qlock;
75/*
76 * These fields all protected by ip6qlock.
77 */
78static VNET_DEFINE(u_int, frag6_nfragpackets);
79static VNET_DEFINE(u_int, frag6_nfrags);
80static VNET_DEFINE(struct ip6q, ip6q); /* ip6 reassemble queue */
81
82#define V_frag6_nfragpackets VNET(frag6_nfragpackets)
83#define V_frag6_nfrags VNET(frag6_nfrags)
84#define V_ip6q VNET(ip6q)
85
86#define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
87#define IP6Q_LOCK() mtx_lock(&ip6qlock)
88#define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock)
89#define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED)
90#define IP6Q_UNLOCK() mtx_unlock(&ip6qlock)
91
92static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
93
94/*
95 * Initialise reassembly queue and fragment identifier.
96 */
97static void
98frag6_change(void *tag)
99{
100
101 V_ip6_maxfragpackets = nmbclusters / 4;
102 V_ip6_maxfrags = nmbclusters / 4;
103}
104
105void
106frag6_init(void)
107{
108
109 V_ip6q.ip6q_next = V_ip6q.ip6q_prev = &V_ip6q;
110 V_ip6_maxfragpackets = nmbclusters / 4;
111 V_ip6_maxfrags = nmbclusters / 4;
109 V_ip6_maxfragpackets = nmbclusters / 4;
110 V_ip6_maxfrags = nmbclusters / 4;
111 V_ip6q.ip6q_next = V_ip6q.ip6q_prev = &V_ip6q;
112
113 if (!IS_DEFAULT_VNET(curvnet))
114 return;
115
112
113 if (!IS_DEFAULT_VNET(curvnet))
114 return;
115
116 IP6Q_LOCK_INIT();
117 EVENTHANDLER_REGISTER(nmbclusters_change,
118 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
116 EVENTHANDLER_REGISTER(nmbclusters_change,
117 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
118
119 IP6Q_LOCK_INIT();
119}
120
121/*
122 * In RFC2460, fragment and reassembly rule do not agree with each other,
123 * in terms of next header field handling in fragment header.
124 * While the sender will use the same value for all of the fragmented packets,
125 * receiver is suggested not to check the consistency.
126 *
127 * fragment rule (p20):
128 * (2) A Fragment header containing:
129 * The Next Header value that identifies the first header of
130 * the Fragmentable Part of the original packet.
131 * -> next header field is same for all fragments
132 *
133 * reassembly rule (p21):
134 * The Next Header field of the last header of the Unfragmentable
135 * Part is obtained from the Next Header field of the first
136 * fragment's Fragment header.
137 * -> should grab it from the first fragment only
138 *
139 * The following note also contradicts with fragment rule - noone is going to
140 * send different fragment with different next header field.
141 *
142 * additional note (p22):
143 * The Next Header values in the Fragment headers of different
144 * fragments of the same original packet may differ. Only the value
145 * from the Offset zero fragment packet is used for reassembly.
146 * -> should grab it from the first fragment only
147 *
148 * There is no explicit reason given in the RFC. Historical reason maybe?
149 */
150/*
151 * Fragment input
152 */
153int
154frag6_input(struct mbuf **mp, int *offp, int proto)
155{
156 struct mbuf *m = *mp, *t;
157 struct ip6_hdr *ip6;
158 struct ip6_frag *ip6f;
159 struct ip6q *q6;
160 struct ip6asfrag *af6, *ip6af, *af6dwn;
161#ifdef IN6_IFSTAT_STRICT
162 struct in6_ifaddr *ia;
163#endif
164 int offset = *offp, nxt, i, next;
165 int first_frag = 0;
166 int fragoff, frgpartlen; /* must be larger than u_int16_t */
167 struct ifnet *dstifp;
168 u_int8_t ecn, ecn0;
169#if 0
170 char ip6buf[INET6_ADDRSTRLEN];
171#endif
172
173 ip6 = mtod(m, struct ip6_hdr *);
174#ifndef PULLDOWN_TEST
175 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
176 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
177#else
178 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
179 if (ip6f == NULL)
180 return (IPPROTO_DONE);
181#endif
182
183 dstifp = NULL;
184#ifdef IN6_IFSTAT_STRICT
185 /* find the destination interface of the packet. */
186 if ((ia = ip6_getdstifaddr(m)) != NULL) {
187 dstifp = ia->ia_ifp;
188 ifa_free(&ia->ia_ifa);
189 }
190#else
191 /* we are violating the spec, this is not the destination interface */
192 if ((m->m_flags & M_PKTHDR) != 0)
193 dstifp = m->m_pkthdr.rcvif;
194#endif
195
196 /* jumbo payload can't contain a fragment header */
197 if (ip6->ip6_plen == 0) {
198 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
199 in6_ifstat_inc(dstifp, ifs6_reass_fail);
200 return IPPROTO_DONE;
201 }
202
203 /*
204 * check whether fragment packet's fragment length is
205 * multiple of 8 octets.
206 * sizeof(struct ip6_frag) == 8
207 * sizeof(struct ip6_hdr) = 40
208 */
209 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
210 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
211 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
212 offsetof(struct ip6_hdr, ip6_plen));
213 in6_ifstat_inc(dstifp, ifs6_reass_fail);
214 return IPPROTO_DONE;
215 }
216
217 V_ip6stat.ip6s_fragments++;
218 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
219
220 /* offset now points to data portion */
221 offset += sizeof(struct ip6_frag);
222
223 IP6Q_LOCK();
224
225 /*
226 * Enforce upper bound on number of fragments.
227 * If maxfrag is 0, never accept fragments.
228 * If maxfrag is -1, accept all fragments without limitation.
229 */
230 if (V_ip6_maxfrags < 0)
231 ;
232 else if (V_frag6_nfrags >= (u_int)V_ip6_maxfrags)
233 goto dropfrag;
234
235 for (q6 = V_ip6q.ip6q_next; q6 != &V_ip6q; q6 = q6->ip6q_next)
236 if (ip6f->ip6f_ident == q6->ip6q_ident &&
237 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
238 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
239#ifdef MAC
240 && mac_ip6q_match(m, q6)
241#endif
242 )
243 break;
244
245 if (q6 == &V_ip6q) {
246 /*
247 * the first fragment to arrive, create a reassembly queue.
248 */
249 first_frag = 1;
250
251 /*
252 * Enforce upper bound on number of fragmented packets
253 * for which we attempt reassembly;
254 * If maxfragpackets is 0, never accept fragments.
255 * If maxfragpackets is -1, accept all fragments without
256 * limitation.
257 */
258 if (V_ip6_maxfragpackets < 0)
259 ;
260 else if (V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets)
261 goto dropfrag;
262 V_frag6_nfragpackets++;
263 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
264 M_NOWAIT);
265 if (q6 == NULL)
266 goto dropfrag;
267 bzero(q6, sizeof(*q6));
268#ifdef MAC
269 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
270 free(q6, M_FTABLE);
271 goto dropfrag;
272 }
273 mac_ip6q_create(m, q6);
274#endif
275 frag6_insque(q6, &V_ip6q);
276
277 /* ip6q_nxt will be filled afterwards, from 1st fragment */
278 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
279#ifdef notyet
280 q6->ip6q_nxtp = (u_char *)nxtp;
281#endif
282 q6->ip6q_ident = ip6f->ip6f_ident;
283 q6->ip6q_ttl = IPV6_FRAGTTL;
284 q6->ip6q_src = ip6->ip6_src;
285 q6->ip6q_dst = ip6->ip6_dst;
286 q6->ip6q_ecn =
287 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
288 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
289
290 q6->ip6q_nfrag = 0;
291 }
292
293 /*
294 * If it's the 1st fragment, record the length of the
295 * unfragmentable part and the next header of the fragment header.
296 */
297 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
298 if (fragoff == 0) {
299 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
300 sizeof(struct ip6_frag);
301 q6->ip6q_nxt = ip6f->ip6f_nxt;
302 }
303
304 /*
305 * Check that the reassembled packet would not exceed 65535 bytes
306 * in size.
307 * If it would exceed, discard the fragment and return an ICMP error.
308 */
309 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
310 if (q6->ip6q_unfrglen >= 0) {
311 /* The 1st fragment has already arrived. */
312 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
313 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
314 offset - sizeof(struct ip6_frag) +
315 offsetof(struct ip6_frag, ip6f_offlg));
316 IP6Q_UNLOCK();
317 return (IPPROTO_DONE);
318 }
319 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
320 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
321 offset - sizeof(struct ip6_frag) +
322 offsetof(struct ip6_frag, ip6f_offlg));
323 IP6Q_UNLOCK();
324 return (IPPROTO_DONE);
325 }
326 /*
327 * If it's the first fragment, do the above check for each
328 * fragment already stored in the reassembly queue.
329 */
330 if (fragoff == 0) {
331 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
332 af6 = af6dwn) {
333 af6dwn = af6->ip6af_down;
334
335 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
336 IPV6_MAXPACKET) {
337 struct mbuf *merr = IP6_REASS_MBUF(af6);
338 struct ip6_hdr *ip6err;
339 int erroff = af6->ip6af_offset;
340
341 /* dequeue the fragment. */
342 frag6_deq(af6);
343 free(af6, M_FTABLE);
344
345 /* adjust pointer. */
346 ip6err = mtod(merr, struct ip6_hdr *);
347
348 /*
349 * Restore source and destination addresses
350 * in the erroneous IPv6 header.
351 */
352 ip6err->ip6_src = q6->ip6q_src;
353 ip6err->ip6_dst = q6->ip6q_dst;
354
355 icmp6_error(merr, ICMP6_PARAM_PROB,
356 ICMP6_PARAMPROB_HEADER,
357 erroff - sizeof(struct ip6_frag) +
358 offsetof(struct ip6_frag, ip6f_offlg));
359 }
360 }
361 }
362
363 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
364 M_NOWAIT);
365 if (ip6af == NULL)
366 goto dropfrag;
367 bzero(ip6af, sizeof(*ip6af));
368 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
369 ip6af->ip6af_off = fragoff;
370 ip6af->ip6af_frglen = frgpartlen;
371 ip6af->ip6af_offset = offset;
372 IP6_REASS_MBUF(ip6af) = m;
373
374 if (first_frag) {
375 af6 = (struct ip6asfrag *)q6;
376 goto insert;
377 }
378
379 /*
380 * Handle ECN by comparing this segment with the first one;
381 * if CE is set, do not lose CE.
382 * drop if CE and not-ECT are mixed for the same packet.
383 */
384 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
385 ecn0 = q6->ip6q_ecn;
386 if (ecn == IPTOS_ECN_CE) {
387 if (ecn0 == IPTOS_ECN_NOTECT) {
388 free(ip6af, M_FTABLE);
389 goto dropfrag;
390 }
391 if (ecn0 != IPTOS_ECN_CE)
392 q6->ip6q_ecn = IPTOS_ECN_CE;
393 }
394 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
395 free(ip6af, M_FTABLE);
396 goto dropfrag;
397 }
398
399 /*
400 * Find a segment which begins after this one does.
401 */
402 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
403 af6 = af6->ip6af_down)
404 if (af6->ip6af_off > ip6af->ip6af_off)
405 break;
406
407#if 0
408 /*
409 * If there is a preceding segment, it may provide some of
410 * our data already. If so, drop the data from the incoming
411 * segment. If it provides all of our data, drop us.
412 */
413 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
414 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
415 - ip6af->ip6af_off;
416 if (i > 0) {
417 if (i >= ip6af->ip6af_frglen)
418 goto dropfrag;
419 m_adj(IP6_REASS_MBUF(ip6af), i);
420 ip6af->ip6af_off += i;
421 ip6af->ip6af_frglen -= i;
422 }
423 }
424
425 /*
426 * While we overlap succeeding segments trim them or,
427 * if they are completely covered, dequeue them.
428 */
429 while (af6 != (struct ip6asfrag *)q6 &&
430 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
431 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
432 if (i < af6->ip6af_frglen) {
433 af6->ip6af_frglen -= i;
434 af6->ip6af_off += i;
435 m_adj(IP6_REASS_MBUF(af6), i);
436 break;
437 }
438 af6 = af6->ip6af_down;
439 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
440 frag6_deq(af6->ip6af_up);
441 }
442#else
443 /*
444 * If the incoming framgent overlaps some existing fragments in
445 * the reassembly queue, drop it, since it is dangerous to override
446 * existing fragments from a security point of view.
447 * We don't know which fragment is the bad guy - here we trust
448 * fragment that came in earlier, with no real reason.
449 *
450 * Note: due to changes after disabling this part, mbuf passed to
451 * m_adj() below now does not meet the requirement.
452 */
453 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
454 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
455 - ip6af->ip6af_off;
456 if (i > 0) {
457#if 0 /* suppress the noisy log */
458 log(LOG_ERR, "%d bytes of a fragment from %s "
459 "overlaps the previous fragment\n",
460 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
461#endif
462 free(ip6af, M_FTABLE);
463 goto dropfrag;
464 }
465 }
466 if (af6 != (struct ip6asfrag *)q6) {
467 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
468 if (i > 0) {
469#if 0 /* suppress the noisy log */
470 log(LOG_ERR, "%d bytes of a fragment from %s "
471 "overlaps the succeeding fragment",
472 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
473#endif
474 free(ip6af, M_FTABLE);
475 goto dropfrag;
476 }
477 }
478#endif
479
480insert:
481#ifdef MAC
482 if (!first_frag)
483 mac_ip6q_update(m, q6);
484#endif
485
486 /*
487 * Stick new segment in its place;
488 * check for complete reassembly.
489 * Move to front of packet queue, as we are
490 * the most recently active fragmented packet.
491 */
492 frag6_enq(ip6af, af6->ip6af_up);
493 V_frag6_nfrags++;
494 q6->ip6q_nfrag++;
495#if 0 /* xxx */
496 if (q6 != V_ip6q.ip6q_next) {
497 frag6_remque(q6);
498 frag6_insque(q6, &V_ip6q);
499 }
500#endif
501 next = 0;
502 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
503 af6 = af6->ip6af_down) {
504 if (af6->ip6af_off != next) {
505 IP6Q_UNLOCK();
506 return IPPROTO_DONE;
507 }
508 next += af6->ip6af_frglen;
509 }
510 if (af6->ip6af_up->ip6af_mff) {
511 IP6Q_UNLOCK();
512 return IPPROTO_DONE;
513 }
514
515 /*
516 * Reassembly is complete; concatenate fragments.
517 */
518 ip6af = q6->ip6q_down;
519 t = m = IP6_REASS_MBUF(ip6af);
520 af6 = ip6af->ip6af_down;
521 frag6_deq(ip6af);
522 while (af6 != (struct ip6asfrag *)q6) {
523 af6dwn = af6->ip6af_down;
524 frag6_deq(af6);
525 while (t->m_next)
526 t = t->m_next;
527 t->m_next = IP6_REASS_MBUF(af6);
528 m_adj(t->m_next, af6->ip6af_offset);
529 free(af6, M_FTABLE);
530 af6 = af6dwn;
531 }
532
533 /* adjust offset to point where the original next header starts */
534 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
535 free(ip6af, M_FTABLE);
536 ip6 = mtod(m, struct ip6_hdr *);
537 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
538 if (q6->ip6q_ecn == IPTOS_ECN_CE)
539 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
540 nxt = q6->ip6q_nxt;
541#ifdef notyet
542 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
543#endif
544
545 /* Delete frag6 header */
546 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
547 /* This is the only possible case with !PULLDOWN_TEST */
548 ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
549 offset);
550 m->m_data += sizeof(struct ip6_frag);
551 m->m_len -= sizeof(struct ip6_frag);
552 } else {
553 /* this comes with no copy if the boundary is on cluster */
554 if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
555 frag6_remque(q6);
556 V_frag6_nfrags -= q6->ip6q_nfrag;
557#ifdef MAC
558 mac_ip6q_destroy(q6);
559#endif
560 free(q6, M_FTABLE);
561 V_frag6_nfragpackets--;
562 goto dropfrag;
563 }
564 m_adj(t, sizeof(struct ip6_frag));
565 m_cat(m, t);
566 }
567
568 /*
569 * Store NXT to the original.
570 */
571 {
572 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
573 *prvnxtp = nxt;
574 }
575
576 frag6_remque(q6);
577 V_frag6_nfrags -= q6->ip6q_nfrag;
578#ifdef MAC
579 mac_ip6q_reassemble(q6, m);
580 mac_ip6q_destroy(q6);
581#endif
582 free(q6, M_FTABLE);
583 V_frag6_nfragpackets--;
584
585 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
586 int plen = 0;
587 for (t = m; t; t = t->m_next)
588 plen += t->m_len;
589 m->m_pkthdr.len = plen;
590 }
591
592 V_ip6stat.ip6s_reassembled++;
593 in6_ifstat_inc(dstifp, ifs6_reass_ok);
594
595 /*
596 * Tell launch routine the next header
597 */
598
599 *mp = m;
600 *offp = offset;
601
602 IP6Q_UNLOCK();
603 return nxt;
604
605 dropfrag:
606 IP6Q_UNLOCK();
607 in6_ifstat_inc(dstifp, ifs6_reass_fail);
608 V_ip6stat.ip6s_fragdropped++;
609 m_freem(m);
610 return IPPROTO_DONE;
611}
612
613/*
614 * Free a fragment reassembly header and all
615 * associated datagrams.
616 */
617void
618frag6_freef(struct ip6q *q6)
619{
620 struct ip6asfrag *af6, *down6;
621
622 IP6Q_LOCK_ASSERT();
623
624 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
625 af6 = down6) {
626 struct mbuf *m = IP6_REASS_MBUF(af6);
627
628 down6 = af6->ip6af_down;
629 frag6_deq(af6);
630
631 /*
632 * Return ICMP time exceeded error for the 1st fragment.
633 * Just free other fragments.
634 */
635 if (af6->ip6af_off == 0) {
636 struct ip6_hdr *ip6;
637
638 /* adjust pointer */
639 ip6 = mtod(m, struct ip6_hdr *);
640
641 /* restore source and destination addresses */
642 ip6->ip6_src = q6->ip6q_src;
643 ip6->ip6_dst = q6->ip6q_dst;
644
645 icmp6_error(m, ICMP6_TIME_EXCEEDED,
646 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
647 } else
648 m_freem(m);
649 free(af6, M_FTABLE);
650 }
651 frag6_remque(q6);
652 V_frag6_nfrags -= q6->ip6q_nfrag;
653#ifdef MAC
654 mac_ip6q_destroy(q6);
655#endif
656 free(q6, M_FTABLE);
657 V_frag6_nfragpackets--;
658}
659
660/*
661 * Put an ip fragment on a reassembly chain.
662 * Like insque, but pointers in middle of structure.
663 */
664void
665frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
666{
667
668 IP6Q_LOCK_ASSERT();
669
670 af6->ip6af_up = up6;
671 af6->ip6af_down = up6->ip6af_down;
672 up6->ip6af_down->ip6af_up = af6;
673 up6->ip6af_down = af6;
674}
675
676/*
677 * To frag6_enq as remque is to insque.
678 */
679void
680frag6_deq(struct ip6asfrag *af6)
681{
682
683 IP6Q_LOCK_ASSERT();
684
685 af6->ip6af_up->ip6af_down = af6->ip6af_down;
686 af6->ip6af_down->ip6af_up = af6->ip6af_up;
687}
688
689void
690frag6_insque(struct ip6q *new, struct ip6q *old)
691{
692
693 IP6Q_LOCK_ASSERT();
694
695 new->ip6q_prev = old;
696 new->ip6q_next = old->ip6q_next;
697 old->ip6q_next->ip6q_prev= new;
698 old->ip6q_next = new;
699}
700
701void
702frag6_remque(struct ip6q *p6)
703{
704
705 IP6Q_LOCK_ASSERT();
706
707 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
708 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
709}
710
711/*
712 * IPv6 reassembling timer processing;
713 * if a timer expires on a reassembly
714 * queue, discard it.
715 */
716void
717frag6_slowtimo(void)
718{
719 VNET_ITERATOR_DECL(vnet_iter);
720 struct ip6q *q6;
721
722 VNET_LIST_RLOCK_NOSLEEP();
723 IP6Q_LOCK();
724 VNET_FOREACH(vnet_iter) {
725 CURVNET_SET(vnet_iter);
726 q6 = V_ip6q.ip6q_next;
727 if (q6)
728 while (q6 != &V_ip6q) {
729 --q6->ip6q_ttl;
730 q6 = q6->ip6q_next;
731 if (q6->ip6q_prev->ip6q_ttl == 0) {
732 V_ip6stat.ip6s_fragtimeout++;
733 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
734 frag6_freef(q6->ip6q_prev);
735 }
736 }
737 /*
738 * If we are over the maximum number of fragments
739 * (due to the limit being lowered), drain off
740 * enough to get down to the new limit.
741 */
742 while (V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets &&
743 V_ip6q.ip6q_prev) {
744 V_ip6stat.ip6s_fragoverflow++;
745 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
746 frag6_freef(V_ip6q.ip6q_prev);
747 }
748 CURVNET_RESTORE();
749 }
750 IP6Q_UNLOCK();
751 VNET_LIST_RUNLOCK_NOSLEEP();
752}
753
754/*
755 * Drain off all datagram fragments.
756 */
757void
758frag6_drain(void)
759{
760 VNET_ITERATOR_DECL(vnet_iter);
761
762 VNET_LIST_RLOCK_NOSLEEP();
763 if (IP6Q_TRYLOCK() == 0) {
764 VNET_LIST_RUNLOCK_NOSLEEP();
765 return;
766 }
767 VNET_FOREACH(vnet_iter) {
768 CURVNET_SET(vnet_iter);
769 while (V_ip6q.ip6q_next != &V_ip6q) {
770 V_ip6stat.ip6s_fragdropped++;
771 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
772 frag6_freef(V_ip6q.ip6q_next);
773 }
774 CURVNET_RESTORE();
775 }
776 IP6Q_UNLOCK();
777 VNET_LIST_RUNLOCK_NOSLEEP();
778}
120}
121
122/*
123 * In RFC2460, fragment and reassembly rule do not agree with each other,
124 * in terms of next header field handling in fragment header.
125 * While the sender will use the same value for all of the fragmented packets,
126 * receiver is suggested not to check the consistency.
127 *
128 * fragment rule (p20):
129 * (2) A Fragment header containing:
130 * The Next Header value that identifies the first header of
131 * the Fragmentable Part of the original packet.
132 * -> next header field is same for all fragments
133 *
134 * reassembly rule (p21):
135 * The Next Header field of the last header of the Unfragmentable
136 * Part is obtained from the Next Header field of the first
137 * fragment's Fragment header.
138 * -> should grab it from the first fragment only
139 *
140 * The following note also contradicts with fragment rule - noone is going to
141 * send different fragment with different next header field.
142 *
143 * additional note (p22):
144 * The Next Header values in the Fragment headers of different
145 * fragments of the same original packet may differ. Only the value
146 * from the Offset zero fragment packet is used for reassembly.
147 * -> should grab it from the first fragment only
148 *
149 * There is no explicit reason given in the RFC. Historical reason maybe?
150 */
151/*
152 * Fragment input
153 */
154int
155frag6_input(struct mbuf **mp, int *offp, int proto)
156{
157 struct mbuf *m = *mp, *t;
158 struct ip6_hdr *ip6;
159 struct ip6_frag *ip6f;
160 struct ip6q *q6;
161 struct ip6asfrag *af6, *ip6af, *af6dwn;
162#ifdef IN6_IFSTAT_STRICT
163 struct in6_ifaddr *ia;
164#endif
165 int offset = *offp, nxt, i, next;
166 int first_frag = 0;
167 int fragoff, frgpartlen; /* must be larger than u_int16_t */
168 struct ifnet *dstifp;
169 u_int8_t ecn, ecn0;
170#if 0
171 char ip6buf[INET6_ADDRSTRLEN];
172#endif
173
174 ip6 = mtod(m, struct ip6_hdr *);
175#ifndef PULLDOWN_TEST
176 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
177 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
178#else
179 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
180 if (ip6f == NULL)
181 return (IPPROTO_DONE);
182#endif
183
184 dstifp = NULL;
185#ifdef IN6_IFSTAT_STRICT
186 /* find the destination interface of the packet. */
187 if ((ia = ip6_getdstifaddr(m)) != NULL) {
188 dstifp = ia->ia_ifp;
189 ifa_free(&ia->ia_ifa);
190 }
191#else
192 /* we are violating the spec, this is not the destination interface */
193 if ((m->m_flags & M_PKTHDR) != 0)
194 dstifp = m->m_pkthdr.rcvif;
195#endif
196
197 /* jumbo payload can't contain a fragment header */
198 if (ip6->ip6_plen == 0) {
199 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
200 in6_ifstat_inc(dstifp, ifs6_reass_fail);
201 return IPPROTO_DONE;
202 }
203
204 /*
205 * check whether fragment packet's fragment length is
206 * multiple of 8 octets.
207 * sizeof(struct ip6_frag) == 8
208 * sizeof(struct ip6_hdr) = 40
209 */
210 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
211 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
212 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
213 offsetof(struct ip6_hdr, ip6_plen));
214 in6_ifstat_inc(dstifp, ifs6_reass_fail);
215 return IPPROTO_DONE;
216 }
217
218 V_ip6stat.ip6s_fragments++;
219 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
220
221 /* offset now points to data portion */
222 offset += sizeof(struct ip6_frag);
223
224 IP6Q_LOCK();
225
226 /*
227 * Enforce upper bound on number of fragments.
228 * If maxfrag is 0, never accept fragments.
229 * If maxfrag is -1, accept all fragments without limitation.
230 */
231 if (V_ip6_maxfrags < 0)
232 ;
233 else if (V_frag6_nfrags >= (u_int)V_ip6_maxfrags)
234 goto dropfrag;
235
236 for (q6 = V_ip6q.ip6q_next; q6 != &V_ip6q; q6 = q6->ip6q_next)
237 if (ip6f->ip6f_ident == q6->ip6q_ident &&
238 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
239 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
240#ifdef MAC
241 && mac_ip6q_match(m, q6)
242#endif
243 )
244 break;
245
246 if (q6 == &V_ip6q) {
247 /*
248 * the first fragment to arrive, create a reassembly queue.
249 */
250 first_frag = 1;
251
252 /*
253 * Enforce upper bound on number of fragmented packets
254 * for which we attempt reassembly;
255 * If maxfragpackets is 0, never accept fragments.
256 * If maxfragpackets is -1, accept all fragments without
257 * limitation.
258 */
259 if (V_ip6_maxfragpackets < 0)
260 ;
261 else if (V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets)
262 goto dropfrag;
263 V_frag6_nfragpackets++;
264 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
265 M_NOWAIT);
266 if (q6 == NULL)
267 goto dropfrag;
268 bzero(q6, sizeof(*q6));
269#ifdef MAC
270 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
271 free(q6, M_FTABLE);
272 goto dropfrag;
273 }
274 mac_ip6q_create(m, q6);
275#endif
276 frag6_insque(q6, &V_ip6q);
277
278 /* ip6q_nxt will be filled afterwards, from 1st fragment */
279 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
280#ifdef notyet
281 q6->ip6q_nxtp = (u_char *)nxtp;
282#endif
283 q6->ip6q_ident = ip6f->ip6f_ident;
284 q6->ip6q_ttl = IPV6_FRAGTTL;
285 q6->ip6q_src = ip6->ip6_src;
286 q6->ip6q_dst = ip6->ip6_dst;
287 q6->ip6q_ecn =
288 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
289 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
290
291 q6->ip6q_nfrag = 0;
292 }
293
294 /*
295 * If it's the 1st fragment, record the length of the
296 * unfragmentable part and the next header of the fragment header.
297 */
298 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
299 if (fragoff == 0) {
300 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
301 sizeof(struct ip6_frag);
302 q6->ip6q_nxt = ip6f->ip6f_nxt;
303 }
304
305 /*
306 * Check that the reassembled packet would not exceed 65535 bytes
307 * in size.
308 * If it would exceed, discard the fragment and return an ICMP error.
309 */
310 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
311 if (q6->ip6q_unfrglen >= 0) {
312 /* The 1st fragment has already arrived. */
313 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
314 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
315 offset - sizeof(struct ip6_frag) +
316 offsetof(struct ip6_frag, ip6f_offlg));
317 IP6Q_UNLOCK();
318 return (IPPROTO_DONE);
319 }
320 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
321 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
322 offset - sizeof(struct ip6_frag) +
323 offsetof(struct ip6_frag, ip6f_offlg));
324 IP6Q_UNLOCK();
325 return (IPPROTO_DONE);
326 }
327 /*
328 * If it's the first fragment, do the above check for each
329 * fragment already stored in the reassembly queue.
330 */
331 if (fragoff == 0) {
332 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
333 af6 = af6dwn) {
334 af6dwn = af6->ip6af_down;
335
336 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
337 IPV6_MAXPACKET) {
338 struct mbuf *merr = IP6_REASS_MBUF(af6);
339 struct ip6_hdr *ip6err;
340 int erroff = af6->ip6af_offset;
341
342 /* dequeue the fragment. */
343 frag6_deq(af6);
344 free(af6, M_FTABLE);
345
346 /* adjust pointer. */
347 ip6err = mtod(merr, struct ip6_hdr *);
348
349 /*
350 * Restore source and destination addresses
351 * in the erroneous IPv6 header.
352 */
353 ip6err->ip6_src = q6->ip6q_src;
354 ip6err->ip6_dst = q6->ip6q_dst;
355
356 icmp6_error(merr, ICMP6_PARAM_PROB,
357 ICMP6_PARAMPROB_HEADER,
358 erroff - sizeof(struct ip6_frag) +
359 offsetof(struct ip6_frag, ip6f_offlg));
360 }
361 }
362 }
363
364 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
365 M_NOWAIT);
366 if (ip6af == NULL)
367 goto dropfrag;
368 bzero(ip6af, sizeof(*ip6af));
369 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
370 ip6af->ip6af_off = fragoff;
371 ip6af->ip6af_frglen = frgpartlen;
372 ip6af->ip6af_offset = offset;
373 IP6_REASS_MBUF(ip6af) = m;
374
375 if (first_frag) {
376 af6 = (struct ip6asfrag *)q6;
377 goto insert;
378 }
379
380 /*
381 * Handle ECN by comparing this segment with the first one;
382 * if CE is set, do not lose CE.
383 * drop if CE and not-ECT are mixed for the same packet.
384 */
385 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
386 ecn0 = q6->ip6q_ecn;
387 if (ecn == IPTOS_ECN_CE) {
388 if (ecn0 == IPTOS_ECN_NOTECT) {
389 free(ip6af, M_FTABLE);
390 goto dropfrag;
391 }
392 if (ecn0 != IPTOS_ECN_CE)
393 q6->ip6q_ecn = IPTOS_ECN_CE;
394 }
395 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
396 free(ip6af, M_FTABLE);
397 goto dropfrag;
398 }
399
400 /*
401 * Find a segment which begins after this one does.
402 */
403 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
404 af6 = af6->ip6af_down)
405 if (af6->ip6af_off > ip6af->ip6af_off)
406 break;
407
408#if 0
409 /*
410 * If there is a preceding segment, it may provide some of
411 * our data already. If so, drop the data from the incoming
412 * segment. If it provides all of our data, drop us.
413 */
414 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
415 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
416 - ip6af->ip6af_off;
417 if (i > 0) {
418 if (i >= ip6af->ip6af_frglen)
419 goto dropfrag;
420 m_adj(IP6_REASS_MBUF(ip6af), i);
421 ip6af->ip6af_off += i;
422 ip6af->ip6af_frglen -= i;
423 }
424 }
425
426 /*
427 * While we overlap succeeding segments trim them or,
428 * if they are completely covered, dequeue them.
429 */
430 while (af6 != (struct ip6asfrag *)q6 &&
431 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
432 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
433 if (i < af6->ip6af_frglen) {
434 af6->ip6af_frglen -= i;
435 af6->ip6af_off += i;
436 m_adj(IP6_REASS_MBUF(af6), i);
437 break;
438 }
439 af6 = af6->ip6af_down;
440 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
441 frag6_deq(af6->ip6af_up);
442 }
443#else
444 /*
445 * If the incoming framgent overlaps some existing fragments in
446 * the reassembly queue, drop it, since it is dangerous to override
447 * existing fragments from a security point of view.
448 * We don't know which fragment is the bad guy - here we trust
449 * fragment that came in earlier, with no real reason.
450 *
451 * Note: due to changes after disabling this part, mbuf passed to
452 * m_adj() below now does not meet the requirement.
453 */
454 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
455 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
456 - ip6af->ip6af_off;
457 if (i > 0) {
458#if 0 /* suppress the noisy log */
459 log(LOG_ERR, "%d bytes of a fragment from %s "
460 "overlaps the previous fragment\n",
461 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
462#endif
463 free(ip6af, M_FTABLE);
464 goto dropfrag;
465 }
466 }
467 if (af6 != (struct ip6asfrag *)q6) {
468 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
469 if (i > 0) {
470#if 0 /* suppress the noisy log */
471 log(LOG_ERR, "%d bytes of a fragment from %s "
472 "overlaps the succeeding fragment",
473 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
474#endif
475 free(ip6af, M_FTABLE);
476 goto dropfrag;
477 }
478 }
479#endif
480
481insert:
482#ifdef MAC
483 if (!first_frag)
484 mac_ip6q_update(m, q6);
485#endif
486
487 /*
488 * Stick new segment in its place;
489 * check for complete reassembly.
490 * Move to front of packet queue, as we are
491 * the most recently active fragmented packet.
492 */
493 frag6_enq(ip6af, af6->ip6af_up);
494 V_frag6_nfrags++;
495 q6->ip6q_nfrag++;
496#if 0 /* xxx */
497 if (q6 != V_ip6q.ip6q_next) {
498 frag6_remque(q6);
499 frag6_insque(q6, &V_ip6q);
500 }
501#endif
502 next = 0;
503 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
504 af6 = af6->ip6af_down) {
505 if (af6->ip6af_off != next) {
506 IP6Q_UNLOCK();
507 return IPPROTO_DONE;
508 }
509 next += af6->ip6af_frglen;
510 }
511 if (af6->ip6af_up->ip6af_mff) {
512 IP6Q_UNLOCK();
513 return IPPROTO_DONE;
514 }
515
516 /*
517 * Reassembly is complete; concatenate fragments.
518 */
519 ip6af = q6->ip6q_down;
520 t = m = IP6_REASS_MBUF(ip6af);
521 af6 = ip6af->ip6af_down;
522 frag6_deq(ip6af);
523 while (af6 != (struct ip6asfrag *)q6) {
524 af6dwn = af6->ip6af_down;
525 frag6_deq(af6);
526 while (t->m_next)
527 t = t->m_next;
528 t->m_next = IP6_REASS_MBUF(af6);
529 m_adj(t->m_next, af6->ip6af_offset);
530 free(af6, M_FTABLE);
531 af6 = af6dwn;
532 }
533
534 /* adjust offset to point where the original next header starts */
535 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
536 free(ip6af, M_FTABLE);
537 ip6 = mtod(m, struct ip6_hdr *);
538 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
539 if (q6->ip6q_ecn == IPTOS_ECN_CE)
540 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
541 nxt = q6->ip6q_nxt;
542#ifdef notyet
543 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
544#endif
545
546 /* Delete frag6 header */
547 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
548 /* This is the only possible case with !PULLDOWN_TEST */
549 ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
550 offset);
551 m->m_data += sizeof(struct ip6_frag);
552 m->m_len -= sizeof(struct ip6_frag);
553 } else {
554 /* this comes with no copy if the boundary is on cluster */
555 if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
556 frag6_remque(q6);
557 V_frag6_nfrags -= q6->ip6q_nfrag;
558#ifdef MAC
559 mac_ip6q_destroy(q6);
560#endif
561 free(q6, M_FTABLE);
562 V_frag6_nfragpackets--;
563 goto dropfrag;
564 }
565 m_adj(t, sizeof(struct ip6_frag));
566 m_cat(m, t);
567 }
568
569 /*
570 * Store NXT to the original.
571 */
572 {
573 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
574 *prvnxtp = nxt;
575 }
576
577 frag6_remque(q6);
578 V_frag6_nfrags -= q6->ip6q_nfrag;
579#ifdef MAC
580 mac_ip6q_reassemble(q6, m);
581 mac_ip6q_destroy(q6);
582#endif
583 free(q6, M_FTABLE);
584 V_frag6_nfragpackets--;
585
586 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
587 int plen = 0;
588 for (t = m; t; t = t->m_next)
589 plen += t->m_len;
590 m->m_pkthdr.len = plen;
591 }
592
593 V_ip6stat.ip6s_reassembled++;
594 in6_ifstat_inc(dstifp, ifs6_reass_ok);
595
596 /*
597 * Tell launch routine the next header
598 */
599
600 *mp = m;
601 *offp = offset;
602
603 IP6Q_UNLOCK();
604 return nxt;
605
606 dropfrag:
607 IP6Q_UNLOCK();
608 in6_ifstat_inc(dstifp, ifs6_reass_fail);
609 V_ip6stat.ip6s_fragdropped++;
610 m_freem(m);
611 return IPPROTO_DONE;
612}
613
614/*
615 * Free a fragment reassembly header and all
616 * associated datagrams.
617 */
618void
619frag6_freef(struct ip6q *q6)
620{
621 struct ip6asfrag *af6, *down6;
622
623 IP6Q_LOCK_ASSERT();
624
625 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
626 af6 = down6) {
627 struct mbuf *m = IP6_REASS_MBUF(af6);
628
629 down6 = af6->ip6af_down;
630 frag6_deq(af6);
631
632 /*
633 * Return ICMP time exceeded error for the 1st fragment.
634 * Just free other fragments.
635 */
636 if (af6->ip6af_off == 0) {
637 struct ip6_hdr *ip6;
638
639 /* adjust pointer */
640 ip6 = mtod(m, struct ip6_hdr *);
641
642 /* restore source and destination addresses */
643 ip6->ip6_src = q6->ip6q_src;
644 ip6->ip6_dst = q6->ip6q_dst;
645
646 icmp6_error(m, ICMP6_TIME_EXCEEDED,
647 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
648 } else
649 m_freem(m);
650 free(af6, M_FTABLE);
651 }
652 frag6_remque(q6);
653 V_frag6_nfrags -= q6->ip6q_nfrag;
654#ifdef MAC
655 mac_ip6q_destroy(q6);
656#endif
657 free(q6, M_FTABLE);
658 V_frag6_nfragpackets--;
659}
660
661/*
662 * Put an ip fragment on a reassembly chain.
663 * Like insque, but pointers in middle of structure.
664 */
665void
666frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
667{
668
669 IP6Q_LOCK_ASSERT();
670
671 af6->ip6af_up = up6;
672 af6->ip6af_down = up6->ip6af_down;
673 up6->ip6af_down->ip6af_up = af6;
674 up6->ip6af_down = af6;
675}
676
677/*
678 * To frag6_enq as remque is to insque.
679 */
680void
681frag6_deq(struct ip6asfrag *af6)
682{
683
684 IP6Q_LOCK_ASSERT();
685
686 af6->ip6af_up->ip6af_down = af6->ip6af_down;
687 af6->ip6af_down->ip6af_up = af6->ip6af_up;
688}
689
690void
691frag6_insque(struct ip6q *new, struct ip6q *old)
692{
693
694 IP6Q_LOCK_ASSERT();
695
696 new->ip6q_prev = old;
697 new->ip6q_next = old->ip6q_next;
698 old->ip6q_next->ip6q_prev= new;
699 old->ip6q_next = new;
700}
701
702void
703frag6_remque(struct ip6q *p6)
704{
705
706 IP6Q_LOCK_ASSERT();
707
708 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
709 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
710}
711
712/*
713 * IPv6 reassembling timer processing;
714 * if a timer expires on a reassembly
715 * queue, discard it.
716 */
717void
718frag6_slowtimo(void)
719{
720 VNET_ITERATOR_DECL(vnet_iter);
721 struct ip6q *q6;
722
723 VNET_LIST_RLOCK_NOSLEEP();
724 IP6Q_LOCK();
725 VNET_FOREACH(vnet_iter) {
726 CURVNET_SET(vnet_iter);
727 q6 = V_ip6q.ip6q_next;
728 if (q6)
729 while (q6 != &V_ip6q) {
730 --q6->ip6q_ttl;
731 q6 = q6->ip6q_next;
732 if (q6->ip6q_prev->ip6q_ttl == 0) {
733 V_ip6stat.ip6s_fragtimeout++;
734 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
735 frag6_freef(q6->ip6q_prev);
736 }
737 }
738 /*
739 * If we are over the maximum number of fragments
740 * (due to the limit being lowered), drain off
741 * enough to get down to the new limit.
742 */
743 while (V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets &&
744 V_ip6q.ip6q_prev) {
745 V_ip6stat.ip6s_fragoverflow++;
746 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
747 frag6_freef(V_ip6q.ip6q_prev);
748 }
749 CURVNET_RESTORE();
750 }
751 IP6Q_UNLOCK();
752 VNET_LIST_RUNLOCK_NOSLEEP();
753}
754
755/*
756 * Drain off all datagram fragments.
757 */
758void
759frag6_drain(void)
760{
761 VNET_ITERATOR_DECL(vnet_iter);
762
763 VNET_LIST_RLOCK_NOSLEEP();
764 if (IP6Q_TRYLOCK() == 0) {
765 VNET_LIST_RUNLOCK_NOSLEEP();
766 return;
767 }
768 VNET_FOREACH(vnet_iter) {
769 CURVNET_SET(vnet_iter);
770 while (V_ip6q.ip6q_next != &V_ip6q) {
771 V_ip6stat.ip6s_fragdropped++;
772 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
773 frag6_freef(V_ip6q.ip6q_next);
774 }
775 CURVNET_RESTORE();
776 }
777 IP6Q_UNLOCK();
778 VNET_LIST_RUNLOCK_NOSLEEP();
779}