xref: /netbsd-src/sys/netinet6/frag6.c (revision a8ea6d9dcd694a0b1b0734e58824d6541b3f4cb9)
1*a8ea6d9dSozaki-r /*	$NetBSD: frag6.c,v 1.78 2024/04/19 05:04:06 ozaki-r Exp $	*/
210c59140Sitojun /*	$KAME: frag6.c,v 1.40 2002/05/27 21:40:31 itojun Exp $	*/
3cd3a345eSthorpej 
474d3c214Sitojun /*
574d3c214Sitojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
674d3c214Sitojun  * All rights reserved.
774d3c214Sitojun  *
874d3c214Sitojun  * Redistribution and use in source and binary forms, with or without
974d3c214Sitojun  * modification, are permitted provided that the following conditions
1074d3c214Sitojun  * are met:
1174d3c214Sitojun  * 1. Redistributions of source code must retain the above copyright
1274d3c214Sitojun  *    notice, this list of conditions and the following disclaimer.
1374d3c214Sitojun  * 2. Redistributions in binary form must reproduce the above copyright
1474d3c214Sitojun  *    notice, this list of conditions and the following disclaimer in the
1574d3c214Sitojun  *    documentation and/or other materials provided with the distribution.
1674d3c214Sitojun  * 3. Neither the name of the project nor the names of its contributors
1774d3c214Sitojun  *    may be used to endorse or promote products derived from this software
1874d3c214Sitojun  *    without specific prior written permission.
1974d3c214Sitojun  *
2074d3c214Sitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2174d3c214Sitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2274d3c214Sitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2374d3c214Sitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2474d3c214Sitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2574d3c214Sitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2674d3c214Sitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2774d3c214Sitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2874d3c214Sitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2974d3c214Sitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3074d3c214Sitojun  * SUCH DAMAGE.
3174d3c214Sitojun  */
3274d3c214Sitojun 
334f2ad952Slukem #include <sys/cdefs.h>
34*a8ea6d9dSozaki-r __KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.78 2024/04/19 05:04:06 ozaki-r Exp $");
359e8d969cSozaki-r 
369e8d969cSozaki-r #ifdef _KERNEL_OPT
379e8d969cSozaki-r #include "opt_net_mpsafe.h"
389e8d969cSozaki-r #endif
394f2ad952Slukem 
4074d3c214Sitojun #include <sys/param.h>
4174d3c214Sitojun #include <sys/systm.h>
4274d3c214Sitojun #include <sys/mbuf.h>
4374d3c214Sitojun #include <sys/errno.h>
4474d3c214Sitojun #include <sys/time.h>
45b92d93ccSrmind #include <sys/kmem.h>
4674d3c214Sitojun #include <sys/kernel.h>
4774d3c214Sitojun #include <sys/syslog.h>
4874d3c214Sitojun 
4974d3c214Sitojun #include <net/if.h>
5074d3c214Sitojun #include <net/route.h>
5174d3c214Sitojun 
5274d3c214Sitojun #include <netinet/in.h>
5374d3c214Sitojun #include <netinet/in_var.h>
5490736ab6Sitojun #include <netinet/ip6.h>
5574d3c214Sitojun #include <netinet6/ip6_var.h>
560dd41b37Sthorpej #include <netinet6/ip6_private.h>
5790736ab6Sitojun #include <netinet/icmp6.h>
5874d3c214Sitojun 
59e3090ee1Smaxv /*
6030cb1ea3Smaxv  * IPv6 reassembly queue structure. Each fragment being reassembled is
6130cb1ea3Smaxv  * attached to one of these structures.
6230cb1ea3Smaxv  *
6330cb1ea3Smaxv  * XXX: Would be better to use TAILQ.
64e3090ee1Smaxv  */
65e3090ee1Smaxv struct	ip6q {
66e3090ee1Smaxv 	u_int32_t	ip6q_head;
67e3090ee1Smaxv 	u_int16_t	ip6q_len;
68e3090ee1Smaxv 	u_int8_t	ip6q_nxt;	/* ip6f_nxt in first fragment */
69e3090ee1Smaxv 	u_int8_t	ip6q_hlim;
70e3090ee1Smaxv 	struct ip6asfrag *ip6q_down;
71e3090ee1Smaxv 	struct ip6asfrag *ip6q_up;
72e3090ee1Smaxv 	u_int32_t	ip6q_ident;
73e3090ee1Smaxv 	u_int8_t	ip6q_ttl;
74e3090ee1Smaxv 	struct in6_addr	ip6q_src, ip6q_dst;
75e3090ee1Smaxv 	struct ip6q	*ip6q_next;
76e3090ee1Smaxv 	struct ip6q	*ip6q_prev;
77e3090ee1Smaxv 	int		ip6q_unfrglen;	/* len of unfragmentable part */
78e3090ee1Smaxv 	int		ip6q_nfrag;	/* # of fragments */
79fbb9ed35Smaxv 	int		ip6q_ipsec;	/* IPsec flags */
80e3090ee1Smaxv };
81e3090ee1Smaxv 
82e3090ee1Smaxv struct	ip6asfrag {
83e3090ee1Smaxv 	u_int32_t	ip6af_head;
84e3090ee1Smaxv 	u_int16_t	ip6af_len;
85e3090ee1Smaxv 	u_int8_t	ip6af_nxt;
86e3090ee1Smaxv 	u_int8_t	ip6af_hlim;
87e3090ee1Smaxv 	/* must not override the above members during reassembling */
88e3090ee1Smaxv 	struct ip6asfrag *ip6af_down;
89e3090ee1Smaxv 	struct ip6asfrag *ip6af_up;
90e3090ee1Smaxv 	struct mbuf	*ip6af_m;
91e3090ee1Smaxv 	int		ip6af_offset;	/* offset in ip6af_m to next header */
92e3090ee1Smaxv 	int		ip6af_frglen;	/* fragmentable part length */
93e3090ee1Smaxv 	int		ip6af_off;	/* fragment offset */
941fd7efccSmaxv 	bool		ip6af_mff;	/* more fragment bit in frag off */
95e3090ee1Smaxv };
96e3090ee1Smaxv 
976cc9c359Sdyoung static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
986cc9c359Sdyoung static void frag6_deq(struct ip6asfrag *);
996cc9c359Sdyoung static void frag6_insque(struct ip6q *, struct ip6q *);
1006cc9c359Sdyoung static void frag6_remque(struct ip6q *);
1016cc9c359Sdyoung static void frag6_freef(struct ip6q *);
10274d3c214Sitojun 
103ac162b77Sdyoung static int frag6_drainwanted;
104ac162b77Sdyoung 
10530cb1ea3Smaxv static u_int frag6_nfragpackets;
10630cb1ea3Smaxv static u_int frag6_nfrags;
10730cb1ea3Smaxv static struct ip6q ip6q;	/* ip6 reassembly queue */
10874d3c214Sitojun 
109e3090ee1Smaxv /* Protects ip6q */
110e3090ee1Smaxv static kmutex_t	frag6_lock __cacheline_aligned;
111766a6d87Sitojun 
11274d3c214Sitojun /*
11374d3c214Sitojun  * Initialise reassembly queue and fragment identifier.
11474d3c214Sitojun  */
11574d3c214Sitojun void
frag6_init(void)116c9395522Smatt frag6_init(void)
11774d3c214Sitojun {
1180647902fSitojun 
119b89812ffSitojun 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
120bec975a5Sozaki-r 	mutex_init(&frag6_lock, MUTEX_DEFAULT, IPL_NONE);
12174d3c214Sitojun }
12274d3c214Sitojun 
123b28be63dSchristos static void
frag6_dropfrag(struct ip6q * q6)124b28be63dSchristos frag6_dropfrag(struct ip6q *q6)
125b28be63dSchristos {
126b28be63dSchristos 	frag6_remque(q6);
127b28be63dSchristos 	frag6_nfrags -= q6->ip6q_nfrag;
128b28be63dSchristos 	kmem_intr_free(q6, sizeof(*q6));
129b28be63dSchristos 	frag6_nfragpackets--;
130b28be63dSchristos }
131b28be63dSchristos 
13274d3c214Sitojun /*
133b92d93ccSrmind  * IPv6 fragment input.
134b92d93ccSrmind  *
135b89812ffSitojun  * In RFC2460, fragment and reassembly rule do not agree with each other,
136b89812ffSitojun  * in terms of next header field handling in fragment header.
137b89812ffSitojun  * While the sender will use the same value for all of the fragmented packets,
138b89812ffSitojun  * receiver is suggested not to check the consistency.
139b89812ffSitojun  *
140b89812ffSitojun  * fragment rule (p20):
141b89812ffSitojun  *	(2) A Fragment header containing:
142b89812ffSitojun  *	The Next Header value that identifies the first header of
143b89812ffSitojun  *	the Fragmentable Part of the original packet.
144b89812ffSitojun  *		-> next header field is same for all fragments
145b89812ffSitojun  *
146b89812ffSitojun  * reassembly rule (p21):
147b89812ffSitojun  *	The Next Header field of the last header of the Unfragmentable
148b89812ffSitojun  *	Part is obtained from the Next Header field of the first
149b89812ffSitojun  *	fragment's Fragment header.
150b89812ffSitojun  *		-> should grab it from the first fragment only
151b89812ffSitojun  *
152b89812ffSitojun  * The following note also contradicts with fragment rule - noone is going to
153b89812ffSitojun  * send different fragment with different next header field.
154b89812ffSitojun  *
155b89812ffSitojun  * additional note (p22):
156b89812ffSitojun  *	The Next Header values in the Fragment headers of different
157b89812ffSitojun  *	fragments of the same original packet may differ.  Only the value
158b89812ffSitojun  *	from the Offset zero fragment packet is used for reassembly.
159b89812ffSitojun  *		-> should grab it from the first fragment only
160b89812ffSitojun  *
161b89812ffSitojun  * There is no explicit reason given in the RFC.  Historical reason maybe?
162be3197e9Smaxv  *
163be3197e9Smaxv  * XXX: It would be better to use a pool, rather than kmem.
164b89812ffSitojun  */
165b92d93ccSrmind int
frag6_input(struct mbuf ** mp,int * offp,int proto)166b92d93ccSrmind frag6_input(struct mbuf **mp, int *offp, int proto)
16774d3c214Sitojun {
16872fa642aSdyoung 	struct rtentry *rt;
16974d3c214Sitojun 	struct mbuf *m = *mp, *t;
17074d3c214Sitojun 	struct ip6_hdr *ip6;
17174d3c214Sitojun 	struct ip6_frag *ip6f;
17274d3c214Sitojun 	struct ip6q *q6;
173b89812ffSitojun 	struct ip6asfrag *af6, *ip6af, *af6dwn;
17474d3c214Sitojun 	int offset = *offp, nxt, i, next;
175fbb9ed35Smaxv 	int ipsecflags = m->m_flags & (M_DECRYPTED|M_AUTHIPHDR);
17674d3c214Sitojun 	int first_frag = 0;
177b89812ffSitojun 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
178ea861f01Sitojun 	struct ifnet *dstifp;
17972f0a6dfSdyoung 	static struct route ro;
18072f0a6dfSdyoung 	union {
18172f0a6dfSdyoung 		struct sockaddr		dst;
18272f0a6dfSdyoung 		struct sockaddr_in6	dst6;
18372f0a6dfSdyoung 	} u;
18474d3c214Sitojun 
18574d3c214Sitojun 	ip6 = mtod(m, struct ip6_hdr *);
186ea861f01Sitojun 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
187ea861f01Sitojun 	if (ip6f == NULL)
188b92d93ccSrmind 		return IPPROTO_DONE;
189ea861f01Sitojun 
190ea861f01Sitojun 	dstifp = NULL;
191ea861f01Sitojun 	/* find the destination interface of the packet. */
19272f0a6dfSdyoung 	sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
193a8d68489Sozaki-r 	if ((rt = rtcache_lookup(&ro, &u.dst)) != NULL)
19472fa642aSdyoung 		dstifp = ((struct in6_ifaddr *)rt->rt_ifa)->ia_ifp;
19574d3c214Sitojun 
19674d3c214Sitojun 	/* jumbo payload can't contain a fragment header */
19774d3c214Sitojun 	if (ip6->ip6_plen == 0) {
19874d3c214Sitojun 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
199ea861f01Sitojun 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
200fbb7e30dSozaki-r 		goto done;
20174d3c214Sitojun 	}
20274d3c214Sitojun 
20374d3c214Sitojun 	/*
2042cd98cd7Smaxv 	 * Check whether fragment packet's fragment length is non-zero and
20574d3c214Sitojun 	 * multiple of 8 octets.
20674d3c214Sitojun 	 * sizeof(struct ip6_frag) == 8
20774d3c214Sitojun 	 * sizeof(struct ip6_hdr) = 40
20874d3c214Sitojun 	 */
209*a8ea6d9dSozaki-r 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset
210*a8ea6d9dSozaki-r 	    - sizeof(struct ip6_frag);
211*a8ea6d9dSozaki-r 	if ((frgpartlen == 0) ||
212*a8ea6d9dSozaki-r 	    ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && (frgpartlen & 0x7) != 0)) {
21310c59140Sitojun 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
214b89812ffSitojun 		    offsetof(struct ip6_hdr, ip6_plen));
215ea861f01Sitojun 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
216fbb7e30dSozaki-r 		goto done;
21774d3c214Sitojun 	}
21874d3c214Sitojun 
2190dd41b37Sthorpej 	IP6_STATINC(IP6_STAT_FRAGMENTS);
220ea861f01Sitojun 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
22174d3c214Sitojun 
222b89812ffSitojun 	/* offset now points to data portion */
22374d3c214Sitojun 	offset += sizeof(struct ip6_frag);
22474d3c214Sitojun 
225dd908208Schristos 	/*
226d407b3e2Schristos 	 * RFC6946: A host that receives an IPv6 packet which includes
227e3090ee1Smaxv 	 * a Fragment Header with the "Fragment Offset" equal to 0 and
228d407b3e2Schristos 	 * the "M" bit equal to 0 MUST process such packet in isolation
229d407b3e2Schristos 	 * from any other packets/fragments.
23030cb1ea3Smaxv 	 *
23130cb1ea3Smaxv 	 * XXX: Would be better to remove this fragment header entirely,
23230cb1ea3Smaxv 	 * for us not to get confused later when looking back at the
23330cb1ea3Smaxv 	 * previous headers in the chain.
234dd908208Schristos 	 */
235dd908208Schristos 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
236dd908208Schristos 	if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
237dd908208Schristos 		IP6_STATINC(IP6_STAT_REASSEMBLED);
238dd908208Schristos 		in6_ifstat_inc(dstifp, ifs6_reass_ok);
239dd908208Schristos 		*offp = offset;
2404c25fb2fSozaki-r 		rtcache_unref(rt, &ro);
241dd908208Schristos 		return ip6f->ip6f_nxt;
242dd908208Schristos 	}
243dd908208Schristos 
244766dd565Szoltan 	mutex_enter(&frag6_lock);
24537bb4bf5Sitojun 
24610c59140Sitojun 	/*
24710c59140Sitojun 	 * Enforce upper bound on number of fragments.
24810c59140Sitojun 	 * If maxfrag is 0, never accept fragments.
24910c59140Sitojun 	 * If maxfrag is -1, accept all fragments without limitation.
25010c59140Sitojun 	 */
25110c59140Sitojun 	if (ip6_maxfrags < 0)
25210c59140Sitojun 		;
25310c59140Sitojun 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
25410c59140Sitojun 		goto dropfrag;
25510c59140Sitojun 
25674d3c214Sitojun 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
25774d3c214Sitojun 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
25874d3c214Sitojun 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
25974d3c214Sitojun 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
26074d3c214Sitojun 			break;
26174d3c214Sitojun 
262fbb9ed35Smaxv 	if (q6 != &ip6q) {
263fbb9ed35Smaxv 		/* All fragments must have the same IPsec flags. */
264fbb9ed35Smaxv 		if (q6->ip6q_ipsec != ipsecflags) {
265fbb9ed35Smaxv 			goto dropfrag;
266fbb9ed35Smaxv 		}
267fbb9ed35Smaxv 	}
268fbb9ed35Smaxv 
26974d3c214Sitojun 	if (q6 == &ip6q) {
27074d3c214Sitojun 		/*
27174d3c214Sitojun 		 * the first fragment to arrive, create a reassembly queue.
27274d3c214Sitojun 		 */
27374d3c214Sitojun 		first_frag = 1;
27474d3c214Sitojun 
27574d3c214Sitojun 		/*
27674d3c214Sitojun 		 * Enforce upper bound on number of fragmented packets
27774d3c214Sitojun 		 * for which we attempt reassembly;
27810c59140Sitojun 		 * If maxfragpackets is 0, never accept fragments.
27910c59140Sitojun 		 * If maxfragpackets is -1, accept all fragments without
28010c59140Sitojun 		 * limitation.
28174d3c214Sitojun 		 */
2822df943e6Sitojun 		if (ip6_maxfragpackets < 0)
2832df943e6Sitojun 			;
2842df943e6Sitojun 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
2852df943e6Sitojun 			goto dropfrag;
2862df943e6Sitojun 		frag6_nfragpackets++;
287b92d93ccSrmind 
288b92d93ccSrmind 		q6 = kmem_intr_zalloc(sizeof(struct ip6q), KM_NOSLEEP);
289b92d93ccSrmind 		if (q6 == NULL) {
29074d3c214Sitojun 			goto dropfrag;
291b92d93ccSrmind 		}
29274d3c214Sitojun 		frag6_insque(q6, &ip6q);
29374d3c214Sitojun 
294b89812ffSitojun 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
29574d3c214Sitojun 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
29674d3c214Sitojun 		q6->ip6q_ident	= ip6f->ip6f_ident;
29774d3c214Sitojun 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
29874d3c214Sitojun 		q6->ip6q_src	= ip6->ip6_src;
29974d3c214Sitojun 		q6->ip6q_dst	= ip6->ip6_dst;
30074d3c214Sitojun 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
30110c59140Sitojun 		q6->ip6q_nfrag = 0;
302fbb9ed35Smaxv 		q6->ip6q_ipsec = ipsecflags;
30374d3c214Sitojun 	}
30474d3c214Sitojun 
30574d3c214Sitojun 	/*
30674d3c214Sitojun 	 * If it's the 1st fragment, record the length of the
30774d3c214Sitojun 	 * unfragmentable part and the next header of the fragment header.
30874d3c214Sitojun 	 */
30974d3c214Sitojun 	if (fragoff == 0) {
31010c59140Sitojun 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
31110c59140Sitojun 		    sizeof(struct ip6_frag);
31274d3c214Sitojun 		q6->ip6q_nxt = ip6f->ip6f_nxt;
31374d3c214Sitojun 	}
31474d3c214Sitojun 
31574d3c214Sitojun 	/*
31674d3c214Sitojun 	 * Check that the reassembled packet would not exceed 65535 bytes
317e3090ee1Smaxv 	 * in size. If it would exceed, discard the fragment and return an
318e3090ee1Smaxv 	 * ICMP error.
31974d3c214Sitojun 	 */
32074d3c214Sitojun 	if (q6->ip6q_unfrglen >= 0) {
32174d3c214Sitojun 		/* The 1st fragment has already arrived. */
32274d3c214Sitojun 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
323766dd565Szoltan 			mutex_exit(&frag6_lock);
32474d3c214Sitojun 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
325b89812ffSitojun 			    offset - sizeof(struct ip6_frag) +
326b89812ffSitojun 			    offsetof(struct ip6_frag, ip6f_offlg));
327fbb7e30dSozaki-r 			goto done;
32874d3c214Sitojun 		}
32910c59140Sitojun 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
330766dd565Szoltan 		mutex_exit(&frag6_lock);
33174d3c214Sitojun 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
332b89812ffSitojun 		    offset - sizeof(struct ip6_frag) +
333b89812ffSitojun 		    offsetof(struct ip6_frag, ip6f_offlg));
334fbb7e30dSozaki-r 		goto done;
33574d3c214Sitojun 	}
336e3090ee1Smaxv 
33774d3c214Sitojun 	/*
33874d3c214Sitojun 	 * If it's the first fragment, do the above check for each
33974d3c214Sitojun 	 * fragment already stored in the reassembly queue.
34074d3c214Sitojun 	 */
34174d3c214Sitojun 	if (fragoff == 0) {
34274d3c214Sitojun 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
34374d3c214Sitojun 		     af6 = af6dwn) {
34474d3c214Sitojun 			af6dwn = af6->ip6af_down;
34574d3c214Sitojun 
34674d3c214Sitojun 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
34774d3c214Sitojun 			    IPV6_MAXPACKET) {
348e3090ee1Smaxv 				struct mbuf *merr = af6->ip6af_m;
34974d3c214Sitojun 				struct ip6_hdr *ip6err;
35074d3c214Sitojun 				int erroff = af6->ip6af_offset;
35174d3c214Sitojun 
35274d3c214Sitojun 				/* dequeue the fragment. */
35374d3c214Sitojun 				frag6_deq(af6);
354b92d93ccSrmind 				kmem_intr_free(af6, sizeof(struct ip6asfrag));
35574d3c214Sitojun 
35674d3c214Sitojun 				/* adjust pointer. */
35774d3c214Sitojun 				ip6err = mtod(merr, struct ip6_hdr *);
35874d3c214Sitojun 
35974d3c214Sitojun 				/*
36074d3c214Sitojun 				 * Restore source and destination addresses
36174d3c214Sitojun 				 * in the erroneous IPv6 header.
36274d3c214Sitojun 				 */
36374d3c214Sitojun 				ip6err->ip6_src = q6->ip6q_src;
36474d3c214Sitojun 				ip6err->ip6_dst = q6->ip6q_dst;
36574d3c214Sitojun 
36674d3c214Sitojun 				icmp6_error(merr, ICMP6_PARAM_PROB,
36774d3c214Sitojun 				    ICMP6_PARAMPROB_HEADER,
368b89812ffSitojun 				    erroff - sizeof(struct ip6_frag) +
369b89812ffSitojun 				    offsetof(struct ip6_frag, ip6f_offlg));
37074d3c214Sitojun 			}
37174d3c214Sitojun 		}
37274d3c214Sitojun 	}
37374d3c214Sitojun 
374b92d93ccSrmind 	ip6af = kmem_intr_zalloc(sizeof(struct ip6asfrag), KM_NOSLEEP);
375b92d93ccSrmind 	if (ip6af == NULL) {
376b89812ffSitojun 		goto dropfrag;
377b92d93ccSrmind 	}
378b89812ffSitojun 	ip6af->ip6af_head = ip6->ip6_flow;
379b89812ffSitojun 	ip6af->ip6af_len = ip6->ip6_plen;
380b89812ffSitojun 	ip6af->ip6af_nxt = ip6->ip6_nxt;
381b89812ffSitojun 	ip6af->ip6af_hlim = ip6->ip6_hlim;
3821fd7efccSmaxv 	ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) != 0;
38374d3c214Sitojun 	ip6af->ip6af_off = fragoff;
38474d3c214Sitojun 	ip6af->ip6af_frglen = frgpartlen;
38574d3c214Sitojun 	ip6af->ip6af_offset = offset;
386e3090ee1Smaxv 	ip6af->ip6af_m = m;
38774d3c214Sitojun 
38874d3c214Sitojun 	if (first_frag) {
38974d3c214Sitojun 		af6 = (struct ip6asfrag *)q6;
39074d3c214Sitojun 		goto insert;
39174d3c214Sitojun 	}
39274d3c214Sitojun 
39374d3c214Sitojun 	/*
39474d3c214Sitojun 	 * Find a segment which begins after this one does.
39574d3c214Sitojun 	 */
39674d3c214Sitojun 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
39774d3c214Sitojun 	     af6 = af6->ip6af_down)
39874d3c214Sitojun 		if (af6->ip6af_off > ip6af->ip6af_off)
39974d3c214Sitojun 			break;
40074d3c214Sitojun 
40174d3c214Sitojun 	/*
402b92d93ccSrmind 	 * If the incoming fragment overlaps some existing fragments in
403b92d93ccSrmind 	 * the reassembly queue - drop it as per RFC 5722.
40474d3c214Sitojun 	 */
40574d3c214Sitojun 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
40674d3c214Sitojun 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
40774d3c214Sitojun 			- ip6af->ip6af_off;
40874d3c214Sitojun 		if (i > 0) {
409b92d93ccSrmind 			kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
41074d3c214Sitojun 			goto dropfrag;
41174d3c214Sitojun 		}
41274d3c214Sitojun 	}
41374d3c214Sitojun 	if (af6 != (struct ip6asfrag *)q6) {
41474d3c214Sitojun 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
41574d3c214Sitojun 		if (i > 0) {
416b92d93ccSrmind 			kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
41774d3c214Sitojun 			goto dropfrag;
41874d3c214Sitojun 		}
41974d3c214Sitojun 	}
42074d3c214Sitojun 
42174d3c214Sitojun insert:
42274d3c214Sitojun 	/*
4234eac1377Smaxv 	 * Stick new segment in its place.
42474d3c214Sitojun 	 */
42574d3c214Sitojun 	frag6_enq(ip6af, af6->ip6af_up);
42610c59140Sitojun 	frag6_nfrags++;
42710c59140Sitojun 	q6->ip6q_nfrag++;
428e3090ee1Smaxv 
429e3090ee1Smaxv 	/*
430e3090ee1Smaxv 	 * Check for complete reassembly.
431e3090ee1Smaxv 	 */
43274d3c214Sitojun 	next = 0;
43374d3c214Sitojun 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
43474d3c214Sitojun 	     af6 = af6->ip6af_down) {
43574d3c214Sitojun 		if (af6->ip6af_off != next) {
436766dd565Szoltan 			mutex_exit(&frag6_lock);
437fbb7e30dSozaki-r 			goto done;
43874d3c214Sitojun 		}
43974d3c214Sitojun 		next += af6->ip6af_frglen;
44074d3c214Sitojun 	}
4411fd7efccSmaxv 	if (af6->ip6af_up->ip6af_mff) {
442766dd565Szoltan 		mutex_exit(&frag6_lock);
443fbb7e30dSozaki-r 		goto done;
44474d3c214Sitojun 	}
44574d3c214Sitojun 
44674d3c214Sitojun 	/*
44774d3c214Sitojun 	 * Reassembly is complete; concatenate fragments.
44874d3c214Sitojun 	 */
44974d3c214Sitojun 	ip6af = q6->ip6q_down;
450e3090ee1Smaxv 	t = m = ip6af->ip6af_m;
45174d3c214Sitojun 	af6 = ip6af->ip6af_down;
452b89812ffSitojun 	frag6_deq(ip6af);
45374d3c214Sitojun 	while (af6 != (struct ip6asfrag *)q6) {
454b89812ffSitojun 		af6dwn = af6->ip6af_down;
455b89812ffSitojun 		frag6_deq(af6);
45674d3c214Sitojun 		while (t->m_next)
45774d3c214Sitojun 			t = t->m_next;
458e3090ee1Smaxv 		t->m_next = af6->ip6af_m;
459b89812ffSitojun 		m_adj(t->m_next, af6->ip6af_offset);
46004b61f50Smaxv 		m_remove_pkthdr(t->m_next);
461b92d93ccSrmind 		kmem_intr_free(af6, sizeof(struct ip6asfrag));
462b89812ffSitojun 		af6 = af6dwn;
46374d3c214Sitojun 	}
46474d3c214Sitojun 
46574d3c214Sitojun 	/* adjust offset to point where the original next header starts */
46674d3c214Sitojun 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
467b92d93ccSrmind 	kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
468b28be63dSchristos 	next += offset - sizeof(struct ip6_hdr);
469b28be63dSchristos 	if ((u_int)next > IPV6_MAXPACKET) {
470b28be63dSchristos 		frag6_dropfrag(q6);
471b28be63dSchristos 		goto dropfrag;
472b28be63dSchristos 	}
473b89812ffSitojun 	ip6 = mtod(m, struct ip6_hdr *);
474b28be63dSchristos 	ip6->ip6_plen = htons(next);
47574d3c214Sitojun 	ip6->ip6_src = q6->ip6q_src;
47674d3c214Sitojun 	ip6->ip6_dst = q6->ip6q_dst;
47774d3c214Sitojun 	nxt = q6->ip6q_nxt;
47874d3c214Sitojun 
47974d3c214Sitojun 	/*
480e3090ee1Smaxv 	 * Delete frag6 header.
48174d3c214Sitojun 	 */
482f724a1d3Smlelstv 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
48353524e44Schristos 		memmove((char *)ip6 + sizeof(struct ip6_frag), ip6, offset);
484b89812ffSitojun 		m->m_data += sizeof(struct ip6_frag);
485b89812ffSitojun 		m->m_len -= sizeof(struct ip6_frag);
486b89812ffSitojun 	} else {
487b89812ffSitojun 		/* this comes with no copy if the boundary is on cluster */
488b89812ffSitojun 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
489b28be63dSchristos 			frag6_dropfrag(q6);
490b89812ffSitojun 			goto dropfrag;
49174d3c214Sitojun 		}
492b89812ffSitojun 		m_adj(t, sizeof(struct ip6_frag));
493b89812ffSitojun 		m_cat(m, t);
494b89812ffSitojun 	}
49574d3c214Sitojun 
496b28be63dSchristos 	frag6_dropfrag(q6);
49774d3c214Sitojun 
49847afec3dSmaxv 	{
49947afec3dSmaxv 		KASSERT(m->m_flags & M_PKTHDR);
50074d3c214Sitojun 		int plen = 0;
50171ad9602Smaxv 		for (t = m; t; t = t->m_next) {
50274d3c214Sitojun 			plen += t->m_len;
50371ad9602Smaxv 		}
50474d3c214Sitojun 		m->m_pkthdr.len = plen;
50510415a4aSmaxv 		/* XXX XXX: clear csum_flags? */
50674d3c214Sitojun 	}
50774d3c214Sitojun 
50871ad9602Smaxv 	/*
50971ad9602Smaxv 	 * Restore NXT to the original.
51071ad9602Smaxv 	 */
51171ad9602Smaxv 	{
51271ad9602Smaxv 		const int prvnxt = ip6_get_prevhdr(m, offset);
51371ad9602Smaxv 		uint8_t *prvnxtp;
51471ad9602Smaxv 
51571ad9602Smaxv 		IP6_EXTHDR_GET(prvnxtp, uint8_t *, m, prvnxt,
51671ad9602Smaxv 		    sizeof(*prvnxtp));
51771ad9602Smaxv 		if (prvnxtp == NULL) {
51871ad9602Smaxv 			goto dropfrag;
51971ad9602Smaxv 		}
52071ad9602Smaxv 		*prvnxtp = nxt;
52171ad9602Smaxv 	}
52271ad9602Smaxv 
5230dd41b37Sthorpej 	IP6_STATINC(IP6_STAT_REASSEMBLED);
524ea861f01Sitojun 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
5254c25fb2fSozaki-r 	rtcache_unref(rt, &ro);
52607595250Smaxv 	mutex_exit(&frag6_lock);
52774d3c214Sitojun 
52874d3c214Sitojun 	/*
52907595250Smaxv 	 * Tell launch routine the next header.
53074d3c214Sitojun 	 */
53174d3c214Sitojun 	*mp = m;
53274d3c214Sitojun 	*offp = offset;
53374d3c214Sitojun 	return nxt;
53474d3c214Sitojun 
53574d3c214Sitojun  dropfrag:
536766dd565Szoltan 	mutex_exit(&frag6_lock);
537ea861f01Sitojun 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
5380dd41b37Sthorpej 	IP6_STATINC(IP6_STAT_FRAGDROPPED);
53974d3c214Sitojun 	m_freem(m);
540fbb7e30dSozaki-r  done:
5414c25fb2fSozaki-r 	rtcache_unref(rt, &ro);
54274d3c214Sitojun 	return IPPROTO_DONE;
54374d3c214Sitojun }
54474d3c214Sitojun 
545766dd565Szoltan int
ip6_reass_packet(struct mbuf ** mp,int offset)546766dd565Szoltan ip6_reass_packet(struct mbuf **mp, int offset)
547766dd565Szoltan {
548766dd565Szoltan 
549b92d93ccSrmind 	if (frag6_input(mp, &offset, IPPROTO_IPV6) == IPPROTO_DONE) {
550766dd565Szoltan 		*mp = NULL;
551b92d93ccSrmind 		return EINVAL;
552766dd565Szoltan 	}
553b92d93ccSrmind 	return 0;
554766dd565Szoltan }
555766dd565Szoltan 
55674d3c214Sitojun /*
55774d3c214Sitojun  * Free a fragment reassembly header and all
55874d3c214Sitojun  * associated datagrams.
55974d3c214Sitojun  */
560e3090ee1Smaxv static void
frag6_freef(struct ip6q * q6)56172cfe732Schristos frag6_freef(struct ip6q *q6)
56274d3c214Sitojun {
56374d3c214Sitojun 	struct ip6asfrag *af6, *down6;
56474d3c214Sitojun 
565766dd565Szoltan 	KASSERT(mutex_owned(&frag6_lock));
566766a6d87Sitojun 
56774d3c214Sitojun 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
56874d3c214Sitojun 	     af6 = down6) {
569e3090ee1Smaxv 		struct mbuf *m = af6->ip6af_m;
57074d3c214Sitojun 
57174d3c214Sitojun 		down6 = af6->ip6af_down;
57274d3c214Sitojun 		frag6_deq(af6);
57374d3c214Sitojun 
57474d3c214Sitojun 		/*
57574d3c214Sitojun 		 * Return ICMP time exceeded error for the 1st fragment.
57674d3c214Sitojun 		 * Just free other fragments.
57774d3c214Sitojun 		 */
57874d3c214Sitojun 		if (af6->ip6af_off == 0) {
57974d3c214Sitojun 			struct ip6_hdr *ip6;
58074d3c214Sitojun 
58174d3c214Sitojun 			/* adjust pointer */
58274d3c214Sitojun 			ip6 = mtod(m, struct ip6_hdr *);
58374d3c214Sitojun 
5844eac1377Smaxv 			/* restore source and destination addresses */
58574d3c214Sitojun 			ip6->ip6_src = q6->ip6q_src;
58674d3c214Sitojun 			ip6->ip6_dst = q6->ip6q_dst;
58774d3c214Sitojun 
58874d3c214Sitojun 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
58974d3c214Sitojun 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
590b92d93ccSrmind 		} else {
59174d3c214Sitojun 			m_freem(m);
592b92d93ccSrmind 		}
593b92d93ccSrmind 		kmem_intr_free(af6, sizeof(struct ip6asfrag));
59474d3c214Sitojun 	}
595e3090ee1Smaxv 
596b28be63dSchristos 	frag6_dropfrag(q6);
59774d3c214Sitojun }
59874d3c214Sitojun 
59974d3c214Sitojun /*
60074d3c214Sitojun  * Put an ip fragment on a reassembly chain.
60174d3c214Sitojun  * Like insque, but pointers in middle of structure.
60274d3c214Sitojun  */
60374d3c214Sitojun void
frag6_enq(struct ip6asfrag * af6,struct ip6asfrag * up6)60472cfe732Schristos frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
60574d3c214Sitojun {
606766a6d87Sitojun 
607766dd565Szoltan 	KASSERT(mutex_owned(&frag6_lock));
608766a6d87Sitojun 
60974d3c214Sitojun 	af6->ip6af_up = up6;
61074d3c214Sitojun 	af6->ip6af_down = up6->ip6af_down;
61174d3c214Sitojun 	up6->ip6af_down->ip6af_up = af6;
61274d3c214Sitojun 	up6->ip6af_down = af6;
61374d3c214Sitojun }
61474d3c214Sitojun 
61574d3c214Sitojun /*
61674d3c214Sitojun  * To frag6_enq as remque is to insque.
61774d3c214Sitojun  */
61874d3c214Sitojun void
frag6_deq(struct ip6asfrag * af6)61972cfe732Schristos frag6_deq(struct ip6asfrag *af6)
62074d3c214Sitojun {
621766a6d87Sitojun 
622766dd565Szoltan 	KASSERT(mutex_owned(&frag6_lock));
623766a6d87Sitojun 
62474d3c214Sitojun 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
62574d3c214Sitojun 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
62674d3c214Sitojun }
62774d3c214Sitojun 
628e3090ee1Smaxv /*
629e3090ee1Smaxv  * Insert newq after oldq.
630e3090ee1Smaxv  */
63174d3c214Sitojun void
frag6_insque(struct ip6q * newq,struct ip6q * oldq)63262dd8805Smatt frag6_insque(struct ip6q *newq, struct ip6q *oldq)
63374d3c214Sitojun {
634766a6d87Sitojun 
635766dd565Szoltan 	KASSERT(mutex_owned(&frag6_lock));
636766a6d87Sitojun 
63762dd8805Smatt 	newq->ip6q_prev = oldq;
63862dd8805Smatt 	newq->ip6q_next = oldq->ip6q_next;
63962dd8805Smatt 	oldq->ip6q_next->ip6q_prev = newq;
64062dd8805Smatt 	oldq->ip6q_next = newq;
64174d3c214Sitojun }
64274d3c214Sitojun 
643e3090ee1Smaxv /*
644e3090ee1Smaxv  * Unlink p6.
645e3090ee1Smaxv  */
64674d3c214Sitojun void
frag6_remque(struct ip6q * p6)64772cfe732Schristos frag6_remque(struct ip6q *p6)
64874d3c214Sitojun {
649766a6d87Sitojun 
650766dd565Szoltan 	KASSERT(mutex_owned(&frag6_lock));
651766a6d87Sitojun 
65274d3c214Sitojun 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
65374d3c214Sitojun 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
65474d3c214Sitojun }
65574d3c214Sitojun 
656ac162b77Sdyoung void
frag6_fasttimo(void)657ac162b77Sdyoung frag6_fasttimo(void)
658ac162b77Sdyoung {
6599e8d969cSozaki-r 
660cead3b88Sozaki-r 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
6613ccd1982Sjakllsch 
662ac162b77Sdyoung 	if (frag6_drainwanted) {
663ac162b77Sdyoung 		frag6_drain();
664ac162b77Sdyoung 		frag6_drainwanted = 0;
665ac162b77Sdyoung 	}
6663ccd1982Sjakllsch 
667cead3b88Sozaki-r 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
668ac162b77Sdyoung }
669ac162b77Sdyoung 
67074d3c214Sitojun /*
671e1f4f779Sitojun  * IPv6 reassembling timer processing;
67274d3c214Sitojun  * if a timer expires on a reassembly
67374d3c214Sitojun  * queue, discard it.
67474d3c214Sitojun  */
67574d3c214Sitojun void
frag6_slowtimo(void)676c9395522Smatt frag6_slowtimo(void)
67774d3c214Sitojun {
67874d3c214Sitojun 	struct ip6q *q6;
67915e29e98Sad 
680cead3b88Sozaki-r 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
6813ccd1982Sjakllsch 
682766dd565Szoltan 	mutex_enter(&frag6_lock);
68374d3c214Sitojun 	q6 = ip6q.ip6q_next;
68430cb1ea3Smaxv 	if (q6) {
68574d3c214Sitojun 		while (q6 != &ip6q) {
68674d3c214Sitojun 			--q6->ip6q_ttl;
68774d3c214Sitojun 			q6 = q6->ip6q_next;
68874d3c214Sitojun 			if (q6->ip6q_prev->ip6q_ttl == 0) {
6890dd41b37Sthorpej 				IP6_STATINC(IP6_STAT_FRAGTIMEOUT);
690ea861f01Sitojun 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
69174d3c214Sitojun 				frag6_freef(q6->ip6q_prev);
69274d3c214Sitojun 			}
69374d3c214Sitojun 		}
69430cb1ea3Smaxv 	}
69530cb1ea3Smaxv 
69674d3c214Sitojun 	/*
69774d3c214Sitojun 	 * If we are over the maximum number of fragments
69874d3c214Sitojun 	 * (due to the limit being lowered), drain off
69974d3c214Sitojun 	 * enough to get down to the new limit.
70074d3c214Sitojun 	 */
7012df943e6Sitojun 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
7022df943e6Sitojun 	    ip6q.ip6q_prev) {
7030dd41b37Sthorpej 		IP6_STATINC(IP6_STAT_FRAGOVERFLOW);
704ea861f01Sitojun 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
70574d3c214Sitojun 		frag6_freef(ip6q.ip6q_prev);
70674d3c214Sitojun 	}
707766dd565Szoltan 	mutex_exit(&frag6_lock);
70874d3c214Sitojun 
709cead3b88Sozaki-r 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
7103ccd1982Sjakllsch 
71174d3c214Sitojun #if 0
71274d3c214Sitojun 	/*
71374d3c214Sitojun 	 * Routing changes might produce a better route than we last used;
71474d3c214Sitojun 	 * make sure we notice eventually, even if forwarding only for one
71574d3c214Sitojun 	 * destination and the cache is never replaced.
71674d3c214Sitojun 	 */
71772f0a6dfSdyoung 	rtcache_free(&ip6_forward_rt);
71872f0a6dfSdyoung 	rtcache_free(&ipsrcchk_rt);
71974d3c214Sitojun #endif
72074d3c214Sitojun }
72174d3c214Sitojun 
722ac162b77Sdyoung void
frag6_drainstub(void)723ac162b77Sdyoung frag6_drainstub(void)
724ac162b77Sdyoung {
725ac162b77Sdyoung 	frag6_drainwanted = 1;
726ac162b77Sdyoung }
727ac162b77Sdyoung 
72874d3c214Sitojun /*
72974d3c214Sitojun  * Drain off all datagram fragments.
73074d3c214Sitojun  */
73174d3c214Sitojun void
frag6_drain(void)732c9395522Smatt frag6_drain(void)
73374d3c214Sitojun {
734766a6d87Sitojun 
735766dd565Szoltan 	if (mutex_tryenter(&frag6_lock)) {
73674d3c214Sitojun 		while (ip6q.ip6q_next != &ip6q) {
7370dd41b37Sthorpej 			IP6_STATINC(IP6_STAT_FRAGDROPPED);
738ea861f01Sitojun 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
73974d3c214Sitojun 			frag6_freef(ip6q.ip6q_next);
74074d3c214Sitojun 		}
741766dd565Szoltan 		mutex_exit(&frag6_lock);
74274d3c214Sitojun 	}
74315e29e98Sad }
744