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