xref: /openbsd-src/sys/netinet6/frag6.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: frag6.c,v 1.67 2016/03/07 18:44:00 naddy Exp $	*/
2 /*	$KAME: frag6.c,v 1.40 2002/05/27 21:40:31 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/domain.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/errno.h>
41 #include <sys/time.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/route.h>
48 
49 #include <netinet/in.h>
50 #include <netinet6/in6_var.h>
51 #include <netinet/ip6.h>
52 #include <netinet6/ip6_var.h>
53 #include <netinet/icmp6.h>
54 #include <netinet/ip.h>		/* for ECN definitions */
55 
56 void frag6_freef(struct ip6q *);
57 
58 static int ip6q_locked;
59 u_int frag6_nfragpackets;
60 u_int frag6_nfrags;
61 TAILQ_HEAD(ip6q_head, ip6q) frag6_queue;	/* ip6 reassemble queue */
62 
63 static __inline int ip6q_lock_try(void);
64 static __inline void ip6q_unlock(void);
65 
66 static __inline int
67 ip6q_lock_try(void)
68 {
69 	int s;
70 
71 	/* Use splvm() due to mbuf allocation. */
72 	s = splvm();
73 	if (ip6q_locked) {
74 		splx(s);
75 		return (0);
76 	}
77 	ip6q_locked = 1;
78 	splx(s);
79 	return (1);
80 }
81 
82 static __inline void
83 ip6q_unlock(void)
84 {
85 	int s;
86 
87 	s = splvm();
88 	ip6q_locked = 0;
89 	splx(s);
90 }
91 
92 #ifdef DIAGNOSTIC
93 #define	IP6Q_LOCK()							\
94 do {									\
95 	if (ip6q_lock_try() == 0) {					\
96 		printf("%s:%d: ip6q already locked\n", __FILE__, __LINE__); \
97 		panic("ip6q_lock");					\
98 	}								\
99 } while (0)
100 #define	IP6Q_LOCK_CHECK()						\
101 do {									\
102 	if (ip6q_locked == 0) {						\
103 		printf("%s:%d: ip6q lock not held\n", __FILE__, __LINE__); \
104 		panic("ip6q lock check");				\
105 	}								\
106 } while (0)
107 #else
108 #define	IP6Q_LOCK()		(void) ip6q_lock_try()
109 #define	IP6Q_LOCK_CHECK()	/* nothing */
110 #endif
111 
112 #define	IP6Q_UNLOCK()		ip6q_unlock()
113 
114 /*
115  * Initialise reassembly queue and fragment identifier.
116  */
117 void
118 frag6_init(void)
119 {
120 
121 	TAILQ_INIT(&frag6_queue);
122 }
123 
124 /*
125  * In RFC2460, fragment and reassembly rule do not agree with each other,
126  * in terms of next header field handling in fragment header.
127  * While the sender will use the same value for all of the fragmented packets,
128  * receiver is suggested not to check the consistency.
129  *
130  * fragment rule (p20):
131  *	(2) A Fragment header containing:
132  *	The Next Header value that identifies the first header of
133  *	the Fragmentable Part of the original packet.
134  *		-> next header field is same for all fragments
135  *
136  * reassembly rule (p21):
137  *	The Next Header field of the last header of the Unfragmentable
138  *	Part is obtained from the Next Header field of the first
139  *	fragment's Fragment header.
140  *		-> should grab it from the first fragment only
141  *
142  * The following note also contradicts with fragment rule - noone is going to
143  * send different fragment with different next header field.
144  *
145  * additional note (p22):
146  *	The Next Header values in the Fragment headers of different
147  *	fragments of the same original packet may differ.  Only the value
148  *	from the Offset zero fragment packet is used for reassembly.
149  *		-> should grab it from the first fragment only
150  *
151  * There is no explicit reason given in the RFC.  Historical reason maybe?
152  */
153 /*
154  * Fragment input
155  */
156 int
157 frag6_input(struct mbuf **mp, int *offp, int proto)
158 {
159 	struct mbuf *m = *mp, *t;
160 	struct ip6_hdr *ip6;
161 	struct ip6_frag *ip6f;
162 	struct ip6q *q6;
163 	struct ip6asfrag *af6, *ip6af, *naf6, *paf6;
164 	int offset = *offp, nxt, i, next;
165 	int first_frag = 0;
166 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
167 	u_int8_t ecn, ecn0;
168 
169 	ip6 = mtod(m, struct ip6_hdr *);
170 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
171 	if (ip6f == NULL)
172 		return IPPROTO_DONE;
173 
174 	/* jumbo payload can't contain a fragment header */
175 	if (ip6->ip6_plen == 0) {
176 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
177 		return IPPROTO_DONE;
178 	}
179 
180 	/*
181 	 * check whether fragment packet's fragment length is
182 	 * multiple of 8 octets.
183 	 * sizeof(struct ip6_frag) == 8
184 	 * sizeof(struct ip6_hdr) = 40
185 	 */
186 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
187 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
188 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
189 		    offsetof(struct ip6_hdr, ip6_plen));
190 		return IPPROTO_DONE;
191 	}
192 
193 	ip6stat.ip6s_fragments++;
194 
195 	/* offset now points to data portion */
196 	offset += sizeof(struct ip6_frag);
197 
198 	/*
199 	 * RFC6946:  A host that receives an IPv6 packet which includes
200 	 * a Fragment Header with the "Fragment Offset" equal to 0 and
201 	 * the "M" bit equal to 0 MUST process such packet in isolation
202 	 * from any other packets/fragments.
203 	 */
204 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
205 	if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
206 		ip6stat.ip6s_reassembled++;
207 		*offp = offset;
208 		return ip6f->ip6f_nxt;
209 	}
210 
211 	IP6Q_LOCK();
212 
213 	/*
214 	 * Enforce upper bound on number of fragments.
215 	 * If maxfrag is 0, never accept fragments.
216 	 * If maxfrag is -1, accept all fragments without limitation.
217 	 */
218 	if (ip6_maxfrags >= 0 && frag6_nfrags >= (u_int)ip6_maxfrags)
219 		goto dropfrag;
220 
221 	TAILQ_FOREACH(q6, &frag6_queue, ip6q_queue)
222 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
223 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
224 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
225 			break;
226 
227 	if (q6 == NULL) {
228 		/*
229 		 * the first fragment to arrive, create a reassembly queue.
230 		 */
231 		first_frag = 1;
232 
233 		/*
234 		 * Enforce upper bound on number of fragmented packets
235 		 * for which we attempt reassembly;
236 		 * If maxfragpackets is 0, never accept fragments.
237 		 * If maxfragpackets is -1, accept all fragments without
238 		 * limitation.
239 		 */
240 		if (ip6_maxfragpackets >= 0 &&
241 		    frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
242 			goto dropfrag;
243 		frag6_nfragpackets++;
244 		q6 = malloc(sizeof(*q6), M_FTABLE, M_NOWAIT | M_ZERO);
245 		if (q6 == NULL)
246 			goto dropfrag;
247 
248 		TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
249 
250 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
251 		LIST_INIT(&q6->ip6q_asfrag);
252 		q6->ip6q_ident	= ip6f->ip6f_ident;
253 		q6->ip6q_ttl	= IPV6_FRAGTTL;
254 		q6->ip6q_src	= ip6->ip6_src;
255 		q6->ip6q_dst	= ip6->ip6_dst;
256 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
257 		q6->ip6q_nfrag = 0;
258 	}
259 
260 	/*
261 	 * If it's the 1st fragment, record the length of the
262 	 * unfragmentable part and the next header of the fragment header.
263 	 */
264 	if (fragoff == 0) {
265 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
266 		    sizeof(struct ip6_frag);
267 		q6->ip6q_nxt = ip6f->ip6f_nxt;
268 	}
269 
270 	/*
271 	 * Check that the reassembled packet would not exceed 65535 bytes
272 	 * in size.
273 	 * If it would exceed, discard the fragment and return an ICMP error.
274 	 */
275 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
276 	if (q6->ip6q_unfrglen >= 0) {
277 		/* The 1st fragment has already arrived. */
278 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
279 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
280 			    offset - sizeof(struct ip6_frag) +
281 			    offsetof(struct ip6_frag, ip6f_offlg));
282 			IP6Q_UNLOCK();
283 			return (IPPROTO_DONE);
284 		}
285 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
286 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
287 			    offset - sizeof(struct ip6_frag) +
288 				offsetof(struct ip6_frag, ip6f_offlg));
289 		IP6Q_UNLOCK();
290 		return (IPPROTO_DONE);
291 	}
292 	/*
293 	 * If it's the first fragment, do the above check for each
294 	 * fragment already stored in the reassembly queue.
295 	 */
296 	if (fragoff == 0) {
297 		LIST_FOREACH_SAFE(af6, &q6->ip6q_asfrag, ip6af_list, naf6) {
298 			if (q6->ip6q_unfrglen + af6->ip6af_off +
299 			    af6->ip6af_frglen > IPV6_MAXPACKET) {
300 				struct mbuf *merr = IP6_REASS_MBUF(af6);
301 				struct ip6_hdr *ip6err;
302 				int erroff = af6->ip6af_offset;
303 
304 				/* dequeue the fragment. */
305 				LIST_REMOVE(af6, ip6af_list);
306 				free(af6, M_FTABLE, 0);
307 
308 				/* adjust pointer. */
309 				ip6err = mtod(merr, struct ip6_hdr *);
310 
311 				/*
312 				 * Restore source and destination addresses
313 				 * in the erroneous IPv6 header.
314 				 */
315 				ip6err->ip6_src = q6->ip6q_src;
316 				ip6err->ip6_dst = q6->ip6q_dst;
317 
318 				icmp6_error(merr, ICMP6_PARAM_PROB,
319 				    ICMP6_PARAMPROB_HEADER,
320 				    erroff - sizeof(struct ip6_frag) +
321 				    offsetof(struct ip6_frag, ip6f_offlg));
322 			}
323 		}
324 	}
325 
326 	ip6af = malloc(sizeof(*ip6af), M_FTABLE, M_NOWAIT | M_ZERO);
327 	if (ip6af == NULL)
328 		goto dropfrag;
329 	ip6af->ip6af_flow = ip6->ip6_flow;
330 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
331 	ip6af->ip6af_off = fragoff;
332 	ip6af->ip6af_frglen = frgpartlen;
333 	ip6af->ip6af_offset = offset;
334 	IP6_REASS_MBUF(ip6af) = m;
335 
336 	if (first_frag) {
337 		paf6 = NULL;
338 		goto insert;
339 	}
340 
341 	/*
342 	 * Handle ECN by comparing this segment with the first one;
343 	 * if CE is set, do not lose CE.
344 	 * drop if CE and not-ECT are mixed for the same packet.
345 	 */
346 	af6 = LIST_FIRST(&q6->ip6q_asfrag);
347 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
348 	ecn0 = (ntohl(af6->ip6af_flow) >> 20) & IPTOS_ECN_MASK;
349 	if (ecn == IPTOS_ECN_CE) {
350 		if (ecn0 == IPTOS_ECN_NOTECT) {
351 			free(ip6af, M_FTABLE, 0);
352 			goto dropfrag;
353 		}
354 		if (ecn0 != IPTOS_ECN_CE)
355 			af6->ip6af_flow |= htonl(IPTOS_ECN_CE << 20);
356 	}
357 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
358 		free(ip6af, M_FTABLE, 0);
359 		goto dropfrag;
360 	}
361 
362 	/*
363 	 * Find a segment which begins after this one does.
364 	 */
365 	for (paf6 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
366 	    af6 != NULL;
367 	    paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list))
368 		if (af6->ip6af_off > ip6af->ip6af_off)
369 			break;
370 
371 	/*
372 	 * RFC 5722, Errata 3089:  When reassembling an IPv6 datagram, if one
373 	 * or more its constituent fragments is determined to be an overlapping
374 	 * fragment, the entire datagram (and any constituent fragments) MUST
375 	 * be silently discarded.
376 	 */
377 	if (paf6 != NULL) {
378 		i = (paf6->ip6af_off + paf6->ip6af_frglen) - ip6af->ip6af_off;
379 		if (i > 0) {
380 #if 0				/* suppress the noisy log */
381 			char ip[INET6_ADDRSTRLEN];
382 			log(LOG_ERR, "%d bytes of a fragment from %s "
383 			    "overlaps the previous fragment\n",
384 			    i,
385 			    inet_ntop(AF_INET6, &q6->ip6q_src, ip, sizeof(ip)));
386 #endif
387 			free(ip6af, M_FTABLE, 0);
388 			goto flushfrags;
389 		}
390 	}
391 	if (af6 != NULL) {
392 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
393 		if (i > 0) {
394 #if 0				/* suppress the noisy log */
395 			char ip[INET6_ADDRSTRLEN];
396 			log(LOG_ERR, "%d bytes of a fragment from %s "
397 			    "overlaps the succeeding fragment",
398 			    i,
399 			    inet_ntop(AF_INET6, &q6->ip6q_src, ip, sizeof(ip)));
400 #endif
401 			free(ip6af, M_FTABLE, 0);
402 			goto flushfrags;
403 		}
404 	}
405 
406  insert:
407 	/*
408 	 * Stick new segment in its place;
409 	 * check for complete reassembly.
410 	 * Move to front of packet queue, as we are
411 	 * the most recently active fragmented packet.
412 	 */
413 	if (paf6 != NULL)
414 		LIST_INSERT_AFTER(paf6, ip6af, ip6af_list);
415 	else
416 		LIST_INSERT_HEAD(&q6->ip6q_asfrag, ip6af, ip6af_list);
417 	frag6_nfrags++;
418 	q6->ip6q_nfrag++;
419 #if 0 /* xxx */
420 	if (q6 != TAILQ_FIRST(&frag6_queue)) {
421 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
422 		TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
423 	}
424 #endif
425 	next = 0;
426 	for (paf6 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
427 	    af6 != NULL;
428 	    paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list)) {
429 		if (af6->ip6af_off != next) {
430 			IP6Q_UNLOCK();
431 			return IPPROTO_DONE;
432 		}
433 		next += af6->ip6af_frglen;
434 	}
435 	if (paf6->ip6af_mff) {
436 		IP6Q_UNLOCK();
437 		return IPPROTO_DONE;
438 	}
439 
440 	/*
441 	 * Reassembly is complete; concatenate fragments.
442 	 */
443 	ip6af = LIST_FIRST(&q6->ip6q_asfrag);
444 	LIST_REMOVE(ip6af, ip6af_list);
445 	t = m = IP6_REASS_MBUF(ip6af);
446 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
447 		LIST_REMOVE(af6, ip6af_list);
448 		while (t->m_next)
449 			t = t->m_next;
450 		t->m_next = IP6_REASS_MBUF(af6);
451 		m_adj(t->m_next, af6->ip6af_offset);
452 		free(af6, M_FTABLE, 0);
453 	}
454 
455 	/* adjust offset to point where the original next header starts */
456 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
457 	free(ip6af, M_FTABLE, 0);
458 	ip6 = mtod(m, struct ip6_hdr *);
459 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
460 	ip6->ip6_src = q6->ip6q_src;
461 	ip6->ip6_dst = q6->ip6q_dst;
462 	nxt = q6->ip6q_nxt;
463 
464 	/* Delete frag6 header */
465 	if (frag6_deletefraghdr(m, offset) != 0) {
466 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
467 		frag6_nfrags -= q6->ip6q_nfrag;
468 		free(q6, M_FTABLE, 0);
469 		frag6_nfragpackets--;
470 		goto dropfrag;
471 	}
472 
473 	/*
474 	 * Store NXT to the original.
475 	 */
476 	{
477 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
478 		*prvnxtp = nxt;
479 	}
480 
481 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
482 	frag6_nfrags -= q6->ip6q_nfrag;
483 	free(q6, M_FTABLE, 0);
484 	frag6_nfragpackets--;
485 
486 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
487 		int plen = 0;
488 		for (t = m; t; t = t->m_next)
489 			plen += t->m_len;
490 		m->m_pkthdr.len = plen;
491 	}
492 
493 	ip6stat.ip6s_reassembled++;
494 
495 	/*
496 	 * Tell launch routine the next header
497 	 */
498 
499 	*mp = m;
500 	*offp = offset;
501 
502 	IP6Q_UNLOCK();
503 	return nxt;
504 
505  flushfrags:
506 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
507 		LIST_REMOVE(af6, ip6af_list);
508 		m_freem(IP6_REASS_MBUF(af6));
509 		free(af6, M_FTABLE, 0);
510 	}
511 	ip6stat.ip6s_fragdropped += q6->ip6q_nfrag;
512 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
513 	frag6_nfrags -= q6->ip6q_nfrag;
514 	free(q6, M_FTABLE, 0);
515 	frag6_nfragpackets--;
516 
517  dropfrag:
518 	ip6stat.ip6s_fragdropped++;
519 	m_freem(m);
520 	IP6Q_UNLOCK();
521 	return IPPROTO_DONE;
522 }
523 
524 /*
525  * Delete fragment header after the unfragmentable header portions.
526  */
527 int
528 frag6_deletefraghdr(struct mbuf *m, int offset)
529 {
530 	struct mbuf *t;
531 
532 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
533 		memmove(mtod(m, caddr_t) + sizeof(struct ip6_frag),
534 		    mtod(m, caddr_t), offset);
535 		m->m_data += sizeof(struct ip6_frag);
536 		m->m_len -= sizeof(struct ip6_frag);
537 	} else {
538 		/* this comes with no copy if the boundary is on cluster */
539 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL)
540 			return (ENOBUFS);
541 		m_adj(t, sizeof(struct ip6_frag));
542 		m_cat(m, t);
543 	}
544 
545 	return (0);
546 }
547 
548 /*
549  * Free a fragment reassembly header and all
550  * associated datagrams.
551  */
552 void
553 frag6_freef(struct ip6q *q6)
554 {
555 	struct ip6asfrag *af6;
556 
557 	IP6Q_LOCK_CHECK();
558 
559 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
560 		struct mbuf *m = IP6_REASS_MBUF(af6);
561 
562 		LIST_REMOVE(af6, ip6af_list);
563 
564 		/*
565 		 * Return ICMP time exceeded error for the 1st fragment.
566 		 * Just free other fragments.
567 		 */
568 		if (af6->ip6af_off == 0) {
569 			struct ip6_hdr *ip6;
570 
571 			/* adjust pointer */
572 			ip6 = mtod(m, struct ip6_hdr *);
573 
574 			/* restore source and destination addresses */
575 			ip6->ip6_src = q6->ip6q_src;
576 			ip6->ip6_dst = q6->ip6q_dst;
577 
578 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
579 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
580 		} else
581 			m_freem(m);
582 		free(af6, M_FTABLE, 0);
583 	}
584 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
585 	frag6_nfrags -= q6->ip6q_nfrag;
586 	free(q6, M_FTABLE, 0);
587 	frag6_nfragpackets--;
588 }
589 
590 /*
591  * IPv6 reassembling timer processing;
592  * if a timer expires on a reassembly
593  * queue, discard it.
594  */
595 void
596 frag6_slowtimo(void)
597 {
598 	struct ip6q *q6, *nq6;
599 	int s = splsoftnet();
600 	extern struct route_in6 ip6_forward_rt;
601 
602 	IP6Q_LOCK();
603 	TAILQ_FOREACH_SAFE(q6, &frag6_queue, ip6q_queue, nq6)
604 		if (--q6->ip6q_ttl == 0) {
605 			ip6stat.ip6s_fragtimeout++;
606 			frag6_freef(q6);
607 		}
608 
609 	/*
610 	 * If we are over the maximum number of fragments
611 	 * (due to the limit being lowered), drain off
612 	 * enough to get down to the new limit.
613 	 */
614 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
615 	    !TAILQ_EMPTY(&frag6_queue)) {
616 		ip6stat.ip6s_fragoverflow++;
617 		frag6_freef(TAILQ_LAST(&frag6_queue, ip6q_head));
618 	}
619 	IP6Q_UNLOCK();
620 
621 	/*
622 	 * Routing changes might produce a better route than we last used;
623 	 * make sure we notice eventually, even if forwarding only for one
624 	 * destination and the cache is never replaced.
625 	 */
626 	if (ip6_forward_rt.ro_rt) {
627 		rtfree(ip6_forward_rt.ro_rt);
628 		ip6_forward_rt.ro_rt = NULL;
629 	}
630 
631 	splx(s);
632 }
633 
634 /*
635  * Drain off all datagram fragments.
636  */
637 void
638 frag6_drain(void)
639 {
640 	struct ip6q *q6;
641 
642 	if (ip6q_lock_try() == 0)
643 		return;
644 	while ((q6 = TAILQ_FIRST(&frag6_queue)) != NULL) {
645 		ip6stat.ip6s_fragdropped++;
646 		frag6_freef(q6);
647 	}
648 	IP6Q_UNLOCK();
649 }
650