xref: /freebsd/sys/netinet/in_pcb.c (revision 6d7682268826ca15ed86237eb25286740b952f1c)
1c398230bSWarner Losh /*-
22469dd60SGarrett Wollman  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3497057eeSRobert Watson  *	The Regents of the University of California.
4111d57a6SRobert Watson  * Copyright (c) 2007-2009 Robert N. M. Watson
579bdc6e5SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6497057eeSRobert Watson  * All rights reserved.
7df8bae1dSRodney W. Grimes  *
879bdc6e5SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
979bdc6e5SRobert Watson  * contract to Juniper Networks, Inc.
1079bdc6e5SRobert Watson  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
20df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
21df8bae1dSRodney W. Grimes  *    without specific prior written permission.
22df8bae1dSRodney W. Grimes  *
23df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
34df8bae1dSRodney W. Grimes  *
352469dd60SGarrett Wollman  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
384b421e2dSMike Silbersack #include <sys/cdefs.h>
394b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
404b421e2dSMike Silbersack 
41497057eeSRobert Watson #include "opt_ddb.h"
426a800098SYoshinobu Inoue #include "opt_ipsec.h"
43efc76f72SBjoern A. Zeeb #include "opt_inet.h"
44cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
4552cd27cbSRobert Watson #include "opt_pcbgroup.h"
467527624eSRobert Watson #include "opt_rss.h"
47cfa1ca9dSYoshinobu Inoue 
48df8bae1dSRodney W. Grimes #include <sys/param.h>
49df8bae1dSRodney W. Grimes #include <sys/systm.h>
50cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
51df8bae1dSRodney W. Grimes #include <sys/malloc.h>
52df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
53aae49dd3SBjoern A. Zeeb #include <sys/callout.h>
54ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h>
55cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
56df8bae1dSRodney W. Grimes #include <sys/protosw.h>
57cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
58df8bae1dSRodney W. Grimes #include <sys/socket.h>
59df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
60acd3428bSRobert Watson #include <sys/priv.h>
61df8bae1dSRodney W. Grimes #include <sys/proc.h>
6279bdc6e5SRobert Watson #include <sys/refcount.h>
6375c13541SPoul-Henning Kamp #include <sys/jail.h>
64101f9fc8SPeter Wemm #include <sys/kernel.h>
65101f9fc8SPeter Wemm #include <sys/sysctl.h>
668781d8e9SBruce Evans 
67497057eeSRobert Watson #ifdef DDB
68497057eeSRobert Watson #include <ddb/ddb.h>
69497057eeSRobert Watson #endif
70497057eeSRobert Watson 
7169c2d429SJeff Roberson #include <vm/uma.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <net/if.h>
7476039bc8SGleb Smirnoff #include <net/if_var.h>
75cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
76*6d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h>
77df8bae1dSRodney W. Grimes #include <net/route.h>
78b2bdc62aSAdrian Chadd #include <net/rss_config.h>
79530c0060SRobert Watson #include <net/vnet.h>
80df8bae1dSRodney W. Grimes 
8167107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
82df8bae1dSRodney W. Grimes #include <netinet/in.h>
83df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
84df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
85340c35deSJonathan Lemon #include <netinet/tcp_var.h>
865f311da2SMike Silbersack #include <netinet/udp.h>
875f311da2SMike Silbersack #include <netinet/udp_var.h>
8867107f45SBjoern A. Zeeb #endif
8967107f45SBjoern A. Zeeb #ifdef INET
9067107f45SBjoern A. Zeeb #include <netinet/in_var.h>
9167107f45SBjoern A. Zeeb #endif
92cfa1ca9dSYoshinobu Inoue #ifdef INET6
93cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
94efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
9567107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
9667107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
97cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
98cfa1ca9dSYoshinobu Inoue 
99df8bae1dSRodney W. Grimes 
100b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
101b9234fafSSam Leffler #include <netipsec/ipsec.h>
102b9234fafSSam Leffler #include <netipsec/key.h>
103b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
104b9234fafSSam Leffler 
105aed55708SRobert Watson #include <security/mac/mac_framework.h>
106aed55708SRobert Watson 
107aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
108aae49dd3SBjoern A. Zeeb 
109101f9fc8SPeter Wemm /*
110101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
111101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
112101f9fc8SPeter Wemm  */
113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
115eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
116eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
117eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
118eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
119101f9fc8SPeter Wemm 
120b0d22693SCrist J. Clark /*
121b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
122b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
123b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
124b0d22693SCrist J. Clark  */
125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
127b0d22693SCrist J. Clark 
1285f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
130eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
131eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
132eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
133eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1343e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount);
135eddfbb76SRobert Watson 
1361e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1376ac48b74SMike Silbersack 
138d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
139d2025bd0SBjoern A. Zeeb #ifdef INET
140fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
141fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
142fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
143fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
14467107f45SBjoern A. Zeeb 
145bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
146bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
147bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
148bbd42ad0SPeter Wemm 
149bbd42ad0SPeter Wemm static int
15082d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
151bbd42ad0SPeter Wemm {
15230a4ab08SBruce Evans 	int error;
15330a4ab08SBruce Evans 
154f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
15530a4ab08SBruce Evans 	if (error == 0) {
156603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
157603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
158603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
159603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
160603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
161603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
162bbd42ad0SPeter Wemm 	}
16330a4ab08SBruce Evans 	return (error);
164bbd42ad0SPeter Wemm }
165bbd42ad0SPeter Wemm 
166bbd42ad0SPeter Wemm #undef RANGECHK
167bbd42ad0SPeter Wemm 
1686472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
1696472ac3dSEd Schouten     "IP Ports");
17033b3ac06SPeter Wemm 
1716df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1726df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1736df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
1746df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1756df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1766df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
1776df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1786df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1796df8a710SGleb Smirnoff 	&VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
1806df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1816df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1826df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
1836df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
1846df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1856df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
1866df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
1876df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1886df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
1896df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
1906df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
1916df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
193eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
1946df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
1956df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
196eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
1986df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
199eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
2006ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
2026df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
203eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
2048b615593SMarko Zec 	"Minimum time to keep sequental port "
2056ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
20683e521ecSBjoern A. Zeeb #endif /* INET */
2070312fbe9SPoul-Henning Kamp 
208c3229e05SDavid Greenman /*
209c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
210c3229e05SDavid Greenman  *
211de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
212de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
213de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
214c3229e05SDavid Greenman  */
215c3229e05SDavid Greenman 
216c3229e05SDavid Greenman /*
2179bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
2189bcd427bSRobert Watson  * arguments in time.
2199bcd427bSRobert Watson  */
2209bcd427bSRobert Watson void
2219bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
2229bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
2239bcd427bSRobert Watson     char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
22452cd27cbSRobert Watson     uint32_t inpcbzone_flags, u_int hashfields)
2259bcd427bSRobert Watson {
2269bcd427bSRobert Watson 
2279bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
228fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
229ff9b006dSJulien Charbon 	INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
2309bcd427bSRobert Watson #ifdef VIMAGE
2319bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
2329bcd427bSRobert Watson #endif
2339bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
2349bcd427bSRobert Watson 	LIST_INIT(pcbinfo->ipi_listhead);
235fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
2369bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
2379bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
2389bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
2399bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
24052cd27cbSRobert Watson #ifdef PCBGROUP
24152cd27cbSRobert Watson 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
24252cd27cbSRobert Watson #endif
2439bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
2449bcd427bSRobert Watson 	    NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
2459bcd427bSRobert Watson 	    inpcbzone_flags);
2469bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
2476acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
2486acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
2499bcd427bSRobert Watson }
2509bcd427bSRobert Watson 
2519bcd427bSRobert Watson /*
2529bcd427bSRobert Watson  * Destroy an inpcbinfo.
2539bcd427bSRobert Watson  */
2549bcd427bSRobert Watson void
2559bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
2569bcd427bSRobert Watson {
2579bcd427bSRobert Watson 
258fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
259fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
260fa046d87SRobert Watson 
2619bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
2629bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
2639bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
26452cd27cbSRobert Watson #ifdef PCBGROUP
26552cd27cbSRobert Watson 	in_pcbgroup_destroy(pcbinfo);
26652cd27cbSRobert Watson #endif
2679bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
268ff9b006dSJulien Charbon 	INP_LIST_LOCK_DESTROY(pcbinfo);
269fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
2709bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
2719bcd427bSRobert Watson }
2729bcd427bSRobert Watson 
2739bcd427bSRobert Watson /*
274c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
275d915b280SStephan Uphoff  * On success return with the PCB locked.
276c3229e05SDavid Greenman  */
277df8bae1dSRodney W. Grimes int
278d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
279df8bae1dSRodney W. Grimes {
280136d4f1cSRobert Watson 	struct inpcb *inp;
28113cf67f3SHajimu UMEMOTO 	int error;
282a557af22SRobert Watson 
283ff9b006dSJulien Charbon #ifdef INVARIANTS
284ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
285ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
286ff9b006dSJulien Charbon 	} else {
28759daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
288ff9b006dSJulien Charbon 	}
289ff9b006dSJulien Charbon #endif
290ff9b006dSJulien Charbon 
291a557af22SRobert Watson 	error = 0;
292d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
293df8bae1dSRodney W. Grimes 	if (inp == NULL)
294df8bae1dSRodney W. Grimes 		return (ENOBUFS);
295d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
29615bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
297df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
29886d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
2998b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
300a557af22SRobert Watson #ifdef MAC
30130d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
302a557af22SRobert Watson 	if (error != 0)
303a557af22SRobert Watson 		goto out;
30430d239bcSRobert Watson 	mac_inpcb_create(so, inp);
305a557af22SRobert Watson #endif
306b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
30713cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
3080bffde27SRobert Watson 	if (error != 0) {
3090bffde27SRobert Watson #ifdef MAC
3100bffde27SRobert Watson 		mac_inpcb_destroy(inp);
3110bffde27SRobert Watson #endif
312a557af22SRobert Watson 		goto out;
3130bffde27SRobert Watson 	}
314b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
315e3fd5ffdSRobert Watson #ifdef INET6
316340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
317340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
318603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
31933841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
320340c35deSJonathan Lemon 	}
32175daea93SPaul Saab #endif
322ff9b006dSJulien Charbon 	INP_WLOCK(inp);
323ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
324712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
3253d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
326df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
32733841545SHajimu UMEMOTO #ifdef INET6
328603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
32933841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
33033841545SHajimu UMEMOTO #endif
331d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
33279bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
333ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
334b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
335a557af22SRobert Watson out:
33686d02c5cSBjoern A. Zeeb 	if (error != 0) {
33786d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
338a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
33986d02c5cSBjoern A. Zeeb 	}
340a557af22SRobert Watson #endif
341a557af22SRobert Watson 	return (error);
342df8bae1dSRodney W. Grimes }
343df8bae1dSRodney W. Grimes 
34467107f45SBjoern A. Zeeb #ifdef INET
345df8bae1dSRodney W. Grimes int
346136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
347df8bae1dSRodney W. Grimes {
3484b932371SIan Dowse 	int anonport, error;
3494b932371SIan Dowse 
3508501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
351fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
35259daba27SSam Leffler 
3534b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
3544b932371SIan Dowse 		return (EINVAL);
3555cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
3564b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
357b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
3584b932371SIan Dowse 	if (error)
3594b932371SIan Dowse 		return (error);
3604b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
3614b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
3624b932371SIan Dowse 		inp->inp_lport = 0;
3634b932371SIan Dowse 		return (EAGAIN);
3644b932371SIan Dowse 	}
3654b932371SIan Dowse 	if (anonport)
3664b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
3674b932371SIan Dowse 	return (0);
3684b932371SIan Dowse }
36967107f45SBjoern A. Zeeb #endif
3704b932371SIan Dowse 
37139c8c62eSHiren Panchasara /*
37239c8c62eSHiren Panchasara  * Select a local port (number) to use.
37339c8c62eSHiren Panchasara  */
374efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
375efc76f72SBjoern A. Zeeb int
376efc76f72SBjoern A. Zeeb in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
37768e0d7e0SRobert Watson     struct ucred *cred, int lookupflags)
378efc76f72SBjoern A. Zeeb {
379efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
380efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
381efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
382efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
383efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
384efc76f72SBjoern A. Zeeb #ifdef INET
385efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
386efc76f72SBjoern A. Zeeb #endif
387efc76f72SBjoern A. Zeeb 
388efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
389efc76f72SBjoern A. Zeeb 
390efc76f72SBjoern A. Zeeb 	/*
391efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
392efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
393efc76f72SBjoern A. Zeeb 	 */
394efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
395fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
396efc76f72SBjoern A. Zeeb 
397efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
398efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
399efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
400efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
401efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
402efc76f72SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
403efc76f72SBjoern A. Zeeb 		if (error)
404efc76f72SBjoern A. Zeeb 			return (error);
405efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
406efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
407efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
408efc76f72SBjoern A. Zeeb 	} else {
409efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
410efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
411efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
412efc76f72SBjoern A. Zeeb 	}
413efc76f72SBjoern A. Zeeb 	/*
414e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
415efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
416efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
417efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
418efc76f72SBjoern A. Zeeb 	 */
419efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
420e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
421e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
422efc76f72SBjoern A. Zeeb 		dorandom = 1;
423efc76f72SBjoern A. Zeeb 	else
424efc76f72SBjoern A. Zeeb 		dorandom = 0;
425efc76f72SBjoern A. Zeeb 	/*
426efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
427efc76f72SBjoern A. Zeeb 	 * we have the only port available.
428efc76f72SBjoern A. Zeeb 	 */
429efc76f72SBjoern A. Zeeb 	if (first == last)
430efc76f72SBjoern A. Zeeb 		dorandom = 0;
431e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
432e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
433efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
434efc76f72SBjoern A. Zeeb 	/*
435efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
436efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
437efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
438efc76f72SBjoern A. Zeeb 	 */
439efc76f72SBjoern A. Zeeb 	if (first > last) {
440efc76f72SBjoern A. Zeeb 		aux = first;
441efc76f72SBjoern A. Zeeb 		first = last;
442efc76f72SBjoern A. Zeeb 		last = aux;
443efc76f72SBjoern A. Zeeb 	}
444efc76f72SBjoern A. Zeeb 
445efc76f72SBjoern A. Zeeb #ifdef INET
446efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
447efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
4484d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
449efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
450efc76f72SBjoern A. Zeeb 		    __func__, inp));
451efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
452efc76f72SBjoern A. Zeeb 	}
453efc76f72SBjoern A. Zeeb #endif
45467107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
455efc76f72SBjoern A. Zeeb 	lport = *lportp;
456efc76f72SBjoern A. Zeeb 
457efc76f72SBjoern A. Zeeb 	if (dorandom)
458efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
459efc76f72SBjoern A. Zeeb 
460efc76f72SBjoern A. Zeeb 	count = last - first;
461efc76f72SBjoern A. Zeeb 
462efc76f72SBjoern A. Zeeb 	do {
463efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
464efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
465efc76f72SBjoern A. Zeeb 		++*lastport;
466efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
467efc76f72SBjoern A. Zeeb 			*lastport = first;
468efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
469efc76f72SBjoern A. Zeeb 
470efc76f72SBjoern A. Zeeb #ifdef INET6
471efc76f72SBjoern A. Zeeb 		if ((inp->inp_vflag & INP_IPV6) != 0)
472efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
47368e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
474efc76f72SBjoern A. Zeeb #endif
475efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
476efc76f72SBjoern A. Zeeb 		else
477efc76f72SBjoern A. Zeeb #endif
478efc76f72SBjoern A. Zeeb #ifdef INET
479efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
48068e0d7e0SRobert Watson 			    lport, lookupflags, cred);
481efc76f72SBjoern A. Zeeb #endif
482efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
483efc76f72SBjoern A. Zeeb 
484efc76f72SBjoern A. Zeeb #ifdef INET
4854d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
486efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
487efc76f72SBjoern A. Zeeb #endif
488efc76f72SBjoern A. Zeeb 	*lportp = lport;
489efc76f72SBjoern A. Zeeb 
490efc76f72SBjoern A. Zeeb 	return (0);
491efc76f72SBjoern A. Zeeb }
492efdf104bSMikolaj Golub 
493efdf104bSMikolaj Golub /*
494efdf104bSMikolaj Golub  * Return cached socket options.
495efdf104bSMikolaj Golub  */
496efdf104bSMikolaj Golub short
497efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
498efdf104bSMikolaj Golub {
499efdf104bSMikolaj Golub    short so_options;
500efdf104bSMikolaj Golub 
501efdf104bSMikolaj Golub    so_options = 0;
502efdf104bSMikolaj Golub 
503efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
504efdf104bSMikolaj Golub 	   so_options |= SO_REUSEPORT;
505efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
506efdf104bSMikolaj Golub 	   so_options |= SO_REUSEADDR;
507efdf104bSMikolaj Golub    return (so_options);
508efdf104bSMikolaj Golub }
509efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
510efc76f72SBjoern A. Zeeb 
5114b932371SIan Dowse /*
5120a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
5130a100a6fSAdrian Chadd  *
5140a100a6fSAdrian Chadd  * ni points to the new inp.
5150a100a6fSAdrian Chadd  * oi points to the exisitng inp.
5160a100a6fSAdrian Chadd  *
5170a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
5180a100a6fSAdrian Chadd  * whether the credentials match.
5190a100a6fSAdrian Chadd  */
520d5bb8bd3SAdrian Chadd int
5210a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
5220a100a6fSAdrian Chadd {
5230a100a6fSAdrian Chadd 	/* Check permissions match */
5240a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5250a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
5260a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
5270a100a6fSAdrian Chadd 		return (0);
5280a100a6fSAdrian Chadd 
5290a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
5300a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5310a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
5320a100a6fSAdrian Chadd 		return (0);
5330a100a6fSAdrian Chadd 
5340a100a6fSAdrian Chadd 	/*
5350a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
5360a100a6fSAdrian Chadd 	 * it is and it matches the checks.
5370a100a6fSAdrian Chadd 	 */
5380a100a6fSAdrian Chadd 	return (1);
5390a100a6fSAdrian Chadd }
5400a100a6fSAdrian Chadd 
541d5bb8bd3SAdrian Chadd #ifdef INET
5420a100a6fSAdrian Chadd /*
5434b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
5444b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
5454b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
5464b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
5474b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
5484b932371SIan Dowse  *
5494b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
5504b932371SIan Dowse  */
5514b932371SIan Dowse int
552136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
553136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
5544b932371SIan Dowse {
5554b932371SIan Dowse 	struct socket *so = inp->inp_socket;
55615bd2b43SDavid Greenman 	struct sockaddr_in *sin;
557c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
5584b932371SIan Dowse 	struct in_addr laddr;
559df8bae1dSRodney W. Grimes 	u_short lport = 0;
56068e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
561413628a7SBjoern A. Zeeb 	int error;
562df8bae1dSRodney W. Grimes 
5638501a69cSRobert Watson 	/*
564fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
5658501a69cSRobert Watson 	 */
56659daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
567fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
56859daba27SSam Leffler 
569603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
570df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5714b932371SIan Dowse 	laddr.s_addr = *laddrp;
5724b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
573df8bae1dSRodney W. Grimes 		return (EINVAL);
574c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
57568e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
5767c2f3cb9SJamie Gritton 	if (nam == NULL) {
5777c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
5787c2f3cb9SJamie Gritton 			return (error);
5797c2f3cb9SJamie Gritton 	} else {
58057bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
58157bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
582df8bae1dSRodney W. Grimes 			return (EINVAL);
583df8bae1dSRodney W. Grimes #ifdef notdef
584df8bae1dSRodney W. Grimes 		/*
585df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
586df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
587df8bae1dSRodney W. Grimes 		 */
588df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
589df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
590df8bae1dSRodney W. Grimes #endif
591b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
592b89e82ddSJamie Gritton 		if (error)
593b89e82ddSJamie Gritton 			return (error);
5944b932371SIan Dowse 		if (sin->sin_port != *lportp) {
5954b932371SIan Dowse 			/* Don't allow the port to change. */
5964b932371SIan Dowse 			if (*lportp != 0)
5974b932371SIan Dowse 				return (EINVAL);
598df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
5994b932371SIan Dowse 		}
6004b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
601df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
602df8bae1dSRodney W. Grimes 			/*
603df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
604df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
605df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
606df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
607df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
608df8bae1dSRodney W. Grimes 			 */
609f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
610df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
611df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
612df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
61383103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
6144209e01aSAdrian Chadd 			/*
6154209e01aSAdrian Chadd 			 * Is the address a local IP address?
616f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
6178696873dSAdrian Chadd 			 * to any endpoint address, local or not.
6184209e01aSAdrian Chadd 			 */
619f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
6208896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
621df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
622df8bae1dSRodney W. Grimes 		}
6234b932371SIan Dowse 		laddr = sin->sin_addr;
624df8bae1dSRodney W. Grimes 		if (lport) {
625df8bae1dSRodney W. Grimes 			struct inpcb *t;
626ae0e7143SRobert Watson 			struct tcptw *tw;
627ae0e7143SRobert Watson 
628df8bae1dSRodney W. Grimes 			/* GROSS */
629603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
630603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
631acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
63232f9753cSRobert Watson 			    0))
6332469dd60SGarrett Wollman 				return (EACCES);
634835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
63586d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
63632f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
637078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
638413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
639340c35deSJonathan Lemon 	/*
640340c35deSJonathan Lemon 	 * XXX
641340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
642340c35deSJonathan Lemon 	 */
6434cc20ab1SSeigo Tanimura 				if (t &&
6440a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
645ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
6464658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
6474658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
6484cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
64952b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
650fc06cd42SMikolaj Golub 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
65186d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
65286d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
6534049a042SGuido van Rooij 					return (EADDRINUSE);
6540a100a6fSAdrian Chadd 
6550a100a6fSAdrian Chadd 				/*
6560a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
6570a100a6fSAdrian Chadd 				 * the credentials need to match and the
6580a100a6fSAdrian Chadd 				 * original socket also has to have been bound
6590a100a6fSAdrian Chadd 				 * with BINDMULTI.
6600a100a6fSAdrian Chadd 				 */
6610a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6620a100a6fSAdrian Chadd 					return (EADDRINUSE);
6634049a042SGuido van Rooij 			}
664c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
66568e0d7e0SRobert Watson 			    lport, lookupflags, cred);
666ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
667ae0e7143SRobert Watson 				/*
668ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
669ae0e7143SRobert Watson 				 * state recycled, we treat the address as
670ae0e7143SRobert Watson 				 * being in use (for now).  This is better
671ae0e7143SRobert Watson 				 * than a panic, but not desirable.
672ae0e7143SRobert Watson 				 */
673ec95b709SMikolaj Golub 				tw = intotw(t);
674ae0e7143SRobert Watson 				if (tw == NULL ||
675ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
676340c35deSJonathan Lemon 					return (EADDRINUSE);
6770a100a6fSAdrian Chadd 			} else if (t &&
6780a100a6fSAdrian Chadd 			    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
6790a100a6fSAdrian Chadd 			    (reuseport & inp_so_options(t)) == 0) {
680e3fd5ffdSRobert Watson #ifdef INET6
68133841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
682cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
683cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
684cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
685fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
686fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
687e3fd5ffdSRobert Watson #endif
688df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
6890a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6900a100a6fSAdrian Chadd 					return (EADDRINUSE);
691df8bae1dSRodney W. Grimes 			}
692cfa1ca9dSYoshinobu Inoue 		}
693df8bae1dSRodney W. Grimes 	}
6944b932371SIan Dowse 	if (*lportp != 0)
6954b932371SIan Dowse 		lport = *lportp;
69633b3ac06SPeter Wemm 	if (lport == 0) {
69768e0d7e0SRobert Watson 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
698efc76f72SBjoern A. Zeeb 		if (error != 0)
699efc76f72SBjoern A. Zeeb 			return (error);
70033b3ac06SPeter Wemm 
70133b3ac06SPeter Wemm 	}
7024b932371SIan Dowse 	*laddrp = laddr.s_addr;
7034b932371SIan Dowse 	*lportp = lport;
704df8bae1dSRodney W. Grimes 	return (0);
705df8bae1dSRodney W. Grimes }
706df8bae1dSRodney W. Grimes 
707999f1343SGarrett Wollman /*
7085200e00eSIan Dowse  * Connect from a socket to a specified address.
7095200e00eSIan Dowse  * Both address and port must be specified in argument sin.
7105200e00eSIan Dowse  * If don't have a local address for this socket yet,
7115200e00eSIan Dowse  * then pick one.
712999f1343SGarrett Wollman  */
713999f1343SGarrett Wollman int
714d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
715d3c1f003SRobert Watson     struct ucred *cred, struct mbuf *m)
716999f1343SGarrett Wollman {
7175200e00eSIan Dowse 	u_short lport, fport;
7185200e00eSIan Dowse 	in_addr_t laddr, faddr;
7195200e00eSIan Dowse 	int anonport, error;
720df8bae1dSRodney W. Grimes 
7218501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
722fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
72327f74fd0SRobert Watson 
7245200e00eSIan Dowse 	lport = inp->inp_lport;
7255200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
7265200e00eSIan Dowse 	anonport = (lport == 0);
7275200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
728b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
7295200e00eSIan Dowse 	if (error)
7305200e00eSIan Dowse 		return (error);
7315200e00eSIan Dowse 
7325200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
7335200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
7345200e00eSIan Dowse 		inp->inp_lport = lport;
7355200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
7365200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
7375200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
7385200e00eSIan Dowse 			inp->inp_lport = 0;
7395200e00eSIan Dowse 			return (EAGAIN);
7405200e00eSIan Dowse 		}
7415200e00eSIan Dowse 	}
7425200e00eSIan Dowse 
7435200e00eSIan Dowse 	/* Commit the remaining changes. */
7445200e00eSIan Dowse 	inp->inp_lport = lport;
7455200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
7465200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
7475200e00eSIan Dowse 	inp->inp_fport = fport;
748d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, m);
7492cb64cb2SGeorge V. Neville-Neil 
7505200e00eSIan Dowse 	if (anonport)
7515200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
7525200e00eSIan Dowse 	return (0);
7535200e00eSIan Dowse }
7545200e00eSIan Dowse 
755d3c1f003SRobert Watson int
756d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
757d3c1f003SRobert Watson {
758d3c1f003SRobert Watson 
759d3c1f003SRobert Watson 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
760d3c1f003SRobert Watson }
761d3c1f003SRobert Watson 
7625200e00eSIan Dowse /*
7630895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
7640895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
7650895aec3SBjoern A. Zeeb  */
766ae190832SSteven Hartland int
7670895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
7680895aec3SBjoern A. Zeeb     struct ucred *cred)
7690895aec3SBjoern A. Zeeb {
7700895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
7710895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
7720895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
7730895aec3SBjoern A. Zeeb 	struct route sro;
7740895aec3SBjoern A. Zeeb 	int error;
7750895aec3SBjoern A. Zeeb 
776413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
7770895aec3SBjoern A. Zeeb 
778592bcae8SBjoern A. Zeeb 	/*
779592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
780592bcae8SBjoern A. Zeeb 	 * if requested.
781592bcae8SBjoern A. Zeeb 	 */
782592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
783592bcae8SBjoern A. Zeeb 		return (0);
784592bcae8SBjoern A. Zeeb 
7850895aec3SBjoern A. Zeeb 	error = 0;
7860895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
7870895aec3SBjoern A. Zeeb 
7880895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
7890895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
7900895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
7910895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
7920895aec3SBjoern A. Zeeb 
7930895aec3SBjoern A. Zeeb 	/*
7940895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
7950895aec3SBjoern A. Zeeb 	 * else punt.
7960895aec3SBjoern A. Zeeb 	 *
7970895aec3SBjoern A. Zeeb 	 * Find out route to destination.
7980895aec3SBjoern A. Zeeb 	 */
7990895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
8006e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
8010895aec3SBjoern A. Zeeb 
8020895aec3SBjoern A. Zeeb 	/*
8030895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
8040895aec3SBjoern A. Zeeb 	 * the outgoing interface.
8050895aec3SBjoern A. Zeeb 	 *
8060895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
8070895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
8080895aec3SBjoern A. Zeeb 	 * the source address from.
8090895aec3SBjoern A. Zeeb 	 */
8100895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
8118c0fec80SRobert Watson 		struct in_ifaddr *ia;
8120895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
8130895aec3SBjoern A. Zeeb 
8144f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
81558a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
8160895aec3SBjoern A. Zeeb 		if (ia == NULL)
8174f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
81858a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
8190895aec3SBjoern A. Zeeb 		if (ia == NULL) {
8200895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
8210895aec3SBjoern A. Zeeb 			goto done;
8220895aec3SBjoern A. Zeeb 		}
8230895aec3SBjoern A. Zeeb 
8240304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8250895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8268c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8270895aec3SBjoern A. Zeeb 			goto done;
8280895aec3SBjoern A. Zeeb 		}
8290895aec3SBjoern A. Zeeb 
8300895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
8318c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
8320895aec3SBjoern A. Zeeb 		ia = NULL;
833137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8340895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8350895aec3SBjoern A. Zeeb 
8360895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8370895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8380895aec3SBjoern A. Zeeb 				continue;
8390895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
840b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8410895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8420895aec3SBjoern A. Zeeb 				break;
8430895aec3SBjoern A. Zeeb 			}
8440895aec3SBjoern A. Zeeb 		}
8450895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8460895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
847137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8480895aec3SBjoern A. Zeeb 			goto done;
8490895aec3SBjoern A. Zeeb 		}
850137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8510895aec3SBjoern A. Zeeb 
8520895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
853b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8540895aec3SBjoern A. Zeeb 		goto done;
8550895aec3SBjoern A. Zeeb 	}
8560895aec3SBjoern A. Zeeb 
8570895aec3SBjoern A. Zeeb 	/*
8580895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
8590895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
8600895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
8610895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
8620895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
8630895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
8640895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
8650895aec3SBjoern A. Zeeb 	 */
8660895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
8678c0fec80SRobert Watson 		struct in_ifaddr *ia;
8689317b04eSRobert Watson 		struct ifnet *ifp;
8690895aec3SBjoern A. Zeeb 
8700895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
8710304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8720895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8730895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8740895aec3SBjoern A. Zeeb 			goto done;
8750895aec3SBjoern A. Zeeb 		}
8760895aec3SBjoern A. Zeeb 
8770895aec3SBjoern A. Zeeb 		/* Jailed. */
8780895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
8790895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
880b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8810895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8820895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8830895aec3SBjoern A. Zeeb 			goto done;
8840895aec3SBjoern A. Zeeb 		}
8850895aec3SBjoern A. Zeeb 
8860895aec3SBjoern A. Zeeb 		/*
8870895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
8880895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
8890895aec3SBjoern A. Zeeb 		 */
8908c0fec80SRobert Watson 		ia = NULL;
8919317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
892137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8939317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8940895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8950895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8960895aec3SBjoern A. Zeeb 				continue;
8970895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
898b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8990895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
9000895aec3SBjoern A. Zeeb 				break;
9010895aec3SBjoern A. Zeeb 			}
9020895aec3SBjoern A. Zeeb 		}
9030895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9040895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
905137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9060895aec3SBjoern A. Zeeb 			goto done;
9070895aec3SBjoern A. Zeeb 		}
908137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
9090895aec3SBjoern A. Zeeb 
9100895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
911b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9120895aec3SBjoern A. Zeeb 		goto done;
9130895aec3SBjoern A. Zeeb 	}
9140895aec3SBjoern A. Zeeb 
9150895aec3SBjoern A. Zeeb 	/*
9160895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
9170895aec3SBjoern A. Zeeb 	 * to ourselves is here.
9180895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
9190895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
9200895aec3SBjoern A. Zeeb 	 * a loopback interface.
9210895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
9220895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
9230895aec3SBjoern A. Zeeb 	 */
9240895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
9250895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
9268c0fec80SRobert Watson 		struct in_ifaddr *ia;
9270895aec3SBjoern A. Zeeb 
9280895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
9290895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
9300895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
9310895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
9320895aec3SBjoern A. Zeeb 
93358a39d8cSAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
93458a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
9350895aec3SBjoern A. Zeeb 		if (ia == NULL)
9364f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
93758a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
938f0bb05fcSQing Li 		if (ia == NULL)
939f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
9400895aec3SBjoern A. Zeeb 
9410304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
9420895aec3SBjoern A. Zeeb 			if (ia == NULL) {
9430895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
9440895aec3SBjoern A. Zeeb 				goto done;
9450895aec3SBjoern A. Zeeb 			}
9460895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
9478c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9480895aec3SBjoern A. Zeeb 			goto done;
9490895aec3SBjoern A. Zeeb 		}
9500895aec3SBjoern A. Zeeb 
9510895aec3SBjoern A. Zeeb 		/* Jailed. */
9520895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9530895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
9540895aec3SBjoern A. Zeeb 
9550895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
9568c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9570895aec3SBjoern A. Zeeb 			ia = NULL;
958137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
9590895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9600895aec3SBjoern A. Zeeb 
9610895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
9620895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
9630895aec3SBjoern A. Zeeb 					continue;
9640895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
965b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
966b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
9670895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
9680895aec3SBjoern A. Zeeb 					break;
9690895aec3SBjoern A. Zeeb 				}
9700895aec3SBjoern A. Zeeb 			}
9710895aec3SBjoern A. Zeeb 			if (ia != NULL) {
9720895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
973137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
9740895aec3SBjoern A. Zeeb 				goto done;
9750895aec3SBjoern A. Zeeb 			}
976137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9770895aec3SBjoern A. Zeeb 		}
9780895aec3SBjoern A. Zeeb 
9790895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
980b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9810895aec3SBjoern A. Zeeb 		goto done;
9820895aec3SBjoern A. Zeeb 	}
9830895aec3SBjoern A. Zeeb 
9840895aec3SBjoern A. Zeeb done:
9850895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
9860895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
9870895aec3SBjoern A. Zeeb 	return (error);
9880895aec3SBjoern A. Zeeb }
9890895aec3SBjoern A. Zeeb 
9900895aec3SBjoern A. Zeeb /*
9915200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
9925200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
9935200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
9945200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
9955200e00eSIan Dowse  * the connect.
9965200e00eSIan Dowse  *
9975200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
9985200e00eSIan Dowse  * and port. These are not updated in the error case.
9995200e00eSIan Dowse  *
10005200e00eSIan Dowse  * If the operation fails because the connection already exists,
10015200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
10025200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
10035200e00eSIan Dowse  * is set to NULL.
10045200e00eSIan Dowse  */
10055200e00eSIan Dowse int
1006136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1007136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1008136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
10095200e00eSIan Dowse {
1010cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
10115200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
10125200e00eSIan Dowse 	struct in_ifaddr *ia;
10135200e00eSIan Dowse 	struct inpcb *oinp;
1014b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
10155200e00eSIan Dowse 	u_short lport, fport;
10165200e00eSIan Dowse 	int error;
10175200e00eSIan Dowse 
10188501a69cSRobert Watson 	/*
10198501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
10208501a69cSRobert Watson 	 * lock is sufficient.
10218501a69cSRobert Watson 	 */
102227f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
1023fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
102427f74fd0SRobert Watson 
10255200e00eSIan Dowse 	if (oinpp != NULL)
10265200e00eSIan Dowse 		*oinpp = NULL;
102757bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
1028df8bae1dSRodney W. Grimes 		return (EINVAL);
1029df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
1030df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
1031df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1032df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
10335200e00eSIan Dowse 	laddr.s_addr = *laddrp;
10345200e00eSIan Dowse 	lport = *lportp;
10355200e00eSIan Dowse 	faddr = sin->sin_addr;
10365200e00eSIan Dowse 	fport = sin->sin_port;
10370895aec3SBjoern A. Zeeb 
1038603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1039df8bae1dSRodney W. Grimes 		/*
1040df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1041df8bae1dSRodney W. Grimes 		 * use the primary local address.
1042df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1043df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1044df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1045df8bae1dSRodney W. Grimes 		 */
1046413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
1047cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
1048413628a7SBjoern A. Zeeb 			faddr =
1049b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1050cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1051b89e82ddSJamie Gritton 			if (cred != NULL &&
1052b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1053b89e82ddSJamie Gritton 				return (error);
10542d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1055cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
10562d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
10572d9cfabaSRobert Watson 			    IFF_BROADCAST)
10585200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
1059603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1060cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
10612d9cfabaSRobert Watson 		}
1062df8bae1dSRodney W. Grimes 	}
10635200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1064d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1065df8bae1dSRodney W. Grimes 		/*
1066df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1067d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1068df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1069df8bae1dSRodney W. Grimes 		 */
10705200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1071df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1072df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1073df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1074df8bae1dSRodney W. Grimes 
1075df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1076df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1077df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
1078cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RLOCK(&in_ifa_tracker);
1079e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1080e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1081e691be70SDaniel Eischen 					    (cred == NULL ||
1082e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1083e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1084df8bae1dSRodney W. Grimes 						break;
1085e691be70SDaniel Eischen 				}
1086e691be70SDaniel Eischen 				if (ia == NULL)
1087d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1088e691be70SDaniel Eischen 				else {
10895200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1090d79fdd98SDaniel Eischen 					error = 0;
1091999f1343SGarrett Wollman 				}
1092cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1093d79fdd98SDaniel Eischen 			}
1094d79fdd98SDaniel Eischen 		}
109504215ed2SRandall Stewart 		if (error)
109604215ed2SRandall Stewart 			return (error);
10970895aec3SBjoern A. Zeeb 	}
1098fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1099fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
11005200e00eSIan Dowse 	if (oinp != NULL) {
11015200e00eSIan Dowse 		if (oinpp != NULL)
11025200e00eSIan Dowse 			*oinpp = oinp;
1103df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1104c3229e05SDavid Greenman 	}
11055200e00eSIan Dowse 	if (lport == 0) {
1106b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1107b0330ed9SPawel Jakub Dawidek 		    cred);
11085a903f8dSPierre Beyssac 		if (error)
11095a903f8dSPierre Beyssac 			return (error);
11105a903f8dSPierre Beyssac 	}
11115200e00eSIan Dowse 	*laddrp = laddr.s_addr;
11125200e00eSIan Dowse 	*lportp = lport;
11135200e00eSIan Dowse 	*faddrp = faddr.s_addr;
11145200e00eSIan Dowse 	*fportp = fport;
1115df8bae1dSRodney W. Grimes 	return (0);
1116df8bae1dSRodney W. Grimes }
1117df8bae1dSRodney W. Grimes 
111826f9a767SRodney W. Grimes void
1119136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1120df8bae1dSRodney W. Grimes {
11216b348152SRobert Watson 
11228501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1123fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1124df8bae1dSRodney W. Grimes 
1125df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1126df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
112715bd2b43SDavid Greenman 	in_pcbrehash(inp);
1128df8bae1dSRodney W. Grimes }
112983e521ecSBjoern A. Zeeb #endif /* INET */
1130df8bae1dSRodney W. Grimes 
11314c7c478dSRobert Watson /*
113228696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1133c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
113428696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
113528696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
11364c7c478dSRobert Watson  */
113726f9a767SRodney W. Grimes void
1138136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1139df8bae1dSRodney W. Grimes {
11404c7c478dSRobert Watson 
1141a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1142c0a211c5SRobert Watson 
11434c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
11444c7c478dSRobert Watson 	inp->inp_socket = NULL;
11454c7c478dSRobert Watson }
11464c7c478dSRobert Watson 
1147c0a211c5SRobert Watson /*
114879bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
114979bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
115079bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
115152cd27cbSRobert Watson  * but where the inpcb lock may already held, or when acquiring a reference
115252cd27cbSRobert Watson  * via a pcbgroup.
115379bdc6e5SRobert Watson  *
115479bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
115579bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
115679bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
115779bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
115879bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
115979bdc6e5SRobert Watson  * inpcb.
116079bdc6e5SRobert Watson  *
116179bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
116279bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
116379bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
116479bdc6e5SRobert Watson  * using in_pcbrele().
1165c0a211c5SRobert Watson  */
116679bdc6e5SRobert Watson void
116779bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
11684c7c478dSRobert Watson {
1169df8bae1dSRodney W. Grimes 
117079bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
117179bdc6e5SRobert Watson 
117279bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
117379bdc6e5SRobert Watson }
117479bdc6e5SRobert Watson 
117579bdc6e5SRobert Watson /*
117679bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
117779bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
117879bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
117979bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
118079bdc6e5SRobert Watson  *
118179bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
118279bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
118379bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
118479bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
118579bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
118679bdc6e5SRobert Watson  */
118779bdc6e5SRobert Watson int
118879bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
118979bdc6e5SRobert Watson {
119079bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
119179bdc6e5SRobert Watson 
119279bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
119379bdc6e5SRobert Watson 
119479bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
119579bdc6e5SRobert Watson 
1196df4e91d3SGleb Smirnoff 	if (refcount_release(&inp->inp_refcount) == 0) {
1197df4e91d3SGleb Smirnoff 		/*
1198df4e91d3SGleb Smirnoff 		 * If the inpcb has been freed, let the caller know, even if
1199df4e91d3SGleb Smirnoff 		 * this isn't the last reference.
1200df4e91d3SGleb Smirnoff 		 */
1201df4e91d3SGleb Smirnoff 		if (inp->inp_flags2 & INP_FREED) {
1202df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1203df4e91d3SGleb Smirnoff 			return (1);
1204df4e91d3SGleb Smirnoff 		}
120579bdc6e5SRobert Watson 		return (0);
1206df4e91d3SGleb Smirnoff 	}
120779bdc6e5SRobert Watson 
120879bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
120979bdc6e5SRobert Watson 
121079bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
121179bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
121279bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
121379bdc6e5SRobert Watson 	return (1);
121479bdc6e5SRobert Watson }
121579bdc6e5SRobert Watson 
121679bdc6e5SRobert Watson int
121779bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
121879bdc6e5SRobert Watson {
121979bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
122079bdc6e5SRobert Watson 
122179bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
122279bdc6e5SRobert Watson 
122379bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
122479bdc6e5SRobert Watson 
1225edd0e0b0SFabien Thomas 	if (refcount_release(&inp->inp_refcount) == 0) {
1226edd0e0b0SFabien Thomas 		/*
1227edd0e0b0SFabien Thomas 		 * If the inpcb has been freed, let the caller know, even if
1228edd0e0b0SFabien Thomas 		 * this isn't the last reference.
1229edd0e0b0SFabien Thomas 		 */
1230edd0e0b0SFabien Thomas 		if (inp->inp_flags2 & INP_FREED) {
1231edd0e0b0SFabien Thomas 			INP_WUNLOCK(inp);
1232edd0e0b0SFabien Thomas 			return (1);
1233edd0e0b0SFabien Thomas 		}
123479bdc6e5SRobert Watson 		return (0);
1235edd0e0b0SFabien Thomas 	}
123679bdc6e5SRobert Watson 
123779bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
123879bdc6e5SRobert Watson 
123979bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
124079bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
124179bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
124279bdc6e5SRobert Watson 	return (1);
124379bdc6e5SRobert Watson }
124479bdc6e5SRobert Watson 
124579bdc6e5SRobert Watson /*
124679bdc6e5SRobert Watson  * Temporary wrapper.
124779bdc6e5SRobert Watson  */
124879bdc6e5SRobert Watson int
124979bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
125079bdc6e5SRobert Watson {
125179bdc6e5SRobert Watson 
125279bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
125379bdc6e5SRobert Watson }
125479bdc6e5SRobert Watson 
125579bdc6e5SRobert Watson /*
125679bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
125779bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
125879bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
125979bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
126079bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
126179bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
126279bdc6e5SRobert Watson  * the pcbinfo lock is held.
126379bdc6e5SRobert Watson  */
126479bdc6e5SRobert Watson void
126579bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
126679bdc6e5SRobert Watson {
126779bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
126879bdc6e5SRobert Watson 
126979bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
127079bdc6e5SRobert Watson 
1271ff9b006dSJulien Charbon #ifdef INVARIANTS
1272ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
1273079672cbSJulien Charbon 		INP_INFO_LOCK_ASSERT(pcbinfo);
1274ff9b006dSJulien Charbon 	} else {
127579bdc6e5SRobert Watson 		INP_INFO_WLOCK_ASSERT(pcbinfo);
1276ff9b006dSJulien Charbon 	}
1277ff9b006dSJulien Charbon #endif
127879bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
127979bdc6e5SRobert Watson 
128079bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1281b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
12826aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
12836974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
128483e521ecSBjoern A. Zeeb #endif
1285ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
128679bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1287c3229e05SDavid Greenman 	in_pcbremlists(inp);
1288ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
12896aee2fc5SBjoern A. Zeeb #ifdef INET6
12906aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
12916aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
12921096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
12936aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
12946aee2fc5SBjoern A. Zeeb 	}
12956aee2fc5SBjoern A. Zeeb #endif
1296df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1297df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
129867107f45SBjoern A. Zeeb #ifdef INET
129971498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
130071498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
130167107f45SBjoern A. Zeeb #endif
130284cc0778SGeorge V. Neville-Neil 	if (inp->inp_route.ro_rt) {
130384cc0778SGeorge V. Neville-Neil 		RTFREE(inp->inp_route.ro_rt);
130484cc0778SGeorge V. Neville-Neil 		inp->inp_route.ro_rt = (struct rtentry *)NULL;
130584cc0778SGeorge V. Neville-Neil 	}
1306*6d768226SGeorge V. Neville-Neil 	if (inp->inp_route.ro_lle)
1307*6d768226SGeorge V. Neville-Neil 		LLE_FREE(inp->inp_route.ro_lle);	/* zeros ro_lle */
130884cc0778SGeorge V. Neville-Neil 
1309cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1310df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
131186d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1312a557af22SRobert Watson #ifdef MAC
131330d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1314a557af22SRobert Watson #endif
131579bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
131628696211SRobert Watson 		INP_WUNLOCK(inp);
131728696211SRobert Watson }
131828696211SRobert Watson 
131928696211SRobert Watson /*
1320c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1321c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1322c0a211c5SRobert Watson  *
1323c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1324c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1325c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1326c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1327c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1328c0a211c5SRobert Watson  * in_pcbdetach().
1329c0a211c5SRobert Watson  *
1330c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1331c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
133210702a28SRobert Watson  */
133310702a28SRobert Watson void
133410702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
133510702a28SRobert Watson {
133610702a28SRobert Watson 
13378501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
133810702a28SRobert Watson 
1339fa046d87SRobert Watson 	/*
1340fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1341fa046d87SRobert Watson 	 * the hash lock...?
1342fa046d87SRobert Watson 	 */
1343ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1344111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
134510702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
134610702a28SRobert Watson 
1347fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
134810702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
134910702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
135010702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
135110702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
135210702a28SRobert Watson 			free(phd, M_PCB);
135310702a28SRobert Watson 		}
1354fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1355111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
135652cd27cbSRobert Watson #ifdef PCBGROUP
135752cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
135852cd27cbSRobert Watson #endif
135910702a28SRobert Watson 	}
136010702a28SRobert Watson }
136110702a28SRobert Watson 
136267107f45SBjoern A. Zeeb #ifdef INET
136354d642bbSRobert Watson /*
136454d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
136554d642bbSRobert Watson  */
136626ef6ac4SDon Lewis struct sockaddr *
1367136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
136826ef6ac4SDon Lewis {
136926ef6ac4SDon Lewis 	struct sockaddr_in *sin;
137026ef6ac4SDon Lewis 
13711ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1372a163d034SWarner Losh 		M_WAITOK | M_ZERO);
137326ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
137426ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
137526ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
137626ef6ac4SDon Lewis 	sin->sin_port = port;
137726ef6ac4SDon Lewis 
137826ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
137926ef6ac4SDon Lewis }
138026ef6ac4SDon Lewis 
1381117bcae7SGarrett Wollman int
138254d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1383df8bae1dSRodney W. Grimes {
1384136d4f1cSRobert Watson 	struct inpcb *inp;
138526ef6ac4SDon Lewis 	struct in_addr addr;
138626ef6ac4SDon Lewis 	in_port_t port;
138742fa505bSDavid Greenman 
1388fdc984f7STor Egge 	inp = sotoinpcb(so);
138954d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
13906466b28aSRobert Watson 
1391a69042a5SRobert Watson 	INP_RLOCK(inp);
139226ef6ac4SDon Lewis 	port = inp->inp_lport;
139326ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1394a69042a5SRobert Watson 	INP_RUNLOCK(inp);
139542fa505bSDavid Greenman 
139626ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1397117bcae7SGarrett Wollman 	return 0;
1398df8bae1dSRodney W. Grimes }
1399df8bae1dSRodney W. Grimes 
1400117bcae7SGarrett Wollman int
140154d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1402df8bae1dSRodney W. Grimes {
1403136d4f1cSRobert Watson 	struct inpcb *inp;
140426ef6ac4SDon Lewis 	struct in_addr addr;
140526ef6ac4SDon Lewis 	in_port_t port;
140642fa505bSDavid Greenman 
1407fdc984f7STor Egge 	inp = sotoinpcb(so);
140854d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
14096466b28aSRobert Watson 
1410a69042a5SRobert Watson 	INP_RLOCK(inp);
141126ef6ac4SDon Lewis 	port = inp->inp_fport;
141226ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1413a69042a5SRobert Watson 	INP_RUNLOCK(inp);
141442fa505bSDavid Greenman 
141526ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1416117bcae7SGarrett Wollman 	return 0;
1417df8bae1dSRodney W. Grimes }
1418df8bae1dSRodney W. Grimes 
141926f9a767SRodney W. Grimes void
1420136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1421136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1422d1c54148SJesper Skriver {
1423f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1424d1c54148SJesper Skriver 
14253dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1426f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
14278501a69cSRobert Watson 		INP_WLOCK(inp);
1428d1c54148SJesper Skriver #ifdef INET6
1429f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
14308501a69cSRobert Watson 			INP_WUNLOCK(inp);
1431d1c54148SJesper Skriver 			continue;
1432f76fcf6dSJeffrey Hsu 		}
1433d1c54148SJesper Skriver #endif
1434d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1435f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
14368501a69cSRobert Watson 			INP_WUNLOCK(inp);
1437d1c54148SJesper Skriver 			continue;
1438d1c54148SJesper Skriver 		}
14393dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
14408501a69cSRobert Watson 			INP_WUNLOCK(inp);
1441f76fcf6dSJeffrey Hsu 	}
14423dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1443d1c54148SJesper Skriver }
1444d1c54148SJesper Skriver 
1445e43cc4aeSHajimu UMEMOTO void
1446136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1447e43cc4aeSHajimu UMEMOTO {
1448e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1449e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1450e43cc4aeSHajimu UMEMOTO 	int i, gap;
1451e43cc4aeSHajimu UMEMOTO 
1452ff9b006dSJulien Charbon 	INP_INFO_WLOCK(pcbinfo);
1453712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
14548501a69cSRobert Watson 		INP_WLOCK(inp);
1455e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1456e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1457e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1458e43cc4aeSHajimu UMEMOTO 			/*
1459e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1460e43cc4aeSHajimu UMEMOTO 			 * detached.
1461e43cc4aeSHajimu UMEMOTO 			 */
1462e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1463e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1464e43cc4aeSHajimu UMEMOTO 
1465e43cc4aeSHajimu UMEMOTO 			/*
1466e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1467e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1468e43cc4aeSHajimu UMEMOTO 			 */
1469e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1470e43cc4aeSHajimu UMEMOTO 			    i++) {
1471e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1472e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1473e43cc4aeSHajimu UMEMOTO 					gap++;
1474e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1475e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1476e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1477e43cc4aeSHajimu UMEMOTO 			}
1478e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1479e43cc4aeSHajimu UMEMOTO 		}
14808501a69cSRobert Watson 		INP_WUNLOCK(inp);
1481e43cc4aeSHajimu UMEMOTO 	}
1482ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(pcbinfo);
1483e43cc4aeSHajimu UMEMOTO }
1484e43cc4aeSHajimu UMEMOTO 
1485df8bae1dSRodney W. Grimes /*
1486fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1487fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1488c3229e05SDavid Greenman  */
1489d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1490df8bae1dSRodney W. Grimes struct inpcb *
1491136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
149268e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1493df8bae1dSRodney W. Grimes {
1494136d4f1cSRobert Watson 	struct inpcb *inp;
1495d5e8a67eSHajimu UMEMOTO #ifdef INET6
1496d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1497d5e8a67eSHajimu UMEMOTO #else
1498d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1499d5e8a67eSHajimu UMEMOTO #endif
1500d5e8a67eSHajimu UMEMOTO 	int wildcard;
15017bc4aca7SDavid Greenman 
150268e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
150368e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
150468e0d7e0SRobert Watson 
1505fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
15061b73ca0bSSam Leffler 
150768e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1508c3229e05SDavid Greenman 		struct inpcbhead *head;
1509c3229e05SDavid Greenman 		/*
1510c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1511c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1512c3229e05SDavid Greenman 		 */
1513712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1514712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1515fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1516cfa1ca9dSYoshinobu Inoue #ifdef INET6
1517413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1518369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1519cfa1ca9dSYoshinobu Inoue 				continue;
1520cfa1ca9dSYoshinobu Inoue #endif
1521c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1522c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1523c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1524c3229e05SDavid Greenman 				/*
1525413628a7SBjoern A. Zeeb 				 * Found?
1526c3229e05SDavid Greenman 				 */
1527413628a7SBjoern A. Zeeb 				if (cred == NULL ||
15280304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
15290304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1530c3229e05SDavid Greenman 					return (inp);
1531df8bae1dSRodney W. Grimes 			}
1532c3229e05SDavid Greenman 		}
1533c3229e05SDavid Greenman 		/*
1534c3229e05SDavid Greenman 		 * Not found.
1535c3229e05SDavid Greenman 		 */
1536c3229e05SDavid Greenman 		return (NULL);
1537c3229e05SDavid Greenman 	} else {
1538c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1539c3229e05SDavid Greenman 		struct inpcbport *phd;
1540c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1541c3229e05SDavid Greenman 		/*
1542c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1543c3229e05SDavid Greenman 		 *
1544c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1545c3229e05SDavid Greenman 		 * port hash list.
1546c3229e05SDavid Greenman 		 */
1547712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1548712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1549fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1550c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1551c3229e05SDavid Greenman 				break;
1552c3229e05SDavid Greenman 		}
1553c3229e05SDavid Greenman 		if (phd != NULL) {
1554c3229e05SDavid Greenman 			/*
1555c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1556c3229e05SDavid Greenman 			 * fit.
1557c3229e05SDavid Greenman 			 */
155837d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1559c3229e05SDavid Greenman 				wildcard = 0;
1560413628a7SBjoern A. Zeeb 				if (cred != NULL &&
15610304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
15620304c731SJamie Gritton 					cred->cr_prison))
1563413628a7SBjoern A. Zeeb 					continue;
1564cfa1ca9dSYoshinobu Inoue #ifdef INET6
1565413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1566369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1567cfa1ca9dSYoshinobu Inoue 					continue;
1568d5e8a67eSHajimu UMEMOTO 				/*
1569d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1570d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1571d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1572d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1573d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1574d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1575d5e8a67eSHajimu UMEMOTO 				 *
1576d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1577d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1578d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1579d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1580d5e8a67eSHajimu UMEMOTO 				 */
1581d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1582d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1583cfa1ca9dSYoshinobu Inoue #endif
1584c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1585c3229e05SDavid Greenman 					wildcard++;
158615bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
158715bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
158815bd2b43SDavid Greenman 						wildcard++;
158915bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
159015bd2b43SDavid Greenman 						continue;
159115bd2b43SDavid Greenman 				} else {
159215bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
159315bd2b43SDavid Greenman 						wildcard++;
159415bd2b43SDavid Greenman 				}
1595df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1596df8bae1dSRodney W. Grimes 					match = inp;
1597df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1598413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1599df8bae1dSRodney W. Grimes 						break;
1600df8bae1dSRodney W. Grimes 				}
1601df8bae1dSRodney W. Grimes 			}
16023dbdc25cSDavid Greenman 		}
1603df8bae1dSRodney W. Grimes 		return (match);
1604df8bae1dSRodney W. Grimes 	}
1605c3229e05SDavid Greenman }
1606d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
160715bd2b43SDavid Greenman 
160852cd27cbSRobert Watson #ifdef PCBGROUP
160952cd27cbSRobert Watson /*
161052cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
161152cd27cbSRobert Watson  */
161252cd27cbSRobert Watson static struct inpcb *
161352cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
161452cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
161552cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
161652cd27cbSRobert Watson {
161752cd27cbSRobert Watson 	struct inpcbhead *head;
161852cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
161952cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
162052cd27cbSRobert Watson 
162152cd27cbSRobert Watson 	/*
162252cd27cbSRobert Watson 	 * First look for an exact match.
162352cd27cbSRobert Watson 	 */
162452cd27cbSRobert Watson 	tmpinp = NULL;
162552cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
162652cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
162752cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
162852cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
162952cd27cbSRobert Watson #ifdef INET6
163052cd27cbSRobert Watson 		/* XXX inp locking */
163152cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
163252cd27cbSRobert Watson 			continue;
163352cd27cbSRobert Watson #endif
163452cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
163552cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
163652cd27cbSRobert Watson 		    inp->inp_fport == fport &&
163752cd27cbSRobert Watson 		    inp->inp_lport == lport) {
163852cd27cbSRobert Watson 			/*
163952cd27cbSRobert Watson 			 * XXX We should be able to directly return
164052cd27cbSRobert Watson 			 * the inp here, without any checks.
164152cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
164252cd27cbSRobert Watson 			 */
164352cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
164452cd27cbSRobert Watson 				goto found;
164552cd27cbSRobert Watson 			if (tmpinp == NULL)
164652cd27cbSRobert Watson 				tmpinp = inp;
164752cd27cbSRobert Watson 		}
164852cd27cbSRobert Watson 	}
164952cd27cbSRobert Watson 	if (tmpinp != NULL) {
165052cd27cbSRobert Watson 		inp = tmpinp;
165152cd27cbSRobert Watson 		goto found;
165252cd27cbSRobert Watson 	}
165352cd27cbSRobert Watson 
16540a100a6fSAdrian Chadd #ifdef	RSS
16550a100a6fSAdrian Chadd 	/*
16560a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
16570a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
16580a100a6fSAdrian Chadd 	 */
16590a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
16600a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
16610a100a6fSAdrian Chadd #ifdef INET6
16620a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
16630a100a6fSAdrian Chadd #endif
16640a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
16650a100a6fSAdrian Chadd 		struct inpcbhead *head;
16660a100a6fSAdrian Chadd 		int injail;
16670a100a6fSAdrian Chadd 
16680a100a6fSAdrian Chadd 		/*
16690a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
16700a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
16710a100a6fSAdrian Chadd 		 *      2. jailed, wild.
16720a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
16730a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
16740a100a6fSAdrian Chadd 		 */
16750a100a6fSAdrian Chadd 
16760a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
16770a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
16780a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
16790a100a6fSAdrian Chadd #ifdef INET6
16800a100a6fSAdrian Chadd 			/* XXX inp locking */
16810a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
16820a100a6fSAdrian Chadd 				continue;
16830a100a6fSAdrian Chadd #endif
16840a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
16850a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
16860a100a6fSAdrian Chadd 				continue;
16870a100a6fSAdrian Chadd 
16880a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
16890a100a6fSAdrian Chadd 			if (injail) {
16900a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
16910a100a6fSAdrian Chadd 				    &laddr) != 0)
16920a100a6fSAdrian Chadd 					continue;
16930a100a6fSAdrian Chadd 			} else {
16940a100a6fSAdrian Chadd 				if (local_exact != NULL)
16950a100a6fSAdrian Chadd 					continue;
16960a100a6fSAdrian Chadd 			}
16970a100a6fSAdrian Chadd 
16980a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
16990a100a6fSAdrian Chadd 				if (injail)
17000a100a6fSAdrian Chadd 					goto found;
17010a100a6fSAdrian Chadd 				else
17020a100a6fSAdrian Chadd 					local_exact = inp;
17030a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
17040a100a6fSAdrian Chadd #ifdef INET6
17050a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
17060a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
17070a100a6fSAdrian Chadd 					local_wild_mapped = inp;
17080a100a6fSAdrian Chadd 				else
17090a100a6fSAdrian Chadd #endif
17100a100a6fSAdrian Chadd 					if (injail)
17110a100a6fSAdrian Chadd 						jail_wild = inp;
17120a100a6fSAdrian Chadd 					else
17130a100a6fSAdrian Chadd 						local_wild = inp;
17140a100a6fSAdrian Chadd 			}
17150a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
17160a100a6fSAdrian Chadd 
17170a100a6fSAdrian Chadd 		inp = jail_wild;
17180a100a6fSAdrian Chadd 		if (inp == NULL)
17190a100a6fSAdrian Chadd 			inp = local_exact;
17200a100a6fSAdrian Chadd 		if (inp == NULL)
17210a100a6fSAdrian Chadd 			inp = local_wild;
17220a100a6fSAdrian Chadd #ifdef INET6
17230a100a6fSAdrian Chadd 		if (inp == NULL)
17240a100a6fSAdrian Chadd 			inp = local_wild_mapped;
17250a100a6fSAdrian Chadd #endif
17260a100a6fSAdrian Chadd 		if (inp != NULL)
17270a100a6fSAdrian Chadd 			goto found;
17280a100a6fSAdrian Chadd 	}
17290a100a6fSAdrian Chadd #endif
17300a100a6fSAdrian Chadd 
173152cd27cbSRobert Watson 	/*
173252cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
173352cd27cbSRobert Watson 	 */
173452cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
173552cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
173652cd27cbSRobert Watson #ifdef INET6
173752cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
173852cd27cbSRobert Watson #endif
173952cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
174052cd27cbSRobert Watson 		struct inpcbhead *head;
174152cd27cbSRobert Watson 		int injail;
174252cd27cbSRobert Watson 
174352cd27cbSRobert Watson 		/*
174452cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
174552cd27cbSRobert Watson 		 *      1. jailed, non-wild.
174652cd27cbSRobert Watson 		 *      2. jailed, wild.
174752cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
174852cd27cbSRobert Watson 		 *      4. non-jailed, wild.
174952cd27cbSRobert Watson 		 */
175052cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
175152cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
175252cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
175352cd27cbSRobert Watson #ifdef INET6
175452cd27cbSRobert Watson 			/* XXX inp locking */
175552cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
175652cd27cbSRobert Watson 				continue;
175752cd27cbSRobert Watson #endif
175852cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
175952cd27cbSRobert Watson 			    inp->inp_lport != lport)
176052cd27cbSRobert Watson 				continue;
176152cd27cbSRobert Watson 
176252cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
176352cd27cbSRobert Watson 			if (injail) {
176452cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
176552cd27cbSRobert Watson 				    &laddr) != 0)
176652cd27cbSRobert Watson 					continue;
176752cd27cbSRobert Watson 			} else {
176852cd27cbSRobert Watson 				if (local_exact != NULL)
176952cd27cbSRobert Watson 					continue;
177052cd27cbSRobert Watson 			}
177152cd27cbSRobert Watson 
177252cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
177352cd27cbSRobert Watson 				if (injail)
177452cd27cbSRobert Watson 					goto found;
177552cd27cbSRobert Watson 				else
177652cd27cbSRobert Watson 					local_exact = inp;
177752cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
177852cd27cbSRobert Watson #ifdef INET6
177952cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
178052cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
178152cd27cbSRobert Watson 					local_wild_mapped = inp;
178252cd27cbSRobert Watson 				else
178383e521ecSBjoern A. Zeeb #endif
178452cd27cbSRobert Watson 					if (injail)
178552cd27cbSRobert Watson 						jail_wild = inp;
178652cd27cbSRobert Watson 					else
178752cd27cbSRobert Watson 						local_wild = inp;
178852cd27cbSRobert Watson 			}
178952cd27cbSRobert Watson 		} /* LIST_FOREACH */
179052cd27cbSRobert Watson 		inp = jail_wild;
179152cd27cbSRobert Watson 		if (inp == NULL)
179252cd27cbSRobert Watson 			inp = local_exact;
179352cd27cbSRobert Watson 		if (inp == NULL)
179452cd27cbSRobert Watson 			inp = local_wild;
179552cd27cbSRobert Watson #ifdef INET6
179652cd27cbSRobert Watson 		if (inp == NULL)
179752cd27cbSRobert Watson 			inp = local_wild_mapped;
179883e521ecSBjoern A. Zeeb #endif
179952cd27cbSRobert Watson 		if (inp != NULL)
180052cd27cbSRobert Watson 			goto found;
180152cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
180252cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
180352cd27cbSRobert Watson 	return (NULL);
180452cd27cbSRobert Watson 
180552cd27cbSRobert Watson found:
180652cd27cbSRobert Watson 	in_pcbref(inp);
180752cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
180852cd27cbSRobert Watson 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
180952cd27cbSRobert Watson 		INP_WLOCK(inp);
181052cd27cbSRobert Watson 		if (in_pcbrele_wlocked(inp))
181152cd27cbSRobert Watson 			return (NULL);
181252cd27cbSRobert Watson 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
181352cd27cbSRobert Watson 		INP_RLOCK(inp);
181452cd27cbSRobert Watson 		if (in_pcbrele_rlocked(inp))
181552cd27cbSRobert Watson 			return (NULL);
181652cd27cbSRobert Watson 	} else
181752cd27cbSRobert Watson 		panic("%s: locking bug", __func__);
181852cd27cbSRobert Watson 	return (inp);
181952cd27cbSRobert Watson }
182052cd27cbSRobert Watson #endif /* PCBGROUP */
182152cd27cbSRobert Watson 
182215bd2b43SDavid Greenman /*
1823fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1824fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1825fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
182615bd2b43SDavid Greenman  */
1827fa046d87SRobert Watson static struct inpcb *
1828fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
182968e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1830136d4f1cSRobert Watson     struct ifnet *ifp)
183115bd2b43SDavid Greenman {
183215bd2b43SDavid Greenman 	struct inpcbhead *head;
1833413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
183415bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
183515bd2b43SDavid Greenman 
183668e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
183768e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
183868e0d7e0SRobert Watson 
1839fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1840602cc7f1SRobert Watson 
184115bd2b43SDavid Greenman 	/*
184215bd2b43SDavid Greenman 	 * First look for an exact match.
184315bd2b43SDavid Greenman 	 */
1844413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1845712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1846712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1847fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1848cfa1ca9dSYoshinobu Inoue #ifdef INET6
1849413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1850369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1851cfa1ca9dSYoshinobu Inoue 			continue;
1852cfa1ca9dSYoshinobu Inoue #endif
18536d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1854ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1855ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1856413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1857413628a7SBjoern A. Zeeb 			/*
1858413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1859413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1860413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1861413628a7SBjoern A. Zeeb 			 */
18620304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1863c3229e05SDavid Greenman 				return (inp);
1864413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1865413628a7SBjoern A. Zeeb 				tmpinp = inp;
1866c3229e05SDavid Greenman 		}
1867413628a7SBjoern A. Zeeb 	}
1868413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1869413628a7SBjoern A. Zeeb 		return (tmpinp);
1870e3fd5ffdSRobert Watson 
1871e3fd5ffdSRobert Watson 	/*
1872e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1873e3fd5ffdSRobert Watson 	 */
187468e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1875413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1876e3fd5ffdSRobert Watson #ifdef INET6
1877cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1878e3fd5ffdSRobert Watson #endif
1879413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1880413628a7SBjoern A. Zeeb 		int injail;
1881413628a7SBjoern A. Zeeb 
1882413628a7SBjoern A. Zeeb 		/*
1883413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1884413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1885413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1886413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1887413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1888413628a7SBjoern A. Zeeb 		 */
18896d6a026bSDavid Greenman 
1890712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1891712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1892fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1893cfa1ca9dSYoshinobu Inoue #ifdef INET6
1894413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1895369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1896cfa1ca9dSYoshinobu Inoue 				continue;
1897cfa1ca9dSYoshinobu Inoue #endif
1898413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1899413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1900413628a7SBjoern A. Zeeb 				continue;
1901413628a7SBjoern A. Zeeb 
19020304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1903413628a7SBjoern A. Zeeb 			if (injail) {
1904b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1905b89e82ddSJamie Gritton 				    &laddr) != 0)
1906413628a7SBjoern A. Zeeb 					continue;
1907413628a7SBjoern A. Zeeb 			} else {
1908413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1909413628a7SBjoern A. Zeeb 					continue;
1910413628a7SBjoern A. Zeeb 			}
1911413628a7SBjoern A. Zeeb 
1912413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1913413628a7SBjoern A. Zeeb 				if (injail)
1914c3229e05SDavid Greenman 					return (inp);
1915413628a7SBjoern A. Zeeb 				else
1916413628a7SBjoern A. Zeeb 					local_exact = inp;
1917413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1918e3fd5ffdSRobert Watson #ifdef INET6
1919413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
19205cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1921cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1922cfa1ca9dSYoshinobu Inoue 				else
192383e521ecSBjoern A. Zeeb #endif
1924413628a7SBjoern A. Zeeb 					if (injail)
1925413628a7SBjoern A. Zeeb 						jail_wild = inp;
1926413628a7SBjoern A. Zeeb 					else
19276d6a026bSDavid Greenman 						local_wild = inp;
19286d6a026bSDavid Greenman 			}
1929413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1930413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1931413628a7SBjoern A. Zeeb 			return (jail_wild);
1932413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1933413628a7SBjoern A. Zeeb 			return (local_exact);
1934413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1935c3229e05SDavid Greenman 			return (local_wild);
1936413628a7SBjoern A. Zeeb #ifdef INET6
1937413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1938413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
193983e521ecSBjoern A. Zeeb #endif
194068e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1941413628a7SBjoern A. Zeeb 
19426d6a026bSDavid Greenman 	return (NULL);
194315bd2b43SDavid Greenman }
1944fa046d87SRobert Watson 
1945fa046d87SRobert Watson /*
1946fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1947fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1948fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1949fa046d87SRobert Watson  */
1950fa046d87SRobert Watson static struct inpcb *
1951fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1952fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1953fa046d87SRobert Watson     struct ifnet *ifp)
1954fa046d87SRobert Watson {
1955fa046d87SRobert Watson 	struct inpcb *inp;
1956fa046d87SRobert Watson 
1957fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1958fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1959fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1960fa046d87SRobert Watson 	if (inp != NULL) {
1961fa046d87SRobert Watson 		in_pcbref(inp);
1962fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1963fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1964fa046d87SRobert Watson 			INP_WLOCK(inp);
1965fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1966fa046d87SRobert Watson 				return (NULL);
1967fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1968fa046d87SRobert Watson 			INP_RLOCK(inp);
1969fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1970fa046d87SRobert Watson 				return (NULL);
1971fa046d87SRobert Watson 		} else
1972fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1973fa046d87SRobert Watson 	} else
1974fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1975fa046d87SRobert Watson 	return (inp);
1976fa046d87SRobert Watson }
1977fa046d87SRobert Watson 
1978fa046d87SRobert Watson /*
1979d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1980d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
198152cd27cbSRobert Watson  *
198252cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
1983fa046d87SRobert Watson  */
1984fa046d87SRobert Watson struct inpcb *
1985fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1986fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1987fa046d87SRobert Watson {
19887527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
198952cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
199052cd27cbSRobert Watson #endif
1991fa046d87SRobert Watson 
1992fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1993fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1994fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1995fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1996fa046d87SRobert Watson 
19977527624eSRobert Watson 	/*
19987527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
19997527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
20007527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
20017527624eSRobert Watson 	 * in software.
20027527624eSRobert Watson 	 *
20037527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
20047527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
20057527624eSRobert Watson 	 * in software.
20067527624eSRobert Watson 	 */
20077527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
200852cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
200952cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
201052cd27cbSRobert Watson 		    fport);
201152cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
201252cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
201352cd27cbSRobert Watson 	}
201452cd27cbSRobert Watson #endif
2015fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2016fa046d87SRobert Watson 	    lookupflags, ifp));
2017fa046d87SRobert Watson }
2018d3c1f003SRobert Watson 
2019d3c1f003SRobert Watson struct inpcb *
2020d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2021d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2022d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2023d3c1f003SRobert Watson {
202452cd27cbSRobert Watson #ifdef PCBGROUP
202552cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
202652cd27cbSRobert Watson #endif
2027d3c1f003SRobert Watson 
2028d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2029d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2030d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2031d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2032d3c1f003SRobert Watson 
203352cd27cbSRobert Watson #ifdef PCBGROUP
20347527624eSRobert Watson 	/*
20357527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
20367527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
20377527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
20387527624eSRobert Watson 	 * using RSS.
20397527624eSRobert Watson 	 *
20407527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
20417527624eSRobert Watson 	 */
20427527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
20437527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
204452cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
204552cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
204652cd27cbSRobert Watson 		if (pcbgroup != NULL)
204752cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
204852cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
20497527624eSRobert Watson #ifndef RSS
205052cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
205152cd27cbSRobert Watson 		    fport);
205252cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
205352cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
20547527624eSRobert Watson #endif
205552cd27cbSRobert Watson 	}
205652cd27cbSRobert Watson #endif
2057d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2058d3c1f003SRobert Watson 	    lookupflags, ifp));
2059d3c1f003SRobert Watson }
206067107f45SBjoern A. Zeeb #endif /* INET */
206115bd2b43SDavid Greenman 
20627bc4aca7SDavid Greenman /*
2063c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
20647bc4aca7SDavid Greenman  */
206552cd27cbSRobert Watson static int
206652cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
206715bd2b43SDavid Greenman {
2068c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2069c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2070c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2071c3229e05SDavid Greenman 	struct inpcbport *phd;
2072cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
207315bd2b43SDavid Greenman 
20748501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2075fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2076fa046d87SRobert Watson 
2077111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2078111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2079602cc7f1SRobert Watson 
2080cfa1ca9dSYoshinobu Inoue #ifdef INET6
2081cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
20821b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2083cfa1ca9dSYoshinobu Inoue 	else
208483e521ecSBjoern A. Zeeb #endif
2085cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2086cfa1ca9dSYoshinobu Inoue 
2087712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2088712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
208915bd2b43SDavid Greenman 
2090712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2091712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2092c3229e05SDavid Greenman 
2093c3229e05SDavid Greenman 	/*
2094c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2095c3229e05SDavid Greenman 	 */
2096fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2097c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2098c3229e05SDavid Greenman 			break;
2099c3229e05SDavid Greenman 	}
2100c3229e05SDavid Greenman 	/*
2101c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2102c3229e05SDavid Greenman 	 */
2103c3229e05SDavid Greenman 	if (phd == NULL) {
21041ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2105c3229e05SDavid Greenman 		if (phd == NULL) {
2106c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2107c3229e05SDavid Greenman 		}
2108c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2109c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2110c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2111c3229e05SDavid Greenman 	}
2112c3229e05SDavid Greenman 	inp->inp_phd = phd;
2113c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2114c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2115111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
211652cd27cbSRobert Watson #ifdef PCBGROUP
211752cd27cbSRobert Watson 	if (do_pcbgroup_update)
211852cd27cbSRobert Watson 		in_pcbgroup_update(inp);
211952cd27cbSRobert Watson #endif
2120c3229e05SDavid Greenman 	return (0);
212115bd2b43SDavid Greenman }
212215bd2b43SDavid Greenman 
2123c3229e05SDavid Greenman /*
212452cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
212552cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
212652cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
212752cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
212852cd27cbSRobert Watson  * pcbgroup until later.
212952cd27cbSRobert Watson  *
213052cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
213152cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
213252cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
213352cd27cbSRobert Watson  */
213452cd27cbSRobert Watson int
213552cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
213652cd27cbSRobert Watson {
213752cd27cbSRobert Watson 
213852cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
213952cd27cbSRobert Watson }
214052cd27cbSRobert Watson 
214152cd27cbSRobert Watson int
214252cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
214352cd27cbSRobert Watson {
214452cd27cbSRobert Watson 
214552cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
214652cd27cbSRobert Watson }
214752cd27cbSRobert Watson 
214852cd27cbSRobert Watson /*
2149c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2150c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2151c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2152c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2153c3229e05SDavid Greenman  */
215415bd2b43SDavid Greenman void
2155d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
215615bd2b43SDavid Greenman {
215759daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
215815bd2b43SDavid Greenman 	struct inpcbhead *head;
2159cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
216015bd2b43SDavid Greenman 
21618501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2162fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2163fa046d87SRobert Watson 
2164111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2165111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2166602cc7f1SRobert Watson 
2167cfa1ca9dSYoshinobu Inoue #ifdef INET6
2168cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
21691b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2170cfa1ca9dSYoshinobu Inoue 	else
217183e521ecSBjoern A. Zeeb #endif
2172cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2173cfa1ca9dSYoshinobu Inoue 
2174712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2175712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
217615bd2b43SDavid Greenman 
2177c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
217815bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
217952cd27cbSRobert Watson 
218052cd27cbSRobert Watson #ifdef PCBGROUP
218152cd27cbSRobert Watson 	if (m != NULL)
218252cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
218352cd27cbSRobert Watson 	else
218452cd27cbSRobert Watson 		in_pcbgroup_update(inp);
218552cd27cbSRobert Watson #endif
2186c3229e05SDavid Greenman }
2187c3229e05SDavid Greenman 
2188d3c1f003SRobert Watson void
2189d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2190d3c1f003SRobert Watson {
2191d3c1f003SRobert Watson 
2192d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2193d3c1f003SRobert Watson }
2194d3c1f003SRobert Watson 
2195c3229e05SDavid Greenman /*
2196c3229e05SDavid Greenman  * Remove PCB from various lists.
2197c3229e05SDavid Greenman  */
21986d888973SRobert Watson static void
2199136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2200c3229e05SDavid Greenman {
220159daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
220259daba27SSam Leffler 
2203ff9b006dSJulien Charbon #ifdef INVARIANTS
2204ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
2205ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
2206ff9b006dSJulien Charbon 	} else {
220759daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
2208ff9b006dSJulien Charbon 	}
2209ff9b006dSJulien Charbon #endif
2210ff9b006dSJulien Charbon 
22118501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2212ff9b006dSJulien Charbon 	INP_LIST_WLOCK_ASSERT(pcbinfo);
221359daba27SSam Leffler 
221459daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2215111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2216c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2217c3229e05SDavid Greenman 
2218fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2219c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2220c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2221fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2222c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2223c3229e05SDavid Greenman 			free(phd, M_PCB);
2224c3229e05SDavid Greenman 		}
2225fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2226111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2227c3229e05SDavid Greenman 	}
2228c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
222959daba27SSam Leffler 	pcbinfo->ipi_count--;
223052cd27cbSRobert Watson #ifdef PCBGROUP
223152cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
223252cd27cbSRobert Watson #endif
223315bd2b43SDavid Greenman }
223475c13541SPoul-Henning Kamp 
2235a557af22SRobert Watson /*
223684cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
223784cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
223884cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
223984cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
224084cc0778SGeorge V. Neville-Neil  */
224184cc0778SGeorge V. Neville-Neil void
224284cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
224384cc0778SGeorge V. Neville-Neil {
224484cc0778SGeorge V. Neville-Neil 
224584cc0778SGeorge V. Neville-Neil 	if (inp->inp_route.ro_rt) {
224684cc0778SGeorge V. Neville-Neil 		RTFREE(inp->inp_route.ro_rt);
224784cc0778SGeorge V. Neville-Neil 		inp->inp_route.ro_rt = (struct rtentry *)NULL;
224884cc0778SGeorge V. Neville-Neil 	}
2249*6d768226SGeorge V. Neville-Neil 	if (inp->inp_route.ro_lle)
2250*6d768226SGeorge V. Neville-Neil 		LLE_FREE(inp->inp_route.ro_lle);	/* zeros ro_lle */
225184cc0778SGeorge V. Neville-Neil 	return;
225284cc0778SGeorge V. Neville-Neil }
225384cc0778SGeorge V. Neville-Neil 
225484cc0778SGeorge V. Neville-Neil /*
2255a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2256a557af22SRobert Watson  * label change into the in_pcb for the socket.
2257a557af22SRobert Watson  */
2258a557af22SRobert Watson void
2259136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2260a557af22SRobert Watson {
2261a557af22SRobert Watson #ifdef MAC
2262a557af22SRobert Watson 	struct inpcb *inp;
2263a557af22SRobert Watson 
22644c7c478dSRobert Watson 	inp = sotoinpcb(so);
22654c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2266602cc7f1SRobert Watson 
22678501a69cSRobert Watson 	INP_WLOCK(inp);
2268310e7cebSRobert Watson 	SOCK_LOCK(so);
2269a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2270310e7cebSRobert Watson 	SOCK_UNLOCK(so);
22718501a69cSRobert Watson 	INP_WUNLOCK(inp);
2272a557af22SRobert Watson #endif
2273a557af22SRobert Watson }
22745f311da2SMike Silbersack 
22755f311da2SMike Silbersack /*
2276ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2277ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2278ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2279ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2280ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
22815f311da2SMike Silbersack  */
2282aae49dd3SBjoern A. Zeeb static void
2283136d4f1cSRobert Watson ipport_tick(void *xtp)
22845f311da2SMike Silbersack {
22858b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2286ad3a630fSRobert Watson 
22875ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
22888b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
22898b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
22908b615593SMarko Zec 		if (V_ipport_tcpallocs <=
22918b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2292603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2293603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2294ad3a630fSRobert Watson 		} else
2295603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2296603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
22978b615593SMarko Zec 		CURVNET_RESTORE();
22988b615593SMarko Zec 	}
22995ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
23005f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
23015f311da2SMike Silbersack }
2302497057eeSRobert Watson 
2303aae49dd3SBjoern A. Zeeb static void
2304aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2305aae49dd3SBjoern A. Zeeb {
2306aae49dd3SBjoern A. Zeeb 
2307aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2308aae49dd3SBjoern A. Zeeb }
2309aae49dd3SBjoern A. Zeeb 
2310aae49dd3SBjoern A. Zeeb /*
2311aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2312aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2313aae49dd3SBjoern A. Zeeb  */
2314aae49dd3SBjoern A. Zeeb static void
2315aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2316aae49dd3SBjoern A. Zeeb {
2317aae49dd3SBjoern A. Zeeb 
2318aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2319fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2320aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2321aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2322aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2323aae49dd3SBjoern A. Zeeb }
2324aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2325aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2326aae49dd3SBjoern A. Zeeb 
23273d585327SKip Macy void
23283d585327SKip Macy inp_wlock(struct inpcb *inp)
23293d585327SKip Macy {
23303d585327SKip Macy 
23318501a69cSRobert Watson 	INP_WLOCK(inp);
23323d585327SKip Macy }
23333d585327SKip Macy 
23343d585327SKip Macy void
23353d585327SKip Macy inp_wunlock(struct inpcb *inp)
23363d585327SKip Macy {
23373d585327SKip Macy 
23388501a69cSRobert Watson 	INP_WUNLOCK(inp);
23393d585327SKip Macy }
23403d585327SKip Macy 
23413d585327SKip Macy void
23423d585327SKip Macy inp_rlock(struct inpcb *inp)
23433d585327SKip Macy {
23443d585327SKip Macy 
2345a69042a5SRobert Watson 	INP_RLOCK(inp);
23463d585327SKip Macy }
23473d585327SKip Macy 
23483d585327SKip Macy void
23493d585327SKip Macy inp_runlock(struct inpcb *inp)
23503d585327SKip Macy {
23513d585327SKip Macy 
2352a69042a5SRobert Watson 	INP_RUNLOCK(inp);
23533d585327SKip Macy }
23543d585327SKip Macy 
23553d585327SKip Macy #ifdef INVARIANTS
23563d585327SKip Macy void
2357e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
23583d585327SKip Macy {
23593d585327SKip Macy 
23608501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
23613d585327SKip Macy }
23623d585327SKip Macy 
23633d585327SKip Macy void
2364e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
23653d585327SKip Macy {
23663d585327SKip Macy 
23673d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
23683d585327SKip Macy }
23693d585327SKip Macy #endif
23703d585327SKip Macy 
23719378e437SKip Macy void
23729378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
23739378e437SKip Macy {
23749378e437SKip Macy 	struct inpcb *inp;
23759378e437SKip Macy 
2376ff9b006dSJulien Charbon 	INP_INFO_WLOCK(&V_tcbinfo);
237797021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
23789378e437SKip Macy 		INP_WLOCK(inp);
23799378e437SKip Macy 		func(inp, arg);
23809378e437SKip Macy 		INP_WUNLOCK(inp);
23819378e437SKip Macy 	}
2382ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(&V_tcbinfo);
23839378e437SKip Macy }
23849378e437SKip Macy 
23859378e437SKip Macy struct socket *
23869378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
23879378e437SKip Macy {
23889378e437SKip Macy 
23899378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23909378e437SKip Macy 	return (inp->inp_socket);
23919378e437SKip Macy }
23929378e437SKip Macy 
23939378e437SKip Macy struct tcpcb *
23949378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
23959378e437SKip Macy {
23969378e437SKip Macy 
23979378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23989378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
23999378e437SKip Macy }
24009378e437SKip Macy 
24019378e437SKip Macy int
24029378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
24039378e437SKip Macy {
24049378e437SKip Macy 
24059378e437SKip Macy 	return (inp->inp_ip_tos);
24069378e437SKip Macy }
24079378e437SKip Macy 
24089378e437SKip Macy void
24099378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
24109378e437SKip Macy {
24119378e437SKip Macy 
24129378e437SKip Macy 	inp->inp_ip_tos = val;
24139378e437SKip Macy }
24149378e437SKip Macy 
24159378e437SKip Macy void
2416df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
24179d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
24189378e437SKip Macy {
24199378e437SKip Macy 
24209d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2421df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2422df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
24239378e437SKip Macy 	*lp = inp->inp_lport;
24249378e437SKip Macy 	*fp = inp->inp_fport;
24259378e437SKip Macy }
24269378e437SKip Macy 
2427dd0e6c38SKip Macy struct inpcb *
2428dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2429dd0e6c38SKip Macy {
2430dd0e6c38SKip Macy 
2431dd0e6c38SKip Macy 	return (sotoinpcb(so));
2432dd0e6c38SKip Macy }
2433dd0e6c38SKip Macy 
2434dd0e6c38SKip Macy struct tcpcb *
2435dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2436dd0e6c38SKip Macy {
2437dd0e6c38SKip Macy 
2438dd0e6c38SKip Macy 	return (sototcpcb(so));
2439dd0e6c38SKip Macy }
2440dd0e6c38SKip Macy 
2441497057eeSRobert Watson #ifdef DDB
2442497057eeSRobert Watson static void
2443497057eeSRobert Watson db_print_indent(int indent)
2444497057eeSRobert Watson {
2445497057eeSRobert Watson 	int i;
2446497057eeSRobert Watson 
2447497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2448497057eeSRobert Watson 		db_printf(" ");
2449497057eeSRobert Watson }
2450497057eeSRobert Watson 
2451497057eeSRobert Watson static void
2452497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2453497057eeSRobert Watson {
2454497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2455497057eeSRobert Watson 
2456497057eeSRobert Watson 	db_print_indent(indent);
2457497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2458497057eeSRobert Watson 
2459497057eeSRobert Watson 	indent += 2;
2460497057eeSRobert Watson 
246103dc38a4SRobert Watson #ifdef INET6
2462dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2463497057eeSRobert Watson 		/* IPv6. */
2464497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2465497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
246683e521ecSBjoern A. Zeeb 	} else
246703dc38a4SRobert Watson #endif
246883e521ecSBjoern A. Zeeb 	{
2469497057eeSRobert Watson 		/* IPv4. */
2470497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2471497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2472497057eeSRobert Watson 	}
2473497057eeSRobert Watson 	db_print_indent(indent);
2474497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2475497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2476497057eeSRobert Watson 	db_print_indent(indent);
2477497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2478497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2479497057eeSRobert Watson }
2480497057eeSRobert Watson 
2481497057eeSRobert Watson static void
2482497057eeSRobert Watson db_print_inpflags(int inp_flags)
2483497057eeSRobert Watson {
2484497057eeSRobert Watson 	int comma;
2485497057eeSRobert Watson 
2486497057eeSRobert Watson 	comma = 0;
2487497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2488497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2489497057eeSRobert Watson 		comma = 1;
2490497057eeSRobert Watson 	}
2491497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2492497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2493497057eeSRobert Watson 		comma = 1;
2494497057eeSRobert Watson 	}
2495497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2496497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2497497057eeSRobert Watson 		comma = 1;
2498497057eeSRobert Watson 	}
2499497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2500497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2501497057eeSRobert Watson 		comma = 1;
2502497057eeSRobert Watson 	}
2503497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2504497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2505497057eeSRobert Watson 		comma = 1;
2506497057eeSRobert Watson 	}
2507497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2508497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2509497057eeSRobert Watson 		comma = 1;
2510497057eeSRobert Watson 	}
2511497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2512497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2513497057eeSRobert Watson 		comma = 1;
2514497057eeSRobert Watson 	}
2515497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2516497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2517497057eeSRobert Watson 		comma = 1;
2518497057eeSRobert Watson 	}
2519497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2520497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2521497057eeSRobert Watson 		comma = 1;
2522497057eeSRobert Watson 	}
2523497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2524497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2525497057eeSRobert Watson 		comma = 1;
2526497057eeSRobert Watson 	}
2527497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2528497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2529497057eeSRobert Watson 		comma = 1;
2530497057eeSRobert Watson 	}
25313cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
25323cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
25333cca425bSMichael Tuexen 		comma = 1;
25343cca425bSMichael Tuexen 	}
2535497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2536497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2537497057eeSRobert Watson 		comma = 1;
2538497057eeSRobert Watson 	}
2539497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2540497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2541497057eeSRobert Watson 		comma = 1;
2542497057eeSRobert Watson 	}
2543497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2544497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2545497057eeSRobert Watson 		comma = 1;
2546497057eeSRobert Watson 	}
2547497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2548497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2549497057eeSRobert Watson 		comma = 1;
2550497057eeSRobert Watson 	}
2551497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2552497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2553497057eeSRobert Watson 		comma = 1;
2554497057eeSRobert Watson 	}
2555497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2556497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2557497057eeSRobert Watson 		comma = 1;
2558497057eeSRobert Watson 	}
2559497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2560497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2561497057eeSRobert Watson 		comma = 1;
2562497057eeSRobert Watson 	}
2563497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2564497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2565497057eeSRobert Watson 		comma = 1;
2566497057eeSRobert Watson 	}
2567497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2568497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2569497057eeSRobert Watson 		comma = 1;
2570497057eeSRobert Watson 	}
2571ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2572ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2573ad71fe3cSRobert Watson 		comma  = 1;
2574ad71fe3cSRobert Watson 	}
2575ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2576ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2577ad71fe3cSRobert Watson 		comma  = 1;
2578ad71fe3cSRobert Watson 	}
2579ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2580ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2581ad71fe3cSRobert Watson 		comma  = 1;
2582ad71fe3cSRobert Watson 	}
2583ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2584ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2585ad71fe3cSRobert Watson 		comma  = 1;
2586ad71fe3cSRobert Watson 	}
2587497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2588497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2589497057eeSRobert Watson 		comma = 1;
2590497057eeSRobert Watson 	}
2591497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2592497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2593497057eeSRobert Watson 		comma = 1;
2594497057eeSRobert Watson 	}
2595497057eeSRobert Watson }
2596497057eeSRobert Watson 
2597497057eeSRobert Watson static void
2598497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2599497057eeSRobert Watson {
2600497057eeSRobert Watson 	int comma;
2601497057eeSRobert Watson 
2602497057eeSRobert Watson 	comma = 0;
2603497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2604497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2605497057eeSRobert Watson 		comma  = 1;
2606497057eeSRobert Watson 	}
2607497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2608497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2609497057eeSRobert Watson 		comma  = 1;
2610497057eeSRobert Watson 	}
2611497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2612497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2613497057eeSRobert Watson 		comma  = 1;
2614497057eeSRobert Watson 	}
2615497057eeSRobert Watson }
2616497057eeSRobert Watson 
26176d888973SRobert Watson static void
2618497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2619497057eeSRobert Watson {
2620497057eeSRobert Watson 
2621497057eeSRobert Watson 	db_print_indent(indent);
2622497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2623497057eeSRobert Watson 
2624497057eeSRobert Watson 	indent += 2;
2625497057eeSRobert Watson 
2626497057eeSRobert Watson 	db_print_indent(indent);
2627497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2628497057eeSRobert Watson 
2629497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2630497057eeSRobert Watson 
2631497057eeSRobert Watson 	db_print_indent(indent);
2632497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2633497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2634497057eeSRobert Watson 
2635497057eeSRobert Watson 	db_print_indent(indent);
2636497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2637497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2638497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2639497057eeSRobert Watson 	db_printf(")\n");
2640497057eeSRobert Watson 
2641497057eeSRobert Watson 	db_print_indent(indent);
2642497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2643497057eeSRobert Watson 	    inp->inp_vflag);
2644497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2645497057eeSRobert Watson 	db_printf(")\n");
2646497057eeSRobert Watson 
2647497057eeSRobert Watson 	db_print_indent(indent);
2648497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2649497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2650497057eeSRobert Watson 
2651497057eeSRobert Watson 	db_print_indent(indent);
2652497057eeSRobert Watson #ifdef INET6
2653497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2654497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2655497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2656497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2657497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2658497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2659497057eeSRobert Watson 		    inp->in6p_hops);
2660497057eeSRobert Watson 	} else
2661497057eeSRobert Watson #endif
2662497057eeSRobert Watson 	{
2663497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2664497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2665497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2666497057eeSRobert Watson 	}
2667497057eeSRobert Watson 
2668497057eeSRobert Watson 	db_print_indent(indent);
2669497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2670497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2671497057eeSRobert Watson }
2672497057eeSRobert Watson 
2673497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2674497057eeSRobert Watson {
2675497057eeSRobert Watson 	struct inpcb *inp;
2676497057eeSRobert Watson 
2677497057eeSRobert Watson 	if (!have_addr) {
2678497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2679497057eeSRobert Watson 		return;
2680497057eeSRobert Watson 	}
2681497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2682497057eeSRobert Watson 
2683497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2684497057eeSRobert Watson }
268583e521ecSBjoern A. Zeeb #endif /* DDB */
2686