xref: /freebsd/sys/netinet/in_pcb.c (revision 266f97b5e9a7958e365e78288616a459b40d924a)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
42469dd60SGarrett Wollman  * Copyright (c) 1982, 1986, 1991, 1993, 1995
5497057eeSRobert Watson  *	The Regents of the University of California.
6111d57a6SRobert Watson  * Copyright (c) 2007-2009 Robert N. M. Watson
779bdc6e5SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8497057eeSRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
1079bdc6e5SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
1179bdc6e5SRobert Watson  * contract to Juniper Networks, Inc.
1279bdc6e5SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
372469dd60SGarrett Wollman  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
404b421e2dSMike Silbersack #include <sys/cdefs.h>
414b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
424b421e2dSMike Silbersack 
43497057eeSRobert Watson #include "opt_ddb.h"
446a800098SYoshinobu Inoue #include "opt_ipsec.h"
45efc76f72SBjoern A. Zeeb #include "opt_inet.h"
46cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
47f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
48*266f97b5SCy Schubert #include "opt_pcbgroup.h"
490c325f53SAlexander V. Chernikov #include "opt_route.h"
507527624eSRobert Watson #include "opt_rss.h"
51cfa1ca9dSYoshinobu Inoue 
52df8bae1dSRodney W. Grimes #include <sys/param.h>
53df8bae1dSRodney W. Grimes #include <sys/systm.h>
54cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
55df8bae1dSRodney W. Grimes #include <sys/malloc.h>
56df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
57aae49dd3SBjoern A. Zeeb #include <sys/callout.h>
58ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h>
59cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
60df8bae1dSRodney W. Grimes #include <sys/protosw.h>
613ee9c3c4SRandall Stewart #include <sys/smp.h>
62df8bae1dSRodney W. Grimes #include <sys/socket.h>
63df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
64f3e7afe2SHans Petter Selasky #include <sys/sockio.h>
65acd3428bSRobert Watson #include <sys/priv.h>
66df8bae1dSRodney W. Grimes #include <sys/proc.h>
6779bdc6e5SRobert Watson #include <sys/refcount.h>
6875c13541SPoul-Henning Kamp #include <sys/jail.h>
69101f9fc8SPeter Wemm #include <sys/kernel.h>
70101f9fc8SPeter Wemm #include <sys/sysctl.h>
718781d8e9SBruce Evans 
72497057eeSRobert Watson #ifdef DDB
73497057eeSRobert Watson #include <ddb/ddb.h>
74497057eeSRobert Watson #endif
75497057eeSRobert Watson 
7669c2d429SJeff Roberson #include <vm/uma.h>
77a034518aSAndrew Gallatin #include <vm/vm.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <net/if.h>
8076039bc8SGleb Smirnoff #include <net/if_var.h>
81cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
826d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h>
83df8bae1dSRodney W. Grimes #include <net/route.h>
84b2bdc62aSAdrian Chadd #include <net/rss_config.h>
85530c0060SRobert Watson #include <net/vnet.h>
86df8bae1dSRodney W. Grimes 
8767107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
88df8bae1dSRodney W. Grimes #include <netinet/in.h>
89df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
900f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h>
9159854ecfSHans Petter Selasky #ifdef INET
9259854ecfSHans Petter Selasky #include <netinet/in_var.h>
939ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h>
9459854ecfSHans Petter Selasky #endif
95df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
96340c35deSJonathan Lemon #include <netinet/tcp_var.h>
973ee9c3c4SRandall Stewart #ifdef TCPHPTS
983ee9c3c4SRandall Stewart #include <netinet/tcp_hpts.h>
993ee9c3c4SRandall Stewart #endif
1005f311da2SMike Silbersack #include <netinet/udp.h>
1015f311da2SMike Silbersack #include <netinet/udp_var.h>
102cfa1ca9dSYoshinobu Inoue #ifdef INET6
103cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
104efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
10567107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
10667107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
107cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1089ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h>
10959854ecfSHans Petter Selasky #endif
110cfa1ca9dSYoshinobu Inoue 
111fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
112b9234fafSSam Leffler 
113aed55708SRobert Watson #include <security/mac/mac_framework.h>
114aed55708SRobert Watson 
1151a43cff9SSean Bruno #define	INPCBLBGROUP_SIZMIN	8
1161a43cff9SSean Bruno #define	INPCBLBGROUP_SIZMAX	256
1171a43cff9SSean Bruno 
118aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
119aae49dd3SBjoern A. Zeeb 
120101f9fc8SPeter Wemm /*
121101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
122101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
123101f9fc8SPeter Wemm  */
124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
130101f9fc8SPeter Wemm 
131b0d22693SCrist J. Clark /*
132b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
133b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
134b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
135b0d22693SCrist J. Clark  */
136eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
137eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
138b0d22693SCrist J. Clark 
1395f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
140eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
141eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
142eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
143eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
144eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1455f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ipport_tcplastcount);
146eddfbb76SRobert Watson 
1471e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1486ac48b74SMike Silbersack 
149*266f97b5SCy Schubert static void	in_pcbremlists(struct inpcb *inp);
150d2025bd0SBjoern A. Zeeb #ifdef INET
151fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
152fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
153fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
154a034518aSAndrew Gallatin 			    int lookupflags, struct ifnet *ifp,
155a034518aSAndrew Gallatin 			    uint8_t numa_domain);
15667107f45SBjoern A. Zeeb 
157bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
158bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
159bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
160bbd42ad0SPeter Wemm 
161bbd42ad0SPeter Wemm static int
16282d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
163bbd42ad0SPeter Wemm {
16430a4ab08SBruce Evans 	int error;
16530a4ab08SBruce Evans 
166f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
16730a4ab08SBruce Evans 	if (error == 0) {
168603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
169603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
170603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
171603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
172603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
173603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
174bbd42ad0SPeter Wemm 	}
17530a4ab08SBruce Evans 	return (error);
176bbd42ad0SPeter Wemm }
177bbd42ad0SPeter Wemm 
178bbd42ad0SPeter Wemm #undef RANGECHK
179bbd42ad0SPeter Wemm 
1807029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange,
1817029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1826472ac3dSEd Schouten     "IP Ports");
18333b3ac06SPeter Wemm 
1846df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1857029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1867029da5cSPawel Biernacki     &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I",
1877029da5cSPawel Biernacki     "");
1886df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1897029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1907029da5cSPawel Biernacki     &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I",
1917029da5cSPawel Biernacki     "");
1926df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1937029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1947029da5cSPawel Biernacki     &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I",
1957029da5cSPawel Biernacki     "");
1966df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1977029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1987029da5cSPawel Biernacki     &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I",
1997029da5cSPawel Biernacki     "");
2006df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
2017029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2027029da5cSPawel Biernacki     &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I",
2037029da5cSPawel Biernacki     "");
2046df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
2057029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2067029da5cSPawel Biernacki     &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I",
2077029da5cSPawel Biernacki     "");
2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
2096df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
2106df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
212eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
2136df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
2146df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
215eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
2176df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
218eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
21927c4abc7SGordon Bergling 	"allocations before switching to a sequential one");
2206df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
2216df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
222eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
22327c4abc7SGordon Bergling 	"Minimum time to keep sequential port "
2246ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
22520abea66SRandall Stewart 
22620abea66SRandall Stewart #ifdef RATELIMIT
2271a714ff2SRandall Stewart counter_u64_t rate_limit_new;
2281a714ff2SRandall Stewart counter_u64_t rate_limit_chg;
22920abea66SRandall Stewart counter_u64_t rate_limit_active;
23020abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail;
23120abea66SRandall Stewart counter_u64_t rate_limit_set_ok;
23220abea66SRandall Stewart 
2337029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
23420abea66SRandall Stewart     "IP Rate Limiting");
23520abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD,
23620abea66SRandall Stewart     &rate_limit_active, "Active rate limited connections");
23720abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD,
23820abea66SRandall Stewart    &rate_limit_alloc_fail, "Rate limited connection failures");
23920abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD,
24020abea66SRandall Stewart    &rate_limit_set_ok, "Rate limited setting succeeded");
2411a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD,
2421a714ff2SRandall Stewart    &rate_limit_new, "Total Rate limit new attempts");
2431a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD,
2441a714ff2SRandall Stewart    &rate_limit_chg, "Total Rate limited change attempts");
2451a714ff2SRandall Stewart 
24620abea66SRandall Stewart #endif /* RATELIMIT */
24720abea66SRandall Stewart 
24883e521ecSBjoern A. Zeeb #endif /* INET */
2490312fbe9SPoul-Henning Kamp 
250c3229e05SDavid Greenman /*
251c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
252c3229e05SDavid Greenman  *
253de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
254de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
255de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
256c3229e05SDavid Greenman  */
257c3229e05SDavid Greenman 
2581a43cff9SSean Bruno static struct inpcblbgroup *
2591a43cff9SSean Bruno in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, u_char vflag,
260a034518aSAndrew Gallatin     uint16_t port, const union in_dependaddr *addr, int size,
261a034518aSAndrew Gallatin     uint8_t numa_domain)
2621a43cff9SSean Bruno {
2631a43cff9SSean Bruno 	struct inpcblbgroup *grp;
2641a43cff9SSean Bruno 	size_t bytes;
2651a43cff9SSean Bruno 
2661a43cff9SSean Bruno 	bytes = __offsetof(struct inpcblbgroup, il_inp[size]);
2671a43cff9SSean Bruno 	grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT);
2681a43cff9SSean Bruno 	if (!grp)
2691a43cff9SSean Bruno 		return (NULL);
2701a43cff9SSean Bruno 	grp->il_vflag = vflag;
2711a43cff9SSean Bruno 	grp->il_lport = port;
272a034518aSAndrew Gallatin 	grp->il_numa_domain = numa_domain;
2731a43cff9SSean Bruno 	grp->il_dependladdr = *addr;
2741a43cff9SSean Bruno 	grp->il_inpsiz = size;
27554af3d0dSMark Johnston 	CK_LIST_INSERT_HEAD(hdr, grp, il_list);
2761a43cff9SSean Bruno 	return (grp);
2771a43cff9SSean Bruno }
2781a43cff9SSean Bruno 
2791a43cff9SSean Bruno static void
28054af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx)
28154af3d0dSMark Johnston {
28254af3d0dSMark Johnston 	struct inpcblbgroup *grp;
28354af3d0dSMark Johnston 
28454af3d0dSMark Johnston 	grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx);
28554af3d0dSMark Johnston 	free(grp, M_PCB);
28654af3d0dSMark Johnston }
28754af3d0dSMark Johnston 
28854af3d0dSMark Johnston static void
2891a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp)
2901a43cff9SSean Bruno {
2911a43cff9SSean Bruno 
29254af3d0dSMark Johnston 	CK_LIST_REMOVE(grp, il_list);
2932a4bd982SGleb Smirnoff 	NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx);
2941a43cff9SSean Bruno }
2951a43cff9SSean Bruno 
2961a43cff9SSean Bruno static struct inpcblbgroup *
2971a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr,
2981a43cff9SSean Bruno     struct inpcblbgroup *old_grp, int size)
2991a43cff9SSean Bruno {
3001a43cff9SSean Bruno 	struct inpcblbgroup *grp;
3011a43cff9SSean Bruno 	int i;
3021a43cff9SSean Bruno 
3031a43cff9SSean Bruno 	grp = in_pcblbgroup_alloc(hdr, old_grp->il_vflag,
304a034518aSAndrew Gallatin 	    old_grp->il_lport, &old_grp->il_dependladdr, size,
305a034518aSAndrew Gallatin 	    old_grp->il_numa_domain);
30679ee680bSMark Johnston 	if (grp == NULL)
3071a43cff9SSean Bruno 		return (NULL);
3081a43cff9SSean Bruno 
3091a43cff9SSean Bruno 	KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
3101a43cff9SSean Bruno 	    ("invalid new local group size %d and old local group count %d",
3111a43cff9SSean Bruno 	     grp->il_inpsiz, old_grp->il_inpcnt));
3121a43cff9SSean Bruno 
3131a43cff9SSean Bruno 	for (i = 0; i < old_grp->il_inpcnt; ++i)
3141a43cff9SSean Bruno 		grp->il_inp[i] = old_grp->il_inp[i];
3151a43cff9SSean Bruno 	grp->il_inpcnt = old_grp->il_inpcnt;
3161a43cff9SSean Bruno 	in_pcblbgroup_free(old_grp);
3171a43cff9SSean Bruno 	return (grp);
3181a43cff9SSean Bruno }
3191a43cff9SSean Bruno 
3201a43cff9SSean Bruno /*
3211a43cff9SSean Bruno  * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i]
3221a43cff9SSean Bruno  * and shrink group if possible.
3231a43cff9SSean Bruno  */
3241a43cff9SSean Bruno static void
3251a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp,
3261a43cff9SSean Bruno     int i)
3271a43cff9SSean Bruno {
32879ee680bSMark Johnston 	struct inpcblbgroup *grp, *new_grp;
3291a43cff9SSean Bruno 
33079ee680bSMark Johnston 	grp = *grpp;
3311a43cff9SSean Bruno 	for (; i + 1 < grp->il_inpcnt; ++i)
3321a43cff9SSean Bruno 		grp->il_inp[i] = grp->il_inp[i + 1];
3331a43cff9SSean Bruno 	grp->il_inpcnt--;
3341a43cff9SSean Bruno 
3351a43cff9SSean Bruno 	if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN &&
33679ee680bSMark Johnston 	    grp->il_inpcnt <= grp->il_inpsiz / 4) {
3371a43cff9SSean Bruno 		/* Shrink this group. */
33879ee680bSMark Johnston 		new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2);
33979ee680bSMark Johnston 		if (new_grp != NULL)
3401a43cff9SSean Bruno 			*grpp = new_grp;
3411a43cff9SSean Bruno 	}
3421a43cff9SSean Bruno }
3431a43cff9SSean Bruno 
3441a43cff9SSean Bruno /*
3451a43cff9SSean Bruno  * Add PCB to load balance group for SO_REUSEPORT_LB option.
3461a43cff9SSean Bruno  */
3471a43cff9SSean Bruno static int
348a034518aSAndrew Gallatin in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain)
3491a43cff9SSean Bruno {
350a7026c7fSMark Johnston 	const static struct timeval interval = { 60, 0 };
351a7026c7fSMark Johnston 	static struct timeval lastprint;
3521a43cff9SSean Bruno 	struct inpcbinfo *pcbinfo;
3531a43cff9SSean Bruno 	struct inpcblbgrouphead *hdr;
3541a43cff9SSean Bruno 	struct inpcblbgroup *grp;
35579ee680bSMark Johnston 	uint32_t idx;
3561a43cff9SSean Bruno 
3571a43cff9SSean Bruno 	pcbinfo = inp->inp_pcbinfo;
3581a43cff9SSean Bruno 
3591a43cff9SSean Bruno 	INP_WLOCK_ASSERT(inp);
3601a43cff9SSean Bruno 	INP_HASH_WLOCK_ASSERT(pcbinfo);
3611a43cff9SSean Bruno 
3621a43cff9SSean Bruno 	/*
3631a43cff9SSean Bruno 	 * Don't allow jailed socket to join local group.
3641a43cff9SSean Bruno 	 */
36579ee680bSMark Johnston 	if (inp->inp_socket != NULL && jailed(inp->inp_socket->so_cred))
3661a43cff9SSean Bruno 		return (0);
3671a43cff9SSean Bruno 
3681a43cff9SSean Bruno #ifdef INET6
3691a43cff9SSean Bruno 	/*
3701a43cff9SSean Bruno 	 * Don't allow IPv4 mapped INET6 wild socket.
3711a43cff9SSean Bruno 	 */
3721a43cff9SSean Bruno 	if ((inp->inp_vflag & INP_IPV4) &&
3731a43cff9SSean Bruno 	    inp->inp_laddr.s_addr == INADDR_ANY &&
3741a43cff9SSean Bruno 	    INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) {
3751a43cff9SSean Bruno 		return (0);
3761a43cff9SSean Bruno 	}
3771a43cff9SSean Bruno #endif
3781a43cff9SSean Bruno 
3799d2877fcSMark Johnston 	idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask);
38079ee680bSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[idx];
38154af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
3821a43cff9SSean Bruno 		if (grp->il_vflag == inp->inp_vflag &&
3831a43cff9SSean Bruno 		    grp->il_lport == inp->inp_lport &&
384a034518aSAndrew Gallatin 		    grp->il_numa_domain == numa_domain &&
3851a43cff9SSean Bruno 		    memcmp(&grp->il_dependladdr,
3861a43cff9SSean Bruno 		    &inp->inp_inc.inc_ie.ie_dependladdr,
38779ee680bSMark Johnston 		    sizeof(grp->il_dependladdr)) == 0)
3881a43cff9SSean Bruno 			break;
3891a43cff9SSean Bruno 	}
3901a43cff9SSean Bruno 	if (grp == NULL) {
3911a43cff9SSean Bruno 		/* Create new load balance group. */
3921a43cff9SSean Bruno 		grp = in_pcblbgroup_alloc(hdr, inp->inp_vflag,
3931a43cff9SSean Bruno 		    inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
394a034518aSAndrew Gallatin 		    INPCBLBGROUP_SIZMIN, numa_domain);
39579ee680bSMark Johnston 		if (grp == NULL)
3961a43cff9SSean Bruno 			return (ENOBUFS);
3971a43cff9SSean Bruno 	} else if (grp->il_inpcnt == grp->il_inpsiz) {
3981a43cff9SSean Bruno 		if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) {
399a7026c7fSMark Johnston 			if (ratecheck(&lastprint, &interval))
4001a43cff9SSean Bruno 				printf("lb group port %d, limit reached\n",
4011a43cff9SSean Bruno 				    ntohs(grp->il_lport));
4021a43cff9SSean Bruno 			return (0);
4031a43cff9SSean Bruno 		}
4041a43cff9SSean Bruno 
4051a43cff9SSean Bruno 		/* Expand this local group. */
4061a43cff9SSean Bruno 		grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2);
40779ee680bSMark Johnston 		if (grp == NULL)
4081a43cff9SSean Bruno 			return (ENOBUFS);
4091a43cff9SSean Bruno 	}
4101a43cff9SSean Bruno 
4111a43cff9SSean Bruno 	KASSERT(grp->il_inpcnt < grp->il_inpsiz,
41279ee680bSMark Johnston 	    ("invalid local group size %d and count %d", grp->il_inpsiz,
41379ee680bSMark Johnston 	    grp->il_inpcnt));
4141a43cff9SSean Bruno 
4151a43cff9SSean Bruno 	grp->il_inp[grp->il_inpcnt] = inp;
4161a43cff9SSean Bruno 	grp->il_inpcnt++;
4171a43cff9SSean Bruno 	return (0);
4181a43cff9SSean Bruno }
4191a43cff9SSean Bruno 
4201a43cff9SSean Bruno /*
4211a43cff9SSean Bruno  * Remove PCB from load balance group.
4221a43cff9SSean Bruno  */
4231a43cff9SSean Bruno static void
4241a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp)
4251a43cff9SSean Bruno {
4261a43cff9SSean Bruno 	struct inpcbinfo *pcbinfo;
4271a43cff9SSean Bruno 	struct inpcblbgrouphead *hdr;
4281a43cff9SSean Bruno 	struct inpcblbgroup *grp;
4291a43cff9SSean Bruno 	int i;
4301a43cff9SSean Bruno 
4311a43cff9SSean Bruno 	pcbinfo = inp->inp_pcbinfo;
4321a43cff9SSean Bruno 
4331a43cff9SSean Bruno 	INP_WLOCK_ASSERT(inp);
4341a43cff9SSean Bruno 	INP_HASH_WLOCK_ASSERT(pcbinfo);
4351a43cff9SSean Bruno 
4361a43cff9SSean Bruno 	hdr = &pcbinfo->ipi_lbgrouphashbase[
4379d2877fcSMark Johnston 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)];
43854af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
4391a43cff9SSean Bruno 		for (i = 0; i < grp->il_inpcnt; ++i) {
4401a43cff9SSean Bruno 			if (grp->il_inp[i] != inp)
4411a43cff9SSean Bruno 				continue;
4421a43cff9SSean Bruno 
4431a43cff9SSean Bruno 			if (grp->il_inpcnt == 1) {
4441a43cff9SSean Bruno 				/* We are the last, free this local group. */
4451a43cff9SSean Bruno 				in_pcblbgroup_free(grp);
4461a43cff9SSean Bruno 			} else {
4471a43cff9SSean Bruno 				/* Pull up inpcbs, shrink group if possible. */
4481a43cff9SSean Bruno 				in_pcblbgroup_reorder(hdr, &grp, i);
4491a43cff9SSean Bruno 			}
4501a43cff9SSean Bruno 			return;
4511a43cff9SSean Bruno 		}
4521a43cff9SSean Bruno 	}
4531a43cff9SSean Bruno }
4541a43cff9SSean Bruno 
455a034518aSAndrew Gallatin int
456a034518aSAndrew Gallatin in_pcblbgroup_numa(struct inpcb *inp, int arg)
457a034518aSAndrew Gallatin {
458a034518aSAndrew Gallatin 	struct inpcbinfo *pcbinfo;
459a034518aSAndrew Gallatin 	struct inpcblbgrouphead *hdr;
460a034518aSAndrew Gallatin 	struct inpcblbgroup *grp;
461a034518aSAndrew Gallatin 	int err, i;
462a034518aSAndrew Gallatin 	uint8_t numa_domain;
463a034518aSAndrew Gallatin 
464a034518aSAndrew Gallatin 	switch (arg) {
465a034518aSAndrew Gallatin 	case TCP_REUSPORT_LB_NUMA_NODOM:
466a034518aSAndrew Gallatin 		numa_domain = M_NODOM;
467a034518aSAndrew Gallatin 		break;
468a034518aSAndrew Gallatin 	case TCP_REUSPORT_LB_NUMA_CURDOM:
469a034518aSAndrew Gallatin 		numa_domain = PCPU_GET(domain);
470a034518aSAndrew Gallatin 		break;
471a034518aSAndrew Gallatin 	default:
472a034518aSAndrew Gallatin 		if (arg < 0 || arg >= vm_ndomains)
473a034518aSAndrew Gallatin 			return (EINVAL);
474a034518aSAndrew Gallatin 		numa_domain = arg;
475a034518aSAndrew Gallatin 	}
476a034518aSAndrew Gallatin 
477a034518aSAndrew Gallatin 	err = 0;
478a034518aSAndrew Gallatin 	pcbinfo = inp->inp_pcbinfo;
479a034518aSAndrew Gallatin 	INP_WLOCK_ASSERT(inp);
480a034518aSAndrew Gallatin 	INP_HASH_WLOCK(pcbinfo);
481a034518aSAndrew Gallatin 	hdr = &pcbinfo->ipi_lbgrouphashbase[
482a034518aSAndrew Gallatin 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)];
483a034518aSAndrew Gallatin 	CK_LIST_FOREACH(grp, hdr, il_list) {
484a034518aSAndrew Gallatin 		for (i = 0; i < grp->il_inpcnt; ++i) {
485a034518aSAndrew Gallatin 			if (grp->il_inp[i] != inp)
486a034518aSAndrew Gallatin 				continue;
487a034518aSAndrew Gallatin 
488a034518aSAndrew Gallatin 			if (grp->il_numa_domain == numa_domain) {
489a034518aSAndrew Gallatin 				goto abort_with_hash_wlock;
490a034518aSAndrew Gallatin 			}
491a034518aSAndrew Gallatin 
492a034518aSAndrew Gallatin 			/* Remove it from the old group. */
493a034518aSAndrew Gallatin 			in_pcbremlbgrouphash(inp);
494a034518aSAndrew Gallatin 
495a034518aSAndrew Gallatin 			/* Add it to the new group based on numa domain. */
496a034518aSAndrew Gallatin 			in_pcbinslbgrouphash(inp, numa_domain);
497a034518aSAndrew Gallatin 			goto abort_with_hash_wlock;
498a034518aSAndrew Gallatin 		}
499a034518aSAndrew Gallatin 	}
500a034518aSAndrew Gallatin 	err = ENOENT;
501a034518aSAndrew Gallatin abort_with_hash_wlock:
502a034518aSAndrew Gallatin 	INP_HASH_WUNLOCK(pcbinfo);
503a034518aSAndrew Gallatin 	return (err);
504a034518aSAndrew Gallatin }
505a034518aSAndrew Gallatin 
506c3229e05SDavid Greenman /*
507cc487c16SGleb Smirnoff  * Different protocols initialize their inpcbs differently - giving
508cc487c16SGleb Smirnoff  * different name to the lock.  But they all are disposed the same.
509cc487c16SGleb Smirnoff  */
510cc487c16SGleb Smirnoff static void
511cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size)
512cc487c16SGleb Smirnoff {
513cc487c16SGleb Smirnoff 	struct inpcb *inp = mem;
514cc487c16SGleb Smirnoff 
515cc487c16SGleb Smirnoff 	INP_LOCK_DESTROY(inp);
516cc487c16SGleb Smirnoff }
517cc487c16SGleb Smirnoff 
518cc487c16SGleb Smirnoff /*
5199bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
5209bcd427bSRobert Watson  * arguments in time.
5219bcd427bSRobert Watson  */
5229bcd427bSRobert Watson void
5239bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
524*266f97b5SCy Schubert     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
525*266f97b5SCy Schubert     char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields)
5269bcd427bSRobert Watson {
5279bcd427bSRobert Watson 
528*266f97b5SCy Schubert 	porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1);
529*266f97b5SCy Schubert 
530*266f97b5SCy Schubert 	INP_INFO_LOCK_INIT(pcbinfo, name);
531*266f97b5SCy Schubert 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
532*266f97b5SCy Schubert 	INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
5339bcd427bSRobert Watson #ifdef VIMAGE
5349bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
5359bcd427bSRobert Watson #endif
536*266f97b5SCy Schubert 	pcbinfo->ipi_listhead = listhead;
537*266f97b5SCy Schubert 	CK_LIST_INIT(pcbinfo->ipi_listhead);
538fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
5399bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
5409bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
5419bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
5429bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
5439d2877fcSMark Johnston 	pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB,
5441a43cff9SSean Bruno 	    &pcbinfo->ipi_lbgrouphashmask);
545*266f97b5SCy Schubert #ifdef PCBGROUP
546*266f97b5SCy Schubert 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
547*266f97b5SCy Schubert #endif
5489bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
549*266f97b5SCy Schubert 	    NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0);
5509bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
5516acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
5526acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
5539bcd427bSRobert Watson }
5549bcd427bSRobert Watson 
5559bcd427bSRobert Watson /*
5569bcd427bSRobert Watson  * Destroy an inpcbinfo.
5579bcd427bSRobert Watson  */
5589bcd427bSRobert Watson void
5599bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
5609bcd427bSRobert Watson {
5619bcd427bSRobert Watson 
562fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
563fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
564fa046d87SRobert Watson 
5659bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
5669bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
5679bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
5681a43cff9SSean Bruno 	hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB,
5691a43cff9SSean Bruno 	    pcbinfo->ipi_lbgrouphashmask);
570*266f97b5SCy Schubert #ifdef PCBGROUP
571*266f97b5SCy Schubert 	in_pcbgroup_destroy(pcbinfo);
572*266f97b5SCy Schubert #endif
5739bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
574*266f97b5SCy Schubert 	INP_LIST_LOCK_DESTROY(pcbinfo);
575*266f97b5SCy Schubert 	INP_HASH_LOCK_DESTROY(pcbinfo);
576*266f97b5SCy Schubert 	INP_INFO_LOCK_DESTROY(pcbinfo);
5779bcd427bSRobert Watson }
5789bcd427bSRobert Watson 
5799bcd427bSRobert Watson /*
580c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
581d915b280SStephan Uphoff  * On success return with the PCB locked.
582c3229e05SDavid Greenman  */
583df8bae1dSRodney W. Grimes int
584d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
585df8bae1dSRodney W. Grimes {
586136d4f1cSRobert Watson 	struct inpcb *inp;
58713cf67f3SHajimu UMEMOTO 	int error;
588a557af22SRobert Watson 
589a557af22SRobert Watson 	error = 0;
590*266f97b5SCy Schubert 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
591df8bae1dSRodney W. Grimes 	if (inp == NULL)
592df8bae1dSRodney W. Grimes 		return (ENOBUFS);
5936a6cefacSGleb Smirnoff 	bzero(&inp->inp_start_zero, inp_zero_size);
59450575ce1SAndrew Gallatin #ifdef NUMA
59550575ce1SAndrew Gallatin 	inp->inp_numa_domain = M_NODOM;
59650575ce1SAndrew Gallatin #endif
59715bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
598df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
59986d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
6008b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
601a557af22SRobert Watson #ifdef MAC
60230d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
603a557af22SRobert Watson 	if (error != 0)
604a557af22SRobert Watson 		goto out;
60530d239bcSRobert Watson 	mac_inpcb_create(so, inp);
606a557af22SRobert Watson #endif
607fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
608fcf59617SAndrey V. Elsukov 	error = ipsec_init_pcbpolicy(inp);
6090bffde27SRobert Watson 	if (error != 0) {
6100bffde27SRobert Watson #ifdef MAC
6110bffde27SRobert Watson 		mac_inpcb_destroy(inp);
6120bffde27SRobert Watson #endif
613a557af22SRobert Watson 		goto out;
6140bffde27SRobert Watson 	}
615b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
616e3fd5ffdSRobert Watson #ifdef INET6
617340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
618340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
619603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
62033841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
621340c35deSJonathan Lemon 	}
622*266f97b5SCy Schubert #endif
623*266f97b5SCy Schubert 	INP_WLOCK(inp);
624*266f97b5SCy Schubert 	INP_LIST_WLOCK(pcbinfo);
625*266f97b5SCy Schubert 	CK_LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
626*266f97b5SCy Schubert 	pcbinfo->ipi_count++;
627*266f97b5SCy Schubert 	so->so_pcb = (caddr_t)inp;
628*266f97b5SCy Schubert #ifdef INET6
629603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
63033841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
63133841545SHajimu UMEMOTO #endif
632*266f97b5SCy Schubert 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
633*266f97b5SCy Schubert 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
634*266f97b5SCy Schubert 
6358c1960d5SMike Karels 	/*
6368c1960d5SMike Karels 	 * Routes in inpcb's can cache L2 as well; they are guaranteed
6378c1960d5SMike Karels 	 * to be cleaned up.
6388c1960d5SMike Karels 	 */
6398c1960d5SMike Karels 	inp->inp_route.ro_flags = RT_LLE_CACHE;
640*266f97b5SCy Schubert 	INP_LIST_WUNLOCK(pcbinfo);
6417a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC)
642a557af22SRobert Watson out:
643*266f97b5SCy Schubert 	if (error != 0) {
64486d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
645*266f97b5SCy Schubert 		uma_zfree(pcbinfo->ipi_zone, inp);
646*266f97b5SCy Schubert 	}
647de2d4784SGleb Smirnoff #endif
648*266f97b5SCy Schubert 	return (error);
649df8bae1dSRodney W. Grimes }
650df8bae1dSRodney W. Grimes 
65167107f45SBjoern A. Zeeb #ifdef INET
652df8bae1dSRodney W. Grimes int
653136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
654df8bae1dSRodney W. Grimes {
6554b932371SIan Dowse 	int anonport, error;
6564b932371SIan Dowse 
657f161d294SMark Johnston 	KASSERT(nam == NULL || nam->sa_family == AF_INET,
658f161d294SMark Johnston 	    ("%s: invalid address family for %p", __func__, nam));
659f161d294SMark Johnston 	KASSERT(nam == NULL || nam->sa_len == sizeof(struct sockaddr_in),
660f161d294SMark Johnston 	    ("%s: invalid address length for %p", __func__, nam));
6618501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
662fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
66359daba27SSam Leffler 
6644b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
6654b932371SIan Dowse 		return (EINVAL);
6665cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
6674b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
668b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
6694b932371SIan Dowse 	if (error)
6704b932371SIan Dowse 		return (error);
6714b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
6724b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
6734b932371SIan Dowse 		inp->inp_lport = 0;
6744b932371SIan Dowse 		return (EAGAIN);
6754b932371SIan Dowse 	}
6764b932371SIan Dowse 	if (anonport)
6774b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
6784b932371SIan Dowse 	return (0);
6794b932371SIan Dowse }
68067107f45SBjoern A. Zeeb #endif
6814b932371SIan Dowse 
682efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
68325102351SMike Karels /*
68425102351SMike Karels  * Assign a local port like in_pcb_lport(), but also used with connect()
68525102351SMike Karels  * and a foreign address and port.  If fsa is non-NULL, choose a local port
68625102351SMike Karels  * that is unused with those, otherwise one that is completely unused.
6872fdbcbeaSMike Karels  * lsa can be NULL for IPv6.
68825102351SMike Karels  */
689efc76f72SBjoern A. Zeeb int
69025102351SMike Karels in_pcb_lport_dest(struct inpcb *inp, struct sockaddr *lsa, u_short *lportp,
69125102351SMike Karels     struct sockaddr *fsa, u_short fport, struct ucred *cred, int lookupflags)
692efc76f72SBjoern A. Zeeb {
693efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
694efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
695efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
696efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
697efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
698efc76f72SBjoern A. Zeeb #ifdef INET
69925102351SMike Karels 	struct in_addr laddr, faddr;
70025102351SMike Karels #endif
70125102351SMike Karels #ifdef INET6
70225102351SMike Karels 	struct in6_addr *laddr6, *faddr6;
703efc76f72SBjoern A. Zeeb #endif
704efc76f72SBjoern A. Zeeb 
705efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
706efc76f72SBjoern A. Zeeb 
707efc76f72SBjoern A. Zeeb 	/*
708efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
709efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
710efc76f72SBjoern A. Zeeb 	 */
711efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
712fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
713efc76f72SBjoern A. Zeeb 
714efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
715efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
716efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
717efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
718efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
719cc426dd3SMateusz Guzik 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT);
720efc76f72SBjoern A. Zeeb 		if (error)
721efc76f72SBjoern A. Zeeb 			return (error);
722efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
723efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
724efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
725efc76f72SBjoern A. Zeeb 	} else {
726efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
727efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
728efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
729efc76f72SBjoern A. Zeeb 	}
730efc76f72SBjoern A. Zeeb 	/*
731e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
732efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
733efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
734efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
735efc76f72SBjoern A. Zeeb 	 */
736efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
737e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
738e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
739efc76f72SBjoern A. Zeeb 		dorandom = 1;
740efc76f72SBjoern A. Zeeb 	else
741efc76f72SBjoern A. Zeeb 		dorandom = 0;
742efc76f72SBjoern A. Zeeb 	/*
743efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
744efc76f72SBjoern A. Zeeb 	 * we have the only port available.
745efc76f72SBjoern A. Zeeb 	 */
746efc76f72SBjoern A. Zeeb 	if (first == last)
747efc76f72SBjoern A. Zeeb 		dorandom = 0;
748e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
749e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
750efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
751efc76f72SBjoern A. Zeeb 	/*
752efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
753efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
754efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
755efc76f72SBjoern A. Zeeb 	 */
756efc76f72SBjoern A. Zeeb 	if (first > last) {
757efc76f72SBjoern A. Zeeb 		aux = first;
758efc76f72SBjoern A. Zeeb 		first = last;
759efc76f72SBjoern A. Zeeb 		last = aux;
760efc76f72SBjoern A. Zeeb 	}
761efc76f72SBjoern A. Zeeb 
762efc76f72SBjoern A. Zeeb #ifdef INET
76325102351SMike Karels 	laddr.s_addr = INADDR_ANY;
7644d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
7652fdbcbeaSMike Karels 		if (lsa != NULL)
76625102351SMike Karels 			laddr = ((struct sockaddr_in *)lsa)->sin_addr;
76725102351SMike Karels 		if (fsa != NULL)
76825102351SMike Karels 			faddr = ((struct sockaddr_in *)fsa)->sin_addr;
769efc76f72SBjoern A. Zeeb 	}
770efc76f72SBjoern A. Zeeb #endif
77125102351SMike Karels #ifdef INET6
7722fdbcbeaSMike Karels 	laddr6 = NULL;
7732fdbcbeaSMike Karels 	if ((inp->inp_vflag & INP_IPV6) != 0) {
7742fdbcbeaSMike Karels 		if (lsa != NULL)
77525102351SMike Karels 			laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr;
77625102351SMike Karels 		if (fsa != NULL)
77725102351SMike Karels 			faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr;
77825102351SMike Karels 	}
77925102351SMike Karels #endif
78025102351SMike Karels 
78125102351SMike Karels 	tmpinp = NULL;
782efc76f72SBjoern A. Zeeb 	lport = *lportp;
783efc76f72SBjoern A. Zeeb 
784efc76f72SBjoern A. Zeeb 	if (dorandom)
785efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
786efc76f72SBjoern A. Zeeb 
787efc76f72SBjoern A. Zeeb 	count = last - first;
788efc76f72SBjoern A. Zeeb 
789efc76f72SBjoern A. Zeeb 	do {
790efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
791efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
792efc76f72SBjoern A. Zeeb 		++*lastport;
793efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
794efc76f72SBjoern A. Zeeb 			*lastport = first;
795efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
796efc76f72SBjoern A. Zeeb 
79725102351SMike Karels 		if (fsa != NULL) {
79825102351SMike Karels #ifdef INET
79925102351SMike Karels 			if (lsa->sa_family == AF_INET) {
80025102351SMike Karels 				tmpinp = in_pcblookup_hash_locked(pcbinfo,
80125102351SMike Karels 				    faddr, fport, laddr, lport, lookupflags,
802a034518aSAndrew Gallatin 				    NULL, M_NODOM);
80325102351SMike Karels 			}
80425102351SMike Karels #endif
80525102351SMike Karels #ifdef INET6
80625102351SMike Karels 			if (lsa->sa_family == AF_INET6) {
80725102351SMike Karels 				tmpinp = in6_pcblookup_hash_locked(pcbinfo,
80825102351SMike Karels 				    faddr6, fport, laddr6, lport, lookupflags,
809a034518aSAndrew Gallatin 				    NULL, M_NODOM);
81025102351SMike Karels 			}
81125102351SMike Karels #endif
81225102351SMike Karels 		} else {
813efc76f72SBjoern A. Zeeb #ifdef INET6
8144616026fSErmal Luçi 			if ((inp->inp_vflag & INP_IPV6) != 0)
815efc76f72SBjoern A. Zeeb 				tmpinp = in6_pcblookup_local(pcbinfo,
81668e0d7e0SRobert Watson 				    &inp->in6p_laddr, lport, lookupflags, cred);
817efc76f72SBjoern A. Zeeb #endif
818efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
819efc76f72SBjoern A. Zeeb 			else
820efc76f72SBjoern A. Zeeb #endif
821efc76f72SBjoern A. Zeeb #ifdef INET
822efc76f72SBjoern A. Zeeb 				tmpinp = in_pcblookup_local(pcbinfo, laddr,
82368e0d7e0SRobert Watson 				    lport, lookupflags, cred);
824efc76f72SBjoern A. Zeeb #endif
82525102351SMike Karels 		}
826efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
827efc76f72SBjoern A. Zeeb 
828efc76f72SBjoern A. Zeeb 	*lportp = lport;
829efc76f72SBjoern A. Zeeb 
830efc76f72SBjoern A. Zeeb 	return (0);
831efc76f72SBjoern A. Zeeb }
832efdf104bSMikolaj Golub 
833efdf104bSMikolaj Golub /*
83425102351SMike Karels  * Select a local port (number) to use.
83525102351SMike Karels  */
83625102351SMike Karels int
83725102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
83825102351SMike Karels     struct ucred *cred, int lookupflags)
83925102351SMike Karels {
84025102351SMike Karels 	struct sockaddr_in laddr;
84125102351SMike Karels 
84225102351SMike Karels 	if (laddrp) {
84325102351SMike Karels 		bzero(&laddr, sizeof(laddr));
84425102351SMike Karels 		laddr.sin_family = AF_INET;
84525102351SMike Karels 		laddr.sin_addr = *laddrp;
84625102351SMike Karels 	}
84725102351SMike Karels 	return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr :
84825102351SMike Karels 	    NULL, lportp, NULL, 0, cred, lookupflags));
84925102351SMike Karels }
85025102351SMike Karels 
85125102351SMike Karels /*
852efdf104bSMikolaj Golub  * Return cached socket options.
853efdf104bSMikolaj Golub  */
8541a43cff9SSean Bruno int
855efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
856efdf104bSMikolaj Golub {
8571a43cff9SSean Bruno 	int so_options;
858efdf104bSMikolaj Golub 
859efdf104bSMikolaj Golub 	so_options = 0;
860efdf104bSMikolaj Golub 
8611a43cff9SSean Bruno 	if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0)
8621a43cff9SSean Bruno 		so_options |= SO_REUSEPORT_LB;
863efdf104bSMikolaj Golub 	if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
864efdf104bSMikolaj Golub 		so_options |= SO_REUSEPORT;
865efdf104bSMikolaj Golub 	if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
866efdf104bSMikolaj Golub 		so_options |= SO_REUSEADDR;
867efdf104bSMikolaj Golub 	return (so_options);
868efdf104bSMikolaj Golub }
869efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
870efc76f72SBjoern A. Zeeb 
8714b932371SIan Dowse /*
8720a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
8730a100a6fSAdrian Chadd  *
8740a100a6fSAdrian Chadd  * ni points to the new inp.
8750a100a6fSAdrian Chadd  * oi points to the exisitng inp.
8760a100a6fSAdrian Chadd  *
8770a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
8780a100a6fSAdrian Chadd  * whether the credentials match.
8790a100a6fSAdrian Chadd  */
880d5bb8bd3SAdrian Chadd int
8810a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
8820a100a6fSAdrian Chadd {
8830a100a6fSAdrian Chadd 	/* Check permissions match */
8840a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
8850a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
8860a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
8870a100a6fSAdrian Chadd 		return (0);
8880a100a6fSAdrian Chadd 
8890a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
8900a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
8910a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
8920a100a6fSAdrian Chadd 		return (0);
8930a100a6fSAdrian Chadd 
8940a100a6fSAdrian Chadd 	/*
8950a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
8960a100a6fSAdrian Chadd 	 * it is and it matches the checks.
8970a100a6fSAdrian Chadd 	 */
8980a100a6fSAdrian Chadd 	return (1);
8990a100a6fSAdrian Chadd }
9000a100a6fSAdrian Chadd 
901d5bb8bd3SAdrian Chadd #ifdef INET
9020a100a6fSAdrian Chadd /*
9034b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
9044b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
9054b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
9064b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
9074b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
9084b932371SIan Dowse  *
9094b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
9104b932371SIan Dowse  */
9114b932371SIan Dowse int
912136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
913136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
9144b932371SIan Dowse {
9154b932371SIan Dowse 	struct socket *so = inp->inp_socket;
91615bd2b43SDavid Greenman 	struct sockaddr_in *sin;
917c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
9184b932371SIan Dowse 	struct in_addr laddr;
919df8bae1dSRodney W. Grimes 	u_short lport = 0;
92068e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
921413628a7SBjoern A. Zeeb 	int error;
922df8bae1dSRodney W. Grimes 
9238501a69cSRobert Watson 	/*
9241a43cff9SSean Bruno 	 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
9251a43cff9SSean Bruno 	 * so that we don't have to add to the (already messy) code below.
9261a43cff9SSean Bruno 	 */
9271a43cff9SSean Bruno 	int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
9281a43cff9SSean Bruno 
9291a43cff9SSean Bruno 	/*
930fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
9318501a69cSRobert Watson 	 */
93259daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
933fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
93459daba27SSam Leffler 
9354b932371SIan Dowse 	laddr.s_addr = *laddrp;
9364b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
937df8bae1dSRodney W. Grimes 		return (EINVAL);
9381a43cff9SSean Bruno 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
93968e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
9404616026fSErmal Luçi 	if (nam == NULL) {
9417c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
9427c2f3cb9SJamie Gritton 			return (error);
9437c2f3cb9SJamie Gritton 	} else {
94457bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
945f161d294SMark Johnston 		KASSERT(sin->sin_family == AF_INET,
946f161d294SMark Johnston 		    ("%s: invalid family for address %p", __func__, sin));
947f161d294SMark Johnston 		KASSERT(sin->sin_len == sizeof(*sin),
948f161d294SMark Johnston 		    ("%s: invalid length for address %p", __func__, sin));
949f161d294SMark Johnston 
950b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
951b89e82ddSJamie Gritton 		if (error)
952b89e82ddSJamie Gritton 			return (error);
9534b932371SIan Dowse 		if (sin->sin_port != *lportp) {
9544b932371SIan Dowse 			/* Don't allow the port to change. */
9554b932371SIan Dowse 			if (*lportp != 0)
9564b932371SIan Dowse 				return (EINVAL);
957df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
9584b932371SIan Dowse 		}
9594b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
960df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
961df8bae1dSRodney W. Grimes 			/*
962df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
963df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
964df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
965df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
966df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
967df8bae1dSRodney W. Grimes 			 */
968f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
969df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
9701a43cff9SSean Bruno 			/*
9711a43cff9SSean Bruno 			 * XXX: How to deal with SO_REUSEPORT_LB here?
9721a43cff9SSean Bruno 			 * Treat same as SO_REUSEPORT for now.
9731a43cff9SSean Bruno 			 */
9741a43cff9SSean Bruno 			if ((so->so_options &
9751a43cff9SSean Bruno 			    (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
9761a43cff9SSean Bruno 				reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
977df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
978df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
97983103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
9804209e01aSAdrian Chadd 			/*
9814209e01aSAdrian Chadd 			 * Is the address a local IP address?
982f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
9838696873dSAdrian Chadd 			 * to any endpoint address, local or not.
9844209e01aSAdrian Chadd 			 */
985f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
9868896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
987df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
988df8bae1dSRodney W. Grimes 		}
9894b932371SIan Dowse 		laddr = sin->sin_addr;
990df8bae1dSRodney W. Grimes 		if (lport) {
991df8bae1dSRodney W. Grimes 			struct inpcb *t;
992ae0e7143SRobert Watson 			struct tcptw *tw;
993ae0e7143SRobert Watson 
994df8bae1dSRodney W. Grimes 			/* GROSS */
995603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
996603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
997cc426dd3SMateusz Guzik 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
9982469dd60SGarrett Wollman 				return (EACCES);
999835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
1000cc426dd3SMateusz Guzik 			    priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
1001078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
1002413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
1003340c35deSJonathan Lemon 	/*
1004340c35deSJonathan Lemon 	 * XXX
1005340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
1006340c35deSJonathan Lemon 	 */
10074cc20ab1SSeigo Tanimura 				if (t &&
10080a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
1009ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
10104658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
10114658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
10124cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
101352b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
10141a43cff9SSean Bruno 				     (t->inp_flags2 & INP_REUSEPORT) ||
10151a43cff9SSean Bruno 				     (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
101686d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
101786d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
10184049a042SGuido van Rooij 					return (EADDRINUSE);
10190a100a6fSAdrian Chadd 
10200a100a6fSAdrian Chadd 				/*
10210a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
10220a100a6fSAdrian Chadd 				 * the credentials need to match and the
10230a100a6fSAdrian Chadd 				 * original socket also has to have been bound
10240a100a6fSAdrian Chadd 				 * with BINDMULTI.
10250a100a6fSAdrian Chadd 				 */
10260a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
10270a100a6fSAdrian Chadd 					return (EADDRINUSE);
10284049a042SGuido van Rooij 			}
1029c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
103068e0d7e0SRobert Watson 			    lport, lookupflags, cred);
1031ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
1032ae0e7143SRobert Watson 				/*
1033ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
1034ae0e7143SRobert Watson 				 * state recycled, we treat the address as
1035ae0e7143SRobert Watson 				 * being in use (for now).  This is better
1036ae0e7143SRobert Watson 				 * than a panic, but not desirable.
1037ae0e7143SRobert Watson 				 */
1038ec95b709SMikolaj Golub 				tw = intotw(t);
1039ae0e7143SRobert Watson 				if (tw == NULL ||
10401a43cff9SSean Bruno 				    ((reuseport & tw->tw_so_options) == 0 &&
10411a43cff9SSean Bruno 					(reuseport_lb &
10421a43cff9SSean Bruno 				            tw->tw_so_options) == 0)) {
1043340c35deSJonathan Lemon 					return (EADDRINUSE);
10441a43cff9SSean Bruno 				}
10450a100a6fSAdrian Chadd 			} else if (t &&
10460a100a6fSAdrian Chadd 				   ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
10471a43cff9SSean Bruno 				   (reuseport & inp_so_options(t)) == 0 &&
10481a43cff9SSean Bruno 				   (reuseport_lb & inp_so_options(t)) == 0) {
1049e3fd5ffdSRobert Watson #ifdef INET6
105033841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
1051cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
1052cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
1053cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
1054fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
1055fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
1056e3fd5ffdSRobert Watson #endif
1057df8bae1dSRodney W. Grimes 						return (EADDRINUSE);
10580a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
10590a100a6fSAdrian Chadd 					return (EADDRINUSE);
1060df8bae1dSRodney W. Grimes 			}
1061cfa1ca9dSYoshinobu Inoue 		}
1062df8bae1dSRodney W. Grimes 	}
10634b932371SIan Dowse 	if (*lportp != 0)
10644b932371SIan Dowse 		lport = *lportp;
106533b3ac06SPeter Wemm 	if (lport == 0) {
10664616026fSErmal Luçi 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
1067efc76f72SBjoern A. Zeeb 		if (error != 0)
1068efc76f72SBjoern A. Zeeb 			return (error);
106933b3ac06SPeter Wemm 	}
10704b932371SIan Dowse 	*laddrp = laddr.s_addr;
10714b932371SIan Dowse 	*lportp = lport;
1072df8bae1dSRodney W. Grimes 	return (0);
1073df8bae1dSRodney W. Grimes }
1074df8bae1dSRodney W. Grimes 
1075999f1343SGarrett Wollman /*
10765200e00eSIan Dowse  * Connect from a socket to a specified address.
10775200e00eSIan Dowse  * Both address and port must be specified in argument sin.
10785200e00eSIan Dowse  * If don't have a local address for this socket yet,
10795200e00eSIan Dowse  * then pick one.
1080999f1343SGarrett Wollman  */
1081999f1343SGarrett Wollman int
1082*266f97b5SCy Schubert in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
1083*266f97b5SCy Schubert     struct ucred *cred, struct mbuf *m, bool rehash)
1084999f1343SGarrett Wollman {
10855200e00eSIan Dowse 	u_short lport, fport;
10865200e00eSIan Dowse 	in_addr_t laddr, faddr;
10875200e00eSIan Dowse 	int anonport, error;
1088df8bae1dSRodney W. Grimes 
10898501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1090fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
109127f74fd0SRobert Watson 
10925200e00eSIan Dowse 	lport = inp->inp_lport;
10935200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
10945200e00eSIan Dowse 	anonport = (lport == 0);
10955200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
1096b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
10975200e00eSIan Dowse 	if (error)
10985200e00eSIan Dowse 		return (error);
10995200e00eSIan Dowse 
11005200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
11015200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
1102fe1274eeSMichael Tuexen 		KASSERT(rehash == true,
1103fe1274eeSMichael Tuexen 		    ("Rehashing required for unbound inps"));
11045200e00eSIan Dowse 		inp->inp_lport = lport;
11055200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
11065200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
11075200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
11085200e00eSIan Dowse 			inp->inp_lport = 0;
11095200e00eSIan Dowse 			return (EAGAIN);
11105200e00eSIan Dowse 		}
11115200e00eSIan Dowse 	}
11125200e00eSIan Dowse 
11135200e00eSIan Dowse 	/* Commit the remaining changes. */
11145200e00eSIan Dowse 	inp->inp_lport = lport;
11155200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
11165200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
11175200e00eSIan Dowse 	inp->inp_fport = fport;
1118fe1274eeSMichael Tuexen 	if (rehash) {
1119*266f97b5SCy Schubert 		in_pcbrehash_mbuf(inp, m);
1120fe1274eeSMichael Tuexen 	} else {
1121*266f97b5SCy Schubert 		in_pcbinshash_mbuf(inp, m);
1122fe1274eeSMichael Tuexen 	}
11232cb64cb2SGeorge V. Neville-Neil 
11245200e00eSIan Dowse 	if (anonport)
11255200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
11265200e00eSIan Dowse 	return (0);
11275200e00eSIan Dowse }
11285200e00eSIan Dowse 
1129*266f97b5SCy Schubert int
1130*266f97b5SCy Schubert in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
1131*266f97b5SCy Schubert {
1132*266f97b5SCy Schubert 
1133*266f97b5SCy Schubert 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL, true));
1134*266f97b5SCy Schubert }
1135*266f97b5SCy Schubert 
11365200e00eSIan Dowse /*
11370895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
11380895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
11390895aec3SBjoern A. Zeeb  */
1140ae190832SSteven Hartland int
11410895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
11420895aec3SBjoern A. Zeeb     struct ucred *cred)
11430895aec3SBjoern A. Zeeb {
11440895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
11450895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
11469ac7c6cfSAlexander V. Chernikov 	struct sockaddr_in *sin, dst;
11479ac7c6cfSAlexander V. Chernikov 	struct nhop_object *nh;
11480895aec3SBjoern A. Zeeb 	int error;
11490895aec3SBjoern A. Zeeb 
1150c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
1151413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
1152592bcae8SBjoern A. Zeeb 	/*
1153592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
1154592bcae8SBjoern A. Zeeb 	 * if requested.
1155592bcae8SBjoern A. Zeeb 	 */
1156592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
1157592bcae8SBjoern A. Zeeb 		return (0);
1158592bcae8SBjoern A. Zeeb 
11590895aec3SBjoern A. Zeeb 	error = 0;
11600895aec3SBjoern A. Zeeb 
11619ac7c6cfSAlexander V. Chernikov 	nh = NULL;
11629ac7c6cfSAlexander V. Chernikov 	bzero(&dst, sizeof(dst));
11639ac7c6cfSAlexander V. Chernikov 	sin = &dst;
11640895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
11650895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
11660895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
11670895aec3SBjoern A. Zeeb 
11680895aec3SBjoern A. Zeeb 	/*
11690895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
11700895aec3SBjoern A. Zeeb 	 * else punt.
11710895aec3SBjoern A. Zeeb 	 *
11720895aec3SBjoern A. Zeeb 	 * Find out route to destination.
11730895aec3SBjoern A. Zeeb 	 */
11740895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
11759ac7c6cfSAlexander V. Chernikov 		nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr,
11769ac7c6cfSAlexander V. Chernikov 		    0, NHR_NONE, 0);
11770895aec3SBjoern A. Zeeb 
11780895aec3SBjoern A. Zeeb 	/*
11790895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
11800895aec3SBjoern A. Zeeb 	 * the outgoing interface.
11810895aec3SBjoern A. Zeeb 	 *
11820895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
11830895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
11840895aec3SBjoern A. Zeeb 	 * the source address from.
11850895aec3SBjoern A. Zeeb 	 */
11869ac7c6cfSAlexander V. Chernikov 	if (nh == NULL || nh->nh_ifp == NULL) {
11878c0fec80SRobert Watson 		struct in_ifaddr *ia;
11880895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
11890895aec3SBjoern A. Zeeb 
11904f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
119158a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
11924f6c66ccSMatt Macy 		if (ia == NULL) {
11934f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
119458a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
11954f6c66ccSMatt Macy 		}
11960895aec3SBjoern A. Zeeb 		if (ia == NULL) {
11970895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
11980895aec3SBjoern A. Zeeb 			goto done;
11990895aec3SBjoern A. Zeeb 		}
12000895aec3SBjoern A. Zeeb 
12010304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
12020895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12030895aec3SBjoern A. Zeeb 			goto done;
12040895aec3SBjoern A. Zeeb 		}
12050895aec3SBjoern A. Zeeb 
12060895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
12070895aec3SBjoern A. Zeeb 		ia = NULL;
1208d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
12090895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
12100895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
12110895aec3SBjoern A. Zeeb 				continue;
12120895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
1213b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
12140895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
12150895aec3SBjoern A. Zeeb 				break;
12160895aec3SBjoern A. Zeeb 			}
12170895aec3SBjoern A. Zeeb 		}
12180895aec3SBjoern A. Zeeb 		if (ia != NULL) {
12190895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12200895aec3SBjoern A. Zeeb 			goto done;
12210895aec3SBjoern A. Zeeb 		}
12220895aec3SBjoern A. Zeeb 
12230895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1224b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
12250895aec3SBjoern A. Zeeb 		goto done;
12260895aec3SBjoern A. Zeeb 	}
12270895aec3SBjoern A. Zeeb 
12280895aec3SBjoern A. Zeeb 	/*
12290895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
12300895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
12310895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
12320895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
12330895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
12340895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
12350895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
12360895aec3SBjoern A. Zeeb 	 */
12379ac7c6cfSAlexander V. Chernikov 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) {
12388c0fec80SRobert Watson 		struct in_ifaddr *ia;
12399317b04eSRobert Watson 		struct ifnet *ifp;
12400895aec3SBjoern A. Zeeb 
12410895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
12420304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
12439ac7c6cfSAlexander V. Chernikov 			ia = (struct in_ifaddr *)nh->nh_ifa;
12440895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12450895aec3SBjoern A. Zeeb 			goto done;
12460895aec3SBjoern A. Zeeb 		}
12470895aec3SBjoern A. Zeeb 
12480895aec3SBjoern A. Zeeb 		/* Jailed. */
12490895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
12509ac7c6cfSAlexander V. Chernikov 		sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
1251b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
12529ac7c6cfSAlexander V. Chernikov 			ia = (struct in_ifaddr *)nh->nh_ifa;
12530895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12540895aec3SBjoern A. Zeeb 			goto done;
12550895aec3SBjoern A. Zeeb 		}
12560895aec3SBjoern A. Zeeb 
12570895aec3SBjoern A. Zeeb 		/*
12580895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
12590895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
12600895aec3SBjoern A. Zeeb 		 */
12618c0fec80SRobert Watson 		ia = NULL;
12629ac7c6cfSAlexander V. Chernikov 		ifp = nh->nh_ifp;
1263d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
12640895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
12650895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
12660895aec3SBjoern A. Zeeb 				continue;
12670895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
1268b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
12690895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
12700895aec3SBjoern A. Zeeb 				break;
12710895aec3SBjoern A. Zeeb 			}
12720895aec3SBjoern A. Zeeb 		}
12730895aec3SBjoern A. Zeeb 		if (ia != NULL) {
12740895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12750895aec3SBjoern A. Zeeb 			goto done;
12760895aec3SBjoern A. Zeeb 		}
12770895aec3SBjoern A. Zeeb 
12780895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1279b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
12800895aec3SBjoern A. Zeeb 		goto done;
12810895aec3SBjoern A. Zeeb 	}
12820895aec3SBjoern A. Zeeb 
12830895aec3SBjoern A. Zeeb 	/*
12840895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
12850895aec3SBjoern A. Zeeb 	 * to ourselves is here.
12860895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
12870895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
12880895aec3SBjoern A. Zeeb 	 * a loopback interface.
12890895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
12900895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
12910895aec3SBjoern A. Zeeb 	 */
12929ac7c6cfSAlexander V. Chernikov 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) {
12938c0fec80SRobert Watson 		struct in_ifaddr *ia;
12940895aec3SBjoern A. Zeeb 
12959ac7c6cfSAlexander V. Chernikov 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst),
129658a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
12970895aec3SBjoern A. Zeeb 		if (ia == NULL)
12989ac7c6cfSAlexander V. Chernikov 			ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0,
129958a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
1300f0bb05fcSQing Li 		if (ia == NULL)
13019ac7c6cfSAlexander V. Chernikov 			ia = ifatoia(ifa_ifwithaddr(sintosa(&dst)));
13020895aec3SBjoern A. Zeeb 
13030304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
13040895aec3SBjoern A. Zeeb 			if (ia == NULL) {
13050895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
13060895aec3SBjoern A. Zeeb 				goto done;
13070895aec3SBjoern A. Zeeb 			}
13080895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
13090895aec3SBjoern A. Zeeb 			goto done;
13100895aec3SBjoern A. Zeeb 		}
13110895aec3SBjoern A. Zeeb 
13120895aec3SBjoern A. Zeeb 		/* Jailed. */
13130895aec3SBjoern A. Zeeb 		if (ia != NULL) {
13140895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
13150895aec3SBjoern A. Zeeb 
13160895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
13170895aec3SBjoern A. Zeeb 			ia = NULL;
1318d7c5a620SMatt Macy 			CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
13190895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
13200895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
13210895aec3SBjoern A. Zeeb 					continue;
13220895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
1323b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
1324b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
13250895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
13260895aec3SBjoern A. Zeeb 					break;
13270895aec3SBjoern A. Zeeb 				}
13280895aec3SBjoern A. Zeeb 			}
13290895aec3SBjoern A. Zeeb 			if (ia != NULL) {
13300895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
13310895aec3SBjoern A. Zeeb 				goto done;
13320895aec3SBjoern A. Zeeb 			}
13330895aec3SBjoern A. Zeeb 		}
13340895aec3SBjoern A. Zeeb 
13350895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1336b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
13370895aec3SBjoern A. Zeeb 		goto done;
13380895aec3SBjoern A. Zeeb 	}
13390895aec3SBjoern A. Zeeb 
13400895aec3SBjoern A. Zeeb done:
13410895aec3SBjoern A. Zeeb 	return (error);
13420895aec3SBjoern A. Zeeb }
13430895aec3SBjoern A. Zeeb 
13440895aec3SBjoern A. Zeeb /*
13455200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
13465200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
13475200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
13485200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
13495200e00eSIan Dowse  * the connect.
13505200e00eSIan Dowse  *
13515200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
13525200e00eSIan Dowse  * and port. These are not updated in the error case.
13535200e00eSIan Dowse  *
13545200e00eSIan Dowse  * If the operation fails because the connection already exists,
13555200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
13565200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
13575200e00eSIan Dowse  * is set to NULL.
13585200e00eSIan Dowse  */
13595200e00eSIan Dowse int
1360136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1361136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1362136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
13635200e00eSIan Dowse {
13645200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
13655200e00eSIan Dowse 	struct in_ifaddr *ia;
13665200e00eSIan Dowse 	struct inpcb *oinp;
1367b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
13685200e00eSIan Dowse 	u_short lport, fport;
13695200e00eSIan Dowse 	int error;
13705200e00eSIan Dowse 
1371f161d294SMark Johnston 	KASSERT(sin->sin_family == AF_INET,
1372f161d294SMark Johnston 	    ("%s: invalid address family for %p", __func__, sin));
1373f161d294SMark Johnston 	KASSERT(sin->sin_len == sizeof(*sin),
1374f161d294SMark Johnston 	    ("%s: invalid address length for %p", __func__, sin));
1375f161d294SMark Johnston 
13768501a69cSRobert Watson 	/*
13778501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
13788501a69cSRobert Watson 	 * lock is sufficient.
13798501a69cSRobert Watson 	 */
1380c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
138127f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
1382fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
138327f74fd0SRobert Watson 
13845200e00eSIan Dowse 	if (oinpp != NULL)
13855200e00eSIan Dowse 		*oinpp = NULL;
1386df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1387df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
13885200e00eSIan Dowse 	laddr.s_addr = *laddrp;
13895200e00eSIan Dowse 	lport = *lportp;
13905200e00eSIan Dowse 	faddr = sin->sin_addr;
13915200e00eSIan Dowse 	fport = sin->sin_port;
13920c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH
13930c325f53SAlexander V. Chernikov 	if (CALC_FLOWID_OUTBOUND) {
13940c325f53SAlexander V. Chernikov 		uint32_t hash_val, hash_type;
13950895aec3SBjoern A. Zeeb 
13960c325f53SAlexander V. Chernikov 		hash_val = fib4_calc_software_hash(laddr, faddr, 0, fport,
13970c325f53SAlexander V. Chernikov 		    inp->inp_socket->so_proto->pr_protocol, &hash_type);
13980c325f53SAlexander V. Chernikov 
13990c325f53SAlexander V. Chernikov 		inp->inp_flowid = hash_val;
14000c325f53SAlexander V. Chernikov 		inp->inp_flowtype = hash_type;
14010c325f53SAlexander V. Chernikov 	}
14020c325f53SAlexander V. Chernikov #endif
1403d7c5a620SMatt Macy 	if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) {
1404df8bae1dSRodney W. Grimes 		/*
1405df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1406df8bae1dSRodney W. Grimes 		 * use the primary local address.
1407df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1408df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1409df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1410df8bae1dSRodney W. Grimes 		 */
1411413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
1412413628a7SBjoern A. Zeeb 			faddr =
1413d7c5a620SMatt Macy 			    IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1414b89e82ddSJamie Gritton 			if (cred != NULL &&
1415b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1416b89e82ddSJamie Gritton 				return (error);
14172d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1418d7c5a620SMatt Macy 			if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
14192d9cfabaSRobert Watson 			    IFF_BROADCAST)
1420d7c5a620SMatt Macy 				faddr = satosin(&CK_STAILQ_FIRST(
1421603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
14222d9cfabaSRobert Watson 		}
1423df8bae1dSRodney W. Grimes 	}
14245200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1425d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1426df8bae1dSRodney W. Grimes 		/*
1427df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1428d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1429df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1430df8bae1dSRodney W. Grimes 		 */
14315200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1432df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1433df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1434df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1435df8bae1dSRodney W. Grimes 
1436df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1437df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1438df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
1439d7c5a620SMatt Macy 				CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1440e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1441e691be70SDaniel Eischen 					    (cred == NULL ||
1442e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1443e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1444df8bae1dSRodney W. Grimes 						break;
1445e691be70SDaniel Eischen 				}
1446e691be70SDaniel Eischen 				if (ia == NULL)
1447d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1448e691be70SDaniel Eischen 				else {
14495200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1450d79fdd98SDaniel Eischen 					error = 0;
1451999f1343SGarrett Wollman 				}
1452d79fdd98SDaniel Eischen 			}
1453d79fdd98SDaniel Eischen 		}
145404215ed2SRandall Stewart 		if (error)
145504215ed2SRandall Stewart 			return (error);
14560895aec3SBjoern A. Zeeb 	}
1457a034518aSAndrew Gallatin 
145825102351SMike Karels 	if (lport != 0) {
145925102351SMike Karels 		oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr,
1460a034518aSAndrew Gallatin 		    fport, laddr, lport, 0, NULL, M_NODOM);
14615200e00eSIan Dowse 		if (oinp != NULL) {
14625200e00eSIan Dowse 			if (oinpp != NULL)
14635200e00eSIan Dowse 				*oinpp = oinp;
1464df8bae1dSRodney W. Grimes 			return (EADDRINUSE);
1465c3229e05SDavid Greenman 		}
146625102351SMike Karels 	} else {
146725102351SMike Karels 		struct sockaddr_in lsin, fsin;
146825102351SMike Karels 
146925102351SMike Karels 		bzero(&lsin, sizeof(lsin));
147025102351SMike Karels 		bzero(&fsin, sizeof(fsin));
147125102351SMike Karels 		lsin.sin_family = AF_INET;
147225102351SMike Karels 		lsin.sin_addr = laddr;
147325102351SMike Karels 		fsin.sin_family = AF_INET;
147425102351SMike Karels 		fsin.sin_addr = faddr;
147525102351SMike Karels 		error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin,
147625102351SMike Karels 		    &lport, (struct sockaddr *)& fsin, fport, cred,
147725102351SMike Karels 		    INPLOOKUP_WILDCARD);
14785a903f8dSPierre Beyssac 		if (error)
14795a903f8dSPierre Beyssac 			return (error);
14805a903f8dSPierre Beyssac 	}
14815200e00eSIan Dowse 	*laddrp = laddr.s_addr;
14825200e00eSIan Dowse 	*lportp = lport;
14835200e00eSIan Dowse 	*faddrp = faddr.s_addr;
14845200e00eSIan Dowse 	*fportp = fport;
1485df8bae1dSRodney W. Grimes 	return (0);
1486df8bae1dSRodney W. Grimes }
1487df8bae1dSRodney W. Grimes 
148826f9a767SRodney W. Grimes void
1489136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1490df8bae1dSRodney W. Grimes {
14916b348152SRobert Watson 
14928501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1493fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1494df8bae1dSRodney W. Grimes 
1495df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1496df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
149715bd2b43SDavid Greenman 	in_pcbrehash(inp);
1498df8bae1dSRodney W. Grimes }
149983e521ecSBjoern A. Zeeb #endif /* INET */
1500df8bae1dSRodney W. Grimes 
15014c7c478dSRobert Watson /*
150228696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1503c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
150428696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
150528696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
15064c7c478dSRobert Watson  */
150726f9a767SRodney W. Grimes void
1508136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1509df8bae1dSRodney W. Grimes {
15104c7c478dSRobert Watson 
1511a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1512c0a211c5SRobert Watson 
1513f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1514f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag != NULL)
1515f3e7afe2SHans Petter Selasky 		in_pcbdetach_txrtlmt(inp);
1516f3e7afe2SHans Petter Selasky #endif
15174c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
15184c7c478dSRobert Watson 	inp->inp_socket = NULL;
15194c7c478dSRobert Watson }
15204c7c478dSRobert Watson 
1521c0a211c5SRobert Watson /*
152279bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
1523*266f97b5SCy Schubert  * stability of an inpcb pointer despite the inpcb lock being released.  This
1524*266f97b5SCy Schubert  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1525*266f97b5SCy Schubert  * but where the inpcb lock may already held, or when acquiring a reference
1526*266f97b5SCy Schubert  * via a pcbgroup.
152779bdc6e5SRobert Watson  *
1528*266f97b5SCy Schubert  * in_pcbref() should be used only to provide brief memory stability, and
1529*266f97b5SCy Schubert  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1530*266f97b5SCy Schubert  * garbage collect the inpcb if it has been in_pcbfree()'d from another
1531*266f97b5SCy Schubert  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
1532*266f97b5SCy Schubert  * lock and rele are the *only* safe operations that may be performed on the
1533*266f97b5SCy Schubert  * inpcb.
1534*266f97b5SCy Schubert  *
1535*266f97b5SCy Schubert  * While the inpcb will not be freed, releasing the inpcb lock means that the
1536*266f97b5SCy Schubert  * connection's state may change, so the caller should be careful to
1537*266f97b5SCy Schubert  * revalidate any cached state on reacquiring the lock.  Drop the reference
1538*266f97b5SCy Schubert  * using in_pcbrele().
1539c0a211c5SRobert Watson  */
154079bdc6e5SRobert Watson void
154179bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
15424c7c478dSRobert Watson {
1543df8bae1dSRodney W. Grimes 
1544*266f97b5SCy Schubert 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1545*266f97b5SCy Schubert 
1546*266f97b5SCy Schubert 	refcount_acquire(&inp->inp_refcount);
154779bdc6e5SRobert Watson }
154879bdc6e5SRobert Watson 
154979bdc6e5SRobert Watson /*
1550*266f97b5SCy Schubert  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1551*266f97b5SCy Schubert  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1552*266f97b5SCy Schubert  * return a flag indicating whether or not the inpcb remains valid.  If it is
1553*266f97b5SCy Schubert  * valid, we return with the inpcb lock held.
1554*266f97b5SCy Schubert  *
1555*266f97b5SCy Schubert  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1556*266f97b5SCy Schubert  * reference on an inpcb.  Historically more work was done here (actually, in
1557*266f97b5SCy Schubert  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1558*266f97b5SCy Schubert  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
1559*266f97b5SCy Schubert  * about memory stability (and continued use of the write lock).
156079bdc6e5SRobert Watson  */
1561*266f97b5SCy Schubert int
156279bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
156379bdc6e5SRobert Watson {
1564*266f97b5SCy Schubert 	struct inpcbinfo *pcbinfo;
1565*266f97b5SCy Schubert 
1566*266f97b5SCy Schubert 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
156779bdc6e5SRobert Watson 
156879bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
156979bdc6e5SRobert Watson 
1570*266f97b5SCy Schubert 	if (refcount_release(&inp->inp_refcount) == 0) {
1571*266f97b5SCy Schubert 		/*
1572*266f97b5SCy Schubert 		 * If the inpcb has been freed, let the caller know, even if
1573*266f97b5SCy Schubert 		 * this isn't the last reference.
1574*266f97b5SCy Schubert 		 */
1575*266f97b5SCy Schubert 		if (inp->inp_flags2 & INP_FREED) {
1576df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1577*266f97b5SCy Schubert 			return (1);
1578*266f97b5SCy Schubert 		}
1579*266f97b5SCy Schubert 		return (0);
1580df4e91d3SGleb Smirnoff 	}
158179bdc6e5SRobert Watson 
1582*266f97b5SCy Schubert 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1583*266f97b5SCy Schubert #ifdef TCPHPTS
1584*266f97b5SCy Schubert 	if (inp->inp_in_hpts || inp->inp_in_input) {
1585*266f97b5SCy Schubert 		struct tcp_hpts_entry *hpts;
1586*266f97b5SCy Schubert 		/*
1587*266f97b5SCy Schubert 		 * We should not be on the hpts at
1588*266f97b5SCy Schubert 		 * this point in any form. we must
1589*266f97b5SCy Schubert 		 * get the lock to be sure.
1590*266f97b5SCy Schubert 		 */
1591*266f97b5SCy Schubert 		hpts = tcp_hpts_lock(inp);
1592*266f97b5SCy Schubert 		if (inp->inp_in_hpts)
1593*266f97b5SCy Schubert 			panic("Hpts:%p inp:%p at free still on hpts",
1594*266f97b5SCy Schubert 			      hpts, inp);
1595*266f97b5SCy Schubert 		mtx_unlock(&hpts->p_mtx);
1596*266f97b5SCy Schubert 		hpts = tcp_input_lock(inp);
1597*266f97b5SCy Schubert 		if (inp->inp_in_input)
1598*266f97b5SCy Schubert 			panic("Hpts:%p inp:%p at free still on input hpts",
1599*266f97b5SCy Schubert 			      hpts, inp);
1600*266f97b5SCy Schubert 		mtx_unlock(&hpts->p_mtx);
1601*266f97b5SCy Schubert 	}
1602*266f97b5SCy Schubert #endif
1603*266f97b5SCy Schubert 	INP_RUNLOCK(inp);
1604*266f97b5SCy Schubert 	pcbinfo = inp->inp_pcbinfo;
1605*266f97b5SCy Schubert 	uma_zfree(pcbinfo->ipi_zone, inp);
1606*266f97b5SCy Schubert 	return (1);
1607*266f97b5SCy Schubert }
1608*266f97b5SCy Schubert 
1609*266f97b5SCy Schubert int
161079bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
161179bdc6e5SRobert Watson {
1612*266f97b5SCy Schubert 	struct inpcbinfo *pcbinfo;
1613*266f97b5SCy Schubert 
1614*266f97b5SCy Schubert 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
161579bdc6e5SRobert Watson 
161679bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
161779bdc6e5SRobert Watson 
1618*266f97b5SCy Schubert 	if (refcount_release(&inp->inp_refcount) == 0) {
1619*266f97b5SCy Schubert 		/*
1620*266f97b5SCy Schubert 		 * If the inpcb has been freed, let the caller know, even if
1621*266f97b5SCy Schubert 		 * this isn't the last reference.
1622*266f97b5SCy Schubert 		 */
1623*266f97b5SCy Schubert 		if (inp->inp_flags2 & INP_FREED) {
1624edd0e0b0SFabien Thomas 			INP_WUNLOCK(inp);
1625*266f97b5SCy Schubert 			return (1);
1626*266f97b5SCy Schubert 		}
1627*266f97b5SCy Schubert 		return (0);
1628*266f97b5SCy Schubert 	}
1629*266f97b5SCy Schubert 
1630*266f97b5SCy Schubert 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1631*266f97b5SCy Schubert #ifdef TCPHPTS
1632*266f97b5SCy Schubert 	if (inp->inp_in_hpts || inp->inp_in_input) {
1633*266f97b5SCy Schubert 		struct tcp_hpts_entry *hpts;
1634*266f97b5SCy Schubert 		/*
1635*266f97b5SCy Schubert 		 * We should not be on the hpts at
1636*266f97b5SCy Schubert 		 * this point in any form. we must
1637*266f97b5SCy Schubert 		 * get the lock to be sure.
1638*266f97b5SCy Schubert 		 */
1639*266f97b5SCy Schubert 		hpts = tcp_hpts_lock(inp);
1640*266f97b5SCy Schubert 		if (inp->inp_in_hpts)
1641*266f97b5SCy Schubert 			panic("Hpts:%p inp:%p at free still on hpts",
1642*266f97b5SCy Schubert 			      hpts, inp);
1643*266f97b5SCy Schubert 		mtx_unlock(&hpts->p_mtx);
1644*266f97b5SCy Schubert 		hpts = tcp_input_lock(inp);
1645*266f97b5SCy Schubert 		if (inp->inp_in_input)
1646*266f97b5SCy Schubert 			panic("Hpts:%p inp:%p at free still on input hpts",
1647*266f97b5SCy Schubert 			      hpts, inp);
1648*266f97b5SCy Schubert 		mtx_unlock(&hpts->p_mtx);
1649*266f97b5SCy Schubert 	}
1650*266f97b5SCy Schubert #endif
1651*266f97b5SCy Schubert 	INP_WUNLOCK(inp);
1652*266f97b5SCy Schubert 	pcbinfo = inp->inp_pcbinfo;
1653*266f97b5SCy Schubert 	uma_zfree(pcbinfo->ipi_zone, inp);
1654*266f97b5SCy Schubert 	return (1);
1655*266f97b5SCy Schubert }
1656*266f97b5SCy Schubert 
1657*266f97b5SCy Schubert static void
1658*266f97b5SCy Schubert inpcbport_free(epoch_context_t ctx)
1659*266f97b5SCy Schubert {
1660*266f97b5SCy Schubert 	struct inpcbport *phd;
1661*266f97b5SCy Schubert 
1662*266f97b5SCy Schubert 	phd = __containerof(ctx, struct inpcbport, phd_epoch_ctx);
1663*266f97b5SCy Schubert 	free(phd, M_PCB);
1664*266f97b5SCy Schubert }
1665*266f97b5SCy Schubert 
1666*266f97b5SCy Schubert static void
1667*266f97b5SCy Schubert in_pcbfree_deferred(epoch_context_t ctx)
1668*266f97b5SCy Schubert {
1669*266f97b5SCy Schubert 	struct inpcb *inp;
1670*266f97b5SCy Schubert 	int released __unused;
1671*266f97b5SCy Schubert 
1672*266f97b5SCy Schubert 	inp = __containerof(ctx, struct inpcb, inp_epoch_ctx);
1673*266f97b5SCy Schubert 
1674*266f97b5SCy Schubert 	INP_WLOCK(inp);
1675*266f97b5SCy Schubert 	CURVNET_SET(inp->inp_vnet);
1676*266f97b5SCy Schubert #ifdef INET
1677*266f97b5SCy Schubert 	struct ip_moptions *imo = inp->inp_moptions;
1678*266f97b5SCy Schubert 	inp->inp_moptions = NULL;
1679*266f97b5SCy Schubert #endif
1680*266f97b5SCy Schubert 	/* XXXRW: Do as much as possible here. */
1681*266f97b5SCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1682*266f97b5SCy Schubert 	if (inp->inp_sp != NULL)
1683*266f97b5SCy Schubert 		ipsec_delete_pcbpolicy(inp);
1684*266f97b5SCy Schubert #endif
1685*266f97b5SCy Schubert #ifdef INET6
1686*266f97b5SCy Schubert 	struct ip6_moptions *im6o = NULL;
1687*266f97b5SCy Schubert 	if (inp->inp_vflag & INP_IPV6PROTO) {
1688*266f97b5SCy Schubert 		ip6_freepcbopts(inp->in6p_outputopts);
1689*266f97b5SCy Schubert 		im6o = inp->in6p_moptions;
1690*266f97b5SCy Schubert 		inp->in6p_moptions = NULL;
1691*266f97b5SCy Schubert 	}
1692*266f97b5SCy Schubert #endif
1693*266f97b5SCy Schubert 	if (inp->inp_options)
1694*266f97b5SCy Schubert 		(void)m_free(inp->inp_options);
1695*266f97b5SCy Schubert 	inp->inp_vflag = 0;
1696*266f97b5SCy Schubert 	crfree(inp->inp_cred);
1697*266f97b5SCy Schubert #ifdef MAC
1698*266f97b5SCy Schubert 	mac_inpcb_destroy(inp);
1699*266f97b5SCy Schubert #endif
1700*266f97b5SCy Schubert 	released = in_pcbrele_wlocked(inp);
1701*266f97b5SCy Schubert 	MPASS(released);
1702*266f97b5SCy Schubert #ifdef INET6
1703*266f97b5SCy Schubert 	ip6_freemoptions(im6o);
1704*266f97b5SCy Schubert #endif
1705*266f97b5SCy Schubert #ifdef INET
1706*266f97b5SCy Schubert 	inp_freemoptions(imo);
1707*266f97b5SCy Schubert #endif
1708*266f97b5SCy Schubert 	CURVNET_RESTORE();
1709addf2b20SMatt Macy }
1710addf2b20SMatt Macy 
171179bdc6e5SRobert Watson /*
171279bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
171379bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
171479bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
171579bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
1716*266f97b5SCy Schubert  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
1717*266f97b5SCy Schubert  * work, including removal from global lists, is done in this context, where
1718*266f97b5SCy Schubert  * the pcbinfo lock is held.
171979bdc6e5SRobert Watson  */
172079bdc6e5SRobert Watson void
172179bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
172279bdc6e5SRobert Watson {
1723f42a83f2SMatt Macy 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1724*266f97b5SCy Schubert 
1725*266f97b5SCy Schubert 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1726*266f97b5SCy Schubert 	KASSERT((inp->inp_flags2 & INP_FREED) == 0,
1727*266f97b5SCy Schubert 	    ("%s: called twice for pcb %p", __func__, inp));
1728*266f97b5SCy Schubert 	if (inp->inp_flags2 & INP_FREED) {
1729*266f97b5SCy Schubert 		INP_WUNLOCK(inp);
1730*266f97b5SCy Schubert 		return;
1731*266f97b5SCy Schubert 	}
173245a48d07SJonathan T. Looney 
173379bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
1734*266f97b5SCy Schubert 	INP_LIST_WLOCK(pcbinfo);
1735*266f97b5SCy Schubert 	in_pcbremlists(inp);
1736*266f97b5SCy Schubert 	INP_LIST_WUNLOCK(pcbinfo);
1737fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
1738*266f97b5SCy Schubert 	/* mark as destruction in progress */
1739*266f97b5SCy Schubert 	inp->inp_flags2 |= INP_FREED;
1740cb6bb230SMatt Macy 	INP_WUNLOCK(inp);
1741*266f97b5SCy Schubert 	NET_EPOCH_CALL(in_pcbfree_deferred, &inp->inp_epoch_ctx);
174228696211SRobert Watson }
174328696211SRobert Watson 
174428696211SRobert Watson /*
1745c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1746c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1747c0a211c5SRobert Watson  *
1748c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1749c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1750c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1751c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1752c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1753c0a211c5SRobert Watson  * in_pcbdetach().
1754c0a211c5SRobert Watson  *
1755c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1756c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
175710702a28SRobert Watson  */
175810702a28SRobert Watson void
175910702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
176010702a28SRobert Watson {
176110702a28SRobert Watson 
17628501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
17636573d758SMatt Macy #ifdef INVARIANTS
17646573d758SMatt Macy 	if (inp->inp_socket != NULL && inp->inp_ppcb != NULL)
17656573d758SMatt Macy 		MPASS(inp->inp_refcount > 1);
17666573d758SMatt Macy #endif
176710702a28SRobert Watson 
1768fa046d87SRobert Watson 	/*
1769fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1770fa046d87SRobert Watson 	 * the hash lock...?
1771fa046d87SRobert Watson 	 */
1772ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1773111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
177410702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
177510702a28SRobert Watson 
1776fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
17771a43cff9SSean Bruno 		in_pcbremlbgrouphash(inp);
1778b872626dSMatt Macy 		CK_LIST_REMOVE(inp, inp_hash);
1779b872626dSMatt Macy 		CK_LIST_REMOVE(inp, inp_portlist);
1780b872626dSMatt Macy 		if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) {
1781b872626dSMatt Macy 			CK_LIST_REMOVE(phd, phd_hash);
1782*266f97b5SCy Schubert 			NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx);
178310702a28SRobert Watson 		}
1784fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1785111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
1786*266f97b5SCy Schubert #ifdef PCBGROUP
1787*266f97b5SCy Schubert 		in_pcbgroup_remove(inp);
1788*266f97b5SCy Schubert #endif
178910702a28SRobert Watson 	}
179010702a28SRobert Watson }
179110702a28SRobert Watson 
179267107f45SBjoern A. Zeeb #ifdef INET
179354d642bbSRobert Watson /*
179454d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
179554d642bbSRobert Watson  */
179626ef6ac4SDon Lewis struct sockaddr *
1797136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
179826ef6ac4SDon Lewis {
179926ef6ac4SDon Lewis 	struct sockaddr_in *sin;
180026ef6ac4SDon Lewis 
18011ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1802a163d034SWarner Losh 		M_WAITOK | M_ZERO);
180326ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
180426ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
180526ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
180626ef6ac4SDon Lewis 	sin->sin_port = port;
180726ef6ac4SDon Lewis 
180826ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
180926ef6ac4SDon Lewis }
181026ef6ac4SDon Lewis 
1811117bcae7SGarrett Wollman int
181254d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1813df8bae1dSRodney W. Grimes {
1814136d4f1cSRobert Watson 	struct inpcb *inp;
181526ef6ac4SDon Lewis 	struct in_addr addr;
181626ef6ac4SDon Lewis 	in_port_t port;
181742fa505bSDavid Greenman 
1818fdc984f7STor Egge 	inp = sotoinpcb(so);
181954d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
18206466b28aSRobert Watson 
1821a69042a5SRobert Watson 	INP_RLOCK(inp);
182226ef6ac4SDon Lewis 	port = inp->inp_lport;
182326ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1824a69042a5SRobert Watson 	INP_RUNLOCK(inp);
182542fa505bSDavid Greenman 
182626ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1827117bcae7SGarrett Wollman 	return 0;
1828df8bae1dSRodney W. Grimes }
1829df8bae1dSRodney W. Grimes 
1830117bcae7SGarrett Wollman int
183154d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1832df8bae1dSRodney W. Grimes {
1833136d4f1cSRobert Watson 	struct inpcb *inp;
183426ef6ac4SDon Lewis 	struct in_addr addr;
183526ef6ac4SDon Lewis 	in_port_t port;
183642fa505bSDavid Greenman 
1837fdc984f7STor Egge 	inp = sotoinpcb(so);
183854d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
18396466b28aSRobert Watson 
1840a69042a5SRobert Watson 	INP_RLOCK(inp);
184126ef6ac4SDon Lewis 	port = inp->inp_fport;
184226ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1843a69042a5SRobert Watson 	INP_RUNLOCK(inp);
184442fa505bSDavid Greenman 
184526ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1846117bcae7SGarrett Wollman 	return 0;
1847df8bae1dSRodney W. Grimes }
1848df8bae1dSRodney W. Grimes 
184926f9a767SRodney W. Grimes void
1850136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1851136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1852d1c54148SJesper Skriver {
1853f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1854d1c54148SJesper Skriver 
18553dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1856*266f97b5SCy Schubert 	CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
18578501a69cSRobert Watson 		INP_WLOCK(inp);
1858d1c54148SJesper Skriver #ifdef INET6
1859f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
18608501a69cSRobert Watson 			INP_WUNLOCK(inp);
1861d1c54148SJesper Skriver 			continue;
1862f76fcf6dSJeffrey Hsu 		}
1863d1c54148SJesper Skriver #endif
1864d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1865f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
18668501a69cSRobert Watson 			INP_WUNLOCK(inp);
1867d1c54148SJesper Skriver 			continue;
1868d1c54148SJesper Skriver 		}
18693dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
18708501a69cSRobert Watson 			INP_WUNLOCK(inp);
1871f76fcf6dSJeffrey Hsu 	}
18723dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1873d1c54148SJesper Skriver }
1874d1c54148SJesper Skriver 
1875e43cc4aeSHajimu UMEMOTO void
1876136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1877e43cc4aeSHajimu UMEMOTO {
1878e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
187959854ecfSHans Petter Selasky 	struct in_multi *inm;
188059854ecfSHans Petter Selasky 	struct in_mfilter *imf;
1881e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1882e43cc4aeSHajimu UMEMOTO 
1883*266f97b5SCy Schubert 	INP_INFO_WLOCK(pcbinfo);
1884*266f97b5SCy Schubert 	CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1885*266f97b5SCy Schubert 		INP_WLOCK(inp);
1886e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1887*266f97b5SCy Schubert 		if ((inp->inp_vflag & INP_IPV4) &&
1888*266f97b5SCy Schubert 		    imo != NULL) {
1889e43cc4aeSHajimu UMEMOTO 			/*
1890e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1891e43cc4aeSHajimu UMEMOTO 			 * detached.
1892e43cc4aeSHajimu UMEMOTO 			 */
1893e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1894e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1895e43cc4aeSHajimu UMEMOTO 
1896e43cc4aeSHajimu UMEMOTO 			/*
1897e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1898e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1899cb6bb230SMatt Macy 			 *
1900cb6bb230SMatt Macy 			 * XXX This can all be deferred to an epoch_call
1901e43cc4aeSHajimu UMEMOTO 			 */
190259854ecfSHans Petter Selasky restart:
190359854ecfSHans Petter Selasky 			IP_MFILTER_FOREACH(imf, &imo->imo_head) {
190459854ecfSHans Petter Selasky 				if ((inm = imf->imf_inm) == NULL)
190559854ecfSHans Petter Selasky 					continue;
190659854ecfSHans Petter Selasky 				if (inm->inm_ifp != ifp)
190759854ecfSHans Petter Selasky 					continue;
190859854ecfSHans Petter Selasky 				ip_mfilter_remove(&imo->imo_head, imf);
1909*266f97b5SCy Schubert 				IN_MULTI_LOCK_ASSERT();
191059854ecfSHans Petter Selasky 				in_leavegroup_locked(inm, NULL);
191159854ecfSHans Petter Selasky 				ip_mfilter_free(imf);
191259854ecfSHans Petter Selasky 				goto restart;
1913e43cc4aeSHajimu UMEMOTO 			}
1914e43cc4aeSHajimu UMEMOTO 		}
1915*266f97b5SCy Schubert 		INP_WUNLOCK(inp);
1916*266f97b5SCy Schubert 	}
1917*266f97b5SCy Schubert 	INP_INFO_WUNLOCK(pcbinfo);
1918e43cc4aeSHajimu UMEMOTO }
1919e43cc4aeSHajimu UMEMOTO 
1920df8bae1dSRodney W. Grimes /*
1921fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1922fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1923c3229e05SDavid Greenman  */
1924d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1925df8bae1dSRodney W. Grimes struct inpcb *
1926136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
192768e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1928df8bae1dSRodney W. Grimes {
1929136d4f1cSRobert Watson 	struct inpcb *inp;
1930d5e8a67eSHajimu UMEMOTO #ifdef INET6
1931d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1932d5e8a67eSHajimu UMEMOTO #else
1933d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1934d5e8a67eSHajimu UMEMOTO #endif
1935d5e8a67eSHajimu UMEMOTO 	int wildcard;
19367bc4aca7SDavid Greenman 
193768e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
193868e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1939*266f97b5SCy Schubert 
1940fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
19411b73ca0bSSam Leffler 
194268e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1943c3229e05SDavid Greenman 		struct inpcbhead *head;
1944c3229e05SDavid Greenman 		/*
1945c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1946c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1947c3229e05SDavid Greenman 		 */
1948712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1949712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1950b872626dSMatt Macy 		CK_LIST_FOREACH(inp, head, inp_hash) {
1951cfa1ca9dSYoshinobu Inoue #ifdef INET6
1952413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1953369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1954cfa1ca9dSYoshinobu Inoue 				continue;
1955cfa1ca9dSYoshinobu Inoue #endif
1956c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1957c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1958c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1959c3229e05SDavid Greenman 				/*
1960413628a7SBjoern A. Zeeb 				 * Found?
1961c3229e05SDavid Greenman 				 */
1962413628a7SBjoern A. Zeeb 				if (cred == NULL ||
19630304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
19640304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1965c3229e05SDavid Greenman 					return (inp);
1966df8bae1dSRodney W. Grimes 			}
1967c3229e05SDavid Greenman 		}
1968c3229e05SDavid Greenman 		/*
1969c3229e05SDavid Greenman 		 * Not found.
1970c3229e05SDavid Greenman 		 */
1971c3229e05SDavid Greenman 		return (NULL);
1972c3229e05SDavid Greenman 	} else {
1973c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1974c3229e05SDavid Greenman 		struct inpcbport *phd;
1975c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1976c3229e05SDavid Greenman 		/*
1977c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1978c3229e05SDavid Greenman 		 *
1979c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1980c3229e05SDavid Greenman 		 * port hash list.
1981c3229e05SDavid Greenman 		 */
1982712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1983712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1984b872626dSMatt Macy 		CK_LIST_FOREACH(phd, porthash, phd_hash) {
1985c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1986c3229e05SDavid Greenman 				break;
1987c3229e05SDavid Greenman 		}
1988c3229e05SDavid Greenman 		if (phd != NULL) {
1989c3229e05SDavid Greenman 			/*
1990c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1991c3229e05SDavid Greenman 			 * fit.
1992c3229e05SDavid Greenman 			 */
1993b872626dSMatt Macy 			CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1994c3229e05SDavid Greenman 				wildcard = 0;
1995413628a7SBjoern A. Zeeb 				if (cred != NULL &&
19960304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
19970304c731SJamie Gritton 					cred->cr_prison))
1998413628a7SBjoern A. Zeeb 					continue;
1999cfa1ca9dSYoshinobu Inoue #ifdef INET6
2000413628a7SBjoern A. Zeeb 				/* XXX inp locking */
2001369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
2002cfa1ca9dSYoshinobu Inoue 					continue;
2003d5e8a67eSHajimu UMEMOTO 				/*
2004d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
2005d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
2006d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
2007d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
2008d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
2009d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
2010d5e8a67eSHajimu UMEMOTO 				 *
2011d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
2012d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
2013d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
2014d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
2015d5e8a67eSHajimu UMEMOTO 				 */
2016d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
2017d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
2018cfa1ca9dSYoshinobu Inoue #endif
2019c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
2020c3229e05SDavid Greenman 					wildcard++;
202115bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
202215bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
202315bd2b43SDavid Greenman 						wildcard++;
202415bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
202515bd2b43SDavid Greenman 						continue;
202615bd2b43SDavid Greenman 				} else {
202715bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
202815bd2b43SDavid Greenman 						wildcard++;
202915bd2b43SDavid Greenman 				}
2030df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
2031df8bae1dSRodney W. Grimes 					match = inp;
2032df8bae1dSRodney W. Grimes 					matchwild = wildcard;
2033413628a7SBjoern A. Zeeb 					if (matchwild == 0)
2034df8bae1dSRodney W. Grimes 						break;
2035df8bae1dSRodney W. Grimes 				}
2036df8bae1dSRodney W. Grimes 			}
20373dbdc25cSDavid Greenman 		}
2038df8bae1dSRodney W. Grimes 		return (match);
2039df8bae1dSRodney W. Grimes 	}
2040c3229e05SDavid Greenman }
2041d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
204215bd2b43SDavid Greenman 
20431a43cff9SSean Bruno static struct inpcb *
20441a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
20451a43cff9SSean Bruno     const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr,
2046a034518aSAndrew Gallatin     uint16_t fport, int lookupflags, int numa_domain)
20471a43cff9SSean Bruno {
2048a034518aSAndrew Gallatin 	struct inpcb *local_wild, *numa_wild;
20491a43cff9SSean Bruno 	const struct inpcblbgrouphead *hdr;
20501a43cff9SSean Bruno 	struct inpcblbgroup *grp;
20518be02ee4SMark Johnston 	uint32_t idx;
20521a43cff9SSean Bruno 
20531a43cff9SSean Bruno 	INP_HASH_LOCK_ASSERT(pcbinfo);
20541a43cff9SSean Bruno 
20559d2877fcSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[
20569d2877fcSMark Johnston 	    INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
20571a43cff9SSean Bruno 
20581a43cff9SSean Bruno 	/*
20591a43cff9SSean Bruno 	 * Order of socket selection:
20601a43cff9SSean Bruno 	 * 1. non-wild.
20611a43cff9SSean Bruno 	 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
20621a43cff9SSean Bruno 	 *
20631a43cff9SSean Bruno 	 * NOTE:
20641a43cff9SSean Bruno 	 * - Load balanced group does not contain jailed sockets
20651a43cff9SSean Bruno 	 * - Load balanced group does not contain IPv4 mapped INET6 wild sockets
20661a43cff9SSean Bruno 	 */
20678be02ee4SMark Johnston 	local_wild = NULL;
2068a034518aSAndrew Gallatin 	numa_wild = NULL;
206954af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
20701a43cff9SSean Bruno #ifdef INET6
20711a43cff9SSean Bruno 		if (!(grp->il_vflag & INP_IPV4))
20721a43cff9SSean Bruno 			continue;
20731a43cff9SSean Bruno #endif
20748be02ee4SMark Johnston 		if (grp->il_lport != lport)
20758be02ee4SMark Johnston 			continue;
20761a43cff9SSean Bruno 
20778be02ee4SMark Johnston 		idx = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, lport, fport) %
20788be02ee4SMark Johnston 		    grp->il_inpcnt;
2079a034518aSAndrew Gallatin 		if (grp->il_laddr.s_addr == laddr->s_addr) {
2080a034518aSAndrew Gallatin 			if (numa_domain == M_NODOM ||
2081a034518aSAndrew Gallatin 			    grp->il_numa_domain == numa_domain) {
20821a43cff9SSean Bruno 				return (grp->il_inp[idx]);
2083a034518aSAndrew Gallatin 			} else {
2084a034518aSAndrew Gallatin 				numa_wild = grp->il_inp[idx];
2085a034518aSAndrew Gallatin 			}
2086a034518aSAndrew Gallatin 		}
20871a43cff9SSean Bruno 		if (grp->il_laddr.s_addr == INADDR_ANY &&
2088a034518aSAndrew Gallatin 		    (lookupflags & INPLOOKUP_WILDCARD) != 0 &&
2089a034518aSAndrew Gallatin 		    (local_wild == NULL || numa_domain == M_NODOM ||
2090a034518aSAndrew Gallatin 			grp->il_numa_domain == numa_domain)) {
20911a43cff9SSean Bruno 			local_wild = grp->il_inp[idx];
20921a43cff9SSean Bruno 		}
2093a034518aSAndrew Gallatin 	}
2094a034518aSAndrew Gallatin 	if (numa_wild != NULL)
2095a034518aSAndrew Gallatin 		return (numa_wild);
2096a034518aSAndrew Gallatin 
20971a43cff9SSean Bruno 	return (local_wild);
20981a43cff9SSean Bruno }
20991a43cff9SSean Bruno 
2100*266f97b5SCy Schubert #ifdef PCBGROUP
2101*266f97b5SCy Schubert /*
2102*266f97b5SCy Schubert  * Lookup PCB in hash list, using pcbgroup tables.
2103*266f97b5SCy Schubert  */
2104*266f97b5SCy Schubert static struct inpcb *
2105*266f97b5SCy Schubert in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
2106*266f97b5SCy Schubert     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
2107*266f97b5SCy Schubert     u_int lport_arg, int lookupflags, struct ifnet *ifp)
2108*266f97b5SCy Schubert {
2109*266f97b5SCy Schubert 	struct inpcbhead *head;
2110*266f97b5SCy Schubert 	struct inpcb *inp, *tmpinp;
2111*266f97b5SCy Schubert 	u_short fport = fport_arg, lport = lport_arg;
2112*266f97b5SCy Schubert 	bool locked;
2113*266f97b5SCy Schubert 
2114*266f97b5SCy Schubert 	/*
2115*266f97b5SCy Schubert 	 * First look for an exact match.
2116*266f97b5SCy Schubert 	 */
2117*266f97b5SCy Schubert 	tmpinp = NULL;
2118*266f97b5SCy Schubert 	INP_GROUP_LOCK(pcbgroup);
2119*266f97b5SCy Schubert 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
2120*266f97b5SCy Schubert 	    pcbgroup->ipg_hashmask)];
2121*266f97b5SCy Schubert 	CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
2122*266f97b5SCy Schubert #ifdef INET6
2123*266f97b5SCy Schubert 		/* XXX inp locking */
2124*266f97b5SCy Schubert 		if ((inp->inp_vflag & INP_IPV4) == 0)
2125*266f97b5SCy Schubert 			continue;
2126*266f97b5SCy Schubert #endif
2127*266f97b5SCy Schubert 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
2128*266f97b5SCy Schubert 		    inp->inp_laddr.s_addr == laddr.s_addr &&
2129*266f97b5SCy Schubert 		    inp->inp_fport == fport &&
2130*266f97b5SCy Schubert 		    inp->inp_lport == lport) {
2131*266f97b5SCy Schubert 			/*
2132*266f97b5SCy Schubert 			 * XXX We should be able to directly return
2133*266f97b5SCy Schubert 			 * the inp here, without any checks.
2134*266f97b5SCy Schubert 			 * Well unless both bound with SO_REUSEPORT?
2135*266f97b5SCy Schubert 			 */
2136*266f97b5SCy Schubert 			if (prison_flag(inp->inp_cred, PR_IP4))
2137*266f97b5SCy Schubert 				goto found;
2138*266f97b5SCy Schubert 			if (tmpinp == NULL)
2139*266f97b5SCy Schubert 				tmpinp = inp;
2140*266f97b5SCy Schubert 		}
2141*266f97b5SCy Schubert 	}
2142*266f97b5SCy Schubert 	if (tmpinp != NULL) {
2143*266f97b5SCy Schubert 		inp = tmpinp;
2144*266f97b5SCy Schubert 		goto found;
2145*266f97b5SCy Schubert 	}
2146*266f97b5SCy Schubert 
2147*266f97b5SCy Schubert #ifdef	RSS
2148*266f97b5SCy Schubert 	/*
2149*266f97b5SCy Schubert 	 * For incoming connections, we may wish to do a wildcard
2150*266f97b5SCy Schubert 	 * match for an RSS-local socket.
2151*266f97b5SCy Schubert 	 */
2152*266f97b5SCy Schubert 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
2153*266f97b5SCy Schubert 		struct inpcb *local_wild = NULL, *local_exact = NULL;
2154*266f97b5SCy Schubert #ifdef INET6
2155*266f97b5SCy Schubert 		struct inpcb *local_wild_mapped = NULL;
2156*266f97b5SCy Schubert #endif
2157*266f97b5SCy Schubert 		struct inpcb *jail_wild = NULL;
2158*266f97b5SCy Schubert 		struct inpcbhead *head;
2159*266f97b5SCy Schubert 		int injail;
2160*266f97b5SCy Schubert 
2161*266f97b5SCy Schubert 		/*
2162*266f97b5SCy Schubert 		 * Order of socket selection - we always prefer jails.
2163*266f97b5SCy Schubert 		 *      1. jailed, non-wild.
2164*266f97b5SCy Schubert 		 *      2. jailed, wild.
2165*266f97b5SCy Schubert 		 *      3. non-jailed, non-wild.
2166*266f97b5SCy Schubert 		 *      4. non-jailed, wild.
2167*266f97b5SCy Schubert 		 */
2168*266f97b5SCy Schubert 
2169*266f97b5SCy Schubert 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
2170*266f97b5SCy Schubert 		    lport, 0, pcbgroup->ipg_hashmask)];
2171*266f97b5SCy Schubert 		CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
2172*266f97b5SCy Schubert #ifdef INET6
2173*266f97b5SCy Schubert 			/* XXX inp locking */
2174*266f97b5SCy Schubert 			if ((inp->inp_vflag & INP_IPV4) == 0)
2175*266f97b5SCy Schubert 				continue;
2176*266f97b5SCy Schubert #endif
2177*266f97b5SCy Schubert 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
2178*266f97b5SCy Schubert 			    inp->inp_lport != lport)
2179*266f97b5SCy Schubert 				continue;
2180*266f97b5SCy Schubert 
2181*266f97b5SCy Schubert 			injail = prison_flag(inp->inp_cred, PR_IP4);
2182*266f97b5SCy Schubert 			if (injail) {
2183*266f97b5SCy Schubert 				if (prison_check_ip4(inp->inp_cred,
2184*266f97b5SCy Schubert 				    &laddr) != 0)
2185*266f97b5SCy Schubert 					continue;
2186*266f97b5SCy Schubert 			} else {
2187*266f97b5SCy Schubert 				if (local_exact != NULL)
2188*266f97b5SCy Schubert 					continue;
2189*266f97b5SCy Schubert 			}
2190*266f97b5SCy Schubert 
2191*266f97b5SCy Schubert 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
2192*266f97b5SCy Schubert 				if (injail)
2193*266f97b5SCy Schubert 					goto found;
2194*266f97b5SCy Schubert 				else
2195*266f97b5SCy Schubert 					local_exact = inp;
2196*266f97b5SCy Schubert 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
2197*266f97b5SCy Schubert #ifdef INET6
2198*266f97b5SCy Schubert 				/* XXX inp locking, NULL check */
2199*266f97b5SCy Schubert 				if (inp->inp_vflag & INP_IPV6PROTO)
2200*266f97b5SCy Schubert 					local_wild_mapped = inp;
2201*266f97b5SCy Schubert 				else
2202*266f97b5SCy Schubert #endif
2203*266f97b5SCy Schubert 					if (injail)
2204*266f97b5SCy Schubert 						jail_wild = inp;
2205*266f97b5SCy Schubert 					else
2206*266f97b5SCy Schubert 						local_wild = inp;
2207*266f97b5SCy Schubert 			}
2208*266f97b5SCy Schubert 		} /* LIST_FOREACH */
2209*266f97b5SCy Schubert 
2210*266f97b5SCy Schubert 		inp = jail_wild;
2211*266f97b5SCy Schubert 		if (inp == NULL)
2212*266f97b5SCy Schubert 			inp = local_exact;
2213*266f97b5SCy Schubert 		if (inp == NULL)
2214*266f97b5SCy Schubert 			inp = local_wild;
2215*266f97b5SCy Schubert #ifdef INET6
2216*266f97b5SCy Schubert 		if (inp == NULL)
2217*266f97b5SCy Schubert 			inp = local_wild_mapped;
2218*266f97b5SCy Schubert #endif
2219*266f97b5SCy Schubert 		if (inp != NULL)
2220*266f97b5SCy Schubert 			goto found;
2221*266f97b5SCy Schubert 	}
2222*266f97b5SCy Schubert #endif
2223*266f97b5SCy Schubert 
2224*266f97b5SCy Schubert 	/*
2225*266f97b5SCy Schubert 	 * Then look for a wildcard match, if requested.
2226*266f97b5SCy Schubert 	 */
2227*266f97b5SCy Schubert 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
2228*266f97b5SCy Schubert 		struct inpcb *local_wild = NULL, *local_exact = NULL;
2229*266f97b5SCy Schubert #ifdef INET6
2230*266f97b5SCy Schubert 		struct inpcb *local_wild_mapped = NULL;
2231*266f97b5SCy Schubert #endif
2232*266f97b5SCy Schubert 		struct inpcb *jail_wild = NULL;
2233*266f97b5SCy Schubert 		struct inpcbhead *head;
2234*266f97b5SCy Schubert 		int injail;
2235*266f97b5SCy Schubert 
2236*266f97b5SCy Schubert 		/*
2237*266f97b5SCy Schubert 		 * Order of socket selection - we always prefer jails.
2238*266f97b5SCy Schubert 		 *      1. jailed, non-wild.
2239*266f97b5SCy Schubert 		 *      2. jailed, wild.
2240*266f97b5SCy Schubert 		 *      3. non-jailed, non-wild.
2241*266f97b5SCy Schubert 		 *      4. non-jailed, wild.
2242*266f97b5SCy Schubert 		 */
2243*266f97b5SCy Schubert 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
2244*266f97b5SCy Schubert 		    0, pcbinfo->ipi_wildmask)];
2245*266f97b5SCy Schubert 		CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
2246*266f97b5SCy Schubert #ifdef INET6
2247*266f97b5SCy Schubert 			/* XXX inp locking */
2248*266f97b5SCy Schubert 			if ((inp->inp_vflag & INP_IPV4) == 0)
2249*266f97b5SCy Schubert 				continue;
2250*266f97b5SCy Schubert #endif
2251*266f97b5SCy Schubert 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
2252*266f97b5SCy Schubert 			    inp->inp_lport != lport)
2253*266f97b5SCy Schubert 				continue;
2254*266f97b5SCy Schubert 
2255*266f97b5SCy Schubert 			injail = prison_flag(inp->inp_cred, PR_IP4);
2256*266f97b5SCy Schubert 			if (injail) {
2257*266f97b5SCy Schubert 				if (prison_check_ip4(inp->inp_cred,
2258*266f97b5SCy Schubert 				    &laddr) != 0)
2259*266f97b5SCy Schubert 					continue;
2260*266f97b5SCy Schubert 			} else {
2261*266f97b5SCy Schubert 				if (local_exact != NULL)
2262*266f97b5SCy Schubert 					continue;
2263*266f97b5SCy Schubert 			}
2264*266f97b5SCy Schubert 
2265*266f97b5SCy Schubert 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
2266*266f97b5SCy Schubert 				if (injail)
2267*266f97b5SCy Schubert 					goto found;
2268*266f97b5SCy Schubert 				else
2269*266f97b5SCy Schubert 					local_exact = inp;
2270*266f97b5SCy Schubert 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
2271*266f97b5SCy Schubert #ifdef INET6
2272*266f97b5SCy Schubert 				/* XXX inp locking, NULL check */
2273*266f97b5SCy Schubert 				if (inp->inp_vflag & INP_IPV6PROTO)
2274*266f97b5SCy Schubert 					local_wild_mapped = inp;
2275*266f97b5SCy Schubert 				else
2276*266f97b5SCy Schubert #endif
2277*266f97b5SCy Schubert 					if (injail)
2278*266f97b5SCy Schubert 						jail_wild = inp;
2279*266f97b5SCy Schubert 					else
2280*266f97b5SCy Schubert 						local_wild = inp;
2281*266f97b5SCy Schubert 			}
2282*266f97b5SCy Schubert 		} /* LIST_FOREACH */
2283*266f97b5SCy Schubert 		inp = jail_wild;
2284*266f97b5SCy Schubert 		if (inp == NULL)
2285*266f97b5SCy Schubert 			inp = local_exact;
2286*266f97b5SCy Schubert 		if (inp == NULL)
2287*266f97b5SCy Schubert 			inp = local_wild;
2288*266f97b5SCy Schubert #ifdef INET6
2289*266f97b5SCy Schubert 		if (inp == NULL)
2290*266f97b5SCy Schubert 			inp = local_wild_mapped;
2291*266f97b5SCy Schubert #endif
2292*266f97b5SCy Schubert 		if (inp != NULL)
2293*266f97b5SCy Schubert 			goto found;
2294*266f97b5SCy Schubert 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
2295*266f97b5SCy Schubert 	INP_GROUP_UNLOCK(pcbgroup);
2296*266f97b5SCy Schubert 	return (NULL);
2297*266f97b5SCy Schubert 
2298*266f97b5SCy Schubert found:
2299*266f97b5SCy Schubert 	if (lookupflags & INPLOOKUP_WLOCKPCB)
2300*266f97b5SCy Schubert 		locked = INP_TRY_WLOCK(inp);
2301*266f97b5SCy Schubert 	else if (lookupflags & INPLOOKUP_RLOCKPCB)
2302*266f97b5SCy Schubert 		locked = INP_TRY_RLOCK(inp);
2303*266f97b5SCy Schubert 	else
2304*266f97b5SCy Schubert 		panic("%s: locking bug", __func__);
2305*266f97b5SCy Schubert 	if (__predict_false(locked && (inp->inp_flags2 & INP_FREED))) {
2306*266f97b5SCy Schubert 		if (lookupflags & INPLOOKUP_WLOCKPCB)
2307*266f97b5SCy Schubert 			INP_WUNLOCK(inp);
2308*266f97b5SCy Schubert 		else
2309*266f97b5SCy Schubert 			INP_RUNLOCK(inp);
2310*266f97b5SCy Schubert 		return (NULL);
2311*266f97b5SCy Schubert 	} else if (!locked)
2312*266f97b5SCy Schubert 		in_pcbref(inp);
2313*266f97b5SCy Schubert 	INP_GROUP_UNLOCK(pcbgroup);
2314*266f97b5SCy Schubert 	if (!locked) {
2315*266f97b5SCy Schubert 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
2316*266f97b5SCy Schubert 			INP_WLOCK(inp);
2317*266f97b5SCy Schubert 			if (in_pcbrele_wlocked(inp))
2318*266f97b5SCy Schubert 				return (NULL);
2319*266f97b5SCy Schubert 		} else {
2320*266f97b5SCy Schubert 			INP_RLOCK(inp);
2321*266f97b5SCy Schubert 			if (in_pcbrele_rlocked(inp))
2322*266f97b5SCy Schubert 				return (NULL);
2323*266f97b5SCy Schubert 		}
2324*266f97b5SCy Schubert 	}
2325*266f97b5SCy Schubert #ifdef INVARIANTS
2326*266f97b5SCy Schubert 	if (lookupflags & INPLOOKUP_WLOCKPCB)
2327*266f97b5SCy Schubert 		INP_WLOCK_ASSERT(inp);
2328*266f97b5SCy Schubert 	else
2329*266f97b5SCy Schubert 		INP_RLOCK_ASSERT(inp);
2330*266f97b5SCy Schubert #endif
2331*266f97b5SCy Schubert 	return (inp);
2332*266f97b5SCy Schubert }
2333*266f97b5SCy Schubert #endif /* PCBGROUP */
2334*266f97b5SCy Schubert 
233515bd2b43SDavid Greenman /*
2336fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
2337*266f97b5SCy Schubert  * that the caller has locked the hash list, and will not perform any further
2338*266f97b5SCy Schubert  * locking or reference operations on either the hash list or the connection.
233915bd2b43SDavid Greenman  */
2340fa046d87SRobert Watson static struct inpcb *
2341fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
234268e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
2343a034518aSAndrew Gallatin     struct ifnet *ifp, uint8_t numa_domain)
234415bd2b43SDavid Greenman {
234515bd2b43SDavid Greenman 	struct inpcbhead *head;
2346413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
234715bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
234815bd2b43SDavid Greenman 
234968e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
235068e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2351d797164aSGleb Smirnoff 	INP_HASH_LOCK_ASSERT(pcbinfo);
2352d797164aSGleb Smirnoff 
235315bd2b43SDavid Greenman 	/*
235415bd2b43SDavid Greenman 	 * First look for an exact match.
235515bd2b43SDavid Greenman 	 */
2356413628a7SBjoern A. Zeeb 	tmpinp = NULL;
2357712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
2358712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
2359b872626dSMatt Macy 	CK_LIST_FOREACH(inp, head, inp_hash) {
2360cfa1ca9dSYoshinobu Inoue #ifdef INET6
2361413628a7SBjoern A. Zeeb 		/* XXX inp locking */
2362369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
2363cfa1ca9dSYoshinobu Inoue 			continue;
2364cfa1ca9dSYoshinobu Inoue #endif
23656d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
2366ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
2367ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
2368413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
2369413628a7SBjoern A. Zeeb 			/*
2370413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
2371413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
2372413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
2373413628a7SBjoern A. Zeeb 			 */
23740304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
2375c3229e05SDavid Greenman 				return (inp);
2376413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
2377413628a7SBjoern A. Zeeb 				tmpinp = inp;
2378c3229e05SDavid Greenman 		}
2379413628a7SBjoern A. Zeeb 	}
2380413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
2381413628a7SBjoern A. Zeeb 		return (tmpinp);
2382e3fd5ffdSRobert Watson 
2383e3fd5ffdSRobert Watson 	/*
23841a43cff9SSean Bruno 	 * Then look in lb group (for wildcard match).
23851a43cff9SSean Bruno 	 */
2386d9ff5789SMark Johnston 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
23871a43cff9SSean Bruno 		inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr,
2388a034518aSAndrew Gallatin 		    fport, lookupflags, numa_domain);
2389d9ff5789SMark Johnston 		if (inp != NULL)
23901a43cff9SSean Bruno 			return (inp);
23911a43cff9SSean Bruno 	}
23921a43cff9SSean Bruno 
23931a43cff9SSean Bruno 	/*
2394e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
2395e3fd5ffdSRobert Watson 	 */
239668e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
2397413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
2398e3fd5ffdSRobert Watson #ifdef INET6
2399cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
2400e3fd5ffdSRobert Watson #endif
2401413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
2402413628a7SBjoern A. Zeeb 		int injail;
2403413628a7SBjoern A. Zeeb 
2404413628a7SBjoern A. Zeeb 		/*
2405413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
2406413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
2407413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
2408413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
2409413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
2410413628a7SBjoern A. Zeeb 		 */
24116d6a026bSDavid Greenman 
2412712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
2413712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
2414b872626dSMatt Macy 		CK_LIST_FOREACH(inp, head, inp_hash) {
2415cfa1ca9dSYoshinobu Inoue #ifdef INET6
2416413628a7SBjoern A. Zeeb 			/* XXX inp locking */
2417369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
2418cfa1ca9dSYoshinobu Inoue 				continue;
2419cfa1ca9dSYoshinobu Inoue #endif
2420413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
2421413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
2422413628a7SBjoern A. Zeeb 				continue;
2423413628a7SBjoern A. Zeeb 
24240304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
2425413628a7SBjoern A. Zeeb 			if (injail) {
2426b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
2427b89e82ddSJamie Gritton 				    &laddr) != 0)
2428413628a7SBjoern A. Zeeb 					continue;
2429413628a7SBjoern A. Zeeb 			} else {
2430413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
2431413628a7SBjoern A. Zeeb 					continue;
2432413628a7SBjoern A. Zeeb 			}
2433413628a7SBjoern A. Zeeb 
2434413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
2435413628a7SBjoern A. Zeeb 				if (injail)
2436c3229e05SDavid Greenman 					return (inp);
2437413628a7SBjoern A. Zeeb 				else
2438413628a7SBjoern A. Zeeb 					local_exact = inp;
2439413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
2440e3fd5ffdSRobert Watson #ifdef INET6
2441413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
24425cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
2443cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
2444cfa1ca9dSYoshinobu Inoue 				else
244583e521ecSBjoern A. Zeeb #endif
2446413628a7SBjoern A. Zeeb 					if (injail)
2447413628a7SBjoern A. Zeeb 						jail_wild = inp;
2448413628a7SBjoern A. Zeeb 					else
24496d6a026bSDavid Greenman 						local_wild = inp;
24506d6a026bSDavid Greenman 			}
2451413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
2452413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
2453413628a7SBjoern A. Zeeb 			return (jail_wild);
2454413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
2455413628a7SBjoern A. Zeeb 			return (local_exact);
2456413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
2457c3229e05SDavid Greenman 			return (local_wild);
2458413628a7SBjoern A. Zeeb #ifdef INET6
2459413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
2460413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
246183e521ecSBjoern A. Zeeb #endif
246268e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
2463413628a7SBjoern A. Zeeb 
24646d6a026bSDavid Greenman 	return (NULL);
246515bd2b43SDavid Greenman }
2466fa046d87SRobert Watson 
2467fa046d87SRobert Watson /*
2468fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
2469fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
2470fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
2471fa046d87SRobert Watson  */
2472fa046d87SRobert Watson static struct inpcb *
2473fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2474fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2475a034518aSAndrew Gallatin     struct ifnet *ifp, uint8_t numa_domain)
2476fa046d87SRobert Watson {
2477fa046d87SRobert Watson 	struct inpcb *inp;
2478fa046d87SRobert Watson 
2479fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
248008d9c920SGleb Smirnoff 	    lookupflags & INPLOOKUP_WILDCARD, ifp, numa_domain);
2481fa046d87SRobert Watson 	if (inp != NULL) {
2482*266f97b5SCy Schubert 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
2483*266f97b5SCy Schubert 			INP_WLOCK(inp);
2484*266f97b5SCy Schubert 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
2485*266f97b5SCy Schubert 			INP_RLOCK(inp);
2486de2d4784SGleb Smirnoff 		} else
2487*266f97b5SCy Schubert 			panic("%s: locking bug", __func__);
2488*266f97b5SCy Schubert 		if (__predict_false(inp->inp_flags2 & INP_FREED)) {
2489*266f97b5SCy Schubert 			INP_UNLOCK(inp);
2490*266f97b5SCy Schubert 			inp = NULL;
2491*266f97b5SCy Schubert 		}
2492*266f97b5SCy Schubert 	}
2493d797164aSGleb Smirnoff 
2494fa046d87SRobert Watson 	return (inp);
2495fa046d87SRobert Watson }
2496fa046d87SRobert Watson 
2497fa046d87SRobert Watson /*
2498d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
2499d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
2500*266f97b5SCy Schubert  *
2501*266f97b5SCy Schubert  * Possibly more of this logic should be in in_pcbgroup.c.
2502fa046d87SRobert Watson  */
2503fa046d87SRobert Watson struct inpcb *
2504fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
2505fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
2506fa046d87SRobert Watson {
2507*266f97b5SCy Schubert #if defined(PCBGROUP) && !defined(RSS)
2508*266f97b5SCy Schubert 	struct inpcbgroup *pcbgroup;
2509*266f97b5SCy Schubert #endif
2510fa046d87SRobert Watson 
2511fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2512fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
25131db08fbeSGleb Smirnoff 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
25141db08fbeSGleb Smirnoff 	    ("%s: LOCKPCB not set", __func__));
2515fa046d87SRobert Watson 
2516*266f97b5SCy Schubert 	/*
2517*266f97b5SCy Schubert 	 * When not using RSS, use connection groups in preference to the
2518*266f97b5SCy Schubert 	 * reservation table when looking up 4-tuples.  When using RSS, just
2519*266f97b5SCy Schubert 	 * use the reservation table, due to the cost of the Toeplitz hash
2520*266f97b5SCy Schubert 	 * in software.
2521*266f97b5SCy Schubert 	 *
2522*266f97b5SCy Schubert 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
2523*266f97b5SCy Schubert 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
2524*266f97b5SCy Schubert 	 * in software.
2525*266f97b5SCy Schubert 	 */
2526*266f97b5SCy Schubert #if defined(PCBGROUP) && !defined(RSS)
2527*266f97b5SCy Schubert 	if (in_pcbgroup_enabled(pcbinfo)) {
2528*266f97b5SCy Schubert 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2529*266f97b5SCy Schubert 		    fport);
2530*266f97b5SCy Schubert 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2531*266f97b5SCy Schubert 		    laddr, lport, lookupflags, ifp));
2532*266f97b5SCy Schubert 	}
2533*266f97b5SCy Schubert #endif
2534fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2535a034518aSAndrew Gallatin 	    lookupflags, ifp, M_NODOM));
2536fa046d87SRobert Watson }
2537d3c1f003SRobert Watson 
2538d3c1f003SRobert Watson struct inpcb *
2539d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2540d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2541d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2542d3c1f003SRobert Watson {
2543*266f97b5SCy Schubert #ifdef PCBGROUP
2544*266f97b5SCy Schubert 	struct inpcbgroup *pcbgroup;
2545*266f97b5SCy Schubert #endif
2546d3c1f003SRobert Watson 
2547d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2548d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
25491db08fbeSGleb Smirnoff 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
25501db08fbeSGleb Smirnoff 	    ("%s: LOCKPCB not set", __func__));
2551d3c1f003SRobert Watson 
2552*266f97b5SCy Schubert #ifdef PCBGROUP
2553*266f97b5SCy Schubert 	/*
2554*266f97b5SCy Schubert 	 * If we can use a hardware-generated hash to look up the connection
2555*266f97b5SCy Schubert 	 * group, use that connection group to find the inpcb.  Otherwise
2556*266f97b5SCy Schubert 	 * fall back on a software hash -- or the reservation table if we're
2557*266f97b5SCy Schubert 	 * using RSS.
2558*266f97b5SCy Schubert 	 *
2559*266f97b5SCy Schubert 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
2560*266f97b5SCy Schubert 	 */
2561*266f97b5SCy Schubert 	if (in_pcbgroup_enabled(pcbinfo) &&
2562*266f97b5SCy Schubert 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
2563*266f97b5SCy Schubert 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
2564*266f97b5SCy Schubert 		    m->m_pkthdr.flowid);
2565*266f97b5SCy Schubert 		if (pcbgroup != NULL)
2566*266f97b5SCy Schubert 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
2567*266f97b5SCy Schubert 			    fport, laddr, lport, lookupflags, ifp));
2568*266f97b5SCy Schubert #ifndef RSS
2569*266f97b5SCy Schubert 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2570*266f97b5SCy Schubert 		    fport);
2571*266f97b5SCy Schubert 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2572*266f97b5SCy Schubert 		    laddr, lport, lookupflags, ifp));
2573*266f97b5SCy Schubert #endif
2574*266f97b5SCy Schubert 	}
2575*266f97b5SCy Schubert #endif
2576d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2577a034518aSAndrew Gallatin 	    lookupflags, ifp, m->m_pkthdr.numa_domain));
2578d3c1f003SRobert Watson }
257967107f45SBjoern A. Zeeb #endif /* INET */
258015bd2b43SDavid Greenman 
25817bc4aca7SDavid Greenman /*
2582c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
25837bc4aca7SDavid Greenman  */
2584*266f97b5SCy Schubert static int
2585*266f97b5SCy Schubert in_pcbinshash_internal(struct inpcb *inp, struct mbuf *m)
258615bd2b43SDavid Greenman {
2587c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2588c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2589c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2590c3229e05SDavid Greenman 	struct inpcbport *phd;
2591cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
25921a43cff9SSean Bruno 	int so_options;
259315bd2b43SDavid Greenman 
25948501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2595fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2596fa046d87SRobert Watson 
2597111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2598111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2599602cc7f1SRobert Watson 
2600cfa1ca9dSYoshinobu Inoue #ifdef INET6
2601cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
26021b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2603cfa1ca9dSYoshinobu Inoue 	else
260483e521ecSBjoern A. Zeeb #endif
2605cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2606cfa1ca9dSYoshinobu Inoue 
2607712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2608712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
260915bd2b43SDavid Greenman 
2610712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2611712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2612c3229e05SDavid Greenman 
2613c3229e05SDavid Greenman 	/*
26141a43cff9SSean Bruno 	 * Add entry to load balance group.
26151a43cff9SSean Bruno 	 * Only do this if SO_REUSEPORT_LB is set.
26161a43cff9SSean Bruno 	 */
26171a43cff9SSean Bruno 	so_options = inp_so_options(inp);
26181a43cff9SSean Bruno 	if (so_options & SO_REUSEPORT_LB) {
2619a034518aSAndrew Gallatin 		int ret = in_pcbinslbgrouphash(inp, M_NODOM);
26201a43cff9SSean Bruno 		if (ret) {
26211a43cff9SSean Bruno 			/* pcb lb group malloc fail (ret=ENOBUFS). */
26221a43cff9SSean Bruno 			return (ret);
26231a43cff9SSean Bruno 		}
26241a43cff9SSean Bruno 	}
26251a43cff9SSean Bruno 
26261a43cff9SSean Bruno 	/*
2627c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2628c3229e05SDavid Greenman 	 */
2629b872626dSMatt Macy 	CK_LIST_FOREACH(phd, pcbporthash, phd_hash) {
2630c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2631c3229e05SDavid Greenman 			break;
2632c3229e05SDavid Greenman 	}
2633c3229e05SDavid Greenman 	/*
2634c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2635c3229e05SDavid Greenman 	 */
2636c3229e05SDavid Greenman 	if (phd == NULL) {
2637*266f97b5SCy Schubert 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2638c3229e05SDavid Greenman 		if (phd == NULL) {
2639c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2640c3229e05SDavid Greenman 		}
2641*266f97b5SCy Schubert 		bzero(&phd->phd_epoch_ctx, sizeof(struct epoch_context));
2642c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2643b872626dSMatt Macy 		CK_LIST_INIT(&phd->phd_pcblist);
2644b872626dSMatt Macy 		CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2645c3229e05SDavid Greenman 	}
2646c3229e05SDavid Greenman 	inp->inp_phd = phd;
2647b872626dSMatt Macy 	CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2648b872626dSMatt Macy 	CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2649111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
2650*266f97b5SCy Schubert #ifdef PCBGROUP
2651*266f97b5SCy Schubert 	if (m != NULL) {
2652*266f97b5SCy Schubert 		in_pcbgroup_update_mbuf(inp, m);
2653*266f97b5SCy Schubert 	} else {
2654*266f97b5SCy Schubert 		in_pcbgroup_update(inp);
2655*266f97b5SCy Schubert 	}
2656*266f97b5SCy Schubert #endif
2657c3229e05SDavid Greenman 	return (0);
265815bd2b43SDavid Greenman }
265915bd2b43SDavid Greenman 
2660*266f97b5SCy Schubert int
2661*266f97b5SCy Schubert in_pcbinshash(struct inpcb *inp)
2662*266f97b5SCy Schubert {
2663*266f97b5SCy Schubert 
2664*266f97b5SCy Schubert 	return (in_pcbinshash_internal(inp, NULL));
2665*266f97b5SCy Schubert }
2666*266f97b5SCy Schubert 
2667*266f97b5SCy Schubert int
2668*266f97b5SCy Schubert in_pcbinshash_mbuf(struct inpcb *inp, struct mbuf *m)
2669*266f97b5SCy Schubert {
2670*266f97b5SCy Schubert 
2671*266f97b5SCy Schubert 	return (in_pcbinshash_internal(inp, m));
2672*266f97b5SCy Schubert }
2673*266f97b5SCy Schubert 
267452cd27cbSRobert Watson /*
2675c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2676c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2677c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2678c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2679c3229e05SDavid Greenman  */
268015bd2b43SDavid Greenman void
2681*266f97b5SCy Schubert in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
268215bd2b43SDavid Greenman {
268359daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
268415bd2b43SDavid Greenman 	struct inpcbhead *head;
2685cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
268615bd2b43SDavid Greenman 
26878501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2688fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2689fa046d87SRobert Watson 
2690111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2691111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2692602cc7f1SRobert Watson 
2693cfa1ca9dSYoshinobu Inoue #ifdef INET6
2694cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
26951b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2696cfa1ca9dSYoshinobu Inoue 	else
269783e521ecSBjoern A. Zeeb #endif
2698cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2699cfa1ca9dSYoshinobu Inoue 
2700712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2701712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
270215bd2b43SDavid Greenman 
2703b872626dSMatt Macy 	CK_LIST_REMOVE(inp, inp_hash);
2704b872626dSMatt Macy 	CK_LIST_INSERT_HEAD(head, inp, inp_hash);
2705*266f97b5SCy Schubert 
2706*266f97b5SCy Schubert #ifdef PCBGROUP
2707*266f97b5SCy Schubert 	if (m != NULL)
2708*266f97b5SCy Schubert 		in_pcbgroup_update_mbuf(inp, m);
2709*266f97b5SCy Schubert 	else
2710*266f97b5SCy Schubert 		in_pcbgroup_update(inp);
2711*266f97b5SCy Schubert #endif
2712*266f97b5SCy Schubert }
2713*266f97b5SCy Schubert 
2714*266f97b5SCy Schubert void
2715*266f97b5SCy Schubert in_pcbrehash(struct inpcb *inp)
2716*266f97b5SCy Schubert {
2717*266f97b5SCy Schubert 
2718*266f97b5SCy Schubert 	in_pcbrehash_mbuf(inp, NULL);
2719*266f97b5SCy Schubert }
2720*266f97b5SCy Schubert 
2721*266f97b5SCy Schubert /*
2722*266f97b5SCy Schubert  * Remove PCB from various lists.
2723*266f97b5SCy Schubert  */
2724*266f97b5SCy Schubert static void
2725*266f97b5SCy Schubert in_pcbremlists(struct inpcb *inp)
2726*266f97b5SCy Schubert {
2727*266f97b5SCy Schubert 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2728*266f97b5SCy Schubert 
2729*266f97b5SCy Schubert 	INP_WLOCK_ASSERT(inp);
2730*266f97b5SCy Schubert 	INP_LIST_WLOCK_ASSERT(pcbinfo);
2731*266f97b5SCy Schubert 
2732*266f97b5SCy Schubert 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2733*266f97b5SCy Schubert 	if (inp->inp_flags & INP_INHASHLIST) {
2734*266f97b5SCy Schubert 		struct inpcbport *phd = inp->inp_phd;
2735*266f97b5SCy Schubert 
2736*266f97b5SCy Schubert 		INP_HASH_WLOCK(pcbinfo);
2737*266f97b5SCy Schubert 
2738*266f97b5SCy Schubert 		/* XXX: Only do if SO_REUSEPORT_LB set? */
2739*266f97b5SCy Schubert 		in_pcbremlbgrouphash(inp);
2740*266f97b5SCy Schubert 
2741*266f97b5SCy Schubert 		CK_LIST_REMOVE(inp, inp_hash);
2742*266f97b5SCy Schubert 		CK_LIST_REMOVE(inp, inp_portlist);
2743*266f97b5SCy Schubert 		if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) {
2744*266f97b5SCy Schubert 			CK_LIST_REMOVE(phd, phd_hash);
2745*266f97b5SCy Schubert 			NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx);
2746*266f97b5SCy Schubert 		}
2747*266f97b5SCy Schubert 		INP_HASH_WUNLOCK(pcbinfo);
2748*266f97b5SCy Schubert 		inp->inp_flags &= ~INP_INHASHLIST;
2749*266f97b5SCy Schubert 	}
2750*266f97b5SCy Schubert 	CK_LIST_REMOVE(inp, inp_list);
2751*266f97b5SCy Schubert 	pcbinfo->ipi_count--;
2752*266f97b5SCy Schubert #ifdef PCBGROUP
2753*266f97b5SCy Schubert 	in_pcbgroup_remove(inp);
2754*266f97b5SCy Schubert #endif
2755c3229e05SDavid Greenman }
2756c3229e05SDavid Greenman 
2757c3229e05SDavid Greenman /*
275884cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
275984cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
276084cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
276184cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
276284cc0778SGeorge V. Neville-Neil  */
276384cc0778SGeorge V. Neville-Neil void
276484cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
276584cc0778SGeorge V. Neville-Neil {
276684cc0778SGeorge V. Neville-Neil 
2767fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
276884cc0778SGeorge V. Neville-Neil 	return;
276984cc0778SGeorge V. Neville-Neil }
277084cc0778SGeorge V. Neville-Neil 
277184cc0778SGeorge V. Neville-Neil /*
2772a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2773a557af22SRobert Watson  * label change into the in_pcb for the socket.
2774a557af22SRobert Watson  */
2775a557af22SRobert Watson void
2776136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2777a557af22SRobert Watson {
2778a557af22SRobert Watson #ifdef MAC
2779a557af22SRobert Watson 	struct inpcb *inp;
2780a557af22SRobert Watson 
27814c7c478dSRobert Watson 	inp = sotoinpcb(so);
27824c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2783602cc7f1SRobert Watson 
27848501a69cSRobert Watson 	INP_WLOCK(inp);
2785310e7cebSRobert Watson 	SOCK_LOCK(so);
2786a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2787310e7cebSRobert Watson 	SOCK_UNLOCK(so);
27888501a69cSRobert Watson 	INP_WUNLOCK(inp);
2789a557af22SRobert Watson #endif
2790a557af22SRobert Watson }
27915f311da2SMike Silbersack 
27925f311da2SMike Silbersack /*
2793ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2794ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2795ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2796ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2797ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
27985f311da2SMike Silbersack  */
2799aae49dd3SBjoern A. Zeeb static void
2800136d4f1cSRobert Watson ipport_tick(void *xtp)
28015f311da2SMike Silbersack {
28028b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2803ad3a630fSRobert Watson 
28045ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
28058b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
28068b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
28078b615593SMarko Zec 		if (V_ipport_tcpallocs <=
28088b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2809603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2810603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2811ad3a630fSRobert Watson 		} else
2812603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2813603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
28148b615593SMarko Zec 		CURVNET_RESTORE();
28158b615593SMarko Zec 	}
28165ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
28175f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
28185f311da2SMike Silbersack }
2819497057eeSRobert Watson 
2820aae49dd3SBjoern A. Zeeb static void
2821aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2822aae49dd3SBjoern A. Zeeb {
2823aae49dd3SBjoern A. Zeeb 
2824aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2825aae49dd3SBjoern A. Zeeb }
2826aae49dd3SBjoern A. Zeeb 
2827aae49dd3SBjoern A. Zeeb /*
2828aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2829aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2830aae49dd3SBjoern A. Zeeb  */
2831aae49dd3SBjoern A. Zeeb static void
2832aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2833aae49dd3SBjoern A. Zeeb {
2834aae49dd3SBjoern A. Zeeb 
2835aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2836fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2837aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2838aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2839aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2840aae49dd3SBjoern A. Zeeb }
2841aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2842aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2843aae49dd3SBjoern A. Zeeb 
28443d585327SKip Macy void
28453d585327SKip Macy inp_wlock(struct inpcb *inp)
28463d585327SKip Macy {
28473d585327SKip Macy 
28488501a69cSRobert Watson 	INP_WLOCK(inp);
28493d585327SKip Macy }
28503d585327SKip Macy 
28513d585327SKip Macy void
28523d585327SKip Macy inp_wunlock(struct inpcb *inp)
28533d585327SKip Macy {
28543d585327SKip Macy 
28558501a69cSRobert Watson 	INP_WUNLOCK(inp);
28563d585327SKip Macy }
28573d585327SKip Macy 
28583d585327SKip Macy void
28593d585327SKip Macy inp_rlock(struct inpcb *inp)
28603d585327SKip Macy {
28613d585327SKip Macy 
2862a69042a5SRobert Watson 	INP_RLOCK(inp);
28633d585327SKip Macy }
28643d585327SKip Macy 
28653d585327SKip Macy void
28663d585327SKip Macy inp_runlock(struct inpcb *inp)
28673d585327SKip Macy {
28683d585327SKip Macy 
2869a69042a5SRobert Watson 	INP_RUNLOCK(inp);
28703d585327SKip Macy }
28713d585327SKip Macy 
2872c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
28733d585327SKip Macy void
2874e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
28753d585327SKip Macy {
28763d585327SKip Macy 
28778501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
28783d585327SKip Macy }
28793d585327SKip Macy 
28803d585327SKip Macy void
2881e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
28823d585327SKip Macy {
28833d585327SKip Macy 
28843d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
28853d585327SKip Macy }
28863d585327SKip Macy #endif
28873d585327SKip Macy 
28889378e437SKip Macy void
28899378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
28909378e437SKip Macy {
28919378e437SKip Macy 	struct inpcb *inp;
28929378e437SKip Macy 
2893*266f97b5SCy Schubert 	INP_INFO_WLOCK(&V_tcbinfo);
2894*266f97b5SCy Schubert 	CK_LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2895*266f97b5SCy Schubert 		INP_WLOCK(inp);
28969378e437SKip Macy 		func(inp, arg);
2897*266f97b5SCy Schubert 		INP_WUNLOCK(inp);
2898*266f97b5SCy Schubert 	}
2899*266f97b5SCy Schubert 	INP_INFO_WUNLOCK(&V_tcbinfo);
29009378e437SKip Macy }
29019378e437SKip Macy 
29029378e437SKip Macy struct socket *
29039378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
29049378e437SKip Macy {
29059378e437SKip Macy 
29069378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
29079378e437SKip Macy 	return (inp->inp_socket);
29089378e437SKip Macy }
29099378e437SKip Macy 
29109378e437SKip Macy struct tcpcb *
29119378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
29129378e437SKip Macy {
29139378e437SKip Macy 
29149378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
29159378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
29169378e437SKip Macy }
29179378e437SKip Macy 
29189378e437SKip Macy int
29199378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
29209378e437SKip Macy {
29219378e437SKip Macy 
29229378e437SKip Macy 	return (inp->inp_ip_tos);
29239378e437SKip Macy }
29249378e437SKip Macy 
29259378e437SKip Macy void
29269378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
29279378e437SKip Macy {
29289378e437SKip Macy 
29299378e437SKip Macy 	inp->inp_ip_tos = val;
29309378e437SKip Macy }
29319378e437SKip Macy 
29329378e437SKip Macy void
2933df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
29349d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
29359378e437SKip Macy {
29369378e437SKip Macy 
29379d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2938df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2939df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
29409378e437SKip Macy 	*lp = inp->inp_lport;
29419378e437SKip Macy 	*fp = inp->inp_fport;
29429378e437SKip Macy }
29439378e437SKip Macy 
2944dd0e6c38SKip Macy struct inpcb *
2945dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2946dd0e6c38SKip Macy {
2947dd0e6c38SKip Macy 
2948dd0e6c38SKip Macy 	return (sotoinpcb(so));
2949dd0e6c38SKip Macy }
2950dd0e6c38SKip Macy 
2951dd0e6c38SKip Macy struct tcpcb *
2952dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2953dd0e6c38SKip Macy {
2954dd0e6c38SKip Macy 
2955dd0e6c38SKip Macy 	return (sototcpcb(so));
2956dd0e6c38SKip Macy }
2957dd0e6c38SKip Macy 
2958cc65eb4eSGleb Smirnoff /*
2959cc65eb4eSGleb Smirnoff  * Create an external-format (``xinpcb'') structure using the information in
2960cc65eb4eSGleb Smirnoff  * the kernel-format in_pcb structure pointed to by inp.  This is done to
2961cc65eb4eSGleb Smirnoff  * reduce the spew of irrelevant information over this interface, to isolate
2962cc65eb4eSGleb Smirnoff  * user code from changes in the kernel structure, and potentially to provide
2963cc65eb4eSGleb Smirnoff  * information-hiding if we decide that some of this information should be
2964cc65eb4eSGleb Smirnoff  * hidden from users.
2965cc65eb4eSGleb Smirnoff  */
2966cc65eb4eSGleb Smirnoff void
2967cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi)
2968cc65eb4eSGleb Smirnoff {
2969cc65eb4eSGleb Smirnoff 
297079db6fe7SMark Johnston 	bzero(xi, sizeof(*xi));
2971cc65eb4eSGleb Smirnoff 	xi->xi_len = sizeof(struct xinpcb);
2972cc65eb4eSGleb Smirnoff 	if (inp->inp_socket)
2973cc65eb4eSGleb Smirnoff 		sotoxsocket(inp->inp_socket, &xi->xi_socket);
2974cc65eb4eSGleb Smirnoff 	bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo));
2975cc65eb4eSGleb Smirnoff 	xi->inp_gencnt = inp->inp_gencnt;
29763a20f06aSBrooks Davis 	xi->inp_ppcb = (uintptr_t)inp->inp_ppcb;
2977cc65eb4eSGleb Smirnoff 	xi->inp_flow = inp->inp_flow;
2978cc65eb4eSGleb Smirnoff 	xi->inp_flowid = inp->inp_flowid;
2979cc65eb4eSGleb Smirnoff 	xi->inp_flowtype = inp->inp_flowtype;
2980cc65eb4eSGleb Smirnoff 	xi->inp_flags = inp->inp_flags;
2981cc65eb4eSGleb Smirnoff 	xi->inp_flags2 = inp->inp_flags2;
2982cc65eb4eSGleb Smirnoff 	xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket;
2983cc65eb4eSGleb Smirnoff 	xi->in6p_cksum = inp->in6p_cksum;
2984cc65eb4eSGleb Smirnoff 	xi->in6p_hops = inp->in6p_hops;
2985cc65eb4eSGleb Smirnoff 	xi->inp_ip_tos = inp->inp_ip_tos;
2986cc65eb4eSGleb Smirnoff 	xi->inp_vflag = inp->inp_vflag;
2987cc65eb4eSGleb Smirnoff 	xi->inp_ip_ttl = inp->inp_ip_ttl;
2988cc65eb4eSGleb Smirnoff 	xi->inp_ip_p = inp->inp_ip_p;
2989cc65eb4eSGleb Smirnoff 	xi->inp_ip_minttl = inp->inp_ip_minttl;
2990cc65eb4eSGleb Smirnoff }
2991cc65eb4eSGleb Smirnoff 
2992497057eeSRobert Watson #ifdef DDB
2993497057eeSRobert Watson static void
2994497057eeSRobert Watson db_print_indent(int indent)
2995497057eeSRobert Watson {
2996497057eeSRobert Watson 	int i;
2997497057eeSRobert Watson 
2998497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2999497057eeSRobert Watson 		db_printf(" ");
3000497057eeSRobert Watson }
3001497057eeSRobert Watson 
3002497057eeSRobert Watson static void
3003497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
3004497057eeSRobert Watson {
3005497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
3006497057eeSRobert Watson 
3007497057eeSRobert Watson 	db_print_indent(indent);
3008497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
3009497057eeSRobert Watson 
3010497057eeSRobert Watson 	indent += 2;
3011497057eeSRobert Watson 
301203dc38a4SRobert Watson #ifdef INET6
3013dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3014497057eeSRobert Watson 		/* IPv6. */
3015497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
3016497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
301783e521ecSBjoern A. Zeeb 	} else
301803dc38a4SRobert Watson #endif
301983e521ecSBjoern A. Zeeb 	{
3020497057eeSRobert Watson 		/* IPv4. */
3021497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
3022497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
3023497057eeSRobert Watson 	}
3024497057eeSRobert Watson 	db_print_indent(indent);
3025497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
3026497057eeSRobert Watson 	    ntohs(inc->inc_lport));
3027497057eeSRobert Watson 	db_print_indent(indent);
3028497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
3029497057eeSRobert Watson 	    ntohs(inc->inc_fport));
3030497057eeSRobert Watson }
3031497057eeSRobert Watson 
3032497057eeSRobert Watson static void
3033497057eeSRobert Watson db_print_inpflags(int inp_flags)
3034497057eeSRobert Watson {
3035497057eeSRobert Watson 	int comma;
3036497057eeSRobert Watson 
3037497057eeSRobert Watson 	comma = 0;
3038497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
3039497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
3040497057eeSRobert Watson 		comma = 1;
3041497057eeSRobert Watson 	}
3042497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
3043497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
3044497057eeSRobert Watson 		comma = 1;
3045497057eeSRobert Watson 	}
3046497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
3047497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
3048497057eeSRobert Watson 		comma = 1;
3049497057eeSRobert Watson 	}
3050dce33a45SErmal Luçi 	if (inp_flags & INP_ORIGDSTADDR) {
3051dce33a45SErmal Luçi 		db_printf("%sINP_ORIGDSTADDR", comma ? ", " : "");
3052dce33a45SErmal Luçi 		comma = 1;
3053dce33a45SErmal Luçi 	}
3054497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
3055497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
3056497057eeSRobert Watson 		comma = 1;
3057497057eeSRobert Watson 	}
3058497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
3059497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
3060497057eeSRobert Watson 		comma = 1;
3061497057eeSRobert Watson 	}
3062497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
3063497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
3064497057eeSRobert Watson 		comma = 1;
3065497057eeSRobert Watson 	}
3066497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
3067497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
3068497057eeSRobert Watson 		comma = 1;
3069497057eeSRobert Watson 	}
3070497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
3071497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
3072497057eeSRobert Watson 		comma = 1;
3073497057eeSRobert Watson 	}
3074497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
3075497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
3076497057eeSRobert Watson 		comma = 1;
3077497057eeSRobert Watson 	}
3078497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
3079497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
3080497057eeSRobert Watson 		comma = 1;
3081497057eeSRobert Watson 	}
3082497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
3083497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
3084497057eeSRobert Watson 		comma = 1;
3085497057eeSRobert Watson 	}
30863cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
30873cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
30883cca425bSMichael Tuexen 		comma = 1;
30893cca425bSMichael Tuexen 	}
3090497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
3091497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
3092497057eeSRobert Watson 		comma = 1;
3093497057eeSRobert Watson 	}
3094497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
3095497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
3096497057eeSRobert Watson 		comma = 1;
3097497057eeSRobert Watson 	}
3098497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
3099497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
3100497057eeSRobert Watson 		comma = 1;
3101497057eeSRobert Watson 	}
3102497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
3103497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
3104497057eeSRobert Watson 		comma = 1;
3105497057eeSRobert Watson 	}
3106497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
3107497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
3108497057eeSRobert Watson 		comma = 1;
3109497057eeSRobert Watson 	}
3110497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
3111497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
3112497057eeSRobert Watson 		comma = 1;
3113497057eeSRobert Watson 	}
3114497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
3115497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
3116497057eeSRobert Watson 		comma = 1;
3117497057eeSRobert Watson 	}
3118497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
3119497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
3120497057eeSRobert Watson 		comma = 1;
3121497057eeSRobert Watson 	}
3122497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
3123497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
3124497057eeSRobert Watson 		comma = 1;
3125497057eeSRobert Watson 	}
3126ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
3127ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
3128ad71fe3cSRobert Watson 		comma  = 1;
3129ad71fe3cSRobert Watson 	}
3130ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
3131ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
3132ad71fe3cSRobert Watson 		comma  = 1;
3133ad71fe3cSRobert Watson 	}
3134ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
3135ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
3136ad71fe3cSRobert Watson 		comma  = 1;
3137ad71fe3cSRobert Watson 	}
3138ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
3139ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
3140ad71fe3cSRobert Watson 		comma  = 1;
3141ad71fe3cSRobert Watson 	}
3142497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
3143497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
3144497057eeSRobert Watson 		comma = 1;
3145497057eeSRobert Watson 	}
3146497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
3147497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
3148497057eeSRobert Watson 		comma = 1;
3149497057eeSRobert Watson 	}
3150497057eeSRobert Watson }
3151497057eeSRobert Watson 
3152497057eeSRobert Watson static void
3153497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
3154497057eeSRobert Watson {
3155497057eeSRobert Watson 	int comma;
3156497057eeSRobert Watson 
3157497057eeSRobert Watson 	comma = 0;
3158497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
3159497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
3160497057eeSRobert Watson 		comma  = 1;
3161497057eeSRobert Watson 	}
3162497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
3163497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
3164497057eeSRobert Watson 		comma  = 1;
3165497057eeSRobert Watson 	}
3166497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
3167497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
3168497057eeSRobert Watson 		comma  = 1;
3169497057eeSRobert Watson 	}
3170497057eeSRobert Watson }
3171497057eeSRobert Watson 
31726d888973SRobert Watson static void
3173497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
3174497057eeSRobert Watson {
3175497057eeSRobert Watson 
3176497057eeSRobert Watson 	db_print_indent(indent);
3177497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
3178497057eeSRobert Watson 
3179497057eeSRobert Watson 	indent += 2;
3180497057eeSRobert Watson 
3181497057eeSRobert Watson 	db_print_indent(indent);
3182497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
3183497057eeSRobert Watson 
3184497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
3185497057eeSRobert Watson 
3186497057eeSRobert Watson 	db_print_indent(indent);
3187497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
3188497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
3189497057eeSRobert Watson 
3190497057eeSRobert Watson 	db_print_indent(indent);
3191497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
3192497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
3193497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
3194497057eeSRobert Watson 	db_printf(")\n");
3195497057eeSRobert Watson 
3196497057eeSRobert Watson 	db_print_indent(indent);
3197497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
3198497057eeSRobert Watson 	    inp->inp_vflag);
3199497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
3200497057eeSRobert Watson 	db_printf(")\n");
3201497057eeSRobert Watson 
3202497057eeSRobert Watson 	db_print_indent(indent);
3203497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
3204497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
3205497057eeSRobert Watson 
3206497057eeSRobert Watson 	db_print_indent(indent);
3207497057eeSRobert Watson #ifdef INET6
3208497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
3209497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
3210497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
3211497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
3212497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
3213497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
3214497057eeSRobert Watson 		    inp->in6p_hops);
3215497057eeSRobert Watson 	} else
3216497057eeSRobert Watson #endif
3217497057eeSRobert Watson 	{
3218497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
3219497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
3220497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
3221497057eeSRobert Watson 	}
3222497057eeSRobert Watson 
3223497057eeSRobert Watson 	db_print_indent(indent);
3224497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
3225497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
3226497057eeSRobert Watson }
3227497057eeSRobert Watson 
3228497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
3229497057eeSRobert Watson {
3230497057eeSRobert Watson 	struct inpcb *inp;
3231497057eeSRobert Watson 
3232497057eeSRobert Watson 	if (!have_addr) {
3233497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
3234497057eeSRobert Watson 		return;
3235497057eeSRobert Watson 	}
3236497057eeSRobert Watson 	inp = (struct inpcb *)addr;
3237497057eeSRobert Watson 
3238497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
3239497057eeSRobert Watson }
324083e521ecSBjoern A. Zeeb #endif /* DDB */
3241f3e7afe2SHans Petter Selasky 
3242f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
3243f3e7afe2SHans Petter Selasky /*
3244f3e7afe2SHans Petter Selasky  * Modify TX rate limit based on the existing "inp->inp_snd_tag",
3245f3e7afe2SHans Petter Selasky  * if any.
3246f3e7afe2SHans Petter Selasky  */
3247f3e7afe2SHans Petter Selasky int
3248f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate)
3249f3e7afe2SHans Petter Selasky {
3250f3e7afe2SHans Petter Selasky 	union if_snd_tag_modify_params params = {
3251f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
325220abea66SRandall Stewart 		.rate_limit.flags = M_NOWAIT,
3253f3e7afe2SHans Petter Selasky 	};
3254f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3255f3e7afe2SHans Petter Selasky 	int error;
3256f3e7afe2SHans Petter Selasky 
3257f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3258f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3259f3e7afe2SHans Petter Selasky 		return (EINVAL);
3260f3e7afe2SHans Petter Selasky 
3261c782ea8bSJohn Baldwin 	if (mst->sw->snd_tag_modify == NULL) {
3262f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
3263f3e7afe2SHans Petter Selasky 	} else {
3264c782ea8bSJohn Baldwin 		error = mst->sw->snd_tag_modify(mst, &params);
3265f3e7afe2SHans Petter Selasky 	}
3266f3e7afe2SHans Petter Selasky 	return (error);
3267f3e7afe2SHans Petter Selasky }
3268f3e7afe2SHans Petter Selasky 
3269f3e7afe2SHans Petter Selasky /*
3270f3e7afe2SHans Petter Selasky  * Query existing TX rate limit based on the existing
3271f3e7afe2SHans Petter Selasky  * "inp->inp_snd_tag", if any.
3272f3e7afe2SHans Petter Selasky  */
3273f3e7afe2SHans Petter Selasky int
3274f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate)
3275f3e7afe2SHans Petter Selasky {
3276f3e7afe2SHans Petter Selasky 	union if_snd_tag_query_params params = { };
3277f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3278f3e7afe2SHans Petter Selasky 	int error;
3279f3e7afe2SHans Petter Selasky 
3280f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3281f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3282f3e7afe2SHans Petter Selasky 		return (EINVAL);
3283f3e7afe2SHans Petter Selasky 
3284c782ea8bSJohn Baldwin 	if (mst->sw->snd_tag_query == NULL) {
3285f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
3286f3e7afe2SHans Petter Selasky 	} else {
3287c782ea8bSJohn Baldwin 		error = mst->sw->snd_tag_query(mst, &params);
3288f3e7afe2SHans Petter Selasky 		if (error == 0 && p_max_pacing_rate != NULL)
3289f3e7afe2SHans Petter Selasky 			*p_max_pacing_rate = params.rate_limit.max_rate;
3290f3e7afe2SHans Petter Selasky 	}
3291f3e7afe2SHans Petter Selasky 	return (error);
3292f3e7afe2SHans Petter Selasky }
3293f3e7afe2SHans Petter Selasky 
3294f3e7afe2SHans Petter Selasky /*
329595ed5015SHans Petter Selasky  * Query existing TX queue level based on the existing
329695ed5015SHans Petter Selasky  * "inp->inp_snd_tag", if any.
329795ed5015SHans Petter Selasky  */
329895ed5015SHans Petter Selasky int
329995ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level)
330095ed5015SHans Petter Selasky {
330195ed5015SHans Petter Selasky 	union if_snd_tag_query_params params = { };
330295ed5015SHans Petter Selasky 	struct m_snd_tag *mst;
330395ed5015SHans Petter Selasky 	int error;
330495ed5015SHans Petter Selasky 
330595ed5015SHans Petter Selasky 	mst = inp->inp_snd_tag;
330695ed5015SHans Petter Selasky 	if (mst == NULL)
330795ed5015SHans Petter Selasky 		return (EINVAL);
330895ed5015SHans Petter Selasky 
3309c782ea8bSJohn Baldwin 	if (mst->sw->snd_tag_query == NULL)
331095ed5015SHans Petter Selasky 		return (EOPNOTSUPP);
331195ed5015SHans Petter Selasky 
3312c782ea8bSJohn Baldwin 	error = mst->sw->snd_tag_query(mst, &params);
331395ed5015SHans Petter Selasky 	if (error == 0 && p_txqueue_level != NULL)
331495ed5015SHans Petter Selasky 		*p_txqueue_level = params.rate_limit.queue_level;
331595ed5015SHans Petter Selasky 	return (error);
331695ed5015SHans Petter Selasky }
331795ed5015SHans Petter Selasky 
331895ed5015SHans Petter Selasky /*
3319f3e7afe2SHans Petter Selasky  * Allocate a new TX rate limit send tag from the network interface
3320f3e7afe2SHans Petter Selasky  * given by the "ifp" argument and save it in "inp->inp_snd_tag":
3321f3e7afe2SHans Petter Selasky  */
3322f3e7afe2SHans Petter Selasky int
3323f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp,
332420abea66SRandall Stewart     uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st)
332520abea66SRandall Stewart 
3326f3e7afe2SHans Petter Selasky {
3327f3e7afe2SHans Petter Selasky 	union if_snd_tag_alloc_params params = {
332895ed5015SHans Petter Selasky 		.rate_limit.hdr.type = (max_pacing_rate == -1U) ?
332995ed5015SHans Petter Selasky 		    IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT,
3330f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowid = flowid,
3331f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowtype = flowtype,
333298085baeSAndrew Gallatin 		.rate_limit.hdr.numa_domain = inp->inp_numa_domain,
3333f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
333420abea66SRandall Stewart 		.rate_limit.flags = M_NOWAIT,
3335f3e7afe2SHans Petter Selasky 	};
3336f3e7afe2SHans Petter Selasky 	int error;
3337f3e7afe2SHans Petter Selasky 
3338f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
3339f3e7afe2SHans Petter Selasky 
334085d8d30fSHans Petter Selasky 	/*
334185d8d30fSHans Petter Selasky 	 * If there is already a send tag, or the INP is being torn
334285d8d30fSHans Petter Selasky 	 * down, allocating a new send tag is not allowed. Else send
334385d8d30fSHans Petter Selasky 	 * tags may leak.
334485d8d30fSHans Petter Selasky 	 */
334585d8d30fSHans Petter Selasky 	if (*st != NULL || (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) != 0)
3346f3e7afe2SHans Petter Selasky 		return (EINVAL);
3347f3e7afe2SHans Petter Selasky 
334836e0a362SJohn Baldwin 	error = m_snd_tag_alloc(ifp, &params, st);
3349903c4ee6SXin LI #ifdef INET
335020abea66SRandall Stewart 	if (error == 0) {
335120abea66SRandall Stewart 		counter_u64_add(rate_limit_set_ok, 1);
335220abea66SRandall Stewart 		counter_u64_add(rate_limit_active, 1);
335336e0a362SJohn Baldwin 	} else if (error != EOPNOTSUPP)
335420abea66SRandall Stewart 		  counter_u64_add(rate_limit_alloc_fail, 1);
3355903c4ee6SXin LI #endif
3356f3e7afe2SHans Petter Selasky 	return (error);
3357f3e7afe2SHans Petter Selasky }
3358f3e7afe2SHans Petter Selasky 
335920abea66SRandall Stewart void
336098d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst)
336120abea66SRandall Stewart {
336220abea66SRandall Stewart 
336398d7a8d9SJohn Baldwin 	m_snd_tag_rele(mst);
3364903c4ee6SXin LI #ifdef INET
336520abea66SRandall Stewart 	counter_u64_add(rate_limit_active, -1);
3366903c4ee6SXin LI #endif
336720abea66SRandall Stewart }
336820abea66SRandall Stewart 
3369f3e7afe2SHans Petter Selasky /*
3370f3e7afe2SHans Petter Selasky  * Free an existing TX rate limit tag based on the "inp->inp_snd_tag",
3371f3e7afe2SHans Petter Selasky  * if any:
3372f3e7afe2SHans Petter Selasky  */
3373f3e7afe2SHans Petter Selasky void
3374f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp)
3375f3e7afe2SHans Petter Selasky {
3376f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3377f3e7afe2SHans Petter Selasky 
3378f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
3379f3e7afe2SHans Petter Selasky 
3380f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3381f3e7afe2SHans Petter Selasky 	inp->inp_snd_tag = NULL;
3382f3e7afe2SHans Petter Selasky 
3383f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3384f3e7afe2SHans Petter Selasky 		return;
3385f3e7afe2SHans Petter Selasky 
3386fb3bc596SJohn Baldwin 	m_snd_tag_rele(mst);
3387093e7231SHans Petter Selasky #ifdef INET
3388093e7231SHans Petter Selasky 	counter_u64_add(rate_limit_active, -1);
3389093e7231SHans Petter Selasky #endif
3390f3e7afe2SHans Petter Selasky }
3391f3e7afe2SHans Petter Selasky 
339220abea66SRandall Stewart int
339320abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate)
339420abea66SRandall Stewart {
339520abea66SRandall Stewart 	int error;
339620abea66SRandall Stewart 
339720abea66SRandall Stewart 	/*
339820abea66SRandall Stewart 	 * If the existing send tag is for the wrong interface due to
339920abea66SRandall Stewart 	 * a route change, first drop the existing tag.  Set the
340020abea66SRandall Stewart 	 * CHANGED flag so that we will keep trying to allocate a new
340120abea66SRandall Stewart 	 * tag if we fail to allocate one this time.
340220abea66SRandall Stewart 	 */
340320abea66SRandall Stewart 	if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) {
340420abea66SRandall Stewart 		in_pcbdetach_txrtlmt(inp);
340520abea66SRandall Stewart 		inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
340620abea66SRandall Stewart 	}
340720abea66SRandall Stewart 
340820abea66SRandall Stewart 	/*
340920abea66SRandall Stewart 	 * NOTE: When attaching to a network interface a reference is
341020abea66SRandall Stewart 	 * made to ensure the network interface doesn't go away until
341120abea66SRandall Stewart 	 * all ratelimit connections are gone. The network interface
341220abea66SRandall Stewart 	 * pointers compared below represent valid network interfaces,
341320abea66SRandall Stewart 	 * except when comparing towards NULL.
341420abea66SRandall Stewart 	 */
341520abea66SRandall Stewart 	if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) {
341620abea66SRandall Stewart 		error = 0;
341720abea66SRandall Stewart 	} else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) {
341820abea66SRandall Stewart 		if (inp->inp_snd_tag != NULL)
341920abea66SRandall Stewart 			in_pcbdetach_txrtlmt(inp);
342020abea66SRandall Stewart 		error = 0;
342120abea66SRandall Stewart 	} else if (inp->inp_snd_tag == NULL) {
342220abea66SRandall Stewart 		/*
342320abea66SRandall Stewart 		 * In order to utilize packet pacing with RSS, we need
342420abea66SRandall Stewart 		 * to wait until there is a valid RSS hash before we
342520abea66SRandall Stewart 		 * can proceed:
342620abea66SRandall Stewart 		 */
342720abea66SRandall Stewart 		if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) {
342820abea66SRandall Stewart 			error = EAGAIN;
342920abea66SRandall Stewart 		} else {
343020abea66SRandall Stewart 			error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb),
343120abea66SRandall Stewart 			    mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag);
343220abea66SRandall Stewart 		}
343320abea66SRandall Stewart 	} else {
343420abea66SRandall Stewart 		error = in_pcbmodify_txrtlmt(inp, max_pacing_rate);
343520abea66SRandall Stewart 	}
343620abea66SRandall Stewart 	if (error == 0 || error == EOPNOTSUPP)
343720abea66SRandall Stewart 		inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
343820abea66SRandall Stewart 
343920abea66SRandall Stewart 	return (error);
344020abea66SRandall Stewart }
344120abea66SRandall Stewart 
3442f3e7afe2SHans Petter Selasky /*
3443f3e7afe2SHans Petter Selasky  * This function should be called when the INP_RATE_LIMIT_CHANGED flag
3444f3e7afe2SHans Petter Selasky  * is set in the fast path and will attach/detach/modify the TX rate
3445f3e7afe2SHans Petter Selasky  * limit send tag based on the socket's so_max_pacing_rate value.
3446f3e7afe2SHans Petter Selasky  */
3447f3e7afe2SHans Petter Selasky void
3448f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb)
3449f3e7afe2SHans Petter Selasky {
3450f3e7afe2SHans Petter Selasky 	struct socket *socket;
3451f3e7afe2SHans Petter Selasky 	uint32_t max_pacing_rate;
3452f3e7afe2SHans Petter Selasky 	bool did_upgrade;
3453f3e7afe2SHans Petter Selasky 	int error;
3454f3e7afe2SHans Petter Selasky 
3455f3e7afe2SHans Petter Selasky 	if (inp == NULL)
3456f3e7afe2SHans Petter Selasky 		return;
3457f3e7afe2SHans Petter Selasky 
3458f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
3459f3e7afe2SHans Petter Selasky 	if (socket == NULL)
3460f3e7afe2SHans Petter Selasky 		return;
3461f3e7afe2SHans Petter Selasky 
3462f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
3463f3e7afe2SHans Petter Selasky 		/*
3464f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
3465f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
3466f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3467f3e7afe2SHans Petter Selasky 		 * write lock.
3468f3e7afe2SHans Petter Selasky 		 */
3469f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3470f3e7afe2SHans Petter Selasky 			return;
3471f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3472f3e7afe2SHans Petter Selasky 	} else {
3473f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3474f3e7afe2SHans Petter Selasky 	}
3475f3e7afe2SHans Petter Selasky 
3476f3e7afe2SHans Petter Selasky 	/*
3477f3e7afe2SHans Petter Selasky 	 * NOTE: The so_max_pacing_rate value is read unlocked,
3478f3e7afe2SHans Petter Selasky 	 * because atomic updates are not required since the variable
3479f3e7afe2SHans Petter Selasky 	 * is checked at every mbuf we send. It is assumed that the
3480f3e7afe2SHans Petter Selasky 	 * variable read itself will be atomic.
3481f3e7afe2SHans Petter Selasky 	 */
3482f3e7afe2SHans Petter Selasky 	max_pacing_rate = socket->so_max_pacing_rate;
3483f3e7afe2SHans Petter Selasky 
348420abea66SRandall Stewart 	error = in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate);
3485fb3bc596SJohn Baldwin 
3486f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3487f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3488f3e7afe2SHans Petter Selasky }
3489f3e7afe2SHans Petter Selasky 
3490f3e7afe2SHans Petter Selasky /*
3491f3e7afe2SHans Petter Selasky  * Track route changes for TX rate limiting.
3492f3e7afe2SHans Petter Selasky  */
3493f3e7afe2SHans Petter Selasky void
3494f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp)
3495f3e7afe2SHans Petter Selasky {
3496f3e7afe2SHans Petter Selasky 	bool did_upgrade;
3497f3e7afe2SHans Petter Selasky 
3498f3e7afe2SHans Petter Selasky 	if (inp == NULL)
3499f3e7afe2SHans Petter Selasky 		return;
3500f3e7afe2SHans Petter Selasky 
3501f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag == NULL)
3502f3e7afe2SHans Petter Selasky 		return;
3503f3e7afe2SHans Petter Selasky 
3504f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
3505f3e7afe2SHans Petter Selasky 		/*
3506f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
3507f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
3508f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3509f3e7afe2SHans Petter Selasky 		 * write lock.
3510f3e7afe2SHans Petter Selasky 		 */
3511f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3512f3e7afe2SHans Petter Selasky 			return;
3513f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3514f3e7afe2SHans Petter Selasky 	} else {
3515f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3516f3e7afe2SHans Petter Selasky 	}
3517f3e7afe2SHans Petter Selasky 
3518f3e7afe2SHans Petter Selasky 	/* detach rate limiting */
3519f3e7afe2SHans Petter Selasky 	in_pcbdetach_txrtlmt(inp);
3520f3e7afe2SHans Petter Selasky 
3521f3e7afe2SHans Petter Selasky 	/* make sure new mbuf send tag allocation is made */
3522f3e7afe2SHans Petter Selasky 	inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
3523f3e7afe2SHans Petter Selasky 
3524f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3525f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3526f3e7afe2SHans Petter Selasky }
352720abea66SRandall Stewart 
3528903c4ee6SXin LI #ifdef INET
352920abea66SRandall Stewart static void
353020abea66SRandall Stewart rl_init(void *st)
353120abea66SRandall Stewart {
35321a714ff2SRandall Stewart 	rate_limit_new = counter_u64_alloc(M_WAITOK);
35331a714ff2SRandall Stewart 	rate_limit_chg = counter_u64_alloc(M_WAITOK);
353420abea66SRandall Stewart 	rate_limit_active = counter_u64_alloc(M_WAITOK);
353520abea66SRandall Stewart 	rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK);
353620abea66SRandall Stewart 	rate_limit_set_ok = counter_u64_alloc(M_WAITOK);
353720abea66SRandall Stewart }
353820abea66SRandall Stewart 
353920abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL);
3540903c4ee6SXin LI #endif
3541f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */
3542