xref: /freebsd/sys/netinet/in_pcb.c (revision d5bb8bd3150b45084f7d49e8725ffef6ac4deedc)
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>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
52aae49dd3SBjoern A. Zeeb #include <sys/callout.h>
53cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
54df8bae1dSRodney W. Grimes #include <sys/protosw.h>
55df8bae1dSRodney W. Grimes #include <sys/socket.h>
56df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
57acd3428bSRobert Watson #include <sys/priv.h>
58df8bae1dSRodney W. Grimes #include <sys/proc.h>
5979bdc6e5SRobert Watson #include <sys/refcount.h>
6075c13541SPoul-Henning Kamp #include <sys/jail.h>
61101f9fc8SPeter Wemm #include <sys/kernel.h>
62101f9fc8SPeter Wemm #include <sys/sysctl.h>
638781d8e9SBruce Evans 
64497057eeSRobert Watson #ifdef DDB
65497057eeSRobert Watson #include <ddb/ddb.h>
66497057eeSRobert Watson #endif
67497057eeSRobert Watson 
6869c2d429SJeff Roberson #include <vm/uma.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <net/if.h>
7176039bc8SGleb Smirnoff #include <net/if_var.h>
72cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
73df8bae1dSRodney W. Grimes #include <net/route.h>
74530c0060SRobert Watson #include <net/vnet.h>
75df8bae1dSRodney W. Grimes 
7667107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
77df8bae1dSRodney W. Grimes #include <netinet/in.h>
78df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
797527624eSRobert Watson #include <netinet/in_rss.h>
80df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
81340c35deSJonathan Lemon #include <netinet/tcp_var.h>
825f311da2SMike Silbersack #include <netinet/udp.h>
835f311da2SMike Silbersack #include <netinet/udp_var.h>
8467107f45SBjoern A. Zeeb #endif
8567107f45SBjoern A. Zeeb #ifdef INET
8667107f45SBjoern A. Zeeb #include <netinet/in_var.h>
8767107f45SBjoern A. Zeeb #endif
88cfa1ca9dSYoshinobu Inoue #ifdef INET6
89cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
90efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
9167107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
9267107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
93cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
94cfa1ca9dSYoshinobu Inoue 
95df8bae1dSRodney W. Grimes 
96b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
97b9234fafSSam Leffler #include <netipsec/ipsec.h>
98b9234fafSSam Leffler #include <netipsec/key.h>
99b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
100b9234fafSSam Leffler 
101aed55708SRobert Watson #include <security/mac/mac_framework.h>
102aed55708SRobert Watson 
103aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
104aae49dd3SBjoern A. Zeeb 
105101f9fc8SPeter Wemm /*
106101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
107101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
108101f9fc8SPeter Wemm  */
109eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
110eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
111eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
112eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
115101f9fc8SPeter Wemm 
116b0d22693SCrist J. Clark /*
117b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
118b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
119b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
120b0d22693SCrist J. Clark  */
121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
123b0d22693SCrist J. Clark 
1245f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1303e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount);
131eddfbb76SRobert Watson 
1321e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1336ac48b74SMike Silbersack 
134d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
135d2025bd0SBjoern A. Zeeb #ifdef INET
136fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
137fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
138fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
139fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
14067107f45SBjoern A. Zeeb 
141bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
142bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
143bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
144bbd42ad0SPeter Wemm 
145bbd42ad0SPeter Wemm static int
14682d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
147bbd42ad0SPeter Wemm {
14830a4ab08SBruce Evans 	int error;
14930a4ab08SBruce Evans 
150f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
15130a4ab08SBruce Evans 	if (error == 0) {
152603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
153603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
154603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
155603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
156603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
157603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
158bbd42ad0SPeter Wemm 	}
15930a4ab08SBruce Evans 	return (error);
160bbd42ad0SPeter Wemm }
161bbd42ad0SPeter Wemm 
162bbd42ad0SPeter Wemm #undef RANGECHK
163bbd42ad0SPeter Wemm 
1646472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
1656472ac3dSEd Schouten     "IP Ports");
16633b3ac06SPeter Wemm 
167eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
168eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
1698b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
170eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
171eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
1728b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
173eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
174eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
1758b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
176eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
177eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
1788b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
179eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
180eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
1818b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
182eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
183eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
1848b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
185eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
186eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
187eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
188eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
189eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
190eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
191eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
192eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
1936ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
194eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
195eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
1968b615593SMarko Zec 	"Minimum time to keep sequental port "
1976ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
19883e521ecSBjoern A. Zeeb #endif /* INET */
1990312fbe9SPoul-Henning Kamp 
200c3229e05SDavid Greenman /*
201c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
202c3229e05SDavid Greenman  *
203de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
204de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
205de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
206c3229e05SDavid Greenman  */
207c3229e05SDavid Greenman 
208c3229e05SDavid Greenman /*
2099bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
2109bcd427bSRobert Watson  * arguments in time.
2119bcd427bSRobert Watson  */
2129bcd427bSRobert Watson void
2139bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
2149bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
2159bcd427bSRobert Watson     char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
21652cd27cbSRobert Watson     uint32_t inpcbzone_flags, u_int hashfields)
2179bcd427bSRobert Watson {
2189bcd427bSRobert Watson 
2199bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
220fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
2219bcd427bSRobert Watson #ifdef VIMAGE
2229bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
2239bcd427bSRobert Watson #endif
2249bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
2259bcd427bSRobert Watson 	LIST_INIT(pcbinfo->ipi_listhead);
226fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
2279bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
2289bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
2299bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
2309bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
23152cd27cbSRobert Watson #ifdef PCBGROUP
23252cd27cbSRobert Watson 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
23352cd27cbSRobert Watson #endif
2349bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
2359bcd427bSRobert Watson 	    NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
2369bcd427bSRobert Watson 	    inpcbzone_flags);
2379bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
2386acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
2396acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
2409bcd427bSRobert Watson }
2419bcd427bSRobert Watson 
2429bcd427bSRobert Watson /*
2439bcd427bSRobert Watson  * Destroy an inpcbinfo.
2449bcd427bSRobert Watson  */
2459bcd427bSRobert Watson void
2469bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
2479bcd427bSRobert Watson {
2489bcd427bSRobert Watson 
249fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
250fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
251fa046d87SRobert Watson 
2529bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
2539bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
2549bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
25552cd27cbSRobert Watson #ifdef PCBGROUP
25652cd27cbSRobert Watson 	in_pcbgroup_destroy(pcbinfo);
25752cd27cbSRobert Watson #endif
2589bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
259fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
2609bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
2619bcd427bSRobert Watson }
2629bcd427bSRobert Watson 
2639bcd427bSRobert Watson /*
264c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
265d915b280SStephan Uphoff  * On success return with the PCB locked.
266c3229e05SDavid Greenman  */
267df8bae1dSRodney W. Grimes int
268d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
269df8bae1dSRodney W. Grimes {
270136d4f1cSRobert Watson 	struct inpcb *inp;
27113cf67f3SHajimu UMEMOTO 	int error;
272a557af22SRobert Watson 
27359daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
274a557af22SRobert Watson 	error = 0;
275d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
276df8bae1dSRodney W. Grimes 	if (inp == NULL)
277df8bae1dSRodney W. Grimes 		return (ENOBUFS);
278d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
27915bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
280df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
28186d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
2828b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
283a557af22SRobert Watson #ifdef MAC
28430d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
285a557af22SRobert Watson 	if (error != 0)
286a557af22SRobert Watson 		goto out;
28730d239bcSRobert Watson 	mac_inpcb_create(so, inp);
288a557af22SRobert Watson #endif
289b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
29013cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
2910bffde27SRobert Watson 	if (error != 0) {
2920bffde27SRobert Watson #ifdef MAC
2930bffde27SRobert Watson 		mac_inpcb_destroy(inp);
2940bffde27SRobert Watson #endif
295a557af22SRobert Watson 		goto out;
2960bffde27SRobert Watson 	}
297b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
298e3fd5ffdSRobert Watson #ifdef INET6
299340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
300340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
301603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
30233841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
303340c35deSJonathan Lemon 	}
30475daea93SPaul Saab #endif
305712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
3063d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
307df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
30833841545SHajimu UMEMOTO #ifdef INET6
309603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
31033841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
31133841545SHajimu UMEMOTO #endif
3128501a69cSRobert Watson 	INP_WLOCK(inp);
313d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
31479bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
315b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
316a557af22SRobert Watson out:
31786d02c5cSBjoern A. Zeeb 	if (error != 0) {
31886d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
319a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
32086d02c5cSBjoern A. Zeeb 	}
321a557af22SRobert Watson #endif
322a557af22SRobert Watson 	return (error);
323df8bae1dSRodney W. Grimes }
324df8bae1dSRodney W. Grimes 
32567107f45SBjoern A. Zeeb #ifdef INET
326df8bae1dSRodney W. Grimes int
327136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
328df8bae1dSRodney W. Grimes {
3294b932371SIan Dowse 	int anonport, error;
3304b932371SIan Dowse 
3318501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
332fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
33359daba27SSam Leffler 
3344b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
3354b932371SIan Dowse 		return (EINVAL);
3365cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
3374b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
338b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
3394b932371SIan Dowse 	if (error)
3404b932371SIan Dowse 		return (error);
3414b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
3424b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
3434b932371SIan Dowse 		inp->inp_lport = 0;
3444b932371SIan Dowse 		return (EAGAIN);
3454b932371SIan Dowse 	}
3464b932371SIan Dowse 	if (anonport)
3474b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
3484b932371SIan Dowse 	return (0);
3494b932371SIan Dowse }
35067107f45SBjoern A. Zeeb #endif
3514b932371SIan Dowse 
352efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
353efc76f72SBjoern A. Zeeb int
354efc76f72SBjoern A. Zeeb in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
35568e0d7e0SRobert Watson     struct ucred *cred, int lookupflags)
356efc76f72SBjoern A. Zeeb {
357efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
358efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
359efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
360efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
361efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
362efc76f72SBjoern A. Zeeb #ifdef INET
363efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
364efc76f72SBjoern A. Zeeb #endif
365efc76f72SBjoern A. Zeeb 
366efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
367efc76f72SBjoern A. Zeeb 
368efc76f72SBjoern A. Zeeb 	/*
369efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
370efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
371efc76f72SBjoern A. Zeeb 	 */
372efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
373fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
374efc76f72SBjoern A. Zeeb 
375efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
376efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
377efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
378efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
379efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
380efc76f72SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
381efc76f72SBjoern A. Zeeb 		if (error)
382efc76f72SBjoern A. Zeeb 			return (error);
383efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
384efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
385efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
386efc76f72SBjoern A. Zeeb 	} else {
387efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
388efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
389efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
390efc76f72SBjoern A. Zeeb 	}
391efc76f72SBjoern A. Zeeb 	/*
392e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
393efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
394efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
395efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
396efc76f72SBjoern A. Zeeb 	 */
397efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
398e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
399e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
400efc76f72SBjoern A. Zeeb 		dorandom = 1;
401efc76f72SBjoern A. Zeeb 	else
402efc76f72SBjoern A. Zeeb 		dorandom = 0;
403efc76f72SBjoern A. Zeeb 	/*
404efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
405efc76f72SBjoern A. Zeeb 	 * we have the only port available.
406efc76f72SBjoern A. Zeeb 	 */
407efc76f72SBjoern A. Zeeb 	if (first == last)
408efc76f72SBjoern A. Zeeb 		dorandom = 0;
409e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
410e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
411efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
412efc76f72SBjoern A. Zeeb 	/*
413efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
414efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
415efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
416efc76f72SBjoern A. Zeeb 	 */
417efc76f72SBjoern A. Zeeb 	if (first > last) {
418efc76f72SBjoern A. Zeeb 		aux = first;
419efc76f72SBjoern A. Zeeb 		first = last;
420efc76f72SBjoern A. Zeeb 		last = aux;
421efc76f72SBjoern A. Zeeb 	}
422efc76f72SBjoern A. Zeeb 
423efc76f72SBjoern A. Zeeb #ifdef INET
424efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
425efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
4264d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
427efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
428efc76f72SBjoern A. Zeeb 		    __func__, inp));
429efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
430efc76f72SBjoern A. Zeeb 	}
431efc76f72SBjoern A. Zeeb #endif
43267107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
433efc76f72SBjoern A. Zeeb 	lport = *lportp;
434efc76f72SBjoern A. Zeeb 
435efc76f72SBjoern A. Zeeb 	if (dorandom)
436efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
437efc76f72SBjoern A. Zeeb 
438efc76f72SBjoern A. Zeeb 	count = last - first;
439efc76f72SBjoern A. Zeeb 
440efc76f72SBjoern A. Zeeb 	do {
441efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
442efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
443efc76f72SBjoern A. Zeeb 		++*lastport;
444efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
445efc76f72SBjoern A. Zeeb 			*lastport = first;
446efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
447efc76f72SBjoern A. Zeeb 
448efc76f72SBjoern A. Zeeb #ifdef INET6
449efc76f72SBjoern A. Zeeb 		if ((inp->inp_vflag & INP_IPV6) != 0)
450efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
45168e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
452efc76f72SBjoern A. Zeeb #endif
453efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
454efc76f72SBjoern A. Zeeb 		else
455efc76f72SBjoern A. Zeeb #endif
456efc76f72SBjoern A. Zeeb #ifdef INET
457efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
45868e0d7e0SRobert Watson 			    lport, lookupflags, cred);
459efc76f72SBjoern A. Zeeb #endif
460efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
461efc76f72SBjoern A. Zeeb 
462efc76f72SBjoern A. Zeeb #ifdef INET
4634d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
464efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
465efc76f72SBjoern A. Zeeb #endif
466efc76f72SBjoern A. Zeeb 	*lportp = lport;
467efc76f72SBjoern A. Zeeb 
468efc76f72SBjoern A. Zeeb 	return (0);
469efc76f72SBjoern A. Zeeb }
470efdf104bSMikolaj Golub 
471efdf104bSMikolaj Golub /*
472efdf104bSMikolaj Golub  * Return cached socket options.
473efdf104bSMikolaj Golub  */
474efdf104bSMikolaj Golub short
475efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
476efdf104bSMikolaj Golub {
477efdf104bSMikolaj Golub    short so_options;
478efdf104bSMikolaj Golub 
479efdf104bSMikolaj Golub    so_options = 0;
480efdf104bSMikolaj Golub 
481efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
482efdf104bSMikolaj Golub 	   so_options |= SO_REUSEPORT;
483efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
484efdf104bSMikolaj Golub 	   so_options |= SO_REUSEADDR;
485efdf104bSMikolaj Golub    return (so_options);
486efdf104bSMikolaj Golub }
487efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
488efc76f72SBjoern A. Zeeb 
4894b932371SIan Dowse /*
4900a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
4910a100a6fSAdrian Chadd  *
4920a100a6fSAdrian Chadd  * ni points to the new inp.
4930a100a6fSAdrian Chadd  * oi points to the exisitng inp.
4940a100a6fSAdrian Chadd  *
4950a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
4960a100a6fSAdrian Chadd  * whether the credentials match.
4970a100a6fSAdrian Chadd  */
498*d5bb8bd3SAdrian Chadd int
4990a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
5000a100a6fSAdrian Chadd {
5010a100a6fSAdrian Chadd 	/* Check permissions match */
5020a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5030a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
5040a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
5050a100a6fSAdrian Chadd 		return (0);
5060a100a6fSAdrian Chadd 
5070a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
5080a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5090a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
5100a100a6fSAdrian Chadd 		return (0);
5110a100a6fSAdrian Chadd 
5120a100a6fSAdrian Chadd 	/*
5130a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
5140a100a6fSAdrian Chadd 	 * it is and it matches the checks.
5150a100a6fSAdrian Chadd 	 */
5160a100a6fSAdrian Chadd 	return (1);
5170a100a6fSAdrian Chadd }
5180a100a6fSAdrian Chadd 
519*d5bb8bd3SAdrian Chadd #ifdef INET
5200a100a6fSAdrian Chadd /*
5214b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
5224b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
5234b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
5244b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
5254b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
5264b932371SIan Dowse  *
5274b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
5284b932371SIan Dowse  */
5294b932371SIan Dowse int
530136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
531136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
5324b932371SIan Dowse {
5334b932371SIan Dowse 	struct socket *so = inp->inp_socket;
53415bd2b43SDavid Greenman 	struct sockaddr_in *sin;
535c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
5364b932371SIan Dowse 	struct in_addr laddr;
537df8bae1dSRodney W. Grimes 	u_short lport = 0;
53868e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
539413628a7SBjoern A. Zeeb 	int error;
540df8bae1dSRodney W. Grimes 
5418501a69cSRobert Watson 	/*
542fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
5438501a69cSRobert Watson 	 */
54459daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
545fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
54659daba27SSam Leffler 
547603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
548df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5494b932371SIan Dowse 	laddr.s_addr = *laddrp;
5504b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
551df8bae1dSRodney W. Grimes 		return (EINVAL);
552c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
55368e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
5547c2f3cb9SJamie Gritton 	if (nam == NULL) {
5557c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
5567c2f3cb9SJamie Gritton 			return (error);
5577c2f3cb9SJamie Gritton 	} else {
55857bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
55957bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
560df8bae1dSRodney W. Grimes 			return (EINVAL);
561df8bae1dSRodney W. Grimes #ifdef notdef
562df8bae1dSRodney W. Grimes 		/*
563df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
564df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
565df8bae1dSRodney W. Grimes 		 */
566df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
567df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
568df8bae1dSRodney W. Grimes #endif
569b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
570b89e82ddSJamie Gritton 		if (error)
571b89e82ddSJamie Gritton 			return (error);
5724b932371SIan Dowse 		if (sin->sin_port != *lportp) {
5734b932371SIan Dowse 			/* Don't allow the port to change. */
5744b932371SIan Dowse 			if (*lportp != 0)
5754b932371SIan Dowse 				return (EINVAL);
576df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
5774b932371SIan Dowse 		}
5784b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
579df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
580df8bae1dSRodney W. Grimes 			/*
581df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
582df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
583df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
584df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
585df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
586df8bae1dSRodney W. Grimes 			 */
587f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
588df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
589df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
590df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
59183103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
5924209e01aSAdrian Chadd 			/*
5934209e01aSAdrian Chadd 			 * Is the address a local IP address?
594f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
5958696873dSAdrian Chadd 			 * to any endpoint address, local or not.
5964209e01aSAdrian Chadd 			 */
597f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
5988896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
599df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
600df8bae1dSRodney W. Grimes 		}
6014b932371SIan Dowse 		laddr = sin->sin_addr;
602df8bae1dSRodney W. Grimes 		if (lport) {
603df8bae1dSRodney W. Grimes 			struct inpcb *t;
604ae0e7143SRobert Watson 			struct tcptw *tw;
605ae0e7143SRobert Watson 
606df8bae1dSRodney W. Grimes 			/* GROSS */
607603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
608603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
609acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
61032f9753cSRobert Watson 			    0))
6112469dd60SGarrett Wollman 				return (EACCES);
612835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
61386d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
61432f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
615078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
616413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
617340c35deSJonathan Lemon 	/*
618340c35deSJonathan Lemon 	 * XXX
619340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
620340c35deSJonathan Lemon 	 */
6214cc20ab1SSeigo Tanimura 				if (t &&
6220a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
623ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
6244658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
6254658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
6264cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
62752b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
628fc06cd42SMikolaj Golub 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
62986d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
63086d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
6314049a042SGuido van Rooij 					return (EADDRINUSE);
6320a100a6fSAdrian Chadd 
6330a100a6fSAdrian Chadd 				/*
6340a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
6350a100a6fSAdrian Chadd 				 * the credentials need to match and the
6360a100a6fSAdrian Chadd 				 * original socket also has to have been bound
6370a100a6fSAdrian Chadd 				 * with BINDMULTI.
6380a100a6fSAdrian Chadd 				 */
6390a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6400a100a6fSAdrian Chadd 					return (EADDRINUSE);
6414049a042SGuido van Rooij 			}
642c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
64368e0d7e0SRobert Watson 			    lport, lookupflags, cred);
644ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
645ae0e7143SRobert Watson 				/*
646ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
647ae0e7143SRobert Watson 				 * state recycled, we treat the address as
648ae0e7143SRobert Watson 				 * being in use (for now).  This is better
649ae0e7143SRobert Watson 				 * than a panic, but not desirable.
650ae0e7143SRobert Watson 				 */
651ec95b709SMikolaj Golub 				tw = intotw(t);
652ae0e7143SRobert Watson 				if (tw == NULL ||
653ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
654340c35deSJonathan Lemon 					return (EADDRINUSE);
6550a100a6fSAdrian Chadd 			} else if (t &&
6560a100a6fSAdrian Chadd 			    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
6570a100a6fSAdrian Chadd 			    (reuseport & inp_so_options(t)) == 0) {
658e3fd5ffdSRobert Watson #ifdef INET6
65933841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
660cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
661cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
662cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
663fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
664fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
665e3fd5ffdSRobert Watson #endif
666df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
6670a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6680a100a6fSAdrian Chadd 					return (EADDRINUSE);
669df8bae1dSRodney W. Grimes 			}
670cfa1ca9dSYoshinobu Inoue 		}
671df8bae1dSRodney W. Grimes 	}
6724b932371SIan Dowse 	if (*lportp != 0)
6734b932371SIan Dowse 		lport = *lportp;
67433b3ac06SPeter Wemm 	if (lport == 0) {
67568e0d7e0SRobert Watson 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
676efc76f72SBjoern A. Zeeb 		if (error != 0)
677efc76f72SBjoern A. Zeeb 			return (error);
67833b3ac06SPeter Wemm 
67933b3ac06SPeter Wemm 	}
6804b932371SIan Dowse 	*laddrp = laddr.s_addr;
6814b932371SIan Dowse 	*lportp = lport;
682df8bae1dSRodney W. Grimes 	return (0);
683df8bae1dSRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
685999f1343SGarrett Wollman /*
6865200e00eSIan Dowse  * Connect from a socket to a specified address.
6875200e00eSIan Dowse  * Both address and port must be specified in argument sin.
6885200e00eSIan Dowse  * If don't have a local address for this socket yet,
6895200e00eSIan Dowse  * then pick one.
690999f1343SGarrett Wollman  */
691999f1343SGarrett Wollman int
692d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
693d3c1f003SRobert Watson     struct ucred *cred, struct mbuf *m)
694999f1343SGarrett Wollman {
6955200e00eSIan Dowse 	u_short lport, fport;
6965200e00eSIan Dowse 	in_addr_t laddr, faddr;
6975200e00eSIan Dowse 	int anonport, error;
698df8bae1dSRodney W. Grimes 
6998501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
700fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
70127f74fd0SRobert Watson 
7025200e00eSIan Dowse 	lport = inp->inp_lport;
7035200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
7045200e00eSIan Dowse 	anonport = (lport == 0);
7055200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
706b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
7075200e00eSIan Dowse 	if (error)
7085200e00eSIan Dowse 		return (error);
7095200e00eSIan Dowse 
7105200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
7115200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
7125200e00eSIan Dowse 		inp->inp_lport = lport;
7135200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
7145200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
7155200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
7165200e00eSIan Dowse 			inp->inp_lport = 0;
7175200e00eSIan Dowse 			return (EAGAIN);
7185200e00eSIan Dowse 		}
7195200e00eSIan Dowse 	}
7205200e00eSIan Dowse 
7215200e00eSIan Dowse 	/* Commit the remaining changes. */
7225200e00eSIan Dowse 	inp->inp_lport = lport;
7235200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
7245200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
7255200e00eSIan Dowse 	inp->inp_fport = fport;
726d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, m);
7272cb64cb2SGeorge V. Neville-Neil 
7285200e00eSIan Dowse 	if (anonport)
7295200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
7305200e00eSIan Dowse 	return (0);
7315200e00eSIan Dowse }
7325200e00eSIan Dowse 
733d3c1f003SRobert Watson int
734d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
735d3c1f003SRobert Watson {
736d3c1f003SRobert Watson 
737d3c1f003SRobert Watson 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
738d3c1f003SRobert Watson }
739d3c1f003SRobert Watson 
7405200e00eSIan Dowse /*
7410895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
7420895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
7430895aec3SBjoern A. Zeeb  */
744ae190832SSteven Hartland int
7450895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
7460895aec3SBjoern A. Zeeb     struct ucred *cred)
7470895aec3SBjoern A. Zeeb {
7480895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
7490895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
7500895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
7510895aec3SBjoern A. Zeeb 	struct route sro;
7520895aec3SBjoern A. Zeeb 	int error;
7530895aec3SBjoern A. Zeeb 
754413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
7550895aec3SBjoern A. Zeeb 
756592bcae8SBjoern A. Zeeb 	/*
757592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
758592bcae8SBjoern A. Zeeb 	 * if requested.
759592bcae8SBjoern A. Zeeb 	 */
760592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
761592bcae8SBjoern A. Zeeb 		return (0);
762592bcae8SBjoern A. Zeeb 
7630895aec3SBjoern A. Zeeb 	error = 0;
7640895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
7650895aec3SBjoern A. Zeeb 
7660895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
7670895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
7680895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
7690895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
7700895aec3SBjoern A. Zeeb 
7710895aec3SBjoern A. Zeeb 	/*
7720895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
7730895aec3SBjoern A. Zeeb 	 * else punt.
7740895aec3SBjoern A. Zeeb 	 *
7750895aec3SBjoern A. Zeeb 	 * Find out route to destination.
7760895aec3SBjoern A. Zeeb 	 */
7770895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
7786e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
7790895aec3SBjoern A. Zeeb 
7800895aec3SBjoern A. Zeeb 	/*
7810895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
7820895aec3SBjoern A. Zeeb 	 * the outgoing interface.
7830895aec3SBjoern A. Zeeb 	 *
7840895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
7850895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
7860895aec3SBjoern A. Zeeb 	 * the source address from.
7870895aec3SBjoern A. Zeeb 	 */
7880895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
7898c0fec80SRobert Watson 		struct in_ifaddr *ia;
7900895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
7910895aec3SBjoern A. Zeeb 
7922f308a34SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
7930895aec3SBjoern A. Zeeb 		if (ia == NULL)
7942f308a34SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
7950895aec3SBjoern A. Zeeb 		if (ia == NULL) {
7960895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
7970895aec3SBjoern A. Zeeb 			goto done;
7980895aec3SBjoern A. Zeeb 		}
7990895aec3SBjoern A. Zeeb 
8000304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8010895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8028c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8030895aec3SBjoern A. Zeeb 			goto done;
8040895aec3SBjoern A. Zeeb 		}
8050895aec3SBjoern A. Zeeb 
8060895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
8078c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
8080895aec3SBjoern A. Zeeb 		ia = NULL;
809137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8100895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8110895aec3SBjoern A. Zeeb 
8120895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8130895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8140895aec3SBjoern A. Zeeb 				continue;
8150895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
816b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8170895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8180895aec3SBjoern A. Zeeb 				break;
8190895aec3SBjoern A. Zeeb 			}
8200895aec3SBjoern A. Zeeb 		}
8210895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8220895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
823137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8240895aec3SBjoern A. Zeeb 			goto done;
8250895aec3SBjoern A. Zeeb 		}
826137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8270895aec3SBjoern A. Zeeb 
8280895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
829b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8300895aec3SBjoern A. Zeeb 		goto done;
8310895aec3SBjoern A. Zeeb 	}
8320895aec3SBjoern A. Zeeb 
8330895aec3SBjoern A. Zeeb 	/*
8340895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
8350895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
8360895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
8370895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
8380895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
8390895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
8400895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
8410895aec3SBjoern A. Zeeb 	 */
8420895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
8438c0fec80SRobert Watson 		struct in_ifaddr *ia;
8449317b04eSRobert Watson 		struct ifnet *ifp;
8450895aec3SBjoern A. Zeeb 
8460895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
8470304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8480895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8490895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8500895aec3SBjoern A. Zeeb 			goto done;
8510895aec3SBjoern A. Zeeb 		}
8520895aec3SBjoern A. Zeeb 
8530895aec3SBjoern A. Zeeb 		/* Jailed. */
8540895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
8550895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
856b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8570895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8580895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8590895aec3SBjoern A. Zeeb 			goto done;
8600895aec3SBjoern A. Zeeb 		}
8610895aec3SBjoern A. Zeeb 
8620895aec3SBjoern A. Zeeb 		/*
8630895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
8640895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
8650895aec3SBjoern A. Zeeb 		 */
8668c0fec80SRobert Watson 		ia = NULL;
8679317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
868137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8699317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8700895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8710895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8720895aec3SBjoern A. Zeeb 				continue;
8730895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
874b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8750895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8760895aec3SBjoern A. Zeeb 				break;
8770895aec3SBjoern A. Zeeb 			}
8780895aec3SBjoern A. Zeeb 		}
8790895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8800895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
881137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8820895aec3SBjoern A. Zeeb 			goto done;
8830895aec3SBjoern A. Zeeb 		}
884137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8850895aec3SBjoern A. Zeeb 
8860895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
887b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8880895aec3SBjoern A. Zeeb 		goto done;
8890895aec3SBjoern A. Zeeb 	}
8900895aec3SBjoern A. Zeeb 
8910895aec3SBjoern A. Zeeb 	/*
8920895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
8930895aec3SBjoern A. Zeeb 	 * to ourselves is here.
8940895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
8950895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
8960895aec3SBjoern A. Zeeb 	 * a loopback interface.
8970895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
8980895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
8990895aec3SBjoern A. Zeeb 	 */
9000895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
9010895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
9028c0fec80SRobert Watson 		struct in_ifaddr *ia;
9030895aec3SBjoern A. Zeeb 
9040895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
9050895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
9060895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
9070895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
9080895aec3SBjoern A. Zeeb 
9092f308a34SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
9100895aec3SBjoern A. Zeeb 		if (ia == NULL)
9112f308a34SAlan Somers 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
912f0bb05fcSQing Li 		if (ia == NULL)
913f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
9140895aec3SBjoern A. Zeeb 
9150304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
9160895aec3SBjoern A. Zeeb 			if (ia == NULL) {
9170895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
9180895aec3SBjoern A. Zeeb 				goto done;
9190895aec3SBjoern A. Zeeb 			}
9200895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
9218c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9220895aec3SBjoern A. Zeeb 			goto done;
9230895aec3SBjoern A. Zeeb 		}
9240895aec3SBjoern A. Zeeb 
9250895aec3SBjoern A. Zeeb 		/* Jailed. */
9260895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9270895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
9280895aec3SBjoern A. Zeeb 
9290895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
9308c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9310895aec3SBjoern A. Zeeb 			ia = NULL;
932137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
9330895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9340895aec3SBjoern A. Zeeb 
9350895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
9360895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
9370895aec3SBjoern A. Zeeb 					continue;
9380895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
939b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
940b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
9410895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
9420895aec3SBjoern A. Zeeb 					break;
9430895aec3SBjoern A. Zeeb 				}
9440895aec3SBjoern A. Zeeb 			}
9450895aec3SBjoern A. Zeeb 			if (ia != NULL) {
9460895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
947137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
9480895aec3SBjoern A. Zeeb 				goto done;
9490895aec3SBjoern A. Zeeb 			}
950137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9510895aec3SBjoern A. Zeeb 		}
9520895aec3SBjoern A. Zeeb 
9530895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
954b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9550895aec3SBjoern A. Zeeb 		goto done;
9560895aec3SBjoern A. Zeeb 	}
9570895aec3SBjoern A. Zeeb 
9580895aec3SBjoern A. Zeeb done:
9590895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
9600895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
9610895aec3SBjoern A. Zeeb 	return (error);
9620895aec3SBjoern A. Zeeb }
9630895aec3SBjoern A. Zeeb 
9640895aec3SBjoern A. Zeeb /*
9655200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
9665200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
9675200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
9685200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
9695200e00eSIan Dowse  * the connect.
9705200e00eSIan Dowse  *
9715200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
9725200e00eSIan Dowse  * and port. These are not updated in the error case.
9735200e00eSIan Dowse  *
9745200e00eSIan Dowse  * If the operation fails because the connection already exists,
9755200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
9765200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
9775200e00eSIan Dowse  * is set to NULL.
9785200e00eSIan Dowse  */
9795200e00eSIan Dowse int
980136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
981136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
982136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
9835200e00eSIan Dowse {
9845200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
9855200e00eSIan Dowse 	struct in_ifaddr *ia;
9865200e00eSIan Dowse 	struct inpcb *oinp;
987b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
9885200e00eSIan Dowse 	u_short lport, fport;
9895200e00eSIan Dowse 	int error;
9905200e00eSIan Dowse 
9918501a69cSRobert Watson 	/*
9928501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
9938501a69cSRobert Watson 	 * lock is sufficient.
9948501a69cSRobert Watson 	 */
99527f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
996fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
99727f74fd0SRobert Watson 
9985200e00eSIan Dowse 	if (oinpp != NULL)
9995200e00eSIan Dowse 		*oinpp = NULL;
100057bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
1001df8bae1dSRodney W. Grimes 		return (EINVAL);
1002df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
1003df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
1004df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1005df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
10065200e00eSIan Dowse 	laddr.s_addr = *laddrp;
10075200e00eSIan Dowse 	lport = *lportp;
10085200e00eSIan Dowse 	faddr = sin->sin_addr;
10095200e00eSIan Dowse 	fport = sin->sin_port;
10100895aec3SBjoern A. Zeeb 
1011603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1012df8bae1dSRodney W. Grimes 		/*
1013df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1014df8bae1dSRodney W. Grimes 		 * use the primary local address.
1015df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1016df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1017df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1018df8bae1dSRodney W. Grimes 		 */
1019413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
10202d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
1021413628a7SBjoern A. Zeeb 			faddr =
1022b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
10232d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
1024b89e82ddSJamie Gritton 			if (cred != NULL &&
1025b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1026b89e82ddSJamie Gritton 				return (error);
10272d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
10282d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
10292d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
10302d9cfabaSRobert Watson 			    IFF_BROADCAST)
10315200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
1032603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
10332d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
10342d9cfabaSRobert Watson 		}
1035df8bae1dSRodney W. Grimes 	}
10365200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1037d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1038df8bae1dSRodney W. Grimes 		/*
1039df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1040d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1041df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1042df8bae1dSRodney W. Grimes 		 */
10435200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1044df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1045df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1046df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1049df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1050df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
10512d9cfabaSRobert Watson 				IN_IFADDR_RLOCK();
1052e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1053e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1054e691be70SDaniel Eischen 					    (cred == NULL ||
1055e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1056e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1057df8bae1dSRodney W. Grimes 						break;
1058e691be70SDaniel Eischen 				}
1059e691be70SDaniel Eischen 				if (ia == NULL)
1060d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1061e691be70SDaniel Eischen 				else {
10625200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1063d79fdd98SDaniel Eischen 					error = 0;
1064999f1343SGarrett Wollman 				}
1065e691be70SDaniel Eischen 				IN_IFADDR_RUNLOCK();
1066d79fdd98SDaniel Eischen 			}
1067d79fdd98SDaniel Eischen 		}
106804215ed2SRandall Stewart 		if (error)
106904215ed2SRandall Stewart 			return (error);
10700895aec3SBjoern A. Zeeb 	}
1071fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1072fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
10735200e00eSIan Dowse 	if (oinp != NULL) {
10745200e00eSIan Dowse 		if (oinpp != NULL)
10755200e00eSIan Dowse 			*oinpp = oinp;
1076df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1077c3229e05SDavid Greenman 	}
10785200e00eSIan Dowse 	if (lport == 0) {
1079b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1080b0330ed9SPawel Jakub Dawidek 		    cred);
10815a903f8dSPierre Beyssac 		if (error)
10825a903f8dSPierre Beyssac 			return (error);
10835a903f8dSPierre Beyssac 	}
10845200e00eSIan Dowse 	*laddrp = laddr.s_addr;
10855200e00eSIan Dowse 	*lportp = lport;
10865200e00eSIan Dowse 	*faddrp = faddr.s_addr;
10875200e00eSIan Dowse 	*fportp = fport;
1088df8bae1dSRodney W. Grimes 	return (0);
1089df8bae1dSRodney W. Grimes }
1090df8bae1dSRodney W. Grimes 
109126f9a767SRodney W. Grimes void
1092136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1093df8bae1dSRodney W. Grimes {
10946b348152SRobert Watson 
10958501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1096fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1099df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
110015bd2b43SDavid Greenman 	in_pcbrehash(inp);
1101df8bae1dSRodney W. Grimes }
110283e521ecSBjoern A. Zeeb #endif /* INET */
1103df8bae1dSRodney W. Grimes 
11044c7c478dSRobert Watson /*
110528696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1106c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
110728696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
110828696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
11094c7c478dSRobert Watson  */
111026f9a767SRodney W. Grimes void
1111136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1112df8bae1dSRodney W. Grimes {
11134c7c478dSRobert Watson 
1114a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1115c0a211c5SRobert Watson 
11164c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
11174c7c478dSRobert Watson 	inp->inp_socket = NULL;
11184c7c478dSRobert Watson }
11194c7c478dSRobert Watson 
1120c0a211c5SRobert Watson /*
112179bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
112279bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
112379bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
112452cd27cbSRobert Watson  * but where the inpcb lock may already held, or when acquiring a reference
112552cd27cbSRobert Watson  * via a pcbgroup.
112679bdc6e5SRobert Watson  *
112779bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
112879bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
112979bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
113079bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
113179bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
113279bdc6e5SRobert Watson  * inpcb.
113379bdc6e5SRobert Watson  *
113479bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
113579bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
113679bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
113779bdc6e5SRobert Watson  * using in_pcbrele().
1138c0a211c5SRobert Watson  */
113979bdc6e5SRobert Watson void
114079bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
11414c7c478dSRobert Watson {
1142df8bae1dSRodney W. Grimes 
114379bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
114479bdc6e5SRobert Watson 
114579bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
114679bdc6e5SRobert Watson }
114779bdc6e5SRobert Watson 
114879bdc6e5SRobert Watson /*
114979bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
115079bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
115179bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
115279bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
115379bdc6e5SRobert Watson  *
115479bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
115579bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
115679bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
115779bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
115879bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
115979bdc6e5SRobert Watson  */
116079bdc6e5SRobert Watson int
116179bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
116279bdc6e5SRobert Watson {
116379bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
116479bdc6e5SRobert Watson 
116579bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
116679bdc6e5SRobert Watson 
116779bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
116879bdc6e5SRobert Watson 
1169df4e91d3SGleb Smirnoff 	if (refcount_release(&inp->inp_refcount) == 0) {
1170df4e91d3SGleb Smirnoff 		/*
1171df4e91d3SGleb Smirnoff 		 * If the inpcb has been freed, let the caller know, even if
1172df4e91d3SGleb Smirnoff 		 * this isn't the last reference.
1173df4e91d3SGleb Smirnoff 		 */
1174df4e91d3SGleb Smirnoff 		if (inp->inp_flags2 & INP_FREED) {
1175df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1176df4e91d3SGleb Smirnoff 			return (1);
1177df4e91d3SGleb Smirnoff 		}
117879bdc6e5SRobert Watson 		return (0);
1179df4e91d3SGleb Smirnoff 	}
118079bdc6e5SRobert Watson 
118179bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
118279bdc6e5SRobert Watson 
118379bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
118479bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
118579bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
118679bdc6e5SRobert Watson 	return (1);
118779bdc6e5SRobert Watson }
118879bdc6e5SRobert Watson 
118979bdc6e5SRobert Watson int
119079bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
119179bdc6e5SRobert Watson {
119279bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
119379bdc6e5SRobert Watson 
119479bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
119579bdc6e5SRobert Watson 
119679bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
119779bdc6e5SRobert Watson 
119879bdc6e5SRobert Watson 	if (refcount_release(&inp->inp_refcount) == 0)
119979bdc6e5SRobert Watson 		return (0);
120079bdc6e5SRobert Watson 
120179bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
120279bdc6e5SRobert Watson 
120379bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
120479bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
120579bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
120679bdc6e5SRobert Watson 	return (1);
120779bdc6e5SRobert Watson }
120879bdc6e5SRobert Watson 
120979bdc6e5SRobert Watson /*
121079bdc6e5SRobert Watson  * Temporary wrapper.
121179bdc6e5SRobert Watson  */
121279bdc6e5SRobert Watson int
121379bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
121479bdc6e5SRobert Watson {
121579bdc6e5SRobert Watson 
121679bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
121779bdc6e5SRobert Watson }
121879bdc6e5SRobert Watson 
121979bdc6e5SRobert Watson /*
122079bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
122179bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
122279bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
122379bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
122479bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
122579bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
122679bdc6e5SRobert Watson  * the pcbinfo lock is held.
122779bdc6e5SRobert Watson  */
122879bdc6e5SRobert Watson void
122979bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
123079bdc6e5SRobert Watson {
123179bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
123279bdc6e5SRobert Watson 
123379bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
123479bdc6e5SRobert Watson 
123579bdc6e5SRobert Watson 	INP_INFO_WLOCK_ASSERT(pcbinfo);
123679bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
123779bdc6e5SRobert Watson 
123879bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1239b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
12406aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
12416974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
124283e521ecSBjoern A. Zeeb #endif
124379bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1244c3229e05SDavid Greenman 	in_pcbremlists(inp);
12456aee2fc5SBjoern A. Zeeb #ifdef INET6
12466aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
12476aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
12481096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
12496aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
12506aee2fc5SBjoern A. Zeeb 	}
12516aee2fc5SBjoern A. Zeeb #endif
1252df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1253df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
125467107f45SBjoern A. Zeeb #ifdef INET
125571498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
125671498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
125767107f45SBjoern A. Zeeb #endif
1258cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1259df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
126086d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1261a557af22SRobert Watson #ifdef MAC
126230d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1263a557af22SRobert Watson #endif
126479bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
126528696211SRobert Watson 		INP_WUNLOCK(inp);
126628696211SRobert Watson }
126728696211SRobert Watson 
126828696211SRobert Watson /*
1269c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1270c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1271c0a211c5SRobert Watson  *
1272c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1273c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1274c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1275c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1276c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1277c0a211c5SRobert Watson  * in_pcbdetach().
1278c0a211c5SRobert Watson  *
1279c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1280c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
128110702a28SRobert Watson  */
128210702a28SRobert Watson void
128310702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
128410702a28SRobert Watson {
128510702a28SRobert Watson 
12868501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
128710702a28SRobert Watson 
1288fa046d87SRobert Watson 	/*
1289fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1290fa046d87SRobert Watson 	 * the hash lock...?
1291fa046d87SRobert Watson 	 */
1292ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1293111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
129410702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
129510702a28SRobert Watson 
1296fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
129710702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
129810702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
129910702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
130010702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
130110702a28SRobert Watson 			free(phd, M_PCB);
130210702a28SRobert Watson 		}
1303fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1304111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
130552cd27cbSRobert Watson #ifdef PCBGROUP
130652cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
130752cd27cbSRobert Watson #endif
130810702a28SRobert Watson 	}
130910702a28SRobert Watson }
131010702a28SRobert Watson 
131167107f45SBjoern A. Zeeb #ifdef INET
131254d642bbSRobert Watson /*
131354d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
131454d642bbSRobert Watson  */
131526ef6ac4SDon Lewis struct sockaddr *
1316136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
131726ef6ac4SDon Lewis {
131826ef6ac4SDon Lewis 	struct sockaddr_in *sin;
131926ef6ac4SDon Lewis 
13201ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1321a163d034SWarner Losh 		M_WAITOK | M_ZERO);
132226ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
132326ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
132426ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
132526ef6ac4SDon Lewis 	sin->sin_port = port;
132626ef6ac4SDon Lewis 
132726ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
132826ef6ac4SDon Lewis }
132926ef6ac4SDon Lewis 
1330117bcae7SGarrett Wollman int
133154d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1332df8bae1dSRodney W. Grimes {
1333136d4f1cSRobert Watson 	struct inpcb *inp;
133426ef6ac4SDon Lewis 	struct in_addr addr;
133526ef6ac4SDon Lewis 	in_port_t port;
133642fa505bSDavid Greenman 
1337fdc984f7STor Egge 	inp = sotoinpcb(so);
133854d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
13396466b28aSRobert Watson 
1340a69042a5SRobert Watson 	INP_RLOCK(inp);
134126ef6ac4SDon Lewis 	port = inp->inp_lport;
134226ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1343a69042a5SRobert Watson 	INP_RUNLOCK(inp);
134442fa505bSDavid Greenman 
134526ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1346117bcae7SGarrett Wollman 	return 0;
1347df8bae1dSRodney W. Grimes }
1348df8bae1dSRodney W. Grimes 
1349117bcae7SGarrett Wollman int
135054d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1351df8bae1dSRodney W. Grimes {
1352136d4f1cSRobert Watson 	struct inpcb *inp;
135326ef6ac4SDon Lewis 	struct in_addr addr;
135426ef6ac4SDon Lewis 	in_port_t port;
135542fa505bSDavid Greenman 
1356fdc984f7STor Egge 	inp = sotoinpcb(so);
135754d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
13586466b28aSRobert Watson 
1359a69042a5SRobert Watson 	INP_RLOCK(inp);
136026ef6ac4SDon Lewis 	port = inp->inp_fport;
136126ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1362a69042a5SRobert Watson 	INP_RUNLOCK(inp);
136342fa505bSDavid Greenman 
136426ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1365117bcae7SGarrett Wollman 	return 0;
1366df8bae1dSRodney W. Grimes }
1367df8bae1dSRodney W. Grimes 
136826f9a767SRodney W. Grimes void
1369136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1370136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1371d1c54148SJesper Skriver {
1372f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1373d1c54148SJesper Skriver 
13743dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1375f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
13768501a69cSRobert Watson 		INP_WLOCK(inp);
1377d1c54148SJesper Skriver #ifdef INET6
1378f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
13798501a69cSRobert Watson 			INP_WUNLOCK(inp);
1380d1c54148SJesper Skriver 			continue;
1381f76fcf6dSJeffrey Hsu 		}
1382d1c54148SJesper Skriver #endif
1383d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1384f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
13858501a69cSRobert Watson 			INP_WUNLOCK(inp);
1386d1c54148SJesper Skriver 			continue;
1387d1c54148SJesper Skriver 		}
13883dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
13898501a69cSRobert Watson 			INP_WUNLOCK(inp);
1390f76fcf6dSJeffrey Hsu 	}
13913dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1392d1c54148SJesper Skriver }
1393d1c54148SJesper Skriver 
1394e43cc4aeSHajimu UMEMOTO void
1395136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1396e43cc4aeSHajimu UMEMOTO {
1397e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1398e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1399e43cc4aeSHajimu UMEMOTO 	int i, gap;
1400e43cc4aeSHajimu UMEMOTO 
1401f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
1402712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
14038501a69cSRobert Watson 		INP_WLOCK(inp);
1404e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1405e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1406e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1407e43cc4aeSHajimu UMEMOTO 			/*
1408e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1409e43cc4aeSHajimu UMEMOTO 			 * detached.
1410e43cc4aeSHajimu UMEMOTO 			 */
1411e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1412e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1413e43cc4aeSHajimu UMEMOTO 
1414e43cc4aeSHajimu UMEMOTO 			/*
1415e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1416e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1417e43cc4aeSHajimu UMEMOTO 			 */
1418e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1419e43cc4aeSHajimu UMEMOTO 			    i++) {
1420e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1421e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1422e43cc4aeSHajimu UMEMOTO 					gap++;
1423e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1424e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1425e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1426e43cc4aeSHajimu UMEMOTO 			}
1427e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1428e43cc4aeSHajimu UMEMOTO 		}
14298501a69cSRobert Watson 		INP_WUNLOCK(inp);
1430e43cc4aeSHajimu UMEMOTO 	}
14313cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
1432e43cc4aeSHajimu UMEMOTO }
1433e43cc4aeSHajimu UMEMOTO 
1434df8bae1dSRodney W. Grimes /*
1435fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1436fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1437c3229e05SDavid Greenman  */
1438d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1439df8bae1dSRodney W. Grimes struct inpcb *
1440136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
144168e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1442df8bae1dSRodney W. Grimes {
1443136d4f1cSRobert Watson 	struct inpcb *inp;
1444d5e8a67eSHajimu UMEMOTO #ifdef INET6
1445d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1446d5e8a67eSHajimu UMEMOTO #else
1447d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1448d5e8a67eSHajimu UMEMOTO #endif
1449d5e8a67eSHajimu UMEMOTO 	int wildcard;
14507bc4aca7SDavid Greenman 
145168e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
145268e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
145368e0d7e0SRobert Watson 
1454fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
14551b73ca0bSSam Leffler 
145668e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1457c3229e05SDavid Greenman 		struct inpcbhead *head;
1458c3229e05SDavid Greenman 		/*
1459c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1460c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1461c3229e05SDavid Greenman 		 */
1462712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1463712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1464fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1465cfa1ca9dSYoshinobu Inoue #ifdef INET6
1466413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1467369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1468cfa1ca9dSYoshinobu Inoue 				continue;
1469cfa1ca9dSYoshinobu Inoue #endif
1470c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1471c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1472c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1473c3229e05SDavid Greenman 				/*
1474413628a7SBjoern A. Zeeb 				 * Found?
1475c3229e05SDavid Greenman 				 */
1476413628a7SBjoern A. Zeeb 				if (cred == NULL ||
14770304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
14780304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1479c3229e05SDavid Greenman 					return (inp);
1480df8bae1dSRodney W. Grimes 			}
1481c3229e05SDavid Greenman 		}
1482c3229e05SDavid Greenman 		/*
1483c3229e05SDavid Greenman 		 * Not found.
1484c3229e05SDavid Greenman 		 */
1485c3229e05SDavid Greenman 		return (NULL);
1486c3229e05SDavid Greenman 	} else {
1487c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1488c3229e05SDavid Greenman 		struct inpcbport *phd;
1489c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1490c3229e05SDavid Greenman 		/*
1491c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1492c3229e05SDavid Greenman 		 *
1493c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1494c3229e05SDavid Greenman 		 * port hash list.
1495c3229e05SDavid Greenman 		 */
1496712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1497712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1498fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1499c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1500c3229e05SDavid Greenman 				break;
1501c3229e05SDavid Greenman 		}
1502c3229e05SDavid Greenman 		if (phd != NULL) {
1503c3229e05SDavid Greenman 			/*
1504c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1505c3229e05SDavid Greenman 			 * fit.
1506c3229e05SDavid Greenman 			 */
150737d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1508c3229e05SDavid Greenman 				wildcard = 0;
1509413628a7SBjoern A. Zeeb 				if (cred != NULL &&
15100304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
15110304c731SJamie Gritton 					cred->cr_prison))
1512413628a7SBjoern A. Zeeb 					continue;
1513cfa1ca9dSYoshinobu Inoue #ifdef INET6
1514413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1515369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1516cfa1ca9dSYoshinobu Inoue 					continue;
1517d5e8a67eSHajimu UMEMOTO 				/*
1518d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1519d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1520d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1521d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1522d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1523d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1524d5e8a67eSHajimu UMEMOTO 				 *
1525d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1526d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1527d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1528d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1529d5e8a67eSHajimu UMEMOTO 				 */
1530d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1531d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1532cfa1ca9dSYoshinobu Inoue #endif
1533c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1534c3229e05SDavid Greenman 					wildcard++;
153515bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
153615bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
153715bd2b43SDavid Greenman 						wildcard++;
153815bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
153915bd2b43SDavid Greenman 						continue;
154015bd2b43SDavid Greenman 				} else {
154115bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
154215bd2b43SDavid Greenman 						wildcard++;
154315bd2b43SDavid Greenman 				}
1544df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1545df8bae1dSRodney W. Grimes 					match = inp;
1546df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1547413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1548df8bae1dSRodney W. Grimes 						break;
1549df8bae1dSRodney W. Grimes 				}
1550df8bae1dSRodney W. Grimes 			}
15513dbdc25cSDavid Greenman 		}
1552df8bae1dSRodney W. Grimes 		return (match);
1553df8bae1dSRodney W. Grimes 	}
1554c3229e05SDavid Greenman }
1555d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
155615bd2b43SDavid Greenman 
155752cd27cbSRobert Watson #ifdef PCBGROUP
155852cd27cbSRobert Watson /*
155952cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
156052cd27cbSRobert Watson  */
156152cd27cbSRobert Watson static struct inpcb *
156252cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
156352cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
156452cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
156552cd27cbSRobert Watson {
156652cd27cbSRobert Watson 	struct inpcbhead *head;
156752cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
156852cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
156952cd27cbSRobert Watson 
157052cd27cbSRobert Watson 	/*
157152cd27cbSRobert Watson 	 * First look for an exact match.
157252cd27cbSRobert Watson 	 */
157352cd27cbSRobert Watson 	tmpinp = NULL;
157452cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
157552cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
157652cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
157752cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
157852cd27cbSRobert Watson #ifdef INET6
157952cd27cbSRobert Watson 		/* XXX inp locking */
158052cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
158152cd27cbSRobert Watson 			continue;
158252cd27cbSRobert Watson #endif
158352cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
158452cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
158552cd27cbSRobert Watson 		    inp->inp_fport == fport &&
158652cd27cbSRobert Watson 		    inp->inp_lport == lport) {
158752cd27cbSRobert Watson 			/*
158852cd27cbSRobert Watson 			 * XXX We should be able to directly return
158952cd27cbSRobert Watson 			 * the inp here, without any checks.
159052cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
159152cd27cbSRobert Watson 			 */
159252cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
159352cd27cbSRobert Watson 				goto found;
159452cd27cbSRobert Watson 			if (tmpinp == NULL)
159552cd27cbSRobert Watson 				tmpinp = inp;
159652cd27cbSRobert Watson 		}
159752cd27cbSRobert Watson 	}
159852cd27cbSRobert Watson 	if (tmpinp != NULL) {
159952cd27cbSRobert Watson 		inp = tmpinp;
160052cd27cbSRobert Watson 		goto found;
160152cd27cbSRobert Watson 	}
160252cd27cbSRobert Watson 
16030a100a6fSAdrian Chadd #ifdef	RSS
16040a100a6fSAdrian Chadd 	/*
16050a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
16060a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
16070a100a6fSAdrian Chadd 	 */
16080a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
16090a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
16100a100a6fSAdrian Chadd #ifdef INET6
16110a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
16120a100a6fSAdrian Chadd #endif
16130a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
16140a100a6fSAdrian Chadd 		struct inpcbhead *head;
16150a100a6fSAdrian Chadd 		int injail;
16160a100a6fSAdrian Chadd 
16170a100a6fSAdrian Chadd 		/*
16180a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
16190a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
16200a100a6fSAdrian Chadd 		 *      2. jailed, wild.
16210a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
16220a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
16230a100a6fSAdrian Chadd 		 */
16240a100a6fSAdrian Chadd 
16250a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
16260a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
16270a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
16280a100a6fSAdrian Chadd #ifdef INET6
16290a100a6fSAdrian Chadd 			/* XXX inp locking */
16300a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
16310a100a6fSAdrian Chadd 				continue;
16320a100a6fSAdrian Chadd #endif
16330a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
16340a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
16350a100a6fSAdrian Chadd 				continue;
16360a100a6fSAdrian Chadd 
16370a100a6fSAdrian Chadd 			/* XXX inp locking */
16380a100a6fSAdrian Chadd 			if (ifp && ifp->if_type == IFT_FAITH &&
16390a100a6fSAdrian Chadd 			    (inp->inp_flags & INP_FAITH) == 0)
16400a100a6fSAdrian Chadd 				continue;
16410a100a6fSAdrian Chadd 
16420a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
16430a100a6fSAdrian Chadd 			if (injail) {
16440a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
16450a100a6fSAdrian Chadd 				    &laddr) != 0)
16460a100a6fSAdrian Chadd 					continue;
16470a100a6fSAdrian Chadd 			} else {
16480a100a6fSAdrian Chadd 				if (local_exact != NULL)
16490a100a6fSAdrian Chadd 					continue;
16500a100a6fSAdrian Chadd 			}
16510a100a6fSAdrian Chadd 
16520a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
16530a100a6fSAdrian Chadd 				if (injail)
16540a100a6fSAdrian Chadd 					goto found;
16550a100a6fSAdrian Chadd 				else
16560a100a6fSAdrian Chadd 					local_exact = inp;
16570a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
16580a100a6fSAdrian Chadd #ifdef INET6
16590a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
16600a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
16610a100a6fSAdrian Chadd 					local_wild_mapped = inp;
16620a100a6fSAdrian Chadd 				else
16630a100a6fSAdrian Chadd #endif
16640a100a6fSAdrian Chadd 					if (injail)
16650a100a6fSAdrian Chadd 						jail_wild = inp;
16660a100a6fSAdrian Chadd 					else
16670a100a6fSAdrian Chadd 						local_wild = inp;
16680a100a6fSAdrian Chadd 			}
16690a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
16700a100a6fSAdrian Chadd 
16710a100a6fSAdrian Chadd 		inp = jail_wild;
16720a100a6fSAdrian Chadd 		if (inp == NULL)
16730a100a6fSAdrian Chadd 			inp = local_exact;
16740a100a6fSAdrian Chadd 		if (inp == NULL)
16750a100a6fSAdrian Chadd 			inp = local_wild;
16760a100a6fSAdrian Chadd #ifdef INET6
16770a100a6fSAdrian Chadd 		if (inp == NULL)
16780a100a6fSAdrian Chadd 			inp = local_wild_mapped;
16790a100a6fSAdrian Chadd #endif
16800a100a6fSAdrian Chadd 		if (inp != NULL)
16810a100a6fSAdrian Chadd 			goto found;
16820a100a6fSAdrian Chadd 	}
16830a100a6fSAdrian Chadd #endif
16840a100a6fSAdrian Chadd 
168552cd27cbSRobert Watson 	/*
168652cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
168752cd27cbSRobert Watson 	 */
168852cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
168952cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
169052cd27cbSRobert Watson #ifdef INET6
169152cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
169252cd27cbSRobert Watson #endif
169352cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
169452cd27cbSRobert Watson 		struct inpcbhead *head;
169552cd27cbSRobert Watson 		int injail;
169652cd27cbSRobert Watson 
169752cd27cbSRobert Watson 		/*
169852cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
169952cd27cbSRobert Watson 		 *      1. jailed, non-wild.
170052cd27cbSRobert Watson 		 *      2. jailed, wild.
170152cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
170252cd27cbSRobert Watson 		 *      4. non-jailed, wild.
170352cd27cbSRobert Watson 		 */
170452cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
170552cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
170652cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
170752cd27cbSRobert Watson #ifdef INET6
170852cd27cbSRobert Watson 			/* XXX inp locking */
170952cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
171052cd27cbSRobert Watson 				continue;
171152cd27cbSRobert Watson #endif
171252cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
171352cd27cbSRobert Watson 			    inp->inp_lport != lport)
171452cd27cbSRobert Watson 				continue;
171552cd27cbSRobert Watson 
171652cd27cbSRobert Watson 			/* XXX inp locking */
171752cd27cbSRobert Watson 			if (ifp && ifp->if_type == IFT_FAITH &&
171852cd27cbSRobert Watson 			    (inp->inp_flags & INP_FAITH) == 0)
171952cd27cbSRobert Watson 				continue;
172052cd27cbSRobert Watson 
172152cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
172252cd27cbSRobert Watson 			if (injail) {
172352cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
172452cd27cbSRobert Watson 				    &laddr) != 0)
172552cd27cbSRobert Watson 					continue;
172652cd27cbSRobert Watson 			} else {
172752cd27cbSRobert Watson 				if (local_exact != NULL)
172852cd27cbSRobert Watson 					continue;
172952cd27cbSRobert Watson 			}
173052cd27cbSRobert Watson 
173152cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
173252cd27cbSRobert Watson 				if (injail)
173352cd27cbSRobert Watson 					goto found;
173452cd27cbSRobert Watson 				else
173552cd27cbSRobert Watson 					local_exact = inp;
173652cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
173752cd27cbSRobert Watson #ifdef INET6
173852cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
173952cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
174052cd27cbSRobert Watson 					local_wild_mapped = inp;
174152cd27cbSRobert Watson 				else
174283e521ecSBjoern A. Zeeb #endif
174352cd27cbSRobert Watson 					if (injail)
174452cd27cbSRobert Watson 						jail_wild = inp;
174552cd27cbSRobert Watson 					else
174652cd27cbSRobert Watson 						local_wild = inp;
174752cd27cbSRobert Watson 			}
174852cd27cbSRobert Watson 		} /* LIST_FOREACH */
174952cd27cbSRobert Watson 		inp = jail_wild;
175052cd27cbSRobert Watson 		if (inp == NULL)
175152cd27cbSRobert Watson 			inp = local_exact;
175252cd27cbSRobert Watson 		if (inp == NULL)
175352cd27cbSRobert Watson 			inp = local_wild;
175452cd27cbSRobert Watson #ifdef INET6
175552cd27cbSRobert Watson 		if (inp == NULL)
175652cd27cbSRobert Watson 			inp = local_wild_mapped;
175783e521ecSBjoern A. Zeeb #endif
175852cd27cbSRobert Watson 		if (inp != NULL)
175952cd27cbSRobert Watson 			goto found;
176052cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
176152cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
176252cd27cbSRobert Watson 	return (NULL);
176352cd27cbSRobert Watson 
176452cd27cbSRobert Watson found:
176552cd27cbSRobert Watson 	in_pcbref(inp);
176652cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
176752cd27cbSRobert Watson 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
176852cd27cbSRobert Watson 		INP_WLOCK(inp);
176952cd27cbSRobert Watson 		if (in_pcbrele_wlocked(inp))
177052cd27cbSRobert Watson 			return (NULL);
177152cd27cbSRobert Watson 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
177252cd27cbSRobert Watson 		INP_RLOCK(inp);
177352cd27cbSRobert Watson 		if (in_pcbrele_rlocked(inp))
177452cd27cbSRobert Watson 			return (NULL);
177552cd27cbSRobert Watson 	} else
177652cd27cbSRobert Watson 		panic("%s: locking bug", __func__);
177752cd27cbSRobert Watson 	return (inp);
177852cd27cbSRobert Watson }
177952cd27cbSRobert Watson #endif /* PCBGROUP */
178052cd27cbSRobert Watson 
178115bd2b43SDavid Greenman /*
1782fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1783fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1784fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
178515bd2b43SDavid Greenman  */
1786fa046d87SRobert Watson static struct inpcb *
1787fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
178868e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1789136d4f1cSRobert Watson     struct ifnet *ifp)
179015bd2b43SDavid Greenman {
179115bd2b43SDavid Greenman 	struct inpcbhead *head;
1792413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
179315bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
179415bd2b43SDavid Greenman 
179568e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
179668e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
179768e0d7e0SRobert Watson 
1798fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1799602cc7f1SRobert Watson 
180015bd2b43SDavid Greenman 	/*
180115bd2b43SDavid Greenman 	 * First look for an exact match.
180215bd2b43SDavid Greenman 	 */
1803413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1804712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1805712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1806fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1807cfa1ca9dSYoshinobu Inoue #ifdef INET6
1808413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1809369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1810cfa1ca9dSYoshinobu Inoue 			continue;
1811cfa1ca9dSYoshinobu Inoue #endif
18126d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1813ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1814ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1815413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1816413628a7SBjoern A. Zeeb 			/*
1817413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1818413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1819413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1820413628a7SBjoern A. Zeeb 			 */
18210304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1822c3229e05SDavid Greenman 				return (inp);
1823413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1824413628a7SBjoern A. Zeeb 				tmpinp = inp;
1825c3229e05SDavid Greenman 		}
1826413628a7SBjoern A. Zeeb 	}
1827413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1828413628a7SBjoern A. Zeeb 		return (tmpinp);
1829e3fd5ffdSRobert Watson 
1830e3fd5ffdSRobert Watson 	/*
1831e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1832e3fd5ffdSRobert Watson 	 */
183368e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1834413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1835e3fd5ffdSRobert Watson #ifdef INET6
1836cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1837e3fd5ffdSRobert Watson #endif
1838413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1839413628a7SBjoern A. Zeeb 		int injail;
1840413628a7SBjoern A. Zeeb 
1841413628a7SBjoern A. Zeeb 		/*
1842413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1843413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1844413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1845413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1846413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1847413628a7SBjoern A. Zeeb 		 */
18486d6a026bSDavid Greenman 
1849712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1850712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1851fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1852cfa1ca9dSYoshinobu Inoue #ifdef INET6
1853413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1854369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1855cfa1ca9dSYoshinobu Inoue 				continue;
1856cfa1ca9dSYoshinobu Inoue #endif
1857413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1858413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1859413628a7SBjoern A. Zeeb 				continue;
1860413628a7SBjoern A. Zeeb 
1861413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1862cfa1ca9dSYoshinobu Inoue 			if (ifp && ifp->if_type == IFT_FAITH &&
1863cfa1ca9dSYoshinobu Inoue 			    (inp->inp_flags & INP_FAITH) == 0)
1864cfa1ca9dSYoshinobu Inoue 				continue;
1865413628a7SBjoern A. Zeeb 
18660304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1867413628a7SBjoern A. Zeeb 			if (injail) {
1868b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1869b89e82ddSJamie Gritton 				    &laddr) != 0)
1870413628a7SBjoern A. Zeeb 					continue;
1871413628a7SBjoern A. Zeeb 			} else {
1872413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1873413628a7SBjoern A. Zeeb 					continue;
1874413628a7SBjoern A. Zeeb 			}
1875413628a7SBjoern A. Zeeb 
1876413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1877413628a7SBjoern A. Zeeb 				if (injail)
1878c3229e05SDavid Greenman 					return (inp);
1879413628a7SBjoern A. Zeeb 				else
1880413628a7SBjoern A. Zeeb 					local_exact = inp;
1881413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1882e3fd5ffdSRobert Watson #ifdef INET6
1883413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
18845cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1885cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1886cfa1ca9dSYoshinobu Inoue 				else
188783e521ecSBjoern A. Zeeb #endif
1888413628a7SBjoern A. Zeeb 					if (injail)
1889413628a7SBjoern A. Zeeb 						jail_wild = inp;
1890413628a7SBjoern A. Zeeb 					else
18916d6a026bSDavid Greenman 						local_wild = inp;
18926d6a026bSDavid Greenman 			}
1893413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1894413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1895413628a7SBjoern A. Zeeb 			return (jail_wild);
1896413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1897413628a7SBjoern A. Zeeb 			return (local_exact);
1898413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1899c3229e05SDavid Greenman 			return (local_wild);
1900413628a7SBjoern A. Zeeb #ifdef INET6
1901413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1902413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
190383e521ecSBjoern A. Zeeb #endif
190468e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1905413628a7SBjoern A. Zeeb 
19066d6a026bSDavid Greenman 	return (NULL);
190715bd2b43SDavid Greenman }
1908fa046d87SRobert Watson 
1909fa046d87SRobert Watson /*
1910fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1911fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1912fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1913fa046d87SRobert Watson  */
1914fa046d87SRobert Watson static struct inpcb *
1915fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1916fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1917fa046d87SRobert Watson     struct ifnet *ifp)
1918fa046d87SRobert Watson {
1919fa046d87SRobert Watson 	struct inpcb *inp;
1920fa046d87SRobert Watson 
1921fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1922fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1923fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1924fa046d87SRobert Watson 	if (inp != NULL) {
1925fa046d87SRobert Watson 		in_pcbref(inp);
1926fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1927fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1928fa046d87SRobert Watson 			INP_WLOCK(inp);
1929fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1930fa046d87SRobert Watson 				return (NULL);
1931fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1932fa046d87SRobert Watson 			INP_RLOCK(inp);
1933fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1934fa046d87SRobert Watson 				return (NULL);
1935fa046d87SRobert Watson 		} else
1936fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1937fa046d87SRobert Watson 	} else
1938fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1939fa046d87SRobert Watson 	return (inp);
1940fa046d87SRobert Watson }
1941fa046d87SRobert Watson 
1942fa046d87SRobert Watson /*
1943d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1944d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
194552cd27cbSRobert Watson  *
194652cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
1947fa046d87SRobert Watson  */
1948fa046d87SRobert Watson struct inpcb *
1949fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1950fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1951fa046d87SRobert Watson {
19527527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
195352cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
195452cd27cbSRobert Watson #endif
1955fa046d87SRobert Watson 
1956fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1957fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1958fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1959fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1960fa046d87SRobert Watson 
19617527624eSRobert Watson 	/*
19627527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
19637527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
19647527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
19657527624eSRobert Watson 	 * in software.
19667527624eSRobert Watson 	 *
19677527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
19687527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
19697527624eSRobert Watson 	 * in software.
19707527624eSRobert Watson 	 */
19717527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
197252cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
197352cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
197452cd27cbSRobert Watson 		    fport);
197552cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
197652cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
197752cd27cbSRobert Watson 	}
197852cd27cbSRobert Watson #endif
1979fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1980fa046d87SRobert Watson 	    lookupflags, ifp));
1981fa046d87SRobert Watson }
1982d3c1f003SRobert Watson 
1983d3c1f003SRobert Watson struct inpcb *
1984d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1985d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1986d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
1987d3c1f003SRobert Watson {
198852cd27cbSRobert Watson #ifdef PCBGROUP
198952cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
199052cd27cbSRobert Watson #endif
1991d3c1f003SRobert Watson 
1992d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1993d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1994d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1995d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1996d3c1f003SRobert Watson 
199752cd27cbSRobert Watson #ifdef PCBGROUP
19987527624eSRobert Watson 	/*
19997527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
20007527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
20017527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
20027527624eSRobert Watson 	 * using RSS.
20037527624eSRobert Watson 	 *
20047527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
20057527624eSRobert Watson 	 */
20067527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
20077527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
200852cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
200952cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
201052cd27cbSRobert Watson 		if (pcbgroup != NULL)
201152cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
201252cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
20137527624eSRobert Watson #ifndef RSS
201452cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
201552cd27cbSRobert Watson 		    fport);
201652cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
201752cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
20187527624eSRobert Watson #endif
201952cd27cbSRobert Watson 	}
202052cd27cbSRobert Watson #endif
2021d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2022d3c1f003SRobert Watson 	    lookupflags, ifp));
2023d3c1f003SRobert Watson }
202467107f45SBjoern A. Zeeb #endif /* INET */
202515bd2b43SDavid Greenman 
20267bc4aca7SDavid Greenman /*
2027c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
20287bc4aca7SDavid Greenman  */
202952cd27cbSRobert Watson static int
203052cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
203115bd2b43SDavid Greenman {
2032c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2033c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2034c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2035c3229e05SDavid Greenman 	struct inpcbport *phd;
2036cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
203715bd2b43SDavid Greenman 
20388501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2039fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2040fa046d87SRobert Watson 
2041111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2042111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2043602cc7f1SRobert Watson 
2044cfa1ca9dSYoshinobu Inoue #ifdef INET6
2045cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
2046cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
2047cfa1ca9dSYoshinobu Inoue 	else
204883e521ecSBjoern A. Zeeb #endif
2049cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2050cfa1ca9dSYoshinobu Inoue 
2051712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2052712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
205315bd2b43SDavid Greenman 
2054712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2055712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2056c3229e05SDavid Greenman 
2057c3229e05SDavid Greenman 	/*
2058c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2059c3229e05SDavid Greenman 	 */
2060fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2061c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2062c3229e05SDavid Greenman 			break;
2063c3229e05SDavid Greenman 	}
2064c3229e05SDavid Greenman 	/*
2065c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2066c3229e05SDavid Greenman 	 */
2067c3229e05SDavid Greenman 	if (phd == NULL) {
20681ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2069c3229e05SDavid Greenman 		if (phd == NULL) {
2070c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2071c3229e05SDavid Greenman 		}
2072c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2073c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2074c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2075c3229e05SDavid Greenman 	}
2076c3229e05SDavid Greenman 	inp->inp_phd = phd;
2077c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2078c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2079111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
208052cd27cbSRobert Watson #ifdef PCBGROUP
208152cd27cbSRobert Watson 	if (do_pcbgroup_update)
208252cd27cbSRobert Watson 		in_pcbgroup_update(inp);
208352cd27cbSRobert Watson #endif
2084c3229e05SDavid Greenman 	return (0);
208515bd2b43SDavid Greenman }
208615bd2b43SDavid Greenman 
2087c3229e05SDavid Greenman /*
208852cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
208952cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
209052cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
209152cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
209252cd27cbSRobert Watson  * pcbgroup until later.
209352cd27cbSRobert Watson  *
209452cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
209552cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
209652cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
209752cd27cbSRobert Watson  */
209852cd27cbSRobert Watson int
209952cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
210052cd27cbSRobert Watson {
210152cd27cbSRobert Watson 
210252cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
210352cd27cbSRobert Watson }
210452cd27cbSRobert Watson 
210552cd27cbSRobert Watson int
210652cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
210752cd27cbSRobert Watson {
210852cd27cbSRobert Watson 
210952cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
211052cd27cbSRobert Watson }
211152cd27cbSRobert Watson 
211252cd27cbSRobert Watson /*
2113c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2114c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2115c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2116c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2117c3229e05SDavid Greenman  */
211815bd2b43SDavid Greenman void
2119d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
212015bd2b43SDavid Greenman {
212159daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
212215bd2b43SDavid Greenman 	struct inpcbhead *head;
2123cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
212415bd2b43SDavid Greenman 
21258501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2126fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2127fa046d87SRobert Watson 
2128111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2129111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2130602cc7f1SRobert Watson 
2131cfa1ca9dSYoshinobu Inoue #ifdef INET6
2132cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
2133cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
2134cfa1ca9dSYoshinobu Inoue 	else
213583e521ecSBjoern A. Zeeb #endif
2136cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2137cfa1ca9dSYoshinobu Inoue 
2138712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2139712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
214015bd2b43SDavid Greenman 
2141c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
214215bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
214352cd27cbSRobert Watson 
214452cd27cbSRobert Watson #ifdef PCBGROUP
214552cd27cbSRobert Watson 	if (m != NULL)
214652cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
214752cd27cbSRobert Watson 	else
214852cd27cbSRobert Watson 		in_pcbgroup_update(inp);
214952cd27cbSRobert Watson #endif
2150c3229e05SDavid Greenman }
2151c3229e05SDavid Greenman 
2152d3c1f003SRobert Watson void
2153d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2154d3c1f003SRobert Watson {
2155d3c1f003SRobert Watson 
2156d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2157d3c1f003SRobert Watson }
2158d3c1f003SRobert Watson 
2159c3229e05SDavid Greenman /*
2160c3229e05SDavid Greenman  * Remove PCB from various lists.
2161c3229e05SDavid Greenman  */
21626d888973SRobert Watson static void
2163136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2164c3229e05SDavid Greenman {
216559daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
216659daba27SSam Leffler 
216759daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
21688501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
216959daba27SSam Leffler 
217059daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2171111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2172c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2173c3229e05SDavid Greenman 
2174fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2175c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2176c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2177fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2178c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2179c3229e05SDavid Greenman 			free(phd, M_PCB);
2180c3229e05SDavid Greenman 		}
2181fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2182111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2183c3229e05SDavid Greenman 	}
2184c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
218559daba27SSam Leffler 	pcbinfo->ipi_count--;
218652cd27cbSRobert Watson #ifdef PCBGROUP
218752cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
218852cd27cbSRobert Watson #endif
218915bd2b43SDavid Greenman }
219075c13541SPoul-Henning Kamp 
2191a557af22SRobert Watson /*
2192a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2193a557af22SRobert Watson  * label change into the in_pcb for the socket.
2194a557af22SRobert Watson  */
2195a557af22SRobert Watson void
2196136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2197a557af22SRobert Watson {
2198a557af22SRobert Watson #ifdef MAC
2199a557af22SRobert Watson 	struct inpcb *inp;
2200a557af22SRobert Watson 
22014c7c478dSRobert Watson 	inp = sotoinpcb(so);
22024c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2203602cc7f1SRobert Watson 
22048501a69cSRobert Watson 	INP_WLOCK(inp);
2205310e7cebSRobert Watson 	SOCK_LOCK(so);
2206a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2207310e7cebSRobert Watson 	SOCK_UNLOCK(so);
22088501a69cSRobert Watson 	INP_WUNLOCK(inp);
2209a557af22SRobert Watson #endif
2210a557af22SRobert Watson }
22115f311da2SMike Silbersack 
22125f311da2SMike Silbersack /*
2213ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2214ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2215ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2216ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2217ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
22185f311da2SMike Silbersack  */
2219aae49dd3SBjoern A. Zeeb static void
2220136d4f1cSRobert Watson ipport_tick(void *xtp)
22215f311da2SMike Silbersack {
22228b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2223ad3a630fSRobert Watson 
22245ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
22258b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
22268b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
22278b615593SMarko Zec 		if (V_ipport_tcpallocs <=
22288b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2229603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2230603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2231ad3a630fSRobert Watson 		} else
2232603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2233603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
22348b615593SMarko Zec 		CURVNET_RESTORE();
22358b615593SMarko Zec 	}
22365ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
22375f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
22385f311da2SMike Silbersack }
2239497057eeSRobert Watson 
2240aae49dd3SBjoern A. Zeeb static void
2241aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2242aae49dd3SBjoern A. Zeeb {
2243aae49dd3SBjoern A. Zeeb 
2244aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2245aae49dd3SBjoern A. Zeeb }
2246aae49dd3SBjoern A. Zeeb 
2247aae49dd3SBjoern A. Zeeb /*
2248aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2249aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2250aae49dd3SBjoern A. Zeeb  */
2251aae49dd3SBjoern A. Zeeb static void
2252aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2253aae49dd3SBjoern A. Zeeb {
2254aae49dd3SBjoern A. Zeeb 
2255aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2256aae49dd3SBjoern A. Zeeb 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2257aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2258aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2259aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2260aae49dd3SBjoern A. Zeeb }
2261aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2262aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2263aae49dd3SBjoern A. Zeeb 
22643d585327SKip Macy void
22653d585327SKip Macy inp_wlock(struct inpcb *inp)
22663d585327SKip Macy {
22673d585327SKip Macy 
22688501a69cSRobert Watson 	INP_WLOCK(inp);
22693d585327SKip Macy }
22703d585327SKip Macy 
22713d585327SKip Macy void
22723d585327SKip Macy inp_wunlock(struct inpcb *inp)
22733d585327SKip Macy {
22743d585327SKip Macy 
22758501a69cSRobert Watson 	INP_WUNLOCK(inp);
22763d585327SKip Macy }
22773d585327SKip Macy 
22783d585327SKip Macy void
22793d585327SKip Macy inp_rlock(struct inpcb *inp)
22803d585327SKip Macy {
22813d585327SKip Macy 
2282a69042a5SRobert Watson 	INP_RLOCK(inp);
22833d585327SKip Macy }
22843d585327SKip Macy 
22853d585327SKip Macy void
22863d585327SKip Macy inp_runlock(struct inpcb *inp)
22873d585327SKip Macy {
22883d585327SKip Macy 
2289a69042a5SRobert Watson 	INP_RUNLOCK(inp);
22903d585327SKip Macy }
22913d585327SKip Macy 
22923d585327SKip Macy #ifdef INVARIANTS
22933d585327SKip Macy void
2294e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
22953d585327SKip Macy {
22963d585327SKip Macy 
22978501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
22983d585327SKip Macy }
22993d585327SKip Macy 
23003d585327SKip Macy void
2301e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
23023d585327SKip Macy {
23033d585327SKip Macy 
23043d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
23053d585327SKip Macy }
23063d585327SKip Macy #endif
23073d585327SKip Macy 
23089378e437SKip Macy void
23099378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
23109378e437SKip Macy {
23119378e437SKip Macy 	struct inpcb *inp;
23129378e437SKip Macy 
2313603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
231497021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
23159378e437SKip Macy 		INP_WLOCK(inp);
23169378e437SKip Macy 		func(inp, arg);
23179378e437SKip Macy 		INP_WUNLOCK(inp);
23189378e437SKip Macy 	}
2319603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
23209378e437SKip Macy }
23219378e437SKip Macy 
23229378e437SKip Macy struct socket *
23239378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
23249378e437SKip Macy {
23259378e437SKip Macy 
23269378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23279378e437SKip Macy 	return (inp->inp_socket);
23289378e437SKip Macy }
23299378e437SKip Macy 
23309378e437SKip Macy struct tcpcb *
23319378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
23329378e437SKip Macy {
23339378e437SKip Macy 
23349378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
23359378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
23369378e437SKip Macy }
23379378e437SKip Macy 
23389378e437SKip Macy int
23399378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
23409378e437SKip Macy {
23419378e437SKip Macy 
23429378e437SKip Macy 	return (inp->inp_ip_tos);
23439378e437SKip Macy }
23449378e437SKip Macy 
23459378e437SKip Macy void
23469378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
23479378e437SKip Macy {
23489378e437SKip Macy 
23499378e437SKip Macy 	inp->inp_ip_tos = val;
23509378e437SKip Macy }
23519378e437SKip Macy 
23529378e437SKip Macy void
2353df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
23549d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
23559378e437SKip Macy {
23569378e437SKip Macy 
23579d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2358df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2359df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
23609378e437SKip Macy 	*lp = inp->inp_lport;
23619378e437SKip Macy 	*fp = inp->inp_fport;
23629378e437SKip Macy }
23639378e437SKip Macy 
2364dd0e6c38SKip Macy struct inpcb *
2365dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2366dd0e6c38SKip Macy {
2367dd0e6c38SKip Macy 
2368dd0e6c38SKip Macy 	return (sotoinpcb(so));
2369dd0e6c38SKip Macy }
2370dd0e6c38SKip Macy 
2371dd0e6c38SKip Macy struct tcpcb *
2372dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2373dd0e6c38SKip Macy {
2374dd0e6c38SKip Macy 
2375dd0e6c38SKip Macy 	return (sototcpcb(so));
2376dd0e6c38SKip Macy }
2377dd0e6c38SKip Macy 
2378497057eeSRobert Watson #ifdef DDB
2379497057eeSRobert Watson static void
2380497057eeSRobert Watson db_print_indent(int indent)
2381497057eeSRobert Watson {
2382497057eeSRobert Watson 	int i;
2383497057eeSRobert Watson 
2384497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2385497057eeSRobert Watson 		db_printf(" ");
2386497057eeSRobert Watson }
2387497057eeSRobert Watson 
2388497057eeSRobert Watson static void
2389497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2390497057eeSRobert Watson {
2391497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2392497057eeSRobert Watson 
2393497057eeSRobert Watson 	db_print_indent(indent);
2394497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2395497057eeSRobert Watson 
2396497057eeSRobert Watson 	indent += 2;
2397497057eeSRobert Watson 
239803dc38a4SRobert Watson #ifdef INET6
2399dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2400497057eeSRobert Watson 		/* IPv6. */
2401497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2402497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
240383e521ecSBjoern A. Zeeb 	} else
240403dc38a4SRobert Watson #endif
240583e521ecSBjoern A. Zeeb 	{
2406497057eeSRobert Watson 		/* IPv4. */
2407497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2408497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2409497057eeSRobert Watson 	}
2410497057eeSRobert Watson 	db_print_indent(indent);
2411497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2412497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2413497057eeSRobert Watson 	db_print_indent(indent);
2414497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2415497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2416497057eeSRobert Watson }
2417497057eeSRobert Watson 
2418497057eeSRobert Watson static void
2419497057eeSRobert Watson db_print_inpflags(int inp_flags)
2420497057eeSRobert Watson {
2421497057eeSRobert Watson 	int comma;
2422497057eeSRobert Watson 
2423497057eeSRobert Watson 	comma = 0;
2424497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2425497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2426497057eeSRobert Watson 		comma = 1;
2427497057eeSRobert Watson 	}
2428497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2429497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2430497057eeSRobert Watson 		comma = 1;
2431497057eeSRobert Watson 	}
2432497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2433497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2434497057eeSRobert Watson 		comma = 1;
2435497057eeSRobert Watson 	}
2436497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2437497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2438497057eeSRobert Watson 		comma = 1;
2439497057eeSRobert Watson 	}
2440497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2441497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2442497057eeSRobert Watson 		comma = 1;
2443497057eeSRobert Watson 	}
2444497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2445497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2446497057eeSRobert Watson 		comma = 1;
2447497057eeSRobert Watson 	}
2448497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2449497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2450497057eeSRobert Watson 		comma = 1;
2451497057eeSRobert Watson 	}
2452497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2453497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2454497057eeSRobert Watson 		comma = 1;
2455497057eeSRobert Watson 	}
2456497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2457497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2458497057eeSRobert Watson 		comma = 1;
2459497057eeSRobert Watson 	}
2460497057eeSRobert Watson 	if (inp_flags & INP_FAITH) {
2461497057eeSRobert Watson 		db_printf("%sINP_FAITH", comma ? ", " : "");
2462497057eeSRobert Watson 		comma = 1;
2463497057eeSRobert Watson 	}
2464497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2465497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2466497057eeSRobert Watson 		comma = 1;
2467497057eeSRobert Watson 	}
2468497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2469497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2470497057eeSRobert Watson 		comma = 1;
2471497057eeSRobert Watson 	}
24723cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
24733cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
24743cca425bSMichael Tuexen 		comma = 1;
24753cca425bSMichael Tuexen 	}
2476497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2477497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2478497057eeSRobert Watson 		comma = 1;
2479497057eeSRobert Watson 	}
2480497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2481497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2482497057eeSRobert Watson 		comma = 1;
2483497057eeSRobert Watson 	}
2484497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2485497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2486497057eeSRobert Watson 		comma = 1;
2487497057eeSRobert Watson 	}
2488497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2489497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2490497057eeSRobert Watson 		comma = 1;
2491497057eeSRobert Watson 	}
2492497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2493497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2494497057eeSRobert Watson 		comma = 1;
2495497057eeSRobert Watson 	}
2496497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2497497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2498497057eeSRobert Watson 		comma = 1;
2499497057eeSRobert Watson 	}
2500497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2501497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2502497057eeSRobert Watson 		comma = 1;
2503497057eeSRobert Watson 	}
2504497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2505497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2506497057eeSRobert Watson 		comma = 1;
2507497057eeSRobert Watson 	}
2508497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2509497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2510497057eeSRobert Watson 		comma = 1;
2511497057eeSRobert Watson 	}
2512ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2513ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2514ad71fe3cSRobert Watson 		comma  = 1;
2515ad71fe3cSRobert Watson 	}
2516ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2517ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2518ad71fe3cSRobert Watson 		comma  = 1;
2519ad71fe3cSRobert Watson 	}
2520ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2521ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2522ad71fe3cSRobert Watson 		comma  = 1;
2523ad71fe3cSRobert Watson 	}
2524ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2525ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2526ad71fe3cSRobert Watson 		comma  = 1;
2527ad71fe3cSRobert Watson 	}
2528497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2529497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2530497057eeSRobert Watson 		comma = 1;
2531497057eeSRobert Watson 	}
2532497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2533497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2534497057eeSRobert Watson 		comma = 1;
2535497057eeSRobert Watson 	}
2536497057eeSRobert Watson }
2537497057eeSRobert Watson 
2538497057eeSRobert Watson static void
2539497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2540497057eeSRobert Watson {
2541497057eeSRobert Watson 	int comma;
2542497057eeSRobert Watson 
2543497057eeSRobert Watson 	comma = 0;
2544497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2545497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2546497057eeSRobert Watson 		comma  = 1;
2547497057eeSRobert Watson 	}
2548497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2549497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2550497057eeSRobert Watson 		comma  = 1;
2551497057eeSRobert Watson 	}
2552497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2553497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2554497057eeSRobert Watson 		comma  = 1;
2555497057eeSRobert Watson 	}
2556497057eeSRobert Watson }
2557497057eeSRobert Watson 
25586d888973SRobert Watson static void
2559497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2560497057eeSRobert Watson {
2561497057eeSRobert Watson 
2562497057eeSRobert Watson 	db_print_indent(indent);
2563497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2564497057eeSRobert Watson 
2565497057eeSRobert Watson 	indent += 2;
2566497057eeSRobert Watson 
2567497057eeSRobert Watson 	db_print_indent(indent);
2568497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2569497057eeSRobert Watson 
2570497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2571497057eeSRobert Watson 
2572497057eeSRobert Watson 	db_print_indent(indent);
2573497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2574497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2575497057eeSRobert Watson 
2576497057eeSRobert Watson 	db_print_indent(indent);
2577497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2578497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2579497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2580497057eeSRobert Watson 	db_printf(")\n");
2581497057eeSRobert Watson 
2582497057eeSRobert Watson 	db_print_indent(indent);
2583497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2584497057eeSRobert Watson 	    inp->inp_vflag);
2585497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2586497057eeSRobert Watson 	db_printf(")\n");
2587497057eeSRobert Watson 
2588497057eeSRobert Watson 	db_print_indent(indent);
2589497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2590497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2591497057eeSRobert Watson 
2592497057eeSRobert Watson 	db_print_indent(indent);
2593497057eeSRobert Watson #ifdef INET6
2594497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2595497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2596497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2597497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2598497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2599497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2600497057eeSRobert Watson 		    inp->in6p_hops);
2601497057eeSRobert Watson 	} else
2602497057eeSRobert Watson #endif
2603497057eeSRobert Watson 	{
2604497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2605497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2606497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2607497057eeSRobert Watson 	}
2608497057eeSRobert Watson 
2609497057eeSRobert Watson 	db_print_indent(indent);
2610497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2611497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2612497057eeSRobert Watson }
2613497057eeSRobert Watson 
2614497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2615497057eeSRobert Watson {
2616497057eeSRobert Watson 	struct inpcb *inp;
2617497057eeSRobert Watson 
2618497057eeSRobert Watson 	if (!have_addr) {
2619497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2620497057eeSRobert Watson 		return;
2621497057eeSRobert Watson 	}
2622497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2623497057eeSRobert Watson 
2624497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2625497057eeSRobert Watson }
262683e521ecSBjoern A. Zeeb #endif /* DDB */
2627