Home | History | Annotate | Line # | Download | only in netinet6
frag6.c revision 1.62
      1 /*	$NetBSD: frag6.c,v 1.62 2018/01/25 15:33:06 maxv 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.62 2018/01/25 15:33:06 maxv Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_net_mpsafe.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/mbuf.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 /*
     62  * IP6 reassembly queue structure.  Each fragment
     63  * being reassembled is attached to one of these structures.
     64  */
     65 struct	ip6q {
     66 	u_int32_t	ip6q_head;
     67 	u_int16_t	ip6q_len;
     68 	u_int8_t	ip6q_nxt;	/* ip6f_nxt in first fragment */
     69 	u_int8_t	ip6q_hlim;
     70 	struct ip6asfrag *ip6q_down;
     71 	struct ip6asfrag *ip6q_up;
     72 	u_int32_t	ip6q_ident;
     73 	u_int8_t	ip6q_ttl;
     74 	struct in6_addr	ip6q_src, ip6q_dst;
     75 	struct ip6q	*ip6q_next;
     76 	struct ip6q	*ip6q_prev;
     77 	int		ip6q_unfrglen;	/* len of unfragmentable part */
     78 	int		ip6q_nfrag;	/* # of fragments */
     79 };
     80 
     81 struct	ip6asfrag {
     82 	u_int32_t	ip6af_head;
     83 	u_int16_t	ip6af_len;
     84 	u_int8_t	ip6af_nxt;
     85 	u_int8_t	ip6af_hlim;
     86 	/* must not override the above members during reassembling */
     87 	struct ip6asfrag *ip6af_down;
     88 	struct ip6asfrag *ip6af_up;
     89 	struct mbuf	*ip6af_m;
     90 	int		ip6af_offset;	/* offset in ip6af_m to next header */
     91 	int		ip6af_frglen;	/* fragmentable part length */
     92 	int		ip6af_off;	/* fragment offset */
     93 	bool		ip6af_more;	/* more fragment bit in frag off */
     94 };
     95 
     96 
     97 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
     98 static void frag6_deq(struct ip6asfrag *);
     99 static void frag6_insque(struct ip6q *, struct ip6q *);
    100 static void frag6_remque(struct ip6q *);
    101 static void frag6_freef(struct ip6q *);
    102 
    103 static int frag6_drainwanted;
    104 
    105 u_int frag6_nfragpackets;
    106 u_int frag6_nfrags;
    107 struct ip6q ip6q;	/* ip6 reassembly queue */
    108 
    109 /* Protects ip6q */
    110 static kmutex_t	frag6_lock __cacheline_aligned;
    111 
    112 /*
    113  * Initialise reassembly queue and fragment identifier.
    114  */
    115 void
    116 frag6_init(void)
    117 {
    118 
    119 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
    120 	mutex_init(&frag6_lock, MUTEX_DEFAULT, IPL_NET);
    121 }
    122 
    123 /*
    124  * IPv6 fragment input.
    125  *
    126  * In RFC2460, fragment and reassembly rule do not agree with each other,
    127  * in terms of next header field handling in fragment header.
    128  * While the sender will use the same value for all of the fragmented packets,
    129  * receiver is suggested not to check the consistency.
    130  *
    131  * fragment rule (p20):
    132  *	(2) A Fragment header containing:
    133  *	The Next Header value that identifies the first header of
    134  *	the Fragmentable Part of the original packet.
    135  *		-> next header field is same for all fragments
    136  *
    137  * reassembly rule (p21):
    138  *	The Next Header field of the last header of the Unfragmentable
    139  *	Part is obtained from the Next Header field of the first
    140  *	fragment's Fragment header.
    141  *		-> should grab it from the first fragment only
    142  *
    143  * The following note also contradicts with fragment rule - noone is going to
    144  * send different fragment with different next header field.
    145  *
    146  * additional note (p22):
    147  *	The Next Header values in the Fragment headers of different
    148  *	fragments of the same original packet may differ.  Only the value
    149  *	from the Offset zero fragment packet is used for reassembly.
    150  *		-> should grab it from the first fragment only
    151  *
    152  * There is no explicit reason given in the RFC.  Historical reason maybe?
    153  */
    154 int
    155 frag6_input(struct mbuf **mp, int *offp, int proto)
    156 {
    157 	struct rtentry *rt;
    158 	struct mbuf *m = *mp, *t;
    159 	struct ip6_hdr *ip6;
    160 	struct ip6_frag *ip6f;
    161 	struct ip6q *q6;
    162 	struct ip6asfrag *af6, *ip6af, *af6dwn;
    163 	int offset = *offp, nxt, i, next;
    164 	int first_frag = 0;
    165 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
    166 	struct ifnet *dstifp;
    167 	static struct route ro;
    168 	union {
    169 		struct sockaddr		dst;
    170 		struct sockaddr_in6	dst6;
    171 	} u;
    172 
    173 	ip6 = mtod(m, struct ip6_hdr *);
    174 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
    175 	if (ip6f == NULL)
    176 		return IPPROTO_DONE;
    177 
    178 	dstifp = NULL;
    179 	/* find the destination interface of the packet. */
    180 	sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
    181 	if ((rt = rtcache_lookup(&ro, &u.dst)) != NULL && rt->rt_ifa != NULL)
    182 		dstifp = ((struct in6_ifaddr *)rt->rt_ifa)->ia_ifp;
    183 
    184 	/* jumbo payload can't contain a fragment header */
    185 	if (ip6->ip6_plen == 0) {
    186 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
    187 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
    188 		goto done;
    189 	}
    190 
    191 	/*
    192 	 * check whether fragment packet's fragment length is
    193 	 * multiple of 8 octets.
    194 	 * sizeof(struct ip6_frag) == 8
    195 	 * sizeof(struct ip6_hdr) = 40
    196 	 */
    197 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
    198 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
    199 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    200 		    offsetof(struct ip6_hdr, ip6_plen));
    201 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
    202 		goto done;
    203 	}
    204 
    205 	IP6_STATINC(IP6_STAT_FRAGMENTS);
    206 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
    207 
    208 	/* offset now points to data portion */
    209 	offset += sizeof(struct ip6_frag);
    210 
    211 	/*
    212 	 * RFC6946: A host that receives an IPv6 packet which includes
    213 	 * a Fragment Header with the "Fragment Offset" equal to 0 and
    214 	 * the "M" bit equal to 0 MUST process such packet in isolation
    215 	 * from any other packets/fragments.
    216 	 */
    217 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
    218 	if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
    219 		IP6_STATINC(IP6_STAT_REASSEMBLED);
    220 		in6_ifstat_inc(dstifp, ifs6_reass_ok);
    221 		*offp = offset;
    222 		rtcache_unref(rt, &ro);
    223 		return ip6f->ip6f_nxt;
    224 	}
    225 
    226 	mutex_enter(&frag6_lock);
    227 
    228 	/*
    229 	 * Enforce upper bound on number of fragments.
    230 	 * If maxfrag is 0, never accept fragments.
    231 	 * If maxfrag is -1, accept all fragments without limitation.
    232 	 */
    233 	if (ip6_maxfrags < 0)
    234 		;
    235 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
    236 		goto dropfrag;
    237 
    238 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
    239 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
    240 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
    241 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
    242 			break;
    243 
    244 	if (q6 == &ip6q) {
    245 		/*
    246 		 * the first fragment to arrive, create a reassembly queue.
    247 		 */
    248 		first_frag = 1;
    249 
    250 		/*
    251 		 * Enforce upper bound on number of fragmented packets
    252 		 * for which we attempt reassembly;
    253 		 * If maxfragpackets is 0, never accept fragments.
    254 		 * If maxfragpackets is -1, accept all fragments without
    255 		 * limitation.
    256 		 */
    257 		if (ip6_maxfragpackets < 0)
    258 			;
    259 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
    260 			goto dropfrag;
    261 		frag6_nfragpackets++;
    262 
    263 		q6 = kmem_intr_zalloc(sizeof(struct ip6q), KM_NOSLEEP);
    264 		if (q6 == NULL) {
    265 			goto dropfrag;
    266 		}
    267 		frag6_insque(q6, &ip6q);
    268 
    269 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
    270 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
    271 		q6->ip6q_ident	= ip6f->ip6f_ident;
    272 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
    273 		q6->ip6q_src	= ip6->ip6_src;
    274 		q6->ip6q_dst	= ip6->ip6_dst;
    275 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
    276 
    277 		q6->ip6q_nfrag = 0;
    278 	}
    279 
    280 	/*
    281 	 * If it's the 1st fragment, record the length of the
    282 	 * unfragmentable part and the next header of the fragment header.
    283 	 */
    284 	if (fragoff == 0) {
    285 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
    286 		    sizeof(struct ip6_frag);
    287 		q6->ip6q_nxt = ip6f->ip6f_nxt;
    288 	}
    289 
    290 	/*
    291 	 * Check that the reassembled packet would not exceed 65535 bytes
    292 	 * in size. If it would exceed, discard the fragment and return an
    293 	 * ICMP error.
    294 	 */
    295 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
    296 	if (q6->ip6q_unfrglen >= 0) {
    297 		/* The 1st fragment has already arrived. */
    298 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
    299 			mutex_exit(&frag6_lock);
    300 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    301 			    offset - sizeof(struct ip6_frag) +
    302 			    offsetof(struct ip6_frag, ip6f_offlg));
    303 			goto done;
    304 		}
    305 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
    306 		mutex_exit(&frag6_lock);
    307 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    308 		    offset - sizeof(struct ip6_frag) +
    309 		    offsetof(struct ip6_frag, ip6f_offlg));
    310 		goto done;
    311 	}
    312 
    313 	/*
    314 	 * If it's the first fragment, do the above check for each
    315 	 * fragment already stored in the reassembly queue.
    316 	 */
    317 	if (fragoff == 0) {
    318 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    319 		     af6 = af6dwn) {
    320 			af6dwn = af6->ip6af_down;
    321 
    322 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
    323 			    IPV6_MAXPACKET) {
    324 				struct mbuf *merr = af6->ip6af_m;
    325 				struct ip6_hdr *ip6err;
    326 				int erroff = af6->ip6af_offset;
    327 
    328 				/* dequeue the fragment. */
    329 				frag6_deq(af6);
    330 				kmem_intr_free(af6, sizeof(struct ip6asfrag));
    331 
    332 				/* adjust pointer. */
    333 				ip6err = mtod(merr, struct ip6_hdr *);
    334 
    335 				/*
    336 				 * Restore source and destination addresses
    337 				 * in the erroneous IPv6 header.
    338 				 */
    339 				ip6err->ip6_src = q6->ip6q_src;
    340 				ip6err->ip6_dst = q6->ip6q_dst;
    341 
    342 				icmp6_error(merr, ICMP6_PARAM_PROB,
    343 				    ICMP6_PARAMPROB_HEADER,
    344 				    erroff - sizeof(struct ip6_frag) +
    345 				    offsetof(struct ip6_frag, ip6f_offlg));
    346 			}
    347 		}
    348 	}
    349 
    350 	ip6af = kmem_intr_zalloc(sizeof(struct ip6asfrag), KM_NOSLEEP);
    351 	if (ip6af == NULL) {
    352 		goto dropfrag;
    353 	}
    354 	ip6af->ip6af_head = ip6->ip6_flow;
    355 	ip6af->ip6af_len = ip6->ip6_plen;
    356 	ip6af->ip6af_nxt = ip6->ip6_nxt;
    357 	ip6af->ip6af_hlim = ip6->ip6_hlim;
    358 	ip6af->ip6af_more = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) != 0;
    359 	ip6af->ip6af_off = fragoff;
    360 	ip6af->ip6af_frglen = frgpartlen;
    361 	ip6af->ip6af_offset = offset;
    362 	ip6af->ip6af_m = m;
    363 
    364 	if (first_frag) {
    365 		af6 = (struct ip6asfrag *)q6;
    366 		goto insert;
    367 	}
    368 
    369 	/*
    370 	 * Find a segment which begins after this one does.
    371 	 */
    372 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    373 	     af6 = af6->ip6af_down)
    374 		if (af6->ip6af_off > ip6af->ip6af_off)
    375 			break;
    376 
    377 	/*
    378 	 * If the incoming fragment overlaps some existing fragments in
    379 	 * the reassembly queue - drop it as per RFC 5722.
    380 	 */
    381 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
    382 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
    383 			- ip6af->ip6af_off;
    384 		if (i > 0) {
    385 			kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
    386 			goto dropfrag;
    387 		}
    388 	}
    389 	if (af6 != (struct ip6asfrag *)q6) {
    390 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
    391 		if (i > 0) {
    392 			kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
    393 			goto dropfrag;
    394 		}
    395 	}
    396 
    397 insert:
    398 	/*
    399 	 * Stick new segment in its place. Move to front of packet queue, as
    400 	 * we are the most recently active fragmented packet.
    401 	 */
    402 	frag6_enq(ip6af, af6->ip6af_up);
    403 	frag6_nfrags++;
    404 	q6->ip6q_nfrag++;
    405 
    406 	/*
    407 	 * Check for complete reassembly.
    408 	 */
    409 	next = 0;
    410 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    411 	     af6 = af6->ip6af_down) {
    412 		if (af6->ip6af_off != next) {
    413 			mutex_exit(&frag6_lock);
    414 			goto done;
    415 		}
    416 		next += af6->ip6af_frglen;
    417 	}
    418 	if (af6->ip6af_up->ip6af_more) {
    419 		mutex_exit(&frag6_lock);
    420 		goto done;
    421 	}
    422 
    423 	/*
    424 	 * Reassembly is complete; concatenate fragments.
    425 	 */
    426 	ip6af = q6->ip6q_down;
    427 	t = m = ip6af->ip6af_m;
    428 	af6 = ip6af->ip6af_down;
    429 	frag6_deq(ip6af);
    430 	while (af6 != (struct ip6asfrag *)q6) {
    431 		af6dwn = af6->ip6af_down;
    432 		frag6_deq(af6);
    433 		while (t->m_next)
    434 			t = t->m_next;
    435 		t->m_next = af6->ip6af_m;
    436 		m_adj(t->m_next, af6->ip6af_offset);
    437 		kmem_intr_free(af6, sizeof(struct ip6asfrag));
    438 		af6 = af6dwn;
    439 	}
    440 
    441 	/* adjust offset to point where the original next header starts */
    442 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
    443 	kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
    444 	ip6 = mtod(m, struct ip6_hdr *);
    445 	ip6->ip6_plen = htons(next + offset - sizeof(struct ip6_hdr));
    446 	ip6->ip6_src = q6->ip6q_src;
    447 	ip6->ip6_dst = q6->ip6q_dst;
    448 	nxt = q6->ip6q_nxt;
    449 
    450 	/*
    451 	 * Delete frag6 header.
    452 	 */
    453 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
    454 		memmove((char *)ip6 + sizeof(struct ip6_frag), ip6, offset);
    455 		m->m_data += sizeof(struct ip6_frag);
    456 		m->m_len -= sizeof(struct ip6_frag);
    457 	} else {
    458 		/* this comes with no copy if the boundary is on cluster */
    459 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
    460 			frag6_remque(q6);
    461 			frag6_nfrags -= q6->ip6q_nfrag;
    462 			kmem_intr_free(q6, sizeof(struct ip6q));
    463 			frag6_nfragpackets--;
    464 			goto dropfrag;
    465 		}
    466 		m_adj(t, sizeof(struct ip6_frag));
    467 		m_cat(m, t);
    468 	}
    469 
    470 	/*
    471 	 * Store NXT to the original.
    472 	 */
    473 	{
    474 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
    475 		*prvnxtp = nxt;
    476 	}
    477 
    478 	frag6_remque(q6);
    479 	frag6_nfrags -= q6->ip6q_nfrag;
    480 	kmem_intr_free(q6, sizeof(struct ip6q));
    481 	frag6_nfragpackets--;
    482 
    483 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
    484 		int plen = 0;
    485 		for (t = m; t; t = t->m_next)
    486 			plen += t->m_len;
    487 		m->m_pkthdr.len = plen;
    488 	}
    489 
    490 	IP6_STATINC(IP6_STAT_REASSEMBLED);
    491 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
    492 	rtcache_unref(rt, &ro);
    493 
    494 	/*
    495 	 * Tell launch routine the next header
    496 	 */
    497 
    498 	*mp = m;
    499 	*offp = offset;
    500 
    501 	mutex_exit(&frag6_lock);
    502 	return nxt;
    503 
    504  dropfrag:
    505 	mutex_exit(&frag6_lock);
    506 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
    507 	IP6_STATINC(IP6_STAT_FRAGDROPPED);
    508 	m_freem(m);
    509  done:
    510 	rtcache_unref(rt, &ro);
    511 	return IPPROTO_DONE;
    512 }
    513 
    514 int
    515 ip6_reass_packet(struct mbuf **mp, int offset)
    516 {
    517 
    518 	if (frag6_input(mp, &offset, IPPROTO_IPV6) == IPPROTO_DONE) {
    519 		*mp = NULL;
    520 		return EINVAL;
    521 	}
    522 	return 0;
    523 }
    524 
    525 /*
    526  * Free a fragment reassembly header and all
    527  * associated datagrams.
    528  */
    529 static void
    530 frag6_freef(struct ip6q *q6)
    531 {
    532 	struct ip6asfrag *af6, *down6;
    533 
    534 	KASSERT(mutex_owned(&frag6_lock));
    535 
    536 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    537 	     af6 = down6) {
    538 		struct mbuf *m = af6->ip6af_m;
    539 
    540 		down6 = af6->ip6af_down;
    541 		frag6_deq(af6);
    542 
    543 		/*
    544 		 * Return ICMP time exceeded error for the 1st fragment.
    545 		 * Just free other fragments.
    546 		 */
    547 		if (af6->ip6af_off == 0) {
    548 			struct ip6_hdr *ip6;
    549 
    550 			/* adjust pointer */
    551 			ip6 = mtod(m, struct ip6_hdr *);
    552 
    553 			/* restoure source and destination addresses */
    554 			ip6->ip6_src = q6->ip6q_src;
    555 			ip6->ip6_dst = q6->ip6q_dst;
    556 
    557 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
    558 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
    559 		} else {
    560 			m_freem(m);
    561 		}
    562 		kmem_intr_free(af6, sizeof(struct ip6asfrag));
    563 	}
    564 
    565 	frag6_remque(q6);
    566 	frag6_nfrags -= q6->ip6q_nfrag;
    567 	kmem_intr_free(q6, sizeof(struct ip6q));
    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(struct ip6asfrag *af6, struct ip6asfrag *up6)
    577 {
    578 
    579 	KASSERT(mutex_owned(&frag6_lock));
    580 
    581 	af6->ip6af_up = up6;
    582 	af6->ip6af_down = up6->ip6af_down;
    583 	up6->ip6af_down->ip6af_up = af6;
    584 	up6->ip6af_down = af6;
    585 }
    586 
    587 /*
    588  * To frag6_enq as remque is to insque.
    589  */
    590 void
    591 frag6_deq(struct ip6asfrag *af6)
    592 {
    593 
    594 	KASSERT(mutex_owned(&frag6_lock));
    595 
    596 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
    597 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
    598 }
    599 
    600 /*
    601  * Insert newq after oldq.
    602  */
    603 void
    604 frag6_insque(struct ip6q *newq, struct ip6q *oldq)
    605 {
    606 
    607 	KASSERT(mutex_owned(&frag6_lock));
    608 
    609 	newq->ip6q_prev = oldq;
    610 	newq->ip6q_next = oldq->ip6q_next;
    611 	oldq->ip6q_next->ip6q_prev = newq;
    612 	oldq->ip6q_next = newq;
    613 }
    614 
    615 /*
    616  * Unlink p6.
    617  */
    618 void
    619 frag6_remque(struct ip6q *p6)
    620 {
    621 
    622 	KASSERT(mutex_owned(&frag6_lock));
    623 
    624 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
    625 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
    626 }
    627 
    628 void
    629 frag6_fasttimo(void)
    630 {
    631 
    632 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
    633 
    634 	if (frag6_drainwanted) {
    635 		frag6_drain();
    636 		frag6_drainwanted = 0;
    637 	}
    638 
    639 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    640 }
    641 
    642 /*
    643  * IPv6 reassembling timer processing;
    644  * if a timer expires on a reassembly
    645  * queue, discard it.
    646  */
    647 void
    648 frag6_slowtimo(void)
    649 {
    650 	struct ip6q *q6;
    651 
    652 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
    653 
    654 	mutex_enter(&frag6_lock);
    655 	q6 = ip6q.ip6q_next;
    656 	if (q6)
    657 		while (q6 != &ip6q) {
    658 			--q6->ip6q_ttl;
    659 			q6 = q6->ip6q_next;
    660 			if (q6->ip6q_prev->ip6q_ttl == 0) {
    661 				IP6_STATINC(IP6_STAT_FRAGTIMEOUT);
    662 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    663 				frag6_freef(q6->ip6q_prev);
    664 			}
    665 		}
    666 	/*
    667 	 * If we are over the maximum number of fragments
    668 	 * (due to the limit being lowered), drain off
    669 	 * enough to get down to the new limit.
    670 	 */
    671 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
    672 	    ip6q.ip6q_prev) {
    673 		IP6_STATINC(IP6_STAT_FRAGOVERFLOW);
    674 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    675 		frag6_freef(ip6q.ip6q_prev);
    676 	}
    677 	mutex_exit(&frag6_lock);
    678 
    679 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    680 
    681 #if 0
    682 	/*
    683 	 * Routing changes might produce a better route than we last used;
    684 	 * make sure we notice eventually, even if forwarding only for one
    685 	 * destination and the cache is never replaced.
    686 	 */
    687 	rtcache_free(&ip6_forward_rt);
    688 	rtcache_free(&ipsrcchk_rt);
    689 #endif
    690 
    691 }
    692 
    693 void
    694 frag6_drainstub(void)
    695 {
    696 	frag6_drainwanted = 1;
    697 }
    698 
    699 /*
    700  * Drain off all datagram fragments.
    701  */
    702 void
    703 frag6_drain(void)
    704 {
    705 
    706 	if (mutex_tryenter(&frag6_lock)) {
    707 		while (ip6q.ip6q_next != &ip6q) {
    708 			IP6_STATINC(IP6_STAT_FRAGDROPPED);
    709 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    710 			frag6_freef(ip6q.ip6q_next);
    711 		}
    712 		mutex_exit(&frag6_lock);
    713 	}
    714 }
    715