xref: /freebsd/sys/netinet/in_pcb.c (revision 98085bae8cc0aaef80c781691eb679d93bc69c25)
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"
4852cd27cbSRobert Watson #include "opt_pcbgroup.h"
497527624eSRobert Watson #include "opt_rss.h"
50cfa1ca9dSYoshinobu Inoue 
51df8bae1dSRodney W. Grimes #include <sys/param.h>
52df8bae1dSRodney W. Grimes #include <sys/systm.h>
53cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
54df8bae1dSRodney W. Grimes #include <sys/malloc.h>
55df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
56aae49dd3SBjoern A. Zeeb #include <sys/callout.h>
57ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h>
58cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
59df8bae1dSRodney W. Grimes #include <sys/protosw.h>
60cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.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>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <net/if.h>
7976039bc8SGleb Smirnoff #include <net/if_var.h>
80cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
816d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h>
82df8bae1dSRodney W. Grimes #include <net/route.h>
83b2bdc62aSAdrian Chadd #include <net/rss_config.h>
84530c0060SRobert Watson #include <net/vnet.h>
85df8bae1dSRodney W. Grimes 
8667107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
87df8bae1dSRodney W. Grimes #include <netinet/in.h>
88df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
8959854ecfSHans Petter Selasky #ifdef INET
9059854ecfSHans Petter Selasky #include <netinet/in_var.h>
9159854ecfSHans Petter Selasky #endif
92df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
93340c35deSJonathan Lemon #include <netinet/tcp_var.h>
943ee9c3c4SRandall Stewart #ifdef TCPHPTS
953ee9c3c4SRandall Stewart #include <netinet/tcp_hpts.h>
963ee9c3c4SRandall Stewart #endif
975f311da2SMike Silbersack #include <netinet/udp.h>
985f311da2SMike Silbersack #include <netinet/udp_var.h>
99cfa1ca9dSYoshinobu Inoue #ifdef INET6
100cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
101efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
10267107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
10367107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
104cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
10559854ecfSHans Petter Selasky #endif
106cfa1ca9dSYoshinobu Inoue 
107fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
108b9234fafSSam Leffler 
109aed55708SRobert Watson #include <security/mac/mac_framework.h>
110aed55708SRobert Watson 
1111a43cff9SSean Bruno #define	INPCBLBGROUP_SIZMIN	8
1121a43cff9SSean Bruno #define	INPCBLBGROUP_SIZMAX	256
1131a43cff9SSean Bruno 
114aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
115aae49dd3SBjoern A. Zeeb 
116101f9fc8SPeter Wemm /*
117101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
118101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
119101f9fc8SPeter Wemm  */
120eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
126101f9fc8SPeter Wemm 
127b0d22693SCrist J. Clark /*
128b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
129b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
130b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
131b0d22693SCrist J. Clark  */
132eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
133eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
134b0d22693SCrist J. Clark 
1355f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
136eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
137eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
138eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
139eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
140eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1415f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ipport_tcplastcount);
142eddfbb76SRobert Watson 
1431e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1446ac48b74SMike Silbersack 
145d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
146d2025bd0SBjoern A. Zeeb #ifdef INET
147fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
148fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
149fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
150fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
15167107f45SBjoern A. Zeeb 
152bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
153bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
154bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
155bbd42ad0SPeter Wemm 
156bbd42ad0SPeter Wemm static int
15782d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
158bbd42ad0SPeter Wemm {
15930a4ab08SBruce Evans 	int error;
16030a4ab08SBruce Evans 
161f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
16230a4ab08SBruce Evans 	if (error == 0) {
163603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
164603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
165603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
166603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
167603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
168603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
169bbd42ad0SPeter Wemm 	}
17030a4ab08SBruce Evans 	return (error);
171bbd42ad0SPeter Wemm }
172bbd42ad0SPeter Wemm 
173bbd42ad0SPeter Wemm #undef RANGECHK
174bbd42ad0SPeter Wemm 
1757029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange,
1767029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1776472ac3dSEd Schouten     "IP Ports");
17833b3ac06SPeter Wemm 
1796df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1807029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1817029da5cSPawel Biernacki     &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I",
1827029da5cSPawel Biernacki     "");
1836df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1847029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1857029da5cSPawel Biernacki     &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I",
1867029da5cSPawel Biernacki     "");
1876df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1887029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1897029da5cSPawel Biernacki     &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I",
1907029da5cSPawel Biernacki     "");
1916df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1927029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1937029da5cSPawel Biernacki     &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I",
1947029da5cSPawel Biernacki     "");
1956df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
1967029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1977029da5cSPawel Biernacki     &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I",
1987029da5cSPawel Biernacki     "");
1996df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
2007029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2017029da5cSPawel Biernacki     &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I",
2027029da5cSPawel Biernacki     "");
2036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
2046df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
2056df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
207eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
2096df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
210eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
2126df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
213eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
2146ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
2156df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
2166df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
217eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
2188b615593SMarko Zec 	"Minimum time to keep sequental port "
2196ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
22020abea66SRandall Stewart 
22120abea66SRandall Stewart #ifdef RATELIMIT
22220abea66SRandall Stewart counter_u64_t rate_limit_active;
22320abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail;
22420abea66SRandall Stewart counter_u64_t rate_limit_set_ok;
22520abea66SRandall Stewart 
2267029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
22720abea66SRandall Stewart     "IP Rate Limiting");
22820abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD,
22920abea66SRandall Stewart     &rate_limit_active, "Active rate limited connections");
23020abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD,
23120abea66SRandall Stewart    &rate_limit_alloc_fail, "Rate limited connection failures");
23220abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD,
23320abea66SRandall Stewart    &rate_limit_set_ok, "Rate limited setting succeeded");
23420abea66SRandall Stewart #endif /* RATELIMIT */
23520abea66SRandall Stewart 
23683e521ecSBjoern A. Zeeb #endif /* INET */
2370312fbe9SPoul-Henning Kamp 
238c3229e05SDavid Greenman /*
239c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
240c3229e05SDavid Greenman  *
241de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
242de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
243de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
244c3229e05SDavid Greenman  */
245c3229e05SDavid Greenman 
2461a43cff9SSean Bruno static struct inpcblbgroup *
2471a43cff9SSean Bruno in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, u_char vflag,
2481a43cff9SSean Bruno     uint16_t port, const union in_dependaddr *addr, int size)
2491a43cff9SSean Bruno {
2501a43cff9SSean Bruno 	struct inpcblbgroup *grp;
2511a43cff9SSean Bruno 	size_t bytes;
2521a43cff9SSean Bruno 
2531a43cff9SSean Bruno 	bytes = __offsetof(struct inpcblbgroup, il_inp[size]);
2541a43cff9SSean Bruno 	grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT);
2551a43cff9SSean Bruno 	if (!grp)
2561a43cff9SSean Bruno 		return (NULL);
2571a43cff9SSean Bruno 	grp->il_vflag = vflag;
2581a43cff9SSean Bruno 	grp->il_lport = port;
2591a43cff9SSean Bruno 	grp->il_dependladdr = *addr;
2601a43cff9SSean Bruno 	grp->il_inpsiz = size;
26154af3d0dSMark Johnston 	CK_LIST_INSERT_HEAD(hdr, grp, il_list);
2621a43cff9SSean Bruno 	return (grp);
2631a43cff9SSean Bruno }
2641a43cff9SSean Bruno 
2651a43cff9SSean Bruno static void
26654af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx)
26754af3d0dSMark Johnston {
26854af3d0dSMark Johnston 	struct inpcblbgroup *grp;
26954af3d0dSMark Johnston 
27054af3d0dSMark Johnston 	grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx);
27154af3d0dSMark Johnston 	free(grp, M_PCB);
27254af3d0dSMark Johnston }
27354af3d0dSMark Johnston 
27454af3d0dSMark Johnston static void
2751a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp)
2761a43cff9SSean Bruno {
2771a43cff9SSean Bruno 
27854af3d0dSMark Johnston 	CK_LIST_REMOVE(grp, il_list);
2792a4bd982SGleb Smirnoff 	NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx);
2801a43cff9SSean Bruno }
2811a43cff9SSean Bruno 
2821a43cff9SSean Bruno static struct inpcblbgroup *
2831a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr,
2841a43cff9SSean Bruno     struct inpcblbgroup *old_grp, int size)
2851a43cff9SSean Bruno {
2861a43cff9SSean Bruno 	struct inpcblbgroup *grp;
2871a43cff9SSean Bruno 	int i;
2881a43cff9SSean Bruno 
2891a43cff9SSean Bruno 	grp = in_pcblbgroup_alloc(hdr, old_grp->il_vflag,
2901a43cff9SSean Bruno 	    old_grp->il_lport, &old_grp->il_dependladdr, size);
29179ee680bSMark Johnston 	if (grp == NULL)
2921a43cff9SSean Bruno 		return (NULL);
2931a43cff9SSean Bruno 
2941a43cff9SSean Bruno 	KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
2951a43cff9SSean Bruno 	    ("invalid new local group size %d and old local group count %d",
2961a43cff9SSean Bruno 	     grp->il_inpsiz, old_grp->il_inpcnt));
2971a43cff9SSean Bruno 
2981a43cff9SSean Bruno 	for (i = 0; i < old_grp->il_inpcnt; ++i)
2991a43cff9SSean Bruno 		grp->il_inp[i] = old_grp->il_inp[i];
3001a43cff9SSean Bruno 	grp->il_inpcnt = old_grp->il_inpcnt;
3011a43cff9SSean Bruno 	in_pcblbgroup_free(old_grp);
3021a43cff9SSean Bruno 	return (grp);
3031a43cff9SSean Bruno }
3041a43cff9SSean Bruno 
3051a43cff9SSean Bruno /*
3061a43cff9SSean Bruno  * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i]
3071a43cff9SSean Bruno  * and shrink group if possible.
3081a43cff9SSean Bruno  */
3091a43cff9SSean Bruno static void
3101a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp,
3111a43cff9SSean Bruno     int i)
3121a43cff9SSean Bruno {
31379ee680bSMark Johnston 	struct inpcblbgroup *grp, *new_grp;
3141a43cff9SSean Bruno 
31579ee680bSMark Johnston 	grp = *grpp;
3161a43cff9SSean Bruno 	for (; i + 1 < grp->il_inpcnt; ++i)
3171a43cff9SSean Bruno 		grp->il_inp[i] = grp->il_inp[i + 1];
3181a43cff9SSean Bruno 	grp->il_inpcnt--;
3191a43cff9SSean Bruno 
3201a43cff9SSean Bruno 	if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN &&
32179ee680bSMark Johnston 	    grp->il_inpcnt <= grp->il_inpsiz / 4) {
3221a43cff9SSean Bruno 		/* Shrink this group. */
32379ee680bSMark Johnston 		new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2);
32479ee680bSMark Johnston 		if (new_grp != NULL)
3251a43cff9SSean Bruno 			*grpp = new_grp;
3261a43cff9SSean Bruno 	}
3271a43cff9SSean Bruno }
3281a43cff9SSean Bruno 
3291a43cff9SSean Bruno /*
3301a43cff9SSean Bruno  * Add PCB to load balance group for SO_REUSEPORT_LB option.
3311a43cff9SSean Bruno  */
3321a43cff9SSean Bruno static int
3331a43cff9SSean Bruno in_pcbinslbgrouphash(struct inpcb *inp)
3341a43cff9SSean Bruno {
335a7026c7fSMark Johnston 	const static struct timeval interval = { 60, 0 };
336a7026c7fSMark Johnston 	static struct timeval lastprint;
3371a43cff9SSean Bruno 	struct inpcbinfo *pcbinfo;
3381a43cff9SSean Bruno 	struct inpcblbgrouphead *hdr;
3391a43cff9SSean Bruno 	struct inpcblbgroup *grp;
34079ee680bSMark Johnston 	uint32_t idx;
3411a43cff9SSean Bruno 
3421a43cff9SSean Bruno 	pcbinfo = inp->inp_pcbinfo;
3431a43cff9SSean Bruno 
3441a43cff9SSean Bruno 	INP_WLOCK_ASSERT(inp);
3451a43cff9SSean Bruno 	INP_HASH_WLOCK_ASSERT(pcbinfo);
3461a43cff9SSean Bruno 
3471a43cff9SSean Bruno 	/*
3481a43cff9SSean Bruno 	 * Don't allow jailed socket to join local group.
3491a43cff9SSean Bruno 	 */
35079ee680bSMark Johnston 	if (inp->inp_socket != NULL && jailed(inp->inp_socket->so_cred))
3511a43cff9SSean Bruno 		return (0);
3521a43cff9SSean Bruno 
3531a43cff9SSean Bruno #ifdef INET6
3541a43cff9SSean Bruno 	/*
3551a43cff9SSean Bruno 	 * Don't allow IPv4 mapped INET6 wild socket.
3561a43cff9SSean Bruno 	 */
3571a43cff9SSean Bruno 	if ((inp->inp_vflag & INP_IPV4) &&
3581a43cff9SSean Bruno 	    inp->inp_laddr.s_addr == INADDR_ANY &&
3591a43cff9SSean Bruno 	    INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) {
3601a43cff9SSean Bruno 		return (0);
3611a43cff9SSean Bruno 	}
3621a43cff9SSean Bruno #endif
3631a43cff9SSean Bruno 
3649d2877fcSMark Johnston 	idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask);
36579ee680bSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[idx];
36654af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
3671a43cff9SSean Bruno 		if (grp->il_vflag == inp->inp_vflag &&
3681a43cff9SSean Bruno 		    grp->il_lport == inp->inp_lport &&
3691a43cff9SSean Bruno 		    memcmp(&grp->il_dependladdr,
3701a43cff9SSean Bruno 		    &inp->inp_inc.inc_ie.ie_dependladdr,
37179ee680bSMark Johnston 		    sizeof(grp->il_dependladdr)) == 0)
3721a43cff9SSean Bruno 			break;
3731a43cff9SSean Bruno 	}
3741a43cff9SSean Bruno 	if (grp == NULL) {
3751a43cff9SSean Bruno 		/* Create new load balance group. */
3761a43cff9SSean Bruno 		grp = in_pcblbgroup_alloc(hdr, inp->inp_vflag,
3771a43cff9SSean Bruno 		    inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
3781a43cff9SSean Bruno 		    INPCBLBGROUP_SIZMIN);
37979ee680bSMark Johnston 		if (grp == NULL)
3801a43cff9SSean Bruno 			return (ENOBUFS);
3811a43cff9SSean Bruno 	} else if (grp->il_inpcnt == grp->il_inpsiz) {
3821a43cff9SSean Bruno 		if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) {
383a7026c7fSMark Johnston 			if (ratecheck(&lastprint, &interval))
3841a43cff9SSean Bruno 				printf("lb group port %d, limit reached\n",
3851a43cff9SSean Bruno 				    ntohs(grp->il_lport));
3861a43cff9SSean Bruno 			return (0);
3871a43cff9SSean Bruno 		}
3881a43cff9SSean Bruno 
3891a43cff9SSean Bruno 		/* Expand this local group. */
3901a43cff9SSean Bruno 		grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2);
39179ee680bSMark Johnston 		if (grp == NULL)
3921a43cff9SSean Bruno 			return (ENOBUFS);
3931a43cff9SSean Bruno 	}
3941a43cff9SSean Bruno 
3951a43cff9SSean Bruno 	KASSERT(grp->il_inpcnt < grp->il_inpsiz,
39679ee680bSMark Johnston 	    ("invalid local group size %d and count %d", grp->il_inpsiz,
39779ee680bSMark Johnston 	    grp->il_inpcnt));
3981a43cff9SSean Bruno 
3991a43cff9SSean Bruno 	grp->il_inp[grp->il_inpcnt] = inp;
4001a43cff9SSean Bruno 	grp->il_inpcnt++;
4011a43cff9SSean Bruno 	return (0);
4021a43cff9SSean Bruno }
4031a43cff9SSean Bruno 
4041a43cff9SSean Bruno /*
4051a43cff9SSean Bruno  * Remove PCB from load balance group.
4061a43cff9SSean Bruno  */
4071a43cff9SSean Bruno static void
4081a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp)
4091a43cff9SSean Bruno {
4101a43cff9SSean Bruno 	struct inpcbinfo *pcbinfo;
4111a43cff9SSean Bruno 	struct inpcblbgrouphead *hdr;
4121a43cff9SSean Bruno 	struct inpcblbgroup *grp;
4131a43cff9SSean Bruno 	int i;
4141a43cff9SSean Bruno 
4151a43cff9SSean Bruno 	pcbinfo = inp->inp_pcbinfo;
4161a43cff9SSean Bruno 
4171a43cff9SSean Bruno 	INP_WLOCK_ASSERT(inp);
4181a43cff9SSean Bruno 	INP_HASH_WLOCK_ASSERT(pcbinfo);
4191a43cff9SSean Bruno 
4201a43cff9SSean Bruno 	hdr = &pcbinfo->ipi_lbgrouphashbase[
4219d2877fcSMark Johnston 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)];
42254af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
4231a43cff9SSean Bruno 		for (i = 0; i < grp->il_inpcnt; ++i) {
4241a43cff9SSean Bruno 			if (grp->il_inp[i] != inp)
4251a43cff9SSean Bruno 				continue;
4261a43cff9SSean Bruno 
4271a43cff9SSean Bruno 			if (grp->il_inpcnt == 1) {
4281a43cff9SSean Bruno 				/* We are the last, free this local group. */
4291a43cff9SSean Bruno 				in_pcblbgroup_free(grp);
4301a43cff9SSean Bruno 			} else {
4311a43cff9SSean Bruno 				/* Pull up inpcbs, shrink group if possible. */
4321a43cff9SSean Bruno 				in_pcblbgroup_reorder(hdr, &grp, i);
4331a43cff9SSean Bruno 			}
4341a43cff9SSean Bruno 			return;
4351a43cff9SSean Bruno 		}
4361a43cff9SSean Bruno 	}
4371a43cff9SSean Bruno }
4381a43cff9SSean Bruno 
439c3229e05SDavid Greenman /*
440cc487c16SGleb Smirnoff  * Different protocols initialize their inpcbs differently - giving
441cc487c16SGleb Smirnoff  * different name to the lock.  But they all are disposed the same.
442cc487c16SGleb Smirnoff  */
443cc487c16SGleb Smirnoff static void
444cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size)
445cc487c16SGleb Smirnoff {
446cc487c16SGleb Smirnoff 	struct inpcb *inp = mem;
447cc487c16SGleb Smirnoff 
448cc487c16SGleb Smirnoff 	INP_LOCK_DESTROY(inp);
449cc487c16SGleb Smirnoff }
450cc487c16SGleb Smirnoff 
451cc487c16SGleb Smirnoff /*
4529bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
4539bcd427bSRobert Watson  * arguments in time.
4549bcd427bSRobert Watson  */
4559bcd427bSRobert Watson void
4569bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
4579bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
458cc487c16SGleb Smirnoff     char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields)
4599bcd427bSRobert Watson {
4609bcd427bSRobert Watson 
4619d2877fcSMark Johnston 	porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1);
4629d2877fcSMark Johnston 
4639bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
464fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
465ff9b006dSJulien Charbon 	INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
4669bcd427bSRobert Watson #ifdef VIMAGE
4679bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
4689bcd427bSRobert Watson #endif
4699bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
470b872626dSMatt Macy 	CK_LIST_INIT(pcbinfo->ipi_listhead);
471fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
4729bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
4739bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
4749bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
4759bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
4769d2877fcSMark Johnston 	pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB,
4771a43cff9SSean Bruno 	    &pcbinfo->ipi_lbgrouphashmask);
47852cd27cbSRobert Watson #ifdef PCBGROUP
47952cd27cbSRobert Watson 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
48052cd27cbSRobert Watson #endif
4819bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
482cc487c16SGleb Smirnoff 	    NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0);
4839bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
4846acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
4856acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
4869bcd427bSRobert Watson }
4879bcd427bSRobert Watson 
4889bcd427bSRobert Watson /*
4899bcd427bSRobert Watson  * Destroy an inpcbinfo.
4909bcd427bSRobert Watson  */
4919bcd427bSRobert Watson void
4929bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
4939bcd427bSRobert Watson {
4949bcd427bSRobert Watson 
495fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
496fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
497fa046d87SRobert Watson 
4989bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
4999bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
5009bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
5011a43cff9SSean Bruno 	hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB,
5021a43cff9SSean Bruno 	    pcbinfo->ipi_lbgrouphashmask);
50352cd27cbSRobert Watson #ifdef PCBGROUP
50452cd27cbSRobert Watson 	in_pcbgroup_destroy(pcbinfo);
50552cd27cbSRobert Watson #endif
5069bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
507ff9b006dSJulien Charbon 	INP_LIST_LOCK_DESTROY(pcbinfo);
508fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
5099bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
5109bcd427bSRobert Watson }
5119bcd427bSRobert Watson 
5129bcd427bSRobert Watson /*
513c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
514d915b280SStephan Uphoff  * On success return with the PCB locked.
515c3229e05SDavid Greenman  */
516df8bae1dSRodney W. Grimes int
517d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
518df8bae1dSRodney W. Grimes {
519136d4f1cSRobert Watson 	struct inpcb *inp;
52013cf67f3SHajimu UMEMOTO 	int error;
521a557af22SRobert Watson 
522a557af22SRobert Watson 	error = 0;
523d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
524df8bae1dSRodney W. Grimes 	if (inp == NULL)
525df8bae1dSRodney W. Grimes 		return (ENOBUFS);
5266a6cefacSGleb Smirnoff 	bzero(&inp->inp_start_zero, inp_zero_size);
52750575ce1SAndrew Gallatin #ifdef NUMA
52850575ce1SAndrew Gallatin 	inp->inp_numa_domain = M_NODOM;
52950575ce1SAndrew Gallatin #endif
53015bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
531df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
53286d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
5338b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
534a557af22SRobert Watson #ifdef MAC
53530d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
536a557af22SRobert Watson 	if (error != 0)
537a557af22SRobert Watson 		goto out;
53830d239bcSRobert Watson 	mac_inpcb_create(so, inp);
539a557af22SRobert Watson #endif
540fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
541fcf59617SAndrey V. Elsukov 	error = ipsec_init_pcbpolicy(inp);
5420bffde27SRobert Watson 	if (error != 0) {
5430bffde27SRobert Watson #ifdef MAC
5440bffde27SRobert Watson 		mac_inpcb_destroy(inp);
5450bffde27SRobert Watson #endif
546a557af22SRobert Watson 		goto out;
5470bffde27SRobert Watson 	}
548b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
549e3fd5ffdSRobert Watson #ifdef INET6
550340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
551340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
552603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
55333841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
554340c35deSJonathan Lemon 	}
55575daea93SPaul Saab #endif
556ff9b006dSJulien Charbon 	INP_WLOCK(inp);
557ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
558b872626dSMatt Macy 	CK_LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
5593d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
560df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
56133841545SHajimu UMEMOTO #ifdef INET6
562603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
56333841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
56433841545SHajimu UMEMOTO #endif
565d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
56679bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
5678c1960d5SMike Karels 
5688c1960d5SMike Karels 	/*
5698c1960d5SMike Karels 	 * Routes in inpcb's can cache L2 as well; they are guaranteed
5708c1960d5SMike Karels 	 * to be cleaned up.
5718c1960d5SMike Karels 	 */
5728c1960d5SMike Karels 	inp->inp_route.ro_flags = RT_LLE_CACHE;
573ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
5747a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC)
575a557af22SRobert Watson out:
57686d02c5cSBjoern A. Zeeb 	if (error != 0) {
57786d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
578a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
57986d02c5cSBjoern A. Zeeb 	}
580a557af22SRobert Watson #endif
581a557af22SRobert Watson 	return (error);
582df8bae1dSRodney W. Grimes }
583df8bae1dSRodney W. Grimes 
58467107f45SBjoern A. Zeeb #ifdef INET
585df8bae1dSRodney W. Grimes int
586136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
587df8bae1dSRodney W. Grimes {
5884b932371SIan Dowse 	int anonport, error;
5894b932371SIan Dowse 
5908501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
591fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
59259daba27SSam Leffler 
5934b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
5944b932371SIan Dowse 		return (EINVAL);
5955cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
5964b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
597b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
5984b932371SIan Dowse 	if (error)
5994b932371SIan Dowse 		return (error);
6004b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
6014b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
6024b932371SIan Dowse 		inp->inp_lport = 0;
6034b932371SIan Dowse 		return (EAGAIN);
6044b932371SIan Dowse 	}
6054b932371SIan Dowse 	if (anonport)
6064b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
6074b932371SIan Dowse 	return (0);
6084b932371SIan Dowse }
60967107f45SBjoern A. Zeeb #endif
6104b932371SIan Dowse 
61139c8c62eSHiren Panchasara /*
61239c8c62eSHiren Panchasara  * Select a local port (number) to use.
61339c8c62eSHiren Panchasara  */
614efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
615efc76f72SBjoern A. Zeeb int
6164616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
6174616026fSErmal Luçi     struct ucred *cred, int lookupflags)
618efc76f72SBjoern A. Zeeb {
619efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
620efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
621efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
622efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
623efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
624efc76f72SBjoern A. Zeeb #ifdef INET
625efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
626efc76f72SBjoern A. Zeeb #endif
627efc76f72SBjoern A. Zeeb 
628efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
629efc76f72SBjoern A. Zeeb 
630efc76f72SBjoern A. Zeeb 	/*
631efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
632efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
633efc76f72SBjoern A. Zeeb 	 */
634efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
635fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
636efc76f72SBjoern A. Zeeb 
637efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
638efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
639efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
640efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
641efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
642cc426dd3SMateusz Guzik 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT);
643efc76f72SBjoern A. Zeeb 		if (error)
644efc76f72SBjoern A. Zeeb 			return (error);
645efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
646efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
647efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
648efc76f72SBjoern A. Zeeb 	} else {
649efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
650efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
651efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
652efc76f72SBjoern A. Zeeb 	}
653efc76f72SBjoern A. Zeeb 	/*
654e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
655efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
656efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
657efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
658efc76f72SBjoern A. Zeeb 	 */
659efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
660e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
661e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
662efc76f72SBjoern A. Zeeb 		dorandom = 1;
663efc76f72SBjoern A. Zeeb 	else
664efc76f72SBjoern A. Zeeb 		dorandom = 0;
665efc76f72SBjoern A. Zeeb 	/*
666efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
667efc76f72SBjoern A. Zeeb 	 * we have the only port available.
668efc76f72SBjoern A. Zeeb 	 */
669efc76f72SBjoern A. Zeeb 	if (first == last)
670efc76f72SBjoern A. Zeeb 		dorandom = 0;
671e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
672e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
673efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
674efc76f72SBjoern A. Zeeb 	/*
675efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
676efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
677efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
678efc76f72SBjoern A. Zeeb 	 */
679efc76f72SBjoern A. Zeeb 	if (first > last) {
680efc76f72SBjoern A. Zeeb 		aux = first;
681efc76f72SBjoern A. Zeeb 		first = last;
682efc76f72SBjoern A. Zeeb 		last = aux;
683efc76f72SBjoern A. Zeeb 	}
684efc76f72SBjoern A. Zeeb 
685efc76f72SBjoern A. Zeeb #ifdef INET
686efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
687efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
6884d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
689efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
690efc76f72SBjoern A. Zeeb 		    __func__, inp));
691efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
692efc76f72SBjoern A. Zeeb 	}
693efc76f72SBjoern A. Zeeb #endif
69467107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
695efc76f72SBjoern A. Zeeb 	lport = *lportp;
696efc76f72SBjoern A. Zeeb 
697efc76f72SBjoern A. Zeeb 	if (dorandom)
698efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
699efc76f72SBjoern A. Zeeb 
700efc76f72SBjoern A. Zeeb 	count = last - first;
701efc76f72SBjoern A. Zeeb 
702efc76f72SBjoern A. Zeeb 	do {
703efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
704efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
705efc76f72SBjoern A. Zeeb 		++*lastport;
706efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
707efc76f72SBjoern A. Zeeb 			*lastport = first;
708efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
709efc76f72SBjoern A. Zeeb 
710efc76f72SBjoern A. Zeeb #ifdef INET6
7114616026fSErmal Luçi 		if ((inp->inp_vflag & INP_IPV6) != 0)
712efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
71368e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
714efc76f72SBjoern A. Zeeb #endif
715efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
716efc76f72SBjoern A. Zeeb 		else
717efc76f72SBjoern A. Zeeb #endif
718efc76f72SBjoern A. Zeeb #ifdef INET
719efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
72068e0d7e0SRobert Watson 			    lport, lookupflags, cred);
721efc76f72SBjoern A. Zeeb #endif
722efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
723efc76f72SBjoern A. Zeeb 
724efc76f72SBjoern A. Zeeb #ifdef INET
7254d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
726efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
727efc76f72SBjoern A. Zeeb #endif
728efc76f72SBjoern A. Zeeb 	*lportp = lport;
729efc76f72SBjoern A. Zeeb 
730efc76f72SBjoern A. Zeeb 	return (0);
731efc76f72SBjoern A. Zeeb }
732efdf104bSMikolaj Golub 
733efdf104bSMikolaj Golub /*
734efdf104bSMikolaj Golub  * Return cached socket options.
735efdf104bSMikolaj Golub  */
7361a43cff9SSean Bruno int
737efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
738efdf104bSMikolaj Golub {
7391a43cff9SSean Bruno 	int so_options;
740efdf104bSMikolaj Golub 
741efdf104bSMikolaj Golub 	so_options = 0;
742efdf104bSMikolaj Golub 
7431a43cff9SSean Bruno 	if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0)
7441a43cff9SSean Bruno 		so_options |= SO_REUSEPORT_LB;
745efdf104bSMikolaj Golub 	if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
746efdf104bSMikolaj Golub 		so_options |= SO_REUSEPORT;
747efdf104bSMikolaj Golub 	if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
748efdf104bSMikolaj Golub 		so_options |= SO_REUSEADDR;
749efdf104bSMikolaj Golub 	return (so_options);
750efdf104bSMikolaj Golub }
751efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
752efc76f72SBjoern A. Zeeb 
7534b932371SIan Dowse /*
7540a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
7550a100a6fSAdrian Chadd  *
7560a100a6fSAdrian Chadd  * ni points to the new inp.
7570a100a6fSAdrian Chadd  * oi points to the exisitng inp.
7580a100a6fSAdrian Chadd  *
7590a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
7600a100a6fSAdrian Chadd  * whether the credentials match.
7610a100a6fSAdrian Chadd  */
762d5bb8bd3SAdrian Chadd int
7630a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
7640a100a6fSAdrian Chadd {
7650a100a6fSAdrian Chadd 	/* Check permissions match */
7660a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
7670a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
7680a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
7690a100a6fSAdrian Chadd 		return (0);
7700a100a6fSAdrian Chadd 
7710a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
7720a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
7730a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
7740a100a6fSAdrian Chadd 		return (0);
7750a100a6fSAdrian Chadd 
7760a100a6fSAdrian Chadd 	/*
7770a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
7780a100a6fSAdrian Chadd 	 * it is and it matches the checks.
7790a100a6fSAdrian Chadd 	 */
7800a100a6fSAdrian Chadd 	return (1);
7810a100a6fSAdrian Chadd }
7820a100a6fSAdrian Chadd 
783d5bb8bd3SAdrian Chadd #ifdef INET
7840a100a6fSAdrian Chadd /*
7854b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
7864b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
7874b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
7884b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
7894b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
7904b932371SIan Dowse  *
7914b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
7924b932371SIan Dowse  */
7934b932371SIan Dowse int
794136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
795136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
7964b932371SIan Dowse {
7974b932371SIan Dowse 	struct socket *so = inp->inp_socket;
79815bd2b43SDavid Greenman 	struct sockaddr_in *sin;
799c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
8004b932371SIan Dowse 	struct in_addr laddr;
801df8bae1dSRodney W. Grimes 	u_short lport = 0;
80268e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
803413628a7SBjoern A. Zeeb 	int error;
804df8bae1dSRodney W. Grimes 
8058501a69cSRobert Watson 	/*
8061a43cff9SSean Bruno 	 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
8071a43cff9SSean Bruno 	 * so that we don't have to add to the (already messy) code below.
8081a43cff9SSean Bruno 	 */
8091a43cff9SSean Bruno 	int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
8101a43cff9SSean Bruno 
8111a43cff9SSean Bruno 	/*
812fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
8138501a69cSRobert Watson 	 */
81459daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
815fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
81659daba27SSam Leffler 
817d7c5a620SMatt Macy 	if (CK_STAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
818df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
8194b932371SIan Dowse 	laddr.s_addr = *laddrp;
8204b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
821df8bae1dSRodney W. Grimes 		return (EINVAL);
8221a43cff9SSean Bruno 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
82368e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
8244616026fSErmal Luçi 	if (nam == NULL) {
8257c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
8267c2f3cb9SJamie Gritton 			return (error);
8277c2f3cb9SJamie Gritton 	} else {
82857bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
82957bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
830df8bae1dSRodney W. Grimes 			return (EINVAL);
831df8bae1dSRodney W. Grimes #ifdef notdef
832df8bae1dSRodney W. Grimes 		/*
833df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
834df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
835df8bae1dSRodney W. Grimes 		 */
836df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
837df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
838df8bae1dSRodney W. Grimes #endif
839b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
840b89e82ddSJamie Gritton 		if (error)
841b89e82ddSJamie Gritton 			return (error);
8424b932371SIan Dowse 		if (sin->sin_port != *lportp) {
8434b932371SIan Dowse 			/* Don't allow the port to change. */
8444b932371SIan Dowse 			if (*lportp != 0)
8454b932371SIan Dowse 				return (EINVAL);
846df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
8474b932371SIan Dowse 		}
8484b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
849df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
850df8bae1dSRodney W. Grimes 			/*
851df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
852df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
853df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
854df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
855df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
856df8bae1dSRodney W. Grimes 			 */
857f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
858df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
8591a43cff9SSean Bruno 			/*
8601a43cff9SSean Bruno 			 * XXX: How to deal with SO_REUSEPORT_LB here?
8611a43cff9SSean Bruno 			 * Treat same as SO_REUSEPORT for now.
8621a43cff9SSean Bruno 			 */
8631a43cff9SSean Bruno 			if ((so->so_options &
8641a43cff9SSean Bruno 			    (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
8651a43cff9SSean Bruno 				reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
866df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
867df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
86883103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
8694209e01aSAdrian Chadd 			/*
8704209e01aSAdrian Chadd 			 * Is the address a local IP address?
871f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
8728696873dSAdrian Chadd 			 * to any endpoint address, local or not.
8734209e01aSAdrian Chadd 			 */
874f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
8758896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
876df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
877df8bae1dSRodney W. Grimes 		}
8784b932371SIan Dowse 		laddr = sin->sin_addr;
879df8bae1dSRodney W. Grimes 		if (lport) {
880df8bae1dSRodney W. Grimes 			struct inpcb *t;
881ae0e7143SRobert Watson 			struct tcptw *tw;
882ae0e7143SRobert Watson 
883df8bae1dSRodney W. Grimes 			/* GROSS */
884603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
885603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
886cc426dd3SMateusz Guzik 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
8872469dd60SGarrett Wollman 				return (EACCES);
888835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
889cc426dd3SMateusz Guzik 			    priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
890078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
891413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
892340c35deSJonathan Lemon 	/*
893340c35deSJonathan Lemon 	 * XXX
894340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
895340c35deSJonathan Lemon 	 */
8964cc20ab1SSeigo Tanimura 				if (t &&
8970a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
898ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
8994658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
9004658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
9014cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
90252b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
9031a43cff9SSean Bruno 				     (t->inp_flags2 & INP_REUSEPORT) ||
9041a43cff9SSean Bruno 				     (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
90586d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
90686d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
9074049a042SGuido van Rooij 					return (EADDRINUSE);
9080a100a6fSAdrian Chadd 
9090a100a6fSAdrian Chadd 				/*
9100a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
9110a100a6fSAdrian Chadd 				 * the credentials need to match and the
9120a100a6fSAdrian Chadd 				 * original socket also has to have been bound
9130a100a6fSAdrian Chadd 				 * with BINDMULTI.
9140a100a6fSAdrian Chadd 				 */
9150a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
9160a100a6fSAdrian Chadd 					return (EADDRINUSE);
9174049a042SGuido van Rooij 			}
918c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
91968e0d7e0SRobert Watson 			    lport, lookupflags, cred);
920ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
921ae0e7143SRobert Watson 				/*
922ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
923ae0e7143SRobert Watson 				 * state recycled, we treat the address as
924ae0e7143SRobert Watson 				 * being in use (for now).  This is better
925ae0e7143SRobert Watson 				 * than a panic, but not desirable.
926ae0e7143SRobert Watson 				 */
927ec95b709SMikolaj Golub 				tw = intotw(t);
928ae0e7143SRobert Watson 				if (tw == NULL ||
9291a43cff9SSean Bruno 				    ((reuseport & tw->tw_so_options) == 0 &&
9301a43cff9SSean Bruno 					(reuseport_lb &
9311a43cff9SSean Bruno 				            tw->tw_so_options) == 0)) {
932340c35deSJonathan Lemon 					return (EADDRINUSE);
9331a43cff9SSean Bruno 				}
9340a100a6fSAdrian Chadd 			} else if (t &&
9350a100a6fSAdrian Chadd 				   ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
9361a43cff9SSean Bruno 				   (reuseport & inp_so_options(t)) == 0 &&
9371a43cff9SSean Bruno 				   (reuseport_lb & inp_so_options(t)) == 0) {
938e3fd5ffdSRobert Watson #ifdef INET6
93933841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
940cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
941cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
942cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
943fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
944fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
945e3fd5ffdSRobert Watson #endif
946df8bae1dSRodney W. Grimes 						return (EADDRINUSE);
9470a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
9480a100a6fSAdrian Chadd 					return (EADDRINUSE);
949df8bae1dSRodney W. Grimes 			}
950cfa1ca9dSYoshinobu Inoue 		}
951df8bae1dSRodney W. Grimes 	}
9524b932371SIan Dowse 	if (*lportp != 0)
9534b932371SIan Dowse 		lport = *lportp;
95433b3ac06SPeter Wemm 	if (lport == 0) {
9554616026fSErmal Luçi 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
956efc76f72SBjoern A. Zeeb 		if (error != 0)
957efc76f72SBjoern A. Zeeb 			return (error);
95833b3ac06SPeter Wemm 
95933b3ac06SPeter Wemm 	}
9604b932371SIan Dowse 	*laddrp = laddr.s_addr;
9614b932371SIan Dowse 	*lportp = lport;
962df8bae1dSRodney W. Grimes 	return (0);
963df8bae1dSRodney W. Grimes }
964df8bae1dSRodney W. Grimes 
965999f1343SGarrett Wollman /*
9665200e00eSIan Dowse  * Connect from a socket to a specified address.
9675200e00eSIan Dowse  * Both address and port must be specified in argument sin.
9685200e00eSIan Dowse  * If don't have a local address for this socket yet,
9695200e00eSIan Dowse  * then pick one.
970999f1343SGarrett Wollman  */
971999f1343SGarrett Wollman int
972d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
973fe1274eeSMichael Tuexen     struct ucred *cred, struct mbuf *m, bool rehash)
974999f1343SGarrett Wollman {
9755200e00eSIan Dowse 	u_short lport, fport;
9765200e00eSIan Dowse 	in_addr_t laddr, faddr;
9775200e00eSIan Dowse 	int anonport, error;
978df8bae1dSRodney W. Grimes 
9798501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
980fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
98127f74fd0SRobert Watson 
9825200e00eSIan Dowse 	lport = inp->inp_lport;
9835200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
9845200e00eSIan Dowse 	anonport = (lport == 0);
9855200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
986b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
9875200e00eSIan Dowse 	if (error)
9885200e00eSIan Dowse 		return (error);
9895200e00eSIan Dowse 
9905200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
9915200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
992fe1274eeSMichael Tuexen 		KASSERT(rehash == true,
993fe1274eeSMichael Tuexen 		    ("Rehashing required for unbound inps"));
9945200e00eSIan Dowse 		inp->inp_lport = lport;
9955200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
9965200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
9975200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
9985200e00eSIan Dowse 			inp->inp_lport = 0;
9995200e00eSIan Dowse 			return (EAGAIN);
10005200e00eSIan Dowse 		}
10015200e00eSIan Dowse 	}
10025200e00eSIan Dowse 
10035200e00eSIan Dowse 	/* Commit the remaining changes. */
10045200e00eSIan Dowse 	inp->inp_lport = lport;
10055200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
10065200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
10075200e00eSIan Dowse 	inp->inp_fport = fport;
1008fe1274eeSMichael Tuexen 	if (rehash) {
1009d3c1f003SRobert Watson 		in_pcbrehash_mbuf(inp, m);
1010fe1274eeSMichael Tuexen 	} else {
1011fe1274eeSMichael Tuexen 		in_pcbinshash_mbuf(inp, m);
1012fe1274eeSMichael Tuexen 	}
10132cb64cb2SGeorge V. Neville-Neil 
10145200e00eSIan Dowse 	if (anonport)
10155200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
10165200e00eSIan Dowse 	return (0);
10175200e00eSIan Dowse }
10185200e00eSIan Dowse 
1019d3c1f003SRobert Watson int
1020d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
1021d3c1f003SRobert Watson {
1022d3c1f003SRobert Watson 
1023fe1274eeSMichael Tuexen 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL, true));
1024d3c1f003SRobert Watson }
1025d3c1f003SRobert Watson 
10265200e00eSIan Dowse /*
10270895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
10280895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
10290895aec3SBjoern A. Zeeb  */
1030ae190832SSteven Hartland int
10310895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
10320895aec3SBjoern A. Zeeb     struct ucred *cred)
10330895aec3SBjoern A. Zeeb {
10340895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
10350895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
10360895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
10370895aec3SBjoern A. Zeeb 	struct route sro;
10380895aec3SBjoern A. Zeeb 	int error;
10390895aec3SBjoern A. Zeeb 
1040c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
1041413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
1042592bcae8SBjoern A. Zeeb 	/*
1043592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
1044592bcae8SBjoern A. Zeeb 	 * if requested.
1045592bcae8SBjoern A. Zeeb 	 */
1046592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
1047592bcae8SBjoern A. Zeeb 		return (0);
1048592bcae8SBjoern A. Zeeb 
10490895aec3SBjoern A. Zeeb 	error = 0;
10500895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
10510895aec3SBjoern A. Zeeb 
10520895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
10530895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
10540895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
10550895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
10560895aec3SBjoern A. Zeeb 
10570895aec3SBjoern A. Zeeb 	/*
10580895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
10590895aec3SBjoern A. Zeeb 	 * else punt.
10600895aec3SBjoern A. Zeeb 	 *
10610895aec3SBjoern A. Zeeb 	 * Find out route to destination.
10620895aec3SBjoern A. Zeeb 	 */
10630895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
10646e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
10650895aec3SBjoern A. Zeeb 
10660895aec3SBjoern A. Zeeb 	/*
10670895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
10680895aec3SBjoern A. Zeeb 	 * the outgoing interface.
10690895aec3SBjoern A. Zeeb 	 *
10700895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
10710895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
10720895aec3SBjoern A. Zeeb 	 * the source address from.
10730895aec3SBjoern A. Zeeb 	 */
10740895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
10758c0fec80SRobert Watson 		struct in_ifaddr *ia;
10760895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
10770895aec3SBjoern A. Zeeb 
10784f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
107958a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
10804f6c66ccSMatt Macy 		if (ia == NULL) {
10814f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
108258a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
10834f6c66ccSMatt Macy 
10844f6c66ccSMatt Macy 		}
10850895aec3SBjoern A. Zeeb 		if (ia == NULL) {
10860895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
10870895aec3SBjoern A. Zeeb 			goto done;
10880895aec3SBjoern A. Zeeb 		}
10890895aec3SBjoern A. Zeeb 
10900304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
10910895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
10920895aec3SBjoern A. Zeeb 			goto done;
10930895aec3SBjoern A. Zeeb 		}
10940895aec3SBjoern A. Zeeb 
10950895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
10960895aec3SBjoern A. Zeeb 		ia = NULL;
1097d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
10980895aec3SBjoern A. Zeeb 
10990895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
11000895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
11010895aec3SBjoern A. Zeeb 				continue;
11020895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
1103b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
11040895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
11050895aec3SBjoern A. Zeeb 				break;
11060895aec3SBjoern A. Zeeb 			}
11070895aec3SBjoern A. Zeeb 		}
11080895aec3SBjoern A. Zeeb 		if (ia != NULL) {
11090895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
11100895aec3SBjoern A. Zeeb 			goto done;
11110895aec3SBjoern A. Zeeb 		}
11120895aec3SBjoern A. Zeeb 
11130895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1114b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
11150895aec3SBjoern A. Zeeb 		goto done;
11160895aec3SBjoern A. Zeeb 	}
11170895aec3SBjoern A. Zeeb 
11180895aec3SBjoern A. Zeeb 	/*
11190895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
11200895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
11210895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
11220895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
11230895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
11240895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
11250895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
11260895aec3SBjoern A. Zeeb 	 */
11270895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
11288c0fec80SRobert Watson 		struct in_ifaddr *ia;
11299317b04eSRobert Watson 		struct ifnet *ifp;
11300895aec3SBjoern A. Zeeb 
11310895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
11320304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
11330895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
11340895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
11350895aec3SBjoern A. Zeeb 			goto done;
11360895aec3SBjoern A. Zeeb 		}
11370895aec3SBjoern A. Zeeb 
11380895aec3SBjoern A. Zeeb 		/* Jailed. */
11390895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
11400895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
1141b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
11420895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
11430895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
11440895aec3SBjoern A. Zeeb 			goto done;
11450895aec3SBjoern A. Zeeb 		}
11460895aec3SBjoern A. Zeeb 
11470895aec3SBjoern A. Zeeb 		/*
11480895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
11490895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
11500895aec3SBjoern A. Zeeb 		 */
11518c0fec80SRobert Watson 		ia = NULL;
11529317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
1153d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
11540895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
11550895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
11560895aec3SBjoern A. Zeeb 				continue;
11570895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
1158b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
11590895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
11600895aec3SBjoern A. Zeeb 				break;
11610895aec3SBjoern A. Zeeb 			}
11620895aec3SBjoern A. Zeeb 		}
11630895aec3SBjoern A. Zeeb 		if (ia != NULL) {
11640895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
11650895aec3SBjoern A. Zeeb 			goto done;
11660895aec3SBjoern A. Zeeb 		}
11670895aec3SBjoern A. Zeeb 
11680895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1169b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
11700895aec3SBjoern A. Zeeb 		goto done;
11710895aec3SBjoern A. Zeeb 	}
11720895aec3SBjoern A. Zeeb 
11730895aec3SBjoern A. Zeeb 	/*
11740895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
11750895aec3SBjoern A. Zeeb 	 * to ourselves is here.
11760895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
11770895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
11780895aec3SBjoern A. Zeeb 	 * a loopback interface.
11790895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
11800895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
11810895aec3SBjoern A. Zeeb 	 */
11820895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
11830895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
11848c0fec80SRobert Watson 		struct in_ifaddr *ia;
11850895aec3SBjoern A. Zeeb 
11860895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
11870895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
11880895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
11890895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
11900895aec3SBjoern A. Zeeb 
119158a39d8cSAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
119258a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
11930895aec3SBjoern A. Zeeb 		if (ia == NULL)
11944f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
119558a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
1196f0bb05fcSQing Li 		if (ia == NULL)
1197f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
11980895aec3SBjoern A. Zeeb 
11990304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
12000895aec3SBjoern A. Zeeb 			if (ia == NULL) {
12010895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
12020895aec3SBjoern A. Zeeb 				goto done;
12030895aec3SBjoern A. Zeeb 			}
12040895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12050895aec3SBjoern A. Zeeb 			goto done;
12060895aec3SBjoern A. Zeeb 		}
12070895aec3SBjoern A. Zeeb 
12080895aec3SBjoern A. Zeeb 		/* Jailed. */
12090895aec3SBjoern A. Zeeb 		if (ia != NULL) {
12100895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
12110895aec3SBjoern A. Zeeb 
12120895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
12130895aec3SBjoern A. Zeeb 			ia = NULL;
1214d7c5a620SMatt Macy 			CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
12150895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
12160895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
12170895aec3SBjoern A. Zeeb 					continue;
12180895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
1219b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
1220b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
12210895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
12220895aec3SBjoern A. Zeeb 					break;
12230895aec3SBjoern A. Zeeb 				}
12240895aec3SBjoern A. Zeeb 			}
12250895aec3SBjoern A. Zeeb 			if (ia != NULL) {
12260895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12270895aec3SBjoern A. Zeeb 				goto done;
12280895aec3SBjoern A. Zeeb 			}
12290895aec3SBjoern A. Zeeb 		}
12300895aec3SBjoern A. Zeeb 
12310895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1232b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
12330895aec3SBjoern A. Zeeb 		goto done;
12340895aec3SBjoern A. Zeeb 	}
12350895aec3SBjoern A. Zeeb 
12360895aec3SBjoern A. Zeeb done:
12370895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
12380895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
12390895aec3SBjoern A. Zeeb 	return (error);
12400895aec3SBjoern A. Zeeb }
12410895aec3SBjoern A. Zeeb 
12420895aec3SBjoern A. Zeeb /*
12435200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
12445200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
12455200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
12465200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
12475200e00eSIan Dowse  * the connect.
12485200e00eSIan Dowse  *
12495200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
12505200e00eSIan Dowse  * and port. These are not updated in the error case.
12515200e00eSIan Dowse  *
12525200e00eSIan Dowse  * If the operation fails because the connection already exists,
12535200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
12545200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
12555200e00eSIan Dowse  * is set to NULL.
12565200e00eSIan Dowse  */
12575200e00eSIan Dowse int
1258136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1259136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1260136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
12615200e00eSIan Dowse {
1262cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
12635200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
12645200e00eSIan Dowse 	struct in_ifaddr *ia;
12655200e00eSIan Dowse 	struct inpcb *oinp;
1266b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
12675200e00eSIan Dowse 	u_short lport, fport;
12685200e00eSIan Dowse 	int error;
12695200e00eSIan Dowse 
12708501a69cSRobert Watson 	/*
12718501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
12728501a69cSRobert Watson 	 * lock is sufficient.
12738501a69cSRobert Watson 	 */
1274c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
127527f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
1276fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
127727f74fd0SRobert Watson 
12785200e00eSIan Dowse 	if (oinpp != NULL)
12795200e00eSIan Dowse 		*oinpp = NULL;
128057bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
1281df8bae1dSRodney W. Grimes 		return (EINVAL);
1282df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
1283df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
1284df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1285df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
12865200e00eSIan Dowse 	laddr.s_addr = *laddrp;
12875200e00eSIan Dowse 	lport = *lportp;
12885200e00eSIan Dowse 	faddr = sin->sin_addr;
12895200e00eSIan Dowse 	fport = sin->sin_port;
12900895aec3SBjoern A. Zeeb 
1291d7c5a620SMatt Macy 	if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) {
1292df8bae1dSRodney W. Grimes 		/*
1293df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1294df8bae1dSRodney W. Grimes 		 * use the primary local address.
1295df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1296df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1297df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1298df8bae1dSRodney W. Grimes 		 */
1299413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
1300cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
1301413628a7SBjoern A. Zeeb 			faddr =
1302d7c5a620SMatt Macy 			    IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1303cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1304b89e82ddSJamie Gritton 			if (cred != NULL &&
1305b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1306b89e82ddSJamie Gritton 				return (error);
13072d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1308cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
1309d7c5a620SMatt Macy 			if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
13102d9cfabaSRobert Watson 			    IFF_BROADCAST)
1311d7c5a620SMatt Macy 				faddr = satosin(&CK_STAILQ_FIRST(
1312603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1313cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
13142d9cfabaSRobert Watson 		}
1315df8bae1dSRodney W. Grimes 	}
13165200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1317d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1318df8bae1dSRodney W. Grimes 		/*
1319df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1320d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1321df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1322df8bae1dSRodney W. Grimes 		 */
13235200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1324df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1325df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1326df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1327df8bae1dSRodney W. Grimes 
1328df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1329df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1330df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
1331cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RLOCK(&in_ifa_tracker);
1332d7c5a620SMatt Macy 				CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1333e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1334e691be70SDaniel Eischen 					    (cred == NULL ||
1335e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1336e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1337df8bae1dSRodney W. Grimes 						break;
1338e691be70SDaniel Eischen 				}
1339e691be70SDaniel Eischen 				if (ia == NULL)
1340d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1341e691be70SDaniel Eischen 				else {
13425200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1343d79fdd98SDaniel Eischen 					error = 0;
1344999f1343SGarrett Wollman 				}
1345cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1346d79fdd98SDaniel Eischen 			}
1347d79fdd98SDaniel Eischen 		}
134804215ed2SRandall Stewart 		if (error)
134904215ed2SRandall Stewart 			return (error);
13500895aec3SBjoern A. Zeeb 	}
1351fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1352fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
13535200e00eSIan Dowse 	if (oinp != NULL) {
13545200e00eSIan Dowse 		if (oinpp != NULL)
13555200e00eSIan Dowse 			*oinpp = oinp;
1356df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1357c3229e05SDavid Greenman 	}
13585200e00eSIan Dowse 	if (lport == 0) {
1359b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1360b0330ed9SPawel Jakub Dawidek 		    cred);
13615a903f8dSPierre Beyssac 		if (error)
13625a903f8dSPierre Beyssac 			return (error);
13635a903f8dSPierre Beyssac 	}
13645200e00eSIan Dowse 	*laddrp = laddr.s_addr;
13655200e00eSIan Dowse 	*lportp = lport;
13665200e00eSIan Dowse 	*faddrp = faddr.s_addr;
13675200e00eSIan Dowse 	*fportp = fport;
1368df8bae1dSRodney W. Grimes 	return (0);
1369df8bae1dSRodney W. Grimes }
1370df8bae1dSRodney W. Grimes 
137126f9a767SRodney W. Grimes void
1372136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1373df8bae1dSRodney W. Grimes {
13746b348152SRobert Watson 
13758501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1376fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1377df8bae1dSRodney W. Grimes 
1378df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1379df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
138015bd2b43SDavid Greenman 	in_pcbrehash(inp);
1381df8bae1dSRodney W. Grimes }
138283e521ecSBjoern A. Zeeb #endif /* INET */
1383df8bae1dSRodney W. Grimes 
13844c7c478dSRobert Watson /*
138528696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1386c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
138728696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
138828696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
13894c7c478dSRobert Watson  */
139026f9a767SRodney W. Grimes void
1391136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1392df8bae1dSRodney W. Grimes {
13934c7c478dSRobert Watson 
1394a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1395c0a211c5SRobert Watson 
1396f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1397f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag != NULL)
1398f3e7afe2SHans Petter Selasky 		in_pcbdetach_txrtlmt(inp);
1399f3e7afe2SHans Petter Selasky #endif
14004c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
14014c7c478dSRobert Watson 	inp->inp_socket = NULL;
14024c7c478dSRobert Watson }
14034c7c478dSRobert Watson 
1404c0a211c5SRobert Watson /*
140579bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
140679bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
140779bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
140852cd27cbSRobert Watson  * but where the inpcb lock may already held, or when acquiring a reference
140952cd27cbSRobert Watson  * via a pcbgroup.
141079bdc6e5SRobert Watson  *
141179bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
141279bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
141379bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
141479bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
141579bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
141679bdc6e5SRobert Watson  * inpcb.
141779bdc6e5SRobert Watson  *
141879bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
141979bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
142079bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
142179bdc6e5SRobert Watson  * using in_pcbrele().
1422c0a211c5SRobert Watson  */
142379bdc6e5SRobert Watson void
142479bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
14254c7c478dSRobert Watson {
1426df8bae1dSRodney W. Grimes 
142779bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
142879bdc6e5SRobert Watson 
142979bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
143079bdc6e5SRobert Watson }
143179bdc6e5SRobert Watson 
143279bdc6e5SRobert Watson /*
143379bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
143479bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
143579bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
143679bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
143779bdc6e5SRobert Watson  *
143879bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
143979bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
144079bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
144179bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
144279bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
144379bdc6e5SRobert Watson  */
144479bdc6e5SRobert Watson int
144579bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
144679bdc6e5SRobert Watson {
144779bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
144879bdc6e5SRobert Watson 
144979bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
145079bdc6e5SRobert Watson 
145179bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
145279bdc6e5SRobert Watson 
1453df4e91d3SGleb Smirnoff 	if (refcount_release(&inp->inp_refcount) == 0) {
1454df4e91d3SGleb Smirnoff 		/*
1455df4e91d3SGleb Smirnoff 		 * If the inpcb has been freed, let the caller know, even if
1456df4e91d3SGleb Smirnoff 		 * this isn't the last reference.
1457df4e91d3SGleb Smirnoff 		 */
1458df4e91d3SGleb Smirnoff 		if (inp->inp_flags2 & INP_FREED) {
1459df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1460df4e91d3SGleb Smirnoff 			return (1);
1461df4e91d3SGleb Smirnoff 		}
146279bdc6e5SRobert Watson 		return (0);
1463df4e91d3SGleb Smirnoff 	}
146479bdc6e5SRobert Watson 
146579bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
14663ee9c3c4SRandall Stewart #ifdef TCPHPTS
14673ee9c3c4SRandall Stewart 	if (inp->inp_in_hpts || inp->inp_in_input) {
14683ee9c3c4SRandall Stewart 		struct tcp_hpts_entry *hpts;
14693ee9c3c4SRandall Stewart 		/*
14703ee9c3c4SRandall Stewart 		 * We should not be on the hpts at
14713ee9c3c4SRandall Stewart 		 * this point in any form. we must
14723ee9c3c4SRandall Stewart 		 * get the lock to be sure.
14733ee9c3c4SRandall Stewart 		 */
14743ee9c3c4SRandall Stewart 		hpts = tcp_hpts_lock(inp);
14753ee9c3c4SRandall Stewart 		if (inp->inp_in_hpts)
14763ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on hpts",
14773ee9c3c4SRandall Stewart 			      hpts, inp);
14783ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
14793ee9c3c4SRandall Stewart 		hpts = tcp_input_lock(inp);
14803ee9c3c4SRandall Stewart 		if (inp->inp_in_input)
14813ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on input hpts",
14823ee9c3c4SRandall Stewart 			      hpts, inp);
14833ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
14843ee9c3c4SRandall Stewart 	}
14853ee9c3c4SRandall Stewart #endif
148679bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
148779bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
148879bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
148979bdc6e5SRobert Watson 	return (1);
149079bdc6e5SRobert Watson }
149179bdc6e5SRobert Watson 
149279bdc6e5SRobert Watson int
149379bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
149479bdc6e5SRobert Watson {
149579bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
149679bdc6e5SRobert Watson 
149779bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
149879bdc6e5SRobert Watson 
149979bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
150079bdc6e5SRobert Watson 
1501edd0e0b0SFabien Thomas 	if (refcount_release(&inp->inp_refcount) == 0) {
1502edd0e0b0SFabien Thomas 		/*
1503edd0e0b0SFabien Thomas 		 * If the inpcb has been freed, let the caller know, even if
1504edd0e0b0SFabien Thomas 		 * this isn't the last reference.
1505edd0e0b0SFabien Thomas 		 */
1506edd0e0b0SFabien Thomas 		if (inp->inp_flags2 & INP_FREED) {
1507edd0e0b0SFabien Thomas 			INP_WUNLOCK(inp);
1508edd0e0b0SFabien Thomas 			return (1);
1509edd0e0b0SFabien Thomas 		}
151079bdc6e5SRobert Watson 		return (0);
1511edd0e0b0SFabien Thomas 	}
151279bdc6e5SRobert Watson 
151379bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
15143ee9c3c4SRandall Stewart #ifdef TCPHPTS
15153ee9c3c4SRandall Stewart 	if (inp->inp_in_hpts || inp->inp_in_input) {
15163ee9c3c4SRandall Stewart 		struct tcp_hpts_entry *hpts;
15173ee9c3c4SRandall Stewart 		/*
15183ee9c3c4SRandall Stewart 		 * We should not be on the hpts at
15193ee9c3c4SRandall Stewart 		 * this point in any form. we must
15203ee9c3c4SRandall Stewart 		 * get the lock to be sure.
15213ee9c3c4SRandall Stewart 		 */
15223ee9c3c4SRandall Stewart 		hpts = tcp_hpts_lock(inp);
15233ee9c3c4SRandall Stewart 		if (inp->inp_in_hpts)
15243ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on hpts",
15253ee9c3c4SRandall Stewart 			      hpts, inp);
15263ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
15273ee9c3c4SRandall Stewart 		hpts = tcp_input_lock(inp);
15283ee9c3c4SRandall Stewart 		if (inp->inp_in_input)
15293ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on input hpts",
15303ee9c3c4SRandall Stewart 			      hpts, inp);
15313ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
15323ee9c3c4SRandall Stewart 	}
15333ee9c3c4SRandall Stewart #endif
153479bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
153579bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
153679bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
153779bdc6e5SRobert Watson 	return (1);
153879bdc6e5SRobert Watson }
153979bdc6e5SRobert Watson 
154079bdc6e5SRobert Watson /*
154179bdc6e5SRobert Watson  * Temporary wrapper.
154279bdc6e5SRobert Watson  */
154379bdc6e5SRobert Watson int
154479bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
154579bdc6e5SRobert Watson {
154679bdc6e5SRobert Watson 
154779bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
154879bdc6e5SRobert Watson }
154979bdc6e5SRobert Watson 
1550ddece765SMatt Macy void
1551ddece765SMatt Macy in_pcblist_rele_rlocked(epoch_context_t ctx)
1552ddece765SMatt Macy {
1553ddece765SMatt Macy 	struct in_pcblist *il;
1554ddece765SMatt Macy 	struct inpcb *inp;
1555ddece765SMatt Macy 	struct inpcbinfo *pcbinfo;
1556ddece765SMatt Macy 	int i, n;
1557ddece765SMatt Macy 
1558ddece765SMatt Macy 	il = __containerof(ctx, struct in_pcblist, il_epoch_ctx);
1559ddece765SMatt Macy 	pcbinfo = il->il_pcbinfo;
1560ddece765SMatt Macy 	n = il->il_count;
1561ddece765SMatt Macy 	INP_INFO_WLOCK(pcbinfo);
1562ddece765SMatt Macy 	for (i = 0; i < n; i++) {
1563ddece765SMatt Macy 		inp = il->il_inp_list[i];
1564ddece765SMatt Macy 		INP_RLOCK(inp);
1565ddece765SMatt Macy 		if (!in_pcbrele_rlocked(inp))
1566ddece765SMatt Macy 			INP_RUNLOCK(inp);
1567ddece765SMatt Macy 	}
1568ddece765SMatt Macy 	INP_INFO_WUNLOCK(pcbinfo);
1569ddece765SMatt Macy 	free(il, M_TEMP);
1570ddece765SMatt Macy }
1571ddece765SMatt Macy 
1572addf2b20SMatt Macy static void
1573f09ee4fcSMatt Macy inpcbport_free(epoch_context_t ctx)
1574f09ee4fcSMatt Macy {
1575f09ee4fcSMatt Macy 	struct inpcbport *phd;
1576f09ee4fcSMatt Macy 
1577f09ee4fcSMatt Macy 	phd = __containerof(ctx, struct inpcbport, phd_epoch_ctx);
1578f09ee4fcSMatt Macy 	free(phd, M_PCB);
1579f09ee4fcSMatt Macy }
1580f09ee4fcSMatt Macy 
1581f09ee4fcSMatt Macy static void
1582addf2b20SMatt Macy in_pcbfree_deferred(epoch_context_t ctx)
1583addf2b20SMatt Macy {
1584addf2b20SMatt Macy 	struct inpcb *inp;
1585addf2b20SMatt Macy 	int released __unused;
1586addf2b20SMatt Macy 
1587addf2b20SMatt Macy 	inp = __containerof(ctx, struct inpcb, inp_epoch_ctx);
1588addf2b20SMatt Macy 
1589addf2b20SMatt Macy 	INP_WLOCK(inp);
1590c7ee62fcSAndrey V. Elsukov 	CURVNET_SET(inp->inp_vnet);
1591addf2b20SMatt Macy #ifdef INET
1592f9be0386SMatt Macy 	struct ip_moptions *imo = inp->inp_moptions;
1593addf2b20SMatt Macy 	inp->inp_moptions = NULL;
1594addf2b20SMatt Macy #endif
1595addf2b20SMatt Macy 	/* XXXRW: Do as much as possible here. */
1596addf2b20SMatt Macy #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1597addf2b20SMatt Macy 	if (inp->inp_sp != NULL)
1598addf2b20SMatt Macy 		ipsec_delete_pcbpolicy(inp);
1599addf2b20SMatt Macy #endif
1600addf2b20SMatt Macy #ifdef INET6
1601f9be0386SMatt Macy 	struct ip6_moptions *im6o = NULL;
1602addf2b20SMatt Macy 	if (inp->inp_vflag & INP_IPV6PROTO) {
1603addf2b20SMatt Macy 		ip6_freepcbopts(inp->in6p_outputopts);
1604f9be0386SMatt Macy 		im6o = inp->in6p_moptions;
1605addf2b20SMatt Macy 		inp->in6p_moptions = NULL;
1606addf2b20SMatt Macy 	}
1607addf2b20SMatt Macy #endif
1608addf2b20SMatt Macy 	if (inp->inp_options)
1609addf2b20SMatt Macy 		(void)m_free(inp->inp_options);
1610addf2b20SMatt Macy 	inp->inp_vflag = 0;
1611addf2b20SMatt Macy 	crfree(inp->inp_cred);
1612addf2b20SMatt Macy #ifdef MAC
1613addf2b20SMatt Macy 	mac_inpcb_destroy(inp);
1614addf2b20SMatt Macy #endif
1615addf2b20SMatt Macy 	released = in_pcbrele_wlocked(inp);
1616addf2b20SMatt Macy 	MPASS(released);
1617f9be0386SMatt Macy #ifdef INET6
1618f9be0386SMatt Macy 	ip6_freemoptions(im6o);
1619f9be0386SMatt Macy #endif
1620f9be0386SMatt Macy #ifdef INET
1621f9be0386SMatt Macy 	inp_freemoptions(imo);
1622f9be0386SMatt Macy #endif
1623c7ee62fcSAndrey V. Elsukov 	CURVNET_RESTORE();
1624addf2b20SMatt Macy }
1625addf2b20SMatt Macy 
162679bdc6e5SRobert Watson /*
162779bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
162879bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
162979bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
163079bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
163179bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
163279bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
163379bdc6e5SRobert Watson  * the pcbinfo lock is held.
163479bdc6e5SRobert Watson  */
163579bdc6e5SRobert Watson void
163679bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
163779bdc6e5SRobert Watson {
1638f42a83f2SMatt Macy 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1639f42a83f2SMatt Macy 
164079bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
164145a48d07SJonathan T. Looney 	KASSERT((inp->inp_flags2 & INP_FREED) == 0,
164245a48d07SJonathan T. Looney 	    ("%s: called twice for pcb %p", __func__, inp));
164345a48d07SJonathan T. Looney 	if (inp->inp_flags2 & INP_FREED) {
164445a48d07SJonathan T. Looney 		INP_WUNLOCK(inp);
164545a48d07SJonathan T. Looney 		return;
164645a48d07SJonathan T. Looney 	}
164745a48d07SJonathan T. Looney 
164879bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
1649f42a83f2SMatt Macy 	INP_LIST_WLOCK(pcbinfo);
1650f42a83f2SMatt Macy 	in_pcbremlists(inp);
1651f42a83f2SMatt Macy 	INP_LIST_WUNLOCK(pcbinfo);
1652fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
1653addf2b20SMatt Macy 	/* mark as destruction in progress */
1654f42a83f2SMatt Macy 	inp->inp_flags2 |= INP_FREED;
1655cb6bb230SMatt Macy 	INP_WUNLOCK(inp);
16562a4bd982SGleb Smirnoff 	NET_EPOCH_CALL(in_pcbfree_deferred, &inp->inp_epoch_ctx);
165728696211SRobert Watson }
165828696211SRobert Watson 
165928696211SRobert Watson /*
1660c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1661c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1662c0a211c5SRobert Watson  *
1663c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1664c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1665c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1666c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1667c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1668c0a211c5SRobert Watson  * in_pcbdetach().
1669c0a211c5SRobert Watson  *
1670c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1671c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
167210702a28SRobert Watson  */
167310702a28SRobert Watson void
167410702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
167510702a28SRobert Watson {
167610702a28SRobert Watson 
16778501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
16786573d758SMatt Macy #ifdef INVARIANTS
16796573d758SMatt Macy 	if (inp->inp_socket != NULL && inp->inp_ppcb != NULL)
16806573d758SMatt Macy 		MPASS(inp->inp_refcount > 1);
16816573d758SMatt Macy #endif
168210702a28SRobert Watson 
1683fa046d87SRobert Watson 	/*
1684fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1685fa046d87SRobert Watson 	 * the hash lock...?
1686fa046d87SRobert Watson 	 */
1687ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1688111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
168910702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
169010702a28SRobert Watson 
1691fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
16921a43cff9SSean Bruno 		in_pcbremlbgrouphash(inp);
1693b872626dSMatt Macy 		CK_LIST_REMOVE(inp, inp_hash);
1694b872626dSMatt Macy 		CK_LIST_REMOVE(inp, inp_portlist);
1695b872626dSMatt Macy 		if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) {
1696b872626dSMatt Macy 			CK_LIST_REMOVE(phd, phd_hash);
16972a4bd982SGleb Smirnoff 			NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx);
169810702a28SRobert Watson 		}
1699fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1700111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
170152cd27cbSRobert Watson #ifdef PCBGROUP
170252cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
170352cd27cbSRobert Watson #endif
170410702a28SRobert Watson 	}
170510702a28SRobert Watson }
170610702a28SRobert Watson 
170767107f45SBjoern A. Zeeb #ifdef INET
170854d642bbSRobert Watson /*
170954d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
171054d642bbSRobert Watson  */
171126ef6ac4SDon Lewis struct sockaddr *
1712136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
171326ef6ac4SDon Lewis {
171426ef6ac4SDon Lewis 	struct sockaddr_in *sin;
171526ef6ac4SDon Lewis 
17161ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1717a163d034SWarner Losh 		M_WAITOK | M_ZERO);
171826ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
171926ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
172026ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
172126ef6ac4SDon Lewis 	sin->sin_port = port;
172226ef6ac4SDon Lewis 
172326ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
172426ef6ac4SDon Lewis }
172526ef6ac4SDon Lewis 
1726117bcae7SGarrett Wollman int
172754d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1728df8bae1dSRodney W. Grimes {
1729136d4f1cSRobert Watson 	struct inpcb *inp;
173026ef6ac4SDon Lewis 	struct in_addr addr;
173126ef6ac4SDon Lewis 	in_port_t port;
173242fa505bSDavid Greenman 
1733fdc984f7STor Egge 	inp = sotoinpcb(so);
173454d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
17356466b28aSRobert Watson 
1736a69042a5SRobert Watson 	INP_RLOCK(inp);
173726ef6ac4SDon Lewis 	port = inp->inp_lport;
173826ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1739a69042a5SRobert Watson 	INP_RUNLOCK(inp);
174042fa505bSDavid Greenman 
174126ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1742117bcae7SGarrett Wollman 	return 0;
1743df8bae1dSRodney W. Grimes }
1744df8bae1dSRodney W. Grimes 
1745117bcae7SGarrett Wollman int
174654d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1747df8bae1dSRodney W. Grimes {
1748136d4f1cSRobert Watson 	struct inpcb *inp;
174926ef6ac4SDon Lewis 	struct in_addr addr;
175026ef6ac4SDon Lewis 	in_port_t port;
175142fa505bSDavid Greenman 
1752fdc984f7STor Egge 	inp = sotoinpcb(so);
175354d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
17546466b28aSRobert Watson 
1755a69042a5SRobert Watson 	INP_RLOCK(inp);
175626ef6ac4SDon Lewis 	port = inp->inp_fport;
175726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1758a69042a5SRobert Watson 	INP_RUNLOCK(inp);
175942fa505bSDavid Greenman 
176026ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1761117bcae7SGarrett Wollman 	return 0;
1762df8bae1dSRodney W. Grimes }
1763df8bae1dSRodney W. Grimes 
176426f9a767SRodney W. Grimes void
1765136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1766136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1767d1c54148SJesper Skriver {
1768f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1769d1c54148SJesper Skriver 
17703dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1771b872626dSMatt Macy 	CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
17728501a69cSRobert Watson 		INP_WLOCK(inp);
1773d1c54148SJesper Skriver #ifdef INET6
1774f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
17758501a69cSRobert Watson 			INP_WUNLOCK(inp);
1776d1c54148SJesper Skriver 			continue;
1777f76fcf6dSJeffrey Hsu 		}
1778d1c54148SJesper Skriver #endif
1779d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1780f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
17818501a69cSRobert Watson 			INP_WUNLOCK(inp);
1782d1c54148SJesper Skriver 			continue;
1783d1c54148SJesper Skriver 		}
17843dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
17858501a69cSRobert Watson 			INP_WUNLOCK(inp);
1786f76fcf6dSJeffrey Hsu 	}
17873dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1788d1c54148SJesper Skriver }
1789d1c54148SJesper Skriver 
1790e43cc4aeSHajimu UMEMOTO void
1791136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1792e43cc4aeSHajimu UMEMOTO {
1793e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
179459854ecfSHans Petter Selasky 	struct in_multi *inm;
179559854ecfSHans Petter Selasky 	struct in_mfilter *imf;
1796e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1797e43cc4aeSHajimu UMEMOTO 
1798ff9b006dSJulien Charbon 	INP_INFO_WLOCK(pcbinfo);
1799b872626dSMatt Macy 	CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
18008501a69cSRobert Watson 		INP_WLOCK(inp);
1801e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1802e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1803e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1804e43cc4aeSHajimu UMEMOTO 			/*
1805e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1806e43cc4aeSHajimu UMEMOTO 			 * detached.
1807e43cc4aeSHajimu UMEMOTO 			 */
1808e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1809e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1810e43cc4aeSHajimu UMEMOTO 
1811e43cc4aeSHajimu UMEMOTO 			/*
1812e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1813e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1814cb6bb230SMatt Macy 			 *
1815cb6bb230SMatt Macy 			 * XXX This can all be deferred to an epoch_call
1816e43cc4aeSHajimu UMEMOTO 			 */
181759854ecfSHans Petter Selasky restart:
181859854ecfSHans Petter Selasky 			IP_MFILTER_FOREACH(imf, &imo->imo_head) {
181959854ecfSHans Petter Selasky 				if ((inm = imf->imf_inm) == NULL)
182059854ecfSHans Petter Selasky 					continue;
182159854ecfSHans Petter Selasky 				if (inm->inm_ifp != ifp)
182259854ecfSHans Petter Selasky 					continue;
182359854ecfSHans Petter Selasky 				ip_mfilter_remove(&imo->imo_head, imf);
1824f3e1324bSStephen Hurd 				IN_MULTI_LOCK_ASSERT();
182559854ecfSHans Petter Selasky 				in_leavegroup_locked(inm, NULL);
182659854ecfSHans Petter Selasky 				ip_mfilter_free(imf);
182759854ecfSHans Petter Selasky 				goto restart;
1828e43cc4aeSHajimu UMEMOTO 			}
1829e43cc4aeSHajimu UMEMOTO 		}
18308501a69cSRobert Watson 		INP_WUNLOCK(inp);
1831e43cc4aeSHajimu UMEMOTO 	}
1832ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(pcbinfo);
1833e43cc4aeSHajimu UMEMOTO }
1834e43cc4aeSHajimu UMEMOTO 
1835df8bae1dSRodney W. Grimes /*
1836fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1837fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1838c3229e05SDavid Greenman  */
1839d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1840df8bae1dSRodney W. Grimes struct inpcb *
1841136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
184268e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1843df8bae1dSRodney W. Grimes {
1844136d4f1cSRobert Watson 	struct inpcb *inp;
1845d5e8a67eSHajimu UMEMOTO #ifdef INET6
1846d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1847d5e8a67eSHajimu UMEMOTO #else
1848d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1849d5e8a67eSHajimu UMEMOTO #endif
1850d5e8a67eSHajimu UMEMOTO 	int wildcard;
18517bc4aca7SDavid Greenman 
185268e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
185368e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
185468e0d7e0SRobert Watson 
1855fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
18561b73ca0bSSam Leffler 
185768e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1858c3229e05SDavid Greenman 		struct inpcbhead *head;
1859c3229e05SDavid Greenman 		/*
1860c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1861c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1862c3229e05SDavid Greenman 		 */
1863712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1864712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1865b872626dSMatt Macy 		CK_LIST_FOREACH(inp, head, inp_hash) {
1866cfa1ca9dSYoshinobu Inoue #ifdef INET6
1867413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1868369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1869cfa1ca9dSYoshinobu Inoue 				continue;
1870cfa1ca9dSYoshinobu Inoue #endif
1871c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1872c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1873c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1874c3229e05SDavid Greenman 				/*
1875413628a7SBjoern A. Zeeb 				 * Found?
1876c3229e05SDavid Greenman 				 */
1877413628a7SBjoern A. Zeeb 				if (cred == NULL ||
18780304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
18790304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1880c3229e05SDavid Greenman 					return (inp);
1881df8bae1dSRodney W. Grimes 			}
1882c3229e05SDavid Greenman 		}
1883c3229e05SDavid Greenman 		/*
1884c3229e05SDavid Greenman 		 * Not found.
1885c3229e05SDavid Greenman 		 */
1886c3229e05SDavid Greenman 		return (NULL);
1887c3229e05SDavid Greenman 	} else {
1888c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1889c3229e05SDavid Greenman 		struct inpcbport *phd;
1890c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1891c3229e05SDavid Greenman 		/*
1892c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1893c3229e05SDavid Greenman 		 *
1894c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1895c3229e05SDavid Greenman 		 * port hash list.
1896c3229e05SDavid Greenman 		 */
1897712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1898712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1899b872626dSMatt Macy 		CK_LIST_FOREACH(phd, porthash, phd_hash) {
1900c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1901c3229e05SDavid Greenman 				break;
1902c3229e05SDavid Greenman 		}
1903c3229e05SDavid Greenman 		if (phd != NULL) {
1904c3229e05SDavid Greenman 			/*
1905c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1906c3229e05SDavid Greenman 			 * fit.
1907c3229e05SDavid Greenman 			 */
1908b872626dSMatt Macy 			CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1909c3229e05SDavid Greenman 				wildcard = 0;
1910413628a7SBjoern A. Zeeb 				if (cred != NULL &&
19110304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
19120304c731SJamie Gritton 					cred->cr_prison))
1913413628a7SBjoern A. Zeeb 					continue;
1914cfa1ca9dSYoshinobu Inoue #ifdef INET6
1915413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1916369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1917cfa1ca9dSYoshinobu Inoue 					continue;
1918d5e8a67eSHajimu UMEMOTO 				/*
1919d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1920d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1921d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1922d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1923d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1924d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1925d5e8a67eSHajimu UMEMOTO 				 *
1926d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1927d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1928d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1929d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1930d5e8a67eSHajimu UMEMOTO 				 */
1931d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1932d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1933cfa1ca9dSYoshinobu Inoue #endif
1934c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1935c3229e05SDavid Greenman 					wildcard++;
193615bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
193715bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
193815bd2b43SDavid Greenman 						wildcard++;
193915bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
194015bd2b43SDavid Greenman 						continue;
194115bd2b43SDavid Greenman 				} else {
194215bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
194315bd2b43SDavid Greenman 						wildcard++;
194415bd2b43SDavid Greenman 				}
1945df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1946df8bae1dSRodney W. Grimes 					match = inp;
1947df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1948413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1949df8bae1dSRodney W. Grimes 						break;
1950df8bae1dSRodney W. Grimes 				}
1951df8bae1dSRodney W. Grimes 			}
19523dbdc25cSDavid Greenman 		}
1953df8bae1dSRodney W. Grimes 		return (match);
1954df8bae1dSRodney W. Grimes 	}
1955c3229e05SDavid Greenman }
1956d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
195715bd2b43SDavid Greenman 
19581a43cff9SSean Bruno static struct inpcb *
19591a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
19601a43cff9SSean Bruno     const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr,
19611a43cff9SSean Bruno     uint16_t fport, int lookupflags)
19621a43cff9SSean Bruno {
19638be02ee4SMark Johnston 	struct inpcb *local_wild;
19641a43cff9SSean Bruno 	const struct inpcblbgrouphead *hdr;
19651a43cff9SSean Bruno 	struct inpcblbgroup *grp;
19668be02ee4SMark Johnston 	uint32_t idx;
19671a43cff9SSean Bruno 
19681a43cff9SSean Bruno 	INP_HASH_LOCK_ASSERT(pcbinfo);
19691a43cff9SSean Bruno 
19709d2877fcSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[
19719d2877fcSMark Johnston 	    INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
19721a43cff9SSean Bruno 
19731a43cff9SSean Bruno 	/*
19741a43cff9SSean Bruno 	 * Order of socket selection:
19751a43cff9SSean Bruno 	 * 1. non-wild.
19761a43cff9SSean Bruno 	 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
19771a43cff9SSean Bruno 	 *
19781a43cff9SSean Bruno 	 * NOTE:
19791a43cff9SSean Bruno 	 * - Load balanced group does not contain jailed sockets
19801a43cff9SSean Bruno 	 * - Load balanced group does not contain IPv4 mapped INET6 wild sockets
19811a43cff9SSean Bruno 	 */
19828be02ee4SMark Johnston 	local_wild = NULL;
198354af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
19841a43cff9SSean Bruno #ifdef INET6
19851a43cff9SSean Bruno 		if (!(grp->il_vflag & INP_IPV4))
19861a43cff9SSean Bruno 			continue;
19871a43cff9SSean Bruno #endif
19888be02ee4SMark Johnston 		if (grp->il_lport != lport)
19898be02ee4SMark Johnston 			continue;
19901a43cff9SSean Bruno 
19918be02ee4SMark Johnston 		idx = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, lport, fport) %
19928be02ee4SMark Johnston 		    grp->il_inpcnt;
19938be02ee4SMark Johnston 		if (grp->il_laddr.s_addr == laddr->s_addr)
19941a43cff9SSean Bruno 			return (grp->il_inp[idx]);
19951a43cff9SSean Bruno 		if (grp->il_laddr.s_addr == INADDR_ANY &&
19968be02ee4SMark Johnston 		    (lookupflags & INPLOOKUP_WILDCARD) != 0)
19971a43cff9SSean Bruno 			local_wild = grp->il_inp[idx];
19981a43cff9SSean Bruno 	}
19991a43cff9SSean Bruno 	return (local_wild);
20001a43cff9SSean Bruno }
20011a43cff9SSean Bruno 
200252cd27cbSRobert Watson #ifdef PCBGROUP
200352cd27cbSRobert Watson /*
200452cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
200552cd27cbSRobert Watson  */
200652cd27cbSRobert Watson static struct inpcb *
200752cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
200852cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
200952cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
201052cd27cbSRobert Watson {
201152cd27cbSRobert Watson 	struct inpcbhead *head;
201252cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
201352cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
20147fb2986fSJonathan T. Looney 	bool locked;
201552cd27cbSRobert Watson 
201652cd27cbSRobert Watson 	/*
201752cd27cbSRobert Watson 	 * First look for an exact match.
201852cd27cbSRobert Watson 	 */
201952cd27cbSRobert Watson 	tmpinp = NULL;
202052cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
202152cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
202252cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
2023feeef850SMatt Macy 	CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
202452cd27cbSRobert Watson #ifdef INET6
202552cd27cbSRobert Watson 		/* XXX inp locking */
202652cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
202752cd27cbSRobert Watson 			continue;
202852cd27cbSRobert Watson #endif
202952cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
203052cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
203152cd27cbSRobert Watson 		    inp->inp_fport == fport &&
203252cd27cbSRobert Watson 		    inp->inp_lport == lport) {
203352cd27cbSRobert Watson 			/*
203452cd27cbSRobert Watson 			 * XXX We should be able to directly return
203552cd27cbSRobert Watson 			 * the inp here, without any checks.
203652cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
203752cd27cbSRobert Watson 			 */
203852cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
203952cd27cbSRobert Watson 				goto found;
204052cd27cbSRobert Watson 			if (tmpinp == NULL)
204152cd27cbSRobert Watson 				tmpinp = inp;
204252cd27cbSRobert Watson 		}
204352cd27cbSRobert Watson 	}
204452cd27cbSRobert Watson 	if (tmpinp != NULL) {
204552cd27cbSRobert Watson 		inp = tmpinp;
204652cd27cbSRobert Watson 		goto found;
204752cd27cbSRobert Watson 	}
204852cd27cbSRobert Watson 
20490a100a6fSAdrian Chadd #ifdef	RSS
20500a100a6fSAdrian Chadd 	/*
20510a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
20520a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
20530a100a6fSAdrian Chadd 	 */
20540a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
20550a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
20560a100a6fSAdrian Chadd #ifdef INET6
20570a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
20580a100a6fSAdrian Chadd #endif
20590a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
20600a100a6fSAdrian Chadd 		struct inpcbhead *head;
20610a100a6fSAdrian Chadd 		int injail;
20620a100a6fSAdrian Chadd 
20630a100a6fSAdrian Chadd 		/*
20640a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
20650a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
20660a100a6fSAdrian Chadd 		 *      2. jailed, wild.
20670a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
20680a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
20690a100a6fSAdrian Chadd 		 */
20700a100a6fSAdrian Chadd 
20710a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
20720a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
2073feeef850SMatt Macy 		CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
20740a100a6fSAdrian Chadd #ifdef INET6
20750a100a6fSAdrian Chadd 			/* XXX inp locking */
20760a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
20770a100a6fSAdrian Chadd 				continue;
20780a100a6fSAdrian Chadd #endif
20790a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
20800a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
20810a100a6fSAdrian Chadd 				continue;
20820a100a6fSAdrian Chadd 
20830a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
20840a100a6fSAdrian Chadd 			if (injail) {
20850a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
20860a100a6fSAdrian Chadd 				    &laddr) != 0)
20870a100a6fSAdrian Chadd 					continue;
20880a100a6fSAdrian Chadd 			} else {
20890a100a6fSAdrian Chadd 				if (local_exact != NULL)
20900a100a6fSAdrian Chadd 					continue;
20910a100a6fSAdrian Chadd 			}
20920a100a6fSAdrian Chadd 
20930a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
20940a100a6fSAdrian Chadd 				if (injail)
20950a100a6fSAdrian Chadd 					goto found;
20960a100a6fSAdrian Chadd 				else
20970a100a6fSAdrian Chadd 					local_exact = inp;
20980a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
20990a100a6fSAdrian Chadd #ifdef INET6
21000a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
21010a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
21020a100a6fSAdrian Chadd 					local_wild_mapped = inp;
21030a100a6fSAdrian Chadd 				else
21040a100a6fSAdrian Chadd #endif
21050a100a6fSAdrian Chadd 					if (injail)
21060a100a6fSAdrian Chadd 						jail_wild = inp;
21070a100a6fSAdrian Chadd 					else
21080a100a6fSAdrian Chadd 						local_wild = inp;
21090a100a6fSAdrian Chadd 			}
21100a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
21110a100a6fSAdrian Chadd 
21120a100a6fSAdrian Chadd 		inp = jail_wild;
21130a100a6fSAdrian Chadd 		if (inp == NULL)
21140a100a6fSAdrian Chadd 			inp = local_exact;
21150a100a6fSAdrian Chadd 		if (inp == NULL)
21160a100a6fSAdrian Chadd 			inp = local_wild;
21170a100a6fSAdrian Chadd #ifdef INET6
21180a100a6fSAdrian Chadd 		if (inp == NULL)
21190a100a6fSAdrian Chadd 			inp = local_wild_mapped;
21200a100a6fSAdrian Chadd #endif
21210a100a6fSAdrian Chadd 		if (inp != NULL)
21220a100a6fSAdrian Chadd 			goto found;
21230a100a6fSAdrian Chadd 	}
21240a100a6fSAdrian Chadd #endif
21250a100a6fSAdrian Chadd 
212652cd27cbSRobert Watson 	/*
212752cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
212852cd27cbSRobert Watson 	 */
212952cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
213052cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
213152cd27cbSRobert Watson #ifdef INET6
213252cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
213352cd27cbSRobert Watson #endif
213452cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
213552cd27cbSRobert Watson 		struct inpcbhead *head;
213652cd27cbSRobert Watson 		int injail;
213752cd27cbSRobert Watson 
213852cd27cbSRobert Watson 		/*
213952cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
214052cd27cbSRobert Watson 		 *      1. jailed, non-wild.
214152cd27cbSRobert Watson 		 *      2. jailed, wild.
214252cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
214352cd27cbSRobert Watson 		 *      4. non-jailed, wild.
214452cd27cbSRobert Watson 		 */
214552cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
214652cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
2147feeef850SMatt Macy 		CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
214852cd27cbSRobert Watson #ifdef INET6
214952cd27cbSRobert Watson 			/* XXX inp locking */
215052cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
215152cd27cbSRobert Watson 				continue;
215252cd27cbSRobert Watson #endif
215352cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
215452cd27cbSRobert Watson 			    inp->inp_lport != lport)
215552cd27cbSRobert Watson 				continue;
215652cd27cbSRobert Watson 
215752cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
215852cd27cbSRobert Watson 			if (injail) {
215952cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
216052cd27cbSRobert Watson 				    &laddr) != 0)
216152cd27cbSRobert Watson 					continue;
216252cd27cbSRobert Watson 			} else {
216352cd27cbSRobert Watson 				if (local_exact != NULL)
216452cd27cbSRobert Watson 					continue;
216552cd27cbSRobert Watson 			}
216652cd27cbSRobert Watson 
216752cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
216852cd27cbSRobert Watson 				if (injail)
216952cd27cbSRobert Watson 					goto found;
217052cd27cbSRobert Watson 				else
217152cd27cbSRobert Watson 					local_exact = inp;
217252cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
217352cd27cbSRobert Watson #ifdef INET6
217452cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
217552cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
217652cd27cbSRobert Watson 					local_wild_mapped = inp;
217752cd27cbSRobert Watson 				else
217883e521ecSBjoern A. Zeeb #endif
217952cd27cbSRobert Watson 					if (injail)
218052cd27cbSRobert Watson 						jail_wild = inp;
218152cd27cbSRobert Watson 					else
218252cd27cbSRobert Watson 						local_wild = inp;
218352cd27cbSRobert Watson 			}
218452cd27cbSRobert Watson 		} /* LIST_FOREACH */
218552cd27cbSRobert Watson 		inp = jail_wild;
218652cd27cbSRobert Watson 		if (inp == NULL)
218752cd27cbSRobert Watson 			inp = local_exact;
218852cd27cbSRobert Watson 		if (inp == NULL)
218952cd27cbSRobert Watson 			inp = local_wild;
219052cd27cbSRobert Watson #ifdef INET6
219152cd27cbSRobert Watson 		if (inp == NULL)
219252cd27cbSRobert Watson 			inp = local_wild_mapped;
219383e521ecSBjoern A. Zeeb #endif
219452cd27cbSRobert Watson 		if (inp != NULL)
219552cd27cbSRobert Watson 			goto found;
219652cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
219752cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
219852cd27cbSRobert Watson 	return (NULL);
219952cd27cbSRobert Watson 
220052cd27cbSRobert Watson found:
22017fb2986fSJonathan T. Looney 	if (lookupflags & INPLOOKUP_WLOCKPCB)
2202b9a9e8e9SNavdeep Parhar 		locked = INP_TRY_WLOCK(inp);
22037fb2986fSJonathan T. Looney 	else if (lookupflags & INPLOOKUP_RLOCKPCB)
2204b9a9e8e9SNavdeep Parhar 		locked = INP_TRY_RLOCK(inp);
22057fb2986fSJonathan T. Looney 	else
22067fb2986fSJonathan T. Looney 		panic("%s: locking bug", __func__);
2207483305b9SMatt Macy 	if (__predict_false(locked && (inp->inp_flags2 & INP_FREED))) {
2208483305b9SMatt Macy 		if (lookupflags & INPLOOKUP_WLOCKPCB)
2209483305b9SMatt Macy 			INP_WUNLOCK(inp);
2210483305b9SMatt Macy 		else
2211483305b9SMatt Macy 			INP_RUNLOCK(inp);
2212483305b9SMatt Macy 		return (NULL);
2213483305b9SMatt Macy 	} else if (!locked)
221452cd27cbSRobert Watson 		in_pcbref(inp);
221552cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
22167fb2986fSJonathan T. Looney 	if (!locked) {
221752cd27cbSRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
221852cd27cbSRobert Watson 			INP_WLOCK(inp);
221952cd27cbSRobert Watson 			if (in_pcbrele_wlocked(inp))
222052cd27cbSRobert Watson 				return (NULL);
22217fb2986fSJonathan T. Looney 		} else {
222252cd27cbSRobert Watson 			INP_RLOCK(inp);
222352cd27cbSRobert Watson 			if (in_pcbrele_rlocked(inp))
222452cd27cbSRobert Watson 				return (NULL);
22257fb2986fSJonathan T. Looney 		}
22267fb2986fSJonathan T. Looney 	}
22277fb2986fSJonathan T. Looney #ifdef INVARIANTS
22287fb2986fSJonathan T. Looney 	if (lookupflags & INPLOOKUP_WLOCKPCB)
22297fb2986fSJonathan T. Looney 		INP_WLOCK_ASSERT(inp);
22307fb2986fSJonathan T. Looney 	else
22317fb2986fSJonathan T. Looney 		INP_RLOCK_ASSERT(inp);
22327fb2986fSJonathan T. Looney #endif
223352cd27cbSRobert Watson 	return (inp);
223452cd27cbSRobert Watson }
223552cd27cbSRobert Watson #endif /* PCBGROUP */
223652cd27cbSRobert Watson 
223715bd2b43SDavid Greenman /*
2238fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
2239fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
2240fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
224115bd2b43SDavid Greenman  */
2242fa046d87SRobert Watson static struct inpcb *
2243fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
224468e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
2245136d4f1cSRobert Watson     struct ifnet *ifp)
224615bd2b43SDavid Greenman {
224715bd2b43SDavid Greenman 	struct inpcbhead *head;
2248413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
224915bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
225015bd2b43SDavid Greenman 
225168e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
225268e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2253d797164aSGleb Smirnoff 	INP_HASH_LOCK_ASSERT(pcbinfo);
2254d797164aSGleb Smirnoff 
225515bd2b43SDavid Greenman 	/*
225615bd2b43SDavid Greenman 	 * First look for an exact match.
225715bd2b43SDavid Greenman 	 */
2258413628a7SBjoern A. Zeeb 	tmpinp = NULL;
2259712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
2260712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
2261b872626dSMatt Macy 	CK_LIST_FOREACH(inp, head, inp_hash) {
2262cfa1ca9dSYoshinobu Inoue #ifdef INET6
2263413628a7SBjoern A. Zeeb 		/* XXX inp locking */
2264369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
2265cfa1ca9dSYoshinobu Inoue 			continue;
2266cfa1ca9dSYoshinobu Inoue #endif
22676d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
2268ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
2269ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
2270413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
2271413628a7SBjoern A. Zeeb 			/*
2272413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
2273413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
2274413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
2275413628a7SBjoern A. Zeeb 			 */
22760304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
2277c3229e05SDavid Greenman 				return (inp);
2278413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
2279413628a7SBjoern A. Zeeb 				tmpinp = inp;
2280c3229e05SDavid Greenman 		}
2281413628a7SBjoern A. Zeeb 	}
2282413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
2283413628a7SBjoern A. Zeeb 		return (tmpinp);
2284e3fd5ffdSRobert Watson 
2285e3fd5ffdSRobert Watson 	/*
22861a43cff9SSean Bruno 	 * Then look in lb group (for wildcard match).
22871a43cff9SSean Bruno 	 */
2288d9ff5789SMark Johnston 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
22891a43cff9SSean Bruno 		inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr,
22901a43cff9SSean Bruno 		    fport, lookupflags);
2291d9ff5789SMark Johnston 		if (inp != NULL)
22921a43cff9SSean Bruno 			return (inp);
22931a43cff9SSean Bruno 	}
22941a43cff9SSean Bruno 
22951a43cff9SSean Bruno 	/*
2296e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
2297e3fd5ffdSRobert Watson 	 */
229868e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
2299413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
2300e3fd5ffdSRobert Watson #ifdef INET6
2301cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
2302e3fd5ffdSRobert Watson #endif
2303413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
2304413628a7SBjoern A. Zeeb 		int injail;
2305413628a7SBjoern A. Zeeb 
2306413628a7SBjoern A. Zeeb 		/*
2307413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
2308413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
2309413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
2310413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
2311413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
2312413628a7SBjoern A. Zeeb 		 */
23136d6a026bSDavid Greenman 
2314712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
2315712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
2316b872626dSMatt Macy 		CK_LIST_FOREACH(inp, head, inp_hash) {
2317cfa1ca9dSYoshinobu Inoue #ifdef INET6
2318413628a7SBjoern A. Zeeb 			/* XXX inp locking */
2319369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
2320cfa1ca9dSYoshinobu Inoue 				continue;
2321cfa1ca9dSYoshinobu Inoue #endif
2322413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
2323413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
2324413628a7SBjoern A. Zeeb 				continue;
2325413628a7SBjoern A. Zeeb 
23260304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
2327413628a7SBjoern A. Zeeb 			if (injail) {
2328b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
2329b89e82ddSJamie Gritton 				    &laddr) != 0)
2330413628a7SBjoern A. Zeeb 					continue;
2331413628a7SBjoern A. Zeeb 			} else {
2332413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
2333413628a7SBjoern A. Zeeb 					continue;
2334413628a7SBjoern A. Zeeb 			}
2335413628a7SBjoern A. Zeeb 
2336413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
2337413628a7SBjoern A. Zeeb 				if (injail)
2338c3229e05SDavid Greenman 					return (inp);
2339413628a7SBjoern A. Zeeb 				else
2340413628a7SBjoern A. Zeeb 					local_exact = inp;
2341413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
2342e3fd5ffdSRobert Watson #ifdef INET6
2343413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
23445cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
2345cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
2346cfa1ca9dSYoshinobu Inoue 				else
234783e521ecSBjoern A. Zeeb #endif
2348413628a7SBjoern A. Zeeb 					if (injail)
2349413628a7SBjoern A. Zeeb 						jail_wild = inp;
2350413628a7SBjoern A. Zeeb 					else
23516d6a026bSDavid Greenman 						local_wild = inp;
23526d6a026bSDavid Greenman 			}
2353413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
2354413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
2355413628a7SBjoern A. Zeeb 			return (jail_wild);
2356413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
2357413628a7SBjoern A. Zeeb 			return (local_exact);
2358413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
2359c3229e05SDavid Greenman 			return (local_wild);
2360413628a7SBjoern A. Zeeb #ifdef INET6
2361413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
2362413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
236383e521ecSBjoern A. Zeeb #endif
236468e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
2365413628a7SBjoern A. Zeeb 
23666d6a026bSDavid Greenman 	return (NULL);
236715bd2b43SDavid Greenman }
2368fa046d87SRobert Watson 
2369fa046d87SRobert Watson /*
2370fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
2371fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
2372fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
2373fa046d87SRobert Watson  */
2374fa046d87SRobert Watson static struct inpcb *
2375fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2376fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2377fa046d87SRobert Watson     struct ifnet *ifp)
2378fa046d87SRobert Watson {
2379fa046d87SRobert Watson 	struct inpcb *inp;
2380fa046d87SRobert Watson 
2381fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
2382fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
2383fa046d87SRobert Watson 	if (inp != NULL) {
2384fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
2385fa046d87SRobert Watson 			INP_WLOCK(inp);
23863d348772SMatt Macy 			if (__predict_false(inp->inp_flags2 & INP_FREED)) {
23873d348772SMatt Macy 				INP_WUNLOCK(inp);
23883d348772SMatt Macy 				inp = NULL;
23893d348772SMatt Macy 			}
23903d348772SMatt Macy 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
2391fa046d87SRobert Watson 			INP_RLOCK(inp);
23923d348772SMatt Macy 			if (__predict_false(inp->inp_flags2 & INP_FREED)) {
23933d348772SMatt Macy 				INP_RUNLOCK(inp);
23943d348772SMatt Macy 				inp = NULL;
23957fb2986fSJonathan T. Looney 			}
23963d348772SMatt Macy 		} else
23973d348772SMatt Macy 			panic("%s: locking bug", __func__);
23987fb2986fSJonathan T. Looney #ifdef INVARIANTS
23993d348772SMatt Macy 		if (inp != NULL) {
24007fb2986fSJonathan T. Looney 			if (lookupflags & INPLOOKUP_WLOCKPCB)
24017fb2986fSJonathan T. Looney 				INP_WLOCK_ASSERT(inp);
24027fb2986fSJonathan T. Looney 			else
24037fb2986fSJonathan T. Looney 				INP_RLOCK_ASSERT(inp);
24043d348772SMatt Macy 		}
24057fb2986fSJonathan T. Looney #endif
24063d348772SMatt Macy 	}
2407d797164aSGleb Smirnoff 
2408fa046d87SRobert Watson 	return (inp);
2409fa046d87SRobert Watson }
2410fa046d87SRobert Watson 
2411fa046d87SRobert Watson /*
2412d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
2413d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
241452cd27cbSRobert Watson  *
241552cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
2416fa046d87SRobert Watson  */
2417fa046d87SRobert Watson struct inpcb *
2418fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
2419fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
2420fa046d87SRobert Watson {
24217527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
242252cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
242352cd27cbSRobert Watson #endif
2424fa046d87SRobert Watson 
2425fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2426fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2427fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2428fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2429fa046d87SRobert Watson 
24307527624eSRobert Watson 	/*
24317527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
24327527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
24337527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
24347527624eSRobert Watson 	 * in software.
24357527624eSRobert Watson 	 *
24367527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
24377527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
24387527624eSRobert Watson 	 * in software.
24397527624eSRobert Watson 	 */
24407527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
244152cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
244252cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
244352cd27cbSRobert Watson 		    fport);
244452cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
244552cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
244652cd27cbSRobert Watson 	}
244752cd27cbSRobert Watson #endif
2448fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2449fa046d87SRobert Watson 	    lookupflags, ifp));
2450fa046d87SRobert Watson }
2451d3c1f003SRobert Watson 
2452d3c1f003SRobert Watson struct inpcb *
2453d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2454d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2455d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2456d3c1f003SRobert Watson {
245752cd27cbSRobert Watson #ifdef PCBGROUP
245852cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
245952cd27cbSRobert Watson #endif
2460d3c1f003SRobert Watson 
2461d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2462d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2463d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2464d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2465d3c1f003SRobert Watson 
246652cd27cbSRobert Watson #ifdef PCBGROUP
24677527624eSRobert Watson 	/*
24687527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
24697527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
24707527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
24717527624eSRobert Watson 	 * using RSS.
24727527624eSRobert Watson 	 *
24737527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
24747527624eSRobert Watson 	 */
24757527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
24767527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
247752cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
247852cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
247952cd27cbSRobert Watson 		if (pcbgroup != NULL)
248052cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
248152cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
24827527624eSRobert Watson #ifndef RSS
248352cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
248452cd27cbSRobert Watson 		    fport);
248552cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
248652cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
24877527624eSRobert Watson #endif
248852cd27cbSRobert Watson 	}
248952cd27cbSRobert Watson #endif
2490d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2491d3c1f003SRobert Watson 	    lookupflags, ifp));
2492d3c1f003SRobert Watson }
249367107f45SBjoern A. Zeeb #endif /* INET */
249415bd2b43SDavid Greenman 
24957bc4aca7SDavid Greenman /*
2496c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
24977bc4aca7SDavid Greenman  */
249852cd27cbSRobert Watson static int
2499fe1274eeSMichael Tuexen in_pcbinshash_internal(struct inpcb *inp, struct mbuf *m)
250015bd2b43SDavid Greenman {
2501c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2502c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2503c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2504c3229e05SDavid Greenman 	struct inpcbport *phd;
2505cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
25061a43cff9SSean Bruno 	int so_options;
250715bd2b43SDavid Greenman 
25088501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2509fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2510fa046d87SRobert Watson 
2511111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2512111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2513602cc7f1SRobert Watson 
2514cfa1ca9dSYoshinobu Inoue #ifdef INET6
2515cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
25161b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2517cfa1ca9dSYoshinobu Inoue 	else
251883e521ecSBjoern A. Zeeb #endif
2519cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2520cfa1ca9dSYoshinobu Inoue 
2521712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2522712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
252315bd2b43SDavid Greenman 
2524712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2525712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2526c3229e05SDavid Greenman 
2527c3229e05SDavid Greenman 	/*
25281a43cff9SSean Bruno 	 * Add entry to load balance group.
25291a43cff9SSean Bruno 	 * Only do this if SO_REUSEPORT_LB is set.
25301a43cff9SSean Bruno 	 */
25311a43cff9SSean Bruno 	so_options = inp_so_options(inp);
25321a43cff9SSean Bruno 	if (so_options & SO_REUSEPORT_LB) {
25331a43cff9SSean Bruno 		int ret = in_pcbinslbgrouphash(inp);
25341a43cff9SSean Bruno 		if (ret) {
25351a43cff9SSean Bruno 			/* pcb lb group malloc fail (ret=ENOBUFS). */
25361a43cff9SSean Bruno 			return (ret);
25371a43cff9SSean Bruno 		}
25381a43cff9SSean Bruno 	}
25391a43cff9SSean Bruno 
25401a43cff9SSean Bruno 	/*
2541c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2542c3229e05SDavid Greenman 	 */
2543b872626dSMatt Macy 	CK_LIST_FOREACH(phd, pcbporthash, phd_hash) {
2544c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2545c3229e05SDavid Greenman 			break;
2546c3229e05SDavid Greenman 	}
2547c3229e05SDavid Greenman 	/*
2548c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2549c3229e05SDavid Greenman 	 */
2550c3229e05SDavid Greenman 	if (phd == NULL) {
25511ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2552c3229e05SDavid Greenman 		if (phd == NULL) {
2553c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2554c3229e05SDavid Greenman 		}
2555f09ee4fcSMatt Macy 		bzero(&phd->phd_epoch_ctx, sizeof(struct epoch_context));
2556c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2557b872626dSMatt Macy 		CK_LIST_INIT(&phd->phd_pcblist);
2558b872626dSMatt Macy 		CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2559c3229e05SDavid Greenman 	}
2560c3229e05SDavid Greenman 	inp->inp_phd = phd;
2561b872626dSMatt Macy 	CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2562b872626dSMatt Macy 	CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2563111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
256452cd27cbSRobert Watson #ifdef PCBGROUP
2565fe1274eeSMichael Tuexen 	if (m != NULL) {
2566fe1274eeSMichael Tuexen 		in_pcbgroup_update_mbuf(inp, m);
2567fe1274eeSMichael Tuexen 	} else {
256852cd27cbSRobert Watson 		in_pcbgroup_update(inp);
2569fe1274eeSMichael Tuexen 	}
257052cd27cbSRobert Watson #endif
2571c3229e05SDavid Greenman 	return (0);
257215bd2b43SDavid Greenman }
257315bd2b43SDavid Greenman 
257452cd27cbSRobert Watson int
257552cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
257652cd27cbSRobert Watson {
257752cd27cbSRobert Watson 
2578fe1274eeSMichael Tuexen 	return (in_pcbinshash_internal(inp, NULL));
257952cd27cbSRobert Watson }
258052cd27cbSRobert Watson 
258152cd27cbSRobert Watson int
2582fe1274eeSMichael Tuexen in_pcbinshash_mbuf(struct inpcb *inp, struct mbuf *m)
258352cd27cbSRobert Watson {
258452cd27cbSRobert Watson 
2585fe1274eeSMichael Tuexen 	return (in_pcbinshash_internal(inp, m));
258652cd27cbSRobert Watson }
258752cd27cbSRobert Watson 
258852cd27cbSRobert Watson /*
2589c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2590c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2591c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2592c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2593c3229e05SDavid Greenman  */
259415bd2b43SDavid Greenman void
2595d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
259615bd2b43SDavid Greenman {
259759daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
259815bd2b43SDavid Greenman 	struct inpcbhead *head;
2599cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
260015bd2b43SDavid Greenman 
26018501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2602fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2603fa046d87SRobert Watson 
2604111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2605111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2606602cc7f1SRobert Watson 
2607cfa1ca9dSYoshinobu Inoue #ifdef INET6
2608cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
26091b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2610cfa1ca9dSYoshinobu Inoue 	else
261183e521ecSBjoern A. Zeeb #endif
2612cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2613cfa1ca9dSYoshinobu Inoue 
2614712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2615712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
261615bd2b43SDavid Greenman 
2617b872626dSMatt Macy 	CK_LIST_REMOVE(inp, inp_hash);
2618b872626dSMatt Macy 	CK_LIST_INSERT_HEAD(head, inp, inp_hash);
261952cd27cbSRobert Watson 
262052cd27cbSRobert Watson #ifdef PCBGROUP
262152cd27cbSRobert Watson 	if (m != NULL)
262252cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
262352cd27cbSRobert Watson 	else
262452cd27cbSRobert Watson 		in_pcbgroup_update(inp);
262552cd27cbSRobert Watson #endif
2626c3229e05SDavid Greenman }
2627c3229e05SDavid Greenman 
2628d3c1f003SRobert Watson void
2629d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2630d3c1f003SRobert Watson {
2631d3c1f003SRobert Watson 
2632d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2633d3c1f003SRobert Watson }
2634d3c1f003SRobert Watson 
2635c3229e05SDavid Greenman /*
2636c3229e05SDavid Greenman  * Remove PCB from various lists.
2637c3229e05SDavid Greenman  */
26386d888973SRobert Watson static void
2639136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2640c3229e05SDavid Greenman {
264159daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
264259daba27SSam Leffler 
26438501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2644ff9b006dSJulien Charbon 	INP_LIST_WLOCK_ASSERT(pcbinfo);
264559daba27SSam Leffler 
264659daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2647111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2648c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2649c3229e05SDavid Greenman 
2650fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
26511a43cff9SSean Bruno 
26521a43cff9SSean Bruno 		/* XXX: Only do if SO_REUSEPORT_LB set? */
26531a43cff9SSean Bruno 		in_pcbremlbgrouphash(inp);
26541a43cff9SSean Bruno 
2655b872626dSMatt Macy 		CK_LIST_REMOVE(inp, inp_hash);
2656b872626dSMatt Macy 		CK_LIST_REMOVE(inp, inp_portlist);
2657b872626dSMatt Macy 		if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) {
2658b872626dSMatt Macy 			CK_LIST_REMOVE(phd, phd_hash);
26592a4bd982SGleb Smirnoff 			NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx);
2660c3229e05SDavid Greenman 		}
2661fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2662111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2663c3229e05SDavid Greenman 	}
2664b872626dSMatt Macy 	CK_LIST_REMOVE(inp, inp_list);
266559daba27SSam Leffler 	pcbinfo->ipi_count--;
266652cd27cbSRobert Watson #ifdef PCBGROUP
266752cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
266852cd27cbSRobert Watson #endif
266915bd2b43SDavid Greenman }
267075c13541SPoul-Henning Kamp 
2671a557af22SRobert Watson /*
267284cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
267384cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
267484cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
267584cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
267684cc0778SGeorge V. Neville-Neil  */
267784cc0778SGeorge V. Neville-Neil void
267884cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
267984cc0778SGeorge V. Neville-Neil {
268084cc0778SGeorge V. Neville-Neil 
2681fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
268284cc0778SGeorge V. Neville-Neil 	return;
268384cc0778SGeorge V. Neville-Neil }
268484cc0778SGeorge V. Neville-Neil 
268584cc0778SGeorge V. Neville-Neil /*
2686a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2687a557af22SRobert Watson  * label change into the in_pcb for the socket.
2688a557af22SRobert Watson  */
2689a557af22SRobert Watson void
2690136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2691a557af22SRobert Watson {
2692a557af22SRobert Watson #ifdef MAC
2693a557af22SRobert Watson 	struct inpcb *inp;
2694a557af22SRobert Watson 
26954c7c478dSRobert Watson 	inp = sotoinpcb(so);
26964c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2697602cc7f1SRobert Watson 
26988501a69cSRobert Watson 	INP_WLOCK(inp);
2699310e7cebSRobert Watson 	SOCK_LOCK(so);
2700a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2701310e7cebSRobert Watson 	SOCK_UNLOCK(so);
27028501a69cSRobert Watson 	INP_WUNLOCK(inp);
2703a557af22SRobert Watson #endif
2704a557af22SRobert Watson }
27055f311da2SMike Silbersack 
27065f311da2SMike Silbersack /*
2707ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2708ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2709ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2710ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2711ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
27125f311da2SMike Silbersack  */
2713aae49dd3SBjoern A. Zeeb static void
2714136d4f1cSRobert Watson ipport_tick(void *xtp)
27155f311da2SMike Silbersack {
27168b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2717ad3a630fSRobert Watson 
27185ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
27198b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
27208b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
27218b615593SMarko Zec 		if (V_ipport_tcpallocs <=
27228b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2723603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2724603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2725ad3a630fSRobert Watson 		} else
2726603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2727603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
27288b615593SMarko Zec 		CURVNET_RESTORE();
27298b615593SMarko Zec 	}
27305ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
27315f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
27325f311da2SMike Silbersack }
2733497057eeSRobert Watson 
2734aae49dd3SBjoern A. Zeeb static void
2735aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2736aae49dd3SBjoern A. Zeeb {
2737aae49dd3SBjoern A. Zeeb 
2738aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2739aae49dd3SBjoern A. Zeeb }
2740aae49dd3SBjoern A. Zeeb 
2741aae49dd3SBjoern A. Zeeb /*
2742aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2743aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2744aae49dd3SBjoern A. Zeeb  */
2745aae49dd3SBjoern A. Zeeb static void
2746aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2747aae49dd3SBjoern A. Zeeb {
2748aae49dd3SBjoern A. Zeeb 
2749aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2750fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2751aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2752aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2753aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2754aae49dd3SBjoern A. Zeeb }
2755aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2756aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2757aae49dd3SBjoern A. Zeeb 
27583d585327SKip Macy void
27593d585327SKip Macy inp_wlock(struct inpcb *inp)
27603d585327SKip Macy {
27613d585327SKip Macy 
27628501a69cSRobert Watson 	INP_WLOCK(inp);
27633d585327SKip Macy }
27643d585327SKip Macy 
27653d585327SKip Macy void
27663d585327SKip Macy inp_wunlock(struct inpcb *inp)
27673d585327SKip Macy {
27683d585327SKip Macy 
27698501a69cSRobert Watson 	INP_WUNLOCK(inp);
27703d585327SKip Macy }
27713d585327SKip Macy 
27723d585327SKip Macy void
27733d585327SKip Macy inp_rlock(struct inpcb *inp)
27743d585327SKip Macy {
27753d585327SKip Macy 
2776a69042a5SRobert Watson 	INP_RLOCK(inp);
27773d585327SKip Macy }
27783d585327SKip Macy 
27793d585327SKip Macy void
27803d585327SKip Macy inp_runlock(struct inpcb *inp)
27813d585327SKip Macy {
27823d585327SKip Macy 
2783a69042a5SRobert Watson 	INP_RUNLOCK(inp);
27843d585327SKip Macy }
27853d585327SKip Macy 
2786c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
27873d585327SKip Macy void
2788e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
27893d585327SKip Macy {
27903d585327SKip Macy 
27918501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
27923d585327SKip Macy }
27933d585327SKip Macy 
27943d585327SKip Macy void
2795e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
27963d585327SKip Macy {
27973d585327SKip Macy 
27983d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
27993d585327SKip Macy }
28003d585327SKip Macy #endif
28013d585327SKip Macy 
28029378e437SKip Macy void
28039378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
28049378e437SKip Macy {
28059378e437SKip Macy 	struct inpcb *inp;
28069378e437SKip Macy 
2807ff9b006dSJulien Charbon 	INP_INFO_WLOCK(&V_tcbinfo);
2808b872626dSMatt Macy 	CK_LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
28099378e437SKip Macy 		INP_WLOCK(inp);
28109378e437SKip Macy 		func(inp, arg);
28119378e437SKip Macy 		INP_WUNLOCK(inp);
28129378e437SKip Macy 	}
2813ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(&V_tcbinfo);
28149378e437SKip Macy }
28159378e437SKip Macy 
28169378e437SKip Macy struct socket *
28179378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
28189378e437SKip Macy {
28199378e437SKip Macy 
28209378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
28219378e437SKip Macy 	return (inp->inp_socket);
28229378e437SKip Macy }
28239378e437SKip Macy 
28249378e437SKip Macy struct tcpcb *
28259378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
28269378e437SKip Macy {
28279378e437SKip Macy 
28289378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
28299378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
28309378e437SKip Macy }
28319378e437SKip Macy 
28329378e437SKip Macy int
28339378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
28349378e437SKip Macy {
28359378e437SKip Macy 
28369378e437SKip Macy 	return (inp->inp_ip_tos);
28379378e437SKip Macy }
28389378e437SKip Macy 
28399378e437SKip Macy void
28409378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
28419378e437SKip Macy {
28429378e437SKip Macy 
28439378e437SKip Macy 	inp->inp_ip_tos = val;
28449378e437SKip Macy }
28459378e437SKip Macy 
28469378e437SKip Macy void
2847df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
28489d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
28499378e437SKip Macy {
28509378e437SKip Macy 
28519d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2852df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2853df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
28549378e437SKip Macy 	*lp = inp->inp_lport;
28559378e437SKip Macy 	*fp = inp->inp_fport;
28569378e437SKip Macy }
28579378e437SKip Macy 
2858dd0e6c38SKip Macy struct inpcb *
2859dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2860dd0e6c38SKip Macy {
2861dd0e6c38SKip Macy 
2862dd0e6c38SKip Macy 	return (sotoinpcb(so));
2863dd0e6c38SKip Macy }
2864dd0e6c38SKip Macy 
2865dd0e6c38SKip Macy struct tcpcb *
2866dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2867dd0e6c38SKip Macy {
2868dd0e6c38SKip Macy 
2869dd0e6c38SKip Macy 	return (sototcpcb(so));
2870dd0e6c38SKip Macy }
2871dd0e6c38SKip Macy 
2872cc65eb4eSGleb Smirnoff /*
2873cc65eb4eSGleb Smirnoff  * Create an external-format (``xinpcb'') structure using the information in
2874cc65eb4eSGleb Smirnoff  * the kernel-format in_pcb structure pointed to by inp.  This is done to
2875cc65eb4eSGleb Smirnoff  * reduce the spew of irrelevant information over this interface, to isolate
2876cc65eb4eSGleb Smirnoff  * user code from changes in the kernel structure, and potentially to provide
2877cc65eb4eSGleb Smirnoff  * information-hiding if we decide that some of this information should be
2878cc65eb4eSGleb Smirnoff  * hidden from users.
2879cc65eb4eSGleb Smirnoff  */
2880cc65eb4eSGleb Smirnoff void
2881cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi)
2882cc65eb4eSGleb Smirnoff {
2883cc65eb4eSGleb Smirnoff 
288479db6fe7SMark Johnston 	bzero(xi, sizeof(*xi));
2885cc65eb4eSGleb Smirnoff 	xi->xi_len = sizeof(struct xinpcb);
2886cc65eb4eSGleb Smirnoff 	if (inp->inp_socket)
2887cc65eb4eSGleb Smirnoff 		sotoxsocket(inp->inp_socket, &xi->xi_socket);
2888cc65eb4eSGleb Smirnoff 	bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo));
2889cc65eb4eSGleb Smirnoff 	xi->inp_gencnt = inp->inp_gencnt;
28903a20f06aSBrooks Davis 	xi->inp_ppcb = (uintptr_t)inp->inp_ppcb;
2891cc65eb4eSGleb Smirnoff 	xi->inp_flow = inp->inp_flow;
2892cc65eb4eSGleb Smirnoff 	xi->inp_flowid = inp->inp_flowid;
2893cc65eb4eSGleb Smirnoff 	xi->inp_flowtype = inp->inp_flowtype;
2894cc65eb4eSGleb Smirnoff 	xi->inp_flags = inp->inp_flags;
2895cc65eb4eSGleb Smirnoff 	xi->inp_flags2 = inp->inp_flags2;
2896cc65eb4eSGleb Smirnoff 	xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket;
2897cc65eb4eSGleb Smirnoff 	xi->in6p_cksum = inp->in6p_cksum;
2898cc65eb4eSGleb Smirnoff 	xi->in6p_hops = inp->in6p_hops;
2899cc65eb4eSGleb Smirnoff 	xi->inp_ip_tos = inp->inp_ip_tos;
2900cc65eb4eSGleb Smirnoff 	xi->inp_vflag = inp->inp_vflag;
2901cc65eb4eSGleb Smirnoff 	xi->inp_ip_ttl = inp->inp_ip_ttl;
2902cc65eb4eSGleb Smirnoff 	xi->inp_ip_p = inp->inp_ip_p;
2903cc65eb4eSGleb Smirnoff 	xi->inp_ip_minttl = inp->inp_ip_minttl;
2904cc65eb4eSGleb Smirnoff }
2905cc65eb4eSGleb Smirnoff 
2906497057eeSRobert Watson #ifdef DDB
2907497057eeSRobert Watson static void
2908497057eeSRobert Watson db_print_indent(int indent)
2909497057eeSRobert Watson {
2910497057eeSRobert Watson 	int i;
2911497057eeSRobert Watson 
2912497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2913497057eeSRobert Watson 		db_printf(" ");
2914497057eeSRobert Watson }
2915497057eeSRobert Watson 
2916497057eeSRobert Watson static void
2917497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2918497057eeSRobert Watson {
2919497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2920497057eeSRobert Watson 
2921497057eeSRobert Watson 	db_print_indent(indent);
2922497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2923497057eeSRobert Watson 
2924497057eeSRobert Watson 	indent += 2;
2925497057eeSRobert Watson 
292603dc38a4SRobert Watson #ifdef INET6
2927dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2928497057eeSRobert Watson 		/* IPv6. */
2929497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2930497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
293183e521ecSBjoern A. Zeeb 	} else
293203dc38a4SRobert Watson #endif
293383e521ecSBjoern A. Zeeb 	{
2934497057eeSRobert Watson 		/* IPv4. */
2935497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2936497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2937497057eeSRobert Watson 	}
2938497057eeSRobert Watson 	db_print_indent(indent);
2939497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2940497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2941497057eeSRobert Watson 	db_print_indent(indent);
2942497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2943497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2944497057eeSRobert Watson }
2945497057eeSRobert Watson 
2946497057eeSRobert Watson static void
2947497057eeSRobert Watson db_print_inpflags(int inp_flags)
2948497057eeSRobert Watson {
2949497057eeSRobert Watson 	int comma;
2950497057eeSRobert Watson 
2951497057eeSRobert Watson 	comma = 0;
2952497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2953497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2954497057eeSRobert Watson 		comma = 1;
2955497057eeSRobert Watson 	}
2956497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2957497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2958497057eeSRobert Watson 		comma = 1;
2959497057eeSRobert Watson 	}
2960497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2961497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2962497057eeSRobert Watson 		comma = 1;
2963497057eeSRobert Watson 	}
2964dce33a45SErmal Luçi 	if (inp_flags & INP_ORIGDSTADDR) {
2965dce33a45SErmal Luçi 		db_printf("%sINP_ORIGDSTADDR", comma ? ", " : "");
2966dce33a45SErmal Luçi 		comma = 1;
2967dce33a45SErmal Luçi 	}
2968497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2969497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2970497057eeSRobert Watson 		comma = 1;
2971497057eeSRobert Watson 	}
2972497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2973497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2974497057eeSRobert Watson 		comma = 1;
2975497057eeSRobert Watson 	}
2976497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2977497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2978497057eeSRobert Watson 		comma = 1;
2979497057eeSRobert Watson 	}
2980497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2981497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2982497057eeSRobert Watson 		comma = 1;
2983497057eeSRobert Watson 	}
2984497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2985497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2986497057eeSRobert Watson 		comma = 1;
2987497057eeSRobert Watson 	}
2988497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2989497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2990497057eeSRobert Watson 		comma = 1;
2991497057eeSRobert Watson 	}
2992497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2993497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2994497057eeSRobert Watson 		comma = 1;
2995497057eeSRobert Watson 	}
2996497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2997497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2998497057eeSRobert Watson 		comma = 1;
2999497057eeSRobert Watson 	}
30003cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
30013cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
30023cca425bSMichael Tuexen 		comma = 1;
30033cca425bSMichael Tuexen 	}
3004497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
3005497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
3006497057eeSRobert Watson 		comma = 1;
3007497057eeSRobert Watson 	}
3008497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
3009497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
3010497057eeSRobert Watson 		comma = 1;
3011497057eeSRobert Watson 	}
3012497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
3013497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
3014497057eeSRobert Watson 		comma = 1;
3015497057eeSRobert Watson 	}
3016497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
3017497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
3018497057eeSRobert Watson 		comma = 1;
3019497057eeSRobert Watson 	}
3020497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
3021497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
3022497057eeSRobert Watson 		comma = 1;
3023497057eeSRobert Watson 	}
3024497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
3025497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
3026497057eeSRobert Watson 		comma = 1;
3027497057eeSRobert Watson 	}
3028497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
3029497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
3030497057eeSRobert Watson 		comma = 1;
3031497057eeSRobert Watson 	}
3032497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
3033497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
3034497057eeSRobert Watson 		comma = 1;
3035497057eeSRobert Watson 	}
3036497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
3037497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
3038497057eeSRobert Watson 		comma = 1;
3039497057eeSRobert Watson 	}
3040ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
3041ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
3042ad71fe3cSRobert Watson 		comma  = 1;
3043ad71fe3cSRobert Watson 	}
3044ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
3045ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
3046ad71fe3cSRobert Watson 		comma  = 1;
3047ad71fe3cSRobert Watson 	}
3048ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
3049ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
3050ad71fe3cSRobert Watson 		comma  = 1;
3051ad71fe3cSRobert Watson 	}
3052ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
3053ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
3054ad71fe3cSRobert Watson 		comma  = 1;
3055ad71fe3cSRobert Watson 	}
3056497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
3057497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
3058497057eeSRobert Watson 		comma = 1;
3059497057eeSRobert Watson 	}
3060497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
3061497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
3062497057eeSRobert Watson 		comma = 1;
3063497057eeSRobert Watson 	}
3064497057eeSRobert Watson }
3065497057eeSRobert Watson 
3066497057eeSRobert Watson static void
3067497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
3068497057eeSRobert Watson {
3069497057eeSRobert Watson 	int comma;
3070497057eeSRobert Watson 
3071497057eeSRobert Watson 	comma = 0;
3072497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
3073497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
3074497057eeSRobert Watson 		comma  = 1;
3075497057eeSRobert Watson 	}
3076497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
3077497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
3078497057eeSRobert Watson 		comma  = 1;
3079497057eeSRobert Watson 	}
3080497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
3081497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
3082497057eeSRobert Watson 		comma  = 1;
3083497057eeSRobert Watson 	}
3084497057eeSRobert Watson }
3085497057eeSRobert Watson 
30866d888973SRobert Watson static void
3087497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
3088497057eeSRobert Watson {
3089497057eeSRobert Watson 
3090497057eeSRobert Watson 	db_print_indent(indent);
3091497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
3092497057eeSRobert Watson 
3093497057eeSRobert Watson 	indent += 2;
3094497057eeSRobert Watson 
3095497057eeSRobert Watson 	db_print_indent(indent);
3096497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
3097497057eeSRobert Watson 
3098497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
3099497057eeSRobert Watson 
3100497057eeSRobert Watson 	db_print_indent(indent);
3101497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
3102497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
3103497057eeSRobert Watson 
3104497057eeSRobert Watson 	db_print_indent(indent);
3105497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
3106497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
3107497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
3108497057eeSRobert Watson 	db_printf(")\n");
3109497057eeSRobert Watson 
3110497057eeSRobert Watson 	db_print_indent(indent);
3111497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
3112497057eeSRobert Watson 	    inp->inp_vflag);
3113497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
3114497057eeSRobert Watson 	db_printf(")\n");
3115497057eeSRobert Watson 
3116497057eeSRobert Watson 	db_print_indent(indent);
3117497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
3118497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
3119497057eeSRobert Watson 
3120497057eeSRobert Watson 	db_print_indent(indent);
3121497057eeSRobert Watson #ifdef INET6
3122497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
3123497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
3124497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
3125497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
3126497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
3127497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
3128497057eeSRobert Watson 		    inp->in6p_hops);
3129497057eeSRobert Watson 	} else
3130497057eeSRobert Watson #endif
3131497057eeSRobert Watson 	{
3132497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
3133497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
3134497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
3135497057eeSRobert Watson 	}
3136497057eeSRobert Watson 
3137497057eeSRobert Watson 	db_print_indent(indent);
3138497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
3139497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
3140497057eeSRobert Watson }
3141497057eeSRobert Watson 
3142497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
3143497057eeSRobert Watson {
3144497057eeSRobert Watson 	struct inpcb *inp;
3145497057eeSRobert Watson 
3146497057eeSRobert Watson 	if (!have_addr) {
3147497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
3148497057eeSRobert Watson 		return;
3149497057eeSRobert Watson 	}
3150497057eeSRobert Watson 	inp = (struct inpcb *)addr;
3151497057eeSRobert Watson 
3152497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
3153497057eeSRobert Watson }
315483e521ecSBjoern A. Zeeb #endif /* DDB */
3155f3e7afe2SHans Petter Selasky 
3156f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
3157f3e7afe2SHans Petter Selasky /*
3158f3e7afe2SHans Petter Selasky  * Modify TX rate limit based on the existing "inp->inp_snd_tag",
3159f3e7afe2SHans Petter Selasky  * if any.
3160f3e7afe2SHans Petter Selasky  */
3161f3e7afe2SHans Petter Selasky int
3162f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate)
3163f3e7afe2SHans Petter Selasky {
3164f3e7afe2SHans Petter Selasky 	union if_snd_tag_modify_params params = {
3165f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
316620abea66SRandall Stewart 		.rate_limit.flags = M_NOWAIT,
3167f3e7afe2SHans Petter Selasky 	};
3168f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3169f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
3170f3e7afe2SHans Petter Selasky 	int error;
3171f3e7afe2SHans Petter Selasky 
3172f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3173f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3174f3e7afe2SHans Petter Selasky 		return (EINVAL);
3175f3e7afe2SHans Petter Selasky 
3176f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
3177f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
3178f3e7afe2SHans Petter Selasky 		return (EINVAL);
3179f3e7afe2SHans Petter Selasky 
3180f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_modify == NULL) {
3181f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
3182f3e7afe2SHans Petter Selasky 	} else {
3183f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_modify(mst, &params);
3184f3e7afe2SHans Petter Selasky 	}
3185f3e7afe2SHans Petter Selasky 	return (error);
3186f3e7afe2SHans Petter Selasky }
3187f3e7afe2SHans Petter Selasky 
3188f3e7afe2SHans Petter Selasky /*
3189f3e7afe2SHans Petter Selasky  * Query existing TX rate limit based on the existing
3190f3e7afe2SHans Petter Selasky  * "inp->inp_snd_tag", if any.
3191f3e7afe2SHans Petter Selasky  */
3192f3e7afe2SHans Petter Selasky int
3193f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate)
3194f3e7afe2SHans Petter Selasky {
3195f3e7afe2SHans Petter Selasky 	union if_snd_tag_query_params params = { };
3196f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3197f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
3198f3e7afe2SHans Petter Selasky 	int error;
3199f3e7afe2SHans Petter Selasky 
3200f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3201f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3202f3e7afe2SHans Petter Selasky 		return (EINVAL);
3203f3e7afe2SHans Petter Selasky 
3204f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
3205f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
3206f3e7afe2SHans Petter Selasky 		return (EINVAL);
3207f3e7afe2SHans Petter Selasky 
3208f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL) {
3209f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
3210f3e7afe2SHans Petter Selasky 	} else {
3211f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_query(mst, &params);
3212f3e7afe2SHans Petter Selasky 		if (error == 0 &&  p_max_pacing_rate != NULL)
3213f3e7afe2SHans Petter Selasky 			*p_max_pacing_rate = params.rate_limit.max_rate;
3214f3e7afe2SHans Petter Selasky 	}
3215f3e7afe2SHans Petter Selasky 	return (error);
3216f3e7afe2SHans Petter Selasky }
3217f3e7afe2SHans Petter Selasky 
3218f3e7afe2SHans Petter Selasky /*
321995ed5015SHans Petter Selasky  * Query existing TX queue level based on the existing
322095ed5015SHans Petter Selasky  * "inp->inp_snd_tag", if any.
322195ed5015SHans Petter Selasky  */
322295ed5015SHans Petter Selasky int
322395ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level)
322495ed5015SHans Petter Selasky {
322595ed5015SHans Petter Selasky 	union if_snd_tag_query_params params = { };
322695ed5015SHans Petter Selasky 	struct m_snd_tag *mst;
322795ed5015SHans Petter Selasky 	struct ifnet *ifp;
322895ed5015SHans Petter Selasky 	int error;
322995ed5015SHans Petter Selasky 
323095ed5015SHans Petter Selasky 	mst = inp->inp_snd_tag;
323195ed5015SHans Petter Selasky 	if (mst == NULL)
323295ed5015SHans Petter Selasky 		return (EINVAL);
323395ed5015SHans Petter Selasky 
323495ed5015SHans Petter Selasky 	ifp = mst->ifp;
323595ed5015SHans Petter Selasky 	if (ifp == NULL)
323695ed5015SHans Petter Selasky 		return (EINVAL);
323795ed5015SHans Petter Selasky 
323895ed5015SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL)
323995ed5015SHans Petter Selasky 		return (EOPNOTSUPP);
324095ed5015SHans Petter Selasky 
324195ed5015SHans Petter Selasky 	error = ifp->if_snd_tag_query(mst, &params);
324295ed5015SHans Petter Selasky 	if (error == 0 &&  p_txqueue_level != NULL)
324395ed5015SHans Petter Selasky 		*p_txqueue_level = params.rate_limit.queue_level;
324495ed5015SHans Petter Selasky 	return (error);
324595ed5015SHans Petter Selasky }
324695ed5015SHans Petter Selasky 
324795ed5015SHans Petter Selasky /*
3248f3e7afe2SHans Petter Selasky  * Allocate a new TX rate limit send tag from the network interface
3249f3e7afe2SHans Petter Selasky  * given by the "ifp" argument and save it in "inp->inp_snd_tag":
3250f3e7afe2SHans Petter Selasky  */
3251f3e7afe2SHans Petter Selasky int
3252f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp,
325320abea66SRandall Stewart     uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st)
325420abea66SRandall Stewart 
3255f3e7afe2SHans Petter Selasky {
3256f3e7afe2SHans Petter Selasky 	union if_snd_tag_alloc_params params = {
325795ed5015SHans Petter Selasky 		.rate_limit.hdr.type = (max_pacing_rate == -1U) ?
325895ed5015SHans Petter Selasky 		    IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT,
3259f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowid = flowid,
3260f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowtype = flowtype,
3261*98085baeSAndrew Gallatin 		.rate_limit.hdr.numa_domain = inp->inp_numa_domain,
3262f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
326320abea66SRandall Stewart 		.rate_limit.flags = M_NOWAIT,
3264f3e7afe2SHans Petter Selasky 	};
3265f3e7afe2SHans Petter Selasky 	int error;
3266f3e7afe2SHans Petter Selasky 
3267f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
3268f3e7afe2SHans Petter Selasky 
326920abea66SRandall Stewart 	if (*st != NULL)
3270f3e7afe2SHans Petter Selasky 		return (EINVAL);
3271f3e7afe2SHans Petter Selasky 
3272f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_alloc == NULL) {
3273f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
3274f3e7afe2SHans Petter Selasky 	} else {
3275f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_alloc(ifp, &params, &inp->inp_snd_tag);
327620abea66SRandall Stewart 
3277903c4ee6SXin LI #ifdef INET
327820abea66SRandall Stewart 		if (error == 0) {
327920abea66SRandall Stewart 			counter_u64_add(rate_limit_set_ok, 1);
328020abea66SRandall Stewart 			counter_u64_add(rate_limit_active, 1);
328120abea66SRandall Stewart 		} else
328220abea66SRandall Stewart 			counter_u64_add(rate_limit_alloc_fail, 1);
3283903c4ee6SXin LI #endif
3284f3e7afe2SHans Petter Selasky 	}
3285f3e7afe2SHans Petter Selasky 	return (error);
3286f3e7afe2SHans Petter Selasky }
3287f3e7afe2SHans Petter Selasky 
328820abea66SRandall Stewart void
328920abea66SRandall Stewart in_pcbdetach_tag(struct ifnet *ifp, struct m_snd_tag *mst)
329020abea66SRandall Stewart {
329120abea66SRandall Stewart 	if (ifp == NULL)
329220abea66SRandall Stewart 		return;
329320abea66SRandall Stewart 
329420abea66SRandall Stewart 	/*
329520abea66SRandall Stewart 	 * If the device was detached while we still had reference(s)
329620abea66SRandall Stewart 	 * on the ifp, we assume if_snd_tag_free() was replaced with
329720abea66SRandall Stewart 	 * stubs.
329820abea66SRandall Stewart 	 */
329920abea66SRandall Stewart 	ifp->if_snd_tag_free(mst);
330020abea66SRandall Stewart 
330120abea66SRandall Stewart 	/* release reference count on network interface */
330220abea66SRandall Stewart 	if_rele(ifp);
3303903c4ee6SXin LI #ifdef INET
330420abea66SRandall Stewart 	counter_u64_add(rate_limit_active, -1);
3305903c4ee6SXin LI #endif
330620abea66SRandall Stewart }
330720abea66SRandall Stewart 
3308f3e7afe2SHans Petter Selasky /*
3309f3e7afe2SHans Petter Selasky  * Free an existing TX rate limit tag based on the "inp->inp_snd_tag",
3310f3e7afe2SHans Petter Selasky  * if any:
3311f3e7afe2SHans Petter Selasky  */
3312f3e7afe2SHans Petter Selasky void
3313f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp)
3314f3e7afe2SHans Petter Selasky {
3315f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3316f3e7afe2SHans Petter Selasky 
3317f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
3318f3e7afe2SHans Petter Selasky 
3319f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3320f3e7afe2SHans Petter Selasky 	inp->inp_snd_tag = NULL;
3321f3e7afe2SHans Petter Selasky 
3322f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3323f3e7afe2SHans Petter Selasky 		return;
3324f3e7afe2SHans Petter Selasky 
3325fb3bc596SJohn Baldwin 	m_snd_tag_rele(mst);
3326f3e7afe2SHans Petter Selasky }
3327f3e7afe2SHans Petter Selasky 
332820abea66SRandall Stewart int
332920abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate)
333020abea66SRandall Stewart {
333120abea66SRandall Stewart 	int error;
333220abea66SRandall Stewart 
333320abea66SRandall Stewart 	/*
333420abea66SRandall Stewart 	 * If the existing send tag is for the wrong interface due to
333520abea66SRandall Stewart 	 * a route change, first drop the existing tag.  Set the
333620abea66SRandall Stewart 	 * CHANGED flag so that we will keep trying to allocate a new
333720abea66SRandall Stewart 	 * tag if we fail to allocate one this time.
333820abea66SRandall Stewart 	 */
333920abea66SRandall Stewart 	if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) {
334020abea66SRandall Stewart 		in_pcbdetach_txrtlmt(inp);
334120abea66SRandall Stewart 		inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
334220abea66SRandall Stewart 	}
334320abea66SRandall Stewart 
334420abea66SRandall Stewart 	/*
334520abea66SRandall Stewart 	 * NOTE: When attaching to a network interface a reference is
334620abea66SRandall Stewart 	 * made to ensure the network interface doesn't go away until
334720abea66SRandall Stewart 	 * all ratelimit connections are gone. The network interface
334820abea66SRandall Stewart 	 * pointers compared below represent valid network interfaces,
334920abea66SRandall Stewart 	 * except when comparing towards NULL.
335020abea66SRandall Stewart 	 */
335120abea66SRandall Stewart 	if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) {
335220abea66SRandall Stewart 		error = 0;
335320abea66SRandall Stewart 	} else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) {
335420abea66SRandall Stewart 		if (inp->inp_snd_tag != NULL)
335520abea66SRandall Stewart 			in_pcbdetach_txrtlmt(inp);
335620abea66SRandall Stewart 		error = 0;
335720abea66SRandall Stewart 	} else if (inp->inp_snd_tag == NULL) {
335820abea66SRandall Stewart 		/*
335920abea66SRandall Stewart 		 * In order to utilize packet pacing with RSS, we need
336020abea66SRandall Stewart 		 * to wait until there is a valid RSS hash before we
336120abea66SRandall Stewart 		 * can proceed:
336220abea66SRandall Stewart 		 */
336320abea66SRandall Stewart 		if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) {
336420abea66SRandall Stewart 			error = EAGAIN;
336520abea66SRandall Stewart 		} else {
336620abea66SRandall Stewart 			error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb),
336720abea66SRandall Stewart 			    mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag);
336820abea66SRandall Stewart 		}
336920abea66SRandall Stewart 	} else {
337020abea66SRandall Stewart 		error = in_pcbmodify_txrtlmt(inp, max_pacing_rate);
337120abea66SRandall Stewart 	}
337220abea66SRandall Stewart 	if (error == 0 || error == EOPNOTSUPP)
337320abea66SRandall Stewart 		inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
337420abea66SRandall Stewart 
337520abea66SRandall Stewart 	return (error);
337620abea66SRandall Stewart }
337720abea66SRandall Stewart 
3378f3e7afe2SHans Petter Selasky /*
3379f3e7afe2SHans Petter Selasky  * This function should be called when the INP_RATE_LIMIT_CHANGED flag
3380f3e7afe2SHans Petter Selasky  * is set in the fast path and will attach/detach/modify the TX rate
3381f3e7afe2SHans Petter Selasky  * limit send tag based on the socket's so_max_pacing_rate value.
3382f3e7afe2SHans Petter Selasky  */
3383f3e7afe2SHans Petter Selasky void
3384f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb)
3385f3e7afe2SHans Petter Selasky {
3386f3e7afe2SHans Petter Selasky 	struct socket *socket;
3387f3e7afe2SHans Petter Selasky 	uint32_t max_pacing_rate;
3388f3e7afe2SHans Petter Selasky 	bool did_upgrade;
3389f3e7afe2SHans Petter Selasky 	int error;
3390f3e7afe2SHans Petter Selasky 
3391f3e7afe2SHans Petter Selasky 	if (inp == NULL)
3392f3e7afe2SHans Petter Selasky 		return;
3393f3e7afe2SHans Petter Selasky 
3394f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
3395f3e7afe2SHans Petter Selasky 	if (socket == NULL)
3396f3e7afe2SHans Petter Selasky 		return;
3397f3e7afe2SHans Petter Selasky 
3398f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
3399f3e7afe2SHans Petter Selasky 		/*
3400f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
3401f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
3402f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3403f3e7afe2SHans Petter Selasky 		 * write lock.
3404f3e7afe2SHans Petter Selasky 		 */
3405f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3406f3e7afe2SHans Petter Selasky 			return;
3407f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3408f3e7afe2SHans Petter Selasky 	} else {
3409f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3410f3e7afe2SHans Petter Selasky 	}
3411f3e7afe2SHans Petter Selasky 
3412f3e7afe2SHans Petter Selasky 	/*
3413f3e7afe2SHans Petter Selasky 	 * NOTE: The so_max_pacing_rate value is read unlocked,
3414f3e7afe2SHans Petter Selasky 	 * because atomic updates are not required since the variable
3415f3e7afe2SHans Petter Selasky 	 * is checked at every mbuf we send. It is assumed that the
3416f3e7afe2SHans Petter Selasky 	 * variable read itself will be atomic.
3417f3e7afe2SHans Petter Selasky 	 */
3418f3e7afe2SHans Petter Selasky 	max_pacing_rate = socket->so_max_pacing_rate;
3419f3e7afe2SHans Petter Selasky 
342020abea66SRandall Stewart 	error = in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate);
3421fb3bc596SJohn Baldwin 
3422f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3423f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3424f3e7afe2SHans Petter Selasky }
3425f3e7afe2SHans Petter Selasky 
3426f3e7afe2SHans Petter Selasky /*
3427f3e7afe2SHans Petter Selasky  * Track route changes for TX rate limiting.
3428f3e7afe2SHans Petter Selasky  */
3429f3e7afe2SHans Petter Selasky void
3430f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp)
3431f3e7afe2SHans Petter Selasky {
3432f3e7afe2SHans Petter Selasky 	bool did_upgrade;
3433f3e7afe2SHans Petter Selasky 
3434f3e7afe2SHans Petter Selasky 	if (inp == NULL)
3435f3e7afe2SHans Petter Selasky 		return;
3436f3e7afe2SHans Petter Selasky 
3437f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag == NULL)
3438f3e7afe2SHans Petter Selasky 		return;
3439f3e7afe2SHans Petter Selasky 
3440f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
3441f3e7afe2SHans Petter Selasky 		/*
3442f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
3443f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
3444f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3445f3e7afe2SHans Petter Selasky 		 * write lock.
3446f3e7afe2SHans Petter Selasky 		 */
3447f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3448f3e7afe2SHans Petter Selasky 			return;
3449f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3450f3e7afe2SHans Petter Selasky 	} else {
3451f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3452f3e7afe2SHans Petter Selasky 	}
3453f3e7afe2SHans Petter Selasky 
3454f3e7afe2SHans Petter Selasky 	/* detach rate limiting */
3455f3e7afe2SHans Petter Selasky 	in_pcbdetach_txrtlmt(inp);
3456f3e7afe2SHans Petter Selasky 
3457f3e7afe2SHans Petter Selasky 	/* make sure new mbuf send tag allocation is made */
3458f3e7afe2SHans Petter Selasky 	inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
3459f3e7afe2SHans Petter Selasky 
3460f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3461f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3462f3e7afe2SHans Petter Selasky }
346320abea66SRandall Stewart 
3464903c4ee6SXin LI #ifdef INET
346520abea66SRandall Stewart static void
346620abea66SRandall Stewart rl_init(void *st)
346720abea66SRandall Stewart {
346820abea66SRandall Stewart 	rate_limit_active = counter_u64_alloc(M_WAITOK);
346920abea66SRandall Stewart 	rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK);
347020abea66SRandall Stewart 	rate_limit_set_ok = counter_u64_alloc(M_WAITOK);
347120abea66SRandall Stewart }
347220abea66SRandall Stewart 
347320abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL);
3474903c4ee6SXin LI #endif
3475f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */
3476