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