xref: /freebsd/sys/netinet/in_pcb.c (revision 136d4f1cf22198d604390fe2b73d230441fe9ddc)
1c398230bSWarner Losh /*-
22469dd60SGarrett Wollman  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
292469dd60SGarrett Wollman  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
336a800098SYoshinobu Inoue #include "opt_ipsec.h"
34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
35a557af22SRobert Watson #include "opt_mac.h"
36cfa1ca9dSYoshinobu Inoue 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39a557af22SRobert Watson #include <sys/mac.h>
40df8bae1dSRodney W. Grimes #include <sys/malloc.h>
41df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
42cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
43df8bae1dSRodney W. Grimes #include <sys/protosw.h>
44df8bae1dSRodney W. Grimes #include <sys/socket.h>
45df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
46df8bae1dSRodney W. Grimes #include <sys/proc.h>
4775c13541SPoul-Henning Kamp #include <sys/jail.h>
48101f9fc8SPeter Wemm #include <sys/kernel.h>
49101f9fc8SPeter Wemm #include <sys/sysctl.h>
508781d8e9SBruce Evans 
5169c2d429SJeff Roberson #include <vm/uma.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes #include <net/if.h>
54cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <netinet/in.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
60df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
61340c35deSJonathan Lemon #include <netinet/tcp_var.h>
625f311da2SMike Silbersack #include <netinet/udp.h>
635f311da2SMike Silbersack #include <netinet/udp_var.h>
64cfa1ca9dSYoshinobu Inoue #ifdef INET6
65cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
66cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
67cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
68cfa1ca9dSYoshinobu Inoue 
69cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
70cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
71cfa1ca9dSYoshinobu Inoue #include <netkey/key.h>
72cfa1ca9dSYoshinobu Inoue #endif /* IPSEC */
73df8bae1dSRodney W. Grimes 
74b9234fafSSam Leffler #ifdef FAST_IPSEC
75b9234fafSSam Leffler #if defined(IPSEC) || defined(IPSEC_ESP)
76b9234fafSSam Leffler #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
77b9234fafSSam Leffler #endif
78b9234fafSSam Leffler 
79b9234fafSSam Leffler #include <netipsec/ipsec.h>
80b9234fafSSam Leffler #include <netipsec/key.h>
81b9234fafSSam Leffler #endif /* FAST_IPSEC */
82b9234fafSSam Leffler 
83101f9fc8SPeter Wemm /*
84101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
85101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
86101f9fc8SPeter Wemm  */
8782cd038dSYoshinobu Inoue int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
8882cd038dSYoshinobu Inoue int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
899e5a5ed4SMike Silbersack int	ipport_firstauto = IPPORT_HIFIRSTAUTO;		/* 49152 */
909e5a5ed4SMike Silbersack int	ipport_lastauto  = IPPORT_HILASTAUTO;		/* 65535 */
9182cd038dSYoshinobu Inoue int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
9282cd038dSYoshinobu Inoue int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
93101f9fc8SPeter Wemm 
94b0d22693SCrist J. Clark /*
95b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
96b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
97b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
98b0d22693SCrist J. Clark  */
99b0d22693SCrist J. Clark int	ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
100b0d22693SCrist J. Clark int	ipport_reservedlow = 0;
101b0d22693SCrist J. Clark 
1025f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
1035f311da2SMike Silbersack int	ipport_randomized = 1;	/* user controlled via sysctl */
1045f311da2SMike Silbersack int	ipport_randomcps = 10;	/* user controlled via sysctl */
1055f311da2SMike Silbersack int	ipport_randomtime = 45;	/* user controlled via sysctl */
1065f311da2SMike Silbersack int	ipport_stoprandom = 0;	/* toggled by ipport_tick */
1075f311da2SMike Silbersack int	ipport_tcpallocs;
1085f311da2SMike Silbersack int	ipport_tcplastcount;
1096ac48b74SMike Silbersack 
110bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
111bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
112bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
113bbd42ad0SPeter Wemm 
114bbd42ad0SPeter Wemm static int
11582d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
116bbd42ad0SPeter Wemm {
11730a4ab08SBruce Evans 	int error;
11830a4ab08SBruce Evans 
11930a4ab08SBruce Evans 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
12030a4ab08SBruce Evans 	if (error == 0) {
121bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
122bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
12330a4ab08SBruce Evans 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
12430a4ab08SBruce Evans 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
12530a4ab08SBruce Evans 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
12630a4ab08SBruce Evans 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
127bbd42ad0SPeter Wemm 	}
12830a4ab08SBruce Evans 	return (error);
129bbd42ad0SPeter Wemm }
130bbd42ad0SPeter Wemm 
131bbd42ad0SPeter Wemm #undef RANGECHK
132bbd42ad0SPeter Wemm 
13333b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
13433b3ac06SPeter Wemm 
135bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
136bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
137bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
138bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
139bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
140bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
141bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
142bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
143bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
144bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
145bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
146bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
147b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
148b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
149b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
150b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
1516ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
1526ee79c59SMaxim Konovalov 	   &ipport_randomized, 0, "Enable random port allocation");
1536ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
1546ee79c59SMaxim Konovalov 	   &ipport_randomcps, 0, "Maximum number of random port "
1556ee79c59SMaxim Konovalov 	   "allocations before switching to a sequental one");
1566ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
1576ee79c59SMaxim Konovalov 	   &ipport_randomtime, 0, "Minimum time to keep sequental port "
1586ee79c59SMaxim Konovalov 	   "allocation before switching to a random one");
1590312fbe9SPoul-Henning Kamp 
160c3229e05SDavid Greenman /*
161c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
162c3229e05SDavid Greenman  *
163de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
164de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
165de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
166c3229e05SDavid Greenman  */
167c3229e05SDavid Greenman 
168c3229e05SDavid Greenman /*
169c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
170c3229e05SDavid Greenman  */
171df8bae1dSRodney W. Grimes int
172136d4f1cSRobert Watson in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, const char *type)
173df8bae1dSRodney W. Grimes {
174136d4f1cSRobert Watson 	struct inpcb *inp;
17513cf67f3SHajimu UMEMOTO 	int error;
176a557af22SRobert Watson 
17759daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
178a557af22SRobert Watson 	error = 0;
179d1dd20beSSam Leffler 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT | M_ZERO);
180df8bae1dSRodney W. Grimes 	if (inp == NULL)
181df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1823d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
18315bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
184df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
185a557af22SRobert Watson #ifdef MAC
186a557af22SRobert Watson 	error = mac_init_inpcb(inp, M_NOWAIT);
187a557af22SRobert Watson 	if (error != 0)
188a557af22SRobert Watson 		goto out;
189310e7cebSRobert Watson 	SOCK_LOCK(so);
190a557af22SRobert Watson 	mac_create_inpcb_from_socket(so, inp);
191310e7cebSRobert Watson 	SOCK_UNLOCK(so);
192a557af22SRobert Watson #endif
1930f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
1940f9ade71SHajimu UMEMOTO #ifdef FAST_IPSEC
19513cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
1960f9ade71SHajimu UMEMOTO #else
1970f9ade71SHajimu UMEMOTO 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
1980f9ade71SHajimu UMEMOTO #endif
199a557af22SRobert Watson 	if (error != 0)
200a557af22SRobert Watson 		goto out;
20113cf67f3SHajimu UMEMOTO #endif /*IPSEC*/
20275daea93SPaul Saab #if defined(INET6)
203340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
204340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
205340c35deSJonathan Lemon 		if (ip6_v6only)
20633841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
207340c35deSJonathan Lemon 	}
20875daea93SPaul Saab #endif
20915bd2b43SDavid Greenman 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
2103d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
211df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
2125bd311a5SSam Leffler 	INP_LOCK_INIT(inp, "inp", type);
21333841545SHajimu UMEMOTO #ifdef INET6
21433841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
21533841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
21633841545SHajimu UMEMOTO #endif
217a557af22SRobert Watson #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
218a557af22SRobert Watson out:
219a557af22SRobert Watson 	if (error != 0)
220a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
221a557af22SRobert Watson #endif
222a557af22SRobert Watson 	return (error);
223df8bae1dSRodney W. Grimes }
224df8bae1dSRodney W. Grimes 
225df8bae1dSRodney W. Grimes int
226136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
227df8bae1dSRodney W. Grimes {
2284b932371SIan Dowse 	int anonport, error;
2294b932371SIan Dowse 
2301b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
23159daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
23259daba27SSam Leffler 
2334b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2344b932371SIan Dowse 		return (EINVAL);
2354b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2364b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2374b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
238b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
2394b932371SIan Dowse 	if (error)
2404b932371SIan Dowse 		return (error);
2414b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2424b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2434b932371SIan Dowse 		inp->inp_lport = 0;
2444b932371SIan Dowse 		return (EAGAIN);
2454b932371SIan Dowse 	}
2464b932371SIan Dowse 	if (anonport)
2474b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2484b932371SIan Dowse 	return (0);
2494b932371SIan Dowse }
2504b932371SIan Dowse 
2514b932371SIan Dowse /*
2524b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2534b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2544b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2554b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2564b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2574b932371SIan Dowse  *
2584b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2594b932371SIan Dowse  */
2604b932371SIan Dowse int
261136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
262136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
2634b932371SIan Dowse {
2644b932371SIan Dowse 	struct socket *so = inp->inp_socket;
26537bd2b30SPeter Wemm 	unsigned short *lastport;
26615bd2b43SDavid Greenman 	struct sockaddr_in *sin;
267c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2684b932371SIan Dowse 	struct in_addr laddr;
269df8bae1dSRodney W. Grimes 	u_short lport = 0;
2704cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
27175c13541SPoul-Henning Kamp 	int error, prison = 0;
2725f311da2SMike Silbersack 	int dorandom;
273df8bae1dSRodney W. Grimes 
2741b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
27559daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
27659daba27SSam Leffler 
27759562606SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
278df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
2794b932371SIan Dowse 	laddr.s_addr = *laddrp;
2804b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
281df8bae1dSRodney W. Grimes 		return (EINVAL);
282c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
2836d6a026bSDavid Greenman 		wild = 1;
284df8bae1dSRodney W. Grimes 	if (nam) {
28557bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
28657bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
287df8bae1dSRodney W. Grimes 			return (EINVAL);
288df8bae1dSRodney W. Grimes #ifdef notdef
289df8bae1dSRodney W. Grimes 		/*
290df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
291df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
292df8bae1dSRodney W. Grimes 		 */
293df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
294df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
295df8bae1dSRodney W. Grimes #endif
296e4bdf25dSPoul-Henning Kamp 		if (sin->sin_addr.s_addr != INADDR_ANY)
297b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
29875c13541SPoul-Henning Kamp 				return(EINVAL);
2994b932371SIan Dowse 		if (sin->sin_port != *lportp) {
3004b932371SIan Dowse 			/* Don't allow the port to change. */
3014b932371SIan Dowse 			if (*lportp != 0)
3024b932371SIan Dowse 				return (EINVAL);
303df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
3044b932371SIan Dowse 		}
3054b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
306df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
307df8bae1dSRodney W. Grimes 			/*
308df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
309df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
310df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
311df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
312df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
313df8bae1dSRodney W. Grimes 			 */
314df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
315df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
316df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
317df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
31883103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
319df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
320df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
321df8bae1dSRodney W. Grimes 		}
3224b932371SIan Dowse 		laddr = sin->sin_addr;
323df8bae1dSRodney W. Grimes 		if (lport) {
324df8bae1dSRodney W. Grimes 			struct inpcb *t;
325df8bae1dSRodney W. Grimes 			/* GROSS */
326b0d22693SCrist J. Clark 			if (ntohs(lport) <= ipport_reservedhigh &&
327b0d22693SCrist J. Clark 			    ntohs(lport) >= ipport_reservedlow &&
32856f21b9dSColin Percival 			    suser_cred(cred, SUSER_ALLOWJAIL))
3292469dd60SGarrett Wollman 				return (EACCES);
330b0330ed9SPawel Jakub Dawidek 			if (jailed(cred))
33175c13541SPoul-Henning Kamp 				prison = 1;
3322f9a2132SBrian Feldman 			if (so->so_cred->cr_uid != 0 &&
33352b65dbeSBill Fenner 			    !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
3344049a042SGuido van Rooij 				t = in_pcblookup_local(inp->inp_pcbinfo,
33575c13541SPoul-Henning Kamp 				    sin->sin_addr, lport,
33675c13541SPoul-Henning Kamp 				    prison ? 0 :  INPLOOKUP_WILDCARD);
337340c35deSJonathan Lemon 	/*
338340c35deSJonathan Lemon 	 * XXX
339340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
340340c35deSJonathan Lemon 	 */
3414cc20ab1SSeigo Tanimura 				if (t &&
3424658dc83SYaroslav Tykhiy 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
3434658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
3444658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
3454cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
34652b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
34752b65dbeSBill Fenner 				     (t->inp_socket->so_options &
34852b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
3492f9a2132SBrian Feldman 				    (so->so_cred->cr_uid !=
350a4eb4405SYaroslav Tykhiy 				     t->inp_socket->so_cred->cr_uid))
3514049a042SGuido van Rooij 					return (EADDRINUSE);
3524049a042SGuido van Rooij 			}
353b0330ed9SPawel Jakub Dawidek 			if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
354970680faSPoul-Henning Kamp 				return (EADDRNOTAVAIL);
355c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
35675c13541SPoul-Henning Kamp 			    lport, prison ? 0 : wild);
357340c35deSJonathan Lemon 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
358340c35deSJonathan Lemon 				if ((reuseport & intotw(t)->tw_so_options) == 0)
359340c35deSJonathan Lemon 					return (EADDRINUSE);
360340c35deSJonathan Lemon 			} else
3614cc20ab1SSeigo Tanimura 			if (t &&
3624cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
363cfa1ca9dSYoshinobu Inoue #if defined(INET6)
36433841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
365cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
366cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
367cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
368cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
369cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
370cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
371df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
372df8bae1dSRodney W. Grimes 			}
373cfa1ca9dSYoshinobu Inoue 		}
374df8bae1dSRodney W. Grimes 	}
3754b932371SIan Dowse 	if (*lportp != 0)
3764b932371SIan Dowse 		lport = *lportp;
37733b3ac06SPeter Wemm 	if (lport == 0) {
3786ac48b74SMike Silbersack 		u_short first, last;
379174624e0SMike Silbersack 		int count;
38033b3ac06SPeter Wemm 
3814b932371SIan Dowse 		if (laddr.s_addr != INADDR_ANY)
382b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &laddr.s_addr))
38375c13541SPoul-Henning Kamp 				return (EINVAL);
384321a2846SPoul-Henning Kamp 
38533b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
38633b3ac06SPeter Wemm 			first = ipport_hifirstauto;	/* sysctl */
38733b3ac06SPeter Wemm 			last  = ipport_hilastauto;
388c3229e05SDavid Greenman 			lastport = &pcbinfo->lasthi;
38933b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
39056f21b9dSColin Percival 			if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
391a29f300eSGarrett Wollman 				return error;
392bbd42ad0SPeter Wemm 			first = ipport_lowfirstauto;	/* 1023 */
393bbd42ad0SPeter Wemm 			last  = ipport_lowlastauto;	/* 600 */
394c3229e05SDavid Greenman 			lastport = &pcbinfo->lastlow;
39533b3ac06SPeter Wemm 		} else {
39633b3ac06SPeter Wemm 			first = ipport_firstauto;	/* sysctl */
39733b3ac06SPeter Wemm 			last  = ipport_lastauto;
398c3229e05SDavid Greenman 			lastport = &pcbinfo->lastport;
39933b3ac06SPeter Wemm 		}
40033b3ac06SPeter Wemm 		/*
4015f311da2SMike Silbersack 		 * For UDP, use random port allocation as long as the user
4025f311da2SMike Silbersack 		 * allows it.  For TCP (and as of yet unknown) connections,
4035f311da2SMike Silbersack 		 * use random port allocation only if the user allows it AND
40429f2a6ecSMaxim Konovalov 		 * ipport_tick() allows it.
4055f311da2SMike Silbersack 		 */
4065f311da2SMike Silbersack 		if (ipport_randomized &&
4075f311da2SMike Silbersack 			(!ipport_stoprandom || pcbinfo == &udbinfo))
4085f311da2SMike Silbersack 			dorandom = 1;
4095f311da2SMike Silbersack 		else
4105f311da2SMike Silbersack 			dorandom = 0;
411e99971bfSMaxim Konovalov 		/*
412e99971bfSMaxim Konovalov 		 * It makes no sense to do random port allocation if
413e99971bfSMaxim Konovalov 		 * we have the only port available.
414e99971bfSMaxim Konovalov 		 */
415e99971bfSMaxim Konovalov 		if (first == last)
416e99971bfSMaxim Konovalov 			dorandom = 0;
4175f311da2SMike Silbersack 		/* Make sure to not include UDP packets in the count. */
4185f311da2SMike Silbersack 		if (pcbinfo != &udbinfo)
4195f311da2SMike Silbersack 			ipport_tcpallocs++;
4205f311da2SMike Silbersack 		/*
42133b3ac06SPeter Wemm 		 * Simple check to ensure all ports are not used up causing
42233b3ac06SPeter Wemm 		 * a deadlock here.
42333b3ac06SPeter Wemm 		 *
42433b3ac06SPeter Wemm 		 * We split the two cases (up and down) so that the direction
42533b3ac06SPeter Wemm 		 * is not being tested on each round of the loop.
42633b3ac06SPeter Wemm 		 */
42733b3ac06SPeter Wemm 		if (first > last) {
42833b3ac06SPeter Wemm 			/*
42933b3ac06SPeter Wemm 			 * counting down
43033b3ac06SPeter Wemm 			 */
4315f311da2SMike Silbersack 			if (dorandom)
4326b2fc10bSMike Silbersack 				*lastport = first -
4336b2fc10bSMike Silbersack 					    (arc4random() % (first - last));
43433b3ac06SPeter Wemm 			count = first - last;
435174624e0SMike Silbersack 
436df8bae1dSRodney W. Grimes 			do {
4376ac48b74SMike Silbersack 				if (count-- < 0)	/* completely used? */
438550b1518SWes Peters 					return (EADDRNOTAVAIL);
43933b3ac06SPeter Wemm 				--*lastport;
44033b3ac06SPeter Wemm 				if (*lastport > first || *lastport < last)
44133b3ac06SPeter Wemm 					*lastport = first;
44215bd2b43SDavid Greenman 				lport = htons(*lastport);
4434b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4444b932371SIan Dowse 			    wild));
44533b3ac06SPeter Wemm 		} else {
44633b3ac06SPeter Wemm 			/*
44733b3ac06SPeter Wemm 			 * counting up
44833b3ac06SPeter Wemm 			 */
4495f311da2SMike Silbersack 			if (dorandom)
4506b2fc10bSMike Silbersack 				*lastport = first +
4516b2fc10bSMike Silbersack 					    (arc4random() % (last - first));
45233b3ac06SPeter Wemm 			count = last - first;
453174624e0SMike Silbersack 
45433b3ac06SPeter Wemm 			do {
4556ac48b74SMike Silbersack 				if (count-- < 0)	/* completely used? */
456550b1518SWes Peters 					return (EADDRNOTAVAIL);
45733b3ac06SPeter Wemm 				++*lastport;
45833b3ac06SPeter Wemm 				if (*lastport < first || *lastport > last)
45933b3ac06SPeter Wemm 					*lastport = first;
46033b3ac06SPeter Wemm 				lport = htons(*lastport);
4614b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4624b932371SIan Dowse 			    wild));
46333b3ac06SPeter Wemm 		}
46433b3ac06SPeter Wemm 	}
465b0330ed9SPawel Jakub Dawidek 	if (prison_ip(cred, 0, &laddr.s_addr))
466e4bdf25dSPoul-Henning Kamp 		return (EINVAL);
4674b932371SIan Dowse 	*laddrp = laddr.s_addr;
4684b932371SIan Dowse 	*lportp = lport;
469df8bae1dSRodney W. Grimes 	return (0);
470df8bae1dSRodney W. Grimes }
471df8bae1dSRodney W. Grimes 
472999f1343SGarrett Wollman /*
4735200e00eSIan Dowse  * Connect from a socket to a specified address.
4745200e00eSIan Dowse  * Both address and port must be specified in argument sin.
4755200e00eSIan Dowse  * If don't have a local address for this socket yet,
4765200e00eSIan Dowse  * then pick one.
477999f1343SGarrett Wollman  */
478999f1343SGarrett Wollman int
479136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
480999f1343SGarrett Wollman {
4815200e00eSIan Dowse 	u_short lport, fport;
4825200e00eSIan Dowse 	in_addr_t laddr, faddr;
4835200e00eSIan Dowse 	int anonport, error;
484df8bae1dSRodney W. Grimes 
48527f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
48627f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
48727f74fd0SRobert Watson 
4885200e00eSIan Dowse 	lport = inp->inp_lport;
4895200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
4905200e00eSIan Dowse 	anonport = (lport == 0);
4915200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
492b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
4935200e00eSIan Dowse 	if (error)
4945200e00eSIan Dowse 		return (error);
4955200e00eSIan Dowse 
4965200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
4975200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
4985200e00eSIan Dowse 		inp->inp_lport = lport;
4995200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
5005200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
5015200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
5025200e00eSIan Dowse 			inp->inp_lport = 0;
5035200e00eSIan Dowse 			return (EAGAIN);
5045200e00eSIan Dowse 		}
5055200e00eSIan Dowse 	}
5065200e00eSIan Dowse 
5075200e00eSIan Dowse 	/* Commit the remaining changes. */
5085200e00eSIan Dowse 	inp->inp_lport = lport;
5095200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
5105200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
5115200e00eSIan Dowse 	inp->inp_fport = fport;
5125200e00eSIan Dowse 	in_pcbrehash(inp);
5130f9ade71SHajimu UMEMOTO #ifdef IPSEC
5140f9ade71SHajimu UMEMOTO 	if (inp->inp_socket->so_type == SOCK_STREAM)
5150f9ade71SHajimu UMEMOTO 		ipsec_pcbconn(inp->inp_sp);
5160f9ade71SHajimu UMEMOTO #endif
5175200e00eSIan Dowse 	if (anonport)
5185200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
5195200e00eSIan Dowse 	return (0);
5205200e00eSIan Dowse }
5215200e00eSIan Dowse 
5225200e00eSIan Dowse /*
5235200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
5245200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
5255200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
5265200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
5275200e00eSIan Dowse  * the connect.
5285200e00eSIan Dowse  *
5295200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
5305200e00eSIan Dowse  * and port. These are not updated in the error case.
5315200e00eSIan Dowse  *
5325200e00eSIan Dowse  * If the operation fails because the connection already exists,
5335200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
5345200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
5355200e00eSIan Dowse  * is set to NULL.
5365200e00eSIan Dowse  */
5375200e00eSIan Dowse int
538136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
539136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
540136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
5415200e00eSIan Dowse {
5425200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
5435200e00eSIan Dowse 	struct in_ifaddr *ia;
5445200e00eSIan Dowse 	struct sockaddr_in sa;
545b0330ed9SPawel Jakub Dawidek 	struct ucred *socred;
5465200e00eSIan Dowse 	struct inpcb *oinp;
5475200e00eSIan Dowse 	struct in_addr laddr, faddr;
5485200e00eSIan Dowse 	u_short lport, fport;
5495200e00eSIan Dowse 	int error;
5505200e00eSIan Dowse 
55127f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
55227f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
55327f74fd0SRobert Watson 
5545200e00eSIan Dowse 	if (oinpp != NULL)
5555200e00eSIan Dowse 		*oinpp = NULL;
55657bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
557df8bae1dSRodney W. Grimes 		return (EINVAL);
558df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
559df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
560df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
561df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5625200e00eSIan Dowse 	laddr.s_addr = *laddrp;
5635200e00eSIan Dowse 	lport = *lportp;
5645200e00eSIan Dowse 	faddr = sin->sin_addr;
5655200e00eSIan Dowse 	fport = sin->sin_port;
566b0330ed9SPawel Jakub Dawidek 	socred = inp->inp_socket->so_cred;
567b0330ed9SPawel Jakub Dawidek 	if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
5685200e00eSIan Dowse 		bzero(&sa, sizeof(sa));
569b0330ed9SPawel Jakub Dawidek 		sa.sin_addr.s_addr = htonl(prison_getip(socred));
5705200e00eSIan Dowse 		sa.sin_len = sizeof(sa);
5715200e00eSIan Dowse 		sa.sin_family = AF_INET;
5725200e00eSIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
573b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, cred);
5745200e00eSIan Dowse 		if (error)
5755200e00eSIan Dowse 			return (error);
5765200e00eSIan Dowse 	}
57759562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
578df8bae1dSRodney W. Grimes 		/*
579df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
580df8bae1dSRodney W. Grimes 		 * use the primary local address.
581df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
582df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
583df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
584df8bae1dSRodney W. Grimes 		 */
5855200e00eSIan Dowse 		if (faddr.s_addr == INADDR_ANY)
5865200e00eSIan Dowse 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
5875200e00eSIan Dowse 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
5885200e00eSIan Dowse 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
5895200e00eSIan Dowse 		    IFF_BROADCAST))
5905200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
5915200e00eSIan Dowse 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
592df8bae1dSRodney W. Grimes 	}
5935200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
59497d8d152SAndre Oppermann 		struct route sro;
595df8bae1dSRodney W. Grimes 
5960cfbbe3bSAndre Oppermann 		bzero(&sro, sizeof(sro));
597df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
598df8bae1dSRodney W. Grimes 		/*
59997d8d152SAndre Oppermann 		 * If route is known our src addr is taken from the i/f,
60097d8d152SAndre Oppermann 		 * else punt.
601df8bae1dSRodney W. Grimes 		 */
60297d8d152SAndre Oppermann 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) {
60397d8d152SAndre Oppermann 			/* Find out route to destination */
60497d8d152SAndre Oppermann 			sro.ro_dst.sa_family = AF_INET;
60597d8d152SAndre Oppermann 			sro.ro_dst.sa_len = sizeof(struct sockaddr_in);
60697d8d152SAndre Oppermann 			((struct sockaddr_in *)&sro.ro_dst)->sin_addr = faddr;
60797d8d152SAndre Oppermann 			rtalloc_ign(&sro, RTF_CLONING);
6084cc20ab1SSeigo Tanimura 		}
609df8bae1dSRodney W. Grimes 		/*
610df8bae1dSRodney W. Grimes 		 * If we found a route, use the address
611914d092fSGleb Smirnoff 		 * corresponding to the outgoing interface.
612df8bae1dSRodney W. Grimes 		 */
613797127a9SGleb Smirnoff 		if (sro.ro_rt) {
61497d8d152SAndre Oppermann 			ia = ifatoia(sro.ro_rt->rt_ifa);
61597d8d152SAndre Oppermann 			RTFREE(sro.ro_rt);
616797127a9SGleb Smirnoff 		}
617df8bae1dSRodney W. Grimes 		if (ia == 0) {
6185200e00eSIan Dowse 			bzero(&sa, sizeof(sa));
6195200e00eSIan Dowse 			sa.sin_addr = faddr;
6205200e00eSIan Dowse 			sa.sin_len = sizeof(sa);
6215200e00eSIan Dowse 			sa.sin_family = AF_INET;
622df8bae1dSRodney W. Grimes 
6235200e00eSIan Dowse 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
624df8bae1dSRodney W. Grimes 			if (ia == 0)
6255200e00eSIan Dowse 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
626df8bae1dSRodney W. Grimes 			if (ia == 0)
627ef14c369SMaxim Konovalov 				return (ENETUNREACH);
628df8bae1dSRodney W. Grimes 		}
629df8bae1dSRodney W. Grimes 		/*
630df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
631df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
632df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
633df8bae1dSRodney W. Grimes 		 */
6345200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
635df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
636df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
637df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
640df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
641df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
64237d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
643df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
644df8bae1dSRodney W. Grimes 						break;
645df8bae1dSRodney W. Grimes 				if (ia == 0)
646df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
647df8bae1dSRodney W. Grimes 			}
648df8bae1dSRodney W. Grimes 		}
6495200e00eSIan Dowse 		laddr = ia->ia_addr.sin_addr;
650999f1343SGarrett Wollman 	}
651999f1343SGarrett Wollman 
6525200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
6535200e00eSIan Dowse 	    0, NULL);
6545200e00eSIan Dowse 	if (oinp != NULL) {
6555200e00eSIan Dowse 		if (oinpp != NULL)
6565200e00eSIan Dowse 			*oinpp = oinp;
657df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
658c3229e05SDavid Greenman 	}
6595200e00eSIan Dowse 	if (lport == 0) {
660b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
661b0330ed9SPawel Jakub Dawidek 		    cred);
6625a903f8dSPierre Beyssac 		if (error)
6635a903f8dSPierre Beyssac 			return (error);
6645a903f8dSPierre Beyssac 	}
6655200e00eSIan Dowse 	*laddrp = laddr.s_addr;
6665200e00eSIan Dowse 	*lportp = lport;
6675200e00eSIan Dowse 	*faddrp = faddr.s_addr;
6685200e00eSIan Dowse 	*fportp = fport;
669df8bae1dSRodney W. Grimes 	return (0);
670df8bae1dSRodney W. Grimes }
671df8bae1dSRodney W. Grimes 
67226f9a767SRodney W. Grimes void
673136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
674df8bae1dSRodney W. Grimes {
6756b348152SRobert Watson 
676fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
67759daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
680df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
68115bd2b43SDavid Greenman 	in_pcbrehash(inp);
6820f9ade71SHajimu UMEMOTO #ifdef IPSEC
6830f9ade71SHajimu UMEMOTO 	ipsec_pcbdisconn(inp->inp_sp);
6840f9ade71SHajimu UMEMOTO #endif
685548c676bSHajimu UMEMOTO 	if (inp->inp_socket->so_state & SS_NOFDREF)
686548c676bSHajimu UMEMOTO 		in_pcbdetach(inp);
687df8bae1dSRodney W. Grimes }
688df8bae1dSRodney W. Grimes 
68926f9a767SRodney W. Grimes void
690136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
691df8bae1dSRodney W. Grimes {
692df8bae1dSRodney W. Grimes 	struct socket *so = inp->inp_socket;
6933d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
694df8bae1dSRodney W. Grimes 
695fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
69659daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
69759daba27SSam Leffler 
6980f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
699cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
700cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
7013d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
702c3229e05SDavid Greenman 	in_pcbremlists(inp);
703340c35deSJonathan Lemon 	if (so) {
70481158452SRobert Watson 		ACCEPT_LOCK();
705395a08c9SRobert Watson 		SOCK_LOCK(so);
70648ac555dSRobert Watson 		so->so_pcb = NULL;
707b1e4abd2SMatthew Dillon 		sotryfree(so);
708340c35deSJonathan Lemon 	}
709df8bae1dSRodney W. Grimes 	if (inp->inp_options)
710df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
711df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
712cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
713f76fcf6dSJeffrey Hsu 	INP_LOCK_DESTROY(inp);
714a557af22SRobert Watson #ifdef MAC
715a557af22SRobert Watson 	mac_destroy_inpcb(inp);
716a557af22SRobert Watson #endif
71769c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
718df8bae1dSRodney W. Grimes }
719df8bae1dSRodney W. Grimes 
72026ef6ac4SDon Lewis struct sockaddr *
721136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
72226ef6ac4SDon Lewis {
72326ef6ac4SDon Lewis 	struct sockaddr_in *sin;
72426ef6ac4SDon Lewis 
72526ef6ac4SDon Lewis 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
726a163d034SWarner Losh 		M_WAITOK | M_ZERO);
72726ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
72826ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
72926ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
73026ef6ac4SDon Lewis 	sin->sin_port = port;
73126ef6ac4SDon Lewis 
73226ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
73326ef6ac4SDon Lewis }
73426ef6ac4SDon Lewis 
735117bcae7SGarrett Wollman /*
736f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
737f76fcf6dSJeffrey Hsu  * The socket must have a valid
738117bcae7SGarrett Wollman  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
739117bcae7SGarrett Wollman  * except through a kernel programming error, so it is acceptable to panic
74057bf258eSGarrett Wollman  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
74157bf258eSGarrett Wollman  * because there actually /is/ a programming error somewhere... XXX)
742117bcae7SGarrett Wollman  */
743117bcae7SGarrett Wollman int
744136d4f1cSRobert Watson in_setsockaddr(struct socket *so, struct sockaddr **nam,
745136d4f1cSRobert Watson     struct inpcbinfo *pcbinfo)
746df8bae1dSRodney W. Grimes {
747136d4f1cSRobert Watson 	struct inpcb *inp;
74826ef6ac4SDon Lewis 	struct in_addr addr;
74926ef6ac4SDon Lewis 	in_port_t port;
75042fa505bSDavid Greenman 
751f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
752fdc984f7STor Egge 	inp = sotoinpcb(so);
753db112f04STor Egge 	if (!inp) {
754f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
755ff079ca4SPeter Wemm 		return ECONNRESET;
756db112f04STor Egge 	}
757f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
75826ef6ac4SDon Lewis 	port = inp->inp_lport;
75926ef6ac4SDon Lewis 	addr = inp->inp_laddr;
760f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
761f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
76242fa505bSDavid Greenman 
76326ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
764117bcae7SGarrett Wollman 	return 0;
765df8bae1dSRodney W. Grimes }
766df8bae1dSRodney W. Grimes 
767f76fcf6dSJeffrey Hsu /*
768f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
769f76fcf6dSJeffrey Hsu  */
770117bcae7SGarrett Wollman int
771136d4f1cSRobert Watson in_setpeeraddr(struct socket *so, struct sockaddr **nam,
772136d4f1cSRobert Watson     struct inpcbinfo *pcbinfo)
773df8bae1dSRodney W. Grimes {
774136d4f1cSRobert Watson 	struct inpcb *inp;
77526ef6ac4SDon Lewis 	struct in_addr addr;
77626ef6ac4SDon Lewis 	in_port_t port;
77742fa505bSDavid Greenman 
778f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
779fdc984f7STor Egge 	inp = sotoinpcb(so);
780db112f04STor Egge 	if (!inp) {
781f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
782ff079ca4SPeter Wemm 		return ECONNRESET;
783db112f04STor Egge 	}
784f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
78526ef6ac4SDon Lewis 	port = inp->inp_fport;
78626ef6ac4SDon Lewis 	addr = inp->inp_faddr;
787f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
788f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
78942fa505bSDavid Greenman 
79026ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
791117bcae7SGarrett Wollman 	return 0;
792df8bae1dSRodney W. Grimes }
793df8bae1dSRodney W. Grimes 
79426f9a767SRodney W. Grimes void
795136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
796136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
797d1c54148SJesper Skriver {
798c693a045SJonathan Lemon 	struct inpcb *inp, *ninp;
799f76fcf6dSJeffrey Hsu 	struct inpcbhead *head;
800d1c54148SJesper Skriver 
8013dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
802f76fcf6dSJeffrey Hsu 	head = pcbinfo->listhead;
803c693a045SJonathan Lemon 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
804f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
805c693a045SJonathan Lemon 		ninp = LIST_NEXT(inp, inp_list);
806d1c54148SJesper Skriver #ifdef INET6
807f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
808f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
809d1c54148SJesper Skriver 			continue;
810f76fcf6dSJeffrey Hsu 		}
811d1c54148SJesper Skriver #endif
812d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
813f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
814f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
815d1c54148SJesper Skriver 			continue;
816d1c54148SJesper Skriver 		}
8173dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
818f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
819f76fcf6dSJeffrey Hsu 	}
8203dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
821d1c54148SJesper Skriver }
822d1c54148SJesper Skriver 
823e43cc4aeSHajimu UMEMOTO void
824136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
825e43cc4aeSHajimu UMEMOTO {
826e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
827e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
828e43cc4aeSHajimu UMEMOTO 	int i, gap;
829e43cc4aeSHajimu UMEMOTO 
830f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
8313cfcc388SJeffrey Hsu 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
832f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
833e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
834e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
835e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
836e43cc4aeSHajimu UMEMOTO 			/*
837e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
838e43cc4aeSHajimu UMEMOTO 			 * detached.
839e43cc4aeSHajimu UMEMOTO 			 */
840e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
841e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
842e43cc4aeSHajimu UMEMOTO 
843e43cc4aeSHajimu UMEMOTO 			/*
844e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
845e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
846e43cc4aeSHajimu UMEMOTO 			 */
847e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
848e43cc4aeSHajimu UMEMOTO 			    i++) {
849e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
850e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
851e43cc4aeSHajimu UMEMOTO 					gap++;
852e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
853e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
854e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
855e43cc4aeSHajimu UMEMOTO 			}
856e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
857e43cc4aeSHajimu UMEMOTO 		}
858f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
859e43cc4aeSHajimu UMEMOTO 	}
8603cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
861e43cc4aeSHajimu UMEMOTO }
862e43cc4aeSHajimu UMEMOTO 
863df8bae1dSRodney W. Grimes /*
864c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
865c3229e05SDavid Greenman  */
866df8bae1dSRodney W. Grimes struct inpcb *
867136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
868136d4f1cSRobert Watson     u_int lport_arg, int wild_okay)
869df8bae1dSRodney W. Grimes {
870136d4f1cSRobert Watson 	struct inpcb *inp;
871df8bae1dSRodney W. Grimes 	int matchwild = 3, wildcard;
872c3229e05SDavid Greenman 	u_short lport = lport_arg;
8737bc4aca7SDavid Greenman 
8741b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
8751b73ca0bSSam Leffler 
876c3229e05SDavid Greenman 	if (!wild_okay) {
877c3229e05SDavid Greenman 		struct inpcbhead *head;
878c3229e05SDavid Greenman 		/*
879c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
880c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
881c3229e05SDavid Greenman 		 */
882c3229e05SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
883fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
884cfa1ca9dSYoshinobu Inoue #ifdef INET6
885369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
886cfa1ca9dSYoshinobu Inoue 				continue;
887cfa1ca9dSYoshinobu Inoue #endif
888c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
889c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
890c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
891c3229e05SDavid Greenman 				/*
892c3229e05SDavid Greenman 				 * Found.
893c3229e05SDavid Greenman 				 */
894c3229e05SDavid Greenman 				return (inp);
895df8bae1dSRodney W. Grimes 			}
896c3229e05SDavid Greenman 		}
897c3229e05SDavid Greenman 		/*
898c3229e05SDavid Greenman 		 * Not found.
899c3229e05SDavid Greenman 		 */
900c3229e05SDavid Greenman 		return (NULL);
901c3229e05SDavid Greenman 	} else {
902c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
903c3229e05SDavid Greenman 		struct inpcbport *phd;
904c3229e05SDavid Greenman 		struct inpcb *match = NULL;
905c3229e05SDavid Greenman 		/*
906c3229e05SDavid Greenman 		 * Best fit PCB lookup.
907c3229e05SDavid Greenman 		 *
908c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
909c3229e05SDavid Greenman 		 * port hash list.
910c3229e05SDavid Greenman 		 */
91196af9ea5SMike Silbersack 		retrylookup:
912c3229e05SDavid Greenman 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
913c3229e05SDavid Greenman 		    pcbinfo->porthashmask)];
914fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
915c3229e05SDavid Greenman 			if (phd->phd_port == lport)
916c3229e05SDavid Greenman 				break;
917c3229e05SDavid Greenman 		}
918c3229e05SDavid Greenman 		if (phd != NULL) {
919c3229e05SDavid Greenman 			/*
920c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
921c3229e05SDavid Greenman 			 * fit.
922c3229e05SDavid Greenman 			 */
92337d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
924c3229e05SDavid Greenman 				wildcard = 0;
925cfa1ca9dSYoshinobu Inoue #ifdef INET6
926369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
927cfa1ca9dSYoshinobu Inoue 					continue;
928cfa1ca9dSYoshinobu Inoue #endif
92996af9ea5SMike Silbersack 				/*
93096af9ea5SMike Silbersack 				 * Clean out old time_wait sockets if they
93196af9ea5SMike Silbersack 				 * are clogging up needed local ports.
93296af9ea5SMike Silbersack 				 */
93396af9ea5SMike Silbersack 				if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
93496af9ea5SMike Silbersack 					if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) {
935f7bbe2c0SSam Leffler 						INP_LOCK(inp);
93696af9ea5SMike Silbersack 						tcp_twclose((struct tcptw *)inp->inp_ppcb, 0);
93796af9ea5SMike Silbersack 						match = NULL;
93896af9ea5SMike Silbersack 						goto retrylookup;
93996af9ea5SMike Silbersack 					}
94096af9ea5SMike Silbersack 				}
941c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
942c3229e05SDavid Greenman 					wildcard++;
94315bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
94415bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
94515bd2b43SDavid Greenman 						wildcard++;
94615bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
94715bd2b43SDavid Greenman 						continue;
94815bd2b43SDavid Greenman 				} else {
94915bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
95015bd2b43SDavid Greenman 						wildcard++;
95115bd2b43SDavid Greenman 				}
952df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
953df8bae1dSRodney W. Grimes 					match = inp;
954df8bae1dSRodney W. Grimes 					matchwild = wildcard;
9553dbdc25cSDavid Greenman 					if (matchwild == 0) {
956df8bae1dSRodney W. Grimes 						break;
957df8bae1dSRodney W. Grimes 					}
958df8bae1dSRodney W. Grimes 				}
9593dbdc25cSDavid Greenman 			}
960c3229e05SDavid Greenman 		}
961df8bae1dSRodney W. Grimes 		return (match);
962df8bae1dSRodney W. Grimes 	}
963c3229e05SDavid Greenman }
96415bd2b43SDavid Greenman 
96515bd2b43SDavid Greenman /*
96615bd2b43SDavid Greenman  * Lookup PCB in hash list.
96715bd2b43SDavid Greenman  */
96815bd2b43SDavid Greenman struct inpcb *
969136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
970136d4f1cSRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
971136d4f1cSRobert Watson     struct ifnet *ifp)
97215bd2b43SDavid Greenman {
97315bd2b43SDavid Greenman 	struct inpcbhead *head;
974136d4f1cSRobert Watson 	struct inpcb *inp;
97515bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
97615bd2b43SDavid Greenman 
97759daba27SSam Leffler 	INP_INFO_RLOCK_ASSERT(pcbinfo);
97815bd2b43SDavid Greenman 	/*
97915bd2b43SDavid Greenman 	 * First look for an exact match.
98015bd2b43SDavid Greenman 	 */
981ddd79a97SDavid Greenman 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
982fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
983cfa1ca9dSYoshinobu Inoue #ifdef INET6
984369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
985cfa1ca9dSYoshinobu Inoue 			continue;
986cfa1ca9dSYoshinobu Inoue #endif
9876d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
988ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
989ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
990c3229e05SDavid Greenman 		    inp->inp_lport == lport) {
991c3229e05SDavid Greenman 			/*
992c3229e05SDavid Greenman 			 * Found.
993c3229e05SDavid Greenman 			 */
994c3229e05SDavid Greenman 			return (inp);
995c3229e05SDavid Greenman 		}
9966d6a026bSDavid Greenman 	}
9976d6a026bSDavid Greenman 	if (wildcard) {
9986d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
999cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1000cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1001cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10026d6a026bSDavid Greenman 
1003ddd79a97SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1004fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1005cfa1ca9dSYoshinobu Inoue #ifdef INET6
1006369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1007cfa1ca9dSYoshinobu Inoue 				continue;
1008cfa1ca9dSYoshinobu Inoue #endif
10096d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1010c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1011cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
1012cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
1013cfa1ca9dSYoshinobu Inoue 					continue;
10146d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1015c3229e05SDavid Greenman 					return (inp);
1016cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1017cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1018cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1019cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
1020cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
1021cfa1ca9dSYoshinobu Inoue 					else
1022cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10236d6a026bSDavid Greenman 					local_wild = inp;
10246d6a026bSDavid Greenman 				}
10256d6a026bSDavid Greenman 			}
1026cfa1ca9dSYoshinobu Inoue 		}
1027cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1028cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
1029cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
1030cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
1031c3229e05SDavid Greenman 		return (local_wild);
10326d6a026bSDavid Greenman 	}
1033c3229e05SDavid Greenman 
1034c3229e05SDavid Greenman 	/*
1035c3229e05SDavid Greenman 	 * Not found.
1036c3229e05SDavid Greenman 	 */
10376d6a026bSDavid Greenman 	return (NULL);
103815bd2b43SDavid Greenman }
103915bd2b43SDavid Greenman 
10407bc4aca7SDavid Greenman /*
1041c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
10427bc4aca7SDavid Greenman  */
1043c3229e05SDavid Greenman int
1044136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
104515bd2b43SDavid Greenman {
1046c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1047c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1048c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1049c3229e05SDavid Greenman 	struct inpcbport *phd;
1050cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
105115bd2b43SDavid Greenman 
105259daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1053cfa1ca9dSYoshinobu Inoue #ifdef INET6
1054cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1055cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1056cfa1ca9dSYoshinobu Inoue 	else
1057cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1058cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1059cfa1ca9dSYoshinobu Inoue 
1060cfa1ca9dSYoshinobu Inoue 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1061c3229e05SDavid Greenman 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
106215bd2b43SDavid Greenman 
1063c3229e05SDavid Greenman 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1064c3229e05SDavid Greenman 	    pcbinfo->porthashmask)];
1065c3229e05SDavid Greenman 
1066c3229e05SDavid Greenman 	/*
1067c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1068c3229e05SDavid Greenman 	 */
1069fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1070c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1071c3229e05SDavid Greenman 			break;
1072c3229e05SDavid Greenman 	}
1073c3229e05SDavid Greenman 	/*
1074c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1075c3229e05SDavid Greenman 	 */
1076c3229e05SDavid Greenman 	if (phd == NULL) {
1077c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1078c3229e05SDavid Greenman 		if (phd == NULL) {
1079c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1080c3229e05SDavid Greenman 		}
1081c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1082c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1083c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1084c3229e05SDavid Greenman 	}
1085c3229e05SDavid Greenman 	inp->inp_phd = phd;
1086c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1087c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1088c3229e05SDavid Greenman 	return (0);
108915bd2b43SDavid Greenman }
109015bd2b43SDavid Greenman 
1091c3229e05SDavid Greenman /*
1092c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1093c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1094c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1095c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1096c3229e05SDavid Greenman  */
109715bd2b43SDavid Greenman void
1098136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp)
109915bd2b43SDavid Greenman {
110059daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
110115bd2b43SDavid Greenman 	struct inpcbhead *head;
1102cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
110315bd2b43SDavid Greenman 
110459daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
11054c2bb15aSRobert Watson 	INP_LOCK_ASSERT(inp);
1106cfa1ca9dSYoshinobu Inoue #ifdef INET6
1107cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1108cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1109cfa1ca9dSYoshinobu Inoue 	else
1110cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1111cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1112cfa1ca9dSYoshinobu Inoue 
111359daba27SSam Leffler 	head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
111459daba27SSam Leffler 		inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
111515bd2b43SDavid Greenman 
1116c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
111715bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1118c3229e05SDavid Greenman }
1119c3229e05SDavid Greenman 
1120c3229e05SDavid Greenman /*
1121c3229e05SDavid Greenman  * Remove PCB from various lists.
1122c3229e05SDavid Greenman  */
112376429de4SYoshinobu Inoue void
1124136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1125c3229e05SDavid Greenman {
112659daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
112759daba27SSam Leffler 
112859daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
112959daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
113059daba27SSam Leffler 
113159daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1132c3229e05SDavid Greenman 	if (inp->inp_lport) {
1133c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1134c3229e05SDavid Greenman 
1135c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1136c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1137fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1138c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1139c3229e05SDavid Greenman 			free(phd, M_PCB);
1140c3229e05SDavid Greenman 		}
1141c3229e05SDavid Greenman 	}
1142c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
114359daba27SSam Leffler 	pcbinfo->ipi_count--;
114415bd2b43SDavid Greenman }
114575c13541SPoul-Henning Kamp 
1146a557af22SRobert Watson /*
1147a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1148a557af22SRobert Watson  * label change into the in_pcb for the socket.
1149a557af22SRobert Watson  */
1150a557af22SRobert Watson void
1151136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1152a557af22SRobert Watson {
1153a557af22SRobert Watson #ifdef MAC
1154a557af22SRobert Watson 	struct inpcb *inp;
1155a557af22SRobert Watson 
1156a557af22SRobert Watson 	inp = (struct inpcb *)so->so_pcb;
1157a557af22SRobert Watson 	INP_LOCK(inp);
1158310e7cebSRobert Watson 	SOCK_LOCK(so);
1159a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1160310e7cebSRobert Watson 	SOCK_UNLOCK(so);
1161a557af22SRobert Watson 	INP_UNLOCK(inp);
1162a557af22SRobert Watson #endif
1163a557af22SRobert Watson }
11645f311da2SMike Silbersack 
11655f311da2SMike Silbersack /*
11665f311da2SMike Silbersack  * ipport_tick runs once per second, determining if random port
11675f311da2SMike Silbersack  * allocation should be continued.  If more than ipport_randomcps
11685f311da2SMike Silbersack  * ports have been allocated in the last second, then we return to
11695f311da2SMike Silbersack  * sequential port allocation. We return to random allocation only
11706ee79c59SMaxim Konovalov  * once we drop below ipport_randomcps for at least ipport_randomtime
11716ee79c59SMaxim Konovalov  * seconds.
11725f311da2SMike Silbersack  */
11735f311da2SMike Silbersack 
11745f311da2SMike Silbersack void
1175136d4f1cSRobert Watson ipport_tick(void *xtp)
11765f311da2SMike Silbersack {
11775f311da2SMike Silbersack 	if (ipport_tcpallocs > ipport_tcplastcount + ipport_randomcps) {
11785f311da2SMike Silbersack 		ipport_stoprandom = ipport_randomtime;
11795f311da2SMike Silbersack 	} else {
11805f311da2SMike Silbersack 		if (ipport_stoprandom > 0)
11815f311da2SMike Silbersack 			ipport_stoprandom--;
11825f311da2SMike Silbersack 	}
11835f311da2SMike Silbersack 	ipport_tcplastcount = ipport_tcpallocs;
11845f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
11855f311da2SMike Silbersack }
1186