xref: /dragonfly/sys/netinet6/frag6.c (revision dd491ed2)
1 /*	$FreeBSD: src/sys/netinet6/frag6.c,v 1.2.2.6 2002/04/28 05:40:26 suz Exp $	*/
2 /*	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc 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 #include <sys/thread2.h>
45 
46 #include <net/if.h>
47 #include <net/route.h>
48 #include <net/netisr2.h>
49 #include <net/netmsg2.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet/icmp6.h>
56 
57 #include <net/net_osdep.h>
58 
59 #define FRAG6_SLOWTIMO		(hz / PR_SLOWHZ)
60 
61 /*
62  * Define it to get a correct behavior on per-interface statistics.
63  * You will need to perform an extra routing table lookup, per fragment,
64  * to do it.  This may, or may not be, a performance hit.
65  */
66 #define IN6_IFSTAT_STRICT
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 static void frag6_slowtimo_dispatch (netmsg_t);
74 static void frag6_slowtimo (void *);
75 static void frag6_drain_dispatch (netmsg_t);
76 
77 /* XXX we eventually need splreass6, or some real semaphore */
78 int frag6_doing_reass;
79 u_int frag6_nfragpackets;
80 u_int frag6_nfrags;
81 struct	ip6q ip6q;	/* ip6 reassemble queue */
82 
83 /* FreeBSD tweak */
84 MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
85 
86 static struct callout		frag6_slowtimo_ch;
87 static struct netmsg_base	frag6_slowtimo_nmsg;
88 static struct netmsg_base	frag6_drain_nmsg;
89 static volatile int		frag6_draining;
90 
91 /*
92  * Initialise reassembly queue and fragment identifier.
93  */
94 void
95 frag6_init(void)
96 {
97 	struct timeval tv;
98 
99 	ip6_maxfragpackets = nmbclusters / 4;
100 	ip6_maxfrags = nmbclusters / 4;
101 
102 	/*
103 	 * in many cases, random() here does NOT return random number
104 	 * as initialization during bootstrap time occur in fixed order.
105 	 */
106 	microtime(&tv);
107 	ip6_id = krandom() ^ tv.tv_usec;
108 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
109 
110 	netmsg_init(&frag6_drain_nmsg, NULL, &netisr_adone_rport,
111 	    MSGF_PRIORITY, frag6_drain_dispatch);
112 
113 	callout_init_mp(&frag6_slowtimo_ch);
114 	netmsg_init(&frag6_slowtimo_nmsg, NULL, &netisr_adone_rport,
115 	    MSGF_PRIORITY, frag6_slowtimo_dispatch);
116 
117 	callout_reset_bycpu(&frag6_slowtimo_ch, FRAG6_SLOWTIMO,
118 	    frag6_slowtimo, NULL, 0);
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 	struct mbuf *m = *mp, *t;
157 	struct ip6_hdr *ip6;
158 	struct ip6_frag *ip6f;
159 	struct ip6q *q6;
160 	struct ip6asfrag *af6, *ip6af, *af6dwn;
161 	int offset = *offp, nxt, i, next;
162 	int first_frag = 0;
163 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
164 	struct ifnet *dstifp;
165 #ifdef IN6_IFSTAT_STRICT
166 	static struct route_in6 ro;
167 	struct sockaddr_in6 *dst;
168 #endif
169 
170 	ip6 = mtod(m, struct ip6_hdr *);
171 #ifndef PULLDOWN_TEST
172 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
173 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
174 #else
175 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
176 	if (ip6f == NULL)
177 		return IPPROTO_DONE;
178 #endif
179 
180 	dstifp = NULL;
181 #ifdef IN6_IFSTAT_STRICT
182 	/* find the destination interface of the packet. */
183 	dst = (struct sockaddr_in6 *)&ro.ro_dst;
184 	if (ro.ro_rt &&
185 	    (!(ro.ro_rt->rt_flags & RTF_UP) ||
186 	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
187 		rtfree(ro.ro_rt);
188 		ro.ro_rt = NULL;
189 	}
190 	if (ro.ro_rt == NULL) {
191 		bzero(dst, sizeof(*dst));
192 		dst->sin6_family = AF_INET6;
193 		dst->sin6_len = sizeof(struct sockaddr_in6);
194 		dst->sin6_addr = ip6->ip6_dst;
195 	}
196 	rtalloc((struct route *)&ro);
197 	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
198 		dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
199 #else
200 	/* we are violating the spec, this is not the destination interface */
201 	if (m->m_flags & M_PKTHDR)
202 		dstifp = m->m_pkthdr.rcvif;
203 #endif
204 
205 	/* jumbo payload can't contain a fragment header */
206 	if (ip6->ip6_plen == 0) {
207 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
208 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
209 		return IPPROTO_DONE;
210 	}
211 
212 	/*
213 	 * check whether fragment packet's fragment length is
214 	 * multiple of 8 octets.
215 	 * sizeof(struct ip6_frag) == 8
216 	 * sizeof(struct ip6_hdr) = 40
217 	 */
218 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
219 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
220 		icmp6_error(m, ICMP6_PARAM_PROB,
221 			    ICMP6_PARAMPROB_HEADER,
222 			    offsetof(struct ip6_hdr, ip6_plen));
223 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
224 		return IPPROTO_DONE;
225 	}
226 
227 	ip6stat.ip6s_fragments++;
228 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
229 
230 	/* offset now points to data portion */
231 	offset += sizeof(struct ip6_frag);
232 
233 	frag6_doing_reass = 1;
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 (ip6_maxfrags < 0)
241 		;
242 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
243 		goto dropfrag;
244 
245 	for (q6 = ip6q.ip6q_next; q6 != &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 			break;
250 
251 	if (q6 == &ip6q) {
252 		/*
253 		 * the first fragment to arrive, create a reassembly queue.
254 		 */
255 		first_frag = 1;
256 
257 		/*
258 		 * Enforce upper bound on number of fragmented packets
259 		 * for which we attempt reassembly;
260 		 * If maxfrag is 0, never accept fragments.
261 		 * If maxfrag is -1, accept all fragments without limitation.
262 		 */
263 		if (ip6_maxfragpackets < 0)
264 			;
265 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
266 			goto dropfrag;
267 		frag6_nfragpackets++;
268 		q6 = (struct ip6q *)kmalloc(sizeof(struct ip6q), M_FTABLE,
269 			M_NOWAIT | M_ZERO);
270 		if (q6 == NULL)
271 			goto dropfrag;
272 
273 		frag6_insque(q6, &ip6q);
274 
275 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
276 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
277 #ifdef notyet
278 		q6->ip6q_nxtp	= (u_char *)nxtp;
279 #endif
280 		q6->ip6q_ident	= ip6f->ip6f_ident;
281 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
282 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
283 		q6->ip6q_src	= ip6->ip6_src;
284 		q6->ip6q_dst	= ip6->ip6_dst;
285 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
286 		q6->ip6q_nfrag = 0;
287 	}
288 
289 	/*
290 	 * If it's the 1st fragment, record the length of the
291 	 * unfragmentable part and the next header of the fragment header.
292 	 */
293 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
294 	if (fragoff == 0) {
295 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr)
296 			- sizeof(struct ip6_frag);
297 		q6->ip6q_nxt = ip6f->ip6f_nxt;
298 	}
299 
300 	/*
301 	 * Check that the reassembled packet would not exceed 65535 bytes
302 	 * in size.
303 	 * If it would exceed, discard the fragment and return an ICMP error.
304 	 */
305 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
306 	if (q6->ip6q_unfrglen >= 0) {
307 		/* The 1st fragment has already arrived. */
308 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
309 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
310 				    offset - sizeof(struct ip6_frag) +
311 					offsetof(struct ip6_frag, ip6f_offlg));
312 			frag6_doing_reass = 0;
313 			return (IPPROTO_DONE);
314 		}
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 		frag6_doing_reass = 0;
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 				kfree(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 *)kmalloc(sizeof(struct ip6asfrag), M_FTABLE,
361 	    M_NOWAIT | M_ZERO);
362 	if (ip6af == NULL)
363 		goto dropfrag;
364 	ip6af->ip6af_head = ip6->ip6_flow;
365 	ip6af->ip6af_len = ip6->ip6_plen;
366 	ip6af->ip6af_nxt = ip6->ip6_nxt;
367 	ip6af->ip6af_hlim = ip6->ip6_hlim;
368 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
369 	ip6af->ip6af_off = fragoff;
370 	ip6af->ip6af_frglen = frgpartlen;
371 	ip6af->ip6af_offset = offset;
372 	IP6_REASS_MBUF(ip6af) = m;
373 
374 	if (first_frag) {
375 		af6 = (struct ip6asfrag *)q6;
376 		goto insert;
377 	}
378 
379 	/*
380 	 * Find a segment which begins after this one does.
381 	 */
382 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
383 	     af6 = af6->ip6af_down)
384 		if (af6->ip6af_off > ip6af->ip6af_off)
385 			break;
386 
387 	/*
388 	 * RFC 5722: Drop overlapping fragments
389 	 */
390 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
391 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
392 			- ip6af->ip6af_off;
393 		if (i > 0) {
394 			kfree(ip6af, M_FTABLE);
395 			goto dropfrag;
396 		}
397 	}
398 	if (af6 != (struct ip6asfrag *)q6) {
399 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
400 		if (i > 0) {
401 			kfree(ip6af, M_FTABLE);
402 			goto dropfrag;
403 		}
404 	}
405 
406 insert:
407 
408 	/*
409 	 * Stick new segment in its place;
410 	 * check for complete reassembly.
411 	 * Move to front of packet queue, as we are
412 	 * the most recently active fragmented packet.
413 	 */
414 	frag6_enq(ip6af, af6->ip6af_up);
415 	frag6_nfrags++;
416 	q6->ip6q_nfrag++;
417 #if 0 /* xxx */
418 	if (q6 != ip6q.ip6q_next) {
419 		frag6_remque(q6);
420 		frag6_insque(q6, &ip6q);
421 	}
422 #endif
423 	next = 0;
424 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
425 	     af6 = af6->ip6af_down) {
426 		if (af6->ip6af_off != next) {
427 			frag6_doing_reass = 0;
428 			return IPPROTO_DONE;
429 		}
430 		next += af6->ip6af_frglen;
431 	}
432 	if (af6->ip6af_up->ip6af_mff) {
433 		frag6_doing_reass = 0;
434 		return IPPROTO_DONE;
435 	}
436 
437 	/*
438 	 * Reassembly is complete; concatenate fragments.
439 	 */
440 	ip6af = q6->ip6q_down;
441 	t = m = IP6_REASS_MBUF(ip6af);
442 	af6 = ip6af->ip6af_down;
443 	frag6_deq(ip6af);
444 	while (af6 != (struct ip6asfrag *)q6) {
445 		af6dwn = af6->ip6af_down;
446 		frag6_deq(af6);
447 		while (t->m_next)
448 			t = t->m_next;
449 		t->m_next = IP6_REASS_MBUF(af6);
450 		m_adj(t->m_next, af6->ip6af_offset);
451 		kfree(af6, M_FTABLE);
452 		af6 = af6dwn;
453 	}
454 
455 	/* adjust offset to point where the original next header starts */
456 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
457 	kfree(ip6af, M_FTABLE);
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 #ifdef notyet
464 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
465 #endif
466 
467 	/*
468 	 * Delete frag6 header with as a few cost as possible.
469 	 */
470 	if (offset < m->m_len) {
471 		bcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
472 		      offset);
473 		m->m_data += sizeof(struct ip6_frag);
474 		m->m_len -= sizeof(struct ip6_frag);
475 	} else {
476 		/* this comes with no copy if the boundary is on cluster */
477 		if ((t = m_split(m, offset, M_NOWAIT)) == NULL) {
478 			frag6_remque(q6);
479 			frag6_nfrags -= q6->ip6q_nfrag;
480 			kfree(q6, M_FTABLE);
481 			frag6_nfragpackets--;
482 			goto dropfrag;
483 		}
484 		m_adj(t, sizeof(struct ip6_frag));
485 		m_cat(m, t);
486 	}
487 
488 	/*
489 	 * Store NXT to the original.
490 	 */
491 	{
492 		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
493 		*prvnxtp = nxt;
494 	}
495 
496 	frag6_remque(q6);
497 	frag6_nfrags -= q6->ip6q_nfrag;
498 	kfree(q6, M_FTABLE);
499 	frag6_nfragpackets--;
500 
501 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
502 		int plen = 0;
503 		for (t = m; t; t = t->m_next)
504 			plen += t->m_len;
505 		m->m_pkthdr.len = plen;
506 	}
507 
508 	ip6stat.ip6s_reassembled++;
509 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
510 
511 	/*
512 	 * Reassembly complete, return the next protocol.
513 	 * Be sure to clear M_HASH to force the packet
514 	 * to be re-characterized.
515 	 */
516 	m->m_flags &= ~M_HASH;
517 
518 	*mp = m;
519 	*offp = offset;
520 
521 	frag6_doing_reass = 0;
522 	return nxt;
523 
524 dropfrag:
525 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
526 	ip6stat.ip6s_fragdropped++;
527 	m_freem(m);
528 	frag6_doing_reass = 0;
529 	return IPPROTO_DONE;
530 }
531 
532 /*
533  * Free a fragment reassembly header and all
534  * associated datagrams.
535  */
536 static void
537 frag6_freef(struct ip6q *q6)
538 {
539 	struct ip6asfrag *af6, *down6;
540 
541 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
542 	     af6 = down6) {
543 		struct mbuf *m = IP6_REASS_MBUF(af6);
544 
545 		down6 = af6->ip6af_down;
546 		frag6_deq(af6);
547 
548 		/*
549 		 * Return ICMP time exceeded error for the 1st fragment.
550 		 * Just free other fragments.
551 		 */
552 		if (af6->ip6af_off == 0) {
553 			struct ip6_hdr *ip6;
554 
555 			/* adjust pointer */
556 			ip6 = mtod(m, struct ip6_hdr *);
557 
558 			/* restoure source and destination addresses */
559 			ip6->ip6_src = q6->ip6q_src;
560 			ip6->ip6_dst = q6->ip6q_dst;
561 
562 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
563 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
564 		} else
565 			m_freem(m);
566 		kfree(af6, M_FTABLE);
567 	}
568 	frag6_remque(q6);
569 	frag6_nfrags -= q6->ip6q_nfrag;
570 	kfree(q6, M_FTABLE);
571 	frag6_nfragpackets--;
572 }
573 
574 /*
575  * Put an ip fragment on a reassembly chain.
576  * Like insque, but pointers in middle of structure.
577  */
578 static void
579 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
580 {
581 	af6->ip6af_up = up6;
582 	af6->ip6af_down = up6->ip6af_down;
583 	up6->ip6af_down->ip6af_up = af6;
584 	up6->ip6af_down = af6;
585 }
586 
587 /*
588  * To frag6_enq as remque is to insque.
589  */
590 static void
591 frag6_deq(struct ip6asfrag *af6)
592 {
593 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
594 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
595 }
596 
597 static void
598 frag6_insque(struct ip6q *new, struct ip6q *old)
599 {
600 	new->ip6q_prev = old;
601 	new->ip6q_next = old->ip6q_next;
602 	old->ip6q_next->ip6q_prev= new;
603 	old->ip6q_next = new;
604 }
605 
606 static void
607 frag6_remque(struct ip6q *p6)
608 {
609 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
610 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
611 }
612 
613 /*
614  * IPv6 reassembling timer processing;
615  * if a timer expires on a reassembly
616  * queue, discard it.
617  */
618 static void
619 frag6_slowtimo_dispatch(netmsg_t nmsg)
620 {
621 	struct ip6q *q6;
622 
623 	ASSERT_NETISR0;
624 
625 	/* Reply ASAP. */
626 	crit_enter();
627 	netisr_replymsg(&nmsg->base, 0);
628 	crit_exit();
629 
630 	frag6_doing_reass = 1;
631 	q6 = ip6q.ip6q_next;
632 	if (q6)
633 		while (q6 != &ip6q) {
634 			--q6->ip6q_ttl;
635 			q6 = q6->ip6q_next;
636 			if (q6->ip6q_prev->ip6q_ttl == 0) {
637 				ip6stat.ip6s_fragtimeout++;
638 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
639 				frag6_freef(q6->ip6q_prev);
640 			}
641 		}
642 	/*
643 	 * If we are over the maximum number of fragments
644 	 * (due to the limit being lowered), drain off
645 	 * enough to get down to the new limit.
646 	 */
647 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
648 	    ip6q.ip6q_prev) {
649 		ip6stat.ip6s_fragoverflow++;
650 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
651 		frag6_freef(ip6q.ip6q_prev);
652 	}
653 	frag6_doing_reass = 0;
654 
655 #if 0
656 	/*
657 	 * Routing changes might produce a better route than we last used;
658 	 * make sure we notice eventually, even if forwarding only for one
659 	 * destination and the cache is never replaced.
660 	 */
661 	if (ip6_forward_rt.ro_rt) {
662 		RTFREE(ip6_forward_rt.ro_rt);
663 		ip6_forward_rt.ro_rt = NULL;
664 	}
665 	if (ipsrcchk_rt.ro_rt) {
666 		RTFREE(ipsrcchk_rt.ro_rt);
667 		ipsrcchk_rt.ro_rt = NULL;
668 	}
669 #endif
670 	callout_reset(&frag6_slowtimo_ch, FRAG6_SLOWTIMO, frag6_slowtimo, NULL);
671 }
672 
673 static void
674 frag6_slowtimo(void *dummy __unused)
675 {
676 	struct netmsg_base *nmsg = &frag6_slowtimo_nmsg;
677 
678 	KKASSERT(mycpuid == 0);
679 
680 	crit_enter();
681 	if (nmsg->lmsg.ms_flags & MSGF_DONE)
682 		netisr_sendmsg_oncpu(nmsg);
683 	crit_exit();
684 }
685 
686 /*
687  * Drain off all datagram fragments.
688  */
689 static void
690 frag6_drain_oncpu(void)
691 {
692 
693 	ASSERT_NETISR0;
694 
695 	if (frag6_doing_reass)
696 		return;
697 	while (ip6q.ip6q_next != &ip6q) {
698 		ip6stat.ip6s_fragdropped++;
699 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
700 		frag6_freef(ip6q.ip6q_next);
701 	}
702 }
703 
704 static void
705 frag6_drain_dispatch(netmsg_t nmsg)
706 {
707 
708 	ASSERT_NETISR0;
709 
710 	crit_enter();
711 	netisr_replymsg(&nmsg->base, 0);
712 	crit_exit();
713 
714 	frag6_drain_oncpu();
715 	frag6_draining = 0;
716 }
717 
718 static void
719 frag6_drain_ipi(void *dummy __unused)
720 {
721 	struct netmsg_base *nmsg = &frag6_drain_nmsg;
722 
723 	KKASSERT(mycpuid == 0);
724 
725 	crit_enter();
726 	if (nmsg->lmsg.ms_flags & MSGF_DONE)
727 		netisr_sendmsg_oncpu(nmsg);
728 	crit_exit();
729 }
730 
731 void
732 frag6_drain(void)
733 {
734 
735 	if (IN_NETISR(0)) {
736 		frag6_drain_oncpu();
737 		return;
738 	}
739 
740 	if (!frag6_nfrags || frag6_draining) {
741 		/* No fragments or is draining; done. */
742 		return;
743 	}
744 	frag6_draining = 1;
745 
746 	/* Target cpu0. */
747 	lwkt_send_ipiq_bycpu(0, frag6_drain_ipi, NULL);
748 }
749