xref: /freebsd/sys/netinet/in_pcb.c (revision fa046d8774c1fa5570e820bc81347c3b0039c6c9)
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 
130*fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
131*fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
132*fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
133*fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
13467107f45SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
13567107f45SBjoern A. Zeeb 
13667107f45SBjoern A. Zeeb #ifdef INET
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);
219*fa046d87SRobert 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);
225*fa046d87SRobert 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 
243*fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
244*fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
245*fa046d87SRobert 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);
250*fa046d87SRobert 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);
323*fa046d87SRobert 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);
365*fa046d87SRobert 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 	/*
486*fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
4878501a69cSRobert Watson 	 */
48859daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
489*fa046d87SRobert 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
624136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
625999f1343SGarrett Wollman {
6265200e00eSIan Dowse 	u_short lport, fport;
6275200e00eSIan Dowse 	in_addr_t laddr, faddr;
6285200e00eSIan Dowse 	int anonport, error;
629df8bae1dSRodney W. Grimes 
6308501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
631*fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
63227f74fd0SRobert Watson 
6335200e00eSIan Dowse 	lport = inp->inp_lport;
6345200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
6355200e00eSIan Dowse 	anonport = (lport == 0);
6365200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
637b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
6385200e00eSIan Dowse 	if (error)
6395200e00eSIan Dowse 		return (error);
6405200e00eSIan Dowse 
6415200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
6425200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
6435200e00eSIan Dowse 		inp->inp_lport = lport;
6445200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
6455200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
6465200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
6475200e00eSIan Dowse 			inp->inp_lport = 0;
6485200e00eSIan Dowse 			return (EAGAIN);
6495200e00eSIan Dowse 		}
6505200e00eSIan Dowse 	}
6515200e00eSIan Dowse 
6525200e00eSIan Dowse 	/* Commit the remaining changes. */
6535200e00eSIan Dowse 	inp->inp_lport = lport;
6545200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
6555200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
6565200e00eSIan Dowse 	inp->inp_fport = fport;
6575200e00eSIan Dowse 	in_pcbrehash(inp);
6582cb64cb2SGeorge V. Neville-Neil 
6595200e00eSIan Dowse 	if (anonport)
6605200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
6615200e00eSIan Dowse 	return (0);
6625200e00eSIan Dowse }
6635200e00eSIan Dowse 
6645200e00eSIan Dowse /*
6650895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
6660895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
6670895aec3SBjoern A. Zeeb  */
6680895aec3SBjoern A. Zeeb static int
6690895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
6700895aec3SBjoern A. Zeeb     struct ucred *cred)
6710895aec3SBjoern A. Zeeb {
6720895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
6730895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
6740895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
6750895aec3SBjoern A. Zeeb 	struct route sro;
6760895aec3SBjoern A. Zeeb 	int error;
6770895aec3SBjoern A. Zeeb 
678413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
6790895aec3SBjoern A. Zeeb 
680592bcae8SBjoern A. Zeeb 	/*
681592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
682592bcae8SBjoern A. Zeeb 	 * if requested.
683592bcae8SBjoern A. Zeeb 	 */
684592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
685592bcae8SBjoern A. Zeeb 		return (0);
686592bcae8SBjoern A. Zeeb 
6870895aec3SBjoern A. Zeeb 	error = 0;
6880895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
6890895aec3SBjoern A. Zeeb 
6900895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
6910895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
6920895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
6930895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
6940895aec3SBjoern A. Zeeb 
6950895aec3SBjoern A. Zeeb 	/*
6960895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
6970895aec3SBjoern A. Zeeb 	 * else punt.
6980895aec3SBjoern A. Zeeb 	 *
6990895aec3SBjoern A. Zeeb 	 * Find out route to destination.
7000895aec3SBjoern A. Zeeb 	 */
7010895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
7026e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
7030895aec3SBjoern A. Zeeb 
7040895aec3SBjoern A. Zeeb 	/*
7050895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
7060895aec3SBjoern A. Zeeb 	 * the outgoing interface.
7070895aec3SBjoern A. Zeeb 	 *
7080895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
7090895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
7100895aec3SBjoern A. Zeeb 	 * the source address from.
7110895aec3SBjoern A. Zeeb 	 */
7120895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
7138c0fec80SRobert Watson 		struct in_ifaddr *ia;
7140895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
7150895aec3SBjoern A. Zeeb 
7160895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
7170895aec3SBjoern A. Zeeb 		if (ia == NULL)
7180ed6142bSQing Li 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
7190895aec3SBjoern A. Zeeb 		if (ia == NULL) {
7200895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
7210895aec3SBjoern A. Zeeb 			goto done;
7220895aec3SBjoern A. Zeeb 		}
7230895aec3SBjoern A. Zeeb 
7240304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
7250895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7268c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
7270895aec3SBjoern A. Zeeb 			goto done;
7280895aec3SBjoern A. Zeeb 		}
7290895aec3SBjoern A. Zeeb 
7300895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
7318c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
7320895aec3SBjoern A. Zeeb 		ia = NULL;
7339317b04eSRobert Watson 		IF_ADDR_LOCK(ifp);
7340895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
7350895aec3SBjoern A. Zeeb 
7360895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
7370895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
7380895aec3SBjoern A. Zeeb 				continue;
7390895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
740b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
7410895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
7420895aec3SBjoern A. Zeeb 				break;
7430895aec3SBjoern A. Zeeb 			}
7440895aec3SBjoern A. Zeeb 		}
7450895aec3SBjoern A. Zeeb 		if (ia != NULL) {
7460895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7479317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
7480895aec3SBjoern A. Zeeb 			goto done;
7490895aec3SBjoern A. Zeeb 		}
7509317b04eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
7510895aec3SBjoern A. Zeeb 
7520895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
753b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
7540895aec3SBjoern A. Zeeb 		goto done;
7550895aec3SBjoern A. Zeeb 	}
7560895aec3SBjoern A. Zeeb 
7570895aec3SBjoern A. Zeeb 	/*
7580895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
7590895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
7600895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
7610895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
7620895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
7630895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
7640895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
7650895aec3SBjoern A. Zeeb 	 */
7660895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
7678c0fec80SRobert Watson 		struct in_ifaddr *ia;
7689317b04eSRobert Watson 		struct ifnet *ifp;
7690895aec3SBjoern A. Zeeb 
7700895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
7710304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
7720895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
7730895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7740895aec3SBjoern A. Zeeb 			goto done;
7750895aec3SBjoern A. Zeeb 		}
7760895aec3SBjoern A. Zeeb 
7770895aec3SBjoern A. Zeeb 		/* Jailed. */
7780895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
7790895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
780b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
7810895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
7820895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7830895aec3SBjoern A. Zeeb 			goto done;
7840895aec3SBjoern A. Zeeb 		}
7850895aec3SBjoern A. Zeeb 
7860895aec3SBjoern A. Zeeb 		/*
7870895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
7880895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
7890895aec3SBjoern A. Zeeb 		 */
7908c0fec80SRobert Watson 		ia = NULL;
7919317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
7929317b04eSRobert Watson 		IF_ADDR_LOCK(ifp);
7939317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
7940895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
7950895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
7960895aec3SBjoern A. Zeeb 				continue;
7970895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
798b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
7990895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8000895aec3SBjoern A. Zeeb 				break;
8010895aec3SBjoern A. Zeeb 			}
8020895aec3SBjoern A. Zeeb 		}
8030895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8040895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8059317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
8060895aec3SBjoern A. Zeeb 			goto done;
8070895aec3SBjoern A. Zeeb 		}
8089317b04eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
8090895aec3SBjoern A. Zeeb 
8100895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
811b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8120895aec3SBjoern A. Zeeb 		goto done;
8130895aec3SBjoern A. Zeeb 	}
8140895aec3SBjoern A. Zeeb 
8150895aec3SBjoern A. Zeeb 	/*
8160895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
8170895aec3SBjoern A. Zeeb 	 * to ourselves is here.
8180895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
8190895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
8200895aec3SBjoern A. Zeeb 	 * a loopback interface.
8210895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
8220895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
8230895aec3SBjoern A. Zeeb 	 */
8240895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
8250895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
8268c0fec80SRobert Watson 		struct in_ifaddr *ia;
8270895aec3SBjoern A. Zeeb 
8280895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
8290895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
8300895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
8310895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
8320895aec3SBjoern A. Zeeb 
8330895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
8340895aec3SBjoern A. Zeeb 		if (ia == NULL)
8350ed6142bSQing Li 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
836f0bb05fcSQing Li 		if (ia == NULL)
837f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
8380895aec3SBjoern A. Zeeb 
8390304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8400895aec3SBjoern A. Zeeb 			if (ia == NULL) {
8410895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
8420895aec3SBjoern A. Zeeb 				goto done;
8430895aec3SBjoern A. Zeeb 			}
8440895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8458c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8460895aec3SBjoern A. Zeeb 			goto done;
8470895aec3SBjoern A. Zeeb 		}
8480895aec3SBjoern A. Zeeb 
8490895aec3SBjoern A. Zeeb 		/* Jailed. */
8500895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8510895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
8520895aec3SBjoern A. Zeeb 
8530895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
8548c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8550895aec3SBjoern A. Zeeb 			ia = NULL;
8569317b04eSRobert Watson 			IF_ADDR_LOCK(ifp);
8570895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8580895aec3SBjoern A. Zeeb 
8590895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
8600895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
8610895aec3SBjoern A. Zeeb 					continue;
8620895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
863b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
864b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
8650895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
8660895aec3SBjoern A. Zeeb 					break;
8670895aec3SBjoern A. Zeeb 				}
8680895aec3SBjoern A. Zeeb 			}
8690895aec3SBjoern A. Zeeb 			if (ia != NULL) {
8700895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8719317b04eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
8720895aec3SBjoern A. Zeeb 				goto done;
8730895aec3SBjoern A. Zeeb 			}
8749317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
8750895aec3SBjoern A. Zeeb 		}
8760895aec3SBjoern A. Zeeb 
8770895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
878b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8790895aec3SBjoern A. Zeeb 		goto done;
8800895aec3SBjoern A. Zeeb 	}
8810895aec3SBjoern A. Zeeb 
8820895aec3SBjoern A. Zeeb done:
8830895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
8840895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
8850895aec3SBjoern A. Zeeb 	return (error);
8860895aec3SBjoern A. Zeeb }
8870895aec3SBjoern A. Zeeb 
8880895aec3SBjoern A. Zeeb /*
8895200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
8905200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
8915200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
8925200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
8935200e00eSIan Dowse  * the connect.
8945200e00eSIan Dowse  *
8955200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
8965200e00eSIan Dowse  * and port. These are not updated in the error case.
8975200e00eSIan Dowse  *
8985200e00eSIan Dowse  * If the operation fails because the connection already exists,
8995200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
9005200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
9015200e00eSIan Dowse  * is set to NULL.
9025200e00eSIan Dowse  */
9035200e00eSIan Dowse int
904136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
905136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
906136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
9075200e00eSIan Dowse {
9085200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
9095200e00eSIan Dowse 	struct in_ifaddr *ia;
9105200e00eSIan Dowse 	struct inpcb *oinp;
911b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
9125200e00eSIan Dowse 	u_short lport, fport;
9135200e00eSIan Dowse 	int error;
9145200e00eSIan Dowse 
9158501a69cSRobert Watson 	/*
9168501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
9178501a69cSRobert Watson 	 * lock is sufficient.
9188501a69cSRobert Watson 	 */
91927f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
920*fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
92127f74fd0SRobert Watson 
9225200e00eSIan Dowse 	if (oinpp != NULL)
9235200e00eSIan Dowse 		*oinpp = NULL;
92457bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
925df8bae1dSRodney W. Grimes 		return (EINVAL);
926df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
927df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
928df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
929df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
9305200e00eSIan Dowse 	laddr.s_addr = *laddrp;
9315200e00eSIan Dowse 	lport = *lportp;
9325200e00eSIan Dowse 	faddr = sin->sin_addr;
9335200e00eSIan Dowse 	fport = sin->sin_port;
9340895aec3SBjoern A. Zeeb 
935603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
936df8bae1dSRodney W. Grimes 		/*
937df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
938df8bae1dSRodney W. Grimes 		 * use the primary local address.
939df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
940df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
941df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
942df8bae1dSRodney W. Grimes 		 */
943413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
9442d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
945413628a7SBjoern A. Zeeb 			faddr =
946b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
9472d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
948b89e82ddSJamie Gritton 			if (cred != NULL &&
949b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
950b89e82ddSJamie Gritton 				return (error);
9512d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
9522d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
9532d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
9542d9cfabaSRobert Watson 			    IFF_BROADCAST)
9555200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
956603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
9572d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
9582d9cfabaSRobert Watson 		}
959df8bae1dSRodney W. Grimes 	}
9605200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
961d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
962df8bae1dSRodney W. Grimes 		/*
963df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
964d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
965df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
966df8bae1dSRodney W. Grimes 		 */
9675200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
968df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
969df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
970df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
973df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
974df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
9752d9cfabaSRobert Watson 				IN_IFADDR_RLOCK();
976e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
977e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
978e691be70SDaniel Eischen 					    (cred == NULL ||
979e691be70SDaniel Eischen 					    prison_check_ip4(cred,
980e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
981df8bae1dSRodney W. Grimes 						break;
982e691be70SDaniel Eischen 				}
983e691be70SDaniel Eischen 				if (ia == NULL)
984d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
985e691be70SDaniel Eischen 				else {
9865200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
987d79fdd98SDaniel Eischen 					error = 0;
988999f1343SGarrett Wollman 				}
989e691be70SDaniel Eischen 				IN_IFADDR_RUNLOCK();
990d79fdd98SDaniel Eischen 			}
991d79fdd98SDaniel Eischen 		}
99204215ed2SRandall Stewart 		if (error)
99304215ed2SRandall Stewart 			return (error);
9940895aec3SBjoern A. Zeeb 	}
995*fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
996*fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
9975200e00eSIan Dowse 	if (oinp != NULL) {
9985200e00eSIan Dowse 		if (oinpp != NULL)
9995200e00eSIan Dowse 			*oinpp = oinp;
1000df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1001c3229e05SDavid Greenman 	}
10025200e00eSIan Dowse 	if (lport == 0) {
1003b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1004b0330ed9SPawel Jakub Dawidek 		    cred);
10055a903f8dSPierre Beyssac 		if (error)
10065a903f8dSPierre Beyssac 			return (error);
10075a903f8dSPierre Beyssac 	}
10085200e00eSIan Dowse 	*laddrp = laddr.s_addr;
10095200e00eSIan Dowse 	*lportp = lport;
10105200e00eSIan Dowse 	*faddrp = faddr.s_addr;
10115200e00eSIan Dowse 	*fportp = fport;
1012df8bae1dSRodney W. Grimes 	return (0);
1013df8bae1dSRodney W. Grimes }
1014df8bae1dSRodney W. Grimes 
101526f9a767SRodney W. Grimes void
1016136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1017df8bae1dSRodney W. Grimes {
10186b348152SRobert Watson 
10198501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1020*fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1021df8bae1dSRodney W. Grimes 
1022df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1023df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
102415bd2b43SDavid Greenman 	in_pcbrehash(inp);
1025df8bae1dSRodney W. Grimes }
102667107f45SBjoern A. Zeeb #endif
1027df8bae1dSRodney W. Grimes 
10284c7c478dSRobert Watson /*
102928696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1030c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
103128696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
103228696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
10334c7c478dSRobert Watson  */
103426f9a767SRodney W. Grimes void
1035136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1036df8bae1dSRodney W. Grimes {
10374c7c478dSRobert Watson 
1038a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1039c0a211c5SRobert Watson 
10404c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
10414c7c478dSRobert Watson 	inp->inp_socket = NULL;
10424c7c478dSRobert Watson }
10434c7c478dSRobert Watson 
1044c0a211c5SRobert Watson /*
104579bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
104679bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
104779bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
104879bdc6e5SRobert Watson  * but where the inpcb lock is already held.
104979bdc6e5SRobert Watson  *
105079bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
105179bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
105279bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
105379bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
105479bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
105579bdc6e5SRobert Watson  * inpcb.
105679bdc6e5SRobert Watson  *
105779bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
105879bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
105979bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
106079bdc6e5SRobert Watson  * using in_pcbrele().
1061c0a211c5SRobert Watson  */
106279bdc6e5SRobert Watson void
106379bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
10644c7c478dSRobert Watson {
1065df8bae1dSRodney W. Grimes 
106679bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
106779bdc6e5SRobert Watson 
106879bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
106979bdc6e5SRobert Watson }
107079bdc6e5SRobert Watson 
107179bdc6e5SRobert Watson /*
107279bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
107379bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
107479bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
107579bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
107679bdc6e5SRobert Watson  *
107779bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
107879bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
107979bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
108079bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
108179bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
108279bdc6e5SRobert Watson  */
108379bdc6e5SRobert Watson int
108479bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
108579bdc6e5SRobert Watson {
108679bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
108779bdc6e5SRobert Watson 
108879bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
108979bdc6e5SRobert Watson 
109079bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
109179bdc6e5SRobert Watson 
109279bdc6e5SRobert Watson 	if (refcount_release(&inp->inp_refcount) == 0)
109379bdc6e5SRobert Watson 		return (0);
109479bdc6e5SRobert Watson 
109579bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
109679bdc6e5SRobert Watson 
109779bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
109879bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
109979bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
110079bdc6e5SRobert Watson 	return (1);
110179bdc6e5SRobert Watson }
110279bdc6e5SRobert Watson 
110379bdc6e5SRobert Watson int
110479bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
110579bdc6e5SRobert Watson {
110679bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
110779bdc6e5SRobert Watson 
110879bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
110979bdc6e5SRobert Watson 
111079bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
111179bdc6e5SRobert Watson 
111279bdc6e5SRobert Watson 	if (refcount_release(&inp->inp_refcount) == 0)
111379bdc6e5SRobert Watson 		return (0);
111479bdc6e5SRobert Watson 
111579bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
111679bdc6e5SRobert Watson 
111779bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
111879bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
111979bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
112079bdc6e5SRobert Watson 	return (1);
112179bdc6e5SRobert Watson }
112279bdc6e5SRobert Watson 
112379bdc6e5SRobert Watson /*
112479bdc6e5SRobert Watson  * Temporary wrapper.
112579bdc6e5SRobert Watson  */
112679bdc6e5SRobert Watson int
112779bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
112879bdc6e5SRobert Watson {
112979bdc6e5SRobert Watson 
113079bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
113179bdc6e5SRobert Watson }
113279bdc6e5SRobert Watson 
113379bdc6e5SRobert Watson /*
113479bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
113579bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
113679bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
113779bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
113879bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
113979bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
114079bdc6e5SRobert Watson  * the pcbinfo lock is held.
114179bdc6e5SRobert Watson  */
114279bdc6e5SRobert Watson void
114379bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
114479bdc6e5SRobert Watson {
114579bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
114679bdc6e5SRobert Watson 
114779bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
114879bdc6e5SRobert Watson 
114979bdc6e5SRobert Watson 	INP_INFO_WLOCK_ASSERT(pcbinfo);
115079bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
115179bdc6e5SRobert Watson 
115279bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1153b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11546aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
11556974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
1156b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
115779bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1158c3229e05SDavid Greenman 	in_pcbremlists(inp);
11596aee2fc5SBjoern A. Zeeb #ifdef INET6
11606aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
11616aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
11621096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
11636aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
11646aee2fc5SBjoern A. Zeeb 	}
11656aee2fc5SBjoern A. Zeeb #endif
1166df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1167df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
116867107f45SBjoern A. Zeeb #ifdef INET
116971498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
117071498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
117167107f45SBjoern A. Zeeb #endif
1172cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
117386d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1174a557af22SRobert Watson #ifdef MAC
117530d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1176a557af22SRobert Watson #endif
117779bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
117828696211SRobert Watson 		INP_WUNLOCK(inp);
117928696211SRobert Watson }
118028696211SRobert Watson 
118128696211SRobert Watson /*
1182c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1183c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1184c0a211c5SRobert Watson  *
1185c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1186c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1187c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1188c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1189c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1190c0a211c5SRobert Watson  * in_pcbdetach().
1191c0a211c5SRobert Watson  *
1192c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1193c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
119410702a28SRobert Watson  */
119510702a28SRobert Watson void
119610702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
119710702a28SRobert Watson {
119810702a28SRobert Watson 
11998501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
120010702a28SRobert Watson 
1201*fa046d87SRobert Watson 	/*
1202*fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1203*fa046d87SRobert Watson 	 * the hash lock...?
1204*fa046d87SRobert Watson 	 */
1205ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1206111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
120710702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
120810702a28SRobert Watson 
1209*fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
121010702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
121110702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
121210702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
121310702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
121410702a28SRobert Watson 			free(phd, M_PCB);
121510702a28SRobert Watson 		}
1216*fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1217111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
121810702a28SRobert Watson 	}
121910702a28SRobert Watson }
122010702a28SRobert Watson 
122167107f45SBjoern A. Zeeb #ifdef INET
122254d642bbSRobert Watson /*
122354d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
122454d642bbSRobert Watson  */
122526ef6ac4SDon Lewis struct sockaddr *
1226136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
122726ef6ac4SDon Lewis {
122826ef6ac4SDon Lewis 	struct sockaddr_in *sin;
122926ef6ac4SDon Lewis 
12301ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1231a163d034SWarner Losh 		M_WAITOK | M_ZERO);
123226ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
123326ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
123426ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
123526ef6ac4SDon Lewis 	sin->sin_port = port;
123626ef6ac4SDon Lewis 
123726ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
123826ef6ac4SDon Lewis }
123926ef6ac4SDon Lewis 
1240117bcae7SGarrett Wollman int
124154d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1242df8bae1dSRodney W. Grimes {
1243136d4f1cSRobert Watson 	struct inpcb *inp;
124426ef6ac4SDon Lewis 	struct in_addr addr;
124526ef6ac4SDon Lewis 	in_port_t port;
124642fa505bSDavid Greenman 
1247fdc984f7STor Egge 	inp = sotoinpcb(so);
124854d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
12496466b28aSRobert Watson 
1250a69042a5SRobert Watson 	INP_RLOCK(inp);
125126ef6ac4SDon Lewis 	port = inp->inp_lport;
125226ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1253a69042a5SRobert Watson 	INP_RUNLOCK(inp);
125442fa505bSDavid Greenman 
125526ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1256117bcae7SGarrett Wollman 	return 0;
1257df8bae1dSRodney W. Grimes }
1258df8bae1dSRodney W. Grimes 
1259117bcae7SGarrett Wollman int
126054d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1261df8bae1dSRodney W. Grimes {
1262136d4f1cSRobert Watson 	struct inpcb *inp;
126326ef6ac4SDon Lewis 	struct in_addr addr;
126426ef6ac4SDon Lewis 	in_port_t port;
126542fa505bSDavid Greenman 
1266fdc984f7STor Egge 	inp = sotoinpcb(so);
126754d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
12686466b28aSRobert Watson 
1269a69042a5SRobert Watson 	INP_RLOCK(inp);
127026ef6ac4SDon Lewis 	port = inp->inp_fport;
127126ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1272a69042a5SRobert Watson 	INP_RUNLOCK(inp);
127342fa505bSDavid Greenman 
127426ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1275117bcae7SGarrett Wollman 	return 0;
1276df8bae1dSRodney W. Grimes }
1277df8bae1dSRodney W. Grimes 
127826f9a767SRodney W. Grimes void
1279136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1280136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1281d1c54148SJesper Skriver {
1282f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1283d1c54148SJesper Skriver 
12843dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1285f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
12868501a69cSRobert Watson 		INP_WLOCK(inp);
1287d1c54148SJesper Skriver #ifdef INET6
1288f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
12898501a69cSRobert Watson 			INP_WUNLOCK(inp);
1290d1c54148SJesper Skriver 			continue;
1291f76fcf6dSJeffrey Hsu 		}
1292d1c54148SJesper Skriver #endif
1293d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1294f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
12958501a69cSRobert Watson 			INP_WUNLOCK(inp);
1296d1c54148SJesper Skriver 			continue;
1297d1c54148SJesper Skriver 		}
12983dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
12998501a69cSRobert Watson 			INP_WUNLOCK(inp);
1300f76fcf6dSJeffrey Hsu 	}
13013dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1302d1c54148SJesper Skriver }
1303d1c54148SJesper Skriver 
1304e43cc4aeSHajimu UMEMOTO void
1305136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1306e43cc4aeSHajimu UMEMOTO {
1307e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1308e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1309e43cc4aeSHajimu UMEMOTO 	int i, gap;
1310e43cc4aeSHajimu UMEMOTO 
1311f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
1312712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
13138501a69cSRobert Watson 		INP_WLOCK(inp);
1314e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1315e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1316e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1317e43cc4aeSHajimu UMEMOTO 			/*
1318e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1319e43cc4aeSHajimu UMEMOTO 			 * detached.
1320e43cc4aeSHajimu UMEMOTO 			 */
1321e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1322e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1323e43cc4aeSHajimu UMEMOTO 
1324e43cc4aeSHajimu UMEMOTO 			/*
1325e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1326e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1327e43cc4aeSHajimu UMEMOTO 			 */
1328e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1329e43cc4aeSHajimu UMEMOTO 			    i++) {
1330e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1331e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1332e43cc4aeSHajimu UMEMOTO 					gap++;
1333e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1334e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1335e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1336e43cc4aeSHajimu UMEMOTO 			}
1337e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1338e43cc4aeSHajimu UMEMOTO 		}
13398501a69cSRobert Watson 		INP_WUNLOCK(inp);
1340e43cc4aeSHajimu UMEMOTO 	}
13413cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
1342e43cc4aeSHajimu UMEMOTO }
1343e43cc4aeSHajimu UMEMOTO 
1344df8bae1dSRodney W. Grimes /*
1345*fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1346*fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1347c3229e05SDavid Greenman  */
1348d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1349df8bae1dSRodney W. Grimes struct inpcb *
1350136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
135168e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1352df8bae1dSRodney W. Grimes {
1353136d4f1cSRobert Watson 	struct inpcb *inp;
1354d5e8a67eSHajimu UMEMOTO #ifdef INET6
1355d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1356d5e8a67eSHajimu UMEMOTO #else
1357d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1358d5e8a67eSHajimu UMEMOTO #endif
1359d5e8a67eSHajimu UMEMOTO 	int wildcard;
13607bc4aca7SDavid Greenman 
136168e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
136268e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
136368e0d7e0SRobert Watson 
1364*fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
13651b73ca0bSSam Leffler 
136668e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1367c3229e05SDavid Greenman 		struct inpcbhead *head;
1368c3229e05SDavid Greenman 		/*
1369c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1370c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1371c3229e05SDavid Greenman 		 */
1372712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1373712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1374fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1375cfa1ca9dSYoshinobu Inoue #ifdef INET6
1376413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1377369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1378cfa1ca9dSYoshinobu Inoue 				continue;
1379cfa1ca9dSYoshinobu Inoue #endif
1380c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1381c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1382c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1383c3229e05SDavid Greenman 				/*
1384413628a7SBjoern A. Zeeb 				 * Found?
1385c3229e05SDavid Greenman 				 */
1386413628a7SBjoern A. Zeeb 				if (cred == NULL ||
13870304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
13880304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1389c3229e05SDavid Greenman 					return (inp);
1390df8bae1dSRodney W. Grimes 			}
1391c3229e05SDavid Greenman 		}
1392c3229e05SDavid Greenman 		/*
1393c3229e05SDavid Greenman 		 * Not found.
1394c3229e05SDavid Greenman 		 */
1395c3229e05SDavid Greenman 		return (NULL);
1396c3229e05SDavid Greenman 	} else {
1397c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1398c3229e05SDavid Greenman 		struct inpcbport *phd;
1399c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1400c3229e05SDavid Greenman 		/*
1401c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1402c3229e05SDavid Greenman 		 *
1403c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1404c3229e05SDavid Greenman 		 * port hash list.
1405c3229e05SDavid Greenman 		 */
1406712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1407712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1408fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1409c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1410c3229e05SDavid Greenman 				break;
1411c3229e05SDavid Greenman 		}
1412c3229e05SDavid Greenman 		if (phd != NULL) {
1413c3229e05SDavid Greenman 			/*
1414c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1415c3229e05SDavid Greenman 			 * fit.
1416c3229e05SDavid Greenman 			 */
141737d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1418c3229e05SDavid Greenman 				wildcard = 0;
1419413628a7SBjoern A. Zeeb 				if (cred != NULL &&
14200304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
14210304c731SJamie Gritton 					cred->cr_prison))
1422413628a7SBjoern A. Zeeb 					continue;
1423cfa1ca9dSYoshinobu Inoue #ifdef INET6
1424413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1425369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1426cfa1ca9dSYoshinobu Inoue 					continue;
1427d5e8a67eSHajimu UMEMOTO 				/*
1428d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1429d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1430d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1431d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1432d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1433d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1434d5e8a67eSHajimu UMEMOTO 				 *
1435d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1436d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1437d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1438d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1439d5e8a67eSHajimu UMEMOTO 				 */
1440d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1441d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1442cfa1ca9dSYoshinobu Inoue #endif
1443c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1444c3229e05SDavid Greenman 					wildcard++;
144515bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
144615bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
144715bd2b43SDavid Greenman 						wildcard++;
144815bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
144915bd2b43SDavid Greenman 						continue;
145015bd2b43SDavid Greenman 				} else {
145115bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
145215bd2b43SDavid Greenman 						wildcard++;
145315bd2b43SDavid Greenman 				}
1454df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1455df8bae1dSRodney W. Grimes 					match = inp;
1456df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1457413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1458df8bae1dSRodney W. Grimes 						break;
1459df8bae1dSRodney W. Grimes 				}
1460df8bae1dSRodney W. Grimes 			}
14613dbdc25cSDavid Greenman 		}
1462df8bae1dSRodney W. Grimes 		return (match);
1463df8bae1dSRodney W. Grimes 	}
1464c3229e05SDavid Greenman }
1465d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
146615bd2b43SDavid Greenman 
146715bd2b43SDavid Greenman /*
1468*fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1469*fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1470*fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
147115bd2b43SDavid Greenman  */
1472*fa046d87SRobert Watson static struct inpcb *
1473*fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
147468e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1475136d4f1cSRobert Watson     struct ifnet *ifp)
147615bd2b43SDavid Greenman {
147715bd2b43SDavid Greenman 	struct inpcbhead *head;
1478413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
147915bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
148015bd2b43SDavid Greenman 
148168e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
148268e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
148368e0d7e0SRobert Watson 
1484*fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1485602cc7f1SRobert Watson 
148615bd2b43SDavid Greenman 	/*
148715bd2b43SDavid Greenman 	 * First look for an exact match.
148815bd2b43SDavid Greenman 	 */
1489413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1490712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1491712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1492fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1493cfa1ca9dSYoshinobu Inoue #ifdef INET6
1494413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1495369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1496cfa1ca9dSYoshinobu Inoue 			continue;
1497cfa1ca9dSYoshinobu Inoue #endif
14986d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1499ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1500ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1501413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1502413628a7SBjoern A. Zeeb 			/*
1503413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1504413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1505413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1506413628a7SBjoern A. Zeeb 			 */
15070304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1508c3229e05SDavid Greenman 				return (inp);
1509413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1510413628a7SBjoern A. Zeeb 				tmpinp = inp;
1511c3229e05SDavid Greenman 		}
1512413628a7SBjoern A. Zeeb 	}
1513413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1514413628a7SBjoern A. Zeeb 		return (tmpinp);
1515e3fd5ffdSRobert Watson 
1516e3fd5ffdSRobert Watson 	/*
1517e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1518e3fd5ffdSRobert Watson 	 */
151968e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1520413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1521e3fd5ffdSRobert Watson #ifdef INET6
1522cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1523e3fd5ffdSRobert Watson #endif
1524413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1525413628a7SBjoern A. Zeeb 		int injail;
1526413628a7SBjoern A. Zeeb 
1527413628a7SBjoern A. Zeeb 		/*
1528413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1529413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1530413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1531413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1532413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1533413628a7SBjoern A. Zeeb 		 */
15346d6a026bSDavid Greenman 
1535712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1536712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1537fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1538cfa1ca9dSYoshinobu Inoue #ifdef INET6
1539413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1540369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1541cfa1ca9dSYoshinobu Inoue 				continue;
1542cfa1ca9dSYoshinobu Inoue #endif
1543413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1544413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1545413628a7SBjoern A. Zeeb 				continue;
1546413628a7SBjoern A. Zeeb 
1547413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1548cfa1ca9dSYoshinobu Inoue 			if (ifp && ifp->if_type == IFT_FAITH &&
1549cfa1ca9dSYoshinobu Inoue 			    (inp->inp_flags & INP_FAITH) == 0)
1550cfa1ca9dSYoshinobu Inoue 				continue;
1551413628a7SBjoern A. Zeeb 
15520304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1553413628a7SBjoern A. Zeeb 			if (injail) {
1554b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1555b89e82ddSJamie Gritton 				    &laddr) != 0)
1556413628a7SBjoern A. Zeeb 					continue;
1557413628a7SBjoern A. Zeeb 			} else {
1558413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1559413628a7SBjoern A. Zeeb 					continue;
1560413628a7SBjoern A. Zeeb 			}
1561413628a7SBjoern A. Zeeb 
1562413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1563413628a7SBjoern A. Zeeb 				if (injail)
1564c3229e05SDavid Greenman 					return (inp);
1565413628a7SBjoern A. Zeeb 				else
1566413628a7SBjoern A. Zeeb 					local_exact = inp;
1567413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1568e3fd5ffdSRobert Watson #ifdef INET6
1569413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
15705cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1571cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1572cfa1ca9dSYoshinobu Inoue 				else
1573413628a7SBjoern A. Zeeb #endif /* INET6 */
1574413628a7SBjoern A. Zeeb 					if (injail)
1575413628a7SBjoern A. Zeeb 						jail_wild = inp;
1576413628a7SBjoern A. Zeeb 					else
15776d6a026bSDavid Greenman 						local_wild = inp;
15786d6a026bSDavid Greenman 			}
1579413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1580413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1581413628a7SBjoern A. Zeeb 			return (jail_wild);
1582413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1583413628a7SBjoern A. Zeeb 			return (local_exact);
1584413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1585c3229e05SDavid Greenman 			return (local_wild);
1586413628a7SBjoern A. Zeeb #ifdef INET6
1587413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1588413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
1589413628a7SBjoern A. Zeeb #endif /* defined(INET6) */
159068e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1591413628a7SBjoern A. Zeeb 
15926d6a026bSDavid Greenman 	return (NULL);
159315bd2b43SDavid Greenman }
1594*fa046d87SRobert Watson 
1595*fa046d87SRobert Watson /*
1596*fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1597*fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1598*fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1599*fa046d87SRobert Watson  */
1600*fa046d87SRobert Watson static struct inpcb *
1601*fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1602*fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1603*fa046d87SRobert Watson     struct ifnet *ifp)
1604*fa046d87SRobert Watson {
1605*fa046d87SRobert Watson 	struct inpcb *inp;
1606*fa046d87SRobert Watson 
1607*fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1608*fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1609*fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1610*fa046d87SRobert Watson 	if (inp != NULL) {
1611*fa046d87SRobert Watson 		in_pcbref(inp);
1612*fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1613*fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1614*fa046d87SRobert Watson 			INP_WLOCK(inp);
1615*fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1616*fa046d87SRobert Watson 				return (NULL);
1617*fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1618*fa046d87SRobert Watson 			INP_RLOCK(inp);
1619*fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1620*fa046d87SRobert Watson 				return (NULL);
1621*fa046d87SRobert Watson 		} else
1622*fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1623*fa046d87SRobert Watson 	} else
1624*fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1625*fa046d87SRobert Watson 	return (inp);
1626*fa046d87SRobert Watson }
1627*fa046d87SRobert Watson 
1628*fa046d87SRobert Watson /*
1629*fa046d87SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple.
1630*fa046d87SRobert Watson  */
1631*fa046d87SRobert Watson struct inpcb *
1632*fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1633*fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1634*fa046d87SRobert Watson {
1635*fa046d87SRobert Watson 
1636*fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1637*fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1638*fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1639*fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
1640*fa046d87SRobert Watson 
1641*fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1642*fa046d87SRobert Watson 	    lookupflags, ifp));
1643*fa046d87SRobert Watson }
164467107f45SBjoern A. Zeeb #endif /* INET */
164515bd2b43SDavid Greenman 
16467bc4aca7SDavid Greenman /*
1647c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
16487bc4aca7SDavid Greenman  */
1649c3229e05SDavid Greenman int
1650136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
165115bd2b43SDavid Greenman {
1652c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1653c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1654c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1655c3229e05SDavid Greenman 	struct inpcbport *phd;
1656cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
165715bd2b43SDavid Greenman 
16588501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1659*fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
1660*fa046d87SRobert Watson 
1661111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1662111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
1663602cc7f1SRobert Watson 
1664cfa1ca9dSYoshinobu Inoue #ifdef INET6
1665cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1666cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1667cfa1ca9dSYoshinobu Inoue 	else
1668cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1669cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1670cfa1ca9dSYoshinobu Inoue 
1671712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1672712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
167315bd2b43SDavid Greenman 
1674712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
1675712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1676c3229e05SDavid Greenman 
1677c3229e05SDavid Greenman 	/*
1678c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1679c3229e05SDavid Greenman 	 */
1680fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1681c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1682c3229e05SDavid Greenman 			break;
1683c3229e05SDavid Greenman 	}
1684c3229e05SDavid Greenman 	/*
1685c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1686c3229e05SDavid Greenman 	 */
1687c3229e05SDavid Greenman 	if (phd == NULL) {
16881ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1689c3229e05SDavid Greenman 		if (phd == NULL) {
1690c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1691c3229e05SDavid Greenman 		}
1692c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1693c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1694c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1695c3229e05SDavid Greenman 	}
1696c3229e05SDavid Greenman 	inp->inp_phd = phd;
1697c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1698c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1699111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
1700c3229e05SDavid Greenman 	return (0);
170115bd2b43SDavid Greenman }
170215bd2b43SDavid Greenman 
1703c3229e05SDavid Greenman /*
1704c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1705c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1706c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1707c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1708c3229e05SDavid Greenman  */
170915bd2b43SDavid Greenman void
1710136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp)
171115bd2b43SDavid Greenman {
171259daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
171315bd2b43SDavid Greenman 	struct inpcbhead *head;
1714cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
171515bd2b43SDavid Greenman 
17168501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1717*fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
1718*fa046d87SRobert Watson 
1719111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
1720111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
1721602cc7f1SRobert Watson 
1722cfa1ca9dSYoshinobu Inoue #ifdef INET6
1723cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1724cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1725cfa1ca9dSYoshinobu Inoue 	else
1726cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1727cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1728cfa1ca9dSYoshinobu Inoue 
1729712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1730712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
173115bd2b43SDavid Greenman 
1732c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
173315bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1734c3229e05SDavid Greenman }
1735c3229e05SDavid Greenman 
1736c3229e05SDavid Greenman /*
1737c3229e05SDavid Greenman  * Remove PCB from various lists.
1738c3229e05SDavid Greenman  */
17396d888973SRobert Watson static void
1740136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1741c3229e05SDavid Greenman {
174259daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
174359daba27SSam Leffler 
174459daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
17458501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
174659daba27SSam Leffler 
174759daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1748111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
1749c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1750c3229e05SDavid Greenman 
1751*fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
1752c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1753c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1754fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1755c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1756c3229e05SDavid Greenman 			free(phd, M_PCB);
1757c3229e05SDavid Greenman 		}
1758*fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
1759111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
1760c3229e05SDavid Greenman 	}
1761c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
176259daba27SSam Leffler 	pcbinfo->ipi_count--;
176315bd2b43SDavid Greenman }
176475c13541SPoul-Henning Kamp 
1765a557af22SRobert Watson /*
1766a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1767a557af22SRobert Watson  * label change into the in_pcb for the socket.
1768a557af22SRobert Watson  */
1769a557af22SRobert Watson void
1770136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1771a557af22SRobert Watson {
1772a557af22SRobert Watson #ifdef MAC
1773a557af22SRobert Watson 	struct inpcb *inp;
1774a557af22SRobert Watson 
17754c7c478dSRobert Watson 	inp = sotoinpcb(so);
17764c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1777602cc7f1SRobert Watson 
17788501a69cSRobert Watson 	INP_WLOCK(inp);
1779310e7cebSRobert Watson 	SOCK_LOCK(so);
1780a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1781310e7cebSRobert Watson 	SOCK_UNLOCK(so);
17828501a69cSRobert Watson 	INP_WUNLOCK(inp);
1783a557af22SRobert Watson #endif
1784a557af22SRobert Watson }
17855f311da2SMike Silbersack 
17865f311da2SMike Silbersack /*
1787ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
1788ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
1789ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
1790ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
1791ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
17925f311da2SMike Silbersack  */
1793aae49dd3SBjoern A. Zeeb static void
1794136d4f1cSRobert Watson ipport_tick(void *xtp)
17955f311da2SMike Silbersack {
17968b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1797ad3a630fSRobert Watson 
17985ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
17998b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
18008b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
18018b615593SMarko Zec 		if (V_ipport_tcpallocs <=
18028b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
1803603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
1804603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
1805ad3a630fSRobert Watson 		} else
1806603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
1807603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
18088b615593SMarko Zec 		CURVNET_RESTORE();
18098b615593SMarko Zec 	}
18105ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
18115f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
18125f311da2SMike Silbersack }
1813497057eeSRobert Watson 
1814aae49dd3SBjoern A. Zeeb static void
1815aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
1816aae49dd3SBjoern A. Zeeb {
1817aae49dd3SBjoern A. Zeeb 
1818aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
1819aae49dd3SBjoern A. Zeeb }
1820aae49dd3SBjoern A. Zeeb 
1821aae49dd3SBjoern A. Zeeb /*
1822aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
1823aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
1824aae49dd3SBjoern A. Zeeb  */
1825aae49dd3SBjoern A. Zeeb static void
1826aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
1827aae49dd3SBjoern A. Zeeb {
1828aae49dd3SBjoern A. Zeeb 
1829aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
1830aae49dd3SBjoern A. Zeeb 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
1831aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
1832aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
1833aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
1834aae49dd3SBjoern A. Zeeb }
1835aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1836aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
1837aae49dd3SBjoern A. Zeeb 
18383d585327SKip Macy void
18393d585327SKip Macy inp_wlock(struct inpcb *inp)
18403d585327SKip Macy {
18413d585327SKip Macy 
18428501a69cSRobert Watson 	INP_WLOCK(inp);
18433d585327SKip Macy }
18443d585327SKip Macy 
18453d585327SKip Macy void
18463d585327SKip Macy inp_wunlock(struct inpcb *inp)
18473d585327SKip Macy {
18483d585327SKip Macy 
18498501a69cSRobert Watson 	INP_WUNLOCK(inp);
18503d585327SKip Macy }
18513d585327SKip Macy 
18523d585327SKip Macy void
18533d585327SKip Macy inp_rlock(struct inpcb *inp)
18543d585327SKip Macy {
18553d585327SKip Macy 
1856a69042a5SRobert Watson 	INP_RLOCK(inp);
18573d585327SKip Macy }
18583d585327SKip Macy 
18593d585327SKip Macy void
18603d585327SKip Macy inp_runlock(struct inpcb *inp)
18613d585327SKip Macy {
18623d585327SKip Macy 
1863a69042a5SRobert Watson 	INP_RUNLOCK(inp);
18643d585327SKip Macy }
18653d585327SKip Macy 
18663d585327SKip Macy #ifdef INVARIANTS
18673d585327SKip Macy void
1868e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
18693d585327SKip Macy {
18703d585327SKip Macy 
18718501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
18723d585327SKip Macy }
18733d585327SKip Macy 
18743d585327SKip Macy void
1875e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
18763d585327SKip Macy {
18773d585327SKip Macy 
18783d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
18793d585327SKip Macy }
18803d585327SKip Macy #endif
18813d585327SKip Macy 
18829378e437SKip Macy void
18839378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
18849378e437SKip Macy {
18859378e437SKip Macy 	struct inpcb *inp;
18869378e437SKip Macy 
1887603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
188897021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
18899378e437SKip Macy 		INP_WLOCK(inp);
18909378e437SKip Macy 		func(inp, arg);
18919378e437SKip Macy 		INP_WUNLOCK(inp);
18929378e437SKip Macy 	}
1893603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
18949378e437SKip Macy }
18959378e437SKip Macy 
18969378e437SKip Macy struct socket *
18979378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
18989378e437SKip Macy {
18999378e437SKip Macy 
19009378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
19019378e437SKip Macy 	return (inp->inp_socket);
19029378e437SKip Macy }
19039378e437SKip Macy 
19049378e437SKip Macy struct tcpcb *
19059378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
19069378e437SKip Macy {
19079378e437SKip Macy 
19089378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
19099378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
19109378e437SKip Macy }
19119378e437SKip Macy 
19129378e437SKip Macy int
19139378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
19149378e437SKip Macy {
19159378e437SKip Macy 
19169378e437SKip Macy 	return (inp->inp_ip_tos);
19179378e437SKip Macy }
19189378e437SKip Macy 
19199378e437SKip Macy void
19209378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
19219378e437SKip Macy {
19229378e437SKip Macy 
19239378e437SKip Macy 	inp->inp_ip_tos = val;
19249378e437SKip Macy }
19259378e437SKip Macy 
19269378e437SKip Macy void
1927df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
19289d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
19299378e437SKip Macy {
19309378e437SKip Macy 
19319d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
1932df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
1933df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
19349378e437SKip Macy 	*lp = inp->inp_lport;
19359378e437SKip Macy 	*fp = inp->inp_fport;
19369378e437SKip Macy }
19379378e437SKip Macy 
1938dd0e6c38SKip Macy struct inpcb *
1939dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
1940dd0e6c38SKip Macy {
1941dd0e6c38SKip Macy 
1942dd0e6c38SKip Macy 	return (sotoinpcb(so));
1943dd0e6c38SKip Macy }
1944dd0e6c38SKip Macy 
1945dd0e6c38SKip Macy struct tcpcb *
1946dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
1947dd0e6c38SKip Macy {
1948dd0e6c38SKip Macy 
1949dd0e6c38SKip Macy 	return (sototcpcb(so));
1950dd0e6c38SKip Macy }
1951dd0e6c38SKip Macy 
1952497057eeSRobert Watson #ifdef DDB
1953497057eeSRobert Watson static void
1954497057eeSRobert Watson db_print_indent(int indent)
1955497057eeSRobert Watson {
1956497057eeSRobert Watson 	int i;
1957497057eeSRobert Watson 
1958497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1959497057eeSRobert Watson 		db_printf(" ");
1960497057eeSRobert Watson }
1961497057eeSRobert Watson 
1962497057eeSRobert Watson static void
1963497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1964497057eeSRobert Watson {
1965497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
1966497057eeSRobert Watson 
1967497057eeSRobert Watson 	db_print_indent(indent);
1968497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
1969497057eeSRobert Watson 
1970497057eeSRobert Watson 	indent += 2;
1971497057eeSRobert Watson 
197203dc38a4SRobert Watson #ifdef INET6
1973dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
1974497057eeSRobert Watson 		/* IPv6. */
1975497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1976497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1977497057eeSRobert Watson 	} else {
197803dc38a4SRobert Watson #endif
1979497057eeSRobert Watson 		/* IPv4. */
1980497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1981497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
198203dc38a4SRobert Watson #ifdef INET6
1983497057eeSRobert Watson 	}
198403dc38a4SRobert Watson #endif
1985497057eeSRobert Watson 	db_print_indent(indent);
1986497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1987497057eeSRobert Watson 	    ntohs(inc->inc_lport));
1988497057eeSRobert Watson 	db_print_indent(indent);
1989497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1990497057eeSRobert Watson 	    ntohs(inc->inc_fport));
1991497057eeSRobert Watson }
1992497057eeSRobert Watson 
1993497057eeSRobert Watson static void
1994497057eeSRobert Watson db_print_inpflags(int inp_flags)
1995497057eeSRobert Watson {
1996497057eeSRobert Watson 	int comma;
1997497057eeSRobert Watson 
1998497057eeSRobert Watson 	comma = 0;
1999497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2000497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2001497057eeSRobert Watson 		comma = 1;
2002497057eeSRobert Watson 	}
2003497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2004497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2005497057eeSRobert Watson 		comma = 1;
2006497057eeSRobert Watson 	}
2007497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2008497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2009497057eeSRobert Watson 		comma = 1;
2010497057eeSRobert Watson 	}
2011497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2012497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2013497057eeSRobert Watson 		comma = 1;
2014497057eeSRobert Watson 	}
2015497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2016497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2017497057eeSRobert Watson 		comma = 1;
2018497057eeSRobert Watson 	}
2019497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2020497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2021497057eeSRobert Watson 		comma = 1;
2022497057eeSRobert Watson 	}
2023497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2024497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2025497057eeSRobert Watson 		comma = 1;
2026497057eeSRobert Watson 	}
2027497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2028497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2029497057eeSRobert Watson 		comma = 1;
2030497057eeSRobert Watson 	}
2031497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2032497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2033497057eeSRobert Watson 		comma = 1;
2034497057eeSRobert Watson 	}
2035497057eeSRobert Watson 	if (inp_flags & INP_FAITH) {
2036497057eeSRobert Watson 		db_printf("%sINP_FAITH", comma ? ", " : "");
2037497057eeSRobert Watson 		comma = 1;
2038497057eeSRobert Watson 	}
2039497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2040497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2041497057eeSRobert Watson 		comma = 1;
2042497057eeSRobert Watson 	}
2043497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2044497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2045497057eeSRobert Watson 		comma = 1;
2046497057eeSRobert Watson 	}
2047497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2048497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2049497057eeSRobert Watson 		comma = 1;
2050497057eeSRobert Watson 	}
2051497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2052497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2053497057eeSRobert Watson 		comma = 1;
2054497057eeSRobert Watson 	}
2055497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2056497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2057497057eeSRobert Watson 		comma = 1;
2058497057eeSRobert Watson 	}
2059497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2060497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2061497057eeSRobert Watson 		comma = 1;
2062497057eeSRobert Watson 	}
2063497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2064497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2065497057eeSRobert Watson 		comma = 1;
2066497057eeSRobert Watson 	}
2067497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2068497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2069497057eeSRobert Watson 		comma = 1;
2070497057eeSRobert Watson 	}
2071497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2072497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2073497057eeSRobert Watson 		comma = 1;
2074497057eeSRobert Watson 	}
2075497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2076497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2077497057eeSRobert Watson 		comma = 1;
2078497057eeSRobert Watson 	}
2079497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2080497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2081497057eeSRobert Watson 		comma = 1;
2082497057eeSRobert Watson 	}
2083ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2084ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2085ad71fe3cSRobert Watson 		comma  = 1;
2086ad71fe3cSRobert Watson 	}
2087ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2088ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2089ad71fe3cSRobert Watson 		comma  = 1;
2090ad71fe3cSRobert Watson 	}
2091ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2092ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2093ad71fe3cSRobert Watson 		comma  = 1;
2094ad71fe3cSRobert Watson 	}
2095ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2096ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2097ad71fe3cSRobert Watson 		comma  = 1;
2098ad71fe3cSRobert Watson 	}
2099497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2100497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2101497057eeSRobert Watson 		comma = 1;
2102497057eeSRobert Watson 	}
2103497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2104497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2105497057eeSRobert Watson 		comma = 1;
2106497057eeSRobert Watson 	}
2107497057eeSRobert Watson }
2108497057eeSRobert Watson 
2109497057eeSRobert Watson static void
2110497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2111497057eeSRobert Watson {
2112497057eeSRobert Watson 	int comma;
2113497057eeSRobert Watson 
2114497057eeSRobert Watson 	comma = 0;
2115497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2116497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2117497057eeSRobert Watson 		comma  = 1;
2118497057eeSRobert Watson 	}
2119497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2120497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2121497057eeSRobert Watson 		comma  = 1;
2122497057eeSRobert Watson 	}
2123497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2124497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2125497057eeSRobert Watson 		comma  = 1;
2126497057eeSRobert Watson 	}
2127497057eeSRobert Watson }
2128497057eeSRobert Watson 
21296d888973SRobert Watson static void
2130497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2131497057eeSRobert Watson {
2132497057eeSRobert Watson 
2133497057eeSRobert Watson 	db_print_indent(indent);
2134497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2135497057eeSRobert Watson 
2136497057eeSRobert Watson 	indent += 2;
2137497057eeSRobert Watson 
2138497057eeSRobert Watson 	db_print_indent(indent);
2139497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2140497057eeSRobert Watson 
2141497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2142497057eeSRobert Watson 
2143497057eeSRobert Watson 	db_print_indent(indent);
2144497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2145497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2146497057eeSRobert Watson 
2147497057eeSRobert Watson 	db_print_indent(indent);
2148497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2149497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2150497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2151497057eeSRobert Watson 	db_printf(")\n");
2152497057eeSRobert Watson 
2153497057eeSRobert Watson 	db_print_indent(indent);
2154497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2155497057eeSRobert Watson 	    inp->inp_vflag);
2156497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2157497057eeSRobert Watson 	db_printf(")\n");
2158497057eeSRobert Watson 
2159497057eeSRobert Watson 	db_print_indent(indent);
2160497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2161497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2162497057eeSRobert Watson 
2163497057eeSRobert Watson 	db_print_indent(indent);
2164497057eeSRobert Watson #ifdef INET6
2165497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2166497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2167497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2168497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2169497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2170497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2171497057eeSRobert Watson 		    inp->in6p_hops);
2172497057eeSRobert Watson 	} else
2173497057eeSRobert Watson #endif
2174497057eeSRobert Watson 	{
2175497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2176497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2177497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2178497057eeSRobert Watson 	}
2179497057eeSRobert Watson 
2180497057eeSRobert Watson 	db_print_indent(indent);
2181497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2182497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2183497057eeSRobert Watson }
2184497057eeSRobert Watson 
2185497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2186497057eeSRobert Watson {
2187497057eeSRobert Watson 	struct inpcb *inp;
2188497057eeSRobert Watson 
2189497057eeSRobert Watson 	if (!have_addr) {
2190497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2191497057eeSRobert Watson 		return;
2192497057eeSRobert Watson 	}
2193497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2194497057eeSRobert Watson 
2195497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2196497057eeSRobert Watson }
2197497057eeSRobert Watson #endif
2198