xref: /freebsd/sys/netinet/in_pcb.c (revision 592bcae8022f9f8d7772d1f20287a821a7fee287)
1c398230bSWarner Losh /*-
22469dd60SGarrett Wollman  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3497057eeSRobert Watson  *	The Regents of the University of California.
4111d57a6SRobert Watson  * Copyright (c) 2007-2009 Robert N. M. Watson
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"
386a800098SYoshinobu Inoue #include "opt_ipsec.h"
39cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
40cfa1ca9dSYoshinobu Inoue 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
49acd3428bSRobert Watson #include <sys/priv.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
5175c13541SPoul-Henning Kamp #include <sys/jail.h>
52101f9fc8SPeter Wemm #include <sys/kernel.h>
53101f9fc8SPeter Wemm #include <sys/sysctl.h>
548781d8e9SBruce Evans 
55497057eeSRobert Watson #ifdef DDB
56497057eeSRobert Watson #include <ddb/ddb.h>
57497057eeSRobert Watson #endif
58497057eeSRobert Watson 
5969c2d429SJeff Roberson #include <vm/uma.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <net/if.h>
62cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
63df8bae1dSRodney W. Grimes #include <net/route.h>
64530c0060SRobert Watson #include <net/vnet.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <netinet/in.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
68df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
70340c35deSJonathan Lemon #include <netinet/tcp_var.h>
715f311da2SMike Silbersack #include <netinet/udp.h>
725f311da2SMike Silbersack #include <netinet/udp_var.h>
73cfa1ca9dSYoshinobu Inoue #ifdef INET6
74cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
75cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
76cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
77cfa1ca9dSYoshinobu Inoue 
78df8bae1dSRodney W. Grimes 
79b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
80b9234fafSSam Leffler #include <netipsec/ipsec.h>
81b9234fafSSam Leffler #include <netipsec/key.h>
82b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
83b9234fafSSam Leffler 
84aed55708SRobert Watson #include <security/mac/mac_framework.h>
85aed55708SRobert Watson 
86101f9fc8SPeter Wemm /*
87101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
88101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
89101f9fc8SPeter Wemm  */
90eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
91eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
92eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
93eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
94eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
95eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
96101f9fc8SPeter Wemm 
97b0d22693SCrist J. Clark /*
98b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
99b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
100b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
101b0d22693SCrist J. Clark  */
102eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
103eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
104b0d22693SCrist J. Clark 
1055f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
106eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
107eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
108eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
109eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
110eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
111eddfbb76SRobert Watson static VNET_DEFINE(int, ipport_tcplastcount);
112eddfbb76SRobert Watson 
1131e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1146ac48b74SMike Silbersack 
115bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
116bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
117bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
118bbd42ad0SPeter Wemm 
1196d888973SRobert Watson static void	in_pcbremlists(struct inpcb *inp);
1206d888973SRobert Watson 
121bbd42ad0SPeter Wemm static int
12282d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
123bbd42ad0SPeter Wemm {
12430a4ab08SBruce Evans 	int error;
12530a4ab08SBruce Evans 
126eddfbb76SRobert Watson #ifdef VIMAGE
127eddfbb76SRobert Watson 	error = vnet_sysctl_handle_int(oidp, arg1, arg2, req);
128eddfbb76SRobert Watson #else
129f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
130eddfbb76SRobert Watson #endif
13130a4ab08SBruce Evans 	if (error == 0) {
132603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
133603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
134603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
135603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
136603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
137603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
138bbd42ad0SPeter Wemm 	}
13930a4ab08SBruce Evans 	return (error);
140bbd42ad0SPeter Wemm }
141bbd42ad0SPeter Wemm 
142bbd42ad0SPeter Wemm #undef RANGECHK
143bbd42ad0SPeter Wemm 
14433b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
14533b3ac06SPeter Wemm 
146eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
147eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
1488b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
149eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
150eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
1518b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
152eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
153eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
1548b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
155eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
156eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
1578b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
158eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
159eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
1608b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
161eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
162eddfbb76SRobert Watson 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
1638b615593SMarko Zec 	&sysctl_net_ipport_check, "I", "");
164eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
165eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
166eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
167eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
168eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
169eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
170eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
171eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
1726ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
173eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
174eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
1758b615593SMarko Zec 	"Minimum time to keep sequental port "
1766ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
1770312fbe9SPoul-Henning Kamp 
178c3229e05SDavid Greenman /*
179c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
180c3229e05SDavid Greenman  *
181de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
182de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
183de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
184c3229e05SDavid Greenman  */
185c3229e05SDavid Greenman 
186c3229e05SDavid Greenman /*
187c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
188d915b280SStephan Uphoff  * On success return with the PCB locked.
189c3229e05SDavid Greenman  */
190df8bae1dSRodney W. Grimes int
191d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
192df8bae1dSRodney W. Grimes {
193136d4f1cSRobert Watson 	struct inpcb *inp;
19413cf67f3SHajimu UMEMOTO 	int error;
195a557af22SRobert Watson 
19659daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
197a557af22SRobert Watson 	error = 0;
198d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
199df8bae1dSRodney W. Grimes 	if (inp == NULL)
200df8bae1dSRodney W. Grimes 		return (ENOBUFS);
201d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
20215bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
203df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
20486d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
2058b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
206a557af22SRobert Watson #ifdef MAC
20730d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
208a557af22SRobert Watson 	if (error != 0)
209a557af22SRobert Watson 		goto out;
21030d239bcSRobert Watson 	mac_inpcb_create(so, inp);
211a557af22SRobert Watson #endif
212b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
21313cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
2140bffde27SRobert Watson 	if (error != 0) {
2150bffde27SRobert Watson #ifdef MAC
2160bffde27SRobert Watson 		mac_inpcb_destroy(inp);
2170bffde27SRobert Watson #endif
218a557af22SRobert Watson 		goto out;
2190bffde27SRobert Watson 	}
220b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
221e3fd5ffdSRobert Watson #ifdef INET6
222340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
223340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
224603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
22533841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
226340c35deSJonathan Lemon 	}
22775daea93SPaul Saab #endif
228712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
2293d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
230df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
23133841545SHajimu UMEMOTO #ifdef INET6
232603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
23333841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
23433841545SHajimu UMEMOTO #endif
2358501a69cSRobert Watson 	INP_WLOCK(inp);
236d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
23728696211SRobert Watson 	inp->inp_refcount = 1;	/* Reference from the inpcbinfo */
238b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
239a557af22SRobert Watson out:
24086d02c5cSBjoern A. Zeeb 	if (error != 0) {
24186d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
242a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
24386d02c5cSBjoern A. Zeeb 	}
244a557af22SRobert Watson #endif
245a557af22SRobert Watson 	return (error);
246df8bae1dSRodney W. Grimes }
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes int
249136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
250df8bae1dSRodney W. Grimes {
2514b932371SIan Dowse 	int anonport, error;
2524b932371SIan Dowse 
2531b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
2548501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
25559daba27SSam Leffler 
2564b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2574b932371SIan Dowse 		return (EINVAL);
2584b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2594b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2604b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
261b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
2624b932371SIan Dowse 	if (error)
2634b932371SIan Dowse 		return (error);
2644b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2654b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2664b932371SIan Dowse 		inp->inp_lport = 0;
2674b932371SIan Dowse 		return (EAGAIN);
2684b932371SIan Dowse 	}
2694b932371SIan Dowse 	if (anonport)
2704b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2714b932371SIan Dowse 	return (0);
2724b932371SIan Dowse }
2734b932371SIan Dowse 
2744b932371SIan Dowse /*
2754b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2764b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2774b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2784b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2794b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2804b932371SIan Dowse  *
2814b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2824b932371SIan Dowse  */
2834b932371SIan Dowse int
284136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
285136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
2864b932371SIan Dowse {
2874b932371SIan Dowse 	struct socket *so = inp->inp_socket;
28837bd2b30SPeter Wemm 	unsigned short *lastport;
28915bd2b43SDavid Greenman 	struct sockaddr_in *sin;
290c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2914b932371SIan Dowse 	struct in_addr laddr;
292df8bae1dSRodney W. Grimes 	u_short lport = 0;
2934cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
294413628a7SBjoern A. Zeeb 	int error;
2955f311da2SMike Silbersack 	int dorandom;
296df8bae1dSRodney W. Grimes 
2978501a69cSRobert Watson 	/*
29872bed082SRobert Watson 	 * Because no actual state changes occur here, a global write lock on
29972bed082SRobert Watson 	 * the pcbinfo isn't required.
3008501a69cSRobert Watson 	 */
3018501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
30259daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
30359daba27SSam Leffler 
304603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
305df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
3064b932371SIan Dowse 	laddr.s_addr = *laddrp;
3074b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
308df8bae1dSRodney W. Grimes 		return (EINVAL);
309c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
310421d8aa6SBjoern A. Zeeb 		wild = INPLOOKUP_WILDCARD;
3117c2f3cb9SJamie Gritton 	if (nam == NULL) {
3127c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
3137c2f3cb9SJamie Gritton 			return (error);
3147c2f3cb9SJamie Gritton 	} else {
31557bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
31657bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
317df8bae1dSRodney W. Grimes 			return (EINVAL);
318df8bae1dSRodney W. Grimes #ifdef notdef
319df8bae1dSRodney W. Grimes 		/*
320df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
321df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
322df8bae1dSRodney W. Grimes 		 */
323df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
324df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
325df8bae1dSRodney W. Grimes #endif
326b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
327b89e82ddSJamie Gritton 		if (error)
328b89e82ddSJamie Gritton 			return (error);
3294b932371SIan Dowse 		if (sin->sin_port != *lportp) {
3304b932371SIan Dowse 			/* Don't allow the port to change. */
3314b932371SIan Dowse 			if (*lportp != 0)
3324b932371SIan Dowse 				return (EINVAL);
333df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
3344b932371SIan Dowse 		}
3354b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
336df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
337df8bae1dSRodney W. Grimes 			/*
338df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
339df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
340df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
341df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
342df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
343df8bae1dSRodney W. Grimes 			 */
344df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
345df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
346df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
347df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
34883103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
3494209e01aSAdrian Chadd 			/*
3504209e01aSAdrian Chadd 			 * Is the address a local IP address?
351f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
3528696873dSAdrian Chadd 			 * to any endpoint address, local or not.
3534209e01aSAdrian Chadd 			 */
354f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
3558896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
356df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
357df8bae1dSRodney W. Grimes 		}
3584b932371SIan Dowse 		laddr = sin->sin_addr;
359df8bae1dSRodney W. Grimes 		if (lport) {
360df8bae1dSRodney W. Grimes 			struct inpcb *t;
361ae0e7143SRobert Watson 			struct tcptw *tw;
362ae0e7143SRobert Watson 
363df8bae1dSRodney W. Grimes 			/* GROSS */
364603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
365603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
366acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
36732f9753cSRobert Watson 			    0))
3682469dd60SGarrett Wollman 				return (EACCES);
369835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
37086d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
37132f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
372078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
373413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
374340c35deSJonathan Lemon 	/*
375340c35deSJonathan Lemon 	 * XXX
376340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
377340c35deSJonathan Lemon 	 */
3784cc20ab1SSeigo Tanimura 				if (t &&
379ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
3804658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
3814658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
3824cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
38352b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
38452b65dbeSBill Fenner 				     (t->inp_socket->so_options &
38552b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
38686d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
38786d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
3884049a042SGuido van Rooij 					return (EADDRINUSE);
3894049a042SGuido van Rooij 			}
390c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
391413628a7SBjoern A. Zeeb 			    lport, wild, cred);
392ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
393ae0e7143SRobert Watson 				/*
394ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
395ae0e7143SRobert Watson 				 * state recycled, we treat the address as
396ae0e7143SRobert Watson 				 * being in use (for now).  This is better
397ae0e7143SRobert Watson 				 * than a panic, but not desirable.
398ae0e7143SRobert Watson 				 */
399ae0e7143SRobert Watson 				tw = intotw(inp);
400ae0e7143SRobert Watson 				if (tw == NULL ||
401ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
402340c35deSJonathan Lemon 					return (EADDRINUSE);
403ae0e7143SRobert Watson 			} else if (t &&
4044cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
405e3fd5ffdSRobert Watson #ifdef INET6
40633841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
407cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
408cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
409cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
410cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
411cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
412e3fd5ffdSRobert Watson #endif
413df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
414df8bae1dSRodney W. Grimes 			}
415cfa1ca9dSYoshinobu Inoue 		}
416df8bae1dSRodney W. Grimes 	}
4174b932371SIan Dowse 	if (*lportp != 0)
4184b932371SIan Dowse 		lport = *lportp;
41933b3ac06SPeter Wemm 	if (lport == 0) {
4201cf6e4f5SRui Paulo 		u_short first, last, aux;
421174624e0SMike Silbersack 		int count;
42233b3ac06SPeter Wemm 
42333b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
424603724d3SBjoern A. Zeeb 			first = V_ipport_hifirstauto;	/* sysctl */
425603724d3SBjoern A. Zeeb 			last  = V_ipport_hilastauto;
426712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lasthi;
42733b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
428acd3428bSRobert Watson 			error = priv_check_cred(cred,
42932f9753cSRobert Watson 			    PRIV_NETINET_RESERVEDPORT, 0);
430acd3428bSRobert Watson 			if (error)
431a29f300eSGarrett Wollman 				return error;
432603724d3SBjoern A. Zeeb 			first = V_ipport_lowfirstauto;	/* 1023 */
433603724d3SBjoern A. Zeeb 			last  = V_ipport_lowlastauto;	/* 600 */
434712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lastlow;
43533b3ac06SPeter Wemm 		} else {
436603724d3SBjoern A. Zeeb 			first = V_ipport_firstauto;	/* sysctl */
437603724d3SBjoern A. Zeeb 			last  = V_ipport_lastauto;
438712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lastport;
43933b3ac06SPeter Wemm 		}
44033b3ac06SPeter Wemm 		/*
4415f311da2SMike Silbersack 		 * For UDP, use random port allocation as long as the user
4425f311da2SMike Silbersack 		 * allows it.  For TCP (and as of yet unknown) connections,
4435f311da2SMike Silbersack 		 * use random port allocation only if the user allows it AND
44429f2a6ecSMaxim Konovalov 		 * ipport_tick() allows it.
4455f311da2SMike Silbersack 		 */
446603724d3SBjoern A. Zeeb 		if (V_ipport_randomized &&
447603724d3SBjoern A. Zeeb 			(!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
4485f311da2SMike Silbersack 			dorandom = 1;
4495f311da2SMike Silbersack 		else
4505f311da2SMike Silbersack 			dorandom = 0;
451e99971bfSMaxim Konovalov 		/*
452e99971bfSMaxim Konovalov 		 * It makes no sense to do random port allocation if
453e99971bfSMaxim Konovalov 		 * we have the only port available.
454e99971bfSMaxim Konovalov 		 */
455e99971bfSMaxim Konovalov 		if (first == last)
456e99971bfSMaxim Konovalov 			dorandom = 0;
4575f311da2SMike Silbersack 		/* Make sure to not include UDP packets in the count. */
458603724d3SBjoern A. Zeeb 		if (pcbinfo != &V_udbinfo)
459603724d3SBjoern A. Zeeb 			V_ipport_tcpallocs++;
4605f311da2SMike Silbersack 		/*
4617e1bc272SBjoern A. Zeeb 		 * Instead of having two loops further down counting up or down
4627e1bc272SBjoern A. Zeeb 		 * make sure that first is always <= last and go with only one
4637e1bc272SBjoern A. Zeeb 		 * code path implementing all logic.
46433b3ac06SPeter Wemm 		 */
46533b3ac06SPeter Wemm 		if (first > last) {
4661cf6e4f5SRui Paulo 			aux = first;
4671cf6e4f5SRui Paulo 			first = last;
4681cf6e4f5SRui Paulo 			last = aux;
4691cf6e4f5SRui Paulo 		}
470174624e0SMike Silbersack 
4715f311da2SMike Silbersack 		if (dorandom)
4726b2fc10bSMike Silbersack 			*lastport = first +
4736b2fc10bSMike Silbersack 				    (arc4random() % (last - first));
4741cf6e4f5SRui Paulo 
47533b3ac06SPeter Wemm 		count = last - first;
476174624e0SMike Silbersack 
47733b3ac06SPeter Wemm 		do {
4786ac48b74SMike Silbersack 			if (count-- < 0)	/* completely used? */
479550b1518SWes Peters 				return (EADDRNOTAVAIL);
48033b3ac06SPeter Wemm 			++*lastport;
48133b3ac06SPeter Wemm 			if (*lastport < first || *lastport > last)
48233b3ac06SPeter Wemm 				*lastport = first;
48333b3ac06SPeter Wemm 			lport = htons(*lastport);
484078b7042SBjoern A. Zeeb 		} while (in_pcblookup_local(pcbinfo, laddr,
485078b7042SBjoern A. Zeeb 		    lport, wild, cred));
48633b3ac06SPeter Wemm 	}
4874b932371SIan Dowse 	*laddrp = laddr.s_addr;
4884b932371SIan Dowse 	*lportp = lport;
489df8bae1dSRodney W. Grimes 	return (0);
490df8bae1dSRodney W. Grimes }
491df8bae1dSRodney W. Grimes 
492999f1343SGarrett Wollman /*
4935200e00eSIan Dowse  * Connect from a socket to a specified address.
4945200e00eSIan Dowse  * Both address and port must be specified in argument sin.
4955200e00eSIan Dowse  * If don't have a local address for this socket yet,
4965200e00eSIan Dowse  * then pick one.
497999f1343SGarrett Wollman  */
498999f1343SGarrett Wollman int
499136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
500999f1343SGarrett Wollman {
5015200e00eSIan Dowse 	u_short lport, fport;
5025200e00eSIan Dowse 	in_addr_t laddr, faddr;
5035200e00eSIan Dowse 	int anonport, error;
504df8bae1dSRodney W. Grimes 
50527f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
5068501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
50727f74fd0SRobert Watson 
5085200e00eSIan Dowse 	lport = inp->inp_lport;
5095200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
5105200e00eSIan Dowse 	anonport = (lport == 0);
5115200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
512b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
5135200e00eSIan Dowse 	if (error)
5145200e00eSIan Dowse 		return (error);
5155200e00eSIan Dowse 
5165200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
5175200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
5185200e00eSIan Dowse 		inp->inp_lport = lport;
5195200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
5205200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
5215200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
5225200e00eSIan Dowse 			inp->inp_lport = 0;
5235200e00eSIan Dowse 			return (EAGAIN);
5245200e00eSIan Dowse 		}
5255200e00eSIan Dowse 	}
5265200e00eSIan Dowse 
5275200e00eSIan Dowse 	/* Commit the remaining changes. */
5285200e00eSIan Dowse 	inp->inp_lport = lport;
5295200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
5305200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
5315200e00eSIan Dowse 	inp->inp_fport = fport;
5325200e00eSIan Dowse 	in_pcbrehash(inp);
5332cb64cb2SGeorge V. Neville-Neil 
5345200e00eSIan Dowse 	if (anonport)
5355200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
5365200e00eSIan Dowse 	return (0);
5375200e00eSIan Dowse }
5385200e00eSIan Dowse 
5395200e00eSIan Dowse /*
5400895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
5410895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
5420895aec3SBjoern A. Zeeb  */
5430895aec3SBjoern A. Zeeb static int
5440895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
5450895aec3SBjoern A. Zeeb     struct ucred *cred)
5460895aec3SBjoern A. Zeeb {
5470895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
5480895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
5490895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
5500895aec3SBjoern A. Zeeb 	struct route sro;
5510895aec3SBjoern A. Zeeb 	int error;
5520895aec3SBjoern A. Zeeb 
553413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
5540895aec3SBjoern A. Zeeb 
555592bcae8SBjoern A. Zeeb 	/*
556592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
557592bcae8SBjoern A. Zeeb 	 * if requested.
558592bcae8SBjoern A. Zeeb 	 */
559592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
560592bcae8SBjoern A. Zeeb 		return (0);
561592bcae8SBjoern A. Zeeb 
5620895aec3SBjoern A. Zeeb 	error = 0;
5630895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
5640895aec3SBjoern A. Zeeb 
5650895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
5660895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
5670895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
5680895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
5690895aec3SBjoern A. Zeeb 
5700895aec3SBjoern A. Zeeb 	/*
5710895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
5720895aec3SBjoern A. Zeeb 	 * else punt.
5730895aec3SBjoern A. Zeeb 	 *
5740895aec3SBjoern A. Zeeb 	 * Find out route to destination.
5750895aec3SBjoern A. Zeeb 	 */
5760895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
5776e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
5780895aec3SBjoern A. Zeeb 
5790895aec3SBjoern A. Zeeb 	/*
5800895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
5810895aec3SBjoern A. Zeeb 	 * the outgoing interface.
5820895aec3SBjoern A. Zeeb 	 *
5830895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
5840895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
5850895aec3SBjoern A. Zeeb 	 * the source address from.
5860895aec3SBjoern A. Zeeb 	 */
5870895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
5888c0fec80SRobert Watson 		struct in_ifaddr *ia;
5890895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
5900895aec3SBjoern A. Zeeb 
5910895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
5920895aec3SBjoern A. Zeeb 		if (ia == NULL)
5930895aec3SBjoern A. Zeeb 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin));
5940895aec3SBjoern A. Zeeb 		if (ia == NULL) {
5950895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
5960895aec3SBjoern A. Zeeb 			goto done;
5970895aec3SBjoern A. Zeeb 		}
5980895aec3SBjoern A. Zeeb 
5990304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
6000895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6018c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
6020895aec3SBjoern A. Zeeb 			goto done;
6030895aec3SBjoern A. Zeeb 		}
6040895aec3SBjoern A. Zeeb 
6050895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
6068c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
6070895aec3SBjoern A. Zeeb 		ia = NULL;
6089317b04eSRobert Watson 		IF_ADDR_LOCK(ifp);
6090895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
6100895aec3SBjoern A. Zeeb 
6110895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
6120895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
6130895aec3SBjoern A. Zeeb 				continue;
6140895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
615b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
6160895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
6170895aec3SBjoern A. Zeeb 				break;
6180895aec3SBjoern A. Zeeb 			}
6190895aec3SBjoern A. Zeeb 		}
6200895aec3SBjoern A. Zeeb 		if (ia != NULL) {
6210895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6229317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
6230895aec3SBjoern A. Zeeb 			goto done;
6240895aec3SBjoern A. Zeeb 		}
6259317b04eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
6260895aec3SBjoern A. Zeeb 
6270895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
628b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
6290895aec3SBjoern A. Zeeb 		goto done;
6300895aec3SBjoern A. Zeeb 	}
6310895aec3SBjoern A. Zeeb 
6320895aec3SBjoern A. Zeeb 	/*
6330895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
6340895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
6350895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
6360895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
6370895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
6380895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
6390895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
6400895aec3SBjoern A. Zeeb 	 */
6410895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
6428c0fec80SRobert Watson 		struct in_ifaddr *ia;
6439317b04eSRobert Watson 		struct ifnet *ifp;
6440895aec3SBjoern A. Zeeb 
6450895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
6460304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
6470895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
6480895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6490895aec3SBjoern A. Zeeb 			goto done;
6500895aec3SBjoern A. Zeeb 		}
6510895aec3SBjoern A. Zeeb 
6520895aec3SBjoern A. Zeeb 		/* Jailed. */
6530895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
6540895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
655b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
6560895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
6570895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
6580895aec3SBjoern A. Zeeb 			goto done;
6590895aec3SBjoern A. Zeeb 		}
6600895aec3SBjoern A. Zeeb 
6610895aec3SBjoern A. Zeeb 		/*
6620895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
6630895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
6640895aec3SBjoern A. Zeeb 		 */
6658c0fec80SRobert Watson 		ia = NULL;
6669317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
6679317b04eSRobert Watson 		IF_ADDR_LOCK(ifp);
6689317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
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;
6809317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
6810895aec3SBjoern A. Zeeb 			goto done;
6820895aec3SBjoern A. Zeeb 		}
6839317b04eSRobert Watson 		IF_ADDR_UNLOCK(ifp);
6840895aec3SBjoern A. Zeeb 
6850895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
686b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
6870895aec3SBjoern A. Zeeb 		goto done;
6880895aec3SBjoern A. Zeeb 	}
6890895aec3SBjoern A. Zeeb 
6900895aec3SBjoern A. Zeeb 	/*
6910895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
6920895aec3SBjoern A. Zeeb 	 * to ourselves is here.
6930895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
6940895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
6950895aec3SBjoern A. Zeeb 	 * a loopback interface.
6960895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
6970895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
6980895aec3SBjoern A. Zeeb 	 */
6990895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
7000895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
7018c0fec80SRobert Watson 		struct in_ifaddr *ia;
7020895aec3SBjoern A. Zeeb 
7030895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
7040895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
7050895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
7060895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
7070895aec3SBjoern A. Zeeb 
7080895aec3SBjoern A. Zeeb 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
7090895aec3SBjoern A. Zeeb 		if (ia == NULL)
7100895aec3SBjoern A. Zeeb 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain)));
711f0bb05fcSQing Li 		if (ia == NULL)
712f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
7130895aec3SBjoern A. Zeeb 
7140304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
7150895aec3SBjoern A. Zeeb 			if (ia == NULL) {
7160895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
7170895aec3SBjoern A. Zeeb 				goto done;
7180895aec3SBjoern A. Zeeb 			}
7190895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7208c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
7210895aec3SBjoern A. Zeeb 			goto done;
7220895aec3SBjoern A. Zeeb 		}
7230895aec3SBjoern A. Zeeb 
7240895aec3SBjoern A. Zeeb 		/* Jailed. */
7250895aec3SBjoern A. Zeeb 		if (ia != NULL) {
7260895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
7270895aec3SBjoern A. Zeeb 
7280895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
7298c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
7300895aec3SBjoern A. Zeeb 			ia = NULL;
7319317b04eSRobert Watson 			IF_ADDR_LOCK(ifp);
7320895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
7330895aec3SBjoern A. Zeeb 
7340895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
7350895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
7360895aec3SBjoern A. Zeeb 					continue;
7370895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
738b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
739b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
7400895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
7410895aec3SBjoern A. Zeeb 					break;
7420895aec3SBjoern A. Zeeb 				}
7430895aec3SBjoern A. Zeeb 			}
7440895aec3SBjoern A. Zeeb 			if (ia != NULL) {
7450895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
7469317b04eSRobert Watson 				IF_ADDR_UNLOCK(ifp);
7470895aec3SBjoern A. Zeeb 				goto done;
7480895aec3SBjoern A. Zeeb 			}
7499317b04eSRobert Watson 			IF_ADDR_UNLOCK(ifp);
7500895aec3SBjoern A. Zeeb 		}
7510895aec3SBjoern A. Zeeb 
7520895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
753b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
7540895aec3SBjoern A. Zeeb 		goto done;
7550895aec3SBjoern A. Zeeb 	}
7560895aec3SBjoern A. Zeeb 
7570895aec3SBjoern A. Zeeb done:
7580895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
7590895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
7600895aec3SBjoern A. Zeeb 	return (error);
7610895aec3SBjoern A. Zeeb }
7620895aec3SBjoern A. Zeeb 
7630895aec3SBjoern A. Zeeb /*
7645200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
7655200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
7665200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
7675200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
7685200e00eSIan Dowse  * the connect.
7695200e00eSIan Dowse  *
7705200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
7715200e00eSIan Dowse  * and port. These are not updated in the error case.
7725200e00eSIan Dowse  *
7735200e00eSIan Dowse  * If the operation fails because the connection already exists,
7745200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
7755200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
7765200e00eSIan Dowse  * is set to NULL.
7775200e00eSIan Dowse  */
7785200e00eSIan Dowse int
779136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
780136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
781136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
7825200e00eSIan Dowse {
7835200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
7845200e00eSIan Dowse 	struct in_ifaddr *ia;
7855200e00eSIan Dowse 	struct inpcb *oinp;
786b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
7875200e00eSIan Dowse 	u_short lport, fport;
7885200e00eSIan Dowse 	int error;
7895200e00eSIan Dowse 
7908501a69cSRobert Watson 	/*
7918501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
7928501a69cSRobert Watson 	 * lock is sufficient.
7938501a69cSRobert Watson 	 */
7948501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
79527f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
79627f74fd0SRobert Watson 
7975200e00eSIan Dowse 	if (oinpp != NULL)
7985200e00eSIan Dowse 		*oinpp = NULL;
79957bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
800df8bae1dSRodney W. Grimes 		return (EINVAL);
801df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
802df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
803df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
804df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
8055200e00eSIan Dowse 	laddr.s_addr = *laddrp;
8065200e00eSIan Dowse 	lport = *lportp;
8075200e00eSIan Dowse 	faddr = sin->sin_addr;
8085200e00eSIan Dowse 	fport = sin->sin_port;
8090895aec3SBjoern A. Zeeb 
810603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
811df8bae1dSRodney W. Grimes 		/*
812df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
813df8bae1dSRodney W. Grimes 		 * use the primary local address.
814df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
815df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
816df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
817df8bae1dSRodney W. Grimes 		 */
818413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
8192d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
820413628a7SBjoern A. Zeeb 			faddr =
821b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
8222d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
823b89e82ddSJamie Gritton 			if (cred != NULL &&
824b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
825b89e82ddSJamie Gritton 				return (error);
8262d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
8272d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
8282d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
8292d9cfabaSRobert Watson 			    IFF_BROADCAST)
8305200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
831603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
8322d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
8332d9cfabaSRobert Watson 		}
834df8bae1dSRodney W. Grimes 	}
8355200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
8360895aec3SBjoern A. Zeeb 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
8370895aec3SBjoern A. Zeeb 		if (error)
8380895aec3SBjoern A. Zeeb 			return (error);
839df8bae1dSRodney W. Grimes 
840df8bae1dSRodney W. Grimes 		/*
841df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
842df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
843df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
844df8bae1dSRodney W. Grimes 		 */
8455200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
846df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
847df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
848df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
849df8bae1dSRodney W. Grimes 
850df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
851df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
852df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
8532d9cfabaSRobert Watson 				IN_IFADDR_RLOCK();
854603724d3SBjoern A. Zeeb 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
855df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
856df8bae1dSRodney W. Grimes 						break;
8572d9cfabaSRobert Watson 				if (ia == NULL) {
8582d9cfabaSRobert Watson 					IN_IFADDR_RUNLOCK();
859df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
8602d9cfabaSRobert Watson 				}
8615200e00eSIan Dowse 				laddr = ia->ia_addr.sin_addr;
8622d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
863999f1343SGarrett Wollman 			}
8640895aec3SBjoern A. Zeeb 		}
8650895aec3SBjoern A. Zeeb 	}
866999f1343SGarrett Wollman 
8675200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
8685200e00eSIan Dowse 	    0, NULL);
8695200e00eSIan Dowse 	if (oinp != NULL) {
8705200e00eSIan Dowse 		if (oinpp != NULL)
8715200e00eSIan Dowse 			*oinpp = oinp;
872df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
873c3229e05SDavid Greenman 	}
8745200e00eSIan Dowse 	if (lport == 0) {
875b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
876b0330ed9SPawel Jakub Dawidek 		    cred);
8775a903f8dSPierre Beyssac 		if (error)
8785a903f8dSPierre Beyssac 			return (error);
8795a903f8dSPierre Beyssac 	}
8805200e00eSIan Dowse 	*laddrp = laddr.s_addr;
8815200e00eSIan Dowse 	*lportp = lport;
8825200e00eSIan Dowse 	*faddrp = faddr.s_addr;
8835200e00eSIan Dowse 	*fportp = fport;
884df8bae1dSRodney W. Grimes 	return (0);
885df8bae1dSRodney W. Grimes }
886df8bae1dSRodney W. Grimes 
88726f9a767SRodney W. Grimes void
888136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
889df8bae1dSRodney W. Grimes {
8906b348152SRobert Watson 
891fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
8928501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
895df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
89615bd2b43SDavid Greenman 	in_pcbrehash(inp);
897df8bae1dSRodney W. Grimes }
898df8bae1dSRodney W. Grimes 
8994c7c478dSRobert Watson /*
90028696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
901c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
90228696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
90328696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
9044c7c478dSRobert Watson  */
90526f9a767SRodney W. Grimes void
906136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
907df8bae1dSRodney W. Grimes {
9084c7c478dSRobert Watson 
909a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
910c0a211c5SRobert Watson 
9114c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
9124c7c478dSRobert Watson 	inp->inp_socket = NULL;
9134c7c478dSRobert Watson }
9144c7c478dSRobert Watson 
915c0a211c5SRobert Watson /*
91628696211SRobert Watson  * in_pcbfree_internal() frees an inpcb that has been detached from its
91728696211SRobert Watson  * socket, and whose reference count has reached 0.  It will also remove the
91828696211SRobert Watson  * inpcb from any global lists it might remain on.
919c0a211c5SRobert Watson  */
92028696211SRobert Watson static void
92128696211SRobert Watson in_pcbfree_internal(struct inpcb *inp)
9224c7c478dSRobert Watson {
9233d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
924df8bae1dSRodney W. Grimes 
925a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
92628696211SRobert Watson 	KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__));
9278501a69cSRobert Watson 
928fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
9298501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
93059daba27SSam Leffler 
931b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
9326aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
9336974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
934b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
9353d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
936c3229e05SDavid Greenman 	in_pcbremlists(inp);
9376aee2fc5SBjoern A. Zeeb #ifdef INET6
9386aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
9396aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
9401096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
9416aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
9426aee2fc5SBjoern A. Zeeb 	}
9436aee2fc5SBjoern A. Zeeb #endif
944df8bae1dSRodney W. Grimes 	if (inp->inp_options)
945df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
94671498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
94771498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
948cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
94986d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
950d915b280SStephan Uphoff 
951a557af22SRobert Watson #ifdef MAC
95230d239bcSRobert Watson 	mac_inpcb_destroy(inp);
953a557af22SRobert Watson #endif
9548501a69cSRobert Watson 	INP_WUNLOCK(inp);
95569c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
956df8bae1dSRodney W. Grimes }
957df8bae1dSRodney W. Grimes 
95810702a28SRobert Watson /*
95928696211SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
96028696211SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
96128696211SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
96228696211SRobert Watson  * but where the inpcb lock is already held.
96328696211SRobert Watson  *
96428696211SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
96528696211SRobert Watson  * connection's state may change, so the caller should be careful to
96628696211SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
96728696211SRobert Watson  * using in_pcbrele().
96828696211SRobert Watson  */
96928696211SRobert Watson void
97028696211SRobert Watson in_pcbref(struct inpcb *inp)
97128696211SRobert Watson {
97228696211SRobert Watson 
97328696211SRobert Watson 	INP_WLOCK_ASSERT(inp);
97428696211SRobert Watson 
97528696211SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
97628696211SRobert Watson 
97728696211SRobert Watson 	inp->inp_refcount++;
97828696211SRobert Watson }
97928696211SRobert Watson 
98028696211SRobert Watson /*
98128696211SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
98228696211SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
98328696211SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
98428696211SRobert Watson  * valid, we return with the inpcb lock held.
98528696211SRobert Watson  */
98628696211SRobert Watson int
98728696211SRobert Watson in_pcbrele(struct inpcb *inp)
98828696211SRobert Watson {
98928696211SRobert Watson #ifdef INVARIANTS
99028696211SRobert Watson 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
99128696211SRobert Watson #endif
99228696211SRobert Watson 
99328696211SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
99428696211SRobert Watson 
99528696211SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
99628696211SRobert Watson 	INP_WLOCK_ASSERT(inp);
99728696211SRobert Watson 
99828696211SRobert Watson 	inp->inp_refcount--;
99928696211SRobert Watson 	if (inp->inp_refcount > 0)
100028696211SRobert Watson 		return (0);
100128696211SRobert Watson 	in_pcbfree_internal(inp);
100228696211SRobert Watson 	return (1);
100328696211SRobert Watson }
100428696211SRobert Watson 
100528696211SRobert Watson /*
100628696211SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
100728696211SRobert Watson  * reference count, which should occur only after the inpcb has been detached
100828696211SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
100928696211SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
101028696211SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.
101128696211SRobert Watson  */
101228696211SRobert Watson void
101328696211SRobert Watson in_pcbfree(struct inpcb *inp)
101428696211SRobert Watson {
101528696211SRobert Watson #ifdef INVARIANTS
101628696211SRobert Watson 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
101728696211SRobert Watson #endif
101828696211SRobert Watson 
101928696211SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL",
102028696211SRobert Watson 	    __func__));
102128696211SRobert Watson 
102228696211SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
102328696211SRobert Watson 	INP_WLOCK_ASSERT(inp);
102428696211SRobert Watson 
102528696211SRobert Watson 	if (!in_pcbrele(inp))
102628696211SRobert Watson 		INP_WUNLOCK(inp);
102728696211SRobert Watson }
102828696211SRobert Watson 
102928696211SRobert Watson /*
1030c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1031c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1032c0a211c5SRobert Watson  *
1033c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1034c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1035c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1036c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1037c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1038c0a211c5SRobert Watson  * in_pcbdetach().
1039c0a211c5SRobert Watson  *
1040c0a211c5SRobert Watson  * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
1041c0a211c5SRobert Watson  * lists, but can lead to confusing netstat output, as open sockets with
1042c0a211c5SRobert Watson  * closed TCP connections will no longer appear to have their bound port
1043c0a211c5SRobert Watson  * number.  An explicit flag would be better, as it would allow us to leave
1044c0a211c5SRobert Watson  * the port number intact after the connection is dropped.
1045c0a211c5SRobert Watson  *
1046c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1047c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
104810702a28SRobert Watson  */
104910702a28SRobert Watson void
105010702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
105110702a28SRobert Watson {
105210702a28SRobert Watson 
10537c5a8ab2SMarcel Moolenaar 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
10548501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
105510702a28SRobert Watson 
1056ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1057111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
105810702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
105910702a28SRobert Watson 
106010702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
106110702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
106210702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
106310702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
106410702a28SRobert Watson 			free(phd, M_PCB);
106510702a28SRobert Watson 		}
1066111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
106710702a28SRobert Watson 	}
106810702a28SRobert Watson }
106910702a28SRobert Watson 
107054d642bbSRobert Watson /*
107154d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
107254d642bbSRobert Watson  */
107326ef6ac4SDon Lewis struct sockaddr *
1074136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
107526ef6ac4SDon Lewis {
107626ef6ac4SDon Lewis 	struct sockaddr_in *sin;
107726ef6ac4SDon Lewis 
10781ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1079a163d034SWarner Losh 		M_WAITOK | M_ZERO);
108026ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
108126ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
108226ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
108326ef6ac4SDon Lewis 	sin->sin_port = port;
108426ef6ac4SDon Lewis 
108526ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
108626ef6ac4SDon Lewis }
108726ef6ac4SDon Lewis 
1088117bcae7SGarrett Wollman int
108954d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1090df8bae1dSRodney W. Grimes {
1091136d4f1cSRobert Watson 	struct inpcb *inp;
109226ef6ac4SDon Lewis 	struct in_addr addr;
109326ef6ac4SDon Lewis 	in_port_t port;
109442fa505bSDavid Greenman 
1095fdc984f7STor Egge 	inp = sotoinpcb(so);
109654d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
10976466b28aSRobert Watson 
1098a69042a5SRobert Watson 	INP_RLOCK(inp);
109926ef6ac4SDon Lewis 	port = inp->inp_lport;
110026ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1101a69042a5SRobert Watson 	INP_RUNLOCK(inp);
110242fa505bSDavid Greenman 
110326ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1104117bcae7SGarrett Wollman 	return 0;
1105df8bae1dSRodney W. Grimes }
1106df8bae1dSRodney W. Grimes 
1107117bcae7SGarrett Wollman int
110854d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1109df8bae1dSRodney W. Grimes {
1110136d4f1cSRobert Watson 	struct inpcb *inp;
111126ef6ac4SDon Lewis 	struct in_addr addr;
111226ef6ac4SDon Lewis 	in_port_t port;
111342fa505bSDavid Greenman 
1114fdc984f7STor Egge 	inp = sotoinpcb(so);
111554d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
11166466b28aSRobert Watson 
1117a69042a5SRobert Watson 	INP_RLOCK(inp);
111826ef6ac4SDon Lewis 	port = inp->inp_fport;
111926ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1120a69042a5SRobert Watson 	INP_RUNLOCK(inp);
112142fa505bSDavid Greenman 
112226ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1123117bcae7SGarrett Wollman 	return 0;
1124df8bae1dSRodney W. Grimes }
1125df8bae1dSRodney W. Grimes 
112626f9a767SRodney W. Grimes void
1127136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1128136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1129d1c54148SJesper Skriver {
1130f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1131d1c54148SJesper Skriver 
11323dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1133f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
11348501a69cSRobert Watson 		INP_WLOCK(inp);
1135d1c54148SJesper Skriver #ifdef INET6
1136f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
11378501a69cSRobert Watson 			INP_WUNLOCK(inp);
1138d1c54148SJesper Skriver 			continue;
1139f76fcf6dSJeffrey Hsu 		}
1140d1c54148SJesper Skriver #endif
1141d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1142f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
11438501a69cSRobert Watson 			INP_WUNLOCK(inp);
1144d1c54148SJesper Skriver 			continue;
1145d1c54148SJesper Skriver 		}
11463dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
11478501a69cSRobert Watson 			INP_WUNLOCK(inp);
1148f76fcf6dSJeffrey Hsu 	}
11493dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1150d1c54148SJesper Skriver }
1151d1c54148SJesper Skriver 
1152e43cc4aeSHajimu UMEMOTO void
1153136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1154e43cc4aeSHajimu UMEMOTO {
1155e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1156e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1157e43cc4aeSHajimu UMEMOTO 	int i, gap;
1158e43cc4aeSHajimu UMEMOTO 
1159f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
1160712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
11618501a69cSRobert Watson 		INP_WLOCK(inp);
1162e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1163e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1164e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1165e43cc4aeSHajimu UMEMOTO 			/*
1166e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1167e43cc4aeSHajimu UMEMOTO 			 * detached.
1168e43cc4aeSHajimu UMEMOTO 			 */
1169e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1170e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1171e43cc4aeSHajimu UMEMOTO 
1172e43cc4aeSHajimu UMEMOTO 			/*
1173e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1174e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1175e43cc4aeSHajimu UMEMOTO 			 */
1176e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1177e43cc4aeSHajimu UMEMOTO 			    i++) {
1178e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1179e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1180e43cc4aeSHajimu UMEMOTO 					gap++;
1181e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1182e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1183e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1184e43cc4aeSHajimu UMEMOTO 			}
1185e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1186e43cc4aeSHajimu UMEMOTO 		}
11878501a69cSRobert Watson 		INP_WUNLOCK(inp);
1188e43cc4aeSHajimu UMEMOTO 	}
11893cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
1190e43cc4aeSHajimu UMEMOTO }
1191e43cc4aeSHajimu UMEMOTO 
1192df8bae1dSRodney W. Grimes /*
1193c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
1194c3229e05SDavid Greenman  */
1195d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1196df8bae1dSRodney W. Grimes struct inpcb *
1197136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1198078b7042SBjoern A. Zeeb     u_short lport, int wild_okay, struct ucred *cred)
1199df8bae1dSRodney W. Grimes {
1200136d4f1cSRobert Watson 	struct inpcb *inp;
1201d5e8a67eSHajimu UMEMOTO #ifdef INET6
1202d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1203d5e8a67eSHajimu UMEMOTO #else
1204d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1205d5e8a67eSHajimu UMEMOTO #endif
1206d5e8a67eSHajimu UMEMOTO 	int wildcard;
12077bc4aca7SDavid Greenman 
12088501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
12091b73ca0bSSam Leffler 
1210c3229e05SDavid Greenman 	if (!wild_okay) {
1211c3229e05SDavid Greenman 		struct inpcbhead *head;
1212c3229e05SDavid Greenman 		/*
1213c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1214c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1215c3229e05SDavid Greenman 		 */
1216712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1217712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1218fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1219cfa1ca9dSYoshinobu Inoue #ifdef INET6
1220413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1221369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1222cfa1ca9dSYoshinobu Inoue 				continue;
1223cfa1ca9dSYoshinobu Inoue #endif
1224c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1225c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1226c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1227c3229e05SDavid Greenman 				/*
1228413628a7SBjoern A. Zeeb 				 * Found?
1229c3229e05SDavid Greenman 				 */
1230413628a7SBjoern A. Zeeb 				if (cred == NULL ||
12310304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
12320304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1233c3229e05SDavid Greenman 					return (inp);
1234df8bae1dSRodney W. Grimes 			}
1235c3229e05SDavid Greenman 		}
1236c3229e05SDavid Greenman 		/*
1237c3229e05SDavid Greenman 		 * Not found.
1238c3229e05SDavid Greenman 		 */
1239c3229e05SDavid Greenman 		return (NULL);
1240c3229e05SDavid Greenman 	} else {
1241c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1242c3229e05SDavid Greenman 		struct inpcbport *phd;
1243c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1244c3229e05SDavid Greenman 		/*
1245c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1246c3229e05SDavid Greenman 		 *
1247c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1248c3229e05SDavid Greenman 		 * port hash list.
1249c3229e05SDavid Greenman 		 */
1250712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1251712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1252fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1253c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1254c3229e05SDavid Greenman 				break;
1255c3229e05SDavid Greenman 		}
1256c3229e05SDavid Greenman 		if (phd != NULL) {
1257c3229e05SDavid Greenman 			/*
1258c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1259c3229e05SDavid Greenman 			 * fit.
1260c3229e05SDavid Greenman 			 */
126137d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1262c3229e05SDavid Greenman 				wildcard = 0;
1263413628a7SBjoern A. Zeeb 				if (cred != NULL &&
12640304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
12650304c731SJamie Gritton 					cred->cr_prison))
1266413628a7SBjoern A. Zeeb 					continue;
1267cfa1ca9dSYoshinobu Inoue #ifdef INET6
1268413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1269369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1270cfa1ca9dSYoshinobu Inoue 					continue;
1271d5e8a67eSHajimu UMEMOTO 				/*
1272d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1273d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1274d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1275d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1276d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1277d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1278d5e8a67eSHajimu UMEMOTO 				 *
1279d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1280d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1281d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1282d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1283d5e8a67eSHajimu UMEMOTO 				 */
1284d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1285d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1286cfa1ca9dSYoshinobu Inoue #endif
1287c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1288c3229e05SDavid Greenman 					wildcard++;
128915bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
129015bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
129115bd2b43SDavid Greenman 						wildcard++;
129215bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
129315bd2b43SDavid Greenman 						continue;
129415bd2b43SDavid Greenman 				} else {
129515bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
129615bd2b43SDavid Greenman 						wildcard++;
129715bd2b43SDavid Greenman 				}
1298df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1299df8bae1dSRodney W. Grimes 					match = inp;
1300df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1301413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1302df8bae1dSRodney W. Grimes 						break;
1303df8bae1dSRodney W. Grimes 				}
1304df8bae1dSRodney W. Grimes 			}
13053dbdc25cSDavid Greenman 		}
1306df8bae1dSRodney W. Grimes 		return (match);
1307df8bae1dSRodney W. Grimes 	}
1308c3229e05SDavid Greenman }
1309d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
131015bd2b43SDavid Greenman 
131115bd2b43SDavid Greenman /*
131215bd2b43SDavid Greenman  * Lookup PCB in hash list.
131315bd2b43SDavid Greenman  */
131415bd2b43SDavid Greenman struct inpcb *
1315136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1316136d4f1cSRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1317136d4f1cSRobert Watson     struct ifnet *ifp)
131815bd2b43SDavid Greenman {
131915bd2b43SDavid Greenman 	struct inpcbhead *head;
1320413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
132115bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
132215bd2b43SDavid Greenman 
13238501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
1324602cc7f1SRobert Watson 
132515bd2b43SDavid Greenman 	/*
132615bd2b43SDavid Greenman 	 * First look for an exact match.
132715bd2b43SDavid Greenman 	 */
1328413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1329712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1330712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1331fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1332cfa1ca9dSYoshinobu Inoue #ifdef INET6
1333413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1334369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1335cfa1ca9dSYoshinobu Inoue 			continue;
1336cfa1ca9dSYoshinobu Inoue #endif
13376d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1338ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1339ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1340413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1341413628a7SBjoern A. Zeeb 			/*
1342413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1343413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1344413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1345413628a7SBjoern A. Zeeb 			 */
13460304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1347c3229e05SDavid Greenman 				return (inp);
1348413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1349413628a7SBjoern A. Zeeb 				tmpinp = inp;
1350c3229e05SDavid Greenman 		}
1351413628a7SBjoern A. Zeeb 	}
1352413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1353413628a7SBjoern A. Zeeb 		return (tmpinp);
1354e3fd5ffdSRobert Watson 
1355e3fd5ffdSRobert Watson 	/*
1356e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1357e3fd5ffdSRobert Watson 	 */
1358413628a7SBjoern A. Zeeb 	if (wildcard == INPLOOKUP_WILDCARD) {
1359413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1360e3fd5ffdSRobert Watson #ifdef INET6
1361cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1362e3fd5ffdSRobert Watson #endif
1363413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1364413628a7SBjoern A. Zeeb 		int injail;
1365413628a7SBjoern A. Zeeb 
1366413628a7SBjoern A. Zeeb 		/*
1367413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1368413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1369413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1370413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1371413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1372413628a7SBjoern A. Zeeb 		 */
13736d6a026bSDavid Greenman 
1374712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1375712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1376fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1377cfa1ca9dSYoshinobu Inoue #ifdef INET6
1378413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1379369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1380cfa1ca9dSYoshinobu Inoue 				continue;
1381cfa1ca9dSYoshinobu Inoue #endif
1382413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1383413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1384413628a7SBjoern A. Zeeb 				continue;
1385413628a7SBjoern A. Zeeb 
1386413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1387cfa1ca9dSYoshinobu Inoue 			if (ifp && ifp->if_type == IFT_FAITH &&
1388cfa1ca9dSYoshinobu Inoue 			    (inp->inp_flags & INP_FAITH) == 0)
1389cfa1ca9dSYoshinobu Inoue 				continue;
1390413628a7SBjoern A. Zeeb 
13910304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1392413628a7SBjoern A. Zeeb 			if (injail) {
1393b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1394b89e82ddSJamie Gritton 				    &laddr) != 0)
1395413628a7SBjoern A. Zeeb 					continue;
1396413628a7SBjoern A. Zeeb 			} else {
1397413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1398413628a7SBjoern A. Zeeb 					continue;
1399413628a7SBjoern A. Zeeb 			}
1400413628a7SBjoern A. Zeeb 
1401413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1402413628a7SBjoern A. Zeeb 				if (injail)
1403c3229e05SDavid Greenman 					return (inp);
1404413628a7SBjoern A. Zeeb 				else
1405413628a7SBjoern A. Zeeb 					local_exact = inp;
1406413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1407e3fd5ffdSRobert Watson #ifdef INET6
1408413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
14095cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1410cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1411cfa1ca9dSYoshinobu Inoue 				else
1412413628a7SBjoern A. Zeeb #endif /* INET6 */
1413413628a7SBjoern A. Zeeb 					if (injail)
1414413628a7SBjoern A. Zeeb 						jail_wild = inp;
1415413628a7SBjoern A. Zeeb 					else
14166d6a026bSDavid Greenman 						local_wild = inp;
14176d6a026bSDavid Greenman 			}
1418413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1419413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1420413628a7SBjoern A. Zeeb 			return (jail_wild);
1421413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1422413628a7SBjoern A. Zeeb 			return (local_exact);
1423413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1424c3229e05SDavid Greenman 			return (local_wild);
1425413628a7SBjoern A. Zeeb #ifdef INET6
1426413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1427413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
1428413628a7SBjoern A. Zeeb #endif /* defined(INET6) */
1429413628a7SBjoern A. Zeeb 	} /* if (wildcard == INPLOOKUP_WILDCARD) */
1430413628a7SBjoern A. Zeeb 
14316d6a026bSDavid Greenman 	return (NULL);
143215bd2b43SDavid Greenman }
143315bd2b43SDavid Greenman 
14347bc4aca7SDavid Greenman /*
1435c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
14367bc4aca7SDavid Greenman  */
1437c3229e05SDavid Greenman int
1438136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
143915bd2b43SDavid Greenman {
1440c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1441c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1442c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1443c3229e05SDavid Greenman 	struct inpcbport *phd;
1444cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
144515bd2b43SDavid Greenman 
144659daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
14478501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1448111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1449111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
1450602cc7f1SRobert Watson 
1451cfa1ca9dSYoshinobu Inoue #ifdef INET6
1452cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1453cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1454cfa1ca9dSYoshinobu Inoue 	else
1455cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1456cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1457cfa1ca9dSYoshinobu Inoue 
1458712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1459712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
146015bd2b43SDavid Greenman 
1461712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
1462712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1463c3229e05SDavid Greenman 
1464c3229e05SDavid Greenman 	/*
1465c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1466c3229e05SDavid Greenman 	 */
1467fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1468c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1469c3229e05SDavid Greenman 			break;
1470c3229e05SDavid Greenman 	}
1471c3229e05SDavid Greenman 	/*
1472c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1473c3229e05SDavid Greenman 	 */
1474c3229e05SDavid Greenman 	if (phd == NULL) {
14751ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1476c3229e05SDavid Greenman 		if (phd == NULL) {
1477c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1478c3229e05SDavid Greenman 		}
1479c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1480c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1481c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1482c3229e05SDavid Greenman 	}
1483c3229e05SDavid Greenman 	inp->inp_phd = phd;
1484c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1485c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1486111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
1487c3229e05SDavid Greenman 	return (0);
148815bd2b43SDavid Greenman }
148915bd2b43SDavid Greenman 
1490c3229e05SDavid Greenman /*
1491c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1492c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1493c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1494c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1495c3229e05SDavid Greenman  */
149615bd2b43SDavid Greenman void
1497136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp)
149815bd2b43SDavid Greenman {
149959daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
150015bd2b43SDavid Greenman 	struct inpcbhead *head;
1501cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
150215bd2b43SDavid Greenman 
150359daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
15048501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1505111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
1506111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
1507602cc7f1SRobert Watson 
1508cfa1ca9dSYoshinobu Inoue #ifdef INET6
1509cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1510cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1511cfa1ca9dSYoshinobu Inoue 	else
1512cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1513cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1514cfa1ca9dSYoshinobu Inoue 
1515712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1516712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
151715bd2b43SDavid Greenman 
1518c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
151915bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1520c3229e05SDavid Greenman }
1521c3229e05SDavid Greenman 
1522c3229e05SDavid Greenman /*
1523c3229e05SDavid Greenman  * Remove PCB from various lists.
1524c3229e05SDavid Greenman  */
15256d888973SRobert Watson static void
1526136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1527c3229e05SDavid Greenman {
152859daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
152959daba27SSam Leffler 
153059daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
15318501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
153259daba27SSam Leffler 
153359daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1534111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
1535c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1536c3229e05SDavid Greenman 
1537c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1538c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1539fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1540c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1541c3229e05SDavid Greenman 			free(phd, M_PCB);
1542c3229e05SDavid Greenman 		}
1543111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
1544c3229e05SDavid Greenman 	}
1545c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
154659daba27SSam Leffler 	pcbinfo->ipi_count--;
154715bd2b43SDavid Greenman }
154875c13541SPoul-Henning Kamp 
1549a557af22SRobert Watson /*
1550a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1551a557af22SRobert Watson  * label change into the in_pcb for the socket.
1552a557af22SRobert Watson  */
1553a557af22SRobert Watson void
1554136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1555a557af22SRobert Watson {
1556a557af22SRobert Watson #ifdef MAC
1557a557af22SRobert Watson 	struct inpcb *inp;
1558a557af22SRobert Watson 
15594c7c478dSRobert Watson 	inp = sotoinpcb(so);
15604c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1561602cc7f1SRobert Watson 
15628501a69cSRobert Watson 	INP_WLOCK(inp);
1563310e7cebSRobert Watson 	SOCK_LOCK(so);
1564a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1565310e7cebSRobert Watson 	SOCK_UNLOCK(so);
15668501a69cSRobert Watson 	INP_WUNLOCK(inp);
1567a557af22SRobert Watson #endif
1568a557af22SRobert Watson }
15695f311da2SMike Silbersack 
15705f311da2SMike Silbersack /*
1571ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
1572ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
1573ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
1574ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
1575ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
15765f311da2SMike Silbersack  */
15775f311da2SMike Silbersack void
1578136d4f1cSRobert Watson ipport_tick(void *xtp)
15795f311da2SMike Silbersack {
15808b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
1581ad3a630fSRobert Watson 
15825ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
15838b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
15848b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
15858b615593SMarko Zec 		if (V_ipport_tcpallocs <=
15868b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
1587603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
1588603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
1589ad3a630fSRobert Watson 		} else
1590603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
1591603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
15928b615593SMarko Zec 		CURVNET_RESTORE();
15938b615593SMarko Zec 	}
15945ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
15955f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
15965f311da2SMike Silbersack }
1597497057eeSRobert Watson 
15983d585327SKip Macy void
15993d585327SKip Macy inp_wlock(struct inpcb *inp)
16003d585327SKip Macy {
16013d585327SKip Macy 
16028501a69cSRobert Watson 	INP_WLOCK(inp);
16033d585327SKip Macy }
16043d585327SKip Macy 
16053d585327SKip Macy void
16063d585327SKip Macy inp_wunlock(struct inpcb *inp)
16073d585327SKip Macy {
16083d585327SKip Macy 
16098501a69cSRobert Watson 	INP_WUNLOCK(inp);
16103d585327SKip Macy }
16113d585327SKip Macy 
16123d585327SKip Macy void
16133d585327SKip Macy inp_rlock(struct inpcb *inp)
16143d585327SKip Macy {
16153d585327SKip Macy 
1616a69042a5SRobert Watson 	INP_RLOCK(inp);
16173d585327SKip Macy }
16183d585327SKip Macy 
16193d585327SKip Macy void
16203d585327SKip Macy inp_runlock(struct inpcb *inp)
16213d585327SKip Macy {
16223d585327SKip Macy 
1623a69042a5SRobert Watson 	INP_RUNLOCK(inp);
16243d585327SKip Macy }
16253d585327SKip Macy 
16263d585327SKip Macy #ifdef INVARIANTS
16273d585327SKip Macy void
1628e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
16293d585327SKip Macy {
16303d585327SKip Macy 
16318501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
16323d585327SKip Macy }
16333d585327SKip Macy 
16343d585327SKip Macy void
1635e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
16363d585327SKip Macy {
16373d585327SKip Macy 
16383d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
16393d585327SKip Macy }
16403d585327SKip Macy #endif
16413d585327SKip Macy 
16429378e437SKip Macy void
16439378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
16449378e437SKip Macy {
16459378e437SKip Macy 	struct inpcb *inp;
16469378e437SKip Macy 
1647603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
164897021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
16499378e437SKip Macy 		INP_WLOCK(inp);
16509378e437SKip Macy 		func(inp, arg);
16519378e437SKip Macy 		INP_WUNLOCK(inp);
16529378e437SKip Macy 	}
1653603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
16549378e437SKip Macy }
16559378e437SKip Macy 
16569378e437SKip Macy struct socket *
16579378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
16589378e437SKip Macy {
16599378e437SKip Macy 
16609378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
16619378e437SKip Macy 	return (inp->inp_socket);
16629378e437SKip Macy }
16639378e437SKip Macy 
16649378e437SKip Macy struct tcpcb *
16659378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
16669378e437SKip Macy {
16679378e437SKip Macy 
16689378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
16699378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
16709378e437SKip Macy }
16719378e437SKip Macy 
16729378e437SKip Macy int
16739378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
16749378e437SKip Macy {
16759378e437SKip Macy 
16769378e437SKip Macy 	return (inp->inp_ip_tos);
16779378e437SKip Macy }
16789378e437SKip Macy 
16799378e437SKip Macy void
16809378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
16819378e437SKip Macy {
16829378e437SKip Macy 
16839378e437SKip Macy 	inp->inp_ip_tos = val;
16849378e437SKip Macy }
16859378e437SKip Macy 
16869378e437SKip Macy void
1687df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
16889d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
16899378e437SKip Macy {
16909378e437SKip Macy 
16919d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
1692df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
1693df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
16949378e437SKip Macy 	*lp = inp->inp_lport;
16959378e437SKip Macy 	*fp = inp->inp_fport;
16969378e437SKip Macy }
16979378e437SKip Macy 
1698dd0e6c38SKip Macy struct inpcb *
1699dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
1700dd0e6c38SKip Macy {
1701dd0e6c38SKip Macy 
1702dd0e6c38SKip Macy 	return (sotoinpcb(so));
1703dd0e6c38SKip Macy }
1704dd0e6c38SKip Macy 
1705dd0e6c38SKip Macy struct tcpcb *
1706dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
1707dd0e6c38SKip Macy {
1708dd0e6c38SKip Macy 
1709dd0e6c38SKip Macy 	return (sototcpcb(so));
1710dd0e6c38SKip Macy }
1711dd0e6c38SKip Macy 
1712497057eeSRobert Watson #ifdef DDB
1713497057eeSRobert Watson static void
1714497057eeSRobert Watson db_print_indent(int indent)
1715497057eeSRobert Watson {
1716497057eeSRobert Watson 	int i;
1717497057eeSRobert Watson 
1718497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1719497057eeSRobert Watson 		db_printf(" ");
1720497057eeSRobert Watson }
1721497057eeSRobert Watson 
1722497057eeSRobert Watson static void
1723497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1724497057eeSRobert Watson {
1725497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
1726497057eeSRobert Watson 
1727497057eeSRobert Watson 	db_print_indent(indent);
1728497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
1729497057eeSRobert Watson 
1730497057eeSRobert Watson 	indent += 2;
1731497057eeSRobert Watson 
173203dc38a4SRobert Watson #ifdef INET6
1733dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
1734497057eeSRobert Watson 		/* IPv6. */
1735497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1736497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1737497057eeSRobert Watson 	} else {
173803dc38a4SRobert Watson #endif
1739497057eeSRobert Watson 		/* IPv4. */
1740497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1741497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
174203dc38a4SRobert Watson #ifdef INET6
1743497057eeSRobert Watson 	}
174403dc38a4SRobert Watson #endif
1745497057eeSRobert Watson 	db_print_indent(indent);
1746497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1747497057eeSRobert Watson 	    ntohs(inc->inc_lport));
1748497057eeSRobert Watson 	db_print_indent(indent);
1749497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1750497057eeSRobert Watson 	    ntohs(inc->inc_fport));
1751497057eeSRobert Watson }
1752497057eeSRobert Watson 
1753497057eeSRobert Watson static void
1754497057eeSRobert Watson db_print_inpflags(int inp_flags)
1755497057eeSRobert Watson {
1756497057eeSRobert Watson 	int comma;
1757497057eeSRobert Watson 
1758497057eeSRobert Watson 	comma = 0;
1759497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
1760497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1761497057eeSRobert Watson 		comma = 1;
1762497057eeSRobert Watson 	}
1763497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
1764497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1765497057eeSRobert Watson 		comma = 1;
1766497057eeSRobert Watson 	}
1767497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
1768497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1769497057eeSRobert Watson 		comma = 1;
1770497057eeSRobert Watson 	}
1771497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
1772497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
1773497057eeSRobert Watson 		comma = 1;
1774497057eeSRobert Watson 	}
1775497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
1776497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1777497057eeSRobert Watson 		comma = 1;
1778497057eeSRobert Watson 	}
1779497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
1780497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
1781497057eeSRobert Watson 		comma = 1;
1782497057eeSRobert Watson 	}
1783497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
1784497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
1785497057eeSRobert Watson 		comma = 1;
1786497057eeSRobert Watson 	}
1787497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
1788497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
1789497057eeSRobert Watson 		comma = 1;
1790497057eeSRobert Watson 	}
1791497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
1792497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
1793497057eeSRobert Watson 		comma = 1;
1794497057eeSRobert Watson 	}
1795497057eeSRobert Watson 	if (inp_flags & INP_FAITH) {
1796497057eeSRobert Watson 		db_printf("%sINP_FAITH", comma ? ", " : "");
1797497057eeSRobert Watson 		comma = 1;
1798497057eeSRobert Watson 	}
1799497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
1800497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
1801497057eeSRobert Watson 		comma = 1;
1802497057eeSRobert Watson 	}
1803497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
1804497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1805497057eeSRobert Watson 		comma = 1;
1806497057eeSRobert Watson 	}
1807497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
1808497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1809497057eeSRobert Watson 		comma = 1;
1810497057eeSRobert Watson 	}
1811497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
1812497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1813497057eeSRobert Watson 		comma = 1;
1814497057eeSRobert Watson 	}
1815497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
1816497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1817497057eeSRobert Watson 		comma = 1;
1818497057eeSRobert Watson 	}
1819497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
1820497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1821497057eeSRobert Watson 		comma = 1;
1822497057eeSRobert Watson 	}
1823497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
1824497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1825497057eeSRobert Watson 		comma = 1;
1826497057eeSRobert Watson 	}
1827497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
1828497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1829497057eeSRobert Watson 		comma = 1;
1830497057eeSRobert Watson 	}
1831497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
1832497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1833497057eeSRobert Watson 		comma = 1;
1834497057eeSRobert Watson 	}
1835497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
1836497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1837497057eeSRobert Watson 		comma = 1;
1838497057eeSRobert Watson 	}
1839497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
1840497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1841497057eeSRobert Watson 		comma = 1;
1842497057eeSRobert Watson 	}
1843ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
1844ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1845ad71fe3cSRobert Watson 		comma  = 1;
1846ad71fe3cSRobert Watson 	}
1847ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
1848ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1849ad71fe3cSRobert Watson 		comma  = 1;
1850ad71fe3cSRobert Watson 	}
1851ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
1852ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
1853ad71fe3cSRobert Watson 		comma  = 1;
1854ad71fe3cSRobert Watson 	}
1855ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
1856ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
1857ad71fe3cSRobert Watson 		comma  = 1;
1858ad71fe3cSRobert Watson 	}
1859497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
1860497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1861497057eeSRobert Watson 		comma = 1;
1862497057eeSRobert Watson 	}
1863497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
1864497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
1865497057eeSRobert Watson 		comma = 1;
1866497057eeSRobert Watson 	}
1867497057eeSRobert Watson }
1868497057eeSRobert Watson 
1869497057eeSRobert Watson static void
1870497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
1871497057eeSRobert Watson {
1872497057eeSRobert Watson 	int comma;
1873497057eeSRobert Watson 
1874497057eeSRobert Watson 	comma = 0;
1875497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
1876497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
1877497057eeSRobert Watson 		comma  = 1;
1878497057eeSRobert Watson 	}
1879497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
1880497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
1881497057eeSRobert Watson 		comma  = 1;
1882497057eeSRobert Watson 	}
1883497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
1884497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1885497057eeSRobert Watson 		comma  = 1;
1886497057eeSRobert Watson 	}
1887497057eeSRobert Watson }
1888497057eeSRobert Watson 
18896d888973SRobert Watson static void
1890497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1891497057eeSRobert Watson {
1892497057eeSRobert Watson 
1893497057eeSRobert Watson 	db_print_indent(indent);
1894497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
1895497057eeSRobert Watson 
1896497057eeSRobert Watson 	indent += 2;
1897497057eeSRobert Watson 
1898497057eeSRobert Watson 	db_print_indent(indent);
1899497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1900497057eeSRobert Watson 
1901497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1902497057eeSRobert Watson 
1903497057eeSRobert Watson 	db_print_indent(indent);
1904497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1905497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1906497057eeSRobert Watson 
1907497057eeSRobert Watson 	db_print_indent(indent);
1908497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
1909497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
1910497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
1911497057eeSRobert Watson 	db_printf(")\n");
1912497057eeSRobert Watson 
1913497057eeSRobert Watson 	db_print_indent(indent);
1914497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1915497057eeSRobert Watson 	    inp->inp_vflag);
1916497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
1917497057eeSRobert Watson 	db_printf(")\n");
1918497057eeSRobert Watson 
1919497057eeSRobert Watson 	db_print_indent(indent);
1920497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1921497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1922497057eeSRobert Watson 
1923497057eeSRobert Watson 	db_print_indent(indent);
1924497057eeSRobert Watson #ifdef INET6
1925497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
1926497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
1927497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
1928497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
1929497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1930497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1931497057eeSRobert Watson 		    inp->in6p_hops);
1932497057eeSRobert Watson 	} else
1933497057eeSRobert Watson #endif
1934497057eeSRobert Watson 	{
1935497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1936497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1937497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
1938497057eeSRobert Watson 	}
1939497057eeSRobert Watson 
1940497057eeSRobert Watson 	db_print_indent(indent);
1941497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1942497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
1943497057eeSRobert Watson }
1944497057eeSRobert Watson 
1945497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1946497057eeSRobert Watson {
1947497057eeSRobert Watson 	struct inpcb *inp;
1948497057eeSRobert Watson 
1949497057eeSRobert Watson 	if (!have_addr) {
1950497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
1951497057eeSRobert Watson 		return;
1952497057eeSRobert Watson 	}
1953497057eeSRobert Watson 	inp = (struct inpcb *)addr;
1954497057eeSRobert Watson 
1955497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
1956497057eeSRobert Watson }
1957497057eeSRobert Watson #endif
1958