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