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