Home | History | Annotate | Line # | Download | only in netinet6
frag6.c revision 1.17
      1 /*	$NetBSD: frag6.c,v 1.17 2002/03/15 10:44:07 itojun Exp $	*/
      2 /*	$KAME: frag6.c,v 1.31 2001/05/17 13:45:34 jinmei Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.17 2002/03/15 10:44:07 itojun 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/in6_pcb.h>
     55 #include <netinet6/ip6_var.h>
     56 #include <netinet/icmp6.h>
     57 
     58 #include <net/net_osdep.h>
     59 
     60 /*
     61  * Define it to get a correct behavior on per-interface statistics.
     62  * You will need to perform an extra routing table lookup, per fragment,
     63  * to do it.  This may, or may not be, a performance hit.
     64  */
     65 #define IN6_IFSTAT_STRICT
     66 
     67 static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
     68 static void frag6_deq __P((struct ip6asfrag *));
     69 static void frag6_insque __P((struct ip6q *, struct ip6q *));
     70 static void frag6_remque __P((struct ip6q *));
     71 static void frag6_freef __P((struct ip6q *));
     72 
     73 static int ip6q_locked;
     74 u_int frag6_nfragpackets;
     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 (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 (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 	struct timeval tv;
    142 
    143 	/*
    144 	 * in many cases, random() here does NOT return random number
    145 	 * as initialization during bootstrap time occur in fixed order.
    146 	 */
    147 	microtime(&tv);
    148 	ip6_id = random() ^ tv.tv_usec;
    149 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
    150 }
    151 
    152 /*
    153  * In RFC2460, fragment and reassembly rule do not agree with each other,
    154  * in terms of next header field handling in fragment header.
    155  * While the sender will use the same value for all of the fragmented packets,
    156  * receiver is suggested not to check the consistency.
    157  *
    158  * fragment rule (p20):
    159  *	(2) A Fragment header containing:
    160  *	The Next Header value that identifies the first header of
    161  *	the Fragmentable Part of the original packet.
    162  *		-> next header field is same for all fragments
    163  *
    164  * reassembly rule (p21):
    165  *	The Next Header field of the last header of the Unfragmentable
    166  *	Part is obtained from the Next Header field of the first
    167  *	fragment's Fragment header.
    168  *		-> should grab it from the first fragment only
    169  *
    170  * The following note also contradicts with fragment rule - noone is going to
    171  * send different fragment with different next header field.
    172  *
    173  * additional note (p22):
    174  *	The Next Header values in the Fragment headers of different
    175  *	fragments of the same original packet may differ.  Only the value
    176  *	from the Offset zero fragment packet is used for reassembly.
    177  *		-> should grab it from the first fragment only
    178  *
    179  * There is no explicit reason given in the RFC.  Historical reason maybe?
    180  */
    181 /*
    182  * Fragment input
    183  */
    184 int
    185 frag6_input(mp, offp, proto)
    186 	struct mbuf **mp;
    187 	int *offp, proto;
    188 {
    189 	struct mbuf *m = *mp, *t;
    190 	struct ip6_hdr *ip6;
    191 	struct ip6_frag *ip6f;
    192 	struct ip6q *q6;
    193 	struct ip6asfrag *af6, *ip6af, *af6dwn;
    194 	int offset = *offp, nxt, i, next;
    195 	int first_frag = 0;
    196 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
    197 	struct ifnet *dstifp;
    198 #ifdef IN6_IFSTAT_STRICT
    199 	static struct route_in6 ro;
    200 	struct sockaddr_in6 *dst;
    201 #endif
    202 
    203 	ip6 = mtod(m, struct ip6_hdr *);
    204 #ifndef PULLDOWN_TEST
    205 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
    206 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
    207 #else
    208 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
    209 	if (ip6f == NULL)
    210 		return IPPROTO_DONE;
    211 #endif
    212 
    213 	dstifp = NULL;
    214 #ifdef IN6_IFSTAT_STRICT
    215 	/* find the destination interface of the packet. */
    216 	dst = (struct sockaddr_in6 *)&ro.ro_dst;
    217 	if (ro.ro_rt
    218 	 && ((ro.ro_rt->rt_flags & RTF_UP) == 0
    219 	  || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
    220 		RTFREE(ro.ro_rt);
    221 		ro.ro_rt = (struct rtentry *)0;
    222 	}
    223 	if (ro.ro_rt == NULL) {
    224 		bzero(dst, sizeof(*dst));
    225 		dst->sin6_family = AF_INET6;
    226 		dst->sin6_len = sizeof(struct sockaddr_in6);
    227 		dst->sin6_addr = ip6->ip6_dst;
    228 	}
    229 	rtalloc((struct route *)&ro);
    230 	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
    231 		dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
    232 #else
    233 	/* we are violating the spec, this is not the destination interface */
    234 	if ((m->m_flags & M_PKTHDR) != 0)
    235 		dstifp = m->m_pkthdr.rcvif;
    236 #endif
    237 
    238 	/* jumbo payload can't contain a fragment header */
    239 	if (ip6->ip6_plen == 0) {
    240 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
    241 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
    242 		return IPPROTO_DONE;
    243 	}
    244 
    245 	/*
    246 	 * check whether fragment packet's fragment length is
    247 	 * multiple of 8 octets.
    248 	 * sizeof(struct ip6_frag) == 8
    249 	 * sizeof(struct ip6_hdr) = 40
    250 	 */
    251 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
    252 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
    253 		icmp6_error(m, ICMP6_PARAM_PROB,
    254 			    ICMP6_PARAMPROB_HEADER,
    255 			    offsetof(struct ip6_hdr, ip6_plen));
    256 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
    257 		return IPPROTO_DONE;
    258 	}
    259 
    260 	ip6stat.ip6s_fragments++;
    261 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
    262 
    263 	/* offset now points to data portion */
    264 	offset += sizeof(struct ip6_frag);
    265 
    266 	IP6Q_LOCK();
    267 
    268 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
    269 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
    270 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
    271 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
    272 			break;
    273 
    274 	if (q6 == &ip6q) {
    275 		/*
    276 		 * the first fragment to arrive, create a reassembly queue.
    277 		 */
    278 		first_frag = 1;
    279 
    280 		/*
    281 		 * Enforce upper bound on number of fragmented packets
    282 		 * for which we attempt reassembly;
    283 		 * If maxfrag is 0, never accept fragments.
    284 		 * If maxfrag is -1, accept all fragments without limitation.
    285 		 */
    286 		if (ip6_maxfragpackets < 0)
    287 			;
    288 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
    289 			goto dropfrag;
    290 		frag6_nfragpackets++;
    291 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
    292 			M_DONTWAIT);
    293 		if (q6 == NULL)
    294 			goto dropfrag;
    295 		bzero(q6, sizeof(*q6));
    296 
    297 		frag6_insque(q6, &ip6q);
    298 
    299 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
    300 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
    301 #ifdef notyet
    302 		q6->ip6q_nxtp	= (u_char *)nxtp;
    303 #endif
    304 		q6->ip6q_ident	= ip6f->ip6f_ident;
    305 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
    306 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
    307 		q6->ip6q_src	= ip6->ip6_src;
    308 		q6->ip6q_dst	= ip6->ip6_dst;
    309 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
    310 	}
    311 
    312 	/*
    313 	 * If it's the 1st fragment, record the length of the
    314 	 * unfragmentable part and the next header of the fragment header.
    315 	 */
    316 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
    317 	if (fragoff == 0) {
    318 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr)
    319 			- sizeof(struct ip6_frag);
    320 		q6->ip6q_nxt = ip6f->ip6f_nxt;
    321 	}
    322 
    323 	/*
    324 	 * Check that the reassembled packet would not exceed 65535 bytes
    325 	 * in size.
    326 	 * If it would exceed, discard the fragment and return an ICMP error.
    327 	 */
    328 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
    329 	if (q6->ip6q_unfrglen >= 0) {
    330 		/* The 1st fragment has already arrived. */
    331 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
    332 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    333 				    offset - sizeof(struct ip6_frag) +
    334 					offsetof(struct ip6_frag, ip6f_offlg));
    335 			IP6Q_UNLOCK();
    336 			return(IPPROTO_DONE);
    337 		}
    338 	}
    339 	else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
    340 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    341 			    offset - sizeof(struct ip6_frag) +
    342 				offsetof(struct ip6_frag, ip6f_offlg));
    343 		IP6Q_UNLOCK();
    344 		return(IPPROTO_DONE);
    345 	}
    346 	/*
    347 	 * If it's the first fragment, do the above check for each
    348 	 * fragment already stored in the reassembly queue.
    349 	 */
    350 	if (fragoff == 0) {
    351 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    352 		     af6 = af6dwn) {
    353 			af6dwn = af6->ip6af_down;
    354 
    355 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
    356 			    IPV6_MAXPACKET) {
    357 				struct mbuf *merr = IP6_REASS_MBUF(af6);
    358 				struct ip6_hdr *ip6err;
    359 				int erroff = af6->ip6af_offset;
    360 
    361 				/* dequeue the fragment. */
    362 				frag6_deq(af6);
    363 				free(af6, M_FTABLE);
    364 
    365 				/* adjust pointer. */
    366 				ip6err = mtod(merr, struct ip6_hdr *);
    367 
    368 				/*
    369 				 * Restore source and destination addresses
    370 				 * in the erroneous IPv6 header.
    371 				 */
    372 				ip6err->ip6_src = q6->ip6q_src;
    373 				ip6err->ip6_dst = q6->ip6q_dst;
    374 
    375 				icmp6_error(merr, ICMP6_PARAM_PROB,
    376 					    ICMP6_PARAMPROB_HEADER,
    377 					    erroff - sizeof(struct ip6_frag) +
    378 						offsetof(struct ip6_frag, ip6f_offlg));
    379 			}
    380 		}
    381 	}
    382 
    383 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
    384 	    M_DONTWAIT);
    385 	if (ip6af == NULL)
    386 		goto dropfrag;
    387 	bzero(ip6af, sizeof(*ip6af));
    388 	ip6af->ip6af_head = ip6->ip6_flow;
    389 	ip6af->ip6af_len = ip6->ip6_plen;
    390 	ip6af->ip6af_nxt = ip6->ip6_nxt;
    391 	ip6af->ip6af_hlim = ip6->ip6_hlim;
    392 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
    393 	ip6af->ip6af_off = fragoff;
    394 	ip6af->ip6af_frglen = frgpartlen;
    395 	ip6af->ip6af_offset = offset;
    396 	IP6_REASS_MBUF(ip6af) = m;
    397 
    398 	if (first_frag) {
    399 		af6 = (struct ip6asfrag *)q6;
    400 		goto insert;
    401 	}
    402 
    403 	/*
    404 	 * Find a segment which begins after this one does.
    405 	 */
    406 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    407 	     af6 = af6->ip6af_down)
    408 		if (af6->ip6af_off > ip6af->ip6af_off)
    409 			break;
    410 
    411 #if 0
    412 	/*
    413 	 * If there is a preceding segment, it may provide some of
    414 	 * our data already.  If so, drop the data from the incoming
    415 	 * segment.  If it provides all of our data, drop us.
    416 	 */
    417 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
    418 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
    419 			- ip6af->ip6af_off;
    420 		if (i > 0) {
    421 			if (i >= ip6af->ip6af_frglen)
    422 				goto dropfrag;
    423 			m_adj(IP6_REASS_MBUF(ip6af), i);
    424 			ip6af->ip6af_off += i;
    425 			ip6af->ip6af_frglen -= i;
    426 		}
    427 	}
    428 
    429 	/*
    430 	 * While we overlap succeeding segments trim them or,
    431 	 * if they are completely covered, dequeue them.
    432 	 */
    433 	while (af6 != (struct ip6asfrag *)q6 &&
    434 	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
    435 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
    436 		if (i < af6->ip6af_frglen) {
    437 			af6->ip6af_frglen -= i;
    438 			af6->ip6af_off += i;
    439 			m_adj(IP6_REASS_MBUF(af6), i);
    440 			break;
    441 		}
    442 		af6 = af6->ip6af_down;
    443 		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
    444 		frag6_deq(af6->ip6af_up);
    445 	}
    446 #else
    447 	/*
    448 	 * If the incoming framgent overlaps some existing fragments in
    449 	 * the reassembly queue, drop it, since it is dangerous to override
    450 	 * existing fragments from a security point of view.
    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 #if 0 /* xxx */
    489 	if (q6 != ip6q.ip6q_next) {
    490 		frag6_remque(q6);
    491 		frag6_insque(q6, &ip6q);
    492 	}
    493 #endif
    494 	next = 0;
    495 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    496 	     af6 = af6->ip6af_down) {
    497 		if (af6->ip6af_off != next) {
    498 			IP6Q_UNLOCK();
    499 			return IPPROTO_DONE;
    500 		}
    501 		next += af6->ip6af_frglen;
    502 	}
    503 	if (af6->ip6af_up->ip6af_mff) {
    504 		IP6Q_UNLOCK();
    505 		return IPPROTO_DONE;
    506 	}
    507 
    508 	/*
    509 	 * Reassembly is complete; concatenate fragments.
    510 	 */
    511 	ip6af = q6->ip6q_down;
    512 	t = m = IP6_REASS_MBUF(ip6af);
    513 	af6 = ip6af->ip6af_down;
    514 	frag6_deq(ip6af);
    515 	while (af6 != (struct ip6asfrag *)q6) {
    516 		af6dwn = af6->ip6af_down;
    517 		frag6_deq(af6);
    518 		while (t->m_next)
    519 			t = t->m_next;
    520 		t->m_next = IP6_REASS_MBUF(af6);
    521 		m_adj(t->m_next, af6->ip6af_offset);
    522 		free(af6, M_FTABLE);
    523 		af6 = af6dwn;
    524 	}
    525 
    526 	/* adjust offset to point where the original next header starts */
    527 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
    528 	free(ip6af, M_FTABLE);
    529 	ip6 = mtod(m, struct ip6_hdr *);
    530 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
    531 	ip6->ip6_src = q6->ip6q_src;
    532 	ip6->ip6_dst = q6->ip6q_dst;
    533 	nxt = q6->ip6q_nxt;
    534 #ifdef notyet
    535 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
    536 #endif
    537 
    538 	/*
    539 	 * Delete frag6 header with as a few cost as possible.
    540 	 */
    541 	if (offset < m->m_len) {
    542 		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
    543 			offset);
    544 		m->m_data += sizeof(struct ip6_frag);
    545 		m->m_len -= sizeof(struct ip6_frag);
    546 	} else {
    547 		/* this comes with no copy if the boundary is on cluster */
    548 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
    549 			frag6_remque(q6);
    550 			free(q6, M_FTABLE);
    551 			frag6_nfragpackets--;
    552 			goto dropfrag;
    553 		}
    554 		m_adj(t, sizeof(struct ip6_frag));
    555 		m_cat(m, t);
    556 	}
    557 
    558 	/*
    559 	 * Store NXT to the original.
    560 	 */
    561 	{
    562 		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
    563 		*prvnxtp = nxt;
    564 	}
    565 
    566 	frag6_remque(q6);
    567 	free(q6, M_FTABLE);
    568 	frag6_nfragpackets--;
    569 
    570 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
    571 		int plen = 0;
    572 		for (t = m; t; t = t->m_next)
    573 			plen += t->m_len;
    574 		m->m_pkthdr.len = plen;
    575 	}
    576 
    577 	ip6stat.ip6s_reassembled++;
    578 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
    579 
    580 	/*
    581 	 * Tell launch routine the next header
    582 	 */
    583 
    584 	*mp = m;
    585 	*offp = offset;
    586 
    587 	IP6Q_UNLOCK();
    588 	return nxt;
    589 
    590  dropfrag:
    591 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
    592 	ip6stat.ip6s_fragdropped++;
    593 	m_freem(m);
    594 	IP6Q_UNLOCK();
    595 	return IPPROTO_DONE;
    596 }
    597 
    598 /*
    599  * Free a fragment reassembly header and all
    600  * associated datagrams.
    601  */
    602 void
    603 frag6_freef(q6)
    604 	struct ip6q *q6;
    605 {
    606 	struct ip6asfrag *af6, *down6;
    607 
    608 	IP6Q_LOCK_CHECK();
    609 
    610 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
    611 	     af6 = down6) {
    612 		struct mbuf *m = IP6_REASS_MBUF(af6);
    613 
    614 		down6 = af6->ip6af_down;
    615 		frag6_deq(af6);
    616 
    617 		/*
    618 		 * Return ICMP time exceeded error for the 1st fragment.
    619 		 * Just free other fragments.
    620 		 */
    621 		if (af6->ip6af_off == 0) {
    622 			struct ip6_hdr *ip6;
    623 
    624 			/* adjust pointer */
    625 			ip6 = mtod(m, struct ip6_hdr *);
    626 
    627 			/* restoure source and destination addresses */
    628 			ip6->ip6_src = q6->ip6q_src;
    629 			ip6->ip6_dst = q6->ip6q_dst;
    630 
    631 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
    632 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
    633 		} else
    634 			m_freem(m);
    635 		free(af6, M_FTABLE);
    636 	}
    637 	frag6_remque(q6);
    638 	free(q6, M_FTABLE);
    639 	frag6_nfragpackets--;
    640 }
    641 
    642 /*
    643  * Put an ip fragment on a reassembly chain.
    644  * Like insque, but pointers in middle of structure.
    645  */
    646 void
    647 frag6_enq(af6, up6)
    648 	struct ip6asfrag *af6, *up6;
    649 {
    650 
    651 	IP6Q_LOCK_CHECK();
    652 
    653 	af6->ip6af_up = up6;
    654 	af6->ip6af_down = up6->ip6af_down;
    655 	up6->ip6af_down->ip6af_up = af6;
    656 	up6->ip6af_down = af6;
    657 }
    658 
    659 /*
    660  * To frag6_enq as remque is to insque.
    661  */
    662 void
    663 frag6_deq(af6)
    664 	struct ip6asfrag *af6;
    665 {
    666 
    667 	IP6Q_LOCK_CHECK();
    668 
    669 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
    670 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
    671 }
    672 
    673 void
    674 frag6_insque(new, old)
    675 	struct ip6q *new, *old;
    676 {
    677 
    678 	IP6Q_LOCK_CHECK();
    679 
    680 	new->ip6q_prev = old;
    681 	new->ip6q_next = old->ip6q_next;
    682 	old->ip6q_next->ip6q_prev= new;
    683 	old->ip6q_next = new;
    684 }
    685 
    686 void
    687 frag6_remque(p6)
    688 	struct ip6q *p6;
    689 {
    690 
    691 	IP6Q_LOCK_CHECK();
    692 
    693 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
    694 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
    695 }
    696 
    697 /*
    698  * IPv6 reassembling timer processing;
    699  * if a timer expires on a reassembly
    700  * queue, discard it.
    701  */
    702 void
    703 frag6_slowtimo()
    704 {
    705 	struct ip6q *q6;
    706 	int s = splsoftnet();
    707 
    708 	IP6Q_LOCK();
    709 	q6 = ip6q.ip6q_next;
    710 	if (q6)
    711 		while (q6 != &ip6q) {
    712 			--q6->ip6q_ttl;
    713 			q6 = q6->ip6q_next;
    714 			if (q6->ip6q_prev->ip6q_ttl == 0) {
    715 				ip6stat.ip6s_fragtimeout++;
    716 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    717 				frag6_freef(q6->ip6q_prev);
    718 			}
    719 		}
    720 	/*
    721 	 * If we are over the maximum number of fragments
    722 	 * (due to the limit being lowered), drain off
    723 	 * enough to get down to the new limit.
    724 	 */
    725 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
    726 	    ip6q.ip6q_prev) {
    727 		ip6stat.ip6s_fragoverflow++;
    728 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    729 		frag6_freef(ip6q.ip6q_prev);
    730 	}
    731 	IP6Q_UNLOCK();
    732 
    733 #if 0
    734 	/*
    735 	 * Routing changes might produce a better route than we last used;
    736 	 * make sure we notice eventually, even if forwarding only for one
    737 	 * destination and the cache is never replaced.
    738 	 */
    739 	if (ip6_forward_rt.ro_rt) {
    740 		RTFREE(ip6_forward_rt.ro_rt);
    741 		ip6_forward_rt.ro_rt = 0;
    742 	}
    743 	if (ipsrcchk_rt.ro_rt) {
    744 		RTFREE(ipsrcchk_rt.ro_rt);
    745 		ipsrcchk_rt.ro_rt = 0;
    746 	}
    747 #endif
    748 
    749 	splx(s);
    750 }
    751 
    752 /*
    753  * Drain off all datagram fragments.
    754  */
    755 void
    756 frag6_drain()
    757 {
    758 
    759 	if (ip6q_lock_try() == 0)
    760 		return;
    761 	while (ip6q.ip6q_next != &ip6q) {
    762 		ip6stat.ip6s_fragdropped++;
    763 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
    764 		frag6_freef(ip6q.ip6q_next);
    765 	}
    766 	IP6Q_UNLOCK();
    767 }
    768