xref: /freebsd/sys/netinet/in_pcb.c (revision b89e82dd87dec9034c8b7f1a01524dceea11ff49)
1c398230bSWarner Losh /*-
22469dd60SGarrett Wollman  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3497057eeSRobert Watson  *	The Regents of the University of California.
428696211SRobert Watson  * Copyright (c) 2007-2008 Robert N. M. Watson
5497057eeSRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
312469dd60SGarrett Wollman  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
37497057eeSRobert Watson #include "opt_ddb.h"
38be9347e3SAdrian Chadd #include "opt_inet.h"
396a800098SYoshinobu Inoue #include "opt_ipsec.h"
40cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
41a557af22SRobert Watson #include "opt_mac.h"
42cfa1ca9dSYoshinobu Inoue 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51acd3428bSRobert Watson #include <sys/priv.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
5375c13541SPoul-Henning Kamp #include <sys/jail.h>
54101f9fc8SPeter Wemm #include <sys/kernel.h>
55101f9fc8SPeter Wemm #include <sys/sysctl.h>
56603724d3SBjoern A. Zeeb #include <sys/vimage.h>
578781d8e9SBruce Evans 
58497057eeSRobert Watson #ifdef DDB
59497057eeSRobert Watson #include <ddb/ddb.h>
60497057eeSRobert Watson #endif
61497057eeSRobert Watson 
6269c2d429SJeff Roberson #include <vm/uma.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <net/if.h>
65cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
66df8bae1dSRodney W. Grimes #include <net/route.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #include <netinet/in.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
70df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
72340c35deSJonathan Lemon #include <netinet/tcp_var.h>
735f311da2SMike Silbersack #include <netinet/udp.h>
745f311da2SMike Silbersack #include <netinet/udp_var.h>
754b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
76cfa1ca9dSYoshinobu Inoue #ifdef INET6
77cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
78cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
794b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h>
80cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
81cfa1ca9dSYoshinobu Inoue 
82df8bae1dSRodney W. Grimes 
83b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
84b9234fafSSam Leffler #include <netipsec/ipsec.h>
85b9234fafSSam Leffler #include <netipsec/key.h>
86b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
87b9234fafSSam Leffler 
88aed55708SRobert Watson #include <security/mac/mac_framework.h>
89aed55708SRobert Watson 
9044e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
91101f9fc8SPeter Wemm /*
92101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
93101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
94101f9fc8SPeter Wemm  */
9544e33a07SMarko Zec int	ipport_lowfirstauto;
9644e33a07SMarko Zec int	ipport_lowlastauto;
9744e33a07SMarko Zec int	ipport_firstauto;
9844e33a07SMarko Zec int	ipport_lastauto;
9944e33a07SMarko Zec int	ipport_hifirstauto;
10044e33a07SMarko Zec int	ipport_hilastauto;
101101f9fc8SPeter Wemm 
102b0d22693SCrist J. Clark /*
103b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
104b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
105b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
106b0d22693SCrist J. Clark  */
10744e33a07SMarko Zec int	ipport_reservedhigh;
10844e33a07SMarko Zec int	ipport_reservedlow;
109b0d22693SCrist J. Clark 
1105f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
11144e33a07SMarko Zec int	ipport_randomized;
11244e33a07SMarko Zec int	ipport_randomcps;
11344e33a07SMarko Zec int	ipport_randomtime;
11444e33a07SMarko Zec int	ipport_stoprandom;
1155f311da2SMike Silbersack int	ipport_tcpallocs;
1165f311da2SMike Silbersack int	ipport_tcplastcount;
11744e33a07SMarko Zec #endif
1186ac48b74SMike Silbersack 
119bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
120bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
121bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
122bbd42ad0SPeter Wemm 
123bbd42ad0SPeter Wemm static int
12482d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
125bbd42ad0SPeter Wemm {
12697021c24SMarko Zec 	INIT_VNET_INET(curvnet);
12730a4ab08SBruce Evans 	int error;
12830a4ab08SBruce Evans 
12930a4ab08SBruce Evans 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
13030a4ab08SBruce Evans 	if (error == 0) {
131603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
132603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
133603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
134603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
135603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
136603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
137bbd42ad0SPeter Wemm 	}
13830a4ab08SBruce Evans 	return (error);
139bbd42ad0SPeter Wemm }
140bbd42ad0SPeter Wemm 
141bbd42ad0SPeter Wemm #undef RANGECHK
142bbd42ad0SPeter Wemm 
14333b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
14433b3ac06SPeter Wemm 
1458b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
1468b615593SMarko Zec 	lowfirst, CTLTYPE_INT|CTLFLAG_RW, ipport_lowfirstauto, 0,
1478b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
1488b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
1498b615593SMarko Zec 	lowlast, CTLTYPE_INT|CTLFLAG_RW, ipport_lowlastauto, 0,
1508b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
1518b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
1528b615593SMarko Zec 	first, CTLTYPE_INT|CTLFLAG_RW, ipport_firstauto, 0,
1538b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
1548b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
1558b615593SMarko Zec 	last, CTLTYPE_INT|CTLFLAG_RW, ipport_lastauto, 0,
1568b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
1578b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
1588b615593SMarko Zec 	hifirst, CTLTYPE_INT|CTLFLAG_RW, ipport_hifirstauto, 0,
1598b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
1608b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
1618b615593SMarko Zec 	hilast, CTLTYPE_INT|CTLFLAG_RW, ipport_hilastauto, 0,
1628b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
1638b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
1648b615593SMarko Zec 	reservedhigh, CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedhigh, 0, "");
1658b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, reservedlow,
1668b615593SMarko Zec 	CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedlow, 0, "");
1678b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomized,
1688b615593SMarko Zec 	CTLFLAG_RW, ipport_randomized, 0, "Enable random port allocation");
1698b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomcps,
1708b615593SMarko Zec 	CTLFLAG_RW, ipport_randomcps, 0, "Maximum number of random port "
1716ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
1728b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomtime,
1738b615593SMarko Zec 	CTLFLAG_RW, ipport_randomtime, 0,
1748b615593SMarko Zec 	"Minimum time to keep sequental port "
1756ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
1760312fbe9SPoul-Henning Kamp 
177c3229e05SDavid Greenman /*
178c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
179c3229e05SDavid Greenman  *
180de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
181de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
182de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
183c3229e05SDavid Greenman  */
184c3229e05SDavid Greenman 
185c3229e05SDavid Greenman /*
186c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
187d915b280SStephan Uphoff  * On success return with the PCB locked.
188c3229e05SDavid Greenman  */
189df8bae1dSRodney W. Grimes int
190d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
191df8bae1dSRodney W. Grimes {
1928b615593SMarko Zec #ifdef INET6
1938b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
1948b615593SMarko Zec #endif
195136d4f1cSRobert Watson 	struct inpcb *inp;
19613cf67f3SHajimu UMEMOTO 	int error;
197a557af22SRobert Watson 
19859daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
199a557af22SRobert Watson 	error = 0;
200d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
201df8bae1dSRodney W. Grimes 	if (inp == NULL)
202df8bae1dSRodney W. Grimes 		return (ENOBUFS);
203d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
20415bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
205df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
20686d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
2078b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
208a557af22SRobert Watson #ifdef MAC
20930d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
210a557af22SRobert Watson 	if (error != 0)
211a557af22SRobert Watson 		goto out;
212310e7cebSRobert Watson 	SOCK_LOCK(so);
21330d239bcSRobert Watson 	mac_inpcb_create(so, inp);
214310e7cebSRobert Watson 	SOCK_UNLOCK(so);
215a557af22SRobert Watson #endif
216b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
21713cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
2180bffde27SRobert Watson 	if (error != 0) {
2190bffde27SRobert Watson #ifdef MAC
2200bffde27SRobert Watson 		mac_inpcb_destroy(inp);
2210bffde27SRobert Watson #endif
222a557af22SRobert Watson 		goto out;
2230bffde27SRobert Watson 	}
224b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
225e3fd5ffdSRobert Watson #ifdef INET6
226340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
227340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
228603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
22933841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
230340c35deSJonathan Lemon 	}
23175daea93SPaul Saab #endif
232712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
2333d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
234df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
23533841545SHajimu UMEMOTO #ifdef INET6
236603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
23733841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
23833841545SHajimu UMEMOTO #endif
2398501a69cSRobert Watson 	INP_WLOCK(inp);
240d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
24128696211SRobert Watson 	inp->inp_refcount = 1;	/* Reference from the inpcbinfo */
242b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
243a557af22SRobert Watson out:
24486d02c5cSBjoern A. Zeeb 	if (error != 0) {
24586d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
246a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
24786d02c5cSBjoern A. Zeeb 	}
248a557af22SRobert Watson #endif
249a557af22SRobert Watson 	return (error);
250df8bae1dSRodney W. Grimes }
251df8bae1dSRodney W. Grimes 
252df8bae1dSRodney W. Grimes int
253136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
254df8bae1dSRodney W. Grimes {
2554b932371SIan Dowse 	int anonport, error;
2564b932371SIan Dowse 
2571b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
2588501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
25959daba27SSam Leffler 
2604b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2614b932371SIan Dowse 		return (EINVAL);
2624b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2634b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2644b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
265b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
2664b932371SIan Dowse 	if (error)
2674b932371SIan Dowse 		return (error);
2684b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2694b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2704b932371SIan Dowse 		inp->inp_lport = 0;
2714b932371SIan Dowse 		return (EAGAIN);
2724b932371SIan Dowse 	}
2734b932371SIan Dowse 	if (anonport)
2744b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2754b932371SIan Dowse 	return (0);
2764b932371SIan Dowse }
2774b932371SIan Dowse 
2784b932371SIan Dowse /*
2794b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2804b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2814b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2824b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2834b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2844b932371SIan Dowse  *
2854b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2864b932371SIan Dowse  */
2874b932371SIan Dowse int
288136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
289136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
2904b932371SIan Dowse {
2918b615593SMarko Zec 	INIT_VNET_INET(inp->inp_vnet);
2924b932371SIan Dowse 	struct socket *so = inp->inp_socket;
29337bd2b30SPeter Wemm 	unsigned short *lastport;
29415bd2b43SDavid Greenman 	struct sockaddr_in *sin;
295c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2964b932371SIan Dowse 	struct in_addr laddr;
297df8bae1dSRodney W. Grimes 	u_short lport = 0;
2984cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
299413628a7SBjoern A. Zeeb 	int error;
3005f311da2SMike Silbersack 	int dorandom;
301df8bae1dSRodney W. Grimes 
3028501a69cSRobert Watson 	/*
30372bed082SRobert Watson 	 * Because no actual state changes occur here, a global write lock on
30472bed082SRobert Watson 	 * the pcbinfo isn't required.
3058501a69cSRobert Watson 	 */
3068501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
30759daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
30859daba27SSam Leffler 
309603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
310df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
3114b932371SIan Dowse 	laddr.s_addr = *laddrp;
3124b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
313df8bae1dSRodney W. Grimes 		return (EINVAL);
314c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
315421d8aa6SBjoern A. Zeeb 		wild = INPLOOKUP_WILDCARD;
316df8bae1dSRodney W. Grimes 	if (nam) {
31757bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
31857bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
319df8bae1dSRodney W. Grimes 			return (EINVAL);
320df8bae1dSRodney W. Grimes #ifdef notdef
321df8bae1dSRodney W. Grimes 		/*
322df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
323df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
324df8bae1dSRodney W. Grimes 		 */
325df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
326df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
327df8bae1dSRodney W. Grimes #endif
328b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
329b89e82ddSJamie Gritton 		if (error)
330b89e82ddSJamie Gritton 			return (error);
3314b932371SIan Dowse 		if (sin->sin_port != *lportp) {
3324b932371SIan Dowse 			/* Don't allow the port to change. */
3334b932371SIan Dowse 			if (*lportp != 0)
3344b932371SIan Dowse 				return (EINVAL);
335df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
3364b932371SIan Dowse 		}
3374b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
338df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
339df8bae1dSRodney W. Grimes 			/*
340df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
341df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
342df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
343df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
344df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
345df8bae1dSRodney W. Grimes 			 */
346df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
347df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
348df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
349df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
35083103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
3514209e01aSAdrian Chadd 			/*
3524209e01aSAdrian Chadd 			 * Is the address a local IP address?
3534209e01aSAdrian Chadd 			 * If INP_NONLOCALOK is set, then the socket may be bound
3548696873dSAdrian Chadd 			 * to any endpoint address, local or not.
3554209e01aSAdrian Chadd 			 */
356be9347e3SAdrian Chadd 			if (
357be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND)
358be9347e3SAdrian Chadd 			    ((inp->inp_flags & INP_NONLOCALOK) == 0) &&
359be9347e3SAdrian Chadd #endif
360be9347e3SAdrian Chadd 			    (ifa_ifwithaddr((struct sockaddr *)sin) == 0))
361df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
362df8bae1dSRodney W. Grimes 		}
3634b932371SIan Dowse 		laddr = sin->sin_addr;
364df8bae1dSRodney W. Grimes 		if (lport) {
365df8bae1dSRodney W. Grimes 			struct inpcb *t;
366ae0e7143SRobert Watson 			struct tcptw *tw;
367ae0e7143SRobert Watson 
368df8bae1dSRodney W. Grimes 			/* GROSS */
369603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
370603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
371acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
37232f9753cSRobert Watson 			    0))
3732469dd60SGarrett Wollman 				return (EACCES);
374835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
37586d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
37632f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
377078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
378413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
379340c35deSJonathan Lemon 	/*
380340c35deSJonathan Lemon 	 * XXX
381340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
382340c35deSJonathan Lemon 	 */
3834cc20ab1SSeigo Tanimura 				if (t &&
3844658dc83SYaroslav Tykhiy 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
3854658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
3864658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
3874cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
38852b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
38952b65dbeSBill Fenner 				     (t->inp_socket->so_options &
39052b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
39186d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
39286d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
3934049a042SGuido van Rooij 					return (EADDRINUSE);
3944049a042SGuido van Rooij 			}
395b89e82ddSJamie Gritton 			error = prison_local_ip4(cred, &sin->sin_addr);
396b89e82ddSJamie Gritton 			if (error)
397b89e82ddSJamie Gritton 				return (error);
398c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
399413628a7SBjoern A. Zeeb 			    lport, wild, cred);
400340c35deSJonathan Lemon 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
401ae0e7143SRobert Watson 				/*
402ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
403ae0e7143SRobert Watson 				 * state recycled, we treat the address as
404ae0e7143SRobert Watson 				 * being in use (for now).  This is better
405ae0e7143SRobert Watson 				 * than a panic, but not desirable.
406ae0e7143SRobert Watson 				 */
407ae0e7143SRobert Watson 				tw = intotw(inp);
408ae0e7143SRobert Watson 				if (tw == NULL ||
409ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
410340c35deSJonathan Lemon 					return (EADDRINUSE);
411ae0e7143SRobert Watson 			} else if (t &&
4124cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
413e3fd5ffdSRobert Watson #ifdef INET6
41433841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
415cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
416cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
417cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
418cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
419cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
420e3fd5ffdSRobert Watson #endif
421df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
422df8bae1dSRodney W. Grimes 			}
423cfa1ca9dSYoshinobu Inoue 		}
424df8bae1dSRodney W. Grimes 	}
4254b932371SIan Dowse 	if (*lportp != 0)
4264b932371SIan Dowse 		lport = *lportp;
42733b3ac06SPeter Wemm 	if (lport == 0) {
4281cf6e4f5SRui Paulo 		u_short first, last, aux;
429174624e0SMike Silbersack 		int count;
43033b3ac06SPeter Wemm 
431b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &laddr);
432b89e82ddSJamie Gritton 		if (error)
433b89e82ddSJamie Gritton 			return (error);
434321a2846SPoul-Henning Kamp 
43533b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
436603724d3SBjoern A. Zeeb 			first = V_ipport_hifirstauto;	/* sysctl */
437603724d3SBjoern A. Zeeb 			last  = V_ipport_hilastauto;
438712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lasthi;
43933b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
440acd3428bSRobert Watson 			error = priv_check_cred(cred,
44132f9753cSRobert Watson 			    PRIV_NETINET_RESERVEDPORT, 0);
442acd3428bSRobert Watson 			if (error)
443a29f300eSGarrett Wollman 				return error;
444603724d3SBjoern A. Zeeb 			first = V_ipport_lowfirstauto;	/* 1023 */
445603724d3SBjoern A. Zeeb 			last  = V_ipport_lowlastauto;	/* 600 */
446712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lastlow;
44733b3ac06SPeter Wemm 		} else {
448603724d3SBjoern A. Zeeb 			first = V_ipport_firstauto;	/* sysctl */
449603724d3SBjoern A. Zeeb 			last  = V_ipport_lastauto;
450712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lastport;
45133b3ac06SPeter Wemm 		}
45233b3ac06SPeter Wemm 		/*
4535f311da2SMike Silbersack 		 * For UDP, use random port allocation as long as the user
4545f311da2SMike Silbersack 		 * allows it.  For TCP (and as of yet unknown) connections,
4555f311da2SMike Silbersack 		 * use random port allocation only if the user allows it AND
45629f2a6ecSMaxim Konovalov 		 * ipport_tick() allows it.
4575f311da2SMike Silbersack 		 */
458603724d3SBjoern A. Zeeb 		if (V_ipport_randomized &&
459603724d3SBjoern A. Zeeb 			(!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
4605f311da2SMike Silbersack 			dorandom = 1;
4615f311da2SMike Silbersack 		else
4625f311da2SMike Silbersack 			dorandom = 0;
463e99971bfSMaxim Konovalov 		/*
464e99971bfSMaxim Konovalov 		 * It makes no sense to do random port allocation if
465e99971bfSMaxim Konovalov 		 * we have the only port available.
466e99971bfSMaxim Konovalov 		 */
467e99971bfSMaxim Konovalov 		if (first == last)
468e99971bfSMaxim Konovalov 			dorandom = 0;
4695f311da2SMike Silbersack 		/* Make sure to not include UDP packets in the count. */
470603724d3SBjoern A. Zeeb 		if (pcbinfo != &V_udbinfo)
471603724d3SBjoern A. Zeeb 			V_ipport_tcpallocs++;
4725f311da2SMike Silbersack 		/*
4737e1bc272SBjoern A. Zeeb 		 * Instead of having two loops further down counting up or down
4747e1bc272SBjoern A. Zeeb 		 * make sure that first is always <= last and go with only one
4757e1bc272SBjoern A. Zeeb 		 * code path implementing all logic.
47633b3ac06SPeter Wemm 		 */
47733b3ac06SPeter Wemm 		if (first > last) {
4781cf6e4f5SRui Paulo 			aux = first;
4791cf6e4f5SRui Paulo 			first = last;
4801cf6e4f5SRui Paulo 			last = aux;
4811cf6e4f5SRui Paulo 		}
482174624e0SMike Silbersack 
4835f311da2SMike Silbersack 		if (dorandom)
4846b2fc10bSMike Silbersack 			*lastport = first +
4856b2fc10bSMike Silbersack 				    (arc4random() % (last - first));
4861cf6e4f5SRui Paulo 
48733b3ac06SPeter Wemm 		count = last - first;
488174624e0SMike Silbersack 
48933b3ac06SPeter Wemm 		do {
4906ac48b74SMike Silbersack 			if (count-- < 0)	/* completely used? */
491550b1518SWes Peters 				return (EADDRNOTAVAIL);
49233b3ac06SPeter Wemm 			++*lastport;
49333b3ac06SPeter Wemm 			if (*lastport < first || *lastport > last)
49433b3ac06SPeter Wemm 				*lastport = first;
49533b3ac06SPeter Wemm 			lport = htons(*lastport);
496078b7042SBjoern A. Zeeb 		} while (in_pcblookup_local(pcbinfo, laddr,
497078b7042SBjoern A. Zeeb 		    lport, wild, cred));
49833b3ac06SPeter Wemm 	}
499b89e82ddSJamie Gritton 	error = prison_local_ip4(cred, &laddr);
500b89e82ddSJamie Gritton 	if (error)
501b89e82ddSJamie Gritton 		return (error);
5024b932371SIan Dowse 	*laddrp = laddr.s_addr;
5034b932371SIan Dowse 	*lportp = lport;
504df8bae1dSRodney W. Grimes 	return (0);
505df8bae1dSRodney W. Grimes }
506df8bae1dSRodney W. Grimes 
507999f1343SGarrett Wollman /*
5085200e00eSIan Dowse  * Connect from a socket to a specified address.
5095200e00eSIan Dowse  * Both address and port must be specified in argument sin.
5105200e00eSIan Dowse  * If don't have a local address for this socket yet,
5115200e00eSIan Dowse  * then pick one.
512999f1343SGarrett Wollman  */
513999f1343SGarrett Wollman int
514136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
515999f1343SGarrett Wollman {
5165200e00eSIan Dowse 	u_short lport, fport;
5175200e00eSIan Dowse 	in_addr_t laddr, faddr;
5185200e00eSIan Dowse 	int anonport, error;
519df8bae1dSRodney W. Grimes 
52027f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
5218501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
52227f74fd0SRobert Watson 
5235200e00eSIan Dowse 	lport = inp->inp_lport;
5245200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
5255200e00eSIan Dowse 	anonport = (lport == 0);
5265200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
527b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
5285200e00eSIan Dowse 	if (error)
5295200e00eSIan Dowse 		return (error);
5305200e00eSIan Dowse 
5315200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
5325200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
5335200e00eSIan Dowse 		inp->inp_lport = lport;
5345200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
5355200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
5365200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
5375200e00eSIan Dowse 			inp->inp_lport = 0;
5385200e00eSIan Dowse 			return (EAGAIN);
5395200e00eSIan Dowse 		}
5405200e00eSIan Dowse 	}
5415200e00eSIan Dowse 
5425200e00eSIan Dowse 	/* Commit the remaining changes. */
5435200e00eSIan Dowse 	inp->inp_lport = lport;
5445200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
5455200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
5465200e00eSIan Dowse 	inp->inp_fport = fport;
5475200e00eSIan Dowse 	in_pcbrehash(inp);
5482cb64cb2SGeorge V. Neville-Neil 
5495200e00eSIan Dowse 	if (anonport)
5505200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
5515200e00eSIan Dowse 	return (0);
5525200e00eSIan Dowse }
5535200e00eSIan Dowse 
5545200e00eSIan Dowse /*
5550895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
5560895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
5570895aec3SBjoern A. Zeeb  */
5580895aec3SBjoern A. Zeeb static int
5590895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
5600895aec3SBjoern A. Zeeb     struct ucred *cred)
5610895aec3SBjoern A. Zeeb {
5620895aec3SBjoern A. Zeeb 	struct in_ifaddr *ia;
5630895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
5640895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
5650895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
5660895aec3SBjoern A. Zeeb 	struct route sro;
5670895aec3SBjoern A. Zeeb 	int error;
5680895aec3SBjoern A. Zeeb 
569413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
5700895aec3SBjoern A. Zeeb 
5710895aec3SBjoern A. Zeeb 	error = 0;
5720895aec3SBjoern A. Zeeb 	ia = NULL;
5730895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
5740895aec3SBjoern A. Zeeb 
5750895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
5760895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
5770895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
5780895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
5790895aec3SBjoern A. Zeeb 
5800895aec3SBjoern A. Zeeb 	/*
5810895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
5820895aec3SBjoern A. Zeeb 	 * else punt.
5830895aec3SBjoern A. Zeeb 	 *
5840895aec3SBjoern A. Zeeb 	 * Find out route to destination.
5850895aec3SBjoern A. Zeeb 	 */
5860895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
5876e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
5880895aec3SBjoern A. Zeeb 
5890895aec3SBjoern A. Zeeb 	/*
5900895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
5910895aec3SBjoern A. Zeeb 	 * the outgoing interface.
5920895aec3SBjoern A. Zeeb 	 *
5930895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
5940895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
5950895aec3SBjoern A. Zeeb 	 * the source address from.
5960895aec3SBjoern A. Zeeb 	 */
5970895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
5980895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
5990895aec3SBjoern A. Zeeb 
6000895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
6010895aec3SBjoern A. Zeeb 		if (ia == NULL)
6020895aec3SBjoern A. Zeeb 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin));
6030895aec3SBjoern A. Zeeb 		if (ia == NULL) {
6040895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
6050895aec3SBjoern A. Zeeb 			goto done;
6060895aec3SBjoern A. Zeeb 		}
6070895aec3SBjoern A. Zeeb 
6080895aec3SBjoern A. Zeeb 		if (cred == NULL || !jailed(cred)) {
6090895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6100895aec3SBjoern A. Zeeb 			goto done;
6110895aec3SBjoern A. Zeeb 		}
6120895aec3SBjoern A. Zeeb 
6130895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
6140895aec3SBjoern A. Zeeb 		ia = NULL;
6150895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
6160895aec3SBjoern A. Zeeb 
6170895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
6180895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
6190895aec3SBjoern A. Zeeb 				continue;
6200895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
621b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
6220895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
6230895aec3SBjoern A. Zeeb 				break;
6240895aec3SBjoern A. Zeeb 			}
6250895aec3SBjoern A. Zeeb 		}
6260895aec3SBjoern A. Zeeb 		if (ia != NULL) {
6270895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6280895aec3SBjoern A. Zeeb 			goto done;
6290895aec3SBjoern A. Zeeb 		}
6300895aec3SBjoern A. Zeeb 
6310895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
632b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
6330895aec3SBjoern A. Zeeb 		goto done;
6340895aec3SBjoern A. Zeeb 	}
6350895aec3SBjoern A. Zeeb 
6360895aec3SBjoern A. Zeeb 	/*
6370895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
6380895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
6390895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
6400895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
6410895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
6420895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
6430895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
6440895aec3SBjoern A. Zeeb 	 */
6450895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
6460895aec3SBjoern A. Zeeb 
6470895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
6480895aec3SBjoern A. Zeeb 		if (cred == NULL || !jailed(cred)) {
6490895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
6500895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6510895aec3SBjoern A. Zeeb 			goto done;
6520895aec3SBjoern A. Zeeb 		}
6530895aec3SBjoern A. Zeeb 
6540895aec3SBjoern A. Zeeb 		/* Jailed. */
6550895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
6560895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
657b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
6580895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
6590895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6600895aec3SBjoern A. Zeeb 			goto done;
6610895aec3SBjoern A. Zeeb 		}
6620895aec3SBjoern A. Zeeb 
6630895aec3SBjoern A. Zeeb 		/*
6640895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
6650895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
6660895aec3SBjoern A. Zeeb 		 */
6670895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &sro.ro_rt->rt_ifp->if_addrhead, ifa_link) {
6680895aec3SBjoern A. Zeeb 
6690895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
6700895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
6710895aec3SBjoern A. Zeeb 				continue;
6720895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
673b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
6740895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
6750895aec3SBjoern A. Zeeb 				break;
6760895aec3SBjoern A. Zeeb 			}
6770895aec3SBjoern A. Zeeb 		}
6780895aec3SBjoern A. Zeeb 		if (ia != NULL) {
6790895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6800895aec3SBjoern A. Zeeb 			goto done;
6810895aec3SBjoern A. Zeeb 		}
6820895aec3SBjoern A. Zeeb 
6830895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
684b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
6850895aec3SBjoern A. Zeeb 		goto done;
6860895aec3SBjoern A. Zeeb 	}
6870895aec3SBjoern A. Zeeb 
6880895aec3SBjoern A. Zeeb 	/*
6890895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
6900895aec3SBjoern A. Zeeb 	 * to ourselves is here.
6910895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
6920895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
6930895aec3SBjoern A. Zeeb 	 * a loopback interface.
6940895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
6950895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
6960895aec3SBjoern A. Zeeb 	 */
6970895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
6980895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
6990895aec3SBjoern A. Zeeb 
7000895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
7010895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
7020895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
7030895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
7040895aec3SBjoern A. Zeeb 
7050895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
7060895aec3SBjoern A. Zeeb 		if (ia == NULL)
7070895aec3SBjoern A. Zeeb 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain)));
7080895aec3SBjoern A. Zeeb 
7090895aec3SBjoern A. Zeeb 		if (cred == NULL || !jailed(cred)) {
71003d8b6fdSBjoern A. Zeeb #if __FreeBSD_version < 800000
71103d8b6fdSBjoern A. Zeeb 			if (ia == NULL)
71203d8b6fdSBjoern A. Zeeb 				ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
71303d8b6fdSBjoern A. Zeeb #endif
7140895aec3SBjoern A. Zeeb 			if (ia == NULL) {
7150895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
7160895aec3SBjoern A. Zeeb 				goto done;
7170895aec3SBjoern A. Zeeb 			}
7180895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7190895aec3SBjoern A. Zeeb 			goto done;
7200895aec3SBjoern A. Zeeb 		}
7210895aec3SBjoern A. Zeeb 
7220895aec3SBjoern A. Zeeb 		/* Jailed. */
7230895aec3SBjoern A. Zeeb 		if (ia != NULL) {
7240895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
7250895aec3SBjoern A. Zeeb 
7260895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
7270895aec3SBjoern A. Zeeb 			ia = NULL;
7280895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
7290895aec3SBjoern A. Zeeb 
7300895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
7310895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
7320895aec3SBjoern A. Zeeb 					continue;
7330895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
734b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
735b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
7360895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
7370895aec3SBjoern A. Zeeb 					break;
7380895aec3SBjoern A. Zeeb 				}
7390895aec3SBjoern A. Zeeb 			}
7400895aec3SBjoern A. Zeeb 			if (ia != NULL) {
7410895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7420895aec3SBjoern A. Zeeb 				goto done;
7430895aec3SBjoern A. Zeeb 			}
7440895aec3SBjoern A. Zeeb 		}
7450895aec3SBjoern A. Zeeb 
7460895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
747b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
7480895aec3SBjoern A. Zeeb 		goto done;
7490895aec3SBjoern A. Zeeb 	}
7500895aec3SBjoern A. Zeeb 
7510895aec3SBjoern A. Zeeb done:
7520895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
7530895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
7540895aec3SBjoern A. Zeeb 	return (error);
7550895aec3SBjoern A. Zeeb }
7560895aec3SBjoern A. Zeeb 
7570895aec3SBjoern A. Zeeb /*
7585200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
7595200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
7605200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
7615200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
7625200e00eSIan Dowse  * the connect.
7635200e00eSIan Dowse  *
7645200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
7655200e00eSIan Dowse  * and port. These are not updated in the error case.
7665200e00eSIan Dowse  *
7675200e00eSIan Dowse  * If the operation fails because the connection already exists,
7685200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
7695200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
7705200e00eSIan Dowse  * is set to NULL.
7715200e00eSIan Dowse  */
7725200e00eSIan Dowse int
773136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
774136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
775136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
7765200e00eSIan Dowse {
7778b615593SMarko Zec 	INIT_VNET_INET(inp->inp_vnet);
7785200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
7795200e00eSIan Dowse 	struct in_ifaddr *ia;
7805200e00eSIan Dowse 	struct inpcb *oinp;
781b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
7825200e00eSIan Dowse 	u_short lport, fport;
7835200e00eSIan Dowse 	int error;
7845200e00eSIan Dowse 
7858501a69cSRobert Watson 	/*
7868501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
7878501a69cSRobert Watson 	 * lock is sufficient.
7888501a69cSRobert Watson 	 */
7898501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
79027f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
79127f74fd0SRobert Watson 
7925200e00eSIan Dowse 	if (oinpp != NULL)
7935200e00eSIan Dowse 		*oinpp = NULL;
79457bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
795df8bae1dSRodney W. Grimes 		return (EINVAL);
796df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
797df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
798df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
799df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
8005200e00eSIan Dowse 	laddr.s_addr = *laddrp;
8015200e00eSIan Dowse 	lport = *lportp;
8025200e00eSIan Dowse 	faddr = sin->sin_addr;
8035200e00eSIan Dowse 	fport = sin->sin_port;
8040895aec3SBjoern A. Zeeb 
805603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
806df8bae1dSRodney W. Grimes 		/*
807df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
808df8bae1dSRodney W. Grimes 		 * use the primary local address.
809df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
810df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
811df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
812df8bae1dSRodney W. Grimes 		 */
813413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
814413628a7SBjoern A. Zeeb 			faddr =
815b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
816b89e82ddSJamie Gritton 			if (cred != NULL &&
817b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
818b89e82ddSJamie Gritton 				return (error);
819413628a7SBjoern A. Zeeb 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
820603724d3SBjoern A. Zeeb 		    (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
8215200e00eSIan Dowse 		    IFF_BROADCAST))
8225200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
823603724d3SBjoern A. Zeeb 			    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
824df8bae1dSRodney W. Grimes 	}
8255200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
8260895aec3SBjoern A. Zeeb 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
8270895aec3SBjoern A. Zeeb 		if (error)
8280895aec3SBjoern A. Zeeb 			return (error);
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes 		/*
831df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
832df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
833df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
834df8bae1dSRodney W. Grimes 		 */
8355200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
836df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
837df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
838df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
839df8bae1dSRodney W. Grimes 
840df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
841df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
842df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
843603724d3SBjoern A. Zeeb 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
844df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
845df8bae1dSRodney W. Grimes 						break;
8468699ea08SBjoern A. Zeeb 				if (ia == NULL)
847df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
8485200e00eSIan Dowse 				laddr = ia->ia_addr.sin_addr;
849999f1343SGarrett Wollman 			}
8500895aec3SBjoern A. Zeeb 		}
8510895aec3SBjoern A. Zeeb 	}
852999f1343SGarrett Wollman 
8535200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
8545200e00eSIan Dowse 	    0, NULL);
8555200e00eSIan Dowse 	if (oinp != NULL) {
8565200e00eSIan Dowse 		if (oinpp != NULL)
8575200e00eSIan Dowse 			*oinpp = oinp;
858df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
859c3229e05SDavid Greenman 	}
8605200e00eSIan Dowse 	if (lport == 0) {
861b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
862b0330ed9SPawel Jakub Dawidek 		    cred);
8635a903f8dSPierre Beyssac 		if (error)
8645a903f8dSPierre Beyssac 			return (error);
8655a903f8dSPierre Beyssac 	}
8665200e00eSIan Dowse 	*laddrp = laddr.s_addr;
8675200e00eSIan Dowse 	*lportp = lport;
8685200e00eSIan Dowse 	*faddrp = faddr.s_addr;
8695200e00eSIan Dowse 	*fportp = fport;
870df8bae1dSRodney W. Grimes 	return (0);
871df8bae1dSRodney W. Grimes }
872df8bae1dSRodney W. Grimes 
87326f9a767SRodney W. Grimes void
874136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
875df8bae1dSRodney W. Grimes {
8766b348152SRobert Watson 
877fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
8788501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
879df8bae1dSRodney W. Grimes 
880df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
881df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
88215bd2b43SDavid Greenman 	in_pcbrehash(inp);
883df8bae1dSRodney W. Grimes }
884df8bae1dSRodney W. Grimes 
8854c7c478dSRobert Watson /*
88628696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
887c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
88828696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
88928696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
8904c7c478dSRobert Watson  */
89126f9a767SRodney W. Grimes void
892136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
893df8bae1dSRodney W. Grimes {
8944c7c478dSRobert Watson 
895a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
896c0a211c5SRobert Watson 
8974c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
8984c7c478dSRobert Watson 	inp->inp_socket = NULL;
8994c7c478dSRobert Watson }
9004c7c478dSRobert Watson 
901c0a211c5SRobert Watson /*
90228696211SRobert Watson  * in_pcbfree_internal() frees an inpcb that has been detached from its
90328696211SRobert Watson  * socket, and whose reference count has reached 0.  It will also remove the
90428696211SRobert Watson  * inpcb from any global lists it might remain on.
905c0a211c5SRobert Watson  */
90628696211SRobert Watson static void
90728696211SRobert Watson in_pcbfree_internal(struct inpcb *inp)
9084c7c478dSRobert Watson {
9093d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
910df8bae1dSRodney W. Grimes 
911a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
91228696211SRobert Watson 	KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__));
9138501a69cSRobert Watson 
914fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
9158501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
91659daba27SSam Leffler 
917b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
9186aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
9196974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
920b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
9213d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
922c3229e05SDavid Greenman 	in_pcbremlists(inp);
9236aee2fc5SBjoern A. Zeeb #ifdef INET6
9246aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
9256aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
9266aee2fc5SBjoern A. Zeeb 		ip6_freemoptions(inp->in6p_moptions);
9276aee2fc5SBjoern A. Zeeb 	}
9286aee2fc5SBjoern A. Zeeb #endif
929df8bae1dSRodney W. Grimes 	if (inp->inp_options)
930df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
93171498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
93271498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
933cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
93486d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
935d915b280SStephan Uphoff 
936a557af22SRobert Watson #ifdef MAC
93730d239bcSRobert Watson 	mac_inpcb_destroy(inp);
938a557af22SRobert Watson #endif
9398501a69cSRobert Watson 	INP_WUNLOCK(inp);
94069c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
941df8bae1dSRodney W. Grimes }
942df8bae1dSRodney W. Grimes 
94310702a28SRobert Watson /*
94428696211SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
94528696211SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
94628696211SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
94728696211SRobert Watson  * but where the inpcb lock is already held.
94828696211SRobert Watson  *
94928696211SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
95028696211SRobert Watson  * connection's state may change, so the caller should be careful to
95128696211SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
95228696211SRobert Watson  * using in_pcbrele().
95328696211SRobert Watson  */
95428696211SRobert Watson void
95528696211SRobert Watson in_pcbref(struct inpcb *inp)
95628696211SRobert Watson {
95728696211SRobert Watson 
95828696211SRobert Watson 	INP_WLOCK_ASSERT(inp);
95928696211SRobert Watson 
96028696211SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
96128696211SRobert Watson 
96228696211SRobert Watson 	inp->inp_refcount++;
96328696211SRobert Watson }
96428696211SRobert Watson 
96528696211SRobert Watson /*
96628696211SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
96728696211SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
96828696211SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
96928696211SRobert Watson  * valid, we return with the inpcb lock held.
97028696211SRobert Watson  */
97128696211SRobert Watson int
97228696211SRobert Watson in_pcbrele(struct inpcb *inp)
97328696211SRobert Watson {
97428696211SRobert Watson #ifdef INVARIANTS
97528696211SRobert Watson 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
97628696211SRobert Watson #endif
97728696211SRobert Watson 
97828696211SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
97928696211SRobert Watson 
98028696211SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
98128696211SRobert Watson 	INP_WLOCK_ASSERT(inp);
98228696211SRobert Watson 
98328696211SRobert Watson 	inp->inp_refcount--;
98428696211SRobert Watson 	if (inp->inp_refcount > 0)
98528696211SRobert Watson 		return (0);
98628696211SRobert Watson 	in_pcbfree_internal(inp);
98728696211SRobert Watson 	return (1);
98828696211SRobert Watson }
98928696211SRobert Watson 
99028696211SRobert Watson /*
99128696211SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
99228696211SRobert Watson  * reference count, which should occur only after the inpcb has been detached
99328696211SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
99428696211SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
99528696211SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.
99628696211SRobert Watson  */
99728696211SRobert Watson void
99828696211SRobert Watson in_pcbfree(struct inpcb *inp)
99928696211SRobert Watson {
100028696211SRobert Watson #ifdef INVARIANTS
100128696211SRobert Watson 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
100228696211SRobert Watson #endif
100328696211SRobert Watson 
100428696211SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL",
100528696211SRobert Watson 	    __func__));
100628696211SRobert Watson 
100728696211SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
100828696211SRobert Watson 	INP_WLOCK_ASSERT(inp);
100928696211SRobert Watson 
101028696211SRobert Watson 	if (!in_pcbrele(inp))
101128696211SRobert Watson 		INP_WUNLOCK(inp);
101228696211SRobert Watson }
101328696211SRobert Watson 
101428696211SRobert Watson /*
1015c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1016c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1017c0a211c5SRobert Watson  *
1018c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1019c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1020c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1021c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1022c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1023c0a211c5SRobert Watson  * in_pcbdetach().
1024c0a211c5SRobert Watson  *
1025c0a211c5SRobert Watson  * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
1026c0a211c5SRobert Watson  * lists, but can lead to confusing netstat output, as open sockets with
1027c0a211c5SRobert Watson  * closed TCP connections will no longer appear to have their bound port
1028c0a211c5SRobert Watson  * number.  An explicit flag would be better, as it would allow us to leave
1029c0a211c5SRobert Watson  * the port number intact after the connection is dropped.
1030c0a211c5SRobert Watson  *
1031c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1032c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
103310702a28SRobert Watson  */
103410702a28SRobert Watson void
103510702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
103610702a28SRobert Watson {
103710702a28SRobert Watson 
10387c5a8ab2SMarcel Moolenaar 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
10398501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
104010702a28SRobert Watson 
104110702a28SRobert Watson 	inp->inp_vflag |= INP_DROPPED;
104210702a28SRobert Watson 	if (inp->inp_lport) {
104310702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
104410702a28SRobert Watson 
104510702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
104610702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
104710702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
104810702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
104910702a28SRobert Watson 			free(phd, M_PCB);
105010702a28SRobert Watson 		}
105110702a28SRobert Watson 		inp->inp_lport = 0;
105210702a28SRobert Watson 	}
105310702a28SRobert Watson }
105410702a28SRobert Watson 
105554d642bbSRobert Watson /*
105654d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
105754d642bbSRobert Watson  */
105826ef6ac4SDon Lewis struct sockaddr *
1059136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
106026ef6ac4SDon Lewis {
106126ef6ac4SDon Lewis 	struct sockaddr_in *sin;
106226ef6ac4SDon Lewis 
10631ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1064a163d034SWarner Losh 		M_WAITOK | M_ZERO);
106526ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
106626ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
106726ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
106826ef6ac4SDon Lewis 	sin->sin_port = port;
106926ef6ac4SDon Lewis 
107026ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
107126ef6ac4SDon Lewis }
107226ef6ac4SDon Lewis 
1073117bcae7SGarrett Wollman int
107454d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1075df8bae1dSRodney W. Grimes {
1076136d4f1cSRobert Watson 	struct inpcb *inp;
107726ef6ac4SDon Lewis 	struct in_addr addr;
107826ef6ac4SDon Lewis 	in_port_t port;
107942fa505bSDavid Greenman 
1080fdc984f7STor Egge 	inp = sotoinpcb(so);
108154d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
10826466b28aSRobert Watson 
1083a69042a5SRobert Watson 	INP_RLOCK(inp);
108426ef6ac4SDon Lewis 	port = inp->inp_lport;
108526ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1086a69042a5SRobert Watson 	INP_RUNLOCK(inp);
108742fa505bSDavid Greenman 
108826ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1089117bcae7SGarrett Wollman 	return 0;
1090df8bae1dSRodney W. Grimes }
1091df8bae1dSRodney W. Grimes 
1092117bcae7SGarrett Wollman int
109354d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1094df8bae1dSRodney W. Grimes {
1095136d4f1cSRobert Watson 	struct inpcb *inp;
109626ef6ac4SDon Lewis 	struct in_addr addr;
109726ef6ac4SDon Lewis 	in_port_t port;
109842fa505bSDavid Greenman 
1099fdc984f7STor Egge 	inp = sotoinpcb(so);
110054d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
11016466b28aSRobert Watson 
1102a69042a5SRobert Watson 	INP_RLOCK(inp);
110326ef6ac4SDon Lewis 	port = inp->inp_fport;
110426ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1105a69042a5SRobert Watson 	INP_RUNLOCK(inp);
110642fa505bSDavid Greenman 
110726ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1108117bcae7SGarrett Wollman 	return 0;
1109df8bae1dSRodney W. Grimes }
1110df8bae1dSRodney W. Grimes 
111126f9a767SRodney W. Grimes void
1112136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1113136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1114d1c54148SJesper Skriver {
1115f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1116d1c54148SJesper Skriver 
11173dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1118f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
11198501a69cSRobert Watson 		INP_WLOCK(inp);
1120d1c54148SJesper Skriver #ifdef INET6
1121f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
11228501a69cSRobert Watson 			INP_WUNLOCK(inp);
1123d1c54148SJesper Skriver 			continue;
1124f76fcf6dSJeffrey Hsu 		}
1125d1c54148SJesper Skriver #endif
1126d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1127f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
11288501a69cSRobert Watson 			INP_WUNLOCK(inp);
1129d1c54148SJesper Skriver 			continue;
1130d1c54148SJesper Skriver 		}
11313dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
11328501a69cSRobert Watson 			INP_WUNLOCK(inp);
1133f76fcf6dSJeffrey Hsu 	}
11343dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1135d1c54148SJesper Skriver }
1136d1c54148SJesper Skriver 
1137e43cc4aeSHajimu UMEMOTO void
1138136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1139e43cc4aeSHajimu UMEMOTO {
1140e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1141e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1142e43cc4aeSHajimu UMEMOTO 	int i, gap;
1143e43cc4aeSHajimu UMEMOTO 
1144f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
1145712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
11468501a69cSRobert Watson 		INP_WLOCK(inp);
1147e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1148e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1149e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1150e43cc4aeSHajimu UMEMOTO 			/*
1151e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1152e43cc4aeSHajimu UMEMOTO 			 * detached.
1153e43cc4aeSHajimu UMEMOTO 			 */
1154e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1155e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1156e43cc4aeSHajimu UMEMOTO 
1157e43cc4aeSHajimu UMEMOTO 			/*
1158e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1159e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1160e43cc4aeSHajimu UMEMOTO 			 */
1161e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1162e43cc4aeSHajimu UMEMOTO 			    i++) {
1163e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1164e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1165e43cc4aeSHajimu UMEMOTO 					gap++;
1166e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1167e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1168e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1169e43cc4aeSHajimu UMEMOTO 			}
1170e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1171e43cc4aeSHajimu UMEMOTO 		}
11728501a69cSRobert Watson 		INP_WUNLOCK(inp);
1173e43cc4aeSHajimu UMEMOTO 	}
11743cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
1175e43cc4aeSHajimu UMEMOTO }
1176e43cc4aeSHajimu UMEMOTO 
1177df8bae1dSRodney W. Grimes /*
1178c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
1179c3229e05SDavid Greenman  */
1180d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1181df8bae1dSRodney W. Grimes struct inpcb *
1182136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1183078b7042SBjoern A. Zeeb     u_short lport, int wild_okay, struct ucred *cred)
1184df8bae1dSRodney W. Grimes {
1185136d4f1cSRobert Watson 	struct inpcb *inp;
1186d5e8a67eSHajimu UMEMOTO #ifdef INET6
1187d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1188d5e8a67eSHajimu UMEMOTO #else
1189d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1190d5e8a67eSHajimu UMEMOTO #endif
1191d5e8a67eSHajimu UMEMOTO 	int wildcard;
11927bc4aca7SDavid Greenman 
11938501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
11941b73ca0bSSam Leffler 
1195c3229e05SDavid Greenman 	if (!wild_okay) {
1196c3229e05SDavid Greenman 		struct inpcbhead *head;
1197c3229e05SDavid Greenman 		/*
1198c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1199c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1200c3229e05SDavid Greenman 		 */
1201712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1202712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1203fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1204cfa1ca9dSYoshinobu Inoue #ifdef INET6
1205413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1206369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1207cfa1ca9dSYoshinobu Inoue 				continue;
1208cfa1ca9dSYoshinobu Inoue #endif
1209c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1210c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1211c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1212c3229e05SDavid Greenman 				/*
1213413628a7SBjoern A. Zeeb 				 * Found?
1214c3229e05SDavid Greenman 				 */
1215413628a7SBjoern A. Zeeb 				if (cred == NULL ||
1216413628a7SBjoern A. Zeeb 				    inp->inp_cred->cr_prison == cred->cr_prison)
1217c3229e05SDavid Greenman 					return (inp);
1218df8bae1dSRodney W. Grimes 			}
1219c3229e05SDavid Greenman 		}
1220c3229e05SDavid Greenman 		/*
1221c3229e05SDavid Greenman 		 * Not found.
1222c3229e05SDavid Greenman 		 */
1223c3229e05SDavid Greenman 		return (NULL);
1224c3229e05SDavid Greenman 	} else {
1225c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1226c3229e05SDavid Greenman 		struct inpcbport *phd;
1227c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1228c3229e05SDavid Greenman 		/*
1229c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1230c3229e05SDavid Greenman 		 *
1231c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1232c3229e05SDavid Greenman 		 * port hash list.
1233c3229e05SDavid Greenman 		 */
1234712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1235712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1236fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1237c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1238c3229e05SDavid Greenman 				break;
1239c3229e05SDavid Greenman 		}
1240c3229e05SDavid Greenman 		if (phd != NULL) {
1241c3229e05SDavid Greenman 			/*
1242c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1243c3229e05SDavid Greenman 			 * fit.
1244c3229e05SDavid Greenman 			 */
124537d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1246c3229e05SDavid Greenman 				wildcard = 0;
1247413628a7SBjoern A. Zeeb 				if (cred != NULL &&
1248413628a7SBjoern A. Zeeb 				    inp->inp_cred->cr_prison != cred->cr_prison)
1249413628a7SBjoern A. Zeeb 					continue;
1250cfa1ca9dSYoshinobu Inoue #ifdef INET6
1251413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1252369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1253cfa1ca9dSYoshinobu Inoue 					continue;
1254d5e8a67eSHajimu UMEMOTO 				/*
1255d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1256d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1257d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1258d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1259d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1260d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1261d5e8a67eSHajimu UMEMOTO 				 *
1262d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1263d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1264d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1265d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1266d5e8a67eSHajimu UMEMOTO 				 */
1267d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1268d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1269cfa1ca9dSYoshinobu Inoue #endif
1270c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1271c3229e05SDavid Greenman 					wildcard++;
127215bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
127315bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
127415bd2b43SDavid Greenman 						wildcard++;
127515bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
127615bd2b43SDavid Greenman 						continue;
127715bd2b43SDavid Greenman 				} else {
127815bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
127915bd2b43SDavid Greenman 						wildcard++;
128015bd2b43SDavid Greenman 				}
1281df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1282df8bae1dSRodney W. Grimes 					match = inp;
1283df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1284413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1285df8bae1dSRodney W. Grimes 						break;
1286df8bae1dSRodney W. Grimes 				}
1287df8bae1dSRodney W. Grimes 			}
12883dbdc25cSDavid Greenman 		}
1289df8bae1dSRodney W. Grimes 		return (match);
1290df8bae1dSRodney W. Grimes 	}
1291c3229e05SDavid Greenman }
1292d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
129315bd2b43SDavid Greenman 
129415bd2b43SDavid Greenman /*
129515bd2b43SDavid Greenman  * Lookup PCB in hash list.
129615bd2b43SDavid Greenman  */
129715bd2b43SDavid Greenman struct inpcb *
1298136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1299136d4f1cSRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1300136d4f1cSRobert Watson     struct ifnet *ifp)
130115bd2b43SDavid Greenman {
130215bd2b43SDavid Greenman 	struct inpcbhead *head;
1303413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
130415bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
130515bd2b43SDavid Greenman 
13068501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
1307602cc7f1SRobert Watson 
130815bd2b43SDavid Greenman 	/*
130915bd2b43SDavid Greenman 	 * First look for an exact match.
131015bd2b43SDavid Greenman 	 */
1311413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1312712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1313712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1314fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1315cfa1ca9dSYoshinobu Inoue #ifdef INET6
1316413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1317369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1318cfa1ca9dSYoshinobu Inoue 			continue;
1319cfa1ca9dSYoshinobu Inoue #endif
13206d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1321ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1322ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1323413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1324413628a7SBjoern A. Zeeb 			/*
1325413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1326413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1327413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1328413628a7SBjoern A. Zeeb 			 */
1329413628a7SBjoern A. Zeeb 			if (jailed(inp->inp_cred))
1330c3229e05SDavid Greenman 				return (inp);
1331413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1332413628a7SBjoern A. Zeeb 				tmpinp = inp;
1333c3229e05SDavid Greenman 		}
1334413628a7SBjoern A. Zeeb 	}
1335413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1336413628a7SBjoern A. Zeeb 		return (tmpinp);
1337e3fd5ffdSRobert Watson 
1338e3fd5ffdSRobert Watson 	/*
1339e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1340e3fd5ffdSRobert Watson 	 */
1341413628a7SBjoern A. Zeeb 	if (wildcard == INPLOOKUP_WILDCARD) {
1342413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1343e3fd5ffdSRobert Watson #ifdef INET6
1344cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1345e3fd5ffdSRobert Watson #endif
1346413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1347413628a7SBjoern A. Zeeb 		int injail;
1348413628a7SBjoern A. Zeeb 
1349413628a7SBjoern A. Zeeb 		/*
1350413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1351413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1352413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1353413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1354413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1355413628a7SBjoern A. Zeeb 		 */
13566d6a026bSDavid Greenman 
1357712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1358712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1359fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1360cfa1ca9dSYoshinobu Inoue #ifdef INET6
1361413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1362369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1363cfa1ca9dSYoshinobu Inoue 				continue;
1364cfa1ca9dSYoshinobu Inoue #endif
1365413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1366413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1367413628a7SBjoern A. Zeeb 				continue;
1368413628a7SBjoern A. Zeeb 
1369413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1370cfa1ca9dSYoshinobu Inoue 			if (ifp && ifp->if_type == IFT_FAITH &&
1371cfa1ca9dSYoshinobu Inoue 			    (inp->inp_flags & INP_FAITH) == 0)
1372cfa1ca9dSYoshinobu Inoue 				continue;
1373413628a7SBjoern A. Zeeb 
1374413628a7SBjoern A. Zeeb 			injail = jailed(inp->inp_cred);
1375413628a7SBjoern A. Zeeb 			if (injail) {
1376b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1377b89e82ddSJamie Gritton 				    &laddr) != 0)
1378413628a7SBjoern A. Zeeb 					continue;
1379413628a7SBjoern A. Zeeb 			} else {
1380413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1381413628a7SBjoern A. Zeeb 					continue;
1382413628a7SBjoern A. Zeeb 			}
1383413628a7SBjoern A. Zeeb 
1384413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1385413628a7SBjoern A. Zeeb 				if (injail)
1386c3229e05SDavid Greenman 					return (inp);
1387413628a7SBjoern A. Zeeb 				else
1388413628a7SBjoern A. Zeeb 					local_exact = inp;
1389413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1390e3fd5ffdSRobert Watson #ifdef INET6
1391413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
13925cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1393cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1394cfa1ca9dSYoshinobu Inoue 				else
1395413628a7SBjoern A. Zeeb #endif /* INET6 */
1396413628a7SBjoern A. Zeeb 					if (injail)
1397413628a7SBjoern A. Zeeb 						jail_wild = inp;
1398413628a7SBjoern A. Zeeb 					else
13996d6a026bSDavid Greenman 						local_wild = inp;
14006d6a026bSDavid Greenman 			}
1401413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1402413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1403413628a7SBjoern A. Zeeb 			return (jail_wild);
1404413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1405413628a7SBjoern A. Zeeb 			return (local_exact);
1406413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1407c3229e05SDavid Greenman 			return (local_wild);
1408413628a7SBjoern A. Zeeb #ifdef INET6
1409413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1410413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
1411413628a7SBjoern A. Zeeb #endif /* defined(INET6) */
1412413628a7SBjoern A. Zeeb 	} /* if (wildcard == INPLOOKUP_WILDCARD) */
1413413628a7SBjoern A. Zeeb 
14146d6a026bSDavid Greenman 	return (NULL);
141515bd2b43SDavid Greenman }
141615bd2b43SDavid Greenman 
14177bc4aca7SDavid Greenman /*
1418c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
14197bc4aca7SDavid Greenman  */
1420c3229e05SDavid Greenman int
1421136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
142215bd2b43SDavid Greenman {
1423c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1424c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1425c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1426c3229e05SDavid Greenman 	struct inpcbport *phd;
1427cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
142815bd2b43SDavid Greenman 
142959daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
14308501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1431602cc7f1SRobert Watson 
1432cfa1ca9dSYoshinobu Inoue #ifdef INET6
1433cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1434cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1435cfa1ca9dSYoshinobu Inoue 	else
1436cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1437cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1438cfa1ca9dSYoshinobu Inoue 
1439712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1440712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
144115bd2b43SDavid Greenman 
1442712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
1443712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1444c3229e05SDavid Greenman 
1445c3229e05SDavid Greenman 	/*
1446c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1447c3229e05SDavid Greenman 	 */
1448fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1449c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1450c3229e05SDavid Greenman 			break;
1451c3229e05SDavid Greenman 	}
1452c3229e05SDavid Greenman 	/*
1453c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1454c3229e05SDavid Greenman 	 */
1455c3229e05SDavid Greenman 	if (phd == NULL) {
14561ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1457c3229e05SDavid Greenman 		if (phd == NULL) {
1458c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1459c3229e05SDavid Greenman 		}
1460c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1461c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1462c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1463c3229e05SDavid Greenman 	}
1464c3229e05SDavid Greenman 	inp->inp_phd = phd;
1465c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1466c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1467c3229e05SDavid Greenman 	return (0);
146815bd2b43SDavid Greenman }
146915bd2b43SDavid Greenman 
1470c3229e05SDavid Greenman /*
1471c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1472c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1473c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1474c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1475c3229e05SDavid Greenman  */
147615bd2b43SDavid Greenman void
1477136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp)
147815bd2b43SDavid Greenman {
147959daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
148015bd2b43SDavid Greenman 	struct inpcbhead *head;
1481cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
148215bd2b43SDavid Greenman 
148359daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
14848501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1485602cc7f1SRobert Watson 
1486cfa1ca9dSYoshinobu Inoue #ifdef INET6
1487cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1488cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1489cfa1ca9dSYoshinobu Inoue 	else
1490cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1491cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1492cfa1ca9dSYoshinobu Inoue 
1493712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1494712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
149515bd2b43SDavid Greenman 
1496c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
149715bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1498c3229e05SDavid Greenman }
1499c3229e05SDavid Greenman 
1500c3229e05SDavid Greenman /*
1501c3229e05SDavid Greenman  * Remove PCB from various lists.
1502c3229e05SDavid Greenman  */
150376429de4SYoshinobu Inoue void
1504136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1505c3229e05SDavid Greenman {
150659daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
150759daba27SSam Leffler 
150859daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
15098501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
151059daba27SSam Leffler 
151159daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1512c3229e05SDavid Greenman 	if (inp->inp_lport) {
1513c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1514c3229e05SDavid Greenman 
1515c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1516c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1517fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1518c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1519c3229e05SDavid Greenman 			free(phd, M_PCB);
1520c3229e05SDavid Greenman 		}
1521c3229e05SDavid Greenman 	}
1522c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
152359daba27SSam Leffler 	pcbinfo->ipi_count--;
152415bd2b43SDavid Greenman }
152575c13541SPoul-Henning Kamp 
1526a557af22SRobert Watson /*
1527a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1528a557af22SRobert Watson  * label change into the in_pcb for the socket.
1529a557af22SRobert Watson  */
1530a557af22SRobert Watson void
1531136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1532a557af22SRobert Watson {
1533a557af22SRobert Watson #ifdef MAC
1534a557af22SRobert Watson 	struct inpcb *inp;
1535a557af22SRobert Watson 
15364c7c478dSRobert Watson 	inp = sotoinpcb(so);
15374c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1538602cc7f1SRobert Watson 
15398501a69cSRobert Watson 	INP_WLOCK(inp);
1540310e7cebSRobert Watson 	SOCK_LOCK(so);
1541a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1542310e7cebSRobert Watson 	SOCK_UNLOCK(so);
15438501a69cSRobert Watson 	INP_WUNLOCK(inp);
1544a557af22SRobert Watson #endif
1545a557af22SRobert Watson }
15465f311da2SMike Silbersack 
15475f311da2SMike Silbersack /*
1548ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
1549ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
1550ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
1551ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
1552ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
15535f311da2SMike Silbersack  */
15545f311da2SMike Silbersack void
1555136d4f1cSRobert Watson ipport_tick(void *xtp)
15565f311da2SMike Silbersack {
15578b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1558ad3a630fSRobert Watson 
15598b615593SMarko Zec 	VNET_LIST_RLOCK();
15608b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
15618b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
15628b615593SMarko Zec 		INIT_VNET_INET(vnet_iter);
15638b615593SMarko Zec 		if (V_ipport_tcpallocs <=
15648b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
1565603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
1566603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
1567ad3a630fSRobert Watson 		} else
1568603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
1569603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
15708b615593SMarko Zec 		CURVNET_RESTORE();
15718b615593SMarko Zec 	}
15728b615593SMarko Zec 	VNET_LIST_RUNLOCK();
15735f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
15745f311da2SMike Silbersack }
1575497057eeSRobert Watson 
15763d585327SKip Macy void
15773d585327SKip Macy inp_wlock(struct inpcb *inp)
15783d585327SKip Macy {
15793d585327SKip Macy 
15808501a69cSRobert Watson 	INP_WLOCK(inp);
15813d585327SKip Macy }
15823d585327SKip Macy 
15833d585327SKip Macy void
15843d585327SKip Macy inp_wunlock(struct inpcb *inp)
15853d585327SKip Macy {
15863d585327SKip Macy 
15878501a69cSRobert Watson 	INP_WUNLOCK(inp);
15883d585327SKip Macy }
15893d585327SKip Macy 
15903d585327SKip Macy void
15913d585327SKip Macy inp_rlock(struct inpcb *inp)
15923d585327SKip Macy {
15933d585327SKip Macy 
1594a69042a5SRobert Watson 	INP_RLOCK(inp);
15953d585327SKip Macy }
15963d585327SKip Macy 
15973d585327SKip Macy void
15983d585327SKip Macy inp_runlock(struct inpcb *inp)
15993d585327SKip Macy {
16003d585327SKip Macy 
1601a69042a5SRobert Watson 	INP_RUNLOCK(inp);
16023d585327SKip Macy }
16033d585327SKip Macy 
16043d585327SKip Macy #ifdef INVARIANTS
16053d585327SKip Macy void
1606e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
16073d585327SKip Macy {
16083d585327SKip Macy 
16098501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
16103d585327SKip Macy }
16113d585327SKip Macy 
16123d585327SKip Macy void
1613e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
16143d585327SKip Macy {
16153d585327SKip Macy 
16163d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
16173d585327SKip Macy }
16183d585327SKip Macy #endif
16193d585327SKip Macy 
16209378e437SKip Macy void
16219378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
16229378e437SKip Macy {
16238b615593SMarko Zec 	INIT_VNET_INET(curvnet);
16249378e437SKip Macy 	struct inpcb *inp;
16259378e437SKip Macy 
1626603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
162797021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
16289378e437SKip Macy 		INP_WLOCK(inp);
16299378e437SKip Macy 		func(inp, arg);
16309378e437SKip Macy 		INP_WUNLOCK(inp);
16319378e437SKip Macy 	}
1632603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
16339378e437SKip Macy }
16349378e437SKip Macy 
16359378e437SKip Macy struct socket *
16369378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
16379378e437SKip Macy {
16389378e437SKip Macy 
16399378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
16409378e437SKip Macy 	return (inp->inp_socket);
16419378e437SKip Macy }
16429378e437SKip Macy 
16439378e437SKip Macy struct tcpcb *
16449378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
16459378e437SKip Macy {
16469378e437SKip Macy 
16479378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
16489378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
16499378e437SKip Macy }
16509378e437SKip Macy 
16519378e437SKip Macy int
16529378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
16539378e437SKip Macy {
16549378e437SKip Macy 
16559378e437SKip Macy 	return (inp->inp_ip_tos);
16569378e437SKip Macy }
16579378e437SKip Macy 
16589378e437SKip Macy void
16599378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
16609378e437SKip Macy {
16619378e437SKip Macy 
16629378e437SKip Macy 	inp->inp_ip_tos = val;
16639378e437SKip Macy }
16649378e437SKip Macy 
16659378e437SKip Macy void
1666df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
16679d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
16689378e437SKip Macy {
16699378e437SKip Macy 
16709d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
1671df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
1672df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
16739378e437SKip Macy 	*lp = inp->inp_lport;
16749378e437SKip Macy 	*fp = inp->inp_fport;
16759378e437SKip Macy }
16769378e437SKip Macy 
1677dd0e6c38SKip Macy struct inpcb *
1678dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
1679dd0e6c38SKip Macy {
1680dd0e6c38SKip Macy 
1681dd0e6c38SKip Macy 	return (sotoinpcb(so));
1682dd0e6c38SKip Macy }
1683dd0e6c38SKip Macy 
1684dd0e6c38SKip Macy struct tcpcb *
1685dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
1686dd0e6c38SKip Macy {
1687dd0e6c38SKip Macy 
1688dd0e6c38SKip Macy 	return (sototcpcb(so));
1689dd0e6c38SKip Macy }
1690dd0e6c38SKip Macy 
1691497057eeSRobert Watson #ifdef DDB
1692497057eeSRobert Watson static void
1693497057eeSRobert Watson db_print_indent(int indent)
1694497057eeSRobert Watson {
1695497057eeSRobert Watson 	int i;
1696497057eeSRobert Watson 
1697497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1698497057eeSRobert Watson 		db_printf(" ");
1699497057eeSRobert Watson }
1700497057eeSRobert Watson 
1701497057eeSRobert Watson static void
1702497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1703497057eeSRobert Watson {
1704497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
1705497057eeSRobert Watson 
1706497057eeSRobert Watson 	db_print_indent(indent);
1707497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
1708497057eeSRobert Watson 
1709497057eeSRobert Watson 	indent += 2;
1710497057eeSRobert Watson 
171103dc38a4SRobert Watson #ifdef INET6
1712dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
1713497057eeSRobert Watson 		/* IPv6. */
1714497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1715497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1716497057eeSRobert Watson 	} else {
171703dc38a4SRobert Watson #endif
1718497057eeSRobert Watson 		/* IPv4. */
1719497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1720497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
172103dc38a4SRobert Watson #ifdef INET6
1722497057eeSRobert Watson 	}
172303dc38a4SRobert Watson #endif
1724497057eeSRobert Watson 	db_print_indent(indent);
1725497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1726497057eeSRobert Watson 	    ntohs(inc->inc_lport));
1727497057eeSRobert Watson 	db_print_indent(indent);
1728497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1729497057eeSRobert Watson 	    ntohs(inc->inc_fport));
1730497057eeSRobert Watson }
1731497057eeSRobert Watson 
1732497057eeSRobert Watson static void
1733497057eeSRobert Watson db_print_inpflags(int inp_flags)
1734497057eeSRobert Watson {
1735497057eeSRobert Watson 	int comma;
1736497057eeSRobert Watson 
1737497057eeSRobert Watson 	comma = 0;
1738497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
1739497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1740497057eeSRobert Watson 		comma = 1;
1741497057eeSRobert Watson 	}
1742497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
1743497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1744497057eeSRobert Watson 		comma = 1;
1745497057eeSRobert Watson 	}
1746497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
1747497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1748497057eeSRobert Watson 		comma = 1;
1749497057eeSRobert Watson 	}
1750497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
1751497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
1752497057eeSRobert Watson 		comma = 1;
1753497057eeSRobert Watson 	}
1754497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
1755497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1756497057eeSRobert Watson 		comma = 1;
1757497057eeSRobert Watson 	}
1758497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
1759497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
1760497057eeSRobert Watson 		comma = 1;
1761497057eeSRobert Watson 	}
1762497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
1763497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
1764497057eeSRobert Watson 		comma = 1;
1765497057eeSRobert Watson 	}
1766497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
1767497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
1768497057eeSRobert Watson 		comma = 1;
1769497057eeSRobert Watson 	}
1770497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
1771497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
1772497057eeSRobert Watson 		comma = 1;
1773497057eeSRobert Watson 	}
1774497057eeSRobert Watson 	if (inp_flags & INP_FAITH) {
1775497057eeSRobert Watson 		db_printf("%sINP_FAITH", comma ? ", " : "");
1776497057eeSRobert Watson 		comma = 1;
1777497057eeSRobert Watson 	}
1778497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
1779497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
1780497057eeSRobert Watson 		comma = 1;
1781497057eeSRobert Watson 	}
1782497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
1783497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1784497057eeSRobert Watson 		comma = 1;
1785497057eeSRobert Watson 	}
1786497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
1787497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1788497057eeSRobert Watson 		comma = 1;
1789497057eeSRobert Watson 	}
1790497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
1791497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1792497057eeSRobert Watson 		comma = 1;
1793497057eeSRobert Watson 	}
1794497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
1795497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1796497057eeSRobert Watson 		comma = 1;
1797497057eeSRobert Watson 	}
1798497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
1799497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1800497057eeSRobert Watson 		comma = 1;
1801497057eeSRobert Watson 	}
1802497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
1803497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1804497057eeSRobert Watson 		comma = 1;
1805497057eeSRobert Watson 	}
1806497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
1807497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1808497057eeSRobert Watson 		comma = 1;
1809497057eeSRobert Watson 	}
1810497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
1811497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1812497057eeSRobert Watson 		comma = 1;
1813497057eeSRobert Watson 	}
1814497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
1815497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1816497057eeSRobert Watson 		comma = 1;
1817497057eeSRobert Watson 	}
1818497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
1819497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1820497057eeSRobert Watson 		comma = 1;
1821497057eeSRobert Watson 	}
1822497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
1823497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1824497057eeSRobert Watson 		comma = 1;
1825497057eeSRobert Watson 	}
1826497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
1827497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
1828497057eeSRobert Watson 		comma = 1;
1829497057eeSRobert Watson 	}
1830497057eeSRobert Watson }
1831497057eeSRobert Watson 
1832497057eeSRobert Watson static void
1833497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
1834497057eeSRobert Watson {
1835497057eeSRobert Watson 	int comma;
1836497057eeSRobert Watson 
1837497057eeSRobert Watson 	comma = 0;
1838497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
1839497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
1840497057eeSRobert Watson 		comma  = 1;
1841497057eeSRobert Watson 	}
1842497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
1843497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
1844497057eeSRobert Watson 		comma  = 1;
1845497057eeSRobert Watson 	}
1846497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
1847497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1848497057eeSRobert Watson 		comma  = 1;
1849497057eeSRobert Watson 	}
1850497057eeSRobert Watson 	if (inp_vflag & INP_TIMEWAIT) {
1851497057eeSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1852497057eeSRobert Watson 		comma  = 1;
1853497057eeSRobert Watson 	}
1854497057eeSRobert Watson 	if (inp_vflag & INP_ONESBCAST) {
1855497057eeSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1856497057eeSRobert Watson 		comma  = 1;
1857497057eeSRobert Watson 	}
1858497057eeSRobert Watson 	if (inp_vflag & INP_DROPPED) {
1859497057eeSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
1860497057eeSRobert Watson 		comma  = 1;
1861497057eeSRobert Watson 	}
1862497057eeSRobert Watson 	if (inp_vflag & INP_SOCKREF) {
1863497057eeSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
1864497057eeSRobert Watson 		comma  = 1;
1865497057eeSRobert Watson 	}
1866497057eeSRobert Watson }
1867497057eeSRobert Watson 
1868497057eeSRobert Watson void
1869497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1870497057eeSRobert Watson {
1871497057eeSRobert Watson 
1872497057eeSRobert Watson 	db_print_indent(indent);
1873497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
1874497057eeSRobert Watson 
1875497057eeSRobert Watson 	indent += 2;
1876497057eeSRobert Watson 
1877497057eeSRobert Watson 	db_print_indent(indent);
1878497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1879497057eeSRobert Watson 
1880497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1881497057eeSRobert Watson 
1882497057eeSRobert Watson 	db_print_indent(indent);
1883497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1884497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1885497057eeSRobert Watson 
1886497057eeSRobert Watson 	db_print_indent(indent);
1887497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
1888497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
1889497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
1890497057eeSRobert Watson 	db_printf(")\n");
1891497057eeSRobert Watson 
1892497057eeSRobert Watson 	db_print_indent(indent);
1893497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1894497057eeSRobert Watson 	    inp->inp_vflag);
1895497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
1896497057eeSRobert Watson 	db_printf(")\n");
1897497057eeSRobert Watson 
1898497057eeSRobert Watson 	db_print_indent(indent);
1899497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1900497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1901497057eeSRobert Watson 
1902497057eeSRobert Watson 	db_print_indent(indent);
1903497057eeSRobert Watson #ifdef INET6
1904497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
1905497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
1906497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
1907497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
1908497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1909497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1910497057eeSRobert Watson 		    inp->in6p_hops);
1911497057eeSRobert Watson 	} else
1912497057eeSRobert Watson #endif
1913497057eeSRobert Watson 	{
1914497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1915497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1916497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
1917497057eeSRobert Watson 	}
1918497057eeSRobert Watson 
1919497057eeSRobert Watson 	db_print_indent(indent);
1920497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1921497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
1922497057eeSRobert Watson }
1923497057eeSRobert Watson 
1924497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1925497057eeSRobert Watson {
1926497057eeSRobert Watson 	struct inpcb *inp;
1927497057eeSRobert Watson 
1928497057eeSRobert Watson 	if (!have_addr) {
1929497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
1930497057eeSRobert Watson 		return;
1931497057eeSRobert Watson 	}
1932497057eeSRobert Watson 	inp = (struct inpcb *)addr;
1933497057eeSRobert Watson 
1934497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
1935497057eeSRobert Watson }
1936497057eeSRobert Watson #endif
1937