frag6.c revision 170275
162587Sitojun/*	$FreeBSD: head/sys/netinet6/frag6.c 170275 2007-06-04 06:06:35Z jinmei $	*/
295023Ssuz/*	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $	*/
362587Sitojun
4139826Simp/*-
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3353541Sshin#include <sys/param.h>
3453541Sshin#include <sys/systm.h>
3553541Sshin#include <sys/malloc.h>
3653541Sshin#include <sys/mbuf.h>
3753541Sshin#include <sys/domain.h>
3853541Sshin#include <sys/protosw.h>
3953541Sshin#include <sys/socket.h>
4053541Sshin#include <sys/errno.h>
4153541Sshin#include <sys/time.h>
4253541Sshin#include <sys/kernel.h>
4353541Sshin#include <sys/syslog.h>
4453541Sshin
4553541Sshin#include <net/if.h>
4653541Sshin#include <net/route.h>
4753541Sshin
4853541Sshin#include <netinet/in.h>
4953541Sshin#include <netinet/in_var.h>
5062587Sitojun#include <netinet/ip6.h>
5153541Sshin#include <netinet6/ip6_var.h>
5262587Sitojun#include <netinet/icmp6.h>
53121684Sume#include <netinet/in_systm.h>	/* for ECN definitions */
54121684Sume#include <netinet/ip.h>		/* for ECN definitions */
5553541Sshin
5653541Sshin/*
5753541Sshin * Define it to get a correct behavior on per-interface statistics.
5853541Sshin * You will need to perform an extra routing table lookup, per fragment,
5953541Sshin * to do it.  This may, or may not be, a performance hit.
6053541Sshin */
6162587Sitojun#define IN6_IFSTAT_STRICT
6253541Sshin
6362587Sitojunstatic void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
6462587Sitojunstatic void frag6_deq __P((struct ip6asfrag *));
6562587Sitojunstatic void frag6_insque __P((struct ip6q *, struct ip6q *));
6662587Sitojunstatic void frag6_remque __P((struct ip6q *));
6762587Sitojunstatic void frag6_freef __P((struct ip6q *));
6853541Sshin
69121346Sumestatic struct mtx ip6qlock;
70121346Sume/*
71121346Sume * These fields all protected by ip6qlock.
72121346Sume */
73121346Sumestatic u_int frag6_nfragpackets;
74121346Sumestatic u_int frag6_nfrags;
75121346Sumestatic struct	ip6q ip6q;	/* ip6 reassemble queue */
7653541Sshin
77121346Sume#define	IP6Q_LOCK_INIT()	mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
78121346Sume#define	IP6Q_LOCK()		mtx_lock(&ip6qlock)
79121346Sume#define	IP6Q_TRYLOCK()		mtx_trylock(&ip6qlock)
80121355Sume#define	IP6Q_LOCK_ASSERT()	mtx_assert(&ip6qlock, MA_OWNED)
81121346Sume#define	IP6Q_UNLOCK()		mtx_unlock(&ip6qlock)
82121345Sume
8369774Sphkstatic MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
8462587Sitojun
8553541Sshin/*
8653541Sshin * Initialise reassembly queue and fragment identifier.
8753541Sshin */
88157927Spsstatic void
89157927Spsfrag6_change(void *tag)
90157927Sps{
91157927Sps
92157927Sps	ip6_maxfragpackets = nmbclusters / 4;
93157927Sps	ip6_maxfrags = nmbclusters / 4;
94157927Sps}
95157927Sps
9653541Sshinvoid
9753541Sshinfrag6_init()
9853541Sshin{
9953541Sshin
10077969Sjesper	ip6_maxfragpackets = nmbclusters / 4;
101121345Sume	ip6_maxfrags = nmbclusters / 4;
102157927Sps	EVENTHANDLER_REGISTER(nmbclusters_change,
103157927Sps	    frag6_change, NULL, EVENTHANDLER_PRI_ANY);
10477969Sjesper
105121346Sume	IP6Q_LOCK_INIT();
106121346Sume
10753541Sshin	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
10853541Sshin}
10953541Sshin
11053541Sshin/*
11162587Sitojun * In RFC2460, fragment and reassembly rule do not agree with each other,
11262587Sitojun * in terms of next header field handling in fragment header.
11362587Sitojun * While the sender will use the same value for all of the fragmented packets,
11462587Sitojun * receiver is suggested not to check the consistency.
11562587Sitojun *
11662587Sitojun * fragment rule (p20):
11762587Sitojun *	(2) A Fragment header containing:
11862587Sitojun *	The Next Header value that identifies the first header of
11962587Sitojun *	the Fragmentable Part of the original packet.
12062587Sitojun *		-> next header field is same for all fragments
12162587Sitojun *
12262587Sitojun * reassembly rule (p21):
12362587Sitojun *	The Next Header field of the last header of the Unfragmentable
12462587Sitojun *	Part is obtained from the Next Header field of the first
12562587Sitojun *	fragment's Fragment header.
12662587Sitojun *		-> should grab it from the first fragment only
12762587Sitojun *
12862587Sitojun * The following note also contradicts with fragment rule - noone is going to
12962587Sitojun * send different fragment with different next header field.
13062587Sitojun *
13162587Sitojun * additional note (p22):
13262587Sitojun *	The Next Header values in the Fragment headers of different
13362587Sitojun *	fragments of the same original packet may differ.  Only the value
13462587Sitojun *	from the Offset zero fragment packet is used for reassembly.
13562587Sitojun *		-> should grab it from the first fragment only
13662587Sitojun *
13762587Sitojun * There is no explicit reason given in the RFC.  Historical reason maybe?
13862587Sitojun */
13962587Sitojun/*
14053541Sshin * Fragment input
14153541Sshin */
14253541Sshinint
14353541Sshinfrag6_input(mp, offp, proto)
14453541Sshin	struct mbuf **mp;
14553541Sshin	int *offp, proto;
14653541Sshin{
14753541Sshin	struct mbuf *m = *mp, *t;
14853541Sshin	struct ip6_hdr *ip6;
14953541Sshin	struct ip6_frag *ip6f;
15053541Sshin	struct ip6q *q6;
15162587Sitojun	struct ip6asfrag *af6, *ip6af, *af6dwn;
152121630Sume#ifdef IN6_IFSTAT_STRICT
153121630Sume	struct in6_ifaddr *ia;
154121630Sume#endif
15553541Sshin	int offset = *offp, nxt, i, next;
15653541Sshin	int first_frag = 0;
15762587Sitojun	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
15853541Sshin	struct ifnet *dstifp;
159121684Sume	u_int8_t ecn, ecn0;
160165118Sbz#if 0
161165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
162165118Sbz#endif
16353541Sshin
16462587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
16562587Sitojun#ifndef PULLDOWN_TEST
16653541Sshin	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
16753541Sshin	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
16862587Sitojun#else
16962587Sitojun	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
17062587Sitojun	if (ip6f == NULL)
171120856Sume		return (IPPROTO_DONE);
17262587Sitojun#endif
17353541Sshin
17453541Sshin	dstifp = NULL;
17553541Sshin#ifdef IN6_IFSTAT_STRICT
17653541Sshin	/* find the destination interface of the packet. */
177121630Sume	if ((ia = ip6_getdstifaddr(m)) != NULL)
178121630Sume		dstifp = ia->ia_ifp;
17953541Sshin#else
18053541Sshin	/* we are violating the spec, this is not the destination interface */
18153541Sshin	if ((m->m_flags & M_PKTHDR) != 0)
18253541Sshin		dstifp = m->m_pkthdr.rcvif;
18353541Sshin#endif
18453541Sshin
18553541Sshin	/* jumbo payload can't contain a fragment header */
18653541Sshin	if (ip6->ip6_plen == 0) {
18753541Sshin		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
18853541Sshin		in6_ifstat_inc(dstifp, ifs6_reass_fail);
18953541Sshin		return IPPROTO_DONE;
19053541Sshin	}
19153541Sshin
19253541Sshin	/*
19353541Sshin	 * check whether fragment packet's fragment length is
19453541Sshin	 * multiple of 8 octets.
19553541Sshin	 * sizeof(struct ip6_frag) == 8
19653541Sshin	 * sizeof(struct ip6_hdr) = 40
19753541Sshin	 */
19853541Sshin	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
19953541Sshin	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
200120891Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
201120891Sume		    offsetof(struct ip6_hdr, ip6_plen));
20253541Sshin		in6_ifstat_inc(dstifp, ifs6_reass_fail);
20353541Sshin		return IPPROTO_DONE;
20453541Sshin	}
20553541Sshin
20653541Sshin	ip6stat.ip6s_fragments++;
20753541Sshin	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
208120891Sume
20962587Sitojun	/* offset now points to data portion */
21053541Sshin	offset += sizeof(struct ip6_frag);
21153541Sshin
212121345Sume	IP6Q_LOCK();
21378064Sume
214121345Sume	/*
215121345Sume	 * Enforce upper bound on number of fragments.
216121345Sume	 * If maxfrag is 0, never accept fragments.
217121345Sume	 * If maxfrag is -1, accept all fragments without limitation.
218121345Sume	 */
219121345Sume	if (ip6_maxfrags < 0)
220121345Sume		;
221121345Sume	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
222121345Sume		goto dropfrag;
223121345Sume
22453541Sshin	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
22553541Sshin		if (ip6f->ip6f_ident == q6->ip6q_ident &&
22653541Sshin		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
22753541Sshin		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
22853541Sshin			break;
22953541Sshin
23053541Sshin	if (q6 == &ip6q) {
23153541Sshin		/*
23253541Sshin		 * the first fragment to arrive, create a reassembly queue.
23353541Sshin		 */
23453541Sshin		first_frag = 1;
23553541Sshin
23653541Sshin		/*
23753541Sshin		 * Enforce upper bound on number of fragmented packets
23853541Sshin		 * for which we attempt reassembly;
239121345Sume		 * If maxfragpackets is 0, never accept fragments.
240121345Sume		 * If maxfragpackets is -1, accept all fragments without
241121345Sume		 * limitation.
24253541Sshin		 */
24378064Sume		if (ip6_maxfragpackets < 0)
24478064Sume			;
24578064Sume		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
24678064Sume			goto dropfrag;
24778064Sume		frag6_nfragpackets++;
24853541Sshin		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
249121607Sume		    M_NOWAIT);
25053541Sshin		if (q6 == NULL)
25153541Sshin			goto dropfrag;
25262587Sitojun		bzero(q6, sizeof(*q6));
25353541Sshin
25453541Sshin		frag6_insque(q6, &ip6q);
25553541Sshin
25662587Sitojun		/* ip6q_nxt will be filled afterwards, from 1st fragment */
25753541Sshin		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
25862587Sitojun#ifdef notyet
25962587Sitojun		q6->ip6q_nxtp	= (u_char *)nxtp;
26062587Sitojun#endif
26153541Sshin		q6->ip6q_ident	= ip6f->ip6f_ident;
26253541Sshin		q6->ip6q_ttl 	= IPV6_FRAGTTL;
26353541Sshin		q6->ip6q_src	= ip6->ip6_src;
26453541Sshin		q6->ip6q_dst	= ip6->ip6_dst;
265170275Sjinmei		q6->ip6q_ecn	=
266170275Sjinmei		    (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
26753541Sshin		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
268121345Sume
269121345Sume		q6->ip6q_nfrag = 0;
27053541Sshin	}
27153541Sshin
27253541Sshin	/*
27353541Sshin	 * If it's the 1st fragment, record the length of the
27453541Sshin	 * unfragmentable part and the next header of the fragment header.
27553541Sshin	 */
27653541Sshin	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
27753541Sshin	if (fragoff == 0) {
278120891Sume		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
279120891Sume		    sizeof(struct ip6_frag);
28053541Sshin		q6->ip6q_nxt = ip6f->ip6f_nxt;
28153541Sshin	}
28253541Sshin
28353541Sshin	/*
28453541Sshin	 * Check that the reassembled packet would not exceed 65535 bytes
28553541Sshin	 * in size.
28653541Sshin	 * If it would exceed, discard the fragment and return an ICMP error.
28753541Sshin	 */
28862587Sitojun	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
28953541Sshin	if (q6->ip6q_unfrglen >= 0) {
29053541Sshin		/* The 1st fragment has already arrived. */
29153541Sshin		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
29253541Sshin			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
293120891Sume			    offset - sizeof(struct ip6_frag) +
294120891Sume			    offsetof(struct ip6_frag, ip6f_offlg));
295121345Sume			IP6Q_UNLOCK();
296120856Sume			return (IPPROTO_DONE);
29753541Sshin		}
298120891Sume	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
29953541Sshin		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
300120891Sume		    offset - sizeof(struct ip6_frag) +
301120891Sume		    offsetof(struct ip6_frag, ip6f_offlg));
302121345Sume		IP6Q_UNLOCK();
303120856Sume		return (IPPROTO_DONE);
30453541Sshin	}
30553541Sshin	/*
30653541Sshin	 * If it's the first fragment, do the above check for each
30753541Sshin	 * fragment already stored in the reassembly queue.
30853541Sshin	 */
30953541Sshin	if (fragoff == 0) {
31053541Sshin		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
31153541Sshin		     af6 = af6dwn) {
31253541Sshin			af6dwn = af6->ip6af_down;
31353541Sshin
31453541Sshin			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
31553541Sshin			    IPV6_MAXPACKET) {
31653541Sshin				struct mbuf *merr = IP6_REASS_MBUF(af6);
31753541Sshin				struct ip6_hdr *ip6err;
31853541Sshin				int erroff = af6->ip6af_offset;
31953541Sshin
32053541Sshin				/* dequeue the fragment. */
32153541Sshin				frag6_deq(af6);
32262587Sitojun				free(af6, M_FTABLE);
32353541Sshin
32453541Sshin				/* adjust pointer. */
32553541Sshin				ip6err = mtod(merr, struct ip6_hdr *);
32653541Sshin
32753541Sshin				/*
32853541Sshin				 * Restore source and destination addresses
32953541Sshin				 * in the erroneous IPv6 header.
33053541Sshin				 */
33153541Sshin				ip6err->ip6_src = q6->ip6q_src;
33253541Sshin				ip6err->ip6_dst = q6->ip6q_dst;
33353541Sshin
33453541Sshin				icmp6_error(merr, ICMP6_PARAM_PROB,
335120891Sume				    ICMP6_PARAMPROB_HEADER,
336120891Sume				    erroff - sizeof(struct ip6_frag) +
337120891Sume				    offsetof(struct ip6_frag, ip6f_offlg));
33853541Sshin			}
33953541Sshin		}
34053541Sshin	}
34153541Sshin
34262587Sitojun	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
343121607Sume	    M_NOWAIT);
34462587Sitojun	if (ip6af == NULL)
34562587Sitojun		goto dropfrag;
34662587Sitojun	bzero(ip6af, sizeof(*ip6af));
34753541Sshin	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
34853541Sshin	ip6af->ip6af_off = fragoff;
34953541Sshin	ip6af->ip6af_frglen = frgpartlen;
35053541Sshin	ip6af->ip6af_offset = offset;
35153541Sshin	IP6_REASS_MBUF(ip6af) = m;
35253541Sshin
35353541Sshin	if (first_frag) {
35453541Sshin		af6 = (struct ip6asfrag *)q6;
35553541Sshin		goto insert;
35653541Sshin	}
35753541Sshin
35853541Sshin	/*
359121684Sume	 * Handle ECN by comparing this segment with the first one;
360121684Sume	 * if CE is set, do not lose CE.
361121684Sume	 * drop if CE and not-ECT are mixed for the same packet.
362121684Sume	 */
363121684Sume	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
364170275Sjinmei	ecn0 = q6->ip6q_ecn;
365121684Sume	if (ecn == IPTOS_ECN_CE) {
366121684Sume		if (ecn0 == IPTOS_ECN_NOTECT) {
367121684Sume			free(ip6af, M_FTABLE);
368121684Sume			goto dropfrag;
369121684Sume		}
370121684Sume		if (ecn0 != IPTOS_ECN_CE)
371170275Sjinmei			q6->ip6q_ecn = IPTOS_ECN_CE;
372121684Sume	}
373121684Sume	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
374121684Sume		free(ip6af, M_FTABLE);
375121684Sume		goto dropfrag;
376121684Sume	}
377121684Sume
378121684Sume	/*
37953541Sshin	 * Find a segment which begins after this one does.
38053541Sshin	 */
38153541Sshin	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
38253541Sshin	     af6 = af6->ip6af_down)
38353541Sshin		if (af6->ip6af_off > ip6af->ip6af_off)
38453541Sshin			break;
38553541Sshin
38662587Sitojun#if 0
38753541Sshin	/*
38862587Sitojun	 * If there is a preceding segment, it may provide some of
38962587Sitojun	 * our data already.  If so, drop the data from the incoming
39062587Sitojun	 * segment.  If it provides all of our data, drop us.
39162587Sitojun	 */
39262587Sitojun	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
39362587Sitojun		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
39462587Sitojun			- ip6af->ip6af_off;
39562587Sitojun		if (i > 0) {
39662587Sitojun			if (i >= ip6af->ip6af_frglen)
39762587Sitojun				goto dropfrag;
39862587Sitojun			m_adj(IP6_REASS_MBUF(ip6af), i);
39962587Sitojun			ip6af->ip6af_off += i;
40062587Sitojun			ip6af->ip6af_frglen -= i;
40162587Sitojun		}
40262587Sitojun	}
40362587Sitojun
40462587Sitojun	/*
40562587Sitojun	 * While we overlap succeeding segments trim them or,
40662587Sitojun	 * if they are completely covered, dequeue them.
40762587Sitojun	 */
40862587Sitojun	while (af6 != (struct ip6asfrag *)q6 &&
40962587Sitojun	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
41062587Sitojun		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
41162587Sitojun		if (i < af6->ip6af_frglen) {
41262587Sitojun			af6->ip6af_frglen -= i;
41362587Sitojun			af6->ip6af_off += i;
41462587Sitojun			m_adj(IP6_REASS_MBUF(af6), i);
41562587Sitojun			break;
41662587Sitojun		}
41762587Sitojun		af6 = af6->ip6af_down;
41862587Sitojun		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
41962587Sitojun		frag6_deq(af6->ip6af_up);
42062587Sitojun	}
42162587Sitojun#else
42262587Sitojun	/*
42353541Sshin	 * If the incoming framgent overlaps some existing fragments in
42453541Sshin	 * the reassembly queue, drop it, since it is dangerous to override
42553541Sshin	 * existing fragments from a security point of view.
426121345Sume	 * We don't know which fragment is the bad guy - here we trust
427121345Sume	 * fragment that came in earlier, with no real reason.
428170275Sjinmei	 *
429170275Sjinmei	 * Note: due to changes after disabling this part, mbuf passed to
430170275Sjinmei	 * m_adj() below now does not meet the requirement.
43153541Sshin	 */
43253541Sshin	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
43353541Sshin		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
43453541Sshin			- ip6af->ip6af_off;
43553541Sshin		if (i > 0) {
43676899Ssumikawa#if 0				/* suppress the noisy log */
43753541Sshin			log(LOG_ERR, "%d bytes of a fragment from %s "
43853541Sshin			    "overlaps the previous fragment\n",
439165118Sbz			    i, ip6_sprintf(ip6buf, &q6->ip6q_src));
44076899Ssumikawa#endif
44176899Ssumikawa			free(ip6af, M_FTABLE);
44253541Sshin			goto dropfrag;
44353541Sshin		}
44453541Sshin	}
44553541Sshin	if (af6 != (struct ip6asfrag *)q6) {
44653541Sshin		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
44753541Sshin		if (i > 0) {
44876899Ssumikawa#if 0				/* suppress the noisy log */
44953541Sshin			log(LOG_ERR, "%d bytes of a fragment from %s "
45053541Sshin			    "overlaps the succeeding fragment",
451165118Sbz			    i, ip6_sprintf(ip6buf, &q6->ip6q_src));
45276899Ssumikawa#endif
45376899Ssumikawa			free(ip6af, M_FTABLE);
45453541Sshin			goto dropfrag;
45553541Sshin		}
45653541Sshin	}
45762587Sitojun#endif
45853541Sshin
45953541Sshininsert:
46053541Sshin
46153541Sshin	/*
46253541Sshin	 * Stick new segment in its place;
46353541Sshin	 * check for complete reassembly.
46453541Sshin	 * Move to front of packet queue, as we are
46553541Sshin	 * the most recently active fragmented packet.
46653541Sshin	 */
46753541Sshin	frag6_enq(ip6af, af6->ip6af_up);
468121345Sume	frag6_nfrags++;
469121345Sume	q6->ip6q_nfrag++;
47062587Sitojun#if 0 /* xxx */
47162587Sitojun	if (q6 != ip6q.ip6q_next) {
47262587Sitojun		frag6_remque(q6);
47362587Sitojun		frag6_insque(q6, &ip6q);
47462587Sitojun	}
47562587Sitojun#endif
47653541Sshin	next = 0;
47753541Sshin	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
47853541Sshin	     af6 = af6->ip6af_down) {
47953541Sshin		if (af6->ip6af_off != next) {
480121345Sume			IP6Q_UNLOCK();
48153541Sshin			return IPPROTO_DONE;
48253541Sshin		}
48353541Sshin		next += af6->ip6af_frglen;
48453541Sshin	}
48553541Sshin	if (af6->ip6af_up->ip6af_mff) {
486121345Sume		IP6Q_UNLOCK();
48753541Sshin		return IPPROTO_DONE;
48853541Sshin	}
48953541Sshin
49053541Sshin	/*
49153541Sshin	 * Reassembly is complete; concatenate fragments.
49253541Sshin	 */
49353541Sshin	ip6af = q6->ip6q_down;
49453541Sshin	t = m = IP6_REASS_MBUF(ip6af);
49553541Sshin	af6 = ip6af->ip6af_down;
49662587Sitojun	frag6_deq(ip6af);
49753541Sshin	while (af6 != (struct ip6asfrag *)q6) {
49862587Sitojun		af6dwn = af6->ip6af_down;
49962587Sitojun		frag6_deq(af6);
50053541Sshin		while (t->m_next)
50153541Sshin			t = t->m_next;
50253541Sshin		t->m_next = IP6_REASS_MBUF(af6);
50362587Sitojun		m_adj(t->m_next, af6->ip6af_offset);
50462587Sitojun		free(af6, M_FTABLE);
50562587Sitojun		af6 = af6dwn;
50653541Sshin	}
50753541Sshin
50853541Sshin	/* adjust offset to point where the original next header starts */
50953541Sshin	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
51062587Sitojun	free(ip6af, M_FTABLE);
51162587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
51253541Sshin	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
513170275Sjinmei	if (q6->ip6q_ecn == IPTOS_ECN_CE)
514170275Sjinmei		ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
51553541Sshin	nxt = q6->ip6q_nxt;
51662587Sitojun#ifdef notyet
51762587Sitojun	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
51862587Sitojun#endif
51953541Sshin
520170275Sjinmei	/* Delete frag6 header */
521170275Sjinmei	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
522170275Sjinmei		/* This is the only possible case with !PULLDOWN_TEST */
52353541Sshin		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
524170275Sjinmei		    offset);
52562587Sitojun		m->m_data += sizeof(struct ip6_frag);
52662587Sitojun		m->m_len -= sizeof(struct ip6_frag);
52762587Sitojun	} else {
52862587Sitojun		/* this comes with no copy if the boundary is on cluster */
529111119Simp		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
53062587Sitojun			frag6_remque(q6);
531121345Sume			frag6_nfrags -= q6->ip6q_nfrag;
53262587Sitojun			free(q6, M_FTABLE);
53362587Sitojun			frag6_nfragpackets--;
53462587Sitojun			goto dropfrag;
53562587Sitojun		}
53662587Sitojun		m_adj(t, sizeof(struct ip6_frag));
53762587Sitojun		m_cat(m, t);
53853541Sshin	}
53953541Sshin
54053541Sshin	/*
54153541Sshin	 * Store NXT to the original.
54253541Sshin	 */
54353541Sshin	{
54453541Sshin		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
54553541Sshin		*prvnxtp = nxt;
54653541Sshin	}
54753541Sshin
54853541Sshin	frag6_remque(q6);
549121345Sume	frag6_nfrags -= q6->ip6q_nfrag;
55053541Sshin	free(q6, M_FTABLE);
55153541Sshin	frag6_nfragpackets--;
55253541Sshin
55353541Sshin	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
55453541Sshin		int plen = 0;
55553541Sshin		for (t = m; t; t = t->m_next)
55653541Sshin			plen += t->m_len;
55753541Sshin		m->m_pkthdr.len = plen;
55853541Sshin	}
559120891Sume
56053541Sshin	ip6stat.ip6s_reassembled++;
56153541Sshin	in6_ifstat_inc(dstifp, ifs6_reass_ok);
56253541Sshin
56353541Sshin	/*
56453541Sshin	 * Tell launch routine the next header
56553541Sshin	 */
56653541Sshin
56753541Sshin	*mp = m;
56853541Sshin	*offp = offset;
56953541Sshin
570121345Sume	IP6Q_UNLOCK();
57153541Sshin	return nxt;
57253541Sshin
57353541Sshin dropfrag:
574121346Sume	IP6Q_UNLOCK();
57553541Sshin	in6_ifstat_inc(dstifp, ifs6_reass_fail);
57653541Sshin	ip6stat.ip6s_fragdropped++;
57753541Sshin	m_freem(m);
57853541Sshin	return IPPROTO_DONE;
57953541Sshin}
58053541Sshin
58153541Sshin/*
58253541Sshin * Free a fragment reassembly header and all
58353541Sshin * associated datagrams.
58453541Sshin */
58553541Sshinvoid
58653541Sshinfrag6_freef(q6)
58753541Sshin	struct ip6q *q6;
58853541Sshin{
58953541Sshin	struct ip6asfrag *af6, *down6;
59053541Sshin
591121355Sume	IP6Q_LOCK_ASSERT();
592121345Sume
59353541Sshin	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
59453541Sshin	     af6 = down6) {
59553541Sshin		struct mbuf *m = IP6_REASS_MBUF(af6);
59653541Sshin
59753541Sshin		down6 = af6->ip6af_down;
59853541Sshin		frag6_deq(af6);
59953541Sshin
60053541Sshin		/*
60153541Sshin		 * Return ICMP time exceeded error for the 1st fragment.
60253541Sshin		 * Just free other fragments.
60353541Sshin		 */
60453541Sshin		if (af6->ip6af_off == 0) {
60553541Sshin			struct ip6_hdr *ip6;
60653541Sshin
60753541Sshin			/* adjust pointer */
60853541Sshin			ip6 = mtod(m, struct ip6_hdr *);
60953541Sshin
610120891Sume			/* restore source and destination addresses */
61153541Sshin			ip6->ip6_src = q6->ip6q_src;
61253541Sshin			ip6->ip6_dst = q6->ip6q_dst;
61353541Sshin
61453541Sshin			icmp6_error(m, ICMP6_TIME_EXCEEDED,
61553541Sshin				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
61662587Sitojun		} else
61753541Sshin			m_freem(m);
61862587Sitojun		free(af6, M_FTABLE);
61953541Sshin	}
62053541Sshin	frag6_remque(q6);
621121345Sume	frag6_nfrags -= q6->ip6q_nfrag;
62253541Sshin	free(q6, M_FTABLE);
62353541Sshin	frag6_nfragpackets--;
62453541Sshin}
62553541Sshin
62653541Sshin/*
62753541Sshin * Put an ip fragment on a reassembly chain.
62853541Sshin * Like insque, but pointers in middle of structure.
62953541Sshin */
63053541Sshinvoid
63153541Sshinfrag6_enq(af6, up6)
63253541Sshin	struct ip6asfrag *af6, *up6;
63353541Sshin{
634121345Sume
635121355Sume	IP6Q_LOCK_ASSERT();
636121345Sume
63753541Sshin	af6->ip6af_up = up6;
63853541Sshin	af6->ip6af_down = up6->ip6af_down;
63953541Sshin	up6->ip6af_down->ip6af_up = af6;
64053541Sshin	up6->ip6af_down = af6;
64153541Sshin}
64253541Sshin
64353541Sshin/*
64453541Sshin * To frag6_enq as remque is to insque.
64553541Sshin */
64653541Sshinvoid
64753541Sshinfrag6_deq(af6)
64853541Sshin	struct ip6asfrag *af6;
64953541Sshin{
650121345Sume
651121355Sume	IP6Q_LOCK_ASSERT();
652121345Sume
65353541Sshin	af6->ip6af_up->ip6af_down = af6->ip6af_down;
65453541Sshin	af6->ip6af_down->ip6af_up = af6->ip6af_up;
65553541Sshin}
65653541Sshin
65753541Sshinvoid
65853541Sshinfrag6_insque(new, old)
65953541Sshin	struct ip6q *new, *old;
66053541Sshin{
661121345Sume
662121355Sume	IP6Q_LOCK_ASSERT();
663121345Sume
66453541Sshin	new->ip6q_prev = old;
66553541Sshin	new->ip6q_next = old->ip6q_next;
66653541Sshin	old->ip6q_next->ip6q_prev= new;
66753541Sshin	old->ip6q_next = new;
66853541Sshin}
66953541Sshin
67053541Sshinvoid
67153541Sshinfrag6_remque(p6)
67253541Sshin	struct ip6q *p6;
67353541Sshin{
674121345Sume
675121355Sume	IP6Q_LOCK_ASSERT();
676121345Sume
67753541Sshin	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
67853541Sshin	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
67953541Sshin}
68053541Sshin
68153541Sshin/*
68278064Sume * IPv6 reassembling timer processing;
68353541Sshin * if a timer expires on a reassembly
68453541Sshin * queue, discard it.
68553541Sshin */
68653541Sshinvoid
68753541Sshinfrag6_slowtimo()
68853541Sshin{
68953541Sshin	struct ip6q *q6;
69053541Sshin
691158295Sbz#if 0
692158295Sbz	GIANT_REQUIRED;	/* XXX bz: ip6_forward_rt */
693158295Sbz#endif
694158295Sbz
695121345Sume	IP6Q_LOCK();
69653541Sshin	q6 = ip6q.ip6q_next;
69753541Sshin	if (q6)
69853541Sshin		while (q6 != &ip6q) {
69953541Sshin			--q6->ip6q_ttl;
70053541Sshin			q6 = q6->ip6q_next;
70153541Sshin			if (q6->ip6q_prev->ip6q_ttl == 0) {
70253541Sshin				ip6stat.ip6s_fragtimeout++;
70353541Sshin				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
70453541Sshin				frag6_freef(q6->ip6q_prev);
70553541Sshin			}
70653541Sshin		}
70753541Sshin	/*
70853541Sshin	 * If we are over the maximum number of fragments
70953541Sshin	 * (due to the limit being lowered), drain off
71053541Sshin	 * enough to get down to the new limit.
71153541Sshin	 */
71278064Sume	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
71378064Sume	    ip6q.ip6q_prev) {
71453541Sshin		ip6stat.ip6s_fragoverflow++;
71553541Sshin		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
71653541Sshin		frag6_freef(ip6q.ip6q_prev);
71753541Sshin	}
718121345Sume	IP6Q_UNLOCK();
71962587Sitojun
72062587Sitojun#if 0
72162587Sitojun	/*
72262587Sitojun	 * Routing changes might produce a better route than we last used;
72362587Sitojun	 * make sure we notice eventually, even if forwarding only for one
72462587Sitojun	 * destination and the cache is never replaced.
72562587Sitojun	 */
72662587Sitojun	if (ip6_forward_rt.ro_rt) {
72762587Sitojun		RTFREE(ip6_forward_rt.ro_rt);
72862587Sitojun		ip6_forward_rt.ro_rt = 0;
72962587Sitojun	}
73062587Sitojun	if (ipsrcchk_rt.ro_rt) {
73162587Sitojun		RTFREE(ipsrcchk_rt.ro_rt);
73262587Sitojun		ipsrcchk_rt.ro_rt = 0;
73362587Sitojun	}
73462587Sitojun#endif
73553541Sshin}
73653541Sshin
73753541Sshin/*
73853541Sshin * Drain off all datagram fragments.
73953541Sshin */
74053541Sshinvoid
74153541Sshinfrag6_drain()
74253541Sshin{
743121345Sume
744121346Sume	if (IP6Q_TRYLOCK() == 0)
74553541Sshin		return;
74653541Sshin	while (ip6q.ip6q_next != &ip6q) {
74753541Sshin		ip6stat.ip6s_fragdropped++;
74853541Sshin		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
74953541Sshin		frag6_freef(ip6q.ip6q_next);
75053541Sshin	}
751121345Sume	IP6Q_UNLOCK();
75253541Sshin}
753