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