frag6.c revision 121355
162587Sitojun/*	$FreeBSD: head/sys/netinet6/frag6.c 121355 2003-10-22 19:03:49Z ume $	*/
295023Ssuz/*	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $	*/
362587Sitojun
453541Sshin/*
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
33120645Sume#include "opt_random_ip_id.h"
34120645Sume
3553541Sshin#include <sys/param.h>
3653541Sshin#include <sys/systm.h>
3753541Sshin#include <sys/malloc.h>
3853541Sshin#include <sys/mbuf.h>
3953541Sshin#include <sys/domain.h>
4053541Sshin#include <sys/protosw.h>
4153541Sshin#include <sys/socket.h>
4253541Sshin#include <sys/errno.h>
4353541Sshin#include <sys/time.h>
4453541Sshin#include <sys/kernel.h>
4553541Sshin#include <sys/syslog.h>
4653541Sshin
4753541Sshin#include <net/if.h>
4853541Sshin#include <net/route.h>
4953541Sshin
5053541Sshin#include <netinet/in.h>
5153541Sshin#include <netinet/in_var.h>
5262587Sitojun#include <netinet/ip6.h>
5353541Sshin#include <netinet6/ip6_var.h>
5462587Sitojun#include <netinet/icmp6.h>
5553541Sshin
5653541Sshin#include <net/net_osdep.h>
5753541Sshin
5853541Sshin/*
5953541Sshin * Define it to get a correct behavior on per-interface statistics.
6053541Sshin * You will need to perform an extra routing table lookup, per fragment,
6153541Sshin * to do it.  This may, or may not be, a performance hit.
6253541Sshin */
6362587Sitojun#define IN6_IFSTAT_STRICT
6453541Sshin
6562587Sitojunstatic void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
6662587Sitojunstatic void frag6_deq __P((struct ip6asfrag *));
6762587Sitojunstatic void frag6_insque __P((struct ip6q *, struct ip6q *));
6862587Sitojunstatic void frag6_remque __P((struct ip6q *));
6962587Sitojunstatic void frag6_freef __P((struct ip6q *));
7053541Sshin
71121346Sumestatic struct mtx ip6qlock;
72121346Sume/*
73121346Sume * These fields all protected by ip6qlock.
74121346Sume */
75121346Sumestatic u_int frag6_nfragpackets;
76121346Sumestatic u_int frag6_nfrags;
77121346Sumestatic struct	ip6q ip6q;	/* ip6 reassemble queue */
7853541Sshin
79121346Sume#define	IP6Q_LOCK_INIT()	mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
80121346Sume#define	IP6Q_LOCK()		mtx_lock(&ip6qlock)
81121346Sume#define	IP6Q_TRYLOCK()		mtx_trylock(&ip6qlock)
82121355Sume#define	IP6Q_LOCK_ASSERT()	mtx_assert(&ip6qlock, MA_OWNED)
83121346Sume#define	IP6Q_UNLOCK()		mtx_unlock(&ip6qlock)
84121345Sume
8569774Sphkstatic MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
8662587Sitojun
8753541Sshin/*
8853541Sshin * Initialise reassembly queue and fragment identifier.
8953541Sshin */
9053541Sshinvoid
9153541Sshinfrag6_init()
9253541Sshin{
9353541Sshin
9477969Sjesper	ip6_maxfragpackets = nmbclusters / 4;
95121345Sume	ip6_maxfrags = nmbclusters / 4;
9677969Sjesper
97121346Sume	IP6Q_LOCK_INIT();
98121346Sume
99120645Sume#ifndef RANDOM_IP_ID
100120648Sume	ip6_id = arc4random();
101120643Sume#endif
10253541Sshin	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
10353541Sshin}
10453541Sshin
10553541Sshin/*
10662587Sitojun * In RFC2460, fragment and reassembly rule do not agree with each other,
10762587Sitojun * in terms of next header field handling in fragment header.
10862587Sitojun * While the sender will use the same value for all of the fragmented packets,
10962587Sitojun * receiver is suggested not to check the consistency.
11062587Sitojun *
11162587Sitojun * fragment rule (p20):
11262587Sitojun *	(2) A Fragment header containing:
11362587Sitojun *	The Next Header value that identifies the first header of
11462587Sitojun *	the Fragmentable Part of the original packet.
11562587Sitojun *		-> next header field is same for all fragments
11662587Sitojun *
11762587Sitojun * reassembly rule (p21):
11862587Sitojun *	The Next Header field of the last header of the Unfragmentable
11962587Sitojun *	Part is obtained from the Next Header field of the first
12062587Sitojun *	fragment's Fragment header.
12162587Sitojun *		-> should grab it from the first fragment only
12262587Sitojun *
12362587Sitojun * The following note also contradicts with fragment rule - noone is going to
12462587Sitojun * send different fragment with different next header field.
12562587Sitojun *
12662587Sitojun * additional note (p22):
12762587Sitojun *	The Next Header values in the Fragment headers of different
12862587Sitojun *	fragments of the same original packet may differ.  Only the value
12962587Sitojun *	from the Offset zero fragment packet is used for reassembly.
13062587Sitojun *		-> should grab it from the first fragment only
13162587Sitojun *
13262587Sitojun * There is no explicit reason given in the RFC.  Historical reason maybe?
13362587Sitojun */
13462587Sitojun/*
13553541Sshin * Fragment input
13653541Sshin */
13753541Sshinint
13853541Sshinfrag6_input(mp, offp, proto)
13953541Sshin	struct mbuf **mp;
14053541Sshin	int *offp, proto;
14153541Sshin{
14253541Sshin	struct mbuf *m = *mp, *t;
14353541Sshin	struct ip6_hdr *ip6;
14453541Sshin	struct ip6_frag *ip6f;
14553541Sshin	struct ip6q *q6;
14662587Sitojun	struct ip6asfrag *af6, *ip6af, *af6dwn;
14753541Sshin	int offset = *offp, nxt, i, next;
14853541Sshin	int first_frag = 0;
14962587Sitojun	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
15053541Sshin	struct ifnet *dstifp;
15153541Sshin#ifdef IN6_IFSTAT_STRICT
15253541Sshin	static struct route_in6 ro;
15353541Sshin	struct sockaddr_in6 *dst;
15453541Sshin#endif
15553541Sshin
15662587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
15762587Sitojun#ifndef PULLDOWN_TEST
15853541Sshin	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
15953541Sshin	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
16062587Sitojun#else
16162587Sitojun	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
16262587Sitojun	if (ip6f == NULL)
163120856Sume		return (IPPROTO_DONE);
16462587Sitojun#endif
16553541Sshin
16653541Sshin	dstifp = NULL;
16753541Sshin#ifdef IN6_IFSTAT_STRICT
16853541Sshin	/* find the destination interface of the packet. */
16953541Sshin	dst = (struct sockaddr_in6 *)&ro.ro_dst;
17053541Sshin	if (ro.ro_rt
17153541Sshin	 && ((ro.ro_rt->rt_flags & RTF_UP) == 0
17253541Sshin	  || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
17353541Sshin		RTFREE(ro.ro_rt);
17453541Sshin		ro.ro_rt = (struct rtentry *)0;
17553541Sshin	}
17653541Sshin	if (ro.ro_rt == NULL) {
17753541Sshin		bzero(dst, sizeof(*dst));
17853541Sshin		dst->sin6_family = AF_INET6;
17953541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
18053541Sshin		dst->sin6_addr = ip6->ip6_dst;
18153541Sshin	}
18254350Sshin	rtalloc((struct route *)&ro);
18353541Sshin	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
18453541Sshin		dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
18553541Sshin#else
18653541Sshin	/* we are violating the spec, this is not the destination interface */
18753541Sshin	if ((m->m_flags & M_PKTHDR) != 0)
18853541Sshin		dstifp = m->m_pkthdr.rcvif;
18953541Sshin#endif
19053541Sshin
19153541Sshin	/* jumbo payload can't contain a fragment header */
19253541Sshin	if (ip6->ip6_plen == 0) {
19353541Sshin		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
19453541Sshin		in6_ifstat_inc(dstifp, ifs6_reass_fail);
19553541Sshin		return IPPROTO_DONE;
19653541Sshin	}
19753541Sshin
19853541Sshin	/*
19953541Sshin	 * check whether fragment packet's fragment length is
20053541Sshin	 * multiple of 8 octets.
20153541Sshin	 * sizeof(struct ip6_frag) == 8
20253541Sshin	 * sizeof(struct ip6_hdr) = 40
20353541Sshin	 */
20453541Sshin	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
20553541Sshin	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
206120891Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
207120891Sume		    offsetof(struct ip6_hdr, ip6_plen));
20853541Sshin		in6_ifstat_inc(dstifp, ifs6_reass_fail);
20953541Sshin		return IPPROTO_DONE;
21053541Sshin	}
21153541Sshin
21253541Sshin	ip6stat.ip6s_fragments++;
21353541Sshin	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
214120891Sume
21562587Sitojun	/* offset now points to data portion */
21653541Sshin	offset += sizeof(struct ip6_frag);
21753541Sshin
218121345Sume	IP6Q_LOCK();
21978064Sume
220121345Sume	/*
221121345Sume	 * Enforce upper bound on number of fragments.
222121345Sume	 * If maxfrag is 0, never accept fragments.
223121345Sume	 * If maxfrag is -1, accept all fragments without limitation.
224121345Sume	 */
225121345Sume	if (ip6_maxfrags < 0)
226121345Sume		;
227121345Sume	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
228121345Sume		goto dropfrag;
229121345Sume
23053541Sshin	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
23153541Sshin		if (ip6f->ip6f_ident == q6->ip6q_ident &&
23253541Sshin		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
23353541Sshin		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
23453541Sshin			break;
23553541Sshin
23653541Sshin	if (q6 == &ip6q) {
23753541Sshin		/*
23853541Sshin		 * the first fragment to arrive, create a reassembly queue.
23953541Sshin		 */
24053541Sshin		first_frag = 1;
24153541Sshin
24253541Sshin		/*
24353541Sshin		 * Enforce upper bound on number of fragmented packets
24453541Sshin		 * for which we attempt reassembly;
245121345Sume		 * If maxfragpackets is 0, never accept fragments.
246121345Sume		 * If maxfragpackets is -1, accept all fragments without
247121345Sume		 * limitation.
24853541Sshin		 */
24978064Sume		if (ip6_maxfragpackets < 0)
25078064Sume			;
25178064Sume		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
25278064Sume			goto dropfrag;
25378064Sume		frag6_nfragpackets++;
25453541Sshin		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
255120891Sume		    M_DONTWAIT);
25653541Sshin		if (q6 == NULL)
25753541Sshin			goto dropfrag;
25862587Sitojun		bzero(q6, sizeof(*q6));
25953541Sshin
26053541Sshin		frag6_insque(q6, &ip6q);
26153541Sshin
26262587Sitojun		/* ip6q_nxt will be filled afterwards, from 1st fragment */
26353541Sshin		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
26462587Sitojun#ifdef notyet
26562587Sitojun		q6->ip6q_nxtp	= (u_char *)nxtp;
26662587Sitojun#endif
26753541Sshin		q6->ip6q_ident	= ip6f->ip6f_ident;
26853541Sshin		q6->ip6q_arrive = 0; /* Is it used anywhere? */
26953541Sshin		q6->ip6q_ttl 	= IPV6_FRAGTTL;
27053541Sshin		q6->ip6q_src	= ip6->ip6_src;
27153541Sshin		q6->ip6q_dst	= ip6->ip6_dst;
27253541Sshin		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
273121345Sume
274121345Sume		q6->ip6q_nfrag = 0;
27553541Sshin	}
27653541Sshin
27753541Sshin	/*
27853541Sshin	 * If it's the 1st fragment, record the length of the
27953541Sshin	 * unfragmentable part and the next header of the fragment header.
28053541Sshin	 */
28153541Sshin	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
28253541Sshin	if (fragoff == 0) {
283120891Sume		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
284120891Sume		    sizeof(struct ip6_frag);
28553541Sshin		q6->ip6q_nxt = ip6f->ip6f_nxt;
28653541Sshin	}
28753541Sshin
28853541Sshin	/*
28953541Sshin	 * Check that the reassembled packet would not exceed 65535 bytes
29053541Sshin	 * in size.
29153541Sshin	 * If it would exceed, discard the fragment and return an ICMP error.
29253541Sshin	 */
29362587Sitojun	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
29453541Sshin	if (q6->ip6q_unfrglen >= 0) {
29553541Sshin		/* The 1st fragment has already arrived. */
29653541Sshin		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
29753541Sshin			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
298120891Sume			    offset - sizeof(struct ip6_frag) +
299120891Sume			    offsetof(struct ip6_frag, ip6f_offlg));
300121345Sume			IP6Q_UNLOCK();
301120856Sume			return (IPPROTO_DONE);
30253541Sshin		}
303120891Sume	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
30453541Sshin		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
305120891Sume		    offset - sizeof(struct ip6_frag) +
306120891Sume		    offsetof(struct ip6_frag, ip6f_offlg));
307121345Sume		IP6Q_UNLOCK();
308120856Sume		return (IPPROTO_DONE);
30953541Sshin	}
31053541Sshin	/*
31153541Sshin	 * If it's the first fragment, do the above check for each
31253541Sshin	 * fragment already stored in the reassembly queue.
31353541Sshin	 */
31453541Sshin	if (fragoff == 0) {
31553541Sshin		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
31653541Sshin		     af6 = af6dwn) {
31753541Sshin			af6dwn = af6->ip6af_down;
31853541Sshin
31953541Sshin			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
32053541Sshin			    IPV6_MAXPACKET) {
32153541Sshin				struct mbuf *merr = IP6_REASS_MBUF(af6);
32253541Sshin				struct ip6_hdr *ip6err;
32353541Sshin				int erroff = af6->ip6af_offset;
32453541Sshin
32553541Sshin				/* dequeue the fragment. */
32653541Sshin				frag6_deq(af6);
32762587Sitojun				free(af6, M_FTABLE);
32853541Sshin
32953541Sshin				/* adjust pointer. */
33053541Sshin				ip6err = mtod(merr, struct ip6_hdr *);
33153541Sshin
33253541Sshin				/*
33353541Sshin				 * Restore source and destination addresses
33453541Sshin				 * in the erroneous IPv6 header.
33553541Sshin				 */
33653541Sshin				ip6err->ip6_src = q6->ip6q_src;
33753541Sshin				ip6err->ip6_dst = q6->ip6q_dst;
33853541Sshin
33953541Sshin				icmp6_error(merr, ICMP6_PARAM_PROB,
340120891Sume				    ICMP6_PARAMPROB_HEADER,
341120891Sume				    erroff - sizeof(struct ip6_frag) +
342120891Sume				    offsetof(struct ip6_frag, ip6f_offlg));
34353541Sshin			}
34453541Sshin		}
34553541Sshin	}
34653541Sshin
34762587Sitojun	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
348111119Simp	    M_DONTWAIT);
34962587Sitojun	if (ip6af == NULL)
35062587Sitojun		goto dropfrag;
35162587Sitojun	bzero(ip6af, sizeof(*ip6af));
35262587Sitojun	ip6af->ip6af_head = ip6->ip6_flow;
35362587Sitojun	ip6af->ip6af_len = ip6->ip6_plen;
35462587Sitojun	ip6af->ip6af_nxt = ip6->ip6_nxt;
35562587Sitojun	ip6af->ip6af_hlim = ip6->ip6_hlim;
35653541Sshin	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
35753541Sshin	ip6af->ip6af_off = fragoff;
35853541Sshin	ip6af->ip6af_frglen = frgpartlen;
35953541Sshin	ip6af->ip6af_offset = offset;
36053541Sshin	IP6_REASS_MBUF(ip6af) = m;
36153541Sshin
36253541Sshin	if (first_frag) {
36353541Sshin		af6 = (struct ip6asfrag *)q6;
36453541Sshin		goto insert;
36553541Sshin	}
36653541Sshin
36753541Sshin	/*
36853541Sshin	 * Find a segment which begins after this one does.
36953541Sshin	 */
37053541Sshin	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
37153541Sshin	     af6 = af6->ip6af_down)
37253541Sshin		if (af6->ip6af_off > ip6af->ip6af_off)
37353541Sshin			break;
37453541Sshin
37562587Sitojun#if 0
37653541Sshin	/*
37762587Sitojun	 * If there is a preceding segment, it may provide some of
37862587Sitojun	 * our data already.  If so, drop the data from the incoming
37962587Sitojun	 * segment.  If it provides all of our data, drop us.
38062587Sitojun	 */
38162587Sitojun	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
38262587Sitojun		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
38362587Sitojun			- ip6af->ip6af_off;
38462587Sitojun		if (i > 0) {
38562587Sitojun			if (i >= ip6af->ip6af_frglen)
38662587Sitojun				goto dropfrag;
38762587Sitojun			m_adj(IP6_REASS_MBUF(ip6af), i);
38862587Sitojun			ip6af->ip6af_off += i;
38962587Sitojun			ip6af->ip6af_frglen -= i;
39062587Sitojun		}
39162587Sitojun	}
39262587Sitojun
39362587Sitojun	/*
39462587Sitojun	 * While we overlap succeeding segments trim them or,
39562587Sitojun	 * if they are completely covered, dequeue them.
39662587Sitojun	 */
39762587Sitojun	while (af6 != (struct ip6asfrag *)q6 &&
39862587Sitojun	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
39962587Sitojun		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
40062587Sitojun		if (i < af6->ip6af_frglen) {
40162587Sitojun			af6->ip6af_frglen -= i;
40262587Sitojun			af6->ip6af_off += i;
40362587Sitojun			m_adj(IP6_REASS_MBUF(af6), i);
40462587Sitojun			break;
40562587Sitojun		}
40662587Sitojun		af6 = af6->ip6af_down;
40762587Sitojun		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
40862587Sitojun		frag6_deq(af6->ip6af_up);
40962587Sitojun	}
41062587Sitojun#else
41162587Sitojun	/*
41253541Sshin	 * If the incoming framgent overlaps some existing fragments in
41353541Sshin	 * the reassembly queue, drop it, since it is dangerous to override
41453541Sshin	 * existing fragments from a security point of view.
415121345Sume	 * We don't know which fragment is the bad guy - here we trust
416121345Sume	 * fragment that came in earlier, with no real reason.
41753541Sshin	 */
41853541Sshin	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
41953541Sshin		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
42053541Sshin			- ip6af->ip6af_off;
42153541Sshin		if (i > 0) {
42276899Ssumikawa#if 0				/* suppress the noisy log */
42353541Sshin			log(LOG_ERR, "%d bytes of a fragment from %s "
42453541Sshin			    "overlaps the previous fragment\n",
42553541Sshin			    i, ip6_sprintf(&q6->ip6q_src));
42676899Ssumikawa#endif
42776899Ssumikawa			free(ip6af, M_FTABLE);
42853541Sshin			goto dropfrag;
42953541Sshin		}
43053541Sshin	}
43153541Sshin	if (af6 != (struct ip6asfrag *)q6) {
43253541Sshin		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
43353541Sshin		if (i > 0) {
43476899Ssumikawa#if 0				/* suppress the noisy log */
43553541Sshin			log(LOG_ERR, "%d bytes of a fragment from %s "
43653541Sshin			    "overlaps the succeeding fragment",
43753541Sshin			    i, ip6_sprintf(&q6->ip6q_src));
43876899Ssumikawa#endif
43976899Ssumikawa			free(ip6af, M_FTABLE);
44053541Sshin			goto dropfrag;
44153541Sshin		}
44253541Sshin	}
44362587Sitojun#endif
44453541Sshin
44553541Sshininsert:
44653541Sshin
44753541Sshin	/*
44853541Sshin	 * Stick new segment in its place;
44953541Sshin	 * check for complete reassembly.
45053541Sshin	 * Move to front of packet queue, as we are
45153541Sshin	 * the most recently active fragmented packet.
45253541Sshin	 */
45353541Sshin	frag6_enq(ip6af, af6->ip6af_up);
454121345Sume	frag6_nfrags++;
455121345Sume	q6->ip6q_nfrag++;
45662587Sitojun#if 0 /* xxx */
45762587Sitojun	if (q6 != ip6q.ip6q_next) {
45862587Sitojun		frag6_remque(q6);
45962587Sitojun		frag6_insque(q6, &ip6q);
46062587Sitojun	}
46162587Sitojun#endif
46253541Sshin	next = 0;
46353541Sshin	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
46453541Sshin	     af6 = af6->ip6af_down) {
46553541Sshin		if (af6->ip6af_off != next) {
466121345Sume			IP6Q_UNLOCK();
46753541Sshin			return IPPROTO_DONE;
46853541Sshin		}
46953541Sshin		next += af6->ip6af_frglen;
47053541Sshin	}
47153541Sshin	if (af6->ip6af_up->ip6af_mff) {
472121345Sume		IP6Q_UNLOCK();
47353541Sshin		return IPPROTO_DONE;
47453541Sshin	}
47553541Sshin
47653541Sshin	/*
47753541Sshin	 * Reassembly is complete; concatenate fragments.
47853541Sshin	 */
47953541Sshin	ip6af = q6->ip6q_down;
48053541Sshin	t = m = IP6_REASS_MBUF(ip6af);
48153541Sshin	af6 = ip6af->ip6af_down;
48262587Sitojun	frag6_deq(ip6af);
48353541Sshin	while (af6 != (struct ip6asfrag *)q6) {
48462587Sitojun		af6dwn = af6->ip6af_down;
48562587Sitojun		frag6_deq(af6);
48653541Sshin		while (t->m_next)
48753541Sshin			t = t->m_next;
48853541Sshin		t->m_next = IP6_REASS_MBUF(af6);
48962587Sitojun		m_adj(t->m_next, af6->ip6af_offset);
49062587Sitojun		free(af6, M_FTABLE);
49162587Sitojun		af6 = af6dwn;
49253541Sshin	}
49353541Sshin
49453541Sshin	/* adjust offset to point where the original next header starts */
49553541Sshin	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
49662587Sitojun	free(ip6af, M_FTABLE);
49762587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
49853541Sshin	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
49953541Sshin	ip6->ip6_src = q6->ip6q_src;
50053541Sshin	ip6->ip6_dst = q6->ip6q_dst;
50153541Sshin	nxt = q6->ip6q_nxt;
50262587Sitojun#ifdef notyet
50362587Sitojun	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
50462587Sitojun#endif
50553541Sshin
50653541Sshin	/*
50753541Sshin	 * Delete frag6 header with as a few cost as possible.
50853541Sshin	 */
50962587Sitojun	if (offset < m->m_len) {
51053541Sshin		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
51153541Sshin			offset);
51262587Sitojun		m->m_data += sizeof(struct ip6_frag);
51362587Sitojun		m->m_len -= sizeof(struct ip6_frag);
51462587Sitojun	} else {
51562587Sitojun		/* this comes with no copy if the boundary is on cluster */
516111119Simp		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
51762587Sitojun			frag6_remque(q6);
518121345Sume			frag6_nfrags -= q6->ip6q_nfrag;
51962587Sitojun			free(q6, M_FTABLE);
52062587Sitojun			frag6_nfragpackets--;
52162587Sitojun			goto dropfrag;
52262587Sitojun		}
52362587Sitojun		m_adj(t, sizeof(struct ip6_frag));
52462587Sitojun		m_cat(m, t);
52553541Sshin	}
52653541Sshin
52753541Sshin	/*
52853541Sshin	 * Store NXT to the original.
52953541Sshin	 */
53053541Sshin	{
53153541Sshin		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
53253541Sshin		*prvnxtp = nxt;
53353541Sshin	}
53453541Sshin
53553541Sshin	frag6_remque(q6);
536121345Sume	frag6_nfrags -= q6->ip6q_nfrag;
53753541Sshin	free(q6, M_FTABLE);
53853541Sshin	frag6_nfragpackets--;
53953541Sshin
54053541Sshin	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
54153541Sshin		int plen = 0;
54253541Sshin		for (t = m; t; t = t->m_next)
54353541Sshin			plen += t->m_len;
54453541Sshin		m->m_pkthdr.len = plen;
54553541Sshin	}
546120891Sume
54753541Sshin	ip6stat.ip6s_reassembled++;
54853541Sshin	in6_ifstat_inc(dstifp, ifs6_reass_ok);
54953541Sshin
55053541Sshin	/*
55153541Sshin	 * Tell launch routine the next header
55253541Sshin	 */
55353541Sshin
55453541Sshin	*mp = m;
55553541Sshin	*offp = offset;
55653541Sshin
557121345Sume	IP6Q_UNLOCK();
55853541Sshin	return nxt;
55953541Sshin
56053541Sshin dropfrag:
561121346Sume	IP6Q_UNLOCK();
56253541Sshin	in6_ifstat_inc(dstifp, ifs6_reass_fail);
56353541Sshin	ip6stat.ip6s_fragdropped++;
56453541Sshin	m_freem(m);
56553541Sshin	return IPPROTO_DONE;
56653541Sshin}
56753541Sshin
56853541Sshin/*
56953541Sshin * Free a fragment reassembly header and all
57053541Sshin * associated datagrams.
57153541Sshin */
57253541Sshinvoid
57353541Sshinfrag6_freef(q6)
57453541Sshin	struct ip6q *q6;
57553541Sshin{
57653541Sshin	struct ip6asfrag *af6, *down6;
57753541Sshin
578121355Sume	IP6Q_LOCK_ASSERT();
579121345Sume
58053541Sshin	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
58153541Sshin	     af6 = down6) {
58253541Sshin		struct mbuf *m = IP6_REASS_MBUF(af6);
58353541Sshin
58453541Sshin		down6 = af6->ip6af_down;
58553541Sshin		frag6_deq(af6);
58653541Sshin
58753541Sshin		/*
58853541Sshin		 * Return ICMP time exceeded error for the 1st fragment.
58953541Sshin		 * Just free other fragments.
59053541Sshin		 */
59153541Sshin		if (af6->ip6af_off == 0) {
59253541Sshin			struct ip6_hdr *ip6;
59353541Sshin
59453541Sshin			/* adjust pointer */
59553541Sshin			ip6 = mtod(m, struct ip6_hdr *);
59653541Sshin
597120891Sume			/* restore source and destination addresses */
59853541Sshin			ip6->ip6_src = q6->ip6q_src;
59953541Sshin			ip6->ip6_dst = q6->ip6q_dst;
60053541Sshin
60153541Sshin			icmp6_error(m, ICMP6_TIME_EXCEEDED,
60253541Sshin				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
60362587Sitojun		} else
60453541Sshin			m_freem(m);
60562587Sitojun		free(af6, M_FTABLE);
60653541Sshin	}
60753541Sshin	frag6_remque(q6);
608121345Sume	frag6_nfrags -= q6->ip6q_nfrag;
60953541Sshin	free(q6, M_FTABLE);
61053541Sshin	frag6_nfragpackets--;
61153541Sshin}
61253541Sshin
61353541Sshin/*
61453541Sshin * Put an ip fragment on a reassembly chain.
61553541Sshin * Like insque, but pointers in middle of structure.
61653541Sshin */
61753541Sshinvoid
61853541Sshinfrag6_enq(af6, up6)
61953541Sshin	struct ip6asfrag *af6, *up6;
62053541Sshin{
621121345Sume
622121355Sume	IP6Q_LOCK_ASSERT();
623121345Sume
62453541Sshin	af6->ip6af_up = up6;
62553541Sshin	af6->ip6af_down = up6->ip6af_down;
62653541Sshin	up6->ip6af_down->ip6af_up = af6;
62753541Sshin	up6->ip6af_down = af6;
62853541Sshin}
62953541Sshin
63053541Sshin/*
63153541Sshin * To frag6_enq as remque is to insque.
63253541Sshin */
63353541Sshinvoid
63453541Sshinfrag6_deq(af6)
63553541Sshin	struct ip6asfrag *af6;
63653541Sshin{
637121345Sume
638121355Sume	IP6Q_LOCK_ASSERT();
639121345Sume
64053541Sshin	af6->ip6af_up->ip6af_down = af6->ip6af_down;
64153541Sshin	af6->ip6af_down->ip6af_up = af6->ip6af_up;
64253541Sshin}
64353541Sshin
64453541Sshinvoid
64553541Sshinfrag6_insque(new, old)
64653541Sshin	struct ip6q *new, *old;
64753541Sshin{
648121345Sume
649121355Sume	IP6Q_LOCK_ASSERT();
650121345Sume
65153541Sshin	new->ip6q_prev = old;
65253541Sshin	new->ip6q_next = old->ip6q_next;
65353541Sshin	old->ip6q_next->ip6q_prev= new;
65453541Sshin	old->ip6q_next = new;
65553541Sshin}
65653541Sshin
65753541Sshinvoid
65853541Sshinfrag6_remque(p6)
65953541Sshin	struct ip6q *p6;
66053541Sshin{
661121345Sume
662121355Sume	IP6Q_LOCK_ASSERT();
663121345Sume
66453541Sshin	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
66553541Sshin	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
66653541Sshin}
66753541Sshin
66853541Sshin/*
66978064Sume * IPv6 reassembling timer processing;
67053541Sshin * if a timer expires on a reassembly
67153541Sshin * queue, discard it.
67253541Sshin */
67353541Sshinvoid
67453541Sshinfrag6_slowtimo()
67553541Sshin{
67653541Sshin	struct ip6q *q6;
67753541Sshin	int s = splnet();
67853541Sshin
679121345Sume	IP6Q_LOCK();
68053541Sshin	q6 = ip6q.ip6q_next;
68153541Sshin	if (q6)
68253541Sshin		while (q6 != &ip6q) {
68353541Sshin			--q6->ip6q_ttl;
68453541Sshin			q6 = q6->ip6q_next;
68553541Sshin			if (q6->ip6q_prev->ip6q_ttl == 0) {
68653541Sshin				ip6stat.ip6s_fragtimeout++;
68753541Sshin				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
68853541Sshin				frag6_freef(q6->ip6q_prev);
68953541Sshin			}
69053541Sshin		}
69153541Sshin	/*
69253541Sshin	 * If we are over the maximum number of fragments
69353541Sshin	 * (due to the limit being lowered), drain off
69453541Sshin	 * enough to get down to the new limit.
69553541Sshin	 */
69678064Sume	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
69778064Sume	    ip6q.ip6q_prev) {
69853541Sshin		ip6stat.ip6s_fragoverflow++;
69953541Sshin		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
70053541Sshin		frag6_freef(ip6q.ip6q_prev);
70153541Sshin	}
702121345Sume	IP6Q_UNLOCK();
70362587Sitojun
70462587Sitojun#if 0
70562587Sitojun	/*
70662587Sitojun	 * Routing changes might produce a better route than we last used;
70762587Sitojun	 * make sure we notice eventually, even if forwarding only for one
70862587Sitojun	 * destination and the cache is never replaced.
70962587Sitojun	 */
71062587Sitojun	if (ip6_forward_rt.ro_rt) {
71162587Sitojun		RTFREE(ip6_forward_rt.ro_rt);
71262587Sitojun		ip6_forward_rt.ro_rt = 0;
71362587Sitojun	}
71462587Sitojun	if (ipsrcchk_rt.ro_rt) {
71562587Sitojun		RTFREE(ipsrcchk_rt.ro_rt);
71662587Sitojun		ipsrcchk_rt.ro_rt = 0;
71762587Sitojun	}
71862587Sitojun#endif
71962587Sitojun
72053541Sshin	splx(s);
72153541Sshin}
72253541Sshin
72353541Sshin/*
72453541Sshin * Drain off all datagram fragments.
72553541Sshin */
72653541Sshinvoid
72753541Sshinfrag6_drain()
72853541Sshin{
729121345Sume
730121346Sume	if (IP6Q_TRYLOCK() == 0)
73153541Sshin		return;
73253541Sshin	while (ip6q.ip6q_next != &ip6q) {
73353541Sshin		ip6stat.ip6s_fragdropped++;
73453541Sshin		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
73553541Sshin		frag6_freef(ip6q.ip6q_next);
73653541Sshin	}
737121345Sume	IP6Q_UNLOCK();
73853541Sshin}
739