xref: /freebsd/sys/netinet6/frag6.c (revision c1131de6)
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.
621f08a07SBjoern A. Zeeb  * Copyright (c) 2019 Netflix, Inc.
782cd038dSYoshinobu Inoue  *
882cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
982cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
1082cd038dSYoshinobu Inoue  * are met:
1182cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1282cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1382cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1482cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1582cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1682cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1782cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1882cd038dSYoshinobu Inoue  *    without specific prior written permission.
1982cd038dSYoshinobu Inoue  *
2082cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2182cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2282cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2382cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2482cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2582cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2682cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2782cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2882cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2982cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3082cd038dSYoshinobu Inoue  * SUCH DAMAGE.
31b48287a3SDavid E. O'Brien  *
32b48287a3SDavid E. O'Brien  *	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
3382cd038dSYoshinobu Inoue  */
3482cd038dSYoshinobu Inoue 
35b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
36b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
37b48287a3SDavid E. O'Brien 
38aaa46574SAdrian Chadd #include "opt_rss.h"
39aaa46574SAdrian Chadd 
4082cd038dSYoshinobu Inoue #include <sys/param.h>
41f349c821SBjoern A. Zeeb #include <sys/systm.h>
421a3044faSBjoern A. Zeeb #include <sys/domain.h>
431a3044faSBjoern A. Zeeb #include <sys/eventhandler.h>
4480d7a853SJonathan T. Looney #include <sys/hash.h>
451a3044faSBjoern A. Zeeb #include <sys/kernel.h>
4682cd038dSYoshinobu Inoue #include <sys/malloc.h>
4782cd038dSYoshinobu Inoue #include <sys/mbuf.h>
4882cd038dSYoshinobu Inoue #include <sys/protosw.h>
4921f08a07SBjoern A. Zeeb #include <sys/queue.h>
5082cd038dSYoshinobu Inoue #include <sys/socket.h>
51757cb678SBjoern A. Zeeb #include <sys/sysctl.h>
5282cd038dSYoshinobu Inoue #include <sys/syslog.h>
5382cd038dSYoshinobu Inoue 
5482cd038dSYoshinobu Inoue #include <net/if.h>
5576039bc8SGleb Smirnoff #include <net/if_var.h>
56aaa46574SAdrian Chadd #include <net/netisr.h>
5782cd038dSYoshinobu Inoue #include <net/route.h>
58eddfbb76SRobert Watson #include <net/vnet.h>
5982cd038dSYoshinobu Inoue 
6082cd038dSYoshinobu Inoue #include <netinet/in.h>
6182cd038dSYoshinobu Inoue #include <netinet/in_var.h>
62686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
6382cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
64686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
6523d374aaSBjoern A. Zeeb #include <netinet/in_systm.h>	/* For ECN definitions. */
6623d374aaSBjoern A. Zeeb #include <netinet/ip.h>		/* For ECN definitions. */
6782cd038dSYoshinobu Inoue 
681a3044faSBjoern A. Zeeb #ifdef MAC
694b908c8bSRobert Watson #include <security/mac/mac_framework.h>
701a3044faSBjoern A. Zeeb #endif
714b908c8bSRobert Watson 
72f1664f32SBjoern A. Zeeb /*
73f1664f32SBjoern A. Zeeb  * A "big picture" of how IPv6 fragment queues are all linked together.
74f1664f32SBjoern A. Zeeb  *
75f1664f32SBjoern A. Zeeb  * struct ip6qbucket ip6qb[...];			hashed buckets
76f1664f32SBjoern A. Zeeb  * ||||||||
77f1664f32SBjoern A. Zeeb  * |
78f1664f32SBjoern A. Zeeb  * +--- TAILQ(struct ip6q, packets) *q6;		tailq entries holding
79f1664f32SBjoern A. Zeeb  *      ||||||||					fragmented packets
80f1664f32SBjoern A. Zeeb  *      |						(1 per original packet)
81f1664f32SBjoern A. Zeeb  *      |
82f1664f32SBjoern A. Zeeb  *      +--- TAILQ(struct ip6asfrag, ip6q_frags) *af6;	tailq entries of IPv6
83f1664f32SBjoern A. Zeeb  *           |                                   *ip6af;fragment packets
84f1664f32SBjoern A. Zeeb  *           |						for one original packet
85f1664f32SBjoern A. Zeeb  *           + *mbuf
86f1664f32SBjoern A. Zeeb  */
87f1664f32SBjoern A. Zeeb 
8823d374aaSBjoern A. Zeeb /* Reassembly headers are stored in hash buckets. */
892ceeacbeSJonathan T. Looney #define	IP6REASS_NHASH_LOG2	10
9080d7a853SJonathan T. Looney #define	IP6REASS_NHASH		(1 << IP6REASS_NHASH_LOG2)
9180d7a853SJonathan T. Looney #define	IP6REASS_HMASK		(IP6REASS_NHASH - 1)
9280d7a853SJonathan T. Looney 
9321f08a07SBjoern A. Zeeb TAILQ_HEAD(ip6qhead, ip6q);
9480d7a853SJonathan T. Looney struct ip6qbucket {
9521f08a07SBjoern A. Zeeb 	struct ip6qhead	packets;
9680d7a853SJonathan T. Looney 	struct mtx	lock;
971e9f3b73SJonathan T. Looney 	int		count;
9880d7a853SJonathan T. Looney };
9980d7a853SJonathan T. Looney 
1001540a98eSBjoern A. Zeeb struct ip6asfrag {
10121f08a07SBjoern A. Zeeb 	TAILQ_ENTRY(ip6asfrag) ip6af_tq;
1021540a98eSBjoern A. Zeeb 	struct mbuf	*ip6af_m;
103f1664f32SBjoern A. Zeeb 	int		ip6af_offset;	/* Offset in ip6af_m to next header. */
104f1664f32SBjoern A. Zeeb 	int		ip6af_frglen;	/* Fragmentable part length. */
105f1664f32SBjoern A. Zeeb 	int		ip6af_off;	/* Fragment offset. */
106f1664f32SBjoern A. Zeeb 	bool		ip6af_mff;	/* More fragment bit in frag off. */
1071540a98eSBjoern A. Zeeb };
1081540a98eSBjoern A. Zeeb 
109487a161cSBjoern A. Zeeb static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");
110487a161cSBjoern A. Zeeb 
11167a10c46SBjoern A. Zeeb #ifdef VIMAGE
11267a10c46SBjoern A. Zeeb /* A flag to indicate if IPv6 fragmentation is initialized. */
11367a10c46SBjoern A. Zeeb VNET_DEFINE_STATIC(bool,		frag6_on);
11467a10c46SBjoern A. Zeeb #define	V_frag6_on			VNET(frag6_on)
11567a10c46SBjoern A. Zeeb #endif
11667a10c46SBjoern A. Zeeb 
117757cb678SBjoern A. Zeeb /* System wide (global) maximum and count of packets in reassembly queues. */
118757cb678SBjoern A. Zeeb static int ip6_maxfrags;
119757cb678SBjoern A. Zeeb static volatile u_int frag6_nfrags = 0;
120757cb678SBjoern A. Zeeb 
121757cb678SBjoern A. Zeeb /* Maximum and current packets in per-VNET reassembly queue. */
122757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragpackets);
12380d7a853SJonathan T. Looney VNET_DEFINE_STATIC(volatile u_int,	frag6_nfragpackets);
124757cb678SBjoern A. Zeeb #define	V_ip6_maxfragpackets		VNET(ip6_maxfragpackets)
125757cb678SBjoern A. Zeeb #define	V_frag6_nfragpackets		VNET(frag6_nfragpackets)
126757cb678SBjoern A. Zeeb 
127757cb678SBjoern A. Zeeb /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
128757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragbucketsize);
129757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragsperpacket);
130757cb678SBjoern A. Zeeb #define	V_ip6_maxfragbucketsize		VNET(ip6_maxfragbucketsize)
131757cb678SBjoern A. Zeeb #define	V_ip6_maxfragsperpacket		VNET(ip6_maxfragsperpacket)
132757cb678SBjoern A. Zeeb 
133757cb678SBjoern A. Zeeb /* Per-VNET reassembly queue buckets. */
1349cb1a47aSBjoern A. Zeeb VNET_DEFINE_STATIC(struct ip6qbucket,	ip6qb[IP6REASS_NHASH]);
1359cb1a47aSBjoern A. Zeeb VNET_DEFINE_STATIC(uint32_t,		ip6qb_hashseed);
1369cb1a47aSBjoern A. Zeeb #define	V_ip6qb				VNET(ip6qb)
1379cb1a47aSBjoern A. Zeeb #define	V_ip6qb_hashseed		VNET(ip6qb_hashseed)
13882cd038dSYoshinobu Inoue 
1399cb1a47aSBjoern A. Zeeb #define	IP6QB_LOCK(_b)		mtx_lock(&V_ip6qb[(_b)].lock)
1409cb1a47aSBjoern A. Zeeb #define	IP6QB_TRYLOCK(_b)	mtx_trylock(&V_ip6qb[(_b)].lock)
1419cb1a47aSBjoern A. Zeeb #define	IP6QB_LOCK_ASSERT(_b)	mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED)
1429cb1a47aSBjoern A. Zeeb #define	IP6QB_UNLOCK(_b)	mtx_unlock(&V_ip6qb[(_b)].lock)
14321f08a07SBjoern A. Zeeb #define	IP6QB_HEAD(_b)		(&V_ip6qb[(_b)].packets)
1449888c401SHajimu UMEMOTO 
14582cd038dSYoshinobu Inoue /*
1462ceeacbeSJonathan T. Looney  * By default, limit the number of IP6 fragments across all reassembly
1472ceeacbeSJonathan T. Looney  * queues to  1/32 of the total number of mbuf clusters.
1482ceeacbeSJonathan T. Looney  *
1492ceeacbeSJonathan T. Looney  * Limit the total number of reassembly queues per VNET to the
1502ceeacbeSJonathan T. Looney  * IP6 fragment limit, but ensure the limit will not allow any bucket
1512ceeacbeSJonathan T. Looney  * to grow above 100 items. (The bucket limit is
1522ceeacbeSJonathan T. Looney  * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
1532ceeacbeSJonathan T. Looney  * multiplier to reach a 100-item limit.)
1542ceeacbeSJonathan T. Looney  * The 100-item limit was chosen as brief testing seems to show that
1552ceeacbeSJonathan T. Looney  * this produces "reasonable" performance on some subset of systems
1562ceeacbeSJonathan T. Looney  * under DoS attack.
1572ceeacbeSJonathan T. Looney  */
1582ceeacbeSJonathan T. Looney #define	IP6_MAXFRAGS		(nmbclusters / 32)
1592ceeacbeSJonathan T. Looney #define	IP6_MAXFRAGPACKETS	(imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
1602ceeacbeSJonathan T. Looney 
161757cb678SBjoern A. Zeeb 
1622ceeacbeSJonathan T. Looney /*
163757cb678SBjoern A. Zeeb  * Sysctls and helper function.
16482cd038dSYoshinobu Inoue  */
165757cb678SBjoern A. Zeeb SYSCTL_DECL(_net_inet6_ip6);
166757cb678SBjoern A. Zeeb 
16765456706SBjoern A. Zeeb SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfrags,
16865456706SBjoern A. Zeeb 	CTLFLAG_RD, __DEVOLATILE(u_int *, &frag6_nfrags), 0,
16965456706SBjoern A. Zeeb 	"Global number of IPv6 fragments across all reassembly queues.");
17065456706SBjoern A. Zeeb 
171757cb678SBjoern A. Zeeb static void
17209b361c7SBjoern A. Zeeb frag6_set_bucketsize(void)
1731e9f3b73SJonathan T. Looney {
1741e9f3b73SJonathan T. Looney 	int i;
1751e9f3b73SJonathan T. Looney 
1761e9f3b73SJonathan T. Looney 	if ((i = V_ip6_maxfragpackets) > 0)
1771e9f3b73SJonathan T. Looney 		V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
1781e9f3b73SJonathan T. Looney }
1791e9f3b73SJonathan T. Looney 
180757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
181757cb678SBjoern A. Zeeb 	CTLFLAG_RW, &ip6_maxfrags, 0,
182757cb678SBjoern A. Zeeb 	"Maximum allowed number of outstanding IPv6 packet fragments. "
183757cb678SBjoern A. Zeeb 	"A value of 0 means no fragmented packets will be accepted, while a "
184757cb678SBjoern A. Zeeb 	"a value of -1 means no limit");
185757cb678SBjoern A. Zeeb 
186757cb678SBjoern A. Zeeb static int
187757cb678SBjoern A. Zeeb sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
188757cb678SBjoern A. Zeeb {
189757cb678SBjoern A. Zeeb 	int error, val;
190757cb678SBjoern A. Zeeb 
191757cb678SBjoern A. Zeeb 	val = V_ip6_maxfragpackets;
192757cb678SBjoern A. Zeeb 	error = sysctl_handle_int(oidp, &val, 0, req);
193757cb678SBjoern A. Zeeb 	if (error != 0 || !req->newptr)
194757cb678SBjoern A. Zeeb 		return (error);
195757cb678SBjoern A. Zeeb 	V_ip6_maxfragpackets = val;
196757cb678SBjoern A. Zeeb 	frag6_set_bucketsize();
197757cb678SBjoern A. Zeeb 	return (0);
198757cb678SBjoern A. Zeeb }
199757cb678SBjoern A. Zeeb SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
200757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
201757cb678SBjoern A. Zeeb 	sysctl_ip6_maxfragpackets, "I",
202757cb678SBjoern A. Zeeb 	"Default maximum number of outstanding fragmented IPv6 packets. "
203757cb678SBjoern A. Zeeb 	"A value of 0 means no fragmented packets will be accepted, while a "
204757cb678SBjoern A. Zeeb 	"a value of -1 means no limit");
20553707abdSBjoern A. Zeeb SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfragpackets,
20653707abdSBjoern A. Zeeb 	CTLFLAG_VNET | CTLFLAG_RD,
20753707abdSBjoern A. Zeeb 	__DEVOLATILE(u_int *, &VNET_NAME(frag6_nfragpackets)), 0,
20853707abdSBjoern A. Zeeb 	"Per-VNET number of IPv6 fragments across all reassembly queues.");
209757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
210757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
211757cb678SBjoern A. Zeeb 	"Maximum allowed number of fragments per packet");
212757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
213757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
214757cb678SBjoern A. Zeeb 	"Maximum number of reassembly queues per hash bucket");
215757cb678SBjoern A. Zeeb 
216757cb678SBjoern A. Zeeb 
217757cb678SBjoern A. Zeeb /*
218c00464a2SBjoern A. Zeeb  * Remove the IPv6 fragmentation header from the mbuf.
219c00464a2SBjoern A. Zeeb  */
220c00464a2SBjoern A. Zeeb int
221c00464a2SBjoern A. Zeeb ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
222c00464a2SBjoern A. Zeeb {
2235778b399SBjoern A. Zeeb 	struct ip6_hdr *ip6;
224c00464a2SBjoern A. Zeeb 	struct mbuf *t;
225c00464a2SBjoern A. Zeeb 
226c00464a2SBjoern A. Zeeb 	/* Delete frag6 header. */
227c00464a2SBjoern A. Zeeb 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
2285778b399SBjoern A. Zeeb 
229c00464a2SBjoern A. Zeeb 		/* This is the only possible case with !PULLDOWN_TEST. */
2305778b399SBjoern A. Zeeb 		ip6 = mtod(m, struct ip6_hdr *);
231c00464a2SBjoern A. Zeeb 		bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
232c00464a2SBjoern A. Zeeb 		    offset);
233c00464a2SBjoern A. Zeeb 		m->m_data += sizeof(struct ip6_frag);
234c00464a2SBjoern A. Zeeb 		m->m_len -= sizeof(struct ip6_frag);
235c00464a2SBjoern A. Zeeb 	} else {
2365778b399SBjoern A. Zeeb 
237c00464a2SBjoern A. Zeeb 		/* This comes with no copy if the boundary is on cluster. */
238c00464a2SBjoern A. Zeeb 		if ((t = m_split(m, offset, wait)) == NULL)
239c00464a2SBjoern A. Zeeb 			return (ENOMEM);
240c00464a2SBjoern A. Zeeb 		m_adj(t, sizeof(struct ip6_frag));
241c00464a2SBjoern A. Zeeb 		m_cat(m, t);
242c00464a2SBjoern A. Zeeb 	}
243c00464a2SBjoern A. Zeeb 
244c00464a2SBjoern A. Zeeb 	m->m_flags |= M_FRAGMENTED;
245c00464a2SBjoern A. Zeeb 	return (0);
246c00464a2SBjoern A. Zeeb }
247c00464a2SBjoern A. Zeeb 
248c00464a2SBjoern A. Zeeb /*
24923d374aaSBjoern A. Zeeb  * Free a fragment reassembly header and all associated datagrams.
250757cb678SBjoern A. Zeeb  */
2514f590175SPaul Saab static void
252c00464a2SBjoern A. Zeeb frag6_freef(struct ip6q *q6, uint32_t bucket)
2534f590175SPaul Saab {
2545778b399SBjoern A. Zeeb 	struct ip6_hdr *ip6;
25521f08a07SBjoern A. Zeeb 	struct ip6asfrag *af6;
2565778b399SBjoern A. Zeeb 	struct mbuf *m;
2574f590175SPaul Saab 
2589cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK_ASSERT(bucket);
259c00464a2SBjoern A. Zeeb 
26021f08a07SBjoern A. Zeeb 	while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
261c00464a2SBjoern A. Zeeb 
262da89a0feSBjoern A. Zeeb 		m = af6->ip6af_m;
26321f08a07SBjoern A. Zeeb 		TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
264c00464a2SBjoern A. Zeeb 
265c00464a2SBjoern A. Zeeb 		/*
266c00464a2SBjoern A. Zeeb 		 * Return ICMP time exceeded error for the 1st fragment.
267c00464a2SBjoern A. Zeeb 		 * Just free other fragments.
268c00464a2SBjoern A. Zeeb 		 */
269a55383e7SHans Petter Selasky 		if (af6->ip6af_off == 0 && m->m_pkthdr.rcvif != NULL) {
270c00464a2SBjoern A. Zeeb 
27123d374aaSBjoern A. Zeeb 			/* Adjust pointer. */
272c00464a2SBjoern A. Zeeb 			ip6 = mtod(m, struct ip6_hdr *);
273c00464a2SBjoern A. Zeeb 
27423d374aaSBjoern A. Zeeb 			/* Restore source and destination addresses. */
275c00464a2SBjoern A. Zeeb 			ip6->ip6_src = q6->ip6q_src;
276c00464a2SBjoern A. Zeeb 			ip6->ip6_dst = q6->ip6q_dst;
277c00464a2SBjoern A. Zeeb 
278c00464a2SBjoern A. Zeeb 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
279c00464a2SBjoern A. Zeeb 			    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
280c00464a2SBjoern A. Zeeb 		} else
281c00464a2SBjoern A. Zeeb 			m_freem(m);
28223d374aaSBjoern A. Zeeb 
283c00464a2SBjoern A. Zeeb 		free(af6, M_FRAG6);
2842adfd64fSJonathan T. Looney 	}
28521f08a07SBjoern A. Zeeb 
28621f08a07SBjoern A. Zeeb 	TAILQ_REMOVE(IP6QB_HEAD(bucket), q6, ip6q_tq);
28721f08a07SBjoern A. Zeeb 	V_ip6qb[bucket].count--;
288c00464a2SBjoern A. Zeeb 	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
289c00464a2SBjoern A. Zeeb #ifdef MAC
290c00464a2SBjoern A. Zeeb 	mac_ip6q_destroy(q6);
291c00464a2SBjoern A. Zeeb #endif
292c00464a2SBjoern A. Zeeb 	free(q6, M_FRAG6);
293c00464a2SBjoern A. Zeeb 	atomic_subtract_int(&V_frag6_nfragpackets, 1);
29482cd038dSYoshinobu Inoue }
29582cd038dSYoshinobu Inoue 
29682cd038dSYoshinobu Inoue /*
297a55383e7SHans Petter Selasky  * Drain off all datagram fragments belonging to
298a55383e7SHans Petter Selasky  * the given network interface.
299a55383e7SHans Petter Selasky  */
300a55383e7SHans Petter Selasky static void
301a55383e7SHans Petter Selasky frag6_cleanup(void *arg __unused, struct ifnet *ifp)
302a55383e7SHans Petter Selasky {
30321f08a07SBjoern A. Zeeb 	struct ip6qhead *head;
30421f08a07SBjoern A. Zeeb 	struct ip6q *q6;
305a55383e7SHans Petter Selasky 	struct ip6asfrag *af6;
30621f08a07SBjoern A. Zeeb 	uint32_t bucket;
307a55383e7SHans Petter Selasky 
308a55383e7SHans Petter Selasky 	KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
309a55383e7SHans Petter Selasky 
3106e6b5143SBjoern A. Zeeb 	CURVNET_SET_QUIET(ifp->if_vnet);
31167a10c46SBjoern A. Zeeb #ifdef VIMAGE
31267a10c46SBjoern A. Zeeb 	/*
31367a10c46SBjoern A. Zeeb 	 * Skip processing if IPv6 reassembly is not initialised or
31467a10c46SBjoern A. Zeeb 	 * torn down by frag6_destroy().
31567a10c46SBjoern A. Zeeb 	 */
3166e6b5143SBjoern A. Zeeb 	if (!V_frag6_on) {
3176e6b5143SBjoern A. Zeeb 		CURVNET_RESTORE();
31867a10c46SBjoern A. Zeeb 		return;
3196e6b5143SBjoern A. Zeeb 	}
32067a10c46SBjoern A. Zeeb #endif
32167a10c46SBjoern A. Zeeb 
32221f08a07SBjoern A. Zeeb 	for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
32321f08a07SBjoern A. Zeeb 		IP6QB_LOCK(bucket);
32421f08a07SBjoern A. Zeeb 		head = IP6QB_HEAD(bucket);
325a55383e7SHans Petter Selasky 		/* Scan fragment list. */
32621f08a07SBjoern A. Zeeb 		TAILQ_FOREACH(q6, head, ip6q_tq) {
32721f08a07SBjoern A. Zeeb 			TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
328a55383e7SHans Petter Selasky 
329f1664f32SBjoern A. Zeeb 				/* Clear no longer valid rcvif pointer. */
330da89a0feSBjoern A. Zeeb 				if (af6->ip6af_m->m_pkthdr.rcvif == ifp)
331da89a0feSBjoern A. Zeeb 					af6->ip6af_m->m_pkthdr.rcvif = NULL;
332a55383e7SHans Petter Selasky 			}
333a55383e7SHans Petter Selasky 		}
33421f08a07SBjoern A. Zeeb 		IP6QB_UNLOCK(bucket);
335a55383e7SHans Petter Selasky 	}
336a55383e7SHans Petter Selasky 	CURVNET_RESTORE();
337a55383e7SHans Petter Selasky }
338a55383e7SHans Petter Selasky EVENTHANDLER_DEFINE(ifnet_departure_event, frag6_cleanup, NULL, 0);
339a55383e7SHans Petter Selasky 
340a55383e7SHans Petter Selasky /*
34123d374aaSBjoern A. Zeeb  * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with
34223d374aaSBjoern A. Zeeb  * each other, in terms of next header field handling in fragment header.
343686cdd19SJun-ichiro itojun Hagino  * While the sender will use the same value for all of the fragmented packets,
34423d374aaSBjoern A. Zeeb  * receiver is suggested not to check for consistency.
345686cdd19SJun-ichiro itojun Hagino  *
34623d374aaSBjoern A. Zeeb  * Fragment rules (p18,p19):
347686cdd19SJun-ichiro itojun Hagino  *	(2)  A Fragment header containing:
34823d374aaSBjoern A. Zeeb  *	The Next Header value that identifies the first header
34923d374aaSBjoern A. Zeeb  *	after the Per-Fragment headers of the original packet.
350686cdd19SJun-ichiro itojun Hagino  *		-> next header field is same for all fragments
351686cdd19SJun-ichiro itojun Hagino  *
35223d374aaSBjoern A. Zeeb  * Reassembly rule (p20):
35323d374aaSBjoern A. Zeeb  *	The Next Header field of the last header of the Per-Fragment
35423d374aaSBjoern A. Zeeb  *	headers is obtained from the Next Header field of the first
355686cdd19SJun-ichiro itojun Hagino  *	fragment's Fragment header.
356686cdd19SJun-ichiro itojun Hagino  *		-> should grab it from the first fragment only
357686cdd19SJun-ichiro itojun Hagino  *
358686cdd19SJun-ichiro itojun Hagino  * The following note also contradicts with fragment rule - no one is going to
359686cdd19SJun-ichiro itojun Hagino  * send different fragment with different next header field.
360686cdd19SJun-ichiro itojun Hagino  *
36123d374aaSBjoern A. Zeeb  * Additional note (p22) [not an error]:
362686cdd19SJun-ichiro itojun Hagino  *	The Next Header values in the Fragment headers of different
363686cdd19SJun-ichiro itojun Hagino  *	fragments of the same original packet may differ.  Only the value
364686cdd19SJun-ichiro itojun Hagino  *	from the Offset zero fragment packet is used for reassembly.
365686cdd19SJun-ichiro itojun Hagino  *		-> should grab it from the first fragment only
366686cdd19SJun-ichiro itojun Hagino  *
367686cdd19SJun-ichiro itojun Hagino  * There is no explicit reason given in the RFC.  Historical reason maybe?
368686cdd19SJun-ichiro itojun Hagino  */
369686cdd19SJun-ichiro itojun Hagino /*
37023d374aaSBjoern A. Zeeb  * Fragment input.
37182cd038dSYoshinobu Inoue  */
37282cd038dSYoshinobu Inoue int
3731272577eSXin LI frag6_input(struct mbuf **mp, int *offp, int proto)
37482cd038dSYoshinobu Inoue {
37521f08a07SBjoern A. Zeeb 	struct mbuf *m, *t;
37682cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6;
37782cd038dSYoshinobu Inoue 	struct ip6_frag *ip6f;
37821f08a07SBjoern A. Zeeb 	struct ip6qhead *head;
37921f08a07SBjoern A. Zeeb 	struct ip6q *q6;
38021f08a07SBjoern A. Zeeb 	struct ip6asfrag *af6, *ip6af, *af6tmp;
38121f08a07SBjoern A. Zeeb 	struct in6_ifaddr *ia6;
38221f08a07SBjoern A. Zeeb 	struct ifnet *dstifp, *srcifp;
383505e91f5SKristof Provost 	uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
384505e91f5SKristof Provost 		    sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
3859cb1a47aSBjoern A. Zeeb 	uint32_t bucket, *hashkeyp;
3865778b399SBjoern A. Zeeb 	int fragoff, frgpartlen;	/* Must be larger than uint16_t. */
3875778b399SBjoern A. Zeeb 	int nxt, offset, plen;
3885778b399SBjoern A. Zeeb 	uint8_t ecn, ecn0;
3895778b399SBjoern A. Zeeb 	bool only_frag;
390aaa46574SAdrian Chadd #ifdef RSS
391aaa46574SAdrian Chadd 	struct ip6_direct_ctx *ip6dc;
3925778b399SBjoern A. Zeeb 	struct m_tag *mtag;
393aaa46574SAdrian Chadd #endif
394aaa46574SAdrian Chadd 
3955778b399SBjoern A. Zeeb 	m = *mp;
3965778b399SBjoern A. Zeeb 	offset = *offp;
3975778b399SBjoern A. Zeeb 
398c1131de6SBjoern A. Zeeb 	M_ASSERTPKTHDR(m);
399c1131de6SBjoern A. Zeeb 
40082cd038dSYoshinobu Inoue 	ip6 = mtod(m, struct ip6_hdr *);
401686cdd19SJun-ichiro itojun Hagino #ifndef PULLDOWN_TEST
402686cdd19SJun-ichiro itojun Hagino 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
40382cd038dSYoshinobu Inoue 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
404686cdd19SJun-ichiro itojun Hagino #else
405686cdd19SJun-ichiro itojun Hagino 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
406686cdd19SJun-ichiro itojun Hagino 	if (ip6f == NULL)
40740e39bbbSHajimu UMEMOTO 		return (IPPROTO_DONE);
408686cdd19SJun-ichiro itojun Hagino #endif
40982cd038dSYoshinobu Inoue 
41082cd038dSYoshinobu Inoue 	dstifp = NULL;
41123d374aaSBjoern A. Zeeb 	/* Find the destination interface of the packet. */
4125778b399SBjoern A. Zeeb 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
4135778b399SBjoern A. Zeeb 	if (ia6 != NULL) {
4145778b399SBjoern A. Zeeb 		dstifp = ia6->ia_ifp;
4155778b399SBjoern A. Zeeb 		ifa_free(&ia6->ia_ifa);
4168c0fec80SRobert Watson 	}
41723d374aaSBjoern A. Zeeb 
41823d374aaSBjoern A. Zeeb 	/* Jumbo payload cannot contain a fragment header. */
41982cd038dSYoshinobu Inoue 	if (ip6->ip6_plen == 0) {
42082cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
42182cd038dSYoshinobu Inoue 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
4225778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
42382cd038dSYoshinobu Inoue 	}
42482cd038dSYoshinobu Inoue 
42582cd038dSYoshinobu Inoue 	/*
42623d374aaSBjoern A. Zeeb 	 * Check whether fragment packet's fragment length is a
42723d374aaSBjoern A. Zeeb 	 * multiple of 8 octets (unless it is the last one).
42882cd038dSYoshinobu Inoue 	 * sizeof(struct ip6_frag) == 8
42982cd038dSYoshinobu Inoue 	 * sizeof(struct ip6_hdr) = 40
43082cd038dSYoshinobu Inoue 	 */
43182cd038dSYoshinobu Inoue 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
43282cd038dSYoshinobu Inoue 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
43306cd0a3fSHajimu UMEMOTO 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
434686cdd19SJun-ichiro itojun Hagino 		    offsetof(struct ip6_hdr, ip6_plen));
43582cd038dSYoshinobu Inoue 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
4365778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
43782cd038dSYoshinobu Inoue 	}
43882cd038dSYoshinobu Inoue 
4399cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_fragments);
44082cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
44182cd038dSYoshinobu Inoue 
4424018ea9aSBjoern A. Zeeb 	/*
4432946a941STom Jones 	 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
444c1131de6SBjoern A. Zeeb 	 * unrelated to any reassembly.  We need to remove the frag hdr
445c1131de6SBjoern A. Zeeb 	 * which is ugly.
44623d374aaSBjoern A. Zeeb 	 * See RFC 6946 and section 4.5 of RFC 8200.
4474018ea9aSBjoern A. Zeeb 	 */
4484018ea9aSBjoern A. Zeeb 	if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
4492946a941STom Jones 		IP6STAT_INC(ip6s_atomicfrags);
450c1131de6SBjoern A. Zeeb 		nxt = ip6f->ip6f_nxt;
451c1131de6SBjoern A. Zeeb 		/*
452c1131de6SBjoern A. Zeeb 		 * Set nxt(-hdr field value) to the original value.
453c1131de6SBjoern A. Zeeb 		 * We cannot just set ip6->ip6_nxt as there might be
454c1131de6SBjoern A. Zeeb 		 * an unfragmentable part with extension headers and
455c1131de6SBjoern A. Zeeb 		 * we must update the last one.
456c1131de6SBjoern A. Zeeb 		 */
457c1131de6SBjoern A. Zeeb 		m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
458c1131de6SBjoern A. Zeeb 		    (caddr_t)&nxt);
459c1131de6SBjoern A. Zeeb 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) -
460c1131de6SBjoern A. Zeeb 		    sizeof(struct ip6_frag));
461c1131de6SBjoern A. Zeeb 		if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0)
462c1131de6SBjoern A. Zeeb 			goto dropfrag2;
463c1131de6SBjoern A. Zeeb 		m->m_pkthdr.len -= sizeof(struct ip6_frag);
4644018ea9aSBjoern A. Zeeb 		in6_ifstat_inc(dstifp, ifs6_reass_ok);
465c1131de6SBjoern A. Zeeb 		*mp = m;
466c1131de6SBjoern A. Zeeb 		return (nxt);
4674018ea9aSBjoern A. Zeeb 	}
4684018ea9aSBjoern A. Zeeb 
469c1131de6SBjoern A. Zeeb 	/* Offset now points to data portion. */
470c1131de6SBjoern A. Zeeb 	offset += sizeof(struct ip6_frag);
471c1131de6SBjoern A. Zeeb 
4725f9f192dSJonathan T. Looney 	/* Get fragment length and discard 0-byte fragments. */
4735f9f192dSJonathan T. Looney 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
4745f9f192dSJonathan T. Looney 	if (frgpartlen == 0) {
4755f9f192dSJonathan T. Looney 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
4765f9f192dSJonathan T. Looney 		    offsetof(struct ip6_hdr, ip6_plen));
4775f9f192dSJonathan T. Looney 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
4785f9f192dSJonathan T. Looney 		IP6STAT_INC(ip6s_fragdropped);
4795778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
4805f9f192dSJonathan T. Looney 	}
4815f9f192dSJonathan T. Looney 
4827715d794SBjoern A. Zeeb 	/*
4837715d794SBjoern A. Zeeb 	 * Enforce upper bound on number of fragments for the entire system.
4847715d794SBjoern A. Zeeb 	 * If maxfrag is 0, never accept fragments.
4857715d794SBjoern A. Zeeb 	 * If maxfrag is -1, accept all fragments without limitation.
4867715d794SBjoern A. Zeeb 	 */
4877715d794SBjoern A. Zeeb 	if (ip6_maxfrags < 0)
4887715d794SBjoern A. Zeeb 		;
4897715d794SBjoern A. Zeeb 	else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
4907715d794SBjoern A. Zeeb 		goto dropfrag2;
4917715d794SBjoern A. Zeeb 
49230809ba9SBjoern A. Zeeb 	/*
49330809ba9SBjoern A. Zeeb 	 * Validate that a full header chain to the ULP is present in the
49430809ba9SBjoern A. Zeeb 	 * packet containing the first fragment as per RFC RFC7112 and
49530809ba9SBjoern A. Zeeb 	 * RFC 8200 pages 18,19:
49630809ba9SBjoern A. Zeeb 	 * The first fragment packet is composed of:
49730809ba9SBjoern A. Zeeb 	 * (3)  Extension headers, if any, and the Upper-Layer header.  These
49830809ba9SBjoern A. Zeeb 	 *      headers must be in the first fragment.  ...
49930809ba9SBjoern A. Zeeb 	 */
50030809ba9SBjoern A. Zeeb 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
50130809ba9SBjoern A. Zeeb 	/* XXX TODO.  thj has D16851 open for this. */
50230809ba9SBjoern A. Zeeb 	/* Send ICMPv6 4,3 in case of violation. */
50330809ba9SBjoern A. Zeeb 
504efdfee93SBjoern A. Zeeb 	/* Store receive network interface pointer for later. */
505efdfee93SBjoern A. Zeeb 	srcifp = m->m_pkthdr.rcvif;
506efdfee93SBjoern A. Zeeb 
50723d374aaSBjoern A. Zeeb 	/* Generate a hash value for fragment bucket selection. */
50880d7a853SJonathan T. Looney 	hashkeyp = hashkey;
50980d7a853SJonathan T. Looney 	memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
51080d7a853SJonathan T. Looney 	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
51180d7a853SJonathan T. Looney 	memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
51280d7a853SJonathan T. Looney 	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
51380d7a853SJonathan T. Looney 	*hashkeyp = ip6f->ip6f_ident;
5149cb1a47aSBjoern A. Zeeb 	bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed);
5159cb1a47aSBjoern A. Zeeb 	bucket &= IP6REASS_HMASK;
5169cb1a47aSBjoern A. Zeeb 	IP6QB_LOCK(bucket);
51721f08a07SBjoern A. Zeeb 	head = IP6QB_HEAD(bucket);
5189888c401SHajimu UMEMOTO 
51921f08a07SBjoern A. Zeeb 	TAILQ_FOREACH(q6, head, ip6q_tq)
52082cd038dSYoshinobu Inoue 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
52182cd038dSYoshinobu Inoue 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
5224b908c8bSRobert Watson 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
5234b908c8bSRobert Watson #ifdef MAC
5244b908c8bSRobert Watson 		    && mac_ip6q_match(m, q6)
5254b908c8bSRobert Watson #endif
5264b908c8bSRobert Watson 		    )
52782cd038dSYoshinobu Inoue 			break;
52882cd038dSYoshinobu Inoue 
5295778b399SBjoern A. Zeeb 	only_frag = false;
53021f08a07SBjoern A. Zeeb 	if (q6 == NULL) {
53123d374aaSBjoern A. Zeeb 
5325778b399SBjoern A. Zeeb 		/* A first fragment to arrive creates a reassembly queue. */
5335778b399SBjoern A. Zeeb 		only_frag = true;
53482cd038dSYoshinobu Inoue 
53582cd038dSYoshinobu Inoue 		/*
53682cd038dSYoshinobu Inoue 		 * Enforce upper bound on number of fragmented packets
53782cd038dSYoshinobu Inoue 		 * for which we attempt reassembly;
5389888c401SHajimu UMEMOTO 		 * If maxfragpackets is 0, never accept fragments.
5399888c401SHajimu UMEMOTO 		 * If maxfragpackets is -1, accept all fragments without
5409888c401SHajimu UMEMOTO 		 * limitation.
54182cd038dSYoshinobu Inoue 		 */
542603724d3SBjoern A. Zeeb 		if (V_ip6_maxfragpackets < 0)
54333841545SHajimu UMEMOTO 			;
5449cb1a47aSBjoern A. Zeeb 		else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize ||
5451e9f3b73SJonathan T. Looney 		    atomic_load_int(&V_frag6_nfragpackets) >=
54680d7a853SJonathan T. Looney 		    (u_int)V_ip6_maxfragpackets)
54733841545SHajimu UMEMOTO 			goto dropfrag;
54823d374aaSBjoern A. Zeeb 
54923d374aaSBjoern A. Zeeb 		/* Allocate IPv6 fragement packet queue entry. */
550487a161cSBjoern A. Zeeb 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6,
551487a161cSBjoern A. Zeeb 		    M_NOWAIT | M_ZERO);
55282cd038dSYoshinobu Inoue 		if (q6 == NULL)
55382cd038dSYoshinobu Inoue 			goto dropfrag;
5544b908c8bSRobert Watson #ifdef MAC
5554b908c8bSRobert Watson 		if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
556487a161cSBjoern A. Zeeb 			free(q6, M_FRAG6);
5574b908c8bSRobert Watson 			goto dropfrag;
5584b908c8bSRobert Watson 		}
5594b908c8bSRobert Watson 		mac_ip6q_create(m, q6);
5604b908c8bSRobert Watson #endif
561702828f6SBjoern A. Zeeb 		atomic_add_int(&V_frag6_nfragpackets, 1);
56282cd038dSYoshinobu Inoue 
56323d374aaSBjoern A. Zeeb 		/* ip6q_nxt will be filled afterwards, from 1st fragment. */
56421f08a07SBjoern A. Zeeb 		TAILQ_INIT(&q6->ip6q_frags);
56582cd038dSYoshinobu Inoue 		q6->ip6q_ident	= ip6f->ip6f_ident;
56682cd038dSYoshinobu Inoue 		q6->ip6q_ttl	= IPV6_FRAGTTL;
56782cd038dSYoshinobu Inoue 		q6->ip6q_src	= ip6->ip6_src;
56882cd038dSYoshinobu Inoue 		q6->ip6q_dst	= ip6->ip6_dst;
5695e9510e3SJINMEI Tatuya 		q6->ip6q_ecn	=
5705e9510e3SJINMEI Tatuya 		    (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
57182cd038dSYoshinobu Inoue 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
5729888c401SHajimu UMEMOTO 
57321f08a07SBjoern A. Zeeb 		/* Add the fragemented packet to the bucket. */
57421f08a07SBjoern A. Zeeb 		TAILQ_INSERT_HEAD(head, q6, ip6q_tq);
57521f08a07SBjoern A. Zeeb 		V_ip6qb[bucket].count++;
57682cd038dSYoshinobu Inoue 	}
57782cd038dSYoshinobu Inoue 
57882cd038dSYoshinobu Inoue 	/*
57923d374aaSBjoern A. Zeeb 	 * If it is the 1st fragment, record the length of the
58082cd038dSYoshinobu Inoue 	 * unfragmentable part and the next header of the fragment header.
581619456bbSBjoern A. Zeeb 	 * Assume the first 1st fragement to arrive will be correct.
582619456bbSBjoern A. Zeeb 	 * We do not have any duplicate checks here yet so another packet
583619456bbSBjoern A. Zeeb 	 * with fragoff == 0 could come and overwrite the ip6q_unfrglen
584619456bbSBjoern A. Zeeb 	 * and worse, the next header, at any time.
58582cd038dSYoshinobu Inoue 	 */
586619456bbSBjoern A. Zeeb 	if (fragoff == 0 && q6->ip6q_unfrglen == -1) {
58706cd0a3fSHajimu UMEMOTO 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
58806cd0a3fSHajimu UMEMOTO 		    sizeof(struct ip6_frag);
58982cd038dSYoshinobu Inoue 		q6->ip6q_nxt = ip6f->ip6f_nxt;
590619456bbSBjoern A. Zeeb 		/* XXX ECN? */
59182cd038dSYoshinobu Inoue 	}
59282cd038dSYoshinobu Inoue 
59382cd038dSYoshinobu Inoue 	/*
59482cd038dSYoshinobu Inoue 	 * Check that the reassembled packet would not exceed 65535 bytes
59582cd038dSYoshinobu Inoue 	 * in size.
59682cd038dSYoshinobu Inoue 	 * If it would exceed, discard the fragment and return an ICMP error.
59782cd038dSYoshinobu Inoue 	 */
59882cd038dSYoshinobu Inoue 	if (q6->ip6q_unfrglen >= 0) {
59982cd038dSYoshinobu Inoue 		/* The 1st fragment has already arrived. */
60082cd038dSYoshinobu Inoue 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
601e5fffe9aSBjoern A. Zeeb 			if (only_frag) {
602e5fffe9aSBjoern A. Zeeb 				TAILQ_REMOVE(head, q6, ip6q_tq);
603e5fffe9aSBjoern A. Zeeb 				V_ip6qb[bucket].count--;
604e5fffe9aSBjoern A. Zeeb 				atomic_subtract_int(&V_frag6_nfragpackets, 1);
605e5fffe9aSBjoern A. Zeeb #ifdef MAC
606e5fffe9aSBjoern A. Zeeb 				mac_ip6q_destroy(q6);
607e5fffe9aSBjoern A. Zeeb #endif
608e5fffe9aSBjoern A. Zeeb 				free(q6, M_FRAG6);
609e5fffe9aSBjoern A. Zeeb 			}
610e5fffe9aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
61182cd038dSYoshinobu Inoue 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
612686cdd19SJun-ichiro itojun Hagino 			    offset - sizeof(struct ip6_frag) +
613686cdd19SJun-ichiro itojun Hagino 			    offsetof(struct ip6_frag, ip6f_offlg));
61482cd038dSYoshinobu Inoue 			return (IPPROTO_DONE);
61582cd038dSYoshinobu Inoue 		}
61606cd0a3fSHajimu UMEMOTO 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
617e5fffe9aSBjoern A. Zeeb 		if (only_frag) {
618e5fffe9aSBjoern A. Zeeb 			TAILQ_REMOVE(head, q6, ip6q_tq);
619e5fffe9aSBjoern A. Zeeb 			V_ip6qb[bucket].count--;
620e5fffe9aSBjoern A. Zeeb 			atomic_subtract_int(&V_frag6_nfragpackets, 1);
621e5fffe9aSBjoern A. Zeeb #ifdef MAC
622e5fffe9aSBjoern A. Zeeb 			mac_ip6q_destroy(q6);
623e5fffe9aSBjoern A. Zeeb #endif
624e5fffe9aSBjoern A. Zeeb 			free(q6, M_FRAG6);
625e5fffe9aSBjoern A. Zeeb 		}
626e5fffe9aSBjoern A. Zeeb 		IP6QB_UNLOCK(bucket);
62782cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
628686cdd19SJun-ichiro itojun Hagino 		    offset - sizeof(struct ip6_frag) +
629686cdd19SJun-ichiro itojun Hagino 		    offsetof(struct ip6_frag, ip6f_offlg));
63082cd038dSYoshinobu Inoue 		return (IPPROTO_DONE);
63182cd038dSYoshinobu Inoue 	}
632f1664f32SBjoern A. Zeeb 
63382cd038dSYoshinobu Inoue 	/*
63423d374aaSBjoern A. Zeeb 	 * If it is the first fragment, do the above check for each
63582cd038dSYoshinobu Inoue 	 * fragment already stored in the reassembly queue.
63682cd038dSYoshinobu Inoue 	 */
637dda02192SBjoern A. Zeeb 	if (fragoff == 0 && !only_frag) {
63821f08a07SBjoern A. Zeeb 		TAILQ_FOREACH_SAFE(af6, &q6->ip6q_frags, ip6af_tq, af6tmp) {
63982cd038dSYoshinobu Inoue 
640dda02192SBjoern A. Zeeb 			if (q6->ip6q_unfrglen + af6->ip6af_off +
641dda02192SBjoern A. Zeeb 			    af6->ip6af_frglen > IPV6_MAXPACKET) {
64282cd038dSYoshinobu Inoue 				struct ip6_hdr *ip6err;
6435778b399SBjoern A. Zeeb 				struct mbuf *merr;
6445778b399SBjoern A. Zeeb 				int erroff;
6455778b399SBjoern A. Zeeb 
646da89a0feSBjoern A. Zeeb 				merr = af6->ip6af_m;
6475778b399SBjoern A. Zeeb 				erroff = af6->ip6af_offset;
64882cd038dSYoshinobu Inoue 
64923d374aaSBjoern A. Zeeb 				/* Dequeue the fragment. */
65021f08a07SBjoern A. Zeeb 				TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
651dda02192SBjoern A. Zeeb 				q6->ip6q_nfrag--;
652dda02192SBjoern A. Zeeb 				atomic_subtract_int(&frag6_nfrags, 1);
653487a161cSBjoern A. Zeeb 				free(af6, M_FRAG6);
65482cd038dSYoshinobu Inoue 
655a55383e7SHans Petter Selasky 				/* Set a valid receive interface pointer. */
656a55383e7SHans Petter Selasky 				merr->m_pkthdr.rcvif = srcifp;
657a55383e7SHans Petter Selasky 
65823d374aaSBjoern A. Zeeb 				/* Adjust pointer. */
65982cd038dSYoshinobu Inoue 				ip6err = mtod(merr, struct ip6_hdr *);
66082cd038dSYoshinobu Inoue 
66182cd038dSYoshinobu Inoue 				/*
66282cd038dSYoshinobu Inoue 				 * Restore source and destination addresses
66382cd038dSYoshinobu Inoue 				 * in the erroneous IPv6 header.
66482cd038dSYoshinobu Inoue 				 */
66582cd038dSYoshinobu Inoue 				ip6err->ip6_src = q6->ip6q_src;
66682cd038dSYoshinobu Inoue 				ip6err->ip6_dst = q6->ip6q_dst;
66782cd038dSYoshinobu Inoue 
66882cd038dSYoshinobu Inoue 				icmp6_error(merr, ICMP6_PARAM_PROB,
66982cd038dSYoshinobu Inoue 				    ICMP6_PARAMPROB_HEADER,
670686cdd19SJun-ichiro itojun Hagino 				    erroff - sizeof(struct ip6_frag) +
671686cdd19SJun-ichiro itojun Hagino 				    offsetof(struct ip6_frag, ip6f_offlg));
67282cd038dSYoshinobu Inoue 			}
67382cd038dSYoshinobu Inoue 		}
67482cd038dSYoshinobu Inoue 	}
67582cd038dSYoshinobu Inoue 
67623d374aaSBjoern A. Zeeb 	/* Allocate an IPv6 fragement queue entry for this fragmented part. */
677487a161cSBjoern A. Zeeb 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6,
678487a161cSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
679686cdd19SJun-ichiro itojun Hagino 	if (ip6af == NULL)
680686cdd19SJun-ichiro itojun Hagino 		goto dropfrag;
68121f08a07SBjoern A. Zeeb 	ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) ? true : false;
68282cd038dSYoshinobu Inoue 	ip6af->ip6af_off = fragoff;
68382cd038dSYoshinobu Inoue 	ip6af->ip6af_frglen = frgpartlen;
68482cd038dSYoshinobu Inoue 	ip6af->ip6af_offset = offset;
685da89a0feSBjoern A. Zeeb 	ip6af->ip6af_m = m;
68682cd038dSYoshinobu Inoue 
6875778b399SBjoern A. Zeeb 	if (only_frag) {
68821f08a07SBjoern A. Zeeb 		/*
68921f08a07SBjoern A. Zeeb 		 * Do a manual insert rather than a hard-to-understand cast
69021f08a07SBjoern A. Zeeb 		 * to a different type relying on data structure order to work.
69121f08a07SBjoern A. Zeeb 		 */
69221f08a07SBjoern A. Zeeb 		TAILQ_INSERT_HEAD(&q6->ip6q_frags, ip6af, ip6af_tq);
69321f08a07SBjoern A. Zeeb 		goto postinsert;
69482cd038dSYoshinobu Inoue 	}
69582cd038dSYoshinobu Inoue 
69623d374aaSBjoern A. Zeeb 	/* Do duplicate, condition, and boundry checks. */
69782cd038dSYoshinobu Inoue 	/*
69859dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
69959dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
70023d374aaSBjoern A. Zeeb 	 * Drop if CE and not-ECT are mixed for the same packet.
70159dfcba4SHajimu UMEMOTO 	 */
70259dfcba4SHajimu UMEMOTO 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
7035e9510e3SJINMEI Tatuya 	ecn0 = q6->ip6q_ecn;
70459dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
70559dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT) {
706487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
70759dfcba4SHajimu UMEMOTO 			goto dropfrag;
70859dfcba4SHajimu UMEMOTO 		}
70959dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
7105e9510e3SJINMEI Tatuya 			q6->ip6q_ecn = IPTOS_ECN_CE;
71159dfcba4SHajimu UMEMOTO 	}
71259dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
713487a161cSBjoern A. Zeeb 		free(ip6af, M_FRAG6);
71459dfcba4SHajimu UMEMOTO 		goto dropfrag;
71559dfcba4SHajimu UMEMOTO 	}
71659dfcba4SHajimu UMEMOTO 
71723d374aaSBjoern A. Zeeb 	/* Find a fragmented part which begins after this one does. */
71821f08a07SBjoern A. Zeeb 	TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq)
71982cd038dSYoshinobu Inoue 		if (af6->ip6af_off > ip6af->ip6af_off)
72082cd038dSYoshinobu Inoue 			break;
72182cd038dSYoshinobu Inoue 
72282cd038dSYoshinobu Inoue 	/*
72382cd038dSYoshinobu Inoue 	 * If the incoming framgent overlaps some existing fragments in
72423d374aaSBjoern A. Zeeb 	 * the reassembly queue, drop both the new fragment and the
72523d374aaSBjoern A. Zeeb 	 * entire reassembly queue.  However, if the new fragment
72623d374aaSBjoern A. Zeeb 	 * is an exact duplicate of an existing fragment, only silently
72723d374aaSBjoern A. Zeeb 	 * drop the existing fragment and leave the fragmentation queue
72823d374aaSBjoern A. Zeeb 	 * unchanged, as allowed by the RFC.  (RFC 8200, 4.5)
72982cd038dSYoshinobu Inoue 	 */
73021f08a07SBjoern A. Zeeb 	if (af6 != NULL)
73121f08a07SBjoern A. Zeeb 		af6tmp = TAILQ_PREV(af6, ip6fraghead, ip6af_tq);
73221f08a07SBjoern A. Zeeb 	else
73321f08a07SBjoern A. Zeeb 		af6tmp = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
73421f08a07SBjoern A. Zeeb 	if (af6tmp != NULL) {
73521f08a07SBjoern A. Zeeb 		if (af6tmp->ip6af_off + af6tmp->ip6af_frglen -
7365778b399SBjoern A. Zeeb 		    ip6af->ip6af_off > 0) {
737cd188da2SBjoern A. Zeeb 			if (af6tmp->ip6af_off != ip6af->ip6af_off ||
738cd188da2SBjoern A. Zeeb 			    af6tmp->ip6af_frglen != ip6af->ip6af_frglen)
739cd188da2SBjoern A. Zeeb 				frag6_freef(q6, bucket);
740487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
74182cd038dSYoshinobu Inoue 			goto dropfrag;
74282cd038dSYoshinobu Inoue 		}
74382cd038dSYoshinobu Inoue 	}
74421f08a07SBjoern A. Zeeb 	if (af6 != NULL) {
7455778b399SBjoern A. Zeeb 		if (ip6af->ip6af_off + ip6af->ip6af_frglen -
7465778b399SBjoern A. Zeeb 		    af6->ip6af_off > 0) {
747cd188da2SBjoern A. Zeeb 			if (af6->ip6af_off != ip6af->ip6af_off ||
748cd188da2SBjoern A. Zeeb 			    af6->ip6af_frglen != ip6af->ip6af_frglen)
749cd188da2SBjoern A. Zeeb 				frag6_freef(q6, bucket);
750487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
75182cd038dSYoshinobu Inoue 			goto dropfrag;
75282cd038dSYoshinobu Inoue 		}
75382cd038dSYoshinobu Inoue 	}
75482cd038dSYoshinobu Inoue 
7554b908c8bSRobert Watson #ifdef MAC
7564b908c8bSRobert Watson 	mac_ip6q_update(m, q6);
7574b908c8bSRobert Watson #endif
75882cd038dSYoshinobu Inoue 
75982cd038dSYoshinobu Inoue 	/*
76023d374aaSBjoern A. Zeeb 	 * Stick new segment in its place; check for complete reassembly.
76123d374aaSBjoern A. Zeeb 	 * If not complete, check fragment limit.  Move to front of packet
76223d374aaSBjoern A. Zeeb 	 * queue, as we are the most recently active fragmented packet.
76382cd038dSYoshinobu Inoue 	 */
76421f08a07SBjoern A. Zeeb 	if (af6 != NULL)
76521f08a07SBjoern A. Zeeb 		TAILQ_INSERT_BEFORE(af6, ip6af, ip6af_tq);
76621f08a07SBjoern A. Zeeb 	else
76721f08a07SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(&q6->ip6q_frags, ip6af, ip6af_tq);
76821f08a07SBjoern A. Zeeb postinsert:
7692adfd64fSJonathan T. Looney 	atomic_add_int(&frag6_nfrags, 1);
7709888c401SHajimu UMEMOTO 	q6->ip6q_nfrag++;
7713c7165b3SBjoern A. Zeeb 
7725778b399SBjoern A. Zeeb 	plen = 0;
77321f08a07SBjoern A. Zeeb 	TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
7745778b399SBjoern A. Zeeb 		if (af6->ip6af_off != plen) {
77503c99d76SJonathan T. Looney 			if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
776198fdaedSTom Jones 				IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
7779cb1a47aSBjoern A. Zeeb 				frag6_freef(q6, bucket);
77803c99d76SJonathan T. Looney 			}
7799cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
7805778b399SBjoern A. Zeeb 			return (IPPROTO_DONE);
78182cd038dSYoshinobu Inoue 		}
7825778b399SBjoern A. Zeeb 		plen += af6->ip6af_frglen;
78382cd038dSYoshinobu Inoue 	}
78421f08a07SBjoern A. Zeeb 	af6 = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
78521f08a07SBjoern A. Zeeb 	if (af6->ip6af_mff) {
78603c99d76SJonathan T. Looney 		if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
787198fdaedSTom Jones 			IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
7889cb1a47aSBjoern A. Zeeb 			frag6_freef(q6, bucket);
78903c99d76SJonathan T. Looney 		}
7909cb1a47aSBjoern A. Zeeb 		IP6QB_UNLOCK(bucket);
7915778b399SBjoern A. Zeeb 		return (IPPROTO_DONE);
79282cd038dSYoshinobu Inoue 	}
79382cd038dSYoshinobu Inoue 
79423d374aaSBjoern A. Zeeb 	/* Reassembly is complete; concatenate fragments. */
79521f08a07SBjoern A. Zeeb 	ip6af = TAILQ_FIRST(&q6->ip6q_frags);
796da89a0feSBjoern A. Zeeb 	t = m = ip6af->ip6af_m;
79721f08a07SBjoern A. Zeeb 	TAILQ_REMOVE(&q6->ip6q_frags, ip6af, ip6af_tq);
79821f08a07SBjoern A. Zeeb 	while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
7999907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_flags &=
800da89a0feSBjoern A. Zeeb 		    af6->ip6af_m->m_pkthdr.csum_flags;
8019907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_data +=
802da89a0feSBjoern A. Zeeb 		    af6->ip6af_m->m_pkthdr.csum_data;
8039907aba3SAndrey V. Elsukov 
80421f08a07SBjoern A. Zeeb 		TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
805efdfee93SBjoern A. Zeeb 		t = m_last(t);
806da89a0feSBjoern A. Zeeb 		m_adj(af6->ip6af_m, af6->ip6af_offset);
807da89a0feSBjoern A. Zeeb 		m_demote_pkthdr(af6->ip6af_m);
808da89a0feSBjoern A. Zeeb 		m_cat(t, af6->ip6af_m);
809487a161cSBjoern A. Zeeb 		free(af6, M_FRAG6);
81082cd038dSYoshinobu Inoue 	}
81182cd038dSYoshinobu Inoue 
8129907aba3SAndrey V. Elsukov 	while (m->m_pkthdr.csum_data & 0xffff0000)
8139907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
8149907aba3SAndrey V. Elsukov 		    (m->m_pkthdr.csum_data >> 16);
8159907aba3SAndrey V. Elsukov 
81623d374aaSBjoern A. Zeeb 	/* Adjust offset to point where the original next header starts. */
81782cd038dSYoshinobu Inoue 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
818487a161cSBjoern A. Zeeb 	free(ip6af, M_FRAG6);
819686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
8205778b399SBjoern A. Zeeb 	ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr));
8215e9510e3SJINMEI Tatuya 	if (q6->ip6q_ecn == IPTOS_ECN_CE)
8225e9510e3SJINMEI Tatuya 		ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
82382cd038dSYoshinobu Inoue 	nxt = q6->ip6q_nxt;
82482cd038dSYoshinobu Inoue 
82521f08a07SBjoern A. Zeeb 	TAILQ_REMOVE(head, q6, ip6q_tq);
82621f08a07SBjoern A. Zeeb 	V_ip6qb[bucket].count--;
8272adfd64fSJonathan T. Looney 	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
82821f08a07SBjoern A. Zeeb 
82921f08a07SBjoern A. Zeeb 	if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
8304b908c8bSRobert Watson #ifdef MAC
8314b908c8bSRobert Watson 		mac_ip6q_destroy(q6);
8324b908c8bSRobert Watson #endif
833487a161cSBjoern A. Zeeb 		free(q6, M_FRAG6);
83480d7a853SJonathan T. Looney 		atomic_subtract_int(&V_frag6_nfragpackets, 1);
8350b438b0fSGleb Smirnoff 
836686cdd19SJun-ichiro itojun Hagino 		goto dropfrag;
83782cd038dSYoshinobu Inoue 	}
83882cd038dSYoshinobu Inoue 
83923d374aaSBjoern A. Zeeb 	/* Set nxt(-hdr field value) to the original value. */
84068e0e5a6SAndrey V. Elsukov 	m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
84168e0e5a6SAndrey V. Elsukov 	    (caddr_t)&nxt);
84282cd038dSYoshinobu Inoue 
8434b908c8bSRobert Watson #ifdef MAC
8444b908c8bSRobert Watson 	mac_ip6q_reassemble(q6, m);
8454b908c8bSRobert Watson 	mac_ip6q_destroy(q6);
8464b908c8bSRobert Watson #endif
847487a161cSBjoern A. Zeeb 	free(q6, M_FRAG6);
84880d7a853SJonathan T. Looney 	atomic_subtract_int(&V_frag6_nfragpackets, 1);
84982cd038dSYoshinobu Inoue 
85082cd038dSYoshinobu Inoue 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
8515778b399SBjoern A. Zeeb 
8525778b399SBjoern A. Zeeb 		plen = 0;
85382cd038dSYoshinobu Inoue 		for (t = m; t; t = t->m_next)
85482cd038dSYoshinobu Inoue 			plen += t->m_len;
85582cd038dSYoshinobu Inoue 		m->m_pkthdr.len = plen;
856a55383e7SHans Petter Selasky 		/* Set a valid receive interface pointer. */
857a55383e7SHans Petter Selasky 		m->m_pkthdr.rcvif = srcifp;
85882cd038dSYoshinobu Inoue 	}
85982cd038dSYoshinobu Inoue 
860aaa46574SAdrian Chadd #ifdef RSS
861aaa46574SAdrian Chadd 	mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
862aaa46574SAdrian Chadd 	    M_NOWAIT);
863aaa46574SAdrian Chadd 	if (mtag == NULL)
864aaa46574SAdrian Chadd 		goto dropfrag;
865aaa46574SAdrian Chadd 
866aaa46574SAdrian Chadd 	ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
867aaa46574SAdrian Chadd 	ip6dc->ip6dc_nxt = nxt;
868aaa46574SAdrian Chadd 	ip6dc->ip6dc_off = offset;
869aaa46574SAdrian Chadd 
870aaa46574SAdrian Chadd 	m_tag_prepend(m, mtag);
871aaa46574SAdrian Chadd #endif
872aaa46574SAdrian Chadd 
8739cb1a47aSBjoern A. Zeeb 	IP6QB_UNLOCK(bucket);
8749cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_reassembled);
87582cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
87682cd038dSYoshinobu Inoue 
877aaa46574SAdrian Chadd #ifdef RSS
87823d374aaSBjoern A. Zeeb 	/* Queue/dispatch for reprocessing. */
879aaa46574SAdrian Chadd 	netisr_dispatch(NETISR_IPV6_DIRECT, m);
8805778b399SBjoern A. Zeeb 	return (IPPROTO_DONE);
881aaa46574SAdrian Chadd #endif
882aaa46574SAdrian Chadd 
88323d374aaSBjoern A. Zeeb 	/* Tell launch routine the next header. */
88482cd038dSYoshinobu Inoue 	*mp = m;
88582cd038dSYoshinobu Inoue 	*offp = offset;
88682cd038dSYoshinobu Inoue 
8875778b399SBjoern A. Zeeb 	return (nxt);
88882cd038dSYoshinobu Inoue 
88982cd038dSYoshinobu Inoue dropfrag:
8909cb1a47aSBjoern A. Zeeb 	IP6QB_UNLOCK(bucket);
8917715d794SBjoern A. Zeeb dropfrag2:
89282cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
8939cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_fragdropped);
89482cd038dSYoshinobu Inoue 	m_freem(m);
8955778b399SBjoern A. Zeeb 	return (IPPROTO_DONE);
89682cd038dSYoshinobu Inoue }
89782cd038dSYoshinobu Inoue 
89882cd038dSYoshinobu Inoue /*
89933841545SHajimu UMEMOTO  * IPv6 reassembling timer processing;
90023d374aaSBjoern A. Zeeb  * if a timer expires on a reassembly queue, discard it.
90182cd038dSYoshinobu Inoue  */
90282cd038dSYoshinobu Inoue void
9031272577eSXin LI frag6_slowtimo(void)
90482cd038dSYoshinobu Inoue {
9058b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
90621f08a07SBjoern A. Zeeb 	struct ip6qhead *head;
90721f08a07SBjoern A. Zeeb 	struct ip6q *q6, *q6tmp;
9089cb1a47aSBjoern A. Zeeb 	uint32_t bucket;
90982cd038dSYoshinobu Inoue 
9105ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
9118b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
9128b615593SMarko Zec 		CURVNET_SET(vnet_iter);
9139cb1a47aSBjoern A. Zeeb 		for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
9149cb1a47aSBjoern A. Zeeb 			IP6QB_LOCK(bucket);
9159cb1a47aSBjoern A. Zeeb 			head = IP6QB_HEAD(bucket);
91621f08a07SBjoern A. Zeeb 			TAILQ_FOREACH_SAFE(q6, head, ip6q_tq, q6tmp)
91721f08a07SBjoern A. Zeeb 				if (--q6->ip6q_ttl == 0) {
918198fdaedSTom Jones 					IP6STAT_ADD(ip6s_fragtimeout,
91921f08a07SBjoern A. Zeeb 						q6->ip6q_nfrag);
92082cd038dSYoshinobu Inoue 					/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
92121f08a07SBjoern A. Zeeb 					frag6_freef(q6, bucket);
92282cd038dSYoshinobu Inoue 				}
92382cd038dSYoshinobu Inoue 			/*
92482cd038dSYoshinobu Inoue 			 * If we are over the maximum number of fragments
92582cd038dSYoshinobu Inoue 			 * (due to the limit being lowered), drain off
92682cd038dSYoshinobu Inoue 			 * enough to get down to the new limit.
9271e9f3b73SJonathan T. Looney 			 * Note that we drain all reassembly queues if
9281e9f3b73SJonathan T. Looney 			 * maxfragpackets is 0 (fragmentation is disabled),
92923d374aaSBjoern A. Zeeb 			 * and do not enforce a limit when maxfragpackets
9301e9f3b73SJonathan T. Looney 			 * is negative.
93182cd038dSYoshinobu Inoue 			 */
9321e9f3b73SJonathan T. Looney 			while ((V_ip6_maxfragpackets == 0 ||
9331e9f3b73SJonathan T. Looney 			    (V_ip6_maxfragpackets > 0 &&
9349cb1a47aSBjoern A. Zeeb 			    V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) &&
93521f08a07SBjoern A. Zeeb 			    (q6 = TAILQ_LAST(head, ip6qhead)) != NULL) {
93621f08a07SBjoern A. Zeeb 				IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
93782cd038dSYoshinobu Inoue 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
93821f08a07SBjoern A. Zeeb 				frag6_freef(q6, bucket);
93980d7a853SJonathan T. Looney 			}
9409cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
94182cd038dSYoshinobu Inoue 		}
9421e9f3b73SJonathan T. Looney 		/*
9431e9f3b73SJonathan T. Looney 		 * If we are still over the maximum number of fragmented
9441e9f3b73SJonathan T. Looney 		 * packets, drain off enough to get down to the new limit.
9451e9f3b73SJonathan T. Looney 		 */
9469cb1a47aSBjoern A. Zeeb 		bucket = 0;
9471e9f3b73SJonathan T. Looney 		while (V_ip6_maxfragpackets >= 0 &&
9481e9f3b73SJonathan T. Looney 		    atomic_load_int(&V_frag6_nfragpackets) >
9491e9f3b73SJonathan T. Looney 		    (u_int)V_ip6_maxfragpackets) {
9509cb1a47aSBjoern A. Zeeb 			IP6QB_LOCK(bucket);
95121f08a07SBjoern A. Zeeb 			q6 = TAILQ_LAST(IP6QB_HEAD(bucket), ip6qhead);
95221f08a07SBjoern A. Zeeb 			if (q6 != NULL) {
95321f08a07SBjoern A. Zeeb 				IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
9541e9f3b73SJonathan T. Looney 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
95521f08a07SBjoern A. Zeeb 				frag6_freef(q6, bucket);
9561e9f3b73SJonathan T. Looney 			}
9579cb1a47aSBjoern A. Zeeb 			IP6QB_UNLOCK(bucket);
9589cb1a47aSBjoern A. Zeeb 			bucket = (bucket + 1) % IP6REASS_NHASH;
9591e9f3b73SJonathan T. Looney 		}
9608b615593SMarko Zec 		CURVNET_RESTORE();
9618b615593SMarko Zec 	}
9625ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
96382cd038dSYoshinobu Inoue }
96482cd038dSYoshinobu Inoue 
96523d374aaSBjoern A. Zeeb /*
96623d374aaSBjoern A. Zeeb  * Eventhandler to adjust limits in case nmbclusters change.
96723d374aaSBjoern A. Zeeb  */
968c00464a2SBjoern A. Zeeb static void
969c00464a2SBjoern A. Zeeb frag6_change(void *tag)
970c00464a2SBjoern A. Zeeb {
971c00464a2SBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
972c00464a2SBjoern A. Zeeb 
973c00464a2SBjoern A. Zeeb 	ip6_maxfrags = IP6_MAXFRAGS;
974c00464a2SBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
975c00464a2SBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
976c00464a2SBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
977c00464a2SBjoern A. Zeeb 		V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
978c00464a2SBjoern A. Zeeb 		frag6_set_bucketsize();
979c00464a2SBjoern A. Zeeb 		CURVNET_RESTORE();
980c00464a2SBjoern A. Zeeb 	}
981c00464a2SBjoern A. Zeeb 	VNET_LIST_RUNLOCK_NOSLEEP();
982c00464a2SBjoern A. Zeeb }
983c00464a2SBjoern A. Zeeb 
984c00464a2SBjoern A. Zeeb /*
985c00464a2SBjoern A. Zeeb  * Initialise reassembly queue and fragment identifier.
986c00464a2SBjoern A. Zeeb  */
987c00464a2SBjoern A. Zeeb void
988c00464a2SBjoern A. Zeeb frag6_init(void)
989c00464a2SBjoern A. Zeeb {
9909cb1a47aSBjoern A. Zeeb 	uint32_t bucket;
991c00464a2SBjoern A. Zeeb 
992c00464a2SBjoern A. Zeeb 	V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
993c00464a2SBjoern A. Zeeb 	frag6_set_bucketsize();
9949cb1a47aSBjoern A. Zeeb 	for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
99521f08a07SBjoern A. Zeeb 		TAILQ_INIT(IP6QB_HEAD(bucket));
996efdfee93SBjoern A. Zeeb 		mtx_init(&V_ip6qb[bucket].lock, "ip6qb", NULL, MTX_DEF);
9979cb1a47aSBjoern A. Zeeb 		V_ip6qb[bucket].count = 0;
998c00464a2SBjoern A. Zeeb 	}
9999cb1a47aSBjoern A. Zeeb 	V_ip6qb_hashseed = arc4random();
1000c00464a2SBjoern A. Zeeb 	V_ip6_maxfragsperpacket = 64;
100167a10c46SBjoern A. Zeeb #ifdef VIMAGE
100267a10c46SBjoern A. Zeeb 	V_frag6_on = true;
100367a10c46SBjoern A. Zeeb #endif
1004c00464a2SBjoern A. Zeeb 	if (!IS_DEFAULT_VNET(curvnet))
1005c00464a2SBjoern A. Zeeb 		return;
1006c00464a2SBjoern A. Zeeb 
1007c00464a2SBjoern A. Zeeb 	ip6_maxfrags = IP6_MAXFRAGS;
1008c00464a2SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(nmbclusters_change,
1009c00464a2SBjoern A. Zeeb 	    frag6_change, NULL, EVENTHANDLER_PRI_ANY);
1010c00464a2SBjoern A. Zeeb }
1011c00464a2SBjoern A. Zeeb 
101282cd038dSYoshinobu Inoue /*
101382cd038dSYoshinobu Inoue  * Drain off all datagram fragments.
101482cd038dSYoshinobu Inoue  */
101567a10c46SBjoern A. Zeeb static void
101667a10c46SBjoern A. Zeeb frag6_drain_one(void)
101782cd038dSYoshinobu Inoue {
101821f08a07SBjoern A. Zeeb 	struct ip6q *q6;
10199cb1a47aSBjoern A. Zeeb 	uint32_t bucket;
10209888c401SHajimu UMEMOTO 
10219cb1a47aSBjoern A. Zeeb 	for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
102267a10c46SBjoern A. Zeeb 		IP6QB_LOCK(bucket);
102321f08a07SBjoern A. Zeeb 		while ((q6 = TAILQ_FIRST(IP6QB_HEAD(bucket))) != NULL) {
10249cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_fragdropped);
102582cd038dSYoshinobu Inoue 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
102621f08a07SBjoern A. Zeeb 			frag6_freef(q6, bucket);
102780d7a853SJonathan T. Looney 		}
10289cb1a47aSBjoern A. Zeeb 		IP6QB_UNLOCK(bucket);
102982cd038dSYoshinobu Inoue 	}
103067a10c46SBjoern A. Zeeb }
103167a10c46SBjoern A. Zeeb 
103267a10c46SBjoern A. Zeeb void
103367a10c46SBjoern A. Zeeb frag6_drain(void)
103467a10c46SBjoern A. Zeeb {
103567a10c46SBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
103667a10c46SBjoern A. Zeeb 
103767a10c46SBjoern A. Zeeb 	VNET_LIST_RLOCK_NOSLEEP();
103867a10c46SBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
103967a10c46SBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
104067a10c46SBjoern A. Zeeb 		frag6_drain_one();
10418b615593SMarko Zec 		CURVNET_RESTORE();
10428b615593SMarko Zec 	}
10435ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
104482cd038dSYoshinobu Inoue }
1045e5ee7060SGleb Smirnoff 
104667a10c46SBjoern A. Zeeb #ifdef VIMAGE
104767a10c46SBjoern A. Zeeb /*
104867a10c46SBjoern A. Zeeb  * Clear up IPv6 reassembly structures.
104967a10c46SBjoern A. Zeeb  */
105067a10c46SBjoern A. Zeeb void
105167a10c46SBjoern A. Zeeb frag6_destroy(void)
105267a10c46SBjoern A. Zeeb {
105367a10c46SBjoern A. Zeeb 	uint32_t bucket;
105467a10c46SBjoern A. Zeeb 
105567a10c46SBjoern A. Zeeb 	frag6_drain_one();
105667a10c46SBjoern A. Zeeb 	V_frag6_on = false;
105767a10c46SBjoern A. Zeeb 	for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
105867a10c46SBjoern A. Zeeb 		KASSERT(V_ip6qb[bucket].count == 0,
105967a10c46SBjoern A. Zeeb 		    ("%s: V_ip6qb[%d] (%p) count not 0 (%d)", __func__,
106067a10c46SBjoern A. Zeeb 		    bucket, &V_ip6qb[bucket], V_ip6qb[bucket].count));
106167a10c46SBjoern A. Zeeb 		mtx_destroy(&V_ip6qb[bucket].lock);
106267a10c46SBjoern A. Zeeb 	}
106367a10c46SBjoern A. Zeeb }
106467a10c46SBjoern A. Zeeb #endif
1065