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