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