xref: /freebsd/sys/netinet6/frag6.c (revision 5778b399)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
35b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
36b48287a3SDavid E. O'Brien 
37aaa46574SAdrian Chadd #include "opt_rss.h"
38aaa46574SAdrian Chadd 
3982cd038dSYoshinobu Inoue #include <sys/param.h>
40f349c821SBjoern A. Zeeb #include <sys/systm.h>
411a3044faSBjoern A. Zeeb #include <sys/domain.h>
421a3044faSBjoern A. Zeeb #include <sys/eventhandler.h>
4380d7a853SJonathan T. Looney #include <sys/hash.h>
441a3044faSBjoern A. Zeeb #include <sys/kernel.h>
4582cd038dSYoshinobu Inoue #include <sys/malloc.h>
4682cd038dSYoshinobu Inoue #include <sys/mbuf.h>
4782cd038dSYoshinobu Inoue #include <sys/protosw.h>
4882cd038dSYoshinobu Inoue #include <sys/socket.h>
49757cb678SBjoern A. Zeeb #include <sys/sysctl.h>
5082cd038dSYoshinobu Inoue #include <sys/syslog.h>
5182cd038dSYoshinobu Inoue 
5282cd038dSYoshinobu Inoue #include <net/if.h>
5376039bc8SGleb Smirnoff #include <net/if_var.h>
54aaa46574SAdrian Chadd #include <net/netisr.h>
5582cd038dSYoshinobu Inoue #include <net/route.h>
56eddfbb76SRobert Watson #include <net/vnet.h>
5782cd038dSYoshinobu Inoue 
5882cd038dSYoshinobu Inoue #include <netinet/in.h>
5982cd038dSYoshinobu Inoue #include <netinet/in_var.h>
60686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
6182cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
62686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
6323d374aaSBjoern A. Zeeb #include <netinet/in_systm.h>	/* For ECN definitions. */
6423d374aaSBjoern A. Zeeb #include <netinet/ip.h>		/* For ECN definitions. */
6582cd038dSYoshinobu Inoue 
661a3044faSBjoern A. Zeeb #ifdef MAC
674b908c8bSRobert Watson #include <security/mac/mac_framework.h>
681a3044faSBjoern A. Zeeb #endif
694b908c8bSRobert Watson 
7023d374aaSBjoern A. Zeeb /* Reassembly headers are stored in hash buckets. */
712ceeacbeSJonathan T. Looney #define	IP6REASS_NHASH_LOG2	10
7280d7a853SJonathan T. Looney #define	IP6REASS_NHASH		(1 << IP6REASS_NHASH_LOG2)
7380d7a853SJonathan T. Looney #define	IP6REASS_HMASK		(IP6REASS_NHASH - 1)
7480d7a853SJonathan T. Looney 
7580d7a853SJonathan T. Looney static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
7680d7a853SJonathan T. Looney     uint32_t bucket __unused);
7780d7a853SJonathan T. Looney static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
7880d7a853SJonathan T. Looney static void frag6_insque_head(struct ip6q *, struct ip6q *,
791e9f3b73SJonathan T. Looney     uint32_t bucket);
801e9f3b73SJonathan T. Looney static void frag6_remque(struct ip6q *, uint32_t bucket);
816bbdbbb8SHans Petter Selasky static void frag6_freef(struct ip6q *, uint32_t bucket);
8280d7a853SJonathan T. Looney 
8380d7a853SJonathan T. Looney struct ip6qbucket {
8480d7a853SJonathan T. Looney 	struct ip6q	ip6q;
8580d7a853SJonathan T. Looney 	struct mtx	lock;
861e9f3b73SJonathan T. Looney 	int		count;
8780d7a853SJonathan T. Looney };
8880d7a853SJonathan T. Looney 
89487a161cSBjoern A. Zeeb static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");
90487a161cSBjoern A. Zeeb 
91757cb678SBjoern A. Zeeb /* System wide (global) maximum and count of packets in reassembly queues. */
92757cb678SBjoern A. Zeeb static int ip6_maxfrags;
93757cb678SBjoern A. Zeeb static volatile u_int frag6_nfrags = 0;
94757cb678SBjoern A. Zeeb 
95757cb678SBjoern A. Zeeb /* Maximum and current packets in per-VNET reassembly queue. */
96757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragpackets);
9780d7a853SJonathan T. Looney VNET_DEFINE_STATIC(volatile u_int,	frag6_nfragpackets);
98757cb678SBjoern A. Zeeb #define	V_ip6_maxfragpackets		VNET(ip6_maxfragpackets)
99757cb678SBjoern A. Zeeb #define	V_frag6_nfragpackets		VNET(frag6_nfragpackets)
100757cb678SBjoern A. Zeeb 
101757cb678SBjoern A. Zeeb /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
102757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragbucketsize);
103757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragsperpacket);
104757cb678SBjoern A. Zeeb #define	V_ip6_maxfragbucketsize		VNET(ip6_maxfragbucketsize)
105757cb678SBjoern A. Zeeb #define	V_ip6_maxfragsperpacket		VNET(ip6_maxfragsperpacket)
106757cb678SBjoern A. Zeeb 
107757cb678SBjoern A. Zeeb /* Per-VNET reassembly queue buckets. */
1089cb1a47aSBjoern A. Zeeb VNET_DEFINE_STATIC(struct ip6qbucket,	ip6qb[IP6REASS_NHASH]);
1099cb1a47aSBjoern A. Zeeb VNET_DEFINE_STATIC(uint32_t,		ip6qb_hashseed);
1109cb1a47aSBjoern A. Zeeb #define	V_ip6qb				VNET(ip6qb)
1119cb1a47aSBjoern A. Zeeb #define	V_ip6qb_hashseed		VNET(ip6qb_hashseed)
11282cd038dSYoshinobu Inoue 
1139cb1a47aSBjoern A. Zeeb #define	IP6QB_LOCK(_b)		mtx_lock(&V_ip6qb[(_b)].lock)
1149cb1a47aSBjoern A. Zeeb #define	IP6QB_TRYLOCK(_b)	mtx_trylock(&V_ip6qb[(_b)].lock)
1159cb1a47aSBjoern A. Zeeb #define	IP6QB_LOCK_ASSERT(_b)	mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED)
1169cb1a47aSBjoern A. Zeeb #define	IP6QB_UNLOCK(_b)	mtx_unlock(&V_ip6qb[(_b)].lock)
1179cb1a47aSBjoern A. Zeeb #define	IP6QB_HEAD(_b)		(&V_ip6qb[(_b)].ip6q)
1189888c401SHajimu UMEMOTO 
11982cd038dSYoshinobu Inoue /*
1202ceeacbeSJonathan T. Looney  * By default, limit the number of IP6 fragments across all reassembly
1212ceeacbeSJonathan T. Looney  * queues to  1/32 of the total number of mbuf clusters.
1222ceeacbeSJonathan T. Looney  *
1232ceeacbeSJonathan T. Looney  * Limit the total number of reassembly queues per VNET to the
1242ceeacbeSJonathan T. Looney  * IP6 fragment limit, but ensure the limit will not allow any bucket
1252ceeacbeSJonathan T. Looney  * to grow above 100 items. (The bucket limit is
1262ceeacbeSJonathan T. Looney  * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
1272ceeacbeSJonathan T. Looney  * multiplier to reach a 100-item limit.)
1282ceeacbeSJonathan T. Looney  * The 100-item limit was chosen as brief testing seems to show that
1292ceeacbeSJonathan T. Looney  * this produces "reasonable" performance on some subset of systems
1302ceeacbeSJonathan T. Looney  * under DoS attack.
1312ceeacbeSJonathan T. Looney  */
1322ceeacbeSJonathan T. Looney #define	IP6_MAXFRAGS		(nmbclusters / 32)
1332ceeacbeSJonathan T. Looney #define	IP6_MAXFRAGPACKETS	(imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
1342ceeacbeSJonathan T. Looney 
135757cb678SBjoern A. Zeeb 
1362ceeacbeSJonathan T. Looney /*
137757cb678SBjoern A. Zeeb  * Sysctls and helper function.
13882cd038dSYoshinobu Inoue  */
139757cb678SBjoern A. Zeeb SYSCTL_DECL(_net_inet6_ip6);
140757cb678SBjoern A. Zeeb 
141757cb678SBjoern A. Zeeb static void
14209b361c7SBjoern A. Zeeb frag6_set_bucketsize(void)
1431e9f3b73SJonathan T. Looney {
1441e9f3b73SJonathan T. Looney 	int i;
1451e9f3b73SJonathan T. Looney 
1461e9f3b73SJonathan T. Looney 	if ((i = V_ip6_maxfragpackets) > 0)
1471e9f3b73SJonathan T. Looney 		V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
1481e9f3b73SJonathan T. Looney }
1491e9f3b73SJonathan T. Looney 
150757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
151757cb678SBjoern A. Zeeb 	CTLFLAG_RW, &ip6_maxfrags, 0,
152757cb678SBjoern A. Zeeb 	"Maximum allowed number of outstanding IPv6 packet fragments. "
153757cb678SBjoern A. Zeeb 	"A value of 0 means no fragmented packets will be accepted, while a "
154757cb678SBjoern A. Zeeb 	"a value of -1 means no limit");
155757cb678SBjoern A. Zeeb 
156757cb678SBjoern A. Zeeb static int
157757cb678SBjoern A. Zeeb sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
158757cb678SBjoern A. Zeeb {
159757cb678SBjoern A. Zeeb 	int error, val;
160757cb678SBjoern A. Zeeb 
161757cb678SBjoern A. Zeeb 	val = V_ip6_maxfragpackets;
162757cb678SBjoern A. Zeeb 	error = sysctl_handle_int(oidp, &val, 0, req);
163757cb678SBjoern A. Zeeb 	if (error != 0 || !req->newptr)
164757cb678SBjoern A. Zeeb 		return (error);
165757cb678SBjoern A. Zeeb 	V_ip6_maxfragpackets = val;
166757cb678SBjoern A. Zeeb 	frag6_set_bucketsize();
167757cb678SBjoern A. Zeeb 	return (0);
168757cb678SBjoern A. Zeeb }
169757cb678SBjoern A. Zeeb SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
170757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
171757cb678SBjoern A. Zeeb 	sysctl_ip6_maxfragpackets, "I",
172757cb678SBjoern A. Zeeb 	"Default maximum number of outstanding fragmented IPv6 packets. "
173757cb678SBjoern A. Zeeb 	"A value of 0 means no fragmented packets will be accepted, while a "
174757cb678SBjoern A. Zeeb 	"a value of -1 means no limit");
175757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
176757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
177757cb678SBjoern A. Zeeb 	"Maximum allowed number of fragments per packet");
178757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
179757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
180757cb678SBjoern A. Zeeb 	"Maximum number of reassembly queues per hash bucket");
181757cb678SBjoern A. Zeeb 
182757cb678SBjoern A. Zeeb 
183757cb678SBjoern A. Zeeb /*
184c00464a2SBjoern A. Zeeb  * Remove the IPv6 fragmentation header from the mbuf.
185c00464a2SBjoern A. Zeeb  */
186c00464a2SBjoern A. Zeeb int
187c00464a2SBjoern A. Zeeb ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
188c00464a2SBjoern A. Zeeb {
1895778b399SBjoern A. Zeeb 	struct ip6_hdr *ip6;
190c00464a2SBjoern A. Zeeb 	struct mbuf *t;
191c00464a2SBjoern A. Zeeb 
192c00464a2SBjoern A. Zeeb 	/* Delete frag6 header. */
193c00464a2SBjoern A. Zeeb 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
1945778b399SBjoern A. Zeeb 
195c00464a2SBjoern A. Zeeb 		/* This is the only possible case with !PULLDOWN_TEST. */
1965778b399SBjoern A. Zeeb 		ip6  = mtod(m, struct ip6_hdr *);
197c00464a2SBjoern A. Zeeb 		bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
198c00464a2SBjoern A. Zeeb 		    offset);
199c00464a2SBjoern A. Zeeb 		m->m_data += sizeof(struct ip6_frag);
200c00464a2SBjoern A. Zeeb 		m->m_len -= sizeof(struct ip6_frag);
201c00464a2SBjoern A. Zeeb 	} else {
2025778b399SBjoern A. Zeeb 
203c00464a2SBjoern A. Zeeb 		/* This comes with no copy if the boundary is on cluster. */
204c00464a2SBjoern A. Zeeb 		if ((t = m_split(m, offset, wait)) == NULL)
205c00464a2SBjoern A. Zeeb 			return (ENOMEM);
206c00464a2SBjoern A. Zeeb 		m_adj(t, sizeof(struct ip6_frag));
207c00464a2SBjoern A. Zeeb 		m_cat(m, t);
208c00464a2SBjoern A. Zeeb 	}
209c00464a2SBjoern A. Zeeb 
210c00464a2SBjoern A. Zeeb 	m->m_flags |= M_FRAGMENTED;
211c00464a2SBjoern A. Zeeb 	return (0);
212c00464a2SBjoern A. Zeeb }
213c00464a2SBjoern A. Zeeb 
214c00464a2SBjoern A. Zeeb /*
21523d374aaSBjoern A. Zeeb  * Free a fragment reassembly header and all associated datagrams.
216757cb678SBjoern A. Zeeb  */
2174f590175SPaul Saab static void
218c00464a2SBjoern A. Zeeb frag6_freef(struct ip6q *q6, uint32_t bucket)
2194f590175SPaul Saab {
2205778b399SBjoern A. Zeeb 	struct ip6_hdr *ip6;
221c00464a2SBjoern A. Zeeb 	struct ip6asfrag *af6, *down6;
2225778b399SBjoern A. Zeeb 	struct mbuf *m;
2234f590175SPaul Saab 
2249cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK_ASSERT(bucket);
225c00464a2SBjoern A. Zeeb 
226c00464a2SBjoern A. Zeeb 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
227c00464a2SBjoern A. Zeeb 	     af6 = down6) {
228c00464a2SBjoern A. Zeeb 
2295778b399SBjoern A. Zeeb 		m = IP6_REASS_MBUF(af6);
230c00464a2SBjoern A. Zeeb 		down6 = af6->ip6af_down;
231c00464a2SBjoern A. Zeeb 		frag6_deq(af6, bucket);
232c00464a2SBjoern A. Zeeb 
233c00464a2SBjoern A. Zeeb 		/*
234c00464a2SBjoern A. Zeeb 		 * Return ICMP time exceeded error for the 1st fragment.
235c00464a2SBjoern A. Zeeb 		 * Just free other fragments.
236c00464a2SBjoern A. Zeeb 		 */
237c00464a2SBjoern A. Zeeb 		if (af6->ip6af_off == 0) {
238c00464a2SBjoern A. Zeeb 
23923d374aaSBjoern A. Zeeb 			/* Adjust pointer. */
240c00464a2SBjoern A. Zeeb 			ip6 = mtod(m, struct ip6_hdr *);
241c00464a2SBjoern A. Zeeb 
24223d374aaSBjoern A. Zeeb 			/* Restore source and destination addresses. */
243c00464a2SBjoern A. Zeeb 			ip6->ip6_src = q6->ip6q_src;
244c00464a2SBjoern A. Zeeb 			ip6->ip6_dst = q6->ip6q_dst;
245c00464a2SBjoern A. Zeeb 
246c00464a2SBjoern A. Zeeb 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
247c00464a2SBjoern A. Zeeb 			    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
248c00464a2SBjoern A. Zeeb 		} else
249c00464a2SBjoern A. Zeeb 			m_freem(m);
25023d374aaSBjoern A. Zeeb 
251c00464a2SBjoern A. Zeeb 		free(af6, M_FRAG6);
2522adfd64fSJonathan T. Looney 	}
253c00464a2SBjoern A. Zeeb 	frag6_remque(q6, bucket);
254c00464a2SBjoern A. Zeeb 	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
255c00464a2SBjoern A. Zeeb #ifdef MAC
256c00464a2SBjoern A. Zeeb 	mac_ip6q_destroy(q6);
257c00464a2SBjoern A. Zeeb #endif
258c00464a2SBjoern A. Zeeb 	free(q6, M_FRAG6);
259c00464a2SBjoern A. Zeeb 	atomic_subtract_int(&V_frag6_nfragpackets, 1);
26082cd038dSYoshinobu Inoue }
26182cd038dSYoshinobu Inoue 
26282cd038dSYoshinobu Inoue /*
26323d374aaSBjoern A. Zeeb  * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with
26423d374aaSBjoern A. Zeeb  * each other, in terms of next header field handling in fragment header.
265686cdd19SJun-ichiro itojun Hagino  * While the sender will use the same value for all of the fragmented packets,
26623d374aaSBjoern A. Zeeb  * receiver is suggested not to check for consistency.
267686cdd19SJun-ichiro itojun Hagino  *
26823d374aaSBjoern A. Zeeb  * Fragment rules (p18,p19):
269686cdd19SJun-ichiro itojun Hagino  *	(2)  A Fragment header containing:
27023d374aaSBjoern A. Zeeb  *	The Next Header value that identifies the first header
27123d374aaSBjoern A. Zeeb  *	after the Per-Fragment headers of the original packet.
272686cdd19SJun-ichiro itojun Hagino  *		-> next header field is same for all fragments
273686cdd19SJun-ichiro itojun Hagino  *
27423d374aaSBjoern A. Zeeb  * Reassembly rule (p20):
27523d374aaSBjoern A. Zeeb  *	The Next Header field of the last header of the Per-Fragment
27623d374aaSBjoern A. Zeeb  *	headers is obtained from the Next Header field of the first
277686cdd19SJun-ichiro itojun Hagino  *	fragment's Fragment header.
278686cdd19SJun-ichiro itojun Hagino  *		-> should grab it from the first fragment only
279686cdd19SJun-ichiro itojun Hagino  *
280686cdd19SJun-ichiro itojun Hagino  * The following note also contradicts with fragment rule - no one is going to
281686cdd19SJun-ichiro itojun Hagino  * send different fragment with different next header field.
282686cdd19SJun-ichiro itojun Hagino  *
28323d374aaSBjoern A. Zeeb  * Additional note (p22) [not an error]:
284686cdd19SJun-ichiro itojun Hagino  *	The Next Header values in the Fragment headers of different
285686cdd19SJun-ichiro itojun Hagino  *	fragments of the same original packet may differ.  Only the value
286686cdd19SJun-ichiro itojun Hagino  *	from the Offset zero fragment packet is used for reassembly.
287686cdd19SJun-ichiro itojun Hagino  *		-> should grab it from the first fragment only
288686cdd19SJun-ichiro itojun Hagino  *
289686cdd19SJun-ichiro itojun Hagino  * There is no explicit reason given in the RFC.  Historical reason maybe?
290686cdd19SJun-ichiro itojun Hagino  */
291686cdd19SJun-ichiro itojun Hagino /*
29223d374aaSBjoern A. Zeeb  * Fragment input.
29382cd038dSYoshinobu Inoue  */
29482cd038dSYoshinobu Inoue int
2951272577eSXin LI frag6_input(struct mbuf **mp, int *offp, int proto)
29682cd038dSYoshinobu Inoue {
2975778b399SBjoern A. Zeeb 	struct ifnet *dstifp;
2985778b399SBjoern A. Zeeb 	struct in6_ifaddr *ia6;
29982cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6;
30082cd038dSYoshinobu Inoue 	struct ip6_frag *ip6f;
30180d7a853SJonathan T. Looney 	struct ip6q *head, *q6;
3025778b399SBjoern A. Zeeb 	struct ip6asfrag *af6, *af6dwn, *ip6af;
3035778b399SBjoern A. Zeeb 	struct mbuf *m, *t;
304505e91f5SKristof Provost 	uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
305505e91f5SKristof Provost 		    sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
3069cb1a47aSBjoern A. Zeeb 	uint32_t bucket, *hashkeyp;
3075778b399SBjoern A. Zeeb 	int fragoff, frgpartlen;	/* Must be larger than uint16_t. */
3085778b399SBjoern A. Zeeb 	int nxt, offset, plen;
3095778b399SBjoern A. Zeeb 	uint8_t ecn, ecn0;
3105778b399SBjoern A. Zeeb 	bool only_frag;
311aaa46574SAdrian Chadd #ifdef RSS
312aaa46574SAdrian Chadd 	struct ip6_direct_ctx *ip6dc;
3135778b399SBjoern A. Zeeb 	struct m_tag *mtag;
314aaa46574SAdrian Chadd #endif
315aaa46574SAdrian Chadd 
3165778b399SBjoern A. Zeeb 	m = *mp;
3175778b399SBjoern A. Zeeb 	offset = *offp;
3185778b399SBjoern A. Zeeb 
31982cd038dSYoshinobu Inoue 	ip6 = mtod(m, struct ip6_hdr *);
320686cdd19SJun-ichiro itojun Hagino #ifndef PULLDOWN_TEST
321686cdd19SJun-ichiro itojun Hagino 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
32282cd038dSYoshinobu Inoue 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
323686cdd19SJun-ichiro itojun Hagino #else
324686cdd19SJun-ichiro itojun Hagino 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
325686cdd19SJun-ichiro itojun Hagino 	if (ip6f == NULL)
32640e39bbbSHajimu UMEMOTO 		return (IPPROTO_DONE);
327686cdd19SJun-ichiro itojun Hagino #endif
32882cd038dSYoshinobu Inoue 
32982cd038dSYoshinobu Inoue 	dstifp = NULL;
33023d374aaSBjoern A. Zeeb 	/* Find the destination interface of the packet. */
3315778b399SBjoern A. Zeeb 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
3325778b399SBjoern A. Zeeb 	if (ia6 != NULL) {
3335778b399SBjoern A. Zeeb 		dstifp = ia6->ia_ifp;
3345778b399SBjoern A. Zeeb 		ifa_free(&ia6->ia_ifa);
3358c0fec80SRobert Watson 	}
33623d374aaSBjoern A. Zeeb 
33723d374aaSBjoern A. Zeeb 	/* Jumbo payload cannot contain a fragment header. */
33882cd038dSYoshinobu Inoue 	if (ip6->ip6_plen == 0) {
33982cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
34082cd038dSYoshinobu Inoue 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
3415778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
34282cd038dSYoshinobu Inoue 	}
34382cd038dSYoshinobu Inoue 
34482cd038dSYoshinobu Inoue 	/*
34523d374aaSBjoern A. Zeeb 	 * Check whether fragment packet's fragment length is a
34623d374aaSBjoern A. Zeeb 	 * multiple of 8 octets (unless it is the last one).
34782cd038dSYoshinobu Inoue 	 * sizeof(struct ip6_frag) == 8
34882cd038dSYoshinobu Inoue 	 * sizeof(struct ip6_hdr) = 40
34982cd038dSYoshinobu Inoue 	 */
35082cd038dSYoshinobu Inoue 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
35182cd038dSYoshinobu Inoue 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
35206cd0a3fSHajimu UMEMOTO 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
353686cdd19SJun-ichiro itojun Hagino 		    offsetof(struct ip6_hdr, ip6_plen));
35482cd038dSYoshinobu Inoue 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
3555778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
35682cd038dSYoshinobu Inoue 	}
35782cd038dSYoshinobu Inoue 
3589cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_fragments);
35982cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
36082cd038dSYoshinobu Inoue 
36123d374aaSBjoern A. Zeeb 	/* Offset now points to data portion. */
36282cd038dSYoshinobu Inoue 	offset += sizeof(struct ip6_frag);
36382cd038dSYoshinobu Inoue 
3644018ea9aSBjoern A. Zeeb 	/*
3652946a941STom Jones 	 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
36623d374aaSBjoern A. Zeeb 	 * unrelated to any reassembly.  Still need to remove the frag hdr.
36723d374aaSBjoern A. Zeeb 	 * See RFC 6946 and section 4.5 of RFC 8200.
3684018ea9aSBjoern A. Zeeb 	 */
3694018ea9aSBjoern A. Zeeb 	if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
3702946a941STom Jones 		IP6STAT_INC(ip6s_atomicfrags);
37123d374aaSBjoern A. Zeeb 		/* XXX-BZ handle correctly. */
3724018ea9aSBjoern A. Zeeb 		in6_ifstat_inc(dstifp, ifs6_reass_ok);
3734018ea9aSBjoern A. Zeeb 		*offp = offset;
374a4061289SAndrey V. Elsukov 		m->m_flags |= M_FRAGMENTED;
3754018ea9aSBjoern A. Zeeb 		return (ip6f->ip6f_nxt);
3764018ea9aSBjoern A. Zeeb 	}
3774018ea9aSBjoern A. Zeeb 
3785f9f192dSJonathan T. Looney 	/* Get fragment length and discard 0-byte fragments. */
3795f9f192dSJonathan T. Looney 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
3805f9f192dSJonathan T. Looney 	if (frgpartlen == 0) {
3815f9f192dSJonathan T. Looney 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
3825f9f192dSJonathan T. Looney 		    offsetof(struct ip6_hdr, ip6_plen));
3835f9f192dSJonathan T. Looney 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
3845f9f192dSJonathan T. Looney 		IP6STAT_INC(ip6s_fragdropped);
3855778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
3865f9f192dSJonathan T. Looney 	}
3875f9f192dSJonathan T. Looney 
38823d374aaSBjoern A. Zeeb 	/* Generate a hash value for fragment bucket selection. */
38980d7a853SJonathan T. Looney 	hashkeyp = hashkey;
39080d7a853SJonathan T. Looney 	memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
39180d7a853SJonathan T. Looney 	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
39280d7a853SJonathan T. Looney 	memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
39380d7a853SJonathan T. Looney 	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
39480d7a853SJonathan T. Looney 	*hashkeyp = ip6f->ip6f_ident;
3959cb1a47aSBjoern A. Zeeb 	bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed);
3969cb1a47aSBjoern A. Zeeb 	bucket &= IP6REASS_HMASK;
3979cb1a47aSBjoern A. Zeeb 	head = IP6QB_HEAD(bucket);
3989cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK(bucket);
3999888c401SHajimu UMEMOTO 
4009888c401SHajimu UMEMOTO 	/*
40123d374aaSBjoern A. Zeeb 	 * Enforce upper bound on number of fragments for the entire system.
4029888c401SHajimu UMEMOTO 	 * If maxfrag is 0, never accept fragments.
4039888c401SHajimu UMEMOTO 	 * If maxfrag is -1, accept all fragments without limitation.
4049888c401SHajimu UMEMOTO 	 */
4052adfd64fSJonathan T. Looney 	if (ip6_maxfrags < 0)
4069888c401SHajimu UMEMOTO 		;
4072adfd64fSJonathan T. Looney 	else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
4089888c401SHajimu UMEMOTO 		goto dropfrag;
40933841545SHajimu UMEMOTO 
41080d7a853SJonathan T. Looney 	for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
41182cd038dSYoshinobu Inoue 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
41282cd038dSYoshinobu Inoue 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
4134b908c8bSRobert Watson 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
4144b908c8bSRobert Watson #ifdef MAC
4154b908c8bSRobert Watson 		    && mac_ip6q_match(m, q6)
4164b908c8bSRobert Watson #endif
4174b908c8bSRobert Watson 		    )
41882cd038dSYoshinobu Inoue 			break;
41982cd038dSYoshinobu Inoue 
4205778b399SBjoern A. Zeeb 	only_frag = false;
42180d7a853SJonathan T. Looney 	if (q6 == head) {
42223d374aaSBjoern A. Zeeb 
4235778b399SBjoern A. Zeeb 		/* A first fragment to arrive creates a reassembly queue. */
4245778b399SBjoern A. Zeeb 		only_frag = true;
42582cd038dSYoshinobu Inoue 
42682cd038dSYoshinobu Inoue 		/*
42782cd038dSYoshinobu Inoue 		 * Enforce upper bound on number of fragmented packets
42882cd038dSYoshinobu Inoue 		 * for which we attempt reassembly;
4299888c401SHajimu UMEMOTO 		 * If maxfragpackets is 0, never accept fragments.
4309888c401SHajimu UMEMOTO 		 * If maxfragpackets is -1, accept all fragments without
4319888c401SHajimu UMEMOTO 		 * limitation.
43282cd038dSYoshinobu Inoue 		 */
433603724d3SBjoern A. Zeeb 		if (V_ip6_maxfragpackets < 0)
43433841545SHajimu UMEMOTO 			;
4359cb1a47aSBjoern A. Zeeb 		else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize ||
4361e9f3b73SJonathan T. Looney 		    atomic_load_int(&V_frag6_nfragpackets) >=
43780d7a853SJonathan T. Looney 		    (u_int)V_ip6_maxfragpackets)
43833841545SHajimu UMEMOTO 			goto dropfrag;
43980d7a853SJonathan T. Looney 		atomic_add_int(&V_frag6_nfragpackets, 1);
44023d374aaSBjoern A. Zeeb 
44123d374aaSBjoern A. Zeeb 		/* Allocate IPv6 fragement packet queue entry. */
442487a161cSBjoern A. Zeeb 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6,
443487a161cSBjoern A. Zeeb 		    M_NOWAIT | M_ZERO);
44482cd038dSYoshinobu Inoue 		if (q6 == NULL)
44582cd038dSYoshinobu Inoue 			goto dropfrag;
4464b908c8bSRobert Watson #ifdef MAC
4474b908c8bSRobert Watson 		if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
448487a161cSBjoern A. Zeeb 			free(q6, M_FRAG6);
4494b908c8bSRobert Watson 			goto dropfrag;
4504b908c8bSRobert Watson 		}
4514b908c8bSRobert Watson 		mac_ip6q_create(m, q6);
4524b908c8bSRobert Watson #endif
4539cb1a47aSBjoern A. Zeeb 		frag6_insque_head(q6, head, bucket);
45482cd038dSYoshinobu Inoue 
45523d374aaSBjoern A. Zeeb 		/* ip6q_nxt will be filled afterwards, from 1st fragment. */
45682cd038dSYoshinobu Inoue 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
457686cdd19SJun-ichiro itojun Hagino #ifdef notyet
458686cdd19SJun-ichiro itojun Hagino 		q6->ip6q_nxtp	= (u_char *)nxtp;
459686cdd19SJun-ichiro itojun Hagino #endif
46082cd038dSYoshinobu Inoue 		q6->ip6q_ident	= ip6f->ip6f_ident;
46182cd038dSYoshinobu Inoue 		q6->ip6q_ttl	= IPV6_FRAGTTL;
46282cd038dSYoshinobu Inoue 		q6->ip6q_src	= ip6->ip6_src;
46382cd038dSYoshinobu Inoue 		q6->ip6q_dst	= ip6->ip6_dst;
4645e9510e3SJINMEI Tatuya 		q6->ip6q_ecn	=
4655e9510e3SJINMEI Tatuya 		    (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
46682cd038dSYoshinobu Inoue 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
4679888c401SHajimu UMEMOTO 
4689888c401SHajimu UMEMOTO 		q6->ip6q_nfrag = 0;
46982cd038dSYoshinobu Inoue 	}
47082cd038dSYoshinobu Inoue 
47182cd038dSYoshinobu Inoue 	/*
47223d374aaSBjoern A. Zeeb 	 * If it is the 1st fragment, record the length of the
47382cd038dSYoshinobu Inoue 	 * unfragmentable part and the next header of the fragment header.
47482cd038dSYoshinobu Inoue 	 */
47582cd038dSYoshinobu Inoue 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
47682cd038dSYoshinobu Inoue 	if (fragoff == 0) {
47706cd0a3fSHajimu UMEMOTO 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
47806cd0a3fSHajimu UMEMOTO 		    sizeof(struct ip6_frag);
47982cd038dSYoshinobu Inoue 		q6->ip6q_nxt = ip6f->ip6f_nxt;
48082cd038dSYoshinobu Inoue 	}
48182cd038dSYoshinobu Inoue 
48282cd038dSYoshinobu Inoue 	/*
48382cd038dSYoshinobu Inoue 	 * Check that the reassembled packet would not exceed 65535 bytes
48482cd038dSYoshinobu Inoue 	 * in size.
48582cd038dSYoshinobu Inoue 	 * If it would exceed, discard the fragment and return an ICMP error.
48682cd038dSYoshinobu Inoue 	 */
48782cd038dSYoshinobu Inoue 	if (q6->ip6q_unfrglen >= 0) {
48882cd038dSYoshinobu Inoue 		/* The 1st fragment has already arrived. */
48982cd038dSYoshinobu Inoue 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
49082cd038dSYoshinobu Inoue 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
491686cdd19SJun-ichiro itojun Hagino 			    offset - sizeof(struct ip6_frag) +
492686cdd19SJun-ichiro itojun Hagino 			    offsetof(struct ip6_frag, ip6f_offlg));
4939cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
49482cd038dSYoshinobu Inoue 			return (IPPROTO_DONE);
49582cd038dSYoshinobu Inoue 		}
49606cd0a3fSHajimu UMEMOTO 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
49782cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
498686cdd19SJun-ichiro itojun Hagino 		    offset - sizeof(struct ip6_frag) +
499686cdd19SJun-ichiro itojun Hagino 		    offsetof(struct ip6_frag, ip6f_offlg));
5009cb1a47aSBjoern A. Zeeb 		IP6QB_UNLOCK(bucket);
50182cd038dSYoshinobu Inoue 		return (IPPROTO_DONE);
50282cd038dSYoshinobu Inoue 	}
50382cd038dSYoshinobu Inoue 	/*
50423d374aaSBjoern A. Zeeb 	 * If it is the first fragment, do the above check for each
50582cd038dSYoshinobu Inoue 	 * fragment already stored in the reassembly queue.
50682cd038dSYoshinobu Inoue 	 */
50782cd038dSYoshinobu Inoue 	if (fragoff == 0) {
50882cd038dSYoshinobu Inoue 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
50982cd038dSYoshinobu Inoue 		     af6 = af6dwn) {
51082cd038dSYoshinobu Inoue 			af6dwn = af6->ip6af_down;
51182cd038dSYoshinobu Inoue 
51282cd038dSYoshinobu Inoue 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
51382cd038dSYoshinobu Inoue 			    IPV6_MAXPACKET) {
51482cd038dSYoshinobu Inoue 				struct ip6_hdr *ip6err;
5155778b399SBjoern A. Zeeb 				struct mbuf *merr;
5165778b399SBjoern A. Zeeb 				int erroff;
5175778b399SBjoern A. Zeeb 
5185778b399SBjoern A. Zeeb 				merr = IP6_REASS_MBUF(af6);
5195778b399SBjoern A. Zeeb 				erroff = af6->ip6af_offset;
52082cd038dSYoshinobu Inoue 
52123d374aaSBjoern A. Zeeb 				/* Dequeue the fragment. */
5229cb1a47aSBjoern A. Zeeb 				frag6_deq(af6, bucket);
523487a161cSBjoern A. Zeeb 				free(af6, M_FRAG6);
52482cd038dSYoshinobu Inoue 
52523d374aaSBjoern A. Zeeb 				/* Adjust pointer. */
52682cd038dSYoshinobu Inoue 				ip6err = mtod(merr, struct ip6_hdr *);
52782cd038dSYoshinobu Inoue 
52882cd038dSYoshinobu Inoue 				/*
52982cd038dSYoshinobu Inoue 				 * Restore source and destination addresses
53082cd038dSYoshinobu Inoue 				 * in the erroneous IPv6 header.
53182cd038dSYoshinobu Inoue 				 */
53282cd038dSYoshinobu Inoue 				ip6err->ip6_src = q6->ip6q_src;
53382cd038dSYoshinobu Inoue 				ip6err->ip6_dst = q6->ip6q_dst;
53482cd038dSYoshinobu Inoue 
53582cd038dSYoshinobu Inoue 				icmp6_error(merr, ICMP6_PARAM_PROB,
53682cd038dSYoshinobu Inoue 				    ICMP6_PARAMPROB_HEADER,
537686cdd19SJun-ichiro itojun Hagino 				    erroff - sizeof(struct ip6_frag) +
538686cdd19SJun-ichiro itojun Hagino 				    offsetof(struct ip6_frag, ip6f_offlg));
53982cd038dSYoshinobu Inoue 			}
54082cd038dSYoshinobu Inoue 		}
54182cd038dSYoshinobu Inoue 	}
54282cd038dSYoshinobu Inoue 
54323d374aaSBjoern A. Zeeb 	/* Allocate an IPv6 fragement queue entry for this fragmented part. */
544487a161cSBjoern A. Zeeb 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6,
545487a161cSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
546686cdd19SJun-ichiro itojun Hagino 	if (ip6af == NULL)
547686cdd19SJun-ichiro itojun Hagino 		goto dropfrag;
54882cd038dSYoshinobu Inoue 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
54982cd038dSYoshinobu Inoue 	ip6af->ip6af_off = fragoff;
55082cd038dSYoshinobu Inoue 	ip6af->ip6af_frglen = frgpartlen;
55182cd038dSYoshinobu Inoue 	ip6af->ip6af_offset = offset;
55282cd038dSYoshinobu Inoue 	IP6_REASS_MBUF(ip6af) = m;
55382cd038dSYoshinobu Inoue 
5545778b399SBjoern A. Zeeb 	if (only_frag) {
55582cd038dSYoshinobu Inoue 		af6 = (struct ip6asfrag *)q6;
55682cd038dSYoshinobu Inoue 		goto insert;
55782cd038dSYoshinobu Inoue 	}
55882cd038dSYoshinobu Inoue 
55923d374aaSBjoern A. Zeeb 	/* Do duplicate, condition, and boundry checks. */
56082cd038dSYoshinobu Inoue 	/*
56159dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
56259dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
56323d374aaSBjoern A. Zeeb 	 * Drop if CE and not-ECT are mixed for the same packet.
56459dfcba4SHajimu UMEMOTO 	 */
56559dfcba4SHajimu UMEMOTO 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
5665e9510e3SJINMEI Tatuya 	ecn0 = q6->ip6q_ecn;
56759dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
56859dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT) {
569487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
57059dfcba4SHajimu UMEMOTO 			goto dropfrag;
57159dfcba4SHajimu UMEMOTO 		}
57259dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
5735e9510e3SJINMEI Tatuya 			q6->ip6q_ecn = IPTOS_ECN_CE;
57459dfcba4SHajimu UMEMOTO 	}
57559dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
576487a161cSBjoern A. Zeeb 		free(ip6af, M_FRAG6);
57759dfcba4SHajimu UMEMOTO 		goto dropfrag;
57859dfcba4SHajimu UMEMOTO 	}
57959dfcba4SHajimu UMEMOTO 
58023d374aaSBjoern A. Zeeb 	/* Find a fragmented part which begins after this one does. */
58182cd038dSYoshinobu Inoue 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
58282cd038dSYoshinobu Inoue 	     af6 = af6->ip6af_down)
58382cd038dSYoshinobu Inoue 		if (af6->ip6af_off > ip6af->ip6af_off)
58482cd038dSYoshinobu Inoue 			break;
58582cd038dSYoshinobu Inoue 
58682cd038dSYoshinobu Inoue 	/*
58782cd038dSYoshinobu Inoue 	 * If the incoming framgent overlaps some existing fragments in
58823d374aaSBjoern A. Zeeb 	 * the reassembly queue, drop both the new fragment and the
58923d374aaSBjoern A. Zeeb 	 * entire reassembly queue.  However, if the new fragment
59023d374aaSBjoern A. Zeeb 	 * is an exact duplicate of an existing fragment, only silently
59123d374aaSBjoern A. Zeeb 	 * drop the existing fragment and leave the fragmentation queue
59223d374aaSBjoern A. Zeeb 	 * unchanged, as allowed by the RFC.  (RFC 8200, 4.5)
59382cd038dSYoshinobu Inoue 	 */
59482cd038dSYoshinobu Inoue 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
5955778b399SBjoern A. Zeeb 		if (af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen -
5965778b399SBjoern A. Zeeb 		    ip6af->ip6af_off > 0) {
597487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
59882cd038dSYoshinobu Inoue 			goto dropfrag;
59982cd038dSYoshinobu Inoue 		}
60082cd038dSYoshinobu Inoue 	}
60182cd038dSYoshinobu Inoue 	if (af6 != (struct ip6asfrag *)q6) {
6025778b399SBjoern A. Zeeb 		if (ip6af->ip6af_off + ip6af->ip6af_frglen -
6035778b399SBjoern A. Zeeb 		    af6->ip6af_off > 0) {
604487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
60582cd038dSYoshinobu Inoue 			goto dropfrag;
60682cd038dSYoshinobu Inoue 		}
60782cd038dSYoshinobu Inoue 	}
60882cd038dSYoshinobu Inoue 
60982cd038dSYoshinobu Inoue insert:
6104b908c8bSRobert Watson #ifdef MAC
6115778b399SBjoern A. Zeeb 	if (!only_frag)
6124b908c8bSRobert Watson 		mac_ip6q_update(m, q6);
6134b908c8bSRobert Watson #endif
61482cd038dSYoshinobu Inoue 
61582cd038dSYoshinobu Inoue 	/*
61623d374aaSBjoern A. Zeeb 	 * Stick new segment in its place; check for complete reassembly.
61723d374aaSBjoern A. Zeeb 	 * If not complete, check fragment limit.  Move to front of packet
61823d374aaSBjoern A. Zeeb 	 * queue, as we are the most recently active fragmented packet.
61982cd038dSYoshinobu Inoue 	 */
6209cb1a47aSBjoern A. Zeeb 	frag6_enq(ip6af, af6->ip6af_up, bucket);
6212adfd64fSJonathan T. Looney 	atomic_add_int(&frag6_nfrags, 1);
6229888c401SHajimu UMEMOTO 	q6->ip6q_nfrag++;
6235778b399SBjoern A. Zeeb 	plen = 0;
62482cd038dSYoshinobu Inoue 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
62582cd038dSYoshinobu Inoue 	     af6 = af6->ip6af_down) {
6265778b399SBjoern A. Zeeb 		if (af6->ip6af_off != plen) {
62703c99d76SJonathan T. Looney 			if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
628198fdaedSTom Jones 				IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
6299cb1a47aSBjoern A. Zeeb 				frag6_freef(q6, bucket);
63003c99d76SJonathan T. Looney 			}
6319cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
6325778b399SBjoern A. Zeeb 			return (IPPROTO_DONE);
63382cd038dSYoshinobu Inoue 		}
6345778b399SBjoern A. Zeeb 		plen += af6->ip6af_frglen;
63582cd038dSYoshinobu Inoue 	}
63682cd038dSYoshinobu Inoue 	if (af6->ip6af_up->ip6af_mff) {
63703c99d76SJonathan T. Looney 		if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
638198fdaedSTom Jones 			IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
6399cb1a47aSBjoern A. Zeeb 			frag6_freef(q6, bucket);
64003c99d76SJonathan T. Looney 		}
6419cb1a47aSBjoern A. Zeeb 		IP6QB_UNLOCK(bucket);
6425778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
64382cd038dSYoshinobu Inoue 	}
64482cd038dSYoshinobu Inoue 
64523d374aaSBjoern A. Zeeb 	/* Reassembly is complete; concatenate fragments. */
64682cd038dSYoshinobu Inoue 	ip6af = q6->ip6q_down;
64782cd038dSYoshinobu Inoue 	t = m = IP6_REASS_MBUF(ip6af);
64882cd038dSYoshinobu Inoue 	af6 = ip6af->ip6af_down;
6499cb1a47aSBjoern A. Zeeb 	frag6_deq(ip6af, bucket);
65082cd038dSYoshinobu Inoue 	while (af6 != (struct ip6asfrag *)q6) {
6519907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_flags &=
6529907aba3SAndrey V. Elsukov 		    IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
6539907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_data +=
6549907aba3SAndrey V. Elsukov 		    IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
6559907aba3SAndrey V. Elsukov 
656686cdd19SJun-ichiro itojun Hagino 		af6dwn = af6->ip6af_down;
6579cb1a47aSBjoern A. Zeeb 		frag6_deq(af6, bucket);
65882cd038dSYoshinobu Inoue 		while (t->m_next)
65982cd038dSYoshinobu Inoue 			t = t->m_next;
660ba99cc0bSAlexander V. Chernikov 		m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
66109b0b8c0SNavdeep Parhar 		m_demote_pkthdr(IP6_REASS_MBUF(af6));
662ba99cc0bSAlexander V. Chernikov 		m_cat(t, IP6_REASS_MBUF(af6));
663487a161cSBjoern A. Zeeb 		free(af6, M_FRAG6);
664686cdd19SJun-ichiro itojun Hagino 		af6 = af6dwn;
66582cd038dSYoshinobu Inoue 	}
66682cd038dSYoshinobu Inoue 
6679907aba3SAndrey V. Elsukov 	while (m->m_pkthdr.csum_data & 0xffff0000)
6689907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
6699907aba3SAndrey V. Elsukov 		    (m->m_pkthdr.csum_data >> 16);
6709907aba3SAndrey V. Elsukov 
67123d374aaSBjoern A. Zeeb 	/* Adjust offset to point where the original next header starts. */
67282cd038dSYoshinobu Inoue 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
673487a161cSBjoern A. Zeeb 	free(ip6af, M_FRAG6);
674686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
6755778b399SBjoern A. Zeeb 	ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr));
6765e9510e3SJINMEI Tatuya 	if (q6->ip6q_ecn == IPTOS_ECN_CE)
6775e9510e3SJINMEI Tatuya 		ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
67882cd038dSYoshinobu Inoue 	nxt = q6->ip6q_nxt;
67982cd038dSYoshinobu Inoue 
6800b438b0fSGleb Smirnoff 	if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
6819cb1a47aSBjoern A. Zeeb 		frag6_remque(q6, bucket);
6822adfd64fSJonathan T. Looney 		atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
6834b908c8bSRobert Watson #ifdef MAC
6844b908c8bSRobert Watson 		mac_ip6q_destroy(q6);
6854b908c8bSRobert Watson #endif
686487a161cSBjoern A. Zeeb 		free(q6, M_FRAG6);
68780d7a853SJonathan T. Looney 		atomic_subtract_int(&V_frag6_nfragpackets, 1);
6880b438b0fSGleb Smirnoff 
689686cdd19SJun-ichiro itojun Hagino 		goto dropfrag;
69082cd038dSYoshinobu Inoue 	}
69182cd038dSYoshinobu Inoue 
69223d374aaSBjoern A. Zeeb 	/* Set nxt(-hdr field value) to the original value. */
69368e0e5a6SAndrey V. Elsukov 	m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
69468e0e5a6SAndrey V. Elsukov 	    (caddr_t)&nxt);
69582cd038dSYoshinobu Inoue 
6969cb1a47aSBjoern A. Zeeb 	frag6_remque(q6, bucket);
6972adfd64fSJonathan T. Looney 	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
6984b908c8bSRobert Watson #ifdef MAC
6994b908c8bSRobert Watson 	mac_ip6q_reassemble(q6, m);
7004b908c8bSRobert Watson 	mac_ip6q_destroy(q6);
7014b908c8bSRobert Watson #endif
702487a161cSBjoern A. Zeeb 	free(q6, M_FRAG6);
70380d7a853SJonathan T. Looney 	atomic_subtract_int(&V_frag6_nfragpackets, 1);
70482cd038dSYoshinobu Inoue 
70582cd038dSYoshinobu Inoue 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
7065778b399SBjoern A. Zeeb 
7075778b399SBjoern A. Zeeb 		plen = 0;
70882cd038dSYoshinobu Inoue 		for (t = m; t; t = t->m_next)
70982cd038dSYoshinobu Inoue 			plen += t->m_len;
71082cd038dSYoshinobu Inoue 		m->m_pkthdr.len = plen;
71182cd038dSYoshinobu Inoue 	}
71282cd038dSYoshinobu Inoue 
713aaa46574SAdrian Chadd #ifdef RSS
714aaa46574SAdrian Chadd 	mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
715aaa46574SAdrian Chadd 	    M_NOWAIT);
716aaa46574SAdrian Chadd 	if (mtag == NULL)
717aaa46574SAdrian Chadd 		goto dropfrag;
718aaa46574SAdrian Chadd 
719aaa46574SAdrian Chadd 	ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
720aaa46574SAdrian Chadd 	ip6dc->ip6dc_nxt = nxt;
721aaa46574SAdrian Chadd 	ip6dc->ip6dc_off = offset;
722aaa46574SAdrian Chadd 
723aaa46574SAdrian Chadd 	m_tag_prepend(m, mtag);
724aaa46574SAdrian Chadd #endif
725aaa46574SAdrian Chadd 
7269cb1a47aSBjoern A. Zeeb 	IP6QB_UNLOCK(bucket);
7279cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_reassembled);
72882cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
72982cd038dSYoshinobu Inoue 
730aaa46574SAdrian Chadd #ifdef RSS
73123d374aaSBjoern A. Zeeb 	/* Queue/dispatch for reprocessing. */
732aaa46574SAdrian Chadd 	netisr_dispatch(NETISR_IPV6_DIRECT, m);
7335778b399SBjoern A. Zeeb 	return (IPPROTO_DONE);
734aaa46574SAdrian Chadd #endif
735aaa46574SAdrian Chadd 
73623d374aaSBjoern A. Zeeb 	/* Tell launch routine the next header. */
73782cd038dSYoshinobu Inoue 	*mp = m;
73882cd038dSYoshinobu Inoue 	*offp = offset;
73982cd038dSYoshinobu Inoue 
7405778b399SBjoern A. Zeeb 	return (nxt);
74182cd038dSYoshinobu Inoue 
74282cd038dSYoshinobu Inoue dropfrag:
7439cb1a47aSBjoern A. Zeeb 	IP6QB_UNLOCK(bucket);
74482cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
7459cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_fragdropped);
74682cd038dSYoshinobu Inoue 	m_freem(m);
7475778b399SBjoern A. Zeeb 	return (IPPROTO_DONE);
74882cd038dSYoshinobu Inoue }
74982cd038dSYoshinobu Inoue 
75082cd038dSYoshinobu Inoue /*
75133841545SHajimu UMEMOTO  * IPv6 reassembling timer processing;
75223d374aaSBjoern A. Zeeb  * if a timer expires on a reassembly queue, discard it.
75382cd038dSYoshinobu Inoue  */
75482cd038dSYoshinobu Inoue void
7551272577eSXin LI frag6_slowtimo(void)
75682cd038dSYoshinobu Inoue {
7578b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
75880d7a853SJonathan T. Looney 	struct ip6q *head, *q6;
7599cb1a47aSBjoern A. Zeeb 	uint32_t bucket;
76082cd038dSYoshinobu Inoue 
7615ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
7628b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
7638b615593SMarko Zec 		CURVNET_SET(vnet_iter);
7649cb1a47aSBjoern A. Zeeb 		for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
7659cb1a47aSBjoern A. Zeeb 			IP6QB_LOCK(bucket);
7669cb1a47aSBjoern A. Zeeb 			head = IP6QB_HEAD(bucket);
76780d7a853SJonathan T. Looney 			q6 = head->ip6q_next;
7681e9f3b73SJonathan T. Looney 			if (q6 == NULL) {
7691e9f3b73SJonathan T. Looney 				/*
7701e9f3b73SJonathan T. Looney 				 * XXXJTL: This should never happen. This
7711e9f3b73SJonathan T. Looney 				 * should turn into an assertion.
7721e9f3b73SJonathan T. Looney 				 */
7739cb1a47aSBjoern A. Zeeb 				IP6QB_UNLOCK(bucket);
7741e9f3b73SJonathan T. Looney 				continue;
7751e9f3b73SJonathan T. Looney 			}
77680d7a853SJonathan T. Looney 			while (q6 != head) {
77782cd038dSYoshinobu Inoue 				--q6->ip6q_ttl;
77882cd038dSYoshinobu Inoue 				q6 = q6->ip6q_next;
77982cd038dSYoshinobu Inoue 				if (q6->ip6q_prev->ip6q_ttl == 0) {
780198fdaedSTom Jones 					IP6STAT_ADD(ip6s_fragtimeout,
781198fdaedSTom Jones 						q6->ip6q_prev->ip6q_nfrag);
78282cd038dSYoshinobu Inoue 					/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
7839cb1a47aSBjoern A. Zeeb 					frag6_freef(q6->ip6q_prev, bucket);
78482cd038dSYoshinobu Inoue 				}
78582cd038dSYoshinobu Inoue 			}
78682cd038dSYoshinobu Inoue 			/*
78782cd038dSYoshinobu Inoue 			 * If we are over the maximum number of fragments
78882cd038dSYoshinobu Inoue 			 * (due to the limit being lowered), drain off
78982cd038dSYoshinobu Inoue 			 * enough to get down to the new limit.
7901e9f3b73SJonathan T. Looney 			 * Note that we drain all reassembly queues if
7911e9f3b73SJonathan T. Looney 			 * maxfragpackets is 0 (fragmentation is disabled),
79223d374aaSBjoern A. Zeeb 			 * and do not enforce a limit when maxfragpackets
7931e9f3b73SJonathan T. Looney 			 * is negative.
79482cd038dSYoshinobu Inoue 			 */
7951e9f3b73SJonathan T. Looney 			while ((V_ip6_maxfragpackets == 0 ||
7961e9f3b73SJonathan T. Looney 			    (V_ip6_maxfragpackets > 0 &&
7979cb1a47aSBjoern A. Zeeb 			    V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) &&
79880d7a853SJonathan T. Looney 			    head->ip6q_prev != head) {
799198fdaedSTom Jones 				IP6STAT_ADD(ip6s_fragoverflow,
800198fdaedSTom Jones 					q6->ip6q_prev->ip6q_nfrag);
80182cd038dSYoshinobu Inoue 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
8029cb1a47aSBjoern A. Zeeb 				frag6_freef(head->ip6q_prev, bucket);
80380d7a853SJonathan T. Looney 			}
8049cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
80582cd038dSYoshinobu Inoue 		}
8061e9f3b73SJonathan T. Looney 		/*
8071e9f3b73SJonathan T. Looney 		 * If we are still over the maximum number of fragmented
8081e9f3b73SJonathan T. Looney 		 * packets, drain off enough to get down to the new limit.
8091e9f3b73SJonathan T. Looney 		 */
8109cb1a47aSBjoern A. Zeeb 		bucket = 0;
8111e9f3b73SJonathan T. Looney 		while (V_ip6_maxfragpackets >= 0 &&
8121e9f3b73SJonathan T. Looney 		    atomic_load_int(&V_frag6_nfragpackets) >
8131e9f3b73SJonathan T. Looney 		    (u_int)V_ip6_maxfragpackets) {
8149cb1a47aSBjoern A. Zeeb 			IP6QB_LOCK(bucket);
8159cb1a47aSBjoern A. Zeeb 			head = IP6QB_HEAD(bucket);
8161e9f3b73SJonathan T. Looney 			if (head->ip6q_prev != head) {
817198fdaedSTom Jones 				IP6STAT_ADD(ip6s_fragoverflow,
818198fdaedSTom Jones 					q6->ip6q_prev->ip6q_nfrag);
8191e9f3b73SJonathan T. Looney 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
8209cb1a47aSBjoern A. Zeeb 				frag6_freef(head->ip6q_prev, bucket);
8211e9f3b73SJonathan T. Looney 			}
8229cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
8239cb1a47aSBjoern A. Zeeb 			bucket = (bucket + 1) % IP6REASS_NHASH;
8241e9f3b73SJonathan T. Looney 		}
8258b615593SMarko Zec 		CURVNET_RESTORE();
8268b615593SMarko Zec 	}
8275ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
82882cd038dSYoshinobu Inoue }
82982cd038dSYoshinobu Inoue 
83023d374aaSBjoern A. Zeeb /*
83123d374aaSBjoern A. Zeeb  * Eventhandler to adjust limits in case nmbclusters change.
83223d374aaSBjoern A. Zeeb  */
833c00464a2SBjoern A. Zeeb static void
834c00464a2SBjoern A. Zeeb frag6_change(void *tag)
835c00464a2SBjoern A. Zeeb {
836c00464a2SBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
837c00464a2SBjoern A. Zeeb 
838c00464a2SBjoern A. Zeeb 	ip6_maxfrags = IP6_MAXFRAGS;
839c00464a2SBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
840c00464a2SBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
841c00464a2SBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
842c00464a2SBjoern A. Zeeb 		V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
843c00464a2SBjoern A. Zeeb 		frag6_set_bucketsize();
844c00464a2SBjoern A. Zeeb 		CURVNET_RESTORE();
845c00464a2SBjoern A. Zeeb 	}
846c00464a2SBjoern A. Zeeb 	VNET_LIST_RUNLOCK_NOSLEEP();
847c00464a2SBjoern A. Zeeb }
848c00464a2SBjoern A. Zeeb 
849c00464a2SBjoern A. Zeeb /*
850c00464a2SBjoern A. Zeeb  * Initialise reassembly queue and fragment identifier.
851c00464a2SBjoern A. Zeeb  */
852c00464a2SBjoern A. Zeeb void
853c00464a2SBjoern A. Zeeb frag6_init(void)
854c00464a2SBjoern A. Zeeb {
855c00464a2SBjoern A. Zeeb 	struct ip6q *q6;
8569cb1a47aSBjoern A. Zeeb 	uint32_t bucket;
857c00464a2SBjoern A. Zeeb 
858c00464a2SBjoern A. Zeeb 	V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
859c00464a2SBjoern A. Zeeb 	frag6_set_bucketsize();
8609cb1a47aSBjoern A. Zeeb 	for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
8619cb1a47aSBjoern A. Zeeb 		q6 = IP6QB_HEAD(bucket);
862c00464a2SBjoern A. Zeeb 		q6->ip6q_next = q6->ip6q_prev = q6;
8639cb1a47aSBjoern A. Zeeb 		mtx_init(&V_ip6qb[bucket].lock, "ip6qlock", NULL, MTX_DEF);
8649cb1a47aSBjoern A. Zeeb 		V_ip6qb[bucket].count = 0;
865c00464a2SBjoern A. Zeeb 	}
8669cb1a47aSBjoern A. Zeeb 	V_ip6qb_hashseed = arc4random();
867c00464a2SBjoern A. Zeeb 	V_ip6_maxfragsperpacket = 64;
868c00464a2SBjoern A. Zeeb 	if (!IS_DEFAULT_VNET(curvnet))
869c00464a2SBjoern A. Zeeb 		return;
870c00464a2SBjoern A. Zeeb 
871c00464a2SBjoern A. Zeeb 	ip6_maxfrags = IP6_MAXFRAGS;
872c00464a2SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(nmbclusters_change,
873c00464a2SBjoern A. Zeeb 	    frag6_change, NULL, EVENTHANDLER_PRI_ANY);
874c00464a2SBjoern A. Zeeb }
875c00464a2SBjoern A. Zeeb 
87682cd038dSYoshinobu Inoue /*
87782cd038dSYoshinobu Inoue  * Drain off all datagram fragments.
87882cd038dSYoshinobu Inoue  */
87982cd038dSYoshinobu Inoue void
8801272577eSXin LI frag6_drain(void)
88182cd038dSYoshinobu Inoue {
8828b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
88380d7a853SJonathan T. Looney 	struct ip6q *head;
8849cb1a47aSBjoern A. Zeeb 	uint32_t bucket;
8859888c401SHajimu UMEMOTO 
8865ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
8878b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
8888b615593SMarko Zec 		CURVNET_SET(vnet_iter);
8899cb1a47aSBjoern A. Zeeb 		for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
8909cb1a47aSBjoern A. Zeeb 			if (IP6QB_TRYLOCK(bucket) == 0)
89180d7a853SJonathan T. Looney 				continue;
8929cb1a47aSBjoern A. Zeeb 			head = IP6QB_HEAD(bucket);
89380d7a853SJonathan T. Looney 			while (head->ip6q_next != head) {
8949cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_fragdropped);
89582cd038dSYoshinobu Inoue 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
8969cb1a47aSBjoern A. Zeeb 				frag6_freef(head->ip6q_next, bucket);
89780d7a853SJonathan T. Looney 			}
8989cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
89982cd038dSYoshinobu Inoue 		}
9008b615593SMarko Zec 		CURVNET_RESTORE();
9018b615593SMarko Zec 	}
9025ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
90382cd038dSYoshinobu Inoue }
904e5ee7060SGleb Smirnoff 
905c00464a2SBjoern A. Zeeb /*
906c00464a2SBjoern A. Zeeb  * Put an ip fragment on a reassembly chain.
907c00464a2SBjoern A. Zeeb  * Like insque, but pointers in middle of structure.
908c00464a2SBjoern A. Zeeb  */
909c00464a2SBjoern A. Zeeb static void
910c00464a2SBjoern A. Zeeb frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
911c00464a2SBjoern A. Zeeb     uint32_t bucket __unused)
912e5ee7060SGleb Smirnoff {
913e5ee7060SGleb Smirnoff 
9149cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK_ASSERT(bucket);
915c00464a2SBjoern A. Zeeb 
916c00464a2SBjoern A. Zeeb 	af6->ip6af_up = up6;
917c00464a2SBjoern A. Zeeb 	af6->ip6af_down = up6->ip6af_down;
918c00464a2SBjoern A. Zeeb 	up6->ip6af_down->ip6af_up = af6;
919c00464a2SBjoern A. Zeeb 	up6->ip6af_down = af6;
920e5ee7060SGleb Smirnoff }
921e5ee7060SGleb Smirnoff 
922c00464a2SBjoern A. Zeeb /*
923c00464a2SBjoern A. Zeeb  * To frag6_enq as remque is to insque.
924c00464a2SBjoern A. Zeeb  */
925c00464a2SBjoern A. Zeeb static void
926c00464a2SBjoern A. Zeeb frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
927c00464a2SBjoern A. Zeeb {
928c00464a2SBjoern A. Zeeb 
9299cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK_ASSERT(bucket);
930c00464a2SBjoern A. Zeeb 
931c00464a2SBjoern A. Zeeb 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
932c00464a2SBjoern A. Zeeb 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
933c00464a2SBjoern A. Zeeb }
934c00464a2SBjoern A. Zeeb 
935c00464a2SBjoern A. Zeeb static void
936c00464a2SBjoern A. Zeeb frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
937c00464a2SBjoern A. Zeeb {
938c00464a2SBjoern A. Zeeb 
9399cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK_ASSERT(bucket);
9409cb1a47aSBjoern A. Zeeb 	KASSERT(IP6QB_HEAD(bucket) == old,
941c00464a2SBjoern A. Zeeb 	    ("%s: attempt to insert at head of wrong bucket"
942c00464a2SBjoern A. Zeeb 	    " (bucket=%u, old=%p)", __func__, bucket, old));
943c00464a2SBjoern A. Zeeb 
944c00464a2SBjoern A. Zeeb 	new->ip6q_prev = old;
945c00464a2SBjoern A. Zeeb 	new->ip6q_next = old->ip6q_next;
946c00464a2SBjoern A. Zeeb 	old->ip6q_next->ip6q_prev= new;
947c00464a2SBjoern A. Zeeb 	old->ip6q_next = new;
9489cb1a47aSBjoern A. Zeeb 	V_ip6qb[bucket].count++;
949c00464a2SBjoern A. Zeeb }
950c00464a2SBjoern A. Zeeb 
951c00464a2SBjoern A. Zeeb static void
952c00464a2SBjoern A. Zeeb frag6_remque(struct ip6q *p6, uint32_t bucket)
953c00464a2SBjoern A. Zeeb {
954c00464a2SBjoern A. Zeeb 
9559cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK_ASSERT(bucket);
956c00464a2SBjoern A. Zeeb 
957c00464a2SBjoern A. Zeeb 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
958c00464a2SBjoern A. Zeeb 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
9599cb1a47aSBjoern A. Zeeb 	V_ip6qb[bucket].count--;
960e5ee7060SGleb Smirnoff }
961