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