xref: /freebsd/sys/netinet6/frag6.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 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 			log(LOG_ERR, "%d bytes of a fragment from %s "
395 			    "overlaps the previous fragment\n",
396 			    i, ip6_sprintf(&q6->ip6q_src));
397 			goto dropfrag;
398 		}
399 	}
400 	if (af6 != (struct ip6asfrag *)q6) {
401 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
402 		if (i > 0) {
403 			log(LOG_ERR, "%d bytes of a fragment from %s "
404 			    "overlaps the succeeding fragment",
405 			    i, ip6_sprintf(&q6->ip6q_src));
406 			goto dropfrag;
407 		}
408 	}
409 #endif
410 
411 insert:
412 
413 	/*
414 	 * Stick new segment in its place;
415 	 * check for complete reassembly.
416 	 * Move to front of packet queue, as we are
417 	 * the most recently active fragmented packet.
418 	 */
419 	frag6_enq(ip6af, af6->ip6af_up);
420 #if 0 /* xxx */
421 	if (q6 != ip6q.ip6q_next) {
422 		frag6_remque(q6);
423 		frag6_insque(q6, &ip6q);
424 	}
425 #endif
426 	next = 0;
427 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
428 	     af6 = af6->ip6af_down) {
429 		if (af6->ip6af_off != next) {
430 			frag6_doing_reass = 0;
431 			return IPPROTO_DONE;
432 		}
433 		next += af6->ip6af_frglen;
434 	}
435 	if (af6->ip6af_up->ip6af_mff) {
436 		frag6_doing_reass = 0;
437 		return IPPROTO_DONE;
438 	}
439 
440 	/*
441 	 * Reassembly is complete; concatenate fragments.
442 	 */
443 	ip6af = q6->ip6q_down;
444 	t = m = IP6_REASS_MBUF(ip6af);
445 	af6 = ip6af->ip6af_down;
446 	frag6_deq(ip6af);
447 	while (af6 != (struct ip6asfrag *)q6) {
448 		af6dwn = af6->ip6af_down;
449 		frag6_deq(af6);
450 		while (t->m_next)
451 			t = t->m_next;
452 		t->m_next = IP6_REASS_MBUF(af6);
453 		m_adj(t->m_next, af6->ip6af_offset);
454 		free(af6, M_FTABLE);
455 		af6 = af6dwn;
456 	}
457 
458 	/* adjust offset to point where the original next header starts */
459 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
460 	free(ip6af, M_FTABLE);
461 	ip6 = mtod(m, struct ip6_hdr *);
462 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
463 	ip6->ip6_src = q6->ip6q_src;
464 	ip6->ip6_dst = q6->ip6q_dst;
465 	nxt = q6->ip6q_nxt;
466 #ifdef notyet
467 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
468 #endif
469 
470 	/*
471 	 * Delete frag6 header with as a few cost as possible.
472 	 */
473 	if (offset < m->m_len) {
474 		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
475 			offset);
476 		m->m_data += sizeof(struct ip6_frag);
477 		m->m_len -= sizeof(struct ip6_frag);
478 	} else {
479 		/* this comes with no copy if the boundary is on cluster */
480 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
481 			frag6_remque(q6);
482 			free(q6, M_FTABLE);
483 			frag6_nfragpackets--;
484 			goto dropfrag;
485 		}
486 		m_adj(t, sizeof(struct ip6_frag));
487 		m_cat(m, t);
488 	}
489 
490 	/*
491 	 * Store NXT to the original.
492 	 */
493 	{
494 		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
495 		*prvnxtp = nxt;
496 	}
497 
498 	frag6_remque(q6);
499 	free(q6, M_FTABLE);
500 	frag6_nfragpackets--;
501 
502 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
503 		int plen = 0;
504 		for (t = m; t; t = t->m_next)
505 			plen += t->m_len;
506 		m->m_pkthdr.len = plen;
507 	}
508 
509 	ip6stat.ip6s_reassembled++;
510 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
511 
512 	/*
513 	 * Tell launch routine the next header
514 	 */
515 
516 	*mp = m;
517 	*offp = offset;
518 
519 	frag6_doing_reass = 0;
520 	return nxt;
521 
522  dropfrag:
523 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
524 	ip6stat.ip6s_fragdropped++;
525 	m_freem(m);
526 	return IPPROTO_DONE;
527 }
528 
529 /*
530  * Free a fragment reassembly header and all
531  * associated datagrams.
532  */
533 void
534 frag6_freef(q6)
535 	struct ip6q *q6;
536 {
537 	struct ip6asfrag *af6, *down6;
538 
539 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
540 	     af6 = down6) {
541 		struct mbuf *m = IP6_REASS_MBUF(af6);
542 
543 		down6 = af6->ip6af_down;
544 		frag6_deq(af6);
545 
546 		/*
547 		 * Return ICMP time exceeded error for the 1st fragment.
548 		 * Just free other fragments.
549 		 */
550 		if (af6->ip6af_off == 0) {
551 			struct ip6_hdr *ip6;
552 
553 			/* adjust pointer */
554 			ip6 = mtod(m, struct ip6_hdr *);
555 
556 			/* restoure source and destination addresses */
557 			ip6->ip6_src = q6->ip6q_src;
558 			ip6->ip6_dst = q6->ip6q_dst;
559 
560 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
561 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
562 		} else
563 			m_freem(m);
564 		free(af6, M_FTABLE);
565 	}
566 	frag6_remque(q6);
567 	free(q6, M_FTABLE);
568 	frag6_nfragpackets--;
569 }
570 
571 /*
572  * Put an ip fragment on a reassembly chain.
573  * Like insque, but pointers in middle of structure.
574  */
575 void
576 frag6_enq(af6, up6)
577 	struct ip6asfrag *af6, *up6;
578 {
579 	af6->ip6af_up = up6;
580 	af6->ip6af_down = up6->ip6af_down;
581 	up6->ip6af_down->ip6af_up = af6;
582 	up6->ip6af_down = af6;
583 }
584 
585 /*
586  * To frag6_enq as remque is to insque.
587  */
588 void
589 frag6_deq(af6)
590 	struct ip6asfrag *af6;
591 {
592 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
593 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
594 }
595 
596 void
597 frag6_insque(new, old)
598 	struct ip6q *new, *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 void
607 frag6_remque(p6)
608 	struct ip6q *p6;
609 {
610 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
611 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
612 }
613 
614 /*
615  * IP timer processing;
616  * if a timer expires on a reassembly
617  * queue, discard it.
618  */
619 void
620 frag6_slowtimo()
621 {
622 	struct ip6q *q6;
623 	int s = splnet();
624 #if 0
625 	extern struct	route_in6 ip6_forward_rt;
626 #endif
627 
628 	frag6_doing_reass = 1;
629 	q6 = ip6q.ip6q_next;
630 	if (q6)
631 		while (q6 != &ip6q) {
632 			--q6->ip6q_ttl;
633 			q6 = q6->ip6q_next;
634 			if (q6->ip6q_prev->ip6q_ttl == 0) {
635 				ip6stat.ip6s_fragtimeout++;
636 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
637 				frag6_freef(q6->ip6q_prev);
638 			}
639 		}
640 	/*
641 	 * If we are over the maximum number of fragments
642 	 * (due to the limit being lowered), drain off
643 	 * enough to get down to the new limit.
644 	 */
645 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets) {
646 		ip6stat.ip6s_fragoverflow++;
647 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
648 		frag6_freef(ip6q.ip6q_prev);
649 	}
650 	frag6_doing_reass = 0;
651 
652 #if 0
653 	/*
654 	 * Routing changes might produce a better route than we last used;
655 	 * make sure we notice eventually, even if forwarding only for one
656 	 * destination and the cache is never replaced.
657 	 */
658 	if (ip6_forward_rt.ro_rt) {
659 		RTFREE(ip6_forward_rt.ro_rt);
660 		ip6_forward_rt.ro_rt = 0;
661 	}
662 	if (ipsrcchk_rt.ro_rt) {
663 		RTFREE(ipsrcchk_rt.ro_rt);
664 		ipsrcchk_rt.ro_rt = 0;
665 	}
666 #endif
667 
668 	splx(s);
669 }
670 
671 /*
672  * Drain off all datagram fragments.
673  */
674 void
675 frag6_drain()
676 {
677 	if (frag6_doing_reass)
678 		return;
679 	while (ip6q.ip6q_next != &ip6q) {
680 		ip6stat.ip6s_fragdropped++;
681 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
682 		frag6_freef(ip6q.ip6q_next);
683 	}
684 }
685