Home | History | Annotate | Line # | Download | only in netinet6
frag6.c revision 1.53
      1 /*	$NetBSD: frag6.c,v 1.53 2012/07/01 22:04:44 rmind Exp $	*/
      2 /*	$KAME: frag6.c,v 1.40 2002/05/27 21:40:31 itojun 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/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.53 2012/07/01 22:04:44 rmind Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/domain.h>
     40 #include <sys/protosw.h>
     41 #include <sys/socket.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/errno.h>
     44 #include <sys/time.h>
     45 #include <sys/kmem.h>
     46 #include <sys/kernel.h>
     47 #include <sys/syslog.h>
     48 
     49 #include <net/if.h>
     50 #include <net/route.h>
     51 
     52 #include <netinet/in.h>
     53 #include <netinet/in_var.h>
     54 #include <netinet/ip6.h>
     55 #include <netinet6/ip6_var.h>
     56 #include <netinet6/ip6_private.h>
     57 #include <netinet/icmp6.h>
     58 
     59 #include <net/net_osdep.h>
     60 
     61 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
     62 static void frag6_deq(struct ip6asfrag *);
     63 static void frag6_insque(struct ip6q *, struct ip6q *);
     64 static void frag6_remque(struct ip6q *);
     65 static void frag6_freef(struct ip6q *);
     66 
     67 static int frag6_drainwanted;
     68 
     69 u_int frag6_nfragpackets;
     70 u_int frag6_nfrags;
     71 struct	ip6q ip6q;	/* ip6 reassemble queue */
     72 
     73 static kmutex_t	frag6_lock;
     74 
     75 /*
     76  * Initialise reassembly queue and fragment identifier.
     77  */
     78 void
     79 frag6_init(void)
     80 {
     81 
     82 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
     83 	mutex_init(&frag6_lock, MUTEX_DEFAULT, IPL_NET);
     84 }
     85 
     86 /*
     87  * IPv6 fragment input.
     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 int
    118 frag6_input(struct mbuf **mp, int *offp, int proto)
    119 {
    120 	struct rtentry *rt;
    121 	struct mbuf *m = *mp, *t;
    122 	struct ip6_hdr *ip6;
    123 	struct ip6_frag *ip6f;
    124 	struct ip6q *q6;
    125 	struct ip6asfrag *af6, *ip6af, *af6dwn;
    126 	int offset = *offp, nxt, i, next;
    127 	int first_frag = 0;
    128 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
    129 	struct ifnet *dstifp;
    130 	static struct route ro;
    131 	union {
    132 		struct sockaddr		dst;
    133 		struct sockaddr_in6	dst6;
    134 	} u;
    135 
    136 	ip6 = mtod(m, struct ip6_hdr *);
    137 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
    138 	if (ip6f == NULL)
    139 		return IPPROTO_DONE;
    140 
    141 	dstifp = NULL;
    142 	/* find the destination interface of the packet. */
    143 	sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
    144 	if ((rt = rtcache_lookup(&ro, &u.dst)) != NULL && rt->rt_ifa != NULL)
    145 		dstifp = ((struct in6_ifaddr *)rt->rt_ifa)->ia_ifp;
    146 
    147 	/* jumbo payload can't contain a fragment header */
    148 	if (ip6->ip6_plen == 0) {
    149 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
    150 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
    151 		return IPPROTO_DONE;
    152 	}
    153 
    154 	/*
    155 	 * check whether fragment packet's fragment length is
    156 	 * multiple of 8 octets.
    157 	 * sizeof(struct ip6_frag) == 8
    158 	 * sizeof(struct ip6_hdr) = 40
    159 	 */
    160 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
    161 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
    162 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    163 		    offsetof(struct ip6_hdr, ip6_plen));
    164 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
    165 		return IPPROTO_DONE;
    166 	}
    167 
    168 	IP6_STATINC(IP6_STAT_FRAGMENTS);
    169 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
    170 
    171 	/* offset now points to data portion */
    172 	offset += sizeof(struct ip6_frag);
    173 
    174 	mutex_enter(&frag6_lock);
    175 
    176 	/*
    177 	 * Enforce upper bound on number of fragments.
    178 	 * If maxfrag is 0, never accept fragments.
    179 	 * If maxfrag is -1, accept all fragments without limitation.
    180 	 */
    181 	if (ip6_maxfrags < 0)
    182 		;
    183 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
    184 		goto dropfrag;
    185 
    186 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
    187 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
    188 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
    189 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
    190 			break;
    191 
    192 	if (q6 == &ip6q) {
    193 		/*
    194 		 * the first fragment to arrive, create a reassembly queue.
    195 		 */
    196 		first_frag = 1;
    197 
    198 		/*
    199 		 * Enforce upper bound on number of fragmented packets
    200 		 * for which we attempt reassembly;
    201 		 * If maxfragpackets is 0, never accept fragments.
    202 		 * If maxfragpackets is -1, accept all fragments without
    203 		 * limitation.
    204 		 */
    205 		if (ip6_maxfragpackets < 0)
    206 			;
    207 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
    208 			goto dropfrag;
    209 		frag6_nfragpackets++;
    210 
    211 		q6 = kmem_intr_zalloc(sizeof(struct ip6q), KM_NOSLEEP);
    212 		if (q6 == NULL) {
    213 			goto dropfrag;
    214 		}
    215 		frag6_insque(q6, &ip6q);
    216 
    217 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
    218 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
    219 #ifdef notyet
    220 		q6->ip6q_nxtp	= (u_char *)nxtp;
    221 #endif
    222 		q6->ip6q_ident	= ip6f->ip6f_ident;
    223 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
    224 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
    225 		q6->ip6q_src	= ip6->ip6_src;
    226 		q6->ip6q_dst	= ip6->ip6_dst;
    227 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
    228 
    229 		q6->ip6q_nfrag = 0;
    230 	}
    231 
    232 	/*
    233 	 * If it's the 1st fragment, record the length of the
    234 	 * unfragmentable part and the next header of the fragment header.
    235 	 */
    236 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
    237 	if (fragoff == 0) {
    238 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
    239 		    sizeof(struct ip6_frag);
    240 		q6->ip6q_nxt = ip6f->ip6f_nxt;
    241 	}
    242 
    243 	/*
    244 	 * Check that the reassembled packet would not exceed 65535 bytes
    245 	 * in size.
    246 	 * If it would exceed, discard the fragment and return an ICMP error.
    247 	 */
    248 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
    249 	if (q6->ip6q_unfrglen >= 0) {
    250 		/* The 1st fragment has already arrived. */
    251 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
    252 			mutex_exit(&frag6_lock);
    253 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    254 			    offset - sizeof(struct ip6_frag) +
    255 			    offsetof(struct ip6_frag, ip6f_offlg));
    256 			return IPPROTO_DONE;
    257 		}
    258 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
    259 		mutex_exit(&frag6_lock);
    260 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    261 			    offset - sizeof(struct ip6_frag) +
    262 				offsetof(struct ip6_frag, ip6f_offlg));
    263 		return IPPROTO_DONE;
    264 	}
    265 	/*
    266 	 * If it's the first fragment, do the above check for each
    267 	 * fragment already stored in the reassembly queue.
    268 	 */
    269 	if (fragoff == 0) {
    270 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    271 		     af6 = af6dwn) {
    272 			af6dwn = af6->ip6af_down;
    273 
    274 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
    275 			    IPV6_MAXPACKET) {
    276 				struct mbuf *merr = IP6_REASS_MBUF(af6);
    277 				struct ip6_hdr *ip6err;
    278 				int erroff = af6->ip6af_offset;
    279 
    280 				/* dequeue the fragment. */
    281 				frag6_deq(af6);
    282 				kmem_intr_free(af6, sizeof(struct ip6asfrag));
    283 
    284 				/* adjust pointer. */
    285 				ip6err = mtod(merr, struct ip6_hdr *);
    286 
    287 				/*
    288 				 * Restore source and destination addresses
    289 				 * in the erroneous IPv6 header.
    290 				 */
    291 				ip6err->ip6_src = q6->ip6q_src;
    292 				ip6err->ip6_dst = q6->ip6q_dst;
    293 
    294 				icmp6_error(merr, ICMP6_PARAM_PROB,
    295 				    ICMP6_PARAMPROB_HEADER,
    296 				    erroff - sizeof(struct ip6_frag) +
    297 				    offsetof(struct ip6_frag, ip6f_offlg));
    298 			}
    299 		}
    300 	}
    301 
    302 	ip6af = kmem_intr_zalloc(sizeof(struct ip6asfrag), KM_NOSLEEP);
    303 	if (ip6af == NULL) {
    304 		goto dropfrag;
    305 	}
    306 	ip6af->ip6af_head = ip6->ip6_flow;
    307 	ip6af->ip6af_len = ip6->ip6_plen;
    308 	ip6af->ip6af_nxt = ip6->ip6_nxt;
    309 	ip6af->ip6af_hlim = ip6->ip6_hlim;
    310 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
    311 	ip6af->ip6af_off = fragoff;
    312 	ip6af->ip6af_frglen = frgpartlen;
    313 	ip6af->ip6af_offset = offset;
    314 	IP6_REASS_MBUF(ip6af) = m;
    315 
    316 	if (first_frag) {
    317 		af6 = (struct ip6asfrag *)q6;
    318 		goto insert;
    319 	}
    320 
    321 	/*
    322 	 * Find a segment which begins after this one does.
    323 	 */
    324 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    325 	     af6 = af6->ip6af_down)
    326 		if (af6->ip6af_off > ip6af->ip6af_off)
    327 			break;
    328 
    329 	/*
    330 	 * If the incoming fragment overlaps some existing fragments in
    331 	 * the reassembly queue - drop it as per RFC 5722.
    332 	 */
    333 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
    334 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
    335 			- ip6af->ip6af_off;
    336 		if (i > 0) {
    337 			kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
    338 			goto dropfrag;
    339 		}
    340 	}
    341 	if (af6 != (struct ip6asfrag *)q6) {
    342 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
    343 		if (i > 0) {
    344 			kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
    345 			goto dropfrag;
    346 		}
    347 	}
    348 
    349 insert:
    350 
    351 	/*
    352 	 * Stick new segment in its place;
    353 	 * check for complete reassembly.
    354 	 * Move to front of packet queue, as we are
    355 	 * the most recently active fragmented packet.
    356 	 */
    357 	frag6_enq(ip6af, af6->ip6af_up);
    358 	frag6_nfrags++;
    359 	q6->ip6q_nfrag++;
    360 #if 0 /* xxx */
    361 	if (q6 != ip6q.ip6q_next) {
    362 		frag6_remque(q6);
    363 		frag6_insque(q6, &ip6q);
    364 	}
    365 #endif
    366 	next = 0;
    367 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    368 	     af6 = af6->ip6af_down) {
    369 		if (af6->ip6af_off != next) {
    370 			mutex_exit(&frag6_lock);
    371 			return IPPROTO_DONE;
    372 		}
    373 		next += af6->ip6af_frglen;
    374 	}
    375 	if (af6->ip6af_up->ip6af_mff) {
    376 		mutex_exit(&frag6_lock);
    377 		return IPPROTO_DONE;
    378 	}
    379 
    380 	/*
    381 	 * Reassembly is complete; concatenate fragments.
    382 	 */
    383 	ip6af = q6->ip6q_down;
    384 	t = m = IP6_REASS_MBUF(ip6af);
    385 	af6 = ip6af->ip6af_down;
    386 	frag6_deq(ip6af);
    387 	while (af6 != (struct ip6asfrag *)q6) {
    388 		af6dwn = af6->ip6af_down;
    389 		frag6_deq(af6);
    390 		while (t->m_next)
    391 			t = t->m_next;
    392 		t->m_next = IP6_REASS_MBUF(af6);
    393 		m_adj(t->m_next, af6->ip6af_offset);
    394 		kmem_intr_free(af6, sizeof(struct ip6asfrag));
    395 		af6 = af6dwn;
    396 	}
    397 
    398 	/* adjust offset to point where the original next header starts */
    399 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
    400 	kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
    401 	ip6 = mtod(m, struct ip6_hdr *);
    402 	ip6->ip6_plen = htons(next + offset - sizeof(struct ip6_hdr));
    403 	ip6->ip6_src = q6->ip6q_src;
    404 	ip6->ip6_dst = q6->ip6q_dst;
    405 	nxt = q6->ip6q_nxt;
    406 #ifdef notyet
    407 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
    408 #endif
    409 
    410 	/*
    411 	 * Delete frag6 header with as a few cost as possible.
    412 	 */
    413 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
    414 		memmove((char *)ip6 + sizeof(struct ip6_frag), ip6, offset);
    415 		m->m_data += sizeof(struct ip6_frag);
    416 		m->m_len -= sizeof(struct ip6_frag);
    417 	} else {
    418 		/* this comes with no copy if the boundary is on cluster */
    419 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
    420 			frag6_remque(q6);
    421 			frag6_nfrags -= q6->ip6q_nfrag;
    422 			kmem_intr_free(q6, sizeof(struct ip6q));
    423 			frag6_nfragpackets--;
    424 			goto dropfrag;
    425 		}
    426 		m_adj(t, sizeof(struct ip6_frag));
    427 		m_cat(m, t);
    428 	}
    429 
    430 	/*
    431 	 * Store NXT to the original.
    432 	 */
    433 	{
    434 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
    435 		*prvnxtp = nxt;
    436 	}
    437 
    438 	frag6_remque(q6);
    439 	frag6_nfrags -= q6->ip6q_nfrag;
    440 	kmem_intr_free(q6, sizeof(struct ip6q));
    441 	frag6_nfragpackets--;
    442 
    443 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
    444 		int plen = 0;
    445 		for (t = m; t; t = t->m_next)
    446 			plen += t->m_len;
    447 		m->m_pkthdr.len = plen;
    448 	}
    449 
    450 	IP6_STATINC(IP6_STAT_REASSEMBLED);
    451 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
    452 
    453 	/*
    454 	 * Tell launch routine the next header
    455 	 */
    456 
    457 	*mp = m;
    458 	*offp = offset;
    459 
    460 	mutex_exit(&frag6_lock);
    461 	return nxt;
    462 
    463  dropfrag:
    464 	mutex_exit(&frag6_lock);
    465 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
    466 	IP6_STATINC(IP6_STAT_FRAGDROPPED);
    467 	m_freem(m);
    468 	return IPPROTO_DONE;
    469 }
    470 
    471 int
    472 ip6_reass_packet(struct mbuf **mp, int offset)
    473 {
    474 
    475 	if (frag6_input(mp, &offset, IPPROTO_IPV6) == IPPROTO_DONE) {
    476 		*mp = NULL;
    477 		return EINVAL;
    478 	}
    479 	return 0;
    480 }
    481 
    482 /*
    483  * Free a fragment reassembly header and all
    484  * associated datagrams.
    485  */
    486 void
    487 frag6_freef(struct ip6q *q6)
    488 {
    489 	struct ip6asfrag *af6, *down6;
    490 
    491 	KASSERT(mutex_owned(&frag6_lock));
    492 
    493 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    494 	     af6 = down6) {
    495 		struct mbuf *m = IP6_REASS_MBUF(af6);
    496 
    497 		down6 = af6->ip6af_down;
    498 		frag6_deq(af6);
    499 
    500 		/*
    501 		 * Return ICMP time exceeded error for the 1st fragment.
    502 		 * Just free other fragments.
    503 		 */
    504 		if (af6->ip6af_off == 0) {
    505 			struct ip6_hdr *ip6;
    506 
    507 			/* adjust pointer */
    508 			ip6 = mtod(m, struct ip6_hdr *);
    509 
    510 			/* restoure source and destination addresses */
    511 			ip6->ip6_src = q6->ip6q_src;
    512 			ip6->ip6_dst = q6->ip6q_dst;
    513 
    514 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
    515 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
    516 		} else {
    517 			m_freem(m);
    518 		}
    519 		kmem_intr_free(af6, sizeof(struct ip6asfrag));
    520 	}
    521 	frag6_remque(q6);
    522 	frag6_nfrags -= q6->ip6q_nfrag;
    523 	kmem_intr_free(q6, sizeof(struct ip6q));
    524 	frag6_nfragpackets--;
    525 }
    526 
    527 /*
    528  * Put an ip fragment on a reassembly chain.
    529  * Like insque, but pointers in middle of structure.
    530  */
    531 void
    532 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
    533 {
    534 
    535 	KASSERT(mutex_owned(&frag6_lock));
    536 
    537 	af6->ip6af_up = up6;
    538 	af6->ip6af_down = up6->ip6af_down;
    539 	up6->ip6af_down->ip6af_up = af6;
    540 	up6->ip6af_down = af6;
    541 }
    542 
    543 /*
    544  * To frag6_enq as remque is to insque.
    545  */
    546 void
    547 frag6_deq(struct ip6asfrag *af6)
    548 {
    549 
    550 	KASSERT(mutex_owned(&frag6_lock));
    551 
    552 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
    553 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
    554 }
    555 
    556 void
    557 frag6_insque(struct ip6q *new, struct ip6q *old)
    558 {
    559 
    560 	KASSERT(mutex_owned(&frag6_lock));
    561 
    562 	new->ip6q_prev = old;
    563 	new->ip6q_next = old->ip6q_next;
    564 	old->ip6q_next->ip6q_prev= new;
    565 	old->ip6q_next = new;
    566 }
    567 
    568 void
    569 frag6_remque(struct ip6q *p6)
    570 {
    571 
    572 	KASSERT(mutex_owned(&frag6_lock));
    573 
    574 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
    575 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
    576 }
    577 
    578 void
    579 frag6_fasttimo(void)
    580 {
    581 	mutex_enter(softnet_lock);
    582 	KERNEL_LOCK(1, NULL);
    583 
    584 	if (frag6_drainwanted) {
    585 		frag6_drain();
    586 		frag6_drainwanted = 0;
    587 	}
    588 
    589 	KERNEL_UNLOCK_ONE(NULL);
    590 	mutex_exit(softnet_lock);
    591 }
    592 
    593 /*
    594  * IPv6 reassembling timer processing;
    595  * if a timer expires on a reassembly
    596  * queue, discard it.
    597  */
    598 void
    599 frag6_slowtimo(void)
    600 {
    601 	struct ip6q *q6;
    602 
    603 	mutex_enter(softnet_lock);
    604 	KERNEL_LOCK(1, NULL);
    605 
    606 	mutex_enter(&frag6_lock);
    607 	q6 = ip6q.ip6q_next;
    608 	if (q6)
    609 		while (q6 != &ip6q) {
    610 			--q6->ip6q_ttl;
    611 			q6 = q6->ip6q_next;
    612 			if (q6->ip6q_prev->ip6q_ttl == 0) {
    613 				IP6_STATINC(IP6_STAT_FRAGTIMEOUT);
    614 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    615 				frag6_freef(q6->ip6q_prev);
    616 			}
    617 		}
    618 	/*
    619 	 * If we are over the maximum number of fragments
    620 	 * (due to the limit being lowered), drain off
    621 	 * enough to get down to the new limit.
    622 	 */
    623 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
    624 	    ip6q.ip6q_prev) {
    625 		IP6_STATINC(IP6_STAT_FRAGOVERFLOW);
    626 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    627 		frag6_freef(ip6q.ip6q_prev);
    628 	}
    629 	mutex_exit(&frag6_lock);
    630 
    631 	KERNEL_UNLOCK_ONE(NULL);
    632 	mutex_exit(softnet_lock);
    633 
    634 #if 0
    635 	/*
    636 	 * Routing changes might produce a better route than we last used;
    637 	 * make sure we notice eventually, even if forwarding only for one
    638 	 * destination and the cache is never replaced.
    639 	 */
    640 	rtcache_free(&ip6_forward_rt);
    641 	rtcache_free(&ipsrcchk_rt);
    642 #endif
    643 
    644 }
    645 
    646 void
    647 frag6_drainstub(void)
    648 {
    649 	frag6_drainwanted = 1;
    650 }
    651 
    652 /*
    653  * Drain off all datagram fragments.
    654  */
    655 void
    656 frag6_drain(void)
    657 {
    658 
    659 	if (mutex_tryenter(&frag6_lock)) {
    660 		while (ip6q.ip6q_next != &ip6q) {
    661 			IP6_STATINC(IP6_STAT_FRAGDROPPED);
    662 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    663 			frag6_freef(ip6q.ip6q_next);
    664 		}
    665 		mutex_exit(&frag6_lock);
    666 	}
    667 }
    668