xref: /freebsd/sys/netinet6/frag6.c (revision f349c821)
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>
6359dfcba4SHajimu UMEMOTO #include <netinet/in_systm.h>	/* for ECN definitions */
6459dfcba4SHajimu UMEMOTO #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 
7031e8f7e5SHajimu UMEMOTO /*
7180d7a853SJonathan T. Looney  * Reassembly headers are stored in hash buckets.
7231e8f7e5SHajimu UMEMOTO  */
732ceeacbeSJonathan T. Looney #define	IP6REASS_NHASH_LOG2	10
7480d7a853SJonathan T. Looney #define	IP6REASS_NHASH		(1 << IP6REASS_NHASH_LOG2)
7580d7a853SJonathan T. Looney #define	IP6REASS_HMASK		(IP6REASS_NHASH - 1)
7680d7a853SJonathan T. Looney 
7780d7a853SJonathan T. Looney static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
7880d7a853SJonathan T. Looney     uint32_t bucket __unused);
7980d7a853SJonathan T. Looney static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
8080d7a853SJonathan T. Looney static void frag6_insque_head(struct ip6q *, struct ip6q *,
811e9f3b73SJonathan T. Looney     uint32_t bucket);
821e9f3b73SJonathan T. Looney static void frag6_remque(struct ip6q *, uint32_t bucket);
836bbdbbb8SHans Petter Selasky static void frag6_freef(struct ip6q *, uint32_t bucket);
8480d7a853SJonathan T. Looney 
8580d7a853SJonathan T. Looney struct ip6qbucket {
8680d7a853SJonathan T. Looney 	struct ip6q	ip6q;
8780d7a853SJonathan T. Looney 	struct mtx	lock;
881e9f3b73SJonathan T. Looney 	int		count;
8980d7a853SJonathan T. Looney };
9080d7a853SJonathan T. Looney 
91487a161cSBjoern A. Zeeb static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");
92487a161cSBjoern A. Zeeb 
93757cb678SBjoern A. Zeeb /* System wide (global) maximum and count of packets in reassembly queues. */
94757cb678SBjoern A. Zeeb static int ip6_maxfrags;
95757cb678SBjoern A. Zeeb static volatile u_int frag6_nfrags = 0;
96757cb678SBjoern A. Zeeb 
97757cb678SBjoern A. Zeeb /* Maximum and current packets in per-VNET reassembly queue. */
98757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragpackets);
9980d7a853SJonathan T. Looney VNET_DEFINE_STATIC(volatile u_int,	frag6_nfragpackets);
100757cb678SBjoern A. Zeeb #define	V_ip6_maxfragpackets		VNET(ip6_maxfragpackets)
101757cb678SBjoern A. Zeeb #define	V_frag6_nfragpackets		VNET(frag6_nfragpackets)
102757cb678SBjoern A. Zeeb 
103757cb678SBjoern A. Zeeb /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
104757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragbucketsize);
105757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int,			ip6_maxfragsperpacket);
106757cb678SBjoern A. Zeeb #define	V_ip6_maxfragbucketsize		VNET(ip6_maxfragbucketsize)
107757cb678SBjoern A. Zeeb #define	V_ip6_maxfragsperpacket		VNET(ip6_maxfragsperpacket)
108757cb678SBjoern A. Zeeb 
109757cb678SBjoern A. Zeeb /* Per-VNET reassembly queue buckets. */
11080d7a853SJonathan T. Looney VNET_DEFINE_STATIC(struct ip6qbucket,	ip6q[IP6REASS_NHASH]);
11180d7a853SJonathan T. Looney VNET_DEFINE_STATIC(uint32_t,		ip6q_hashseed);
1121e77c105SRobert Watson #define	V_ip6q				VNET(ip6q)
11380d7a853SJonathan T. Looney #define	V_ip6q_hashseed			VNET(ip6q_hashseed)
11482cd038dSYoshinobu Inoue 
11580d7a853SJonathan T. Looney #define	IP6Q_LOCK(i)		mtx_lock(&V_ip6q[(i)].lock)
11680d7a853SJonathan T. Looney #define	IP6Q_TRYLOCK(i)		mtx_trylock(&V_ip6q[(i)].lock)
11780d7a853SJonathan T. Looney #define	IP6Q_LOCK_ASSERT(i)	mtx_assert(&V_ip6q[(i)].lock, MA_OWNED)
11880d7a853SJonathan T. Looney #define	IP6Q_UNLOCK(i)		mtx_unlock(&V_ip6q[(i)].lock)
11980d7a853SJonathan T. Looney #define	IP6Q_HEAD(i)		(&V_ip6q[(i)].ip6q)
1209888c401SHajimu UMEMOTO 
12182cd038dSYoshinobu Inoue /*
1222ceeacbeSJonathan T. Looney  * By default, limit the number of IP6 fragments across all reassembly
1232ceeacbeSJonathan T. Looney  * queues to  1/32 of the total number of mbuf clusters.
1242ceeacbeSJonathan T. Looney  *
1252ceeacbeSJonathan T. Looney  * Limit the total number of reassembly queues per VNET to the
1262ceeacbeSJonathan T. Looney  * IP6 fragment limit, but ensure the limit will not allow any bucket
1272ceeacbeSJonathan T. Looney  * to grow above 100 items. (The bucket limit is
1282ceeacbeSJonathan T. Looney  * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
1292ceeacbeSJonathan T. Looney  * multiplier to reach a 100-item limit.)
1302ceeacbeSJonathan T. Looney  * The 100-item limit was chosen as brief testing seems to show that
1312ceeacbeSJonathan T. Looney  * this produces "reasonable" performance on some subset of systems
1322ceeacbeSJonathan T. Looney  * under DoS attack.
1332ceeacbeSJonathan T. Looney  */
1342ceeacbeSJonathan T. Looney #define	IP6_MAXFRAGS		(nmbclusters / 32)
1352ceeacbeSJonathan T. Looney #define	IP6_MAXFRAGPACKETS	(imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
1362ceeacbeSJonathan T. Looney 
137757cb678SBjoern A. Zeeb 
1382ceeacbeSJonathan T. Looney /*
139757cb678SBjoern A. Zeeb  * Sysctls and helper function.
14082cd038dSYoshinobu Inoue  */
141757cb678SBjoern A. Zeeb SYSCTL_DECL(_net_inet6_ip6);
142757cb678SBjoern A. Zeeb 
143757cb678SBjoern A. Zeeb static void
14409b361c7SBjoern A. Zeeb frag6_set_bucketsize(void)
1451e9f3b73SJonathan T. Looney {
1461e9f3b73SJonathan T. Looney 	int i;
1471e9f3b73SJonathan T. Looney 
1481e9f3b73SJonathan T. Looney 	if ((i = V_ip6_maxfragpackets) > 0)
1491e9f3b73SJonathan T. Looney 		V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
1501e9f3b73SJonathan T. Looney }
1511e9f3b73SJonathan T. Looney 
152757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
153757cb678SBjoern A. Zeeb 	CTLFLAG_RW, &ip6_maxfrags, 0,
154757cb678SBjoern A. Zeeb 	"Maximum allowed number of outstanding IPv6 packet fragments. "
155757cb678SBjoern A. Zeeb 	"A value of 0 means no fragmented packets will be accepted, while a "
156757cb678SBjoern A. Zeeb 	"a value of -1 means no limit");
157757cb678SBjoern A. Zeeb 
158757cb678SBjoern A. Zeeb static int
159757cb678SBjoern A. Zeeb sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
160757cb678SBjoern A. Zeeb {
161757cb678SBjoern A. Zeeb 	int error, val;
162757cb678SBjoern A. Zeeb 
163757cb678SBjoern A. Zeeb 	val = V_ip6_maxfragpackets;
164757cb678SBjoern A. Zeeb 	error = sysctl_handle_int(oidp, &val, 0, req);
165757cb678SBjoern A. Zeeb 	if (error != 0 || !req->newptr)
166757cb678SBjoern A. Zeeb 		return (error);
167757cb678SBjoern A. Zeeb 	V_ip6_maxfragpackets = val;
168757cb678SBjoern A. Zeeb 	frag6_set_bucketsize();
169757cb678SBjoern A. Zeeb 	return (0);
170757cb678SBjoern A. Zeeb }
171757cb678SBjoern A. Zeeb SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
172757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
173757cb678SBjoern A. Zeeb 	sysctl_ip6_maxfragpackets, "I",
174757cb678SBjoern A. Zeeb 	"Default maximum number of outstanding fragmented IPv6 packets. "
175757cb678SBjoern A. Zeeb 	"A value of 0 means no fragmented packets will be accepted, while a "
176757cb678SBjoern A. Zeeb 	"a value of -1 means no limit");
177757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
178757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
179757cb678SBjoern A. Zeeb 	"Maximum allowed number of fragments per packet");
180757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
181757cb678SBjoern A. Zeeb 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
182757cb678SBjoern A. Zeeb 	"Maximum number of reassembly queues per hash bucket");
183757cb678SBjoern A. Zeeb 
184757cb678SBjoern A. Zeeb 
185757cb678SBjoern A. Zeeb /*
186757cb678SBjoern A. Zeeb  * Initialise reassembly queue and fragment identifier.
187757cb678SBjoern A. Zeeb  */
1884f590175SPaul Saab static void
1894f590175SPaul Saab frag6_change(void *tag)
1904f590175SPaul Saab {
1912adfd64fSJonathan T. Looney 	VNET_ITERATOR_DECL(vnet_iter);
1924f590175SPaul Saab 
1932ceeacbeSJonathan T. Looney 	ip6_maxfrags = IP6_MAXFRAGS;
1942adfd64fSJonathan T. Looney 	VNET_LIST_RLOCK_NOSLEEP();
1952adfd64fSJonathan T. Looney 	VNET_FOREACH(vnet_iter) {
1962adfd64fSJonathan T. Looney 		CURVNET_SET(vnet_iter);
1972ceeacbeSJonathan T. Looney 		V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
1981e9f3b73SJonathan T. Looney 		frag6_set_bucketsize();
1992adfd64fSJonathan T. Looney 		CURVNET_RESTORE();
2002adfd64fSJonathan T. Looney 	}
2012adfd64fSJonathan T. Looney 	VNET_LIST_RUNLOCK_NOSLEEP();
2024f590175SPaul Saab }
2034f590175SPaul Saab 
20482cd038dSYoshinobu Inoue void
2051272577eSXin LI frag6_init(void)
20682cd038dSYoshinobu Inoue {
20780d7a853SJonathan T. Looney 	struct ip6q *q6;
20880d7a853SJonathan T. Looney 	int i;
20982cd038dSYoshinobu Inoue 
2102ceeacbeSJonathan T. Looney 	V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
2111e9f3b73SJonathan T. Looney 	frag6_set_bucketsize();
21280d7a853SJonathan T. Looney 	for (i = 0; i < IP6REASS_NHASH; i++) {
21380d7a853SJonathan T. Looney 		q6 = IP6Q_HEAD(i);
21480d7a853SJonathan T. Looney 		q6->ip6q_next = q6->ip6q_prev = q6;
21580d7a853SJonathan T. Looney 		mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
2161e9f3b73SJonathan T. Looney 		V_ip6q[i].count = 0;
21780d7a853SJonathan T. Looney 	}
21880d7a853SJonathan T. Looney 	V_ip6q_hashseed = arc4random();
21903c99d76SJonathan T. Looney 	V_ip6_maxfragsperpacket = 64;
2201ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
2211ed81b73SMarko Zec 		return;
22296c2b042SJesper Skriver 
2232ceeacbeSJonathan T. Looney 	ip6_maxfrags = IP6_MAXFRAGS;
2241ed81b73SMarko Zec 	EVENTHANDLER_REGISTER(nmbclusters_change,
2251ed81b73SMarko Zec 	    frag6_change, NULL, EVENTHANDLER_PRI_ANY);
22682cd038dSYoshinobu Inoue }
22782cd038dSYoshinobu Inoue 
22882cd038dSYoshinobu Inoue /*
229686cdd19SJun-ichiro itojun Hagino  * In RFC2460, fragment and reassembly rule do not agree with each other,
230686cdd19SJun-ichiro itojun Hagino  * in terms of next header field handling in fragment header.
231686cdd19SJun-ichiro itojun Hagino  * While the sender will use the same value for all of the fragmented packets,
232686cdd19SJun-ichiro itojun Hagino  * receiver is suggested not to check the consistency.
233686cdd19SJun-ichiro itojun Hagino  *
234686cdd19SJun-ichiro itojun Hagino  * fragment rule (p20):
235686cdd19SJun-ichiro itojun Hagino  *	(2) A Fragment header containing:
236686cdd19SJun-ichiro itojun Hagino  *	The Next Header value that identifies the first header of
237686cdd19SJun-ichiro itojun Hagino  *	the Fragmentable Part of the original packet.
238686cdd19SJun-ichiro itojun Hagino  *		-> next header field is same for all fragments
239686cdd19SJun-ichiro itojun Hagino  *
240686cdd19SJun-ichiro itojun Hagino  * reassembly rule (p21):
241686cdd19SJun-ichiro itojun Hagino  *	The Next Header field of the last header of the Unfragmentable
242686cdd19SJun-ichiro itojun Hagino  *	Part is obtained from the Next Header field of the first
243686cdd19SJun-ichiro itojun Hagino  *	fragment's Fragment header.
244686cdd19SJun-ichiro itojun Hagino  *		-> should grab it from the first fragment only
245686cdd19SJun-ichiro itojun Hagino  *
246686cdd19SJun-ichiro itojun Hagino  * The following note also contradicts with fragment rule - no one is going to
247686cdd19SJun-ichiro itojun Hagino  * send different fragment with different next header field.
248686cdd19SJun-ichiro itojun Hagino  *
249686cdd19SJun-ichiro itojun Hagino  * additional note (p22):
250686cdd19SJun-ichiro itojun Hagino  *	The Next Header values in the Fragment headers of different
251686cdd19SJun-ichiro itojun Hagino  *	fragments of the same original packet may differ.  Only the value
252686cdd19SJun-ichiro itojun Hagino  *	from the Offset zero fragment packet is used for reassembly.
253686cdd19SJun-ichiro itojun Hagino  *		-> should grab it from the first fragment only
254686cdd19SJun-ichiro itojun Hagino  *
255686cdd19SJun-ichiro itojun Hagino  * There is no explicit reason given in the RFC.  Historical reason maybe?
256686cdd19SJun-ichiro itojun Hagino  */
257686cdd19SJun-ichiro itojun Hagino /*
25882cd038dSYoshinobu Inoue  * Fragment input
25982cd038dSYoshinobu Inoue  */
26082cd038dSYoshinobu Inoue int
2611272577eSXin LI frag6_input(struct mbuf **mp, int *offp, int proto)
26282cd038dSYoshinobu Inoue {
26382cd038dSYoshinobu Inoue 	struct mbuf *m = *mp, *t;
26482cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6;
26582cd038dSYoshinobu Inoue 	struct ip6_frag *ip6f;
26680d7a853SJonathan T. Looney 	struct ip6q *head, *q6;
267686cdd19SJun-ichiro itojun Hagino 	struct ip6asfrag *af6, *ip6af, *af6dwn;
2682a5aafceSHajimu UMEMOTO 	struct in6_ifaddr *ia;
26982cd038dSYoshinobu Inoue 	int offset = *offp, nxt, i, next;
27082cd038dSYoshinobu Inoue 	int first_frag = 0;
271686cdd19SJun-ichiro itojun Hagino 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
272505e91f5SKristof Provost 	uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
273505e91f5SKristof Provost 		    sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
274505e91f5SKristof Provost 	uint32_t hash, *hashkeyp;
27582cd038dSYoshinobu Inoue 	struct ifnet *dstifp;
27659dfcba4SHajimu UMEMOTO 	u_int8_t ecn, ecn0;
277aaa46574SAdrian Chadd #ifdef RSS
278aaa46574SAdrian Chadd 	struct m_tag *mtag;
279aaa46574SAdrian Chadd 	struct ip6_direct_ctx *ip6dc;
280aaa46574SAdrian Chadd #endif
281aaa46574SAdrian Chadd 
28282cd038dSYoshinobu Inoue 	ip6 = mtod(m, struct ip6_hdr *);
283686cdd19SJun-ichiro itojun Hagino #ifndef PULLDOWN_TEST
284686cdd19SJun-ichiro itojun Hagino 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
28582cd038dSYoshinobu Inoue 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
286686cdd19SJun-ichiro itojun Hagino #else
287686cdd19SJun-ichiro itojun Hagino 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
288686cdd19SJun-ichiro itojun Hagino 	if (ip6f == NULL)
28940e39bbbSHajimu UMEMOTO 		return (IPPROTO_DONE);
290686cdd19SJun-ichiro itojun Hagino #endif
29182cd038dSYoshinobu Inoue 
29282cd038dSYoshinobu Inoue 	dstifp = NULL;
29382cd038dSYoshinobu Inoue 	/* find the destination interface of the packet. */
2943e88eb90SAndrey V. Elsukov 	ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
2953e88eb90SAndrey V. Elsukov 	if (ia != NULL) {
2962a5aafceSHajimu UMEMOTO 		dstifp = ia->ia_ifp;
2978c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
2988c0fec80SRobert Watson 	}
29982cd038dSYoshinobu Inoue 	/* jumbo payload can't contain a fragment header */
30082cd038dSYoshinobu Inoue 	if (ip6->ip6_plen == 0) {
30182cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
30282cd038dSYoshinobu Inoue 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
30382cd038dSYoshinobu Inoue 		return IPPROTO_DONE;
30482cd038dSYoshinobu Inoue 	}
30582cd038dSYoshinobu Inoue 
30682cd038dSYoshinobu Inoue 	/*
30782cd038dSYoshinobu Inoue 	 * check whether fragment packet's fragment length is
30882cd038dSYoshinobu Inoue 	 * multiple of 8 octets.
30982cd038dSYoshinobu Inoue 	 * sizeof(struct ip6_frag) == 8
31082cd038dSYoshinobu Inoue 	 * sizeof(struct ip6_hdr) = 40
31182cd038dSYoshinobu Inoue 	 */
31282cd038dSYoshinobu Inoue 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
31382cd038dSYoshinobu Inoue 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
31406cd0a3fSHajimu UMEMOTO 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
315686cdd19SJun-ichiro itojun Hagino 		    offsetof(struct ip6_hdr, ip6_plen));
31682cd038dSYoshinobu Inoue 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
31782cd038dSYoshinobu Inoue 		return IPPROTO_DONE;
31882cd038dSYoshinobu Inoue 	}
31982cd038dSYoshinobu Inoue 
3209cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_fragments);
32182cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
32282cd038dSYoshinobu Inoue 
323686cdd19SJun-ichiro itojun Hagino 	/* offset now points to data portion */
32482cd038dSYoshinobu Inoue 	offset += sizeof(struct ip6_frag);
32582cd038dSYoshinobu Inoue 
3264018ea9aSBjoern A. Zeeb 	/*
3272946a941STom Jones 	 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
3282946a941STom Jones 	 * unrelated to any reassembly (see RFC 6946 and section 4.5 of RFC
3292946a941STom Jones 	 * 8200).  Just skip the fragment header.
3304018ea9aSBjoern A. Zeeb 	 */
3314018ea9aSBjoern A. Zeeb 	if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
3322946a941STom Jones 		IP6STAT_INC(ip6s_atomicfrags);
3334018ea9aSBjoern A. Zeeb 		in6_ifstat_inc(dstifp, ifs6_reass_ok);
3344018ea9aSBjoern A. Zeeb 		*offp = offset;
335a4061289SAndrey V. Elsukov 		m->m_flags |= M_FRAGMENTED;
3364018ea9aSBjoern A. Zeeb 		return (ip6f->ip6f_nxt);
3374018ea9aSBjoern A. Zeeb 	}
3384018ea9aSBjoern A. Zeeb 
3395f9f192dSJonathan T. Looney 	/* Get fragment length and discard 0-byte fragments. */
3405f9f192dSJonathan T. Looney 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
3415f9f192dSJonathan T. Looney 	if (frgpartlen == 0) {
3425f9f192dSJonathan T. Looney 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
3435f9f192dSJonathan T. Looney 		    offsetof(struct ip6_hdr, ip6_plen));
3445f9f192dSJonathan T. Looney 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
3455f9f192dSJonathan T. Looney 		IP6STAT_INC(ip6s_fragdropped);
3465f9f192dSJonathan T. Looney 		return IPPROTO_DONE;
3475f9f192dSJonathan T. Looney 	}
3485f9f192dSJonathan T. Looney 
34980d7a853SJonathan T. Looney 	hashkeyp = hashkey;
35080d7a853SJonathan T. Looney 	memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
35180d7a853SJonathan T. Looney 	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
35280d7a853SJonathan T. Looney 	memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
35380d7a853SJonathan T. Looney 	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
35480d7a853SJonathan T. Looney 	*hashkeyp = ip6f->ip6f_ident;
35580d7a853SJonathan T. Looney 	hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
35680d7a853SJonathan T. Looney 	hash &= IP6REASS_HMASK;
35780d7a853SJonathan T. Looney 	head = IP6Q_HEAD(hash);
35880d7a853SJonathan T. Looney 	IP6Q_LOCK(hash);
3599888c401SHajimu UMEMOTO 
3609888c401SHajimu UMEMOTO 	/*
3619888c401SHajimu UMEMOTO 	 * Enforce upper bound on number of fragments.
3629888c401SHajimu UMEMOTO 	 * If maxfrag is 0, never accept fragments.
3639888c401SHajimu UMEMOTO 	 * If maxfrag is -1, accept all fragments without limitation.
3649888c401SHajimu UMEMOTO 	 */
3652adfd64fSJonathan T. Looney 	if (ip6_maxfrags < 0)
3669888c401SHajimu UMEMOTO 		;
3672adfd64fSJonathan T. Looney 	else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
3689888c401SHajimu UMEMOTO 		goto dropfrag;
36933841545SHajimu UMEMOTO 
37080d7a853SJonathan T. Looney 	for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
37182cd038dSYoshinobu Inoue 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
37282cd038dSYoshinobu Inoue 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
3734b908c8bSRobert Watson 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
3744b908c8bSRobert Watson #ifdef MAC
3754b908c8bSRobert Watson 		    && mac_ip6q_match(m, q6)
3764b908c8bSRobert Watson #endif
3774b908c8bSRobert Watson 		    )
37882cd038dSYoshinobu Inoue 			break;
37982cd038dSYoshinobu Inoue 
38080d7a853SJonathan T. Looney 	if (q6 == head) {
38182cd038dSYoshinobu Inoue 		/*
38282cd038dSYoshinobu Inoue 		 * the first fragment to arrive, create a reassembly queue.
38382cd038dSYoshinobu Inoue 		 */
38482cd038dSYoshinobu Inoue 		first_frag = 1;
38582cd038dSYoshinobu Inoue 
38682cd038dSYoshinobu Inoue 		/*
38782cd038dSYoshinobu Inoue 		 * Enforce upper bound on number of fragmented packets
38882cd038dSYoshinobu Inoue 		 * for which we attempt reassembly;
3899888c401SHajimu UMEMOTO 		 * If maxfragpackets is 0, never accept fragments.
3909888c401SHajimu UMEMOTO 		 * If maxfragpackets is -1, accept all fragments without
3919888c401SHajimu UMEMOTO 		 * limitation.
39282cd038dSYoshinobu Inoue 		 */
393603724d3SBjoern A. Zeeb 		if (V_ip6_maxfragpackets < 0)
39433841545SHajimu UMEMOTO 			;
3951e9f3b73SJonathan T. Looney 		else if (V_ip6q[hash].count >= V_ip6_maxfragbucketsize ||
3961e9f3b73SJonathan T. Looney 		    atomic_load_int(&V_frag6_nfragpackets) >=
39780d7a853SJonathan T. Looney 		    (u_int)V_ip6_maxfragpackets)
39833841545SHajimu UMEMOTO 			goto dropfrag;
39980d7a853SJonathan T. Looney 		atomic_add_int(&V_frag6_nfragpackets, 1);
400487a161cSBjoern A. Zeeb 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6,
401487a161cSBjoern A. Zeeb 		    M_NOWAIT | M_ZERO);
40282cd038dSYoshinobu Inoue 		if (q6 == NULL)
40382cd038dSYoshinobu Inoue 			goto dropfrag;
4044b908c8bSRobert Watson #ifdef MAC
4054b908c8bSRobert Watson 		if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
406487a161cSBjoern A. Zeeb 			free(q6, M_FRAG6);
4074b908c8bSRobert Watson 			goto dropfrag;
4084b908c8bSRobert Watson 		}
4094b908c8bSRobert Watson 		mac_ip6q_create(m, q6);
4104b908c8bSRobert Watson #endif
41180d7a853SJonathan T. Looney 		frag6_insque_head(q6, head, hash);
41282cd038dSYoshinobu Inoue 
413686cdd19SJun-ichiro itojun Hagino 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
41482cd038dSYoshinobu Inoue 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
415686cdd19SJun-ichiro itojun Hagino #ifdef notyet
416686cdd19SJun-ichiro itojun Hagino 		q6->ip6q_nxtp	= (u_char *)nxtp;
417686cdd19SJun-ichiro itojun Hagino #endif
41882cd038dSYoshinobu Inoue 		q6->ip6q_ident	= ip6f->ip6f_ident;
41982cd038dSYoshinobu Inoue 		q6->ip6q_ttl	= IPV6_FRAGTTL;
42082cd038dSYoshinobu Inoue 		q6->ip6q_src	= ip6->ip6_src;
42182cd038dSYoshinobu Inoue 		q6->ip6q_dst	= ip6->ip6_dst;
4225e9510e3SJINMEI Tatuya 		q6->ip6q_ecn	=
4235e9510e3SJINMEI Tatuya 		    (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
42482cd038dSYoshinobu Inoue 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
4259888c401SHajimu UMEMOTO 
4269888c401SHajimu UMEMOTO 		q6->ip6q_nfrag = 0;
42782cd038dSYoshinobu Inoue 	}
42882cd038dSYoshinobu Inoue 
42982cd038dSYoshinobu Inoue 	/*
43082cd038dSYoshinobu Inoue 	 * If it's the 1st fragment, record the length of the
43182cd038dSYoshinobu Inoue 	 * unfragmentable part and the next header of the fragment header.
43282cd038dSYoshinobu Inoue 	 */
43382cd038dSYoshinobu Inoue 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
43482cd038dSYoshinobu Inoue 	if (fragoff == 0) {
43506cd0a3fSHajimu UMEMOTO 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
43606cd0a3fSHajimu UMEMOTO 		    sizeof(struct ip6_frag);
43782cd038dSYoshinobu Inoue 		q6->ip6q_nxt = ip6f->ip6f_nxt;
43882cd038dSYoshinobu Inoue 	}
43982cd038dSYoshinobu Inoue 
44082cd038dSYoshinobu Inoue 	/*
44182cd038dSYoshinobu Inoue 	 * Check that the reassembled packet would not exceed 65535 bytes
44282cd038dSYoshinobu Inoue 	 * in size.
44382cd038dSYoshinobu Inoue 	 * If it would exceed, discard the fragment and return an ICMP error.
44482cd038dSYoshinobu Inoue 	 */
44582cd038dSYoshinobu Inoue 	if (q6->ip6q_unfrglen >= 0) {
44682cd038dSYoshinobu Inoue 		/* The 1st fragment has already arrived. */
44782cd038dSYoshinobu Inoue 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
44882cd038dSYoshinobu Inoue 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
449686cdd19SJun-ichiro itojun Hagino 			    offset - sizeof(struct ip6_frag) +
450686cdd19SJun-ichiro itojun Hagino 			    offsetof(struct ip6_frag, ip6f_offlg));
45180d7a853SJonathan T. Looney 			IP6Q_UNLOCK(hash);
45282cd038dSYoshinobu Inoue 			return (IPPROTO_DONE);
45382cd038dSYoshinobu Inoue 		}
45406cd0a3fSHajimu UMEMOTO 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
45582cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
456686cdd19SJun-ichiro itojun Hagino 		    offset - sizeof(struct ip6_frag) +
457686cdd19SJun-ichiro itojun Hagino 		    offsetof(struct ip6_frag, ip6f_offlg));
45880d7a853SJonathan T. Looney 		IP6Q_UNLOCK(hash);
45982cd038dSYoshinobu Inoue 		return (IPPROTO_DONE);
46082cd038dSYoshinobu Inoue 	}
46182cd038dSYoshinobu Inoue 	/*
46282cd038dSYoshinobu Inoue 	 * If it's the first fragment, do the above check for each
46382cd038dSYoshinobu Inoue 	 * fragment already stored in the reassembly queue.
46482cd038dSYoshinobu Inoue 	 */
46582cd038dSYoshinobu Inoue 	if (fragoff == 0) {
46682cd038dSYoshinobu Inoue 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
46782cd038dSYoshinobu Inoue 		     af6 = af6dwn) {
46882cd038dSYoshinobu Inoue 			af6dwn = af6->ip6af_down;
46982cd038dSYoshinobu Inoue 
47082cd038dSYoshinobu Inoue 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
47182cd038dSYoshinobu Inoue 			    IPV6_MAXPACKET) {
47282cd038dSYoshinobu Inoue 				struct mbuf *merr = IP6_REASS_MBUF(af6);
47382cd038dSYoshinobu Inoue 				struct ip6_hdr *ip6err;
47482cd038dSYoshinobu Inoue 				int erroff = af6->ip6af_offset;
47582cd038dSYoshinobu Inoue 
47682cd038dSYoshinobu Inoue 				/* dequeue the fragment. */
47780d7a853SJonathan T. Looney 				frag6_deq(af6, hash);
478487a161cSBjoern A. Zeeb 				free(af6, M_FRAG6);
47982cd038dSYoshinobu Inoue 
48082cd038dSYoshinobu Inoue 				/* adjust pointer. */
48182cd038dSYoshinobu Inoue 				ip6err = mtod(merr, struct ip6_hdr *);
48282cd038dSYoshinobu Inoue 
48382cd038dSYoshinobu Inoue 				/*
48482cd038dSYoshinobu Inoue 				 * Restore source and destination addresses
48582cd038dSYoshinobu Inoue 				 * in the erroneous IPv6 header.
48682cd038dSYoshinobu Inoue 				 */
48782cd038dSYoshinobu Inoue 				ip6err->ip6_src = q6->ip6q_src;
48882cd038dSYoshinobu Inoue 				ip6err->ip6_dst = q6->ip6q_dst;
48982cd038dSYoshinobu Inoue 
49082cd038dSYoshinobu Inoue 				icmp6_error(merr, ICMP6_PARAM_PROB,
49182cd038dSYoshinobu Inoue 				    ICMP6_PARAMPROB_HEADER,
492686cdd19SJun-ichiro itojun Hagino 				    erroff - sizeof(struct ip6_frag) +
493686cdd19SJun-ichiro itojun Hagino 				    offsetof(struct ip6_frag, ip6f_offlg));
49482cd038dSYoshinobu Inoue 			}
49582cd038dSYoshinobu Inoue 		}
49682cd038dSYoshinobu Inoue 	}
49782cd038dSYoshinobu Inoue 
498487a161cSBjoern A. Zeeb 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6,
499487a161cSBjoern A. Zeeb 	    M_NOWAIT | M_ZERO);
500686cdd19SJun-ichiro itojun Hagino 	if (ip6af == NULL)
501686cdd19SJun-ichiro itojun Hagino 		goto dropfrag;
50282cd038dSYoshinobu Inoue 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
50382cd038dSYoshinobu Inoue 	ip6af->ip6af_off = fragoff;
50482cd038dSYoshinobu Inoue 	ip6af->ip6af_frglen = frgpartlen;
50582cd038dSYoshinobu Inoue 	ip6af->ip6af_offset = offset;
50682cd038dSYoshinobu Inoue 	IP6_REASS_MBUF(ip6af) = m;
50782cd038dSYoshinobu Inoue 
50882cd038dSYoshinobu Inoue 	if (first_frag) {
50982cd038dSYoshinobu Inoue 		af6 = (struct ip6asfrag *)q6;
51082cd038dSYoshinobu Inoue 		goto insert;
51182cd038dSYoshinobu Inoue 	}
51282cd038dSYoshinobu Inoue 
51382cd038dSYoshinobu Inoue 	/*
51459dfcba4SHajimu UMEMOTO 	 * Handle ECN by comparing this segment with the first one;
51559dfcba4SHajimu UMEMOTO 	 * if CE is set, do not lose CE.
51659dfcba4SHajimu UMEMOTO 	 * drop if CE and not-ECT are mixed for the same packet.
51759dfcba4SHajimu UMEMOTO 	 */
51859dfcba4SHajimu UMEMOTO 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
5195e9510e3SJINMEI Tatuya 	ecn0 = q6->ip6q_ecn;
52059dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_CE) {
52159dfcba4SHajimu UMEMOTO 		if (ecn0 == IPTOS_ECN_NOTECT) {
522487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
52359dfcba4SHajimu UMEMOTO 			goto dropfrag;
52459dfcba4SHajimu UMEMOTO 		}
52559dfcba4SHajimu UMEMOTO 		if (ecn0 != IPTOS_ECN_CE)
5265e9510e3SJINMEI Tatuya 			q6->ip6q_ecn = IPTOS_ECN_CE;
52759dfcba4SHajimu UMEMOTO 	}
52859dfcba4SHajimu UMEMOTO 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
529487a161cSBjoern A. Zeeb 		free(ip6af, M_FRAG6);
53059dfcba4SHajimu UMEMOTO 		goto dropfrag;
53159dfcba4SHajimu UMEMOTO 	}
53259dfcba4SHajimu UMEMOTO 
53359dfcba4SHajimu UMEMOTO 	/*
53482cd038dSYoshinobu Inoue 	 * Find a segment which begins after this one does.
53582cd038dSYoshinobu Inoue 	 */
53682cd038dSYoshinobu Inoue 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
53782cd038dSYoshinobu Inoue 	     af6 = af6->ip6af_down)
53882cd038dSYoshinobu Inoue 		if (af6->ip6af_off > ip6af->ip6af_off)
53982cd038dSYoshinobu Inoue 			break;
54082cd038dSYoshinobu Inoue 
54182cd038dSYoshinobu Inoue 	/*
54282cd038dSYoshinobu Inoue 	 * If the incoming framgent overlaps some existing fragments in
54382cd038dSYoshinobu Inoue 	 * the reassembly queue, drop it, since it is dangerous to override
54482cd038dSYoshinobu Inoue 	 * existing fragments from a security point of view.
5459888c401SHajimu UMEMOTO 	 * We don't know which fragment is the bad guy - here we trust
5469888c401SHajimu UMEMOTO 	 * fragment that came in earlier, with no real reason.
5475e9510e3SJINMEI Tatuya 	 *
5485e9510e3SJINMEI Tatuya 	 * Note: due to changes after disabling this part, mbuf passed to
5495e9510e3SJINMEI Tatuya 	 * m_adj() below now does not meet the requirement.
55082cd038dSYoshinobu Inoue 	 */
55182cd038dSYoshinobu Inoue 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
55282cd038dSYoshinobu Inoue 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
55382cd038dSYoshinobu Inoue 			- ip6af->ip6af_off;
55482cd038dSYoshinobu Inoue 		if (i > 0) {
555487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
55682cd038dSYoshinobu Inoue 			goto dropfrag;
55782cd038dSYoshinobu Inoue 		}
55882cd038dSYoshinobu Inoue 	}
55982cd038dSYoshinobu Inoue 	if (af6 != (struct ip6asfrag *)q6) {
56082cd038dSYoshinobu Inoue 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
56182cd038dSYoshinobu Inoue 		if (i > 0) {
562487a161cSBjoern A. Zeeb 			free(ip6af, M_FRAG6);
56382cd038dSYoshinobu Inoue 			goto dropfrag;
56482cd038dSYoshinobu Inoue 		}
56582cd038dSYoshinobu Inoue 	}
56682cd038dSYoshinobu Inoue 
56782cd038dSYoshinobu Inoue insert:
5684b908c8bSRobert Watson #ifdef MAC
5694b908c8bSRobert Watson 	if (!first_frag)
5704b908c8bSRobert Watson 		mac_ip6q_update(m, q6);
5714b908c8bSRobert Watson #endif
57282cd038dSYoshinobu Inoue 
57382cd038dSYoshinobu Inoue 	/*
57482cd038dSYoshinobu Inoue 	 * Stick new segment in its place;
57582cd038dSYoshinobu Inoue 	 * check for complete reassembly.
57603c99d76SJonathan T. Looney 	 * If not complete, check fragment limit.
57782cd038dSYoshinobu Inoue 	 * Move to front of packet queue, as we are
57882cd038dSYoshinobu Inoue 	 * the most recently active fragmented packet.
57982cd038dSYoshinobu Inoue 	 */
58080d7a853SJonathan T. Looney 	frag6_enq(ip6af, af6->ip6af_up, hash);
5812adfd64fSJonathan T. Looney 	atomic_add_int(&frag6_nfrags, 1);
5829888c401SHajimu UMEMOTO 	q6->ip6q_nfrag++;
58382cd038dSYoshinobu Inoue 	next = 0;
58482cd038dSYoshinobu Inoue 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
58582cd038dSYoshinobu Inoue 	     af6 = af6->ip6af_down) {
58682cd038dSYoshinobu Inoue 		if (af6->ip6af_off != next) {
58703c99d76SJonathan T. Looney 			if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
588198fdaedSTom Jones 				IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
5896bbdbbb8SHans Petter Selasky 				frag6_freef(q6, hash);
59003c99d76SJonathan T. Looney 			}
59180d7a853SJonathan T. Looney 			IP6Q_UNLOCK(hash);
59282cd038dSYoshinobu Inoue 			return IPPROTO_DONE;
59382cd038dSYoshinobu Inoue 		}
59482cd038dSYoshinobu Inoue 		next += af6->ip6af_frglen;
59582cd038dSYoshinobu Inoue 	}
59682cd038dSYoshinobu Inoue 	if (af6->ip6af_up->ip6af_mff) {
59703c99d76SJonathan T. Looney 		if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
598198fdaedSTom Jones 			IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
5996bbdbbb8SHans Petter Selasky 			frag6_freef(q6, hash);
60003c99d76SJonathan T. Looney 		}
60180d7a853SJonathan T. Looney 		IP6Q_UNLOCK(hash);
60282cd038dSYoshinobu Inoue 		return IPPROTO_DONE;
60382cd038dSYoshinobu Inoue 	}
60482cd038dSYoshinobu Inoue 
60582cd038dSYoshinobu Inoue 	/*
60682cd038dSYoshinobu Inoue 	 * Reassembly is complete; concatenate fragments.
60782cd038dSYoshinobu Inoue 	 */
60882cd038dSYoshinobu Inoue 	ip6af = q6->ip6q_down;
60982cd038dSYoshinobu Inoue 	t = m = IP6_REASS_MBUF(ip6af);
61082cd038dSYoshinobu Inoue 	af6 = ip6af->ip6af_down;
61180d7a853SJonathan T. Looney 	frag6_deq(ip6af, hash);
61282cd038dSYoshinobu Inoue 	while (af6 != (struct ip6asfrag *)q6) {
6139907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_flags &=
6149907aba3SAndrey V. Elsukov 		    IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
6159907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_data +=
6169907aba3SAndrey V. Elsukov 		    IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
6179907aba3SAndrey V. Elsukov 
618686cdd19SJun-ichiro itojun Hagino 		af6dwn = af6->ip6af_down;
61980d7a853SJonathan T. Looney 		frag6_deq(af6, hash);
62082cd038dSYoshinobu Inoue 		while (t->m_next)
62182cd038dSYoshinobu Inoue 			t = t->m_next;
622ba99cc0bSAlexander V. Chernikov 		m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
62309b0b8c0SNavdeep Parhar 		m_demote_pkthdr(IP6_REASS_MBUF(af6));
624ba99cc0bSAlexander V. Chernikov 		m_cat(t, IP6_REASS_MBUF(af6));
625487a161cSBjoern A. Zeeb 		free(af6, M_FRAG6);
626686cdd19SJun-ichiro itojun Hagino 		af6 = af6dwn;
62782cd038dSYoshinobu Inoue 	}
62882cd038dSYoshinobu Inoue 
6299907aba3SAndrey V. Elsukov 	while (m->m_pkthdr.csum_data & 0xffff0000)
6309907aba3SAndrey V. Elsukov 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
6319907aba3SAndrey V. Elsukov 		    (m->m_pkthdr.csum_data >> 16);
6329907aba3SAndrey V. Elsukov 
63382cd038dSYoshinobu Inoue 	/* adjust offset to point where the original next header starts */
63482cd038dSYoshinobu Inoue 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
635487a161cSBjoern A. Zeeb 	free(ip6af, M_FRAG6);
636686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
63782cd038dSYoshinobu Inoue 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
6385e9510e3SJINMEI Tatuya 	if (q6->ip6q_ecn == IPTOS_ECN_CE)
6395e9510e3SJINMEI Tatuya 		ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
64082cd038dSYoshinobu Inoue 	nxt = q6->ip6q_nxt;
64182cd038dSYoshinobu Inoue 
6420b438b0fSGleb Smirnoff 	if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
64380d7a853SJonathan T. Looney 		frag6_remque(q6, hash);
6442adfd64fSJonathan T. Looney 		atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
6454b908c8bSRobert Watson #ifdef MAC
6464b908c8bSRobert Watson 		mac_ip6q_destroy(q6);
6474b908c8bSRobert Watson #endif
648487a161cSBjoern A. Zeeb 		free(q6, M_FRAG6);
64980d7a853SJonathan T. Looney 		atomic_subtract_int(&V_frag6_nfragpackets, 1);
6500b438b0fSGleb Smirnoff 
651686cdd19SJun-ichiro itojun Hagino 		goto dropfrag;
65282cd038dSYoshinobu Inoue 	}
65382cd038dSYoshinobu Inoue 
65482cd038dSYoshinobu Inoue 	/*
65582cd038dSYoshinobu Inoue 	 * Store NXT to the original.
65682cd038dSYoshinobu Inoue 	 */
65768e0e5a6SAndrey V. Elsukov 	m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
65868e0e5a6SAndrey V. Elsukov 	    (caddr_t)&nxt);
65982cd038dSYoshinobu Inoue 
66080d7a853SJonathan T. Looney 	frag6_remque(q6, hash);
6612adfd64fSJonathan T. Looney 	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
6624b908c8bSRobert Watson #ifdef MAC
6634b908c8bSRobert Watson 	mac_ip6q_reassemble(q6, m);
6644b908c8bSRobert Watson 	mac_ip6q_destroy(q6);
6654b908c8bSRobert Watson #endif
666487a161cSBjoern A. Zeeb 	free(q6, M_FRAG6);
66780d7a853SJonathan T. Looney 	atomic_subtract_int(&V_frag6_nfragpackets, 1);
66882cd038dSYoshinobu Inoue 
66982cd038dSYoshinobu Inoue 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
67082cd038dSYoshinobu Inoue 		int plen = 0;
67182cd038dSYoshinobu Inoue 		for (t = m; t; t = t->m_next)
67282cd038dSYoshinobu Inoue 			plen += t->m_len;
67382cd038dSYoshinobu Inoue 		m->m_pkthdr.len = plen;
67482cd038dSYoshinobu Inoue 	}
67582cd038dSYoshinobu Inoue 
676aaa46574SAdrian Chadd #ifdef RSS
677aaa46574SAdrian Chadd 	mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
678aaa46574SAdrian Chadd 	    M_NOWAIT);
679aaa46574SAdrian Chadd 	if (mtag == NULL)
680aaa46574SAdrian Chadd 		goto dropfrag;
681aaa46574SAdrian Chadd 
682aaa46574SAdrian Chadd 	ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
683aaa46574SAdrian Chadd 	ip6dc->ip6dc_nxt = nxt;
684aaa46574SAdrian Chadd 	ip6dc->ip6dc_off = offset;
685aaa46574SAdrian Chadd 
686aaa46574SAdrian Chadd 	m_tag_prepend(m, mtag);
687aaa46574SAdrian Chadd #endif
688aaa46574SAdrian Chadd 
68980d7a853SJonathan T. Looney 	IP6Q_UNLOCK(hash);
6909cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_reassembled);
69182cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
69282cd038dSYoshinobu Inoue 
693aaa46574SAdrian Chadd #ifdef RSS
694aaa46574SAdrian Chadd 	/*
695aaa46574SAdrian Chadd 	 * Queue/dispatch for reprocessing.
696aaa46574SAdrian Chadd 	 */
697aaa46574SAdrian Chadd 	netisr_dispatch(NETISR_IPV6_DIRECT, m);
698aaa46574SAdrian Chadd 	return IPPROTO_DONE;
699aaa46574SAdrian Chadd #endif
700aaa46574SAdrian Chadd 
70182cd038dSYoshinobu Inoue 	/*
70282cd038dSYoshinobu Inoue 	 * Tell launch routine the next header
70382cd038dSYoshinobu Inoue 	 */
70482cd038dSYoshinobu Inoue 
70582cd038dSYoshinobu Inoue 	*mp = m;
70682cd038dSYoshinobu Inoue 	*offp = offset;
70782cd038dSYoshinobu Inoue 
70882cd038dSYoshinobu Inoue 	return nxt;
70982cd038dSYoshinobu Inoue 
71082cd038dSYoshinobu Inoue  dropfrag:
71180d7a853SJonathan T. Looney 	IP6Q_UNLOCK(hash);
71282cd038dSYoshinobu Inoue 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
7139cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_fragdropped);
71482cd038dSYoshinobu Inoue 	m_freem(m);
71582cd038dSYoshinobu Inoue 	return IPPROTO_DONE;
71682cd038dSYoshinobu Inoue }
71782cd038dSYoshinobu Inoue 
71882cd038dSYoshinobu Inoue /*
71982cd038dSYoshinobu Inoue  * Free a fragment reassembly header and all
72082cd038dSYoshinobu Inoue  * associated datagrams.
72182cd038dSYoshinobu Inoue  */
72280d7a853SJonathan T. Looney static void
7236bbdbbb8SHans Petter Selasky frag6_freef(struct ip6q *q6, uint32_t bucket)
72482cd038dSYoshinobu Inoue {
72582cd038dSYoshinobu Inoue 	struct ip6asfrag *af6, *down6;
72682cd038dSYoshinobu Inoue 
72780d7a853SJonathan T. Looney 	IP6Q_LOCK_ASSERT(bucket);
7289888c401SHajimu UMEMOTO 
72982cd038dSYoshinobu Inoue 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
73082cd038dSYoshinobu Inoue 	     af6 = down6) {
73182cd038dSYoshinobu Inoue 		struct mbuf *m = IP6_REASS_MBUF(af6);
73282cd038dSYoshinobu Inoue 
73382cd038dSYoshinobu Inoue 		down6 = af6->ip6af_down;
73480d7a853SJonathan T. Looney 		frag6_deq(af6, bucket);
73582cd038dSYoshinobu Inoue 
73682cd038dSYoshinobu Inoue 		/*
73782cd038dSYoshinobu Inoue 		 * Return ICMP time exceeded error for the 1st fragment.
73882cd038dSYoshinobu Inoue 		 * Just free other fragments.
73982cd038dSYoshinobu Inoue 		 */
7406bbdbbb8SHans Petter Selasky 		if (af6->ip6af_off == 0) {
74182cd038dSYoshinobu Inoue 			struct ip6_hdr *ip6;
74282cd038dSYoshinobu Inoue 
74382cd038dSYoshinobu Inoue 			/* adjust pointer */
74482cd038dSYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
74582cd038dSYoshinobu Inoue 
74606cd0a3fSHajimu UMEMOTO 			/* restore source and destination addresses */
74782cd038dSYoshinobu Inoue 			ip6->ip6_src = q6->ip6q_src;
74882cd038dSYoshinobu Inoue 			ip6->ip6_dst = q6->ip6q_dst;
74982cd038dSYoshinobu Inoue 
75082cd038dSYoshinobu Inoue 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
75182cd038dSYoshinobu Inoue 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
752686cdd19SJun-ichiro itojun Hagino 		} else
75382cd038dSYoshinobu Inoue 			m_freem(m);
754487a161cSBjoern A. Zeeb 		free(af6, M_FRAG6);
75582cd038dSYoshinobu Inoue 	}
75680d7a853SJonathan T. Looney 	frag6_remque(q6, bucket);
7572adfd64fSJonathan T. Looney 	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
7584b908c8bSRobert Watson #ifdef MAC
7594b908c8bSRobert Watson 	mac_ip6q_destroy(q6);
7604b908c8bSRobert Watson #endif
761487a161cSBjoern A. Zeeb 	free(q6, M_FRAG6);
76280d7a853SJonathan T. Looney 	atomic_subtract_int(&V_frag6_nfragpackets, 1);
76382cd038dSYoshinobu Inoue }
76482cd038dSYoshinobu Inoue 
76582cd038dSYoshinobu Inoue /*
76682cd038dSYoshinobu Inoue  * Put an ip fragment on a reassembly chain.
76782cd038dSYoshinobu Inoue  * Like insque, but pointers in middle of structure.
76882cd038dSYoshinobu Inoue  */
76980d7a853SJonathan T. Looney static void
77080d7a853SJonathan T. Looney frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
77180d7a853SJonathan T. Looney     uint32_t bucket __unused)
77282cd038dSYoshinobu Inoue {
7739888c401SHajimu UMEMOTO 
77480d7a853SJonathan T. Looney 	IP6Q_LOCK_ASSERT(bucket);
7759888c401SHajimu UMEMOTO 
77682cd038dSYoshinobu Inoue 	af6->ip6af_up = up6;
77782cd038dSYoshinobu Inoue 	af6->ip6af_down = up6->ip6af_down;
77882cd038dSYoshinobu Inoue 	up6->ip6af_down->ip6af_up = af6;
77982cd038dSYoshinobu Inoue 	up6->ip6af_down = af6;
78082cd038dSYoshinobu Inoue }
78182cd038dSYoshinobu Inoue 
78282cd038dSYoshinobu Inoue /*
78382cd038dSYoshinobu Inoue  * To frag6_enq as remque is to insque.
78482cd038dSYoshinobu Inoue  */
78580d7a853SJonathan T. Looney static void
78680d7a853SJonathan T. Looney frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
78782cd038dSYoshinobu Inoue {
7889888c401SHajimu UMEMOTO 
78980d7a853SJonathan T. Looney 	IP6Q_LOCK_ASSERT(bucket);
7909888c401SHajimu UMEMOTO 
79182cd038dSYoshinobu Inoue 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
79282cd038dSYoshinobu Inoue 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
79382cd038dSYoshinobu Inoue }
79482cd038dSYoshinobu Inoue 
79580d7a853SJonathan T. Looney static void
7961e9f3b73SJonathan T. Looney frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
79782cd038dSYoshinobu Inoue {
7989888c401SHajimu UMEMOTO 
79980d7a853SJonathan T. Looney 	IP6Q_LOCK_ASSERT(bucket);
80080d7a853SJonathan T. Looney 	KASSERT(IP6Q_HEAD(bucket) == old,
80180d7a853SJonathan T. Looney 	    ("%s: attempt to insert at head of wrong bucket"
80280d7a853SJonathan T. Looney 	    " (bucket=%u, old=%p)", __func__, bucket, old));
8039888c401SHajimu UMEMOTO 
80482cd038dSYoshinobu Inoue 	new->ip6q_prev = old;
80582cd038dSYoshinobu Inoue 	new->ip6q_next = old->ip6q_next;
80682cd038dSYoshinobu Inoue 	old->ip6q_next->ip6q_prev= new;
80782cd038dSYoshinobu Inoue 	old->ip6q_next = new;
8081e9f3b73SJonathan T. Looney 	V_ip6q[bucket].count++;
80982cd038dSYoshinobu Inoue }
81082cd038dSYoshinobu Inoue 
81180d7a853SJonathan T. Looney static void
8121e9f3b73SJonathan T. Looney frag6_remque(struct ip6q *p6, uint32_t bucket)
81382cd038dSYoshinobu Inoue {
8149888c401SHajimu UMEMOTO 
81580d7a853SJonathan T. Looney 	IP6Q_LOCK_ASSERT(bucket);
8169888c401SHajimu UMEMOTO 
81782cd038dSYoshinobu Inoue 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
81882cd038dSYoshinobu Inoue 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
8191e9f3b73SJonathan T. Looney 	V_ip6q[bucket].count--;
82082cd038dSYoshinobu Inoue }
82182cd038dSYoshinobu Inoue 
82282cd038dSYoshinobu Inoue /*
82333841545SHajimu UMEMOTO  * IPv6 reassembling timer processing;
82482cd038dSYoshinobu Inoue  * if a timer expires on a reassembly
82582cd038dSYoshinobu Inoue  * queue, discard it.
82682cd038dSYoshinobu Inoue  */
82782cd038dSYoshinobu Inoue void
8281272577eSXin LI frag6_slowtimo(void)
82982cd038dSYoshinobu Inoue {
8308b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
83180d7a853SJonathan T. Looney 	struct ip6q *head, *q6;
83280d7a853SJonathan T. Looney 	int i;
83382cd038dSYoshinobu Inoue 
8345ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
8358b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
8368b615593SMarko Zec 		CURVNET_SET(vnet_iter);
83780d7a853SJonathan T. Looney 		for (i = 0; i < IP6REASS_NHASH; i++) {
83880d7a853SJonathan T. Looney 			IP6Q_LOCK(i);
83980d7a853SJonathan T. Looney 			head = IP6Q_HEAD(i);
84080d7a853SJonathan T. Looney 			q6 = head->ip6q_next;
8411e9f3b73SJonathan T. Looney 			if (q6 == NULL) {
8421e9f3b73SJonathan T. Looney 				/*
8431e9f3b73SJonathan T. Looney 				 * XXXJTL: This should never happen. This
8441e9f3b73SJonathan T. Looney 				 * should turn into an assertion.
8451e9f3b73SJonathan T. Looney 				 */
8461e9f3b73SJonathan T. Looney 				IP6Q_UNLOCK(i);
8471e9f3b73SJonathan T. Looney 				continue;
8481e9f3b73SJonathan T. Looney 			}
84980d7a853SJonathan T. Looney 			while (q6 != head) {
85082cd038dSYoshinobu Inoue 				--q6->ip6q_ttl;
85182cd038dSYoshinobu Inoue 				q6 = q6->ip6q_next;
85282cd038dSYoshinobu Inoue 				if (q6->ip6q_prev->ip6q_ttl == 0) {
853198fdaedSTom Jones 					IP6STAT_ADD(ip6s_fragtimeout,
854198fdaedSTom Jones 						q6->ip6q_prev->ip6q_nfrag);
85582cd038dSYoshinobu Inoue 					/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
8566bbdbbb8SHans Petter Selasky 					frag6_freef(q6->ip6q_prev, i);
85782cd038dSYoshinobu Inoue 				}
85882cd038dSYoshinobu Inoue 			}
85982cd038dSYoshinobu Inoue 			/*
86082cd038dSYoshinobu Inoue 			 * If we are over the maximum number of fragments
86182cd038dSYoshinobu Inoue 			 * (due to the limit being lowered), drain off
86282cd038dSYoshinobu Inoue 			 * enough to get down to the new limit.
8631e9f3b73SJonathan T. Looney 			 * Note that we drain all reassembly queues if
8641e9f3b73SJonathan T. Looney 			 * maxfragpackets is 0 (fragmentation is disabled),
8651e9f3b73SJonathan T. Looney 			 * and don't enforce a limit when maxfragpackets
8661e9f3b73SJonathan T. Looney 			 * is negative.
86782cd038dSYoshinobu Inoue 			 */
8681e9f3b73SJonathan T. Looney 			while ((V_ip6_maxfragpackets == 0 ||
8691e9f3b73SJonathan T. Looney 			    (V_ip6_maxfragpackets > 0 &&
8701e9f3b73SJonathan T. Looney 			    V_ip6q[i].count > V_ip6_maxfragbucketsize)) &&
87180d7a853SJonathan T. Looney 			    head->ip6q_prev != head) {
872198fdaedSTom Jones 				IP6STAT_ADD(ip6s_fragoverflow,
873198fdaedSTom Jones 					q6->ip6q_prev->ip6q_nfrag);
87482cd038dSYoshinobu Inoue 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
8756bbdbbb8SHans Petter Selasky 				frag6_freef(head->ip6q_prev, i);
87680d7a853SJonathan T. Looney 			}
87780d7a853SJonathan T. Looney 			IP6Q_UNLOCK(i);
87882cd038dSYoshinobu Inoue 		}
8791e9f3b73SJonathan T. Looney 		/*
8801e9f3b73SJonathan T. Looney 		 * If we are still over the maximum number of fragmented
8811e9f3b73SJonathan T. Looney 		 * packets, drain off enough to get down to the new limit.
8821e9f3b73SJonathan T. Looney 		 */
8831e9f3b73SJonathan T. Looney 		i = 0;
8841e9f3b73SJonathan T. Looney 		while (V_ip6_maxfragpackets >= 0 &&
8851e9f3b73SJonathan T. Looney 		    atomic_load_int(&V_frag6_nfragpackets) >
8861e9f3b73SJonathan T. Looney 		    (u_int)V_ip6_maxfragpackets) {
8871e9f3b73SJonathan T. Looney 			IP6Q_LOCK(i);
8881e9f3b73SJonathan T. Looney 			head = IP6Q_HEAD(i);
8891e9f3b73SJonathan T. Looney 			if (head->ip6q_prev != head) {
890198fdaedSTom Jones 				IP6STAT_ADD(ip6s_fragoverflow,
891198fdaedSTom Jones 					q6->ip6q_prev->ip6q_nfrag);
8921e9f3b73SJonathan T. Looney 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
8936bbdbbb8SHans Petter Selasky 				frag6_freef(head->ip6q_prev, i);
8941e9f3b73SJonathan T. Looney 			}
8951e9f3b73SJonathan T. Looney 			IP6Q_UNLOCK(i);
8961e9f3b73SJonathan T. Looney 			i = (i + 1) % IP6REASS_NHASH;
8971e9f3b73SJonathan T. Looney 		}
8988b615593SMarko Zec 		CURVNET_RESTORE();
8998b615593SMarko Zec 	}
9005ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
90182cd038dSYoshinobu Inoue }
90282cd038dSYoshinobu Inoue 
90382cd038dSYoshinobu Inoue /*
90482cd038dSYoshinobu Inoue  * Drain off all datagram fragments.
90582cd038dSYoshinobu Inoue  */
90682cd038dSYoshinobu Inoue void
9071272577eSXin LI frag6_drain(void)
90882cd038dSYoshinobu Inoue {
9098b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
91080d7a853SJonathan T. Looney 	struct ip6q *head;
91180d7a853SJonathan T. Looney 	int i;
9129888c401SHajimu UMEMOTO 
9135ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
9148b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
9158b615593SMarko Zec 		CURVNET_SET(vnet_iter);
91680d7a853SJonathan T. Looney 		for (i = 0; i < IP6REASS_NHASH; i++) {
91780d7a853SJonathan T. Looney 			if (IP6Q_TRYLOCK(i) == 0)
91880d7a853SJonathan T. Looney 				continue;
91980d7a853SJonathan T. Looney 			head = IP6Q_HEAD(i);
92080d7a853SJonathan T. Looney 			while (head->ip6q_next != head) {
9219cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_fragdropped);
92282cd038dSYoshinobu Inoue 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
9236bbdbbb8SHans Petter Selasky 				frag6_freef(head->ip6q_next, i);
92480d7a853SJonathan T. Looney 			}
92580d7a853SJonathan T. Looney 			IP6Q_UNLOCK(i);
92682cd038dSYoshinobu Inoue 		}
9278b615593SMarko Zec 		CURVNET_RESTORE();
9288b615593SMarko Zec 	}
9295ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
93082cd038dSYoshinobu Inoue }
931e5ee7060SGleb Smirnoff 
932e5ee7060SGleb Smirnoff int
933e5ee7060SGleb Smirnoff ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
934e5ee7060SGleb Smirnoff {
935e5ee7060SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
936e5ee7060SGleb Smirnoff 	struct mbuf *t;
937e5ee7060SGleb Smirnoff 
938e5ee7060SGleb Smirnoff 	/* Delete frag6 header. */
939e5ee7060SGleb Smirnoff 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
940e5ee7060SGleb Smirnoff 		/* This is the only possible case with !PULLDOWN_TEST. */
941e5ee7060SGleb Smirnoff 		bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
942e5ee7060SGleb Smirnoff 		    offset);
943e5ee7060SGleb Smirnoff 		m->m_data += sizeof(struct ip6_frag);
944e5ee7060SGleb Smirnoff 		m->m_len -= sizeof(struct ip6_frag);
945e5ee7060SGleb Smirnoff 	} else {
946e5ee7060SGleb Smirnoff 		/* This comes with no copy if the boundary is on cluster. */
947e5ee7060SGleb Smirnoff 		if ((t = m_split(m, offset, wait)) == NULL)
948e5ee7060SGleb Smirnoff 			return (ENOMEM);
949e5ee7060SGleb Smirnoff 		m_adj(t, sizeof(struct ip6_frag));
950e5ee7060SGleb Smirnoff 		m_cat(m, t);
951e5ee7060SGleb Smirnoff 	}
952e5ee7060SGleb Smirnoff 
953a4061289SAndrey V. Elsukov 	m->m_flags |= M_FRAGMENTED;
954e5ee7060SGleb Smirnoff 	return (0);
955e5ee7060SGleb Smirnoff }
956