xref: /freebsd/sys/netinet/in_pcb.c (revision cc0a3c8ca4561297b4df88fec347ba004d04277c)
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>
50*cc0a3c8cSAndrey 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>
54cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
55df8bae1dSRodney W. Grimes #include <sys/protosw.h>
56*cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
57df8bae1dSRodney W. Grimes #include <sys/socket.h>
58df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
59acd3428bSRobert Watson #include <sys/priv.h>
60df8bae1dSRodney W. Grimes #include <sys/proc.h>
6179bdc6e5SRobert Watson #include <sys/refcount.h>
6275c13541SPoul-Henning Kamp #include <sys/jail.h>
63101f9fc8SPeter Wemm #include <sys/kernel.h>
64101f9fc8SPeter Wemm #include <sys/sysctl.h>
658781d8e9SBruce Evans 
66497057eeSRobert Watson #ifdef DDB
67497057eeSRobert Watson #include <ddb/ddb.h>
68497057eeSRobert Watson #endif
69497057eeSRobert Watson 
7069c2d429SJeff Roberson #include <vm/uma.h>
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes #include <net/if.h>
7376039bc8SGleb Smirnoff #include <net/if_var.h>
74cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
75df8bae1dSRodney W. Grimes #include <net/route.h>
76b2bdc62aSAdrian Chadd #include <net/rss_config.h>
77530c0060SRobert Watson #include <net/vnet.h>
78df8bae1dSRodney W. Grimes 
7967107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
80df8bae1dSRodney W. Grimes #include <netinet/in.h>
81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
82df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
83340c35deSJonathan Lemon #include <netinet/tcp_var.h>
845f311da2SMike Silbersack #include <netinet/udp.h>
855f311da2SMike Silbersack #include <netinet/udp_var.h>
8667107f45SBjoern A. Zeeb #endif
8767107f45SBjoern A. Zeeb #ifdef INET
8867107f45SBjoern A. Zeeb #include <netinet/in_var.h>
8967107f45SBjoern A. Zeeb #endif
90cfa1ca9dSYoshinobu Inoue #ifdef INET6
91cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
92efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
9367107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
9467107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
95cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
96cfa1ca9dSYoshinobu Inoue 
97df8bae1dSRodney W. Grimes 
98b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
99b9234fafSSam Leffler #include <netipsec/ipsec.h>
100b9234fafSSam Leffler #include <netipsec/key.h>
101b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
102b9234fafSSam Leffler 
103aed55708SRobert Watson #include <security/mac/mac_framework.h>
104aed55708SRobert Watson 
105aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
106aae49dd3SBjoern A. Zeeb 
107101f9fc8SPeter Wemm /*
108101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
109101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
110101f9fc8SPeter Wemm  */
111eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
112eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
115eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
116eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
117101f9fc8SPeter Wemm 
118b0d22693SCrist J. Clark /*
119b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
120b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
121b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
122b0d22693SCrist J. Clark  */
123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
125b0d22693SCrist J. Clark 
1265f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
130eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
131eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1323e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount);
133eddfbb76SRobert Watson 
1341e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1356ac48b74SMike Silbersack 
136d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
137d2025bd0SBjoern A. Zeeb #ifdef INET
138fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
139fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
140fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
141fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
14267107f45SBjoern A. Zeeb 
143bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
144bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
145bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
146bbd42ad0SPeter Wemm 
147bbd42ad0SPeter Wemm static int
14882d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
149bbd42ad0SPeter Wemm {
15030a4ab08SBruce Evans 	int error;
15130a4ab08SBruce Evans 
152f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
15330a4ab08SBruce Evans 	if (error == 0) {
154603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
155603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
156603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
157603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
158603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
159603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
160bbd42ad0SPeter Wemm 	}
16130a4ab08SBruce Evans 	return (error);
162bbd42ad0SPeter Wemm }
163bbd42ad0SPeter Wemm 
164bbd42ad0SPeter Wemm #undef RANGECHK
165bbd42ad0SPeter Wemm 
1666472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
1676472ac3dSEd Schouten     "IP Ports");
16833b3ac06SPeter Wemm 
1696df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1706df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1716df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
1726df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1736df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1746df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
1756df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1766df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1776df8a710SGleb Smirnoff 	&VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
1786df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1796df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1806df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
1816df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
1826df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1836df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
1846df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
1856df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1866df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
1876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
1886df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
1896df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
191eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
1936df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
194eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
1956df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
1966df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
197eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
1986ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
2006df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
201eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
2028b615593SMarko Zec 	"Minimum time to keep sequental port "
2036ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
20483e521ecSBjoern A. Zeeb #endif /* INET */
2050312fbe9SPoul-Henning Kamp 
206c3229e05SDavid Greenman /*
207c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
208c3229e05SDavid Greenman  *
209de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
210de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
211de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
212c3229e05SDavid Greenman  */
213c3229e05SDavid Greenman 
214c3229e05SDavid Greenman /*
2159bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
2169bcd427bSRobert Watson  * arguments in time.
2179bcd427bSRobert Watson  */
2189bcd427bSRobert Watson void
2199bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
2209bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
2219bcd427bSRobert Watson     char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
22252cd27cbSRobert Watson     uint32_t inpcbzone_flags, u_int hashfields)
2239bcd427bSRobert Watson {
2249bcd427bSRobert Watson 
2259bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
226fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
2279bcd427bSRobert Watson #ifdef VIMAGE
2289bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
2299bcd427bSRobert Watson #endif
2309bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
2319bcd427bSRobert Watson 	LIST_INIT(pcbinfo->ipi_listhead);
232fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
2339bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
2349bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
2359bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
2369bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
23752cd27cbSRobert Watson #ifdef PCBGROUP
23852cd27cbSRobert Watson 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
23952cd27cbSRobert Watson #endif
2409bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
2419bcd427bSRobert Watson 	    NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
2429bcd427bSRobert Watson 	    inpcbzone_flags);
2439bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
2446acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
2456acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
2469bcd427bSRobert Watson }
2479bcd427bSRobert Watson 
2489bcd427bSRobert Watson /*
2499bcd427bSRobert Watson  * Destroy an inpcbinfo.
2509bcd427bSRobert Watson  */
2519bcd427bSRobert Watson void
2529bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
2539bcd427bSRobert Watson {
2549bcd427bSRobert Watson 
255fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
256fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
257fa046d87SRobert Watson 
2589bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
2599bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
2609bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
26152cd27cbSRobert Watson #ifdef PCBGROUP
26252cd27cbSRobert Watson 	in_pcbgroup_destroy(pcbinfo);
26352cd27cbSRobert Watson #endif
2649bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
265fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
2669bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
2679bcd427bSRobert Watson }
2689bcd427bSRobert Watson 
2699bcd427bSRobert Watson /*
270c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
271d915b280SStephan Uphoff  * On success return with the PCB locked.
272c3229e05SDavid Greenman  */
273df8bae1dSRodney W. Grimes int
274d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
275df8bae1dSRodney W. Grimes {
276136d4f1cSRobert Watson 	struct inpcb *inp;
27713cf67f3SHajimu UMEMOTO 	int error;
278a557af22SRobert Watson 
27959daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
280a557af22SRobert Watson 	error = 0;
281d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
282df8bae1dSRodney W. Grimes 	if (inp == NULL)
283df8bae1dSRodney W. Grimes 		return (ENOBUFS);
284d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
28515bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
286df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
28786d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
2888b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
289a557af22SRobert Watson #ifdef MAC
29030d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
291a557af22SRobert Watson 	if (error != 0)
292a557af22SRobert Watson 		goto out;
29330d239bcSRobert Watson 	mac_inpcb_create(so, inp);
294a557af22SRobert Watson #endif
295b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
29613cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
2970bffde27SRobert Watson 	if (error != 0) {
2980bffde27SRobert Watson #ifdef MAC
2990bffde27SRobert Watson 		mac_inpcb_destroy(inp);
3000bffde27SRobert Watson #endif
301a557af22SRobert Watson 		goto out;
3020bffde27SRobert Watson 	}
303b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
304e3fd5ffdSRobert Watson #ifdef INET6
305340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
306340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
307603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
30833841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
309340c35deSJonathan Lemon 	}
31075daea93SPaul Saab #endif
311712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
3123d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
313df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
31433841545SHajimu UMEMOTO #ifdef INET6
315603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
31633841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
31733841545SHajimu UMEMOTO #endif
3188501a69cSRobert Watson 	INP_WLOCK(inp);
319d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
32079bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
321b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
322a557af22SRobert Watson out:
32386d02c5cSBjoern A. Zeeb 	if (error != 0) {
32486d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
325a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
32686d02c5cSBjoern A. Zeeb 	}
327a557af22SRobert Watson #endif
328a557af22SRobert Watson 	return (error);
329df8bae1dSRodney W. Grimes }
330df8bae1dSRodney W. Grimes 
33167107f45SBjoern A. Zeeb #ifdef INET
332df8bae1dSRodney W. Grimes int
333136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
334df8bae1dSRodney W. Grimes {
3354b932371SIan Dowse 	int anonport, error;
3364b932371SIan Dowse 
3378501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
338fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
33959daba27SSam Leffler 
3404b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
3414b932371SIan Dowse 		return (EINVAL);
3425cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
3434b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
344b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
3454b932371SIan Dowse 	if (error)
3464b932371SIan Dowse 		return (error);
3474b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
3484b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
3494b932371SIan Dowse 		inp->inp_lport = 0;
3504b932371SIan Dowse 		return (EAGAIN);
3514b932371SIan Dowse 	}
3524b932371SIan Dowse 	if (anonport)
3534b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
3544b932371SIan Dowse 	return (0);
3554b932371SIan Dowse }
35667107f45SBjoern A. Zeeb #endif
3574b932371SIan Dowse 
35839c8c62eSHiren Panchasara /*
35939c8c62eSHiren Panchasara  * Select a local port (number) to use.
36039c8c62eSHiren Panchasara  */
361efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
362efc76f72SBjoern A. Zeeb int
363efc76f72SBjoern A. Zeeb in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
36468e0d7e0SRobert Watson     struct ucred *cred, int lookupflags)
365efc76f72SBjoern A. Zeeb {
366efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
367efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
368efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
369efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
370efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
371efc76f72SBjoern A. Zeeb #ifdef INET
372efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
373efc76f72SBjoern A. Zeeb #endif
374efc76f72SBjoern A. Zeeb 
375efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
376efc76f72SBjoern A. Zeeb 
377efc76f72SBjoern A. Zeeb 	/*
378efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
379efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
380efc76f72SBjoern A. Zeeb 	 */
381efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
382fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
383efc76f72SBjoern A. Zeeb 
384efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
385efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
386efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
387efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
388efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
389efc76f72SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
390efc76f72SBjoern A. Zeeb 		if (error)
391efc76f72SBjoern A. Zeeb 			return (error);
392efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
393efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
394efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
395efc76f72SBjoern A. Zeeb 	} else {
396efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
397efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
398efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
399efc76f72SBjoern A. Zeeb 	}
400efc76f72SBjoern A. Zeeb 	/*
401e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
402efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
403efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
404efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
405efc76f72SBjoern A. Zeeb 	 */
406efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
407e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
408e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
409efc76f72SBjoern A. Zeeb 		dorandom = 1;
410efc76f72SBjoern A. Zeeb 	else
411efc76f72SBjoern A. Zeeb 		dorandom = 0;
412efc76f72SBjoern A. Zeeb 	/*
413efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
414efc76f72SBjoern A. Zeeb 	 * we have the only port available.
415efc76f72SBjoern A. Zeeb 	 */
416efc76f72SBjoern A. Zeeb 	if (first == last)
417efc76f72SBjoern A. Zeeb 		dorandom = 0;
418e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
419e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
420efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
421efc76f72SBjoern A. Zeeb 	/*
422efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
423efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
424efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
425efc76f72SBjoern A. Zeeb 	 */
426efc76f72SBjoern A. Zeeb 	if (first > last) {
427efc76f72SBjoern A. Zeeb 		aux = first;
428efc76f72SBjoern A. Zeeb 		first = last;
429efc76f72SBjoern A. Zeeb 		last = aux;
430efc76f72SBjoern A. Zeeb 	}
431efc76f72SBjoern A. Zeeb 
432efc76f72SBjoern A. Zeeb #ifdef INET
433efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
434efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
4354d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
436efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
437efc76f72SBjoern A. Zeeb 		    __func__, inp));
438efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
439efc76f72SBjoern A. Zeeb 	}
440efc76f72SBjoern A. Zeeb #endif
44167107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
442efc76f72SBjoern A. Zeeb 	lport = *lportp;
443efc76f72SBjoern A. Zeeb 
444efc76f72SBjoern A. Zeeb 	if (dorandom)
445efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
446efc76f72SBjoern A. Zeeb 
447efc76f72SBjoern A. Zeeb 	count = last - first;
448efc76f72SBjoern A. Zeeb 
449efc76f72SBjoern A. Zeeb 	do {
450efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
451efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
452efc76f72SBjoern A. Zeeb 		++*lastport;
453efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
454efc76f72SBjoern A. Zeeb 			*lastport = first;
455efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
456efc76f72SBjoern A. Zeeb 
457efc76f72SBjoern A. Zeeb #ifdef INET6
458efc76f72SBjoern A. Zeeb 		if ((inp->inp_vflag & INP_IPV6) != 0)
459efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
46068e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
461efc76f72SBjoern A. Zeeb #endif
462efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
463efc76f72SBjoern A. Zeeb 		else
464efc76f72SBjoern A. Zeeb #endif
465efc76f72SBjoern A. Zeeb #ifdef INET
466efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
46768e0d7e0SRobert Watson 			    lport, lookupflags, cred);
468efc76f72SBjoern A. Zeeb #endif
469efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
470efc76f72SBjoern A. Zeeb 
471efc76f72SBjoern A. Zeeb #ifdef INET
4724d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
473efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
474efc76f72SBjoern A. Zeeb #endif
475efc76f72SBjoern A. Zeeb 	*lportp = lport;
476efc76f72SBjoern A. Zeeb 
477efc76f72SBjoern A. Zeeb 	return (0);
478efc76f72SBjoern A. Zeeb }
479efdf104bSMikolaj Golub 
480efdf104bSMikolaj Golub /*
481efdf104bSMikolaj Golub  * Return cached socket options.
482efdf104bSMikolaj Golub  */
483efdf104bSMikolaj Golub short
484efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
485efdf104bSMikolaj Golub {
486efdf104bSMikolaj Golub    short so_options;
487efdf104bSMikolaj Golub 
488efdf104bSMikolaj Golub    so_options = 0;
489efdf104bSMikolaj Golub 
490efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
491efdf104bSMikolaj Golub 	   so_options |= SO_REUSEPORT;
492efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
493efdf104bSMikolaj Golub 	   so_options |= SO_REUSEADDR;
494efdf104bSMikolaj Golub    return (so_options);
495efdf104bSMikolaj Golub }
496efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
497efc76f72SBjoern A. Zeeb 
4984b932371SIan Dowse /*
4990a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
5000a100a6fSAdrian Chadd  *
5010a100a6fSAdrian Chadd  * ni points to the new inp.
5020a100a6fSAdrian Chadd  * oi points to the exisitng inp.
5030a100a6fSAdrian Chadd  *
5040a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
5050a100a6fSAdrian Chadd  * whether the credentials match.
5060a100a6fSAdrian Chadd  */
507d5bb8bd3SAdrian Chadd int
5080a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
5090a100a6fSAdrian Chadd {
5100a100a6fSAdrian Chadd 	/* Check permissions match */
5110a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5120a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
5130a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
5140a100a6fSAdrian Chadd 		return (0);
5150a100a6fSAdrian Chadd 
5160a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
5170a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5180a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
5190a100a6fSAdrian Chadd 		return (0);
5200a100a6fSAdrian Chadd 
5210a100a6fSAdrian Chadd 	/*
5220a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
5230a100a6fSAdrian Chadd 	 * it is and it matches the checks.
5240a100a6fSAdrian Chadd 	 */
5250a100a6fSAdrian Chadd 	return (1);
5260a100a6fSAdrian Chadd }
5270a100a6fSAdrian Chadd 
528d5bb8bd3SAdrian Chadd #ifdef INET
5290a100a6fSAdrian Chadd /*
5304b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
5314b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
5324b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
5334b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
5344b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
5354b932371SIan Dowse  *
5364b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
5374b932371SIan Dowse  */
5384b932371SIan Dowse int
539136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
540136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
5414b932371SIan Dowse {
5424b932371SIan Dowse 	struct socket *so = inp->inp_socket;
54315bd2b43SDavid Greenman 	struct sockaddr_in *sin;
544c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
5454b932371SIan Dowse 	struct in_addr laddr;
546df8bae1dSRodney W. Grimes 	u_short lport = 0;
54768e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
548413628a7SBjoern A. Zeeb 	int error;
549df8bae1dSRodney W. Grimes 
5508501a69cSRobert Watson 	/*
551fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
5528501a69cSRobert Watson 	 */
55359daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
554fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
55559daba27SSam Leffler 
556603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
557df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5584b932371SIan Dowse 	laddr.s_addr = *laddrp;
5594b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
560df8bae1dSRodney W. Grimes 		return (EINVAL);
561c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
56268e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
5637c2f3cb9SJamie Gritton 	if (nam == NULL) {
5647c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
5657c2f3cb9SJamie Gritton 			return (error);
5667c2f3cb9SJamie Gritton 	} else {
56757bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
56857bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
569df8bae1dSRodney W. Grimes 			return (EINVAL);
570df8bae1dSRodney W. Grimes #ifdef notdef
571df8bae1dSRodney W. Grimes 		/*
572df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
573df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
574df8bae1dSRodney W. Grimes 		 */
575df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
576df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
577df8bae1dSRodney W. Grimes #endif
578b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
579b89e82ddSJamie Gritton 		if (error)
580b89e82ddSJamie Gritton 			return (error);
5814b932371SIan Dowse 		if (sin->sin_port != *lportp) {
5824b932371SIan Dowse 			/* Don't allow the port to change. */
5834b932371SIan Dowse 			if (*lportp != 0)
5844b932371SIan Dowse 				return (EINVAL);
585df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
5864b932371SIan Dowse 		}
5874b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
588df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
589df8bae1dSRodney W. Grimes 			/*
590df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
591df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
592df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
593df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
594df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
595df8bae1dSRodney W. Grimes 			 */
596f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
597df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
598df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
599df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
60083103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
6014209e01aSAdrian Chadd 			/*
6024209e01aSAdrian Chadd 			 * Is the address a local IP address?
603f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
6048696873dSAdrian Chadd 			 * to any endpoint address, local or not.
6054209e01aSAdrian Chadd 			 */
606f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
6078896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
608df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
609df8bae1dSRodney W. Grimes 		}
6104b932371SIan Dowse 		laddr = sin->sin_addr;
611df8bae1dSRodney W. Grimes 		if (lport) {
612df8bae1dSRodney W. Grimes 			struct inpcb *t;
613ae0e7143SRobert Watson 			struct tcptw *tw;
614ae0e7143SRobert Watson 
615df8bae1dSRodney W. Grimes 			/* GROSS */
616603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
617603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
618acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
61932f9753cSRobert Watson 			    0))
6202469dd60SGarrett Wollman 				return (EACCES);
621835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
62286d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
62332f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
624078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
625413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
626340c35deSJonathan Lemon 	/*
627340c35deSJonathan Lemon 	 * XXX
628340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
629340c35deSJonathan Lemon 	 */
6304cc20ab1SSeigo Tanimura 				if (t &&
6310a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
632ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
6334658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
6344658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
6354cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
63652b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
637fc06cd42SMikolaj Golub 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
63886d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
63986d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
6404049a042SGuido van Rooij 					return (EADDRINUSE);
6410a100a6fSAdrian Chadd 
6420a100a6fSAdrian Chadd 				/*
6430a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
6440a100a6fSAdrian Chadd 				 * the credentials need to match and the
6450a100a6fSAdrian Chadd 				 * original socket also has to have been bound
6460a100a6fSAdrian Chadd 				 * with BINDMULTI.
6470a100a6fSAdrian Chadd 				 */
6480a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6490a100a6fSAdrian Chadd 					return (EADDRINUSE);
6504049a042SGuido van Rooij 			}
651c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
65268e0d7e0SRobert Watson 			    lport, lookupflags, cred);
653ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
654ae0e7143SRobert Watson 				/*
655ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
656ae0e7143SRobert Watson 				 * state recycled, we treat the address as
657ae0e7143SRobert Watson 				 * being in use (for now).  This is better
658ae0e7143SRobert Watson 				 * than a panic, but not desirable.
659ae0e7143SRobert Watson 				 */
660ec95b709SMikolaj Golub 				tw = intotw(t);
661ae0e7143SRobert Watson 				if (tw == NULL ||
662ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
663340c35deSJonathan Lemon 					return (EADDRINUSE);
6640a100a6fSAdrian Chadd 			} else if (t &&
6650a100a6fSAdrian Chadd 			    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
6660a100a6fSAdrian Chadd 			    (reuseport & inp_so_options(t)) == 0) {
667e3fd5ffdSRobert Watson #ifdef INET6
66833841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
669cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
670cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
671cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
672fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
673fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
674e3fd5ffdSRobert Watson #endif
675df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
6760a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6770a100a6fSAdrian Chadd 					return (EADDRINUSE);
678df8bae1dSRodney W. Grimes 			}
679cfa1ca9dSYoshinobu Inoue 		}
680df8bae1dSRodney W. Grimes 	}
6814b932371SIan Dowse 	if (*lportp != 0)
6824b932371SIan Dowse 		lport = *lportp;
68333b3ac06SPeter Wemm 	if (lport == 0) {
68468e0d7e0SRobert Watson 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
685efc76f72SBjoern A. Zeeb 		if (error != 0)
686efc76f72SBjoern A. Zeeb 			return (error);
68733b3ac06SPeter Wemm 
68833b3ac06SPeter Wemm 	}
6894b932371SIan Dowse 	*laddrp = laddr.s_addr;
6904b932371SIan Dowse 	*lportp = lport;
691df8bae1dSRodney W. Grimes 	return (0);
692df8bae1dSRodney W. Grimes }
693df8bae1dSRodney W. Grimes 
694999f1343SGarrett Wollman /*
6955200e00eSIan Dowse  * Connect from a socket to a specified address.
6965200e00eSIan Dowse  * Both address and port must be specified in argument sin.
6975200e00eSIan Dowse  * If don't have a local address for this socket yet,
6985200e00eSIan Dowse  * then pick one.
699999f1343SGarrett Wollman  */
700999f1343SGarrett Wollman int
701d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
702d3c1f003SRobert Watson     struct ucred *cred, struct mbuf *m)
703999f1343SGarrett Wollman {
7045200e00eSIan Dowse 	u_short lport, fport;
7055200e00eSIan Dowse 	in_addr_t laddr, faddr;
7065200e00eSIan Dowse 	int anonport, error;
707df8bae1dSRodney W. Grimes 
7088501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
709fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
71027f74fd0SRobert Watson 
7115200e00eSIan Dowse 	lport = inp->inp_lport;
7125200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
7135200e00eSIan Dowse 	anonport = (lport == 0);
7145200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
715b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
7165200e00eSIan Dowse 	if (error)
7175200e00eSIan Dowse 		return (error);
7185200e00eSIan Dowse 
7195200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
7205200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
7215200e00eSIan Dowse 		inp->inp_lport = lport;
7225200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
7235200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
7245200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
7255200e00eSIan Dowse 			inp->inp_lport = 0;
7265200e00eSIan Dowse 			return (EAGAIN);
7275200e00eSIan Dowse 		}
7285200e00eSIan Dowse 	}
7295200e00eSIan Dowse 
7305200e00eSIan Dowse 	/* Commit the remaining changes. */
7315200e00eSIan Dowse 	inp->inp_lport = lport;
7325200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
7335200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
7345200e00eSIan Dowse 	inp->inp_fport = fport;
735d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, m);
7362cb64cb2SGeorge V. Neville-Neil 
7375200e00eSIan Dowse 	if (anonport)
7385200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
7395200e00eSIan Dowse 	return (0);
7405200e00eSIan Dowse }
7415200e00eSIan Dowse 
742d3c1f003SRobert Watson int
743d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
744d3c1f003SRobert Watson {
745d3c1f003SRobert Watson 
746d3c1f003SRobert Watson 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
747d3c1f003SRobert Watson }
748d3c1f003SRobert Watson 
7495200e00eSIan Dowse /*
7500895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
7510895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
7520895aec3SBjoern A. Zeeb  */
753ae190832SSteven Hartland int
7540895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
7550895aec3SBjoern A. Zeeb     struct ucred *cred)
7560895aec3SBjoern A. Zeeb {
7570895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
7580895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
7590895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
7600895aec3SBjoern A. Zeeb 	struct route sro;
7610895aec3SBjoern A. Zeeb 	int error;
7620895aec3SBjoern A. Zeeb 
763413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
7640895aec3SBjoern A. Zeeb 
765592bcae8SBjoern A. Zeeb 	/*
766592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
767592bcae8SBjoern A. Zeeb 	 * if requested.
768592bcae8SBjoern A. Zeeb 	 */
769592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
770592bcae8SBjoern A. Zeeb 		return (0);
771592bcae8SBjoern A. Zeeb 
7720895aec3SBjoern A. Zeeb 	error = 0;
7730895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
7740895aec3SBjoern A. Zeeb 
7750895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
7760895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
7770895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
7780895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
7790895aec3SBjoern A. Zeeb 
7800895aec3SBjoern A. Zeeb 	/*
7810895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
7820895aec3SBjoern A. Zeeb 	 * else punt.
7830895aec3SBjoern A. Zeeb 	 *
7840895aec3SBjoern A. Zeeb 	 * Find out route to destination.
7850895aec3SBjoern A. Zeeb 	 */
7860895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
7876e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
7880895aec3SBjoern A. Zeeb 
7890895aec3SBjoern A. Zeeb 	/*
7900895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
7910895aec3SBjoern A. Zeeb 	 * the outgoing interface.
7920895aec3SBjoern A. Zeeb 	 *
7930895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
7940895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
7950895aec3SBjoern A. Zeeb 	 * the source address from.
7960895aec3SBjoern A. Zeeb 	 */
7970895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
7988c0fec80SRobert Watson 		struct in_ifaddr *ia;
7990895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
8000895aec3SBjoern A. Zeeb 
8014f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
80258a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
8030895aec3SBjoern A. Zeeb 		if (ia == NULL)
8044f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
80558a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
8060895aec3SBjoern A. Zeeb 		if (ia == NULL) {
8070895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
8080895aec3SBjoern A. Zeeb 			goto done;
8090895aec3SBjoern A. Zeeb 		}
8100895aec3SBjoern A. Zeeb 
8110304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8120895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8138c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8140895aec3SBjoern A. Zeeb 			goto done;
8150895aec3SBjoern A. Zeeb 		}
8160895aec3SBjoern A. Zeeb 
8170895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
8188c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
8190895aec3SBjoern A. Zeeb 		ia = NULL;
820137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8210895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8220895aec3SBjoern A. Zeeb 
8230895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8240895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8250895aec3SBjoern A. Zeeb 				continue;
8260895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
827b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8280895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8290895aec3SBjoern A. Zeeb 				break;
8300895aec3SBjoern A. Zeeb 			}
8310895aec3SBjoern A. Zeeb 		}
8320895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8330895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
834137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8350895aec3SBjoern A. Zeeb 			goto done;
8360895aec3SBjoern A. Zeeb 		}
837137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8380895aec3SBjoern A. Zeeb 
8390895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
840b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8410895aec3SBjoern A. Zeeb 		goto done;
8420895aec3SBjoern A. Zeeb 	}
8430895aec3SBjoern A. Zeeb 
8440895aec3SBjoern A. Zeeb 	/*
8450895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
8460895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
8470895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
8480895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
8490895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
8500895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
8510895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
8520895aec3SBjoern A. Zeeb 	 */
8530895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
8548c0fec80SRobert Watson 		struct in_ifaddr *ia;
8559317b04eSRobert Watson 		struct ifnet *ifp;
8560895aec3SBjoern A. Zeeb 
8570895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
8580304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8590895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8600895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8610895aec3SBjoern A. Zeeb 			goto done;
8620895aec3SBjoern A. Zeeb 		}
8630895aec3SBjoern A. Zeeb 
8640895aec3SBjoern A. Zeeb 		/* Jailed. */
8650895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
8660895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
867b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8680895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8690895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8700895aec3SBjoern A. Zeeb 			goto done;
8710895aec3SBjoern A. Zeeb 		}
8720895aec3SBjoern A. Zeeb 
8730895aec3SBjoern A. Zeeb 		/*
8740895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
8750895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
8760895aec3SBjoern A. Zeeb 		 */
8778c0fec80SRobert Watson 		ia = NULL;
8789317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
879137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8809317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8810895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8820895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8830895aec3SBjoern A. Zeeb 				continue;
8840895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
885b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8860895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8870895aec3SBjoern A. Zeeb 				break;
8880895aec3SBjoern A. Zeeb 			}
8890895aec3SBjoern A. Zeeb 		}
8900895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8910895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
892137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8930895aec3SBjoern A. Zeeb 			goto done;
8940895aec3SBjoern A. Zeeb 		}
895137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8960895aec3SBjoern A. Zeeb 
8970895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
898b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8990895aec3SBjoern A. Zeeb 		goto done;
9000895aec3SBjoern A. Zeeb 	}
9010895aec3SBjoern A. Zeeb 
9020895aec3SBjoern A. Zeeb 	/*
9030895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
9040895aec3SBjoern A. Zeeb 	 * to ourselves is here.
9050895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
9060895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
9070895aec3SBjoern A. Zeeb 	 * a loopback interface.
9080895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
9090895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
9100895aec3SBjoern A. Zeeb 	 */
9110895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
9120895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
9138c0fec80SRobert Watson 		struct in_ifaddr *ia;
9140895aec3SBjoern A. Zeeb 
9150895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
9160895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
9170895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
9180895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
9190895aec3SBjoern A. Zeeb 
92058a39d8cSAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
92158a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
9220895aec3SBjoern A. Zeeb 		if (ia == NULL)
9234f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
92458a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
925f0bb05fcSQing Li 		if (ia == NULL)
926f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
9270895aec3SBjoern A. Zeeb 
9280304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
9290895aec3SBjoern A. Zeeb 			if (ia == NULL) {
9300895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
9310895aec3SBjoern A. Zeeb 				goto done;
9320895aec3SBjoern A. Zeeb 			}
9330895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
9348c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9350895aec3SBjoern A. Zeeb 			goto done;
9360895aec3SBjoern A. Zeeb 		}
9370895aec3SBjoern A. Zeeb 
9380895aec3SBjoern A. Zeeb 		/* Jailed. */
9390895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9400895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
9410895aec3SBjoern A. Zeeb 
9420895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
9438c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9440895aec3SBjoern A. Zeeb 			ia = NULL;
945137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
9460895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9470895aec3SBjoern A. Zeeb 
9480895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
9490895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
9500895aec3SBjoern A. Zeeb 					continue;
9510895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
952b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
953b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
9540895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
9550895aec3SBjoern A. Zeeb 					break;
9560895aec3SBjoern A. Zeeb 				}
9570895aec3SBjoern A. Zeeb 			}
9580895aec3SBjoern A. Zeeb 			if (ia != NULL) {
9590895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
960137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
9610895aec3SBjoern A. Zeeb 				goto done;
9620895aec3SBjoern A. Zeeb 			}
963137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9640895aec3SBjoern A. Zeeb 		}
9650895aec3SBjoern A. Zeeb 
9660895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
967b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9680895aec3SBjoern A. Zeeb 		goto done;
9690895aec3SBjoern A. Zeeb 	}
9700895aec3SBjoern A. Zeeb 
9710895aec3SBjoern A. Zeeb done:
9720895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
9730895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
9740895aec3SBjoern A. Zeeb 	return (error);
9750895aec3SBjoern A. Zeeb }
9760895aec3SBjoern A. Zeeb 
9770895aec3SBjoern A. Zeeb /*
9785200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
9795200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
9805200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
9815200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
9825200e00eSIan Dowse  * the connect.
9835200e00eSIan Dowse  *
9845200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
9855200e00eSIan Dowse  * and port. These are not updated in the error case.
9865200e00eSIan Dowse  *
9875200e00eSIan Dowse  * If the operation fails because the connection already exists,
9885200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
9895200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
9905200e00eSIan Dowse  * is set to NULL.
9915200e00eSIan Dowse  */
9925200e00eSIan Dowse int
993136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
994136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
995136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
9965200e00eSIan Dowse {
997*cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
9985200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
9995200e00eSIan Dowse 	struct in_ifaddr *ia;
10005200e00eSIan Dowse 	struct inpcb *oinp;
1001b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
10025200e00eSIan Dowse 	u_short lport, fport;
10035200e00eSIan Dowse 	int error;
10045200e00eSIan Dowse 
10058501a69cSRobert Watson 	/*
10068501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
10078501a69cSRobert Watson 	 * lock is sufficient.
10088501a69cSRobert Watson 	 */
100927f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
1010fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
101127f74fd0SRobert Watson 
10125200e00eSIan Dowse 	if (oinpp != NULL)
10135200e00eSIan Dowse 		*oinpp = NULL;
101457bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
1015df8bae1dSRodney W. Grimes 		return (EINVAL);
1016df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
1017df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
1018df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1019df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
10205200e00eSIan Dowse 	laddr.s_addr = *laddrp;
10215200e00eSIan Dowse 	lport = *lportp;
10225200e00eSIan Dowse 	faddr = sin->sin_addr;
10235200e00eSIan Dowse 	fport = sin->sin_port;
10240895aec3SBjoern A. Zeeb 
1025603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1026df8bae1dSRodney W. Grimes 		/*
1027df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1028df8bae1dSRodney W. Grimes 		 * use the primary local address.
1029df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1030df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1031df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1032df8bae1dSRodney W. Grimes 		 */
1033413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
1034*cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
1035413628a7SBjoern A. Zeeb 			faddr =
1036b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1037*cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1038b89e82ddSJamie Gritton 			if (cred != NULL &&
1039b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1040b89e82ddSJamie Gritton 				return (error);
10412d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1042*cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
10432d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
10442d9cfabaSRobert Watson 			    IFF_BROADCAST)
10455200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
1046603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1047*cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
10482d9cfabaSRobert Watson 		}
1049df8bae1dSRodney W. Grimes 	}
10505200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1051d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1052df8bae1dSRodney W. Grimes 		/*
1053df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1054d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1055df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1056df8bae1dSRodney W. Grimes 		 */
10575200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1058df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1059df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1060df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1063df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1064df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
1065*cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RLOCK(&in_ifa_tracker);
1066e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1067e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1068e691be70SDaniel Eischen 					    (cred == NULL ||
1069e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1070e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1071df8bae1dSRodney W. Grimes 						break;
1072e691be70SDaniel Eischen 				}
1073e691be70SDaniel Eischen 				if (ia == NULL)
1074d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1075e691be70SDaniel Eischen 				else {
10765200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1077d79fdd98SDaniel Eischen 					error = 0;
1078999f1343SGarrett Wollman 				}
1079*cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1080d79fdd98SDaniel Eischen 			}
1081d79fdd98SDaniel Eischen 		}
108204215ed2SRandall Stewart 		if (error)
108304215ed2SRandall Stewart 			return (error);
10840895aec3SBjoern A. Zeeb 	}
1085fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1086fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
10875200e00eSIan Dowse 	if (oinp != NULL) {
10885200e00eSIan Dowse 		if (oinpp != NULL)
10895200e00eSIan Dowse 			*oinpp = oinp;
1090df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1091c3229e05SDavid Greenman 	}
10925200e00eSIan Dowse 	if (lport == 0) {
1093b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1094b0330ed9SPawel Jakub Dawidek 		    cred);
10955a903f8dSPierre Beyssac 		if (error)
10965a903f8dSPierre Beyssac 			return (error);
10975a903f8dSPierre Beyssac 	}
10985200e00eSIan Dowse 	*laddrp = laddr.s_addr;
10995200e00eSIan Dowse 	*lportp = lport;
11005200e00eSIan Dowse 	*faddrp = faddr.s_addr;
11015200e00eSIan Dowse 	*fportp = fport;
1102df8bae1dSRodney W. Grimes 	return (0);
1103df8bae1dSRodney W. Grimes }
1104df8bae1dSRodney W. Grimes 
110526f9a767SRodney W. Grimes void
1106136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1107df8bae1dSRodney W. Grimes {
11086b348152SRobert Watson 
11098501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1110fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1111df8bae1dSRodney W. Grimes 
1112df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1113df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
111415bd2b43SDavid Greenman 	in_pcbrehash(inp);
1115df8bae1dSRodney W. Grimes }
111683e521ecSBjoern A. Zeeb #endif /* INET */
1117df8bae1dSRodney W. Grimes 
11184c7c478dSRobert Watson /*
111928696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1120c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
112128696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
112228696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
11234c7c478dSRobert Watson  */
112426f9a767SRodney W. Grimes void
1125136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1126df8bae1dSRodney W. Grimes {
11274c7c478dSRobert Watson 
1128a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1129c0a211c5SRobert Watson 
11304c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
11314c7c478dSRobert Watson 	inp->inp_socket = NULL;
11324c7c478dSRobert Watson }
11334c7c478dSRobert Watson 
1134c0a211c5SRobert Watson /*
113579bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
113679bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
113779bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
113852cd27cbSRobert Watson  * but where the inpcb lock may already held, or when acquiring a reference
113952cd27cbSRobert Watson  * via a pcbgroup.
114079bdc6e5SRobert Watson  *
114179bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
114279bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
114379bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
114479bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
114579bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
114679bdc6e5SRobert Watson  * inpcb.
114779bdc6e5SRobert Watson  *
114879bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
114979bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
115079bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
115179bdc6e5SRobert Watson  * using in_pcbrele().
1152c0a211c5SRobert Watson  */
115379bdc6e5SRobert Watson void
115479bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
11554c7c478dSRobert Watson {
1156df8bae1dSRodney W. Grimes 
115779bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
115879bdc6e5SRobert Watson 
115979bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
116079bdc6e5SRobert Watson }
116179bdc6e5SRobert Watson 
116279bdc6e5SRobert Watson /*
116379bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
116479bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
116579bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
116679bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
116779bdc6e5SRobert Watson  *
116879bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
116979bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
117079bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
117179bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
117279bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
117379bdc6e5SRobert Watson  */
117479bdc6e5SRobert Watson int
117579bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
117679bdc6e5SRobert Watson {
117779bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
117879bdc6e5SRobert Watson 
117979bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
118079bdc6e5SRobert Watson 
118179bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
118279bdc6e5SRobert Watson 
1183df4e91d3SGleb Smirnoff 	if (refcount_release(&inp->inp_refcount) == 0) {
1184df4e91d3SGleb Smirnoff 		/*
1185df4e91d3SGleb Smirnoff 		 * If the inpcb has been freed, let the caller know, even if
1186df4e91d3SGleb Smirnoff 		 * this isn't the last reference.
1187df4e91d3SGleb Smirnoff 		 */
1188df4e91d3SGleb Smirnoff 		if (inp->inp_flags2 & INP_FREED) {
1189df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1190df4e91d3SGleb Smirnoff 			return (1);
1191df4e91d3SGleb Smirnoff 		}
119279bdc6e5SRobert Watson 		return (0);
1193df4e91d3SGleb Smirnoff 	}
119479bdc6e5SRobert Watson 
119579bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
119679bdc6e5SRobert Watson 
119779bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
119879bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
119979bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
120079bdc6e5SRobert Watson 	return (1);
120179bdc6e5SRobert Watson }
120279bdc6e5SRobert Watson 
120379bdc6e5SRobert Watson int
120479bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
120579bdc6e5SRobert Watson {
120679bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
120779bdc6e5SRobert Watson 
120879bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
120979bdc6e5SRobert Watson 
121079bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
121179bdc6e5SRobert Watson 
121279bdc6e5SRobert Watson 	if (refcount_release(&inp->inp_refcount) == 0)
121379bdc6e5SRobert Watson 		return (0);
121479bdc6e5SRobert Watson 
121579bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
121679bdc6e5SRobert Watson 
121779bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
121879bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
121979bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
122079bdc6e5SRobert Watson 	return (1);
122179bdc6e5SRobert Watson }
122279bdc6e5SRobert Watson 
122379bdc6e5SRobert Watson /*
122479bdc6e5SRobert Watson  * Temporary wrapper.
122579bdc6e5SRobert Watson  */
122679bdc6e5SRobert Watson int
122779bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
122879bdc6e5SRobert Watson {
122979bdc6e5SRobert Watson 
123079bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
123179bdc6e5SRobert Watson }
123279bdc6e5SRobert Watson 
123379bdc6e5SRobert Watson /*
123479bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
123579bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
123679bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
123779bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
123879bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
123979bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
124079bdc6e5SRobert Watson  * the pcbinfo lock is held.
124179bdc6e5SRobert Watson  */
124279bdc6e5SRobert Watson void
124379bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
124479bdc6e5SRobert Watson {
124579bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
124679bdc6e5SRobert Watson 
124779bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
124879bdc6e5SRobert Watson 
124979bdc6e5SRobert Watson 	INP_INFO_WLOCK_ASSERT(pcbinfo);
125079bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
125179bdc6e5SRobert Watson 
125279bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1253b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
12546aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
12556974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
125683e521ecSBjoern A. Zeeb #endif
125779bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1258c3229e05SDavid Greenman 	in_pcbremlists(inp);
12596aee2fc5SBjoern A. Zeeb #ifdef INET6
12606aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
12616aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
12621096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
12636aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
12646aee2fc5SBjoern A. Zeeb 	}
12656aee2fc5SBjoern A. Zeeb #endif
1266df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1267df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
126867107f45SBjoern A. Zeeb #ifdef INET
126971498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
127071498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
127167107f45SBjoern A. Zeeb #endif
1272cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1273df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
127486d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1275a557af22SRobert Watson #ifdef MAC
127630d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1277a557af22SRobert Watson #endif
127879bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
127928696211SRobert Watson 		INP_WUNLOCK(inp);
128028696211SRobert Watson }
128128696211SRobert Watson 
128228696211SRobert Watson /*
1283c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1284c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1285c0a211c5SRobert Watson  *
1286c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1287c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1288c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1289c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1290c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1291c0a211c5SRobert Watson  * in_pcbdetach().
1292c0a211c5SRobert Watson  *
1293c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1294c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
129510702a28SRobert Watson  */
129610702a28SRobert Watson void
129710702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
129810702a28SRobert Watson {
129910702a28SRobert Watson 
13008501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
130110702a28SRobert Watson 
1302fa046d87SRobert Watson 	/*
1303fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1304fa046d87SRobert Watson 	 * the hash lock...?
1305fa046d87SRobert Watson 	 */
1306ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1307111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
130810702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
130910702a28SRobert Watson 
1310fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
131110702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
131210702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
131310702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
131410702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
131510702a28SRobert Watson 			free(phd, M_PCB);
131610702a28SRobert Watson 		}
1317fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1318111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
131952cd27cbSRobert Watson #ifdef PCBGROUP
132052cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
132152cd27cbSRobert Watson #endif
132210702a28SRobert Watson 	}
132310702a28SRobert Watson }
132410702a28SRobert Watson 
132567107f45SBjoern A. Zeeb #ifdef INET
132654d642bbSRobert Watson /*
132754d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
132854d642bbSRobert Watson  */
132926ef6ac4SDon Lewis struct sockaddr *
1330136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
133126ef6ac4SDon Lewis {
133226ef6ac4SDon Lewis 	struct sockaddr_in *sin;
133326ef6ac4SDon Lewis 
13341ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1335a163d034SWarner Losh 		M_WAITOK | M_ZERO);
133626ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
133726ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
133826ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
133926ef6ac4SDon Lewis 	sin->sin_port = port;
134026ef6ac4SDon Lewis 
134126ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
134226ef6ac4SDon Lewis }
134326ef6ac4SDon Lewis 
1344117bcae7SGarrett Wollman int
134554d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1346df8bae1dSRodney W. Grimes {
1347136d4f1cSRobert Watson 	struct inpcb *inp;
134826ef6ac4SDon Lewis 	struct in_addr addr;
134926ef6ac4SDon Lewis 	in_port_t port;
135042fa505bSDavid Greenman 
1351fdc984f7STor Egge 	inp = sotoinpcb(so);
135254d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
13536466b28aSRobert Watson 
1354a69042a5SRobert Watson 	INP_RLOCK(inp);
135526ef6ac4SDon Lewis 	port = inp->inp_lport;
135626ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1357a69042a5SRobert Watson 	INP_RUNLOCK(inp);
135842fa505bSDavid Greenman 
135926ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1360117bcae7SGarrett Wollman 	return 0;
1361df8bae1dSRodney W. Grimes }
1362df8bae1dSRodney W. Grimes 
1363117bcae7SGarrett Wollman int
136454d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1365df8bae1dSRodney W. Grimes {
1366136d4f1cSRobert Watson 	struct inpcb *inp;
136726ef6ac4SDon Lewis 	struct in_addr addr;
136826ef6ac4SDon Lewis 	in_port_t port;
136942fa505bSDavid Greenman 
1370fdc984f7STor Egge 	inp = sotoinpcb(so);
137154d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
13726466b28aSRobert Watson 
1373a69042a5SRobert Watson 	INP_RLOCK(inp);
137426ef6ac4SDon Lewis 	port = inp->inp_fport;
137526ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1376a69042a5SRobert Watson 	INP_RUNLOCK(inp);
137742fa505bSDavid Greenman 
137826ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1379117bcae7SGarrett Wollman 	return 0;
1380df8bae1dSRodney W. Grimes }
1381df8bae1dSRodney W. Grimes 
138226f9a767SRodney W. Grimes void
1383136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1384136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1385d1c54148SJesper Skriver {
1386f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1387d1c54148SJesper Skriver 
13883dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1389f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
13908501a69cSRobert Watson 		INP_WLOCK(inp);
1391d1c54148SJesper Skriver #ifdef INET6
1392f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
13938501a69cSRobert Watson 			INP_WUNLOCK(inp);
1394d1c54148SJesper Skriver 			continue;
1395f76fcf6dSJeffrey Hsu 		}
1396d1c54148SJesper Skriver #endif
1397d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1398f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
13998501a69cSRobert Watson 			INP_WUNLOCK(inp);
1400d1c54148SJesper Skriver 			continue;
1401d1c54148SJesper Skriver 		}
14023dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
14038501a69cSRobert Watson 			INP_WUNLOCK(inp);
1404f76fcf6dSJeffrey Hsu 	}
14053dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1406d1c54148SJesper Skriver }
1407d1c54148SJesper Skriver 
1408e43cc4aeSHajimu UMEMOTO void
1409136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1410e43cc4aeSHajimu UMEMOTO {
1411e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1412e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1413e43cc4aeSHajimu UMEMOTO 	int i, gap;
1414e43cc4aeSHajimu UMEMOTO 
1415f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
1416712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
14178501a69cSRobert Watson 		INP_WLOCK(inp);
1418e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1419e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1420e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1421e43cc4aeSHajimu UMEMOTO 			/*
1422e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1423e43cc4aeSHajimu UMEMOTO 			 * detached.
1424e43cc4aeSHajimu UMEMOTO 			 */
1425e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1426e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1427e43cc4aeSHajimu UMEMOTO 
1428e43cc4aeSHajimu UMEMOTO 			/*
1429e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1430e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1431e43cc4aeSHajimu UMEMOTO 			 */
1432e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1433e43cc4aeSHajimu UMEMOTO 			    i++) {
1434e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1435e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1436e43cc4aeSHajimu UMEMOTO 					gap++;
1437e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1438e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1439e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1440e43cc4aeSHajimu UMEMOTO 			}
1441e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1442e43cc4aeSHajimu UMEMOTO 		}
14438501a69cSRobert Watson 		INP_WUNLOCK(inp);
1444e43cc4aeSHajimu UMEMOTO 	}
14453cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
1446e43cc4aeSHajimu UMEMOTO }
1447e43cc4aeSHajimu UMEMOTO 
1448df8bae1dSRodney W. Grimes /*
1449fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1450fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1451c3229e05SDavid Greenman  */
1452d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1453df8bae1dSRodney W. Grimes struct inpcb *
1454136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
145568e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1456df8bae1dSRodney W. Grimes {
1457136d4f1cSRobert Watson 	struct inpcb *inp;
1458d5e8a67eSHajimu UMEMOTO #ifdef INET6
1459d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1460d5e8a67eSHajimu UMEMOTO #else
1461d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1462d5e8a67eSHajimu UMEMOTO #endif
1463d5e8a67eSHajimu UMEMOTO 	int wildcard;
14647bc4aca7SDavid Greenman 
146568e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
146668e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
146768e0d7e0SRobert Watson 
1468fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
14691b73ca0bSSam Leffler 
147068e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1471c3229e05SDavid Greenman 		struct inpcbhead *head;
1472c3229e05SDavid Greenman 		/*
1473c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1474c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1475c3229e05SDavid Greenman 		 */
1476712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1477712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1478fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1479cfa1ca9dSYoshinobu Inoue #ifdef INET6
1480413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1481369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1482cfa1ca9dSYoshinobu Inoue 				continue;
1483cfa1ca9dSYoshinobu Inoue #endif
1484c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1485c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1486c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1487c3229e05SDavid Greenman 				/*
1488413628a7SBjoern A. Zeeb 				 * Found?
1489c3229e05SDavid Greenman 				 */
1490413628a7SBjoern A. Zeeb 				if (cred == NULL ||
14910304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
14920304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1493c3229e05SDavid Greenman 					return (inp);
1494df8bae1dSRodney W. Grimes 			}
1495c3229e05SDavid Greenman 		}
1496c3229e05SDavid Greenman 		/*
1497c3229e05SDavid Greenman 		 * Not found.
1498c3229e05SDavid Greenman 		 */
1499c3229e05SDavid Greenman 		return (NULL);
1500c3229e05SDavid Greenman 	} else {
1501c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1502c3229e05SDavid Greenman 		struct inpcbport *phd;
1503c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1504c3229e05SDavid Greenman 		/*
1505c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1506c3229e05SDavid Greenman 		 *
1507c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1508c3229e05SDavid Greenman 		 * port hash list.
1509c3229e05SDavid Greenman 		 */
1510712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1511712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1512fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1513c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1514c3229e05SDavid Greenman 				break;
1515c3229e05SDavid Greenman 		}
1516c3229e05SDavid Greenman 		if (phd != NULL) {
1517c3229e05SDavid Greenman 			/*
1518c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1519c3229e05SDavid Greenman 			 * fit.
1520c3229e05SDavid Greenman 			 */
152137d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1522c3229e05SDavid Greenman 				wildcard = 0;
1523413628a7SBjoern A. Zeeb 				if (cred != NULL &&
15240304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
15250304c731SJamie Gritton 					cred->cr_prison))
1526413628a7SBjoern A. Zeeb 					continue;
1527cfa1ca9dSYoshinobu Inoue #ifdef INET6
1528413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1529369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1530cfa1ca9dSYoshinobu Inoue 					continue;
1531d5e8a67eSHajimu UMEMOTO 				/*
1532d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1533d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1534d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1535d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1536d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1537d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1538d5e8a67eSHajimu UMEMOTO 				 *
1539d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1540d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1541d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1542d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1543d5e8a67eSHajimu UMEMOTO 				 */
1544d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1545d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1546cfa1ca9dSYoshinobu Inoue #endif
1547c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1548c3229e05SDavid Greenman 					wildcard++;
154915bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
155015bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
155115bd2b43SDavid Greenman 						wildcard++;
155215bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
155315bd2b43SDavid Greenman 						continue;
155415bd2b43SDavid Greenman 				} else {
155515bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
155615bd2b43SDavid Greenman 						wildcard++;
155715bd2b43SDavid Greenman 				}
1558df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1559df8bae1dSRodney W. Grimes 					match = inp;
1560df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1561413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1562df8bae1dSRodney W. Grimes 						break;
1563df8bae1dSRodney W. Grimes 				}
1564df8bae1dSRodney W. Grimes 			}
15653dbdc25cSDavid Greenman 		}
1566df8bae1dSRodney W. Grimes 		return (match);
1567df8bae1dSRodney W. Grimes 	}
1568c3229e05SDavid Greenman }
1569d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
157015bd2b43SDavid Greenman 
157152cd27cbSRobert Watson #ifdef PCBGROUP
157252cd27cbSRobert Watson /*
157352cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
157452cd27cbSRobert Watson  */
157552cd27cbSRobert Watson static struct inpcb *
157652cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
157752cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
157852cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
157952cd27cbSRobert Watson {
158052cd27cbSRobert Watson 	struct inpcbhead *head;
158152cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
158252cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
158352cd27cbSRobert Watson 
158452cd27cbSRobert Watson 	/*
158552cd27cbSRobert Watson 	 * First look for an exact match.
158652cd27cbSRobert Watson 	 */
158752cd27cbSRobert Watson 	tmpinp = NULL;
158852cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
158952cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
159052cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
159152cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
159252cd27cbSRobert Watson #ifdef INET6
159352cd27cbSRobert Watson 		/* XXX inp locking */
159452cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
159552cd27cbSRobert Watson 			continue;
159652cd27cbSRobert Watson #endif
159752cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
159852cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
159952cd27cbSRobert Watson 		    inp->inp_fport == fport &&
160052cd27cbSRobert Watson 		    inp->inp_lport == lport) {
160152cd27cbSRobert Watson 			/*
160252cd27cbSRobert Watson 			 * XXX We should be able to directly return
160352cd27cbSRobert Watson 			 * the inp here, without any checks.
160452cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
160552cd27cbSRobert Watson 			 */
160652cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
160752cd27cbSRobert Watson 				goto found;
160852cd27cbSRobert Watson 			if (tmpinp == NULL)
160952cd27cbSRobert Watson 				tmpinp = inp;
161052cd27cbSRobert Watson 		}
161152cd27cbSRobert Watson 	}
161252cd27cbSRobert Watson 	if (tmpinp != NULL) {
161352cd27cbSRobert Watson 		inp = tmpinp;
161452cd27cbSRobert Watson 		goto found;
161552cd27cbSRobert Watson 	}
161652cd27cbSRobert Watson 
16170a100a6fSAdrian Chadd #ifdef	RSS
16180a100a6fSAdrian Chadd 	/*
16190a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
16200a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
16210a100a6fSAdrian Chadd 	 */
16220a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
16230a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
16240a100a6fSAdrian Chadd #ifdef INET6
16250a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
16260a100a6fSAdrian Chadd #endif
16270a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
16280a100a6fSAdrian Chadd 		struct inpcbhead *head;
16290a100a6fSAdrian Chadd 		int injail;
16300a100a6fSAdrian Chadd 
16310a100a6fSAdrian Chadd 		/*
16320a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
16330a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
16340a100a6fSAdrian Chadd 		 *      2. jailed, wild.
16350a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
16360a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
16370a100a6fSAdrian Chadd 		 */
16380a100a6fSAdrian Chadd 
16390a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
16400a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
16410a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
16420a100a6fSAdrian Chadd #ifdef INET6
16430a100a6fSAdrian Chadd 			/* XXX inp locking */
16440a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
16450a100a6fSAdrian Chadd 				continue;
16460a100a6fSAdrian Chadd #endif
16470a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
16480a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
16490a100a6fSAdrian Chadd 				continue;
16500a100a6fSAdrian Chadd 
16510a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
16520a100a6fSAdrian Chadd 			if (injail) {
16530a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
16540a100a6fSAdrian Chadd 				    &laddr) != 0)
16550a100a6fSAdrian Chadd 					continue;
16560a100a6fSAdrian Chadd 			} else {
16570a100a6fSAdrian Chadd 				if (local_exact != NULL)
16580a100a6fSAdrian Chadd 					continue;
16590a100a6fSAdrian Chadd 			}
16600a100a6fSAdrian Chadd 
16610a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
16620a100a6fSAdrian Chadd 				if (injail)
16630a100a6fSAdrian Chadd 					goto found;
16640a100a6fSAdrian Chadd 				else
16650a100a6fSAdrian Chadd 					local_exact = inp;
16660a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
16670a100a6fSAdrian Chadd #ifdef INET6
16680a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
16690a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
16700a100a6fSAdrian Chadd 					local_wild_mapped = inp;
16710a100a6fSAdrian Chadd 				else
16720a100a6fSAdrian Chadd #endif
16730a100a6fSAdrian Chadd 					if (injail)
16740a100a6fSAdrian Chadd 						jail_wild = inp;
16750a100a6fSAdrian Chadd 					else
16760a100a6fSAdrian Chadd 						local_wild = inp;
16770a100a6fSAdrian Chadd 			}
16780a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
16790a100a6fSAdrian Chadd 
16800a100a6fSAdrian Chadd 		inp = jail_wild;
16810a100a6fSAdrian Chadd 		if (inp == NULL)
16820a100a6fSAdrian Chadd 			inp = local_exact;
16830a100a6fSAdrian Chadd 		if (inp == NULL)
16840a100a6fSAdrian Chadd 			inp = local_wild;
16850a100a6fSAdrian Chadd #ifdef INET6
16860a100a6fSAdrian Chadd 		if (inp == NULL)
16870a100a6fSAdrian Chadd 			inp = local_wild_mapped;
16880a100a6fSAdrian Chadd #endif
16890a100a6fSAdrian Chadd 		if (inp != NULL)
16900a100a6fSAdrian Chadd 			goto found;
16910a100a6fSAdrian Chadd 	}
16920a100a6fSAdrian Chadd #endif
16930a100a6fSAdrian Chadd 
169452cd27cbSRobert Watson 	/*
169552cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
169652cd27cbSRobert Watson 	 */
169752cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
169852cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
169952cd27cbSRobert Watson #ifdef INET6
170052cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
170152cd27cbSRobert Watson #endif
170252cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
170352cd27cbSRobert Watson 		struct inpcbhead *head;
170452cd27cbSRobert Watson 		int injail;
170552cd27cbSRobert Watson 
170652cd27cbSRobert Watson 		/*
170752cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
170852cd27cbSRobert Watson 		 *      1. jailed, non-wild.
170952cd27cbSRobert Watson 		 *      2. jailed, wild.
171052cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
171152cd27cbSRobert Watson 		 *      4. non-jailed, wild.
171252cd27cbSRobert Watson 		 */
171352cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
171452cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
171552cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
171652cd27cbSRobert Watson #ifdef INET6
171752cd27cbSRobert Watson 			/* XXX inp locking */
171852cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
171952cd27cbSRobert Watson 				continue;
172052cd27cbSRobert Watson #endif
172152cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
172252cd27cbSRobert Watson 			    inp->inp_lport != lport)
172352cd27cbSRobert Watson 				continue;
172452cd27cbSRobert Watson 
172552cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
172652cd27cbSRobert Watson 			if (injail) {
172752cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
172852cd27cbSRobert Watson 				    &laddr) != 0)
172952cd27cbSRobert Watson 					continue;
173052cd27cbSRobert Watson 			} else {
173152cd27cbSRobert Watson 				if (local_exact != NULL)
173252cd27cbSRobert Watson 					continue;
173352cd27cbSRobert Watson 			}
173452cd27cbSRobert Watson 
173552cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
173652cd27cbSRobert Watson 				if (injail)
173752cd27cbSRobert Watson 					goto found;
173852cd27cbSRobert Watson 				else
173952cd27cbSRobert Watson 					local_exact = inp;
174052cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
174152cd27cbSRobert Watson #ifdef INET6
174252cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
174352cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
174452cd27cbSRobert Watson 					local_wild_mapped = inp;
174552cd27cbSRobert Watson 				else
174683e521ecSBjoern A. Zeeb #endif
174752cd27cbSRobert Watson 					if (injail)
174852cd27cbSRobert Watson 						jail_wild = inp;
174952cd27cbSRobert Watson 					else
175052cd27cbSRobert Watson 						local_wild = inp;
175152cd27cbSRobert Watson 			}
175252cd27cbSRobert Watson 		} /* LIST_FOREACH */
175352cd27cbSRobert Watson 		inp = jail_wild;
175452cd27cbSRobert Watson 		if (inp == NULL)
175552cd27cbSRobert Watson 			inp = local_exact;
175652cd27cbSRobert Watson 		if (inp == NULL)
175752cd27cbSRobert Watson 			inp = local_wild;
175852cd27cbSRobert Watson #ifdef INET6
175952cd27cbSRobert Watson 		if (inp == NULL)
176052cd27cbSRobert Watson 			inp = local_wild_mapped;
176183e521ecSBjoern A. Zeeb #endif
176252cd27cbSRobert Watson 		if (inp != NULL)
176352cd27cbSRobert Watson 			goto found;
176452cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
176552cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
176652cd27cbSRobert Watson 	return (NULL);
176752cd27cbSRobert Watson 
176852cd27cbSRobert Watson found:
176952cd27cbSRobert Watson 	in_pcbref(inp);
177052cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
177152cd27cbSRobert Watson 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
177252cd27cbSRobert Watson 		INP_WLOCK(inp);
177352cd27cbSRobert Watson 		if (in_pcbrele_wlocked(inp))
177452cd27cbSRobert Watson 			return (NULL);
177552cd27cbSRobert Watson 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
177652cd27cbSRobert Watson 		INP_RLOCK(inp);
177752cd27cbSRobert Watson 		if (in_pcbrele_rlocked(inp))
177852cd27cbSRobert Watson 			return (NULL);
177952cd27cbSRobert Watson 	} else
178052cd27cbSRobert Watson 		panic("%s: locking bug", __func__);
178152cd27cbSRobert Watson 	return (inp);
178252cd27cbSRobert Watson }
178352cd27cbSRobert Watson #endif /* PCBGROUP */
178452cd27cbSRobert Watson 
178515bd2b43SDavid Greenman /*
1786fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1787fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1788fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
178915bd2b43SDavid Greenman  */
1790fa046d87SRobert Watson static struct inpcb *
1791fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
179268e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1793136d4f1cSRobert Watson     struct ifnet *ifp)
179415bd2b43SDavid Greenman {
179515bd2b43SDavid Greenman 	struct inpcbhead *head;
1796413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
179715bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
179815bd2b43SDavid Greenman 
179968e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
180068e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
180168e0d7e0SRobert Watson 
1802fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1803602cc7f1SRobert Watson 
180415bd2b43SDavid Greenman 	/*
180515bd2b43SDavid Greenman 	 * First look for an exact match.
180615bd2b43SDavid Greenman 	 */
1807413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1808712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1809712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1810fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1811cfa1ca9dSYoshinobu Inoue #ifdef INET6
1812413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1813369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1814cfa1ca9dSYoshinobu Inoue 			continue;
1815cfa1ca9dSYoshinobu Inoue #endif
18166d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1817ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1818ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1819413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1820413628a7SBjoern A. Zeeb 			/*
1821413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1822413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1823413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1824413628a7SBjoern A. Zeeb 			 */
18250304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1826c3229e05SDavid Greenman 				return (inp);
1827413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1828413628a7SBjoern A. Zeeb 				tmpinp = inp;
1829c3229e05SDavid Greenman 		}
1830413628a7SBjoern A. Zeeb 	}
1831413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1832413628a7SBjoern A. Zeeb 		return (tmpinp);
1833e3fd5ffdSRobert Watson 
1834e3fd5ffdSRobert Watson 	/*
1835e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1836e3fd5ffdSRobert Watson 	 */
183768e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1838413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1839e3fd5ffdSRobert Watson #ifdef INET6
1840cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1841e3fd5ffdSRobert Watson #endif
1842413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1843413628a7SBjoern A. Zeeb 		int injail;
1844413628a7SBjoern A. Zeeb 
1845413628a7SBjoern A. Zeeb 		/*
1846413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1847413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1848413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1849413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1850413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1851413628a7SBjoern A. Zeeb 		 */
18526d6a026bSDavid Greenman 
1853712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1854712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1855fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1856cfa1ca9dSYoshinobu Inoue #ifdef INET6
1857413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1858369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1859cfa1ca9dSYoshinobu Inoue 				continue;
1860cfa1ca9dSYoshinobu Inoue #endif
1861413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1862413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1863413628a7SBjoern A. Zeeb 				continue;
1864413628a7SBjoern A. Zeeb 
18650304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1866413628a7SBjoern A. Zeeb 			if (injail) {
1867b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1868b89e82ddSJamie Gritton 				    &laddr) != 0)
1869413628a7SBjoern A. Zeeb 					continue;
1870413628a7SBjoern A. Zeeb 			} else {
1871413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1872413628a7SBjoern A. Zeeb 					continue;
1873413628a7SBjoern A. Zeeb 			}
1874413628a7SBjoern A. Zeeb 
1875413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1876413628a7SBjoern A. Zeeb 				if (injail)
1877c3229e05SDavid Greenman 					return (inp);
1878413628a7SBjoern A. Zeeb 				else
1879413628a7SBjoern A. Zeeb 					local_exact = inp;
1880413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1881e3fd5ffdSRobert Watson #ifdef INET6
1882413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
18835cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1884cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1885cfa1ca9dSYoshinobu Inoue 				else
188683e521ecSBjoern A. Zeeb #endif
1887413628a7SBjoern A. Zeeb 					if (injail)
1888413628a7SBjoern A. Zeeb 						jail_wild = inp;
1889413628a7SBjoern A. Zeeb 					else
18906d6a026bSDavid Greenman 						local_wild = inp;
18916d6a026bSDavid Greenman 			}
1892413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1893413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1894413628a7SBjoern A. Zeeb 			return (jail_wild);
1895413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1896413628a7SBjoern A. Zeeb 			return (local_exact);
1897413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1898c3229e05SDavid Greenman 			return (local_wild);
1899413628a7SBjoern A. Zeeb #ifdef INET6
1900413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1901413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
190283e521ecSBjoern A. Zeeb #endif
190368e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1904413628a7SBjoern A. Zeeb 
19056d6a026bSDavid Greenman 	return (NULL);
190615bd2b43SDavid Greenman }
1907fa046d87SRobert Watson 
1908fa046d87SRobert Watson /*
1909fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1910fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1911fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1912fa046d87SRobert Watson  */
1913fa046d87SRobert Watson static struct inpcb *
1914fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1915fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1916fa046d87SRobert Watson     struct ifnet *ifp)
1917fa046d87SRobert Watson {
1918fa046d87SRobert Watson 	struct inpcb *inp;
1919fa046d87SRobert Watson 
1920fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1921fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1922fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1923fa046d87SRobert Watson 	if (inp != NULL) {
1924fa046d87SRobert Watson 		in_pcbref(inp);
1925fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1926fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1927fa046d87SRobert Watson 			INP_WLOCK(inp);
1928fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1929fa046d87SRobert Watson 				return (NULL);
1930fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1931fa046d87SRobert Watson 			INP_RLOCK(inp);
1932fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1933fa046d87SRobert Watson 				return (NULL);
1934fa046d87SRobert Watson 		} else
1935fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1936fa046d87SRobert Watson 	} else
1937fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1938fa046d87SRobert Watson 	return (inp);
1939fa046d87SRobert Watson }
1940fa046d87SRobert Watson 
1941fa046d87SRobert Watson /*
1942d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1943d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
194452cd27cbSRobert Watson  *
194552cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
1946fa046d87SRobert Watson  */
1947fa046d87SRobert Watson struct inpcb *
1948fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1949fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1950fa046d87SRobert Watson {
19517527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
195252cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
195352cd27cbSRobert Watson #endif
1954fa046d87SRobert Watson 
1955fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1956fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1957fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1958fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1959fa046d87SRobert Watson 
19607527624eSRobert Watson 	/*
19617527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
19627527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
19637527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
19647527624eSRobert Watson 	 * in software.
19657527624eSRobert Watson 	 *
19667527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
19677527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
19687527624eSRobert Watson 	 * in software.
19697527624eSRobert Watson 	 */
19707527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
197152cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
197252cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
197352cd27cbSRobert Watson 		    fport);
197452cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
197552cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
197652cd27cbSRobert Watson 	}
197752cd27cbSRobert Watson #endif
1978fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1979fa046d87SRobert Watson 	    lookupflags, ifp));
1980fa046d87SRobert Watson }
1981d3c1f003SRobert Watson 
1982d3c1f003SRobert Watson struct inpcb *
1983d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1984d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1985d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
1986d3c1f003SRobert Watson {
198752cd27cbSRobert Watson #ifdef PCBGROUP
198852cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
198952cd27cbSRobert Watson #endif
1990d3c1f003SRobert Watson 
1991d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1992d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1993d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1994d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1995d3c1f003SRobert Watson 
199652cd27cbSRobert Watson #ifdef PCBGROUP
19977527624eSRobert Watson 	/*
19987527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
19997527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
20007527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
20017527624eSRobert Watson 	 * using RSS.
20027527624eSRobert Watson 	 *
20037527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
20047527624eSRobert Watson 	 */
20057527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
20067527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
200752cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
200852cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
200952cd27cbSRobert Watson 		if (pcbgroup != NULL)
201052cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
201152cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
20127527624eSRobert Watson #ifndef RSS
201352cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
201452cd27cbSRobert Watson 		    fport);
201552cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
201652cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
20177527624eSRobert Watson #endif
201852cd27cbSRobert Watson 	}
201952cd27cbSRobert Watson #endif
2020d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2021d3c1f003SRobert Watson 	    lookupflags, ifp));
2022d3c1f003SRobert Watson }
202367107f45SBjoern A. Zeeb #endif /* INET */
202415bd2b43SDavid Greenman 
20257bc4aca7SDavid Greenman /*
2026c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
20277bc4aca7SDavid Greenman  */
202852cd27cbSRobert Watson static int
202952cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
203015bd2b43SDavid Greenman {
2031c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2032c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2033c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2034c3229e05SDavid Greenman 	struct inpcbport *phd;
2035cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
203615bd2b43SDavid Greenman 
20378501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2038fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2039fa046d87SRobert Watson 
2040111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2041111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2042602cc7f1SRobert Watson 
2043cfa1ca9dSYoshinobu Inoue #ifdef INET6
2044cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
20451b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2046cfa1ca9dSYoshinobu Inoue 	else
204783e521ecSBjoern A. Zeeb #endif
2048cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2049cfa1ca9dSYoshinobu Inoue 
2050712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2051712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
205215bd2b43SDavid Greenman 
2053712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2054712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2055c3229e05SDavid Greenman 
2056c3229e05SDavid Greenman 	/*
2057c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2058c3229e05SDavid Greenman 	 */
2059fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2060c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2061c3229e05SDavid Greenman 			break;
2062c3229e05SDavid Greenman 	}
2063c3229e05SDavid Greenman 	/*
2064c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2065c3229e05SDavid Greenman 	 */
2066c3229e05SDavid Greenman 	if (phd == NULL) {
20671ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2068c3229e05SDavid Greenman 		if (phd == NULL) {
2069c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2070c3229e05SDavid Greenman 		}
2071c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2072c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2073c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2074c3229e05SDavid Greenman 	}
2075c3229e05SDavid Greenman 	inp->inp_phd = phd;
2076c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2077c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2078111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
207952cd27cbSRobert Watson #ifdef PCBGROUP
208052cd27cbSRobert Watson 	if (do_pcbgroup_update)
208152cd27cbSRobert Watson 		in_pcbgroup_update(inp);
208252cd27cbSRobert Watson #endif
2083c3229e05SDavid Greenman 	return (0);
208415bd2b43SDavid Greenman }
208515bd2b43SDavid Greenman 
2086c3229e05SDavid Greenman /*
208752cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
208852cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
208952cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
209052cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
209152cd27cbSRobert Watson  * pcbgroup until later.
209252cd27cbSRobert Watson  *
209352cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
209452cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
209552cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
209652cd27cbSRobert Watson  */
209752cd27cbSRobert Watson int
209852cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
209952cd27cbSRobert Watson {
210052cd27cbSRobert Watson 
210152cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
210252cd27cbSRobert Watson }
210352cd27cbSRobert Watson 
210452cd27cbSRobert Watson int
210552cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
210652cd27cbSRobert Watson {
210752cd27cbSRobert Watson 
210852cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
210952cd27cbSRobert Watson }
211052cd27cbSRobert Watson 
211152cd27cbSRobert Watson /*
2112c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2113c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2114c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2115c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2116c3229e05SDavid Greenman  */
211715bd2b43SDavid Greenman void
2118d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
211915bd2b43SDavid Greenman {
212059daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
212115bd2b43SDavid Greenman 	struct inpcbhead *head;
2122cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
212315bd2b43SDavid Greenman 
21248501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2125fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2126fa046d87SRobert Watson 
2127111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2128111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2129602cc7f1SRobert Watson 
2130cfa1ca9dSYoshinobu Inoue #ifdef INET6
2131cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
21321b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2133cfa1ca9dSYoshinobu Inoue 	else
213483e521ecSBjoern A. Zeeb #endif
2135cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2136cfa1ca9dSYoshinobu Inoue 
2137712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2138712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
213915bd2b43SDavid Greenman 
2140c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
214115bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
214252cd27cbSRobert Watson 
214352cd27cbSRobert Watson #ifdef PCBGROUP
214452cd27cbSRobert Watson 	if (m != NULL)
214552cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
214652cd27cbSRobert Watson 	else
214752cd27cbSRobert Watson 		in_pcbgroup_update(inp);
214852cd27cbSRobert Watson #endif
2149c3229e05SDavid Greenman }
2150c3229e05SDavid Greenman 
2151d3c1f003SRobert Watson void
2152d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2153d3c1f003SRobert Watson {
2154d3c1f003SRobert Watson 
2155d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2156d3c1f003SRobert Watson }
2157d3c1f003SRobert Watson 
2158c3229e05SDavid Greenman /*
2159c3229e05SDavid Greenman  * Remove PCB from various lists.
2160c3229e05SDavid Greenman  */
21616d888973SRobert Watson static void
2162136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2163c3229e05SDavid Greenman {
216459daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
216559daba27SSam Leffler 
216659daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
21678501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
216859daba27SSam Leffler 
216959daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2170111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2171c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2172c3229e05SDavid Greenman 
2173fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2174c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2175c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2176fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2177c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2178c3229e05SDavid Greenman 			free(phd, M_PCB);
2179c3229e05SDavid Greenman 		}
2180fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2181111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2182c3229e05SDavid Greenman 	}
2183c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
218459daba27SSam Leffler 	pcbinfo->ipi_count--;
218552cd27cbSRobert Watson #ifdef PCBGROUP
218652cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
218752cd27cbSRobert Watson #endif
218815bd2b43SDavid Greenman }
218975c13541SPoul-Henning Kamp 
2190a557af22SRobert Watson /*
2191a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2192a557af22SRobert Watson  * label change into the in_pcb for the socket.
2193a557af22SRobert Watson  */
2194a557af22SRobert Watson void
2195136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2196a557af22SRobert Watson {
2197a557af22SRobert Watson #ifdef MAC
2198a557af22SRobert Watson 	struct inpcb *inp;
2199a557af22SRobert Watson 
22004c7c478dSRobert Watson 	inp = sotoinpcb(so);
22014c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2202602cc7f1SRobert Watson 
22038501a69cSRobert Watson 	INP_WLOCK(inp);
2204310e7cebSRobert Watson 	SOCK_LOCK(so);
2205a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2206310e7cebSRobert Watson 	SOCK_UNLOCK(so);
22078501a69cSRobert Watson 	INP_WUNLOCK(inp);
2208a557af22SRobert Watson #endif
2209a557af22SRobert Watson }
22105f311da2SMike Silbersack 
22115f311da2SMike Silbersack /*
2212ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2213ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2214ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2215ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2216ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
22175f311da2SMike Silbersack  */
2218aae49dd3SBjoern A. Zeeb static void
2219136d4f1cSRobert Watson ipport_tick(void *xtp)
22205f311da2SMike Silbersack {
22218b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2222ad3a630fSRobert Watson 
22235ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
22248b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
22258b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
22268b615593SMarko Zec 		if (V_ipport_tcpallocs <=
22278b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2228603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2229603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2230ad3a630fSRobert Watson 		} else
2231603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2232603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
22338b615593SMarko Zec 		CURVNET_RESTORE();
22348b615593SMarko Zec 	}
22355ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
22365f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
22375f311da2SMike Silbersack }
2238497057eeSRobert Watson 
2239aae49dd3SBjoern A. Zeeb static void
2240aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2241aae49dd3SBjoern A. Zeeb {
2242aae49dd3SBjoern A. Zeeb 
2243aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2244aae49dd3SBjoern A. Zeeb }
2245aae49dd3SBjoern A. Zeeb 
2246aae49dd3SBjoern A. Zeeb /*
2247aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2248aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2249aae49dd3SBjoern A. Zeeb  */
2250aae49dd3SBjoern A. Zeeb static void
2251aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2252aae49dd3SBjoern A. Zeeb {
2253aae49dd3SBjoern A. Zeeb 
2254aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2255fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2256aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2257aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2258aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2259aae49dd3SBjoern A. Zeeb }
2260aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2261aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2262aae49dd3SBjoern A. Zeeb 
22633d585327SKip Macy void
22643d585327SKip Macy inp_wlock(struct inpcb *inp)
22653d585327SKip Macy {
22663d585327SKip Macy 
22678501a69cSRobert Watson 	INP_WLOCK(inp);
22683d585327SKip Macy }
22693d585327SKip Macy 
22703d585327SKip Macy void
22713d585327SKip Macy inp_wunlock(struct inpcb *inp)
22723d585327SKip Macy {
22733d585327SKip Macy 
22748501a69cSRobert Watson 	INP_WUNLOCK(inp);
22753d585327SKip Macy }
22763d585327SKip Macy 
22773d585327SKip Macy void
22783d585327SKip Macy inp_rlock(struct inpcb *inp)
22793d585327SKip Macy {
22803d585327SKip Macy 
2281a69042a5SRobert Watson 	INP_RLOCK(inp);
22823d585327SKip Macy }
22833d585327SKip Macy 
22843d585327SKip Macy void
22853d585327SKip Macy inp_runlock(struct inpcb *inp)
22863d585327SKip Macy {
22873d585327SKip Macy 
2288a69042a5SRobert Watson 	INP_RUNLOCK(inp);
22893d585327SKip Macy }
22903d585327SKip Macy 
22913d585327SKip Macy #ifdef INVARIANTS
22923d585327SKip Macy void
2293e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
22943d585327SKip Macy {
22953d585327SKip Macy 
22968501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
22973d585327SKip Macy }
22983d585327SKip Macy 
22993d585327SKip Macy void
2300e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
23013d585327SKip Macy {
23023d585327SKip Macy 
23033d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
23043d585327SKip Macy }
23053d585327SKip Macy #endif
23063d585327SKip Macy 
23079378e437SKip Macy void
23089378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
23099378e437SKip Macy {
23109378e437SKip Macy 	struct inpcb *inp;
23119378e437SKip Macy 
2312603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
231397021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
23149378e437SKip Macy 		INP_WLOCK(inp);
23159378e437SKip Macy 		func(inp, arg);
23169378e437SKip Macy 		INP_WUNLOCK(inp);
23179378e437SKip Macy 	}
2318603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
23199378e437SKip Macy }
23209378e437SKip Macy 
23219378e437SKip Macy struct socket *
23229378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
23239378e437SKip Macy {
23249378e437SKip Macy 
23259378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23269378e437SKip Macy 	return (inp->inp_socket);
23279378e437SKip Macy }
23289378e437SKip Macy 
23299378e437SKip Macy struct tcpcb *
23309378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
23319378e437SKip Macy {
23329378e437SKip Macy 
23339378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23349378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
23359378e437SKip Macy }
23369378e437SKip Macy 
23379378e437SKip Macy int
23389378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
23399378e437SKip Macy {
23409378e437SKip Macy 
23419378e437SKip Macy 	return (inp->inp_ip_tos);
23429378e437SKip Macy }
23439378e437SKip Macy 
23449378e437SKip Macy void
23459378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
23469378e437SKip Macy {
23479378e437SKip Macy 
23489378e437SKip Macy 	inp->inp_ip_tos = val;
23499378e437SKip Macy }
23509378e437SKip Macy 
23519378e437SKip Macy void
2352df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
23539d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
23549378e437SKip Macy {
23559378e437SKip Macy 
23569d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2357df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2358df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
23599378e437SKip Macy 	*lp = inp->inp_lport;
23609378e437SKip Macy 	*fp = inp->inp_fport;
23619378e437SKip Macy }
23629378e437SKip Macy 
2363dd0e6c38SKip Macy struct inpcb *
2364dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2365dd0e6c38SKip Macy {
2366dd0e6c38SKip Macy 
2367dd0e6c38SKip Macy 	return (sotoinpcb(so));
2368dd0e6c38SKip Macy }
2369dd0e6c38SKip Macy 
2370dd0e6c38SKip Macy struct tcpcb *
2371dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2372dd0e6c38SKip Macy {
2373dd0e6c38SKip Macy 
2374dd0e6c38SKip Macy 	return (sototcpcb(so));
2375dd0e6c38SKip Macy }
2376dd0e6c38SKip Macy 
2377497057eeSRobert Watson #ifdef DDB
2378497057eeSRobert Watson static void
2379497057eeSRobert Watson db_print_indent(int indent)
2380497057eeSRobert Watson {
2381497057eeSRobert Watson 	int i;
2382497057eeSRobert Watson 
2383497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2384497057eeSRobert Watson 		db_printf(" ");
2385497057eeSRobert Watson }
2386497057eeSRobert Watson 
2387497057eeSRobert Watson static void
2388497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2389497057eeSRobert Watson {
2390497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2391497057eeSRobert Watson 
2392497057eeSRobert Watson 	db_print_indent(indent);
2393497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2394497057eeSRobert Watson 
2395497057eeSRobert Watson 	indent += 2;
2396497057eeSRobert Watson 
239703dc38a4SRobert Watson #ifdef INET6
2398dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2399497057eeSRobert Watson 		/* IPv6. */
2400497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2401497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
240283e521ecSBjoern A. Zeeb 	} else
240303dc38a4SRobert Watson #endif
240483e521ecSBjoern A. Zeeb 	{
2405497057eeSRobert Watson 		/* IPv4. */
2406497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2407497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2408497057eeSRobert Watson 	}
2409497057eeSRobert Watson 	db_print_indent(indent);
2410497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2411497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2412497057eeSRobert Watson 	db_print_indent(indent);
2413497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2414497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2415497057eeSRobert Watson }
2416497057eeSRobert Watson 
2417497057eeSRobert Watson static void
2418497057eeSRobert Watson db_print_inpflags(int inp_flags)
2419497057eeSRobert Watson {
2420497057eeSRobert Watson 	int comma;
2421497057eeSRobert Watson 
2422497057eeSRobert Watson 	comma = 0;
2423497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2424497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2425497057eeSRobert Watson 		comma = 1;
2426497057eeSRobert Watson 	}
2427497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2428497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2429497057eeSRobert Watson 		comma = 1;
2430497057eeSRobert Watson 	}
2431497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2432497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2433497057eeSRobert Watson 		comma = 1;
2434497057eeSRobert Watson 	}
2435497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2436497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2437497057eeSRobert Watson 		comma = 1;
2438497057eeSRobert Watson 	}
2439497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2440497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2441497057eeSRobert Watson 		comma = 1;
2442497057eeSRobert Watson 	}
2443497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2444497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2445497057eeSRobert Watson 		comma = 1;
2446497057eeSRobert Watson 	}
2447497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2448497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2449497057eeSRobert Watson 		comma = 1;
2450497057eeSRobert Watson 	}
2451497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2452497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2453497057eeSRobert Watson 		comma = 1;
2454497057eeSRobert Watson 	}
2455497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2456497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2457497057eeSRobert Watson 		comma = 1;
2458497057eeSRobert Watson 	}
2459497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2460497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2461497057eeSRobert Watson 		comma = 1;
2462497057eeSRobert Watson 	}
2463497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2464497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2465497057eeSRobert Watson 		comma = 1;
2466497057eeSRobert Watson 	}
24673cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
24683cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
24693cca425bSMichael Tuexen 		comma = 1;
24703cca425bSMichael Tuexen 	}
2471497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2472497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2473497057eeSRobert Watson 		comma = 1;
2474497057eeSRobert Watson 	}
2475497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2476497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2477497057eeSRobert Watson 		comma = 1;
2478497057eeSRobert Watson 	}
2479497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2480497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2481497057eeSRobert Watson 		comma = 1;
2482497057eeSRobert Watson 	}
2483497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2484497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2485497057eeSRobert Watson 		comma = 1;
2486497057eeSRobert Watson 	}
2487497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2488497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2489497057eeSRobert Watson 		comma = 1;
2490497057eeSRobert Watson 	}
2491497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2492497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2493497057eeSRobert Watson 		comma = 1;
2494497057eeSRobert Watson 	}
2495497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2496497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2497497057eeSRobert Watson 		comma = 1;
2498497057eeSRobert Watson 	}
2499497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2500497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2501497057eeSRobert Watson 		comma = 1;
2502497057eeSRobert Watson 	}
2503497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2504497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2505497057eeSRobert Watson 		comma = 1;
2506497057eeSRobert Watson 	}
2507ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2508ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2509ad71fe3cSRobert Watson 		comma  = 1;
2510ad71fe3cSRobert Watson 	}
2511ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2512ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2513ad71fe3cSRobert Watson 		comma  = 1;
2514ad71fe3cSRobert Watson 	}
2515ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2516ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2517ad71fe3cSRobert Watson 		comma  = 1;
2518ad71fe3cSRobert Watson 	}
2519ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2520ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2521ad71fe3cSRobert Watson 		comma  = 1;
2522ad71fe3cSRobert Watson 	}
2523497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2524497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2525497057eeSRobert Watson 		comma = 1;
2526497057eeSRobert Watson 	}
2527497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2528497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2529497057eeSRobert Watson 		comma = 1;
2530497057eeSRobert Watson 	}
2531497057eeSRobert Watson }
2532497057eeSRobert Watson 
2533497057eeSRobert Watson static void
2534497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2535497057eeSRobert Watson {
2536497057eeSRobert Watson 	int comma;
2537497057eeSRobert Watson 
2538497057eeSRobert Watson 	comma = 0;
2539497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2540497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2541497057eeSRobert Watson 		comma  = 1;
2542497057eeSRobert Watson 	}
2543497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2544497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2545497057eeSRobert Watson 		comma  = 1;
2546497057eeSRobert Watson 	}
2547497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2548497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2549497057eeSRobert Watson 		comma  = 1;
2550497057eeSRobert Watson 	}
2551497057eeSRobert Watson }
2552497057eeSRobert Watson 
25536d888973SRobert Watson static void
2554497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2555497057eeSRobert Watson {
2556497057eeSRobert Watson 
2557497057eeSRobert Watson 	db_print_indent(indent);
2558497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2559497057eeSRobert Watson 
2560497057eeSRobert Watson 	indent += 2;
2561497057eeSRobert Watson 
2562497057eeSRobert Watson 	db_print_indent(indent);
2563497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2564497057eeSRobert Watson 
2565497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2566497057eeSRobert Watson 
2567497057eeSRobert Watson 	db_print_indent(indent);
2568497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2569497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2570497057eeSRobert Watson 
2571497057eeSRobert Watson 	db_print_indent(indent);
2572497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2573497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2574497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2575497057eeSRobert Watson 	db_printf(")\n");
2576497057eeSRobert Watson 
2577497057eeSRobert Watson 	db_print_indent(indent);
2578497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2579497057eeSRobert Watson 	    inp->inp_vflag);
2580497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2581497057eeSRobert Watson 	db_printf(")\n");
2582497057eeSRobert Watson 
2583497057eeSRobert Watson 	db_print_indent(indent);
2584497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2585497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2586497057eeSRobert Watson 
2587497057eeSRobert Watson 	db_print_indent(indent);
2588497057eeSRobert Watson #ifdef INET6
2589497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2590497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2591497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2592497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2593497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2594497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2595497057eeSRobert Watson 		    inp->in6p_hops);
2596497057eeSRobert Watson 	} else
2597497057eeSRobert Watson #endif
2598497057eeSRobert Watson 	{
2599497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2600497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2601497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2602497057eeSRobert Watson 	}
2603497057eeSRobert Watson 
2604497057eeSRobert Watson 	db_print_indent(indent);
2605497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2606497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2607497057eeSRobert Watson }
2608497057eeSRobert Watson 
2609497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2610497057eeSRobert Watson {
2611497057eeSRobert Watson 	struct inpcb *inp;
2612497057eeSRobert Watson 
2613497057eeSRobert Watson 	if (!have_addr) {
2614497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2615497057eeSRobert Watson 		return;
2616497057eeSRobert Watson 	}
2617497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2618497057eeSRobert Watson 
2619497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2620497057eeSRobert Watson }
262183e521ecSBjoern A. Zeeb #endif /* DDB */
2622