xref: /dragonfly/sys/netinet6/frag6.c (revision 556932ec)
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 
459 	/*
460 	 * Make sure completed packet does not overflow the max packet length.
461 	 */
462 	if ((u_int)next + (u_int)offset - sizeof(struct ip6_hdr) >
463 	    IPV6_MAXPACKET)
464 	{
465 		frag6_freef(q6);
466 		goto dropfrag;
467 	}
468 
469 	ip6 = mtod(m, struct ip6_hdr *);
470 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
471 	ip6->ip6_src = q6->ip6q_src;
472 	ip6->ip6_dst = q6->ip6q_dst;
473 	nxt = q6->ip6q_nxt;
474 #ifdef notyet
475 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
476 #endif
477 
478 	/*
479 	 * Delete frag6 header with as a few cost as possible.
480 	 */
481 	if (offset < m->m_len) {
482 		bcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
483 		      offset);
484 		m->m_data += sizeof(struct ip6_frag);
485 		m->m_len -= sizeof(struct ip6_frag);
486 	} else {
487 		/* this comes with no copy if the boundary is on cluster */
488 		if ((t = m_split(m, offset, M_NOWAIT)) == NULL) {
489 			frag6_remque(q6);
490 			frag6_nfrags -= q6->ip6q_nfrag;
491 			kfree(q6, M_FTABLE);
492 			frag6_nfragpackets--;
493 			goto dropfrag;
494 		}
495 		m_adj(t, sizeof(struct ip6_frag));
496 		m_cat(m, t);
497 	}
498 
499 	/*
500 	 * Store NXT to the original.
501 	 */
502 	{
503 		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
504 		*prvnxtp = nxt;
505 	}
506 
507 	frag6_remque(q6);
508 	frag6_nfrags -= q6->ip6q_nfrag;
509 	kfree(q6, M_FTABLE);
510 	frag6_nfragpackets--;
511 
512 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
513 		int plen = 0;
514 		for (t = m; t; t = t->m_next)
515 			plen += t->m_len;
516 		m->m_pkthdr.len = plen;
517 	}
518 
519 	ip6stat.ip6s_reassembled++;
520 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
521 
522 	/*
523 	 * Reassembly complete, return the next protocol.
524 	 * Be sure to clear M_HASH to force the packet
525 	 * to be re-characterized.
526 	 */
527 	m->m_flags &= ~M_HASH;
528 
529 	*mp = m;
530 	*offp = offset;
531 
532 	frag6_doing_reass = 0;
533 	return nxt;
534 
535 dropfrag:
536 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
537 	ip6stat.ip6s_fragdropped++;
538 	m_freem(m);
539 	frag6_doing_reass = 0;
540 	return IPPROTO_DONE;
541 }
542 
543 /*
544  * Free a fragment reassembly header and all
545  * associated datagrams.
546  */
547 static void
548 frag6_freef(struct ip6q *q6)
549 {
550 	struct ip6asfrag *af6, *down6;
551 
552 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
553 	     af6 = down6) {
554 		struct mbuf *m = IP6_REASS_MBUF(af6);
555 
556 		down6 = af6->ip6af_down;
557 		frag6_deq(af6);
558 
559 		/*
560 		 * Return ICMP time exceeded error for the 1st fragment.
561 		 * Just free other fragments.
562 		 */
563 		if (af6->ip6af_off == 0) {
564 			struct ip6_hdr *ip6;
565 
566 			/* adjust pointer */
567 			ip6 = mtod(m, struct ip6_hdr *);
568 
569 			/* restoure source and destination addresses */
570 			ip6->ip6_src = q6->ip6q_src;
571 			ip6->ip6_dst = q6->ip6q_dst;
572 
573 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
574 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
575 		} else
576 			m_freem(m);
577 		kfree(af6, M_FTABLE);
578 	}
579 	frag6_remque(q6);
580 	frag6_nfrags -= q6->ip6q_nfrag;
581 	kfree(q6, M_FTABLE);
582 	frag6_nfragpackets--;
583 }
584 
585 /*
586  * Put an ip fragment on a reassembly chain.
587  * Like insque, but pointers in middle of structure.
588  */
589 static void
590 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
591 {
592 	af6->ip6af_up = up6;
593 	af6->ip6af_down = up6->ip6af_down;
594 	up6->ip6af_down->ip6af_up = af6;
595 	up6->ip6af_down = af6;
596 }
597 
598 /*
599  * To frag6_enq as remque is to insque.
600  */
601 static void
602 frag6_deq(struct ip6asfrag *af6)
603 {
604 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
605 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
606 }
607 
608 static void
609 frag6_insque(struct ip6q *new, struct ip6q *old)
610 {
611 	new->ip6q_prev = old;
612 	new->ip6q_next = old->ip6q_next;
613 	old->ip6q_next->ip6q_prev= new;
614 	old->ip6q_next = new;
615 }
616 
617 static void
618 frag6_remque(struct ip6q *p6)
619 {
620 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
621 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
622 }
623 
624 /*
625  * IPv6 reassembling timer processing;
626  * if a timer expires on a reassembly
627  * queue, discard it.
628  */
629 static void
630 frag6_slowtimo_dispatch(netmsg_t nmsg)
631 {
632 	struct ip6q *q6;
633 
634 	ASSERT_NETISR0;
635 
636 	/* Reply ASAP. */
637 	crit_enter();
638 	netisr_replymsg(&nmsg->base, 0);
639 	crit_exit();
640 
641 	frag6_doing_reass = 1;
642 	q6 = ip6q.ip6q_next;
643 	if (q6)
644 		while (q6 != &ip6q) {
645 			--q6->ip6q_ttl;
646 			q6 = q6->ip6q_next;
647 			if (q6->ip6q_prev->ip6q_ttl == 0) {
648 				ip6stat.ip6s_fragtimeout++;
649 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
650 				frag6_freef(q6->ip6q_prev);
651 			}
652 		}
653 	/*
654 	 * If we are over the maximum number of fragments
655 	 * (due to the limit being lowered), drain off
656 	 * enough to get down to the new limit.
657 	 */
658 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
659 	    ip6q.ip6q_prev) {
660 		ip6stat.ip6s_fragoverflow++;
661 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
662 		frag6_freef(ip6q.ip6q_prev);
663 	}
664 	frag6_doing_reass = 0;
665 
666 #if 0
667 	/*
668 	 * Routing changes might produce a better route than we last used;
669 	 * make sure we notice eventually, even if forwarding only for one
670 	 * destination and the cache is never replaced.
671 	 */
672 	if (ip6_forward_rt.ro_rt) {
673 		RTFREE(ip6_forward_rt.ro_rt);
674 		ip6_forward_rt.ro_rt = NULL;
675 	}
676 	if (ipsrcchk_rt.ro_rt) {
677 		RTFREE(ipsrcchk_rt.ro_rt);
678 		ipsrcchk_rt.ro_rt = NULL;
679 	}
680 #endif
681 	callout_reset(&frag6_slowtimo_ch, FRAG6_SLOWTIMO, frag6_slowtimo, NULL);
682 }
683 
684 static void
685 frag6_slowtimo(void *dummy __unused)
686 {
687 	struct netmsg_base *nmsg = &frag6_slowtimo_nmsg;
688 
689 	KKASSERT(mycpuid == 0);
690 
691 	crit_enter();
692 	if (nmsg->lmsg.ms_flags & MSGF_DONE)
693 		netisr_sendmsg_oncpu(nmsg);
694 	crit_exit();
695 }
696 
697 /*
698  * Drain off all datagram fragments.
699  */
700 static void
701 frag6_drain_oncpu(void)
702 {
703 
704 	ASSERT_NETISR0;
705 
706 	if (frag6_doing_reass)
707 		return;
708 	while (ip6q.ip6q_next != &ip6q) {
709 		ip6stat.ip6s_fragdropped++;
710 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
711 		frag6_freef(ip6q.ip6q_next);
712 	}
713 }
714 
715 static void
716 frag6_drain_dispatch(netmsg_t nmsg)
717 {
718 
719 	ASSERT_NETISR0;
720 
721 	crit_enter();
722 	netisr_replymsg(&nmsg->base, 0);
723 	crit_exit();
724 
725 	frag6_drain_oncpu();
726 	frag6_draining = 0;
727 }
728 
729 static void
730 frag6_drain_ipi(void *dummy __unused)
731 {
732 	struct netmsg_base *nmsg = &frag6_drain_nmsg;
733 
734 	KKASSERT(mycpuid == 0);
735 
736 	crit_enter();
737 	if (nmsg->lmsg.ms_flags & MSGF_DONE)
738 		netisr_sendmsg_oncpu(nmsg);
739 	crit_exit();
740 }
741 
742 void
743 frag6_drain(void)
744 {
745 
746 	if (IN_NETISR(0)) {
747 		frag6_drain_oncpu();
748 		return;
749 	}
750 
751 	if (!frag6_nfrags || frag6_draining) {
752 		/* No fragments or is draining; done. */
753 		return;
754 	}
755 	frag6_draining = 1;
756 
757 	/* Target cpu0. */
758 	lwkt_send_ipiq_bycpu(0, frag6_drain_ipi, NULL);
759 }
760