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