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