xref: /freebsd/sys/netinet/in_pcb.c (revision 5f539170784cf8aed6989e055ff3ac190d0de80b)
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.
85ebea466SGleb Smirnoff  * Copyright (c) 2021-2022 Gleb Smirnoff <glebius@FreeBSD.org>
9497057eeSRobert Watson  * All rights reserved.
10df8bae1dSRodney W. Grimes  *
1179bdc6e5SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
1279bdc6e5SRobert Watson  * contract to Juniper Networks, Inc.
1379bdc6e5SRobert Watson  *
14df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
15df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
16df8bae1dSRodney W. Grimes  * are met:
17df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
19df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
21df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
394b421e2dSMike Silbersack #include <sys/cdefs.h>
40497057eeSRobert Watson #include "opt_ddb.h"
416a800098SYoshinobu Inoue #include "opt_ipsec.h"
42efc76f72SBjoern A. Zeeb #include "opt_inet.h"
43cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
44f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
450c325f53SAlexander V. Chernikov #include "opt_route.h"
467527624eSRobert Watson #include "opt_rss.h"
47cfa1ca9dSYoshinobu Inoue 
48df8bae1dSRodney W. Grimes #include <sys/param.h>
49a0577692SGleb Smirnoff #include <sys/hash.h>
50df8bae1dSRodney W. Grimes #include <sys/systm.h>
51a0577692SGleb Smirnoff #include <sys/libkern.h>
52cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
53df8bae1dSRodney W. Grimes #include <sys/malloc.h>
54df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
55ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h>
56cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
57fdb987beSMark Johnston #include <sys/proc.h>
58df8bae1dSRodney W. Grimes #include <sys/protosw.h>
593ee9c3c4SRandall Stewart #include <sys/smp.h>
60fdb987beSMark Johnston #include <sys/smr.h>
61df8bae1dSRodney W. Grimes #include <sys/socket.h>
62df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
63f3e7afe2SHans Petter Selasky #include <sys/sockio.h>
64acd3428bSRobert Watson #include <sys/priv.h>
65df8bae1dSRodney W. Grimes #include <sys/proc.h>
6679bdc6e5SRobert Watson #include <sys/refcount.h>
6775c13541SPoul-Henning Kamp #include <sys/jail.h>
68101f9fc8SPeter Wemm #include <sys/kernel.h>
69101f9fc8SPeter Wemm #include <sys/sysctl.h>
708781d8e9SBruce Evans 
71497057eeSRobert Watson #ifdef DDB
72497057eeSRobert Watson #include <ddb/ddb.h>
73497057eeSRobert Watson #endif
74497057eeSRobert Watson 
7569c2d429SJeff Roberson #include <vm/uma.h>
76a034518aSAndrew Gallatin #include <vm/vm.h>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <net/if.h>
7976039bc8SGleb Smirnoff #include <net/if_var.h>
803d0d5b21SJustin Hibbits #include <net/if_private.h>
81cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
826d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h>
83df8bae1dSRodney W. Grimes #include <net/route.h>
84b2bdc62aSAdrian Chadd #include <net/rss_config.h>
85530c0060SRobert Watson #include <net/vnet.h>
86df8bae1dSRodney W. Grimes 
8767107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
88df8bae1dSRodney W. Grimes #include <netinet/in.h>
89df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
900f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h>
9173bebcc5SGleb Smirnoff #include <netinet/tcp.h>
9259854ecfSHans Petter Selasky #ifdef INET
9359854ecfSHans Petter Selasky #include <netinet/in_var.h>
949ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h>
9559854ecfSHans Petter Selasky #endif
96df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
97cfa1ca9dSYoshinobu Inoue #ifdef INET6
98cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
99efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
10067107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
10167107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
102cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1039ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h>
10459854ecfSHans Petter Selasky #endif
105cfa1ca9dSYoshinobu Inoue 
106fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
107b9234fafSSam Leffler 
108aed55708SRobert Watson #include <security/mac/mac_framework.h>
109aed55708SRobert Watson 
1101a43cff9SSean Bruno #define	INPCBLBGROUP_SIZMIN	8
1111a43cff9SSean Bruno #define	INPCBLBGROUP_SIZMAX	256
112a43e7a96SGleb Smirnoff 
113a43e7a96SGleb Smirnoff #define	INP_FREED	0x00000200	/* Went through in_pcbfree(). */
114a43e7a96SGleb Smirnoff #define	INP_INLBGROUP	0x01000000	/* Inserted into inpcblbgroup. */
1151a43cff9SSean Bruno 
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 
13519acc506SGleb Smirnoff /* Enable random ephemeral port allocation by default. */
13619acc506SGleb Smirnoff VNET_DEFINE(int, ipport_randomized) = 1;
1376ac48b74SMike Silbersack 
138d2025bd0SBjoern A. Zeeb #ifdef INET
139fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
140fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
141fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
142da806e8dSMark Johnston 			    int lookupflags, uint8_t numa_domain, int fib);
14367107f45SBjoern A. Zeeb 
144bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
145bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
146bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
147bbd42ad0SPeter Wemm 
148bbd42ad0SPeter Wemm static int
14982d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
150bbd42ad0SPeter Wemm {
15130a4ab08SBruce Evans 	int error;
15230a4ab08SBruce Evans 
153f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
15430a4ab08SBruce Evans 	if (error == 0) {
155603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
156603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
157603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
158603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
159603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
160603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
161bbd42ad0SPeter Wemm 	}
16230a4ab08SBruce Evans 	return (error);
163bbd42ad0SPeter Wemm }
164bbd42ad0SPeter Wemm 
165bbd42ad0SPeter Wemm #undef RANGECHK
166bbd42ad0SPeter Wemm 
1677029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange,
1687029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1696472ac3dSEd Schouten     "IP Ports");
17033b3ac06SPeter Wemm 
1716df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1727029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1737029da5cSPawel Biernacki     &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I",
1747029da5cSPawel Biernacki     "");
1756df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1767029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1777029da5cSPawel Biernacki     &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I",
1787029da5cSPawel Biernacki     "");
1796df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1807029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1817029da5cSPawel Biernacki     &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I",
1827029da5cSPawel Biernacki     "");
1836df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1847029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1857029da5cSPawel Biernacki     &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I",
1867029da5cSPawel Biernacki     "");
1876df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
1887029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1897029da5cSPawel Biernacki     &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I",
1907029da5cSPawel Biernacki     "");
1916df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
1927029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1937029da5cSPawel Biernacki     &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I",
1947029da5cSPawel Biernacki     "");
1956df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
1966df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
1976df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
199eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
2016df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
202eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
20320abea66SRandall Stewart 
20420abea66SRandall Stewart #ifdef RATELIMIT
2051a714ff2SRandall Stewart counter_u64_t rate_limit_new;
2061a714ff2SRandall Stewart counter_u64_t rate_limit_chg;
20720abea66SRandall Stewart counter_u64_t rate_limit_active;
20820abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail;
20920abea66SRandall Stewart counter_u64_t rate_limit_set_ok;
21020abea66SRandall Stewart 
2117029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
21220abea66SRandall Stewart     "IP Rate Limiting");
21320abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD,
21420abea66SRandall Stewart     &rate_limit_active, "Active rate limited connections");
21520abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD,
21620abea66SRandall Stewart    &rate_limit_alloc_fail, "Rate limited connection failures");
21720abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD,
21820abea66SRandall Stewart    &rate_limit_set_ok, "Rate limited setting succeeded");
2191a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD,
2201a714ff2SRandall Stewart    &rate_limit_new, "Total Rate limit new attempts");
2211a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD,
2221a714ff2SRandall Stewart    &rate_limit_chg, "Total Rate limited change attempts");
22320abea66SRandall Stewart #endif /* RATELIMIT */
22420abea66SRandall Stewart 
22583e521ecSBjoern A. Zeeb #endif /* INET */
2260312fbe9SPoul-Henning Kamp 
227a0577692SGleb Smirnoff VNET_DEFINE(uint32_t, in_pcbhashseed);
228a0577692SGleb Smirnoff static void
229a0577692SGleb Smirnoff in_pcbhashseed_init(void)
230a0577692SGleb Smirnoff {
231a0577692SGleb Smirnoff 
232a0577692SGleb Smirnoff 	V_in_pcbhashseed = arc4random();
233a0577692SGleb Smirnoff }
234a0577692SGleb Smirnoff VNET_SYSINIT(in_pcbhashseed_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
235ec6e7677SZhenlei Huang     in_pcbhashseed_init, NULL);
236a0577692SGleb Smirnoff 
2373b62f335SKristof Provost #ifdef INET
238417b35a9SMark Johnston VNET_DEFINE_STATIC(int, connect_inaddr_wild) = 1;
239417b35a9SMark Johnston #define	V_connect_inaddr_wild	VNET(connect_inaddr_wild)
240417b35a9SMark Johnston SYSCTL_INT(_net_inet_ip, OID_AUTO, connect_inaddr_wild,
241417b35a9SMark Johnston     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(connect_inaddr_wild), 0,
242417b35a9SMark Johnston     "Allow connecting to INADDR_ANY or INADDR_BROADCAST for connect(2)");
2433b62f335SKristof Provost #endif
244417b35a9SMark Johnston 
2453ba34b07SGleb Smirnoff static void in_pcbremhash(struct inpcb *);
2463ba34b07SGleb Smirnoff 
247c3229e05SDavid Greenman /*
248c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
249c3229e05SDavid Greenman  *
250de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
251de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
252de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
253c3229e05SDavid Greenman  */
254c3229e05SDavid Greenman 
2551a43cff9SSean Bruno static struct inpcblbgroup *
256a600aabeSMark Johnston in_pcblbgroup_alloc(struct ucred *cred, u_char vflag, uint16_t port,
257bbd0084bSMark Johnston     const union in_dependaddr *addr, int size, uint8_t numa_domain, int fib)
2581a43cff9SSean Bruno {
2591a43cff9SSean Bruno 	struct inpcblbgroup *grp;
2601a43cff9SSean Bruno 	size_t bytes;
2611a43cff9SSean Bruno 
2621a43cff9SSean Bruno 	bytes = __offsetof(struct inpcblbgroup, il_inp[size]);
2631a43cff9SSean Bruno 	grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT);
264d93ec8cbSMark Johnston 	if (grp == NULL)
2651a43cff9SSean Bruno 		return (NULL);
2667cbb6b6eSMark Johnston 	LIST_INIT(&grp->il_pending);
267d93ec8cbSMark Johnston 	grp->il_cred = crhold(cred);
2681a43cff9SSean Bruno 	grp->il_vflag = vflag;
2691a43cff9SSean Bruno 	grp->il_lport = port;
270a034518aSAndrew Gallatin 	grp->il_numa_domain = numa_domain;
271bbd0084bSMark Johnston 	grp->il_fibnum = fib;
2721a43cff9SSean Bruno 	grp->il_dependladdr = *addr;
2731a43cff9SSean Bruno 	grp->il_inpsiz = size;
2741a43cff9SSean Bruno 	return (grp);
2751a43cff9SSean Bruno }
2761a43cff9SSean Bruno 
2771a43cff9SSean Bruno static void
27854af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx)
27954af3d0dSMark Johnston {
28054af3d0dSMark Johnston 	struct inpcblbgroup *grp;
28154af3d0dSMark Johnston 
28254af3d0dSMark Johnston 	grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx);
283d93ec8cbSMark Johnston 	crfree(grp->il_cred);
28454af3d0dSMark Johnston 	free(grp, M_PCB);
28554af3d0dSMark Johnston }
28654af3d0dSMark Johnston 
28754af3d0dSMark Johnston static void
2881a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp)
2891a43cff9SSean Bruno {
2907cbb6b6eSMark Johnston 	KASSERT(LIST_EMPTY(&grp->il_pending),
2917cbb6b6eSMark Johnston 	    ("local group %p still has pending inps", grp));
2921a43cff9SSean Bruno 
29354af3d0dSMark Johnston 	CK_LIST_REMOVE(grp, il_list);
2942a4bd982SGleb Smirnoff 	NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx);
2951a43cff9SSean Bruno }
2961a43cff9SSean Bruno 
2977cbb6b6eSMark Johnston static struct inpcblbgroup *
2987cbb6b6eSMark Johnston in_pcblbgroup_find(struct inpcb *inp)
2997cbb6b6eSMark Johnston {
3007cbb6b6eSMark Johnston 	struct inpcbinfo *pcbinfo;
3017cbb6b6eSMark Johnston 	struct inpcblbgroup *grp;
3027cbb6b6eSMark Johnston 	struct inpcblbgrouphead *hdr;
3037cbb6b6eSMark Johnston 
3047cbb6b6eSMark Johnston 	INP_LOCK_ASSERT(inp);
3057cbb6b6eSMark Johnston 
3067cbb6b6eSMark Johnston 	pcbinfo = inp->inp_pcbinfo;
3077cbb6b6eSMark Johnston 	INP_HASH_LOCK_ASSERT(pcbinfo);
3087cbb6b6eSMark Johnston 
3097cbb6b6eSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[
3107cbb6b6eSMark Johnston 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)];
3117cbb6b6eSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
3127cbb6b6eSMark Johnston 		struct inpcb *inp1;
3137cbb6b6eSMark Johnston 
3147cbb6b6eSMark Johnston 		for (unsigned int i = 0; i < grp->il_inpcnt; i++) {
3157cbb6b6eSMark Johnston 			if (inp == grp->il_inp[i])
3167cbb6b6eSMark Johnston 				goto found;
3177cbb6b6eSMark Johnston 		}
3187cbb6b6eSMark Johnston 		LIST_FOREACH(inp1, &grp->il_pending, inp_lbgroup_list) {
3197cbb6b6eSMark Johnston 			if (inp == inp1)
3207cbb6b6eSMark Johnston 				goto found;
3217cbb6b6eSMark Johnston 		}
3227cbb6b6eSMark Johnston 	}
3237cbb6b6eSMark Johnston found:
3247cbb6b6eSMark Johnston 	return (grp);
3257cbb6b6eSMark Johnston }
3267cbb6b6eSMark Johnston 
327a600aabeSMark Johnston static void
328a600aabeSMark Johnston in_pcblbgroup_insert(struct inpcblbgroup *grp, struct inpcb *inp)
329a600aabeSMark Johnston {
330a600aabeSMark Johnston 	KASSERT(grp->il_inpcnt < grp->il_inpsiz,
331a600aabeSMark Johnston 	    ("invalid local group size %d and count %d", grp->il_inpsiz,
332a600aabeSMark Johnston 	    grp->il_inpcnt));
333a600aabeSMark Johnston 	INP_WLOCK_ASSERT(inp);
334a600aabeSMark Johnston 
3357cbb6b6eSMark Johnston 	if (inp->inp_socket->so_proto->pr_listen != pr_listen_notsupp &&
3367cbb6b6eSMark Johnston 	    !SOLISTENING(inp->inp_socket)) {
3377cbb6b6eSMark Johnston 		/*
3387cbb6b6eSMark Johnston 		 * If this is a TCP socket, it should not be visible to lbgroup
3397cbb6b6eSMark Johnston 		 * lookups until listen() has been called.
3407cbb6b6eSMark Johnston 		 */
3417cbb6b6eSMark Johnston 		LIST_INSERT_HEAD(&grp->il_pending, inp, inp_lbgroup_list);
3428b3d2c19SMark Johnston 		grp->il_pendcnt++;
3437cbb6b6eSMark Johnston 	} else {
344a600aabeSMark Johnston 		grp->il_inp[grp->il_inpcnt] = inp;
345a600aabeSMark Johnston 
346a600aabeSMark Johnston 		/*
3477cbb6b6eSMark Johnston 		 * Synchronize with in_pcblookup_lbgroup(): make sure that we
3487cbb6b6eSMark Johnston 		 * don't expose a null slot to the lookup path.
349a600aabeSMark Johnston 		 */
350a600aabeSMark Johnston 		atomic_store_rel_int(&grp->il_inpcnt, grp->il_inpcnt + 1);
351a600aabeSMark Johnston 	}
352a600aabeSMark Johnston 
3537cbb6b6eSMark Johnston 	inp->inp_flags |= INP_INLBGROUP;
3547cbb6b6eSMark Johnston }
3557cbb6b6eSMark Johnston 
3561a43cff9SSean Bruno static struct inpcblbgroup *
3571a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr,
3581a43cff9SSean Bruno     struct inpcblbgroup *old_grp, int size)
3591a43cff9SSean Bruno {
3601a43cff9SSean Bruno 	struct inpcblbgroup *grp;
3611a43cff9SSean Bruno 	int i;
3621a43cff9SSean Bruno 
363a600aabeSMark Johnston 	grp = in_pcblbgroup_alloc(old_grp->il_cred, old_grp->il_vflag,
364a034518aSAndrew Gallatin 	    old_grp->il_lport, &old_grp->il_dependladdr, size,
365bbd0084bSMark Johnston 	    old_grp->il_numa_domain, old_grp->il_fibnum);
36679ee680bSMark Johnston 	if (grp == NULL)
3671a43cff9SSean Bruno 		return (NULL);
3681a43cff9SSean Bruno 
3691a43cff9SSean Bruno 	KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
3701a43cff9SSean Bruno 	    ("invalid new local group size %d and old local group count %d",
3711a43cff9SSean Bruno 	     grp->il_inpsiz, old_grp->il_inpcnt));
3721a43cff9SSean Bruno 
3731a43cff9SSean Bruno 	for (i = 0; i < old_grp->il_inpcnt; ++i)
3741a43cff9SSean Bruno 		grp->il_inp[i] = old_grp->il_inp[i];
3751a43cff9SSean Bruno 	grp->il_inpcnt = old_grp->il_inpcnt;
376a600aabeSMark Johnston 	CK_LIST_INSERT_HEAD(hdr, grp, il_list);
3777cbb6b6eSMark Johnston 	LIST_SWAP(&old_grp->il_pending, &grp->il_pending, inpcb,
3787cbb6b6eSMark Johnston 	    inp_lbgroup_list);
3798b3d2c19SMark Johnston 	grp->il_pendcnt = old_grp->il_pendcnt;
3808b3d2c19SMark Johnston 	old_grp->il_pendcnt = 0;
3811a43cff9SSean Bruno 	in_pcblbgroup_free(old_grp);
3821a43cff9SSean Bruno 	return (grp);
3831a43cff9SSean Bruno }
3841a43cff9SSean Bruno 
3851a43cff9SSean Bruno /*
3861a43cff9SSean Bruno  * Add PCB to load balance group for SO_REUSEPORT_LB option.
3871a43cff9SSean Bruno  */
3881a43cff9SSean Bruno static int
389a034518aSAndrew Gallatin in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain)
3901a43cff9SSean Bruno {
391a7026c7fSMark Johnston 	const static struct timeval interval = { 60, 0 };
392a7026c7fSMark Johnston 	static struct timeval lastprint;
3931a43cff9SSean Bruno 	struct inpcbinfo *pcbinfo;
3941a43cff9SSean Bruno 	struct inpcblbgrouphead *hdr;
3951a43cff9SSean Bruno 	struct inpcblbgroup *grp;
39679ee680bSMark Johnston 	uint32_t idx;
397bbd0084bSMark Johnston 	int fib;
3981a43cff9SSean Bruno 
3991a43cff9SSean Bruno 	pcbinfo = inp->inp_pcbinfo;
4001a43cff9SSean Bruno 
4011a43cff9SSean Bruno 	INP_WLOCK_ASSERT(inp);
4021a43cff9SSean Bruno 	INP_HASH_WLOCK_ASSERT(pcbinfo);
4031a43cff9SSean Bruno 
404bbd0084bSMark Johnston 	fib = (inp->inp_flags & INP_BOUNDFIB) != 0 ?
405bbd0084bSMark Johnston 	    inp->inp_inc.inc_fibnum : RT_ALL_FIBS;
406bbd0084bSMark Johnston 
4071a43cff9SSean Bruno #ifdef INET6
4081a43cff9SSean Bruno 	/*
4091a43cff9SSean Bruno 	 * Don't allow IPv4 mapped INET6 wild socket.
4101a43cff9SSean Bruno 	 */
4111a43cff9SSean Bruno 	if ((inp->inp_vflag & INP_IPV4) &&
4121a43cff9SSean Bruno 	    inp->inp_laddr.s_addr == INADDR_ANY &&
4131a43cff9SSean Bruno 	    INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) {
4141a43cff9SSean Bruno 		return (0);
4151a43cff9SSean Bruno 	}
4161a43cff9SSean Bruno #endif
4171a43cff9SSean Bruno 
4189d2877fcSMark Johnston 	idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask);
41979ee680bSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[idx];
42054af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
421d93ec8cbSMark Johnston 		if (grp->il_cred->cr_prison == inp->inp_cred->cr_prison &&
422d93ec8cbSMark Johnston 		    grp->il_vflag == inp->inp_vflag &&
4231a43cff9SSean Bruno 		    grp->il_lport == inp->inp_lport &&
424a034518aSAndrew Gallatin 		    grp->il_numa_domain == numa_domain &&
425bbd0084bSMark Johnston 		    grp->il_fibnum == fib &&
4261a43cff9SSean Bruno 		    memcmp(&grp->il_dependladdr,
4271a43cff9SSean Bruno 		    &inp->inp_inc.inc_ie.ie_dependladdr,
428d93ec8cbSMark Johnston 		    sizeof(grp->il_dependladdr)) == 0) {
4291a43cff9SSean Bruno 			break;
4301a43cff9SSean Bruno 		}
431d93ec8cbSMark Johnston 	}
4321a43cff9SSean Bruno 	if (grp == NULL) {
4331a43cff9SSean Bruno 		/* Create new load balance group. */
434a600aabeSMark Johnston 		grp = in_pcblbgroup_alloc(inp->inp_cred, inp->inp_vflag,
4351a43cff9SSean Bruno 		    inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
436bbd0084bSMark Johnston 		    INPCBLBGROUP_SIZMIN, numa_domain, fib);
43779ee680bSMark Johnston 		if (grp == NULL)
4381a43cff9SSean Bruno 			return (ENOBUFS);
439a600aabeSMark Johnston 		in_pcblbgroup_insert(grp, inp);
440a600aabeSMark Johnston 		CK_LIST_INSERT_HEAD(hdr, grp, il_list);
4418b3d2c19SMark Johnston 	} else if (grp->il_inpcnt + grp->il_pendcnt == grp->il_inpsiz) {
4421a43cff9SSean Bruno 		if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) {
443a7026c7fSMark Johnston 			if (ratecheck(&lastprint, &interval))
4441a43cff9SSean Bruno 				printf("lb group port %d, limit reached\n",
4451a43cff9SSean Bruno 				    ntohs(grp->il_lport));
4461a43cff9SSean Bruno 			return (0);
4471a43cff9SSean Bruno 		}
4481a43cff9SSean Bruno 
4491a43cff9SSean Bruno 		/* Expand this local group. */
4501a43cff9SSean Bruno 		grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2);
45179ee680bSMark Johnston 		if (grp == NULL)
4521a43cff9SSean Bruno 			return (ENOBUFS);
453a600aabeSMark Johnston 		in_pcblbgroup_insert(grp, inp);
454a600aabeSMark Johnston 	} else {
455a600aabeSMark Johnston 		in_pcblbgroup_insert(grp, inp);
4561a43cff9SSean Bruno 	}
4571a43cff9SSean Bruno 	return (0);
4581a43cff9SSean Bruno }
4591a43cff9SSean Bruno 
4601a43cff9SSean Bruno /*
4611a43cff9SSean Bruno  * Remove PCB from load balance group.
4621a43cff9SSean Bruno  */
4631a43cff9SSean Bruno static void
4641a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp)
4651a43cff9SSean Bruno {
4661a43cff9SSean Bruno 	struct inpcbinfo *pcbinfo;
4671a43cff9SSean Bruno 	struct inpcblbgrouphead *hdr;
4681a43cff9SSean Bruno 	struct inpcblbgroup *grp;
4697cbb6b6eSMark Johnston 	struct inpcb *inp1;
4701a43cff9SSean Bruno 	int i;
4711a43cff9SSean Bruno 
4721a43cff9SSean Bruno 	pcbinfo = inp->inp_pcbinfo;
4731a43cff9SSean Bruno 
4741a43cff9SSean Bruno 	INP_WLOCK_ASSERT(inp);
475a43e7a96SGleb Smirnoff 	MPASS(inp->inp_flags & INP_INLBGROUP);
4761a43cff9SSean Bruno 	INP_HASH_WLOCK_ASSERT(pcbinfo);
4771a43cff9SSean Bruno 
4781a43cff9SSean Bruno 	hdr = &pcbinfo->ipi_lbgrouphashbase[
4799d2877fcSMark Johnston 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)];
48054af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
4811a43cff9SSean Bruno 		for (i = 0; i < grp->il_inpcnt; ++i) {
4821a43cff9SSean Bruno 			if (grp->il_inp[i] != inp)
4831a43cff9SSean Bruno 				continue;
4841a43cff9SSean Bruno 
4857cbb6b6eSMark Johnston 			if (grp->il_inpcnt == 1 &&
4867cbb6b6eSMark Johnston 			    LIST_EMPTY(&grp->il_pending)) {
4871a43cff9SSean Bruno 				/* We are the last, free this local group. */
4881a43cff9SSean Bruno 				in_pcblbgroup_free(grp);
4891a43cff9SSean Bruno 			} else {
490a600aabeSMark Johnston 				grp->il_inp[i] =
491a600aabeSMark Johnston 				    grp->il_inp[grp->il_inpcnt - 1];
492a600aabeSMark Johnston 
493a600aabeSMark Johnston 				/*
494a600aabeSMark Johnston 				 * Synchronize with in_pcblookup_lbgroup().
495a600aabeSMark Johnston 				 */
496a600aabeSMark Johnston 				atomic_store_rel_int(&grp->il_inpcnt,
497a600aabeSMark Johnston 				    grp->il_inpcnt - 1);
4981a43cff9SSean Bruno 			}
499a43e7a96SGleb Smirnoff 			inp->inp_flags &= ~INP_INLBGROUP;
5001a43cff9SSean Bruno 			return;
5011a43cff9SSean Bruno 		}
5027cbb6b6eSMark Johnston 		LIST_FOREACH(inp1, &grp->il_pending, inp_lbgroup_list) {
5037cbb6b6eSMark Johnston 			if (inp == inp1) {
5047cbb6b6eSMark Johnston 				LIST_REMOVE(inp, inp_lbgroup_list);
5058b3d2c19SMark Johnston 				grp->il_pendcnt--;
5067cbb6b6eSMark Johnston 				inp->inp_flags &= ~INP_INLBGROUP;
5077cbb6b6eSMark Johnston 				return;
5081a43cff9SSean Bruno 			}
5097cbb6b6eSMark Johnston 		}
5107cbb6b6eSMark Johnston 	}
5117cbb6b6eSMark Johnston 	__assert_unreachable();
5121a43cff9SSean Bruno }
5131a43cff9SSean Bruno 
514a034518aSAndrew Gallatin int
515a034518aSAndrew Gallatin in_pcblbgroup_numa(struct inpcb *inp, int arg)
516a034518aSAndrew Gallatin {
517a034518aSAndrew Gallatin 	struct inpcbinfo *pcbinfo;
5187cbb6b6eSMark Johnston 	int error;
519a034518aSAndrew Gallatin 	uint8_t numa_domain;
520a034518aSAndrew Gallatin 
521a034518aSAndrew Gallatin 	switch (arg) {
522a034518aSAndrew Gallatin 	case TCP_REUSPORT_LB_NUMA_NODOM:
523a034518aSAndrew Gallatin 		numa_domain = M_NODOM;
524a034518aSAndrew Gallatin 		break;
525a034518aSAndrew Gallatin 	case TCP_REUSPORT_LB_NUMA_CURDOM:
526a034518aSAndrew Gallatin 		numa_domain = PCPU_GET(domain);
527a034518aSAndrew Gallatin 		break;
528a034518aSAndrew Gallatin 	default:
529a034518aSAndrew Gallatin 		if (arg < 0 || arg >= vm_ndomains)
530a034518aSAndrew Gallatin 			return (EINVAL);
531a034518aSAndrew Gallatin 		numa_domain = arg;
532a034518aSAndrew Gallatin 	}
533a034518aSAndrew Gallatin 
534a034518aSAndrew Gallatin 	pcbinfo = inp->inp_pcbinfo;
535a034518aSAndrew Gallatin 	INP_WLOCK_ASSERT(inp);
536a034518aSAndrew Gallatin 	INP_HASH_WLOCK(pcbinfo);
5377cbb6b6eSMark Johnston 	if (in_pcblbgroup_find(inp) != NULL) {
538a034518aSAndrew Gallatin 		/* Remove it from the old group. */
539a034518aSAndrew Gallatin 		in_pcbremlbgrouphash(inp);
540a034518aSAndrew Gallatin 		/* Add it to the new group based on numa domain. */
541a034518aSAndrew Gallatin 		in_pcbinslbgrouphash(inp, numa_domain);
5427cbb6b6eSMark Johnston 		error = 0;
5437cbb6b6eSMark Johnston 	} else {
5447cbb6b6eSMark Johnston 		error = ENOENT;
545a034518aSAndrew Gallatin 	}
546a034518aSAndrew Gallatin 	INP_HASH_WUNLOCK(pcbinfo);
5477cbb6b6eSMark Johnston 	return (error);
548a034518aSAndrew Gallatin }
549a034518aSAndrew Gallatin 
550db0ac6deSCy Schubert /* Make sure it is safe to use hashinit(9) on CK_LIST. */
551db0ac6deSCy Schubert CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb)));
552db0ac6deSCy Schubert 
553cc487c16SGleb Smirnoff /*
554fec8a8c7SGleb Smirnoff  * Initialize an inpcbinfo - a per-VNET instance of connections db.
5559bcd427bSRobert Watson  */
5569bcd427bSRobert Watson void
557fec8a8c7SGleb Smirnoff in_pcbinfo_init(struct inpcbinfo *pcbinfo, struct inpcbstorage *pcbstor,
558fec8a8c7SGleb Smirnoff     u_int hash_nelements, u_int porthash_nelements)
5599bcd427bSRobert Watson {
5609bcd427bSRobert Watson 
561fec8a8c7SGleb Smirnoff 	mtx_init(&pcbinfo->ipi_lock, pcbstor->ips_infolock_name, NULL, MTX_DEF);
562fec8a8c7SGleb Smirnoff 	mtx_init(&pcbinfo->ipi_hash_lock, pcbstor->ips_hashlock_name,
563fec8a8c7SGleb Smirnoff 	    NULL, MTX_DEF);
5649bcd427bSRobert Watson #ifdef VIMAGE
5659bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
5669bcd427bSRobert Watson #endif
567db0ac6deSCy Schubert 	CK_LIST_INIT(&pcbinfo->ipi_listhead);
568fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
569fdb987beSMark Johnston 	pcbinfo->ipi_hash_exact = hashinit(hash_nelements, M_PCB,
570fdb987beSMark Johnston 	    &pcbinfo->ipi_hashmask);
571fdb987beSMark Johnston 	pcbinfo->ipi_hash_wild = hashinit(hash_nelements, M_PCB,
5729bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
573db0ac6deSCy Schubert 	porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1);
5749bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
5759bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
5769d2877fcSMark Johnston 	pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB,
5771a43cff9SSean Bruno 	    &pcbinfo->ipi_lbgrouphashmask);
578fec8a8c7SGleb Smirnoff 	pcbinfo->ipi_zone = pcbstor->ips_zone;
579db0ac6deSCy Schubert 	pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone);
5809bcd427bSRobert Watson }
5819bcd427bSRobert Watson 
5829bcd427bSRobert Watson /*
5839bcd427bSRobert Watson  * Destroy an inpcbinfo.
5849bcd427bSRobert Watson  */
5859bcd427bSRobert Watson void
5869bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
5879bcd427bSRobert Watson {
5889bcd427bSRobert Watson 
589fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
590fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
591fa046d87SRobert Watson 
592fdb987beSMark Johnston 	hashdestroy(pcbinfo->ipi_hash_exact, M_PCB, pcbinfo->ipi_hashmask);
593fdb987beSMark Johnston 	hashdestroy(pcbinfo->ipi_hash_wild, M_PCB, pcbinfo->ipi_hashmask);
5949bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
5959bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
5961a43cff9SSean Bruno 	hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB,
5971a43cff9SSean Bruno 	    pcbinfo->ipi_lbgrouphashmask);
598db0ac6deSCy Schubert 	mtx_destroy(&pcbinfo->ipi_hash_lock);
599db0ac6deSCy Schubert 	mtx_destroy(&pcbinfo->ipi_lock);
6009bcd427bSRobert Watson }
6019bcd427bSRobert Watson 
6029bcd427bSRobert Watson /*
603fec8a8c7SGleb Smirnoff  * Initialize a pcbstorage - per protocol zones to allocate inpcbs.
604fec8a8c7SGleb Smirnoff  */
605fec8a8c7SGleb Smirnoff static void inpcb_fini(void *, int);
606fec8a8c7SGleb Smirnoff void
607fec8a8c7SGleb Smirnoff in_pcbstorage_init(void *arg)
608fec8a8c7SGleb Smirnoff {
609fec8a8c7SGleb Smirnoff 	struct inpcbstorage *pcbstor = arg;
610fec8a8c7SGleb Smirnoff 
611fec8a8c7SGleb Smirnoff 	pcbstor->ips_zone = uma_zcreate(pcbstor->ips_zone_name,
6125fd1a67eSMark Johnston 	    pcbstor->ips_size, NULL, NULL, pcbstor->ips_pcbinit,
613c4a4b263SAndrew Gallatin 	    inpcb_fini, UMA_ALIGN_CACHE, UMA_ZONE_SMR);
614fec8a8c7SGleb Smirnoff }
615fec8a8c7SGleb Smirnoff 
616fec8a8c7SGleb Smirnoff /*
617fec8a8c7SGleb Smirnoff  * Destroy a pcbstorage - used by unloadable protocols.
618fec8a8c7SGleb Smirnoff  */
619fec8a8c7SGleb Smirnoff void
620fec8a8c7SGleb Smirnoff in_pcbstorage_destroy(void *arg)
621fec8a8c7SGleb Smirnoff {
622fec8a8c7SGleb Smirnoff 	struct inpcbstorage *pcbstor = arg;
623fec8a8c7SGleb Smirnoff 
624fec8a8c7SGleb Smirnoff 	uma_zdestroy(pcbstor->ips_zone);
625fec8a8c7SGleb Smirnoff }
626fec8a8c7SGleb Smirnoff 
627fec8a8c7SGleb Smirnoff /*
628c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
629d915b280SStephan Uphoff  * On success return with the PCB locked.
630c3229e05SDavid Greenman  */
631df8bae1dSRodney W. Grimes int
632d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
633df8bae1dSRodney W. Grimes {
634136d4f1cSRobert Watson 	struct inpcb *inp;
635fe5324acSJohn Baldwin #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC)
63613cf67f3SHajimu UMEMOTO 	int error;
637fe5324acSJohn Baldwin #endif
638a557af22SRobert Watson 
639db0ac6deSCy Schubert 	inp = uma_zalloc_smr(pcbinfo->ipi_zone, M_NOWAIT);
640df8bae1dSRodney W. Grimes 	if (inp == NULL)
641df8bae1dSRodney W. Grimes 		return (ENOBUFS);
6426a6cefacSGleb Smirnoff 	bzero(&inp->inp_start_zero, inp_zero_size);
64350575ce1SAndrew Gallatin #ifdef NUMA
64450575ce1SAndrew Gallatin 	inp->inp_numa_domain = M_NODOM;
64550575ce1SAndrew Gallatin #endif
64615bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
647df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
64886d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
6498b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
650a557af22SRobert Watson #ifdef MAC
65130d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
652a557af22SRobert Watson 	if (error != 0)
653a557af22SRobert Watson 		goto out;
65430d239bcSRobert Watson 	mac_inpcb_create(so, inp);
655a557af22SRobert Watson #endif
656fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
657fcf59617SAndrey V. Elsukov 	error = ipsec_init_pcbpolicy(inp);
6580bffde27SRobert Watson 	if (error != 0) {
6590bffde27SRobert Watson #ifdef MAC
6600bffde27SRobert Watson 		mac_inpcb_destroy(inp);
6610bffde27SRobert Watson #endif
662a557af22SRobert Watson 		goto out;
6630bffde27SRobert Watson 	}
664b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
665e3fd5ffdSRobert Watson #ifdef INET6
666340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
667c7a62c92SGleb Smirnoff 		inp->inp_vflag |= INP_IPV6PROTO | INP_IPV6;
668603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
66933841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
670c7a62c92SGleb Smirnoff #ifdef INET
671c7a62c92SGleb Smirnoff 		else
672c7a62c92SGleb Smirnoff 			inp->inp_vflag |= INP_IPV4;
673c7a62c92SGleb Smirnoff #endif
674603724d3SBjoern A. Zeeb 		if (V_ip6_auto_flowlabel)
67533841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_AUTOFLOWLABEL;
676c7a62c92SGleb Smirnoff 		inp->in6p_hops = -1;	/* use kernel default */
677c7a62c92SGleb Smirnoff 	}
678c7a62c92SGleb Smirnoff #endif
679c7a62c92SGleb Smirnoff #if defined(INET) && defined(INET6)
680c7a62c92SGleb Smirnoff 	else
681c7a62c92SGleb Smirnoff #endif
682c7a62c92SGleb Smirnoff #ifdef INET
683c7a62c92SGleb Smirnoff 		inp->inp_vflag |= INP_IPV4;
68433841545SHajimu UMEMOTO #endif
685fdb987beSMark Johnston 	inp->inp_smr = SMR_SEQ_INVALID;
686fdb987beSMark Johnston 
6878c1960d5SMike Karels 	/*
6888c1960d5SMike Karels 	 * Routes in inpcb's can cache L2 as well; they are guaranteed
6898c1960d5SMike Karels 	 * to be cleaned up.
6908c1960d5SMike Karels 	 */
6918c1960d5SMike Karels 	inp->inp_route.ro_flags = RT_LLE_CACHE;
692db0ac6deSCy Schubert 	refcount_init(&inp->inp_refcount, 1);   /* Reference from socket. */
693db0ac6deSCy Schubert 	INP_WLOCK(inp);
694db0ac6deSCy Schubert 	INP_INFO_WLOCK(pcbinfo);
695db0ac6deSCy Schubert 	pcbinfo->ipi_count++;
696db0ac6deSCy Schubert 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
697db0ac6deSCy Schubert 	CK_LIST_INSERT_HEAD(&pcbinfo->ipi_listhead, inp, inp_list);
698db0ac6deSCy Schubert 	INP_INFO_WUNLOCK(pcbinfo);
699db0ac6deSCy Schubert 	so->so_pcb = inp;
700db0ac6deSCy Schubert 
701db0ac6deSCy Schubert 	return (0);
702db0ac6deSCy Schubert 
7037a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC)
704a557af22SRobert Watson out:
70530cf0fbfSRichard Scheffenegger 	crfree(inp->inp_cred);
70630cf0fbfSRichard Scheffenegger #ifdef INVARIANTS
70730cf0fbfSRichard Scheffenegger 	inp->inp_cred = NULL;
70830cf0fbfSRichard Scheffenegger #endif
709db0ac6deSCy Schubert 	uma_zfree_smr(pcbinfo->ipi_zone, inp);
710266f97b5SCy Schubert 	return (error);
711db0ac6deSCy Schubert #endif
712df8bae1dSRodney W. Grimes }
713df8bae1dSRodney W. Grimes 
71467107f45SBjoern A. Zeeb #ifdef INET
715df8bae1dSRodney W. Grimes int
716bbd0084bSMark Johnston in_pcbbind(struct inpcb *inp, struct sockaddr_in *sin, int flags,
717bbd0084bSMark Johnston     struct ucred *cred)
718df8bae1dSRodney W. Grimes {
7194b932371SIan Dowse 	int anonport, error;
7204b932371SIan Dowse 
72196871af0SGleb Smirnoff 	KASSERT(sin == NULL || sin->sin_family == AF_INET,
72296871af0SGleb Smirnoff 	    ("%s: invalid address family for %p", __func__, sin));
72396871af0SGleb Smirnoff 	KASSERT(sin == NULL || sin->sin_len == sizeof(struct sockaddr_in),
72496871af0SGleb Smirnoff 	    ("%s: invalid address length for %p", __func__, sin));
7258501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
726fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
72759daba27SSam Leffler 
7284b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
7294b932371SIan Dowse 		return (EINVAL);
73096871af0SGleb Smirnoff 	anonport = sin == NULL || sin->sin_port == 0;
73196871af0SGleb Smirnoff 	error = in_pcbbind_setup(inp, sin, &inp->inp_laddr.s_addr,
732bbd0084bSMark Johnston 	    &inp->inp_lport, flags, cred);
7334b932371SIan Dowse 	if (error)
7344b932371SIan Dowse 		return (error);
7354b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
7364b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
7374b932371SIan Dowse 		inp->inp_lport = 0;
738bbd0084bSMark Johnston 		inp->inp_flags &= ~INP_BOUNDFIB;
7394b932371SIan Dowse 		return (EAGAIN);
7404b932371SIan Dowse 	}
7414b932371SIan Dowse 	if (anonport)
7424b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
7434b932371SIan Dowse 	return (0);
7444b932371SIan Dowse }
74567107f45SBjoern A. Zeeb #endif
7464b932371SIan Dowse 
747efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
74825102351SMike Karels /*
74925102351SMike Karels  * Assign a local port like in_pcb_lport(), but also used with connect()
75025102351SMike Karels  * and a foreign address and port.  If fsa is non-NULL, choose a local port
75125102351SMike Karels  * that is unused with those, otherwise one that is completely unused.
7522fdbcbeaSMike Karels  * lsa can be NULL for IPv6.
75325102351SMike Karels  */
754efc76f72SBjoern A. Zeeb int
755bafe022bSGleb Smirnoff in_pcb_lport_dest(const struct inpcb *inp, struct sockaddr *lsa,
756bafe022bSGleb Smirnoff     u_short *lportp, struct sockaddr *fsa, u_short fport, struct ucred *cred,
757bafe022bSGleb Smirnoff     int lookupflags)
758efc76f72SBjoern A. Zeeb {
759efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
760efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
761efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
76219acc506SGleb Smirnoff 	int count, error;
763efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
764efc76f72SBjoern A. Zeeb #ifdef INET
76525102351SMike Karels 	struct in_addr laddr, faddr;
76625102351SMike Karels #endif
76725102351SMike Karels #ifdef INET6
76825102351SMike Karels 	struct in6_addr *laddr6, *faddr6;
769efc76f72SBjoern A. Zeeb #endif
770efc76f72SBjoern A. Zeeb 
771efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
772efc76f72SBjoern A. Zeeb 
773efc76f72SBjoern A. Zeeb 	/*
774efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
775efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
776efc76f72SBjoern A. Zeeb 	 */
777efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
778fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
779efc76f72SBjoern A. Zeeb 
780efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
781efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
782efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
783efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
784efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
785cc426dd3SMateusz Guzik 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT);
786efc76f72SBjoern A. Zeeb 		if (error)
787efc76f72SBjoern A. Zeeb 			return (error);
788efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
789efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
790efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
791efc76f72SBjoern A. Zeeb 	} else {
792efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
793efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
794efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
795efc76f72SBjoern A. Zeeb 	}
79619acc506SGleb Smirnoff 
797efc76f72SBjoern A. Zeeb 	/*
798efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
799efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
800efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
801efc76f72SBjoern A. Zeeb 	 */
802efc76f72SBjoern A. Zeeb 	if (first > last) {
803efc76f72SBjoern A. Zeeb 		aux = first;
804efc76f72SBjoern A. Zeeb 		first = last;
805efc76f72SBjoern A. Zeeb 		last = aux;
806efc76f72SBjoern A. Zeeb 	}
807efc76f72SBjoern A. Zeeb 
808efc76f72SBjoern A. Zeeb #ifdef INET
809637f317cSMike Karels 	laddr.s_addr = INADDR_ANY;	/* used by INET6+INET below too */
8104d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
8112fdbcbeaSMike Karels 		if (lsa != NULL)
81225102351SMike Karels 			laddr = ((struct sockaddr_in *)lsa)->sin_addr;
81325102351SMike Karels 		if (fsa != NULL)
81425102351SMike Karels 			faddr = ((struct sockaddr_in *)fsa)->sin_addr;
815efc76f72SBjoern A. Zeeb 	}
816efc76f72SBjoern A. Zeeb #endif
81725102351SMike Karels #ifdef INET6
8182fdbcbeaSMike Karels 	laddr6 = NULL;
8192fdbcbeaSMike Karels 	if ((inp->inp_vflag & INP_IPV6) != 0) {
8202fdbcbeaSMike Karels 		if (lsa != NULL)
82125102351SMike Karels 			laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr;
82225102351SMike Karels 		if (fsa != NULL)
82325102351SMike Karels 			faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr;
82425102351SMike Karels 	}
82525102351SMike Karels #endif
82625102351SMike Karels 
82725102351SMike Karels 	tmpinp = NULL;
828efc76f72SBjoern A. Zeeb 	lport = *lportp;
829efc76f72SBjoern A. Zeeb 
83019acc506SGleb Smirnoff 	if (V_ipport_randomized)
831efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
832efc76f72SBjoern A. Zeeb 
833efc76f72SBjoern A. Zeeb 	count = last - first;
834efc76f72SBjoern A. Zeeb 
835efc76f72SBjoern A. Zeeb 	do {
836efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
837efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
838efc76f72SBjoern A. Zeeb 		++*lastport;
839efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
840efc76f72SBjoern A. Zeeb 			*lastport = first;
841efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
842efc76f72SBjoern A. Zeeb 
84325102351SMike Karels 		if (fsa != NULL) {
84425102351SMike Karels #ifdef INET
84525102351SMike Karels 			if (lsa->sa_family == AF_INET) {
84625102351SMike Karels 				tmpinp = in_pcblookup_hash_locked(pcbinfo,
84725102351SMike Karels 				    faddr, fport, laddr, lport, lookupflags,
848da806e8dSMark Johnston 				    M_NODOM, RT_ALL_FIBS);
84925102351SMike Karels 			}
85025102351SMike Karels #endif
85125102351SMike Karels #ifdef INET6
85225102351SMike Karels 			if (lsa->sa_family == AF_INET6) {
85325102351SMike Karels 				tmpinp = in6_pcblookup_hash_locked(pcbinfo,
85425102351SMike Karels 				    faddr6, fport, laddr6, lport, lookupflags,
855da806e8dSMark Johnston 				    M_NODOM, RT_ALL_FIBS);
85625102351SMike Karels 			}
85725102351SMike Karels #endif
85825102351SMike Karels 		} else {
859efc76f72SBjoern A. Zeeb #ifdef INET6
860637f317cSMike Karels 			if ((inp->inp_vflag & INP_IPV6) != 0) {
861efc76f72SBjoern A. Zeeb 				tmpinp = in6_pcblookup_local(pcbinfo,
8629a413162SMark Johnston 				    &inp->in6p_laddr, lport, RT_ALL_FIBS,
8639a413162SMark Johnston 				    lookupflags, cred);
864637f317cSMike Karels #ifdef INET
865637f317cSMike Karels 				if (tmpinp == NULL &&
866637f317cSMike Karels 				    (inp->inp_vflag & INP_IPV4))
867637f317cSMike Karels 					tmpinp = in_pcblookup_local(pcbinfo,
8689a413162SMark Johnston 					    laddr, lport, RT_ALL_FIBS,
8699a413162SMark Johnston 					    lookupflags, cred);
870637f317cSMike Karels #endif
871637f317cSMike Karels 			}
872efc76f72SBjoern A. Zeeb #endif
873efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
874efc76f72SBjoern A. Zeeb 			else
875efc76f72SBjoern A. Zeeb #endif
876efc76f72SBjoern A. Zeeb #ifdef INET
877efc76f72SBjoern A. Zeeb 				tmpinp = in_pcblookup_local(pcbinfo, laddr,
8789a413162SMark Johnston 				    lport, RT_ALL_FIBS, lookupflags, cred);
879efc76f72SBjoern A. Zeeb #endif
88025102351SMike Karels 		}
881efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
882efc76f72SBjoern A. Zeeb 
883efc76f72SBjoern A. Zeeb 	*lportp = lport;
884efc76f72SBjoern A. Zeeb 
885efc76f72SBjoern A. Zeeb 	return (0);
886efc76f72SBjoern A. Zeeb }
887efdf104bSMikolaj Golub 
888efdf104bSMikolaj Golub /*
88925102351SMike Karels  * Select a local port (number) to use.
89025102351SMike Karels  */
89125102351SMike Karels int
89225102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
89325102351SMike Karels     struct ucred *cred, int lookupflags)
89425102351SMike Karels {
89525102351SMike Karels 	struct sockaddr_in laddr;
89625102351SMike Karels 
89725102351SMike Karels 	if (laddrp) {
89825102351SMike Karels 		bzero(&laddr, sizeof(laddr));
89925102351SMike Karels 		laddr.sin_family = AF_INET;
90025102351SMike Karels 		laddr.sin_addr = *laddrp;
90125102351SMike Karels 	}
90225102351SMike Karels 	return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr :
90325102351SMike Karels 	    NULL, lportp, NULL, 0, cred, lookupflags));
90425102351SMike Karels }
905efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
906efc76f72SBjoern A. Zeeb 
907d5bb8bd3SAdrian Chadd #ifdef INET
9080a100a6fSAdrian Chadd /*
90901f8ce83SMark Johnston  * Determine whether the inpcb can be bound to the specified address/port tuple.
91001f8ce83SMark Johnston  */
91101f8ce83SMark Johnston static int
91201f8ce83SMark Johnston in_pcbbind_avail(struct inpcb *inp, const struct in_addr laddr,
913bbd0084bSMark Johnston     const u_short lport, const int fib, int sooptions, int lookupflags,
914bbd0084bSMark Johnston     struct ucred *cred)
91501f8ce83SMark Johnston {
91601f8ce83SMark Johnston 	int reuseport, reuseport_lb;
91701f8ce83SMark Johnston 
91801f8ce83SMark Johnston 	INP_LOCK_ASSERT(inp);
91901f8ce83SMark Johnston 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
92001f8ce83SMark Johnston 
92101f8ce83SMark Johnston 	reuseport = (sooptions & SO_REUSEPORT);
92201f8ce83SMark Johnston 	reuseport_lb = (sooptions & SO_REUSEPORT_LB);
92301f8ce83SMark Johnston 
92401f8ce83SMark Johnston 	if (IN_MULTICAST(ntohl(laddr.s_addr))) {
92501f8ce83SMark Johnston 		/*
92601f8ce83SMark Johnston 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
92701f8ce83SMark Johnston 		 * allow complete duplication of binding if
92801f8ce83SMark Johnston 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
92901f8ce83SMark Johnston 		 * and a multicast address is bound on both
93001f8ce83SMark Johnston 		 * new and duplicated sockets.
93101f8ce83SMark Johnston 		 */
93201f8ce83SMark Johnston 		if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0)
93301f8ce83SMark Johnston 			reuseport = SO_REUSEADDR | SO_REUSEPORT;
93401f8ce83SMark Johnston 		/*
93501f8ce83SMark Johnston 		 * XXX: How to deal with SO_REUSEPORT_LB here?
93601f8ce83SMark Johnston 		 * Treat same as SO_REUSEPORT for now.
93701f8ce83SMark Johnston 		 */
93801f8ce83SMark Johnston 		if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0)
93901f8ce83SMark Johnston 			reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB;
94001f8ce83SMark Johnston 	} else if (!in_nullhost(laddr)) {
94101f8ce83SMark Johnston 		struct sockaddr_in sin;
94201f8ce83SMark Johnston 
94301f8ce83SMark Johnston 		memset(&sin, 0, sizeof(sin));
94401f8ce83SMark Johnston 		sin.sin_family = AF_INET;
94501f8ce83SMark Johnston 		sin.sin_len = sizeof(sin);
94601f8ce83SMark Johnston 		sin.sin_addr = laddr;
94701f8ce83SMark Johnston 
94801f8ce83SMark Johnston 		/*
94901f8ce83SMark Johnston 		 * Is the address a local IP address?
95001f8ce83SMark Johnston 		 * If INP_BINDANY is set, then the socket may be bound
95101f8ce83SMark Johnston 		 * to any endpoint address, local or not.
95201f8ce83SMark Johnston 		 */
95301f8ce83SMark Johnston 		if ((inp->inp_flags & INP_BINDANY) == 0 &&
95401f8ce83SMark Johnston 		    ifa_ifwithaddr_check((const struct sockaddr *)&sin) == 0)
95501f8ce83SMark Johnston 			return (EADDRNOTAVAIL);
95601f8ce83SMark Johnston 	}
95701f8ce83SMark Johnston 
95801f8ce83SMark Johnston 	if (lport != 0) {
95901f8ce83SMark Johnston 		struct inpcb *t;
96001f8ce83SMark Johnston 
96101f8ce83SMark Johnston 		if (ntohs(lport) <= V_ipport_reservedhigh &&
96201f8ce83SMark Johnston 		    ntohs(lport) >= V_ipport_reservedlow &&
96301f8ce83SMark Johnston 		    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
96401f8ce83SMark Johnston 			return (EACCES);
96501f8ce83SMark Johnston 
96601f8ce83SMark Johnston 		if (!IN_MULTICAST(ntohl(laddr.s_addr)) &&
96701f8ce83SMark Johnston 		    priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
968c9756953SMark Johnston 			/*
969c9756953SMark Johnston 			 * If a socket owned by a different user is already
970c9756953SMark Johnston 			 * bound to this port, fail.  In particular, SO_REUSE*
971c9756953SMark Johnston 			 * can only be used to share a port among sockets owned
972c9756953SMark Johnston 			 * by the same user.
973c9756953SMark Johnston 			 *
974c9756953SMark Johnston 			 * However, we can share a port with a connected socket
975c9756953SMark Johnston 			 * which has a unique 4-tuple.
976c9756953SMark Johnston 			 */
97701f8ce83SMark Johnston 			t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport,
9789a413162SMark Johnston 			    RT_ALL_FIBS, INPLOOKUP_WILDCARD, cred);
97901f8ce83SMark Johnston 			if (t != NULL &&
98001f8ce83SMark Johnston 			    (inp->inp_socket->so_type != SOCK_STREAM ||
98101f8ce83SMark Johnston 			     in_nullhost(t->inp_faddr)) &&
98201f8ce83SMark Johnston 			    (inp->inp_cred->cr_uid != t->inp_cred->cr_uid))
98301f8ce83SMark Johnston 				return (EADDRINUSE);
98401f8ce83SMark Johnston 		}
985bbd0084bSMark Johnston 		t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport, fib,
986bbd0084bSMark Johnston 		    lookupflags, cred);
98701f8ce83SMark Johnston 		if (t != NULL && ((reuseport | reuseport_lb) &
98801f8ce83SMark Johnston 		    t->inp_socket->so_options) == 0) {
98901f8ce83SMark Johnston #ifdef INET6
99001f8ce83SMark Johnston 			if (!in_nullhost(laddr) ||
99101f8ce83SMark Johnston 			    !in_nullhost(t->inp_laddr) ||
99201f8ce83SMark Johnston 			    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
99301f8ce83SMark Johnston 			    (t->inp_vflag & INP_IPV6PROTO) == 0)
99401f8ce83SMark Johnston #endif
99501f8ce83SMark Johnston 				return (EADDRINUSE);
99601f8ce83SMark Johnston 		}
99701f8ce83SMark Johnston 	}
99801f8ce83SMark Johnston 	return (0);
99901f8ce83SMark Johnston }
100001f8ce83SMark Johnston 
100101f8ce83SMark Johnston /*
10024b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
10034b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
10044b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
10054b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
10064b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
10074b932371SIan Dowse  *
10084b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
10094b932371SIan Dowse  */
10104b932371SIan Dowse int
101196871af0SGleb Smirnoff in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp,
1012bbd0084bSMark Johnston     u_short *lportp, int flags, struct ucred *cred)
10134b932371SIan Dowse {
10144b932371SIan Dowse 	struct socket *so = inp->inp_socket;
10154b932371SIan Dowse 	struct in_addr laddr;
1016df8bae1dSRodney W. Grimes 	u_short lport = 0;
1017bbd0084bSMark Johnston 	int error, fib, lookupflags, sooptions;
1018df8bae1dSRodney W. Grimes 
10198501a69cSRobert Watson 	/*
1020fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
10218501a69cSRobert Watson 	 */
102259daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
1023ffb3d384SMark Johnston 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
102459daba27SSam Leffler 
10254b932371SIan Dowse 	laddr.s_addr = *laddrp;
102696871af0SGleb Smirnoff 	if (sin != NULL && laddr.s_addr != INADDR_ANY)
1027df8bae1dSRodney W. Grimes 		return (EINVAL);
102801f8ce83SMark Johnston 
102901f8ce83SMark Johnston 	lookupflags = 0;
103001f8ce83SMark Johnston 	sooptions = atomic_load_int(&so->so_options);
103101f8ce83SMark Johnston 	if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0)
103268e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
103396871af0SGleb Smirnoff 	if (sin == NULL) {
10347c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
10357c2f3cb9SJamie Gritton 			return (error);
10367c2f3cb9SJamie Gritton 	} else {
1037f161d294SMark Johnston 		KASSERT(sin->sin_family == AF_INET,
1038f161d294SMark Johnston 		    ("%s: invalid family for address %p", __func__, sin));
1039f161d294SMark Johnston 		KASSERT(sin->sin_len == sizeof(*sin),
1040f161d294SMark Johnston 		    ("%s: invalid length for address %p", __func__, sin));
1041f161d294SMark Johnston 
1042b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
1043b89e82ddSJamie Gritton 		if (error)
1044b89e82ddSJamie Gritton 			return (error);
10454b932371SIan Dowse 		if (sin->sin_port != *lportp) {
10464b932371SIan Dowse 			/* Don't allow the port to change. */
10474b932371SIan Dowse 			if (*lportp != 0)
10484b932371SIan Dowse 				return (EINVAL);
1049df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
10504b932371SIan Dowse 		}
105145a77bf2SMark Johnston 		laddr = sin->sin_addr;
105245a77bf2SMark Johnston 
1053bbd0084bSMark Johnston 		fib = (flags & INPBIND_FIB) != 0 ? inp->inp_inc.inc_fibnum :
1054bbd0084bSMark Johnston 		    RT_ALL_FIBS;
1055bbd0084bSMark Johnston 
105601f8ce83SMark Johnston 		/* See if this address/port combo is available. */
1057bbd0084bSMark Johnston 		error = in_pcbbind_avail(inp, laddr, lport, fib, sooptions,
105845a77bf2SMark Johnston 		    lookupflags, cred);
105901f8ce83SMark Johnston 		if (error != 0)
106001f8ce83SMark Johnston 			return (error);
1061df8bae1dSRodney W. Grimes 	}
10624b932371SIan Dowse 	if (*lportp != 0)
10634b932371SIan Dowse 		lport = *lportp;
106433b3ac06SPeter Wemm 	if (lport == 0) {
10654616026fSErmal Luçi 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
1066efc76f72SBjoern A. Zeeb 		if (error != 0)
1067efc76f72SBjoern A. Zeeb 			return (error);
106833b3ac06SPeter Wemm 	}
10694b932371SIan Dowse 	*laddrp = laddr.s_addr;
10704b932371SIan Dowse 	*lportp = lport;
1071bbd0084bSMark Johnston 	if ((flags & INPBIND_FIB) != 0)
1072bbd0084bSMark Johnston 		inp->inp_flags |= INP_BOUNDFIB;
1073df8bae1dSRodney W. Grimes 	return (0);
1074df8bae1dSRodney W. Grimes }
1075df8bae1dSRodney W. Grimes 
1076999f1343SGarrett Wollman /*
10775200e00eSIan Dowse  * Connect from a socket to a specified address.
10785200e00eSIan Dowse  * Both address and port must be specified in argument sin.
10795200e00eSIan Dowse  * If don't have a local address for this socket yet,
10805200e00eSIan Dowse  * then pick one.
1081999f1343SGarrett Wollman  */
1082999f1343SGarrett Wollman int
10830b4539eeSGleb Smirnoff in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred)
1084999f1343SGarrett Wollman {
10852af953b1SGleb Smirnoff 	struct in_addr laddr, faddr;
10862af953b1SGleb Smirnoff 	u_short lport;
10872af953b1SGleb Smirnoff 	int error;
10882af953b1SGleb Smirnoff 	bool anonport;
1089df8bae1dSRodney W. Grimes 
10908501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1091fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1092fdb987beSMark Johnston 	KASSERT(in_nullhost(inp->inp_faddr),
1093fdb987beSMark Johnston 	    ("%s: inp is already connected", __func__));
10942af953b1SGleb Smirnoff 	KASSERT(sin->sin_family == AF_INET,
10952af953b1SGleb Smirnoff 	    ("%s: invalid address family for %p", __func__, sin));
10962af953b1SGleb Smirnoff 	KASSERT(sin->sin_len == sizeof(*sin),
10972af953b1SGleb Smirnoff 	    ("%s: invalid address length for %p", __func__, sin));
109827f74fd0SRobert Watson 
10992af953b1SGleb Smirnoff 	if (sin->sin_port == 0)
11002af953b1SGleb Smirnoff 		return (EADDRNOTAVAIL);
11012af953b1SGleb Smirnoff 
11022af953b1SGleb Smirnoff 	anonport = (inp->inp_lport == 0);
11032af953b1SGleb Smirnoff 
11042af953b1SGleb Smirnoff 	if (__predict_false(in_broadcast(sin->sin_addr))) {
11052af953b1SGleb Smirnoff 		if (!V_connect_inaddr_wild || CK_STAILQ_EMPTY(&V_in_ifaddrhead))
11062af953b1SGleb Smirnoff 			return (ENETUNREACH);
11072af953b1SGleb Smirnoff 		/*
11082af953b1SGleb Smirnoff 		 * If the destination address is INADDR_ANY, use the primary
11092af953b1SGleb Smirnoff 		 * local address.  If the supplied address is INADDR_BROADCAST,
11102af953b1SGleb Smirnoff 		 * and the primary interface supports broadcast, choose the
11112af953b1SGleb Smirnoff 		 * broadcast address for that interface.
11122af953b1SGleb Smirnoff 		 */
11132af953b1SGleb Smirnoff 		if (in_nullhost(sin->sin_addr)) {
11142af953b1SGleb Smirnoff 			faddr =
11152af953b1SGleb Smirnoff 			    IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
11162af953b1SGleb Smirnoff 			if ((error = prison_get_ip4(cred, &faddr)) != 0)
11172af953b1SGleb Smirnoff 				return (error);
11182af953b1SGleb Smirnoff 		} else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
11192af953b1SGleb Smirnoff 			if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags
11202af953b1SGleb Smirnoff 			    & IFF_BROADCAST)
11212af953b1SGleb Smirnoff 				faddr = satosin(&CK_STAILQ_FIRST(
11222af953b1SGleb Smirnoff 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
11232af953b1SGleb Smirnoff 			else
11242af953b1SGleb Smirnoff 				faddr = sin->sin_addr;
11252af953b1SGleb Smirnoff 		}
11262af953b1SGleb Smirnoff 	} else
11272af953b1SGleb Smirnoff 		faddr = sin->sin_addr;
11282af953b1SGleb Smirnoff 
11292af953b1SGleb Smirnoff 	if (in_nullhost(inp->inp_laddr)) {
11302af953b1SGleb Smirnoff 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
11312af953b1SGleb Smirnoff 		/*
11322af953b1SGleb Smirnoff 		 * If the destination address is multicast and an outgoing
11332af953b1SGleb Smirnoff 		 * interface has been set as a multicast option, prefer the
11342af953b1SGleb Smirnoff 		 * address of that interface as our source address.
11352af953b1SGleb Smirnoff 		 */
11362af953b1SGleb Smirnoff 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
11372af953b1SGleb Smirnoff 		    inp->inp_moptions != NULL &&
11382af953b1SGleb Smirnoff 		    inp->inp_moptions->imo_multicast_ifp != NULL) {
11392af953b1SGleb Smirnoff 			struct ifnet *ifp =
11402af953b1SGleb Smirnoff 			    inp->inp_moptions->imo_multicast_ifp;
11412af953b1SGleb Smirnoff 			struct in_ifaddr *ia;
11422af953b1SGleb Smirnoff 
11432af953b1SGleb Smirnoff 			CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
11442af953b1SGleb Smirnoff 				if (ia->ia_ifp == ifp &&
11452af953b1SGleb Smirnoff 				    prison_check_ip4(cred,
11462af953b1SGleb Smirnoff 				    &ia->ia_addr.sin_addr) == 0)
11472af953b1SGleb Smirnoff 					break;
11482af953b1SGleb Smirnoff 			}
11492af953b1SGleb Smirnoff 			if (ia == NULL)
11502af953b1SGleb Smirnoff 				return (EADDRNOTAVAIL);
11512af953b1SGleb Smirnoff 			laddr = ia->ia_addr.sin_addr;
11522af953b1SGleb Smirnoff 			error = 0;
11532af953b1SGleb Smirnoff 		}
11545200e00eSIan Dowse 		if (error)
11555200e00eSIan Dowse 			return (error);
11562af953b1SGleb Smirnoff 	} else
11572af953b1SGleb Smirnoff 		laddr = inp->inp_laddr;
11585200e00eSIan Dowse 
11592af953b1SGleb Smirnoff 	if (anonport) {
11602af953b1SGleb Smirnoff 		struct sockaddr_in lsin = {
11612af953b1SGleb Smirnoff 			.sin_family = AF_INET,
11622af953b1SGleb Smirnoff 			.sin_addr = laddr,
11632af953b1SGleb Smirnoff 		};
11642af953b1SGleb Smirnoff 		struct sockaddr_in fsin = {
11652af953b1SGleb Smirnoff 			.sin_family = AF_INET,
11662af953b1SGleb Smirnoff 			.sin_addr = faddr,
11672af953b1SGleb Smirnoff 		};
11682af953b1SGleb Smirnoff 
11692af953b1SGleb Smirnoff 		error = in_pcb_lport_dest(inp, (struct sockaddr *)&lsin,
11702af953b1SGleb Smirnoff 		    &lport, (struct sockaddr *)&fsin, sin->sin_port, cred,
11712af953b1SGleb Smirnoff 		    INPLOOKUP_WILDCARD);
11722af953b1SGleb Smirnoff 		if (error)
11732af953b1SGleb Smirnoff 			return (error);
11742af953b1SGleb Smirnoff 	} else if (in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr,
11752af953b1SGleb Smirnoff 	    sin->sin_port, laddr, inp->inp_lport, 0, M_NODOM, RT_ALL_FIBS) !=
11762af953b1SGleb Smirnoff 	    NULL)
11772af953b1SGleb Smirnoff 		return (EADDRINUSE);
11782af953b1SGleb Smirnoff 	else
11792af953b1SGleb Smirnoff 		lport = inp->inp_lport;
11802af953b1SGleb Smirnoff 
11812af953b1SGleb Smirnoff 	inp->inp_faddr = faddr;
11822af953b1SGleb Smirnoff 	inp->inp_fport = sin->sin_port;
1183fdb987beSMark Johnston 
11845200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
11855200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
11865200e00eSIan Dowse 		inp->inp_lport = lport;
11872af953b1SGleb Smirnoff 		inp->inp_laddr = laddr;
11885200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
1189fdb987beSMark Johnston 			inp->inp_laddr.s_addr = inp->inp_faddr.s_addr =
1190fdb987beSMark Johnston 			    INADDR_ANY;
1191fdb987beSMark Johnston 			inp->inp_lport = inp->inp_fport = 0;
11925200e00eSIan Dowse 			return (EAGAIN);
11935200e00eSIan Dowse 		}
1194fdb987beSMark Johnston 	} else {
11955200e00eSIan Dowse 		inp->inp_lport = lport;
11962af953b1SGleb Smirnoff 		inp->inp_laddr = laddr;
1197fdb987beSMark Johnston 		if ((inp->inp_flags & INP_INHASHLIST) != 0)
1198db0ac6deSCy Schubert 			in_pcbrehash(inp);
1199fdb987beSMark Johnston 		else
1200db0ac6deSCy Schubert 			in_pcbinshash(inp);
1201fe1274eeSMichael Tuexen 	}
120224e5c2eeSGleb Smirnoff #ifdef ROUTE_MPATH
120324e5c2eeSGleb Smirnoff 	if (CALC_FLOWID_OUTBOUND) {
120424e5c2eeSGleb Smirnoff 		uint32_t hash_val, hash_type;
12052cb64cb2SGeorge V. Neville-Neil 
120624e5c2eeSGleb Smirnoff 		hash_val = fib4_calc_software_hash(inp->inp_laddr,
12072af953b1SGleb Smirnoff 		    inp->inp_faddr, 0, sin->sin_port,
120824e5c2eeSGleb Smirnoff 		    inp->inp_socket->so_proto->pr_protocol, &hash_type);
120924e5c2eeSGleb Smirnoff 
121024e5c2eeSGleb Smirnoff 		inp->inp_flowid = hash_val;
121124e5c2eeSGleb Smirnoff 		inp->inp_flowtype = hash_type;
121224e5c2eeSGleb Smirnoff 	}
121324e5c2eeSGleb Smirnoff #endif
12145200e00eSIan Dowse 	if (anonport)
12155200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
12165200e00eSIan Dowse 	return (0);
12175200e00eSIan Dowse }
12185200e00eSIan Dowse 
12195200e00eSIan Dowse /*
12200895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
12210895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
12220895aec3SBjoern A. Zeeb  */
1223ae190832SSteven Hartland int
1224bafe022bSGleb Smirnoff in_pcbladdr(const struct inpcb *inp, struct in_addr *faddr,
1225bafe022bSGleb Smirnoff     struct in_addr *laddr, struct ucred *cred)
12260895aec3SBjoern A. Zeeb {
12270895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
12280895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
12299ac7c6cfSAlexander V. Chernikov 	struct sockaddr_in *sin, dst;
12309ac7c6cfSAlexander V. Chernikov 	struct nhop_object *nh;
12310895aec3SBjoern A. Zeeb 	int error;
12320895aec3SBjoern A. Zeeb 
1233c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
1234413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
1235ac1750ddSMark Johnston 
1236592bcae8SBjoern A. Zeeb 	/*
1237592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
1238592bcae8SBjoern A. Zeeb 	 * if requested.
1239592bcae8SBjoern A. Zeeb 	 */
1240ac1750ddSMark Johnston 	if (!prison_saddrsel_ip4(cred, laddr))
1241592bcae8SBjoern A. Zeeb 		return (0);
1242592bcae8SBjoern A. Zeeb 
12430895aec3SBjoern A. Zeeb 	error = 0;
12440895aec3SBjoern A. Zeeb 
12459ac7c6cfSAlexander V. Chernikov 	nh = NULL;
12469ac7c6cfSAlexander V. Chernikov 	bzero(&dst, sizeof(dst));
12479ac7c6cfSAlexander V. Chernikov 	sin = &dst;
12480895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
12490895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
12500895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
12510895aec3SBjoern A. Zeeb 
12520895aec3SBjoern A. Zeeb 	/*
12530895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
12540895aec3SBjoern A. Zeeb 	 * else punt.
12550895aec3SBjoern A. Zeeb 	 *
12560895aec3SBjoern A. Zeeb 	 * Find out route to destination.
12570895aec3SBjoern A. Zeeb 	 */
12580895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
12599ac7c6cfSAlexander V. Chernikov 		nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr,
12609ac7c6cfSAlexander V. Chernikov 		    0, NHR_NONE, 0);
12610895aec3SBjoern A. Zeeb 
12620895aec3SBjoern A. Zeeb 	/*
12630895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
12640895aec3SBjoern A. Zeeb 	 * the outgoing interface.
12650895aec3SBjoern A. Zeeb 	 *
12660895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
12670895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
12680895aec3SBjoern A. Zeeb 	 * the source address from.
12690895aec3SBjoern A. Zeeb 	 */
12709ac7c6cfSAlexander V. Chernikov 	if (nh == NULL || nh->nh_ifp == NULL) {
12718c0fec80SRobert Watson 		struct in_ifaddr *ia;
12720895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
12730895aec3SBjoern A. Zeeb 
12744f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
127558a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
12764f6c66ccSMatt Macy 		if (ia == NULL) {
12774f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
127858a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
12794f6c66ccSMatt Macy 		}
12800895aec3SBjoern A. Zeeb 		if (ia == NULL) {
12810895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
12820895aec3SBjoern A. Zeeb 			goto done;
12830895aec3SBjoern A. Zeeb 		}
12840895aec3SBjoern A. Zeeb 
1285ac1750ddSMark Johnston 		if (!prison_flag(cred, PR_IP4)) {
12860895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
12870895aec3SBjoern A. Zeeb 			goto done;
12880895aec3SBjoern A. Zeeb 		}
12890895aec3SBjoern A. Zeeb 
12900895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
12910895aec3SBjoern A. Zeeb 		ia = NULL;
1292d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
12930895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
12940895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
12950895aec3SBjoern A. Zeeb 				continue;
12960895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
1297b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
12980895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
12990895aec3SBjoern A. Zeeb 				break;
13000895aec3SBjoern A. Zeeb 			}
13010895aec3SBjoern A. Zeeb 		}
13020895aec3SBjoern A. Zeeb 		if (ia != NULL) {
13030895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
13040895aec3SBjoern A. Zeeb 			goto done;
13050895aec3SBjoern A. Zeeb 		}
13060895aec3SBjoern A. Zeeb 
13070895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1308b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
13090895aec3SBjoern A. Zeeb 		goto done;
13100895aec3SBjoern A. Zeeb 	}
13110895aec3SBjoern A. Zeeb 
13120895aec3SBjoern A. Zeeb 	/*
13130895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
13140895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
13150895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
13160895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
13170895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
13180895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
13190895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
13200895aec3SBjoern A. Zeeb 	 */
13219ac7c6cfSAlexander V. Chernikov 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) {
13228c0fec80SRobert Watson 		struct in_ifaddr *ia;
13239317b04eSRobert Watson 		struct ifnet *ifp;
13240895aec3SBjoern A. Zeeb 
13250895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
1326ac1750ddSMark Johnston 		if (!prison_flag(cred, PR_IP4)) {
13279ac7c6cfSAlexander V. Chernikov 			ia = (struct in_ifaddr *)nh->nh_ifa;
13280895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
13290895aec3SBjoern A. Zeeb 			goto done;
13300895aec3SBjoern A. Zeeb 		}
13310895aec3SBjoern A. Zeeb 
13320895aec3SBjoern A. Zeeb 		/* Jailed. */
13330895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
13349ac7c6cfSAlexander V. Chernikov 		sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
1335b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
13369ac7c6cfSAlexander V. Chernikov 			ia = (struct in_ifaddr *)nh->nh_ifa;
13370895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
13380895aec3SBjoern A. Zeeb 			goto done;
13390895aec3SBjoern A. Zeeb 		}
13400895aec3SBjoern A. Zeeb 
13410895aec3SBjoern A. Zeeb 		/*
13420895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
13430895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
13440895aec3SBjoern A. Zeeb 		 */
13458c0fec80SRobert Watson 		ia = NULL;
13469ac7c6cfSAlexander V. Chernikov 		ifp = nh->nh_ifp;
1347d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
13480895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
13490895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
13500895aec3SBjoern A. Zeeb 				continue;
13510895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
1352b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
13530895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
13540895aec3SBjoern A. Zeeb 				break;
13550895aec3SBjoern A. Zeeb 			}
13560895aec3SBjoern A. Zeeb 		}
13570895aec3SBjoern A. Zeeb 		if (ia != NULL) {
13580895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
13590895aec3SBjoern A. Zeeb 			goto done;
13600895aec3SBjoern A. Zeeb 		}
13610895aec3SBjoern A. Zeeb 
13620895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1363b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
13640895aec3SBjoern A. Zeeb 		goto done;
13650895aec3SBjoern A. Zeeb 	}
13660895aec3SBjoern A. Zeeb 
13670895aec3SBjoern A. Zeeb 	/*
13680895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
13690895aec3SBjoern A. Zeeb 	 * to ourselves is here.
13700895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
13710895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
13720895aec3SBjoern A. Zeeb 	 * a loopback interface.
13730895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
13740895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
13750895aec3SBjoern A. Zeeb 	 */
13769ac7c6cfSAlexander V. Chernikov 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) {
13778c0fec80SRobert Watson 		struct in_ifaddr *ia;
13780895aec3SBjoern A. Zeeb 
13799ac7c6cfSAlexander V. Chernikov 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst),
138058a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
13810895aec3SBjoern A. Zeeb 		if (ia == NULL)
13829ac7c6cfSAlexander V. Chernikov 			ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0,
138358a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
1384f0bb05fcSQing Li 		if (ia == NULL)
13859ac7c6cfSAlexander V. Chernikov 			ia = ifatoia(ifa_ifwithaddr(sintosa(&dst)));
13860895aec3SBjoern A. Zeeb 
1387ac1750ddSMark Johnston 		if (!prison_flag(cred, PR_IP4)) {
13880895aec3SBjoern A. Zeeb 			if (ia == NULL) {
13890895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
13900895aec3SBjoern A. Zeeb 				goto done;
13910895aec3SBjoern A. Zeeb 			}
13920895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
13930895aec3SBjoern A. Zeeb 			goto done;
13940895aec3SBjoern A. Zeeb 		}
13950895aec3SBjoern A. Zeeb 
13960895aec3SBjoern A. Zeeb 		/* Jailed. */
13970895aec3SBjoern A. Zeeb 		if (ia != NULL) {
13980895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
13990895aec3SBjoern A. Zeeb 
14000895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
14010895aec3SBjoern A. Zeeb 			ia = NULL;
1402d7c5a620SMatt Macy 			CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
14030895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
14040895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
14050895aec3SBjoern A. Zeeb 					continue;
14060895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
1407b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
1408b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
14090895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
14100895aec3SBjoern A. Zeeb 					break;
14110895aec3SBjoern A. Zeeb 				}
14120895aec3SBjoern A. Zeeb 			}
14130895aec3SBjoern A. Zeeb 			if (ia != NULL) {
14140895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
14150895aec3SBjoern A. Zeeb 				goto done;
14160895aec3SBjoern A. Zeeb 			}
14170895aec3SBjoern A. Zeeb 		}
14180895aec3SBjoern A. Zeeb 
14190895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1420b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
14210895aec3SBjoern A. Zeeb 		goto done;
14220895aec3SBjoern A. Zeeb 	}
14230895aec3SBjoern A. Zeeb 
14240895aec3SBjoern A. Zeeb done:
1425713264f6SMark Johnston 	if (error == 0 && laddr->s_addr == INADDR_ANY)
1426713264f6SMark Johnston 		return (EHOSTUNREACH);
14270895aec3SBjoern A. Zeeb 	return (error);
14280895aec3SBjoern A. Zeeb }
14290895aec3SBjoern A. Zeeb 
143026f9a767SRodney W. Grimes void
1431136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1432df8bae1dSRodney W. Grimes {
14336b348152SRobert Watson 
14348501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1435fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1436fdb987beSMark Johnston 	KASSERT(inp->inp_smr == SMR_SEQ_INVALID,
1437fdb987beSMark Johnston 	    ("%s: inp %p was already disconnected", __func__, inp));
1438df8bae1dSRodney W. Grimes 
1439fdb987beSMark Johnston 	in_pcbremhash_locked(inp);
1440fdb987beSMark Johnston 
1441fdb987beSMark Johnston 	/* See the comment in in_pcbinshash(). */
1442fdb987beSMark Johnston 	inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr);
14432589ec0fSMark Johnston 	inp->inp_laddr.s_addr = INADDR_ANY;
1444df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1445df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
1446df8bae1dSRodney W. Grimes }
144783e521ecSBjoern A. Zeeb #endif /* INET */
1448df8bae1dSRodney W. Grimes 
14497cbb6b6eSMark Johnston void
14507cbb6b6eSMark Johnston in_pcblisten(struct inpcb *inp)
14517cbb6b6eSMark Johnston {
14527cbb6b6eSMark Johnston 	struct inpcblbgroup *grp;
14537cbb6b6eSMark Johnston 
14547cbb6b6eSMark Johnston 	INP_WLOCK_ASSERT(inp);
14557cbb6b6eSMark Johnston 
14567cbb6b6eSMark Johnston 	if ((inp->inp_flags & INP_INLBGROUP) != 0) {
14577cbb6b6eSMark Johnston 		struct inpcbinfo *pcbinfo;
14587cbb6b6eSMark Johnston 
14597cbb6b6eSMark Johnston 		pcbinfo = inp->inp_pcbinfo;
14607cbb6b6eSMark Johnston 		INP_HASH_WLOCK(pcbinfo);
14617cbb6b6eSMark Johnston 		grp = in_pcblbgroup_find(inp);
14627cbb6b6eSMark Johnston 		LIST_REMOVE(inp, inp_lbgroup_list);
14638b3d2c19SMark Johnston 		grp->il_pendcnt--;
14647cbb6b6eSMark Johnston 		in_pcblbgroup_insert(grp, inp);
14657cbb6b6eSMark Johnston 		INP_HASH_WUNLOCK(pcbinfo);
14667cbb6b6eSMark Johnston 	}
14677cbb6b6eSMark Johnston }
14687cbb6b6eSMark Johnston 
14694c7c478dSRobert Watson /*
1470db0ac6deSCy Schubert  * inpcb hash lookups are protected by SMR section.
1471db0ac6deSCy Schubert  *
1472db0ac6deSCy Schubert  * Once desired pcb has been found, switching from SMR section to a pcb
1473db0ac6deSCy Schubert  * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK
1474db0ac6deSCy Schubert  * here because SMR is a critical section.
1475db0ac6deSCy Schubert  * In 99%+ cases inp_smr_lock() would obtain the lock immediately.
1476db0ac6deSCy Schubert  */
14777b92493aSMark Johnston void
1478db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock)
1479db0ac6deSCy Schubert {
1480db0ac6deSCy Schubert 
1481db0ac6deSCy Schubert 	lock == INPLOOKUP_RLOCKPCB ?
1482db0ac6deSCy Schubert 	    rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock);
1483db0ac6deSCy Schubert }
1484db0ac6deSCy Schubert 
14857b92493aSMark Johnston void
1486db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock)
1487db0ac6deSCy Schubert {
1488db0ac6deSCy Schubert 
1489db0ac6deSCy Schubert 	lock == INPLOOKUP_RLOCKPCB ?
1490db0ac6deSCy Schubert 	    rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock);
1491db0ac6deSCy Schubert }
1492db0ac6deSCy Schubert 
14937b92493aSMark Johnston int
1494db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock)
1495db0ac6deSCy Schubert {
1496db0ac6deSCy Schubert 
1497db0ac6deSCy Schubert 	return (lock == INPLOOKUP_RLOCKPCB ?
1498db0ac6deSCy Schubert 	    rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock));
1499db0ac6deSCy Schubert }
1500db0ac6deSCy Schubert 
1501db0ac6deSCy Schubert static inline bool
1502f567d55fSGleb Smirnoff _inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock, const int ignflags)
1503db0ac6deSCy Schubert {
1504db0ac6deSCy Schubert 
1505db0ac6deSCy Schubert 	MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB);
1506db0ac6deSCy Schubert 	SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr);
1507db0ac6deSCy Schubert 
1508db0ac6deSCy Schubert 	if (__predict_true(inp_trylock(inp, lock))) {
1509f567d55fSGleb Smirnoff 		if (__predict_false(inp->inp_flags & ignflags)) {
1510db0ac6deSCy Schubert 			smr_exit(inp->inp_pcbinfo->ipi_smr);
1511db0ac6deSCy Schubert 			inp_unlock(inp, lock);
1512db0ac6deSCy Schubert 			return (false);
1513db0ac6deSCy Schubert 		}
1514db0ac6deSCy Schubert 		smr_exit(inp->inp_pcbinfo->ipi_smr);
1515db0ac6deSCy Schubert 		return (true);
1516db0ac6deSCy Schubert 	}
1517db0ac6deSCy Schubert 
1518db0ac6deSCy Schubert 	if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) {
1519db0ac6deSCy Schubert 		smr_exit(inp->inp_pcbinfo->ipi_smr);
1520db0ac6deSCy Schubert 		inp_lock(inp, lock);
1521db0ac6deSCy Schubert 		if (__predict_false(in_pcbrele(inp, lock)))
1522db0ac6deSCy Schubert 			return (false);
1523db0ac6deSCy Schubert 		/*
1524db0ac6deSCy Schubert 		 * inp acquired through refcount & lock for sure didn't went
1525db0ac6deSCy Schubert 		 * through uma_zfree().  However, it may have already went
1526db0ac6deSCy Schubert 		 * through in_pcbfree() and has another reference, that
1527db0ac6deSCy Schubert 		 * prevented its release by our in_pcbrele().
1528db0ac6deSCy Schubert 		 */
1529f567d55fSGleb Smirnoff 		if (__predict_false(inp->inp_flags & ignflags)) {
1530db0ac6deSCy Schubert 			inp_unlock(inp, lock);
1531db0ac6deSCy Schubert 			return (false);
1532db0ac6deSCy Schubert 		}
1533db0ac6deSCy Schubert 		return (true);
1534db0ac6deSCy Schubert 	} else {
1535db0ac6deSCy Schubert 		smr_exit(inp->inp_pcbinfo->ipi_smr);
1536db0ac6deSCy Schubert 		return (false);
1537db0ac6deSCy Schubert 	}
1538db0ac6deSCy Schubert }
1539db0ac6deSCy Schubert 
1540f567d55fSGleb Smirnoff bool
1541f567d55fSGleb Smirnoff inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock)
1542f567d55fSGleb Smirnoff {
1543f567d55fSGleb Smirnoff 
1544f567d55fSGleb Smirnoff 	/*
1545f567d55fSGleb Smirnoff 	 * in_pcblookup() family of functions ignore not only freed entries,
1546f567d55fSGleb Smirnoff 	 * that may be found due to lockless access to the hash, but dropped
1547f567d55fSGleb Smirnoff 	 * entries, too.
1548f567d55fSGleb Smirnoff 	 */
1549f567d55fSGleb Smirnoff 	return (_inp_smr_lock(inp, lock, INP_FREED | INP_DROPPED));
1550f567d55fSGleb Smirnoff }
1551f567d55fSGleb Smirnoff 
1552db0ac6deSCy Schubert /*
1553db0ac6deSCy Schubert  * inp_next() - inpcb hash/list traversal iterator
1554db0ac6deSCy Schubert  *
1555db0ac6deSCy Schubert  * Requires initialized struct inpcb_iterator for context.
1556db0ac6deSCy Schubert  * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR().
1557db0ac6deSCy Schubert  *
1558db0ac6deSCy Schubert  * - Iterator can have either write-lock or read-lock semantics, that can not
1559db0ac6deSCy Schubert  *   be changed later.
1560db0ac6deSCy Schubert  * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through
1561db0ac6deSCy Schubert  *   a single hash slot.  Note: only rip_input() does the latter.
1562db0ac6deSCy Schubert  * - Iterator may have optional bool matching function.  The matching function
1563db0ac6deSCy Schubert  *   will be executed for each inpcb in the SMR context, so it can not acquire
1564db0ac6deSCy Schubert  *   locks and can safely access only immutable fields of inpcb.
1565db0ac6deSCy Schubert  *
1566db0ac6deSCy Schubert  * A fresh initialized iterator has NULL inpcb in its context and that
1567db0ac6deSCy Schubert  * means that inp_next() call would return the very first inpcb on the list
1568db0ac6deSCy Schubert  * locked with desired semantic.  In all following calls the context pointer
1569db0ac6deSCy Schubert  * shall hold the current inpcb pointer.  The KPI user is not supposed to
1570db0ac6deSCy Schubert  * unlock the current inpcb!  Upon end of traversal inp_next() will return NULL
1571db0ac6deSCy Schubert  * and write NULL to its context.  After end of traversal an iterator can be
1572db0ac6deSCy Schubert  * reused.
1573db0ac6deSCy Schubert  *
1574db0ac6deSCy Schubert  * List traversals have the following features/constraints:
1575db0ac6deSCy Schubert  * - New entries won't be seen, as they are always added to the head of a list.
1576db0ac6deSCy Schubert  * - Removed entries won't stop traversal as long as they are not added to
1577db0ac6deSCy Schubert  *   a different list. This is violated by in_pcbrehash().
1578db0ac6deSCy Schubert  */
1579db0ac6deSCy Schubert #define	II_LIST_FIRST(ipi, hash)					\
1580db0ac6deSCy Schubert 		(((hash) == INP_ALL_LIST) ?				\
1581db0ac6deSCy Schubert 		    CK_LIST_FIRST(&(ipi)->ipi_listhead) :		\
1582fdb987beSMark Johnston 		    CK_LIST_FIRST(&(ipi)->ipi_hash_exact[(hash)]))
1583db0ac6deSCy Schubert #define	II_LIST_NEXT(inp, hash)						\
1584db0ac6deSCy Schubert 		(((hash) == INP_ALL_LIST) ?				\
1585db0ac6deSCy Schubert 		    CK_LIST_NEXT((inp), inp_list) :			\
1586fdb987beSMark Johnston 		    CK_LIST_NEXT((inp), inp_hash_exact))
1587db0ac6deSCy Schubert #define	II_LOCK_ASSERT(inp, lock)					\
1588db0ac6deSCy Schubert 		rw_assert(&(inp)->inp_lock,				\
1589db0ac6deSCy Schubert 		    (lock) == INPLOOKUP_RLOCKPCB ?  RA_RLOCKED : RA_WLOCKED )
1590db0ac6deSCy Schubert struct inpcb *
1591db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii)
1592db0ac6deSCy Schubert {
1593db0ac6deSCy Schubert 	const struct inpcbinfo *ipi = ii->ipi;
1594db0ac6deSCy Schubert 	inp_match_t *match = ii->match;
1595db0ac6deSCy Schubert 	void *ctx = ii->ctx;
1596db0ac6deSCy Schubert 	inp_lookup_t lock = ii->lock;
1597db0ac6deSCy Schubert 	int hash = ii->hash;
1598db0ac6deSCy Schubert 	struct inpcb *inp;
1599db0ac6deSCy Schubert 
1600db0ac6deSCy Schubert 	if (ii->inp == NULL) {		/* First call. */
1601db0ac6deSCy Schubert 		smr_enter(ipi->ipi_smr);
1602db0ac6deSCy Schubert 		/* This is unrolled CK_LIST_FOREACH(). */
1603db0ac6deSCy Schubert 		for (inp = II_LIST_FIRST(ipi, hash);
1604db0ac6deSCy Schubert 		    inp != NULL;
1605db0ac6deSCy Schubert 		    inp = II_LIST_NEXT(inp, hash)) {
1606db0ac6deSCy Schubert 			if (match != NULL && (match)(inp, ctx) == false)
1607db0ac6deSCy Schubert 				continue;
1608f567d55fSGleb Smirnoff 			if (__predict_true(_inp_smr_lock(inp, lock, INP_FREED)))
1609db0ac6deSCy Schubert 				break;
1610db0ac6deSCy Schubert 			else {
1611db0ac6deSCy Schubert 				smr_enter(ipi->ipi_smr);
1612db0ac6deSCy Schubert 				MPASS(inp != II_LIST_FIRST(ipi, hash));
1613db0ac6deSCy Schubert 				inp = II_LIST_FIRST(ipi, hash);
1614430df2abSMichael Tuexen 				if (inp == NULL)
1615430df2abSMichael Tuexen 					break;
1616db0ac6deSCy Schubert 			}
1617db0ac6deSCy Schubert 		}
1618db0ac6deSCy Schubert 
1619db0ac6deSCy Schubert 		if (inp == NULL)
1620db0ac6deSCy Schubert 			smr_exit(ipi->ipi_smr);
1621db0ac6deSCy Schubert 		else
1622db0ac6deSCy Schubert 			ii->inp = inp;
1623db0ac6deSCy Schubert 
1624db0ac6deSCy Schubert 		return (inp);
1625db0ac6deSCy Schubert 	}
1626db0ac6deSCy Schubert 
1627db0ac6deSCy Schubert 	/* Not a first call. */
1628db0ac6deSCy Schubert 	smr_enter(ipi->ipi_smr);
1629db0ac6deSCy Schubert restart:
1630db0ac6deSCy Schubert 	inp = ii->inp;
1631db0ac6deSCy Schubert 	II_LOCK_ASSERT(inp, lock);
1632db0ac6deSCy Schubert next:
1633db0ac6deSCy Schubert 	inp = II_LIST_NEXT(inp, hash);
1634db0ac6deSCy Schubert 	if (inp == NULL) {
1635db0ac6deSCy Schubert 		smr_exit(ipi->ipi_smr);
1636db0ac6deSCy Schubert 		goto found;
1637db0ac6deSCy Schubert 	}
1638db0ac6deSCy Schubert 
1639db0ac6deSCy Schubert 	if (match != NULL && (match)(inp, ctx) == false)
1640db0ac6deSCy Schubert 		goto next;
1641db0ac6deSCy Schubert 
1642db0ac6deSCy Schubert 	if (__predict_true(inp_trylock(inp, lock))) {
1643db0ac6deSCy Schubert 		if (__predict_false(inp->inp_flags & INP_FREED)) {
1644db0ac6deSCy Schubert 			/*
1645db0ac6deSCy Schubert 			 * Entries are never inserted in middle of a list, thus
1646db0ac6deSCy Schubert 			 * as long as we are in SMR, we can continue traversal.
1647db0ac6deSCy Schubert 			 * Jump to 'restart' should yield in the same result,
1648db0ac6deSCy Schubert 			 * but could produce unnecessary looping.  Could this
1649db0ac6deSCy Schubert 			 * looping be unbound?
1650db0ac6deSCy Schubert 			 */
1651db0ac6deSCy Schubert 			inp_unlock(inp, lock);
1652db0ac6deSCy Schubert 			goto next;
1653db0ac6deSCy Schubert 		} else {
1654db0ac6deSCy Schubert 			smr_exit(ipi->ipi_smr);
1655db0ac6deSCy Schubert 			goto found;
1656db0ac6deSCy Schubert 		}
1657db0ac6deSCy Schubert 	}
1658db0ac6deSCy Schubert 
1659db0ac6deSCy Schubert 	/*
1660db0ac6deSCy Schubert 	 * Can't obtain lock immediately, thus going hard.  Once we exit the
1661db0ac6deSCy Schubert 	 * SMR section we can no longer jump to 'next', and our only stable
1662db0ac6deSCy Schubert 	 * anchoring point is ii->inp, which we keep locked for this case, so
1663db0ac6deSCy Schubert 	 * we jump to 'restart'.
1664db0ac6deSCy Schubert 	 */
1665db0ac6deSCy Schubert 	if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) {
1666db0ac6deSCy Schubert 		smr_exit(ipi->ipi_smr);
1667db0ac6deSCy Schubert 		inp_lock(inp, lock);
1668db0ac6deSCy Schubert 		if (__predict_false(in_pcbrele(inp, lock))) {
1669db0ac6deSCy Schubert 			smr_enter(ipi->ipi_smr);
1670db0ac6deSCy Schubert 			goto restart;
1671db0ac6deSCy Schubert 		}
1672db0ac6deSCy Schubert 		/*
1673db0ac6deSCy Schubert 		 * See comment in inp_smr_lock().
1674db0ac6deSCy Schubert 		 */
1675db0ac6deSCy Schubert 		if (__predict_false(inp->inp_flags & INP_FREED)) {
1676db0ac6deSCy Schubert 			inp_unlock(inp, lock);
1677db0ac6deSCy Schubert 			smr_enter(ipi->ipi_smr);
1678db0ac6deSCy Schubert 			goto restart;
1679db0ac6deSCy Schubert 		}
1680db0ac6deSCy Schubert 	} else
1681db0ac6deSCy Schubert 		goto next;
1682db0ac6deSCy Schubert 
1683db0ac6deSCy Schubert found:
1684db0ac6deSCy Schubert 	inp_unlock(ii->inp, lock);
1685db0ac6deSCy Schubert 	ii->inp = inp;
1686db0ac6deSCy Schubert 
1687db0ac6deSCy Schubert 	return (ii->inp);
1688db0ac6deSCy Schubert }
1689db0ac6deSCy Schubert 
1690db0ac6deSCy Schubert /*
169179bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
1692db0ac6deSCy Schubert  * stability of an inpcb pointer despite the inpcb lock being released or
1693db0ac6deSCy Schubert  * SMR section exited.
169479bdc6e5SRobert Watson  *
1695db0ac6deSCy Schubert  * To free a reference later in_pcbrele_(r|w)locked() must be performed.
1696c0a211c5SRobert Watson  */
169779bdc6e5SRobert Watson void
169879bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
16994c7c478dSRobert Watson {
1700db0ac6deSCy Schubert 	u_int old __diagused;
1701df8bae1dSRodney W. Grimes 
1702db0ac6deSCy Schubert 	old = refcount_acquire(&inp->inp_refcount);
1703db0ac6deSCy Schubert 	KASSERT(old > 0, ("%s: refcount 0", __func__));
170479bdc6e5SRobert Watson }
170579bdc6e5SRobert Watson 
170679bdc6e5SRobert Watson /*
1707db0ac6deSCy Schubert  * Drop a refcount on an inpcb elevated using in_pcbref(), potentially
1708db0ac6deSCy Schubert  * freeing the pcb, if the reference was very last.
170979bdc6e5SRobert Watson  */
1710db0ac6deSCy Schubert bool
171179bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
171279bdc6e5SRobert Watson {
171379bdc6e5SRobert Watson 
171479bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
171579bdc6e5SRobert Watson 
1716c7ea65ecSMark Johnston 	if (!refcount_release(&inp->inp_refcount))
1717db0ac6deSCy Schubert 		return (false);
1718db0ac6deSCy Schubert 
1719db0ac6deSCy Schubert 	MPASS(inp->inp_flags & INP_FREED);
1720db0ac6deSCy Schubert 	MPASS(inp->inp_socket == NULL);
17215fd1a67eSMark Johnston 	crfree(inp->inp_cred);
17225fd1a67eSMark Johnston #ifdef INVARIANTS
17235fd1a67eSMark Johnston 	inp->inp_cred = NULL;
17245fd1a67eSMark Johnston #endif
1725df4e91d3SGleb Smirnoff 	INP_RUNLOCK(inp);
1726db0ac6deSCy Schubert 	uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp);
1727db0ac6deSCy Schubert 	return (true);
1728df4e91d3SGleb Smirnoff }
172979bdc6e5SRobert Watson 
1730db0ac6deSCy Schubert bool
173179bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
173279bdc6e5SRobert Watson {
173379bdc6e5SRobert Watson 
173479bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
173579bdc6e5SRobert Watson 
1736c7ea65ecSMark Johnston 	if (!refcount_release(&inp->inp_refcount))
1737db0ac6deSCy Schubert 		return (false);
1738db0ac6deSCy Schubert 
1739db0ac6deSCy Schubert 	MPASS(inp->inp_flags & INP_FREED);
1740db0ac6deSCy Schubert 	MPASS(inp->inp_socket == NULL);
17415fd1a67eSMark Johnston 	crfree(inp->inp_cred);
17425fd1a67eSMark Johnston #ifdef INVARIANTS
17435fd1a67eSMark Johnston 	inp->inp_cred = NULL;
17445fd1a67eSMark Johnston #endif
1745edd0e0b0SFabien Thomas 	INP_WUNLOCK(inp);
1746db0ac6deSCy Schubert 	uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp);
1747db0ac6deSCy Schubert 	return (true);
1748addf2b20SMatt Macy }
1749addf2b20SMatt Macy 
17507b92493aSMark Johnston bool
17517b92493aSMark Johnston in_pcbrele(struct inpcb *inp, const inp_lookup_t lock)
17527b92493aSMark Johnston {
17537b92493aSMark Johnston 
17547b92493aSMark Johnston 	return (lock == INPLOOKUP_RLOCKPCB ?
17557b92493aSMark Johnston 	    in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp));
17567b92493aSMark Johnston }
17577b92493aSMark Johnston 
175879bdc6e5SRobert Watson /*
175979bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
176079bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
176179bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
176279bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
1763db0ac6deSCy Schubert  * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked.
1764db0ac6deSCy Schubert  *  Almost all work, including removal from global lists, is done in this
1765db0ac6deSCy Schubert  * context, where the pcbinfo lock is held.
176679bdc6e5SRobert Watson  */
176779bdc6e5SRobert Watson void
176879bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
176979bdc6e5SRobert Watson {
1770f42a83f2SMatt Macy 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1771db0ac6deSCy Schubert #ifdef INET
1772db0ac6deSCy Schubert 	struct ip_moptions *imo;
1773db0ac6deSCy Schubert #endif
1774db0ac6deSCy Schubert #ifdef INET6
1775db0ac6deSCy Schubert 	struct ip6_moptions *im6o;
1776db0ac6deSCy Schubert #endif
177745a48d07SJonathan T. Looney 
177879bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
1779a13039e2SGleb Smirnoff 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1780db0ac6deSCy Schubert 	KASSERT((inp->inp_flags & INP_FREED) == 0,
1781db0ac6deSCy Schubert 	    ("%s: called twice for pcb %p", __func__, inp));
1782db0ac6deSCy Schubert 
1783a13039e2SGleb Smirnoff 	/*
1784a13039e2SGleb Smirnoff 	 * in_pcblookup_local() and in6_pcblookup_local() may return an inpcb
1785a13039e2SGleb Smirnoff 	 * from the hash without acquiring inpcb lock, they rely on the hash
1786a13039e2SGleb Smirnoff 	 * lock, thus in_pcbremhash() should be the first action.
1787a13039e2SGleb Smirnoff 	 */
1788a13039e2SGleb Smirnoff 	if (inp->inp_flags & INP_INHASHLIST)
1789a13039e2SGleb Smirnoff 		in_pcbremhash(inp);
1790db0ac6deSCy Schubert 	INP_INFO_WLOCK(pcbinfo);
1791db0ac6deSCy Schubert 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1792db0ac6deSCy Schubert 	pcbinfo->ipi_count--;
1793db0ac6deSCy Schubert 	CK_LIST_REMOVE(inp, inp_list);
1794db0ac6deSCy Schubert 	INP_INFO_WUNLOCK(pcbinfo);
1795db0ac6deSCy Schubert 
1796a13039e2SGleb Smirnoff #ifdef RATELIMIT
1797a13039e2SGleb Smirnoff 	if (inp->inp_snd_tag != NULL)
1798a13039e2SGleb Smirnoff 		in_pcbdetach_txrtlmt(inp);
1799a13039e2SGleb Smirnoff #endif
1800a13039e2SGleb Smirnoff 	inp->inp_flags |= INP_FREED;
1801a13039e2SGleb Smirnoff 	inp->inp_socket->so_pcb = NULL;
1802a13039e2SGleb Smirnoff 	inp->inp_socket = NULL;
1803db0ac6deSCy Schubert 
1804fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
1805db0ac6deSCy Schubert #ifdef MAC
1806db0ac6deSCy Schubert 	mac_inpcb_destroy(inp);
1807db0ac6deSCy Schubert #endif
1808db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1809db0ac6deSCy Schubert 	if (inp->inp_sp != NULL)
1810db0ac6deSCy Schubert 		ipsec_delete_pcbpolicy(inp);
1811db0ac6deSCy Schubert #endif
1812db0ac6deSCy Schubert #ifdef INET
1813db0ac6deSCy Schubert 	if (inp->inp_options)
1814db0ac6deSCy Schubert 		(void)m_free(inp->inp_options);
18154a0c6403SGleb Smirnoff 	DEBUG_POISON_POINTER(inp->inp_options);
1816db0ac6deSCy Schubert 	imo = inp->inp_moptions;
18174a0c6403SGleb Smirnoff 	DEBUG_POISON_POINTER(inp->inp_moptions);
1818db0ac6deSCy Schubert #endif
1819db0ac6deSCy Schubert #ifdef INET6
1820db0ac6deSCy Schubert 	if (inp->inp_vflag & INP_IPV6PROTO) {
1821db0ac6deSCy Schubert 		ip6_freepcbopts(inp->in6p_outputopts);
18224a0c6403SGleb Smirnoff 		DEBUG_POISON_POINTER(inp->in6p_outputopts);
1823db0ac6deSCy Schubert 		im6o = inp->in6p_moptions;
18244a0c6403SGleb Smirnoff 		DEBUG_POISON_POINTER(inp->in6p_moptions);
1825db0ac6deSCy Schubert 	} else
1826db0ac6deSCy Schubert 		im6o = NULL;
1827db0ac6deSCy Schubert #endif
1828db0ac6deSCy Schubert 
1829db0ac6deSCy Schubert 	if (__predict_false(in_pcbrele_wlocked(inp) == false)) {
1830cb6bb230SMatt Macy 		INP_WUNLOCK(inp);
1831db0ac6deSCy Schubert 	}
1832db0ac6deSCy Schubert #ifdef INET6
1833db0ac6deSCy Schubert 	ip6_freemoptions(im6o);
1834db0ac6deSCy Schubert #endif
1835db0ac6deSCy Schubert #ifdef INET
1836db0ac6deSCy Schubert 	inp_freemoptions(imo);
1837db0ac6deSCy Schubert #endif
1838eb93b99dSGleb Smirnoff }
1839eb93b99dSGleb Smirnoff 
1840eb93b99dSGleb Smirnoff /*
1841eb93b99dSGleb Smirnoff  * Different protocols initialize their inpcbs differently - giving
1842eb93b99dSGleb Smirnoff  * different name to the lock.  But they all are disposed the same.
1843eb93b99dSGleb Smirnoff  */
1844eb93b99dSGleb Smirnoff static void
1845eb93b99dSGleb Smirnoff inpcb_fini(void *mem, int size)
1846eb93b99dSGleb Smirnoff {
1847eb93b99dSGleb Smirnoff 	struct inpcb *inp = mem;
1848eb93b99dSGleb Smirnoff 
1849eb93b99dSGleb Smirnoff 	INP_LOCK_DESTROY(inp);
185028696211SRobert Watson }
185128696211SRobert Watson 
185228696211SRobert Watson /*
1853c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1854c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1855c0a211c5SRobert Watson  *
1856c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1857c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1858c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1859c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1860c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1861c0a211c5SRobert Watson  * in_pcbdetach().
1862c0a211c5SRobert Watson  *
1863c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1864bbbd7aabSGleb Smirnoff  * in_pcbpurgeif0()?
186510702a28SRobert Watson  */
186610702a28SRobert Watson void
186710702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
186810702a28SRobert Watson {
186910702a28SRobert Watson 
18708501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
187110702a28SRobert Watson 
1872ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
18733ba34b07SGleb Smirnoff 	if (inp->inp_flags & INP_INHASHLIST)
18743ba34b07SGleb Smirnoff 		in_pcbremhash(inp);
187510702a28SRobert Watson }
187610702a28SRobert Watson 
187767107f45SBjoern A. Zeeb #ifdef INET
187854d642bbSRobert Watson /*
187954d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
188054d642bbSRobert Watson  */
1881117bcae7SGarrett Wollman int
18820fac350cSGleb Smirnoff in_getsockaddr(struct socket *so, struct sockaddr *sa)
1883df8bae1dSRodney W. Grimes {
1884136d4f1cSRobert Watson 	struct inpcb *inp;
188542fa505bSDavid Greenman 
1886fdc984f7STor Egge 	inp = sotoinpcb(so);
188754d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
18886466b28aSRobert Watson 
18890fac350cSGleb Smirnoff 	*(struct sockaddr_in *)sa = (struct sockaddr_in ){
18900fac350cSGleb Smirnoff 		.sin_len = sizeof(struct sockaddr_in),
18910fac350cSGleb Smirnoff 		.sin_family = AF_INET,
18920fac350cSGleb Smirnoff 		.sin_port = inp->inp_lport,
18930fac350cSGleb Smirnoff 		.sin_addr = inp->inp_laddr,
18940fac350cSGleb Smirnoff 	};
189542fa505bSDavid Greenman 
18960fac350cSGleb Smirnoff 	return (0);
1897df8bae1dSRodney W. Grimes }
1898df8bae1dSRodney W. Grimes 
1899117bcae7SGarrett Wollman int
19000fac350cSGleb Smirnoff in_getpeeraddr(struct socket *so, struct sockaddr *sa)
1901df8bae1dSRodney W. Grimes {
1902136d4f1cSRobert Watson 	struct inpcb *inp;
190342fa505bSDavid Greenman 
1904fdc984f7STor Egge 	inp = sotoinpcb(so);
190554d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
19066466b28aSRobert Watson 
19070fac350cSGleb Smirnoff 	*(struct sockaddr_in *)sa = (struct sockaddr_in ){
19080fac350cSGleb Smirnoff 		.sin_len = sizeof(struct sockaddr_in),
19090fac350cSGleb Smirnoff 		.sin_family = AF_INET,
19100fac350cSGleb Smirnoff 		.sin_port = inp->inp_fport,
19110fac350cSGleb Smirnoff 		.sin_addr = inp->inp_faddr,
19120fac350cSGleb Smirnoff 	};
191342fa505bSDavid Greenman 
19140fac350cSGleb Smirnoff 	return (0);
1915df8bae1dSRodney W. Grimes }
1916df8bae1dSRodney W. Grimes 
1917db0ac6deSCy Schubert static bool
1918db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused)
1919db0ac6deSCy Schubert {
1920db0ac6deSCy Schubert 
1921db0ac6deSCy Schubert 	if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL)
1922db0ac6deSCy Schubert 		return (true);
1923db0ac6deSCy Schubert 	else
1924db0ac6deSCy Schubert 		return (false);
1925db0ac6deSCy Schubert }
1926db0ac6deSCy Schubert 
1927e43cc4aeSHajimu UMEMOTO void
1928136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1929e43cc4aeSHajimu UMEMOTO {
1930db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB,
1931db0ac6deSCy Schubert 	    inp_v4_multi_match, NULL);
1932e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
193359854ecfSHans Petter Selasky 	struct in_multi *inm;
193459854ecfSHans Petter Selasky 	struct in_mfilter *imf;
1935e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1936e43cc4aeSHajimu UMEMOTO 
1937db0ac6deSCy Schubert 	IN_MULTI_LOCK_ASSERT();
1938db0ac6deSCy Schubert 
1939db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
1940db0ac6deSCy Schubert 		INP_WLOCK_ASSERT(inp);
1941db0ac6deSCy Schubert 
1942e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1943e43cc4aeSHajimu UMEMOTO 		/*
1944e43cc4aeSHajimu UMEMOTO 		 * Unselect the outgoing interface if it is being
1945e43cc4aeSHajimu UMEMOTO 		 * detached.
1946e43cc4aeSHajimu UMEMOTO 		 */
1947e43cc4aeSHajimu UMEMOTO 		if (imo->imo_multicast_ifp == ifp)
1948e43cc4aeSHajimu UMEMOTO 			imo->imo_multicast_ifp = NULL;
1949e43cc4aeSHajimu UMEMOTO 
1950e43cc4aeSHajimu UMEMOTO 		/*
1951e43cc4aeSHajimu UMEMOTO 		 * Drop multicast group membership if we joined
1952e43cc4aeSHajimu UMEMOTO 		 * through the interface being detached.
1953cb6bb230SMatt Macy 		 *
1954cb6bb230SMatt Macy 		 * XXX This can all be deferred to an epoch_call
1955e43cc4aeSHajimu UMEMOTO 		 */
195659854ecfSHans Petter Selasky restart:
195759854ecfSHans Petter Selasky 		IP_MFILTER_FOREACH(imf, &imo->imo_head) {
195859854ecfSHans Petter Selasky 			if ((inm = imf->imf_inm) == NULL)
195959854ecfSHans Petter Selasky 				continue;
196059854ecfSHans Petter Selasky 			if (inm->inm_ifp != ifp)
196159854ecfSHans Petter Selasky 				continue;
196259854ecfSHans Petter Selasky 			ip_mfilter_remove(&imo->imo_head, imf);
196359854ecfSHans Petter Selasky 			in_leavegroup_locked(inm, NULL);
196459854ecfSHans Petter Selasky 			ip_mfilter_free(imf);
196559854ecfSHans Petter Selasky 			goto restart;
1966e43cc4aeSHajimu UMEMOTO 		}
1967e43cc4aeSHajimu UMEMOTO 	}
1968e43cc4aeSHajimu UMEMOTO }
1969e43cc4aeSHajimu UMEMOTO 
1970df8bae1dSRodney W. Grimes /*
1971fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1972fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1973c3229e05SDavid Greenman  */
1974d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1975df8bae1dSRodney W. Grimes struct inpcb *
1976136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
19779a413162SMark Johnston     u_short lport, int fib, int lookupflags, struct ucred *cred)
1978df8bae1dSRodney W. Grimes {
1979136d4f1cSRobert Watson 	struct inpcb *inp;
1980d5e8a67eSHajimu UMEMOTO #ifdef INET6
1981d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1982d5e8a67eSHajimu UMEMOTO #else
1983d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1984d5e8a67eSHajimu UMEMOTO #endif
1985d5e8a67eSHajimu UMEMOTO 	int wildcard;
19867bc4aca7SDavid Greenman 
198768e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
198868e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
19899a413162SMark Johnston 	KASSERT(fib == RT_ALL_FIBS || (fib >= 0 && fib < V_rt_numfibs),
19909a413162SMark Johnston 	    ("%s: invalid fib %d", __func__, fib));
19919a413162SMark Johnston 
1992fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
19931b73ca0bSSam Leffler 
199468e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1995c3229e05SDavid Greenman 		struct inpcbhead *head;
1996c3229e05SDavid Greenman 		/*
1997c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1998c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1999c3229e05SDavid Greenman 		 */
2000fdb987beSMark Johnston 		head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
2001a0577692SGleb Smirnoff 		    pcbinfo->ipi_hashmask)];
2002fdb987beSMark Johnston 		CK_LIST_FOREACH(inp, head, inp_hash_wild) {
2003cfa1ca9dSYoshinobu Inoue #ifdef INET6
2004413628a7SBjoern A. Zeeb 			/* XXX inp locking */
2005369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
2006cfa1ca9dSYoshinobu Inoue 				continue;
2007cfa1ca9dSYoshinobu Inoue #endif
2008c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
2009c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
20109a413162SMark Johnston 			    inp->inp_lport == lport && (fib == RT_ALL_FIBS ||
20119a413162SMark Johnston 			    inp->inp_inc.inc_fibnum == fib)) {
2012c3229e05SDavid Greenman 				/*
2013413628a7SBjoern A. Zeeb 				 * Found?
2014c3229e05SDavid Greenman 				 */
2015ac1750ddSMark Johnston 				if (prison_equal_ip4(cred->cr_prison,
20160304c731SJamie Gritton 				    inp->inp_cred->cr_prison))
2017c3229e05SDavid Greenman 					return (inp);
2018df8bae1dSRodney W. Grimes 			}
2019c3229e05SDavid Greenman 		}
2020c3229e05SDavid Greenman 		/*
2021c3229e05SDavid Greenman 		 * Not found.
2022c3229e05SDavid Greenman 		 */
2023c3229e05SDavid Greenman 		return (NULL);
2024c3229e05SDavid Greenman 	} else {
2025*5f539170SGleb Smirnoff 		struct inpcbhead *porthash;
2026c3229e05SDavid Greenman 		struct inpcb *match = NULL;
2027*5f539170SGleb Smirnoff 
2028c3229e05SDavid Greenman 		/*
2029c3229e05SDavid Greenman 		 * Port is in use by one or more PCBs. Look for best
2030c3229e05SDavid Greenman 		 * fit.
2031c3229e05SDavid Greenman 		 */
2032*5f539170SGleb Smirnoff 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
2033*5f539170SGleb Smirnoff 		    pcbinfo->ipi_porthashmask)];
2034*5f539170SGleb Smirnoff 		CK_LIST_FOREACH(inp, porthash, inp_portlist) {
2035*5f539170SGleb Smirnoff 			if (inp->inp_lport != lport)
2036*5f539170SGleb Smirnoff 				continue;
2037ac1750ddSMark Johnston 			if (!prison_equal_ip4(inp->inp_cred->cr_prison,
20380304c731SJamie Gritton 			    cred->cr_prison))
2039413628a7SBjoern A. Zeeb 				continue;
20409a413162SMark Johnston 			if (fib != RT_ALL_FIBS &&
20419a413162SMark Johnston 			    inp->inp_inc.inc_fibnum != fib)
20429a413162SMark Johnston 				continue;
2043*5f539170SGleb Smirnoff 			wildcard = 0;
2044cfa1ca9dSYoshinobu Inoue #ifdef INET6
2045413628a7SBjoern A. Zeeb 			/* XXX inp locking */
2046369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
2047cfa1ca9dSYoshinobu Inoue 				continue;
2048d5e8a67eSHajimu UMEMOTO 			/*
2049*5f539170SGleb Smirnoff 			 * We never select the PCB that has INP_IPV6 flag and
2050*5f539170SGleb Smirnoff 			 * is bound to :: if we have another PCB which is bound
2051*5f539170SGleb Smirnoff 			 * to 0.0.0.0.  If a PCB has the INP_IPV6 flag, then we
2052*5f539170SGleb Smirnoff 			 * set its cost higher than IPv4 only PCBs.
2053d5e8a67eSHajimu UMEMOTO 			 *
2054*5f539170SGleb Smirnoff 			 * Note that the case only happens when a socket is
2055*5f539170SGleb Smirnoff 			 * bound to ::, under the condition that the use of the
2056d5e8a67eSHajimu UMEMOTO 			 * mapped address is allowed.
2057d5e8a67eSHajimu UMEMOTO 			 */
2058d5e8a67eSHajimu UMEMOTO 			if ((inp->inp_vflag & INP_IPV6) != 0)
2059d5e8a67eSHajimu UMEMOTO 				wildcard += INP_LOOKUP_MAPPED_PCB_COST;
2060cfa1ca9dSYoshinobu Inoue #endif
2061c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr != INADDR_ANY)
2062c3229e05SDavid Greenman 				wildcard++;
206315bd2b43SDavid Greenman 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
206415bd2b43SDavid Greenman 				if (laddr.s_addr == INADDR_ANY)
206515bd2b43SDavid Greenman 					wildcard++;
206615bd2b43SDavid Greenman 				else if (inp->inp_laddr.s_addr != laddr.s_addr)
206715bd2b43SDavid Greenman 					continue;
206815bd2b43SDavid Greenman 			} else {
206915bd2b43SDavid Greenman 				if (laddr.s_addr != INADDR_ANY)
207015bd2b43SDavid Greenman 					wildcard++;
207115bd2b43SDavid Greenman 			}
2072df8bae1dSRodney W. Grimes 			if (wildcard < matchwild) {
2073df8bae1dSRodney W. Grimes 				match = inp;
2074df8bae1dSRodney W. Grimes 				matchwild = wildcard;
2075413628a7SBjoern A. Zeeb 				if (matchwild == 0)
2076df8bae1dSRodney W. Grimes 					break;
2077df8bae1dSRodney W. Grimes 			}
2078df8bae1dSRodney W. Grimes 		}
2079df8bae1dSRodney W. Grimes 		return (match);
2080df8bae1dSRodney W. Grimes 	}
2081c3229e05SDavid Greenman }
2082d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
208315bd2b43SDavid Greenman 
2084d93ec8cbSMark Johnston static bool
2085da806e8dSMark Johnston in_pcblookup_lb_match(const struct inpcblbgroup *grp, int domain, int fib)
2086d93ec8cbSMark Johnston {
2087da806e8dSMark Johnston 	return ((domain == M_NODOM || domain == grp->il_numa_domain) &&
2088da806e8dSMark Johnston 	    (fib == RT_ALL_FIBS || fib == grp->il_fibnum));
2089d93ec8cbSMark Johnston }
2090d93ec8cbSMark Johnston 
20911a43cff9SSean Bruno static struct inpcb *
20921a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
20934130ea61SMark Johnston     const struct in_addr *faddr, uint16_t fport, const struct in_addr *laddr,
2094da806e8dSMark Johnston     uint16_t lport, int domain, int fib)
20951a43cff9SSean Bruno {
20961a43cff9SSean Bruno 	const struct inpcblbgrouphead *hdr;
20971a43cff9SSean Bruno 	struct inpcblbgroup *grp;
2098d93ec8cbSMark Johnston 	struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild;
2099a600aabeSMark Johnston 	struct inpcb *inp;
2100a600aabeSMark Johnston 	u_int count;
21011a43cff9SSean Bruno 
21021a43cff9SSean Bruno 	INP_HASH_LOCK_ASSERT(pcbinfo);
2103a600aabeSMark Johnston 	NET_EPOCH_ASSERT();
21041a43cff9SSean Bruno 
21059d2877fcSMark Johnston 	hdr = &pcbinfo->ipi_lbgrouphashbase[
21069d2877fcSMark Johnston 	    INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
21071a43cff9SSean Bruno 
21081a43cff9SSean Bruno 	/*
2109d93ec8cbSMark Johnston 	 * Search for an LB group match based on the following criteria:
2110d93ec8cbSMark Johnston 	 * - prefer jailed groups to non-jailed groups
2111d93ec8cbSMark Johnston 	 * - prefer exact source address matches to wildcard matches
2112d93ec8cbSMark Johnston 	 * - prefer groups bound to the specified NUMA domain
21131a43cff9SSean Bruno 	 */
2114d93ec8cbSMark Johnston 	jail_exact = jail_wild = local_exact = local_wild = NULL;
211554af3d0dSMark Johnston 	CK_LIST_FOREACH(grp, hdr, il_list) {
2116d93ec8cbSMark Johnston 		bool injail;
2117d93ec8cbSMark Johnston 
21181a43cff9SSean Bruno #ifdef INET6
21191a43cff9SSean Bruno 		if (!(grp->il_vflag & INP_IPV4))
21201a43cff9SSean Bruno 			continue;
21211a43cff9SSean Bruno #endif
21228be02ee4SMark Johnston 		if (grp->il_lport != lport)
21238be02ee4SMark Johnston 			continue;
21241a43cff9SSean Bruno 
2125d93ec8cbSMark Johnston 		injail = prison_flag(grp->il_cred, PR_IP4) != 0;
2126d93ec8cbSMark Johnston 		if (injail && prison_check_ip4_locked(grp->il_cred->cr_prison,
2127d93ec8cbSMark Johnston 		    laddr) != 0)
2128d93ec8cbSMark Johnston 			continue;
2129a034518aSAndrew Gallatin 
2130d93ec8cbSMark Johnston 		if (grp->il_laddr.s_addr == laddr->s_addr) {
2131d93ec8cbSMark Johnston 			if (injail) {
2132d93ec8cbSMark Johnston 				jail_exact = grp;
2133da806e8dSMark Johnston 				if (in_pcblookup_lb_match(grp, domain, fib))
2134d93ec8cbSMark Johnston 					/* This is a perfect match. */
2135d93ec8cbSMark Johnston 					goto out;
2136d93ec8cbSMark Johnston 			} else if (local_exact == NULL ||
2137da806e8dSMark Johnston 			    in_pcblookup_lb_match(grp, domain, fib)) {
2138d93ec8cbSMark Johnston 				local_exact = grp;
2139d93ec8cbSMark Johnston 			}
21404130ea61SMark Johnston 		} else if (grp->il_laddr.s_addr == INADDR_ANY) {
2141d93ec8cbSMark Johnston 			if (injail) {
2142d93ec8cbSMark Johnston 				if (jail_wild == NULL ||
2143da806e8dSMark Johnston 				    in_pcblookup_lb_match(grp, domain, fib))
2144d93ec8cbSMark Johnston 					jail_wild = grp;
2145d93ec8cbSMark Johnston 			} else if (local_wild == NULL ||
2146da806e8dSMark Johnston 			    in_pcblookup_lb_match(grp, domain, fib)) {
2147d93ec8cbSMark Johnston 				local_wild = grp;
2148d93ec8cbSMark Johnston 			}
2149d93ec8cbSMark Johnston 		}
2150d93ec8cbSMark Johnston 	}
2151d93ec8cbSMark Johnston 
2152d93ec8cbSMark Johnston 	if (jail_exact != NULL)
2153d93ec8cbSMark Johnston 		grp = jail_exact;
2154d93ec8cbSMark Johnston 	else if (jail_wild != NULL)
2155d93ec8cbSMark Johnston 		grp = jail_wild;
2156d93ec8cbSMark Johnston 	else if (local_exact != NULL)
2157d93ec8cbSMark Johnston 		grp = local_exact;
2158d93ec8cbSMark Johnston 	else
2159d93ec8cbSMark Johnston 		grp = local_wild;
2160d93ec8cbSMark Johnston 	if (grp == NULL)
2161d93ec8cbSMark Johnston 		return (NULL);
2162a600aabeSMark Johnston 
2163d93ec8cbSMark Johnston out:
2164a600aabeSMark Johnston 	/*
2165a600aabeSMark Johnston 	 * Synchronize with in_pcblbgroup_insert().
2166a600aabeSMark Johnston 	 */
2167a600aabeSMark Johnston 	count = atomic_load_acq_int(&grp->il_inpcnt);
2168a600aabeSMark Johnston 	if (count == 0)
2169a600aabeSMark Johnston 		return (NULL);
2170a600aabeSMark Johnston 	inp = grp->il_inp[INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) % count];
2171a600aabeSMark Johnston 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2172a600aabeSMark Johnston 	return (inp);
21731a43cff9SSean Bruno }
21741a43cff9SSean Bruno 
21753e98dcb3SMark Johnston static bool
21763e98dcb3SMark Johnston in_pcblookup_exact_match(const struct inpcb *inp, struct in_addr faddr,
21774130ea61SMark Johnston     u_short fport, struct in_addr laddr, u_short lport)
217815bd2b43SDavid Greenman {
2179cfa1ca9dSYoshinobu Inoue #ifdef INET6
2180413628a7SBjoern A. Zeeb 	/* XXX inp locking */
2181369dc8ceSEivind Eklund 	if ((inp->inp_vflag & INP_IPV4) == 0)
21823e98dcb3SMark Johnston 		return (false);
2183cfa1ca9dSYoshinobu Inoue #endif
21846d6a026bSDavid Greenman 	if (inp->inp_faddr.s_addr == faddr.s_addr &&
2185ca98b82cSDavid Greenman 	    inp->inp_laddr.s_addr == laddr.s_addr &&
2186ca98b82cSDavid Greenman 	    inp->inp_fport == fport &&
2187220d8921SGleb Smirnoff 	    inp->inp_lport == lport)
21883e98dcb3SMark Johnston 		return (true);
21893e98dcb3SMark Johnston 	return (false);
21903e98dcb3SMark Johnston }
21913e98dcb3SMark Johnston 
21923e98dcb3SMark Johnston static struct inpcb *
21933e98dcb3SMark Johnston in_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in_addr faddr,
21943e98dcb3SMark Johnston     u_short fport, struct in_addr laddr, u_short lport)
21953e98dcb3SMark Johnston {
21963e98dcb3SMark Johnston 	struct inpcbhead *head;
21973e98dcb3SMark Johnston 	struct inpcb *inp;
21983e98dcb3SMark Johnston 
21993e98dcb3SMark Johnston 	INP_HASH_LOCK_ASSERT(pcbinfo);
22003e98dcb3SMark Johnston 
22013e98dcb3SMark Johnston 	head = &pcbinfo->ipi_hash_exact[INP_PCBHASH(&faddr, lport, fport,
22023e98dcb3SMark Johnston 	    pcbinfo->ipi_hashmask)];
22033e98dcb3SMark Johnston 	CK_LIST_FOREACH(inp, head, inp_hash_exact) {
22043e98dcb3SMark Johnston 		if (in_pcblookup_exact_match(inp, faddr, fport, laddr, lport))
2205c3229e05SDavid Greenman 			return (inp);
2206c3229e05SDavid Greenman 	}
22073e98dcb3SMark Johnston 	return (NULL);
22083e98dcb3SMark Johnston }
22093e98dcb3SMark Johnston 
22103e98dcb3SMark Johnston typedef enum {
22113e98dcb3SMark Johnston 	INPLOOKUP_MATCH_NONE = 0,
22123e98dcb3SMark Johnston 	INPLOOKUP_MATCH_WILD = 1,
22133e98dcb3SMark Johnston 	INPLOOKUP_MATCH_LADDR = 2,
22143e98dcb3SMark Johnston } inp_lookup_match_t;
22153e98dcb3SMark Johnston 
22163e98dcb3SMark Johnston static inp_lookup_match_t
22173e98dcb3SMark Johnston in_pcblookup_wild_match(const struct inpcb *inp, struct in_addr laddr,
2218da806e8dSMark Johnston     u_short lport, int fib)
22193e98dcb3SMark Johnston {
22203e98dcb3SMark Johnston #ifdef INET6
22213e98dcb3SMark Johnston 	/* XXX inp locking */
22223e98dcb3SMark Johnston 	if ((inp->inp_vflag & INP_IPV4) == 0)
22233e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_NONE);
22243e98dcb3SMark Johnston #endif
22253e98dcb3SMark Johnston 	if (inp->inp_faddr.s_addr != INADDR_ANY || inp->inp_lport != lport)
22263e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_NONE);
2227da806e8dSMark Johnston 	if (fib != RT_ALL_FIBS && inp->inp_inc.inc_fibnum != fib)
2228da806e8dSMark Johnston 		return (INPLOOKUP_MATCH_NONE);
22293e98dcb3SMark Johnston 	if (inp->inp_laddr.s_addr == INADDR_ANY)
22303e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_WILD);
22313e98dcb3SMark Johnston 	if (inp->inp_laddr.s_addr == laddr.s_addr)
22323e98dcb3SMark Johnston 		return (INPLOOKUP_MATCH_LADDR);
22333e98dcb3SMark Johnston 	return (INPLOOKUP_MATCH_NONE);
22344130ea61SMark Johnston }
2235e3fd5ffdSRobert Watson 
22367b92493aSMark Johnston #define	INP_LOOKUP_AGAIN	((struct inpcb *)(uintptr_t)-1)
22377b92493aSMark Johnston 
22387b92493aSMark Johnston static struct inpcb *
223921d7ac8cSMark Johnston in_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo, struct in_addr laddr,
2240da806e8dSMark Johnston     u_short lport, int fib, const inp_lookup_t lockflags)
22417b92493aSMark Johnston {
22427b92493aSMark Johnston 	struct inpcbhead *head;
22437b92493aSMark Johnston 	struct inpcb *inp;
22447b92493aSMark Johnston 
22457b92493aSMark Johnston 	KASSERT(SMR_ENTERED(pcbinfo->ipi_smr),
22467b92493aSMark Johnston 	    ("%s: not in SMR read section", __func__));
22477b92493aSMark Johnston 
22487b92493aSMark Johnston 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
22497b92493aSMark Johnston 	    pcbinfo->ipi_hashmask)];
22507b92493aSMark Johnston 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
22517b92493aSMark Johnston 		inp_lookup_match_t match;
22527b92493aSMark Johnston 
2253da806e8dSMark Johnston 		match = in_pcblookup_wild_match(inp, laddr, lport, fib);
22547b92493aSMark Johnston 		if (match == INPLOOKUP_MATCH_NONE)
22557b92493aSMark Johnston 			continue;
22567b92493aSMark Johnston 
22577b92493aSMark Johnston 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
2258da806e8dSMark Johnston 			match = in_pcblookup_wild_match(inp, laddr, lport, fib);
2259a306ed50SMark Johnston 			if (match != INPLOOKUP_MATCH_NONE &&
2260a306ed50SMark Johnston 			    prison_check_ip4_locked(inp->inp_cred->cr_prison,
2261a306ed50SMark Johnston 			    &laddr) == 0)
22627b92493aSMark Johnston 				return (inp);
22637b92493aSMark Johnston 			inp_unlock(inp, lockflags);
22647b92493aSMark Johnston 		}
22657b92493aSMark Johnston 
22667b92493aSMark Johnston 		/*
22677b92493aSMark Johnston 		 * The matching socket disappeared out from under us.  Fall back
22687b92493aSMark Johnston 		 * to a serialized lookup.
22697b92493aSMark Johnston 		 */
22707b92493aSMark Johnston 		return (INP_LOOKUP_AGAIN);
22717b92493aSMark Johnston 	}
22727b92493aSMark Johnston 	return (NULL);
22737b92493aSMark Johnston }
22747b92493aSMark Johnston 
22754130ea61SMark Johnston static struct inpcb *
227621d7ac8cSMark Johnston in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr laddr,
2277da806e8dSMark Johnston     u_short lport, int fib)
22784130ea61SMark Johnston {
22794130ea61SMark Johnston 	struct inpcbhead *head;
22804130ea61SMark Johnston 	struct inpcb *inp, *local_wild, *local_exact, *jail_wild;
2281e3fd5ffdSRobert Watson #ifdef INET6
22824130ea61SMark Johnston 	struct inpcb *local_wild_mapped;
2283e3fd5ffdSRobert Watson #endif
2284413628a7SBjoern A. Zeeb 
22854130ea61SMark Johnston 	INP_HASH_LOCK_ASSERT(pcbinfo);
2286d93ec8cbSMark Johnston 
2287d93ec8cbSMark Johnston 	/*
2288413628a7SBjoern A. Zeeb 	 * Order of socket selection - we always prefer jails.
2289413628a7SBjoern A. Zeeb 	 *      1. jailed, non-wild.
2290413628a7SBjoern A. Zeeb 	 *      2. jailed, wild.
2291413628a7SBjoern A. Zeeb 	 *      3. non-jailed, non-wild.
2292413628a7SBjoern A. Zeeb 	 *      4. non-jailed, wild.
2293413628a7SBjoern A. Zeeb 	 */
2294fdb987beSMark Johnston 	head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
2295a0577692SGleb Smirnoff 	    pcbinfo->ipi_hashmask)];
22964130ea61SMark Johnston 	local_wild = local_exact = jail_wild = NULL;
22974130ea61SMark Johnston #ifdef INET6
22984130ea61SMark Johnston 	local_wild_mapped = NULL;
22994130ea61SMark Johnston #endif
2300fdb987beSMark Johnston 	CK_LIST_FOREACH(inp, head, inp_hash_wild) {
23013e98dcb3SMark Johnston 		inp_lookup_match_t match;
23024130ea61SMark Johnston 		bool injail;
23034130ea61SMark Johnston 
2304da806e8dSMark Johnston 		match = in_pcblookup_wild_match(inp, laddr, lport, fib);
23053e98dcb3SMark Johnston 		if (match == INPLOOKUP_MATCH_NONE)
2306413628a7SBjoern A. Zeeb 			continue;
2307413628a7SBjoern A. Zeeb 
23084130ea61SMark Johnston 		injail = prison_flag(inp->inp_cred, PR_IP4) != 0;
2309413628a7SBjoern A. Zeeb 		if (injail) {
23104130ea61SMark Johnston 			if (prison_check_ip4_locked(inp->inp_cred->cr_prison,
23114130ea61SMark Johnston 			    &laddr) != 0)
2312413628a7SBjoern A. Zeeb 				continue;
2313413628a7SBjoern A. Zeeb 		} else {
2314413628a7SBjoern A. Zeeb 			if (local_exact != NULL)
2315413628a7SBjoern A. Zeeb 				continue;
2316413628a7SBjoern A. Zeeb 		}
2317413628a7SBjoern A. Zeeb 
23183e98dcb3SMark Johnston 		if (match == INPLOOKUP_MATCH_LADDR) {
2319413628a7SBjoern A. Zeeb 			if (injail)
2320c3229e05SDavid Greenman 				return (inp);
2321413628a7SBjoern A. Zeeb 			local_exact = inp;
23223e98dcb3SMark Johnston 		} else {
2323e3fd5ffdSRobert Watson #ifdef INET6
2324413628a7SBjoern A. Zeeb 			/* XXX inp locking, NULL check */
23255cd54324SBjoern A. Zeeb 			if (inp->inp_vflag & INP_IPV6PROTO)
2326cfa1ca9dSYoshinobu Inoue 				local_wild_mapped = inp;
2327cfa1ca9dSYoshinobu Inoue 			else
232883e521ecSBjoern A. Zeeb #endif
2329413628a7SBjoern A. Zeeb 				if (injail)
2330413628a7SBjoern A. Zeeb 					jail_wild = inp;
2331413628a7SBjoern A. Zeeb 				else
23326d6a026bSDavid Greenman 					local_wild = inp;
23336d6a026bSDavid Greenman 		}
23344130ea61SMark Johnston 	}
2335413628a7SBjoern A. Zeeb 	if (jail_wild != NULL)
2336413628a7SBjoern A. Zeeb 		return (jail_wild);
2337413628a7SBjoern A. Zeeb 	if (local_exact != NULL)
2338413628a7SBjoern A. Zeeb 		return (local_exact);
2339413628a7SBjoern A. Zeeb 	if (local_wild != NULL)
2340c3229e05SDavid Greenman 		return (local_wild);
2341413628a7SBjoern A. Zeeb #ifdef INET6
2342413628a7SBjoern A. Zeeb 	if (local_wild_mapped != NULL)
2343413628a7SBjoern A. Zeeb 		return (local_wild_mapped);
234483e521ecSBjoern A. Zeeb #endif
23456d6a026bSDavid Greenman 	return (NULL);
234615bd2b43SDavid Greenman }
2347fa046d87SRobert Watson 
2348fa046d87SRobert Watson /*
23494130ea61SMark Johnston  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
23504130ea61SMark Johnston  * that the caller has either locked the hash list, which usually happens
23514130ea61SMark Johnston  * for bind(2) operations, or is in SMR section, which happens when sorting
23524130ea61SMark Johnston  * out incoming packets.
2353fa046d87SRobert Watson  */
2354fa046d87SRobert Watson static struct inpcb *
23554130ea61SMark Johnston in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
23564130ea61SMark Johnston     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
2357da806e8dSMark Johnston     uint8_t numa_domain, int fib)
23584130ea61SMark Johnston {
23594130ea61SMark Johnston 	struct inpcb *inp;
23604130ea61SMark Johnston 	const u_short fport = fport_arg, lport = lport_arg;
23614130ea61SMark Johnston 
2362da806e8dSMark Johnston 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD | INPLOOKUP_FIB)) == 0,
23634130ea61SMark Johnston 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
23644130ea61SMark Johnston 	KASSERT(faddr.s_addr != INADDR_ANY,
23654130ea61SMark Johnston 	    ("%s: invalid foreign address", __func__));
23664130ea61SMark Johnston 	KASSERT(laddr.s_addr != INADDR_ANY,
23674130ea61SMark Johnston 	    ("%s: invalid local address", __func__));
23687b92493aSMark Johnston 	INP_HASH_WLOCK_ASSERT(pcbinfo);
23694130ea61SMark Johnston 
23704130ea61SMark Johnston 	inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
23714130ea61SMark Johnston 	if (inp != NULL)
23724130ea61SMark Johnston 		return (inp);
23734130ea61SMark Johnston 
23744130ea61SMark Johnston 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
23757b92493aSMark Johnston 		inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport,
2376da806e8dSMark Johnston 		    &laddr, lport, numa_domain, fib);
23774130ea61SMark Johnston 		if (inp == NULL) {
237821d7ac8cSMark Johnston 			inp = in_pcblookup_hash_wild_locked(pcbinfo, laddr,
2379da806e8dSMark Johnston 			    lport, fib);
23804130ea61SMark Johnston 		}
23814130ea61SMark Johnston 	}
23824130ea61SMark Johnston 
23834130ea61SMark Johnston 	return (inp);
23844130ea61SMark Johnston }
23854130ea61SMark Johnston 
23864130ea61SMark Johnston static struct inpcb *
23877b92493aSMark Johnston in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2388fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2389da806e8dSMark Johnston     uint8_t numa_domain, int fib)
2390fa046d87SRobert Watson {
2391fa046d87SRobert Watson 	struct inpcb *inp;
23927b92493aSMark Johnston 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
23937b92493aSMark Johnston 
23947b92493aSMark Johnston 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
23957b92493aSMark Johnston 	    ("%s: LOCKPCB not set", __func__));
23967b92493aSMark Johnston 
23977b92493aSMark Johnston 	INP_HASH_WLOCK(pcbinfo);
23987b92493aSMark Johnston 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
2399da806e8dSMark Johnston 	    lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain, fib);
24007b92493aSMark Johnston 	if (inp != NULL && !inp_trylock(inp, lockflags)) {
24017b92493aSMark Johnston 		in_pcbref(inp);
24027b92493aSMark Johnston 		INP_HASH_WUNLOCK(pcbinfo);
24037b92493aSMark Johnston 		inp_lock(inp, lockflags);
24047b92493aSMark Johnston 		if (in_pcbrele(inp, lockflags))
24057b92493aSMark Johnston 			/* XXX-MJ or retry until we get a negative match? */
24067b92493aSMark Johnston 			inp = NULL;
24077b92493aSMark Johnston 	} else {
24087b92493aSMark Johnston 		INP_HASH_WUNLOCK(pcbinfo);
24097b92493aSMark Johnston 	}
24107b92493aSMark Johnston 	return (inp);
24117b92493aSMark Johnston }
24127b92493aSMark Johnston 
24137b92493aSMark Johnston static struct inpcb *
24147b92493aSMark Johnston in_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr,
24157b92493aSMark Johnston     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
2416da806e8dSMark Johnston     uint8_t numa_domain, int fib)
24177b92493aSMark Johnston {
24187b92493aSMark Johnston 	struct inpcb *inp;
24197b92493aSMark Johnston 	const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
24207b92493aSMark Johnston 	const u_short fport = fport_arg, lport = lport_arg;
2421fa046d87SRobert Watson 
2422675e2618SMark Johnston 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2423675e2618SMark Johnston 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2424675e2618SMark Johnston 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2425675e2618SMark Johnston 	    ("%s: LOCKPCB not set", __func__));
2426675e2618SMark Johnston 
2427db0ac6deSCy Schubert 	smr_enter(pcbinfo->ipi_smr);
24287b92493aSMark Johnston 	inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
2429fa046d87SRobert Watson 	if (inp != NULL) {
24307b92493aSMark Johnston 		if (__predict_true(inp_smr_lock(inp, lockflags))) {
24317b92493aSMark Johnston 			/*
24327b92493aSMark Johnston 			 * Revalidate the 4-tuple, the socket could have been
24337b92493aSMark Johnston 			 * disconnected.
24347b92493aSMark Johnston 			 */
24357b92493aSMark Johnston 			if (__predict_true(in_pcblookup_exact_match(inp,
24367b92493aSMark Johnston 			    faddr, fport, laddr, lport)))
24377b92493aSMark Johnston 				return (inp);
24387b92493aSMark Johnston 			inp_unlock(inp, lockflags);
24397b92493aSMark Johnston 		}
24407b92493aSMark Johnston 
24417b92493aSMark Johnston 		/*
24427b92493aSMark Johnston 		 * We failed to lock the inpcb, or its connection state changed
24437b92493aSMark Johnston 		 * out from under us.  Fall back to a precise search.
24447b92493aSMark Johnston 		 */
24457b92493aSMark Johnston 		return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2446da806e8dSMark Johnston 		    lookupflags, numa_domain, fib));
24477b92493aSMark Johnston 	}
24487b92493aSMark Johnston 
24497b92493aSMark Johnston 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
24507b92493aSMark Johnston 		inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport,
2451da806e8dSMark Johnston 		    &laddr, lport, numa_domain, fib);
24527b92493aSMark Johnston 		if (inp != NULL) {
24537b92493aSMark Johnston 			if (__predict_true(inp_smr_lock(inp, lockflags))) {
24547b92493aSMark Johnston 				if (__predict_true(in_pcblookup_wild_match(inp,
2455da806e8dSMark Johnston 				    laddr, lport, fib) != INPLOOKUP_MATCH_NONE))
24567b92493aSMark Johnston 					return (inp);
24577b92493aSMark Johnston 				inp_unlock(inp, lockflags);
24587b92493aSMark Johnston 			}
24597b92493aSMark Johnston 			inp = INP_LOOKUP_AGAIN;
24607b92493aSMark Johnston 		} else {
246121d7ac8cSMark Johnston 			inp = in_pcblookup_hash_wild_smr(pcbinfo, laddr, lport,
2462da806e8dSMark Johnston 			    fib, lockflags);
24637b92493aSMark Johnston 		}
24647b92493aSMark Johnston 		if (inp == INP_LOOKUP_AGAIN) {
24657b92493aSMark Johnston 			return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr,
2466da806e8dSMark Johnston 			    lport, lookupflags, numa_domain, fib));
24677b92493aSMark Johnston 		}
24687b92493aSMark Johnston 	}
24697b92493aSMark Johnston 
24707b92493aSMark Johnston 	if (inp == NULL)
2471db0ac6deSCy Schubert 		smr_exit(pcbinfo->ipi_smr);
2472d797164aSGleb Smirnoff 
2473fa046d87SRobert Watson 	return (inp);
2474fa046d87SRobert Watson }
2475fa046d87SRobert Watson 
2476fa046d87SRobert Watson /*
2477d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
2478d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
2479fa046d87SRobert Watson  */
2480fa046d87SRobert Watson struct inpcb *
2481fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
24824130ea61SMark Johnston     struct in_addr laddr, u_int lport, int lookupflags,
2483da806e8dSMark Johnston     struct ifnet *ifp)
2484fa046d87SRobert Watson {
2485da806e8dSMark Johnston 	int fib;
2486da806e8dSMark Johnston 
2487da806e8dSMark Johnston 	fib = (lookupflags & INPLOOKUP_FIB) ? if_getfib(ifp) : RT_ALL_FIBS;
24884130ea61SMark Johnston 	return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
2489da806e8dSMark Johnston 	    lookupflags, M_NODOM, fib));
2490fa046d87SRobert Watson }
2491d3c1f003SRobert Watson 
2492d3c1f003SRobert Watson struct inpcb *
2493d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2494d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
24954130ea61SMark Johnston     struct ifnet *ifp __unused, struct mbuf *m)
2496d3c1f003SRobert Watson {
2497da806e8dSMark Johnston 	int fib;
2498da806e8dSMark Johnston 
2499da806e8dSMark Johnston 	M_ASSERTPKTHDR(m);
2500da806e8dSMark Johnston 	fib = (lookupflags & INPLOOKUP_FIB) ? M_GETFIB(m) : RT_ALL_FIBS;
25014130ea61SMark Johnston 	return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
2502da806e8dSMark Johnston 	    lookupflags, m->m_pkthdr.numa_domain, fib));
2503d3c1f003SRobert Watson }
250467107f45SBjoern A. Zeeb #endif /* INET */
250515bd2b43SDavid Greenman 
25067b92493aSMark Johnston static bool
25077b92493aSMark Johnston in_pcbjailed(const struct inpcb *inp, unsigned int flag)
25087b92493aSMark Johnston {
25097b92493aSMark Johnston 	return (prison_flag(inp->inp_cred, flag) != 0);
25107b92493aSMark Johnston }
25117b92493aSMark Johnston 
25127b92493aSMark Johnston /*
25137b92493aSMark Johnston  * Insert the PCB into a hash chain using ordering rules which ensure that
25147b92493aSMark Johnston  * in_pcblookup_hash_wild_*() always encounter the highest-ranking PCB first.
25157b92493aSMark Johnston  *
25167b92493aSMark Johnston  * Specifically, keep jailed PCBs in front of non-jailed PCBs, and keep PCBs
251793998d16SMark Johnston  * with exact local addresses ahead of wildcard PCBs.  Unbound v4-mapped v6 PCBs
251893998d16SMark Johnston  * always appear last no matter whether they are jailed.
25197b92493aSMark Johnston  */
25207b92493aSMark Johnston static void
25217b92493aSMark Johnston _in_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp)
25227b92493aSMark Johnston {
25237b92493aSMark Johnston 	struct inpcb *last;
25247b92493aSMark Johnston 	bool bound, injail;
25257b92493aSMark Johnston 
252693998d16SMark Johnston 	INP_LOCK_ASSERT(inp);
25277b92493aSMark Johnston 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
25287b92493aSMark Johnston 
25297b92493aSMark Johnston 	last = NULL;
25307b92493aSMark Johnston 	bound = inp->inp_laddr.s_addr != INADDR_ANY;
253193998d16SMark Johnston 	if (!bound && (inp->inp_vflag & INP_IPV6PROTO) != 0) {
253293998d16SMark Johnston 		CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) {
253393998d16SMark Johnston 			if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) {
253493998d16SMark Johnston 				CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild);
253593998d16SMark Johnston 				return;
253693998d16SMark Johnston 			}
253793998d16SMark Johnston 		}
253893998d16SMark Johnston 		CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild);
253993998d16SMark Johnston 		return;
254093998d16SMark Johnston 	}
254193998d16SMark Johnston 
25427b92493aSMark Johnston 	injail = in_pcbjailed(inp, PR_IP4);
25437b92493aSMark Johnston 	if (!injail) {
25447b92493aSMark Johnston 		CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) {
254593998d16SMark Johnston 			if (!in_pcbjailed(last, PR_IP4))
25467b92493aSMark Johnston 				break;
25477b92493aSMark Johnston 			if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) {
25487b92493aSMark Johnston 				CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild);
25497b92493aSMark Johnston 				return;
25507b92493aSMark Johnston 			}
25517b92493aSMark Johnston 		}
25527b92493aSMark Johnston 	} else if (!CK_LIST_EMPTY(pcbhash) &&
25537b92493aSMark Johnston 	    !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP4)) {
25547b92493aSMark Johnston 		CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild);
25557b92493aSMark Johnston 		return;
25567b92493aSMark Johnston 	}
25577b92493aSMark Johnston 	if (!bound) {
25587b92493aSMark Johnston 		CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) {
25597b92493aSMark Johnston 			if (last->inp_laddr.s_addr == INADDR_ANY)
25607b92493aSMark Johnston 				break;
25617b92493aSMark Johnston 			if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) {
25627b92493aSMark Johnston 				CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild);
25637b92493aSMark Johnston 				return;
25647b92493aSMark Johnston 			}
25657b92493aSMark Johnston 		}
25667b92493aSMark Johnston 	}
25677b92493aSMark Johnston 	if (last == NULL)
25687b92493aSMark Johnston 		CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild);
25697b92493aSMark Johnston 	else
25707b92493aSMark Johnston 		CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild);
25717b92493aSMark Johnston }
25727b92493aSMark Johnston 
25737b92493aSMark Johnston #ifdef INET6
25747b92493aSMark Johnston /*
25757b92493aSMark Johnston  * See the comment above _in_pcbinshash_wild().
25767b92493aSMark Johnston  */
25777b92493aSMark Johnston static void
25787b92493aSMark Johnston _in6_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp)
25797b92493aSMark Johnston {
25807b92493aSMark Johnston 	struct inpcb *last;
25817b92493aSMark Johnston 	bool bound, injail;
25827b92493aSMark Johnston 
258393998d16SMark Johnston 	INP_LOCK_ASSERT(inp);
25847b92493aSMark Johnston 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
25857b92493aSMark Johnston 
25867b92493aSMark Johnston 	last = NULL;
25877b92493aSMark Johnston 	bound = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr);
25887b92493aSMark Johnston 	injail = in_pcbjailed(inp, PR_IP6);
25897b92493aSMark Johnston 	if (!injail) {
25907b92493aSMark Johnston 		CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) {
259193998d16SMark Johnston 			if (!in_pcbjailed(last, PR_IP6))
25927b92493aSMark Johnston 				break;
25937b92493aSMark Johnston 			if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) {
25947b92493aSMark Johnston 				CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild);
25957b92493aSMark Johnston 				return;
25967b92493aSMark Johnston 			}
25977b92493aSMark Johnston 		}
25987b92493aSMark Johnston 	} else if (!CK_LIST_EMPTY(pcbhash) &&
25997b92493aSMark Johnston 	    !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP6)) {
26007b92493aSMark Johnston 		CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild);
26017b92493aSMark Johnston 		return;
26027b92493aSMark Johnston 	}
26037b92493aSMark Johnston 	if (!bound) {
26047b92493aSMark Johnston 		CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) {
26057b92493aSMark Johnston 			if (IN6_IS_ADDR_UNSPECIFIED(&last->in6p_laddr))
26067b92493aSMark Johnston 				break;
26077b92493aSMark Johnston 			if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) {
26087b92493aSMark Johnston 				CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild);
26097b92493aSMark Johnston 				return;
26107b92493aSMark Johnston 			}
26117b92493aSMark Johnston 		}
26127b92493aSMark Johnston 	}
26137b92493aSMark Johnston 	if (last == NULL)
26147b92493aSMark Johnston 		CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild);
26157b92493aSMark Johnston 	else
26167b92493aSMark Johnston 		CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild);
26177b92493aSMark Johnston }
26187b92493aSMark Johnston #endif
26197b92493aSMark Johnston 
26207bc4aca7SDavid Greenman /*
2621c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
26227bc4aca7SDavid Greenman  */
2623db0ac6deSCy Schubert int
2624db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp)
262515bd2b43SDavid Greenman {
2626*5f539170SGleb Smirnoff 	struct inpcbhead *pcbhash, *pcbporthash;
2627c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2628fdb987beSMark Johnston 	uint32_t hash;
2629fdb987beSMark Johnston 	bool connected;
263015bd2b43SDavid Greenman 
26318501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2632fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2633111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2634111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2635602cc7f1SRobert Watson 
2636cfa1ca9dSYoshinobu Inoue #ifdef INET6
2637fdb987beSMark Johnston 	if (inp->inp_vflag & INP_IPV6) {
2638fdb987beSMark Johnston 		hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport,
2639fdb987beSMark Johnston 		    inp->inp_fport, pcbinfo->ipi_hashmask);
2640fdb987beSMark Johnston 		connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr);
2641fdb987beSMark Johnston 	} else
264283e521ecSBjoern A. Zeeb #endif
2643fdb987beSMark Johnston 	{
2644fdb987beSMark Johnston 		hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport,
2645fdb987beSMark Johnston 		    inp->inp_fport, pcbinfo->ipi_hashmask);
2646fdb987beSMark Johnston 		connected = !in_nullhost(inp->inp_faddr);
2647fdb987beSMark Johnston 	}
2648fdb987beSMark Johnston 
2649fdb987beSMark Johnston 	if (connected)
2650fdb987beSMark Johnston 		pcbhash = &pcbinfo->ipi_hash_exact[hash];
2651fdb987beSMark Johnston 	else
2652fdb987beSMark Johnston 		pcbhash = &pcbinfo->ipi_hash_wild[hash];
265315bd2b43SDavid Greenman 
2654712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2655712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2656c3229e05SDavid Greenman 
2657c3229e05SDavid Greenman 	/*
26581a43cff9SSean Bruno 	 * Add entry to load balance group.
26591a43cff9SSean Bruno 	 * Only do this if SO_REUSEPORT_LB is set.
26601a43cff9SSean Bruno 	 */
2661e3ba0d6aSGleb Smirnoff 	if ((inp->inp_socket->so_options & SO_REUSEPORT_LB) != 0) {
2662a152dd86SMark Johnston 		int error = in_pcbinslbgrouphash(inp, M_NODOM);
2663a152dd86SMark Johnston 		if (error != 0)
2664a152dd86SMark Johnston 			return (error);
26651a43cff9SSean Bruno 	}
26661a43cff9SSean Bruno 
26671a43cff9SSean Bruno 	/*
2668fdb987beSMark Johnston 	 * The PCB may have been disconnected in the past.  Before we can safely
2669fdb987beSMark Johnston 	 * make it visible in the hash table, we must wait for all readers which
2670fdb987beSMark Johnston 	 * may be traversing this PCB to finish.
2671fdb987beSMark Johnston 	 */
2672fdb987beSMark Johnston 	if (inp->inp_smr != SMR_SEQ_INVALID) {
2673fdb987beSMark Johnston 		smr_wait(pcbinfo->ipi_smr, inp->inp_smr);
2674fdb987beSMark Johnston 		inp->inp_smr = SMR_SEQ_INVALID;
2675fdb987beSMark Johnston 	}
2676fdb987beSMark Johnston 
2677fdb987beSMark Johnston 	if (connected)
2678fdb987beSMark Johnston 		CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_exact);
26797b92493aSMark Johnston 	else {
26807b92493aSMark Johnston #ifdef INET6
26817b92493aSMark Johnston 		if ((inp->inp_vflag & INP_IPV6) != 0)
26827b92493aSMark Johnston 			_in6_pcbinshash_wild(pcbhash, inp);
2683fdb987beSMark Johnston 		else
26847b92493aSMark Johnston #endif
26857b92493aSMark Johnston 			_in_pcbinshash_wild(pcbhash, inp);
26867b92493aSMark Johnston 	}
2687*5f539170SGleb Smirnoff 	CK_LIST_INSERT_HEAD(pcbporthash, inp, inp_portlist);
2688111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
2689db0ac6deSCy Schubert 
2690c3229e05SDavid Greenman 	return (0);
269115bd2b43SDavid Greenman }
269215bd2b43SDavid Greenman 
2693fdb987beSMark Johnston void
2694fdb987beSMark Johnston in_pcbremhash_locked(struct inpcb *inp)
26953ba34b07SGleb Smirnoff {
26963ba34b07SGleb Smirnoff 
26973ba34b07SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
2698fdb987beSMark Johnston 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
26993ba34b07SGleb Smirnoff 	MPASS(inp->inp_flags & INP_INHASHLIST);
27003ba34b07SGleb Smirnoff 
2701a43e7a96SGleb Smirnoff 	if ((inp->inp_flags & INP_INLBGROUP) != 0)
27023ba34b07SGleb Smirnoff 		in_pcbremlbgrouphash(inp);
2703fdb987beSMark Johnston #ifdef INET6
2704fdb987beSMark Johnston 	if (inp->inp_vflag & INP_IPV6) {
2705fdb987beSMark Johnston 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
2706fdb987beSMark Johnston 			CK_LIST_REMOVE(inp, inp_hash_wild);
2707fdb987beSMark Johnston 		else
2708fdb987beSMark Johnston 			CK_LIST_REMOVE(inp, inp_hash_exact);
2709fdb987beSMark Johnston 	} else
2710fdb987beSMark Johnston #endif
2711fdb987beSMark Johnston 	{
2712fdb987beSMark Johnston 		if (in_nullhost(inp->inp_faddr))
2713fdb987beSMark Johnston 			CK_LIST_REMOVE(inp, inp_hash_wild);
2714fdb987beSMark Johnston 		else
2715fdb987beSMark Johnston 			CK_LIST_REMOVE(inp, inp_hash_exact);
2716fdb987beSMark Johnston 	}
27173ba34b07SGleb Smirnoff 	CK_LIST_REMOVE(inp, inp_portlist);
27183ba34b07SGleb Smirnoff 	inp->inp_flags &= ~INP_INHASHLIST;
27193ba34b07SGleb Smirnoff }
27203ba34b07SGleb Smirnoff 
2721fdb987beSMark Johnston static void
2722fdb987beSMark Johnston in_pcbremhash(struct inpcb *inp)
2723fdb987beSMark Johnston {
2724fdb987beSMark Johnston 	INP_HASH_WLOCK(inp->inp_pcbinfo);
2725fdb987beSMark Johnston 	in_pcbremhash_locked(inp);
2726fdb987beSMark Johnston 	INP_HASH_WUNLOCK(inp->inp_pcbinfo);
2727fdb987beSMark Johnston }
2728fdb987beSMark Johnston 
272952cd27cbSRobert Watson /*
2730c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2731c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2732c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2733c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2734c3229e05SDavid Greenman  */
273515bd2b43SDavid Greenman void
2736db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp)
273715bd2b43SDavid Greenman {
273859daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
273915bd2b43SDavid Greenman 	struct inpcbhead *head;
2740fdb987beSMark Johnston 	uint32_t hash;
2741fdb987beSMark Johnston 	bool connected;
274215bd2b43SDavid Greenman 
27438501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2744fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2745111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2746fdb987beSMark Johnston 	    ("%s: !INP_INHASHLIST", __func__));
2747fdb987beSMark Johnston 	KASSERT(inp->inp_smr == SMR_SEQ_INVALID,
2748fdb987beSMark Johnston 	    ("%s: inp was disconnected", __func__));
2749602cc7f1SRobert Watson 
2750cfa1ca9dSYoshinobu Inoue #ifdef INET6
2751fdb987beSMark Johnston 	if (inp->inp_vflag & INP_IPV6) {
2752fdb987beSMark Johnston 		hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport,
2753fdb987beSMark Johnston 		    inp->inp_fport, pcbinfo->ipi_hashmask);
2754fdb987beSMark Johnston 		connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr);
2755fdb987beSMark Johnston 	} else
275683e521ecSBjoern A. Zeeb #endif
2757fdb987beSMark Johnston 	{
2758fdb987beSMark Johnston 		hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport,
2759fdb987beSMark Johnston 		    inp->inp_fport, pcbinfo->ipi_hashmask);
2760fdb987beSMark Johnston 		connected = !in_nullhost(inp->inp_faddr);
2761fdb987beSMark Johnston 	}
276215bd2b43SDavid Greenman 
2763fdb987beSMark Johnston 	/*
2764fdb987beSMark Johnston 	 * When rehashing, the caller must ensure that either the new or the old
2765fdb987beSMark Johnston 	 * foreign address was unspecified.
2766fdb987beSMark Johnston 	 */
2767fdb987beSMark Johnston 	if (connected)
2768fdb987beSMark Johnston 		CK_LIST_REMOVE(inp, inp_hash_wild);
2769fdb987beSMark Johnston 	else
2770fdb987beSMark Johnston 		CK_LIST_REMOVE(inp, inp_hash_exact);
2771fdb987beSMark Johnston 
2772fdb987beSMark Johnston 	if (connected) {
2773fdb987beSMark Johnston 		head = &pcbinfo->ipi_hash_exact[hash];
2774fdb987beSMark Johnston 		CK_LIST_INSERT_HEAD(head, inp, inp_hash_exact);
2775fdb987beSMark Johnston 	} else {
2776fdb987beSMark Johnston 		head = &pcbinfo->ipi_hash_wild[hash];
2777fdb987beSMark Johnston 		CK_LIST_INSERT_HEAD(head, inp, inp_hash_wild);
2778fdb987beSMark Johnston 	}
2779c3229e05SDavid Greenman }
2780c3229e05SDavid Greenman 
2781c3229e05SDavid Greenman /*
278284cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
278384cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
278484cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
278584cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
278684cc0778SGeorge V. Neville-Neil  */
278784cc0778SGeorge V. Neville-Neil void
278884cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
278984cc0778SGeorge V. Neville-Neil {
279084cc0778SGeorge V. Neville-Neil 
2791fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
279284cc0778SGeorge V. Neville-Neil 	return;
279384cc0778SGeorge V. Neville-Neil }
279484cc0778SGeorge V. Neville-Neil 
279584cc0778SGeorge V. Neville-Neil /*
2796a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2797a557af22SRobert Watson  * label change into the in_pcb for the socket.
2798a557af22SRobert Watson  */
2799a557af22SRobert Watson void
2800136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2801a557af22SRobert Watson {
2802a557af22SRobert Watson #ifdef MAC
2803a557af22SRobert Watson 	struct inpcb *inp;
2804a557af22SRobert Watson 
28054c7c478dSRobert Watson 	inp = sotoinpcb(so);
28064c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2807602cc7f1SRobert Watson 
28088501a69cSRobert Watson 	INP_WLOCK(inp);
2809310e7cebSRobert Watson 	SOCK_LOCK(so);
2810a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2811310e7cebSRobert Watson 	SOCK_UNLOCK(so);
28128501a69cSRobert Watson 	INP_WUNLOCK(inp);
2813a557af22SRobert Watson #endif
2814a557af22SRobert Watson }
28155f311da2SMike Silbersack 
28163d585327SKip Macy void
28173d585327SKip Macy inp_wlock(struct inpcb *inp)
28183d585327SKip Macy {
28193d585327SKip Macy 
28208501a69cSRobert Watson 	INP_WLOCK(inp);
28213d585327SKip Macy }
28223d585327SKip Macy 
28233d585327SKip Macy void
28243d585327SKip Macy inp_wunlock(struct inpcb *inp)
28253d585327SKip Macy {
28263d585327SKip Macy 
28278501a69cSRobert Watson 	INP_WUNLOCK(inp);
28283d585327SKip Macy }
28293d585327SKip Macy 
28303d585327SKip Macy void
28313d585327SKip Macy inp_rlock(struct inpcb *inp)
28323d585327SKip Macy {
28333d585327SKip Macy 
2834a69042a5SRobert Watson 	INP_RLOCK(inp);
28353d585327SKip Macy }
28363d585327SKip Macy 
28373d585327SKip Macy void
28383d585327SKip Macy inp_runlock(struct inpcb *inp)
28393d585327SKip Macy {
28403d585327SKip Macy 
2841a69042a5SRobert Watson 	INP_RUNLOCK(inp);
28423d585327SKip Macy }
28433d585327SKip Macy 
2844c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
28453d585327SKip Macy void
2846e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
28473d585327SKip Macy {
28483d585327SKip Macy 
28498501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
28503d585327SKip Macy }
28513d585327SKip Macy 
28523d585327SKip Macy void
2853e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
28543d585327SKip Macy {
28553d585327SKip Macy 
28563d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
28573d585327SKip Macy }
28583d585327SKip Macy #endif
28593d585327SKip Macy 
28609378e437SKip Macy void
286124cf7a8dSGleb Smirnoff inp_apply_all(struct inpcbinfo *pcbinfo,
286224cf7a8dSGleb Smirnoff     void (*func)(struct inpcb *, void *), void *arg)
28639378e437SKip Macy {
286424cf7a8dSGleb Smirnoff 	struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo,
2865db0ac6deSCy Schubert 	    INPLOOKUP_WLOCKPCB);
28669378e437SKip Macy 	struct inpcb *inp;
28679378e437SKip Macy 
2868db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL)
28699378e437SKip Macy 		func(inp, arg);
28709378e437SKip Macy }
28719378e437SKip Macy 
28729378e437SKip Macy struct socket *
28739378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
28749378e437SKip Macy {
28759378e437SKip Macy 
28769378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
28779378e437SKip Macy 	return (inp->inp_socket);
28789378e437SKip Macy }
28799378e437SKip Macy 
28809378e437SKip Macy void
2881df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
28829d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
28839378e437SKip Macy {
28849378e437SKip Macy 
28859d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2886df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2887df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
28889378e437SKip Macy 	*lp = inp->inp_lport;
28899378e437SKip Macy 	*fp = inp->inp_fport;
28909378e437SKip Macy }
28919378e437SKip Macy 
2892cc65eb4eSGleb Smirnoff /*
2893cc65eb4eSGleb Smirnoff  * Create an external-format (``xinpcb'') structure using the information in
2894cc65eb4eSGleb Smirnoff  * the kernel-format in_pcb structure pointed to by inp.  This is done to
2895cc65eb4eSGleb Smirnoff  * reduce the spew of irrelevant information over this interface, to isolate
2896cc65eb4eSGleb Smirnoff  * user code from changes in the kernel structure, and potentially to provide
2897cc65eb4eSGleb Smirnoff  * information-hiding if we decide that some of this information should be
2898cc65eb4eSGleb Smirnoff  * hidden from users.
2899cc65eb4eSGleb Smirnoff  */
2900cc65eb4eSGleb Smirnoff void
2901cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi)
2902cc65eb4eSGleb Smirnoff {
2903cc65eb4eSGleb Smirnoff 
290479db6fe7SMark Johnston 	bzero(xi, sizeof(*xi));
2905cc65eb4eSGleb Smirnoff 	xi->xi_len = sizeof(struct xinpcb);
2906cc65eb4eSGleb Smirnoff 	if (inp->inp_socket)
2907cc65eb4eSGleb Smirnoff 		sotoxsocket(inp->inp_socket, &xi->xi_socket);
2908cc65eb4eSGleb Smirnoff 	bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo));
2909cc65eb4eSGleb Smirnoff 	xi->inp_gencnt = inp->inp_gencnt;
2910cc65eb4eSGleb Smirnoff 	xi->inp_flow = inp->inp_flow;
2911cc65eb4eSGleb Smirnoff 	xi->inp_flowid = inp->inp_flowid;
2912cc65eb4eSGleb Smirnoff 	xi->inp_flowtype = inp->inp_flowtype;
2913cc65eb4eSGleb Smirnoff 	xi->inp_flags = inp->inp_flags;
2914cc65eb4eSGleb Smirnoff 	xi->inp_flags2 = inp->inp_flags2;
2915cc65eb4eSGleb Smirnoff 	xi->in6p_cksum = inp->in6p_cksum;
2916cc65eb4eSGleb Smirnoff 	xi->in6p_hops = inp->in6p_hops;
2917cc65eb4eSGleb Smirnoff 	xi->inp_ip_tos = inp->inp_ip_tos;
2918cc65eb4eSGleb Smirnoff 	xi->inp_vflag = inp->inp_vflag;
2919cc65eb4eSGleb Smirnoff 	xi->inp_ip_ttl = inp->inp_ip_ttl;
2920cc65eb4eSGleb Smirnoff 	xi->inp_ip_p = inp->inp_ip_p;
2921cc65eb4eSGleb Smirnoff 	xi->inp_ip_minttl = inp->inp_ip_minttl;
2922cc65eb4eSGleb Smirnoff }
2923cc65eb4eSGleb Smirnoff 
2924a35bdd44SMichael Tuexen int
2925a35bdd44SMichael Tuexen sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo,
2926a35bdd44SMichael Tuexen     int (*ctloutput_set)(struct inpcb *, struct sockopt *))
2927a35bdd44SMichael Tuexen {
2928a35bdd44SMichael Tuexen 	struct sockopt sopt;
2929a35bdd44SMichael Tuexen 	struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo,
2930a35bdd44SMichael Tuexen 	    INPLOOKUP_WLOCKPCB);
2931a35bdd44SMichael Tuexen 	struct inpcb *inp;
2932a35bdd44SMichael Tuexen 	struct sockopt_parameters *params;
2933a35bdd44SMichael Tuexen 	struct socket *so;
2934a35bdd44SMichael Tuexen 	int error;
2935a35bdd44SMichael Tuexen 	char buf[1024];
2936a35bdd44SMichael Tuexen 
2937a35bdd44SMichael Tuexen 	if (req->oldptr != NULL || req->oldlen != 0)
2938a35bdd44SMichael Tuexen 		return (EINVAL);
2939a35bdd44SMichael Tuexen 	if (req->newptr == NULL)
2940a35bdd44SMichael Tuexen 		return (EPERM);
2941a35bdd44SMichael Tuexen 	if (req->newlen > sizeof(buf))
2942a35bdd44SMichael Tuexen 		return (ENOMEM);
2943a35bdd44SMichael Tuexen 	error = SYSCTL_IN(req, buf, req->newlen);
2944a35bdd44SMichael Tuexen 	if (error != 0)
2945a35bdd44SMichael Tuexen 		return (error);
2946a35bdd44SMichael Tuexen 	if (req->newlen < sizeof(struct sockopt_parameters))
2947a35bdd44SMichael Tuexen 		return (EINVAL);
2948a35bdd44SMichael Tuexen 	params = (struct sockopt_parameters *)buf;
2949a35bdd44SMichael Tuexen 	sopt.sopt_level = params->sop_level;
2950a35bdd44SMichael Tuexen 	sopt.sopt_name = params->sop_optname;
2951a35bdd44SMichael Tuexen 	sopt.sopt_dir = SOPT_SET;
2952a35bdd44SMichael Tuexen 	sopt.sopt_val = params->sop_optval;
2953a35bdd44SMichael Tuexen 	sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters);
2954a35bdd44SMichael Tuexen 	sopt.sopt_td = NULL;
2955a0aeb1ceSMichael Tuexen #ifdef INET6
2956a35bdd44SMichael Tuexen 	if (params->sop_inc.inc_flags & INC_ISIPV6) {
2957a35bdd44SMichael Tuexen 		if (IN6_IS_SCOPE_LINKLOCAL(&params->sop_inc.inc6_laddr))
2958a35bdd44SMichael Tuexen 			params->sop_inc.inc6_laddr.s6_addr16[1] =
2959a35bdd44SMichael Tuexen 			    htons(params->sop_inc.inc6_zoneid & 0xffff);
2960a35bdd44SMichael Tuexen 		if (IN6_IS_SCOPE_LINKLOCAL(&params->sop_inc.inc6_faddr))
2961a35bdd44SMichael Tuexen 			params->sop_inc.inc6_faddr.s6_addr16[1] =
2962a35bdd44SMichael Tuexen 			    htons(params->sop_inc.inc6_zoneid & 0xffff);
2963a35bdd44SMichael Tuexen 	}
2964a0aeb1ceSMichael Tuexen #endif
296513720136SMichael Tuexen 	if (params->sop_inc.inc_lport != htons(0) &&
296613720136SMichael Tuexen 	    params->sop_inc.inc_fport != htons(0)) {
2967a0aeb1ceSMichael Tuexen #ifdef INET6
2968a35bdd44SMichael Tuexen 		if (params->sop_inc.inc_flags & INC_ISIPV6)
2969a35bdd44SMichael Tuexen 			inpi.hash = INP6_PCBHASH(
2970a35bdd44SMichael Tuexen 			    &params->sop_inc.inc6_faddr,
2971a35bdd44SMichael Tuexen 			    params->sop_inc.inc_lport,
2972a35bdd44SMichael Tuexen 			    params->sop_inc.inc_fport,
2973a35bdd44SMichael Tuexen 			    pcbinfo->ipi_hashmask);
2974a35bdd44SMichael Tuexen 		else
2975a0aeb1ceSMichael Tuexen #endif
2976a35bdd44SMichael Tuexen 			inpi.hash = INP_PCBHASH(
2977a35bdd44SMichael Tuexen 			    &params->sop_inc.inc_faddr,
2978a35bdd44SMichael Tuexen 			    params->sop_inc.inc_lport,
2979a35bdd44SMichael Tuexen 			    params->sop_inc.inc_fport,
2980a35bdd44SMichael Tuexen 			    pcbinfo->ipi_hashmask);
2981a35bdd44SMichael Tuexen 	}
2982a35bdd44SMichael Tuexen 	while ((inp = inp_next(&inpi)) != NULL)
2983a35bdd44SMichael Tuexen 		if (inp->inp_gencnt == params->sop_id) {
298453af6903SGleb Smirnoff 			if (inp->inp_flags & INP_DROPPED) {
2985a35bdd44SMichael Tuexen 				INP_WUNLOCK(inp);
2986a35bdd44SMichael Tuexen 				return (ECONNRESET);
2987a35bdd44SMichael Tuexen 			}
2988a35bdd44SMichael Tuexen 			so = inp->inp_socket;
2989a35bdd44SMichael Tuexen 			KASSERT(so != NULL, ("inp_socket == NULL"));
2990a35bdd44SMichael Tuexen 			soref(so);
2991bed7633bSMichael Tuexen 			if (params->sop_level == SOL_SOCKET) {
2992bed7633bSMichael Tuexen 				INP_WUNLOCK(inp);
2993bed7633bSMichael Tuexen 				error = sosetopt(so, &sopt);
2994bed7633bSMichael Tuexen 			} else
2995a35bdd44SMichael Tuexen 				error = (*ctloutput_set)(inp, &sopt);
2996a35bdd44SMichael Tuexen 			sorele(so);
2997a35bdd44SMichael Tuexen 			break;
2998a35bdd44SMichael Tuexen 		}
2999a35bdd44SMichael Tuexen 	if (inp == NULL)
3000a35bdd44SMichael Tuexen 		error = ESRCH;
3001a35bdd44SMichael Tuexen 	return (error);
3002a35bdd44SMichael Tuexen }
3003a35bdd44SMichael Tuexen 
3004497057eeSRobert Watson #ifdef DDB
3005497057eeSRobert Watson static void
3006497057eeSRobert Watson db_print_indent(int indent)
3007497057eeSRobert Watson {
3008497057eeSRobert Watson 	int i;
3009497057eeSRobert Watson 
3010497057eeSRobert Watson 	for (i = 0; i < indent; i++)
3011497057eeSRobert Watson 		db_printf(" ");
3012497057eeSRobert Watson }
3013497057eeSRobert Watson 
3014497057eeSRobert Watson static void
3015497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
3016497057eeSRobert Watson {
3017497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
3018497057eeSRobert Watson 
3019497057eeSRobert Watson 	db_print_indent(indent);
3020497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
3021497057eeSRobert Watson 
3022497057eeSRobert Watson 	indent += 2;
3023497057eeSRobert Watson 
302403dc38a4SRobert Watson #ifdef INET6
3025dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3026497057eeSRobert Watson 		/* IPv6. */
3027497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
3028497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
302983e521ecSBjoern A. Zeeb 	} else
303003dc38a4SRobert Watson #endif
303183e521ecSBjoern A. Zeeb 	{
3032497057eeSRobert Watson 		/* IPv4. */
3033497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
3034497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
3035497057eeSRobert Watson 	}
3036497057eeSRobert Watson 	db_print_indent(indent);
3037497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
3038497057eeSRobert Watson 	    ntohs(inc->inc_lport));
3039497057eeSRobert Watson 	db_print_indent(indent);
3040497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
3041497057eeSRobert Watson 	    ntohs(inc->inc_fport));
3042497057eeSRobert Watson }
3043497057eeSRobert Watson 
3044497057eeSRobert Watson static void
3045497057eeSRobert Watson db_print_inpflags(int inp_flags)
3046497057eeSRobert Watson {
3047497057eeSRobert Watson 	int comma;
3048497057eeSRobert Watson 
3049497057eeSRobert Watson 	comma = 0;
3050497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
3051497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
3052497057eeSRobert Watson 		comma = 1;
3053497057eeSRobert Watson 	}
3054497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
3055497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
3056497057eeSRobert Watson 		comma = 1;
3057497057eeSRobert Watson 	}
3058497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
3059497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
3060497057eeSRobert Watson 		comma = 1;
3061497057eeSRobert Watson 	}
3062dce33a45SErmal Luçi 	if (inp_flags & INP_ORIGDSTADDR) {
3063dce33a45SErmal Luçi 		db_printf("%sINP_ORIGDSTADDR", comma ? ", " : "");
3064dce33a45SErmal Luçi 		comma = 1;
3065dce33a45SErmal Luçi 	}
3066497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
3067497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
3068497057eeSRobert Watson 		comma = 1;
3069497057eeSRobert Watson 	}
3070497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
3071497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
3072497057eeSRobert Watson 		comma = 1;
3073497057eeSRobert Watson 	}
3074497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
3075497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
3076497057eeSRobert Watson 		comma = 1;
3077497057eeSRobert Watson 	}
3078497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
3079497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
3080497057eeSRobert Watson 		comma = 1;
3081497057eeSRobert Watson 	}
3082497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
3083497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
3084497057eeSRobert Watson 		comma = 1;
3085497057eeSRobert Watson 	}
3086497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
3087497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
3088497057eeSRobert Watson 		comma = 1;
3089497057eeSRobert Watson 	}
3090497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
3091497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
3092497057eeSRobert Watson 		comma = 1;
3093497057eeSRobert Watson 	}
3094497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
3095497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
3096497057eeSRobert Watson 		comma = 1;
3097497057eeSRobert Watson 	}
30983cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
30993cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
31003cca425bSMichael Tuexen 		comma = 1;
31013cca425bSMichael Tuexen 	}
3102497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
3103497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
3104497057eeSRobert Watson 		comma = 1;
3105497057eeSRobert Watson 	}
3106497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
3107497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
3108497057eeSRobert Watson 		comma = 1;
3109497057eeSRobert Watson 	}
3110497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
3111497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
3112497057eeSRobert Watson 		comma = 1;
3113497057eeSRobert Watson 	}
3114497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
3115497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
3116497057eeSRobert Watson 		comma = 1;
3117497057eeSRobert Watson 	}
3118497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
3119497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
3120497057eeSRobert Watson 		comma = 1;
3121497057eeSRobert Watson 	}
3122497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
3123497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
3124497057eeSRobert Watson 		comma = 1;
3125497057eeSRobert Watson 	}
3126497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
3127497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
3128497057eeSRobert Watson 		comma = 1;
3129497057eeSRobert Watson 	}
3130497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
3131497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
3132497057eeSRobert Watson 		comma = 1;
3133497057eeSRobert Watson 	}
3134497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
3135497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
3136497057eeSRobert Watson 		comma = 1;
3137497057eeSRobert Watson 	}
3138ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
3139ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
3140ad71fe3cSRobert Watson 		comma  = 1;
3141ad71fe3cSRobert Watson 	}
3142ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
3143ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
3144ad71fe3cSRobert Watson 		comma  = 1;
3145ad71fe3cSRobert Watson 	}
3146ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
3147ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
3148ad71fe3cSRobert Watson 		comma  = 1;
3149ad71fe3cSRobert Watson 	}
3150497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
3151497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
3152497057eeSRobert Watson 		comma = 1;
3153497057eeSRobert Watson 	}
3154497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
3155497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
3156497057eeSRobert Watson 		comma = 1;
3157497057eeSRobert Watson 	}
3158497057eeSRobert Watson }
3159497057eeSRobert Watson 
3160497057eeSRobert Watson static void
3161497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
3162497057eeSRobert Watson {
3163497057eeSRobert Watson 	int comma;
3164497057eeSRobert Watson 
3165497057eeSRobert Watson 	comma = 0;
3166497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
3167497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
3168497057eeSRobert Watson 		comma  = 1;
3169497057eeSRobert Watson 	}
3170497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
3171497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
3172497057eeSRobert Watson 		comma  = 1;
3173497057eeSRobert Watson 	}
3174497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
3175497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
3176497057eeSRobert Watson 		comma  = 1;
3177497057eeSRobert Watson 	}
3178497057eeSRobert Watson }
3179497057eeSRobert Watson 
31806d888973SRobert Watson static void
3181497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
3182497057eeSRobert Watson {
3183497057eeSRobert Watson 
3184497057eeSRobert Watson 	db_print_indent(indent);
3185497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
3186497057eeSRobert Watson 
3187497057eeSRobert Watson 	indent += 2;
3188497057eeSRobert Watson 
3189497057eeSRobert Watson 	db_print_indent(indent);
3190497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
3191497057eeSRobert Watson 
3192497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
3193497057eeSRobert Watson 
3194497057eeSRobert Watson 	db_print_indent(indent);
3195497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
3196497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
3197497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
3198497057eeSRobert Watson 	db_printf(")\n");
3199497057eeSRobert Watson 
3200497057eeSRobert Watson 	db_print_indent(indent);
3201497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
3202497057eeSRobert Watson 	    inp->inp_vflag);
3203497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
3204497057eeSRobert Watson 	db_printf(")\n");
3205497057eeSRobert Watson 
3206497057eeSRobert Watson 	db_print_indent(indent);
3207497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
3208497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
3209497057eeSRobert Watson 
3210497057eeSRobert Watson 	db_print_indent(indent);
3211497057eeSRobert Watson #ifdef INET6
3212497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
3213497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
3214497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
3215497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
3216497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
3217497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
3218497057eeSRobert Watson 		    inp->in6p_hops);
3219497057eeSRobert Watson 	} else
3220497057eeSRobert Watson #endif
3221497057eeSRobert Watson 	{
3222497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
3223497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
3224497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
3225497057eeSRobert Watson 	}
3226497057eeSRobert Watson 
3227497057eeSRobert Watson 	db_print_indent(indent);
3228*5f539170SGleb Smirnoff 	db_printf("inp_gencnt: %ju\n", (uintmax_t)inp->inp_gencnt);
3229497057eeSRobert Watson }
3230497057eeSRobert Watson 
3231497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
3232497057eeSRobert Watson {
3233497057eeSRobert Watson 	struct inpcb *inp;
3234497057eeSRobert Watson 
3235497057eeSRobert Watson 	if (!have_addr) {
3236497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
3237497057eeSRobert Watson 		return;
3238497057eeSRobert Watson 	}
3239497057eeSRobert Watson 	inp = (struct inpcb *)addr;
3240497057eeSRobert Watson 
3241497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
3242497057eeSRobert Watson }
324383e521ecSBjoern A. Zeeb #endif /* DDB */
3244f3e7afe2SHans Petter Selasky 
3245f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
3246f3e7afe2SHans Petter Selasky /*
3247f3e7afe2SHans Petter Selasky  * Modify TX rate limit based on the existing "inp->inp_snd_tag",
3248f3e7afe2SHans Petter Selasky  * if any.
3249f3e7afe2SHans Petter Selasky  */
3250f3e7afe2SHans Petter Selasky int
3251f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate)
3252f3e7afe2SHans Petter Selasky {
3253f3e7afe2SHans Petter Selasky 	union if_snd_tag_modify_params params = {
3254f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
325520abea66SRandall Stewart 		.rate_limit.flags = M_NOWAIT,
3256f3e7afe2SHans Petter Selasky 	};
3257f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3258f3e7afe2SHans Petter Selasky 	int error;
3259f3e7afe2SHans Petter Selasky 
3260f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3261f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3262f3e7afe2SHans Petter Selasky 		return (EINVAL);
3263f3e7afe2SHans Petter Selasky 
3264c782ea8bSJohn Baldwin 	if (mst->sw->snd_tag_modify == NULL) {
3265f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
3266f3e7afe2SHans Petter Selasky 	} else {
3267c782ea8bSJohn Baldwin 		error = mst->sw->snd_tag_modify(mst, &params);
3268f3e7afe2SHans Petter Selasky 	}
3269f3e7afe2SHans Petter Selasky 	return (error);
3270f3e7afe2SHans Petter Selasky }
3271f3e7afe2SHans Petter Selasky 
3272f3e7afe2SHans Petter Selasky /*
3273f3e7afe2SHans Petter Selasky  * Query existing TX rate limit based on the existing
3274f3e7afe2SHans Petter Selasky  * "inp->inp_snd_tag", if any.
3275f3e7afe2SHans Petter Selasky  */
3276f3e7afe2SHans Petter Selasky int
3277f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate)
3278f3e7afe2SHans Petter Selasky {
3279f3e7afe2SHans Petter Selasky 	union if_snd_tag_query_params params = { };
3280f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3281f3e7afe2SHans Petter Selasky 	int error;
3282f3e7afe2SHans Petter Selasky 
3283f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3284f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3285f3e7afe2SHans Petter Selasky 		return (EINVAL);
3286f3e7afe2SHans Petter Selasky 
3287c782ea8bSJohn Baldwin 	if (mst->sw->snd_tag_query == NULL) {
3288f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
3289f3e7afe2SHans Petter Selasky 	} else {
3290c782ea8bSJohn Baldwin 		error = mst->sw->snd_tag_query(mst, &params);
3291f3e7afe2SHans Petter Selasky 		if (error == 0 && p_max_pacing_rate != NULL)
3292f3e7afe2SHans Petter Selasky 			*p_max_pacing_rate = params.rate_limit.max_rate;
3293f3e7afe2SHans Petter Selasky 	}
3294f3e7afe2SHans Petter Selasky 	return (error);
3295f3e7afe2SHans Petter Selasky }
3296f3e7afe2SHans Petter Selasky 
3297f3e7afe2SHans Petter Selasky /*
329895ed5015SHans Petter Selasky  * Query existing TX queue level based on the existing
329995ed5015SHans Petter Selasky  * "inp->inp_snd_tag", if any.
330095ed5015SHans Petter Selasky  */
330195ed5015SHans Petter Selasky int
330295ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level)
330395ed5015SHans Petter Selasky {
330495ed5015SHans Petter Selasky 	union if_snd_tag_query_params params = { };
330595ed5015SHans Petter Selasky 	struct m_snd_tag *mst;
330695ed5015SHans Petter Selasky 	int error;
330795ed5015SHans Petter Selasky 
330895ed5015SHans Petter Selasky 	mst = inp->inp_snd_tag;
330995ed5015SHans Petter Selasky 	if (mst == NULL)
331095ed5015SHans Petter Selasky 		return (EINVAL);
331195ed5015SHans Petter Selasky 
3312c782ea8bSJohn Baldwin 	if (mst->sw->snd_tag_query == NULL)
331395ed5015SHans Petter Selasky 		return (EOPNOTSUPP);
331495ed5015SHans Petter Selasky 
3315c782ea8bSJohn Baldwin 	error = mst->sw->snd_tag_query(mst, &params);
331695ed5015SHans Petter Selasky 	if (error == 0 && p_txqueue_level != NULL)
331795ed5015SHans Petter Selasky 		*p_txqueue_level = params.rate_limit.queue_level;
331895ed5015SHans Petter Selasky 	return (error);
331995ed5015SHans Petter Selasky }
332095ed5015SHans Petter Selasky 
332195ed5015SHans Petter Selasky /*
3322f3e7afe2SHans Petter Selasky  * Allocate a new TX rate limit send tag from the network interface
3323f3e7afe2SHans Petter Selasky  * given by the "ifp" argument and save it in "inp->inp_snd_tag":
3324f3e7afe2SHans Petter Selasky  */
3325f3e7afe2SHans Petter Selasky int
3326f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp,
332720abea66SRandall Stewart     uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st)
332820abea66SRandall Stewart 
3329f3e7afe2SHans Petter Selasky {
3330f3e7afe2SHans Petter Selasky 	union if_snd_tag_alloc_params params = {
333195ed5015SHans Petter Selasky 		.rate_limit.hdr.type = (max_pacing_rate == -1U) ?
333295ed5015SHans Petter Selasky 		    IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT,
3333f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowid = flowid,
3334f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowtype = flowtype,
333598085baeSAndrew Gallatin 		.rate_limit.hdr.numa_domain = inp->inp_numa_domain,
3336f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
333720abea66SRandall Stewart 		.rate_limit.flags = M_NOWAIT,
3338f3e7afe2SHans Petter Selasky 	};
3339f3e7afe2SHans Petter Selasky 	int error;
3340f3e7afe2SHans Petter Selasky 
3341f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
3342f3e7afe2SHans Petter Selasky 
334385d8d30fSHans Petter Selasky 	/*
334485d8d30fSHans Petter Selasky 	 * If there is already a send tag, or the INP is being torn
334585d8d30fSHans Petter Selasky 	 * down, allocating a new send tag is not allowed. Else send
334685d8d30fSHans Petter Selasky 	 * tags may leak.
334785d8d30fSHans Petter Selasky 	 */
334853af6903SGleb Smirnoff 	if (*st != NULL || (inp->inp_flags & INP_DROPPED) != 0)
3349f3e7afe2SHans Petter Selasky 		return (EINVAL);
3350f3e7afe2SHans Petter Selasky 
335136e0a362SJohn Baldwin 	error = m_snd_tag_alloc(ifp, &params, st);
3352903c4ee6SXin LI #ifdef INET
335320abea66SRandall Stewart 	if (error == 0) {
335420abea66SRandall Stewart 		counter_u64_add(rate_limit_set_ok, 1);
335520abea66SRandall Stewart 		counter_u64_add(rate_limit_active, 1);
335636e0a362SJohn Baldwin 	} else if (error != EOPNOTSUPP)
335720abea66SRandall Stewart 		  counter_u64_add(rate_limit_alloc_fail, 1);
3358903c4ee6SXin LI #endif
3359f3e7afe2SHans Petter Selasky 	return (error);
3360f3e7afe2SHans Petter Selasky }
3361f3e7afe2SHans Petter Selasky 
336220abea66SRandall Stewart void
336398d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst)
336420abea66SRandall Stewart {
336520abea66SRandall Stewart 
336698d7a8d9SJohn Baldwin 	m_snd_tag_rele(mst);
3367903c4ee6SXin LI #ifdef INET
336820abea66SRandall Stewart 	counter_u64_add(rate_limit_active, -1);
3369903c4ee6SXin LI #endif
337020abea66SRandall Stewart }
337120abea66SRandall Stewart 
3372f3e7afe2SHans Petter Selasky /*
3373f3e7afe2SHans Petter Selasky  * Free an existing TX rate limit tag based on the "inp->inp_snd_tag",
3374f3e7afe2SHans Petter Selasky  * if any:
3375f3e7afe2SHans Petter Selasky  */
3376f3e7afe2SHans Petter Selasky void
3377f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp)
3378f3e7afe2SHans Petter Selasky {
3379f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
3380f3e7afe2SHans Petter Selasky 
3381f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
3382f3e7afe2SHans Petter Selasky 
3383f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
3384f3e7afe2SHans Petter Selasky 	inp->inp_snd_tag = NULL;
3385f3e7afe2SHans Petter Selasky 
3386f3e7afe2SHans Petter Selasky 	if (mst == NULL)
3387f3e7afe2SHans Petter Selasky 		return;
3388f3e7afe2SHans Petter Selasky 
3389fb3bc596SJohn Baldwin 	m_snd_tag_rele(mst);
3390093e7231SHans Petter Selasky #ifdef INET
3391093e7231SHans Petter Selasky 	counter_u64_add(rate_limit_active, -1);
3392093e7231SHans Petter Selasky #endif
3393f3e7afe2SHans Petter Selasky }
3394f3e7afe2SHans Petter Selasky 
339520abea66SRandall Stewart int
339620abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate)
339720abea66SRandall Stewart {
339820abea66SRandall Stewart 	int error;
339920abea66SRandall Stewart 
340020abea66SRandall Stewart 	/*
340120abea66SRandall Stewart 	 * If the existing send tag is for the wrong interface due to
340220abea66SRandall Stewart 	 * a route change, first drop the existing tag.  Set the
340320abea66SRandall Stewart 	 * CHANGED flag so that we will keep trying to allocate a new
340420abea66SRandall Stewart 	 * tag if we fail to allocate one this time.
340520abea66SRandall Stewart 	 */
340620abea66SRandall Stewart 	if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) {
340720abea66SRandall Stewart 		in_pcbdetach_txrtlmt(inp);
340820abea66SRandall Stewart 		inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
340920abea66SRandall Stewart 	}
341020abea66SRandall Stewart 
341120abea66SRandall Stewart 	/*
341220abea66SRandall Stewart 	 * NOTE: When attaching to a network interface a reference is
341320abea66SRandall Stewart 	 * made to ensure the network interface doesn't go away until
341420abea66SRandall Stewart 	 * all ratelimit connections are gone. The network interface
341520abea66SRandall Stewart 	 * pointers compared below represent valid network interfaces,
341620abea66SRandall Stewart 	 * except when comparing towards NULL.
341720abea66SRandall Stewart 	 */
341820abea66SRandall Stewart 	if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) {
341920abea66SRandall Stewart 		error = 0;
342020abea66SRandall Stewart 	} else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) {
342120abea66SRandall Stewart 		if (inp->inp_snd_tag != NULL)
342220abea66SRandall Stewart 			in_pcbdetach_txrtlmt(inp);
342320abea66SRandall Stewart 		error = 0;
342420abea66SRandall Stewart 	} else if (inp->inp_snd_tag == NULL) {
342520abea66SRandall Stewart 		/*
342620abea66SRandall Stewart 		 * In order to utilize packet pacing with RSS, we need
342720abea66SRandall Stewart 		 * to wait until there is a valid RSS hash before we
342820abea66SRandall Stewart 		 * can proceed:
342920abea66SRandall Stewart 		 */
343020abea66SRandall Stewart 		if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) {
343120abea66SRandall Stewart 			error = EAGAIN;
343220abea66SRandall Stewart 		} else {
343320abea66SRandall Stewart 			error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb),
343420abea66SRandall Stewart 			    mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag);
343520abea66SRandall Stewart 		}
343620abea66SRandall Stewart 	} else {
343720abea66SRandall Stewart 		error = in_pcbmodify_txrtlmt(inp, max_pacing_rate);
343820abea66SRandall Stewart 	}
343920abea66SRandall Stewart 	if (error == 0 || error == EOPNOTSUPP)
344020abea66SRandall Stewart 		inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
344120abea66SRandall Stewart 
344220abea66SRandall Stewart 	return (error);
344320abea66SRandall Stewart }
344420abea66SRandall Stewart 
3445f3e7afe2SHans Petter Selasky /*
3446f3e7afe2SHans Petter Selasky  * This function should be called when the INP_RATE_LIMIT_CHANGED flag
3447f3e7afe2SHans Petter Selasky  * is set in the fast path and will attach/detach/modify the TX rate
3448f3e7afe2SHans Petter Selasky  * limit send tag based on the socket's so_max_pacing_rate value.
3449f3e7afe2SHans Petter Selasky  */
3450f3e7afe2SHans Petter Selasky void
3451f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb)
3452f3e7afe2SHans Petter Selasky {
3453f3e7afe2SHans Petter Selasky 	struct socket *socket;
3454f3e7afe2SHans Petter Selasky 	uint32_t max_pacing_rate;
3455f3e7afe2SHans Petter Selasky 	bool did_upgrade;
3456f3e7afe2SHans Petter Selasky 
3457f3e7afe2SHans Petter Selasky 	if (inp == NULL)
3458f3e7afe2SHans Petter Selasky 		return;
3459f3e7afe2SHans Petter Selasky 
3460f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
3461f3e7afe2SHans Petter Selasky 	if (socket == NULL)
3462f3e7afe2SHans Petter Selasky 		return;
3463f3e7afe2SHans Petter Selasky 
3464f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
3465f3e7afe2SHans Petter Selasky 		/*
3466f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
3467f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
3468f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3469f3e7afe2SHans Petter Selasky 		 * write lock.
3470f3e7afe2SHans Petter Selasky 		 */
3471f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3472f3e7afe2SHans Petter Selasky 			return;
3473f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3474f3e7afe2SHans Petter Selasky 	} else {
3475f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3476f3e7afe2SHans Petter Selasky 	}
3477f3e7afe2SHans Petter Selasky 
3478f3e7afe2SHans Petter Selasky 	/*
3479f3e7afe2SHans Petter Selasky 	 * NOTE: The so_max_pacing_rate value is read unlocked,
3480f3e7afe2SHans Petter Selasky 	 * because atomic updates are not required since the variable
3481f3e7afe2SHans Petter Selasky 	 * is checked at every mbuf we send. It is assumed that the
3482f3e7afe2SHans Petter Selasky 	 * variable read itself will be atomic.
3483f3e7afe2SHans Petter Selasky 	 */
3484f3e7afe2SHans Petter Selasky 	max_pacing_rate = socket->so_max_pacing_rate;
3485f3e7afe2SHans Petter Selasky 
3486bab34d63SJohn Baldwin 	in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate);
3487fb3bc596SJohn Baldwin 
3488f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3489f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3490f3e7afe2SHans Petter Selasky }
3491f3e7afe2SHans Petter Selasky 
3492f3e7afe2SHans Petter Selasky /*
3493f3e7afe2SHans Petter Selasky  * Track route changes for TX rate limiting.
3494f3e7afe2SHans Petter Selasky  */
3495f3e7afe2SHans Petter Selasky void
3496f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp)
3497f3e7afe2SHans Petter Selasky {
3498f3e7afe2SHans Petter Selasky 	bool did_upgrade;
3499f3e7afe2SHans Petter Selasky 
3500f3e7afe2SHans Petter Selasky 	if (inp == NULL)
3501f3e7afe2SHans Petter Selasky 		return;
3502f3e7afe2SHans Petter Selasky 
3503f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag == NULL)
3504f3e7afe2SHans Petter Selasky 		return;
3505f3e7afe2SHans Petter Selasky 
3506f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
3507f3e7afe2SHans Petter Selasky 		/*
3508f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
3509f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
3510f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3511f3e7afe2SHans Petter Selasky 		 * write lock.
3512f3e7afe2SHans Petter Selasky 		 */
3513f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3514f3e7afe2SHans Petter Selasky 			return;
3515f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3516f3e7afe2SHans Petter Selasky 	} else {
3517f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3518f3e7afe2SHans Petter Selasky 	}
3519f3e7afe2SHans Petter Selasky 
3520f3e7afe2SHans Petter Selasky 	/* detach rate limiting */
3521f3e7afe2SHans Petter Selasky 	in_pcbdetach_txrtlmt(inp);
3522f3e7afe2SHans Petter Selasky 
3523f3e7afe2SHans Petter Selasky 	/* make sure new mbuf send tag allocation is made */
3524f3e7afe2SHans Petter Selasky 	inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
3525f3e7afe2SHans Petter Selasky 
3526f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3527f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3528f3e7afe2SHans Petter Selasky }
352920abea66SRandall Stewart 
3530903c4ee6SXin LI #ifdef INET
353120abea66SRandall Stewart static void
353220abea66SRandall Stewart rl_init(void *st)
353320abea66SRandall Stewart {
35341a714ff2SRandall Stewart 	rate_limit_new = counter_u64_alloc(M_WAITOK);
35351a714ff2SRandall Stewart 	rate_limit_chg = counter_u64_alloc(M_WAITOK);
353620abea66SRandall Stewart 	rate_limit_active = counter_u64_alloc(M_WAITOK);
353720abea66SRandall Stewart 	rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK);
353820abea66SRandall Stewart 	rate_limit_set_ok = counter_u64_alloc(M_WAITOK);
353920abea66SRandall Stewart }
354020abea66SRandall Stewart 
354120abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL);
3542903c4ee6SXin LI #endif
3543f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */
3544