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