xref: /freebsd/sys/netinet/in_pcb.c (revision 6ac48b7409037c8cf05bba9b65f011bd3bf363cd)
1df8bae1dSRodney W. Grimes /*
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>
62cfa1ca9dSYoshinobu Inoue #ifdef INET6
63cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
64cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
65cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
66cfa1ca9dSYoshinobu Inoue 
67cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
68cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
69cfa1ca9dSYoshinobu Inoue #include <netkey/key.h>
70cfa1ca9dSYoshinobu Inoue #endif /* IPSEC */
71df8bae1dSRodney W. Grimes 
72b9234fafSSam Leffler #ifdef FAST_IPSEC
73b9234fafSSam Leffler #if defined(IPSEC) || defined(IPSEC_ESP)
74b9234fafSSam Leffler #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
75b9234fafSSam Leffler #endif
76b9234fafSSam Leffler 
77b9234fafSSam Leffler #include <netipsec/ipsec.h>
78b9234fafSSam Leffler #include <netipsec/key.h>
79b9234fafSSam Leffler #endif /* FAST_IPSEC */
80b9234fafSSam Leffler 
81101f9fc8SPeter Wemm /*
82101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
83101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
84101f9fc8SPeter Wemm  */
8582cd038dSYoshinobu Inoue int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
8682cd038dSYoshinobu Inoue int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
879e5a5ed4SMike Silbersack int	ipport_firstauto = IPPORT_HIFIRSTAUTO;		/* 49152 */
889e5a5ed4SMike Silbersack int	ipport_lastauto  = IPPORT_HILASTAUTO;		/* 65535 */
8982cd038dSYoshinobu Inoue int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
9082cd038dSYoshinobu Inoue int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
91101f9fc8SPeter Wemm 
92b0d22693SCrist J. Clark /*
93b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
94b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
95b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
96b0d22693SCrist J. Clark  */
97b0d22693SCrist J. Clark int	ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
98b0d22693SCrist J. Clark int	ipport_reservedlow = 0;
99b0d22693SCrist J. Clark 
1006ac48b74SMike Silbersack /* Shall we allocate ephemeral ports in random order? */
1016ac48b74SMike Silbersack int	ipport_randomized = 1;
1026ac48b74SMike Silbersack 
103bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
104bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
105bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
106bbd42ad0SPeter Wemm 
107bbd42ad0SPeter Wemm static int
10882d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
109bbd42ad0SPeter Wemm {
11030a4ab08SBruce Evans 	int error;
11130a4ab08SBruce Evans 
11230a4ab08SBruce Evans 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
11330a4ab08SBruce Evans 	if (error == 0) {
114bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
115bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
11630a4ab08SBruce Evans 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
11730a4ab08SBruce Evans 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
11830a4ab08SBruce Evans 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
11930a4ab08SBruce Evans 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
120bbd42ad0SPeter Wemm 	}
12130a4ab08SBruce Evans 	return (error);
122bbd42ad0SPeter Wemm }
123bbd42ad0SPeter Wemm 
124bbd42ad0SPeter Wemm #undef RANGECHK
125bbd42ad0SPeter Wemm 
12633b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
12733b3ac06SPeter Wemm 
128bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
129bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
130bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
131bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
132bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
133bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
134bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
135bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
136bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
137bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
138bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
139bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
140b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
141b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
142b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
143b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
1446ac48b74SMike Silbersack SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
1456ac48b74SMike Silbersack 	   CTLFLAG_RW, &ipport_randomized, 0, "");
1460312fbe9SPoul-Henning Kamp 
147c3229e05SDavid Greenman /*
148c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
149c3229e05SDavid Greenman  *
150c3229e05SDavid Greenman  * NOTE: It is assumed that most of these functions will be called at
151c3229e05SDavid Greenman  * splnet(). XXX - There are, unfortunately, a few exceptions to this
152c3229e05SDavid Greenman  * rule that should be fixed.
153c3229e05SDavid Greenman  */
154c3229e05SDavid Greenman 
155c3229e05SDavid Greenman /*
156c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
157c3229e05SDavid Greenman  */
158df8bae1dSRodney W. Grimes int
1596823b823SPawel Jakub Dawidek in_pcballoc(so, pcbinfo, type)
160df8bae1dSRodney W. Grimes 	struct socket *so;
16115bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
1625bd311a5SSam Leffler 	const char *type;
163df8bae1dSRodney W. Grimes {
164df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
16513cf67f3SHajimu UMEMOTO 	int error;
166a557af22SRobert Watson 
16759daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
168a557af22SRobert Watson 	error = 0;
169d1dd20beSSam Leffler 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT | M_ZERO);
170df8bae1dSRodney W. Grimes 	if (inp == NULL)
171df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1723d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
17315bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
174df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
175a557af22SRobert Watson #ifdef MAC
176a557af22SRobert Watson 	error = mac_init_inpcb(inp, M_NOWAIT);
177a557af22SRobert Watson 	if (error != 0)
178a557af22SRobert Watson 		goto out;
179a557af22SRobert Watson 	mac_create_inpcb_from_socket(so, inp);
180a557af22SRobert Watson #endif
1810f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
1820f9ade71SHajimu UMEMOTO #ifdef FAST_IPSEC
18313cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
1840f9ade71SHajimu UMEMOTO #else
1850f9ade71SHajimu UMEMOTO 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
1860f9ade71SHajimu UMEMOTO #endif
187a557af22SRobert Watson 	if (error != 0)
188a557af22SRobert Watson 		goto out;
18913cf67f3SHajimu UMEMOTO #endif /*IPSEC*/
19075daea93SPaul Saab #if defined(INET6)
191340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
192340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
193340c35deSJonathan Lemon 		if (ip6_v6only)
19433841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
195340c35deSJonathan Lemon 	}
19675daea93SPaul Saab #endif
19715bd2b43SDavid Greenman 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
1983d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
199df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
2005bd311a5SSam Leffler 	INP_LOCK_INIT(inp, "inp", type);
20133841545SHajimu UMEMOTO #ifdef INET6
20233841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
20333841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
20433841545SHajimu UMEMOTO #endif
205a557af22SRobert Watson #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
206a557af22SRobert Watson out:
207a557af22SRobert Watson 	if (error != 0)
208a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
209a557af22SRobert Watson #endif
210a557af22SRobert Watson 	return (error);
211df8bae1dSRodney W. Grimes }
212df8bae1dSRodney W. Grimes 
213df8bae1dSRodney W. Grimes int
214b0330ed9SPawel Jakub Dawidek in_pcbbind(inp, nam, cred)
215df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
21657bf258eSGarrett Wollman 	struct sockaddr *nam;
217b0330ed9SPawel Jakub Dawidek 	struct ucred *cred;
218df8bae1dSRodney W. Grimes {
2194b932371SIan Dowse 	int anonport, error;
2204b932371SIan Dowse 
2211b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
22259daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
22359daba27SSam Leffler 
2244b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2254b932371SIan Dowse 		return (EINVAL);
2264b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2274b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2284b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
229b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
2304b932371SIan Dowse 	if (error)
2314b932371SIan Dowse 		return (error);
2324b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2334b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2344b932371SIan Dowse 		inp->inp_lport = 0;
2354b932371SIan Dowse 		return (EAGAIN);
2364b932371SIan Dowse 	}
2374b932371SIan Dowse 	if (anonport)
2384b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2394b932371SIan Dowse 	return (0);
2404b932371SIan Dowse }
2414b932371SIan Dowse 
2424b932371SIan Dowse /*
2434b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2444b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2454b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2464b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2474b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2484b932371SIan Dowse  *
2494b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2504b932371SIan Dowse  */
2514b932371SIan Dowse int
252b0330ed9SPawel Jakub Dawidek in_pcbbind_setup(inp, nam, laddrp, lportp, cred)
2534b932371SIan Dowse 	struct inpcb *inp;
2544b932371SIan Dowse 	struct sockaddr *nam;
2554b932371SIan Dowse 	in_addr_t *laddrp;
2564b932371SIan Dowse 	u_short *lportp;
257b0330ed9SPawel Jakub Dawidek 	struct ucred *cred;
2584b932371SIan Dowse {
2594b932371SIan Dowse 	struct socket *so = inp->inp_socket;
26037bd2b30SPeter Wemm 	unsigned short *lastport;
26115bd2b43SDavid Greenman 	struct sockaddr_in *sin;
262c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2634b932371SIan Dowse 	struct in_addr laddr;
264df8bae1dSRodney W. Grimes 	u_short lport = 0;
2654cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
26675c13541SPoul-Henning Kamp 	int error, prison = 0;
267df8bae1dSRodney W. Grimes 
2681b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
26959daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
27059daba27SSam Leffler 
27159562606SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
272df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
2734b932371SIan Dowse 	laddr.s_addr = *laddrp;
2744b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
275df8bae1dSRodney W. Grimes 		return (EINVAL);
276c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
2776d6a026bSDavid Greenman 		wild = 1;
278df8bae1dSRodney W. Grimes 	if (nam) {
27957bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
28057bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
281df8bae1dSRodney W. Grimes 			return (EINVAL);
282df8bae1dSRodney W. Grimes #ifdef notdef
283df8bae1dSRodney W. Grimes 		/*
284df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
285df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
286df8bae1dSRodney W. Grimes 		 */
287df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
288df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
289df8bae1dSRodney W. Grimes #endif
290e4bdf25dSPoul-Henning Kamp 		if (sin->sin_addr.s_addr != INADDR_ANY)
291b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
29275c13541SPoul-Henning Kamp 				return(EINVAL);
2934b932371SIan Dowse 		if (sin->sin_port != *lportp) {
2944b932371SIan Dowse 			/* Don't allow the port to change. */
2954b932371SIan Dowse 			if (*lportp != 0)
2964b932371SIan Dowse 				return (EINVAL);
297df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
2984b932371SIan Dowse 		}
2994b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
300df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
301df8bae1dSRodney W. Grimes 			/*
302df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
303df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
304df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
305df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
306df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
307df8bae1dSRodney W. Grimes 			 */
308df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
309df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
310df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
311df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
31283103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
313df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
314df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
315df8bae1dSRodney W. Grimes 		}
3164b932371SIan Dowse 		laddr = sin->sin_addr;
317df8bae1dSRodney W. Grimes 		if (lport) {
318df8bae1dSRodney W. Grimes 			struct inpcb *t;
319df8bae1dSRodney W. Grimes 			/* GROSS */
320b0d22693SCrist J. Clark 			if (ntohs(lport) <= ipport_reservedhigh &&
321b0d22693SCrist J. Clark 			    ntohs(lport) >= ipport_reservedlow &&
322b0330ed9SPawel Jakub Dawidek 			    suser_cred(cred, PRISON_ROOT))
3232469dd60SGarrett Wollman 				return (EACCES);
324b0330ed9SPawel Jakub Dawidek 			if (jailed(cred))
32575c13541SPoul-Henning Kamp 				prison = 1;
3262f9a2132SBrian Feldman 			if (so->so_cred->cr_uid != 0 &&
32752b65dbeSBill Fenner 			    !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
3284049a042SGuido van Rooij 				t = in_pcblookup_local(inp->inp_pcbinfo,
32975c13541SPoul-Henning Kamp 				    sin->sin_addr, lport,
33075c13541SPoul-Henning Kamp 				    prison ? 0 :  INPLOOKUP_WILDCARD);
331340c35deSJonathan Lemon 	/*
332340c35deSJonathan Lemon 	 * XXX
333340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
334340c35deSJonathan Lemon 	 */
335340c35deSJonathan Lemon 				if (t && (t->inp_vflag & INP_TIMEWAIT)) {
336340c35deSJonathan Lemon 					if ((ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
337340c35deSJonathan Lemon 					    ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
338340c35deSJonathan Lemon 					    (intotw(t)->tw_so_options & SO_REUSEPORT) == 0) &&
339340c35deSJonathan Lemon 					    (so->so_cred->cr_uid != intotw(t)->tw_cred->cr_uid))
340340c35deSJonathan Lemon 						return (EADDRINUSE);
341340c35deSJonathan Lemon 				} else
3424cc20ab1SSeigo Tanimura 				if (t &&
3434cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
34452b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
34552b65dbeSBill Fenner 				     (t->inp_socket->so_options &
34652b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
3472f9a2132SBrian Feldman 				    (so->so_cred->cr_uid !=
348cfa1ca9dSYoshinobu Inoue 				     t->inp_socket->so_cred->cr_uid)) {
349cfa1ca9dSYoshinobu Inoue #if defined(INET6)
35033841545SHajimu UMEMOTO 					if (ntohl(sin->sin_addr.s_addr) !=
351cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
352cfa1ca9dSYoshinobu Inoue 					    ntohl(t->inp_laddr.s_addr) !=
353cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
354cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(so) ==
355cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(t->inp_socket))
356cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
3574049a042SGuido van Rooij 					return (EADDRINUSE);
3584049a042SGuido van Rooij 				}
359cfa1ca9dSYoshinobu Inoue 			}
360b0330ed9SPawel Jakub Dawidek 			if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
361970680faSPoul-Henning Kamp 				return (EADDRNOTAVAIL);
362c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
36375c13541SPoul-Henning Kamp 			    lport, prison ? 0 : wild);
364340c35deSJonathan Lemon 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
365340c35deSJonathan Lemon 				if ((reuseport & intotw(t)->tw_so_options) == 0)
366340c35deSJonathan Lemon 					return (EADDRINUSE);
367340c35deSJonathan Lemon 			} else
3684cc20ab1SSeigo Tanimura 			if (t &&
3694cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
370cfa1ca9dSYoshinobu Inoue #if defined(INET6)
37133841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
372cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
373cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
374cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
375cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
376cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
377cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
378df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
379df8bae1dSRodney W. Grimes 			}
380cfa1ca9dSYoshinobu Inoue 		}
381df8bae1dSRodney W. Grimes 	}
3824b932371SIan Dowse 	if (*lportp != 0)
3834b932371SIan Dowse 		lport = *lportp;
38433b3ac06SPeter Wemm 	if (lport == 0) {
3856ac48b74SMike Silbersack 		u_short first, last;
3866dd946b3SMike Silbersack 		int count, loopcount;
38733b3ac06SPeter Wemm 
3884b932371SIan Dowse 		if (laddr.s_addr != INADDR_ANY)
389b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &laddr.s_addr))
39075c13541SPoul-Henning Kamp 				return (EINVAL);
391321a2846SPoul-Henning Kamp 
39233b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
39333b3ac06SPeter Wemm 			first = ipport_hifirstauto;	/* sysctl */
39433b3ac06SPeter Wemm 			last  = ipport_hilastauto;
395c3229e05SDavid Greenman 			lastport = &pcbinfo->lasthi;
39633b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
397b0330ed9SPawel Jakub Dawidek 			if ((error = suser_cred(cred, PRISON_ROOT)) != 0)
398a29f300eSGarrett Wollman 				return error;
399bbd42ad0SPeter Wemm 			first = ipport_lowfirstauto;	/* 1023 */
400bbd42ad0SPeter Wemm 			last  = ipport_lowlastauto;	/* 600 */
401c3229e05SDavid Greenman 			lastport = &pcbinfo->lastlow;
40233b3ac06SPeter Wemm 		} else {
40333b3ac06SPeter Wemm 			first = ipport_firstauto;	/* sysctl */
40433b3ac06SPeter Wemm 			last  = ipport_lastauto;
405c3229e05SDavid Greenman 			lastport = &pcbinfo->lastport;
40633b3ac06SPeter Wemm 		}
40733b3ac06SPeter Wemm 		/*
40833b3ac06SPeter Wemm 		 * Simple check to ensure all ports are not used up causing
40933b3ac06SPeter Wemm 		 * a deadlock here.
41033b3ac06SPeter Wemm 		 *
41133b3ac06SPeter Wemm 		 * We split the two cases (up and down) so that the direction
41233b3ac06SPeter Wemm 		 * is not being tested on each round of the loop.
41333b3ac06SPeter Wemm 		 */
4146ac48b74SMike Silbersack 		loopcount = 0;
41533b3ac06SPeter Wemm 		if (first > last) {
41633b3ac06SPeter Wemm 			/*
41733b3ac06SPeter Wemm 			 * counting down
41833b3ac06SPeter Wemm 			 */
4196ac48b74SMike Silbersack 			if (ipport_randomized)
4206ac48b74SMike Silbersack 				*lastport = first - (arc4random() % (first - last));
42133b3ac06SPeter Wemm 			count = first - last;
422df8bae1dSRodney W. Grimes 			do {
4236ac48b74SMike Silbersack 				if (count-- < 0)	/* completely used? */
424550b1518SWes Peters 					return (EADDRNOTAVAIL);
42533b3ac06SPeter Wemm 				--*lastport;
42633b3ac06SPeter Wemm 				if (*lastport > first || *lastport < last)
42733b3ac06SPeter Wemm 					*lastport = first;
42815bd2b43SDavid Greenman 				lport = htons(*lastport);
4294b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4304b932371SIan Dowse 			    wild));
43133b3ac06SPeter Wemm 		} else {
43233b3ac06SPeter Wemm 			/*
43333b3ac06SPeter Wemm 			 * counting up
43433b3ac06SPeter Wemm 			 */
4356ac48b74SMike Silbersack 			if (ipport_randomized)
4366ac48b74SMike Silbersack 				*lastport = first + (arc4random() % (last - first));
43733b3ac06SPeter Wemm 			count = last - first;
43833b3ac06SPeter Wemm 			do {
4396ac48b74SMike Silbersack 				if (count-- < 0)	/* completely used? */
440550b1518SWes Peters 					return (EADDRNOTAVAIL);
44133b3ac06SPeter Wemm 				++*lastport;
44233b3ac06SPeter Wemm 				if (*lastport < first || *lastport > last)
44333b3ac06SPeter Wemm 					*lastport = first;
44433b3ac06SPeter Wemm 				lport = htons(*lastport);
4454b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4464b932371SIan Dowse 			    wild));
44733b3ac06SPeter Wemm 		}
44833b3ac06SPeter Wemm 	}
449b0330ed9SPawel Jakub Dawidek 	if (prison_ip(cred, 0, &laddr.s_addr))
450e4bdf25dSPoul-Henning Kamp 		return (EINVAL);
4514b932371SIan Dowse 	*laddrp = laddr.s_addr;
4524b932371SIan Dowse 	*lportp = lport;
453df8bae1dSRodney W. Grimes 	return (0);
454df8bae1dSRodney W. Grimes }
455df8bae1dSRodney W. Grimes 
456999f1343SGarrett Wollman /*
4575200e00eSIan Dowse  * Connect from a socket to a specified address.
4585200e00eSIan Dowse  * Both address and port must be specified in argument sin.
4595200e00eSIan Dowse  * If don't have a local address for this socket yet,
4605200e00eSIan Dowse  * then pick one.
461999f1343SGarrett Wollman  */
462999f1343SGarrett Wollman int
463b0330ed9SPawel Jakub Dawidek in_pcbconnect(inp, nam, cred)
464999f1343SGarrett Wollman 	register struct inpcb *inp;
46557bf258eSGarrett Wollman 	struct sockaddr *nam;
466b0330ed9SPawel Jakub Dawidek 	struct ucred *cred;
467999f1343SGarrett Wollman {
4685200e00eSIan Dowse 	u_short lport, fport;
4695200e00eSIan Dowse 	in_addr_t laddr, faddr;
4705200e00eSIan Dowse 	int anonport, error;
471df8bae1dSRodney W. Grimes 
4725200e00eSIan Dowse 	lport = inp->inp_lport;
4735200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
4745200e00eSIan Dowse 	anonport = (lport == 0);
4755200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
476b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
4775200e00eSIan Dowse 	if (error)
4785200e00eSIan Dowse 		return (error);
4795200e00eSIan Dowse 
4805200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
4815200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
4825200e00eSIan Dowse 		inp->inp_lport = lport;
4835200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
4845200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
4855200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
4865200e00eSIan Dowse 			inp->inp_lport = 0;
4875200e00eSIan Dowse 			return (EAGAIN);
4885200e00eSIan Dowse 		}
4895200e00eSIan Dowse 	}
4905200e00eSIan Dowse 
4915200e00eSIan Dowse 	/* Commit the remaining changes. */
4925200e00eSIan Dowse 	inp->inp_lport = lport;
4935200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
4945200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
4955200e00eSIan Dowse 	inp->inp_fport = fport;
4965200e00eSIan Dowse 	in_pcbrehash(inp);
4970f9ade71SHajimu UMEMOTO #ifdef IPSEC
4980f9ade71SHajimu UMEMOTO 	if (inp->inp_socket->so_type == SOCK_STREAM)
4990f9ade71SHajimu UMEMOTO 		ipsec_pcbconn(inp->inp_sp);
5000f9ade71SHajimu UMEMOTO #endif
5015200e00eSIan Dowse 	if (anonport)
5025200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
5035200e00eSIan Dowse 	return (0);
5045200e00eSIan Dowse }
5055200e00eSIan Dowse 
5065200e00eSIan Dowse /*
5075200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
5085200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
5095200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
5105200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
5115200e00eSIan Dowse  * the connect.
5125200e00eSIan Dowse  *
5135200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
5145200e00eSIan Dowse  * and port. These are not updated in the error case.
5155200e00eSIan Dowse  *
5165200e00eSIan Dowse  * If the operation fails because the connection already exists,
5175200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
5185200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
5195200e00eSIan Dowse  * is set to NULL.
5205200e00eSIan Dowse  */
5215200e00eSIan Dowse int
522b0330ed9SPawel Jakub Dawidek in_pcbconnect_setup(inp, nam, laddrp, lportp, faddrp, fportp, oinpp, cred)
5235200e00eSIan Dowse 	register struct inpcb *inp;
5245200e00eSIan Dowse 	struct sockaddr *nam;
5255200e00eSIan Dowse 	in_addr_t *laddrp;
5265200e00eSIan Dowse 	u_short *lportp;
5275200e00eSIan Dowse 	in_addr_t *faddrp;
5285200e00eSIan Dowse 	u_short *fportp;
5295200e00eSIan Dowse 	struct inpcb **oinpp;
530b0330ed9SPawel Jakub Dawidek 	struct ucred *cred;
5315200e00eSIan Dowse {
5325200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
5335200e00eSIan Dowse 	struct in_ifaddr *ia;
5345200e00eSIan Dowse 	struct sockaddr_in sa;
535b0330ed9SPawel Jakub Dawidek 	struct ucred *socred;
5365200e00eSIan Dowse 	struct inpcb *oinp;
5375200e00eSIan Dowse 	struct in_addr laddr, faddr;
5385200e00eSIan Dowse 	u_short lport, fport;
5395200e00eSIan Dowse 	int error;
5405200e00eSIan Dowse 
5415200e00eSIan Dowse 	if (oinpp != NULL)
5425200e00eSIan Dowse 		*oinpp = NULL;
54357bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
544df8bae1dSRodney W. Grimes 		return (EINVAL);
545df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
546df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
547df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
548df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5495200e00eSIan Dowse 	laddr.s_addr = *laddrp;
5505200e00eSIan Dowse 	lport = *lportp;
5515200e00eSIan Dowse 	faddr = sin->sin_addr;
5525200e00eSIan Dowse 	fport = sin->sin_port;
553b0330ed9SPawel Jakub Dawidek 	socred = inp->inp_socket->so_cred;
554b0330ed9SPawel Jakub Dawidek 	if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
5555200e00eSIan Dowse 		bzero(&sa, sizeof(sa));
556b0330ed9SPawel Jakub Dawidek 		sa.sin_addr.s_addr = htonl(prison_getip(socred));
5575200e00eSIan Dowse 		sa.sin_len = sizeof(sa);
5585200e00eSIan Dowse 		sa.sin_family = AF_INET;
5595200e00eSIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
560b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, cred);
5615200e00eSIan Dowse 		if (error)
5625200e00eSIan Dowse 			return (error);
5635200e00eSIan Dowse 	}
56459562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
565df8bae1dSRodney W. Grimes 		/*
566df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
567df8bae1dSRodney W. Grimes 		 * use the primary local address.
568df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
569df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
570df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
571df8bae1dSRodney W. Grimes 		 */
5725200e00eSIan Dowse 		if (faddr.s_addr == INADDR_ANY)
5735200e00eSIan Dowse 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
5745200e00eSIan Dowse 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
5755200e00eSIan Dowse 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
5765200e00eSIan Dowse 		    IFF_BROADCAST))
5775200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
5785200e00eSIan Dowse 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
579df8bae1dSRodney W. Grimes 	}
5805200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
58197d8d152SAndre Oppermann 		struct route sro;
582df8bae1dSRodney W. Grimes 
5830cfbbe3bSAndre Oppermann 		bzero(&sro, sizeof(sro));
584df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
585df8bae1dSRodney W. Grimes 		/*
58697d8d152SAndre Oppermann 		 * If route is known our src addr is taken from the i/f,
58797d8d152SAndre Oppermann 		 * else punt.
588df8bae1dSRodney W. Grimes 		 */
58997d8d152SAndre Oppermann 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) {
59097d8d152SAndre Oppermann 			/* Find out route to destination */
59197d8d152SAndre Oppermann 			sro.ro_dst.sa_family = AF_INET;
59297d8d152SAndre Oppermann 			sro.ro_dst.sa_len = sizeof(struct sockaddr_in);
59397d8d152SAndre Oppermann 			((struct sockaddr_in *)&sro.ro_dst)->sin_addr = faddr;
59497d8d152SAndre Oppermann 			rtalloc_ign(&sro, RTF_CLONING);
5954cc20ab1SSeigo Tanimura 		}
596df8bae1dSRodney W. Grimes 		/*
597df8bae1dSRodney W. Grimes 		 * If we found a route, use the address
598df8bae1dSRodney W. Grimes 		 * corresponding to the outgoing interface
599df8bae1dSRodney W. Grimes 		 * unless it is the loopback (in case a route
600df8bae1dSRodney W. Grimes 		 * to our address on another net goes to loopback).
601df8bae1dSRodney W. Grimes 		 */
60297d8d152SAndre Oppermann 		if (sro.ro_rt && !(sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
60397d8d152SAndre Oppermann 			ia = ifatoia(sro.ro_rt->rt_ifa);
60497d8d152SAndre Oppermann 		if (sro.ro_rt)
60597d8d152SAndre Oppermann 			RTFREE(sro.ro_rt);
606df8bae1dSRodney W. Grimes 		if (ia == 0) {
6075200e00eSIan Dowse 			bzero(&sa, sizeof(sa));
6085200e00eSIan Dowse 			sa.sin_addr = faddr;
6095200e00eSIan Dowse 			sa.sin_len = sizeof(sa);
6105200e00eSIan Dowse 			sa.sin_family = AF_INET;
611df8bae1dSRodney W. Grimes 
6125200e00eSIan Dowse 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
613df8bae1dSRodney W. Grimes 			if (ia == 0)
6145200e00eSIan Dowse 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
615df8bae1dSRodney W. Grimes 			if (ia == 0)
616fc2ffbe6SPoul-Henning Kamp 				ia = TAILQ_FIRST(&in_ifaddrhead);
617df8bae1dSRodney W. Grimes 			if (ia == 0)
618df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
619df8bae1dSRodney W. Grimes 		}
620df8bae1dSRodney W. Grimes 		/*
621df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
622df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
623df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
624df8bae1dSRodney W. Grimes 		 */
6255200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
626df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
627df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
628df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
629df8bae1dSRodney W. Grimes 
630df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
631df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
632df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
63337d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
634df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
635df8bae1dSRodney W. Grimes 						break;
636df8bae1dSRodney W. Grimes 				if (ia == 0)
637df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
638df8bae1dSRodney W. Grimes 			}
639df8bae1dSRodney W. Grimes 		}
6405200e00eSIan Dowse 		laddr = ia->ia_addr.sin_addr;
641999f1343SGarrett Wollman 	}
642999f1343SGarrett Wollman 
6435200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
6445200e00eSIan Dowse 	    0, NULL);
6455200e00eSIan Dowse 	if (oinp != NULL) {
6465200e00eSIan Dowse 		if (oinpp != NULL)
6475200e00eSIan Dowse 			*oinpp = oinp;
648df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
649c3229e05SDavid Greenman 	}
6505200e00eSIan Dowse 	if (lport == 0) {
651b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
652b0330ed9SPawel Jakub Dawidek 		    cred);
6535a903f8dSPierre Beyssac 		if (error)
6545a903f8dSPierre Beyssac 			return (error);
6555a903f8dSPierre Beyssac 	}
6565200e00eSIan Dowse 	*laddrp = laddr.s_addr;
6575200e00eSIan Dowse 	*lportp = lport;
6585200e00eSIan Dowse 	*faddrp = faddr.s_addr;
6595200e00eSIan Dowse 	*fportp = fport;
660df8bae1dSRodney W. Grimes 	return (0);
661df8bae1dSRodney W. Grimes }
662df8bae1dSRodney W. Grimes 
66326f9a767SRodney W. Grimes void
664df8bae1dSRodney W. Grimes in_pcbdisconnect(inp)
665df8bae1dSRodney W. Grimes 	struct inpcb *inp;
666df8bae1dSRodney W. Grimes {
66759daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
670df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
67115bd2b43SDavid Greenman 	in_pcbrehash(inp);
6720f9ade71SHajimu UMEMOTO #ifdef IPSEC
6730f9ade71SHajimu UMEMOTO 	ipsec_pcbdisconn(inp->inp_sp);
6740f9ade71SHajimu UMEMOTO #endif
675548c676bSHajimu UMEMOTO 	if (inp->inp_socket->so_state & SS_NOFDREF)
676548c676bSHajimu UMEMOTO 		in_pcbdetach(inp);
677df8bae1dSRodney W. Grimes }
678df8bae1dSRodney W. Grimes 
67926f9a767SRodney W. Grimes void
680df8bae1dSRodney W. Grimes in_pcbdetach(inp)
681df8bae1dSRodney W. Grimes 	struct inpcb *inp;
682df8bae1dSRodney W. Grimes {
683df8bae1dSRodney W. Grimes 	struct socket *so = inp->inp_socket;
6843d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
685df8bae1dSRodney W. Grimes 
68659daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
68759daba27SSam Leffler 
6880f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
689cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
690cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
6913d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
692c3229e05SDavid Greenman 	in_pcbremlists(inp);
693340c35deSJonathan Lemon 	if (so) {
694df8bae1dSRodney W. Grimes 		so->so_pcb = 0;
695b1e4abd2SMatthew Dillon 		sotryfree(so);
696340c35deSJonathan Lemon 	}
697df8bae1dSRodney W. Grimes 	if (inp->inp_options)
698df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
699df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
700cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
701f76fcf6dSJeffrey Hsu 	INP_LOCK_DESTROY(inp);
702a557af22SRobert Watson #ifdef MAC
703a557af22SRobert Watson 	mac_destroy_inpcb(inp);
704a557af22SRobert Watson #endif
70569c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
706df8bae1dSRodney W. Grimes }
707df8bae1dSRodney W. Grimes 
70826ef6ac4SDon Lewis struct sockaddr *
70926ef6ac4SDon Lewis in_sockaddr(port, addr_p)
71026ef6ac4SDon Lewis 	in_port_t port;
71126ef6ac4SDon Lewis 	struct in_addr *addr_p;
71226ef6ac4SDon Lewis {
71326ef6ac4SDon Lewis 	struct sockaddr_in *sin;
71426ef6ac4SDon Lewis 
71526ef6ac4SDon Lewis 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
716a163d034SWarner Losh 		M_WAITOK | M_ZERO);
71726ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
71826ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
71926ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
72026ef6ac4SDon Lewis 	sin->sin_port = port;
72126ef6ac4SDon Lewis 
72226ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
72326ef6ac4SDon Lewis }
72426ef6ac4SDon Lewis 
725117bcae7SGarrett Wollman /*
726f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
727f76fcf6dSJeffrey Hsu  * The socket must have a valid
728117bcae7SGarrett Wollman  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
729117bcae7SGarrett Wollman  * except through a kernel programming error, so it is acceptable to panic
73057bf258eSGarrett Wollman  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
73157bf258eSGarrett Wollman  * because there actually /is/ a programming error somewhere... XXX)
732117bcae7SGarrett Wollman  */
733117bcae7SGarrett Wollman int
734f76fcf6dSJeffrey Hsu in_setsockaddr(so, nam, pcbinfo)
735117bcae7SGarrett Wollman 	struct socket *so;
73657bf258eSGarrett Wollman 	struct sockaddr **nam;
737f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
738df8bae1dSRodney W. Grimes {
739fdc984f7STor Egge 	int s;
740fdc984f7STor Egge 	register struct inpcb *inp;
74126ef6ac4SDon Lewis 	struct in_addr addr;
74226ef6ac4SDon Lewis 	in_port_t port;
74342fa505bSDavid Greenman 
744fdc984f7STor Egge 	s = splnet();
745f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
746fdc984f7STor Egge 	inp = sotoinpcb(so);
747db112f04STor Egge 	if (!inp) {
748f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
749db112f04STor Egge 		splx(s);
750ff079ca4SPeter Wemm 		return ECONNRESET;
751db112f04STor Egge 	}
752f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
75326ef6ac4SDon Lewis 	port = inp->inp_lport;
75426ef6ac4SDon Lewis 	addr = inp->inp_laddr;
755f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
756f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
757db112f04STor Egge 	splx(s);
75842fa505bSDavid Greenman 
75926ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
760117bcae7SGarrett Wollman 	return 0;
761df8bae1dSRodney W. Grimes }
762df8bae1dSRodney W. Grimes 
763f76fcf6dSJeffrey Hsu /*
764f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
765f76fcf6dSJeffrey Hsu  */
766117bcae7SGarrett Wollman int
767f76fcf6dSJeffrey Hsu in_setpeeraddr(so, nam, pcbinfo)
768117bcae7SGarrett Wollman 	struct socket *so;
76957bf258eSGarrett Wollman 	struct sockaddr **nam;
770f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
771df8bae1dSRodney W. Grimes {
772fdc984f7STor Egge 	int s;
773f76fcf6dSJeffrey Hsu 	register struct inpcb *inp;
77426ef6ac4SDon Lewis 	struct in_addr addr;
77526ef6ac4SDon Lewis 	in_port_t port;
77642fa505bSDavid Greenman 
777fdc984f7STor Egge 	s = splnet();
778f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
779fdc984f7STor Egge 	inp = sotoinpcb(so);
780db112f04STor Egge 	if (!inp) {
781f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
782db112f04STor Egge 		splx(s);
783ff079ca4SPeter Wemm 		return ECONNRESET;
784db112f04STor Egge 	}
785f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
78626ef6ac4SDon Lewis 	port = inp->inp_fport;
78726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
788f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
789f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
790db112f04STor Egge 	splx(s);
79142fa505bSDavid Greenman 
79226ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
793117bcae7SGarrett Wollman 	return 0;
794df8bae1dSRodney W. Grimes }
795df8bae1dSRodney W. Grimes 
79626f9a767SRodney W. Grimes void
797f76fcf6dSJeffrey Hsu in_pcbnotifyall(pcbinfo, faddr, errno, notify)
798f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
799df8bae1dSRodney W. Grimes 	struct in_addr faddr;
800c693a045SJonathan Lemon 	int errno;
8013ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int);
802d1c54148SJesper Skriver {
803c693a045SJonathan Lemon 	struct inpcb *inp, *ninp;
804f76fcf6dSJeffrey Hsu 	struct inpcbhead *head;
805c693a045SJonathan Lemon 	int s;
806d1c54148SJesper Skriver 
807d1c54148SJesper Skriver 	s = splnet();
8083dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
809f76fcf6dSJeffrey Hsu 	head = pcbinfo->listhead;
810c693a045SJonathan Lemon 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
811f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
812c693a045SJonathan Lemon 		ninp = LIST_NEXT(inp, inp_list);
813d1c54148SJesper Skriver #ifdef INET6
814f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
815f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
816d1c54148SJesper Skriver 			continue;
817f76fcf6dSJeffrey Hsu 		}
818d1c54148SJesper Skriver #endif
819d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
820f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
821f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
822d1c54148SJesper Skriver 			continue;
823d1c54148SJesper Skriver 		}
8243dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
825f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
826f76fcf6dSJeffrey Hsu 	}
8273dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
828d1c54148SJesper Skriver 	splx(s);
829d1c54148SJesper Skriver }
830d1c54148SJesper Skriver 
831e43cc4aeSHajimu UMEMOTO void
832f76fcf6dSJeffrey Hsu in_pcbpurgeif0(pcbinfo, ifp)
833f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
834e43cc4aeSHajimu UMEMOTO 	struct ifnet *ifp;
835e43cc4aeSHajimu UMEMOTO {
836e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
837e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
838e43cc4aeSHajimu UMEMOTO 	int i, gap;
839e43cc4aeSHajimu UMEMOTO 
840f76fcf6dSJeffrey Hsu 	/* why no splnet here? XXX */
841f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
8423cfcc388SJeffrey Hsu 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
843f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
844e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
845e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
846e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
847e43cc4aeSHajimu UMEMOTO 			/*
848e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
849e43cc4aeSHajimu UMEMOTO 			 * detached.
850e43cc4aeSHajimu UMEMOTO 			 */
851e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
852e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
853e43cc4aeSHajimu UMEMOTO 
854e43cc4aeSHajimu UMEMOTO 			/*
855e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
856e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
857e43cc4aeSHajimu UMEMOTO 			 */
858e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
859e43cc4aeSHajimu UMEMOTO 			    i++) {
860e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
861e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
862e43cc4aeSHajimu UMEMOTO 					gap++;
863e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
864e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
865e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
866e43cc4aeSHajimu UMEMOTO 			}
867e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
868e43cc4aeSHajimu UMEMOTO 		}
869f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
870e43cc4aeSHajimu UMEMOTO 	}
8713cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
872e43cc4aeSHajimu UMEMOTO }
873e43cc4aeSHajimu UMEMOTO 
874df8bae1dSRodney W. Grimes /*
875c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
876c3229e05SDavid Greenman  */
877df8bae1dSRodney W. Grimes struct inpcb *
878c3229e05SDavid Greenman in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
8796d6a026bSDavid Greenman 	struct inpcbinfo *pcbinfo;
880c3229e05SDavid Greenman 	struct in_addr laddr;
881c3229e05SDavid Greenman 	u_int lport_arg;
8826d6a026bSDavid Greenman 	int wild_okay;
883df8bae1dSRodney W. Grimes {
884f1d19042SArchie Cobbs 	register struct inpcb *inp;
885df8bae1dSRodney W. Grimes 	int matchwild = 3, wildcard;
886c3229e05SDavid Greenman 	u_short lport = lport_arg;
8877bc4aca7SDavid Greenman 
8881b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
8891b73ca0bSSam Leffler 
890c3229e05SDavid Greenman 	if (!wild_okay) {
891c3229e05SDavid Greenman 		struct inpcbhead *head;
892c3229e05SDavid Greenman 		/*
893c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
894c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
895c3229e05SDavid Greenman 		 */
896c3229e05SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
897fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
898cfa1ca9dSYoshinobu Inoue #ifdef INET6
899369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
900cfa1ca9dSYoshinobu Inoue 				continue;
901cfa1ca9dSYoshinobu Inoue #endif
902c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
903c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
904c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
905c3229e05SDavid Greenman 				/*
906c3229e05SDavid Greenman 				 * Found.
907c3229e05SDavid Greenman 				 */
908c3229e05SDavid Greenman 				return (inp);
909df8bae1dSRodney W. Grimes 			}
910c3229e05SDavid Greenman 		}
911c3229e05SDavid Greenman 		/*
912c3229e05SDavid Greenman 		 * Not found.
913c3229e05SDavid Greenman 		 */
914c3229e05SDavid Greenman 		return (NULL);
915c3229e05SDavid Greenman 	} else {
916c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
917c3229e05SDavid Greenman 		struct inpcbport *phd;
918c3229e05SDavid Greenman 		struct inpcb *match = NULL;
919c3229e05SDavid Greenman 		/*
920c3229e05SDavid Greenman 		 * Best fit PCB lookup.
921c3229e05SDavid Greenman 		 *
922c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
923c3229e05SDavid Greenman 		 * port hash list.
924c3229e05SDavid Greenman 		 */
92596af9ea5SMike Silbersack 		retrylookup:
926c3229e05SDavid Greenman 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
927c3229e05SDavid Greenman 		    pcbinfo->porthashmask)];
928fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
929c3229e05SDavid Greenman 			if (phd->phd_port == lport)
930c3229e05SDavid Greenman 				break;
931c3229e05SDavid Greenman 		}
932c3229e05SDavid Greenman 		if (phd != NULL) {
933c3229e05SDavid Greenman 			/*
934c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
935c3229e05SDavid Greenman 			 * fit.
936c3229e05SDavid Greenman 			 */
93737d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
938c3229e05SDavid Greenman 				wildcard = 0;
939cfa1ca9dSYoshinobu Inoue #ifdef INET6
940369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
941cfa1ca9dSYoshinobu Inoue 					continue;
942cfa1ca9dSYoshinobu Inoue #endif
94396af9ea5SMike Silbersack 				/*
94496af9ea5SMike Silbersack 				 * Clean out old time_wait sockets if they
94596af9ea5SMike Silbersack 				 * are clogging up needed local ports.
94696af9ea5SMike Silbersack 				 */
94796af9ea5SMike Silbersack 				if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
94896af9ea5SMike Silbersack 					if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) {
949f7bbe2c0SSam Leffler 						INP_LOCK(inp);
95096af9ea5SMike Silbersack 						tcp_twclose((struct tcptw *)inp->inp_ppcb, 0);
95196af9ea5SMike Silbersack 						match = NULL;
95296af9ea5SMike Silbersack 						goto retrylookup;
95396af9ea5SMike Silbersack 					}
95496af9ea5SMike Silbersack 				}
955c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
956c3229e05SDavid Greenman 					wildcard++;
95715bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
95815bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
95915bd2b43SDavid Greenman 						wildcard++;
96015bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
96115bd2b43SDavid Greenman 						continue;
96215bd2b43SDavid Greenman 				} else {
96315bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
96415bd2b43SDavid Greenman 						wildcard++;
96515bd2b43SDavid Greenman 				}
966df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
967df8bae1dSRodney W. Grimes 					match = inp;
968df8bae1dSRodney W. Grimes 					matchwild = wildcard;
9693dbdc25cSDavid Greenman 					if (matchwild == 0) {
970df8bae1dSRodney W. Grimes 						break;
971df8bae1dSRodney W. Grimes 					}
972df8bae1dSRodney W. Grimes 				}
9733dbdc25cSDavid Greenman 			}
974c3229e05SDavid Greenman 		}
975df8bae1dSRodney W. Grimes 		return (match);
976df8bae1dSRodney W. Grimes 	}
977c3229e05SDavid Greenman }
97815bd2b43SDavid Greenman 
97915bd2b43SDavid Greenman /*
98015bd2b43SDavid Greenman  * Lookup PCB in hash list.
98115bd2b43SDavid Greenman  */
98215bd2b43SDavid Greenman struct inpcb *
983cfa1ca9dSYoshinobu Inoue in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
984cfa1ca9dSYoshinobu Inoue 		  ifp)
98515bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
98615bd2b43SDavid Greenman 	struct in_addr faddr, laddr;
98715bd2b43SDavid Greenman 	u_int fport_arg, lport_arg;
9886d6a026bSDavid Greenman 	int wildcard;
989cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
99015bd2b43SDavid Greenman {
99115bd2b43SDavid Greenman 	struct inpcbhead *head;
99215bd2b43SDavid Greenman 	register struct inpcb *inp;
99315bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
99415bd2b43SDavid Greenman 
99559daba27SSam Leffler 	INP_INFO_RLOCK_ASSERT(pcbinfo);
99615bd2b43SDavid Greenman 	/*
99715bd2b43SDavid Greenman 	 * First look for an exact match.
99815bd2b43SDavid Greenman 	 */
999ddd79a97SDavid Greenman 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1000fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1001cfa1ca9dSYoshinobu Inoue #ifdef INET6
1002369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1003cfa1ca9dSYoshinobu Inoue 			continue;
1004cfa1ca9dSYoshinobu Inoue #endif
10056d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1006ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1007ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1008c3229e05SDavid Greenman 		    inp->inp_lport == lport) {
1009c3229e05SDavid Greenman 			/*
1010c3229e05SDavid Greenman 			 * Found.
1011c3229e05SDavid Greenman 			 */
1012c3229e05SDavid Greenman 			return (inp);
1013c3229e05SDavid Greenman 		}
10146d6a026bSDavid Greenman 	}
10156d6a026bSDavid Greenman 	if (wildcard) {
10166d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
1017cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1018cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1019cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10206d6a026bSDavid Greenman 
1021ddd79a97SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1022fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1023cfa1ca9dSYoshinobu Inoue #ifdef INET6
1024369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1025cfa1ca9dSYoshinobu Inoue 				continue;
1026cfa1ca9dSYoshinobu Inoue #endif
10276d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1028c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1029cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
1030cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
1031cfa1ca9dSYoshinobu Inoue 					continue;
10326d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1033c3229e05SDavid Greenman 					return (inp);
1034cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1035cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1036cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1037cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
1038cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
1039cfa1ca9dSYoshinobu Inoue 					else
1040cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10416d6a026bSDavid Greenman 					local_wild = inp;
10426d6a026bSDavid Greenman 				}
10436d6a026bSDavid Greenman 			}
1044cfa1ca9dSYoshinobu Inoue 		}
1045cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1046cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
1047cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
1048cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
1049c3229e05SDavid Greenman 		return (local_wild);
10506d6a026bSDavid Greenman 	}
1051c3229e05SDavid Greenman 
1052c3229e05SDavid Greenman 	/*
1053c3229e05SDavid Greenman 	 * Not found.
1054c3229e05SDavid Greenman 	 */
10556d6a026bSDavid Greenman 	return (NULL);
105615bd2b43SDavid Greenman }
105715bd2b43SDavid Greenman 
10587bc4aca7SDavid Greenman /*
1059c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
10607bc4aca7SDavid Greenman  */
1061c3229e05SDavid Greenman int
106215bd2b43SDavid Greenman in_pcbinshash(inp)
106315bd2b43SDavid Greenman 	struct inpcb *inp;
106415bd2b43SDavid Greenman {
1065c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1066c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1067c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1068c3229e05SDavid Greenman 	struct inpcbport *phd;
1069cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
107015bd2b43SDavid Greenman 
107159daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1072cfa1ca9dSYoshinobu Inoue #ifdef INET6
1073cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1074cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1075cfa1ca9dSYoshinobu Inoue 	else
1076cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1077cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1078cfa1ca9dSYoshinobu Inoue 
1079cfa1ca9dSYoshinobu Inoue 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1080c3229e05SDavid Greenman 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
108115bd2b43SDavid Greenman 
1082c3229e05SDavid Greenman 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1083c3229e05SDavid Greenman 	    pcbinfo->porthashmask)];
1084c3229e05SDavid Greenman 
1085c3229e05SDavid Greenman 	/*
1086c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1087c3229e05SDavid Greenman 	 */
1088fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1089c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1090c3229e05SDavid Greenman 			break;
1091c3229e05SDavid Greenman 	}
1092c3229e05SDavid Greenman 	/*
1093c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1094c3229e05SDavid Greenman 	 */
1095c3229e05SDavid Greenman 	if (phd == NULL) {
1096c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1097c3229e05SDavid Greenman 		if (phd == NULL) {
1098c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1099c3229e05SDavid Greenman 		}
1100c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1101c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1102c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1103c3229e05SDavid Greenman 	}
1104c3229e05SDavid Greenman 	inp->inp_phd = phd;
1105c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1106c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1107c3229e05SDavid Greenman 	return (0);
110815bd2b43SDavid Greenman }
110915bd2b43SDavid Greenman 
1110c3229e05SDavid Greenman /*
1111c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1112c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1113c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1114c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1115c3229e05SDavid Greenman  */
111615bd2b43SDavid Greenman void
111715bd2b43SDavid Greenman in_pcbrehash(inp)
111815bd2b43SDavid Greenman 	struct inpcb *inp;
111915bd2b43SDavid Greenman {
112059daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
112115bd2b43SDavid Greenman 	struct inpcbhead *head;
1122cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
112315bd2b43SDavid Greenman 
112459daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
112559daba27SSam Leffler 	/* XXX? INP_LOCK_ASSERT(inp); */
1126cfa1ca9dSYoshinobu Inoue #ifdef INET6
1127cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1128cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1129cfa1ca9dSYoshinobu Inoue 	else
1130cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1131cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1132cfa1ca9dSYoshinobu Inoue 
113359daba27SSam Leffler 	head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
113459daba27SSam Leffler 		inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
113515bd2b43SDavid Greenman 
1136c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
113715bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1138c3229e05SDavid Greenman }
1139c3229e05SDavid Greenman 
1140c3229e05SDavid Greenman /*
1141c3229e05SDavid Greenman  * Remove PCB from various lists.
1142c3229e05SDavid Greenman  */
114376429de4SYoshinobu Inoue void
1144c3229e05SDavid Greenman in_pcbremlists(inp)
1145c3229e05SDavid Greenman 	struct inpcb *inp;
1146c3229e05SDavid Greenman {
114759daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
114859daba27SSam Leffler 
114959daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
115059daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
115159daba27SSam Leffler 
115259daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1153c3229e05SDavid Greenman 	if (inp->inp_lport) {
1154c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1155c3229e05SDavid Greenman 
1156c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1157c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1158fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1159c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1160c3229e05SDavid Greenman 			free(phd, M_PCB);
1161c3229e05SDavid Greenman 		}
1162c3229e05SDavid Greenman 	}
1163c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
116459daba27SSam Leffler 	pcbinfo->ipi_count--;
116515bd2b43SDavid Greenman }
116675c13541SPoul-Henning Kamp 
1167a557af22SRobert Watson /*
1168a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1169a557af22SRobert Watson  * label change into the in_pcb for the socket.
1170a557af22SRobert Watson  */
1171a557af22SRobert Watson void
1172a557af22SRobert Watson in_pcbsosetlabel(so)
1173a557af22SRobert Watson 	struct socket *so;
1174a557af22SRobert Watson {
1175a557af22SRobert Watson #ifdef MAC
1176a557af22SRobert Watson 	struct inpcb *inp;
1177a557af22SRobert Watson 
1178a557af22SRobert Watson 	/* XXX: Will assert socket lock when we have them. */
1179a557af22SRobert Watson 	inp = (struct inpcb *)so->so_pcb;
1180a557af22SRobert Watson 	INP_LOCK(inp);
1181a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1182a557af22SRobert Watson 	INP_UNLOCK(inp);
1183a557af22SRobert Watson #endif
1184a557af22SRobert Watson }
1185