xref: /freebsd/sys/netinet/in_pcb.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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>
39df8bae1dSRodney W. Grimes #include <sys/malloc.h>
40df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
41cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
42df8bae1dSRodney W. Grimes #include <sys/protosw.h>
43df8bae1dSRodney W. Grimes #include <sys/socket.h>
44df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
45acd3428bSRobert Watson #include <sys/priv.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 
83aed55708SRobert Watson #include <security/mac/mac_framework.h>
84aed55708SRobert Watson 
85101f9fc8SPeter Wemm /*
86101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
87101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
88101f9fc8SPeter Wemm  */
8982cd038dSYoshinobu Inoue int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
9082cd038dSYoshinobu Inoue int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
919e5a5ed4SMike Silbersack int	ipport_firstauto = IPPORT_HIFIRSTAUTO;		/* 49152 */
929e5a5ed4SMike Silbersack int	ipport_lastauto  = IPPORT_HILASTAUTO;		/* 65535 */
9382cd038dSYoshinobu Inoue int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
9482cd038dSYoshinobu Inoue int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
95101f9fc8SPeter Wemm 
96b0d22693SCrist J. Clark /*
97b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
98b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
99b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
100b0d22693SCrist J. Clark  */
101b0d22693SCrist J. Clark int	ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
102b0d22693SCrist J. Clark int	ipport_reservedlow = 0;
103b0d22693SCrist J. Clark 
1045f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
1055f311da2SMike Silbersack int	ipport_randomized = 1;	/* user controlled via sysctl */
1065f311da2SMike Silbersack int	ipport_randomcps = 10;	/* user controlled via sysctl */
1075f311da2SMike Silbersack int	ipport_randomtime = 45;	/* user controlled via sysctl */
1085f311da2SMike Silbersack int	ipport_stoprandom = 0;	/* toggled by ipport_tick */
1095f311da2SMike Silbersack int	ipport_tcpallocs;
1105f311da2SMike Silbersack int	ipport_tcplastcount;
1116ac48b74SMike Silbersack 
112bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
113bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
114bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
115bbd42ad0SPeter Wemm 
116bbd42ad0SPeter Wemm static int
11782d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
118bbd42ad0SPeter Wemm {
11930a4ab08SBruce Evans 	int error;
12030a4ab08SBruce Evans 
12130a4ab08SBruce Evans 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
12230a4ab08SBruce Evans 	if (error == 0) {
123bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
124bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
12530a4ab08SBruce Evans 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
12630a4ab08SBruce Evans 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
12730a4ab08SBruce Evans 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
12830a4ab08SBruce Evans 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
129bbd42ad0SPeter Wemm 	}
13030a4ab08SBruce Evans 	return (error);
131bbd42ad0SPeter Wemm }
132bbd42ad0SPeter Wemm 
133bbd42ad0SPeter Wemm #undef RANGECHK
134bbd42ad0SPeter Wemm 
13533b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
13633b3ac06SPeter Wemm 
137bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
138bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
139bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
140bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
141bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
142bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
143bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
144bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
145bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
146bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
147bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
148bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
149b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
150b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
151b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
152b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
1536ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
1546ee79c59SMaxim Konovalov 	   &ipport_randomized, 0, "Enable random port allocation");
1556ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
1566ee79c59SMaxim Konovalov 	   &ipport_randomcps, 0, "Maximum number of random port "
1576ee79c59SMaxim Konovalov 	   "allocations before switching to a sequental one");
1586ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
1596ee79c59SMaxim Konovalov 	   &ipport_randomtime, 0, "Minimum time to keep sequental port "
1606ee79c59SMaxim Konovalov 	   "allocation before switching to a random one");
1610312fbe9SPoul-Henning Kamp 
162c3229e05SDavid Greenman /*
163c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
164c3229e05SDavid Greenman  *
165de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
166de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
167de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
168c3229e05SDavid Greenman  */
169c3229e05SDavid Greenman 
170c3229e05SDavid Greenman /*
171c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
172d915b280SStephan Uphoff  * On success return with the PCB locked.
173c3229e05SDavid Greenman  */
174df8bae1dSRodney W. Grimes int
175d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
176df8bae1dSRodney W. Grimes {
177136d4f1cSRobert Watson 	struct inpcb *inp;
17813cf67f3SHajimu UMEMOTO 	int error;
179a557af22SRobert Watson 
18059daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
181a557af22SRobert Watson 	error = 0;
182d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
183df8bae1dSRodney W. Grimes 	if (inp == NULL)
184df8bae1dSRodney W. Grimes 		return (ENOBUFS);
185d915b280SStephan Uphoff 	bzero(inp,inp_zero_size);
18615bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
187df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
188a557af22SRobert Watson #ifdef MAC
189a557af22SRobert Watson 	error = mac_init_inpcb(inp, M_NOWAIT);
190a557af22SRobert Watson 	if (error != 0)
191a557af22SRobert Watson 		goto out;
192310e7cebSRobert Watson 	SOCK_LOCK(so);
193a557af22SRobert Watson 	mac_create_inpcb_from_socket(so, inp);
194310e7cebSRobert Watson 	SOCK_UNLOCK(so);
195a557af22SRobert Watson #endif
1960f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
1970f9ade71SHajimu UMEMOTO #ifdef FAST_IPSEC
19813cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
1990f9ade71SHajimu UMEMOTO #else
2000f9ade71SHajimu UMEMOTO 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
2010f9ade71SHajimu UMEMOTO #endif
202a557af22SRobert Watson 	if (error != 0)
203a557af22SRobert Watson 		goto out;
20413cf67f3SHajimu UMEMOTO #endif /*IPSEC*/
20575daea93SPaul Saab #if defined(INET6)
206340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
207340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
208340c35deSJonathan Lemon 		if (ip6_v6only)
20933841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
210340c35deSJonathan Lemon 	}
21175daea93SPaul Saab #endif
21215bd2b43SDavid Greenman 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
2133d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
214df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
21533841545SHajimu UMEMOTO #ifdef INET6
21633841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
21733841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
21833841545SHajimu UMEMOTO #endif
219d915b280SStephan Uphoff 	INP_LOCK(inp);
220d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
221d915b280SStephan Uphoff 
222a557af22SRobert Watson #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
223a557af22SRobert Watson out:
224a557af22SRobert Watson 	if (error != 0)
225a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
226a557af22SRobert Watson #endif
227a557af22SRobert Watson 	return (error);
228df8bae1dSRodney W. Grimes }
229df8bae1dSRodney W. Grimes 
230df8bae1dSRodney W. Grimes int
231136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
232df8bae1dSRodney W. Grimes {
2334b932371SIan Dowse 	int anonport, error;
2344b932371SIan Dowse 
2351b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
23659daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
23759daba27SSam Leffler 
2384b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2394b932371SIan Dowse 		return (EINVAL);
2404b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2414b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2424b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
243b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
2444b932371SIan Dowse 	if (error)
2454b932371SIan Dowse 		return (error);
2464b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2474b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2484b932371SIan Dowse 		inp->inp_lport = 0;
2494b932371SIan Dowse 		return (EAGAIN);
2504b932371SIan Dowse 	}
2514b932371SIan Dowse 	if (anonport)
2524b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2534b932371SIan Dowse 	return (0);
2544b932371SIan Dowse }
2554b932371SIan Dowse 
2564b932371SIan Dowse /*
2574b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2584b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2594b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2604b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2614b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2624b932371SIan Dowse  *
2634b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2644b932371SIan Dowse  */
2654b932371SIan Dowse int
266136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
267136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
2684b932371SIan Dowse {
2694b932371SIan Dowse 	struct socket *so = inp->inp_socket;
27037bd2b30SPeter Wemm 	unsigned short *lastport;
27115bd2b43SDavid Greenman 	struct sockaddr_in *sin;
272c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2734b932371SIan Dowse 	struct in_addr laddr;
274df8bae1dSRodney W. Grimes 	u_short lport = 0;
2754cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
27675c13541SPoul-Henning Kamp 	int error, prison = 0;
2775f311da2SMike Silbersack 	int dorandom;
278df8bae1dSRodney W. Grimes 
2791b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
28059daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
28159daba27SSam Leffler 
28259562606SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
283df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
2844b932371SIan Dowse 	laddr.s_addr = *laddrp;
2854b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
286df8bae1dSRodney W. Grimes 		return (EINVAL);
287c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
288421d8aa6SBjoern A. Zeeb 		wild = INPLOOKUP_WILDCARD;
289df8bae1dSRodney W. Grimes 	if (nam) {
29057bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
29157bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
292df8bae1dSRodney W. Grimes 			return (EINVAL);
293df8bae1dSRodney W. Grimes #ifdef notdef
294df8bae1dSRodney W. Grimes 		/*
295df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
296df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
297df8bae1dSRodney W. Grimes 		 */
298df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
299df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
300df8bae1dSRodney W. Grimes #endif
301e4bdf25dSPoul-Henning Kamp 		if (sin->sin_addr.s_addr != INADDR_ANY)
302b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
30375c13541SPoul-Henning Kamp 				return(EINVAL);
3044b932371SIan Dowse 		if (sin->sin_port != *lportp) {
3054b932371SIan Dowse 			/* Don't allow the port to change. */
3064b932371SIan Dowse 			if (*lportp != 0)
3074b932371SIan Dowse 				return (EINVAL);
308df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
3094b932371SIan Dowse 		}
3104b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
311df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
312df8bae1dSRodney W. Grimes 			/*
313df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
314df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
315df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
316df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
317df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
318df8bae1dSRodney W. Grimes 			 */
319df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
320df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
321df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
322df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
32383103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
324df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
325df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
326df8bae1dSRodney W. Grimes 		}
3274b932371SIan Dowse 		laddr = sin->sin_addr;
328df8bae1dSRodney W. Grimes 		if (lport) {
329df8bae1dSRodney W. Grimes 			struct inpcb *t;
330ae0e7143SRobert Watson 			struct tcptw *tw;
331ae0e7143SRobert Watson 
332df8bae1dSRodney W. Grimes 			/* GROSS */
333b0d22693SCrist J. Clark 			if (ntohs(lport) <= ipport_reservedhigh &&
334b0d22693SCrist J. Clark 			    ntohs(lport) >= ipport_reservedlow &&
335acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
336acd3428bSRobert Watson 			    SUSER_ALLOWJAIL))
3372469dd60SGarrett Wollman 				return (EACCES);
338b0330ed9SPawel Jakub Dawidek 			if (jailed(cred))
33975c13541SPoul-Henning Kamp 				prison = 1;
340835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
341835d4b89SPawel Jakub Dawidek 			    suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
3424049a042SGuido van Rooij 				t = in_pcblookup_local(inp->inp_pcbinfo,
34375c13541SPoul-Henning Kamp 				    sin->sin_addr, lport,
34475c13541SPoul-Henning Kamp 				    prison ? 0 :  INPLOOKUP_WILDCARD);
345340c35deSJonathan Lemon 	/*
346340c35deSJonathan Lemon 	 * XXX
347340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
348340c35deSJonathan Lemon 	 */
3494cc20ab1SSeigo Tanimura 				if (t &&
3504658dc83SYaroslav Tykhiy 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
3514658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
3524658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
3534cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
35452b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
35552b65dbeSBill Fenner 				     (t->inp_socket->so_options &
35652b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
3572f9a2132SBrian Feldman 				    (so->so_cred->cr_uid !=
358a4eb4405SYaroslav Tykhiy 				     t->inp_socket->so_cred->cr_uid))
3594049a042SGuido van Rooij 					return (EADDRINUSE);
3604049a042SGuido van Rooij 			}
361b0330ed9SPawel Jakub Dawidek 			if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
362970680faSPoul-Henning Kamp 				return (EADDRNOTAVAIL);
363c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
36475c13541SPoul-Henning Kamp 			    lport, prison ? 0 : wild);
365340c35deSJonathan Lemon 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
366ae0e7143SRobert Watson 				/*
367ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
368ae0e7143SRobert Watson 				 * state recycled, we treat the address as
369ae0e7143SRobert Watson 				 * being in use (for now).  This is better
370ae0e7143SRobert Watson 				 * than a panic, but not desirable.
371ae0e7143SRobert Watson 				 */
372ae0e7143SRobert Watson 				tw = intotw(inp);
373ae0e7143SRobert Watson 				if (tw == NULL ||
374ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
375340c35deSJonathan Lemon 					return (EADDRINUSE);
376ae0e7143SRobert Watson 			} else if (t &&
3774cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
378cfa1ca9dSYoshinobu Inoue #if defined(INET6)
37933841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
380cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
381cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
382cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
383cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
384cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
385cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
386df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
387df8bae1dSRodney W. Grimes 			}
388cfa1ca9dSYoshinobu Inoue 		}
389df8bae1dSRodney W. Grimes 	}
3904b932371SIan Dowse 	if (*lportp != 0)
3914b932371SIan Dowse 		lport = *lportp;
39233b3ac06SPeter Wemm 	if (lport == 0) {
3936ac48b74SMike Silbersack 		u_short first, last;
394174624e0SMike Silbersack 		int count;
39533b3ac06SPeter Wemm 
3964b932371SIan Dowse 		if (laddr.s_addr != INADDR_ANY)
397b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &laddr.s_addr))
39875c13541SPoul-Henning Kamp 				return (EINVAL);
399321a2846SPoul-Henning Kamp 
40033b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
40133b3ac06SPeter Wemm 			first = ipport_hifirstauto;	/* sysctl */
40233b3ac06SPeter Wemm 			last  = ipport_hilastauto;
403c3229e05SDavid Greenman 			lastport = &pcbinfo->lasthi;
40433b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
405acd3428bSRobert Watson 			error = priv_check_cred(cred,
406acd3428bSRobert Watson 			    PRIV_NETINET_RESERVEDPORT, SUSER_ALLOWJAIL);
407acd3428bSRobert Watson 			if (error)
408a29f300eSGarrett Wollman 				return error;
409bbd42ad0SPeter Wemm 			first = ipport_lowfirstauto;	/* 1023 */
410bbd42ad0SPeter Wemm 			last  = ipport_lowlastauto;	/* 600 */
411c3229e05SDavid Greenman 			lastport = &pcbinfo->lastlow;
41233b3ac06SPeter Wemm 		} else {
41333b3ac06SPeter Wemm 			first = ipport_firstauto;	/* sysctl */
41433b3ac06SPeter Wemm 			last  = ipport_lastauto;
415c3229e05SDavid Greenman 			lastport = &pcbinfo->lastport;
41633b3ac06SPeter Wemm 		}
41733b3ac06SPeter Wemm 		/*
4185f311da2SMike Silbersack 		 * For UDP, use random port allocation as long as the user
4195f311da2SMike Silbersack 		 * allows it.  For TCP (and as of yet unknown) connections,
4205f311da2SMike Silbersack 		 * use random port allocation only if the user allows it AND
42129f2a6ecSMaxim Konovalov 		 * ipport_tick() allows it.
4225f311da2SMike Silbersack 		 */
4235f311da2SMike Silbersack 		if (ipport_randomized &&
4245f311da2SMike Silbersack 			(!ipport_stoprandom || pcbinfo == &udbinfo))
4255f311da2SMike Silbersack 			dorandom = 1;
4265f311da2SMike Silbersack 		else
4275f311da2SMike Silbersack 			dorandom = 0;
428e99971bfSMaxim Konovalov 		/*
429e99971bfSMaxim Konovalov 		 * It makes no sense to do random port allocation if
430e99971bfSMaxim Konovalov 		 * we have the only port available.
431e99971bfSMaxim Konovalov 		 */
432e99971bfSMaxim Konovalov 		if (first == last)
433e99971bfSMaxim Konovalov 			dorandom = 0;
4345f311da2SMike Silbersack 		/* Make sure to not include UDP packets in the count. */
4355f311da2SMike Silbersack 		if (pcbinfo != &udbinfo)
4365f311da2SMike Silbersack 			ipport_tcpallocs++;
4375f311da2SMike Silbersack 		/*
43833b3ac06SPeter Wemm 		 * Simple check to ensure all ports are not used up causing
43933b3ac06SPeter Wemm 		 * a deadlock here.
44033b3ac06SPeter Wemm 		 *
44133b3ac06SPeter Wemm 		 * We split the two cases (up and down) so that the direction
44233b3ac06SPeter Wemm 		 * is not being tested on each round of the loop.
44333b3ac06SPeter Wemm 		 */
44433b3ac06SPeter Wemm 		if (first > last) {
44533b3ac06SPeter Wemm 			/*
44633b3ac06SPeter Wemm 			 * counting down
44733b3ac06SPeter Wemm 			 */
4485f311da2SMike Silbersack 			if (dorandom)
4496b2fc10bSMike Silbersack 				*lastport = first -
4506b2fc10bSMike Silbersack 					    (arc4random() % (first - last));
45133b3ac06SPeter Wemm 			count = first - last;
452174624e0SMike Silbersack 
453df8bae1dSRodney W. Grimes 			do {
4546ac48b74SMike Silbersack 				if (count-- < 0)	/* completely used? */
455550b1518SWes Peters 					return (EADDRNOTAVAIL);
45633b3ac06SPeter Wemm 				--*lastport;
45733b3ac06SPeter Wemm 				if (*lastport > first || *lastport < last)
45833b3ac06SPeter Wemm 					*lastport = first;
45915bd2b43SDavid Greenman 				lport = htons(*lastport);
4604b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4614b932371SIan Dowse 			    wild));
46233b3ac06SPeter Wemm 		} else {
46333b3ac06SPeter Wemm 			/*
46433b3ac06SPeter Wemm 			 * counting up
46533b3ac06SPeter Wemm 			 */
4665f311da2SMike Silbersack 			if (dorandom)
4676b2fc10bSMike Silbersack 				*lastport = first +
4686b2fc10bSMike Silbersack 					    (arc4random() % (last - first));
46933b3ac06SPeter Wemm 			count = last - first;
470174624e0SMike Silbersack 
47133b3ac06SPeter Wemm 			do {
4726ac48b74SMike Silbersack 				if (count-- < 0)	/* completely used? */
473550b1518SWes Peters 					return (EADDRNOTAVAIL);
47433b3ac06SPeter Wemm 				++*lastport;
47533b3ac06SPeter Wemm 				if (*lastport < first || *lastport > last)
47633b3ac06SPeter Wemm 					*lastport = first;
47733b3ac06SPeter Wemm 				lport = htons(*lastport);
4784b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4794b932371SIan Dowse 			    wild));
48033b3ac06SPeter Wemm 		}
48133b3ac06SPeter Wemm 	}
482b0330ed9SPawel Jakub Dawidek 	if (prison_ip(cred, 0, &laddr.s_addr))
483e4bdf25dSPoul-Henning Kamp 		return (EINVAL);
4844b932371SIan Dowse 	*laddrp = laddr.s_addr;
4854b932371SIan Dowse 	*lportp = lport;
486df8bae1dSRodney W. Grimes 	return (0);
487df8bae1dSRodney W. Grimes }
488df8bae1dSRodney W. Grimes 
489999f1343SGarrett Wollman /*
4905200e00eSIan Dowse  * Connect from a socket to a specified address.
4915200e00eSIan Dowse  * Both address and port must be specified in argument sin.
4925200e00eSIan Dowse  * If don't have a local address for this socket yet,
4935200e00eSIan Dowse  * then pick one.
494999f1343SGarrett Wollman  */
495999f1343SGarrett Wollman int
496136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
497999f1343SGarrett Wollman {
4985200e00eSIan Dowse 	u_short lport, fport;
4995200e00eSIan Dowse 	in_addr_t laddr, faddr;
5005200e00eSIan Dowse 	int anonport, error;
501df8bae1dSRodney W. Grimes 
50227f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
50327f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
50427f74fd0SRobert Watson 
5055200e00eSIan Dowse 	lport = inp->inp_lport;
5065200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
5075200e00eSIan Dowse 	anonport = (lport == 0);
5085200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
509b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
5105200e00eSIan Dowse 	if (error)
5115200e00eSIan Dowse 		return (error);
5125200e00eSIan Dowse 
5135200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
5145200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
5155200e00eSIan Dowse 		inp->inp_lport = lport;
5165200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
5175200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
5185200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
5195200e00eSIan Dowse 			inp->inp_lport = 0;
5205200e00eSIan Dowse 			return (EAGAIN);
5215200e00eSIan Dowse 		}
5225200e00eSIan Dowse 	}
5235200e00eSIan Dowse 
5245200e00eSIan Dowse 	/* Commit the remaining changes. */
5255200e00eSIan Dowse 	inp->inp_lport = lport;
5265200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
5275200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
5285200e00eSIan Dowse 	inp->inp_fport = fport;
5295200e00eSIan Dowse 	in_pcbrehash(inp);
5300f9ade71SHajimu UMEMOTO #ifdef IPSEC
5310f9ade71SHajimu UMEMOTO 	if (inp->inp_socket->so_type == SOCK_STREAM)
5320f9ade71SHajimu UMEMOTO 		ipsec_pcbconn(inp->inp_sp);
5330f9ade71SHajimu UMEMOTO #endif
5345200e00eSIan Dowse 	if (anonport)
5355200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
5365200e00eSIan Dowse 	return (0);
5375200e00eSIan Dowse }
5385200e00eSIan Dowse 
5395200e00eSIan Dowse /*
5405200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
5415200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
5425200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
5435200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
5445200e00eSIan Dowse  * the connect.
5455200e00eSIan Dowse  *
5465200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
5475200e00eSIan Dowse  * and port. These are not updated in the error case.
5485200e00eSIan Dowse  *
5495200e00eSIan Dowse  * If the operation fails because the connection already exists,
5505200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
5515200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
5525200e00eSIan Dowse  * is set to NULL.
5535200e00eSIan Dowse  */
5545200e00eSIan Dowse int
555136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
556136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
557136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
5585200e00eSIan Dowse {
5595200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
5605200e00eSIan Dowse 	struct in_ifaddr *ia;
5615200e00eSIan Dowse 	struct sockaddr_in sa;
562b0330ed9SPawel Jakub Dawidek 	struct ucred *socred;
5635200e00eSIan Dowse 	struct inpcb *oinp;
5645200e00eSIan Dowse 	struct in_addr laddr, faddr;
5655200e00eSIan Dowse 	u_short lport, fport;
5665200e00eSIan Dowse 	int error;
5675200e00eSIan Dowse 
56827f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
56927f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
57027f74fd0SRobert Watson 
5715200e00eSIan Dowse 	if (oinpp != NULL)
5725200e00eSIan Dowse 		*oinpp = NULL;
57357bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
574df8bae1dSRodney W. Grimes 		return (EINVAL);
575df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
576df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
577df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
578df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5795200e00eSIan Dowse 	laddr.s_addr = *laddrp;
5805200e00eSIan Dowse 	lport = *lportp;
5815200e00eSIan Dowse 	faddr = sin->sin_addr;
5825200e00eSIan Dowse 	fport = sin->sin_port;
583b0330ed9SPawel Jakub Dawidek 	socred = inp->inp_socket->so_cred;
584b0330ed9SPawel Jakub Dawidek 	if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
5855200e00eSIan Dowse 		bzero(&sa, sizeof(sa));
586b0330ed9SPawel Jakub Dawidek 		sa.sin_addr.s_addr = htonl(prison_getip(socred));
5875200e00eSIan Dowse 		sa.sin_len = sizeof(sa);
5885200e00eSIan Dowse 		sa.sin_family = AF_INET;
5895200e00eSIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
590b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, cred);
5915200e00eSIan Dowse 		if (error)
5925200e00eSIan Dowse 			return (error);
5935200e00eSIan Dowse 	}
59459562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
595df8bae1dSRodney W. Grimes 		/*
596df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
597df8bae1dSRodney W. Grimes 		 * use the primary local address.
598df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
599df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
600df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
601df8bae1dSRodney W. Grimes 		 */
6025200e00eSIan Dowse 		if (faddr.s_addr == INADDR_ANY)
6035200e00eSIan Dowse 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
6045200e00eSIan Dowse 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
6055200e00eSIan Dowse 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
6065200e00eSIan Dowse 		    IFF_BROADCAST))
6075200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
6085200e00eSIan Dowse 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
609df8bae1dSRodney W. Grimes 	}
6105200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
611df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
612df8bae1dSRodney W. Grimes 		/*
61397d8d152SAndre Oppermann 		 * If route is known our src addr is taken from the i/f,
61497d8d152SAndre Oppermann 		 * else punt.
615cf744713SAndre Oppermann 		 *
616cf744713SAndre Oppermann 		 * Find out route to destination
617df8bae1dSRodney W. Grimes 		 */
618cf744713SAndre Oppermann 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
619cf744713SAndre Oppermann 			ia = ip_rtaddr(faddr);
620df8bae1dSRodney W. Grimes 		/*
621cf744713SAndre Oppermann 		 * If we found a route, use the address corresponding to
622cf744713SAndre Oppermann 		 * the outgoing interface.
623cf744713SAndre Oppermann 		 *
624cf744713SAndre Oppermann 		 * Otherwise assume faddr is reachable on a directly connected
625cf744713SAndre Oppermann 		 * network and try to find a corresponding interface to take
626cf744713SAndre Oppermann 		 * the source address from.
627df8bae1dSRodney W. Grimes 		 */
628df8bae1dSRodney W. Grimes 		if (ia == 0) {
6295200e00eSIan Dowse 			bzero(&sa, sizeof(sa));
6305200e00eSIan Dowse 			sa.sin_addr = faddr;
6315200e00eSIan Dowse 			sa.sin_len = sizeof(sa);
6325200e00eSIan Dowse 			sa.sin_family = AF_INET;
633df8bae1dSRodney W. Grimes 
6345200e00eSIan Dowse 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
635df8bae1dSRodney W. Grimes 			if (ia == 0)
6365200e00eSIan Dowse 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
637df8bae1dSRodney W. Grimes 			if (ia == 0)
638ef14c369SMaxim Konovalov 				return (ENETUNREACH);
639df8bae1dSRodney W. Grimes 		}
640df8bae1dSRodney W. Grimes 		/*
641df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
642df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
643df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
644df8bae1dSRodney W. Grimes 		 */
6455200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
646df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
647df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
648df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
651df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
652df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
65337d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
654df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
655df8bae1dSRodney W. Grimes 						break;
656df8bae1dSRodney W. Grimes 				if (ia == 0)
657df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
658df8bae1dSRodney W. Grimes 			}
659df8bae1dSRodney W. Grimes 		}
6605200e00eSIan Dowse 		laddr = ia->ia_addr.sin_addr;
661999f1343SGarrett Wollman 	}
662999f1343SGarrett Wollman 
6635200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
6645200e00eSIan Dowse 	    0, NULL);
6655200e00eSIan Dowse 	if (oinp != NULL) {
6665200e00eSIan Dowse 		if (oinpp != NULL)
6675200e00eSIan Dowse 			*oinpp = oinp;
668df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
669c3229e05SDavid Greenman 	}
6705200e00eSIan Dowse 	if (lport == 0) {
671b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
672b0330ed9SPawel Jakub Dawidek 		    cred);
6735a903f8dSPierre Beyssac 		if (error)
6745a903f8dSPierre Beyssac 			return (error);
6755a903f8dSPierre Beyssac 	}
6765200e00eSIan Dowse 	*laddrp = laddr.s_addr;
6775200e00eSIan Dowse 	*lportp = lport;
6785200e00eSIan Dowse 	*faddrp = faddr.s_addr;
6795200e00eSIan Dowse 	*fportp = fport;
680df8bae1dSRodney W. Grimes 	return (0);
681df8bae1dSRodney W. Grimes }
682df8bae1dSRodney W. Grimes 
68326f9a767SRodney W. Grimes void
684136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
685df8bae1dSRodney W. Grimes {
6866b348152SRobert Watson 
687fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
68859daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
691df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
69215bd2b43SDavid Greenman 	in_pcbrehash(inp);
6930f9ade71SHajimu UMEMOTO #ifdef IPSEC
6940f9ade71SHajimu UMEMOTO 	ipsec_pcbdisconn(inp->inp_sp);
6950f9ade71SHajimu UMEMOTO #endif
696df8bae1dSRodney W. Grimes }
697df8bae1dSRodney W. Grimes 
6984c7c478dSRobert Watson /*
6994c7c478dSRobert Watson  * In the old world order, in_pcbdetach() served two functions: to detach the
7004c7c478dSRobert Watson  * pcb from the socket/potentially free the socket, and to free the pcb
7014c7c478dSRobert Watson  * itself.  In the new world order, the protocol code is responsible for
7024c7c478dSRobert Watson  * managing the relationship with the socket, and this code simply frees the
7034c7c478dSRobert Watson  * pcb.
7044c7c478dSRobert Watson  */
70526f9a767SRodney W. Grimes void
706136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
707df8bae1dSRodney W. Grimes {
7084c7c478dSRobert Watson 
7094c7c478dSRobert Watson 	KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
7104c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
7114c7c478dSRobert Watson 	inp->inp_socket = NULL;
7124c7c478dSRobert Watson }
7134c7c478dSRobert Watson 
7144c7c478dSRobert Watson void
7154c7c478dSRobert Watson in_pcbfree(struct inpcb *inp)
7164c7c478dSRobert Watson {
7173d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
718df8bae1dSRodney W. Grimes 
7194c7c478dSRobert Watson 	KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
720fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
72159daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
72259daba27SSam Leffler 
7230f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
724cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
725cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
7263d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
727c3229e05SDavid Greenman 	in_pcbremlists(inp);
728df8bae1dSRodney W. Grimes 	if (inp->inp_options)
729df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
730df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
731cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
732d915b280SStephan Uphoff 
733a557af22SRobert Watson #ifdef MAC
734a557af22SRobert Watson 	mac_destroy_inpcb(inp);
735a557af22SRobert Watson #endif
736d915b280SStephan Uphoff 	INP_UNLOCK(inp);
73769c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
738df8bae1dSRodney W. Grimes }
739df8bae1dSRodney W. Grimes 
74010702a28SRobert Watson /*
74110702a28SRobert Watson  * TCP needs to maintain its inpcb structure after the TCP connection has
74210702a28SRobert Watson  * been torn down.  However, it must be disconnected from the inpcb hashes as
74310702a28SRobert Watson  * it must not prevent binding of future connections to the same port/ip
74410702a28SRobert Watson  * combination by other inpcbs.
74510702a28SRobert Watson  */
74610702a28SRobert Watson void
74710702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
74810702a28SRobert Watson {
74910702a28SRobert Watson 
7507c5a8ab2SMarcel Moolenaar 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
75110702a28SRobert Watson 	INP_LOCK_ASSERT(inp);
75210702a28SRobert Watson 
75310702a28SRobert Watson 	inp->inp_vflag |= INP_DROPPED;
75410702a28SRobert Watson 	if (inp->inp_lport) {
75510702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
75610702a28SRobert Watson 
75710702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
75810702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
75910702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
76010702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
76110702a28SRobert Watson 			free(phd, M_PCB);
76210702a28SRobert Watson 		}
76310702a28SRobert Watson 		inp->inp_lport = 0;
76410702a28SRobert Watson 	}
76510702a28SRobert Watson }
76610702a28SRobert Watson 
76726ef6ac4SDon Lewis struct sockaddr *
768136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
76926ef6ac4SDon Lewis {
77026ef6ac4SDon Lewis 	struct sockaddr_in *sin;
77126ef6ac4SDon Lewis 
77226ef6ac4SDon Lewis 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
773a163d034SWarner Losh 		M_WAITOK | M_ZERO);
77426ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
77526ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
77626ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
77726ef6ac4SDon Lewis 	sin->sin_port = port;
77826ef6ac4SDon Lewis 
77926ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
78026ef6ac4SDon Lewis }
78126ef6ac4SDon Lewis 
782117bcae7SGarrett Wollman /*
783f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
784f76fcf6dSJeffrey Hsu  * The socket must have a valid
785117bcae7SGarrett Wollman  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
786117bcae7SGarrett Wollman  * except through a kernel programming error, so it is acceptable to panic
78757bf258eSGarrett Wollman  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
78857bf258eSGarrett Wollman  * because there actually /is/ a programming error somewhere... XXX)
789117bcae7SGarrett Wollman  */
790117bcae7SGarrett Wollman int
791136d4f1cSRobert Watson in_setsockaddr(struct socket *so, struct sockaddr **nam,
792136d4f1cSRobert Watson     struct inpcbinfo *pcbinfo)
793df8bae1dSRodney W. Grimes {
794136d4f1cSRobert Watson 	struct inpcb *inp;
79526ef6ac4SDon Lewis 	struct in_addr addr;
79626ef6ac4SDon Lewis 	in_port_t port;
79742fa505bSDavid Greenman 
798fdc984f7STor Egge 	inp = sotoinpcb(so);
7996466b28aSRobert Watson 	KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
8006466b28aSRobert Watson 
801f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
80226ef6ac4SDon Lewis 	port = inp->inp_lport;
80326ef6ac4SDon Lewis 	addr = inp->inp_laddr;
804f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
80542fa505bSDavid Greenman 
80626ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
807117bcae7SGarrett Wollman 	return 0;
808df8bae1dSRodney W. Grimes }
809df8bae1dSRodney W. Grimes 
810f76fcf6dSJeffrey Hsu /*
811f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
812f76fcf6dSJeffrey Hsu  */
813117bcae7SGarrett Wollman int
814136d4f1cSRobert Watson in_setpeeraddr(struct socket *so, struct sockaddr **nam,
815136d4f1cSRobert Watson     struct inpcbinfo *pcbinfo)
816df8bae1dSRodney W. Grimes {
817136d4f1cSRobert Watson 	struct inpcb *inp;
81826ef6ac4SDon Lewis 	struct in_addr addr;
81926ef6ac4SDon Lewis 	in_port_t port;
82042fa505bSDavid Greenman 
821fdc984f7STor Egge 	inp = sotoinpcb(so);
8226466b28aSRobert Watson 	KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
8236466b28aSRobert Watson 
824f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
82526ef6ac4SDon Lewis 	port = inp->inp_fport;
82626ef6ac4SDon Lewis 	addr = inp->inp_faddr;
827f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
82842fa505bSDavid Greenman 
82926ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
830117bcae7SGarrett Wollman 	return 0;
831df8bae1dSRodney W. Grimes }
832df8bae1dSRodney W. Grimes 
83326f9a767SRodney W. Grimes void
834136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
835136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
836d1c54148SJesper Skriver {
837c693a045SJonathan Lemon 	struct inpcb *inp, *ninp;
838f76fcf6dSJeffrey Hsu 	struct inpcbhead *head;
839d1c54148SJesper Skriver 
8403dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
841f76fcf6dSJeffrey Hsu 	head = pcbinfo->listhead;
842c693a045SJonathan Lemon 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
843f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
844c693a045SJonathan Lemon 		ninp = LIST_NEXT(inp, inp_list);
845d1c54148SJesper Skriver #ifdef INET6
846f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
847f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
848d1c54148SJesper Skriver 			continue;
849f76fcf6dSJeffrey Hsu 		}
850d1c54148SJesper Skriver #endif
851d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
852f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
853f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
854d1c54148SJesper Skriver 			continue;
855d1c54148SJesper Skriver 		}
8563dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
857f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
858f76fcf6dSJeffrey Hsu 	}
8593dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
860d1c54148SJesper Skriver }
861d1c54148SJesper Skriver 
862e43cc4aeSHajimu UMEMOTO void
863136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
864e43cc4aeSHajimu UMEMOTO {
865e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
866e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
867e43cc4aeSHajimu UMEMOTO 	int i, gap;
868e43cc4aeSHajimu UMEMOTO 
869f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
8703cfcc388SJeffrey Hsu 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
871f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
872e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
873e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
874e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
875e43cc4aeSHajimu UMEMOTO 			/*
876e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
877e43cc4aeSHajimu UMEMOTO 			 * detached.
878e43cc4aeSHajimu UMEMOTO 			 */
879e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
880e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
881e43cc4aeSHajimu UMEMOTO 
882e43cc4aeSHajimu UMEMOTO 			/*
883e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
884e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
885e43cc4aeSHajimu UMEMOTO 			 */
886e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
887e43cc4aeSHajimu UMEMOTO 			    i++) {
888e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
889e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
890e43cc4aeSHajimu UMEMOTO 					gap++;
891e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
892e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
893e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
894e43cc4aeSHajimu UMEMOTO 			}
895e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
896e43cc4aeSHajimu UMEMOTO 		}
897f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
898e43cc4aeSHajimu UMEMOTO 	}
8993cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
900e43cc4aeSHajimu UMEMOTO }
901e43cc4aeSHajimu UMEMOTO 
902df8bae1dSRodney W. Grimes /*
903c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
904c3229e05SDavid Greenman  */
905d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
906df8bae1dSRodney W. Grimes struct inpcb *
907136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
908136d4f1cSRobert Watson     u_int lport_arg, int wild_okay)
909df8bae1dSRodney W. Grimes {
910136d4f1cSRobert Watson 	struct inpcb *inp;
911d5e8a67eSHajimu UMEMOTO #ifdef INET6
912d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
913d5e8a67eSHajimu UMEMOTO #else
914d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
915d5e8a67eSHajimu UMEMOTO #endif
916d5e8a67eSHajimu UMEMOTO 	int wildcard;
917c3229e05SDavid Greenman 	u_short lport = lport_arg;
9187bc4aca7SDavid Greenman 
9191b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
9201b73ca0bSSam Leffler 
921c3229e05SDavid Greenman 	if (!wild_okay) {
922c3229e05SDavid Greenman 		struct inpcbhead *head;
923c3229e05SDavid Greenman 		/*
924c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
925c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
926c3229e05SDavid Greenman 		 */
927c3229e05SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
928fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
929cfa1ca9dSYoshinobu Inoue #ifdef INET6
930369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
931cfa1ca9dSYoshinobu Inoue 				continue;
932cfa1ca9dSYoshinobu Inoue #endif
933c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
934c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
935c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
936c3229e05SDavid Greenman 				/*
937c3229e05SDavid Greenman 				 * Found.
938c3229e05SDavid Greenman 				 */
939c3229e05SDavid Greenman 				return (inp);
940df8bae1dSRodney W. Grimes 			}
941c3229e05SDavid Greenman 		}
942c3229e05SDavid Greenman 		/*
943c3229e05SDavid Greenman 		 * Not found.
944c3229e05SDavid Greenman 		 */
945c3229e05SDavid Greenman 		return (NULL);
946c3229e05SDavid Greenman 	} else {
947c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
948c3229e05SDavid Greenman 		struct inpcbport *phd;
949c3229e05SDavid Greenman 		struct inpcb *match = NULL;
950c3229e05SDavid Greenman 		/*
951c3229e05SDavid Greenman 		 * Best fit PCB lookup.
952c3229e05SDavid Greenman 		 *
953c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
954c3229e05SDavid Greenman 		 * port hash list.
955c3229e05SDavid Greenman 		 */
956c3229e05SDavid Greenman 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
957c3229e05SDavid Greenman 		    pcbinfo->porthashmask)];
958fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
959c3229e05SDavid Greenman 			if (phd->phd_port == lport)
960c3229e05SDavid Greenman 				break;
961c3229e05SDavid Greenman 		}
962c3229e05SDavid Greenman 		if (phd != NULL) {
963c3229e05SDavid Greenman 			/*
964c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
965c3229e05SDavid Greenman 			 * fit.
966c3229e05SDavid Greenman 			 */
96737d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
968c3229e05SDavid Greenman 				wildcard = 0;
969cfa1ca9dSYoshinobu Inoue #ifdef INET6
970369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
971cfa1ca9dSYoshinobu Inoue 					continue;
972d5e8a67eSHajimu UMEMOTO 				/*
973d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
974d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
975d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
976d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
977d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
978d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
979d5e8a67eSHajimu UMEMOTO 				 *
980d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
981d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
982d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
983d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
984d5e8a67eSHajimu UMEMOTO 				 */
985d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
986d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
987cfa1ca9dSYoshinobu Inoue #endif
988c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
989c3229e05SDavid Greenman 					wildcard++;
99015bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
99115bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
99215bd2b43SDavid Greenman 						wildcard++;
99315bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
99415bd2b43SDavid Greenman 						continue;
99515bd2b43SDavid Greenman 				} else {
99615bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
99715bd2b43SDavid Greenman 						wildcard++;
99815bd2b43SDavid Greenman 				}
999df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1000df8bae1dSRodney W. Grimes 					match = inp;
1001df8bae1dSRodney W. Grimes 					matchwild = wildcard;
10023dbdc25cSDavid Greenman 					if (matchwild == 0) {
1003df8bae1dSRodney W. Grimes 						break;
1004df8bae1dSRodney W. Grimes 					}
1005df8bae1dSRodney W. Grimes 				}
10063dbdc25cSDavid Greenman 			}
1007c3229e05SDavid Greenman 		}
1008df8bae1dSRodney W. Grimes 		return (match);
1009df8bae1dSRodney W. Grimes 	}
1010c3229e05SDavid Greenman }
1011d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
101215bd2b43SDavid Greenman 
101315bd2b43SDavid Greenman /*
101415bd2b43SDavid Greenman  * Lookup PCB in hash list.
101515bd2b43SDavid Greenman  */
101615bd2b43SDavid Greenman struct inpcb *
1017136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1018136d4f1cSRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1019136d4f1cSRobert Watson     struct ifnet *ifp)
102015bd2b43SDavid Greenman {
102115bd2b43SDavid Greenman 	struct inpcbhead *head;
1022136d4f1cSRobert Watson 	struct inpcb *inp;
102315bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
102415bd2b43SDavid Greenman 
102559daba27SSam Leffler 	INP_INFO_RLOCK_ASSERT(pcbinfo);
1026602cc7f1SRobert Watson 
102715bd2b43SDavid Greenman 	/*
102815bd2b43SDavid Greenman 	 * First look for an exact match.
102915bd2b43SDavid Greenman 	 */
1030ddd79a97SDavid Greenman 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1031fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1032cfa1ca9dSYoshinobu Inoue #ifdef INET6
1033369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1034cfa1ca9dSYoshinobu Inoue 			continue;
1035cfa1ca9dSYoshinobu Inoue #endif
10366d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1037ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1038ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1039c3229e05SDavid Greenman 		    inp->inp_lport == lport) {
1040c3229e05SDavid Greenman 			/*
1041c3229e05SDavid Greenman 			 * Found.
1042c3229e05SDavid Greenman 			 */
1043c3229e05SDavid Greenman 			return (inp);
1044c3229e05SDavid Greenman 		}
10456d6a026bSDavid Greenman 	}
10466d6a026bSDavid Greenman 	if (wildcard) {
10476d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
1048cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1049cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1050cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10516d6a026bSDavid Greenman 
1052ddd79a97SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1053fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1054cfa1ca9dSYoshinobu Inoue #ifdef INET6
1055369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1056cfa1ca9dSYoshinobu Inoue 				continue;
1057cfa1ca9dSYoshinobu Inoue #endif
10586d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1059c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1060cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
1061cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
1062cfa1ca9dSYoshinobu Inoue 					continue;
10636d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1064c3229e05SDavid Greenman 					return (inp);
1065cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1066cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1067cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1068cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
1069cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
1070cfa1ca9dSYoshinobu Inoue 					else
1071cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10726d6a026bSDavid Greenman 					local_wild = inp;
10736d6a026bSDavid Greenman 				}
10746d6a026bSDavid Greenman 			}
1075cfa1ca9dSYoshinobu Inoue 		}
1076cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1077cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
1078cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
1079cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
1080c3229e05SDavid Greenman 		return (local_wild);
10816d6a026bSDavid Greenman 	}
1082c3229e05SDavid Greenman 
1083c3229e05SDavid Greenman 	/*
1084c3229e05SDavid Greenman 	 * Not found.
1085c3229e05SDavid Greenman 	 */
10866d6a026bSDavid Greenman 	return (NULL);
108715bd2b43SDavid Greenman }
108815bd2b43SDavid Greenman 
10897bc4aca7SDavid Greenman /*
1090c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
10917bc4aca7SDavid Greenman  */
1092c3229e05SDavid Greenman int
1093136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
109415bd2b43SDavid Greenman {
1095c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1096c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1097c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1098c3229e05SDavid Greenman 	struct inpcbport *phd;
1099cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
110015bd2b43SDavid Greenman 
110159daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1102602cc7f1SRobert Watson 	INP_LOCK_ASSERT(inp);
1103602cc7f1SRobert Watson 
1104cfa1ca9dSYoshinobu Inoue #ifdef INET6
1105cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1106cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1107cfa1ca9dSYoshinobu Inoue 	else
1108cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1109cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1110cfa1ca9dSYoshinobu Inoue 
1111cfa1ca9dSYoshinobu Inoue 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1112c3229e05SDavid Greenman 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
111315bd2b43SDavid Greenman 
1114c3229e05SDavid Greenman 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1115c3229e05SDavid Greenman 	    pcbinfo->porthashmask)];
1116c3229e05SDavid Greenman 
1117c3229e05SDavid Greenman 	/*
1118c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1119c3229e05SDavid Greenman 	 */
1120fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1121c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1122c3229e05SDavid Greenman 			break;
1123c3229e05SDavid Greenman 	}
1124c3229e05SDavid Greenman 	/*
1125c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1126c3229e05SDavid Greenman 	 */
1127c3229e05SDavid Greenman 	if (phd == NULL) {
1128c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1129c3229e05SDavid Greenman 		if (phd == NULL) {
1130c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1131c3229e05SDavid Greenman 		}
1132c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1133c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1134c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1135c3229e05SDavid Greenman 	}
1136c3229e05SDavid Greenman 	inp->inp_phd = phd;
1137c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1138c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1139c3229e05SDavid Greenman 	return (0);
114015bd2b43SDavid Greenman }
114115bd2b43SDavid Greenman 
1142c3229e05SDavid Greenman /*
1143c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1144c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1145c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1146c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1147c3229e05SDavid Greenman  */
114815bd2b43SDavid Greenman void
1149136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp)
115015bd2b43SDavid Greenman {
115159daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
115215bd2b43SDavid Greenman 	struct inpcbhead *head;
1153cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
115415bd2b43SDavid Greenman 
115559daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
11564c2bb15aSRobert Watson 	INP_LOCK_ASSERT(inp);
1157602cc7f1SRobert Watson 
1158cfa1ca9dSYoshinobu Inoue #ifdef INET6
1159cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1160cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1161cfa1ca9dSYoshinobu Inoue 	else
1162cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1163cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1164cfa1ca9dSYoshinobu Inoue 
116559daba27SSam Leffler 	head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
116659daba27SSam Leffler 		inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
116715bd2b43SDavid Greenman 
1168c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
116915bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1170c3229e05SDavid Greenman }
1171c3229e05SDavid Greenman 
1172c3229e05SDavid Greenman /*
1173c3229e05SDavid Greenman  * Remove PCB from various lists.
1174c3229e05SDavid Greenman  */
117576429de4SYoshinobu Inoue void
1176136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1177c3229e05SDavid Greenman {
117859daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
117959daba27SSam Leffler 
118059daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
118159daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
118259daba27SSam Leffler 
118359daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1184c3229e05SDavid Greenman 	if (inp->inp_lport) {
1185c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1186c3229e05SDavid Greenman 
1187c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1188c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1189fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1190c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1191c3229e05SDavid Greenman 			free(phd, M_PCB);
1192c3229e05SDavid Greenman 		}
1193c3229e05SDavid Greenman 	}
1194c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
119559daba27SSam Leffler 	pcbinfo->ipi_count--;
119615bd2b43SDavid Greenman }
119775c13541SPoul-Henning Kamp 
1198a557af22SRobert Watson /*
1199a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1200a557af22SRobert Watson  * label change into the in_pcb for the socket.
1201a557af22SRobert Watson  */
1202a557af22SRobert Watson void
1203136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1204a557af22SRobert Watson {
1205a557af22SRobert Watson #ifdef MAC
1206a557af22SRobert Watson 	struct inpcb *inp;
1207a557af22SRobert Watson 
12084c7c478dSRobert Watson 	inp = sotoinpcb(so);
12094c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1210602cc7f1SRobert Watson 
1211a557af22SRobert Watson 	INP_LOCK(inp);
1212310e7cebSRobert Watson 	SOCK_LOCK(so);
1213a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1214310e7cebSRobert Watson 	SOCK_UNLOCK(so);
1215a557af22SRobert Watson 	INP_UNLOCK(inp);
1216a557af22SRobert Watson #endif
1217a557af22SRobert Watson }
12185f311da2SMike Silbersack 
12195f311da2SMike Silbersack /*
1220ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
1221ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
1222ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
1223ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
1224ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
12255f311da2SMike Silbersack  */
12265f311da2SMike Silbersack void
1227136d4f1cSRobert Watson ipport_tick(void *xtp)
12285f311da2SMike Silbersack {
1229ad3a630fSRobert Watson 
1230ad3a630fSRobert Watson 	if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
12315f311da2SMike Silbersack 		if (ipport_stoprandom > 0)
12325f311da2SMike Silbersack 			ipport_stoprandom--;
1233ad3a630fSRobert Watson 	} else
1234ad3a630fSRobert Watson 		ipport_stoprandom = ipport_randomtime;
12355f311da2SMike Silbersack 	ipport_tcplastcount = ipport_tcpallocs;
12365f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
12375f311da2SMike Silbersack }
1238