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