xref: /freebsd/sys/netinet/in_pcb.c (revision 84cc0778d06816e6b0676b9f7133e747e3d8b7c8)
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>
76df8bae1dSRodney W. Grimes #include <net/route.h>
77b2bdc62aSAdrian Chadd #include <net/rss_config.h>
78530c0060SRobert Watson #include <net/vnet.h>
79df8bae1dSRodney W. Grimes 
8067107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
81df8bae1dSRodney W. Grimes #include <netinet/in.h>
82df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
83df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
84340c35deSJonathan Lemon #include <netinet/tcp_var.h>
855f311da2SMike Silbersack #include <netinet/udp.h>
865f311da2SMike Silbersack #include <netinet/udp_var.h>
8767107f45SBjoern A. Zeeb #endif
8867107f45SBjoern A. Zeeb #ifdef INET
8967107f45SBjoern A. Zeeb #include <netinet/in_var.h>
9067107f45SBjoern A. Zeeb #endif
91cfa1ca9dSYoshinobu Inoue #ifdef INET6
92cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
93efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
9467107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
9567107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
96cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
97cfa1ca9dSYoshinobu Inoue 
98df8bae1dSRodney W. Grimes 
99b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
100b9234fafSSam Leffler #include <netipsec/ipsec.h>
101b9234fafSSam Leffler #include <netipsec/key.h>
102b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
103b9234fafSSam Leffler 
104aed55708SRobert Watson #include <security/mac/mac_framework.h>
105aed55708SRobert Watson 
106aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
107aae49dd3SBjoern A. Zeeb 
108101f9fc8SPeter Wemm /*
109101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
110101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
111101f9fc8SPeter Wemm  */
112eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
115eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
116eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
117eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
118101f9fc8SPeter Wemm 
119b0d22693SCrist J. Clark /*
120b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
121b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
122b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
123b0d22693SCrist J. Clark  */
124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
126b0d22693SCrist J. Clark 
1275f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
130eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
131eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
132eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1333e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount);
134eddfbb76SRobert Watson 
1351e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1366ac48b74SMike Silbersack 
137d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
138d2025bd0SBjoern A. Zeeb #ifdef INET
139fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
140fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
141fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
142fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
14367107f45SBjoern A. Zeeb 
144bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
145bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
146bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
147bbd42ad0SPeter Wemm 
148bbd42ad0SPeter Wemm static int
14982d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
150bbd42ad0SPeter Wemm {
15130a4ab08SBruce Evans 	int error;
15230a4ab08SBruce Evans 
153f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
15430a4ab08SBruce Evans 	if (error == 0) {
155603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
156603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
157603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
158603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
159603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
160603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
161bbd42ad0SPeter Wemm 	}
16230a4ab08SBruce Evans 	return (error);
163bbd42ad0SPeter Wemm }
164bbd42ad0SPeter Wemm 
165bbd42ad0SPeter Wemm #undef RANGECHK
166bbd42ad0SPeter Wemm 
1676472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
1686472ac3dSEd Schouten     "IP Ports");
16933b3ac06SPeter Wemm 
1706df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1716df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1726df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
1736df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1746df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1756df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
1766df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1776df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1786df8a710SGleb Smirnoff 	&VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
1796df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1806df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1816df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
1826df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
1836df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1846df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
1856df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
1866df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1876df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
1886df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
1896df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
1906df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
192eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
1946df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
195eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
1976df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
198eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
1996ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
2016df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
202eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
2038b615593SMarko Zec 	"Minimum time to keep sequental port "
2046ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
20583e521ecSBjoern A. Zeeb #endif /* INET */
2060312fbe9SPoul-Henning Kamp 
207c3229e05SDavid Greenman /*
208c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
209c3229e05SDavid Greenman  *
210de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
211de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
212de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
213c3229e05SDavid Greenman  */
214c3229e05SDavid Greenman 
215c3229e05SDavid Greenman /*
2169bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
2179bcd427bSRobert Watson  * arguments in time.
2189bcd427bSRobert Watson  */
2199bcd427bSRobert Watson void
2209bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
2219bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
2229bcd427bSRobert Watson     char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
22352cd27cbSRobert Watson     uint32_t inpcbzone_flags, u_int hashfields)
2249bcd427bSRobert Watson {
2259bcd427bSRobert Watson 
2269bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
227fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
228ff9b006dSJulien Charbon 	INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
2299bcd427bSRobert Watson #ifdef VIMAGE
2309bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
2319bcd427bSRobert Watson #endif
2329bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
2339bcd427bSRobert Watson 	LIST_INIT(pcbinfo->ipi_listhead);
234fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
2359bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
2369bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
2379bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
2389bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
23952cd27cbSRobert Watson #ifdef PCBGROUP
24052cd27cbSRobert Watson 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
24152cd27cbSRobert Watson #endif
2429bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
2439bcd427bSRobert Watson 	    NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
2449bcd427bSRobert Watson 	    inpcbzone_flags);
2459bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
2466acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
2476acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
2489bcd427bSRobert Watson }
2499bcd427bSRobert Watson 
2509bcd427bSRobert Watson /*
2519bcd427bSRobert Watson  * Destroy an inpcbinfo.
2529bcd427bSRobert Watson  */
2539bcd427bSRobert Watson void
2549bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
2559bcd427bSRobert Watson {
2569bcd427bSRobert Watson 
257fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
258fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
259fa046d87SRobert Watson 
2609bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
2619bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
2629bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
26352cd27cbSRobert Watson #ifdef PCBGROUP
26452cd27cbSRobert Watson 	in_pcbgroup_destroy(pcbinfo);
26552cd27cbSRobert Watson #endif
2669bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
267ff9b006dSJulien Charbon 	INP_LIST_LOCK_DESTROY(pcbinfo);
268fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
2699bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
2709bcd427bSRobert Watson }
2719bcd427bSRobert Watson 
2729bcd427bSRobert Watson /*
273c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
274d915b280SStephan Uphoff  * On success return with the PCB locked.
275c3229e05SDavid Greenman  */
276df8bae1dSRodney W. Grimes int
277d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
278df8bae1dSRodney W. Grimes {
279136d4f1cSRobert Watson 	struct inpcb *inp;
28013cf67f3SHajimu UMEMOTO 	int error;
281a557af22SRobert Watson 
282ff9b006dSJulien Charbon #ifdef INVARIANTS
283ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
284ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
285ff9b006dSJulien Charbon 	} else {
28659daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
287ff9b006dSJulien Charbon 	}
288ff9b006dSJulien Charbon #endif
289ff9b006dSJulien Charbon 
290a557af22SRobert Watson 	error = 0;
291d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
292df8bae1dSRodney W. Grimes 	if (inp == NULL)
293df8bae1dSRodney W. Grimes 		return (ENOBUFS);
294d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
29515bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
296df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
29786d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
2988b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
299a557af22SRobert Watson #ifdef MAC
30030d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
301a557af22SRobert Watson 	if (error != 0)
302a557af22SRobert Watson 		goto out;
30330d239bcSRobert Watson 	mac_inpcb_create(so, inp);
304a557af22SRobert Watson #endif
305b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
30613cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
3070bffde27SRobert Watson 	if (error != 0) {
3080bffde27SRobert Watson #ifdef MAC
3090bffde27SRobert Watson 		mac_inpcb_destroy(inp);
3100bffde27SRobert Watson #endif
311a557af22SRobert Watson 		goto out;
3120bffde27SRobert Watson 	}
313b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
314e3fd5ffdSRobert Watson #ifdef INET6
315340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
316340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
317603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
31833841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
319340c35deSJonathan Lemon 	}
32075daea93SPaul Saab #endif
321ff9b006dSJulien Charbon 	INP_WLOCK(inp);
322ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
323712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
3243d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
325df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
32633841545SHajimu UMEMOTO #ifdef INET6
327603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
32833841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
32933841545SHajimu UMEMOTO #endif
330d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
33179bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
332ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
333b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
334a557af22SRobert Watson out:
33586d02c5cSBjoern A. Zeeb 	if (error != 0) {
33686d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
337a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
33886d02c5cSBjoern A. Zeeb 	}
339a557af22SRobert Watson #endif
340a557af22SRobert Watson 	return (error);
341df8bae1dSRodney W. Grimes }
342df8bae1dSRodney W. Grimes 
34367107f45SBjoern A. Zeeb #ifdef INET
344df8bae1dSRodney W. Grimes int
345136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
346df8bae1dSRodney W. Grimes {
3474b932371SIan Dowse 	int anonport, error;
3484b932371SIan Dowse 
3498501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
350fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
35159daba27SSam Leffler 
3524b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
3534b932371SIan Dowse 		return (EINVAL);
3545cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
3554b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
356b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
3574b932371SIan Dowse 	if (error)
3584b932371SIan Dowse 		return (error);
3594b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
3604b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
3614b932371SIan Dowse 		inp->inp_lport = 0;
3624b932371SIan Dowse 		return (EAGAIN);
3634b932371SIan Dowse 	}
3644b932371SIan Dowse 	if (anonport)
3654b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
3664b932371SIan Dowse 	return (0);
3674b932371SIan Dowse }
36867107f45SBjoern A. Zeeb #endif
3694b932371SIan Dowse 
37039c8c62eSHiren Panchasara /*
37139c8c62eSHiren Panchasara  * Select a local port (number) to use.
37239c8c62eSHiren Panchasara  */
373efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
374efc76f72SBjoern A. Zeeb int
375efc76f72SBjoern A. Zeeb in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
37668e0d7e0SRobert Watson     struct ucred *cred, int lookupflags)
377efc76f72SBjoern A. Zeeb {
378efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
379efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
380efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
381efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
382efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
383efc76f72SBjoern A. Zeeb #ifdef INET
384efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
385efc76f72SBjoern A. Zeeb #endif
386efc76f72SBjoern A. Zeeb 
387efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
388efc76f72SBjoern A. Zeeb 
389efc76f72SBjoern A. Zeeb 	/*
390efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
391efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
392efc76f72SBjoern A. Zeeb 	 */
393efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
394fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
395efc76f72SBjoern A. Zeeb 
396efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
397efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
398efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
399efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
400efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
401efc76f72SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
402efc76f72SBjoern A. Zeeb 		if (error)
403efc76f72SBjoern A. Zeeb 			return (error);
404efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
405efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
406efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
407efc76f72SBjoern A. Zeeb 	} else {
408efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
409efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
410efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
411efc76f72SBjoern A. Zeeb 	}
412efc76f72SBjoern A. Zeeb 	/*
413e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
414efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
415efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
416efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
417efc76f72SBjoern A. Zeeb 	 */
418efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
419e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
420e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
421efc76f72SBjoern A. Zeeb 		dorandom = 1;
422efc76f72SBjoern A. Zeeb 	else
423efc76f72SBjoern A. Zeeb 		dorandom = 0;
424efc76f72SBjoern A. Zeeb 	/*
425efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
426efc76f72SBjoern A. Zeeb 	 * we have the only port available.
427efc76f72SBjoern A. Zeeb 	 */
428efc76f72SBjoern A. Zeeb 	if (first == last)
429efc76f72SBjoern A. Zeeb 		dorandom = 0;
430e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
431e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
432efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
433efc76f72SBjoern A. Zeeb 	/*
434efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
435efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
436efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
437efc76f72SBjoern A. Zeeb 	 */
438efc76f72SBjoern A. Zeeb 	if (first > last) {
439efc76f72SBjoern A. Zeeb 		aux = first;
440efc76f72SBjoern A. Zeeb 		first = last;
441efc76f72SBjoern A. Zeeb 		last = aux;
442efc76f72SBjoern A. Zeeb 	}
443efc76f72SBjoern A. Zeeb 
444efc76f72SBjoern A. Zeeb #ifdef INET
445efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
446efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
4474d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
448efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
449efc76f72SBjoern A. Zeeb 		    __func__, inp));
450efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
451efc76f72SBjoern A. Zeeb 	}
452efc76f72SBjoern A. Zeeb #endif
45367107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
454efc76f72SBjoern A. Zeeb 	lport = *lportp;
455efc76f72SBjoern A. Zeeb 
456efc76f72SBjoern A. Zeeb 	if (dorandom)
457efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
458efc76f72SBjoern A. Zeeb 
459efc76f72SBjoern A. Zeeb 	count = last - first;
460efc76f72SBjoern A. Zeeb 
461efc76f72SBjoern A. Zeeb 	do {
462efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
463efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
464efc76f72SBjoern A. Zeeb 		++*lastport;
465efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
466efc76f72SBjoern A. Zeeb 			*lastport = first;
467efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
468efc76f72SBjoern A. Zeeb 
469efc76f72SBjoern A. Zeeb #ifdef INET6
470efc76f72SBjoern A. Zeeb 		if ((inp->inp_vflag & INP_IPV6) != 0)
471efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
47268e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
473efc76f72SBjoern A. Zeeb #endif
474efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
475efc76f72SBjoern A. Zeeb 		else
476efc76f72SBjoern A. Zeeb #endif
477efc76f72SBjoern A. Zeeb #ifdef INET
478efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
47968e0d7e0SRobert Watson 			    lport, lookupflags, cred);
480efc76f72SBjoern A. Zeeb #endif
481efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
482efc76f72SBjoern A. Zeeb 
483efc76f72SBjoern A. Zeeb #ifdef INET
4844d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
485efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
486efc76f72SBjoern A. Zeeb #endif
487efc76f72SBjoern A. Zeeb 	*lportp = lport;
488efc76f72SBjoern A. Zeeb 
489efc76f72SBjoern A. Zeeb 	return (0);
490efc76f72SBjoern A. Zeeb }
491efdf104bSMikolaj Golub 
492efdf104bSMikolaj Golub /*
493efdf104bSMikolaj Golub  * Return cached socket options.
494efdf104bSMikolaj Golub  */
495efdf104bSMikolaj Golub short
496efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
497efdf104bSMikolaj Golub {
498efdf104bSMikolaj Golub    short so_options;
499efdf104bSMikolaj Golub 
500efdf104bSMikolaj Golub    so_options = 0;
501efdf104bSMikolaj Golub 
502efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
503efdf104bSMikolaj Golub 	   so_options |= SO_REUSEPORT;
504efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
505efdf104bSMikolaj Golub 	   so_options |= SO_REUSEADDR;
506efdf104bSMikolaj Golub    return (so_options);
507efdf104bSMikolaj Golub }
508efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
509efc76f72SBjoern A. Zeeb 
5104b932371SIan Dowse /*
5110a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
5120a100a6fSAdrian Chadd  *
5130a100a6fSAdrian Chadd  * ni points to the new inp.
5140a100a6fSAdrian Chadd  * oi points to the exisitng inp.
5150a100a6fSAdrian Chadd  *
5160a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
5170a100a6fSAdrian Chadd  * whether the credentials match.
5180a100a6fSAdrian Chadd  */
519d5bb8bd3SAdrian Chadd int
5200a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
5210a100a6fSAdrian Chadd {
5220a100a6fSAdrian Chadd 	/* Check permissions match */
5230a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5240a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
5250a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
5260a100a6fSAdrian Chadd 		return (0);
5270a100a6fSAdrian Chadd 
5280a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
5290a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5300a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
5310a100a6fSAdrian Chadd 		return (0);
5320a100a6fSAdrian Chadd 
5330a100a6fSAdrian Chadd 	/*
5340a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
5350a100a6fSAdrian Chadd 	 * it is and it matches the checks.
5360a100a6fSAdrian Chadd 	 */
5370a100a6fSAdrian Chadd 	return (1);
5380a100a6fSAdrian Chadd }
5390a100a6fSAdrian Chadd 
540d5bb8bd3SAdrian Chadd #ifdef INET
5410a100a6fSAdrian Chadd /*
5424b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
5434b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
5444b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
5454b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
5464b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
5474b932371SIan Dowse  *
5484b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
5494b932371SIan Dowse  */
5504b932371SIan Dowse int
551136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
552136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
5534b932371SIan Dowse {
5544b932371SIan Dowse 	struct socket *so = inp->inp_socket;
55515bd2b43SDavid Greenman 	struct sockaddr_in *sin;
556c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
5574b932371SIan Dowse 	struct in_addr laddr;
558df8bae1dSRodney W. Grimes 	u_short lport = 0;
55968e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
560413628a7SBjoern A. Zeeb 	int error;
561df8bae1dSRodney W. Grimes 
5628501a69cSRobert Watson 	/*
563fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
5648501a69cSRobert Watson 	 */
56559daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
566fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
56759daba27SSam Leffler 
568603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
569df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5704b932371SIan Dowse 	laddr.s_addr = *laddrp;
5714b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
572df8bae1dSRodney W. Grimes 		return (EINVAL);
573c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
57468e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
5757c2f3cb9SJamie Gritton 	if (nam == NULL) {
5767c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
5777c2f3cb9SJamie Gritton 			return (error);
5787c2f3cb9SJamie Gritton 	} else {
57957bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
58057bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
581df8bae1dSRodney W. Grimes 			return (EINVAL);
582df8bae1dSRodney W. Grimes #ifdef notdef
583df8bae1dSRodney W. Grimes 		/*
584df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
585df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
586df8bae1dSRodney W. Grimes 		 */
587df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
588df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
589df8bae1dSRodney W. Grimes #endif
590b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
591b89e82ddSJamie Gritton 		if (error)
592b89e82ddSJamie Gritton 			return (error);
5934b932371SIan Dowse 		if (sin->sin_port != *lportp) {
5944b932371SIan Dowse 			/* Don't allow the port to change. */
5954b932371SIan Dowse 			if (*lportp != 0)
5964b932371SIan Dowse 				return (EINVAL);
597df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
5984b932371SIan Dowse 		}
5994b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
600df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
601df8bae1dSRodney W. Grimes 			/*
602df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
603df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
604df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
605df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
606df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
607df8bae1dSRodney W. Grimes 			 */
608f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
609df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
610df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
611df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
61283103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
6134209e01aSAdrian Chadd 			/*
6144209e01aSAdrian Chadd 			 * Is the address a local IP address?
615f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
6168696873dSAdrian Chadd 			 * to any endpoint address, local or not.
6174209e01aSAdrian Chadd 			 */
618f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
6198896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
620df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
621df8bae1dSRodney W. Grimes 		}
6224b932371SIan Dowse 		laddr = sin->sin_addr;
623df8bae1dSRodney W. Grimes 		if (lport) {
624df8bae1dSRodney W. Grimes 			struct inpcb *t;
625ae0e7143SRobert Watson 			struct tcptw *tw;
626ae0e7143SRobert Watson 
627df8bae1dSRodney W. Grimes 			/* GROSS */
628603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
629603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
630acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
63132f9753cSRobert Watson 			    0))
6322469dd60SGarrett Wollman 				return (EACCES);
633835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
63486d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
63532f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
636078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
637413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
638340c35deSJonathan Lemon 	/*
639340c35deSJonathan Lemon 	 * XXX
640340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
641340c35deSJonathan Lemon 	 */
6424cc20ab1SSeigo Tanimura 				if (t &&
6430a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
644ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
6454658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
6464658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
6474cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
64852b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
649fc06cd42SMikolaj Golub 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
65086d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
65186d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
6524049a042SGuido van Rooij 					return (EADDRINUSE);
6530a100a6fSAdrian Chadd 
6540a100a6fSAdrian Chadd 				/*
6550a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
6560a100a6fSAdrian Chadd 				 * the credentials need to match and the
6570a100a6fSAdrian Chadd 				 * original socket also has to have been bound
6580a100a6fSAdrian Chadd 				 * with BINDMULTI.
6590a100a6fSAdrian Chadd 				 */
6600a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6610a100a6fSAdrian Chadd 					return (EADDRINUSE);
6624049a042SGuido van Rooij 			}
663c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
66468e0d7e0SRobert Watson 			    lport, lookupflags, cred);
665ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
666ae0e7143SRobert Watson 				/*
667ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
668ae0e7143SRobert Watson 				 * state recycled, we treat the address as
669ae0e7143SRobert Watson 				 * being in use (for now).  This is better
670ae0e7143SRobert Watson 				 * than a panic, but not desirable.
671ae0e7143SRobert Watson 				 */
672ec95b709SMikolaj Golub 				tw = intotw(t);
673ae0e7143SRobert Watson 				if (tw == NULL ||
674ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
675340c35deSJonathan Lemon 					return (EADDRINUSE);
6760a100a6fSAdrian Chadd 			} else if (t &&
6770a100a6fSAdrian Chadd 			    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
6780a100a6fSAdrian Chadd 			    (reuseport & inp_so_options(t)) == 0) {
679e3fd5ffdSRobert Watson #ifdef INET6
68033841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
681cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
682cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
683cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
684fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
685fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
686e3fd5ffdSRobert Watson #endif
687df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
6880a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6890a100a6fSAdrian Chadd 					return (EADDRINUSE);
690df8bae1dSRodney W. Grimes 			}
691cfa1ca9dSYoshinobu Inoue 		}
692df8bae1dSRodney W. Grimes 	}
6934b932371SIan Dowse 	if (*lportp != 0)
6944b932371SIan Dowse 		lport = *lportp;
69533b3ac06SPeter Wemm 	if (lport == 0) {
69668e0d7e0SRobert Watson 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
697efc76f72SBjoern A. Zeeb 		if (error != 0)
698efc76f72SBjoern A. Zeeb 			return (error);
69933b3ac06SPeter Wemm 
70033b3ac06SPeter Wemm 	}
7014b932371SIan Dowse 	*laddrp = laddr.s_addr;
7024b932371SIan Dowse 	*lportp = lport;
703df8bae1dSRodney W. Grimes 	return (0);
704df8bae1dSRodney W. Grimes }
705df8bae1dSRodney W. Grimes 
706999f1343SGarrett Wollman /*
7075200e00eSIan Dowse  * Connect from a socket to a specified address.
7085200e00eSIan Dowse  * Both address and port must be specified in argument sin.
7095200e00eSIan Dowse  * If don't have a local address for this socket yet,
7105200e00eSIan Dowse  * then pick one.
711999f1343SGarrett Wollman  */
712999f1343SGarrett Wollman int
713d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
714d3c1f003SRobert Watson     struct ucred *cred, struct mbuf *m)
715999f1343SGarrett Wollman {
7165200e00eSIan Dowse 	u_short lport, fport;
7175200e00eSIan Dowse 	in_addr_t laddr, faddr;
7185200e00eSIan Dowse 	int anonport, error;
719df8bae1dSRodney W. Grimes 
7208501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
721fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
72227f74fd0SRobert Watson 
7235200e00eSIan Dowse 	lport = inp->inp_lport;
7245200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
7255200e00eSIan Dowse 	anonport = (lport == 0);
7265200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
727b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
7285200e00eSIan Dowse 	if (error)
7295200e00eSIan Dowse 		return (error);
7305200e00eSIan Dowse 
7315200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
7325200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
7335200e00eSIan Dowse 		inp->inp_lport = lport;
7345200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
7355200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
7365200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
7375200e00eSIan Dowse 			inp->inp_lport = 0;
7385200e00eSIan Dowse 			return (EAGAIN);
7395200e00eSIan Dowse 		}
7405200e00eSIan Dowse 	}
7415200e00eSIan Dowse 
7425200e00eSIan Dowse 	/* Commit the remaining changes. */
7435200e00eSIan Dowse 	inp->inp_lport = lport;
7445200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
7455200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
7465200e00eSIan Dowse 	inp->inp_fport = fport;
747d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, m);
7482cb64cb2SGeorge V. Neville-Neil 
7495200e00eSIan Dowse 	if (anonport)
7505200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
7515200e00eSIan Dowse 	return (0);
7525200e00eSIan Dowse }
7535200e00eSIan Dowse 
754d3c1f003SRobert Watson int
755d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
756d3c1f003SRobert Watson {
757d3c1f003SRobert Watson 
758d3c1f003SRobert Watson 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
759d3c1f003SRobert Watson }
760d3c1f003SRobert Watson 
7615200e00eSIan Dowse /*
7620895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
7630895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
7640895aec3SBjoern A. Zeeb  */
765ae190832SSteven Hartland int
7660895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
7670895aec3SBjoern A. Zeeb     struct ucred *cred)
7680895aec3SBjoern A. Zeeb {
7690895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
7700895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
7710895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
7720895aec3SBjoern A. Zeeb 	struct route sro;
7730895aec3SBjoern A. Zeeb 	int error;
7740895aec3SBjoern A. Zeeb 
775413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
7760895aec3SBjoern A. Zeeb 
777592bcae8SBjoern A. Zeeb 	/*
778592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
779592bcae8SBjoern A. Zeeb 	 * if requested.
780592bcae8SBjoern A. Zeeb 	 */
781592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
782592bcae8SBjoern A. Zeeb 		return (0);
783592bcae8SBjoern A. Zeeb 
7840895aec3SBjoern A. Zeeb 	error = 0;
7850895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
7860895aec3SBjoern A. Zeeb 
7870895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
7880895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
7890895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
7900895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
7910895aec3SBjoern A. Zeeb 
7920895aec3SBjoern A. Zeeb 	/*
7930895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
7940895aec3SBjoern A. Zeeb 	 * else punt.
7950895aec3SBjoern A. Zeeb 	 *
7960895aec3SBjoern A. Zeeb 	 * Find out route to destination.
7970895aec3SBjoern A. Zeeb 	 */
7980895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
7996e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
8000895aec3SBjoern A. Zeeb 
8010895aec3SBjoern A. Zeeb 	/*
8020895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
8030895aec3SBjoern A. Zeeb 	 * the outgoing interface.
8040895aec3SBjoern A. Zeeb 	 *
8050895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
8060895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
8070895aec3SBjoern A. Zeeb 	 * the source address from.
8080895aec3SBjoern A. Zeeb 	 */
8090895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
8108c0fec80SRobert Watson 		struct in_ifaddr *ia;
8110895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
8120895aec3SBjoern A. Zeeb 
8134f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
81458a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
8150895aec3SBjoern A. Zeeb 		if (ia == NULL)
8164f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
81758a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
8180895aec3SBjoern A. Zeeb 		if (ia == NULL) {
8190895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
8200895aec3SBjoern A. Zeeb 			goto done;
8210895aec3SBjoern A. Zeeb 		}
8220895aec3SBjoern A. Zeeb 
8230304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8240895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8258c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8260895aec3SBjoern A. Zeeb 			goto done;
8270895aec3SBjoern A. Zeeb 		}
8280895aec3SBjoern A. Zeeb 
8290895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
8308c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
8310895aec3SBjoern A. Zeeb 		ia = NULL;
832137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8330895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8340895aec3SBjoern A. Zeeb 
8350895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8360895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8370895aec3SBjoern A. Zeeb 				continue;
8380895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
839b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8400895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8410895aec3SBjoern A. Zeeb 				break;
8420895aec3SBjoern A. Zeeb 			}
8430895aec3SBjoern A. Zeeb 		}
8440895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8450895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
846137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8470895aec3SBjoern A. Zeeb 			goto done;
8480895aec3SBjoern A. Zeeb 		}
849137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8500895aec3SBjoern A. Zeeb 
8510895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
852b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8530895aec3SBjoern A. Zeeb 		goto done;
8540895aec3SBjoern A. Zeeb 	}
8550895aec3SBjoern A. Zeeb 
8560895aec3SBjoern A. Zeeb 	/*
8570895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
8580895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
8590895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
8600895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
8610895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
8620895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
8630895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
8640895aec3SBjoern A. Zeeb 	 */
8650895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
8668c0fec80SRobert Watson 		struct in_ifaddr *ia;
8679317b04eSRobert Watson 		struct ifnet *ifp;
8680895aec3SBjoern A. Zeeb 
8690895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
8700304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8710895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8720895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8730895aec3SBjoern A. Zeeb 			goto done;
8740895aec3SBjoern A. Zeeb 		}
8750895aec3SBjoern A. Zeeb 
8760895aec3SBjoern A. Zeeb 		/* Jailed. */
8770895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
8780895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
879b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8800895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8810895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8820895aec3SBjoern A. Zeeb 			goto done;
8830895aec3SBjoern A. Zeeb 		}
8840895aec3SBjoern A. Zeeb 
8850895aec3SBjoern A. Zeeb 		/*
8860895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
8870895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
8880895aec3SBjoern A. Zeeb 		 */
8898c0fec80SRobert Watson 		ia = NULL;
8909317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
891137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8929317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8930895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8940895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8950895aec3SBjoern A. Zeeb 				continue;
8960895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
897b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8980895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8990895aec3SBjoern A. Zeeb 				break;
9000895aec3SBjoern A. Zeeb 			}
9010895aec3SBjoern A. Zeeb 		}
9020895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9030895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
904137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9050895aec3SBjoern A. Zeeb 			goto done;
9060895aec3SBjoern A. Zeeb 		}
907137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
9080895aec3SBjoern A. Zeeb 
9090895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
910b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9110895aec3SBjoern A. Zeeb 		goto done;
9120895aec3SBjoern A. Zeeb 	}
9130895aec3SBjoern A. Zeeb 
9140895aec3SBjoern A. Zeeb 	/*
9150895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
9160895aec3SBjoern A. Zeeb 	 * to ourselves is here.
9170895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
9180895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
9190895aec3SBjoern A. Zeeb 	 * a loopback interface.
9200895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
9210895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
9220895aec3SBjoern A. Zeeb 	 */
9230895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
9240895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
9258c0fec80SRobert Watson 		struct in_ifaddr *ia;
9260895aec3SBjoern A. Zeeb 
9270895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
9280895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
9290895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
9300895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
9310895aec3SBjoern A. Zeeb 
93258a39d8cSAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
93358a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
9340895aec3SBjoern A. Zeeb 		if (ia == NULL)
9354f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
93658a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
937f0bb05fcSQing Li 		if (ia == NULL)
938f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
9390895aec3SBjoern A. Zeeb 
9400304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
9410895aec3SBjoern A. Zeeb 			if (ia == NULL) {
9420895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
9430895aec3SBjoern A. Zeeb 				goto done;
9440895aec3SBjoern A. Zeeb 			}
9450895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
9468c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9470895aec3SBjoern A. Zeeb 			goto done;
9480895aec3SBjoern A. Zeeb 		}
9490895aec3SBjoern A. Zeeb 
9500895aec3SBjoern A. Zeeb 		/* Jailed. */
9510895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9520895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
9530895aec3SBjoern A. Zeeb 
9540895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
9558c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9560895aec3SBjoern A. Zeeb 			ia = NULL;
957137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
9580895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9590895aec3SBjoern A. Zeeb 
9600895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
9610895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
9620895aec3SBjoern A. Zeeb 					continue;
9630895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
964b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
965b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
9660895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
9670895aec3SBjoern A. Zeeb 					break;
9680895aec3SBjoern A. Zeeb 				}
9690895aec3SBjoern A. Zeeb 			}
9700895aec3SBjoern A. Zeeb 			if (ia != NULL) {
9710895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
972137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
9730895aec3SBjoern A. Zeeb 				goto done;
9740895aec3SBjoern A. Zeeb 			}
975137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9760895aec3SBjoern A. Zeeb 		}
9770895aec3SBjoern A. Zeeb 
9780895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
979b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9800895aec3SBjoern A. Zeeb 		goto done;
9810895aec3SBjoern A. Zeeb 	}
9820895aec3SBjoern A. Zeeb 
9830895aec3SBjoern A. Zeeb done:
9840895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
9850895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
9860895aec3SBjoern A. Zeeb 	return (error);
9870895aec3SBjoern A. Zeeb }
9880895aec3SBjoern A. Zeeb 
9890895aec3SBjoern A. Zeeb /*
9905200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
9915200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
9925200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
9935200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
9945200e00eSIan Dowse  * the connect.
9955200e00eSIan Dowse  *
9965200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
9975200e00eSIan Dowse  * and port. These are not updated in the error case.
9985200e00eSIan Dowse  *
9995200e00eSIan Dowse  * If the operation fails because the connection already exists,
10005200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
10015200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
10025200e00eSIan Dowse  * is set to NULL.
10035200e00eSIan Dowse  */
10045200e00eSIan Dowse int
1005136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1006136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1007136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
10085200e00eSIan Dowse {
1009cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
10105200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
10115200e00eSIan Dowse 	struct in_ifaddr *ia;
10125200e00eSIan Dowse 	struct inpcb *oinp;
1013b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
10145200e00eSIan Dowse 	u_short lport, fport;
10155200e00eSIan Dowse 	int error;
10165200e00eSIan Dowse 
10178501a69cSRobert Watson 	/*
10188501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
10198501a69cSRobert Watson 	 * lock is sufficient.
10208501a69cSRobert Watson 	 */
102127f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
1022fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
102327f74fd0SRobert Watson 
10245200e00eSIan Dowse 	if (oinpp != NULL)
10255200e00eSIan Dowse 		*oinpp = NULL;
102657bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
1027df8bae1dSRodney W. Grimes 		return (EINVAL);
1028df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
1029df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
1030df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1031df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
10325200e00eSIan Dowse 	laddr.s_addr = *laddrp;
10335200e00eSIan Dowse 	lport = *lportp;
10345200e00eSIan Dowse 	faddr = sin->sin_addr;
10355200e00eSIan Dowse 	fport = sin->sin_port;
10360895aec3SBjoern A. Zeeb 
1037603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1038df8bae1dSRodney W. Grimes 		/*
1039df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1040df8bae1dSRodney W. Grimes 		 * use the primary local address.
1041df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1042df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1043df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1044df8bae1dSRodney W. Grimes 		 */
1045413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
1046cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
1047413628a7SBjoern A. Zeeb 			faddr =
1048b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1049cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1050b89e82ddSJamie Gritton 			if (cred != NULL &&
1051b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1052b89e82ddSJamie Gritton 				return (error);
10532d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1054cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
10552d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
10562d9cfabaSRobert Watson 			    IFF_BROADCAST)
10575200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
1058603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1059cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
10602d9cfabaSRobert Watson 		}
1061df8bae1dSRodney W. Grimes 	}
10625200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1063d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1064df8bae1dSRodney W. Grimes 		/*
1065df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1066d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1067df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1068df8bae1dSRodney W. Grimes 		 */
10695200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1070df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1071df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1072df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1073df8bae1dSRodney W. Grimes 
1074df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1075df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1076df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
1077cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RLOCK(&in_ifa_tracker);
1078e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1079e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1080e691be70SDaniel Eischen 					    (cred == NULL ||
1081e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1082e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1083df8bae1dSRodney W. Grimes 						break;
1084e691be70SDaniel Eischen 				}
1085e691be70SDaniel Eischen 				if (ia == NULL)
1086d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1087e691be70SDaniel Eischen 				else {
10885200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1089d79fdd98SDaniel Eischen 					error = 0;
1090999f1343SGarrett Wollman 				}
1091cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1092d79fdd98SDaniel Eischen 			}
1093d79fdd98SDaniel Eischen 		}
109404215ed2SRandall Stewart 		if (error)
109504215ed2SRandall Stewart 			return (error);
10960895aec3SBjoern A. Zeeb 	}
1097fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1098fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
10995200e00eSIan Dowse 	if (oinp != NULL) {
11005200e00eSIan Dowse 		if (oinpp != NULL)
11015200e00eSIan Dowse 			*oinpp = oinp;
1102df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1103c3229e05SDavid Greenman 	}
11045200e00eSIan Dowse 	if (lport == 0) {
1105b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1106b0330ed9SPawel Jakub Dawidek 		    cred);
11075a903f8dSPierre Beyssac 		if (error)
11085a903f8dSPierre Beyssac 			return (error);
11095a903f8dSPierre Beyssac 	}
11105200e00eSIan Dowse 	*laddrp = laddr.s_addr;
11115200e00eSIan Dowse 	*lportp = lport;
11125200e00eSIan Dowse 	*faddrp = faddr.s_addr;
11135200e00eSIan Dowse 	*fportp = fport;
1114df8bae1dSRodney W. Grimes 	return (0);
1115df8bae1dSRodney W. Grimes }
1116df8bae1dSRodney W. Grimes 
111726f9a767SRodney W. Grimes void
1118136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1119df8bae1dSRodney W. Grimes {
11206b348152SRobert Watson 
11218501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1122fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1123df8bae1dSRodney W. Grimes 
1124df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1125df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
112615bd2b43SDavid Greenman 	in_pcbrehash(inp);
1127df8bae1dSRodney W. Grimes }
112883e521ecSBjoern A. Zeeb #endif /* INET */
1129df8bae1dSRodney W. Grimes 
11304c7c478dSRobert Watson /*
113128696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1132c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
113328696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
113428696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
11354c7c478dSRobert Watson  */
113626f9a767SRodney W. Grimes void
1137136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1138df8bae1dSRodney W. Grimes {
11394c7c478dSRobert Watson 
1140a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1141c0a211c5SRobert Watson 
11424c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
11434c7c478dSRobert Watson 	inp->inp_socket = NULL;
11444c7c478dSRobert Watson }
11454c7c478dSRobert Watson 
1146c0a211c5SRobert Watson /*
114779bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
114879bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
114979bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
115052cd27cbSRobert Watson  * but where the inpcb lock may already held, or when acquiring a reference
115152cd27cbSRobert Watson  * via a pcbgroup.
115279bdc6e5SRobert Watson  *
115379bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
115479bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
115579bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
115679bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
115779bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
115879bdc6e5SRobert Watson  * inpcb.
115979bdc6e5SRobert Watson  *
116079bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
116179bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
116279bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
116379bdc6e5SRobert Watson  * using in_pcbrele().
1164c0a211c5SRobert Watson  */
116579bdc6e5SRobert Watson void
116679bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
11674c7c478dSRobert Watson {
1168df8bae1dSRodney W. Grimes 
116979bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
117079bdc6e5SRobert Watson 
117179bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
117279bdc6e5SRobert Watson }
117379bdc6e5SRobert Watson 
117479bdc6e5SRobert Watson /*
117579bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
117679bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
117779bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
117879bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
117979bdc6e5SRobert Watson  *
118079bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
118179bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
118279bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
118379bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
118479bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
118579bdc6e5SRobert Watson  */
118679bdc6e5SRobert Watson int
118779bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
118879bdc6e5SRobert Watson {
118979bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
119079bdc6e5SRobert Watson 
119179bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
119279bdc6e5SRobert Watson 
119379bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
119479bdc6e5SRobert Watson 
1195df4e91d3SGleb Smirnoff 	if (refcount_release(&inp->inp_refcount) == 0) {
1196df4e91d3SGleb Smirnoff 		/*
1197df4e91d3SGleb Smirnoff 		 * If the inpcb has been freed, let the caller know, even if
1198df4e91d3SGleb Smirnoff 		 * this isn't the last reference.
1199df4e91d3SGleb Smirnoff 		 */
1200df4e91d3SGleb Smirnoff 		if (inp->inp_flags2 & INP_FREED) {
1201df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1202df4e91d3SGleb Smirnoff 			return (1);
1203df4e91d3SGleb Smirnoff 		}
120479bdc6e5SRobert Watson 		return (0);
1205df4e91d3SGleb Smirnoff 	}
120679bdc6e5SRobert Watson 
120779bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
120879bdc6e5SRobert Watson 
120979bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
121079bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
121179bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
121279bdc6e5SRobert Watson 	return (1);
121379bdc6e5SRobert Watson }
121479bdc6e5SRobert Watson 
121579bdc6e5SRobert Watson int
121679bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
121779bdc6e5SRobert Watson {
121879bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
121979bdc6e5SRobert Watson 
122079bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
122179bdc6e5SRobert Watson 
122279bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
122379bdc6e5SRobert Watson 
1224edd0e0b0SFabien Thomas 	if (refcount_release(&inp->inp_refcount) == 0) {
1225edd0e0b0SFabien Thomas 		/*
1226edd0e0b0SFabien Thomas 		 * If the inpcb has been freed, let the caller know, even if
1227edd0e0b0SFabien Thomas 		 * this isn't the last reference.
1228edd0e0b0SFabien Thomas 		 */
1229edd0e0b0SFabien Thomas 		if (inp->inp_flags2 & INP_FREED) {
1230edd0e0b0SFabien Thomas 			INP_WUNLOCK(inp);
1231edd0e0b0SFabien Thomas 			return (1);
1232edd0e0b0SFabien Thomas 		}
123379bdc6e5SRobert Watson 		return (0);
1234edd0e0b0SFabien Thomas 	}
123579bdc6e5SRobert Watson 
123679bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
123779bdc6e5SRobert Watson 
123879bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
123979bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
124079bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
124179bdc6e5SRobert Watson 	return (1);
124279bdc6e5SRobert Watson }
124379bdc6e5SRobert Watson 
124479bdc6e5SRobert Watson /*
124579bdc6e5SRobert Watson  * Temporary wrapper.
124679bdc6e5SRobert Watson  */
124779bdc6e5SRobert Watson int
124879bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
124979bdc6e5SRobert Watson {
125079bdc6e5SRobert Watson 
125179bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
125279bdc6e5SRobert Watson }
125379bdc6e5SRobert Watson 
125479bdc6e5SRobert Watson /*
125579bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
125679bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
125779bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
125879bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
125979bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
126079bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
126179bdc6e5SRobert Watson  * the pcbinfo lock is held.
126279bdc6e5SRobert Watson  */
126379bdc6e5SRobert Watson void
126479bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
126579bdc6e5SRobert Watson {
126679bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
126779bdc6e5SRobert Watson 
126879bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
126979bdc6e5SRobert Watson 
1270ff9b006dSJulien Charbon #ifdef INVARIANTS
1271ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
1272079672cbSJulien Charbon 		INP_INFO_LOCK_ASSERT(pcbinfo);
1273ff9b006dSJulien Charbon 	} else {
127479bdc6e5SRobert Watson 		INP_INFO_WLOCK_ASSERT(pcbinfo);
1275ff9b006dSJulien Charbon 	}
1276ff9b006dSJulien Charbon #endif
127779bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
127879bdc6e5SRobert Watson 
127979bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1280b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
12816aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
12826974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
128383e521ecSBjoern A. Zeeb #endif
1284ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
128579bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1286c3229e05SDavid Greenman 	in_pcbremlists(inp);
1287ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
12886aee2fc5SBjoern A. Zeeb #ifdef INET6
12896aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
12906aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
12911096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
12926aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
12936aee2fc5SBjoern A. Zeeb 	}
12946aee2fc5SBjoern A. Zeeb #endif
1295df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1296df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
129767107f45SBjoern A. Zeeb #ifdef INET
129871498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
129971498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
130067107f45SBjoern A. Zeeb #endif
1301*84cc0778SGeorge V. Neville-Neil 	if (inp->inp_route.ro_rt) {
1302*84cc0778SGeorge V. Neville-Neil 		RTFREE(inp->inp_route.ro_rt);
1303*84cc0778SGeorge V. Neville-Neil 		inp->inp_route.ro_rt = (struct rtentry *)NULL;
1304*84cc0778SGeorge V. Neville-Neil 	}
1305*84cc0778SGeorge V. Neville-Neil 
1306cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1307df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
130886d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1309a557af22SRobert Watson #ifdef MAC
131030d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1311a557af22SRobert Watson #endif
131279bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
131328696211SRobert Watson 		INP_WUNLOCK(inp);
131428696211SRobert Watson }
131528696211SRobert Watson 
131628696211SRobert Watson /*
1317c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1318c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1319c0a211c5SRobert Watson  *
1320c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1321c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1322c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1323c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1324c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1325c0a211c5SRobert Watson  * in_pcbdetach().
1326c0a211c5SRobert Watson  *
1327c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1328c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
132910702a28SRobert Watson  */
133010702a28SRobert Watson void
133110702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
133210702a28SRobert Watson {
133310702a28SRobert Watson 
13348501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
133510702a28SRobert Watson 
1336fa046d87SRobert Watson 	/*
1337fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1338fa046d87SRobert Watson 	 * the hash lock...?
1339fa046d87SRobert Watson 	 */
1340ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1341111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
134210702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
134310702a28SRobert Watson 
1344fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
134510702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
134610702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
134710702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
134810702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
134910702a28SRobert Watson 			free(phd, M_PCB);
135010702a28SRobert Watson 		}
1351fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1352111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
135352cd27cbSRobert Watson #ifdef PCBGROUP
135452cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
135552cd27cbSRobert Watson #endif
135610702a28SRobert Watson 	}
135710702a28SRobert Watson }
135810702a28SRobert Watson 
135967107f45SBjoern A. Zeeb #ifdef INET
136054d642bbSRobert Watson /*
136154d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
136254d642bbSRobert Watson  */
136326ef6ac4SDon Lewis struct sockaddr *
1364136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
136526ef6ac4SDon Lewis {
136626ef6ac4SDon Lewis 	struct sockaddr_in *sin;
136726ef6ac4SDon Lewis 
13681ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1369a163d034SWarner Losh 		M_WAITOK | M_ZERO);
137026ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
137126ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
137226ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
137326ef6ac4SDon Lewis 	sin->sin_port = port;
137426ef6ac4SDon Lewis 
137526ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
137626ef6ac4SDon Lewis }
137726ef6ac4SDon Lewis 
1378117bcae7SGarrett Wollman int
137954d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1380df8bae1dSRodney W. Grimes {
1381136d4f1cSRobert Watson 	struct inpcb *inp;
138226ef6ac4SDon Lewis 	struct in_addr addr;
138326ef6ac4SDon Lewis 	in_port_t port;
138442fa505bSDavid Greenman 
1385fdc984f7STor Egge 	inp = sotoinpcb(so);
138654d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
13876466b28aSRobert Watson 
1388a69042a5SRobert Watson 	INP_RLOCK(inp);
138926ef6ac4SDon Lewis 	port = inp->inp_lport;
139026ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1391a69042a5SRobert Watson 	INP_RUNLOCK(inp);
139242fa505bSDavid Greenman 
139326ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1394117bcae7SGarrett Wollman 	return 0;
1395df8bae1dSRodney W. Grimes }
1396df8bae1dSRodney W. Grimes 
1397117bcae7SGarrett Wollman int
139854d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1399df8bae1dSRodney W. Grimes {
1400136d4f1cSRobert Watson 	struct inpcb *inp;
140126ef6ac4SDon Lewis 	struct in_addr addr;
140226ef6ac4SDon Lewis 	in_port_t port;
140342fa505bSDavid Greenman 
1404fdc984f7STor Egge 	inp = sotoinpcb(so);
140554d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
14066466b28aSRobert Watson 
1407a69042a5SRobert Watson 	INP_RLOCK(inp);
140826ef6ac4SDon Lewis 	port = inp->inp_fport;
140926ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1410a69042a5SRobert Watson 	INP_RUNLOCK(inp);
141142fa505bSDavid Greenman 
141226ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1413117bcae7SGarrett Wollman 	return 0;
1414df8bae1dSRodney W. Grimes }
1415df8bae1dSRodney W. Grimes 
141626f9a767SRodney W. Grimes void
1417136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1418136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1419d1c54148SJesper Skriver {
1420f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1421d1c54148SJesper Skriver 
14223dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1423f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
14248501a69cSRobert Watson 		INP_WLOCK(inp);
1425d1c54148SJesper Skriver #ifdef INET6
1426f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
14278501a69cSRobert Watson 			INP_WUNLOCK(inp);
1428d1c54148SJesper Skriver 			continue;
1429f76fcf6dSJeffrey Hsu 		}
1430d1c54148SJesper Skriver #endif
1431d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1432f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
14338501a69cSRobert Watson 			INP_WUNLOCK(inp);
1434d1c54148SJesper Skriver 			continue;
1435d1c54148SJesper Skriver 		}
14363dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
14378501a69cSRobert Watson 			INP_WUNLOCK(inp);
1438f76fcf6dSJeffrey Hsu 	}
14393dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1440d1c54148SJesper Skriver }
1441d1c54148SJesper Skriver 
1442e43cc4aeSHajimu UMEMOTO void
1443136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1444e43cc4aeSHajimu UMEMOTO {
1445e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1446e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1447e43cc4aeSHajimu UMEMOTO 	int i, gap;
1448e43cc4aeSHajimu UMEMOTO 
1449ff9b006dSJulien Charbon 	INP_INFO_WLOCK(pcbinfo);
1450712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
14518501a69cSRobert Watson 		INP_WLOCK(inp);
1452e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1453e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1454e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1455e43cc4aeSHajimu UMEMOTO 			/*
1456e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1457e43cc4aeSHajimu UMEMOTO 			 * detached.
1458e43cc4aeSHajimu UMEMOTO 			 */
1459e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1460e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1461e43cc4aeSHajimu UMEMOTO 
1462e43cc4aeSHajimu UMEMOTO 			/*
1463e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1464e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1465e43cc4aeSHajimu UMEMOTO 			 */
1466e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1467e43cc4aeSHajimu UMEMOTO 			    i++) {
1468e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1469e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1470e43cc4aeSHajimu UMEMOTO 					gap++;
1471e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1472e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1473e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1474e43cc4aeSHajimu UMEMOTO 			}
1475e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1476e43cc4aeSHajimu UMEMOTO 		}
14778501a69cSRobert Watson 		INP_WUNLOCK(inp);
1478e43cc4aeSHajimu UMEMOTO 	}
1479ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(pcbinfo);
1480e43cc4aeSHajimu UMEMOTO }
1481e43cc4aeSHajimu UMEMOTO 
1482df8bae1dSRodney W. Grimes /*
1483fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1484fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1485c3229e05SDavid Greenman  */
1486d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1487df8bae1dSRodney W. Grimes struct inpcb *
1488136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
148968e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1490df8bae1dSRodney W. Grimes {
1491136d4f1cSRobert Watson 	struct inpcb *inp;
1492d5e8a67eSHajimu UMEMOTO #ifdef INET6
1493d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1494d5e8a67eSHajimu UMEMOTO #else
1495d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1496d5e8a67eSHajimu UMEMOTO #endif
1497d5e8a67eSHajimu UMEMOTO 	int wildcard;
14987bc4aca7SDavid Greenman 
149968e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
150068e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
150168e0d7e0SRobert Watson 
1502fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
15031b73ca0bSSam Leffler 
150468e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1505c3229e05SDavid Greenman 		struct inpcbhead *head;
1506c3229e05SDavid Greenman 		/*
1507c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1508c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1509c3229e05SDavid Greenman 		 */
1510712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1511712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1512fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1513cfa1ca9dSYoshinobu Inoue #ifdef INET6
1514413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1515369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1516cfa1ca9dSYoshinobu Inoue 				continue;
1517cfa1ca9dSYoshinobu Inoue #endif
1518c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1519c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1520c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1521c3229e05SDavid Greenman 				/*
1522413628a7SBjoern A. Zeeb 				 * Found?
1523c3229e05SDavid Greenman 				 */
1524413628a7SBjoern A. Zeeb 				if (cred == NULL ||
15250304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
15260304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1527c3229e05SDavid Greenman 					return (inp);
1528df8bae1dSRodney W. Grimes 			}
1529c3229e05SDavid Greenman 		}
1530c3229e05SDavid Greenman 		/*
1531c3229e05SDavid Greenman 		 * Not found.
1532c3229e05SDavid Greenman 		 */
1533c3229e05SDavid Greenman 		return (NULL);
1534c3229e05SDavid Greenman 	} else {
1535c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1536c3229e05SDavid Greenman 		struct inpcbport *phd;
1537c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1538c3229e05SDavid Greenman 		/*
1539c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1540c3229e05SDavid Greenman 		 *
1541c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1542c3229e05SDavid Greenman 		 * port hash list.
1543c3229e05SDavid Greenman 		 */
1544712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1545712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1546fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1547c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1548c3229e05SDavid Greenman 				break;
1549c3229e05SDavid Greenman 		}
1550c3229e05SDavid Greenman 		if (phd != NULL) {
1551c3229e05SDavid Greenman 			/*
1552c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1553c3229e05SDavid Greenman 			 * fit.
1554c3229e05SDavid Greenman 			 */
155537d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1556c3229e05SDavid Greenman 				wildcard = 0;
1557413628a7SBjoern A. Zeeb 				if (cred != NULL &&
15580304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
15590304c731SJamie Gritton 					cred->cr_prison))
1560413628a7SBjoern A. Zeeb 					continue;
1561cfa1ca9dSYoshinobu Inoue #ifdef INET6
1562413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1563369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1564cfa1ca9dSYoshinobu Inoue 					continue;
1565d5e8a67eSHajimu UMEMOTO 				/*
1566d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1567d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1568d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1569d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1570d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1571d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1572d5e8a67eSHajimu UMEMOTO 				 *
1573d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1574d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1575d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1576d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1577d5e8a67eSHajimu UMEMOTO 				 */
1578d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1579d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1580cfa1ca9dSYoshinobu Inoue #endif
1581c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1582c3229e05SDavid Greenman 					wildcard++;
158315bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
158415bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
158515bd2b43SDavid Greenman 						wildcard++;
158615bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
158715bd2b43SDavid Greenman 						continue;
158815bd2b43SDavid Greenman 				} else {
158915bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
159015bd2b43SDavid Greenman 						wildcard++;
159115bd2b43SDavid Greenman 				}
1592df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1593df8bae1dSRodney W. Grimes 					match = inp;
1594df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1595413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1596df8bae1dSRodney W. Grimes 						break;
1597df8bae1dSRodney W. Grimes 				}
1598df8bae1dSRodney W. Grimes 			}
15993dbdc25cSDavid Greenman 		}
1600df8bae1dSRodney W. Grimes 		return (match);
1601df8bae1dSRodney W. Grimes 	}
1602c3229e05SDavid Greenman }
1603d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
160415bd2b43SDavid Greenman 
160552cd27cbSRobert Watson #ifdef PCBGROUP
160652cd27cbSRobert Watson /*
160752cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
160852cd27cbSRobert Watson  */
160952cd27cbSRobert Watson static struct inpcb *
161052cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
161152cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
161252cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
161352cd27cbSRobert Watson {
161452cd27cbSRobert Watson 	struct inpcbhead *head;
161552cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
161652cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
161752cd27cbSRobert Watson 
161852cd27cbSRobert Watson 	/*
161952cd27cbSRobert Watson 	 * First look for an exact match.
162052cd27cbSRobert Watson 	 */
162152cd27cbSRobert Watson 	tmpinp = NULL;
162252cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
162352cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
162452cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
162552cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
162652cd27cbSRobert Watson #ifdef INET6
162752cd27cbSRobert Watson 		/* XXX inp locking */
162852cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
162952cd27cbSRobert Watson 			continue;
163052cd27cbSRobert Watson #endif
163152cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
163252cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
163352cd27cbSRobert Watson 		    inp->inp_fport == fport &&
163452cd27cbSRobert Watson 		    inp->inp_lport == lport) {
163552cd27cbSRobert Watson 			/*
163652cd27cbSRobert Watson 			 * XXX We should be able to directly return
163752cd27cbSRobert Watson 			 * the inp here, without any checks.
163852cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
163952cd27cbSRobert Watson 			 */
164052cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
164152cd27cbSRobert Watson 				goto found;
164252cd27cbSRobert Watson 			if (tmpinp == NULL)
164352cd27cbSRobert Watson 				tmpinp = inp;
164452cd27cbSRobert Watson 		}
164552cd27cbSRobert Watson 	}
164652cd27cbSRobert Watson 	if (tmpinp != NULL) {
164752cd27cbSRobert Watson 		inp = tmpinp;
164852cd27cbSRobert Watson 		goto found;
164952cd27cbSRobert Watson 	}
165052cd27cbSRobert Watson 
16510a100a6fSAdrian Chadd #ifdef	RSS
16520a100a6fSAdrian Chadd 	/*
16530a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
16540a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
16550a100a6fSAdrian Chadd 	 */
16560a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
16570a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
16580a100a6fSAdrian Chadd #ifdef INET6
16590a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
16600a100a6fSAdrian Chadd #endif
16610a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
16620a100a6fSAdrian Chadd 		struct inpcbhead *head;
16630a100a6fSAdrian Chadd 		int injail;
16640a100a6fSAdrian Chadd 
16650a100a6fSAdrian Chadd 		/*
16660a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
16670a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
16680a100a6fSAdrian Chadd 		 *      2. jailed, wild.
16690a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
16700a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
16710a100a6fSAdrian Chadd 		 */
16720a100a6fSAdrian Chadd 
16730a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
16740a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
16750a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
16760a100a6fSAdrian Chadd #ifdef INET6
16770a100a6fSAdrian Chadd 			/* XXX inp locking */
16780a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
16790a100a6fSAdrian Chadd 				continue;
16800a100a6fSAdrian Chadd #endif
16810a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
16820a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
16830a100a6fSAdrian Chadd 				continue;
16840a100a6fSAdrian Chadd 
16850a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
16860a100a6fSAdrian Chadd 			if (injail) {
16870a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
16880a100a6fSAdrian Chadd 				    &laddr) != 0)
16890a100a6fSAdrian Chadd 					continue;
16900a100a6fSAdrian Chadd 			} else {
16910a100a6fSAdrian Chadd 				if (local_exact != NULL)
16920a100a6fSAdrian Chadd 					continue;
16930a100a6fSAdrian Chadd 			}
16940a100a6fSAdrian Chadd 
16950a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
16960a100a6fSAdrian Chadd 				if (injail)
16970a100a6fSAdrian Chadd 					goto found;
16980a100a6fSAdrian Chadd 				else
16990a100a6fSAdrian Chadd 					local_exact = inp;
17000a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
17010a100a6fSAdrian Chadd #ifdef INET6
17020a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
17030a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
17040a100a6fSAdrian Chadd 					local_wild_mapped = inp;
17050a100a6fSAdrian Chadd 				else
17060a100a6fSAdrian Chadd #endif
17070a100a6fSAdrian Chadd 					if (injail)
17080a100a6fSAdrian Chadd 						jail_wild = inp;
17090a100a6fSAdrian Chadd 					else
17100a100a6fSAdrian Chadd 						local_wild = inp;
17110a100a6fSAdrian Chadd 			}
17120a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
17130a100a6fSAdrian Chadd 
17140a100a6fSAdrian Chadd 		inp = jail_wild;
17150a100a6fSAdrian Chadd 		if (inp == NULL)
17160a100a6fSAdrian Chadd 			inp = local_exact;
17170a100a6fSAdrian Chadd 		if (inp == NULL)
17180a100a6fSAdrian Chadd 			inp = local_wild;
17190a100a6fSAdrian Chadd #ifdef INET6
17200a100a6fSAdrian Chadd 		if (inp == NULL)
17210a100a6fSAdrian Chadd 			inp = local_wild_mapped;
17220a100a6fSAdrian Chadd #endif
17230a100a6fSAdrian Chadd 		if (inp != NULL)
17240a100a6fSAdrian Chadd 			goto found;
17250a100a6fSAdrian Chadd 	}
17260a100a6fSAdrian Chadd #endif
17270a100a6fSAdrian Chadd 
172852cd27cbSRobert Watson 	/*
172952cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
173052cd27cbSRobert Watson 	 */
173152cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
173252cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
173352cd27cbSRobert Watson #ifdef INET6
173452cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
173552cd27cbSRobert Watson #endif
173652cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
173752cd27cbSRobert Watson 		struct inpcbhead *head;
173852cd27cbSRobert Watson 		int injail;
173952cd27cbSRobert Watson 
174052cd27cbSRobert Watson 		/*
174152cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
174252cd27cbSRobert Watson 		 *      1. jailed, non-wild.
174352cd27cbSRobert Watson 		 *      2. jailed, wild.
174452cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
174552cd27cbSRobert Watson 		 *      4. non-jailed, wild.
174652cd27cbSRobert Watson 		 */
174752cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
174852cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
174952cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
175052cd27cbSRobert Watson #ifdef INET6
175152cd27cbSRobert Watson 			/* XXX inp locking */
175252cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
175352cd27cbSRobert Watson 				continue;
175452cd27cbSRobert Watson #endif
175552cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
175652cd27cbSRobert Watson 			    inp->inp_lport != lport)
175752cd27cbSRobert Watson 				continue;
175852cd27cbSRobert Watson 
175952cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
176052cd27cbSRobert Watson 			if (injail) {
176152cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
176252cd27cbSRobert Watson 				    &laddr) != 0)
176352cd27cbSRobert Watson 					continue;
176452cd27cbSRobert Watson 			} else {
176552cd27cbSRobert Watson 				if (local_exact != NULL)
176652cd27cbSRobert Watson 					continue;
176752cd27cbSRobert Watson 			}
176852cd27cbSRobert Watson 
176952cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
177052cd27cbSRobert Watson 				if (injail)
177152cd27cbSRobert Watson 					goto found;
177252cd27cbSRobert Watson 				else
177352cd27cbSRobert Watson 					local_exact = inp;
177452cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
177552cd27cbSRobert Watson #ifdef INET6
177652cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
177752cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
177852cd27cbSRobert Watson 					local_wild_mapped = inp;
177952cd27cbSRobert Watson 				else
178083e521ecSBjoern A. Zeeb #endif
178152cd27cbSRobert Watson 					if (injail)
178252cd27cbSRobert Watson 						jail_wild = inp;
178352cd27cbSRobert Watson 					else
178452cd27cbSRobert Watson 						local_wild = inp;
178552cd27cbSRobert Watson 			}
178652cd27cbSRobert Watson 		} /* LIST_FOREACH */
178752cd27cbSRobert Watson 		inp = jail_wild;
178852cd27cbSRobert Watson 		if (inp == NULL)
178952cd27cbSRobert Watson 			inp = local_exact;
179052cd27cbSRobert Watson 		if (inp == NULL)
179152cd27cbSRobert Watson 			inp = local_wild;
179252cd27cbSRobert Watson #ifdef INET6
179352cd27cbSRobert Watson 		if (inp == NULL)
179452cd27cbSRobert Watson 			inp = local_wild_mapped;
179583e521ecSBjoern A. Zeeb #endif
179652cd27cbSRobert Watson 		if (inp != NULL)
179752cd27cbSRobert Watson 			goto found;
179852cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
179952cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
180052cd27cbSRobert Watson 	return (NULL);
180152cd27cbSRobert Watson 
180252cd27cbSRobert Watson found:
180352cd27cbSRobert Watson 	in_pcbref(inp);
180452cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
180552cd27cbSRobert Watson 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
180652cd27cbSRobert Watson 		INP_WLOCK(inp);
180752cd27cbSRobert Watson 		if (in_pcbrele_wlocked(inp))
180852cd27cbSRobert Watson 			return (NULL);
180952cd27cbSRobert Watson 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
181052cd27cbSRobert Watson 		INP_RLOCK(inp);
181152cd27cbSRobert Watson 		if (in_pcbrele_rlocked(inp))
181252cd27cbSRobert Watson 			return (NULL);
181352cd27cbSRobert Watson 	} else
181452cd27cbSRobert Watson 		panic("%s: locking bug", __func__);
181552cd27cbSRobert Watson 	return (inp);
181652cd27cbSRobert Watson }
181752cd27cbSRobert Watson #endif /* PCBGROUP */
181852cd27cbSRobert Watson 
181915bd2b43SDavid Greenman /*
1820fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1821fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1822fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
182315bd2b43SDavid Greenman  */
1824fa046d87SRobert Watson static struct inpcb *
1825fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
182668e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1827136d4f1cSRobert Watson     struct ifnet *ifp)
182815bd2b43SDavid Greenman {
182915bd2b43SDavid Greenman 	struct inpcbhead *head;
1830413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
183115bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
183215bd2b43SDavid Greenman 
183368e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
183468e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
183568e0d7e0SRobert Watson 
1836fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1837602cc7f1SRobert Watson 
183815bd2b43SDavid Greenman 	/*
183915bd2b43SDavid Greenman 	 * First look for an exact match.
184015bd2b43SDavid Greenman 	 */
1841413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1842712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1843712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1844fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1845cfa1ca9dSYoshinobu Inoue #ifdef INET6
1846413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1847369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1848cfa1ca9dSYoshinobu Inoue 			continue;
1849cfa1ca9dSYoshinobu Inoue #endif
18506d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1851ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1852ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1853413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1854413628a7SBjoern A. Zeeb 			/*
1855413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1856413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1857413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1858413628a7SBjoern A. Zeeb 			 */
18590304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1860c3229e05SDavid Greenman 				return (inp);
1861413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1862413628a7SBjoern A. Zeeb 				tmpinp = inp;
1863c3229e05SDavid Greenman 		}
1864413628a7SBjoern A. Zeeb 	}
1865413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1866413628a7SBjoern A. Zeeb 		return (tmpinp);
1867e3fd5ffdSRobert Watson 
1868e3fd5ffdSRobert Watson 	/*
1869e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1870e3fd5ffdSRobert Watson 	 */
187168e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1872413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1873e3fd5ffdSRobert Watson #ifdef INET6
1874cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1875e3fd5ffdSRobert Watson #endif
1876413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1877413628a7SBjoern A. Zeeb 		int injail;
1878413628a7SBjoern A. Zeeb 
1879413628a7SBjoern A. Zeeb 		/*
1880413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1881413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1882413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1883413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1884413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1885413628a7SBjoern A. Zeeb 		 */
18866d6a026bSDavid Greenman 
1887712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1888712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1889fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1890cfa1ca9dSYoshinobu Inoue #ifdef INET6
1891413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1892369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1893cfa1ca9dSYoshinobu Inoue 				continue;
1894cfa1ca9dSYoshinobu Inoue #endif
1895413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1896413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1897413628a7SBjoern A. Zeeb 				continue;
1898413628a7SBjoern A. Zeeb 
18990304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1900413628a7SBjoern A. Zeeb 			if (injail) {
1901b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1902b89e82ddSJamie Gritton 				    &laddr) != 0)
1903413628a7SBjoern A. Zeeb 					continue;
1904413628a7SBjoern A. Zeeb 			} else {
1905413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1906413628a7SBjoern A. Zeeb 					continue;
1907413628a7SBjoern A. Zeeb 			}
1908413628a7SBjoern A. Zeeb 
1909413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1910413628a7SBjoern A. Zeeb 				if (injail)
1911c3229e05SDavid Greenman 					return (inp);
1912413628a7SBjoern A. Zeeb 				else
1913413628a7SBjoern A. Zeeb 					local_exact = inp;
1914413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1915e3fd5ffdSRobert Watson #ifdef INET6
1916413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
19175cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1918cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1919cfa1ca9dSYoshinobu Inoue 				else
192083e521ecSBjoern A. Zeeb #endif
1921413628a7SBjoern A. Zeeb 					if (injail)
1922413628a7SBjoern A. Zeeb 						jail_wild = inp;
1923413628a7SBjoern A. Zeeb 					else
19246d6a026bSDavid Greenman 						local_wild = inp;
19256d6a026bSDavid Greenman 			}
1926413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1927413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1928413628a7SBjoern A. Zeeb 			return (jail_wild);
1929413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1930413628a7SBjoern A. Zeeb 			return (local_exact);
1931413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1932c3229e05SDavid Greenman 			return (local_wild);
1933413628a7SBjoern A. Zeeb #ifdef INET6
1934413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1935413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
193683e521ecSBjoern A. Zeeb #endif
193768e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1938413628a7SBjoern A. Zeeb 
19396d6a026bSDavid Greenman 	return (NULL);
194015bd2b43SDavid Greenman }
1941fa046d87SRobert Watson 
1942fa046d87SRobert Watson /*
1943fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1944fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1945fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1946fa046d87SRobert Watson  */
1947fa046d87SRobert Watson static struct inpcb *
1948fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1949fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1950fa046d87SRobert Watson     struct ifnet *ifp)
1951fa046d87SRobert Watson {
1952fa046d87SRobert Watson 	struct inpcb *inp;
1953fa046d87SRobert Watson 
1954fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1955fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1956fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1957fa046d87SRobert Watson 	if (inp != NULL) {
1958fa046d87SRobert Watson 		in_pcbref(inp);
1959fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1960fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1961fa046d87SRobert Watson 			INP_WLOCK(inp);
1962fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1963fa046d87SRobert Watson 				return (NULL);
1964fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1965fa046d87SRobert Watson 			INP_RLOCK(inp);
1966fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1967fa046d87SRobert Watson 				return (NULL);
1968fa046d87SRobert Watson 		} else
1969fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1970fa046d87SRobert Watson 	} else
1971fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1972fa046d87SRobert Watson 	return (inp);
1973fa046d87SRobert Watson }
1974fa046d87SRobert Watson 
1975fa046d87SRobert Watson /*
1976d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1977d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
197852cd27cbSRobert Watson  *
197952cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
1980fa046d87SRobert Watson  */
1981fa046d87SRobert Watson struct inpcb *
1982fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1983fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1984fa046d87SRobert Watson {
19857527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
198652cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
198752cd27cbSRobert Watson #endif
1988fa046d87SRobert Watson 
1989fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1990fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1991fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1992fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1993fa046d87SRobert Watson 
19947527624eSRobert Watson 	/*
19957527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
19967527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
19977527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
19987527624eSRobert Watson 	 * in software.
19997527624eSRobert Watson 	 *
20007527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
20017527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
20027527624eSRobert Watson 	 * in software.
20037527624eSRobert Watson 	 */
20047527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
200552cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
200652cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
200752cd27cbSRobert Watson 		    fport);
200852cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
200952cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
201052cd27cbSRobert Watson 	}
201152cd27cbSRobert Watson #endif
2012fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2013fa046d87SRobert Watson 	    lookupflags, ifp));
2014fa046d87SRobert Watson }
2015d3c1f003SRobert Watson 
2016d3c1f003SRobert Watson struct inpcb *
2017d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2018d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2019d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2020d3c1f003SRobert Watson {
202152cd27cbSRobert Watson #ifdef PCBGROUP
202252cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
202352cd27cbSRobert Watson #endif
2024d3c1f003SRobert Watson 
2025d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2026d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2027d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2028d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2029d3c1f003SRobert Watson 
203052cd27cbSRobert Watson #ifdef PCBGROUP
20317527624eSRobert Watson 	/*
20327527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
20337527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
20347527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
20357527624eSRobert Watson 	 * using RSS.
20367527624eSRobert Watson 	 *
20377527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
20387527624eSRobert Watson 	 */
20397527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
20407527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
204152cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
204252cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
204352cd27cbSRobert Watson 		if (pcbgroup != NULL)
204452cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
204552cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
20467527624eSRobert Watson #ifndef RSS
204752cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
204852cd27cbSRobert Watson 		    fport);
204952cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
205052cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
20517527624eSRobert Watson #endif
205252cd27cbSRobert Watson 	}
205352cd27cbSRobert Watson #endif
2054d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2055d3c1f003SRobert Watson 	    lookupflags, ifp));
2056d3c1f003SRobert Watson }
205767107f45SBjoern A. Zeeb #endif /* INET */
205815bd2b43SDavid Greenman 
20597bc4aca7SDavid Greenman /*
2060c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
20617bc4aca7SDavid Greenman  */
206252cd27cbSRobert Watson static int
206352cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
206415bd2b43SDavid Greenman {
2065c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2066c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2067c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2068c3229e05SDavid Greenman 	struct inpcbport *phd;
2069cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
207015bd2b43SDavid Greenman 
20718501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2072fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2073fa046d87SRobert Watson 
2074111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2075111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2076602cc7f1SRobert Watson 
2077cfa1ca9dSYoshinobu Inoue #ifdef INET6
2078cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
20791b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2080cfa1ca9dSYoshinobu Inoue 	else
208183e521ecSBjoern A. Zeeb #endif
2082cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2083cfa1ca9dSYoshinobu Inoue 
2084712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2085712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
208615bd2b43SDavid Greenman 
2087712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2088712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2089c3229e05SDavid Greenman 
2090c3229e05SDavid Greenman 	/*
2091c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2092c3229e05SDavid Greenman 	 */
2093fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2094c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2095c3229e05SDavid Greenman 			break;
2096c3229e05SDavid Greenman 	}
2097c3229e05SDavid Greenman 	/*
2098c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2099c3229e05SDavid Greenman 	 */
2100c3229e05SDavid Greenman 	if (phd == NULL) {
21011ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2102c3229e05SDavid Greenman 		if (phd == NULL) {
2103c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2104c3229e05SDavid Greenman 		}
2105c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2106c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2107c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2108c3229e05SDavid Greenman 	}
2109c3229e05SDavid Greenman 	inp->inp_phd = phd;
2110c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2111c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2112111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
211352cd27cbSRobert Watson #ifdef PCBGROUP
211452cd27cbSRobert Watson 	if (do_pcbgroup_update)
211552cd27cbSRobert Watson 		in_pcbgroup_update(inp);
211652cd27cbSRobert Watson #endif
2117c3229e05SDavid Greenman 	return (0);
211815bd2b43SDavid Greenman }
211915bd2b43SDavid Greenman 
2120c3229e05SDavid Greenman /*
212152cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
212252cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
212352cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
212452cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
212552cd27cbSRobert Watson  * pcbgroup until later.
212652cd27cbSRobert Watson  *
212752cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
212852cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
212952cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
213052cd27cbSRobert Watson  */
213152cd27cbSRobert Watson int
213252cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
213352cd27cbSRobert Watson {
213452cd27cbSRobert Watson 
213552cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
213652cd27cbSRobert Watson }
213752cd27cbSRobert Watson 
213852cd27cbSRobert Watson int
213952cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
214052cd27cbSRobert Watson {
214152cd27cbSRobert Watson 
214252cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
214352cd27cbSRobert Watson }
214452cd27cbSRobert Watson 
214552cd27cbSRobert Watson /*
2146c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2147c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2148c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2149c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2150c3229e05SDavid Greenman  */
215115bd2b43SDavid Greenman void
2152d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
215315bd2b43SDavid Greenman {
215459daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
215515bd2b43SDavid Greenman 	struct inpcbhead *head;
2156cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
215715bd2b43SDavid Greenman 
21588501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2159fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2160fa046d87SRobert Watson 
2161111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2162111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2163602cc7f1SRobert Watson 
2164cfa1ca9dSYoshinobu Inoue #ifdef INET6
2165cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
21661b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2167cfa1ca9dSYoshinobu Inoue 	else
216883e521ecSBjoern A. Zeeb #endif
2169cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2170cfa1ca9dSYoshinobu Inoue 
2171712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2172712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
217315bd2b43SDavid Greenman 
2174c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
217515bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
217652cd27cbSRobert Watson 
217752cd27cbSRobert Watson #ifdef PCBGROUP
217852cd27cbSRobert Watson 	if (m != NULL)
217952cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
218052cd27cbSRobert Watson 	else
218152cd27cbSRobert Watson 		in_pcbgroup_update(inp);
218252cd27cbSRobert Watson #endif
2183c3229e05SDavid Greenman }
2184c3229e05SDavid Greenman 
2185d3c1f003SRobert Watson void
2186d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2187d3c1f003SRobert Watson {
2188d3c1f003SRobert Watson 
2189d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2190d3c1f003SRobert Watson }
2191d3c1f003SRobert Watson 
2192c3229e05SDavid Greenman /*
2193c3229e05SDavid Greenman  * Remove PCB from various lists.
2194c3229e05SDavid Greenman  */
21956d888973SRobert Watson static void
2196136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2197c3229e05SDavid Greenman {
219859daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
219959daba27SSam Leffler 
2200ff9b006dSJulien Charbon #ifdef INVARIANTS
2201ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
2202ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
2203ff9b006dSJulien Charbon 	} else {
220459daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
2205ff9b006dSJulien Charbon 	}
2206ff9b006dSJulien Charbon #endif
2207ff9b006dSJulien Charbon 
22088501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2209ff9b006dSJulien Charbon 	INP_LIST_WLOCK_ASSERT(pcbinfo);
221059daba27SSam Leffler 
221159daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2212111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2213c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2214c3229e05SDavid Greenman 
2215fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2216c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2217c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2218fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2219c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2220c3229e05SDavid Greenman 			free(phd, M_PCB);
2221c3229e05SDavid Greenman 		}
2222fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2223111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2224c3229e05SDavid Greenman 	}
2225c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
222659daba27SSam Leffler 	pcbinfo->ipi_count--;
222752cd27cbSRobert Watson #ifdef PCBGROUP
222852cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
222952cd27cbSRobert Watson #endif
223015bd2b43SDavid Greenman }
223175c13541SPoul-Henning Kamp 
2232a557af22SRobert Watson /*
2233*84cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
2234*84cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
2235*84cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
2236*84cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
2237*84cc0778SGeorge V. Neville-Neil  */
2238*84cc0778SGeorge V. Neville-Neil void
2239*84cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
2240*84cc0778SGeorge V. Neville-Neil {
2241*84cc0778SGeorge V. Neville-Neil 
2242*84cc0778SGeorge V. Neville-Neil 	if (inp->inp_route.ro_rt) {
2243*84cc0778SGeorge V. Neville-Neil 		RTFREE(inp->inp_route.ro_rt);
2244*84cc0778SGeorge V. Neville-Neil 		inp->inp_route.ro_rt = (struct rtentry *)NULL;
2245*84cc0778SGeorge V. Neville-Neil 	}
2246*84cc0778SGeorge V. Neville-Neil 	return;
2247*84cc0778SGeorge V. Neville-Neil }
2248*84cc0778SGeorge V. Neville-Neil 
2249*84cc0778SGeorge V. Neville-Neil /*
2250a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2251a557af22SRobert Watson  * label change into the in_pcb for the socket.
2252a557af22SRobert Watson  */
2253a557af22SRobert Watson void
2254136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2255a557af22SRobert Watson {
2256a557af22SRobert Watson #ifdef MAC
2257a557af22SRobert Watson 	struct inpcb *inp;
2258a557af22SRobert Watson 
22594c7c478dSRobert Watson 	inp = sotoinpcb(so);
22604c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2261602cc7f1SRobert Watson 
22628501a69cSRobert Watson 	INP_WLOCK(inp);
2263310e7cebSRobert Watson 	SOCK_LOCK(so);
2264a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2265310e7cebSRobert Watson 	SOCK_UNLOCK(so);
22668501a69cSRobert Watson 	INP_WUNLOCK(inp);
2267a557af22SRobert Watson #endif
2268a557af22SRobert Watson }
22695f311da2SMike Silbersack 
22705f311da2SMike Silbersack /*
2271ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2272ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2273ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2274ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2275ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
22765f311da2SMike Silbersack  */
2277aae49dd3SBjoern A. Zeeb static void
2278136d4f1cSRobert Watson ipport_tick(void *xtp)
22795f311da2SMike Silbersack {
22808b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2281ad3a630fSRobert Watson 
22825ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
22838b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
22848b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
22858b615593SMarko Zec 		if (V_ipport_tcpallocs <=
22868b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2287603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2288603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2289ad3a630fSRobert Watson 		} else
2290603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2291603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
22928b615593SMarko Zec 		CURVNET_RESTORE();
22938b615593SMarko Zec 	}
22945ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
22955f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
22965f311da2SMike Silbersack }
2297497057eeSRobert Watson 
2298aae49dd3SBjoern A. Zeeb static void
2299aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2300aae49dd3SBjoern A. Zeeb {
2301aae49dd3SBjoern A. Zeeb 
2302aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2303aae49dd3SBjoern A. Zeeb }
2304aae49dd3SBjoern A. Zeeb 
2305aae49dd3SBjoern A. Zeeb /*
2306aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2307aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2308aae49dd3SBjoern A. Zeeb  */
2309aae49dd3SBjoern A. Zeeb static void
2310aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2311aae49dd3SBjoern A. Zeeb {
2312aae49dd3SBjoern A. Zeeb 
2313aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2314fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2315aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2316aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2317aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2318aae49dd3SBjoern A. Zeeb }
2319aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2320aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2321aae49dd3SBjoern A. Zeeb 
23223d585327SKip Macy void
23233d585327SKip Macy inp_wlock(struct inpcb *inp)
23243d585327SKip Macy {
23253d585327SKip Macy 
23268501a69cSRobert Watson 	INP_WLOCK(inp);
23273d585327SKip Macy }
23283d585327SKip Macy 
23293d585327SKip Macy void
23303d585327SKip Macy inp_wunlock(struct inpcb *inp)
23313d585327SKip Macy {
23323d585327SKip Macy 
23338501a69cSRobert Watson 	INP_WUNLOCK(inp);
23343d585327SKip Macy }
23353d585327SKip Macy 
23363d585327SKip Macy void
23373d585327SKip Macy inp_rlock(struct inpcb *inp)
23383d585327SKip Macy {
23393d585327SKip Macy 
2340a69042a5SRobert Watson 	INP_RLOCK(inp);
23413d585327SKip Macy }
23423d585327SKip Macy 
23433d585327SKip Macy void
23443d585327SKip Macy inp_runlock(struct inpcb *inp)
23453d585327SKip Macy {
23463d585327SKip Macy 
2347a69042a5SRobert Watson 	INP_RUNLOCK(inp);
23483d585327SKip Macy }
23493d585327SKip Macy 
23503d585327SKip Macy #ifdef INVARIANTS
23513d585327SKip Macy void
2352e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
23533d585327SKip Macy {
23543d585327SKip Macy 
23558501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
23563d585327SKip Macy }
23573d585327SKip Macy 
23583d585327SKip Macy void
2359e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
23603d585327SKip Macy {
23613d585327SKip Macy 
23623d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
23633d585327SKip Macy }
23643d585327SKip Macy #endif
23653d585327SKip Macy 
23669378e437SKip Macy void
23679378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
23689378e437SKip Macy {
23699378e437SKip Macy 	struct inpcb *inp;
23709378e437SKip Macy 
2371ff9b006dSJulien Charbon 	INP_INFO_WLOCK(&V_tcbinfo);
237297021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
23739378e437SKip Macy 		INP_WLOCK(inp);
23749378e437SKip Macy 		func(inp, arg);
23759378e437SKip Macy 		INP_WUNLOCK(inp);
23769378e437SKip Macy 	}
2377ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(&V_tcbinfo);
23789378e437SKip Macy }
23799378e437SKip Macy 
23809378e437SKip Macy struct socket *
23819378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
23829378e437SKip Macy {
23839378e437SKip Macy 
23849378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23859378e437SKip Macy 	return (inp->inp_socket);
23869378e437SKip Macy }
23879378e437SKip Macy 
23889378e437SKip Macy struct tcpcb *
23899378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
23909378e437SKip Macy {
23919378e437SKip Macy 
23929378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23939378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
23949378e437SKip Macy }
23959378e437SKip Macy 
23969378e437SKip Macy int
23979378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
23989378e437SKip Macy {
23999378e437SKip Macy 
24009378e437SKip Macy 	return (inp->inp_ip_tos);
24019378e437SKip Macy }
24029378e437SKip Macy 
24039378e437SKip Macy void
24049378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
24059378e437SKip Macy {
24069378e437SKip Macy 
24079378e437SKip Macy 	inp->inp_ip_tos = val;
24089378e437SKip Macy }
24099378e437SKip Macy 
24109378e437SKip Macy void
2411df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
24129d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
24139378e437SKip Macy {
24149378e437SKip Macy 
24159d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2416df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2417df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
24189378e437SKip Macy 	*lp = inp->inp_lport;
24199378e437SKip Macy 	*fp = inp->inp_fport;
24209378e437SKip Macy }
24219378e437SKip Macy 
2422dd0e6c38SKip Macy struct inpcb *
2423dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2424dd0e6c38SKip Macy {
2425dd0e6c38SKip Macy 
2426dd0e6c38SKip Macy 	return (sotoinpcb(so));
2427dd0e6c38SKip Macy }
2428dd0e6c38SKip Macy 
2429dd0e6c38SKip Macy struct tcpcb *
2430dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2431dd0e6c38SKip Macy {
2432dd0e6c38SKip Macy 
2433dd0e6c38SKip Macy 	return (sototcpcb(so));
2434dd0e6c38SKip Macy }
2435dd0e6c38SKip Macy 
2436497057eeSRobert Watson #ifdef DDB
2437497057eeSRobert Watson static void
2438497057eeSRobert Watson db_print_indent(int indent)
2439497057eeSRobert Watson {
2440497057eeSRobert Watson 	int i;
2441497057eeSRobert Watson 
2442497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2443497057eeSRobert Watson 		db_printf(" ");
2444497057eeSRobert Watson }
2445497057eeSRobert Watson 
2446497057eeSRobert Watson static void
2447497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2448497057eeSRobert Watson {
2449497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2450497057eeSRobert Watson 
2451497057eeSRobert Watson 	db_print_indent(indent);
2452497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2453497057eeSRobert Watson 
2454497057eeSRobert Watson 	indent += 2;
2455497057eeSRobert Watson 
245603dc38a4SRobert Watson #ifdef INET6
2457dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2458497057eeSRobert Watson 		/* IPv6. */
2459497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2460497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
246183e521ecSBjoern A. Zeeb 	} else
246203dc38a4SRobert Watson #endif
246383e521ecSBjoern A. Zeeb 	{
2464497057eeSRobert Watson 		/* IPv4. */
2465497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2466497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2467497057eeSRobert Watson 	}
2468497057eeSRobert Watson 	db_print_indent(indent);
2469497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2470497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2471497057eeSRobert Watson 	db_print_indent(indent);
2472497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2473497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2474497057eeSRobert Watson }
2475497057eeSRobert Watson 
2476497057eeSRobert Watson static void
2477497057eeSRobert Watson db_print_inpflags(int inp_flags)
2478497057eeSRobert Watson {
2479497057eeSRobert Watson 	int comma;
2480497057eeSRobert Watson 
2481497057eeSRobert Watson 	comma = 0;
2482497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2483497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2484497057eeSRobert Watson 		comma = 1;
2485497057eeSRobert Watson 	}
2486497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2487497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2488497057eeSRobert Watson 		comma = 1;
2489497057eeSRobert Watson 	}
2490497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2491497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2492497057eeSRobert Watson 		comma = 1;
2493497057eeSRobert Watson 	}
2494497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2495497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2496497057eeSRobert Watson 		comma = 1;
2497497057eeSRobert Watson 	}
2498497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2499497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2500497057eeSRobert Watson 		comma = 1;
2501497057eeSRobert Watson 	}
2502497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2503497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2504497057eeSRobert Watson 		comma = 1;
2505497057eeSRobert Watson 	}
2506497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2507497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2508497057eeSRobert Watson 		comma = 1;
2509497057eeSRobert Watson 	}
2510497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2511497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2512497057eeSRobert Watson 		comma = 1;
2513497057eeSRobert Watson 	}
2514497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2515497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2516497057eeSRobert Watson 		comma = 1;
2517497057eeSRobert Watson 	}
2518497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2519497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2520497057eeSRobert Watson 		comma = 1;
2521497057eeSRobert Watson 	}
2522497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2523497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2524497057eeSRobert Watson 		comma = 1;
2525497057eeSRobert Watson 	}
25263cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
25273cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
25283cca425bSMichael Tuexen 		comma = 1;
25293cca425bSMichael Tuexen 	}
2530497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2531497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2532497057eeSRobert Watson 		comma = 1;
2533497057eeSRobert Watson 	}
2534497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2535497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2536497057eeSRobert Watson 		comma = 1;
2537497057eeSRobert Watson 	}
2538497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2539497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2540497057eeSRobert Watson 		comma = 1;
2541497057eeSRobert Watson 	}
2542497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2543497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2544497057eeSRobert Watson 		comma = 1;
2545497057eeSRobert Watson 	}
2546497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2547497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2548497057eeSRobert Watson 		comma = 1;
2549497057eeSRobert Watson 	}
2550497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2551497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2552497057eeSRobert Watson 		comma = 1;
2553497057eeSRobert Watson 	}
2554497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2555497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2556497057eeSRobert Watson 		comma = 1;
2557497057eeSRobert Watson 	}
2558497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2559497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2560497057eeSRobert Watson 		comma = 1;
2561497057eeSRobert Watson 	}
2562497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2563497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2564497057eeSRobert Watson 		comma = 1;
2565497057eeSRobert Watson 	}
2566ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2567ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2568ad71fe3cSRobert Watson 		comma  = 1;
2569ad71fe3cSRobert Watson 	}
2570ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2571ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2572ad71fe3cSRobert Watson 		comma  = 1;
2573ad71fe3cSRobert Watson 	}
2574ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2575ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2576ad71fe3cSRobert Watson 		comma  = 1;
2577ad71fe3cSRobert Watson 	}
2578ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2579ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2580ad71fe3cSRobert Watson 		comma  = 1;
2581ad71fe3cSRobert Watson 	}
2582497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2583497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2584497057eeSRobert Watson 		comma = 1;
2585497057eeSRobert Watson 	}
2586497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2587497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2588497057eeSRobert Watson 		comma = 1;
2589497057eeSRobert Watson 	}
2590497057eeSRobert Watson }
2591497057eeSRobert Watson 
2592497057eeSRobert Watson static void
2593497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2594497057eeSRobert Watson {
2595497057eeSRobert Watson 	int comma;
2596497057eeSRobert Watson 
2597497057eeSRobert Watson 	comma = 0;
2598497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2599497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2600497057eeSRobert Watson 		comma  = 1;
2601497057eeSRobert Watson 	}
2602497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2603497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2604497057eeSRobert Watson 		comma  = 1;
2605497057eeSRobert Watson 	}
2606497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2607497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2608497057eeSRobert Watson 		comma  = 1;
2609497057eeSRobert Watson 	}
2610497057eeSRobert Watson }
2611497057eeSRobert Watson 
26126d888973SRobert Watson static void
2613497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2614497057eeSRobert Watson {
2615497057eeSRobert Watson 
2616497057eeSRobert Watson 	db_print_indent(indent);
2617497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2618497057eeSRobert Watson 
2619497057eeSRobert Watson 	indent += 2;
2620497057eeSRobert Watson 
2621497057eeSRobert Watson 	db_print_indent(indent);
2622497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2623497057eeSRobert Watson 
2624497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2625497057eeSRobert Watson 
2626497057eeSRobert Watson 	db_print_indent(indent);
2627497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2628497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2629497057eeSRobert Watson 
2630497057eeSRobert Watson 	db_print_indent(indent);
2631497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2632497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2633497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2634497057eeSRobert Watson 	db_printf(")\n");
2635497057eeSRobert Watson 
2636497057eeSRobert Watson 	db_print_indent(indent);
2637497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2638497057eeSRobert Watson 	    inp->inp_vflag);
2639497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2640497057eeSRobert Watson 	db_printf(")\n");
2641497057eeSRobert Watson 
2642497057eeSRobert Watson 	db_print_indent(indent);
2643497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2644497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2645497057eeSRobert Watson 
2646497057eeSRobert Watson 	db_print_indent(indent);
2647497057eeSRobert Watson #ifdef INET6
2648497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2649497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2650497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2651497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2652497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2653497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2654497057eeSRobert Watson 		    inp->in6p_hops);
2655497057eeSRobert Watson 	} else
2656497057eeSRobert Watson #endif
2657497057eeSRobert Watson 	{
2658497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2659497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2660497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2661497057eeSRobert Watson 	}
2662497057eeSRobert Watson 
2663497057eeSRobert Watson 	db_print_indent(indent);
2664497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2665497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2666497057eeSRobert Watson }
2667497057eeSRobert Watson 
2668497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2669497057eeSRobert Watson {
2670497057eeSRobert Watson 	struct inpcb *inp;
2671497057eeSRobert Watson 
2672497057eeSRobert Watson 	if (!have_addr) {
2673497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2674497057eeSRobert Watson 		return;
2675497057eeSRobert Watson 	}
2676497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2677497057eeSRobert Watson 
2678497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2679497057eeSRobert Watson }
268083e521ecSBjoern A. Zeeb #endif /* DDB */
2681