xref: /netbsd-src/sys/netinet6/frag6.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: frag6.c,v 1.52 2011/12/31 20:41:59 christos 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/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.52 2011/12/31 20:41:59 christos Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46 #include <sys/kernel.h>
47 #include <sys/syslog.h>
48 
49 #include <net/if.h>
50 #include <net/route.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip6.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet6/ip6_private.h>
57 #include <netinet/icmp6.h>
58 
59 #include <net/net_osdep.h>
60 
61 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
62 static void frag6_deq(struct ip6asfrag *);
63 static void frag6_insque(struct ip6q *, struct ip6q *);
64 static void frag6_remque(struct ip6q *);
65 static void frag6_freef(struct ip6q *);
66 
67 static int frag6_drainwanted;
68 
69 u_int frag6_nfragpackets;
70 u_int frag6_nfrags;
71 struct	ip6q ip6q;	/* ip6 reassemble queue */
72 
73 static kmutex_t	frag6_lock;
74 
75 /*
76  * Initialise reassembly queue and fragment identifier.
77  */
78 void
79 frag6_init(void)
80 {
81 
82 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
83 	mutex_init(&frag6_lock, MUTEX_DEFAULT, IPL_NET);
84 }
85 
86 /*
87  * In RFC2460, fragment and reassembly rule do not agree with each other,
88  * in terms of next header field handling in fragment header.
89  * While the sender will use the same value for all of the fragmented packets,
90  * receiver is suggested not to check the consistency.
91  *
92  * fragment rule (p20):
93  *	(2) A Fragment header containing:
94  *	The Next Header value that identifies the first header of
95  *	the Fragmentable Part of the original packet.
96  *		-> next header field is same for all fragments
97  *
98  * reassembly rule (p21):
99  *	The Next Header field of the last header of the Unfragmentable
100  *	Part is obtained from the Next Header field of the first
101  *	fragment's Fragment header.
102  *		-> should grab it from the first fragment only
103  *
104  * The following note also contradicts with fragment rule - noone is going to
105  * send different fragment with different next header field.
106  *
107  * additional note (p22):
108  *	The Next Header values in the Fragment headers of different
109  *	fragments of the same original packet may differ.  Only the value
110  *	from the Offset zero fragment packet is used for reassembly.
111  *		-> should grab it from the first fragment only
112  *
113  * There is no explicit reason given in the RFC.  Historical reason maybe?
114  */
115 /*
116  * Fragment input
117  */
118 static int
119 frag6_in(struct mbuf **mp, int *offp)
120 {
121 	struct rtentry *rt;
122 	struct mbuf *m = *mp, *t;
123 	struct ip6_hdr *ip6;
124 	struct ip6_frag *ip6f;
125 	struct ip6q *q6;
126 	struct ip6asfrag *af6, *ip6af, *af6dwn;
127 	int offset = *offp, nxt, i, next;
128 	int first_frag = 0;
129 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
130 	struct ifnet *dstifp;
131 	static struct route ro;
132 	union {
133 		struct sockaddr		dst;
134 		struct sockaddr_in6	dst6;
135 	} u;
136 
137 	ip6 = mtod(m, struct ip6_hdr *);
138 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
139 	if (ip6f == NULL)
140 		return -1;
141 
142 	dstifp = NULL;
143 	/* find the destination interface of the packet. */
144 	sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
145 	if ((rt = rtcache_lookup(&ro, &u.dst)) != NULL && rt->rt_ifa != NULL)
146 		dstifp = ((struct in6_ifaddr *)rt->rt_ifa)->ia_ifp;
147 
148 	/* jumbo payload can't contain a fragment header */
149 	if (ip6->ip6_plen == 0) {
150 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
151 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
152 		return -1;
153 	}
154 
155 	/*
156 	 * check whether fragment packet's fragment length is
157 	 * multiple of 8 octets.
158 	 * sizeof(struct ip6_frag) == 8
159 	 * sizeof(struct ip6_hdr) = 40
160 	 */
161 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
162 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
163 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
164 		    offsetof(struct ip6_hdr, ip6_plen));
165 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
166 		return -1;
167 	}
168 
169 	IP6_STATINC(IP6_STAT_FRAGMENTS);
170 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
171 
172 	/* offset now points to data portion */
173 	offset += sizeof(struct ip6_frag);
174 
175 	mutex_enter(&frag6_lock);
176 
177 	/*
178 	 * Enforce upper bound on number of fragments.
179 	 * If maxfrag is 0, never accept fragments.
180 	 * If maxfrag is -1, accept all fragments without limitation.
181 	 */
182 	if (ip6_maxfrags < 0)
183 		;
184 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
185 		goto dropfrag;
186 
187 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
188 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
189 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
190 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
191 			break;
192 
193 	if (q6 == &ip6q) {
194 		/*
195 		 * the first fragment to arrive, create a reassembly queue.
196 		 */
197 		first_frag = 1;
198 
199 		/*
200 		 * Enforce upper bound on number of fragmented packets
201 		 * for which we attempt reassembly;
202 		 * If maxfragpackets is 0, never accept fragments.
203 		 * If maxfragpackets is -1, accept all fragments without
204 		 * limitation.
205 		 */
206 		if (ip6_maxfragpackets < 0)
207 			;
208 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
209 			goto dropfrag;
210 		frag6_nfragpackets++;
211 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
212 		    M_DONTWAIT);
213 		if (q6 == NULL)
214 			goto dropfrag;
215 		memset(q6, 0, sizeof(*q6));
216 		frag6_insque(q6, &ip6q);
217 
218 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
219 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
220 #ifdef notyet
221 		q6->ip6q_nxtp	= (u_char *)nxtp;
222 #endif
223 		q6->ip6q_ident	= ip6f->ip6f_ident;
224 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
225 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
226 		q6->ip6q_src	= ip6->ip6_src;
227 		q6->ip6q_dst	= ip6->ip6_dst;
228 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
229 
230 		q6->ip6q_nfrag = 0;
231 	}
232 
233 	/*
234 	 * If it's the 1st fragment, record the length of the
235 	 * unfragmentable part and the next header of the fragment header.
236 	 */
237 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
238 	if (fragoff == 0) {
239 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
240 		    sizeof(struct ip6_frag);
241 		q6->ip6q_nxt = ip6f->ip6f_nxt;
242 	}
243 
244 	/*
245 	 * Check that the reassembled packet would not exceed 65535 bytes
246 	 * in size.
247 	 * If it would exceed, discard the fragment and return an ICMP error.
248 	 */
249 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
250 	if (q6->ip6q_unfrglen >= 0) {
251 		/* The 1st fragment has already arrived. */
252 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
253 			mutex_exit(&frag6_lock);
254 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
255 			    offset - sizeof(struct ip6_frag) +
256 			    offsetof(struct ip6_frag, ip6f_offlg));
257 			return -1;
258 		}
259 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
260 		mutex_exit(&frag6_lock);
261 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
262 			    offset - sizeof(struct ip6_frag) +
263 				offsetof(struct ip6_frag, ip6f_offlg));
264 		return -1;
265 	}
266 	/*
267 	 * If it's the first fragment, do the above check for each
268 	 * fragment already stored in the reassembly queue.
269 	 */
270 	if (fragoff == 0) {
271 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
272 		     af6 = af6dwn) {
273 			af6dwn = af6->ip6af_down;
274 
275 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
276 			    IPV6_MAXPACKET) {
277 				struct mbuf *merr = IP6_REASS_MBUF(af6);
278 				struct ip6_hdr *ip6err;
279 				int erroff = af6->ip6af_offset;
280 
281 				/* dequeue the fragment. */
282 				frag6_deq(af6);
283 				free(af6, M_FTABLE);
284 
285 				/* adjust pointer. */
286 				ip6err = mtod(merr, struct ip6_hdr *);
287 
288 				/*
289 				 * Restore source and destination addresses
290 				 * in the erroneous IPv6 header.
291 				 */
292 				ip6err->ip6_src = q6->ip6q_src;
293 				ip6err->ip6_dst = q6->ip6q_dst;
294 
295 				icmp6_error(merr, ICMP6_PARAM_PROB,
296 				    ICMP6_PARAMPROB_HEADER,
297 				    erroff - sizeof(struct ip6_frag) +
298 				    offsetof(struct ip6_frag, ip6f_offlg));
299 			}
300 		}
301 	}
302 
303 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
304 	    M_DONTWAIT);
305 	if (ip6af == NULL)
306 		goto dropfrag;
307 	memset(ip6af, 0, sizeof(*ip6af));
308 	ip6af->ip6af_head = ip6->ip6_flow;
309 	ip6af->ip6af_len = ip6->ip6_plen;
310 	ip6af->ip6af_nxt = ip6->ip6_nxt;
311 	ip6af->ip6af_hlim = ip6->ip6_hlim;
312 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
313 	ip6af->ip6af_off = fragoff;
314 	ip6af->ip6af_frglen = frgpartlen;
315 	ip6af->ip6af_offset = offset;
316 	IP6_REASS_MBUF(ip6af) = m;
317 
318 	if (first_frag) {
319 		af6 = (struct ip6asfrag *)q6;
320 		goto insert;
321 	}
322 
323 	/*
324 	 * Find a segment which begins after this one does.
325 	 */
326 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
327 	     af6 = af6->ip6af_down)
328 		if (af6->ip6af_off > ip6af->ip6af_off)
329 			break;
330 
331 #if 0
332 	/*
333 	 * If there is a preceding segment, it may provide some of
334 	 * our data already.  If so, drop the data from the incoming
335 	 * segment.  If it provides all of our data, drop us.
336 	 */
337 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
338 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
339 			- ip6af->ip6af_off;
340 		if (i > 0) {
341 			if (i >= ip6af->ip6af_frglen)
342 				goto dropfrag;
343 			m_adj(IP6_REASS_MBUF(ip6af), i);
344 			ip6af->ip6af_off += i;
345 			ip6af->ip6af_frglen -= i;
346 		}
347 	}
348 
349 	/*
350 	 * While we overlap succeeding segments trim them or,
351 	 * if they are completely covered, dequeue them.
352 	 */
353 	while (af6 != (struct ip6asfrag *)q6 &&
354 	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
355 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
356 		if (i < af6->ip6af_frglen) {
357 			af6->ip6af_frglen -= i;
358 			af6->ip6af_off += i;
359 			m_adj(IP6_REASS_MBUF(af6), i);
360 			break;
361 		}
362 		af6 = af6->ip6af_down;
363 		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
364 		frag6_deq(af6->ip6af_up);
365 	}
366 #else
367 	/*
368 	 * If the incoming framgent overlaps some existing fragments in
369 	 * the reassembly queue, drop it, since it is dangerous to override
370 	 * existing fragments from a security point of view.
371 	 * We don't know which fragment is the bad guy - here we trust
372 	 * fragment that came in earlier, with no real reason.
373 	 */
374 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
375 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
376 			- ip6af->ip6af_off;
377 		if (i > 0) {
378 #if 0				/* suppress the noisy log */
379 			log(LOG_ERR, "%d bytes of a fragment from %s "
380 			    "overlaps the previous fragment\n",
381 			    i, ip6_sprintf(&q6->ip6q_src));
382 #endif
383 			free(ip6af, M_FTABLE);
384 			goto dropfrag;
385 		}
386 	}
387 	if (af6 != (struct ip6asfrag *)q6) {
388 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
389 		if (i > 0) {
390 #if 0				/* suppress the noisy log */
391 			log(LOG_ERR, "%d bytes of a fragment from %s "
392 			    "overlaps the succeeding fragment",
393 			    i, ip6_sprintf(&q6->ip6q_src));
394 #endif
395 			free(ip6af, M_FTABLE);
396 			goto dropfrag;
397 		}
398 	}
399 #endif
400 
401 insert:
402 
403 	/*
404 	 * Stick new segment in its place;
405 	 * check for complete reassembly.
406 	 * Move to front of packet queue, as we are
407 	 * the most recently active fragmented packet.
408 	 */
409 	frag6_enq(ip6af, af6->ip6af_up);
410 	frag6_nfrags++;
411 	q6->ip6q_nfrag++;
412 #if 0 /* xxx */
413 	if (q6 != ip6q.ip6q_next) {
414 		frag6_remque(q6);
415 		frag6_insque(q6, &ip6q);
416 	}
417 #endif
418 	next = 0;
419 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
420 	     af6 = af6->ip6af_down) {
421 		if (af6->ip6af_off != next) {
422 			mutex_exit(&frag6_lock);
423 			return 0;
424 		}
425 		next += af6->ip6af_frglen;
426 	}
427 	if (af6->ip6af_up->ip6af_mff) {
428 		mutex_exit(&frag6_lock);
429 		return 0;
430 	}
431 
432 	/*
433 	 * Reassembly is complete; concatenate fragments.
434 	 */
435 	ip6af = q6->ip6q_down;
436 	t = m = IP6_REASS_MBUF(ip6af);
437 	af6 = ip6af->ip6af_down;
438 	frag6_deq(ip6af);
439 	while (af6 != (struct ip6asfrag *)q6) {
440 		af6dwn = af6->ip6af_down;
441 		frag6_deq(af6);
442 		while (t->m_next)
443 			t = t->m_next;
444 		t->m_next = IP6_REASS_MBUF(af6);
445 		m_adj(t->m_next, af6->ip6af_offset);
446 		free(af6, M_FTABLE);
447 		af6 = af6dwn;
448 	}
449 
450 	/* adjust offset to point where the original next header starts */
451 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
452 	free(ip6af, M_FTABLE);
453 	ip6 = mtod(m, struct ip6_hdr *);
454 	ip6->ip6_plen = htons(next + offset - sizeof(struct ip6_hdr));
455 	ip6->ip6_src = q6->ip6q_src;
456 	ip6->ip6_dst = q6->ip6q_dst;
457 	nxt = q6->ip6q_nxt;
458 #ifdef notyet
459 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
460 #endif
461 
462 	/*
463 	 * Delete frag6 header with as a few cost as possible.
464 	 */
465 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
466 		memmove((char *)ip6 + sizeof(struct ip6_frag), ip6, offset);
467 		m->m_data += sizeof(struct ip6_frag);
468 		m->m_len -= sizeof(struct ip6_frag);
469 	} else {
470 		/* this comes with no copy if the boundary is on cluster */
471 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
472 			frag6_remque(q6);
473 			frag6_nfrags -= q6->ip6q_nfrag;
474 			free(q6, M_FTABLE);
475 			frag6_nfragpackets--;
476 			goto dropfrag;
477 		}
478 		m_adj(t, sizeof(struct ip6_frag));
479 		m_cat(m, t);
480 	}
481 
482 	/*
483 	 * Store NXT to the original.
484 	 */
485 	{
486 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
487 		*prvnxtp = nxt;
488 	}
489 
490 	frag6_remque(q6);
491 	frag6_nfrags -= q6->ip6q_nfrag;
492 	free(q6, M_FTABLE);
493 	frag6_nfragpackets--;
494 
495 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
496 		int plen = 0;
497 		for (t = m; t; t = t->m_next)
498 			plen += t->m_len;
499 		m->m_pkthdr.len = plen;
500 	}
501 
502 	IP6_STATINC(IP6_STAT_REASSEMBLED);
503 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
504 
505 	/*
506 	 * Tell launch routine the next header
507 	 */
508 
509 	*mp = m;
510 	*offp = offset;
511 
512 	mutex_exit(&frag6_lock);
513 	return nxt;
514 
515  dropfrag:
516 	mutex_exit(&frag6_lock);
517 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
518 	IP6_STATINC(IP6_STAT_FRAGDROPPED);
519 	m_freem(m);
520 	return -1;
521 }
522 
523 int
524 frag6_input(struct mbuf **mp, int *offp, int proto)
525 {
526 	int ret = frag6_in(mp, offp);
527 
528 	if (ret > 0) {
529 		return ret;
530 	}
531 	return IPPROTO_DONE;
532 }
533 
534 int
535 ip6_reass_packet(struct mbuf **mp, int offset)
536 {
537 	int ret = frag6_in(mp, &offset);
538 
539 	if (ret <= 0) {
540 		*mp = NULL;
541 	}
542 	return ret < 0 ? ret : 0;
543 }
544 
545 /*
546  * Free a fragment reassembly header and all
547  * associated datagrams.
548  */
549 void
550 frag6_freef(struct ip6q *q6)
551 {
552 	struct ip6asfrag *af6, *down6;
553 
554 	KASSERT(mutex_owned(&frag6_lock));
555 
556 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
557 	     af6 = down6) {
558 		struct mbuf *m = IP6_REASS_MBUF(af6);
559 
560 		down6 = af6->ip6af_down;
561 		frag6_deq(af6);
562 
563 		/*
564 		 * Return ICMP time exceeded error for the 1st fragment.
565 		 * Just free other fragments.
566 		 */
567 		if (af6->ip6af_off == 0) {
568 			struct ip6_hdr *ip6;
569 
570 			/* adjust pointer */
571 			ip6 = mtod(m, struct ip6_hdr *);
572 
573 			/* restoure source and destination addresses */
574 			ip6->ip6_src = q6->ip6q_src;
575 			ip6->ip6_dst = q6->ip6q_dst;
576 
577 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
578 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
579 		} else
580 			m_freem(m);
581 		free(af6, M_FTABLE);
582 	}
583 	frag6_remque(q6);
584 	frag6_nfrags -= q6->ip6q_nfrag;
585 	free(q6, M_FTABLE);
586 	frag6_nfragpackets--;
587 }
588 
589 /*
590  * Put an ip fragment on a reassembly chain.
591  * Like insque, but pointers in middle of structure.
592  */
593 void
594 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
595 {
596 
597 	KASSERT(mutex_owned(&frag6_lock));
598 
599 	af6->ip6af_up = up6;
600 	af6->ip6af_down = up6->ip6af_down;
601 	up6->ip6af_down->ip6af_up = af6;
602 	up6->ip6af_down = af6;
603 }
604 
605 /*
606  * To frag6_enq as remque is to insque.
607  */
608 void
609 frag6_deq(struct ip6asfrag *af6)
610 {
611 
612 	KASSERT(mutex_owned(&frag6_lock));
613 
614 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
615 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
616 }
617 
618 void
619 frag6_insque(struct ip6q *new, struct ip6q *old)
620 {
621 
622 	KASSERT(mutex_owned(&frag6_lock));
623 
624 	new->ip6q_prev = old;
625 	new->ip6q_next = old->ip6q_next;
626 	old->ip6q_next->ip6q_prev= new;
627 	old->ip6q_next = new;
628 }
629 
630 void
631 frag6_remque(struct ip6q *p6)
632 {
633 
634 	KASSERT(mutex_owned(&frag6_lock));
635 
636 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
637 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
638 }
639 
640 void
641 frag6_fasttimo(void)
642 {
643 	mutex_enter(softnet_lock);
644 	KERNEL_LOCK(1, NULL);
645 
646 	if (frag6_drainwanted) {
647 		frag6_drain();
648 		frag6_drainwanted = 0;
649 	}
650 
651 	KERNEL_UNLOCK_ONE(NULL);
652 	mutex_exit(softnet_lock);
653 }
654 
655 /*
656  * IPv6 reassembling timer processing;
657  * if a timer expires on a reassembly
658  * queue, discard it.
659  */
660 void
661 frag6_slowtimo(void)
662 {
663 	struct ip6q *q6;
664 
665 	mutex_enter(softnet_lock);
666 	KERNEL_LOCK(1, NULL);
667 
668 	mutex_enter(&frag6_lock);
669 	q6 = ip6q.ip6q_next;
670 	if (q6)
671 		while (q6 != &ip6q) {
672 			--q6->ip6q_ttl;
673 			q6 = q6->ip6q_next;
674 			if (q6->ip6q_prev->ip6q_ttl == 0) {
675 				IP6_STATINC(IP6_STAT_FRAGTIMEOUT);
676 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
677 				frag6_freef(q6->ip6q_prev);
678 			}
679 		}
680 	/*
681 	 * If we are over the maximum number of fragments
682 	 * (due to the limit being lowered), drain off
683 	 * enough to get down to the new limit.
684 	 */
685 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
686 	    ip6q.ip6q_prev) {
687 		IP6_STATINC(IP6_STAT_FRAGOVERFLOW);
688 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
689 		frag6_freef(ip6q.ip6q_prev);
690 	}
691 	mutex_exit(&frag6_lock);
692 
693 	KERNEL_UNLOCK_ONE(NULL);
694 	mutex_exit(softnet_lock);
695 
696 #if 0
697 	/*
698 	 * Routing changes might produce a better route than we last used;
699 	 * make sure we notice eventually, even if forwarding only for one
700 	 * destination and the cache is never replaced.
701 	 */
702 	rtcache_free(&ip6_forward_rt);
703 	rtcache_free(&ipsrcchk_rt);
704 #endif
705 
706 }
707 
708 void
709 frag6_drainstub(void)
710 {
711 	frag6_drainwanted = 1;
712 }
713 
714 /*
715  * Drain off all datagram fragments.
716  */
717 void
718 frag6_drain(void)
719 {
720 
721 	if (mutex_tryenter(&frag6_lock)) {
722 		while (ip6q.ip6q_next != &ip6q) {
723 			IP6_STATINC(IP6_STAT_FRAGDROPPED);
724 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
725 			frag6_freef(ip6q.ip6q_next);
726 		}
727 		mutex_exit(&frag6_lock);
728 	}
729 }
730