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