xref: /freebsd/sys/netinet6/frag6.c (revision 33644623554bb0fc57ed3c7d874193a498679b22)
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 "opt_mac.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46 #include <sys/kernel.h>
47 #include <sys/syslog.h>
48 #include <sys/vimage.h>
49 
50 #include <net/if.h>
51 #include <net/route.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/ip6_var.h>
57 #include <netinet/icmp6.h>
58 #include <netinet/in_systm.h>	/* for ECN definitions */
59 #include <netinet/ip.h>		/* for ECN definitions */
60 
61 #include <security/mac/mac_framework.h>
62 
63 /*
64  * Define it to get a correct behavior on per-interface statistics.
65  * You will need to perform an extra routing table lookup, per fragment,
66  * to do it.  This may, or may not be, a performance hit.
67  */
68 #define IN6_IFSTAT_STRICT
69 
70 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
71 static void frag6_deq(struct ip6asfrag *);
72 static void frag6_insque(struct ip6q *, struct ip6q *);
73 static void frag6_remque(struct ip6q *);
74 static void frag6_freef(struct ip6q *);
75 
76 static struct mtx ip6qlock;
77 /*
78  * These fields all protected by ip6qlock.
79  */
80 #ifdef VIMAGE_GLOBALS
81 static u_int frag6_nfragpackets;
82 static u_int frag6_nfrags;
83 static struct	ip6q ip6q;	/* ip6 reassemble queue */
84 #endif
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 	INIT_VNET_INET6(curvnet);
101 
102 	V_ip6_maxfragpackets = nmbclusters / 4;
103 	V_ip6_maxfrags = nmbclusters / 4;
104 }
105 
106 void
107 frag6_init(void)
108 {
109 	INIT_VNET_INET6(curvnet);
110 
111 	V_ip6_maxfragpackets = nmbclusters / 4;
112 	V_ip6_maxfrags = nmbclusters / 4;
113 	EVENTHANDLER_REGISTER(nmbclusters_change,
114 	    frag6_change, NULL, EVENTHANDLER_PRI_ANY);
115 
116 	IP6Q_LOCK_INIT();
117 
118 	V_ip6q.ip6q_next = V_ip6q.ip6q_prev = &V_ip6q;
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  */
153 int
154 frag6_input(struct mbuf **mp, int *offp, int proto)
155 {
156 	INIT_VNET_INET6(curvnet);
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 #else
190 	/* we are violating the spec, this is not the destination interface */
191 	if ((m->m_flags & M_PKTHDR) != 0)
192 		dstifp = m->m_pkthdr.rcvif;
193 #endif
194 
195 	/* jumbo payload can't contain a fragment header */
196 	if (ip6->ip6_plen == 0) {
197 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
198 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
199 		return IPPROTO_DONE;
200 	}
201 
202 	/*
203 	 * check whether fragment packet's fragment length is
204 	 * multiple of 8 octets.
205 	 * sizeof(struct ip6_frag) == 8
206 	 * sizeof(struct ip6_hdr) = 40
207 	 */
208 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
209 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
210 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
211 		    offsetof(struct ip6_hdr, ip6_plen));
212 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
213 		return IPPROTO_DONE;
214 	}
215 
216 	V_ip6stat.ip6s_fragments++;
217 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
218 
219 	/* offset now points to data portion */
220 	offset += sizeof(struct ip6_frag);
221 
222 	IP6Q_LOCK();
223 
224 	/*
225 	 * Enforce upper bound on number of fragments.
226 	 * If maxfrag is 0, never accept fragments.
227 	 * If maxfrag is -1, accept all fragments without limitation.
228 	 */
229 	if (V_ip6_maxfrags < 0)
230 		;
231 	else if (V_frag6_nfrags >= (u_int)V_ip6_maxfrags)
232 		goto dropfrag;
233 
234 	for (q6 = V_ip6q.ip6q_next; q6 != &V_ip6q; q6 = q6->ip6q_next)
235 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
236 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
237 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
238 #ifdef MAC
239 		    && mac_ip6q_match(m, q6)
240 #endif
241 		    )
242 			break;
243 
244 	if (q6 == &V_ip6q) {
245 		/*
246 		 * the first fragment to arrive, create a reassembly queue.
247 		 */
248 		first_frag = 1;
249 
250 		/*
251 		 * Enforce upper bound on number of fragmented packets
252 		 * for which we attempt reassembly;
253 		 * If maxfragpackets is 0, never accept fragments.
254 		 * If maxfragpackets is -1, accept all fragments without
255 		 * limitation.
256 		 */
257 		if (V_ip6_maxfragpackets < 0)
258 			;
259 		else if (V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets)
260 			goto dropfrag;
261 		V_frag6_nfragpackets++;
262 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
263 		    M_NOWAIT);
264 		if (q6 == NULL)
265 			goto dropfrag;
266 		bzero(q6, sizeof(*q6));
267 #ifdef MAC
268 		if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
269 			free(q6, M_FTABLE);
270 			goto dropfrag;
271 		}
272 		mac_ip6q_create(m, q6);
273 #endif
274 		frag6_insque(q6, &V_ip6q);
275 
276 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
277 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
278 #ifdef notyet
279 		q6->ip6q_nxtp	= (u_char *)nxtp;
280 #endif
281 		q6->ip6q_ident	= ip6f->ip6f_ident;
282 		q6->ip6q_ttl	= IPV6_FRAGTTL;
283 		q6->ip6q_src	= ip6->ip6_src;
284 		q6->ip6q_dst	= ip6->ip6_dst;
285 		q6->ip6q_ecn	=
286 		    (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
287 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
288 
289 		q6->ip6q_nfrag = 0;
290 	}
291 
292 	/*
293 	 * If it's the 1st fragment, record the length of the
294 	 * unfragmentable part and the next header of the fragment header.
295 	 */
296 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
297 	if (fragoff == 0) {
298 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
299 		    sizeof(struct ip6_frag);
300 		q6->ip6q_nxt = ip6f->ip6f_nxt;
301 	}
302 
303 	/*
304 	 * Check that the reassembled packet would not exceed 65535 bytes
305 	 * in size.
306 	 * If it would exceed, discard the fragment and return an ICMP error.
307 	 */
308 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
309 	if (q6->ip6q_unfrglen >= 0) {
310 		/* The 1st fragment has already arrived. */
311 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
312 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
313 			    offset - sizeof(struct ip6_frag) +
314 			    offsetof(struct ip6_frag, ip6f_offlg));
315 			IP6Q_UNLOCK();
316 			return (IPPROTO_DONE);
317 		}
318 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
319 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
320 		    offset - sizeof(struct ip6_frag) +
321 		    offsetof(struct ip6_frag, ip6f_offlg));
322 		IP6Q_UNLOCK();
323 		return (IPPROTO_DONE);
324 	}
325 	/*
326 	 * If it's the first fragment, do the above check for each
327 	 * fragment already stored in the reassembly queue.
328 	 */
329 	if (fragoff == 0) {
330 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
331 		     af6 = af6dwn) {
332 			af6dwn = af6->ip6af_down;
333 
334 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
335 			    IPV6_MAXPACKET) {
336 				struct mbuf *merr = IP6_REASS_MBUF(af6);
337 				struct ip6_hdr *ip6err;
338 				int erroff = af6->ip6af_offset;
339 
340 				/* dequeue the fragment. */
341 				frag6_deq(af6);
342 				free(af6, M_FTABLE);
343 
344 				/* adjust pointer. */
345 				ip6err = mtod(merr, struct ip6_hdr *);
346 
347 				/*
348 				 * Restore source and destination addresses
349 				 * in the erroneous IPv6 header.
350 				 */
351 				ip6err->ip6_src = q6->ip6q_src;
352 				ip6err->ip6_dst = q6->ip6q_dst;
353 
354 				icmp6_error(merr, ICMP6_PARAM_PROB,
355 				    ICMP6_PARAMPROB_HEADER,
356 				    erroff - sizeof(struct ip6_frag) +
357 				    offsetof(struct ip6_frag, ip6f_offlg));
358 			}
359 		}
360 	}
361 
362 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
363 	    M_NOWAIT);
364 	if (ip6af == NULL)
365 		goto dropfrag;
366 	bzero(ip6af, sizeof(*ip6af));
367 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
368 	ip6af->ip6af_off = fragoff;
369 	ip6af->ip6af_frglen = frgpartlen;
370 	ip6af->ip6af_offset = offset;
371 	IP6_REASS_MBUF(ip6af) = m;
372 
373 	if (first_frag) {
374 		af6 = (struct ip6asfrag *)q6;
375 		goto insert;
376 	}
377 
378 	/*
379 	 * Handle ECN by comparing this segment with the first one;
380 	 * if CE is set, do not lose CE.
381 	 * drop if CE and not-ECT are mixed for the same packet.
382 	 */
383 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
384 	ecn0 = q6->ip6q_ecn;
385 	if (ecn == IPTOS_ECN_CE) {
386 		if (ecn0 == IPTOS_ECN_NOTECT) {
387 			free(ip6af, M_FTABLE);
388 			goto dropfrag;
389 		}
390 		if (ecn0 != IPTOS_ECN_CE)
391 			q6->ip6q_ecn = IPTOS_ECN_CE;
392 	}
393 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
394 		free(ip6af, M_FTABLE);
395 		goto dropfrag;
396 	}
397 
398 	/*
399 	 * Find a segment which begins after this one does.
400 	 */
401 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
402 	     af6 = af6->ip6af_down)
403 		if (af6->ip6af_off > ip6af->ip6af_off)
404 			break;
405 
406 #if 0
407 	/*
408 	 * If there is a preceding segment, it may provide some of
409 	 * our data already.  If so, drop the data from the incoming
410 	 * segment.  If it provides all of our data, drop us.
411 	 */
412 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
413 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
414 			- ip6af->ip6af_off;
415 		if (i > 0) {
416 			if (i >= ip6af->ip6af_frglen)
417 				goto dropfrag;
418 			m_adj(IP6_REASS_MBUF(ip6af), i);
419 			ip6af->ip6af_off += i;
420 			ip6af->ip6af_frglen -= i;
421 		}
422 	}
423 
424 	/*
425 	 * While we overlap succeeding segments trim them or,
426 	 * if they are completely covered, dequeue them.
427 	 */
428 	while (af6 != (struct ip6asfrag *)q6 &&
429 	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
430 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
431 		if (i < af6->ip6af_frglen) {
432 			af6->ip6af_frglen -= i;
433 			af6->ip6af_off += i;
434 			m_adj(IP6_REASS_MBUF(af6), i);
435 			break;
436 		}
437 		af6 = af6->ip6af_down;
438 		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
439 		frag6_deq(af6->ip6af_up);
440 	}
441 #else
442 	/*
443 	 * If the incoming framgent overlaps some existing fragments in
444 	 * the reassembly queue, drop it, since it is dangerous to override
445 	 * existing fragments from a security point of view.
446 	 * We don't know which fragment is the bad guy - here we trust
447 	 * fragment that came in earlier, with no real reason.
448 	 *
449 	 * Note: due to changes after disabling this part, mbuf passed to
450 	 * m_adj() below now does not meet the requirement.
451 	 */
452 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
453 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
454 			- ip6af->ip6af_off;
455 		if (i > 0) {
456 #if 0				/* suppress the noisy log */
457 			log(LOG_ERR, "%d bytes of a fragment from %s "
458 			    "overlaps the previous fragment\n",
459 			    i, ip6_sprintf(ip6buf, &q6->ip6q_src));
460 #endif
461 			free(ip6af, M_FTABLE);
462 			goto dropfrag;
463 		}
464 	}
465 	if (af6 != (struct ip6asfrag *)q6) {
466 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
467 		if (i > 0) {
468 #if 0				/* suppress the noisy log */
469 			log(LOG_ERR, "%d bytes of a fragment from %s "
470 			    "overlaps the succeeding fragment",
471 			    i, ip6_sprintf(ip6buf, &q6->ip6q_src));
472 #endif
473 			free(ip6af, M_FTABLE);
474 			goto dropfrag;
475 		}
476 	}
477 #endif
478 
479 insert:
480 #ifdef MAC
481 	if (!first_frag)
482 		mac_ip6q_update(m, q6);
483 #endif
484 
485 	/*
486 	 * Stick new segment in its place;
487 	 * check for complete reassembly.
488 	 * Move to front of packet queue, as we are
489 	 * the most recently active fragmented packet.
490 	 */
491 	frag6_enq(ip6af, af6->ip6af_up);
492 	V_frag6_nfrags++;
493 	q6->ip6q_nfrag++;
494 #if 0 /* xxx */
495 	if (q6 != V_ip6q.ip6q_next) {
496 		frag6_remque(q6);
497 		frag6_insque(q6, &V_ip6q);
498 	}
499 #endif
500 	next = 0;
501 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
502 	     af6 = af6->ip6af_down) {
503 		if (af6->ip6af_off != next) {
504 			IP6Q_UNLOCK();
505 			return IPPROTO_DONE;
506 		}
507 		next += af6->ip6af_frglen;
508 	}
509 	if (af6->ip6af_up->ip6af_mff) {
510 		IP6Q_UNLOCK();
511 		return IPPROTO_DONE;
512 	}
513 
514 	/*
515 	 * Reassembly is complete; concatenate fragments.
516 	 */
517 	ip6af = q6->ip6q_down;
518 	t = m = IP6_REASS_MBUF(ip6af);
519 	af6 = ip6af->ip6af_down;
520 	frag6_deq(ip6af);
521 	while (af6 != (struct ip6asfrag *)q6) {
522 		af6dwn = af6->ip6af_down;
523 		frag6_deq(af6);
524 		while (t->m_next)
525 			t = t->m_next;
526 		t->m_next = IP6_REASS_MBUF(af6);
527 		m_adj(t->m_next, af6->ip6af_offset);
528 		free(af6, M_FTABLE);
529 		af6 = af6dwn;
530 	}
531 
532 	/* adjust offset to point where the original next header starts */
533 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
534 	free(ip6af, M_FTABLE);
535 	ip6 = mtod(m, struct ip6_hdr *);
536 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
537 	if (q6->ip6q_ecn == IPTOS_ECN_CE)
538 		ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
539 	nxt = q6->ip6q_nxt;
540 #ifdef notyet
541 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
542 #endif
543 
544 	/* Delete frag6 header */
545 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
546 		/* This is the only possible case with !PULLDOWN_TEST */
547 		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
548 		    offset);
549 		m->m_data += sizeof(struct ip6_frag);
550 		m->m_len -= sizeof(struct ip6_frag);
551 	} else {
552 		/* this comes with no copy if the boundary is on cluster */
553 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
554 			frag6_remque(q6);
555 			V_frag6_nfrags -= q6->ip6q_nfrag;
556 #ifdef MAC
557 			mac_ip6q_destroy(q6);
558 #endif
559 			free(q6, M_FTABLE);
560 			V_frag6_nfragpackets--;
561 			goto dropfrag;
562 		}
563 		m_adj(t, sizeof(struct ip6_frag));
564 		m_cat(m, t);
565 	}
566 
567 	/*
568 	 * Store NXT to the original.
569 	 */
570 	{
571 		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
572 		*prvnxtp = nxt;
573 	}
574 
575 	frag6_remque(q6);
576 	V_frag6_nfrags -= q6->ip6q_nfrag;
577 #ifdef MAC
578 	mac_ip6q_reassemble(q6, m);
579 	mac_ip6q_destroy(q6);
580 #endif
581 	free(q6, M_FTABLE);
582 	V_frag6_nfragpackets--;
583 
584 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
585 		int plen = 0;
586 		for (t = m; t; t = t->m_next)
587 			plen += t->m_len;
588 		m->m_pkthdr.len = plen;
589 	}
590 
591 	V_ip6stat.ip6s_reassembled++;
592 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
593 
594 	/*
595 	 * Tell launch routine the next header
596 	 */
597 
598 	*mp = m;
599 	*offp = offset;
600 
601 	IP6Q_UNLOCK();
602 	return nxt;
603 
604  dropfrag:
605 	IP6Q_UNLOCK();
606 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
607 	V_ip6stat.ip6s_fragdropped++;
608 	m_freem(m);
609 	return IPPROTO_DONE;
610 }
611 
612 /*
613  * Free a fragment reassembly header and all
614  * associated datagrams.
615  */
616 void
617 frag6_freef(struct ip6q *q6)
618 {
619 	INIT_VNET_INET6(curvnet);
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  */
664 void
665 frag6_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  */
679 void
680 frag6_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 
689 void
690 frag6_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 
701 void
702 frag6_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  */
716 void
717 frag6_slowtimo(void)
718 {
719 	VNET_ITERATOR_DECL(vnet_iter);
720 	struct ip6q *q6;
721 
722 	IP6Q_LOCK();
723 	VNET_LIST_RLOCK();
724 	VNET_FOREACH(vnet_iter) {
725 		CURVNET_SET(vnet_iter);
726 		INIT_VNET_INET6(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 	VNET_LIST_RUNLOCK();
752 	IP6Q_UNLOCK();
753 
754 #if 0
755 	/*
756 	 * Routing changes might produce a better route than we last used;
757 	 * make sure we notice eventually, even if forwarding only for one
758 	 * destination and the cache is never replaced.
759 	 */
760 	if (V_ip6_forward_rt.ro_rt) {
761 		RTFREE(V_ip6_forward_rt.ro_rt);
762 		V_ip6_forward_rt.ro_rt = 0;
763 	}
764 	if (ipsrcchk_rt.ro_rt) {
765 		RTFREE(ipsrcchk_rt.ro_rt);
766 		ipsrcchk_rt.ro_rt = 0;
767 	}
768 #endif
769 }
770 
771 /*
772  * Drain off all datagram fragments.
773  */
774 void
775 frag6_drain(void)
776 {
777 	VNET_ITERATOR_DECL(vnet_iter);
778 
779 	if (IP6Q_TRYLOCK() == 0)
780 		return;
781 	VNET_LIST_RLOCK();
782 	VNET_FOREACH(vnet_iter) {
783 		CURVNET_SET(vnet_iter);
784 		INIT_VNET_INET6(vnet_iter);
785 		while (V_ip6q.ip6q_next != &V_ip6q) {
786 			V_ip6stat.ip6s_fragdropped++;
787 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
788 			frag6_freef(V_ip6q.ip6q_next);
789 		}
790 		CURVNET_RESTORE();
791 	}
792 	VNET_LIST_RUNLOCK();
793 	IP6Q_UNLOCK();
794 }
795