xref: /netbsd-src/sys/netinet6/frag6.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: frag6.c,v 1.16 2001/11/13 00:56:57 lukem Exp $	*/
2 /*	$KAME: frag6.c,v 1.31 2001/05/17 13:45:34 jinmei 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.16 2001/11/13 00:56:57 lukem 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/errno.h>
44 #include <sys/time.h>
45 #include <sys/kernel.h>
46 #include <sys/syslog.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/in6_pcb.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet/icmp6.h>
57 
58 #include <net/net_osdep.h>
59 
60 /*
61  * Define it to get a correct behavior on per-interface statistics.
62  * You will need to perform an extra routing table lookup, per fragment,
63  * to do it.  This may, or may not be, a performance hit.
64  */
65 #define IN6_IFSTAT_STRICT
66 
67 static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
68 static void frag6_deq __P((struct ip6asfrag *));
69 static void frag6_insque __P((struct ip6q *, struct ip6q *));
70 static void frag6_remque __P((struct ip6q *));
71 static void frag6_freef __P((struct ip6q *));
72 
73 /* XXX we eventually need splreass6, or some real semaphore */
74 int frag6_doing_reass;
75 u_int frag6_nfragpackets;
76 struct	ip6q ip6q;	/* ip6 reassemble queue */
77 
78 #ifndef offsetof		/* XXX */
79 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
80 #endif
81 
82 /*
83  * Initialise reassembly queue and fragment identifier.
84  */
85 void
86 frag6_init()
87 {
88 	struct timeval tv;
89 
90 	/*
91 	 * in many cases, random() here does NOT return random number
92 	 * as initialization during bootstrap time occur in fixed order.
93 	 */
94 	microtime(&tv);
95 	ip6_id = random() ^ tv.tv_usec;
96 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
97 }
98 
99 /*
100  * In RFC2460, fragment and reassembly rule do not agree with each other,
101  * in terms of next header field handling in fragment header.
102  * While the sender will use the same value for all of the fragmented packets,
103  * receiver is suggested not to check the consistency.
104  *
105  * fragment rule (p20):
106  *	(2) A Fragment header containing:
107  *	The Next Header value that identifies the first header of
108  *	the Fragmentable Part of the original packet.
109  *		-> next header field is same for all fragments
110  *
111  * reassembly rule (p21):
112  *	The Next Header field of the last header of the Unfragmentable
113  *	Part is obtained from the Next Header field of the first
114  *	fragment's Fragment header.
115  *		-> should grab it from the first fragment only
116  *
117  * The following note also contradicts with fragment rule - noone is going to
118  * send different fragment with different next header field.
119  *
120  * additional note (p22):
121  *	The Next Header values in the Fragment headers of different
122  *	fragments of the same original packet may differ.  Only the value
123  *	from the Offset zero fragment packet is used for reassembly.
124  *		-> should grab it from the first fragment only
125  *
126  * There is no explicit reason given in the RFC.  Historical reason maybe?
127  */
128 /*
129  * Fragment input
130  */
131 int
132 frag6_input(mp, offp, proto)
133 	struct mbuf **mp;
134 	int *offp, proto;
135 {
136 	struct mbuf *m = *mp, *t;
137 	struct ip6_hdr *ip6;
138 	struct ip6_frag *ip6f;
139 	struct ip6q *q6;
140 	struct ip6asfrag *af6, *ip6af, *af6dwn;
141 	int offset = *offp, nxt, i, next;
142 	int first_frag = 0;
143 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
144 	struct ifnet *dstifp;
145 #ifdef IN6_IFSTAT_STRICT
146 	static struct route_in6 ro;
147 	struct sockaddr_in6 *dst;
148 #endif
149 
150 	ip6 = mtod(m, struct ip6_hdr *);
151 #ifndef PULLDOWN_TEST
152 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
153 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
154 #else
155 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
156 	if (ip6f == NULL)
157 		return IPPROTO_DONE;
158 #endif
159 
160 	dstifp = NULL;
161 #ifdef IN6_IFSTAT_STRICT
162 	/* find the destination interface of the packet. */
163 	dst = (struct sockaddr_in6 *)&ro.ro_dst;
164 	if (ro.ro_rt
165 	 && ((ro.ro_rt->rt_flags & RTF_UP) == 0
166 	  || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
167 		RTFREE(ro.ro_rt);
168 		ro.ro_rt = (struct rtentry *)0;
169 	}
170 	if (ro.ro_rt == NULL) {
171 		bzero(dst, sizeof(*dst));
172 		dst->sin6_family = AF_INET6;
173 		dst->sin6_len = sizeof(struct sockaddr_in6);
174 		dst->sin6_addr = ip6->ip6_dst;
175 	}
176 	rtalloc((struct route *)&ro);
177 	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
178 		dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
179 #else
180 	/* we are violating the spec, this is not the destination interface */
181 	if ((m->m_flags & M_PKTHDR) != 0)
182 		dstifp = m->m_pkthdr.rcvif;
183 #endif
184 
185 	/* jumbo payload can't contain a fragment header */
186 	if (ip6->ip6_plen == 0) {
187 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
188 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
189 		return IPPROTO_DONE;
190 	}
191 
192 	/*
193 	 * check whether fragment packet's fragment length is
194 	 * multiple of 8 octets.
195 	 * sizeof(struct ip6_frag) == 8
196 	 * sizeof(struct ip6_hdr) = 40
197 	 */
198 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
199 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
200 		icmp6_error(m, ICMP6_PARAM_PROB,
201 			    ICMP6_PARAMPROB_HEADER,
202 			    offsetof(struct ip6_hdr, ip6_plen));
203 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
204 		return IPPROTO_DONE;
205 	}
206 
207 	ip6stat.ip6s_fragments++;
208 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
209 
210 	/* offset now points to data portion */
211 	offset += sizeof(struct ip6_frag);
212 
213 	frag6_doing_reass = 1;
214 
215 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
216 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
217 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
218 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
219 			break;
220 
221 	if (q6 == &ip6q) {
222 		/*
223 		 * the first fragment to arrive, create a reassembly queue.
224 		 */
225 		first_frag = 1;
226 
227 		/*
228 		 * Enforce upper bound on number of fragmented packets
229 		 * for which we attempt reassembly;
230 		 * If maxfrag is 0, never accept fragments.
231 		 * If maxfrag is -1, accept all fragments without limitation.
232 		 */
233 		if (ip6_maxfragpackets < 0)
234 			;
235 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
236 			goto dropfrag;
237 		frag6_nfragpackets++;
238 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
239 			M_DONTWAIT);
240 		if (q6 == NULL)
241 			goto dropfrag;
242 		bzero(q6, sizeof(*q6));
243 
244 		frag6_insque(q6, &ip6q);
245 
246 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
247 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
248 #ifdef notyet
249 		q6->ip6q_nxtp	= (u_char *)nxtp;
250 #endif
251 		q6->ip6q_ident	= ip6f->ip6f_ident;
252 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
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 	}
258 
259 	/*
260 	 * If it's the 1st fragment, record the length of the
261 	 * unfragmentable part and the next header of the fragment header.
262 	 */
263 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
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 			frag6_doing_reass = 0;
283 			return(IPPROTO_DONE);
284 		}
285 	}
286 	else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
287 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
288 			    offset - sizeof(struct ip6_frag) +
289 				offsetof(struct ip6_frag, ip6f_offlg));
290 		frag6_doing_reass = 0;
291 		return(IPPROTO_DONE);
292 	}
293 	/*
294 	 * If it's the first fragment, do the above check for each
295 	 * fragment already stored in the reassembly queue.
296 	 */
297 	if (fragoff == 0) {
298 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
299 		     af6 = af6dwn) {
300 			af6dwn = af6->ip6af_down;
301 
302 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
303 			    IPV6_MAXPACKET) {
304 				struct mbuf *merr = IP6_REASS_MBUF(af6);
305 				struct ip6_hdr *ip6err;
306 				int erroff = af6->ip6af_offset;
307 
308 				/* dequeue the fragment. */
309 				frag6_deq(af6);
310 				free(af6, M_FTABLE);
311 
312 				/* adjust pointer. */
313 				ip6err = mtod(merr, struct ip6_hdr *);
314 
315 				/*
316 				 * Restore source and destination addresses
317 				 * in the erroneous IPv6 header.
318 				 */
319 				ip6err->ip6_src = q6->ip6q_src;
320 				ip6err->ip6_dst = q6->ip6q_dst;
321 
322 				icmp6_error(merr, ICMP6_PARAM_PROB,
323 					    ICMP6_PARAMPROB_HEADER,
324 					    erroff - sizeof(struct ip6_frag) +
325 						offsetof(struct ip6_frag, ip6f_offlg));
326 			}
327 		}
328 	}
329 
330 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
331 	    M_DONTWAIT);
332 	if (ip6af == NULL)
333 		goto dropfrag;
334 	bzero(ip6af, sizeof(*ip6af));
335 	ip6af->ip6af_head = ip6->ip6_flow;
336 	ip6af->ip6af_len = ip6->ip6_plen;
337 	ip6af->ip6af_nxt = ip6->ip6_nxt;
338 	ip6af->ip6af_hlim = ip6->ip6_hlim;
339 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
340 	ip6af->ip6af_off = fragoff;
341 	ip6af->ip6af_frglen = frgpartlen;
342 	ip6af->ip6af_offset = offset;
343 	IP6_REASS_MBUF(ip6af) = m;
344 
345 	if (first_frag) {
346 		af6 = (struct ip6asfrag *)q6;
347 		goto insert;
348 	}
349 
350 	/*
351 	 * Find a segment which begins after this one does.
352 	 */
353 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
354 	     af6 = af6->ip6af_down)
355 		if (af6->ip6af_off > ip6af->ip6af_off)
356 			break;
357 
358 #if 0
359 	/*
360 	 * If there is a preceding segment, it may provide some of
361 	 * our data already.  If so, drop the data from the incoming
362 	 * segment.  If it provides all of our data, drop us.
363 	 */
364 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
365 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
366 			- ip6af->ip6af_off;
367 		if (i > 0) {
368 			if (i >= ip6af->ip6af_frglen)
369 				goto dropfrag;
370 			m_adj(IP6_REASS_MBUF(ip6af), i);
371 			ip6af->ip6af_off += i;
372 			ip6af->ip6af_frglen -= i;
373 		}
374 	}
375 
376 	/*
377 	 * While we overlap succeeding segments trim them or,
378 	 * if they are completely covered, dequeue them.
379 	 */
380 	while (af6 != (struct ip6asfrag *)q6 &&
381 	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
382 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
383 		if (i < af6->ip6af_frglen) {
384 			af6->ip6af_frglen -= i;
385 			af6->ip6af_off += i;
386 			m_adj(IP6_REASS_MBUF(af6), i);
387 			break;
388 		}
389 		af6 = af6->ip6af_down;
390 		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
391 		frag6_deq(af6->ip6af_up);
392 	}
393 #else
394 	/*
395 	 * If the incoming framgent overlaps some existing fragments in
396 	 * the reassembly queue, drop it, since it is dangerous to override
397 	 * existing fragments from a security point of view.
398 	 */
399 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
400 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
401 			- ip6af->ip6af_off;
402 		if (i > 0) {
403 #if 0				/* suppress the noisy log */
404 			log(LOG_ERR, "%d bytes of a fragment from %s "
405 			    "overlaps the previous fragment\n",
406 			    i, ip6_sprintf(&q6->ip6q_src));
407 #endif
408 			free(ip6af, M_FTABLE);
409 			goto dropfrag;
410 		}
411 	}
412 	if (af6 != (struct ip6asfrag *)q6) {
413 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
414 		if (i > 0) {
415 #if 0				/* suppress the noisy log */
416 			log(LOG_ERR, "%d bytes of a fragment from %s "
417 			    "overlaps the succeeding fragment",
418 			    i, ip6_sprintf(&q6->ip6q_src));
419 #endif
420 			free(ip6af, M_FTABLE);
421 			goto dropfrag;
422 		}
423 	}
424 #endif
425 
426 insert:
427 
428 	/*
429 	 * Stick new segment in its place;
430 	 * check for complete reassembly.
431 	 * Move to front of packet queue, as we are
432 	 * the most recently active fragmented packet.
433 	 */
434 	frag6_enq(ip6af, af6->ip6af_up);
435 #if 0 /* xxx */
436 	if (q6 != ip6q.ip6q_next) {
437 		frag6_remque(q6);
438 		frag6_insque(q6, &ip6q);
439 	}
440 #endif
441 	next = 0;
442 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
443 	     af6 = af6->ip6af_down) {
444 		if (af6->ip6af_off != next) {
445 			frag6_doing_reass = 0;
446 			return IPPROTO_DONE;
447 		}
448 		next += af6->ip6af_frglen;
449 	}
450 	if (af6->ip6af_up->ip6af_mff) {
451 		frag6_doing_reass = 0;
452 		return IPPROTO_DONE;
453 	}
454 
455 	/*
456 	 * Reassembly is complete; concatenate fragments.
457 	 */
458 	ip6af = q6->ip6q_down;
459 	t = m = IP6_REASS_MBUF(ip6af);
460 	af6 = ip6af->ip6af_down;
461 	frag6_deq(ip6af);
462 	while (af6 != (struct ip6asfrag *)q6) {
463 		af6dwn = af6->ip6af_down;
464 		frag6_deq(af6);
465 		while (t->m_next)
466 			t = t->m_next;
467 		t->m_next = IP6_REASS_MBUF(af6);
468 		m_adj(t->m_next, af6->ip6af_offset);
469 		free(af6, M_FTABLE);
470 		af6 = af6dwn;
471 	}
472 
473 	/* adjust offset to point where the original next header starts */
474 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
475 	free(ip6af, M_FTABLE);
476 	ip6 = mtod(m, struct ip6_hdr *);
477 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
478 	ip6->ip6_src = q6->ip6q_src;
479 	ip6->ip6_dst = q6->ip6q_dst;
480 	nxt = q6->ip6q_nxt;
481 #ifdef notyet
482 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
483 #endif
484 
485 	/*
486 	 * Delete frag6 header with as a few cost as possible.
487 	 */
488 	if (offset < m->m_len) {
489 		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
490 			offset);
491 		m->m_data += sizeof(struct ip6_frag);
492 		m->m_len -= sizeof(struct ip6_frag);
493 	} else {
494 		/* this comes with no copy if the boundary is on cluster */
495 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
496 			frag6_remque(q6);
497 			free(q6, M_FTABLE);
498 			frag6_nfragpackets--;
499 			goto dropfrag;
500 		}
501 		m_adj(t, sizeof(struct ip6_frag));
502 		m_cat(m, t);
503 	}
504 
505 	/*
506 	 * Store NXT to the original.
507 	 */
508 	{
509 		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
510 		*prvnxtp = nxt;
511 	}
512 
513 	frag6_remque(q6);
514 	free(q6, M_FTABLE);
515 	frag6_nfragpackets--;
516 
517 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
518 		int plen = 0;
519 		for (t = m; t; t = t->m_next)
520 			plen += t->m_len;
521 		m->m_pkthdr.len = plen;
522 	}
523 
524 	ip6stat.ip6s_reassembled++;
525 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
526 
527 	/*
528 	 * Tell launch routine the next header
529 	 */
530 
531 	*mp = m;
532 	*offp = offset;
533 
534 	frag6_doing_reass = 0;
535 	return nxt;
536 
537  dropfrag:
538 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
539 	ip6stat.ip6s_fragdropped++;
540 	m_freem(m);
541 	frag6_doing_reass = 0;
542 	return IPPROTO_DONE;
543 }
544 
545 /*
546  * Free a fragment reassembly header and all
547  * associated datagrams.
548  */
549 void
550 frag6_freef(q6)
551 	struct ip6q *q6;
552 {
553 	struct ip6asfrag *af6, *down6;
554 
555 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
556 	     af6 = down6) {
557 		struct mbuf *m = IP6_REASS_MBUF(af6);
558 
559 		down6 = af6->ip6af_down;
560 		frag6_deq(af6);
561 
562 		/*
563 		 * Return ICMP time exceeded error for the 1st fragment.
564 		 * Just free other fragments.
565 		 */
566 		if (af6->ip6af_off == 0) {
567 			struct ip6_hdr *ip6;
568 
569 			/* adjust pointer */
570 			ip6 = mtod(m, struct ip6_hdr *);
571 
572 			/* restoure source and destination addresses */
573 			ip6->ip6_src = q6->ip6q_src;
574 			ip6->ip6_dst = q6->ip6q_dst;
575 
576 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
577 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
578 		} else
579 			m_freem(m);
580 		free(af6, M_FTABLE);
581 	}
582 	frag6_remque(q6);
583 	free(q6, M_FTABLE);
584 	frag6_nfragpackets--;
585 }
586 
587 /*
588  * Put an ip fragment on a reassembly chain.
589  * Like insque, but pointers in middle of structure.
590  */
591 void
592 frag6_enq(af6, up6)
593 	struct ip6asfrag *af6, *up6;
594 {
595 	af6->ip6af_up = up6;
596 	af6->ip6af_down = up6->ip6af_down;
597 	up6->ip6af_down->ip6af_up = af6;
598 	up6->ip6af_down = af6;
599 }
600 
601 /*
602  * To frag6_enq as remque is to insque.
603  */
604 void
605 frag6_deq(af6)
606 	struct ip6asfrag *af6;
607 {
608 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
609 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
610 }
611 
612 void
613 frag6_insque(new, old)
614 	struct ip6q *new, *old;
615 {
616 	new->ip6q_prev = old;
617 	new->ip6q_next = old->ip6q_next;
618 	old->ip6q_next->ip6q_prev= new;
619 	old->ip6q_next = new;
620 }
621 
622 void
623 frag6_remque(p6)
624 	struct ip6q *p6;
625 {
626 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
627 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
628 }
629 
630 /*
631  * IPv6 reassembling timer processing;
632  * if a timer expires on a reassembly
633  * queue, discard it.
634  */
635 void
636 frag6_slowtimo()
637 {
638 	struct ip6q *q6;
639 	int s = splsoftnet();
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 = 0;
675 	}
676 	if (ipsrcchk_rt.ro_rt) {
677 		RTFREE(ipsrcchk_rt.ro_rt);
678 		ipsrcchk_rt.ro_rt = 0;
679 	}
680 #endif
681 
682 	splx(s);
683 }
684 
685 /*
686  * Drain off all datagram fragments.
687  */
688 void
689 frag6_drain()
690 {
691 	if (frag6_doing_reass)
692 		return;
693 	while (ip6q.ip6q_next != &ip6q) {
694 		ip6stat.ip6s_fragdropped++;
695 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
696 		frag6_freef(ip6q.ip6q_next);
697 	}
698 }
699