xref: /freebsd/sys/netinet/in_pcb.c (revision d3c1f00350b5323c94b4d5a525cfc9d2be7b3547)
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"
45cfa1ca9dSYoshinobu Inoue 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50aae49dd3SBjoern A. Zeeb #include <sys/callout.h>
51cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55acd3428bSRobert Watson #include <sys/priv.h>
56df8bae1dSRodney W. Grimes #include <sys/proc.h>
5779bdc6e5SRobert Watson #include <sys/refcount.h>
5875c13541SPoul-Henning Kamp #include <sys/jail.h>
59101f9fc8SPeter Wemm #include <sys/kernel.h>
60101f9fc8SPeter Wemm #include <sys/sysctl.h>
618781d8e9SBruce Evans 
62497057eeSRobert Watson #ifdef DDB
63497057eeSRobert Watson #include <ddb/ddb.h>
64497057eeSRobert Watson #endif
65497057eeSRobert Watson 
6669c2d429SJeff Roberson #include <vm/uma.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #include <net/if.h>
69cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
70df8bae1dSRodney W. Grimes #include <net/route.h>
71530c0060SRobert Watson #include <net/vnet.h>
72df8bae1dSRodney W. Grimes 
7367107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
74df8bae1dSRodney W. Grimes #include <netinet/in.h>
75df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
76df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
77340c35deSJonathan Lemon #include <netinet/tcp_var.h>
785f311da2SMike Silbersack #include <netinet/udp.h>
795f311da2SMike Silbersack #include <netinet/udp_var.h>
8067107f45SBjoern A. Zeeb #endif
8167107f45SBjoern A. Zeeb #ifdef INET
8267107f45SBjoern A. Zeeb #include <netinet/in_var.h>
8367107f45SBjoern A. Zeeb #endif
84cfa1ca9dSYoshinobu Inoue #ifdef INET6
85cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
86efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
8767107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
8867107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
89cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
90cfa1ca9dSYoshinobu Inoue 
91df8bae1dSRodney W. Grimes 
92b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
93b9234fafSSam Leffler #include <netipsec/ipsec.h>
94b9234fafSSam Leffler #include <netipsec/key.h>
95b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
96b9234fafSSam Leffler 
97aed55708SRobert Watson #include <security/mac/mac_framework.h>
98aed55708SRobert Watson 
99aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
100aae49dd3SBjoern A. Zeeb 
101101f9fc8SPeter Wemm /*
102101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
103101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
104101f9fc8SPeter Wemm  */
105eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
106eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
107eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
108eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
109eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
110eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
111101f9fc8SPeter Wemm 
112b0d22693SCrist J. Clark /*
113b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
114b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
115b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
116b0d22693SCrist J. Clark  */
117eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
118eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
119b0d22693SCrist J. Clark 
1205f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1263e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount);
127eddfbb76SRobert Watson 
1281e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1296ac48b74SMike Silbersack 
130d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
131d2025bd0SBjoern A. Zeeb #ifdef INET
132fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
133fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
134fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
135fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
13667107f45SBjoern A. Zeeb 
137bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
138bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
139bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
140bbd42ad0SPeter Wemm 
141bbd42ad0SPeter Wemm static int
14282d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
143bbd42ad0SPeter Wemm {
14430a4ab08SBruce Evans 	int error;
14530a4ab08SBruce Evans 
146eddfbb76SRobert Watson #ifdef VIMAGE
147eddfbb76SRobert Watson 	error = vnet_sysctl_handle_int(oidp, arg1, arg2, req);
148eddfbb76SRobert Watson #else
149f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
150eddfbb76SRobert Watson #endif
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 
16433b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
16533b3ac06SPeter Wemm 
166eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
167eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
1688b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
169eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
170eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
1718b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
172eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
173eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
1748b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
175eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
176eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
1778b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
178eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
179eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
1808b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
181eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
182eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
1838b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
184eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
185eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
186eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
187eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
188eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
189eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
190eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
191eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
1926ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
193eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
194eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
1958b615593SMarko Zec 	"Minimum time to keep sequental port "
1966ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
19767107f45SBjoern A. Zeeb #endif
1980312fbe9SPoul-Henning Kamp 
199c3229e05SDavid Greenman /*
200c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
201c3229e05SDavid Greenman  *
202de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
203de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
204de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
205c3229e05SDavid Greenman  */
206c3229e05SDavid Greenman 
207c3229e05SDavid Greenman /*
2089bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
2099bcd427bSRobert Watson  * arguments in time.
2109bcd427bSRobert Watson  */
2119bcd427bSRobert Watson void
2129bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
2139bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
2149bcd427bSRobert Watson     char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
2159bcd427bSRobert Watson     uint32_t inpcbzone_flags)
2169bcd427bSRobert Watson {
2179bcd427bSRobert Watson 
2189bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
219fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
2209bcd427bSRobert Watson #ifdef VIMAGE
2219bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
2229bcd427bSRobert Watson #endif
2239bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
2249bcd427bSRobert Watson 	LIST_INIT(pcbinfo->ipi_listhead);
225fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
2269bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
2279bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
2289bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
2299bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
2309bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
2319bcd427bSRobert Watson 	    NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
2329bcd427bSRobert Watson 	    inpcbzone_flags);
2339bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
2349bcd427bSRobert Watson }
2359bcd427bSRobert Watson 
2369bcd427bSRobert Watson /*
2379bcd427bSRobert Watson  * Destroy an inpcbinfo.
2389bcd427bSRobert Watson  */
2399bcd427bSRobert Watson void
2409bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
2419bcd427bSRobert Watson {
2429bcd427bSRobert Watson 
243fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
244fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
245fa046d87SRobert Watson 
2469bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
2479bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
2489bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
2499bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
250fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
2519bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
2529bcd427bSRobert Watson }
2539bcd427bSRobert Watson 
2549bcd427bSRobert Watson /*
255c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
256d915b280SStephan Uphoff  * On success return with the PCB locked.
257c3229e05SDavid Greenman  */
258df8bae1dSRodney W. Grimes int
259d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
260df8bae1dSRodney W. Grimes {
261136d4f1cSRobert Watson 	struct inpcb *inp;
26213cf67f3SHajimu UMEMOTO 	int error;
263a557af22SRobert Watson 
26459daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
265a557af22SRobert Watson 	error = 0;
266d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
267df8bae1dSRodney W. Grimes 	if (inp == NULL)
268df8bae1dSRodney W. Grimes 		return (ENOBUFS);
269d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
27015bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
271df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
27286d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
2738b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
274a557af22SRobert Watson #ifdef MAC
27530d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
276a557af22SRobert Watson 	if (error != 0)
277a557af22SRobert Watson 		goto out;
27830d239bcSRobert Watson 	mac_inpcb_create(so, inp);
279a557af22SRobert Watson #endif
280b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
28113cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
2820bffde27SRobert Watson 	if (error != 0) {
2830bffde27SRobert Watson #ifdef MAC
2840bffde27SRobert Watson 		mac_inpcb_destroy(inp);
2850bffde27SRobert Watson #endif
286a557af22SRobert Watson 		goto out;
2870bffde27SRobert Watson 	}
288b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
289e3fd5ffdSRobert Watson #ifdef INET6
290340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
291340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
292603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
29333841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
294340c35deSJonathan Lemon 	}
29575daea93SPaul Saab #endif
296712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
2973d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
298df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
29933841545SHajimu UMEMOTO #ifdef INET6
300603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
30133841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
30233841545SHajimu UMEMOTO #endif
3038501a69cSRobert Watson 	INP_WLOCK(inp);
304d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
30579bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
306b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
307a557af22SRobert Watson out:
30886d02c5cSBjoern A. Zeeb 	if (error != 0) {
30986d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
310a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
31186d02c5cSBjoern A. Zeeb 	}
312a557af22SRobert Watson #endif
313a557af22SRobert Watson 	return (error);
314df8bae1dSRodney W. Grimes }
315df8bae1dSRodney W. Grimes 
31667107f45SBjoern A. Zeeb #ifdef INET
317df8bae1dSRodney W. Grimes int
318136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
319df8bae1dSRodney W. Grimes {
3204b932371SIan Dowse 	int anonport, error;
3214b932371SIan Dowse 
3228501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
323fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
32459daba27SSam Leffler 
3254b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
3264b932371SIan Dowse 		return (EINVAL);
3274b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
3284b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
3294b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
330b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
3314b932371SIan Dowse 	if (error)
3324b932371SIan Dowse 		return (error);
3334b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
3344b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
3354b932371SIan Dowse 		inp->inp_lport = 0;
3364b932371SIan Dowse 		return (EAGAIN);
3374b932371SIan Dowse 	}
3384b932371SIan Dowse 	if (anonport)
3394b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
3404b932371SIan Dowse 	return (0);
3414b932371SIan Dowse }
34267107f45SBjoern A. Zeeb #endif
3434b932371SIan Dowse 
344efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
345efc76f72SBjoern A. Zeeb int
346efc76f72SBjoern A. Zeeb in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
34768e0d7e0SRobert Watson     struct ucred *cred, int lookupflags)
348efc76f72SBjoern A. Zeeb {
349efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
350efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
351efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
352efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
353efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
354efc76f72SBjoern A. Zeeb #ifdef INET
355efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
356efc76f72SBjoern A. Zeeb #endif
357efc76f72SBjoern A. Zeeb 
358efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
359efc76f72SBjoern A. Zeeb 
360efc76f72SBjoern A. Zeeb 	/*
361efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
362efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
363efc76f72SBjoern A. Zeeb 	 */
364efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
365fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
366efc76f72SBjoern A. Zeeb 
367efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
368efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
369efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
370efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
371efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
372efc76f72SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
373efc76f72SBjoern A. Zeeb 		if (error)
374efc76f72SBjoern A. Zeeb 			return (error);
375efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
376efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
377efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
378efc76f72SBjoern A. Zeeb 	} else {
379efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
380efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
381efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
382efc76f72SBjoern A. Zeeb 	}
383efc76f72SBjoern A. Zeeb 	/*
384efc76f72SBjoern A. Zeeb 	 * For UDP, use random port allocation as long as the user
385efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
386efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
387efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
388efc76f72SBjoern A. Zeeb 	 */
389efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
390efc76f72SBjoern A. Zeeb 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
391efc76f72SBjoern A. Zeeb 		dorandom = 1;
392efc76f72SBjoern A. Zeeb 	else
393efc76f72SBjoern A. Zeeb 		dorandom = 0;
394efc76f72SBjoern A. Zeeb 	/*
395efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
396efc76f72SBjoern A. Zeeb 	 * we have the only port available.
397efc76f72SBjoern A. Zeeb 	 */
398efc76f72SBjoern A. Zeeb 	if (first == last)
399efc76f72SBjoern A. Zeeb 		dorandom = 0;
400efc76f72SBjoern A. Zeeb 	/* Make sure to not include UDP packets in the count. */
401efc76f72SBjoern A. Zeeb 	if (pcbinfo != &V_udbinfo)
402efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
403efc76f72SBjoern A. Zeeb 	/*
404efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
405efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
406efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
407efc76f72SBjoern A. Zeeb 	 */
408efc76f72SBjoern A. Zeeb 	if (first > last) {
409efc76f72SBjoern A. Zeeb 		aux = first;
410efc76f72SBjoern A. Zeeb 		first = last;
411efc76f72SBjoern A. Zeeb 		last = aux;
412efc76f72SBjoern A. Zeeb 	}
413efc76f72SBjoern A. Zeeb 
414efc76f72SBjoern A. Zeeb #ifdef INET
415efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
416efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
4174d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
418efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
419efc76f72SBjoern A. Zeeb 		    __func__, inp));
420efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
421efc76f72SBjoern A. Zeeb 	}
422efc76f72SBjoern A. Zeeb #endif
42367107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
424efc76f72SBjoern A. Zeeb 	lport = *lportp;
425efc76f72SBjoern A. Zeeb 
426efc76f72SBjoern A. Zeeb 	if (dorandom)
427efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
428efc76f72SBjoern A. Zeeb 
429efc76f72SBjoern A. Zeeb 	count = last - first;
430efc76f72SBjoern A. Zeeb 
431efc76f72SBjoern A. Zeeb 	do {
432efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
433efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
434efc76f72SBjoern A. Zeeb 		++*lastport;
435efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
436efc76f72SBjoern A. Zeeb 			*lastport = first;
437efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
438efc76f72SBjoern A. Zeeb 
439efc76f72SBjoern A. Zeeb #ifdef INET6
440efc76f72SBjoern A. Zeeb 		if ((inp->inp_vflag & INP_IPV6) != 0)
441efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
44268e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
443efc76f72SBjoern A. Zeeb #endif
444efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
445efc76f72SBjoern A. Zeeb 		else
446efc76f72SBjoern A. Zeeb #endif
447efc76f72SBjoern A. Zeeb #ifdef INET
448efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
44968e0d7e0SRobert Watson 			    lport, lookupflags, cred);
450efc76f72SBjoern A. Zeeb #endif
451efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
452efc76f72SBjoern A. Zeeb 
453efc76f72SBjoern A. Zeeb #ifdef INET
4544d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
455efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
456efc76f72SBjoern A. Zeeb #endif
457efc76f72SBjoern A. Zeeb 	*lportp = lport;
458efc76f72SBjoern A. Zeeb 
459efc76f72SBjoern A. Zeeb 	return (0);
460efc76f72SBjoern A. Zeeb }
461efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
462efc76f72SBjoern A. Zeeb 
46367107f45SBjoern A. Zeeb #ifdef INET
4644b932371SIan Dowse /*
4654b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
4664b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
4674b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
4684b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
4694b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
4704b932371SIan Dowse  *
4714b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
4724b932371SIan Dowse  */
4734b932371SIan Dowse int
474136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
475136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
4764b932371SIan Dowse {
4774b932371SIan Dowse 	struct socket *so = inp->inp_socket;
47815bd2b43SDavid Greenman 	struct sockaddr_in *sin;
479c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
4804b932371SIan Dowse 	struct in_addr laddr;
481df8bae1dSRodney W. Grimes 	u_short lport = 0;
48268e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
483413628a7SBjoern A. Zeeb 	int error;
484df8bae1dSRodney W. Grimes 
4858501a69cSRobert Watson 	/*
486fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
4878501a69cSRobert Watson 	 */
48859daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
489fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
49059daba27SSam Leffler 
491603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
492df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
4934b932371SIan Dowse 	laddr.s_addr = *laddrp;
4944b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
495df8bae1dSRodney W. Grimes 		return (EINVAL);
496c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
49768e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
4987c2f3cb9SJamie Gritton 	if (nam == NULL) {
4997c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
5007c2f3cb9SJamie Gritton 			return (error);
5017c2f3cb9SJamie Gritton 	} else {
50257bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
50357bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
504df8bae1dSRodney W. Grimes 			return (EINVAL);
505df8bae1dSRodney W. Grimes #ifdef notdef
506df8bae1dSRodney W. Grimes 		/*
507df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
508df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
509df8bae1dSRodney W. Grimes 		 */
510df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
511df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
512df8bae1dSRodney W. Grimes #endif
513b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
514b89e82ddSJamie Gritton 		if (error)
515b89e82ddSJamie Gritton 			return (error);
5164b932371SIan Dowse 		if (sin->sin_port != *lportp) {
5174b932371SIan Dowse 			/* Don't allow the port to change. */
5184b932371SIan Dowse 			if (*lportp != 0)
5194b932371SIan Dowse 				return (EINVAL);
520df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
5214b932371SIan Dowse 		}
5224b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
523df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
524df8bae1dSRodney W. Grimes 			/*
525df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
526df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
527df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
528df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
529df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
530df8bae1dSRodney W. Grimes 			 */
531df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
532df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
533df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
534df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
53583103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
5364209e01aSAdrian Chadd 			/*
5374209e01aSAdrian Chadd 			 * Is the address a local IP address?
538f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
5398696873dSAdrian Chadd 			 * to any endpoint address, local or not.
5404209e01aSAdrian Chadd 			 */
541f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
5428896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
543df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
544df8bae1dSRodney W. Grimes 		}
5454b932371SIan Dowse 		laddr = sin->sin_addr;
546df8bae1dSRodney W. Grimes 		if (lport) {
547df8bae1dSRodney W. Grimes 			struct inpcb *t;
548ae0e7143SRobert Watson 			struct tcptw *tw;
549ae0e7143SRobert Watson 
550df8bae1dSRodney W. Grimes 			/* GROSS */
551603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
552603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
553acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
55432f9753cSRobert Watson 			    0))
5552469dd60SGarrett Wollman 				return (EACCES);
556835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
55786d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
55832f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
559078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
560413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
561340c35deSJonathan Lemon 	/*
562340c35deSJonathan Lemon 	 * XXX
563340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
564340c35deSJonathan Lemon 	 */
5654cc20ab1SSeigo Tanimura 				if (t &&
566ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
5674658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
5684658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
5694cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
57052b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
57152b65dbeSBill Fenner 				     (t->inp_socket->so_options &
57252b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
57386d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
57486d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
5754049a042SGuido van Rooij 					return (EADDRINUSE);
5764049a042SGuido van Rooij 			}
577c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
57868e0d7e0SRobert Watson 			    lport, lookupflags, cred);
579ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
580ae0e7143SRobert Watson 				/*
581ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
582ae0e7143SRobert Watson 				 * state recycled, we treat the address as
583ae0e7143SRobert Watson 				 * being in use (for now).  This is better
584ae0e7143SRobert Watson 				 * than a panic, but not desirable.
585ae0e7143SRobert Watson 				 */
586ae0e7143SRobert Watson 				tw = intotw(inp);
587ae0e7143SRobert Watson 				if (tw == NULL ||
588ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
589340c35deSJonathan Lemon 					return (EADDRINUSE);
590ae0e7143SRobert Watson 			} else if (t &&
5914cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
592e3fd5ffdSRobert Watson #ifdef INET6
59333841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
594cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
595cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
596cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
597cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
598cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
599e3fd5ffdSRobert Watson #endif
600df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
601df8bae1dSRodney W. Grimes 			}
602cfa1ca9dSYoshinobu Inoue 		}
603df8bae1dSRodney W. Grimes 	}
6044b932371SIan Dowse 	if (*lportp != 0)
6054b932371SIan Dowse 		lport = *lportp;
60633b3ac06SPeter Wemm 	if (lport == 0) {
60768e0d7e0SRobert Watson 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
608efc76f72SBjoern A. Zeeb 		if (error != 0)
609efc76f72SBjoern A. Zeeb 			return (error);
61033b3ac06SPeter Wemm 
61133b3ac06SPeter Wemm 	}
6124b932371SIan Dowse 	*laddrp = laddr.s_addr;
6134b932371SIan Dowse 	*lportp = lport;
614df8bae1dSRodney W. Grimes 	return (0);
615df8bae1dSRodney W. Grimes }
616df8bae1dSRodney W. Grimes 
617999f1343SGarrett Wollman /*
6185200e00eSIan Dowse  * Connect from a socket to a specified address.
6195200e00eSIan Dowse  * Both address and port must be specified in argument sin.
6205200e00eSIan Dowse  * If don't have a local address for this socket yet,
6215200e00eSIan Dowse  * then pick one.
622999f1343SGarrett Wollman  */
623999f1343SGarrett Wollman int
624*d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
625*d3c1f003SRobert Watson     struct ucred *cred, struct mbuf *m)
626999f1343SGarrett Wollman {
6275200e00eSIan Dowse 	u_short lport, fport;
6285200e00eSIan Dowse 	in_addr_t laddr, faddr;
6295200e00eSIan Dowse 	int anonport, error;
630df8bae1dSRodney W. Grimes 
6318501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
632fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
63327f74fd0SRobert Watson 
6345200e00eSIan Dowse 	lport = inp->inp_lport;
6355200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
6365200e00eSIan Dowse 	anonport = (lport == 0);
6375200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
638b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
6395200e00eSIan Dowse 	if (error)
6405200e00eSIan Dowse 		return (error);
6415200e00eSIan Dowse 
6425200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
6435200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
6445200e00eSIan Dowse 		inp->inp_lport = lport;
6455200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
6465200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
6475200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
6485200e00eSIan Dowse 			inp->inp_lport = 0;
6495200e00eSIan Dowse 			return (EAGAIN);
6505200e00eSIan Dowse 		}
6515200e00eSIan Dowse 	}
6525200e00eSIan Dowse 
6535200e00eSIan Dowse 	/* Commit the remaining changes. */
6545200e00eSIan Dowse 	inp->inp_lport = lport;
6555200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
6565200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
6575200e00eSIan Dowse 	inp->inp_fport = fport;
658*d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, m);
6592cb64cb2SGeorge V. Neville-Neil 
6605200e00eSIan Dowse 	if (anonport)
6615200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
6625200e00eSIan Dowse 	return (0);
6635200e00eSIan Dowse }
6645200e00eSIan Dowse 
665*d3c1f003SRobert Watson int
666*d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
667*d3c1f003SRobert Watson {
668*d3c1f003SRobert Watson 
669*d3c1f003SRobert Watson 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
670*d3c1f003SRobert Watson }
671*d3c1f003SRobert Watson 
6725200e00eSIan Dowse /*
6730895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
6740895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
6750895aec3SBjoern A. Zeeb  */
6760895aec3SBjoern A. Zeeb static int
6770895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
6780895aec3SBjoern A. Zeeb     struct ucred *cred)
6790895aec3SBjoern A. Zeeb {
6800895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
6810895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
6820895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
6830895aec3SBjoern A. Zeeb 	struct route sro;
6840895aec3SBjoern A. Zeeb 	int error;
6850895aec3SBjoern A. Zeeb 
686413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
6870895aec3SBjoern A. Zeeb 
688592bcae8SBjoern A. Zeeb 	/*
689592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
690592bcae8SBjoern A. Zeeb 	 * if requested.
691592bcae8SBjoern A. Zeeb 	 */
692592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
693592bcae8SBjoern A. Zeeb 		return (0);
694592bcae8SBjoern A. Zeeb 
6950895aec3SBjoern A. Zeeb 	error = 0;
6960895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
6970895aec3SBjoern A. Zeeb 
6980895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
6990895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
7000895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
7010895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
7020895aec3SBjoern A. Zeeb 
7030895aec3SBjoern A. Zeeb 	/*
7040895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
7050895aec3SBjoern A. Zeeb 	 * else punt.
7060895aec3SBjoern A. Zeeb 	 *
7070895aec3SBjoern A. Zeeb 	 * Find out route to destination.
7080895aec3SBjoern A. Zeeb 	 */
7090895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
7106e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
7110895aec3SBjoern A. Zeeb 
7120895aec3SBjoern A. Zeeb 	/*
7130895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
7140895aec3SBjoern A. Zeeb 	 * the outgoing interface.
7150895aec3SBjoern A. Zeeb 	 *
7160895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
7170895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
7180895aec3SBjoern A. Zeeb 	 * the source address from.
7190895aec3SBjoern A. Zeeb 	 */
7200895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
7218c0fec80SRobert Watson 		struct in_ifaddr *ia;
7220895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
7230895aec3SBjoern A. Zeeb 
7240895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
7250895aec3SBjoern A. Zeeb 		if (ia == NULL)
7260ed6142bSQing Li 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
7270895aec3SBjoern A. Zeeb 		if (ia == NULL) {
7280895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
7290895aec3SBjoern A. Zeeb 			goto done;
7300895aec3SBjoern A. Zeeb 		}
7310895aec3SBjoern A. Zeeb 
7320304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
7330895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7348c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
7350895aec3SBjoern A. Zeeb 			goto done;
7360895aec3SBjoern A. Zeeb 		}
7370895aec3SBjoern A. Zeeb 
7380895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
7398c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
7400895aec3SBjoern A. Zeeb 		ia = NULL;
7419317b04eSRobert Watson 		IF_ADDR_LOCK(ifp);
7420895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
7430895aec3SBjoern A. Zeeb 
7440895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
7450895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
7460895aec3SBjoern A. Zeeb 				continue;
7470895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
748b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
7490895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
7500895aec3SBjoern A. Zeeb 				break;
7510895aec3SBjoern A. Zeeb 			}
7520895aec3SBjoern A. Zeeb 		}
7530895aec3SBjoern A. Zeeb 		if (ia != NULL) {
7540895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7559317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
7560895aec3SBjoern A. Zeeb 			goto done;
7570895aec3SBjoern A. Zeeb 		}
7589317b04eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
7590895aec3SBjoern A. Zeeb 
7600895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
761b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
7620895aec3SBjoern A. Zeeb 		goto done;
7630895aec3SBjoern A. Zeeb 	}
7640895aec3SBjoern A. Zeeb 
7650895aec3SBjoern A. Zeeb 	/*
7660895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
7670895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
7680895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
7690895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
7700895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
7710895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
7720895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
7730895aec3SBjoern A. Zeeb 	 */
7740895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
7758c0fec80SRobert Watson 		struct in_ifaddr *ia;
7769317b04eSRobert Watson 		struct ifnet *ifp;
7770895aec3SBjoern A. Zeeb 
7780895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
7790304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
7800895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
7810895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7820895aec3SBjoern A. Zeeb 			goto done;
7830895aec3SBjoern A. Zeeb 		}
7840895aec3SBjoern A. Zeeb 
7850895aec3SBjoern A. Zeeb 		/* Jailed. */
7860895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
7870895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
788b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
7890895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
7900895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7910895aec3SBjoern A. Zeeb 			goto done;
7920895aec3SBjoern A. Zeeb 		}
7930895aec3SBjoern A. Zeeb 
7940895aec3SBjoern A. Zeeb 		/*
7950895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
7960895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
7970895aec3SBjoern A. Zeeb 		 */
7988c0fec80SRobert Watson 		ia = NULL;
7999317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
8009317b04eSRobert Watson 		IF_ADDR_LOCK(ifp);
8019317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8020895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8030895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8040895aec3SBjoern A. Zeeb 				continue;
8050895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
806b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8070895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8080895aec3SBjoern A. Zeeb 				break;
8090895aec3SBjoern A. Zeeb 			}
8100895aec3SBjoern A. Zeeb 		}
8110895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8120895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8139317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
8140895aec3SBjoern A. Zeeb 			goto done;
8150895aec3SBjoern A. Zeeb 		}
8169317b04eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
8170895aec3SBjoern A. Zeeb 
8180895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
819b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8200895aec3SBjoern A. Zeeb 		goto done;
8210895aec3SBjoern A. Zeeb 	}
8220895aec3SBjoern A. Zeeb 
8230895aec3SBjoern A. Zeeb 	/*
8240895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
8250895aec3SBjoern A. Zeeb 	 * to ourselves is here.
8260895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
8270895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
8280895aec3SBjoern A. Zeeb 	 * a loopback interface.
8290895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
8300895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
8310895aec3SBjoern A. Zeeb 	 */
8320895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
8330895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
8348c0fec80SRobert Watson 		struct in_ifaddr *ia;
8350895aec3SBjoern A. Zeeb 
8360895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
8370895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
8380895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
8390895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
8400895aec3SBjoern A. Zeeb 
8410895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
8420895aec3SBjoern A. Zeeb 		if (ia == NULL)
8430ed6142bSQing Li 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
844f0bb05fcSQing Li 		if (ia == NULL)
845f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
8460895aec3SBjoern A. Zeeb 
8470304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8480895aec3SBjoern A. Zeeb 			if (ia == NULL) {
8490895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
8500895aec3SBjoern A. Zeeb 				goto done;
8510895aec3SBjoern A. Zeeb 			}
8520895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8538c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8540895aec3SBjoern A. Zeeb 			goto done;
8550895aec3SBjoern A. Zeeb 		}
8560895aec3SBjoern A. Zeeb 
8570895aec3SBjoern A. Zeeb 		/* Jailed. */
8580895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8590895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
8600895aec3SBjoern A. Zeeb 
8610895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
8628c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8630895aec3SBjoern A. Zeeb 			ia = NULL;
8649317b04eSRobert Watson 			IF_ADDR_LOCK(ifp);
8650895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8660895aec3SBjoern A. Zeeb 
8670895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
8680895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
8690895aec3SBjoern A. Zeeb 					continue;
8700895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
871b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
872b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
8730895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
8740895aec3SBjoern A. Zeeb 					break;
8750895aec3SBjoern A. Zeeb 				}
8760895aec3SBjoern A. Zeeb 			}
8770895aec3SBjoern A. Zeeb 			if (ia != NULL) {
8780895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8799317b04eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
8800895aec3SBjoern A. Zeeb 				goto done;
8810895aec3SBjoern A. Zeeb 			}
8829317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
8830895aec3SBjoern A. Zeeb 		}
8840895aec3SBjoern A. Zeeb 
8850895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
886b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8870895aec3SBjoern A. Zeeb 		goto done;
8880895aec3SBjoern A. Zeeb 	}
8890895aec3SBjoern A. Zeeb 
8900895aec3SBjoern A. Zeeb done:
8910895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
8920895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
8930895aec3SBjoern A. Zeeb 	return (error);
8940895aec3SBjoern A. Zeeb }
8950895aec3SBjoern A. Zeeb 
8960895aec3SBjoern A. Zeeb /*
8975200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
8985200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
8995200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
9005200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
9015200e00eSIan Dowse  * the connect.
9025200e00eSIan Dowse  *
9035200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
9045200e00eSIan Dowse  * and port. These are not updated in the error case.
9055200e00eSIan Dowse  *
9065200e00eSIan Dowse  * If the operation fails because the connection already exists,
9075200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
9085200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
9095200e00eSIan Dowse  * is set to NULL.
9105200e00eSIan Dowse  */
9115200e00eSIan Dowse int
912136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
913136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
914136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
9155200e00eSIan Dowse {
9165200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
9175200e00eSIan Dowse 	struct in_ifaddr *ia;
9185200e00eSIan Dowse 	struct inpcb *oinp;
919b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
9205200e00eSIan Dowse 	u_short lport, fport;
9215200e00eSIan Dowse 	int error;
9225200e00eSIan Dowse 
9238501a69cSRobert Watson 	/*
9248501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
9258501a69cSRobert Watson 	 * lock is sufficient.
9268501a69cSRobert Watson 	 */
92727f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
928fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
92927f74fd0SRobert Watson 
9305200e00eSIan Dowse 	if (oinpp != NULL)
9315200e00eSIan Dowse 		*oinpp = NULL;
93257bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
933df8bae1dSRodney W. Grimes 		return (EINVAL);
934df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
935df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
936df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
937df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
9385200e00eSIan Dowse 	laddr.s_addr = *laddrp;
9395200e00eSIan Dowse 	lport = *lportp;
9405200e00eSIan Dowse 	faddr = sin->sin_addr;
9415200e00eSIan Dowse 	fport = sin->sin_port;
9420895aec3SBjoern A. Zeeb 
943603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
944df8bae1dSRodney W. Grimes 		/*
945df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
946df8bae1dSRodney W. Grimes 		 * use the primary local address.
947df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
948df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
949df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
950df8bae1dSRodney W. Grimes 		 */
951413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
9522d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
953413628a7SBjoern A. Zeeb 			faddr =
954b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
9552d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
956b89e82ddSJamie Gritton 			if (cred != NULL &&
957b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
958b89e82ddSJamie Gritton 				return (error);
9592d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
9602d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
9612d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
9622d9cfabaSRobert Watson 			    IFF_BROADCAST)
9635200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
964603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
9652d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
9662d9cfabaSRobert Watson 		}
967df8bae1dSRodney W. Grimes 	}
9685200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
969d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
970df8bae1dSRodney W. Grimes 		/*
971df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
972d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
973df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
974df8bae1dSRodney W. Grimes 		 */
9755200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
976df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
977df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
978df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
981df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
982df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
9832d9cfabaSRobert Watson 				IN_IFADDR_RLOCK();
984e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
985e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
986e691be70SDaniel Eischen 					    (cred == NULL ||
987e691be70SDaniel Eischen 					    prison_check_ip4(cred,
988e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
989df8bae1dSRodney W. Grimes 						break;
990e691be70SDaniel Eischen 				}
991e691be70SDaniel Eischen 				if (ia == NULL)
992d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
993e691be70SDaniel Eischen 				else {
9945200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
995d79fdd98SDaniel Eischen 					error = 0;
996999f1343SGarrett Wollman 				}
997e691be70SDaniel Eischen 				IN_IFADDR_RUNLOCK();
998d79fdd98SDaniel Eischen 			}
999d79fdd98SDaniel Eischen 		}
100004215ed2SRandall Stewart 		if (error)
100104215ed2SRandall Stewart 			return (error);
10020895aec3SBjoern A. Zeeb 	}
1003fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1004fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
10055200e00eSIan Dowse 	if (oinp != NULL) {
10065200e00eSIan Dowse 		if (oinpp != NULL)
10075200e00eSIan Dowse 			*oinpp = oinp;
1008df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1009c3229e05SDavid Greenman 	}
10105200e00eSIan Dowse 	if (lport == 0) {
1011b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1012b0330ed9SPawel Jakub Dawidek 		    cred);
10135a903f8dSPierre Beyssac 		if (error)
10145a903f8dSPierre Beyssac 			return (error);
10155a903f8dSPierre Beyssac 	}
10165200e00eSIan Dowse 	*laddrp = laddr.s_addr;
10175200e00eSIan Dowse 	*lportp = lport;
10185200e00eSIan Dowse 	*faddrp = faddr.s_addr;
10195200e00eSIan Dowse 	*fportp = fport;
1020df8bae1dSRodney W. Grimes 	return (0);
1021df8bae1dSRodney W. Grimes }
1022df8bae1dSRodney W. Grimes 
102326f9a767SRodney W. Grimes void
1024136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1025df8bae1dSRodney W. Grimes {
10266b348152SRobert Watson 
10278501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1028fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1031df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
103215bd2b43SDavid Greenman 	in_pcbrehash(inp);
1033df8bae1dSRodney W. Grimes }
103467107f45SBjoern A. Zeeb #endif
1035df8bae1dSRodney W. Grimes 
10364c7c478dSRobert Watson /*
103728696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1038c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
103928696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
104028696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
10414c7c478dSRobert Watson  */
104226f9a767SRodney W. Grimes void
1043136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1044df8bae1dSRodney W. Grimes {
10454c7c478dSRobert Watson 
1046a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1047c0a211c5SRobert Watson 
10484c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
10494c7c478dSRobert Watson 	inp->inp_socket = NULL;
10504c7c478dSRobert Watson }
10514c7c478dSRobert Watson 
1052c0a211c5SRobert Watson /*
105379bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
105479bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
105579bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
105679bdc6e5SRobert Watson  * but where the inpcb lock is already held.
105779bdc6e5SRobert Watson  *
105879bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
105979bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
106079bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
106179bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
106279bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
106379bdc6e5SRobert Watson  * inpcb.
106479bdc6e5SRobert Watson  *
106579bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
106679bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
106779bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
106879bdc6e5SRobert Watson  * using in_pcbrele().
1069c0a211c5SRobert Watson  */
107079bdc6e5SRobert Watson void
107179bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
10724c7c478dSRobert Watson {
1073df8bae1dSRodney W. Grimes 
107479bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
107579bdc6e5SRobert Watson 
107679bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
107779bdc6e5SRobert Watson }
107879bdc6e5SRobert Watson 
107979bdc6e5SRobert Watson /*
108079bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
108179bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
108279bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
108379bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
108479bdc6e5SRobert Watson  *
108579bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
108679bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
108779bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
108879bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
108979bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
109079bdc6e5SRobert Watson  */
109179bdc6e5SRobert Watson int
109279bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
109379bdc6e5SRobert Watson {
109479bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
109579bdc6e5SRobert Watson 
109679bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
109779bdc6e5SRobert Watson 
109879bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
109979bdc6e5SRobert Watson 
110079bdc6e5SRobert Watson 	if (refcount_release(&inp->inp_refcount) == 0)
110179bdc6e5SRobert Watson 		return (0);
110279bdc6e5SRobert Watson 
110379bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
110479bdc6e5SRobert Watson 
110579bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
110679bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
110779bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
110879bdc6e5SRobert Watson 	return (1);
110979bdc6e5SRobert Watson }
111079bdc6e5SRobert Watson 
111179bdc6e5SRobert Watson int
111279bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
111379bdc6e5SRobert Watson {
111479bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
111579bdc6e5SRobert Watson 
111679bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
111779bdc6e5SRobert Watson 
111879bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
111979bdc6e5SRobert Watson 
112079bdc6e5SRobert Watson 	if (refcount_release(&inp->inp_refcount) == 0)
112179bdc6e5SRobert Watson 		return (0);
112279bdc6e5SRobert Watson 
112379bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
112479bdc6e5SRobert Watson 
112579bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
112679bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
112779bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
112879bdc6e5SRobert Watson 	return (1);
112979bdc6e5SRobert Watson }
113079bdc6e5SRobert Watson 
113179bdc6e5SRobert Watson /*
113279bdc6e5SRobert Watson  * Temporary wrapper.
113379bdc6e5SRobert Watson  */
113479bdc6e5SRobert Watson int
113579bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
113679bdc6e5SRobert Watson {
113779bdc6e5SRobert Watson 
113879bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
113979bdc6e5SRobert Watson }
114079bdc6e5SRobert Watson 
114179bdc6e5SRobert Watson /*
114279bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
114379bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
114479bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
114579bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
114679bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
114779bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
114879bdc6e5SRobert Watson  * the pcbinfo lock is held.
114979bdc6e5SRobert Watson  */
115079bdc6e5SRobert Watson void
115179bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
115279bdc6e5SRobert Watson {
115379bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
115479bdc6e5SRobert Watson 
115579bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
115679bdc6e5SRobert Watson 
115779bdc6e5SRobert Watson 	INP_INFO_WLOCK_ASSERT(pcbinfo);
115879bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
115979bdc6e5SRobert Watson 
116079bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1161b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11626aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
11636974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
1164b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
116579bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1166c3229e05SDavid Greenman 	in_pcbremlists(inp);
11676aee2fc5SBjoern A. Zeeb #ifdef INET6
11686aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
11696aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
11701096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
11716aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
11726aee2fc5SBjoern A. Zeeb 	}
11736aee2fc5SBjoern A. Zeeb #endif
1174df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1175df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
117667107f45SBjoern A. Zeeb #ifdef INET
117771498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
117871498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
117967107f45SBjoern A. Zeeb #endif
1180cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
118186d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1182a557af22SRobert Watson #ifdef MAC
118330d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1184a557af22SRobert Watson #endif
118579bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
118628696211SRobert Watson 		INP_WUNLOCK(inp);
118728696211SRobert Watson }
118828696211SRobert Watson 
118928696211SRobert Watson /*
1190c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1191c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1192c0a211c5SRobert Watson  *
1193c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1194c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1195c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1196c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1197c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1198c0a211c5SRobert Watson  * in_pcbdetach().
1199c0a211c5SRobert Watson  *
1200c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1201c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
120210702a28SRobert Watson  */
120310702a28SRobert Watson void
120410702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
120510702a28SRobert Watson {
120610702a28SRobert Watson 
12078501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
120810702a28SRobert Watson 
1209fa046d87SRobert Watson 	/*
1210fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1211fa046d87SRobert Watson 	 * the hash lock...?
1212fa046d87SRobert Watson 	 */
1213ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1214111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
121510702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
121610702a28SRobert Watson 
1217fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
121810702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
121910702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
122010702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
122110702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
122210702a28SRobert Watson 			free(phd, M_PCB);
122310702a28SRobert Watson 		}
1224fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1225111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
122610702a28SRobert Watson 	}
122710702a28SRobert Watson }
122810702a28SRobert Watson 
122967107f45SBjoern A. Zeeb #ifdef INET
123054d642bbSRobert Watson /*
123154d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
123254d642bbSRobert Watson  */
123326ef6ac4SDon Lewis struct sockaddr *
1234136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
123526ef6ac4SDon Lewis {
123626ef6ac4SDon Lewis 	struct sockaddr_in *sin;
123726ef6ac4SDon Lewis 
12381ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1239a163d034SWarner Losh 		M_WAITOK | M_ZERO);
124026ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
124126ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
124226ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
124326ef6ac4SDon Lewis 	sin->sin_port = port;
124426ef6ac4SDon Lewis 
124526ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
124626ef6ac4SDon Lewis }
124726ef6ac4SDon Lewis 
1248117bcae7SGarrett Wollman int
124954d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1250df8bae1dSRodney W. Grimes {
1251136d4f1cSRobert Watson 	struct inpcb *inp;
125226ef6ac4SDon Lewis 	struct in_addr addr;
125326ef6ac4SDon Lewis 	in_port_t port;
125442fa505bSDavid Greenman 
1255fdc984f7STor Egge 	inp = sotoinpcb(so);
125654d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
12576466b28aSRobert Watson 
1258a69042a5SRobert Watson 	INP_RLOCK(inp);
125926ef6ac4SDon Lewis 	port = inp->inp_lport;
126026ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1261a69042a5SRobert Watson 	INP_RUNLOCK(inp);
126242fa505bSDavid Greenman 
126326ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1264117bcae7SGarrett Wollman 	return 0;
1265df8bae1dSRodney W. Grimes }
1266df8bae1dSRodney W. Grimes 
1267117bcae7SGarrett Wollman int
126854d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1269df8bae1dSRodney W. Grimes {
1270136d4f1cSRobert Watson 	struct inpcb *inp;
127126ef6ac4SDon Lewis 	struct in_addr addr;
127226ef6ac4SDon Lewis 	in_port_t port;
127342fa505bSDavid Greenman 
1274fdc984f7STor Egge 	inp = sotoinpcb(so);
127554d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
12766466b28aSRobert Watson 
1277a69042a5SRobert Watson 	INP_RLOCK(inp);
127826ef6ac4SDon Lewis 	port = inp->inp_fport;
127926ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1280a69042a5SRobert Watson 	INP_RUNLOCK(inp);
128142fa505bSDavid Greenman 
128226ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1283117bcae7SGarrett Wollman 	return 0;
1284df8bae1dSRodney W. Grimes }
1285df8bae1dSRodney W. Grimes 
128626f9a767SRodney W. Grimes void
1287136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1288136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1289d1c54148SJesper Skriver {
1290f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1291d1c54148SJesper Skriver 
12923dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1293f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
12948501a69cSRobert Watson 		INP_WLOCK(inp);
1295d1c54148SJesper Skriver #ifdef INET6
1296f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
12978501a69cSRobert Watson 			INP_WUNLOCK(inp);
1298d1c54148SJesper Skriver 			continue;
1299f76fcf6dSJeffrey Hsu 		}
1300d1c54148SJesper Skriver #endif
1301d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1302f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
13038501a69cSRobert Watson 			INP_WUNLOCK(inp);
1304d1c54148SJesper Skriver 			continue;
1305d1c54148SJesper Skriver 		}
13063dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
13078501a69cSRobert Watson 			INP_WUNLOCK(inp);
1308f76fcf6dSJeffrey Hsu 	}
13093dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1310d1c54148SJesper Skriver }
1311d1c54148SJesper Skriver 
1312e43cc4aeSHajimu UMEMOTO void
1313136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1314e43cc4aeSHajimu UMEMOTO {
1315e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1316e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1317e43cc4aeSHajimu UMEMOTO 	int i, gap;
1318e43cc4aeSHajimu UMEMOTO 
1319f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
1320712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
13218501a69cSRobert Watson 		INP_WLOCK(inp);
1322e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1323e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1324e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1325e43cc4aeSHajimu UMEMOTO 			/*
1326e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1327e43cc4aeSHajimu UMEMOTO 			 * detached.
1328e43cc4aeSHajimu UMEMOTO 			 */
1329e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1330e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1331e43cc4aeSHajimu UMEMOTO 
1332e43cc4aeSHajimu UMEMOTO 			/*
1333e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1334e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1335e43cc4aeSHajimu UMEMOTO 			 */
1336e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1337e43cc4aeSHajimu UMEMOTO 			    i++) {
1338e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1339e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1340e43cc4aeSHajimu UMEMOTO 					gap++;
1341e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1342e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1343e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1344e43cc4aeSHajimu UMEMOTO 			}
1345e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1346e43cc4aeSHajimu UMEMOTO 		}
13478501a69cSRobert Watson 		INP_WUNLOCK(inp);
1348e43cc4aeSHajimu UMEMOTO 	}
13493cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
1350e43cc4aeSHajimu UMEMOTO }
1351e43cc4aeSHajimu UMEMOTO 
1352df8bae1dSRodney W. Grimes /*
1353fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1354fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1355c3229e05SDavid Greenman  */
1356d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1357df8bae1dSRodney W. Grimes struct inpcb *
1358136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
135968e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1360df8bae1dSRodney W. Grimes {
1361136d4f1cSRobert Watson 	struct inpcb *inp;
1362d5e8a67eSHajimu UMEMOTO #ifdef INET6
1363d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1364d5e8a67eSHajimu UMEMOTO #else
1365d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1366d5e8a67eSHajimu UMEMOTO #endif
1367d5e8a67eSHajimu UMEMOTO 	int wildcard;
13687bc4aca7SDavid Greenman 
136968e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
137068e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
137168e0d7e0SRobert Watson 
1372fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
13731b73ca0bSSam Leffler 
137468e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1375c3229e05SDavid Greenman 		struct inpcbhead *head;
1376c3229e05SDavid Greenman 		/*
1377c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1378c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1379c3229e05SDavid Greenman 		 */
1380712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1381712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1382fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1383cfa1ca9dSYoshinobu Inoue #ifdef INET6
1384413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1385369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1386cfa1ca9dSYoshinobu Inoue 				continue;
1387cfa1ca9dSYoshinobu Inoue #endif
1388c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1389c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1390c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1391c3229e05SDavid Greenman 				/*
1392413628a7SBjoern A. Zeeb 				 * Found?
1393c3229e05SDavid Greenman 				 */
1394413628a7SBjoern A. Zeeb 				if (cred == NULL ||
13950304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
13960304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1397c3229e05SDavid Greenman 					return (inp);
1398df8bae1dSRodney W. Grimes 			}
1399c3229e05SDavid Greenman 		}
1400c3229e05SDavid Greenman 		/*
1401c3229e05SDavid Greenman 		 * Not found.
1402c3229e05SDavid Greenman 		 */
1403c3229e05SDavid Greenman 		return (NULL);
1404c3229e05SDavid Greenman 	} else {
1405c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1406c3229e05SDavid Greenman 		struct inpcbport *phd;
1407c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1408c3229e05SDavid Greenman 		/*
1409c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1410c3229e05SDavid Greenman 		 *
1411c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1412c3229e05SDavid Greenman 		 * port hash list.
1413c3229e05SDavid Greenman 		 */
1414712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1415712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1416fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1417c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1418c3229e05SDavid Greenman 				break;
1419c3229e05SDavid Greenman 		}
1420c3229e05SDavid Greenman 		if (phd != NULL) {
1421c3229e05SDavid Greenman 			/*
1422c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1423c3229e05SDavid Greenman 			 * fit.
1424c3229e05SDavid Greenman 			 */
142537d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1426c3229e05SDavid Greenman 				wildcard = 0;
1427413628a7SBjoern A. Zeeb 				if (cred != NULL &&
14280304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
14290304c731SJamie Gritton 					cred->cr_prison))
1430413628a7SBjoern A. Zeeb 					continue;
1431cfa1ca9dSYoshinobu Inoue #ifdef INET6
1432413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1433369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1434cfa1ca9dSYoshinobu Inoue 					continue;
1435d5e8a67eSHajimu UMEMOTO 				/*
1436d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1437d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1438d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1439d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1440d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1441d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1442d5e8a67eSHajimu UMEMOTO 				 *
1443d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1444d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1445d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1446d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1447d5e8a67eSHajimu UMEMOTO 				 */
1448d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1449d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1450cfa1ca9dSYoshinobu Inoue #endif
1451c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1452c3229e05SDavid Greenman 					wildcard++;
145315bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
145415bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
145515bd2b43SDavid Greenman 						wildcard++;
145615bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
145715bd2b43SDavid Greenman 						continue;
145815bd2b43SDavid Greenman 				} else {
145915bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
146015bd2b43SDavid Greenman 						wildcard++;
146115bd2b43SDavid Greenman 				}
1462df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1463df8bae1dSRodney W. Grimes 					match = inp;
1464df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1465413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1466df8bae1dSRodney W. Grimes 						break;
1467df8bae1dSRodney W. Grimes 				}
1468df8bae1dSRodney W. Grimes 			}
14693dbdc25cSDavid Greenman 		}
1470df8bae1dSRodney W. Grimes 		return (match);
1471df8bae1dSRodney W. Grimes 	}
1472c3229e05SDavid Greenman }
1473d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
147415bd2b43SDavid Greenman 
147515bd2b43SDavid Greenman /*
1476fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1477fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1478fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
147915bd2b43SDavid Greenman  */
1480fa046d87SRobert Watson static struct inpcb *
1481fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
148268e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1483136d4f1cSRobert Watson     struct ifnet *ifp)
148415bd2b43SDavid Greenman {
148515bd2b43SDavid Greenman 	struct inpcbhead *head;
1486413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
148715bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
148815bd2b43SDavid Greenman 
148968e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
149068e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
149168e0d7e0SRobert Watson 
1492fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1493602cc7f1SRobert Watson 
149415bd2b43SDavid Greenman 	/*
149515bd2b43SDavid Greenman 	 * First look for an exact match.
149615bd2b43SDavid Greenman 	 */
1497413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1498712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1499712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1500fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1501cfa1ca9dSYoshinobu Inoue #ifdef INET6
1502413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1503369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1504cfa1ca9dSYoshinobu Inoue 			continue;
1505cfa1ca9dSYoshinobu Inoue #endif
15066d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1507ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1508ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1509413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1510413628a7SBjoern A. Zeeb 			/*
1511413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1512413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1513413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1514413628a7SBjoern A. Zeeb 			 */
15150304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1516c3229e05SDavid Greenman 				return (inp);
1517413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1518413628a7SBjoern A. Zeeb 				tmpinp = inp;
1519c3229e05SDavid Greenman 		}
1520413628a7SBjoern A. Zeeb 	}
1521413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1522413628a7SBjoern A. Zeeb 		return (tmpinp);
1523e3fd5ffdSRobert Watson 
1524e3fd5ffdSRobert Watson 	/*
1525e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1526e3fd5ffdSRobert Watson 	 */
152768e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1528413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1529e3fd5ffdSRobert Watson #ifdef INET6
1530cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1531e3fd5ffdSRobert Watson #endif
1532413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1533413628a7SBjoern A. Zeeb 		int injail;
1534413628a7SBjoern A. Zeeb 
1535413628a7SBjoern A. Zeeb 		/*
1536413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1537413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1538413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1539413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1540413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1541413628a7SBjoern A. Zeeb 		 */
15426d6a026bSDavid Greenman 
1543712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1544712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1545fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1546cfa1ca9dSYoshinobu Inoue #ifdef INET6
1547413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1548369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1549cfa1ca9dSYoshinobu Inoue 				continue;
1550cfa1ca9dSYoshinobu Inoue #endif
1551413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1552413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1553413628a7SBjoern A. Zeeb 				continue;
1554413628a7SBjoern A. Zeeb 
1555413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1556cfa1ca9dSYoshinobu Inoue 			if (ifp && ifp->if_type == IFT_FAITH &&
1557cfa1ca9dSYoshinobu Inoue 			    (inp->inp_flags & INP_FAITH) == 0)
1558cfa1ca9dSYoshinobu Inoue 				continue;
1559413628a7SBjoern A. Zeeb 
15600304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1561413628a7SBjoern A. Zeeb 			if (injail) {
1562b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1563b89e82ddSJamie Gritton 				    &laddr) != 0)
1564413628a7SBjoern A. Zeeb 					continue;
1565413628a7SBjoern A. Zeeb 			} else {
1566413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1567413628a7SBjoern A. Zeeb 					continue;
1568413628a7SBjoern A. Zeeb 			}
1569413628a7SBjoern A. Zeeb 
1570413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1571413628a7SBjoern A. Zeeb 				if (injail)
1572c3229e05SDavid Greenman 					return (inp);
1573413628a7SBjoern A. Zeeb 				else
1574413628a7SBjoern A. Zeeb 					local_exact = inp;
1575413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1576e3fd5ffdSRobert Watson #ifdef INET6
1577413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
15785cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1579cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1580cfa1ca9dSYoshinobu Inoue 				else
1581413628a7SBjoern A. Zeeb #endif /* INET6 */
1582413628a7SBjoern A. Zeeb 					if (injail)
1583413628a7SBjoern A. Zeeb 						jail_wild = inp;
1584413628a7SBjoern A. Zeeb 					else
15856d6a026bSDavid Greenman 						local_wild = inp;
15866d6a026bSDavid Greenman 			}
1587413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1588413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1589413628a7SBjoern A. Zeeb 			return (jail_wild);
1590413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1591413628a7SBjoern A. Zeeb 			return (local_exact);
1592413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1593c3229e05SDavid Greenman 			return (local_wild);
1594413628a7SBjoern A. Zeeb #ifdef INET6
1595413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1596413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
1597413628a7SBjoern A. Zeeb #endif /* defined(INET6) */
159868e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1599413628a7SBjoern A. Zeeb 
16006d6a026bSDavid Greenman 	return (NULL);
160115bd2b43SDavid Greenman }
1602fa046d87SRobert Watson 
1603fa046d87SRobert Watson /*
1604fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1605fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1606fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1607fa046d87SRobert Watson  */
1608fa046d87SRobert Watson static struct inpcb *
1609fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1610fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1611fa046d87SRobert Watson     struct ifnet *ifp)
1612fa046d87SRobert Watson {
1613fa046d87SRobert Watson 	struct inpcb *inp;
1614fa046d87SRobert Watson 
1615fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1616fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1617fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1618fa046d87SRobert Watson 	if (inp != NULL) {
1619fa046d87SRobert Watson 		in_pcbref(inp);
1620fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1621fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1622fa046d87SRobert Watson 			INP_WLOCK(inp);
1623fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1624fa046d87SRobert Watson 				return (NULL);
1625fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1626fa046d87SRobert Watson 			INP_RLOCK(inp);
1627fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1628fa046d87SRobert Watson 				return (NULL);
1629fa046d87SRobert Watson 		} else
1630fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1631fa046d87SRobert Watson 	} else
1632fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1633fa046d87SRobert Watson 	return (inp);
1634fa046d87SRobert Watson }
1635fa046d87SRobert Watson 
1636fa046d87SRobert Watson /*
1637*d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1638*d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
1639fa046d87SRobert Watson  */
1640fa046d87SRobert Watson struct inpcb *
1641fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1642fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1643fa046d87SRobert Watson {
1644fa046d87SRobert Watson 
1645fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1646fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1647fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1648fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1649fa046d87SRobert Watson 
1650fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1651fa046d87SRobert Watson 	    lookupflags, ifp));
1652fa046d87SRobert Watson }
1653*d3c1f003SRobert Watson 
1654*d3c1f003SRobert Watson struct inpcb *
1655*d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1656*d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1657*d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
1658*d3c1f003SRobert Watson {
1659*d3c1f003SRobert Watson 
1660*d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1661*d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1662*d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1663*d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1664*d3c1f003SRobert Watson 
1665*d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1666*d3c1f003SRobert Watson 	    lookupflags, ifp));
1667*d3c1f003SRobert Watson }
166867107f45SBjoern A. Zeeb #endif /* INET */
166915bd2b43SDavid Greenman 
16707bc4aca7SDavid Greenman /*
1671c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
16727bc4aca7SDavid Greenman  */
1673c3229e05SDavid Greenman int
1674136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
167515bd2b43SDavid Greenman {
1676c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1677c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1678c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1679c3229e05SDavid Greenman 	struct inpcbport *phd;
1680cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
168115bd2b43SDavid Greenman 
16828501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1683fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
1684fa046d87SRobert Watson 
1685111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1686111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
1687602cc7f1SRobert Watson 
1688cfa1ca9dSYoshinobu Inoue #ifdef INET6
1689cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1690cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1691cfa1ca9dSYoshinobu Inoue 	else
1692cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1693cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1694cfa1ca9dSYoshinobu Inoue 
1695712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1696712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
169715bd2b43SDavid Greenman 
1698712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
1699712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1700c3229e05SDavid Greenman 
1701c3229e05SDavid Greenman 	/*
1702c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1703c3229e05SDavid Greenman 	 */
1704fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1705c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1706c3229e05SDavid Greenman 			break;
1707c3229e05SDavid Greenman 	}
1708c3229e05SDavid Greenman 	/*
1709c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1710c3229e05SDavid Greenman 	 */
1711c3229e05SDavid Greenman 	if (phd == NULL) {
17121ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1713c3229e05SDavid Greenman 		if (phd == NULL) {
1714c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1715c3229e05SDavid Greenman 		}
1716c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1717c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1718c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1719c3229e05SDavid Greenman 	}
1720c3229e05SDavid Greenman 	inp->inp_phd = phd;
1721c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1722c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1723111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
1724c3229e05SDavid Greenman 	return (0);
172515bd2b43SDavid Greenman }
172615bd2b43SDavid Greenman 
1727c3229e05SDavid Greenman /*
1728c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1729c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1730c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1731c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1732c3229e05SDavid Greenman  */
173315bd2b43SDavid Greenman void
1734*d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
173515bd2b43SDavid Greenman {
173659daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
173715bd2b43SDavid Greenman 	struct inpcbhead *head;
1738cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
173915bd2b43SDavid Greenman 
17408501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1741fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
1742fa046d87SRobert Watson 
1743111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
1744111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
1745602cc7f1SRobert Watson 
1746cfa1ca9dSYoshinobu Inoue #ifdef INET6
1747cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1748cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1749cfa1ca9dSYoshinobu Inoue 	else
1750cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1751cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1752cfa1ca9dSYoshinobu Inoue 
1753712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1754712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
175515bd2b43SDavid Greenman 
1756c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
175715bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1758c3229e05SDavid Greenman }
1759c3229e05SDavid Greenman 
1760*d3c1f003SRobert Watson void
1761*d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
1762*d3c1f003SRobert Watson {
1763*d3c1f003SRobert Watson 
1764*d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
1765*d3c1f003SRobert Watson }
1766*d3c1f003SRobert Watson 
1767c3229e05SDavid Greenman /*
1768c3229e05SDavid Greenman  * Remove PCB from various lists.
1769c3229e05SDavid Greenman  */
17706d888973SRobert Watson static void
1771136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1772c3229e05SDavid Greenman {
177359daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
177459daba27SSam Leffler 
177559daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
17768501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
177759daba27SSam Leffler 
177859daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1779111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
1780c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1781c3229e05SDavid Greenman 
1782fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
1783c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1784c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1785fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1786c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1787c3229e05SDavid Greenman 			free(phd, M_PCB);
1788c3229e05SDavid Greenman 		}
1789fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
1790111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
1791c3229e05SDavid Greenman 	}
1792c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
179359daba27SSam Leffler 	pcbinfo->ipi_count--;
179415bd2b43SDavid Greenman }
179575c13541SPoul-Henning Kamp 
1796a557af22SRobert Watson /*
1797a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1798a557af22SRobert Watson  * label change into the in_pcb for the socket.
1799a557af22SRobert Watson  */
1800a557af22SRobert Watson void
1801136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1802a557af22SRobert Watson {
1803a557af22SRobert Watson #ifdef MAC
1804a557af22SRobert Watson 	struct inpcb *inp;
1805a557af22SRobert Watson 
18064c7c478dSRobert Watson 	inp = sotoinpcb(so);
18074c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1808602cc7f1SRobert Watson 
18098501a69cSRobert Watson 	INP_WLOCK(inp);
1810310e7cebSRobert Watson 	SOCK_LOCK(so);
1811a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1812310e7cebSRobert Watson 	SOCK_UNLOCK(so);
18138501a69cSRobert Watson 	INP_WUNLOCK(inp);
1814a557af22SRobert Watson #endif
1815a557af22SRobert Watson }
18165f311da2SMike Silbersack 
18175f311da2SMike Silbersack /*
1818ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
1819ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
1820ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
1821ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
1822ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
18235f311da2SMike Silbersack  */
1824aae49dd3SBjoern A. Zeeb static void
1825136d4f1cSRobert Watson ipport_tick(void *xtp)
18265f311da2SMike Silbersack {
18278b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1828ad3a630fSRobert Watson 
18295ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
18308b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
18318b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
18328b615593SMarko Zec 		if (V_ipport_tcpallocs <=
18338b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
1834603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
1835603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
1836ad3a630fSRobert Watson 		} else
1837603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
1838603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
18398b615593SMarko Zec 		CURVNET_RESTORE();
18408b615593SMarko Zec 	}
18415ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
18425f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
18435f311da2SMike Silbersack }
1844497057eeSRobert Watson 
1845aae49dd3SBjoern A. Zeeb static void
1846aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
1847aae49dd3SBjoern A. Zeeb {
1848aae49dd3SBjoern A. Zeeb 
1849aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
1850aae49dd3SBjoern A. Zeeb }
1851aae49dd3SBjoern A. Zeeb 
1852aae49dd3SBjoern A. Zeeb /*
1853aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
1854aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
1855aae49dd3SBjoern A. Zeeb  */
1856aae49dd3SBjoern A. Zeeb static void
1857aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
1858aae49dd3SBjoern A. Zeeb {
1859aae49dd3SBjoern A. Zeeb 
1860aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
1861aae49dd3SBjoern A. Zeeb 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
1862aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
1863aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
1864aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
1865aae49dd3SBjoern A. Zeeb }
1866aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1867aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
1868aae49dd3SBjoern A. Zeeb 
18693d585327SKip Macy void
18703d585327SKip Macy inp_wlock(struct inpcb *inp)
18713d585327SKip Macy {
18723d585327SKip Macy 
18738501a69cSRobert Watson 	INP_WLOCK(inp);
18743d585327SKip Macy }
18753d585327SKip Macy 
18763d585327SKip Macy void
18773d585327SKip Macy inp_wunlock(struct inpcb *inp)
18783d585327SKip Macy {
18793d585327SKip Macy 
18808501a69cSRobert Watson 	INP_WUNLOCK(inp);
18813d585327SKip Macy }
18823d585327SKip Macy 
18833d585327SKip Macy void
18843d585327SKip Macy inp_rlock(struct inpcb *inp)
18853d585327SKip Macy {
18863d585327SKip Macy 
1887a69042a5SRobert Watson 	INP_RLOCK(inp);
18883d585327SKip Macy }
18893d585327SKip Macy 
18903d585327SKip Macy void
18913d585327SKip Macy inp_runlock(struct inpcb *inp)
18923d585327SKip Macy {
18933d585327SKip Macy 
1894a69042a5SRobert Watson 	INP_RUNLOCK(inp);
18953d585327SKip Macy }
18963d585327SKip Macy 
18973d585327SKip Macy #ifdef INVARIANTS
18983d585327SKip Macy void
1899e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
19003d585327SKip Macy {
19013d585327SKip Macy 
19028501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
19033d585327SKip Macy }
19043d585327SKip Macy 
19053d585327SKip Macy void
1906e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
19073d585327SKip Macy {
19083d585327SKip Macy 
19093d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
19103d585327SKip Macy }
19113d585327SKip Macy #endif
19123d585327SKip Macy 
19139378e437SKip Macy void
19149378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
19159378e437SKip Macy {
19169378e437SKip Macy 	struct inpcb *inp;
19179378e437SKip Macy 
1918603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
191997021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
19209378e437SKip Macy 		INP_WLOCK(inp);
19219378e437SKip Macy 		func(inp, arg);
19229378e437SKip Macy 		INP_WUNLOCK(inp);
19239378e437SKip Macy 	}
1924603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
19259378e437SKip Macy }
19269378e437SKip Macy 
19279378e437SKip Macy struct socket *
19289378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
19299378e437SKip Macy {
19309378e437SKip Macy 
19319378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
19329378e437SKip Macy 	return (inp->inp_socket);
19339378e437SKip Macy }
19349378e437SKip Macy 
19359378e437SKip Macy struct tcpcb *
19369378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
19379378e437SKip Macy {
19389378e437SKip Macy 
19399378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
19409378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
19419378e437SKip Macy }
19429378e437SKip Macy 
19439378e437SKip Macy int
19449378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
19459378e437SKip Macy {
19469378e437SKip Macy 
19479378e437SKip Macy 	return (inp->inp_ip_tos);
19489378e437SKip Macy }
19499378e437SKip Macy 
19509378e437SKip Macy void
19519378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
19529378e437SKip Macy {
19539378e437SKip Macy 
19549378e437SKip Macy 	inp->inp_ip_tos = val;
19559378e437SKip Macy }
19569378e437SKip Macy 
19579378e437SKip Macy void
1958df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
19599d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
19609378e437SKip Macy {
19619378e437SKip Macy 
19629d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
1963df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
1964df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
19659378e437SKip Macy 	*lp = inp->inp_lport;
19669378e437SKip Macy 	*fp = inp->inp_fport;
19679378e437SKip Macy }
19689378e437SKip Macy 
1969dd0e6c38SKip Macy struct inpcb *
1970dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
1971dd0e6c38SKip Macy {
1972dd0e6c38SKip Macy 
1973dd0e6c38SKip Macy 	return (sotoinpcb(so));
1974dd0e6c38SKip Macy }
1975dd0e6c38SKip Macy 
1976dd0e6c38SKip Macy struct tcpcb *
1977dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
1978dd0e6c38SKip Macy {
1979dd0e6c38SKip Macy 
1980dd0e6c38SKip Macy 	return (sototcpcb(so));
1981dd0e6c38SKip Macy }
1982dd0e6c38SKip Macy 
1983497057eeSRobert Watson #ifdef DDB
1984497057eeSRobert Watson static void
1985497057eeSRobert Watson db_print_indent(int indent)
1986497057eeSRobert Watson {
1987497057eeSRobert Watson 	int i;
1988497057eeSRobert Watson 
1989497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1990497057eeSRobert Watson 		db_printf(" ");
1991497057eeSRobert Watson }
1992497057eeSRobert Watson 
1993497057eeSRobert Watson static void
1994497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1995497057eeSRobert Watson {
1996497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
1997497057eeSRobert Watson 
1998497057eeSRobert Watson 	db_print_indent(indent);
1999497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2000497057eeSRobert Watson 
2001497057eeSRobert Watson 	indent += 2;
2002497057eeSRobert Watson 
200303dc38a4SRobert Watson #ifdef INET6
2004dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2005497057eeSRobert Watson 		/* IPv6. */
2006497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2007497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
2008497057eeSRobert Watson 	} else {
200903dc38a4SRobert Watson #endif
2010497057eeSRobert Watson 		/* IPv4. */
2011497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2012497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
201303dc38a4SRobert Watson #ifdef INET6
2014497057eeSRobert Watson 	}
201503dc38a4SRobert Watson #endif
2016497057eeSRobert Watson 	db_print_indent(indent);
2017497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2018497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2019497057eeSRobert Watson 	db_print_indent(indent);
2020497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2021497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2022497057eeSRobert Watson }
2023497057eeSRobert Watson 
2024497057eeSRobert Watson static void
2025497057eeSRobert Watson db_print_inpflags(int inp_flags)
2026497057eeSRobert Watson {
2027497057eeSRobert Watson 	int comma;
2028497057eeSRobert Watson 
2029497057eeSRobert Watson 	comma = 0;
2030497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2031497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2032497057eeSRobert Watson 		comma = 1;
2033497057eeSRobert Watson 	}
2034497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2035497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2036497057eeSRobert Watson 		comma = 1;
2037497057eeSRobert Watson 	}
2038497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2039497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2040497057eeSRobert Watson 		comma = 1;
2041497057eeSRobert Watson 	}
2042497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2043497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2044497057eeSRobert Watson 		comma = 1;
2045497057eeSRobert Watson 	}
2046497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2047497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2048497057eeSRobert Watson 		comma = 1;
2049497057eeSRobert Watson 	}
2050497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2051497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2052497057eeSRobert Watson 		comma = 1;
2053497057eeSRobert Watson 	}
2054497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2055497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2056497057eeSRobert Watson 		comma = 1;
2057497057eeSRobert Watson 	}
2058497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2059497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2060497057eeSRobert Watson 		comma = 1;
2061497057eeSRobert Watson 	}
2062497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2063497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2064497057eeSRobert Watson 		comma = 1;
2065497057eeSRobert Watson 	}
2066497057eeSRobert Watson 	if (inp_flags & INP_FAITH) {
2067497057eeSRobert Watson 		db_printf("%sINP_FAITH", comma ? ", " : "");
2068497057eeSRobert Watson 		comma = 1;
2069497057eeSRobert Watson 	}
2070497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2071497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2072497057eeSRobert Watson 		comma = 1;
2073497057eeSRobert Watson 	}
2074497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2075497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2076497057eeSRobert Watson 		comma = 1;
2077497057eeSRobert Watson 	}
2078497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2079497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2080497057eeSRobert Watson 		comma = 1;
2081497057eeSRobert Watson 	}
2082497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2083497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2084497057eeSRobert Watson 		comma = 1;
2085497057eeSRobert Watson 	}
2086497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2087497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2088497057eeSRobert Watson 		comma = 1;
2089497057eeSRobert Watson 	}
2090497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2091497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2092497057eeSRobert Watson 		comma = 1;
2093497057eeSRobert Watson 	}
2094497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2095497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2096497057eeSRobert Watson 		comma = 1;
2097497057eeSRobert Watson 	}
2098497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2099497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2100497057eeSRobert Watson 		comma = 1;
2101497057eeSRobert Watson 	}
2102497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2103497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2104497057eeSRobert Watson 		comma = 1;
2105497057eeSRobert Watson 	}
2106497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2107497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2108497057eeSRobert Watson 		comma = 1;
2109497057eeSRobert Watson 	}
2110497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2111497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2112497057eeSRobert Watson 		comma = 1;
2113497057eeSRobert Watson 	}
2114ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2115ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2116ad71fe3cSRobert Watson 		comma  = 1;
2117ad71fe3cSRobert Watson 	}
2118ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2119ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2120ad71fe3cSRobert Watson 		comma  = 1;
2121ad71fe3cSRobert Watson 	}
2122ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2123ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2124ad71fe3cSRobert Watson 		comma  = 1;
2125ad71fe3cSRobert Watson 	}
2126ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2127ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2128ad71fe3cSRobert Watson 		comma  = 1;
2129ad71fe3cSRobert Watson 	}
2130497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2131497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2132497057eeSRobert Watson 		comma = 1;
2133497057eeSRobert Watson 	}
2134497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2135497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2136497057eeSRobert Watson 		comma = 1;
2137497057eeSRobert Watson 	}
2138497057eeSRobert Watson }
2139497057eeSRobert Watson 
2140497057eeSRobert Watson static void
2141497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2142497057eeSRobert Watson {
2143497057eeSRobert Watson 	int comma;
2144497057eeSRobert Watson 
2145497057eeSRobert Watson 	comma = 0;
2146497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2147497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2148497057eeSRobert Watson 		comma  = 1;
2149497057eeSRobert Watson 	}
2150497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2151497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2152497057eeSRobert Watson 		comma  = 1;
2153497057eeSRobert Watson 	}
2154497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2155497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2156497057eeSRobert Watson 		comma  = 1;
2157497057eeSRobert Watson 	}
2158497057eeSRobert Watson }
2159497057eeSRobert Watson 
21606d888973SRobert Watson static void
2161497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2162497057eeSRobert Watson {
2163497057eeSRobert Watson 
2164497057eeSRobert Watson 	db_print_indent(indent);
2165497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2166497057eeSRobert Watson 
2167497057eeSRobert Watson 	indent += 2;
2168497057eeSRobert Watson 
2169497057eeSRobert Watson 	db_print_indent(indent);
2170497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2171497057eeSRobert Watson 
2172497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2173497057eeSRobert Watson 
2174497057eeSRobert Watson 	db_print_indent(indent);
2175497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2176497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2177497057eeSRobert Watson 
2178497057eeSRobert Watson 	db_print_indent(indent);
2179497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2180497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2181497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2182497057eeSRobert Watson 	db_printf(")\n");
2183497057eeSRobert Watson 
2184497057eeSRobert Watson 	db_print_indent(indent);
2185497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2186497057eeSRobert Watson 	    inp->inp_vflag);
2187497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2188497057eeSRobert Watson 	db_printf(")\n");
2189497057eeSRobert Watson 
2190497057eeSRobert Watson 	db_print_indent(indent);
2191497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2192497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2193497057eeSRobert Watson 
2194497057eeSRobert Watson 	db_print_indent(indent);
2195497057eeSRobert Watson #ifdef INET6
2196497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2197497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2198497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2199497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2200497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2201497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2202497057eeSRobert Watson 		    inp->in6p_hops);
2203497057eeSRobert Watson 	} else
2204497057eeSRobert Watson #endif
2205497057eeSRobert Watson 	{
2206497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2207497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2208497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2209497057eeSRobert Watson 	}
2210497057eeSRobert Watson 
2211497057eeSRobert Watson 	db_print_indent(indent);
2212497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2213497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2214497057eeSRobert Watson }
2215497057eeSRobert Watson 
2216497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2217497057eeSRobert Watson {
2218497057eeSRobert Watson 	struct inpcb *inp;
2219497057eeSRobert Watson 
2220497057eeSRobert Watson 	if (!have_addr) {
2221497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2222497057eeSRobert Watson 		return;
2223497057eeSRobert Watson 	}
2224497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2225497057eeSRobert Watson 
2226497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2227497057eeSRobert Watson }
2228497057eeSRobert Watson #endif
2229