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