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