xref: /freebsd/sys/netinet/ip_reass.c (revision a55383e7)
11dbefcc0SGleb Smirnoff /*-
21dbefcc0SGleb Smirnoff  * Copyright (c) 2015 Gleb Smirnoff <glebius@FreeBSD.org>
31dbefcc0SGleb Smirnoff  * Copyright (c) 2015 Adrian Chadd <adrian@FreeBSD.org>
41dbefcc0SGleb Smirnoff  * Copyright (c) 1982, 1986, 1988, 1993
51dbefcc0SGleb Smirnoff  *	The Regents of the University of California.  All rights reserved.
61dbefcc0SGleb Smirnoff  *
71dbefcc0SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
81dbefcc0SGleb Smirnoff  * modification, are permitted provided that the following conditions
91dbefcc0SGleb Smirnoff  * are met:
101dbefcc0SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
111dbefcc0SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
121dbefcc0SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
131dbefcc0SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
141dbefcc0SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
161dbefcc0SGleb Smirnoff  *    may be used to endorse or promote products derived from this software
171dbefcc0SGleb Smirnoff  *    without specific prior written permission.
181dbefcc0SGleb Smirnoff  *
191dbefcc0SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201dbefcc0SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211dbefcc0SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221dbefcc0SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231dbefcc0SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241dbefcc0SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251dbefcc0SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261dbefcc0SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271dbefcc0SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281dbefcc0SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291dbefcc0SGleb Smirnoff  * SUCH DAMAGE.
301dbefcc0SGleb Smirnoff  *
311dbefcc0SGleb Smirnoff  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
321dbefcc0SGleb Smirnoff  */
331dbefcc0SGleb Smirnoff 
341dbefcc0SGleb Smirnoff #include <sys/cdefs.h>
351dbefcc0SGleb Smirnoff __FBSDID("$FreeBSD$");
361dbefcc0SGleb Smirnoff 
371dbefcc0SGleb Smirnoff #include "opt_rss.h"
381dbefcc0SGleb Smirnoff 
391dbefcc0SGleb Smirnoff #include <sys/param.h>
401dbefcc0SGleb Smirnoff #include <sys/systm.h>
411dbefcc0SGleb Smirnoff #include <sys/eventhandler.h>
4204f44499SHans Petter Selasky #include <sys/kernel.h>
43c047fd1bSGleb Smirnoff #include <sys/hash.h>
441dbefcc0SGleb Smirnoff #include <sys/mbuf.h>
451dbefcc0SGleb Smirnoff #include <sys/malloc.h>
46ff790bbaSJonathan T. Looney #include <sys/limits.h>
471dbefcc0SGleb Smirnoff #include <sys/lock.h>
481dbefcc0SGleb Smirnoff #include <sys/mutex.h>
491dbefcc0SGleb Smirnoff #include <sys/sysctl.h>
50a55383e7SHans Petter Selasky #include <sys/socket.h>
511dbefcc0SGleb Smirnoff 
52a55383e7SHans Petter Selasky #include <net/if.h>
53a55383e7SHans Petter Selasky #include <net/if_var.h>
541dbefcc0SGleb Smirnoff #include <net/rss_config.h>
553e217461SAdrian Chadd #include <net/netisr.h>
561dbefcc0SGleb Smirnoff #include <net/vnet.h>
571dbefcc0SGleb Smirnoff 
581dbefcc0SGleb Smirnoff #include <netinet/in.h>
591dbefcc0SGleb Smirnoff #include <netinet/ip.h>
601dbefcc0SGleb Smirnoff #include <netinet/ip_var.h>
611dbefcc0SGleb Smirnoff #include <netinet/in_rss.h>
621dbefcc0SGleb Smirnoff #ifdef MAC
631dbefcc0SGleb Smirnoff #include <security/mac/mac_framework.h>
641dbefcc0SGleb Smirnoff #endif
651dbefcc0SGleb Smirnoff 
661dbefcc0SGleb Smirnoff SYSCTL_DECL(_net_inet_ip);
671dbefcc0SGleb Smirnoff 
681dbefcc0SGleb Smirnoff /*
691dbefcc0SGleb Smirnoff  * Reassembly headers are stored in hash buckets.
701dbefcc0SGleb Smirnoff  */
71a967df1cSJonathan T. Looney #define	IPREASS_NHASH_LOG2	10
721dbefcc0SGleb Smirnoff #define	IPREASS_NHASH		(1 << IPREASS_NHASH_LOG2)
731dbefcc0SGleb Smirnoff #define	IPREASS_HMASK		(IPREASS_NHASH - 1)
741dbefcc0SGleb Smirnoff 
751dbefcc0SGleb Smirnoff struct ipqbucket {
761dbefcc0SGleb Smirnoff 	TAILQ_HEAD(ipqhead, ipq) head;
771dbefcc0SGleb Smirnoff 	struct mtx		 lock;
78ff790bbaSJonathan T. Looney 	int			 count;
791dbefcc0SGleb Smirnoff };
801dbefcc0SGleb Smirnoff 
815f901c92SAndrew Turner VNET_DEFINE_STATIC(struct ipqbucket, ipq[IPREASS_NHASH]);
821dbefcc0SGleb Smirnoff #define	V_ipq		VNET(ipq)
835f901c92SAndrew Turner VNET_DEFINE_STATIC(uint32_t, ipq_hashseed);
84c047fd1bSGleb Smirnoff #define V_ipq_hashseed   VNET(ipq_hashseed)
851dbefcc0SGleb Smirnoff 
861dbefcc0SGleb Smirnoff #define	IPQ_LOCK(i)	mtx_lock(&V_ipq[i].lock)
871dbefcc0SGleb Smirnoff #define	IPQ_TRYLOCK(i)	mtx_trylock(&V_ipq[i].lock)
881dbefcc0SGleb Smirnoff #define	IPQ_UNLOCK(i)	mtx_unlock(&V_ipq[i].lock)
891dbefcc0SGleb Smirnoff #define	IPQ_LOCK_ASSERT(i)	mtx_assert(&V_ipq[i].lock, MA_OWNED)
901dbefcc0SGleb Smirnoff 
91ff790bbaSJonathan T. Looney VNET_DEFINE_STATIC(int, ipreass_maxbucketsize);
92ff790bbaSJonathan T. Looney #define	V_ipreass_maxbucketsize	VNET(ipreass_maxbucketsize)
93ff790bbaSJonathan T. Looney 
941dbefcc0SGleb Smirnoff void		ipreass_init(void);
951dbefcc0SGleb Smirnoff void		ipreass_drain(void);
961dbefcc0SGleb Smirnoff void		ipreass_slowtimo(void);
971dbefcc0SGleb Smirnoff #ifdef VIMAGE
981dbefcc0SGleb Smirnoff void		ipreass_destroy(void);
991dbefcc0SGleb Smirnoff #endif
1001dbefcc0SGleb Smirnoff static int	sysctl_maxfragpackets(SYSCTL_HANDLER_ARGS);
101ff790bbaSJonathan T. Looney static int	sysctl_maxfragbucketsize(SYSCTL_HANDLER_ARGS);
1021dbefcc0SGleb Smirnoff static void	ipreass_zone_change(void *);
1031dbefcc0SGleb Smirnoff static void	ipreass_drain_tomax(void);
104ff790bbaSJonathan T. Looney static void	ipq_free(struct ipqbucket *, struct ipq *);
1051dbefcc0SGleb Smirnoff static struct ipq * ipq_reuse(int);
1061dbefcc0SGleb Smirnoff 
1071dbefcc0SGleb Smirnoff static inline void
108ff790bbaSJonathan T. Looney ipq_timeout(struct ipqbucket *bucket, struct ipq *fp)
1091dbefcc0SGleb Smirnoff {
1101dbefcc0SGleb Smirnoff 
1111dbefcc0SGleb Smirnoff 	IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags);
112ff790bbaSJonathan T. Looney 	ipq_free(bucket, fp);
1131dbefcc0SGleb Smirnoff }
1141dbefcc0SGleb Smirnoff 
1151dbefcc0SGleb Smirnoff static inline void
116ff790bbaSJonathan T. Looney ipq_drop(struct ipqbucket *bucket, struct ipq *fp)
1171dbefcc0SGleb Smirnoff {
1181dbefcc0SGleb Smirnoff 
1191dbefcc0SGleb Smirnoff 	IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
120ff790bbaSJonathan T. Looney 	ipq_free(bucket, fp);
1211dbefcc0SGleb Smirnoff }
1221dbefcc0SGleb Smirnoff 
123a967df1cSJonathan T. Looney /*
124a967df1cSJonathan T. Looney  * By default, limit the number of IP fragments across all reassembly
125a967df1cSJonathan T. Looney  * queues to  1/32 of the total number of mbuf clusters.
126a967df1cSJonathan T. Looney  *
127a967df1cSJonathan T. Looney  * Limit the total number of reassembly queues per VNET to the
128a967df1cSJonathan T. Looney  * IP fragment limit, but ensure the limit will not allow any bucket
129a967df1cSJonathan T. Looney  * to grow above 100 items. (The bucket limit is
130a967df1cSJonathan T. Looney  * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
131a967df1cSJonathan T. Looney  * multiplier to reach a 100-item limit.)
132a967df1cSJonathan T. Looney  * The 100-item limit was chosen as brief testing seems to show that
133a967df1cSJonathan T. Looney  * this produces "reasonable" performance on some subset of systems
134a967df1cSJonathan T. Looney  * under DoS attack.
135a967df1cSJonathan T. Looney  */
136a967df1cSJonathan T. Looney #define	IP_MAXFRAGS		(nmbclusters / 32)
137a967df1cSJonathan T. Looney #define	IP_MAXFRAGPACKETS	(imin(IP_MAXFRAGS, IPREASS_NHASH * 50))
138a967df1cSJonathan T. Looney 
1397b9c5eb0SJonathan T. Looney static int		maxfrags;
1407b9c5eb0SJonathan T. Looney static volatile u_int	nfrags;
1417b9c5eb0SJonathan T. Looney SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfrags, CTLFLAG_RW,
1427b9c5eb0SJonathan T. Looney     &maxfrags, 0,
1437b9c5eb0SJonathan T. Looney     "Maximum number of IPv4 fragments allowed across all reassembly queues");
1447b9c5eb0SJonathan T. Looney SYSCTL_UINT(_net_inet_ip, OID_AUTO, curfrags, CTLFLAG_RD,
1457b9c5eb0SJonathan T. Looney     __DEVOLATILE(u_int *, &nfrags), 0,
1467b9c5eb0SJonathan T. Looney     "Current number of IPv4 fragments across all reassembly queues");
1477b9c5eb0SJonathan T. Looney 
1485f901c92SAndrew Turner VNET_DEFINE_STATIC(uma_zone_t, ipq_zone);
1491dbefcc0SGleb Smirnoff #define	V_ipq_zone	VNET(ipq_zone)
1501dbefcc0SGleb Smirnoff SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_VNET |
1511dbefcc0SGleb Smirnoff     CTLTYPE_INT | CTLFLAG_RW, NULL, 0, sysctl_maxfragpackets, "I",
1521dbefcc0SGleb Smirnoff     "Maximum number of IPv4 fragment reassembly queue entries");
1531dbefcc0SGleb Smirnoff SYSCTL_UMA_CUR(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_VNET,
1541dbefcc0SGleb Smirnoff     &VNET_NAME(ipq_zone),
1551dbefcc0SGleb Smirnoff     "Current number of IPv4 fragment reassembly queue entries");
1561dbefcc0SGleb Smirnoff 
1575f901c92SAndrew Turner VNET_DEFINE_STATIC(int, noreass);
1581dbefcc0SGleb Smirnoff #define	V_noreass	VNET(noreass)
1591dbefcc0SGleb Smirnoff 
1605f901c92SAndrew Turner VNET_DEFINE_STATIC(int, maxfragsperpacket);
1611dbefcc0SGleb Smirnoff #define	V_maxfragsperpacket	VNET(maxfragsperpacket)
1621dbefcc0SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_VNET | CTLFLAG_RW,
1631dbefcc0SGleb Smirnoff     &VNET_NAME(maxfragsperpacket), 0,
1641dbefcc0SGleb Smirnoff     "Maximum number of IPv4 fragments allowed per packet");
165ff790bbaSJonathan T. Looney SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragbucketsize,
166ff790bbaSJonathan T. Looney     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
167ff790bbaSJonathan T. Looney     sysctl_maxfragbucketsize, "I",
168ff790bbaSJonathan T. Looney     "Maximum number of IPv4 fragment reassembly queue entries per bucket");
1691dbefcc0SGleb Smirnoff 
1701dbefcc0SGleb Smirnoff /*
1711dbefcc0SGleb Smirnoff  * Take incoming datagram fragment and try to reassemble it into
1721dbefcc0SGleb Smirnoff  * whole datagram.  If the argument is the first fragment or one
1731dbefcc0SGleb Smirnoff  * in between the function will return NULL and store the mbuf
1741dbefcc0SGleb Smirnoff  * in the fragment chain.  If the argument is the last fragment
1751dbefcc0SGleb Smirnoff  * the packet will be reassembled and the pointer to the new
1761dbefcc0SGleb Smirnoff  * mbuf returned for further processing.  Only m_tags attached
1771dbefcc0SGleb Smirnoff  * to the first packet/fragment are preserved.
1781dbefcc0SGleb Smirnoff  * The IP header is *NOT* adjusted out of iplen.
1791dbefcc0SGleb Smirnoff  */
1801dbefcc0SGleb Smirnoff #define	M_IP_FRAG	M_PROTO9
1811dbefcc0SGleb Smirnoff struct mbuf *
1821dbefcc0SGleb Smirnoff ip_reass(struct mbuf *m)
1831dbefcc0SGleb Smirnoff {
1841dbefcc0SGleb Smirnoff 	struct ip *ip;
1851dbefcc0SGleb Smirnoff 	struct mbuf *p, *q, *nq, *t;
1861dbefcc0SGleb Smirnoff 	struct ipq *fp;
187a55383e7SHans Petter Selasky 	struct ifnet *srcifp;
1881dbefcc0SGleb Smirnoff 	struct ipqhead *head;
1897b9c5eb0SJonathan T. Looney 	int i, hlen, next, tmpmax;
1901dbefcc0SGleb Smirnoff 	u_int8_t ecn, ecn0;
1915d9bd455SJonathan T. Looney 	uint32_t hash, hashkey[3];
1921dbefcc0SGleb Smirnoff #ifdef	RSS
1931dbefcc0SGleb Smirnoff 	uint32_t rss_hash, rss_type;
1941dbefcc0SGleb Smirnoff #endif
1951dbefcc0SGleb Smirnoff 
1961dbefcc0SGleb Smirnoff 	/*
1971dbefcc0SGleb Smirnoff 	 * If no reassembling or maxfragsperpacket are 0,
1981dbefcc0SGleb Smirnoff 	 * never accept fragments.
1997b9c5eb0SJonathan T. Looney 	 * Also, drop packet if it would exceed the maximum
2007b9c5eb0SJonathan T. Looney 	 * number of fragments.
2011dbefcc0SGleb Smirnoff 	 */
2027b9c5eb0SJonathan T. Looney 	tmpmax = maxfrags;
2037b9c5eb0SJonathan T. Looney 	if (V_noreass == 1 || V_maxfragsperpacket == 0 ||
2047b9c5eb0SJonathan T. Looney 	    (tmpmax >= 0 && atomic_load_int(&nfrags) >= (u_int)tmpmax)) {
2051dbefcc0SGleb Smirnoff 		IPSTAT_INC(ips_fragments);
2061dbefcc0SGleb Smirnoff 		IPSTAT_INC(ips_fragdropped);
2071dbefcc0SGleb Smirnoff 		m_freem(m);
2081dbefcc0SGleb Smirnoff 		return (NULL);
2091dbefcc0SGleb Smirnoff 	}
2101dbefcc0SGleb Smirnoff 
2111dbefcc0SGleb Smirnoff 	ip = mtod(m, struct ip *);
2121dbefcc0SGleb Smirnoff 	hlen = ip->ip_hl << 2;
2131dbefcc0SGleb Smirnoff 
2141dbefcc0SGleb Smirnoff 	/*
2151dbefcc0SGleb Smirnoff 	 * Adjust ip_len to not reflect header,
2161dbefcc0SGleb Smirnoff 	 * convert offset of this to bytes.
2171dbefcc0SGleb Smirnoff 	 */
2181dbefcc0SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
2191dbefcc0SGleb Smirnoff 	/*
2201dbefcc0SGleb Smirnoff 	 * Make sure that fragments have a data length
2212157f3c3SJonathan T. Looney 	 * that's a non-zero multiple of 8 bytes, unless
2222157f3c3SJonathan T. Looney 	 * this is the last fragment.
2231dbefcc0SGleb Smirnoff 	 */
2242157f3c3SJonathan T. Looney 	if (ip->ip_len == htons(0) ||
2252157f3c3SJonathan T. Looney 	    ((ip->ip_off & htons(IP_MF)) && (ntohs(ip->ip_len) & 0x7) != 0)) {
2261dbefcc0SGleb Smirnoff 		IPSTAT_INC(ips_toosmall); /* XXX */
2271dbefcc0SGleb Smirnoff 		IPSTAT_INC(ips_fragdropped);
2281dbefcc0SGleb Smirnoff 		m_freem(m);
2291dbefcc0SGleb Smirnoff 		return (NULL);
2301dbefcc0SGleb Smirnoff 	}
2312157f3c3SJonathan T. Looney 	if (ip->ip_off & htons(IP_MF))
2321dbefcc0SGleb Smirnoff 		m->m_flags |= M_IP_FRAG;
2332157f3c3SJonathan T. Looney 	else
2341dbefcc0SGleb Smirnoff 		m->m_flags &= ~M_IP_FRAG;
2351dbefcc0SGleb Smirnoff 	ip->ip_off = htons(ntohs(ip->ip_off) << 3);
2361dbefcc0SGleb Smirnoff 
2371dbefcc0SGleb Smirnoff 	/*
23886af1d02SMark Johnston 	 * Make sure the fragment lies within a packet of valid size.
23986af1d02SMark Johnston 	 */
24086af1d02SMark Johnston 	if (ntohs(ip->ip_len) + ntohs(ip->ip_off) > IP_MAXPACKET) {
24186af1d02SMark Johnston 		IPSTAT_INC(ips_toolong);
24286af1d02SMark Johnston 		IPSTAT_INC(ips_fragdropped);
24386af1d02SMark Johnston 		m_freem(m);
24486af1d02SMark Johnston 		return (NULL);
24586af1d02SMark Johnston 	}
24686af1d02SMark Johnston 
24786af1d02SMark Johnston 	/*
248a55383e7SHans Petter Selasky 	 * Store receive network interface pointer for later.
249a55383e7SHans Petter Selasky 	 */
250a55383e7SHans Petter Selasky 	srcifp = m->m_pkthdr.rcvif;
251a55383e7SHans Petter Selasky 
252a55383e7SHans Petter Selasky 	/*
2531dbefcc0SGleb Smirnoff 	 * Attempt reassembly; if it succeeds, proceed.
2541dbefcc0SGleb Smirnoff 	 * ip_reass() will return a different mbuf.
2551dbefcc0SGleb Smirnoff 	 */
2561dbefcc0SGleb Smirnoff 	IPSTAT_INC(ips_fragments);
2571dbefcc0SGleb Smirnoff 	m->m_pkthdr.PH_loc.ptr = ip;
2581dbefcc0SGleb Smirnoff 
2591dbefcc0SGleb Smirnoff 	/*
2601dbefcc0SGleb Smirnoff 	 * Presence of header sizes in mbufs
2611dbefcc0SGleb Smirnoff 	 * would confuse code below.
2621dbefcc0SGleb Smirnoff 	 */
2631dbefcc0SGleb Smirnoff 	m->m_data += hlen;
2641dbefcc0SGleb Smirnoff 	m->m_len -= hlen;
2651dbefcc0SGleb Smirnoff 
2665d9bd455SJonathan T. Looney 	hashkey[0] = ip->ip_src.s_addr;
2675d9bd455SJonathan T. Looney 	hashkey[1] = ip->ip_dst.s_addr;
2685d9bd455SJonathan T. Looney 	hashkey[2] = (uint32_t)ip->ip_p << 16;
2695d9bd455SJonathan T. Looney 	hashkey[2] += ip->ip_id;
2705d9bd455SJonathan T. Looney 	hash = jenkins_hash32(hashkey, nitems(hashkey), V_ipq_hashseed);
2715d9bd455SJonathan T. Looney 	hash &= IPREASS_HMASK;
2721dbefcc0SGleb Smirnoff 	head = &V_ipq[hash].head;
2731dbefcc0SGleb Smirnoff 	IPQ_LOCK(hash);
2741dbefcc0SGleb Smirnoff 
2751dbefcc0SGleb Smirnoff 	/*
2761dbefcc0SGleb Smirnoff 	 * Look for queue of fragments
2771dbefcc0SGleb Smirnoff 	 * of this datagram.
2781dbefcc0SGleb Smirnoff 	 */
2791dbefcc0SGleb Smirnoff 	TAILQ_FOREACH(fp, head, ipq_list)
2801dbefcc0SGleb Smirnoff 		if (ip->ip_id == fp->ipq_id &&
2811dbefcc0SGleb Smirnoff 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
2821dbefcc0SGleb Smirnoff 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
2831dbefcc0SGleb Smirnoff #ifdef MAC
2841dbefcc0SGleb Smirnoff 		    mac_ipq_match(m, fp) &&
2851dbefcc0SGleb Smirnoff #endif
2861dbefcc0SGleb Smirnoff 		    ip->ip_p == fp->ipq_p)
2871dbefcc0SGleb Smirnoff 			break;
2881dbefcc0SGleb Smirnoff 	/*
2891dbefcc0SGleb Smirnoff 	 * If first fragment to arrive, create a reassembly queue.
2901dbefcc0SGleb Smirnoff 	 */
2911dbefcc0SGleb Smirnoff 	if (fp == NULL) {
292ff790bbaSJonathan T. Looney 		if (V_ipq[hash].count < V_ipreass_maxbucketsize)
2931dbefcc0SGleb Smirnoff 			fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
2941dbefcc0SGleb Smirnoff 		if (fp == NULL)
2951dbefcc0SGleb Smirnoff 			fp = ipq_reuse(hash);
296ff790bbaSJonathan T. Looney 		if (fp == NULL)
297ff790bbaSJonathan T. Looney 			goto dropfrag;
2981dbefcc0SGleb Smirnoff #ifdef MAC
2991dbefcc0SGleb Smirnoff 		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
3001dbefcc0SGleb Smirnoff 			uma_zfree(V_ipq_zone, fp);
3011dbefcc0SGleb Smirnoff 			fp = NULL;
3021dbefcc0SGleb Smirnoff 			goto dropfrag;
3031dbefcc0SGleb Smirnoff 		}
3041dbefcc0SGleb Smirnoff 		mac_ipq_create(m, fp);
3051dbefcc0SGleb Smirnoff #endif
3061dbefcc0SGleb Smirnoff 		TAILQ_INSERT_HEAD(head, fp, ipq_list);
307ff790bbaSJonathan T. Looney 		V_ipq[hash].count++;
3081dbefcc0SGleb Smirnoff 		fp->ipq_nfrags = 1;
3097b9c5eb0SJonathan T. Looney 		atomic_add_int(&nfrags, 1);
3101dbefcc0SGleb Smirnoff 		fp->ipq_ttl = IPFRAGTTL;
3111dbefcc0SGleb Smirnoff 		fp->ipq_p = ip->ip_p;
3121dbefcc0SGleb Smirnoff 		fp->ipq_id = ip->ip_id;
3131dbefcc0SGleb Smirnoff 		fp->ipq_src = ip->ip_src;
3141dbefcc0SGleb Smirnoff 		fp->ipq_dst = ip->ip_dst;
3151dbefcc0SGleb Smirnoff 		fp->ipq_frags = m;
3162157f3c3SJonathan T. Looney 		if (m->m_flags & M_IP_FRAG)
3172157f3c3SJonathan T. Looney 			fp->ipq_maxoff = -1;
3182157f3c3SJonathan T. Looney 		else
3192157f3c3SJonathan T. Looney 			fp->ipq_maxoff = ntohs(ip->ip_off) + ntohs(ip->ip_len);
3201dbefcc0SGleb Smirnoff 		m->m_nextpkt = NULL;
3211dbefcc0SGleb Smirnoff 		goto done;
3221dbefcc0SGleb Smirnoff 	} else {
3232157f3c3SJonathan T. Looney 		/*
3242157f3c3SJonathan T. Looney 		 * If we already saw the last fragment, make sure
3252157f3c3SJonathan T. Looney 		 * this fragment's offset looks sane. Otherwise, if
3262157f3c3SJonathan T. Looney 		 * this is the last fragment, record its endpoint.
3272157f3c3SJonathan T. Looney 		 */
3282157f3c3SJonathan T. Looney 		if (fp->ipq_maxoff > 0) {
3292157f3c3SJonathan T. Looney 			i = ntohs(ip->ip_off) + ntohs(ip->ip_len);
3302157f3c3SJonathan T. Looney 			if (((m->m_flags & M_IP_FRAG) && i >= fp->ipq_maxoff) ||
3312157f3c3SJonathan T. Looney 			    ((m->m_flags & M_IP_FRAG) == 0 &&
3322157f3c3SJonathan T. Looney 			    i != fp->ipq_maxoff)) {
3332157f3c3SJonathan T. Looney 				fp = NULL;
3342157f3c3SJonathan T. Looney 				goto dropfrag;
3352157f3c3SJonathan T. Looney 			}
3362157f3c3SJonathan T. Looney 		} else if ((m->m_flags & M_IP_FRAG) == 0)
3372157f3c3SJonathan T. Looney 			fp->ipq_maxoff = ntohs(ip->ip_off) + ntohs(ip->ip_len);
3381dbefcc0SGleb Smirnoff 		fp->ipq_nfrags++;
3397b9c5eb0SJonathan T. Looney 		atomic_add_int(&nfrags, 1);
3401dbefcc0SGleb Smirnoff #ifdef MAC
3411dbefcc0SGleb Smirnoff 		mac_ipq_update(m, fp);
3421dbefcc0SGleb Smirnoff #endif
3431dbefcc0SGleb Smirnoff 	}
3441dbefcc0SGleb Smirnoff 
3451dbefcc0SGleb Smirnoff #define GETIP(m)	((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
3461dbefcc0SGleb Smirnoff 
3471dbefcc0SGleb Smirnoff 	/*
3481dbefcc0SGleb Smirnoff 	 * Handle ECN by comparing this segment with the first one;
3491dbefcc0SGleb Smirnoff 	 * if CE is set, do not lose CE.
3501dbefcc0SGleb Smirnoff 	 * drop if CE and not-ECT are mixed for the same packet.
3511dbefcc0SGleb Smirnoff 	 */
3521dbefcc0SGleb Smirnoff 	ecn = ip->ip_tos & IPTOS_ECN_MASK;
3531dbefcc0SGleb Smirnoff 	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
3541dbefcc0SGleb Smirnoff 	if (ecn == IPTOS_ECN_CE) {
3551dbefcc0SGleb Smirnoff 		if (ecn0 == IPTOS_ECN_NOTECT)
3561dbefcc0SGleb Smirnoff 			goto dropfrag;
3571dbefcc0SGleb Smirnoff 		if (ecn0 != IPTOS_ECN_CE)
3581dbefcc0SGleb Smirnoff 			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
3591dbefcc0SGleb Smirnoff 	}
3601dbefcc0SGleb Smirnoff 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
3611dbefcc0SGleb Smirnoff 		goto dropfrag;
3621dbefcc0SGleb Smirnoff 
3631dbefcc0SGleb Smirnoff 	/*
3641dbefcc0SGleb Smirnoff 	 * Find a segment which begins after this one does.
3651dbefcc0SGleb Smirnoff 	 */
3661dbefcc0SGleb Smirnoff 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
3671dbefcc0SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
3681dbefcc0SGleb Smirnoff 			break;
3691dbefcc0SGleb Smirnoff 
3701dbefcc0SGleb Smirnoff 	/*
3711dbefcc0SGleb Smirnoff 	 * If there is a preceding segment, it may provide some of
3721dbefcc0SGleb Smirnoff 	 * our data already.  If so, drop the data from the incoming
3731dbefcc0SGleb Smirnoff 	 * segment.  If it provides all of our data, drop us, otherwise
3741dbefcc0SGleb Smirnoff 	 * stick new segment in the proper place.
3751dbefcc0SGleb Smirnoff 	 *
3761dbefcc0SGleb Smirnoff 	 * If some of the data is dropped from the preceding
3771dbefcc0SGleb Smirnoff 	 * segment, then it's checksum is invalidated.
3781dbefcc0SGleb Smirnoff 	 */
3791dbefcc0SGleb Smirnoff 	if (p) {
3801dbefcc0SGleb Smirnoff 		i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
3811dbefcc0SGleb Smirnoff 		    ntohs(ip->ip_off);
3821dbefcc0SGleb Smirnoff 		if (i > 0) {
3831dbefcc0SGleb Smirnoff 			if (i >= ntohs(ip->ip_len))
3841dbefcc0SGleb Smirnoff 				goto dropfrag;
3851dbefcc0SGleb Smirnoff 			m_adj(m, i);
3861dbefcc0SGleb Smirnoff 			m->m_pkthdr.csum_flags = 0;
3871dbefcc0SGleb Smirnoff 			ip->ip_off = htons(ntohs(ip->ip_off) + i);
3881dbefcc0SGleb Smirnoff 			ip->ip_len = htons(ntohs(ip->ip_len) - i);
3891dbefcc0SGleb Smirnoff 		}
3901dbefcc0SGleb Smirnoff 		m->m_nextpkt = p->m_nextpkt;
3911dbefcc0SGleb Smirnoff 		p->m_nextpkt = m;
3921dbefcc0SGleb Smirnoff 	} else {
3931dbefcc0SGleb Smirnoff 		m->m_nextpkt = fp->ipq_frags;
3941dbefcc0SGleb Smirnoff 		fp->ipq_frags = m;
3951dbefcc0SGleb Smirnoff 	}
3961dbefcc0SGleb Smirnoff 
3971dbefcc0SGleb Smirnoff 	/*
3981dbefcc0SGleb Smirnoff 	 * While we overlap succeeding segments trim them or,
3991dbefcc0SGleb Smirnoff 	 * if they are completely covered, dequeue them.
4001dbefcc0SGleb Smirnoff 	 */
4011dbefcc0SGleb Smirnoff 	for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
4021dbefcc0SGleb Smirnoff 	    ntohs(GETIP(q)->ip_off); q = nq) {
4031dbefcc0SGleb Smirnoff 		i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
4041dbefcc0SGleb Smirnoff 		    ntohs(GETIP(q)->ip_off);
4051dbefcc0SGleb Smirnoff 		if (i < ntohs(GETIP(q)->ip_len)) {
4061dbefcc0SGleb Smirnoff 			GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
4071dbefcc0SGleb Smirnoff 			GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
4081dbefcc0SGleb Smirnoff 			m_adj(q, i);
4091dbefcc0SGleb Smirnoff 			q->m_pkthdr.csum_flags = 0;
4101dbefcc0SGleb Smirnoff 			break;
4111dbefcc0SGleb Smirnoff 		}
4121dbefcc0SGleb Smirnoff 		nq = q->m_nextpkt;
4131dbefcc0SGleb Smirnoff 		m->m_nextpkt = nq;
4141dbefcc0SGleb Smirnoff 		IPSTAT_INC(ips_fragdropped);
4151dbefcc0SGleb Smirnoff 		fp->ipq_nfrags--;
4167b9c5eb0SJonathan T. Looney 		atomic_subtract_int(&nfrags, 1);
4171dbefcc0SGleb Smirnoff 		m_freem(q);
4181dbefcc0SGleb Smirnoff 	}
4191dbefcc0SGleb Smirnoff 
4201dbefcc0SGleb Smirnoff 	/*
4211dbefcc0SGleb Smirnoff 	 * Check for complete reassembly and perform frag per packet
4221dbefcc0SGleb Smirnoff 	 * limiting.
4231dbefcc0SGleb Smirnoff 	 *
4241dbefcc0SGleb Smirnoff 	 * Frag limiting is performed here so that the nth frag has
4251dbefcc0SGleb Smirnoff 	 * a chance to complete the packet before we drop the packet.
4261dbefcc0SGleb Smirnoff 	 * As a result, n+1 frags are actually allowed per packet, but
4271dbefcc0SGleb Smirnoff 	 * only n will ever be stored. (n = maxfragsperpacket.)
4281dbefcc0SGleb Smirnoff 	 *
4291dbefcc0SGleb Smirnoff 	 */
4301dbefcc0SGleb Smirnoff 	next = 0;
4311dbefcc0SGleb Smirnoff 	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
4321dbefcc0SGleb Smirnoff 		if (ntohs(GETIP(q)->ip_off) != next) {
4331dbefcc0SGleb Smirnoff 			if (fp->ipq_nfrags > V_maxfragsperpacket)
434ff790bbaSJonathan T. Looney 				ipq_drop(&V_ipq[hash], fp);
4351dbefcc0SGleb Smirnoff 			goto done;
4361dbefcc0SGleb Smirnoff 		}
4371dbefcc0SGleb Smirnoff 		next += ntohs(GETIP(q)->ip_len);
4381dbefcc0SGleb Smirnoff 	}
4391dbefcc0SGleb Smirnoff 	/* Make sure the last packet didn't have the IP_MF flag */
4401dbefcc0SGleb Smirnoff 	if (p->m_flags & M_IP_FRAG) {
4411dbefcc0SGleb Smirnoff 		if (fp->ipq_nfrags > V_maxfragsperpacket)
442ff790bbaSJonathan T. Looney 			ipq_drop(&V_ipq[hash], fp);
4431dbefcc0SGleb Smirnoff 		goto done;
4441dbefcc0SGleb Smirnoff 	}
4451dbefcc0SGleb Smirnoff 
4461dbefcc0SGleb Smirnoff 	/*
4471dbefcc0SGleb Smirnoff 	 * Reassembly is complete.  Make sure the packet is a sane size.
4481dbefcc0SGleb Smirnoff 	 */
4491dbefcc0SGleb Smirnoff 	q = fp->ipq_frags;
4501dbefcc0SGleb Smirnoff 	ip = GETIP(q);
4511dbefcc0SGleb Smirnoff 	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
4521dbefcc0SGleb Smirnoff 		IPSTAT_INC(ips_toolong);
453ff790bbaSJonathan T. Looney 		ipq_drop(&V_ipq[hash], fp);
4541dbefcc0SGleb Smirnoff 		goto done;
4551dbefcc0SGleb Smirnoff 	}
4561dbefcc0SGleb Smirnoff 
4571dbefcc0SGleb Smirnoff 	/*
4581dbefcc0SGleb Smirnoff 	 * Concatenate fragments.
4591dbefcc0SGleb Smirnoff 	 */
4601dbefcc0SGleb Smirnoff 	m = q;
4611dbefcc0SGleb Smirnoff 	t = m->m_next;
4621dbefcc0SGleb Smirnoff 	m->m_next = NULL;
4631dbefcc0SGleb Smirnoff 	m_cat(m, t);
4641dbefcc0SGleb Smirnoff 	nq = q->m_nextpkt;
4651dbefcc0SGleb Smirnoff 	q->m_nextpkt = NULL;
4661dbefcc0SGleb Smirnoff 	for (q = nq; q != NULL; q = nq) {
4671dbefcc0SGleb Smirnoff 		nq = q->m_nextpkt;
4681dbefcc0SGleb Smirnoff 		q->m_nextpkt = NULL;
4691dbefcc0SGleb Smirnoff 		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
4701dbefcc0SGleb Smirnoff 		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
47109b0b8c0SNavdeep Parhar 		m_demote_pkthdr(q);
4721dbefcc0SGleb Smirnoff 		m_cat(m, q);
4731dbefcc0SGleb Smirnoff 	}
4741dbefcc0SGleb Smirnoff 	/*
4751dbefcc0SGleb Smirnoff 	 * In order to do checksumming faster we do 'end-around carry' here
4761dbefcc0SGleb Smirnoff 	 * (and not in for{} loop), though it implies we are not going to
4771dbefcc0SGleb Smirnoff 	 * reassemble more than 64k fragments.
4781dbefcc0SGleb Smirnoff 	 */
4791dbefcc0SGleb Smirnoff 	while (m->m_pkthdr.csum_data & 0xffff0000)
4801dbefcc0SGleb Smirnoff 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
4811dbefcc0SGleb Smirnoff 		    (m->m_pkthdr.csum_data >> 16);
4827b9c5eb0SJonathan T. Looney 	atomic_subtract_int(&nfrags, fp->ipq_nfrags);
4831dbefcc0SGleb Smirnoff #ifdef MAC
4841dbefcc0SGleb Smirnoff 	mac_ipq_reassemble(fp, m);
4851dbefcc0SGleb Smirnoff 	mac_ipq_destroy(fp);
4861dbefcc0SGleb Smirnoff #endif
4871dbefcc0SGleb Smirnoff 
4881dbefcc0SGleb Smirnoff 	/*
4891dbefcc0SGleb Smirnoff 	 * Create header for new ip packet by modifying header of first
4901dbefcc0SGleb Smirnoff 	 * packet;  dequeue and discard fragment reassembly header.
4911dbefcc0SGleb Smirnoff 	 * Make header visible.
4921dbefcc0SGleb Smirnoff 	 */
4931dbefcc0SGleb Smirnoff 	ip->ip_len = htons((ip->ip_hl << 2) + next);
4941dbefcc0SGleb Smirnoff 	ip->ip_src = fp->ipq_src;
4951dbefcc0SGleb Smirnoff 	ip->ip_dst = fp->ipq_dst;
4961dbefcc0SGleb Smirnoff 	TAILQ_REMOVE(head, fp, ipq_list);
497ff790bbaSJonathan T. Looney 	V_ipq[hash].count--;
4981dbefcc0SGleb Smirnoff 	uma_zfree(V_ipq_zone, fp);
4991dbefcc0SGleb Smirnoff 	m->m_len += (ip->ip_hl << 2);
5001dbefcc0SGleb Smirnoff 	m->m_data -= (ip->ip_hl << 2);
5011dbefcc0SGleb Smirnoff 	/* some debugging cruft by sklower, below, will go away soon */
502a55383e7SHans Petter Selasky 	if (m->m_flags & M_PKTHDR) {	/* XXX this should be done elsewhere */
5031dbefcc0SGleb Smirnoff 		m_fixhdr(m);
504a55383e7SHans Petter Selasky 		/* set valid receive interface pointer */
505a55383e7SHans Petter Selasky 		m->m_pkthdr.rcvif = srcifp;
506a55383e7SHans Petter Selasky 	}
5071dbefcc0SGleb Smirnoff 	IPSTAT_INC(ips_reassembled);
5081dbefcc0SGleb Smirnoff 	IPQ_UNLOCK(hash);
5091dbefcc0SGleb Smirnoff 
5101dbefcc0SGleb Smirnoff #ifdef	RSS
5111dbefcc0SGleb Smirnoff 	/*
5121dbefcc0SGleb Smirnoff 	 * Query the RSS layer for the flowid / flowtype for the
5131dbefcc0SGleb Smirnoff 	 * mbuf payload.
5141dbefcc0SGleb Smirnoff 	 *
5151dbefcc0SGleb Smirnoff 	 * For now, just assume we have to calculate a new one.
5161dbefcc0SGleb Smirnoff 	 * Later on we should check to see if the assigned flowid matches
5171dbefcc0SGleb Smirnoff 	 * what RSS wants for the given IP protocol and if so, just keep it.
5181dbefcc0SGleb Smirnoff 	 *
5191dbefcc0SGleb Smirnoff 	 * We then queue into the relevant netisr so it can be dispatched
5201dbefcc0SGleb Smirnoff 	 * to the correct CPU.
5211dbefcc0SGleb Smirnoff 	 *
5221dbefcc0SGleb Smirnoff 	 * Note - this may return 1, which means the flowid in the mbuf
5231dbefcc0SGleb Smirnoff 	 * is correct for the configured RSS hash types and can be used.
5241dbefcc0SGleb Smirnoff 	 */
5251dbefcc0SGleb Smirnoff 	if (rss_mbuf_software_hash_v4(m, 0, &rss_hash, &rss_type) == 0) {
5261dbefcc0SGleb Smirnoff 		m->m_pkthdr.flowid = rss_hash;
5271dbefcc0SGleb Smirnoff 		M_HASHTYPE_SET(m, rss_type);
5281dbefcc0SGleb Smirnoff 	}
5291dbefcc0SGleb Smirnoff 
5301dbefcc0SGleb Smirnoff 	/*
5311dbefcc0SGleb Smirnoff 	 * Queue/dispatch for reprocessing.
5321dbefcc0SGleb Smirnoff 	 *
5331dbefcc0SGleb Smirnoff 	 * Note: this is much slower than just handling the frame in the
5341dbefcc0SGleb Smirnoff 	 * current receive context.  It's likely worth investigating
5351dbefcc0SGleb Smirnoff 	 * why this is.
5361dbefcc0SGleb Smirnoff 	 */
5371dbefcc0SGleb Smirnoff 	netisr_dispatch(NETISR_IP_DIRECT, m);
5381dbefcc0SGleb Smirnoff 	return (NULL);
5391dbefcc0SGleb Smirnoff #endif
5401dbefcc0SGleb Smirnoff 
5411dbefcc0SGleb Smirnoff 	/* Handle in-line */
5421dbefcc0SGleb Smirnoff 	return (m);
5431dbefcc0SGleb Smirnoff 
5441dbefcc0SGleb Smirnoff dropfrag:
5451dbefcc0SGleb Smirnoff 	IPSTAT_INC(ips_fragdropped);
5467b9c5eb0SJonathan T. Looney 	if (fp != NULL) {
5471dbefcc0SGleb Smirnoff 		fp->ipq_nfrags--;
5487b9c5eb0SJonathan T. Looney 		atomic_subtract_int(&nfrags, 1);
5497b9c5eb0SJonathan T. Looney 	}
5501dbefcc0SGleb Smirnoff 	m_freem(m);
5511dbefcc0SGleb Smirnoff done:
5521dbefcc0SGleb Smirnoff 	IPQ_UNLOCK(hash);
5531dbefcc0SGleb Smirnoff 	return (NULL);
5541dbefcc0SGleb Smirnoff 
5551dbefcc0SGleb Smirnoff #undef GETIP
5561dbefcc0SGleb Smirnoff }
5571dbefcc0SGleb Smirnoff 
5581dbefcc0SGleb Smirnoff /*
5591dbefcc0SGleb Smirnoff  * Initialize IP reassembly structures.
5601dbefcc0SGleb Smirnoff  */
5611dbefcc0SGleb Smirnoff void
5621dbefcc0SGleb Smirnoff ipreass_init(void)
5631dbefcc0SGleb Smirnoff {
564ff790bbaSJonathan T. Looney 	int max;
5651dbefcc0SGleb Smirnoff 
5661dbefcc0SGleb Smirnoff 	for (int i = 0; i < IPREASS_NHASH; i++) {
5671dbefcc0SGleb Smirnoff 		TAILQ_INIT(&V_ipq[i].head);
5681dbefcc0SGleb Smirnoff 		mtx_init(&V_ipq[i].lock, "IP reassembly", NULL,
5691dbefcc0SGleb Smirnoff 		    MTX_DEF | MTX_DUPOK);
570ff790bbaSJonathan T. Looney 		V_ipq[i].count = 0;
5711dbefcc0SGleb Smirnoff 	}
572c047fd1bSGleb Smirnoff 	V_ipq_hashseed = arc4random();
5731dbefcc0SGleb Smirnoff 	V_maxfragsperpacket = 16;
5741dbefcc0SGleb Smirnoff 	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
5751dbefcc0SGleb Smirnoff 	    NULL, UMA_ALIGN_PTR, 0);
576a967df1cSJonathan T. Looney 	max = IP_MAXFRAGPACKETS;
577ff790bbaSJonathan T. Looney 	max = uma_zone_set_max(V_ipq_zone, max);
578ff790bbaSJonathan T. Looney 	V_ipreass_maxbucketsize = imax(max / (IPREASS_NHASH / 2), 1);
5791dbefcc0SGleb Smirnoff 
5807b9c5eb0SJonathan T. Looney 	if (IS_DEFAULT_VNET(curvnet)) {
581a967df1cSJonathan T. Looney 		maxfrags = IP_MAXFRAGS;
5821dbefcc0SGleb Smirnoff 		EVENTHANDLER_REGISTER(nmbclusters_change, ipreass_zone_change,
5831dbefcc0SGleb Smirnoff 		    NULL, EVENTHANDLER_PRI_ANY);
5841dbefcc0SGleb Smirnoff 	}
5857b9c5eb0SJonathan T. Looney }
5861dbefcc0SGleb Smirnoff 
5871dbefcc0SGleb Smirnoff /*
5881dbefcc0SGleb Smirnoff  * If a timer expires on a reassembly queue, discard it.
5891dbefcc0SGleb Smirnoff  */
5901dbefcc0SGleb Smirnoff void
5911dbefcc0SGleb Smirnoff ipreass_slowtimo(void)
5921dbefcc0SGleb Smirnoff {
5931dbefcc0SGleb Smirnoff 	struct ipq *fp, *tmp;
5941dbefcc0SGleb Smirnoff 
5951dbefcc0SGleb Smirnoff 	for (int i = 0; i < IPREASS_NHASH; i++) {
5961dbefcc0SGleb Smirnoff 		IPQ_LOCK(i);
5971dbefcc0SGleb Smirnoff 		TAILQ_FOREACH_SAFE(fp, &V_ipq[i].head, ipq_list, tmp)
5981dbefcc0SGleb Smirnoff 		if (--fp->ipq_ttl == 0)
599ff790bbaSJonathan T. Looney 				ipq_timeout(&V_ipq[i], fp);
6001dbefcc0SGleb Smirnoff 		IPQ_UNLOCK(i);
6011dbefcc0SGleb Smirnoff 	}
6021dbefcc0SGleb Smirnoff }
6031dbefcc0SGleb Smirnoff 
6041dbefcc0SGleb Smirnoff /*
6051dbefcc0SGleb Smirnoff  * Drain off all datagram fragments.
6061dbefcc0SGleb Smirnoff  */
6071dbefcc0SGleb Smirnoff void
6081dbefcc0SGleb Smirnoff ipreass_drain(void)
6091dbefcc0SGleb Smirnoff {
6101dbefcc0SGleb Smirnoff 
6111dbefcc0SGleb Smirnoff 	for (int i = 0; i < IPREASS_NHASH; i++) {
6121dbefcc0SGleb Smirnoff 		IPQ_LOCK(i);
6131dbefcc0SGleb Smirnoff 		while(!TAILQ_EMPTY(&V_ipq[i].head))
614ff790bbaSJonathan T. Looney 			ipq_drop(&V_ipq[i], TAILQ_FIRST(&V_ipq[i].head));
615ff790bbaSJonathan T. Looney 		KASSERT(V_ipq[i].count == 0,
616ff790bbaSJonathan T. Looney 		    ("%s: V_ipq[%d] count %d (V_ipq=%p)", __func__, i,
617ff790bbaSJonathan T. Looney 		    V_ipq[i].count, V_ipq));
6181dbefcc0SGleb Smirnoff 		IPQ_UNLOCK(i);
6191dbefcc0SGleb Smirnoff 	}
6201dbefcc0SGleb Smirnoff }
6211dbefcc0SGleb Smirnoff 
622a55383e7SHans Petter Selasky /*
623a55383e7SHans Petter Selasky  * Drain off all datagram fragments belonging to
624a55383e7SHans Petter Selasky  * the given network interface.
625a55383e7SHans Petter Selasky  */
626a55383e7SHans Petter Selasky static void
627a55383e7SHans Petter Selasky ipreass_cleanup(void *arg __unused, struct ifnet *ifp)
628a55383e7SHans Petter Selasky {
629a55383e7SHans Petter Selasky 	struct ipq *fp, *temp;
630a55383e7SHans Petter Selasky 	struct mbuf *m;
631a55383e7SHans Petter Selasky 	int i;
632a55383e7SHans Petter Selasky 
633a55383e7SHans Petter Selasky 	KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
634a55383e7SHans Petter Selasky 
635a55383e7SHans Petter Selasky 	/*
636a55383e7SHans Petter Selasky 	 * Skip processing if IPv4 reassembly is not initialised or
637a55383e7SHans Petter Selasky 	 * torn down by ipreass_destroy().
638a55383e7SHans Petter Selasky 	 */
639a55383e7SHans Petter Selasky 	if (V_ipq_zone == NULL)
640a55383e7SHans Petter Selasky 		return;
641a55383e7SHans Petter Selasky 
642a55383e7SHans Petter Selasky 	CURVNET_SET_QUIET(ifp->if_vnet);
643a55383e7SHans Petter Selasky 	for (i = 0; i < IPREASS_NHASH; i++) {
644a55383e7SHans Petter Selasky 		IPQ_LOCK(i);
645a55383e7SHans Petter Selasky 		/* Scan fragment list. */
646a55383e7SHans Petter Selasky 		TAILQ_FOREACH_SAFE(fp, &V_ipq[i].head, ipq_list, temp) {
647a55383e7SHans Petter Selasky 			for (m = fp->ipq_frags; m != NULL; m = m->m_nextpkt) {
648a55383e7SHans Petter Selasky 				/* clear no longer valid rcvif pointer */
649a55383e7SHans Petter Selasky 				if (m->m_pkthdr.rcvif == ifp)
650a55383e7SHans Petter Selasky 					m->m_pkthdr.rcvif = NULL;
651a55383e7SHans Petter Selasky 			}
652a55383e7SHans Petter Selasky 		}
653a55383e7SHans Petter Selasky 		IPQ_UNLOCK(i);
654a55383e7SHans Petter Selasky 	}
655a55383e7SHans Petter Selasky 	CURVNET_RESTORE();
656a55383e7SHans Petter Selasky }
657a55383e7SHans Petter Selasky EVENTHANDLER_DEFINE(ifnet_departure_event, ipreass_cleanup, NULL, 0);
658a55383e7SHans Petter Selasky 
6591dbefcc0SGleb Smirnoff #ifdef VIMAGE
6601dbefcc0SGleb Smirnoff /*
6611dbefcc0SGleb Smirnoff  * Destroy IP reassembly structures.
6621dbefcc0SGleb Smirnoff  */
6631dbefcc0SGleb Smirnoff void
6641dbefcc0SGleb Smirnoff ipreass_destroy(void)
6651dbefcc0SGleb Smirnoff {
6661dbefcc0SGleb Smirnoff 
6671dbefcc0SGleb Smirnoff 	ipreass_drain();
6681dbefcc0SGleb Smirnoff 	uma_zdestroy(V_ipq_zone);
669a55383e7SHans Petter Selasky 	V_ipq_zone = NULL;
6701dbefcc0SGleb Smirnoff 	for (int i = 0; i < IPREASS_NHASH; i++)
6711dbefcc0SGleb Smirnoff 		mtx_destroy(&V_ipq[i].lock);
6721dbefcc0SGleb Smirnoff }
6731dbefcc0SGleb Smirnoff #endif
6741dbefcc0SGleb Smirnoff 
6751dbefcc0SGleb Smirnoff /*
6761dbefcc0SGleb Smirnoff  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
6771dbefcc0SGleb Smirnoff  * max has slightly different semantics than the sysctl, for historical
6781dbefcc0SGleb Smirnoff  * reasons.
6791dbefcc0SGleb Smirnoff  */
6801dbefcc0SGleb Smirnoff static void
6811dbefcc0SGleb Smirnoff ipreass_drain_tomax(void)
6821dbefcc0SGleb Smirnoff {
683ff790bbaSJonathan T. Looney 	struct ipq *fp;
6841dbefcc0SGleb Smirnoff 	int target;
6851dbefcc0SGleb Smirnoff 
6861dbefcc0SGleb Smirnoff 	/*
687ff790bbaSJonathan T. Looney 	 * Make sure each bucket is under the new limit. If
688ff790bbaSJonathan T. Looney 	 * necessary, drop enough of the oldest elements from
689ff790bbaSJonathan T. Looney 	 * each bucket to get under the new limit.
690ff790bbaSJonathan T. Looney 	 */
691ff790bbaSJonathan T. Looney 	for (int i = 0; i < IPREASS_NHASH; i++) {
692ff790bbaSJonathan T. Looney 		IPQ_LOCK(i);
693ff790bbaSJonathan T. Looney 		while (V_ipq[i].count > V_ipreass_maxbucketsize &&
694ff790bbaSJonathan T. Looney 		    (fp = TAILQ_LAST(&V_ipq[i].head, ipqhead)) != NULL)
695ff790bbaSJonathan T. Looney 			ipq_timeout(&V_ipq[i], fp);
696ff790bbaSJonathan T. Looney 		IPQ_UNLOCK(i);
697ff790bbaSJonathan T. Looney 	}
698ff790bbaSJonathan T. Looney 
699ff790bbaSJonathan T. Looney 	/*
7001dbefcc0SGleb Smirnoff 	 * If we are over the maximum number of fragments,
7011dbefcc0SGleb Smirnoff 	 * drain off enough to get down to the new limit,
7021dbefcc0SGleb Smirnoff 	 * stripping off last elements on queues.  Every
7031dbefcc0SGleb Smirnoff 	 * run we strip the oldest element from each bucket.
7041dbefcc0SGleb Smirnoff 	 */
7051dbefcc0SGleb Smirnoff 	target = uma_zone_get_max(V_ipq_zone);
7061dbefcc0SGleb Smirnoff 	while (uma_zone_get_cur(V_ipq_zone) > target) {
7071dbefcc0SGleb Smirnoff 		for (int i = 0; i < IPREASS_NHASH; i++) {
7081dbefcc0SGleb Smirnoff 			IPQ_LOCK(i);
7091dbefcc0SGleb Smirnoff 			fp = TAILQ_LAST(&V_ipq[i].head, ipqhead);
7101dbefcc0SGleb Smirnoff 			if (fp != NULL)
711ff790bbaSJonathan T. Looney 				ipq_timeout(&V_ipq[i], fp);
7121dbefcc0SGleb Smirnoff 			IPQ_UNLOCK(i);
7131dbefcc0SGleb Smirnoff 		}
7141dbefcc0SGleb Smirnoff 	}
7151dbefcc0SGleb Smirnoff }
7161dbefcc0SGleb Smirnoff 
7171dbefcc0SGleb Smirnoff static void
7181dbefcc0SGleb Smirnoff ipreass_zone_change(void *tag)
7191dbefcc0SGleb Smirnoff {
7207b9c5eb0SJonathan T. Looney 	VNET_ITERATOR_DECL(vnet_iter);
7217b9c5eb0SJonathan T. Looney 	int max;
7221dbefcc0SGleb Smirnoff 
723a967df1cSJonathan T. Looney 	maxfrags = IP_MAXFRAGS;
724a967df1cSJonathan T. Looney 	max = IP_MAXFRAGPACKETS;
7257b9c5eb0SJonathan T. Looney 	VNET_LIST_RLOCK_NOSLEEP();
7267b9c5eb0SJonathan T. Looney 	VNET_FOREACH(vnet_iter) {
7277b9c5eb0SJonathan T. Looney 		CURVNET_SET(vnet_iter);
728ff790bbaSJonathan T. Looney 		max = uma_zone_set_max(V_ipq_zone, max);
729ff790bbaSJonathan T. Looney 		V_ipreass_maxbucketsize = imax(max / (IPREASS_NHASH / 2), 1);
7301dbefcc0SGleb Smirnoff 		ipreass_drain_tomax();
7317b9c5eb0SJonathan T. Looney 		CURVNET_RESTORE();
7327b9c5eb0SJonathan T. Looney 	}
7337b9c5eb0SJonathan T. Looney 	VNET_LIST_RUNLOCK_NOSLEEP();
7341dbefcc0SGleb Smirnoff }
7351dbefcc0SGleb Smirnoff 
7361dbefcc0SGleb Smirnoff /*
7371dbefcc0SGleb Smirnoff  * Change the limit on the UMA zone, or disable the fragment allocation
7381dbefcc0SGleb Smirnoff  * at all.  Since 0 and -1 is a special values here, we need our own handler,
7391dbefcc0SGleb Smirnoff  * instead of sysctl_handle_uma_zone_max().
7401dbefcc0SGleb Smirnoff  */
7411dbefcc0SGleb Smirnoff static int
7421dbefcc0SGleb Smirnoff sysctl_maxfragpackets(SYSCTL_HANDLER_ARGS)
7431dbefcc0SGleb Smirnoff {
7441dbefcc0SGleb Smirnoff 	int error, max;
7451dbefcc0SGleb Smirnoff 
7461dbefcc0SGleb Smirnoff 	if (V_noreass == 0) {
7471dbefcc0SGleb Smirnoff 		max = uma_zone_get_max(V_ipq_zone);
7481dbefcc0SGleb Smirnoff 		if (max == 0)
7491dbefcc0SGleb Smirnoff 			max = -1;
7501dbefcc0SGleb Smirnoff 	} else
7511dbefcc0SGleb Smirnoff 		max = 0;
7521dbefcc0SGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
7531dbefcc0SGleb Smirnoff 	if (error || !req->newptr)
7541dbefcc0SGleb Smirnoff 		return (error);
7551dbefcc0SGleb Smirnoff 	if (max > 0) {
7561dbefcc0SGleb Smirnoff 		/*
7571dbefcc0SGleb Smirnoff 		 * XXXRW: Might be a good idea to sanity check the argument
7581dbefcc0SGleb Smirnoff 		 * and place an extreme upper bound.
7591dbefcc0SGleb Smirnoff 		 */
7601dbefcc0SGleb Smirnoff 		max = uma_zone_set_max(V_ipq_zone, max);
761ff790bbaSJonathan T. Looney 		V_ipreass_maxbucketsize = imax(max / (IPREASS_NHASH / 2), 1);
7621dbefcc0SGleb Smirnoff 		ipreass_drain_tomax();
7631dbefcc0SGleb Smirnoff 		V_noreass = 0;
7641dbefcc0SGleb Smirnoff 	} else if (max == 0) {
7651dbefcc0SGleb Smirnoff 		V_noreass = 1;
7661dbefcc0SGleb Smirnoff 		ipreass_drain();
7671dbefcc0SGleb Smirnoff 	} else if (max == -1) {
7681dbefcc0SGleb Smirnoff 		V_noreass = 0;
7691dbefcc0SGleb Smirnoff 		uma_zone_set_max(V_ipq_zone, 0);
770ff790bbaSJonathan T. Looney 		V_ipreass_maxbucketsize = INT_MAX;
7711dbefcc0SGleb Smirnoff 	} else
7721dbefcc0SGleb Smirnoff 		return (EINVAL);
7731dbefcc0SGleb Smirnoff 	return (0);
7741dbefcc0SGleb Smirnoff }
7751dbefcc0SGleb Smirnoff 
7761dbefcc0SGleb Smirnoff /*
7771dbefcc0SGleb Smirnoff  * Seek for old fragment queue header that can be reused.  Try to
7781dbefcc0SGleb Smirnoff  * reuse a header from currently locked hash bucket.
7791dbefcc0SGleb Smirnoff  */
7801dbefcc0SGleb Smirnoff static struct ipq *
7811dbefcc0SGleb Smirnoff ipq_reuse(int start)
7821dbefcc0SGleb Smirnoff {
7831dbefcc0SGleb Smirnoff 	struct ipq *fp;
784ff790bbaSJonathan T. Looney 	int bucket, i;
7851dbefcc0SGleb Smirnoff 
7861dbefcc0SGleb Smirnoff 	IPQ_LOCK_ASSERT(start);
7871dbefcc0SGleb Smirnoff 
788ff790bbaSJonathan T. Looney 	for (i = 0; i < IPREASS_NHASH; i++) {
789ff790bbaSJonathan T. Looney 		bucket = (start + i) % IPREASS_NHASH;
790ff790bbaSJonathan T. Looney 		if (bucket != start && IPQ_TRYLOCK(bucket) == 0)
7911dbefcc0SGleb Smirnoff 			continue;
792ff790bbaSJonathan T. Looney 		fp = TAILQ_LAST(&V_ipq[bucket].head, ipqhead);
7931dbefcc0SGleb Smirnoff 		if (fp) {
7941dbefcc0SGleb Smirnoff 			struct mbuf *m;
7951dbefcc0SGleb Smirnoff 
7961dbefcc0SGleb Smirnoff 			IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags);
7977b9c5eb0SJonathan T. Looney 			atomic_subtract_int(&nfrags, fp->ipq_nfrags);
7981dbefcc0SGleb Smirnoff 			while (fp->ipq_frags) {
7991dbefcc0SGleb Smirnoff 				m = fp->ipq_frags;
8001dbefcc0SGleb Smirnoff 				fp->ipq_frags = m->m_nextpkt;
8011dbefcc0SGleb Smirnoff 				m_freem(m);
8021dbefcc0SGleb Smirnoff 			}
803ff790bbaSJonathan T. Looney 			TAILQ_REMOVE(&V_ipq[bucket].head, fp, ipq_list);
804ff790bbaSJonathan T. Looney 			V_ipq[bucket].count--;
805ff790bbaSJonathan T. Looney 			if (bucket != start)
806ff790bbaSJonathan T. Looney 				IPQ_UNLOCK(bucket);
807ff790bbaSJonathan T. Looney 			break;
808ff790bbaSJonathan T. Looney 		}
809ff790bbaSJonathan T. Looney 		if (bucket != start)
810ff790bbaSJonathan T. Looney 			IPQ_UNLOCK(bucket);
811ff790bbaSJonathan T. Looney 	}
8121dbefcc0SGleb Smirnoff 	IPQ_LOCK_ASSERT(start);
8131dbefcc0SGleb Smirnoff 	return (fp);
8141dbefcc0SGleb Smirnoff }
8151dbefcc0SGleb Smirnoff 
8161dbefcc0SGleb Smirnoff /*
8171dbefcc0SGleb Smirnoff  * Free a fragment reassembly header and all associated datagrams.
8181dbefcc0SGleb Smirnoff  */
8191dbefcc0SGleb Smirnoff static void
820ff790bbaSJonathan T. Looney ipq_free(struct ipqbucket *bucket, struct ipq *fp)
8211dbefcc0SGleb Smirnoff {
8221dbefcc0SGleb Smirnoff 	struct mbuf *q;
8231dbefcc0SGleb Smirnoff 
8247b9c5eb0SJonathan T. Looney 	atomic_subtract_int(&nfrags, fp->ipq_nfrags);
8251dbefcc0SGleb Smirnoff 	while (fp->ipq_frags) {
8261dbefcc0SGleb Smirnoff 		q = fp->ipq_frags;
8271dbefcc0SGleb Smirnoff 		fp->ipq_frags = q->m_nextpkt;
8281dbefcc0SGleb Smirnoff 		m_freem(q);
8291dbefcc0SGleb Smirnoff 	}
830ff790bbaSJonathan T. Looney 	TAILQ_REMOVE(&bucket->head, fp, ipq_list);
831ff790bbaSJonathan T. Looney 	bucket->count--;
8321dbefcc0SGleb Smirnoff 	uma_zfree(V_ipq_zone, fp);
8331dbefcc0SGleb Smirnoff }
834ff790bbaSJonathan T. Looney 
835ff790bbaSJonathan T. Looney /*
836ff790bbaSJonathan T. Looney  * Get or set the maximum number of reassembly queues per bucket.
837ff790bbaSJonathan T. Looney  */
838ff790bbaSJonathan T. Looney static int
839ff790bbaSJonathan T. Looney sysctl_maxfragbucketsize(SYSCTL_HANDLER_ARGS)
840ff790bbaSJonathan T. Looney {
841ff790bbaSJonathan T. Looney 	int error, max;
842ff790bbaSJonathan T. Looney 
843ff790bbaSJonathan T. Looney 	max = V_ipreass_maxbucketsize;
844ff790bbaSJonathan T. Looney 	error = sysctl_handle_int(oidp, &max, 0, req);
845ff790bbaSJonathan T. Looney 	if (error || !req->newptr)
846ff790bbaSJonathan T. Looney 		return (error);
847ff790bbaSJonathan T. Looney 	if (max <= 0)
848ff790bbaSJonathan T. Looney 		return (EINVAL);
849ff790bbaSJonathan T. Looney 	V_ipreass_maxbucketsize = max;
850ff790bbaSJonathan T. Looney 	ipreass_drain_tomax();
851ff790bbaSJonathan T. Looney 	return (0);
852ff790bbaSJonathan T. Looney }
853