xref: /freebsd/sys/netinet/in_pcb.c (revision ea8d14925c6a7e96949493984f558411d0fa4380)
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>
54*ea8d1492SAlexander 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
1301cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1302df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
130386d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1304a557af22SRobert Watson #ifdef MAC
130530d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1306a557af22SRobert Watson #endif
130779bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
130828696211SRobert Watson 		INP_WUNLOCK(inp);
130928696211SRobert Watson }
131028696211SRobert Watson 
131128696211SRobert Watson /*
1312c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1313c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1314c0a211c5SRobert Watson  *
1315c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1316c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1317c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1318c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1319c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1320c0a211c5SRobert Watson  * in_pcbdetach().
1321c0a211c5SRobert Watson  *
1322c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1323c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
132410702a28SRobert Watson  */
132510702a28SRobert Watson void
132610702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
132710702a28SRobert Watson {
132810702a28SRobert Watson 
13298501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
133010702a28SRobert Watson 
1331fa046d87SRobert Watson 	/*
1332fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1333fa046d87SRobert Watson 	 * the hash lock...?
1334fa046d87SRobert Watson 	 */
1335ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1336111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
133710702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
133810702a28SRobert Watson 
1339fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
134010702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
134110702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
134210702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
134310702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
134410702a28SRobert Watson 			free(phd, M_PCB);
134510702a28SRobert Watson 		}
1346fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1347111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
134852cd27cbSRobert Watson #ifdef PCBGROUP
134952cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
135052cd27cbSRobert Watson #endif
135110702a28SRobert Watson 	}
135210702a28SRobert Watson }
135310702a28SRobert Watson 
135467107f45SBjoern A. Zeeb #ifdef INET
135554d642bbSRobert Watson /*
135654d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
135754d642bbSRobert Watson  */
135826ef6ac4SDon Lewis struct sockaddr *
1359136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
136026ef6ac4SDon Lewis {
136126ef6ac4SDon Lewis 	struct sockaddr_in *sin;
136226ef6ac4SDon Lewis 
13631ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1364a163d034SWarner Losh 		M_WAITOK | M_ZERO);
136526ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
136626ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
136726ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
136826ef6ac4SDon Lewis 	sin->sin_port = port;
136926ef6ac4SDon Lewis 
137026ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
137126ef6ac4SDon Lewis }
137226ef6ac4SDon Lewis 
1373117bcae7SGarrett Wollman int
137454d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1375df8bae1dSRodney W. Grimes {
1376136d4f1cSRobert Watson 	struct inpcb *inp;
137726ef6ac4SDon Lewis 	struct in_addr addr;
137826ef6ac4SDon Lewis 	in_port_t port;
137942fa505bSDavid Greenman 
1380fdc984f7STor Egge 	inp = sotoinpcb(so);
138154d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
13826466b28aSRobert Watson 
1383a69042a5SRobert Watson 	INP_RLOCK(inp);
138426ef6ac4SDon Lewis 	port = inp->inp_lport;
138526ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1386a69042a5SRobert Watson 	INP_RUNLOCK(inp);
138742fa505bSDavid Greenman 
138826ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1389117bcae7SGarrett Wollman 	return 0;
1390df8bae1dSRodney W. Grimes }
1391df8bae1dSRodney W. Grimes 
1392117bcae7SGarrett Wollman int
139354d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1394df8bae1dSRodney W. Grimes {
1395136d4f1cSRobert Watson 	struct inpcb *inp;
139626ef6ac4SDon Lewis 	struct in_addr addr;
139726ef6ac4SDon Lewis 	in_port_t port;
139842fa505bSDavid Greenman 
1399fdc984f7STor Egge 	inp = sotoinpcb(so);
140054d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
14016466b28aSRobert Watson 
1402a69042a5SRobert Watson 	INP_RLOCK(inp);
140326ef6ac4SDon Lewis 	port = inp->inp_fport;
140426ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1405a69042a5SRobert Watson 	INP_RUNLOCK(inp);
140642fa505bSDavid Greenman 
140726ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1408117bcae7SGarrett Wollman 	return 0;
1409df8bae1dSRodney W. Grimes }
1410df8bae1dSRodney W. Grimes 
141126f9a767SRodney W. Grimes void
1412136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1413136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1414d1c54148SJesper Skriver {
1415f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1416d1c54148SJesper Skriver 
14173dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1418f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
14198501a69cSRobert Watson 		INP_WLOCK(inp);
1420d1c54148SJesper Skriver #ifdef INET6
1421f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
14228501a69cSRobert Watson 			INP_WUNLOCK(inp);
1423d1c54148SJesper Skriver 			continue;
1424f76fcf6dSJeffrey Hsu 		}
1425d1c54148SJesper Skriver #endif
1426d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1427f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
14288501a69cSRobert Watson 			INP_WUNLOCK(inp);
1429d1c54148SJesper Skriver 			continue;
1430d1c54148SJesper Skriver 		}
14313dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
14328501a69cSRobert Watson 			INP_WUNLOCK(inp);
1433f76fcf6dSJeffrey Hsu 	}
14343dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1435d1c54148SJesper Skriver }
1436d1c54148SJesper Skriver 
1437e43cc4aeSHajimu UMEMOTO void
1438136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1439e43cc4aeSHajimu UMEMOTO {
1440e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1441e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1442e43cc4aeSHajimu UMEMOTO 	int i, gap;
1443e43cc4aeSHajimu UMEMOTO 
1444ff9b006dSJulien Charbon 	INP_INFO_WLOCK(pcbinfo);
1445712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
14468501a69cSRobert Watson 		INP_WLOCK(inp);
1447e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1448e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1449e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1450e43cc4aeSHajimu UMEMOTO 			/*
1451e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1452e43cc4aeSHajimu UMEMOTO 			 * detached.
1453e43cc4aeSHajimu UMEMOTO 			 */
1454e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1455e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1456e43cc4aeSHajimu UMEMOTO 
1457e43cc4aeSHajimu UMEMOTO 			/*
1458e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1459e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1460e43cc4aeSHajimu UMEMOTO 			 */
1461e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1462e43cc4aeSHajimu UMEMOTO 			    i++) {
1463e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1464e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1465e43cc4aeSHajimu UMEMOTO 					gap++;
1466e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1467e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1468e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1469e43cc4aeSHajimu UMEMOTO 			}
1470e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1471e43cc4aeSHajimu UMEMOTO 		}
14728501a69cSRobert Watson 		INP_WUNLOCK(inp);
1473e43cc4aeSHajimu UMEMOTO 	}
1474ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(pcbinfo);
1475e43cc4aeSHajimu UMEMOTO }
1476e43cc4aeSHajimu UMEMOTO 
1477df8bae1dSRodney W. Grimes /*
1478fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1479fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1480c3229e05SDavid Greenman  */
1481d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1482df8bae1dSRodney W. Grimes struct inpcb *
1483136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
148468e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1485df8bae1dSRodney W. Grimes {
1486136d4f1cSRobert Watson 	struct inpcb *inp;
1487d5e8a67eSHajimu UMEMOTO #ifdef INET6
1488d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1489d5e8a67eSHajimu UMEMOTO #else
1490d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1491d5e8a67eSHajimu UMEMOTO #endif
1492d5e8a67eSHajimu UMEMOTO 	int wildcard;
14937bc4aca7SDavid Greenman 
149468e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
149568e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
149668e0d7e0SRobert Watson 
1497fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
14981b73ca0bSSam Leffler 
149968e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1500c3229e05SDavid Greenman 		struct inpcbhead *head;
1501c3229e05SDavid Greenman 		/*
1502c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1503c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1504c3229e05SDavid Greenman 		 */
1505712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1506712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1507fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1508cfa1ca9dSYoshinobu Inoue #ifdef INET6
1509413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1510369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1511cfa1ca9dSYoshinobu Inoue 				continue;
1512cfa1ca9dSYoshinobu Inoue #endif
1513c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1514c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1515c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1516c3229e05SDavid Greenman 				/*
1517413628a7SBjoern A. Zeeb 				 * Found?
1518c3229e05SDavid Greenman 				 */
1519413628a7SBjoern A. Zeeb 				if (cred == NULL ||
15200304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
15210304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1522c3229e05SDavid Greenman 					return (inp);
1523df8bae1dSRodney W. Grimes 			}
1524c3229e05SDavid Greenman 		}
1525c3229e05SDavid Greenman 		/*
1526c3229e05SDavid Greenman 		 * Not found.
1527c3229e05SDavid Greenman 		 */
1528c3229e05SDavid Greenman 		return (NULL);
1529c3229e05SDavid Greenman 	} else {
1530c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1531c3229e05SDavid Greenman 		struct inpcbport *phd;
1532c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1533c3229e05SDavid Greenman 		/*
1534c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1535c3229e05SDavid Greenman 		 *
1536c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1537c3229e05SDavid Greenman 		 * port hash list.
1538c3229e05SDavid Greenman 		 */
1539712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1540712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1541fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1542c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1543c3229e05SDavid Greenman 				break;
1544c3229e05SDavid Greenman 		}
1545c3229e05SDavid Greenman 		if (phd != NULL) {
1546c3229e05SDavid Greenman 			/*
1547c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1548c3229e05SDavid Greenman 			 * fit.
1549c3229e05SDavid Greenman 			 */
155037d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1551c3229e05SDavid Greenman 				wildcard = 0;
1552413628a7SBjoern A. Zeeb 				if (cred != NULL &&
15530304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
15540304c731SJamie Gritton 					cred->cr_prison))
1555413628a7SBjoern A. Zeeb 					continue;
1556cfa1ca9dSYoshinobu Inoue #ifdef INET6
1557413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1558369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1559cfa1ca9dSYoshinobu Inoue 					continue;
1560d5e8a67eSHajimu UMEMOTO 				/*
1561d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1562d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1563d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1564d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1565d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1566d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1567d5e8a67eSHajimu UMEMOTO 				 *
1568d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1569d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1570d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1571d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1572d5e8a67eSHajimu UMEMOTO 				 */
1573d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1574d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1575cfa1ca9dSYoshinobu Inoue #endif
1576c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1577c3229e05SDavid Greenman 					wildcard++;
157815bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
157915bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
158015bd2b43SDavid Greenman 						wildcard++;
158115bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
158215bd2b43SDavid Greenman 						continue;
158315bd2b43SDavid Greenman 				} else {
158415bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
158515bd2b43SDavid Greenman 						wildcard++;
158615bd2b43SDavid Greenman 				}
1587df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1588df8bae1dSRodney W. Grimes 					match = inp;
1589df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1590413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1591df8bae1dSRodney W. Grimes 						break;
1592df8bae1dSRodney W. Grimes 				}
1593df8bae1dSRodney W. Grimes 			}
15943dbdc25cSDavid Greenman 		}
1595df8bae1dSRodney W. Grimes 		return (match);
1596df8bae1dSRodney W. Grimes 	}
1597c3229e05SDavid Greenman }
1598d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
159915bd2b43SDavid Greenman 
160052cd27cbSRobert Watson #ifdef PCBGROUP
160152cd27cbSRobert Watson /*
160252cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
160352cd27cbSRobert Watson  */
160452cd27cbSRobert Watson static struct inpcb *
160552cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
160652cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
160752cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
160852cd27cbSRobert Watson {
160952cd27cbSRobert Watson 	struct inpcbhead *head;
161052cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
161152cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
161252cd27cbSRobert Watson 
161352cd27cbSRobert Watson 	/*
161452cd27cbSRobert Watson 	 * First look for an exact match.
161552cd27cbSRobert Watson 	 */
161652cd27cbSRobert Watson 	tmpinp = NULL;
161752cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
161852cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
161952cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
162052cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
162152cd27cbSRobert Watson #ifdef INET6
162252cd27cbSRobert Watson 		/* XXX inp locking */
162352cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
162452cd27cbSRobert Watson 			continue;
162552cd27cbSRobert Watson #endif
162652cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
162752cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
162852cd27cbSRobert Watson 		    inp->inp_fport == fport &&
162952cd27cbSRobert Watson 		    inp->inp_lport == lport) {
163052cd27cbSRobert Watson 			/*
163152cd27cbSRobert Watson 			 * XXX We should be able to directly return
163252cd27cbSRobert Watson 			 * the inp here, without any checks.
163352cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
163452cd27cbSRobert Watson 			 */
163552cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
163652cd27cbSRobert Watson 				goto found;
163752cd27cbSRobert Watson 			if (tmpinp == NULL)
163852cd27cbSRobert Watson 				tmpinp = inp;
163952cd27cbSRobert Watson 		}
164052cd27cbSRobert Watson 	}
164152cd27cbSRobert Watson 	if (tmpinp != NULL) {
164252cd27cbSRobert Watson 		inp = tmpinp;
164352cd27cbSRobert Watson 		goto found;
164452cd27cbSRobert Watson 	}
164552cd27cbSRobert Watson 
16460a100a6fSAdrian Chadd #ifdef	RSS
16470a100a6fSAdrian Chadd 	/*
16480a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
16490a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
16500a100a6fSAdrian Chadd 	 */
16510a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
16520a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
16530a100a6fSAdrian Chadd #ifdef INET6
16540a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
16550a100a6fSAdrian Chadd #endif
16560a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
16570a100a6fSAdrian Chadd 		struct inpcbhead *head;
16580a100a6fSAdrian Chadd 		int injail;
16590a100a6fSAdrian Chadd 
16600a100a6fSAdrian Chadd 		/*
16610a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
16620a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
16630a100a6fSAdrian Chadd 		 *      2. jailed, wild.
16640a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
16650a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
16660a100a6fSAdrian Chadd 		 */
16670a100a6fSAdrian Chadd 
16680a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
16690a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
16700a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
16710a100a6fSAdrian Chadd #ifdef INET6
16720a100a6fSAdrian Chadd 			/* XXX inp locking */
16730a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
16740a100a6fSAdrian Chadd 				continue;
16750a100a6fSAdrian Chadd #endif
16760a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
16770a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
16780a100a6fSAdrian Chadd 				continue;
16790a100a6fSAdrian Chadd 
16800a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
16810a100a6fSAdrian Chadd 			if (injail) {
16820a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
16830a100a6fSAdrian Chadd 				    &laddr) != 0)
16840a100a6fSAdrian Chadd 					continue;
16850a100a6fSAdrian Chadd 			} else {
16860a100a6fSAdrian Chadd 				if (local_exact != NULL)
16870a100a6fSAdrian Chadd 					continue;
16880a100a6fSAdrian Chadd 			}
16890a100a6fSAdrian Chadd 
16900a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
16910a100a6fSAdrian Chadd 				if (injail)
16920a100a6fSAdrian Chadd 					goto found;
16930a100a6fSAdrian Chadd 				else
16940a100a6fSAdrian Chadd 					local_exact = inp;
16950a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
16960a100a6fSAdrian Chadd #ifdef INET6
16970a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
16980a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
16990a100a6fSAdrian Chadd 					local_wild_mapped = inp;
17000a100a6fSAdrian Chadd 				else
17010a100a6fSAdrian Chadd #endif
17020a100a6fSAdrian Chadd 					if (injail)
17030a100a6fSAdrian Chadd 						jail_wild = inp;
17040a100a6fSAdrian Chadd 					else
17050a100a6fSAdrian Chadd 						local_wild = inp;
17060a100a6fSAdrian Chadd 			}
17070a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
17080a100a6fSAdrian Chadd 
17090a100a6fSAdrian Chadd 		inp = jail_wild;
17100a100a6fSAdrian Chadd 		if (inp == NULL)
17110a100a6fSAdrian Chadd 			inp = local_exact;
17120a100a6fSAdrian Chadd 		if (inp == NULL)
17130a100a6fSAdrian Chadd 			inp = local_wild;
17140a100a6fSAdrian Chadd #ifdef INET6
17150a100a6fSAdrian Chadd 		if (inp == NULL)
17160a100a6fSAdrian Chadd 			inp = local_wild_mapped;
17170a100a6fSAdrian Chadd #endif
17180a100a6fSAdrian Chadd 		if (inp != NULL)
17190a100a6fSAdrian Chadd 			goto found;
17200a100a6fSAdrian Chadd 	}
17210a100a6fSAdrian Chadd #endif
17220a100a6fSAdrian Chadd 
172352cd27cbSRobert Watson 	/*
172452cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
172552cd27cbSRobert Watson 	 */
172652cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
172752cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
172852cd27cbSRobert Watson #ifdef INET6
172952cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
173052cd27cbSRobert Watson #endif
173152cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
173252cd27cbSRobert Watson 		struct inpcbhead *head;
173352cd27cbSRobert Watson 		int injail;
173452cd27cbSRobert Watson 
173552cd27cbSRobert Watson 		/*
173652cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
173752cd27cbSRobert Watson 		 *      1. jailed, non-wild.
173852cd27cbSRobert Watson 		 *      2. jailed, wild.
173952cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
174052cd27cbSRobert Watson 		 *      4. non-jailed, wild.
174152cd27cbSRobert Watson 		 */
174252cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
174352cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
174452cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
174552cd27cbSRobert Watson #ifdef INET6
174652cd27cbSRobert Watson 			/* XXX inp locking */
174752cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
174852cd27cbSRobert Watson 				continue;
174952cd27cbSRobert Watson #endif
175052cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
175152cd27cbSRobert Watson 			    inp->inp_lport != lport)
175252cd27cbSRobert Watson 				continue;
175352cd27cbSRobert Watson 
175452cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
175552cd27cbSRobert Watson 			if (injail) {
175652cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
175752cd27cbSRobert Watson 				    &laddr) != 0)
175852cd27cbSRobert Watson 					continue;
175952cd27cbSRobert Watson 			} else {
176052cd27cbSRobert Watson 				if (local_exact != NULL)
176152cd27cbSRobert Watson 					continue;
176252cd27cbSRobert Watson 			}
176352cd27cbSRobert Watson 
176452cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
176552cd27cbSRobert Watson 				if (injail)
176652cd27cbSRobert Watson 					goto found;
176752cd27cbSRobert Watson 				else
176852cd27cbSRobert Watson 					local_exact = inp;
176952cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
177052cd27cbSRobert Watson #ifdef INET6
177152cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
177252cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
177352cd27cbSRobert Watson 					local_wild_mapped = inp;
177452cd27cbSRobert Watson 				else
177583e521ecSBjoern A. Zeeb #endif
177652cd27cbSRobert Watson 					if (injail)
177752cd27cbSRobert Watson 						jail_wild = inp;
177852cd27cbSRobert Watson 					else
177952cd27cbSRobert Watson 						local_wild = inp;
178052cd27cbSRobert Watson 			}
178152cd27cbSRobert Watson 		} /* LIST_FOREACH */
178252cd27cbSRobert Watson 		inp = jail_wild;
178352cd27cbSRobert Watson 		if (inp == NULL)
178452cd27cbSRobert Watson 			inp = local_exact;
178552cd27cbSRobert Watson 		if (inp == NULL)
178652cd27cbSRobert Watson 			inp = local_wild;
178752cd27cbSRobert Watson #ifdef INET6
178852cd27cbSRobert Watson 		if (inp == NULL)
178952cd27cbSRobert Watson 			inp = local_wild_mapped;
179083e521ecSBjoern A. Zeeb #endif
179152cd27cbSRobert Watson 		if (inp != NULL)
179252cd27cbSRobert Watson 			goto found;
179352cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
179452cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
179552cd27cbSRobert Watson 	return (NULL);
179652cd27cbSRobert Watson 
179752cd27cbSRobert Watson found:
179852cd27cbSRobert Watson 	in_pcbref(inp);
179952cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
180052cd27cbSRobert Watson 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
180152cd27cbSRobert Watson 		INP_WLOCK(inp);
180252cd27cbSRobert Watson 		if (in_pcbrele_wlocked(inp))
180352cd27cbSRobert Watson 			return (NULL);
180452cd27cbSRobert Watson 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
180552cd27cbSRobert Watson 		INP_RLOCK(inp);
180652cd27cbSRobert Watson 		if (in_pcbrele_rlocked(inp))
180752cd27cbSRobert Watson 			return (NULL);
180852cd27cbSRobert Watson 	} else
180952cd27cbSRobert Watson 		panic("%s: locking bug", __func__);
181052cd27cbSRobert Watson 	return (inp);
181152cd27cbSRobert Watson }
181252cd27cbSRobert Watson #endif /* PCBGROUP */
181352cd27cbSRobert Watson 
181415bd2b43SDavid Greenman /*
1815fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1816fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1817fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
181815bd2b43SDavid Greenman  */
1819fa046d87SRobert Watson static struct inpcb *
1820fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
182168e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1822136d4f1cSRobert Watson     struct ifnet *ifp)
182315bd2b43SDavid Greenman {
182415bd2b43SDavid Greenman 	struct inpcbhead *head;
1825413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
182615bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
182715bd2b43SDavid Greenman 
182868e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
182968e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
183068e0d7e0SRobert Watson 
1831fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1832602cc7f1SRobert Watson 
183315bd2b43SDavid Greenman 	/*
183415bd2b43SDavid Greenman 	 * First look for an exact match.
183515bd2b43SDavid Greenman 	 */
1836413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1837712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1838712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1839fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1840cfa1ca9dSYoshinobu Inoue #ifdef INET6
1841413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1842369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1843cfa1ca9dSYoshinobu Inoue 			continue;
1844cfa1ca9dSYoshinobu Inoue #endif
18456d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1846ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1847ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1848413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1849413628a7SBjoern A. Zeeb 			/*
1850413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1851413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1852413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1853413628a7SBjoern A. Zeeb 			 */
18540304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1855c3229e05SDavid Greenman 				return (inp);
1856413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1857413628a7SBjoern A. Zeeb 				tmpinp = inp;
1858c3229e05SDavid Greenman 		}
1859413628a7SBjoern A. Zeeb 	}
1860413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1861413628a7SBjoern A. Zeeb 		return (tmpinp);
1862e3fd5ffdSRobert Watson 
1863e3fd5ffdSRobert Watson 	/*
1864e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1865e3fd5ffdSRobert Watson 	 */
186668e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1867413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1868e3fd5ffdSRobert Watson #ifdef INET6
1869cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1870e3fd5ffdSRobert Watson #endif
1871413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1872413628a7SBjoern A. Zeeb 		int injail;
1873413628a7SBjoern A. Zeeb 
1874413628a7SBjoern A. Zeeb 		/*
1875413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1876413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1877413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1878413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1879413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1880413628a7SBjoern A. Zeeb 		 */
18816d6a026bSDavid Greenman 
1882712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1883712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1884fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1885cfa1ca9dSYoshinobu Inoue #ifdef INET6
1886413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1887369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1888cfa1ca9dSYoshinobu Inoue 				continue;
1889cfa1ca9dSYoshinobu Inoue #endif
1890413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1891413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1892413628a7SBjoern A. Zeeb 				continue;
1893413628a7SBjoern A. Zeeb 
18940304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1895413628a7SBjoern A. Zeeb 			if (injail) {
1896b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1897b89e82ddSJamie Gritton 				    &laddr) != 0)
1898413628a7SBjoern A. Zeeb 					continue;
1899413628a7SBjoern A. Zeeb 			} else {
1900413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1901413628a7SBjoern A. Zeeb 					continue;
1902413628a7SBjoern A. Zeeb 			}
1903413628a7SBjoern A. Zeeb 
1904413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1905413628a7SBjoern A. Zeeb 				if (injail)
1906c3229e05SDavid Greenman 					return (inp);
1907413628a7SBjoern A. Zeeb 				else
1908413628a7SBjoern A. Zeeb 					local_exact = inp;
1909413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1910e3fd5ffdSRobert Watson #ifdef INET6
1911413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
19125cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1913cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1914cfa1ca9dSYoshinobu Inoue 				else
191583e521ecSBjoern A. Zeeb #endif
1916413628a7SBjoern A. Zeeb 					if (injail)
1917413628a7SBjoern A. Zeeb 						jail_wild = inp;
1918413628a7SBjoern A. Zeeb 					else
19196d6a026bSDavid Greenman 						local_wild = inp;
19206d6a026bSDavid Greenman 			}
1921413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1922413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1923413628a7SBjoern A. Zeeb 			return (jail_wild);
1924413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1925413628a7SBjoern A. Zeeb 			return (local_exact);
1926413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1927c3229e05SDavid Greenman 			return (local_wild);
1928413628a7SBjoern A. Zeeb #ifdef INET6
1929413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1930413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
193183e521ecSBjoern A. Zeeb #endif
193268e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1933413628a7SBjoern A. Zeeb 
19346d6a026bSDavid Greenman 	return (NULL);
193515bd2b43SDavid Greenman }
1936fa046d87SRobert Watson 
1937fa046d87SRobert Watson /*
1938fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1939fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1940fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1941fa046d87SRobert Watson  */
1942fa046d87SRobert Watson static struct inpcb *
1943fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1944fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1945fa046d87SRobert Watson     struct ifnet *ifp)
1946fa046d87SRobert Watson {
1947fa046d87SRobert Watson 	struct inpcb *inp;
1948fa046d87SRobert Watson 
1949fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1950fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1951fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1952fa046d87SRobert Watson 	if (inp != NULL) {
1953fa046d87SRobert Watson 		in_pcbref(inp);
1954fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1955fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1956fa046d87SRobert Watson 			INP_WLOCK(inp);
1957fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1958fa046d87SRobert Watson 				return (NULL);
1959fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1960fa046d87SRobert Watson 			INP_RLOCK(inp);
1961fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1962fa046d87SRobert Watson 				return (NULL);
1963fa046d87SRobert Watson 		} else
1964fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1965fa046d87SRobert Watson 	} else
1966fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1967fa046d87SRobert Watson 	return (inp);
1968fa046d87SRobert Watson }
1969fa046d87SRobert Watson 
1970fa046d87SRobert Watson /*
1971d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1972d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
197352cd27cbSRobert Watson  *
197452cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
1975fa046d87SRobert Watson  */
1976fa046d87SRobert Watson struct inpcb *
1977fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1978fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1979fa046d87SRobert Watson {
19807527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
198152cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
198252cd27cbSRobert Watson #endif
1983fa046d87SRobert Watson 
1984fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1985fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1986fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1987fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1988fa046d87SRobert Watson 
19897527624eSRobert Watson 	/*
19907527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
19917527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
19927527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
19937527624eSRobert Watson 	 * in software.
19947527624eSRobert Watson 	 *
19957527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
19967527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
19977527624eSRobert Watson 	 * in software.
19987527624eSRobert Watson 	 */
19997527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
200052cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
200152cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
200252cd27cbSRobert Watson 		    fport);
200352cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
200452cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
200552cd27cbSRobert Watson 	}
200652cd27cbSRobert Watson #endif
2007fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2008fa046d87SRobert Watson 	    lookupflags, ifp));
2009fa046d87SRobert Watson }
2010d3c1f003SRobert Watson 
2011d3c1f003SRobert Watson struct inpcb *
2012d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2013d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2014d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2015d3c1f003SRobert Watson {
201652cd27cbSRobert Watson #ifdef PCBGROUP
201752cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
201852cd27cbSRobert Watson #endif
2019d3c1f003SRobert Watson 
2020d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2021d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2022d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2023d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2024d3c1f003SRobert Watson 
202552cd27cbSRobert Watson #ifdef PCBGROUP
20267527624eSRobert Watson 	/*
20277527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
20287527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
20297527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
20307527624eSRobert Watson 	 * using RSS.
20317527624eSRobert Watson 	 *
20327527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
20337527624eSRobert Watson 	 */
20347527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
20357527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
203652cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
203752cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
203852cd27cbSRobert Watson 		if (pcbgroup != NULL)
203952cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
204052cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
20417527624eSRobert Watson #ifndef RSS
204252cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
204352cd27cbSRobert Watson 		    fport);
204452cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
204552cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
20467527624eSRobert Watson #endif
204752cd27cbSRobert Watson 	}
204852cd27cbSRobert Watson #endif
2049d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2050d3c1f003SRobert Watson 	    lookupflags, ifp));
2051d3c1f003SRobert Watson }
205267107f45SBjoern A. Zeeb #endif /* INET */
205315bd2b43SDavid Greenman 
20547bc4aca7SDavid Greenman /*
2055c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
20567bc4aca7SDavid Greenman  */
205752cd27cbSRobert Watson static int
205852cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
205915bd2b43SDavid Greenman {
2060c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2061c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2062c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2063c3229e05SDavid Greenman 	struct inpcbport *phd;
2064cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
206515bd2b43SDavid Greenman 
20668501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2067fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2068fa046d87SRobert Watson 
2069111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2070111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2071602cc7f1SRobert Watson 
2072cfa1ca9dSYoshinobu Inoue #ifdef INET6
2073cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
20741b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2075cfa1ca9dSYoshinobu Inoue 	else
207683e521ecSBjoern A. Zeeb #endif
2077cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2078cfa1ca9dSYoshinobu Inoue 
2079712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2080712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
208115bd2b43SDavid Greenman 
2082712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2083712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2084c3229e05SDavid Greenman 
2085c3229e05SDavid Greenman 	/*
2086c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2087c3229e05SDavid Greenman 	 */
2088fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2089c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2090c3229e05SDavid Greenman 			break;
2091c3229e05SDavid Greenman 	}
2092c3229e05SDavid Greenman 	/*
2093c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2094c3229e05SDavid Greenman 	 */
2095c3229e05SDavid Greenman 	if (phd == NULL) {
20961ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2097c3229e05SDavid Greenman 		if (phd == NULL) {
2098c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2099c3229e05SDavid Greenman 		}
2100c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2101c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2102c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2103c3229e05SDavid Greenman 	}
2104c3229e05SDavid Greenman 	inp->inp_phd = phd;
2105c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2106c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2107111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
210852cd27cbSRobert Watson #ifdef PCBGROUP
210952cd27cbSRobert Watson 	if (do_pcbgroup_update)
211052cd27cbSRobert Watson 		in_pcbgroup_update(inp);
211152cd27cbSRobert Watson #endif
2112c3229e05SDavid Greenman 	return (0);
211315bd2b43SDavid Greenman }
211415bd2b43SDavid Greenman 
2115c3229e05SDavid Greenman /*
211652cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
211752cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
211852cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
211952cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
212052cd27cbSRobert Watson  * pcbgroup until later.
212152cd27cbSRobert Watson  *
212252cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
212352cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
212452cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
212552cd27cbSRobert Watson  */
212652cd27cbSRobert Watson int
212752cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
212852cd27cbSRobert Watson {
212952cd27cbSRobert Watson 
213052cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
213152cd27cbSRobert Watson }
213252cd27cbSRobert Watson 
213352cd27cbSRobert Watson int
213452cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
213552cd27cbSRobert Watson {
213652cd27cbSRobert Watson 
213752cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
213852cd27cbSRobert Watson }
213952cd27cbSRobert Watson 
214052cd27cbSRobert Watson /*
2141c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2142c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2143c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2144c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2145c3229e05SDavid Greenman  */
214615bd2b43SDavid Greenman void
2147d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
214815bd2b43SDavid Greenman {
214959daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
215015bd2b43SDavid Greenman 	struct inpcbhead *head;
2151cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
215215bd2b43SDavid Greenman 
21538501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2154fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2155fa046d87SRobert Watson 
2156111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2157111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2158602cc7f1SRobert Watson 
2159cfa1ca9dSYoshinobu Inoue #ifdef INET6
2160cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
21611b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2162cfa1ca9dSYoshinobu Inoue 	else
216383e521ecSBjoern A. Zeeb #endif
2164cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2165cfa1ca9dSYoshinobu Inoue 
2166712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2167712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
216815bd2b43SDavid Greenman 
2169c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
217015bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
217152cd27cbSRobert Watson 
217252cd27cbSRobert Watson #ifdef PCBGROUP
217352cd27cbSRobert Watson 	if (m != NULL)
217452cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
217552cd27cbSRobert Watson 	else
217652cd27cbSRobert Watson 		in_pcbgroup_update(inp);
217752cd27cbSRobert Watson #endif
2178c3229e05SDavid Greenman }
2179c3229e05SDavid Greenman 
2180d3c1f003SRobert Watson void
2181d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2182d3c1f003SRobert Watson {
2183d3c1f003SRobert Watson 
2184d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2185d3c1f003SRobert Watson }
2186d3c1f003SRobert Watson 
2187c3229e05SDavid Greenman /*
2188c3229e05SDavid Greenman  * Remove PCB from various lists.
2189c3229e05SDavid Greenman  */
21906d888973SRobert Watson static void
2191136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2192c3229e05SDavid Greenman {
219359daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
219459daba27SSam Leffler 
2195ff9b006dSJulien Charbon #ifdef INVARIANTS
2196ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
2197ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
2198ff9b006dSJulien Charbon 	} else {
219959daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
2200ff9b006dSJulien Charbon 	}
2201ff9b006dSJulien Charbon #endif
2202ff9b006dSJulien Charbon 
22038501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2204ff9b006dSJulien Charbon 	INP_LIST_WLOCK_ASSERT(pcbinfo);
220559daba27SSam Leffler 
220659daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2207111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2208c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2209c3229e05SDavid Greenman 
2210fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2211c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2212c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2213fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2214c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2215c3229e05SDavid Greenman 			free(phd, M_PCB);
2216c3229e05SDavid Greenman 		}
2217fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2218111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2219c3229e05SDavid Greenman 	}
2220c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
222159daba27SSam Leffler 	pcbinfo->ipi_count--;
222252cd27cbSRobert Watson #ifdef PCBGROUP
222352cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
222452cd27cbSRobert Watson #endif
222515bd2b43SDavid Greenman }
222675c13541SPoul-Henning Kamp 
2227a557af22SRobert Watson /*
2228a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2229a557af22SRobert Watson  * label change into the in_pcb for the socket.
2230a557af22SRobert Watson  */
2231a557af22SRobert Watson void
2232136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2233a557af22SRobert Watson {
2234a557af22SRobert Watson #ifdef MAC
2235a557af22SRobert Watson 	struct inpcb *inp;
2236a557af22SRobert Watson 
22374c7c478dSRobert Watson 	inp = sotoinpcb(so);
22384c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2239602cc7f1SRobert Watson 
22408501a69cSRobert Watson 	INP_WLOCK(inp);
2241310e7cebSRobert Watson 	SOCK_LOCK(so);
2242a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2243310e7cebSRobert Watson 	SOCK_UNLOCK(so);
22448501a69cSRobert Watson 	INP_WUNLOCK(inp);
2245a557af22SRobert Watson #endif
2246a557af22SRobert Watson }
22475f311da2SMike Silbersack 
22485f311da2SMike Silbersack /*
2249ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2250ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2251ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2252ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2253ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
22545f311da2SMike Silbersack  */
2255aae49dd3SBjoern A. Zeeb static void
2256136d4f1cSRobert Watson ipport_tick(void *xtp)
22575f311da2SMike Silbersack {
22588b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2259ad3a630fSRobert Watson 
22605ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
22618b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
22628b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
22638b615593SMarko Zec 		if (V_ipport_tcpallocs <=
22648b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2265603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2266603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2267ad3a630fSRobert Watson 		} else
2268603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2269603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
22708b615593SMarko Zec 		CURVNET_RESTORE();
22718b615593SMarko Zec 	}
22725ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
22735f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
22745f311da2SMike Silbersack }
2275497057eeSRobert Watson 
2276aae49dd3SBjoern A. Zeeb static void
2277aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2278aae49dd3SBjoern A. Zeeb {
2279aae49dd3SBjoern A. Zeeb 
2280aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2281aae49dd3SBjoern A. Zeeb }
2282aae49dd3SBjoern A. Zeeb 
2283aae49dd3SBjoern A. Zeeb /*
2284aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2285aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2286aae49dd3SBjoern A. Zeeb  */
2287aae49dd3SBjoern A. Zeeb static void
2288aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2289aae49dd3SBjoern A. Zeeb {
2290aae49dd3SBjoern A. Zeeb 
2291aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2292fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2293aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2294aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2295aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2296aae49dd3SBjoern A. Zeeb }
2297aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2298aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2299aae49dd3SBjoern A. Zeeb 
23003d585327SKip Macy void
23013d585327SKip Macy inp_wlock(struct inpcb *inp)
23023d585327SKip Macy {
23033d585327SKip Macy 
23048501a69cSRobert Watson 	INP_WLOCK(inp);
23053d585327SKip Macy }
23063d585327SKip Macy 
23073d585327SKip Macy void
23083d585327SKip Macy inp_wunlock(struct inpcb *inp)
23093d585327SKip Macy {
23103d585327SKip Macy 
23118501a69cSRobert Watson 	INP_WUNLOCK(inp);
23123d585327SKip Macy }
23133d585327SKip Macy 
23143d585327SKip Macy void
23153d585327SKip Macy inp_rlock(struct inpcb *inp)
23163d585327SKip Macy {
23173d585327SKip Macy 
2318a69042a5SRobert Watson 	INP_RLOCK(inp);
23193d585327SKip Macy }
23203d585327SKip Macy 
23213d585327SKip Macy void
23223d585327SKip Macy inp_runlock(struct inpcb *inp)
23233d585327SKip Macy {
23243d585327SKip Macy 
2325a69042a5SRobert Watson 	INP_RUNLOCK(inp);
23263d585327SKip Macy }
23273d585327SKip Macy 
23283d585327SKip Macy #ifdef INVARIANTS
23293d585327SKip Macy void
2330e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
23313d585327SKip Macy {
23323d585327SKip Macy 
23338501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
23343d585327SKip Macy }
23353d585327SKip Macy 
23363d585327SKip Macy void
2337e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
23383d585327SKip Macy {
23393d585327SKip Macy 
23403d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
23413d585327SKip Macy }
23423d585327SKip Macy #endif
23433d585327SKip Macy 
23449378e437SKip Macy void
23459378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
23469378e437SKip Macy {
23479378e437SKip Macy 	struct inpcb *inp;
23489378e437SKip Macy 
2349ff9b006dSJulien Charbon 	INP_INFO_WLOCK(&V_tcbinfo);
235097021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
23519378e437SKip Macy 		INP_WLOCK(inp);
23529378e437SKip Macy 		func(inp, arg);
23539378e437SKip Macy 		INP_WUNLOCK(inp);
23549378e437SKip Macy 	}
2355ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(&V_tcbinfo);
23569378e437SKip Macy }
23579378e437SKip Macy 
23589378e437SKip Macy struct socket *
23599378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
23609378e437SKip Macy {
23619378e437SKip Macy 
23629378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23639378e437SKip Macy 	return (inp->inp_socket);
23649378e437SKip Macy }
23659378e437SKip Macy 
23669378e437SKip Macy struct tcpcb *
23679378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
23689378e437SKip Macy {
23699378e437SKip Macy 
23709378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23719378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
23729378e437SKip Macy }
23739378e437SKip Macy 
23749378e437SKip Macy int
23759378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
23769378e437SKip Macy {
23779378e437SKip Macy 
23789378e437SKip Macy 	return (inp->inp_ip_tos);
23799378e437SKip Macy }
23809378e437SKip Macy 
23819378e437SKip Macy void
23829378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
23839378e437SKip Macy {
23849378e437SKip Macy 
23859378e437SKip Macy 	inp->inp_ip_tos = val;
23869378e437SKip Macy }
23879378e437SKip Macy 
23889378e437SKip Macy void
2389df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
23909d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
23919378e437SKip Macy {
23929378e437SKip Macy 
23939d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2394df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2395df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
23969378e437SKip Macy 	*lp = inp->inp_lport;
23979378e437SKip Macy 	*fp = inp->inp_fport;
23989378e437SKip Macy }
23999378e437SKip Macy 
2400dd0e6c38SKip Macy struct inpcb *
2401dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2402dd0e6c38SKip Macy {
2403dd0e6c38SKip Macy 
2404dd0e6c38SKip Macy 	return (sotoinpcb(so));
2405dd0e6c38SKip Macy }
2406dd0e6c38SKip Macy 
2407dd0e6c38SKip Macy struct tcpcb *
2408dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2409dd0e6c38SKip Macy {
2410dd0e6c38SKip Macy 
2411dd0e6c38SKip Macy 	return (sototcpcb(so));
2412dd0e6c38SKip Macy }
2413dd0e6c38SKip Macy 
2414497057eeSRobert Watson #ifdef DDB
2415497057eeSRobert Watson static void
2416497057eeSRobert Watson db_print_indent(int indent)
2417497057eeSRobert Watson {
2418497057eeSRobert Watson 	int i;
2419497057eeSRobert Watson 
2420497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2421497057eeSRobert Watson 		db_printf(" ");
2422497057eeSRobert Watson }
2423497057eeSRobert Watson 
2424497057eeSRobert Watson static void
2425497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2426497057eeSRobert Watson {
2427497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2428497057eeSRobert Watson 
2429497057eeSRobert Watson 	db_print_indent(indent);
2430497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2431497057eeSRobert Watson 
2432497057eeSRobert Watson 	indent += 2;
2433497057eeSRobert Watson 
243403dc38a4SRobert Watson #ifdef INET6
2435dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2436497057eeSRobert Watson 		/* IPv6. */
2437497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2438497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
243983e521ecSBjoern A. Zeeb 	} else
244003dc38a4SRobert Watson #endif
244183e521ecSBjoern A. Zeeb 	{
2442497057eeSRobert Watson 		/* IPv4. */
2443497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2444497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2445497057eeSRobert Watson 	}
2446497057eeSRobert Watson 	db_print_indent(indent);
2447497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2448497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2449497057eeSRobert Watson 	db_print_indent(indent);
2450497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2451497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2452497057eeSRobert Watson }
2453497057eeSRobert Watson 
2454497057eeSRobert Watson static void
2455497057eeSRobert Watson db_print_inpflags(int inp_flags)
2456497057eeSRobert Watson {
2457497057eeSRobert Watson 	int comma;
2458497057eeSRobert Watson 
2459497057eeSRobert Watson 	comma = 0;
2460497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2461497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2462497057eeSRobert Watson 		comma = 1;
2463497057eeSRobert Watson 	}
2464497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2465497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2466497057eeSRobert Watson 		comma = 1;
2467497057eeSRobert Watson 	}
2468497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2469497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2470497057eeSRobert Watson 		comma = 1;
2471497057eeSRobert Watson 	}
2472497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2473497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2474497057eeSRobert Watson 		comma = 1;
2475497057eeSRobert Watson 	}
2476497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2477497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2478497057eeSRobert Watson 		comma = 1;
2479497057eeSRobert Watson 	}
2480497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2481497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2482497057eeSRobert Watson 		comma = 1;
2483497057eeSRobert Watson 	}
2484497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2485497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2486497057eeSRobert Watson 		comma = 1;
2487497057eeSRobert Watson 	}
2488497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2489497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2490497057eeSRobert Watson 		comma = 1;
2491497057eeSRobert Watson 	}
2492497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2493497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2494497057eeSRobert Watson 		comma = 1;
2495497057eeSRobert Watson 	}
2496497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2497497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2498497057eeSRobert Watson 		comma = 1;
2499497057eeSRobert Watson 	}
2500497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2501497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2502497057eeSRobert Watson 		comma = 1;
2503497057eeSRobert Watson 	}
25043cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
25053cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
25063cca425bSMichael Tuexen 		comma = 1;
25073cca425bSMichael Tuexen 	}
2508497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2509497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2510497057eeSRobert Watson 		comma = 1;
2511497057eeSRobert Watson 	}
2512497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2513497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2514497057eeSRobert Watson 		comma = 1;
2515497057eeSRobert Watson 	}
2516497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2517497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2518497057eeSRobert Watson 		comma = 1;
2519497057eeSRobert Watson 	}
2520497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2521497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2522497057eeSRobert Watson 		comma = 1;
2523497057eeSRobert Watson 	}
2524497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2525497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2526497057eeSRobert Watson 		comma = 1;
2527497057eeSRobert Watson 	}
2528497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2529497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2530497057eeSRobert Watson 		comma = 1;
2531497057eeSRobert Watson 	}
2532497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2533497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2534497057eeSRobert Watson 		comma = 1;
2535497057eeSRobert Watson 	}
2536497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2537497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2538497057eeSRobert Watson 		comma = 1;
2539497057eeSRobert Watson 	}
2540497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2541497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2542497057eeSRobert Watson 		comma = 1;
2543497057eeSRobert Watson 	}
2544ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2545ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2546ad71fe3cSRobert Watson 		comma  = 1;
2547ad71fe3cSRobert Watson 	}
2548ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2549ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2550ad71fe3cSRobert Watson 		comma  = 1;
2551ad71fe3cSRobert Watson 	}
2552ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2553ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2554ad71fe3cSRobert Watson 		comma  = 1;
2555ad71fe3cSRobert Watson 	}
2556ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2557ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2558ad71fe3cSRobert Watson 		comma  = 1;
2559ad71fe3cSRobert Watson 	}
2560497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2561497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2562497057eeSRobert Watson 		comma = 1;
2563497057eeSRobert Watson 	}
2564497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2565497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2566497057eeSRobert Watson 		comma = 1;
2567497057eeSRobert Watson 	}
2568497057eeSRobert Watson }
2569497057eeSRobert Watson 
2570497057eeSRobert Watson static void
2571497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2572497057eeSRobert Watson {
2573497057eeSRobert Watson 	int comma;
2574497057eeSRobert Watson 
2575497057eeSRobert Watson 	comma = 0;
2576497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2577497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2578497057eeSRobert Watson 		comma  = 1;
2579497057eeSRobert Watson 	}
2580497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2581497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2582497057eeSRobert Watson 		comma  = 1;
2583497057eeSRobert Watson 	}
2584497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2585497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2586497057eeSRobert Watson 		comma  = 1;
2587497057eeSRobert Watson 	}
2588497057eeSRobert Watson }
2589497057eeSRobert Watson 
25906d888973SRobert Watson static void
2591497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2592497057eeSRobert Watson {
2593497057eeSRobert Watson 
2594497057eeSRobert Watson 	db_print_indent(indent);
2595497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2596497057eeSRobert Watson 
2597497057eeSRobert Watson 	indent += 2;
2598497057eeSRobert Watson 
2599497057eeSRobert Watson 	db_print_indent(indent);
2600497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2601497057eeSRobert Watson 
2602497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2603497057eeSRobert Watson 
2604497057eeSRobert Watson 	db_print_indent(indent);
2605497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2606497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2607497057eeSRobert Watson 
2608497057eeSRobert Watson 	db_print_indent(indent);
2609497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2610497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2611497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2612497057eeSRobert Watson 	db_printf(")\n");
2613497057eeSRobert Watson 
2614497057eeSRobert Watson 	db_print_indent(indent);
2615497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2616497057eeSRobert Watson 	    inp->inp_vflag);
2617497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2618497057eeSRobert Watson 	db_printf(")\n");
2619497057eeSRobert Watson 
2620497057eeSRobert Watson 	db_print_indent(indent);
2621497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2622497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2623497057eeSRobert Watson 
2624497057eeSRobert Watson 	db_print_indent(indent);
2625497057eeSRobert Watson #ifdef INET6
2626497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2627497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2628497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2629497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2630497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2631497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2632497057eeSRobert Watson 		    inp->in6p_hops);
2633497057eeSRobert Watson 	} else
2634497057eeSRobert Watson #endif
2635497057eeSRobert Watson 	{
2636497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2637497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2638497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2639497057eeSRobert Watson 	}
2640497057eeSRobert Watson 
2641497057eeSRobert Watson 	db_print_indent(indent);
2642497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2643497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2644497057eeSRobert Watson }
2645497057eeSRobert Watson 
2646497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2647497057eeSRobert Watson {
2648497057eeSRobert Watson 	struct inpcb *inp;
2649497057eeSRobert Watson 
2650497057eeSRobert Watson 	if (!have_addr) {
2651497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2652497057eeSRobert Watson 		return;
2653497057eeSRobert Watson 	}
2654497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2655497057eeSRobert Watson 
2656497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2657497057eeSRobert Watson }
265883e521ecSBjoern A. Zeeb #endif /* DDB */
2659