xref: /freebsd/sys/netinet/in_pcb.c (revision a557af222b70694470f63e2a0f1bf58c9dcc73fd)
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
332469dd60SGarrett Wollman  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
39a557af22SRobert Watson #include "opt_mac.h"
40cfa1ca9dSYoshinobu Inoue 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43104a9b7eSAlexander Kabaev #include <sys/limits.h>
44a557af22SRobert Watson #include <sys/mac.h>
45df8bae1dSRodney W. Grimes #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
5275c13541SPoul-Henning Kamp #include <sys/jail.h>
53101f9fc8SPeter Wemm #include <sys/kernel.h>
54101f9fc8SPeter Wemm #include <sys/sysctl.h>
558781d8e9SBruce Evans 
5669c2d429SJeff Roberson #include <vm/uma.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <net/if.h>
59cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
65df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
66340c35deSJonathan Lemon #include <netinet/tcp_var.h>
67cfa1ca9dSYoshinobu Inoue #ifdef INET6
68cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
69cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
70cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
71cfa1ca9dSYoshinobu Inoue 
72cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
73cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
74cfa1ca9dSYoshinobu Inoue #include <netkey/key.h>
75cfa1ca9dSYoshinobu Inoue #endif /* IPSEC */
76df8bae1dSRodney W. Grimes 
77b9234fafSSam Leffler #ifdef FAST_IPSEC
78b9234fafSSam Leffler #if defined(IPSEC) || defined(IPSEC_ESP)
79b9234fafSSam Leffler #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
80b9234fafSSam Leffler #endif
81b9234fafSSam Leffler 
82b9234fafSSam Leffler #include <netipsec/ipsec.h>
83b9234fafSSam Leffler #include <netipsec/key.h>
84b9234fafSSam Leffler #endif /* FAST_IPSEC */
85b9234fafSSam Leffler 
86df8bae1dSRodney W. Grimes struct	in_addr zeroin_addr;
87df8bae1dSRodney W. Grimes 
88101f9fc8SPeter Wemm /*
89101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
90101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
91101f9fc8SPeter Wemm  */
9282cd038dSYoshinobu Inoue int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
9382cd038dSYoshinobu Inoue int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
949e5a5ed4SMike Silbersack int	ipport_firstauto = IPPORT_HIFIRSTAUTO;		/* 49152 */
959e5a5ed4SMike Silbersack int	ipport_lastauto  = IPPORT_HILASTAUTO;		/* 65535 */
9682cd038dSYoshinobu Inoue int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
9782cd038dSYoshinobu Inoue int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
98101f9fc8SPeter Wemm 
99b0d22693SCrist J. Clark /*
100b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
101b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
102b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
103b0d22693SCrist J. Clark  */
104b0d22693SCrist J. Clark int	ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
105b0d22693SCrist J. Clark int	ipport_reservedlow = 0;
106b0d22693SCrist J. Clark 
107bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
108bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
109bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
110bbd42ad0SPeter Wemm 
111bbd42ad0SPeter Wemm static int
11282d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
113bbd42ad0SPeter Wemm {
114bbd42ad0SPeter Wemm 	int error = sysctl_handle_int(oidp,
115bbd42ad0SPeter Wemm 		oidp->oid_arg1, oidp->oid_arg2, req);
116bbd42ad0SPeter Wemm 	if (!error) {
117bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
118bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
119bbd42ad0SPeter Wemm 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
120bbd42ad0SPeter Wemm 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
121bbd42ad0SPeter Wemm 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
122bbd42ad0SPeter Wemm 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
123bbd42ad0SPeter Wemm 	}
124bbd42ad0SPeter Wemm 	return error;
125bbd42ad0SPeter Wemm }
126bbd42ad0SPeter Wemm 
127bbd42ad0SPeter Wemm #undef RANGECHK
128bbd42ad0SPeter Wemm 
12933b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
13033b3ac06SPeter Wemm 
131bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
132bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
133bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
134bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
135bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
136bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
137bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
138bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
139bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
140bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
141bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
142bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
143b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
144b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
145b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
146b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
1470312fbe9SPoul-Henning Kamp 
148c3229e05SDavid Greenman /*
149c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
150c3229e05SDavid Greenman  *
151c3229e05SDavid Greenman  * NOTE: It is assumed that most of these functions will be called at
152c3229e05SDavid Greenman  * splnet(). XXX - There are, unfortunately, a few exceptions to this
153c3229e05SDavid Greenman  * rule that should be fixed.
154c3229e05SDavid Greenman  */
155c3229e05SDavid Greenman 
156c3229e05SDavid Greenman /*
157c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
158c3229e05SDavid Greenman  */
159df8bae1dSRodney W. Grimes int
160b40ce416SJulian Elischer in_pcballoc(so, pcbinfo, td)
161df8bae1dSRodney W. Grimes 	struct socket *so;
16215bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
163b40ce416SJulian Elischer 	struct thread *td;
164df8bae1dSRodney W. Grimes {
165df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
16613cf67f3SHajimu UMEMOTO 	int error;
167a557af22SRobert Watson 
16859daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
169a557af22SRobert Watson 	error = 0;
170d1dd20beSSam Leffler 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT | M_ZERO);
171df8bae1dSRodney W. Grimes 	if (inp == NULL)
172df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1733d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
17415bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
175df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
176a557af22SRobert Watson #ifdef MAC
177a557af22SRobert Watson 	error = mac_init_inpcb(inp, M_NOWAIT);
178a557af22SRobert Watson 	if (error != 0)
179a557af22SRobert Watson 		goto out;
180a557af22SRobert Watson 	mac_create_inpcb_from_socket(so, inp);
181a557af22SRobert Watson #endif
1820f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
1830f9ade71SHajimu UMEMOTO #ifdef FAST_IPSEC
18413cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
1850f9ade71SHajimu UMEMOTO #else
1860f9ade71SHajimu UMEMOTO 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
1870f9ade71SHajimu UMEMOTO #endif
188a557af22SRobert Watson 	if (error != 0)
189a557af22SRobert Watson 		goto out;
19013cf67f3SHajimu UMEMOTO #endif /*IPSEC*/
19175daea93SPaul Saab #if defined(INET6)
192340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
193340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
194340c35deSJonathan Lemon 		if (ip6_v6only)
19533841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
196340c35deSJonathan Lemon 	}
19775daea93SPaul Saab #endif
19815bd2b43SDavid Greenman 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
1993d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
200df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
201f76fcf6dSJeffrey Hsu 	INP_LOCK_INIT(inp, "inp");
20233841545SHajimu UMEMOTO #ifdef INET6
20333841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
20433841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
20533841545SHajimu UMEMOTO #endif
206a557af22SRobert Watson #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
207a557af22SRobert Watson out:
208a557af22SRobert Watson 	if (error != 0)
209a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
210a557af22SRobert Watson #endif
211a557af22SRobert Watson 	return (error);
212df8bae1dSRodney W. Grimes }
213df8bae1dSRodney W. Grimes 
214df8bae1dSRodney W. Grimes int
215b40ce416SJulian Elischer in_pcbbind(inp, nam, td)
216df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
21757bf258eSGarrett Wollman 	struct sockaddr *nam;
218b40ce416SJulian Elischer 	struct thread *td;
219df8bae1dSRodney W. Grimes {
2204b932371SIan Dowse 	int anonport, error;
2214b932371SIan Dowse 
2221b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
22359daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
22459daba27SSam Leffler 
2254b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2264b932371SIan Dowse 		return (EINVAL);
2274b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2284b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2294b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
2304b932371SIan Dowse 	    &inp->inp_lport, td);
2314b932371SIan Dowse 	if (error)
2324b932371SIan Dowse 		return (error);
2334b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2344b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2354b932371SIan Dowse 		inp->inp_lport = 0;
2364b932371SIan Dowse 		return (EAGAIN);
2374b932371SIan Dowse 	}
2384b932371SIan Dowse 	if (anonport)
2394b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2404b932371SIan Dowse 	return (0);
2414b932371SIan Dowse }
2424b932371SIan Dowse 
2434b932371SIan Dowse /*
2444b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2454b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2464b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2474b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2484b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2494b932371SIan Dowse  *
2504b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2514b932371SIan Dowse  */
2524b932371SIan Dowse int
2534b932371SIan Dowse in_pcbbind_setup(inp, nam, laddrp, lportp, td)
2544b932371SIan Dowse 	struct inpcb *inp;
2554b932371SIan Dowse 	struct sockaddr *nam;
2564b932371SIan Dowse 	in_addr_t *laddrp;
2574b932371SIan Dowse 	u_short *lportp;
2584b932371SIan Dowse 	struct thread *td;
2594b932371SIan Dowse {
2604b932371SIan Dowse 	struct socket *so = inp->inp_socket;
26137bd2b30SPeter Wemm 	unsigned short *lastport;
26215bd2b43SDavid Greenman 	struct sockaddr_in *sin;
263c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2644b932371SIan Dowse 	struct in_addr laddr;
265df8bae1dSRodney W. Grimes 	u_short lport = 0;
2664cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
26775c13541SPoul-Henning Kamp 	int error, prison = 0;
268df8bae1dSRodney W. Grimes 
2691b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
27059daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
27159daba27SSam Leffler 
27259562606SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
273df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
2744b932371SIan Dowse 	laddr.s_addr = *laddrp;
2754b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
276df8bae1dSRodney W. Grimes 		return (EINVAL);
277c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
2786d6a026bSDavid Greenman 		wild = 1;
279df8bae1dSRodney W. Grimes 	if (nam) {
28057bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
28157bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
282df8bae1dSRodney W. Grimes 			return (EINVAL);
283df8bae1dSRodney W. Grimes #ifdef notdef
284df8bae1dSRodney W. Grimes 		/*
285df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
286df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
287df8bae1dSRodney W. Grimes 		 */
288df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
289df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
290df8bae1dSRodney W. Grimes #endif
291e4bdf25dSPoul-Henning Kamp 		if (sin->sin_addr.s_addr != INADDR_ANY)
292a854ed98SJohn Baldwin 			if (prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr))
29375c13541SPoul-Henning Kamp 				return(EINVAL);
2944b932371SIan Dowse 		if (sin->sin_port != *lportp) {
2954b932371SIan Dowse 			/* Don't allow the port to change. */
2964b932371SIan Dowse 			if (*lportp != 0)
2974b932371SIan Dowse 				return (EINVAL);
298df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
2994b932371SIan Dowse 		}
3004b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
301df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
302df8bae1dSRodney W. Grimes 			/*
303df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
304df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
305df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
306df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
307df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
308df8bae1dSRodney W. Grimes 			 */
309df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
310df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
311df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
312df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
31383103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
314df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
315df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
316df8bae1dSRodney W. Grimes 		}
3174b932371SIan Dowse 		laddr = sin->sin_addr;
318df8bae1dSRodney W. Grimes 		if (lport) {
319df8bae1dSRodney W. Grimes 			struct inpcb *t;
320df8bae1dSRodney W. Grimes 			/* GROSS */
321b0d22693SCrist J. Clark 			if (ntohs(lport) <= ipport_reservedhigh &&
322b0d22693SCrist J. Clark 			    ntohs(lport) >= ipport_reservedlow &&
323b0d22693SCrist J. Clark 			    td && suser_cred(td->td_ucred, PRISON_ROOT))
3242469dd60SGarrett Wollman 				return (EACCES);
325a854ed98SJohn Baldwin 			if (td && jailed(td->td_ucred))
32675c13541SPoul-Henning Kamp 				prison = 1;
3272f9a2132SBrian Feldman 			if (so->so_cred->cr_uid != 0 &&
32852b65dbeSBill Fenner 			    !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
3294049a042SGuido van Rooij 				t = in_pcblookup_local(inp->inp_pcbinfo,
33075c13541SPoul-Henning Kamp 				    sin->sin_addr, lport,
33175c13541SPoul-Henning Kamp 				    prison ? 0 :  INPLOOKUP_WILDCARD);
332340c35deSJonathan Lemon 	/*
333340c35deSJonathan Lemon 	 * XXX
334340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
335340c35deSJonathan Lemon 	 */
336340c35deSJonathan Lemon 				if (t && (t->inp_vflag & INP_TIMEWAIT)) {
337340c35deSJonathan Lemon 					if ((ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
338340c35deSJonathan Lemon 					    ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
339340c35deSJonathan Lemon 					    (intotw(t)->tw_so_options & SO_REUSEPORT) == 0) &&
340340c35deSJonathan Lemon 					    (so->so_cred->cr_uid != intotw(t)->tw_cred->cr_uid))
341340c35deSJonathan Lemon 						return (EADDRINUSE);
342340c35deSJonathan Lemon 				} else
3434cc20ab1SSeigo Tanimura 				if (t &&
3444cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
34552b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
34652b65dbeSBill Fenner 				     (t->inp_socket->so_options &
34752b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
3482f9a2132SBrian Feldman 				    (so->so_cred->cr_uid !=
349cfa1ca9dSYoshinobu Inoue 				     t->inp_socket->so_cred->cr_uid)) {
350cfa1ca9dSYoshinobu Inoue #if defined(INET6)
35133841545SHajimu UMEMOTO 					if (ntohl(sin->sin_addr.s_addr) !=
352cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
353cfa1ca9dSYoshinobu Inoue 					    ntohl(t->inp_laddr.s_addr) !=
354cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
355cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(so) ==
356cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(t->inp_socket))
357cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
3584049a042SGuido van Rooij 					return (EADDRINUSE);
3594049a042SGuido van Rooij 				}
360cfa1ca9dSYoshinobu Inoue 			}
361970680faSPoul-Henning Kamp 			if (prison &&
362a854ed98SJohn Baldwin 			    prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr))
363970680faSPoul-Henning Kamp 				return (EADDRNOTAVAIL);
364c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
36575c13541SPoul-Henning Kamp 			    lport, prison ? 0 : wild);
366340c35deSJonathan Lemon 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
367340c35deSJonathan Lemon 				if ((reuseport & intotw(t)->tw_so_options) == 0)
368340c35deSJonathan Lemon 					return (EADDRINUSE);
369340c35deSJonathan Lemon 			} else
3704cc20ab1SSeigo Tanimura 			if (t &&
3714cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
372cfa1ca9dSYoshinobu Inoue #if defined(INET6)
37333841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
374cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
375cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
376cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
377cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
378cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
379cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
380df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
381df8bae1dSRodney W. Grimes 			}
382cfa1ca9dSYoshinobu Inoue 		}
383df8bae1dSRodney W. Grimes 	}
3844b932371SIan Dowse 	if (*lportp != 0)
3854b932371SIan Dowse 		lport = *lportp;
38633b3ac06SPeter Wemm 	if (lport == 0) {
3878b149b51SJohn Baldwin 		u_short first, last;
38833b3ac06SPeter Wemm 		int count;
38933b3ac06SPeter Wemm 
3904b932371SIan Dowse 		if (laddr.s_addr != INADDR_ANY)
3914b932371SIan Dowse 			if (prison_ip(td->td_ucred, 0, &laddr.s_addr))
39275c13541SPoul-Henning Kamp 				return (EINVAL);
393321a2846SPoul-Henning Kamp 
39433b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
39533b3ac06SPeter Wemm 			first = ipport_hifirstauto;	/* sysctl */
39633b3ac06SPeter Wemm 			last  = ipport_hilastauto;
397c3229e05SDavid Greenman 			lastport = &pcbinfo->lasthi;
39833b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
3994b932371SIan Dowse 			if (td && (error = suser_cred(td->td_ucred,
4004b932371SIan Dowse 			    PRISON_ROOT)) != 0)
401a29f300eSGarrett Wollman 				return error;
402bbd42ad0SPeter Wemm 			first = ipport_lowfirstauto;	/* 1023 */
403bbd42ad0SPeter Wemm 			last  = ipport_lowlastauto;	/* 600 */
404c3229e05SDavid Greenman 			lastport = &pcbinfo->lastlow;
40533b3ac06SPeter Wemm 		} else {
40633b3ac06SPeter Wemm 			first = ipport_firstauto;	/* sysctl */
40733b3ac06SPeter Wemm 			last  = ipport_lastauto;
408c3229e05SDavid Greenman 			lastport = &pcbinfo->lastport;
40933b3ac06SPeter Wemm 		}
41033b3ac06SPeter Wemm 		/*
41133b3ac06SPeter Wemm 		 * Simple check to ensure all ports are not used up causing
41233b3ac06SPeter Wemm 		 * a deadlock here.
41333b3ac06SPeter Wemm 		 *
41433b3ac06SPeter Wemm 		 * We split the two cases (up and down) so that the direction
41533b3ac06SPeter Wemm 		 * is not being tested on each round of the loop.
41633b3ac06SPeter Wemm 		 */
41733b3ac06SPeter Wemm 		if (first > last) {
41833b3ac06SPeter Wemm 			/*
41933b3ac06SPeter Wemm 			 * counting down
42033b3ac06SPeter Wemm 			 */
42133b3ac06SPeter Wemm 			count = first - last;
42233b3ac06SPeter Wemm 
423df8bae1dSRodney W. Grimes 			do {
4244b932371SIan Dowse 				if (count-- < 0)	/* completely used? */
425550b1518SWes Peters 					return (EADDRNOTAVAIL);
42633b3ac06SPeter Wemm 				--*lastport;
42733b3ac06SPeter Wemm 				if (*lastport > first || *lastport < last)
42833b3ac06SPeter Wemm 					*lastport = first;
42915bd2b43SDavid Greenman 				lport = htons(*lastport);
4304b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4314b932371SIan Dowse 			    wild));
43233b3ac06SPeter Wemm 		} else {
43333b3ac06SPeter Wemm 			/*
43433b3ac06SPeter Wemm 			 * counting up
43533b3ac06SPeter Wemm 			 */
43633b3ac06SPeter Wemm 			count = last - first;
43733b3ac06SPeter Wemm 
43833b3ac06SPeter Wemm 			do {
4394b932371SIan Dowse 				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 	}
4494b932371SIan Dowse 	if (prison_ip(td->td_ucred, 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
4635200e00eSIan Dowse in_pcbconnect(inp, nam, td)
464999f1343SGarrett Wollman 	register struct inpcb *inp;
46557bf258eSGarrett Wollman 	struct sockaddr *nam;
4665200e00eSIan Dowse 	struct thread *td;
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,
4765200e00eSIan Dowse 	    NULL, td);
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
5225200e00eSIan Dowse in_pcbconnect_setup(inp, nam, laddrp, lportp, faddrp, fportp, oinpp, td)
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;
5305200e00eSIan Dowse 	struct thread *td;
5315200e00eSIan Dowse {
5325200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
5335200e00eSIan Dowse 	struct in_ifaddr *ia;
5345200e00eSIan Dowse 	struct sockaddr_in sa;
5355200e00eSIan Dowse 	struct ucred *cred;
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;
5535200e00eSIan Dowse 	cred = inp->inp_socket->so_cred;
5545200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY && jailed(cred)) {
5555200e00eSIan Dowse 		bzero(&sa, sizeof(sa));
5565200e00eSIan Dowse 		sa.sin_addr.s_addr = htonl(prison_getip(cred));
5575200e00eSIan Dowse 		sa.sin_len = sizeof(sa);
5585200e00eSIan Dowse 		sa.sin_family = AF_INET;
5595200e00eSIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
5605200e00eSIan Dowse 		    &laddr.s_addr, &lport, td);
5615200e00eSIan Dowse 		if (error)
5625200e00eSIan Dowse 			return (error);
5635200e00eSIan Dowse 	}
5645200e00eSIan Dowse 
56559562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
566df8bae1dSRodney W. Grimes 		/*
567df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
568df8bae1dSRodney W. Grimes 		 * use the primary local address.
569df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
570df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
571df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
572df8bae1dSRodney W. Grimes 		 */
5735200e00eSIan Dowse 		if (faddr.s_addr == INADDR_ANY)
5745200e00eSIan Dowse 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
5755200e00eSIan Dowse 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
5765200e00eSIan Dowse 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
5775200e00eSIan Dowse 		    IFF_BROADCAST))
5785200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
5795200e00eSIan Dowse 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
580df8bae1dSRodney W. Grimes 	}
5815200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
582df8bae1dSRodney W. Grimes 		register struct route *ro;
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
585df8bae1dSRodney W. Grimes 		/*
586df8bae1dSRodney W. Grimes 		 * If route is known or can be allocated now,
587df8bae1dSRodney W. Grimes 		 * our src addr is taken from the i/f, else punt.
588a4a6e773SHajimu UMEMOTO 		 * Note that we should check the address family of the cached
589a4a6e773SHajimu UMEMOTO 		 * destination, in case of sharing the cache with IPv6.
590df8bae1dSRodney W. Grimes 		 */
591df8bae1dSRodney W. Grimes 		ro = &inp->inp_route;
5923ab2096bSIan Dowse 		if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
5933ab2096bSIan Dowse 		    ro->ro_dst.sa_family != AF_INET ||
5945200e00eSIan Dowse 		    satosin(&ro->ro_dst)->sin_addr.s_addr != faddr.s_addr ||
595df8bae1dSRodney W. Grimes 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
596df8bae1dSRodney W. Grimes 			RTFREE(ro->ro_rt);
597df8bae1dSRodney W. Grimes 			ro->ro_rt = (struct rtentry *)0;
598df8bae1dSRodney W. Grimes 		}
599df8bae1dSRodney W. Grimes 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
600df8bae1dSRodney W. Grimes 		    (ro->ro_rt == (struct rtentry *)0 ||
601df8bae1dSRodney W. Grimes 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
602df8bae1dSRodney W. Grimes 			/* No route yet, so try to acquire one */
603a4a6e773SHajimu UMEMOTO 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
604df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_family = AF_INET;
605df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
6065200e00eSIan Dowse 			((struct sockaddr_in *)&ro->ro_dst)->sin_addr = faddr;
607df8bae1dSRodney W. Grimes 			rtalloc(ro);
6084cc20ab1SSeigo Tanimura 		}
609df8bae1dSRodney W. Grimes 		/*
610df8bae1dSRodney W. Grimes 		 * If we found a route, use the address
611df8bae1dSRodney W. Grimes 		 * corresponding to the outgoing interface
612df8bae1dSRodney W. Grimes 		 * unless it is the loopback (in case a route
613df8bae1dSRodney W. Grimes 		 * to our address on another net goes to loopback).
614df8bae1dSRodney W. Grimes 		 */
615df8bae1dSRodney W. Grimes 		if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
616df8bae1dSRodney W. Grimes 			ia = ifatoia(ro->ro_rt->rt_ifa);
617df8bae1dSRodney W. Grimes 		if (ia == 0) {
6185200e00eSIan Dowse 			bzero(&sa, sizeof(sa));
6195200e00eSIan Dowse 			sa.sin_addr = faddr;
6205200e00eSIan Dowse 			sa.sin_len = sizeof(sa);
6215200e00eSIan Dowse 			sa.sin_family = AF_INET;
622df8bae1dSRodney W. Grimes 
6235200e00eSIan Dowse 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
624df8bae1dSRodney W. Grimes 			if (ia == 0)
6255200e00eSIan Dowse 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
626df8bae1dSRodney W. Grimes 			if (ia == 0)
627fc2ffbe6SPoul-Henning Kamp 				ia = TAILQ_FIRST(&in_ifaddrhead);
628df8bae1dSRodney W. Grimes 			if (ia == 0)
629df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
630df8bae1dSRodney W. Grimes 		}
631df8bae1dSRodney W. Grimes 		/*
632df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
633df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
634df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
635df8bae1dSRodney W. Grimes 		 */
6365200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
637df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
638df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
639df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
642df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
643df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
64437d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
645df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
646df8bae1dSRodney W. Grimes 						break;
647df8bae1dSRodney W. Grimes 				if (ia == 0)
648df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
649df8bae1dSRodney W. Grimes 			}
650df8bae1dSRodney W. Grimes 		}
6515200e00eSIan Dowse 		laddr = ia->ia_addr.sin_addr;
652999f1343SGarrett Wollman 	}
653999f1343SGarrett Wollman 
6545200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
6555200e00eSIan Dowse 	    0, NULL);
6565200e00eSIan Dowse 	if (oinp != NULL) {
6575200e00eSIan Dowse 		if (oinpp != NULL)
6585200e00eSIan Dowse 			*oinpp = oinp;
659df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
660c3229e05SDavid Greenman 	}
6615200e00eSIan Dowse 	if (lport == 0) {
6625200e00eSIan Dowse 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, td);
6635a903f8dSPierre Beyssac 		if (error)
6645a903f8dSPierre Beyssac 			return (error);
6655a903f8dSPierre Beyssac 	}
6665200e00eSIan Dowse 	*laddrp = laddr.s_addr;
6675200e00eSIan Dowse 	*lportp = lport;
6685200e00eSIan Dowse 	*faddrp = faddr.s_addr;
6695200e00eSIan Dowse 	*fportp = fport;
670df8bae1dSRodney W. Grimes 	return (0);
671df8bae1dSRodney W. Grimes }
672df8bae1dSRodney W. Grimes 
67326f9a767SRodney W. Grimes void
674df8bae1dSRodney W. Grimes in_pcbdisconnect(inp)
675df8bae1dSRodney W. Grimes 	struct inpcb *inp;
676df8bae1dSRodney W. Grimes {
67759daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
680df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
68115bd2b43SDavid Greenman 	in_pcbrehash(inp);
6824cc20ab1SSeigo Tanimura 	if (inp->inp_socket->so_state & SS_NOFDREF)
683df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
6840f9ade71SHajimu UMEMOTO #ifdef IPSEC
6850f9ade71SHajimu UMEMOTO 	ipsec_pcbdisconn(inp->inp_sp);
6860f9ade71SHajimu UMEMOTO #endif
687df8bae1dSRodney W. Grimes }
688df8bae1dSRodney W. Grimes 
68926f9a767SRodney W. Grimes void
690df8bae1dSRodney W. Grimes in_pcbdetach(inp)
691df8bae1dSRodney W. Grimes 	struct inpcb *inp;
692df8bae1dSRodney W. Grimes {
693df8bae1dSRodney W. Grimes 	struct socket *so = inp->inp_socket;
6943d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
695df8bae1dSRodney W. Grimes 
69659daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
69759daba27SSam Leffler 
6980f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
699cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
700cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
7013d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
702c3229e05SDavid Greenman 	in_pcbremlists(inp);
703340c35deSJonathan Lemon 	if (so) {
704df8bae1dSRodney W. Grimes 		so->so_pcb = 0;
705b1e4abd2SMatthew Dillon 		sotryfree(so);
706340c35deSJonathan Lemon 	}
707df8bae1dSRodney W. Grimes 	if (inp->inp_options)
708df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
709be2ac88cSJonathan Lemon 	if (inp->inp_route.ro_rt)
710d1dd20beSSam Leffler 		RTFREE(inp->inp_route.ro_rt);
711df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
712cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
713f76fcf6dSJeffrey Hsu 	INP_LOCK_DESTROY(inp);
714a557af22SRobert Watson #ifdef MAC
715a557af22SRobert Watson 	mac_destroy_inpcb(inp);
716a557af22SRobert Watson #endif
71769c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
718df8bae1dSRodney W. Grimes }
719df8bae1dSRodney W. Grimes 
72026ef6ac4SDon Lewis struct sockaddr *
72126ef6ac4SDon Lewis in_sockaddr(port, addr_p)
72226ef6ac4SDon Lewis 	in_port_t port;
72326ef6ac4SDon Lewis 	struct in_addr *addr_p;
72426ef6ac4SDon Lewis {
72526ef6ac4SDon Lewis 	struct sockaddr_in *sin;
72626ef6ac4SDon Lewis 
72726ef6ac4SDon Lewis 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
728a163d034SWarner Losh 		M_WAITOK | M_ZERO);
72926ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
73026ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
73126ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
73226ef6ac4SDon Lewis 	sin->sin_port = port;
73326ef6ac4SDon Lewis 
73426ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
73526ef6ac4SDon Lewis }
73626ef6ac4SDon Lewis 
737117bcae7SGarrett Wollman /*
738f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
739f76fcf6dSJeffrey Hsu  * The socket must have a valid
740117bcae7SGarrett Wollman  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
741117bcae7SGarrett Wollman  * except through a kernel programming error, so it is acceptable to panic
74257bf258eSGarrett Wollman  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
74357bf258eSGarrett Wollman  * because there actually /is/ a programming error somewhere... XXX)
744117bcae7SGarrett Wollman  */
745117bcae7SGarrett Wollman int
746f76fcf6dSJeffrey Hsu in_setsockaddr(so, nam, pcbinfo)
747117bcae7SGarrett Wollman 	struct socket *so;
74857bf258eSGarrett Wollman 	struct sockaddr **nam;
749f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
750df8bae1dSRodney W. Grimes {
751fdc984f7STor Egge 	int s;
752fdc984f7STor Egge 	register struct inpcb *inp;
75326ef6ac4SDon Lewis 	struct in_addr addr;
75426ef6ac4SDon Lewis 	in_port_t port;
75542fa505bSDavid Greenman 
756fdc984f7STor Egge 	s = splnet();
757f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
758fdc984f7STor Egge 	inp = sotoinpcb(so);
759db112f04STor Egge 	if (!inp) {
760f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
761db112f04STor Egge 		splx(s);
762ff079ca4SPeter Wemm 		return ECONNRESET;
763db112f04STor Egge 	}
764f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
76526ef6ac4SDon Lewis 	port = inp->inp_lport;
76626ef6ac4SDon Lewis 	addr = inp->inp_laddr;
767f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
768f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
769db112f04STor Egge 	splx(s);
77042fa505bSDavid Greenman 
77126ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
772117bcae7SGarrett Wollman 	return 0;
773df8bae1dSRodney W. Grimes }
774df8bae1dSRodney W. Grimes 
775f76fcf6dSJeffrey Hsu /*
776f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
777f76fcf6dSJeffrey Hsu  */
778117bcae7SGarrett Wollman int
779f76fcf6dSJeffrey Hsu in_setpeeraddr(so, nam, pcbinfo)
780117bcae7SGarrett Wollman 	struct socket *so;
78157bf258eSGarrett Wollman 	struct sockaddr **nam;
782f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
783df8bae1dSRodney W. Grimes {
784fdc984f7STor Egge 	int s;
785f76fcf6dSJeffrey Hsu 	register struct inpcb *inp;
78626ef6ac4SDon Lewis 	struct in_addr addr;
78726ef6ac4SDon Lewis 	in_port_t port;
78842fa505bSDavid Greenman 
789fdc984f7STor Egge 	s = splnet();
790f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
791fdc984f7STor Egge 	inp = sotoinpcb(so);
792db112f04STor Egge 	if (!inp) {
793f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
794db112f04STor Egge 		splx(s);
795ff079ca4SPeter Wemm 		return ECONNRESET;
796db112f04STor Egge 	}
797f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
79826ef6ac4SDon Lewis 	port = inp->inp_fport;
79926ef6ac4SDon Lewis 	addr = inp->inp_faddr;
800f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
801f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
802db112f04STor Egge 	splx(s);
80342fa505bSDavid Greenman 
80426ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
805117bcae7SGarrett Wollman 	return 0;
806df8bae1dSRodney W. Grimes }
807df8bae1dSRodney W. Grimes 
80826f9a767SRodney W. Grimes void
809f76fcf6dSJeffrey Hsu in_pcbnotifyall(pcbinfo, faddr, errno, notify)
810f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
811df8bae1dSRodney W. Grimes 	struct in_addr faddr;
812c693a045SJonathan Lemon 	int errno;
8133ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int);
814d1c54148SJesper Skriver {
815c693a045SJonathan Lemon 	struct inpcb *inp, *ninp;
816f76fcf6dSJeffrey Hsu 	struct inpcbhead *head;
817c693a045SJonathan Lemon 	int s;
818d1c54148SJesper Skriver 
819d1c54148SJesper Skriver 	s = splnet();
8203dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
821f76fcf6dSJeffrey Hsu 	head = pcbinfo->listhead;
822c693a045SJonathan Lemon 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
823f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
824c693a045SJonathan Lemon 		ninp = LIST_NEXT(inp, inp_list);
825d1c54148SJesper Skriver #ifdef INET6
826f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
827f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
828d1c54148SJesper Skriver 			continue;
829f76fcf6dSJeffrey Hsu 		}
830d1c54148SJesper Skriver #endif
831d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
832f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
833f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
834d1c54148SJesper Skriver 			continue;
835d1c54148SJesper Skriver 		}
8363dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
837f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
838f76fcf6dSJeffrey Hsu 	}
8393dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
840d1c54148SJesper Skriver 	splx(s);
841d1c54148SJesper Skriver }
842d1c54148SJesper Skriver 
843e43cc4aeSHajimu UMEMOTO void
844f76fcf6dSJeffrey Hsu in_pcbpurgeif0(pcbinfo, ifp)
845f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
846e43cc4aeSHajimu UMEMOTO 	struct ifnet *ifp;
847e43cc4aeSHajimu UMEMOTO {
848e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
849e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
850e43cc4aeSHajimu UMEMOTO 	int i, gap;
851e43cc4aeSHajimu UMEMOTO 
852f76fcf6dSJeffrey Hsu 	/* why no splnet here? XXX */
853f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
8543cfcc388SJeffrey Hsu 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
855f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
856e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
857e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
858e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
859e43cc4aeSHajimu UMEMOTO 			/*
860e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
861e43cc4aeSHajimu UMEMOTO 			 * detached.
862e43cc4aeSHajimu UMEMOTO 			 */
863e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
864e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
865e43cc4aeSHajimu UMEMOTO 
866e43cc4aeSHajimu UMEMOTO 			/*
867e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
868e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
869e43cc4aeSHajimu UMEMOTO 			 */
870e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
871e43cc4aeSHajimu UMEMOTO 			    i++) {
872e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
873e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
874e43cc4aeSHajimu UMEMOTO 					gap++;
875e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
876e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
877e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
878e43cc4aeSHajimu UMEMOTO 			}
879e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
880e43cc4aeSHajimu UMEMOTO 		}
881f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
882e43cc4aeSHajimu UMEMOTO 	}
8833cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
884e43cc4aeSHajimu UMEMOTO }
885e43cc4aeSHajimu UMEMOTO 
886df8bae1dSRodney W. Grimes /*
887df8bae1dSRodney W. Grimes  * Check for alternatives when higher level complains
888df8bae1dSRodney W. Grimes  * about service problems.  For now, invalidate cached
889df8bae1dSRodney W. Grimes  * routing information.  If the route was created dynamically
890df8bae1dSRodney W. Grimes  * (by a redirect), time to try a default gateway again.
891df8bae1dSRodney W. Grimes  */
89226f9a767SRodney W. Grimes void
893df8bae1dSRodney W. Grimes in_losing(inp)
894df8bae1dSRodney W. Grimes 	struct inpcb *inp;
895df8bae1dSRodney W. Grimes {
896df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
897df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
898df8bae1dSRodney W. Grimes 
89959daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
90059daba27SSam Leffler 
901df8bae1dSRodney W. Grimes 	if ((rt = inp->inp_route.ro_rt)) {
902d1dd20beSSam Leffler 		RT_LOCK(rt);
903d1dd20beSSam Leffler 		inp->inp_route.ro_rt = NULL;
904df8bae1dSRodney W. Grimes 		bzero((caddr_t)&info, sizeof(info));
9058071913dSRuslan Ermilov 		info.rti_flags = rt->rt_flags;
9068071913dSRuslan Ermilov 		info.rti_info[RTAX_DST] = rt_key(rt);
907df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
908df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
909df8bae1dSRodney W. Grimes 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
9109c63e9dbSSam Leffler 		if (rt->rt_flags & RTF_DYNAMIC)
9119c63e9dbSSam Leffler 			rtexpunge(rt);
9129c63e9dbSSam Leffler 		RTFREE_LOCKED(rt);
913df8bae1dSRodney W. Grimes 		/*
914df8bae1dSRodney W. Grimes 		 * A new route can be allocated
915df8bae1dSRodney W. Grimes 		 * the next time output is attempted.
916df8bae1dSRodney W. Grimes 		 */
917df8bae1dSRodney W. Grimes 	}
918df8bae1dSRodney W. Grimes }
919df8bae1dSRodney W. Grimes 
920df8bae1dSRodney W. Grimes /*
921df8bae1dSRodney W. Grimes  * After a routing change, flush old routing
922df8bae1dSRodney W. Grimes  * and allocate a (hopefully) better one.
923df8bae1dSRodney W. Grimes  */
9243ce144eaSJeffrey Hsu struct inpcb *
925df8bae1dSRodney W. Grimes in_rtchange(inp, errno)
926df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
927df8bae1dSRodney W. Grimes 	int errno;
928df8bae1dSRodney W. Grimes {
92959daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
93059daba27SSam Leffler 
931df8bae1dSRodney W. Grimes 	if (inp->inp_route.ro_rt) {
932d1dd20beSSam Leffler 		RTFREE(inp->inp_route.ro_rt);
933df8bae1dSRodney W. Grimes 		inp->inp_route.ro_rt = 0;
934df8bae1dSRodney W. Grimes 		/*
935df8bae1dSRodney W. Grimes 		 * A new route can be allocated the next time
936df8bae1dSRodney W. Grimes 		 * output is attempted.
937df8bae1dSRodney W. Grimes 		 */
938df8bae1dSRodney W. Grimes 	}
9393ce144eaSJeffrey Hsu 	return inp;
940df8bae1dSRodney W. Grimes }
941df8bae1dSRodney W. Grimes 
942c3229e05SDavid Greenman /*
943c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
944c3229e05SDavid Greenman  */
945df8bae1dSRodney W. Grimes struct inpcb *
946c3229e05SDavid Greenman in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
9476d6a026bSDavid Greenman 	struct inpcbinfo *pcbinfo;
948c3229e05SDavid Greenman 	struct in_addr laddr;
949c3229e05SDavid Greenman 	u_int lport_arg;
9506d6a026bSDavid Greenman 	int wild_okay;
951df8bae1dSRodney W. Grimes {
952f1d19042SArchie Cobbs 	register struct inpcb *inp;
953df8bae1dSRodney W. Grimes 	int matchwild = 3, wildcard;
954c3229e05SDavid Greenman 	u_short lport = lport_arg;
9557bc4aca7SDavid Greenman 
9561b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
9571b73ca0bSSam Leffler 
958c3229e05SDavid Greenman 	if (!wild_okay) {
959c3229e05SDavid Greenman 		struct inpcbhead *head;
960c3229e05SDavid Greenman 		/*
961c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
962c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
963c3229e05SDavid Greenman 		 */
964c3229e05SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
965fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
966cfa1ca9dSYoshinobu Inoue #ifdef INET6
967369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
968cfa1ca9dSYoshinobu Inoue 				continue;
969cfa1ca9dSYoshinobu Inoue #endif
970c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
971c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
972c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
973c3229e05SDavid Greenman 				/*
974c3229e05SDavid Greenman 				 * Found.
975c3229e05SDavid Greenman 				 */
976c3229e05SDavid Greenman 				return (inp);
977df8bae1dSRodney W. Grimes 			}
978c3229e05SDavid Greenman 		}
979c3229e05SDavid Greenman 		/*
980c3229e05SDavid Greenman 		 * Not found.
981c3229e05SDavid Greenman 		 */
982c3229e05SDavid Greenman 		return (NULL);
983c3229e05SDavid Greenman 	} else {
984c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
985c3229e05SDavid Greenman 		struct inpcbport *phd;
986c3229e05SDavid Greenman 		struct inpcb *match = NULL;
987c3229e05SDavid Greenman 		/*
988c3229e05SDavid Greenman 		 * Best fit PCB lookup.
989c3229e05SDavid Greenman 		 *
990c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
991c3229e05SDavid Greenman 		 * port hash list.
992c3229e05SDavid Greenman 		 */
99396af9ea5SMike Silbersack 		retrylookup:
994c3229e05SDavid Greenman 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
995c3229e05SDavid Greenman 		    pcbinfo->porthashmask)];
996fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
997c3229e05SDavid Greenman 			if (phd->phd_port == lport)
998c3229e05SDavid Greenman 				break;
999c3229e05SDavid Greenman 		}
1000c3229e05SDavid Greenman 		if (phd != NULL) {
1001c3229e05SDavid Greenman 			/*
1002c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1003c3229e05SDavid Greenman 			 * fit.
1004c3229e05SDavid Greenman 			 */
100537d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1006c3229e05SDavid Greenman 				wildcard = 0;
1007cfa1ca9dSYoshinobu Inoue #ifdef INET6
1008369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1009cfa1ca9dSYoshinobu Inoue 					continue;
1010cfa1ca9dSYoshinobu Inoue #endif
101196af9ea5SMike Silbersack 				/*
101296af9ea5SMike Silbersack 				 * Clean out old time_wait sockets if they
101396af9ea5SMike Silbersack 				 * are clogging up needed local ports.
101496af9ea5SMike Silbersack 				 */
101596af9ea5SMike Silbersack 				if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
101696af9ea5SMike Silbersack 					if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) {
1017f7bbe2c0SSam Leffler 						INP_LOCK(inp);
101896af9ea5SMike Silbersack 						tcp_twclose((struct tcptw *)inp->inp_ppcb, 0);
101996af9ea5SMike Silbersack 						match = NULL;
102096af9ea5SMike Silbersack 						goto retrylookup;
102196af9ea5SMike Silbersack 					}
102296af9ea5SMike Silbersack 				}
1023c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1024c3229e05SDavid Greenman 					wildcard++;
102515bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
102615bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
102715bd2b43SDavid Greenman 						wildcard++;
102815bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
102915bd2b43SDavid Greenman 						continue;
103015bd2b43SDavid Greenman 				} else {
103115bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
103215bd2b43SDavid Greenman 						wildcard++;
103315bd2b43SDavid Greenman 				}
1034df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1035df8bae1dSRodney W. Grimes 					match = inp;
1036df8bae1dSRodney W. Grimes 					matchwild = wildcard;
10373dbdc25cSDavid Greenman 					if (matchwild == 0) {
1038df8bae1dSRodney W. Grimes 						break;
1039df8bae1dSRodney W. Grimes 					}
1040df8bae1dSRodney W. Grimes 				}
10413dbdc25cSDavid Greenman 			}
1042c3229e05SDavid Greenman 		}
1043df8bae1dSRodney W. Grimes 		return (match);
1044df8bae1dSRodney W. Grimes 	}
1045c3229e05SDavid Greenman }
104615bd2b43SDavid Greenman 
104715bd2b43SDavid Greenman /*
104815bd2b43SDavid Greenman  * Lookup PCB in hash list.
104915bd2b43SDavid Greenman  */
105015bd2b43SDavid Greenman struct inpcb *
1051cfa1ca9dSYoshinobu Inoue in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
1052cfa1ca9dSYoshinobu Inoue 		  ifp)
105315bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
105415bd2b43SDavid Greenman 	struct in_addr faddr, laddr;
105515bd2b43SDavid Greenman 	u_int fport_arg, lport_arg;
10566d6a026bSDavid Greenman 	int wildcard;
1057cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
105815bd2b43SDavid Greenman {
105915bd2b43SDavid Greenman 	struct inpcbhead *head;
106015bd2b43SDavid Greenman 	register struct inpcb *inp;
106115bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
106215bd2b43SDavid Greenman 
106359daba27SSam Leffler 	INP_INFO_RLOCK_ASSERT(pcbinfo);
106415bd2b43SDavid Greenman 	/*
106515bd2b43SDavid Greenman 	 * First look for an exact match.
106615bd2b43SDavid Greenman 	 */
1067ddd79a97SDavid Greenman 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1068fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1069cfa1ca9dSYoshinobu Inoue #ifdef INET6
1070369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1071cfa1ca9dSYoshinobu Inoue 			continue;
1072cfa1ca9dSYoshinobu Inoue #endif
10736d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1074ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1075ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1076c3229e05SDavid Greenman 		    inp->inp_lport == lport) {
1077c3229e05SDavid Greenman 			/*
1078c3229e05SDavid Greenman 			 * Found.
1079c3229e05SDavid Greenman 			 */
1080c3229e05SDavid Greenman 			return (inp);
1081c3229e05SDavid Greenman 		}
10826d6a026bSDavid Greenman 	}
10836d6a026bSDavid Greenman 	if (wildcard) {
10846d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
1085cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1086cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1087cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10886d6a026bSDavid Greenman 
1089ddd79a97SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1090fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1091cfa1ca9dSYoshinobu Inoue #ifdef INET6
1092369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1093cfa1ca9dSYoshinobu Inoue 				continue;
1094cfa1ca9dSYoshinobu Inoue #endif
10956d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1096c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1097cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
1098cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
1099cfa1ca9dSYoshinobu Inoue 					continue;
11006d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1101c3229e05SDavid Greenman 					return (inp);
1102cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1103cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1104cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1105cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
1106cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
1107cfa1ca9dSYoshinobu Inoue 					else
1108cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
11096d6a026bSDavid Greenman 					local_wild = inp;
11106d6a026bSDavid Greenman 				}
11116d6a026bSDavid Greenman 			}
1112cfa1ca9dSYoshinobu Inoue 		}
1113cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1114cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
1115cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
1116cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
1117c3229e05SDavid Greenman 		return (local_wild);
11186d6a026bSDavid Greenman 	}
1119c3229e05SDavid Greenman 
1120c3229e05SDavid Greenman 	/*
1121c3229e05SDavid Greenman 	 * Not found.
1122c3229e05SDavid Greenman 	 */
11236d6a026bSDavid Greenman 	return (NULL);
112415bd2b43SDavid Greenman }
112515bd2b43SDavid Greenman 
11267bc4aca7SDavid Greenman /*
1127c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
11287bc4aca7SDavid Greenman  */
1129c3229e05SDavid Greenman int
113015bd2b43SDavid Greenman in_pcbinshash(inp)
113115bd2b43SDavid Greenman 	struct inpcb *inp;
113215bd2b43SDavid Greenman {
1133c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1134c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1135c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1136c3229e05SDavid Greenman 	struct inpcbport *phd;
1137cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
113815bd2b43SDavid Greenman 
113959daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1140cfa1ca9dSYoshinobu Inoue #ifdef INET6
1141cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1142cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1143cfa1ca9dSYoshinobu Inoue 	else
1144cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1145cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1146cfa1ca9dSYoshinobu Inoue 
1147cfa1ca9dSYoshinobu Inoue 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1148c3229e05SDavid Greenman 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
114915bd2b43SDavid Greenman 
1150c3229e05SDavid Greenman 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1151c3229e05SDavid Greenman 	    pcbinfo->porthashmask)];
1152c3229e05SDavid Greenman 
1153c3229e05SDavid Greenman 	/*
1154c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1155c3229e05SDavid Greenman 	 */
1156fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1157c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1158c3229e05SDavid Greenman 			break;
1159c3229e05SDavid Greenman 	}
1160c3229e05SDavid Greenman 	/*
1161c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1162c3229e05SDavid Greenman 	 */
1163c3229e05SDavid Greenman 	if (phd == NULL) {
1164c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1165c3229e05SDavid Greenman 		if (phd == NULL) {
1166c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1167c3229e05SDavid Greenman 		}
1168c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1169c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1170c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1171c3229e05SDavid Greenman 	}
1172c3229e05SDavid Greenman 	inp->inp_phd = phd;
1173c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1174c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1175c3229e05SDavid Greenman 	return (0);
117615bd2b43SDavid Greenman }
117715bd2b43SDavid Greenman 
1178c3229e05SDavid Greenman /*
1179c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1180c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1181c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1182c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1183c3229e05SDavid Greenman  */
118415bd2b43SDavid Greenman void
118515bd2b43SDavid Greenman in_pcbrehash(inp)
118615bd2b43SDavid Greenman 	struct inpcb *inp;
118715bd2b43SDavid Greenman {
118859daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
118915bd2b43SDavid Greenman 	struct inpcbhead *head;
1190cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
119115bd2b43SDavid Greenman 
119259daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
119359daba27SSam Leffler 	/* XXX? INP_LOCK_ASSERT(inp); */
1194cfa1ca9dSYoshinobu Inoue #ifdef INET6
1195cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1196cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1197cfa1ca9dSYoshinobu Inoue 	else
1198cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1199cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1200cfa1ca9dSYoshinobu Inoue 
120159daba27SSam Leffler 	head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
120259daba27SSam Leffler 		inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
120315bd2b43SDavid Greenman 
1204c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
120515bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1206c3229e05SDavid Greenman }
1207c3229e05SDavid Greenman 
1208c3229e05SDavid Greenman /*
1209c3229e05SDavid Greenman  * Remove PCB from various lists.
1210c3229e05SDavid Greenman  */
121176429de4SYoshinobu Inoue void
1212c3229e05SDavid Greenman in_pcbremlists(inp)
1213c3229e05SDavid Greenman 	struct inpcb *inp;
1214c3229e05SDavid Greenman {
121559daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
121659daba27SSam Leffler 
121759daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
121859daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
121959daba27SSam Leffler 
122059daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1221c3229e05SDavid Greenman 	if (inp->inp_lport) {
1222c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1223c3229e05SDavid Greenman 
1224c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1225c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1226fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1227c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1228c3229e05SDavid Greenman 			free(phd, M_PCB);
1229c3229e05SDavid Greenman 		}
1230c3229e05SDavid Greenman 	}
1231c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
123259daba27SSam Leffler 	pcbinfo->ipi_count--;
123315bd2b43SDavid Greenman }
123475c13541SPoul-Henning Kamp 
1235a557af22SRobert Watson /*
1236a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1237a557af22SRobert Watson  * label change into the in_pcb for the socket.
1238a557af22SRobert Watson  */
1239a557af22SRobert Watson void
1240a557af22SRobert Watson in_pcbsosetlabel(so)
1241a557af22SRobert Watson 	struct socket *so;
1242a557af22SRobert Watson {
1243a557af22SRobert Watson #ifdef MAC
1244a557af22SRobert Watson 	struct inpcb *inp;
1245a557af22SRobert Watson 
1246a557af22SRobert Watson 	/* XXX: Will assert socket lock when we have them. */
1247a557af22SRobert Watson 	inp = (struct inpcb *)so->so_pcb;
1248a557af22SRobert Watson 	INP_LOCK(inp);
1249a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1250a557af22SRobert Watson 	INP_UNLOCK(inp);
1251a557af22SRobert Watson #endif
1252a557af22SRobert Watson }
1253a557af22SRobert Watson 
125475c13541SPoul-Henning Kamp int
1255ad278afdSJohn Baldwin prison_xinpcb(struct thread *td, struct inpcb *inp)
125675c13541SPoul-Henning Kamp {
1257ad278afdSJohn Baldwin 	if (!jailed(td->td_ucred))
125875c13541SPoul-Henning Kamp 		return (0);
1259ad278afdSJohn Baldwin 	if (ntohl(inp->inp_laddr.s_addr) == prison_getip(td->td_ucred))
126075c13541SPoul-Henning Kamp 		return (0);
126175c13541SPoul-Henning Kamp 	return (1);
126275c13541SPoul-Henning Kamp }
1263