xref: /openbsd-src/sys/netinet6/frag6.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: frag6.c,v 1.11 2001/06/09 06:43:37 angelos 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/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 <dev/rndvar.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 #ifndef offsetof		/* XXX */
75 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
76 #endif
77 
78 /*
79  * Initialise reassembly queue and fragment identifier.
80  */
81 void
82 frag6_init()
83 {
84 	ip6_id = arc4random();
85 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
86 }
87 
88 /*
89  * In RFC2460, fragment and reassembly rule do not agree with each other,
90  * in terms of next header field handling in fragment header.
91  * While the sender will use the same value for all of the fragmented packets,
92  * receiver is suggested not to check the consistency.
93  *
94  * fragment rule (p20):
95  *	(2) A Fragment header containing:
96  *	The Next Header value that identifies the first header of
97  *	the Fragmentable Part of the original packet.
98  *		-> next header field is same for all fragments
99  *
100  * reassembly rule (p21):
101  *	The Next Header field of the last header of the Unfragmentable
102  *	Part is obtained from the Next Header field of the first
103  *	fragment's Fragment header.
104  *		-> should grab it from the first fragment only
105  *
106  * The following note also contradicts with fragment rule - noone is going to
107  * send different fragment with different next header field.
108  *
109  * additional note (p22):
110  *	The Next Header values in the Fragment headers of different
111  *	fragments of the same original packet may differ.  Only the value
112  *	from the Offset zero fragment packet is used for reassembly.
113  *		-> should grab it from the first fragment only
114  *
115  * There is no explicit reason given in the RFC.  Historical reason maybe?
116  */
117 /*
118  * Fragment input
119  */
120 int
121 frag6_input(mp, offp, proto)
122 	struct mbuf **mp;
123 	int *offp, proto;
124 {
125 	struct mbuf *m = *mp, *t;
126 	struct ip6_hdr *ip6;
127 	struct ip6_frag *ip6f;
128 	struct ip6q *q6;
129 	struct ip6asfrag *af6, *ip6af, *af6dwn;
130 	int offset = *offp, nxt, i, next;
131 	int first_frag = 0;
132 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
133 	struct ifnet *dstifp;
134 #ifdef IN6_IFSTAT_STRICT
135 	static struct route_in6 ro;
136 	struct sockaddr_in6 *dst;
137 #endif
138 
139 	ip6 = mtod(m, struct ip6_hdr *);
140 #ifndef PULLDOWN_TEST
141 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
142 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
143 #else
144 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
145 	if (ip6f == NULL)
146 		return IPPROTO_DONE;
147 #endif
148 
149 	dstifp = NULL;
150 #ifdef IN6_IFSTAT_STRICT
151 	/* find the destination interface of the packet. */
152 	dst = (struct sockaddr_in6 *)&ro.ro_dst;
153 	if (ro.ro_rt
154 	 && ((ro.ro_rt->rt_flags & RTF_UP) == 0
155 	  || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
156 		RTFREE(ro.ro_rt);
157 		ro.ro_rt = (struct rtentry *)0;
158 	}
159 	if (ro.ro_rt == NULL) {
160 		bzero(dst, sizeof(*dst));
161 		dst->sin6_family = AF_INET6;
162 		dst->sin6_len = sizeof(struct sockaddr_in6);
163 		dst->sin6_addr = ip6->ip6_dst;
164 	}
165 
166 	rtalloc((struct route *)&ro);
167 
168 	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
169 		dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
170 #else
171 	/* we are violating the spec, this is not the destination interface */
172 	if ((m->m_flags & M_PKTHDR) != 0)
173 		dstifp = m->m_pkthdr.rcvif;
174 #endif
175 
176 	/* jumbo payload can't contain a fragment header */
177 	if (ip6->ip6_plen == 0) {
178 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
179 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
180 		return IPPROTO_DONE;
181 	}
182 
183 	/*
184 	 * check whether fragment packet's fragment length is
185 	 * multiple of 8 octets.
186 	 * sizeof(struct ip6_frag) == 8
187 	 * sizeof(struct ip6_hdr) = 40
188 	 */
189 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
190 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
191 		icmp6_error(m, ICMP6_PARAM_PROB,
192 			    ICMP6_PARAMPROB_HEADER,
193 			    offsetof(struct ip6_hdr, ip6_plen));
194 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
195 		return IPPROTO_DONE;
196 	}
197 
198 	ip6stat.ip6s_fragments++;
199 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
200 
201 	/* offset now points to data portion */
202 	offset += sizeof(struct ip6_frag);
203 
204 	frag6_doing_reass = 1;
205 
206 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
207 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
208 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
209 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
210 			break;
211 
212 	if (q6 == &ip6q) {
213 		/*
214 		 * the first fragment to arrive, create a reassembly queue.
215 		 */
216 		first_frag = 1;
217 
218 		/*
219 		 * Enforce upper bound on number of fragmented packets
220 		 * for which we attempt reassembly;
221 		 * If maxfrag is 0, never accept fragments.
222 		 * If maxfrag is -1, accept all fragments without limitation.
223 		 */
224 		if (ip6_maxfragpackets < 0)
225 			;
226 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
227 			goto dropfrag;
228 		frag6_nfragpackets++;
229 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
230 			M_DONTWAIT);
231 		if (q6 == NULL)
232 			goto dropfrag;
233 		bzero(q6, sizeof(*q6));
234 
235 		frag6_insque(q6, &ip6q);
236 
237 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
238 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
239 #ifdef notyet
240 		q6->ip6q_nxtp	= (u_char *)nxtp;
241 #endif
242 		q6->ip6q_ident	= ip6f->ip6f_ident;
243 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
244 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
245 		q6->ip6q_src	= ip6->ip6_src;
246 		q6->ip6q_dst	= ip6->ip6_dst;
247 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
248 	}
249 
250 	/*
251 	 * If it's the 1st fragment, record the length of the
252 	 * unfragmentable part and the next header of the fragment header.
253 	 */
254 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
255 	if (fragoff == 0) {
256 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr)
257 			- sizeof(struct ip6_frag);
258 		q6->ip6q_nxt = ip6f->ip6f_nxt;
259 	}
260 
261 	/*
262 	 * Check that the reassembled packet would not exceed 65535 bytes
263 	 * in size.
264 	 * If it would exceed, discard the fragment and return an ICMP error.
265 	 */
266 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
267 	if (q6->ip6q_unfrglen >= 0) {
268 		/* The 1st fragment has already arrived. */
269 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
270 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
271 				    offset - sizeof(struct ip6_frag) +
272 					offsetof(struct ip6_frag, ip6f_offlg));
273 			frag6_doing_reass = 0;
274 			return(IPPROTO_DONE);
275 		}
276 	}
277 	else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
278 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
279 			    offset - sizeof(struct ip6_frag) +
280 				offsetof(struct ip6_frag, ip6f_offlg));
281 		frag6_doing_reass = 0;
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 	frag6_doing_reass = 0;
533 	return IPPROTO_DONE;
534 }
535 
536 /*
537  * Free a fragment reassembly header and all
538  * associated datagrams.
539  */
540 void
541 frag6_freef(q6)
542 	struct ip6q *q6;
543 {
544 	struct ip6asfrag *af6, *down6;
545 
546 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
547 	     af6 = down6) {
548 		struct mbuf *m = IP6_REASS_MBUF(af6);
549 
550 		down6 = af6->ip6af_down;
551 		frag6_deq(af6);
552 
553 		/*
554 		 * Return ICMP time exceeded error for the 1st fragment.
555 		 * Just free other fragments.
556 		 */
557 		if (af6->ip6af_off == 0) {
558 			struct ip6_hdr *ip6;
559 
560 			/* adjust pointer */
561 			ip6 = mtod(m, struct ip6_hdr *);
562 
563 			/* restoure source and destination addresses */
564 			ip6->ip6_src = q6->ip6q_src;
565 			ip6->ip6_dst = q6->ip6q_dst;
566 
567 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
568 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
569 		} else
570 			m_freem(m);
571 		free(af6, M_FTABLE);
572 	}
573 	frag6_remque(q6);
574 	free(q6, M_FTABLE);
575 	frag6_nfragpackets--;
576 }
577 
578 /*
579  * Put an ip fragment on a reassembly chain.
580  * Like insque, but pointers in middle of structure.
581  */
582 void
583 frag6_enq(af6, up6)
584 	struct ip6asfrag *af6, *up6;
585 {
586 	af6->ip6af_up = up6;
587 	af6->ip6af_down = up6->ip6af_down;
588 	up6->ip6af_down->ip6af_up = af6;
589 	up6->ip6af_down = af6;
590 }
591 
592 /*
593  * To frag6_enq as remque is to insque.
594  */
595 void
596 frag6_deq(af6)
597 	struct ip6asfrag *af6;
598 {
599 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
600 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
601 }
602 
603 void
604 frag6_insque(new, old)
605 	struct ip6q *new, *old;
606 {
607 	new->ip6q_prev = old;
608 	new->ip6q_next = old->ip6q_next;
609 	old->ip6q_next->ip6q_prev= new;
610 	old->ip6q_next = new;
611 }
612 
613 void
614 frag6_remque(p6)
615 	struct ip6q *p6;
616 {
617 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
618 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
619 }
620 
621 /*
622  * IPv6 reassembling timer processing;
623  * if a timer expires on a reassembly
624  * queue, discard it.
625  */
626 void
627 frag6_slowtimo()
628 {
629 	struct ip6q *q6;
630 	int s = splnet();
631 
632 	frag6_doing_reass = 1;
633 	q6 = ip6q.ip6q_next;
634 	if (q6)
635 		while (q6 != &ip6q) {
636 			--q6->ip6q_ttl;
637 			q6 = q6->ip6q_next;
638 			if (q6->ip6q_prev->ip6q_ttl == 0) {
639 				ip6stat.ip6s_fragtimeout++;
640 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
641 				frag6_freef(q6->ip6q_prev);
642 			}
643 		}
644 	/*
645 	 * If we are over the maximum number of fragments
646 	 * (due to the limit being lowered), drain off
647 	 * enough to get down to the new limit.
648 	 */
649 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
650 	    ip6q.ip6q_prev) {
651 		ip6stat.ip6s_fragoverflow++;
652 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
653 		frag6_freef(ip6q.ip6q_prev);
654 	}
655 	frag6_doing_reass = 0;
656 
657 #if 0
658 	/*
659 	 * Routing changes might produce a better route than we last used;
660 	 * make sure we notice eventually, even if forwarding only for one
661 	 * destination and the cache is never replaced.
662 	 */
663 	if (ip6_forward_rt.ro_rt) {
664 		RTFREE(ip6_forward_rt.ro_rt);
665 		ip6_forward_rt.ro_rt = 0;
666 	}
667 	if (ipsrcchk_rt.ro_rt) {
668 		RTFREE(ipsrcchk_rt.ro_rt);
669 		ipsrcchk_rt.ro_rt = 0;
670 	}
671 #endif
672 
673 	splx(s);
674 }
675 
676 /*
677  * Drain off all datagram fragments.
678  */
679 void
680 frag6_drain()
681 {
682 	if (frag6_doing_reass)
683 		return;
684 	while (ip6q.ip6q_next != &ip6q) {
685 		ip6stat.ip6s_fragdropped++;
686 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
687 		frag6_freef(ip6q.ip6q_next);
688 	}
689 }
690