xref: /freebsd/sys/netinet/in_pcb.c (revision 340c35de6a249d9dd8b4f72f7c75c851a158db09)
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"
39cfa1ca9dSYoshinobu Inoue 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
45df8bae1dSRodney W. Grimes #include <sys/protosw.h>
46df8bae1dSRodney W. Grimes #include <sys/socket.h>
47df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
48df8bae1dSRodney W. Grimes #include <sys/proc.h>
4975c13541SPoul-Henning Kamp #include <sys/jail.h>
50101f9fc8SPeter Wemm #include <sys/kernel.h>
51101f9fc8SPeter Wemm #include <sys/sysctl.h>
528781d8e9SBruce Evans 
5308637435SBruce Evans #include <machine/limits.h>
5408637435SBruce Evans 
5569c2d429SJeff Roberson #include <vm/uma.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <net/if.h>
58cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <netinet/in.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
65340c35deSJonathan Lemon #include <netinet/tcp_var.h>
66cfa1ca9dSYoshinobu Inoue #ifdef INET6
67cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
68cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
69cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
70cfa1ca9dSYoshinobu Inoue 
71cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
72cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
73cfa1ca9dSYoshinobu Inoue #include <netkey/key.h>
74cfa1ca9dSYoshinobu Inoue #endif /* IPSEC */
75df8bae1dSRodney W. Grimes 
76b9234fafSSam Leffler #ifdef FAST_IPSEC
77b9234fafSSam Leffler #if defined(IPSEC) || defined(IPSEC_ESP)
78b9234fafSSam Leffler #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
79b9234fafSSam Leffler #endif
80b9234fafSSam Leffler 
81b9234fafSSam Leffler #include <netipsec/ipsec.h>
82b9234fafSSam Leffler #include <netipsec/key.h>
83b9234fafSSam Leffler #define	IPSEC
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 
99bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
100bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
101bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
102bbd42ad0SPeter Wemm 
103bbd42ad0SPeter Wemm static int
10482d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
105bbd42ad0SPeter Wemm {
106bbd42ad0SPeter Wemm 	int error = sysctl_handle_int(oidp,
107bbd42ad0SPeter Wemm 		oidp->oid_arg1, oidp->oid_arg2, req);
108bbd42ad0SPeter Wemm 	if (!error) {
109bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
110bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
111bbd42ad0SPeter Wemm 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
112bbd42ad0SPeter Wemm 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
113bbd42ad0SPeter Wemm 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
114bbd42ad0SPeter Wemm 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
115bbd42ad0SPeter Wemm 	}
116bbd42ad0SPeter Wemm 	return error;
117bbd42ad0SPeter Wemm }
118bbd42ad0SPeter Wemm 
119bbd42ad0SPeter Wemm #undef RANGECHK
120bbd42ad0SPeter Wemm 
12133b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
12233b3ac06SPeter Wemm 
123bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
124bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
125bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
126bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
127bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
128bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
129bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
130bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
131bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
132bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
133bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
134bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
1350312fbe9SPoul-Henning Kamp 
136c3229e05SDavid Greenman /*
137c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
138c3229e05SDavid Greenman  *
139c3229e05SDavid Greenman  * NOTE: It is assumed that most of these functions will be called at
140c3229e05SDavid Greenman  * splnet(). XXX - There are, unfortunately, a few exceptions to this
141c3229e05SDavid Greenman  * rule that should be fixed.
142c3229e05SDavid Greenman  */
143c3229e05SDavid Greenman 
144c3229e05SDavid Greenman /*
145c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
146c3229e05SDavid Greenman  */
147df8bae1dSRodney W. Grimes int
148b40ce416SJulian Elischer in_pcballoc(so, pcbinfo, td)
149df8bae1dSRodney W. Grimes 	struct socket *so;
15015bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
151b40ce416SJulian Elischer 	struct thread *td;
152df8bae1dSRodney W. Grimes {
153df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
15413cf67f3SHajimu UMEMOTO #ifdef IPSEC
15513cf67f3SHajimu UMEMOTO 	int error;
15613cf67f3SHajimu UMEMOTO #endif
157df8bae1dSRodney W. Grimes 
158f76fcf6dSJeffrey Hsu 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
159df8bae1dSRodney W. Grimes 	if (inp == NULL)
160df8bae1dSRodney W. Grimes 		return (ENOBUFS);
161df8bae1dSRodney W. Grimes 	bzero((caddr_t)inp, sizeof(*inp));
1623d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
16315bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
164df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
16513cf67f3SHajimu UMEMOTO #ifdef IPSEC
16613cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
16713cf67f3SHajimu UMEMOTO 	if (error != 0) {
16869c2d429SJeff Roberson 		uma_zfree(pcbinfo->ipi_zone, inp);
16913cf67f3SHajimu UMEMOTO 		return error;
17013cf67f3SHajimu UMEMOTO 	}
17113cf67f3SHajimu UMEMOTO #endif /*IPSEC*/
17275daea93SPaul Saab #if defined(INET6)
173340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
174340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
175340c35deSJonathan Lemon 		if (ip6_v6only)
17633841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
177340c35deSJonathan Lemon 	}
17875daea93SPaul Saab #endif
17915bd2b43SDavid Greenman 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
1803d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
181df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
182f76fcf6dSJeffrey Hsu 	INP_LOCK_INIT(inp, "inp");
18333841545SHajimu UMEMOTO #ifdef INET6
18433841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
18533841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
18633841545SHajimu UMEMOTO #endif
187df8bae1dSRodney W. Grimes 	return (0);
188df8bae1dSRodney W. Grimes }
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes int
191b40ce416SJulian Elischer in_pcbbind(inp, nam, td)
192df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
19357bf258eSGarrett Wollman 	struct sockaddr *nam;
194b40ce416SJulian Elischer 	struct thread *td;
195df8bae1dSRodney W. Grimes {
1964b932371SIan Dowse 	int anonport, error;
1974b932371SIan Dowse 
1984b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
1994b932371SIan Dowse 		return (EINVAL);
2004b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2014b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2024b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
2034b932371SIan Dowse 	    &inp->inp_lport, td);
2044b932371SIan Dowse 	if (error)
2054b932371SIan Dowse 		return (error);
2064b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2074b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2084b932371SIan Dowse 		inp->inp_lport = 0;
2094b932371SIan Dowse 		return (EAGAIN);
2104b932371SIan Dowse 	}
2114b932371SIan Dowse 	if (anonport)
2124b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2134b932371SIan Dowse 	return (0);
2144b932371SIan Dowse }
2154b932371SIan Dowse 
2164b932371SIan Dowse /*
2174b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2184b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2194b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2204b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2214b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2224b932371SIan Dowse  *
2234b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2244b932371SIan Dowse  */
2254b932371SIan Dowse int
2264b932371SIan Dowse in_pcbbind_setup(inp, nam, laddrp, lportp, td)
2274b932371SIan Dowse 	struct inpcb *inp;
2284b932371SIan Dowse 	struct sockaddr *nam;
2294b932371SIan Dowse 	in_addr_t *laddrp;
2304b932371SIan Dowse 	u_short *lportp;
2314b932371SIan Dowse 	struct thread *td;
2324b932371SIan Dowse {
2334b932371SIan Dowse 	struct socket *so = inp->inp_socket;
23437bd2b30SPeter Wemm 	unsigned short *lastport;
23515bd2b43SDavid Greenman 	struct sockaddr_in *sin;
236c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2374b932371SIan Dowse 	struct in_addr laddr;
238df8bae1dSRodney W. Grimes 	u_short lport = 0;
2394cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
24075c13541SPoul-Henning Kamp 	int error, prison = 0;
241df8bae1dSRodney W. Grimes 
24259562606SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
243df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
2444b932371SIan Dowse 	laddr.s_addr = *laddrp;
2454b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
246df8bae1dSRodney W. Grimes 		return (EINVAL);
247c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
2486d6a026bSDavid Greenman 		wild = 1;
249df8bae1dSRodney W. Grimes 	if (nam) {
25057bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
25157bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
252df8bae1dSRodney W. Grimes 			return (EINVAL);
253df8bae1dSRodney W. Grimes #ifdef notdef
254df8bae1dSRodney W. Grimes 		/*
255df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
256df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
257df8bae1dSRodney W. Grimes 		 */
258df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
259df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
260df8bae1dSRodney W. Grimes #endif
261e4bdf25dSPoul-Henning Kamp 		if (sin->sin_addr.s_addr != INADDR_ANY)
262a854ed98SJohn Baldwin 			if (prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr))
26375c13541SPoul-Henning Kamp 				return(EINVAL);
2644b932371SIan Dowse 		if (sin->sin_port != *lportp) {
2654b932371SIan Dowse 			/* Don't allow the port to change. */
2664b932371SIan Dowse 			if (*lportp != 0)
2674b932371SIan Dowse 				return (EINVAL);
268df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
2694b932371SIan Dowse 		}
2704b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
271df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
272df8bae1dSRodney W. Grimes 			/*
273df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
274df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
275df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
276df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
277df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
278df8bae1dSRodney W. Grimes 			 */
279df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
280df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
281df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
282df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
28383103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
284df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
285df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
286df8bae1dSRodney W. Grimes 		}
2874b932371SIan Dowse 		laddr = sin->sin_addr;
288df8bae1dSRodney W. Grimes 		if (lport) {
289df8bae1dSRodney W. Grimes 			struct inpcb *t;
290df8bae1dSRodney W. Grimes 			/* GROSS */
29144731cabSJohn Baldwin 			if (ntohs(lport) < IPPORT_RESERVED && td &&
29244731cabSJohn Baldwin 			    suser_cred(td->td_ucred, PRISON_ROOT))
2932469dd60SGarrett Wollman 				return (EACCES);
294a854ed98SJohn Baldwin 			if (td && jailed(td->td_ucred))
29575c13541SPoul-Henning Kamp 				prison = 1;
2962f9a2132SBrian Feldman 			if (so->so_cred->cr_uid != 0 &&
29752b65dbeSBill Fenner 			    !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
2984049a042SGuido van Rooij 				t = in_pcblookup_local(inp->inp_pcbinfo,
29975c13541SPoul-Henning Kamp 				    sin->sin_addr, lport,
30075c13541SPoul-Henning Kamp 				    prison ? 0 :  INPLOOKUP_WILDCARD);
301340c35deSJonathan Lemon 	/*
302340c35deSJonathan Lemon 	 * XXX
303340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
304340c35deSJonathan Lemon 	 */
305340c35deSJonathan Lemon 				if (t && (t->inp_vflag & INP_TIMEWAIT)) {
306340c35deSJonathan Lemon 					if ((ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
307340c35deSJonathan Lemon 					    ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
308340c35deSJonathan Lemon 					    (intotw(t)->tw_so_options & SO_REUSEPORT) == 0) &&
309340c35deSJonathan Lemon 					    (so->so_cred->cr_uid != intotw(t)->tw_cred->cr_uid))
310340c35deSJonathan Lemon 						return (EADDRINUSE);
311340c35deSJonathan Lemon 				} else
3124cc20ab1SSeigo Tanimura 				if (t &&
3134cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
31452b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
31552b65dbeSBill Fenner 				     (t->inp_socket->so_options &
31652b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
3172f9a2132SBrian Feldman 				    (so->so_cred->cr_uid !=
318cfa1ca9dSYoshinobu Inoue 				     t->inp_socket->so_cred->cr_uid)) {
319cfa1ca9dSYoshinobu Inoue #if defined(INET6)
32033841545SHajimu UMEMOTO 					if (ntohl(sin->sin_addr.s_addr) !=
321cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
322cfa1ca9dSYoshinobu Inoue 					    ntohl(t->inp_laddr.s_addr) !=
323cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
324cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(so) ==
325cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(t->inp_socket))
326cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
3274049a042SGuido van Rooij 					return (EADDRINUSE);
3284049a042SGuido van Rooij 				}
329cfa1ca9dSYoshinobu Inoue 			}
330970680faSPoul-Henning Kamp 			if (prison &&
331a854ed98SJohn Baldwin 			    prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr))
332970680faSPoul-Henning Kamp 				return (EADDRNOTAVAIL);
333c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
33475c13541SPoul-Henning Kamp 			    lport, prison ? 0 : wild);
335340c35deSJonathan Lemon 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
336340c35deSJonathan Lemon 				if ((reuseport & intotw(t)->tw_so_options) == 0)
337340c35deSJonathan Lemon 					return (EADDRINUSE);
338340c35deSJonathan Lemon 			} else
3394cc20ab1SSeigo Tanimura 			if (t &&
3404cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
341cfa1ca9dSYoshinobu Inoue #if defined(INET6)
34233841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
343cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
344cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
345cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
346cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
347cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
348cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
349df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
350df8bae1dSRodney W. Grimes 			}
351cfa1ca9dSYoshinobu Inoue 		}
352df8bae1dSRodney W. Grimes 	}
3534b932371SIan Dowse 	if (*lportp != 0)
3544b932371SIan Dowse 		lport = *lportp;
35533b3ac06SPeter Wemm 	if (lport == 0) {
35633b3ac06SPeter Wemm 		ushort first, last;
35733b3ac06SPeter Wemm 		int count;
35833b3ac06SPeter Wemm 
3594b932371SIan Dowse 		if (laddr.s_addr != INADDR_ANY)
3604b932371SIan Dowse 			if (prison_ip(td->td_ucred, 0, &laddr.s_addr))
36175c13541SPoul-Henning Kamp 				return (EINVAL);
362321a2846SPoul-Henning Kamp 
36333b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
36433b3ac06SPeter Wemm 			first = ipport_hifirstauto;	/* sysctl */
36533b3ac06SPeter Wemm 			last  = ipport_hilastauto;
366c3229e05SDavid Greenman 			lastport = &pcbinfo->lasthi;
36733b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
3684b932371SIan Dowse 			if (td && (error = suser_cred(td->td_ucred,
3694b932371SIan Dowse 			    PRISON_ROOT)) != 0)
370a29f300eSGarrett Wollman 				return error;
371bbd42ad0SPeter Wemm 			first = ipport_lowfirstauto;	/* 1023 */
372bbd42ad0SPeter Wemm 			last  = ipport_lowlastauto;	/* 600 */
373c3229e05SDavid Greenman 			lastport = &pcbinfo->lastlow;
37433b3ac06SPeter Wemm 		} else {
37533b3ac06SPeter Wemm 			first = ipport_firstauto;	/* sysctl */
37633b3ac06SPeter Wemm 			last  = ipport_lastauto;
377c3229e05SDavid Greenman 			lastport = &pcbinfo->lastport;
37833b3ac06SPeter Wemm 		}
37933b3ac06SPeter Wemm 		/*
38033b3ac06SPeter Wemm 		 * Simple check to ensure all ports are not used up causing
38133b3ac06SPeter Wemm 		 * a deadlock here.
38233b3ac06SPeter Wemm 		 *
38333b3ac06SPeter Wemm 		 * We split the two cases (up and down) so that the direction
38433b3ac06SPeter Wemm 		 * is not being tested on each round of the loop.
38533b3ac06SPeter Wemm 		 */
38633b3ac06SPeter Wemm 		if (first > last) {
38733b3ac06SPeter Wemm 			/*
38833b3ac06SPeter Wemm 			 * counting down
38933b3ac06SPeter Wemm 			 */
39033b3ac06SPeter Wemm 			count = first - last;
39133b3ac06SPeter Wemm 
392df8bae1dSRodney W. Grimes 			do {
3934b932371SIan Dowse 				if (count-- < 0)	/* completely used? */
394550b1518SWes Peters 					return (EADDRNOTAVAIL);
39533b3ac06SPeter Wemm 				--*lastport;
39633b3ac06SPeter Wemm 				if (*lastport > first || *lastport < last)
39733b3ac06SPeter Wemm 					*lastport = first;
39815bd2b43SDavid Greenman 				lport = htons(*lastport);
3994b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4004b932371SIan Dowse 			    wild));
40133b3ac06SPeter Wemm 		} else {
40233b3ac06SPeter Wemm 			/*
40333b3ac06SPeter Wemm 			 * counting up
40433b3ac06SPeter Wemm 			 */
40533b3ac06SPeter Wemm 			count = last - first;
40633b3ac06SPeter Wemm 
40733b3ac06SPeter Wemm 			do {
4084b932371SIan Dowse 				if (count-- < 0)	/* completely used? */
409550b1518SWes Peters 					return (EADDRNOTAVAIL);
41033b3ac06SPeter Wemm 				++*lastport;
41133b3ac06SPeter Wemm 				if (*lastport < first || *lastport > last)
41233b3ac06SPeter Wemm 					*lastport = first;
41333b3ac06SPeter Wemm 				lport = htons(*lastport);
4144b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4154b932371SIan Dowse 			    wild));
41633b3ac06SPeter Wemm 		}
41733b3ac06SPeter Wemm 	}
4184b932371SIan Dowse 	if (prison_ip(td->td_ucred, 0, &laddr.s_addr))
419e4bdf25dSPoul-Henning Kamp 		return (EINVAL);
4204b932371SIan Dowse 	*laddrp = laddr.s_addr;
4214b932371SIan Dowse 	*lportp = lport;
422df8bae1dSRodney W. Grimes 	return (0);
423df8bae1dSRodney W. Grimes }
424df8bae1dSRodney W. Grimes 
425999f1343SGarrett Wollman /*
4265200e00eSIan Dowse  * Connect from a socket to a specified address.
4275200e00eSIan Dowse  * Both address and port must be specified in argument sin.
4285200e00eSIan Dowse  * If don't have a local address for this socket yet,
4295200e00eSIan Dowse  * then pick one.
430999f1343SGarrett Wollman  */
431999f1343SGarrett Wollman int
4325200e00eSIan Dowse in_pcbconnect(inp, nam, td)
433999f1343SGarrett Wollman 	register struct inpcb *inp;
43457bf258eSGarrett Wollman 	struct sockaddr *nam;
4355200e00eSIan Dowse 	struct thread *td;
436999f1343SGarrett Wollman {
4375200e00eSIan Dowse 	u_short lport, fport;
4385200e00eSIan Dowse 	in_addr_t laddr, faddr;
4395200e00eSIan Dowse 	int anonport, error;
440df8bae1dSRodney W. Grimes 
4415200e00eSIan Dowse 	lport = inp->inp_lport;
4425200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
4435200e00eSIan Dowse 	anonport = (lport == 0);
4445200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
4455200e00eSIan Dowse 	    NULL, td);
4465200e00eSIan Dowse 	if (error)
4475200e00eSIan Dowse 		return (error);
4485200e00eSIan Dowse 
4495200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
4505200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
4515200e00eSIan Dowse 		inp->inp_lport = lport;
4525200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
4535200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
4545200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
4555200e00eSIan Dowse 			inp->inp_lport = 0;
4565200e00eSIan Dowse 			return (EAGAIN);
4575200e00eSIan Dowse 		}
4585200e00eSIan Dowse 	}
4595200e00eSIan Dowse 
4605200e00eSIan Dowse 	/* Commit the remaining changes. */
4615200e00eSIan Dowse 	inp->inp_lport = lport;
4625200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
4635200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
4645200e00eSIan Dowse 	inp->inp_fport = fport;
4655200e00eSIan Dowse 	in_pcbrehash(inp);
4665200e00eSIan Dowse 	if (anonport)
4675200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
4685200e00eSIan Dowse 	return (0);
4695200e00eSIan Dowse }
4705200e00eSIan Dowse 
4715200e00eSIan Dowse /*
4725200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
4735200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
4745200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
4755200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
4765200e00eSIan Dowse  * the connect.
4775200e00eSIan Dowse  *
4785200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
4795200e00eSIan Dowse  * and port. These are not updated in the error case.
4805200e00eSIan Dowse  *
4815200e00eSIan Dowse  * If the operation fails because the connection already exists,
4825200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
4835200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
4845200e00eSIan Dowse  * is set to NULL.
4855200e00eSIan Dowse  */
4865200e00eSIan Dowse int
4875200e00eSIan Dowse in_pcbconnect_setup(inp, nam, laddrp, lportp, faddrp, fportp, oinpp, td)
4885200e00eSIan Dowse 	register struct inpcb *inp;
4895200e00eSIan Dowse 	struct sockaddr *nam;
4905200e00eSIan Dowse 	in_addr_t *laddrp;
4915200e00eSIan Dowse 	u_short *lportp;
4925200e00eSIan Dowse 	in_addr_t *faddrp;
4935200e00eSIan Dowse 	u_short *fportp;
4945200e00eSIan Dowse 	struct inpcb **oinpp;
4955200e00eSIan Dowse 	struct thread *td;
4965200e00eSIan Dowse {
4975200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
4985200e00eSIan Dowse 	struct in_ifaddr *ia;
4995200e00eSIan Dowse 	struct sockaddr_in sa;
5005200e00eSIan Dowse 	struct ucred *cred;
5015200e00eSIan Dowse 	struct inpcb *oinp;
5025200e00eSIan Dowse 	struct in_addr laddr, faddr;
5035200e00eSIan Dowse 	u_short lport, fport;
5045200e00eSIan Dowse 	int error;
5055200e00eSIan Dowse 
5065200e00eSIan Dowse 	if (oinpp != NULL)
5075200e00eSIan Dowse 		*oinpp = NULL;
50857bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
509df8bae1dSRodney W. Grimes 		return (EINVAL);
510df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
511df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
512df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
513df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5145200e00eSIan Dowse 	laddr.s_addr = *laddrp;
5155200e00eSIan Dowse 	lport = *lportp;
5165200e00eSIan Dowse 	faddr = sin->sin_addr;
5175200e00eSIan Dowse 	fport = sin->sin_port;
5185200e00eSIan Dowse 	cred = inp->inp_socket->so_cred;
5195200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY && jailed(cred)) {
5205200e00eSIan Dowse 		bzero(&sa, sizeof(sa));
5215200e00eSIan Dowse 		sa.sin_addr.s_addr = htonl(prison_getip(cred));
5225200e00eSIan Dowse 		sa.sin_len = sizeof(sa);
5235200e00eSIan Dowse 		sa.sin_family = AF_INET;
5245200e00eSIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
5255200e00eSIan Dowse 		    &laddr.s_addr, &lport, td);
5265200e00eSIan Dowse 		if (error)
5275200e00eSIan Dowse 			return (error);
5285200e00eSIan Dowse 	}
5295200e00eSIan Dowse 
53059562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
531df8bae1dSRodney W. Grimes 		/*
532df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
533df8bae1dSRodney W. Grimes 		 * use the primary local address.
534df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
535df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
536df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
537df8bae1dSRodney W. Grimes 		 */
5385200e00eSIan Dowse 		if (faddr.s_addr == INADDR_ANY)
5395200e00eSIan Dowse 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
5405200e00eSIan Dowse 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
5415200e00eSIan Dowse 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
5425200e00eSIan Dowse 		    IFF_BROADCAST))
5435200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
5445200e00eSIan Dowse 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
545df8bae1dSRodney W. Grimes 	}
5465200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
547df8bae1dSRodney W. Grimes 		register struct route *ro;
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
550df8bae1dSRodney W. Grimes 		/*
551df8bae1dSRodney W. Grimes 		 * If route is known or can be allocated now,
552df8bae1dSRodney W. Grimes 		 * our src addr is taken from the i/f, else punt.
553a4a6e773SHajimu UMEMOTO 		 * Note that we should check the address family of the cached
554a4a6e773SHajimu UMEMOTO 		 * destination, in case of sharing the cache with IPv6.
555df8bae1dSRodney W. Grimes 		 */
556df8bae1dSRodney W. Grimes 		ro = &inp->inp_route;
557df8bae1dSRodney W. Grimes 		if (ro->ro_rt &&
558a4a6e773SHajimu UMEMOTO 		    (ro->ro_dst.sa_family != AF_INET ||
5595200e00eSIan Dowse 		     satosin(&ro->ro_dst)->sin_addr.s_addr != faddr.s_addr ||
560df8bae1dSRodney W. Grimes 		     inp->inp_socket->so_options & SO_DONTROUTE)) {
561df8bae1dSRodney W. Grimes 			RTFREE(ro->ro_rt);
562df8bae1dSRodney W. Grimes 			ro->ro_rt = (struct rtentry *)0;
563df8bae1dSRodney W. Grimes 		}
564df8bae1dSRodney W. Grimes 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
565df8bae1dSRodney W. Grimes 		    (ro->ro_rt == (struct rtentry *)0 ||
566df8bae1dSRodney W. Grimes 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
567df8bae1dSRodney W. Grimes 			/* No route yet, so try to acquire one */
568a4a6e773SHajimu UMEMOTO 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
569df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_family = AF_INET;
570df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
5715200e00eSIan Dowse 			((struct sockaddr_in *)&ro->ro_dst)->sin_addr = faddr;
572df8bae1dSRodney W. Grimes 			rtalloc(ro);
5734cc20ab1SSeigo Tanimura 		}
574df8bae1dSRodney W. Grimes 		/*
575df8bae1dSRodney W. Grimes 		 * If we found a route, use the address
576df8bae1dSRodney W. Grimes 		 * corresponding to the outgoing interface
577df8bae1dSRodney W. Grimes 		 * unless it is the loopback (in case a route
578df8bae1dSRodney W. Grimes 		 * to our address on another net goes to loopback).
579df8bae1dSRodney W. Grimes 		 */
580df8bae1dSRodney W. Grimes 		if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
581df8bae1dSRodney W. Grimes 			ia = ifatoia(ro->ro_rt->rt_ifa);
582df8bae1dSRodney W. Grimes 		if (ia == 0) {
5835200e00eSIan Dowse 			bzero(&sa, sizeof(sa));
5845200e00eSIan Dowse 			sa.sin_addr = faddr;
5855200e00eSIan Dowse 			sa.sin_len = sizeof(sa);
5865200e00eSIan Dowse 			sa.sin_family = AF_INET;
587df8bae1dSRodney W. Grimes 
5885200e00eSIan Dowse 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
589df8bae1dSRodney W. Grimes 			if (ia == 0)
5905200e00eSIan Dowse 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
591df8bae1dSRodney W. Grimes 			if (ia == 0)
592fc2ffbe6SPoul-Henning Kamp 				ia = TAILQ_FIRST(&in_ifaddrhead);
593df8bae1dSRodney W. Grimes 			if (ia == 0)
594df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
595df8bae1dSRodney W. Grimes 		}
596df8bae1dSRodney W. Grimes 		/*
597df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
598df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
599df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
600df8bae1dSRodney W. Grimes 		 */
6015200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
602df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
603df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
604df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
607df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
608df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
60937d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
610df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
611df8bae1dSRodney W. Grimes 						break;
612df8bae1dSRodney W. Grimes 				if (ia == 0)
613df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
614df8bae1dSRodney W. Grimes 			}
615df8bae1dSRodney W. Grimes 		}
6165200e00eSIan Dowse 		laddr = ia->ia_addr.sin_addr;
617999f1343SGarrett Wollman 	}
618999f1343SGarrett Wollman 
6195200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
6205200e00eSIan Dowse 	    0, NULL);
6215200e00eSIan Dowse 	if (oinp != NULL) {
6225200e00eSIan Dowse 		if (oinpp != NULL)
6235200e00eSIan Dowse 			*oinpp = oinp;
624df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
625c3229e05SDavid Greenman 	}
6265200e00eSIan Dowse 	if (lport == 0) {
6275200e00eSIan Dowse 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, td);
6285a903f8dSPierre Beyssac 		if (error)
6295a903f8dSPierre Beyssac 			return (error);
6305a903f8dSPierre Beyssac 	}
6315200e00eSIan Dowse 	*laddrp = laddr.s_addr;
6325200e00eSIan Dowse 	*lportp = lport;
6335200e00eSIan Dowse 	*faddrp = faddr.s_addr;
6345200e00eSIan Dowse 	*fportp = fport;
635df8bae1dSRodney W. Grimes 	return (0);
636df8bae1dSRodney W. Grimes }
637df8bae1dSRodney W. Grimes 
63826f9a767SRodney W. Grimes void
639df8bae1dSRodney W. Grimes in_pcbdisconnect(inp)
640df8bae1dSRodney W. Grimes 	struct inpcb *inp;
641df8bae1dSRodney W. Grimes {
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
644df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
64515bd2b43SDavid Greenman 	in_pcbrehash(inp);
6464cc20ab1SSeigo Tanimura 	if (inp->inp_socket->so_state & SS_NOFDREF)
647df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
648df8bae1dSRodney W. Grimes }
649df8bae1dSRodney W. Grimes 
65026f9a767SRodney W. Grimes void
651df8bae1dSRodney W. Grimes in_pcbdetach(inp)
652df8bae1dSRodney W. Grimes 	struct inpcb *inp;
653df8bae1dSRodney W. Grimes {
654df8bae1dSRodney W. Grimes 	struct socket *so = inp->inp_socket;
6553d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
656df8bae1dSRodney W. Grimes 
657cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
658cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
659cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
6603d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
661c3229e05SDavid Greenman 	in_pcbremlists(inp);
662340c35deSJonathan Lemon 	if (so) {
663df8bae1dSRodney W. Grimes 		so->so_pcb = 0;
664b1e4abd2SMatthew Dillon 		sotryfree(so);
665340c35deSJonathan Lemon 	}
666df8bae1dSRodney W. Grimes 	if (inp->inp_options)
667df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
668be2ac88cSJonathan Lemon 	if (inp->inp_route.ro_rt)
669be2ac88cSJonathan Lemon 		rtfree(inp->inp_route.ro_rt);
670df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
671cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
672f76fcf6dSJeffrey Hsu 	INP_LOCK_DESTROY(inp);
67369c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
674df8bae1dSRodney W. Grimes }
675df8bae1dSRodney W. Grimes 
67626ef6ac4SDon Lewis struct sockaddr *
67726ef6ac4SDon Lewis in_sockaddr(port, addr_p)
67826ef6ac4SDon Lewis 	in_port_t port;
67926ef6ac4SDon Lewis 	struct in_addr *addr_p;
68026ef6ac4SDon Lewis {
68126ef6ac4SDon Lewis 	struct sockaddr_in *sin;
68226ef6ac4SDon Lewis 
68326ef6ac4SDon Lewis 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
684a163d034SWarner Losh 		M_WAITOK | M_ZERO);
68526ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
68626ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
68726ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
68826ef6ac4SDon Lewis 	sin->sin_port = port;
68926ef6ac4SDon Lewis 
69026ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
69126ef6ac4SDon Lewis }
69226ef6ac4SDon Lewis 
693117bcae7SGarrett Wollman /*
694f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
695f76fcf6dSJeffrey Hsu  * The socket must have a valid
696117bcae7SGarrett Wollman  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
697117bcae7SGarrett Wollman  * except through a kernel programming error, so it is acceptable to panic
69857bf258eSGarrett Wollman  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
69957bf258eSGarrett Wollman  * because there actually /is/ a programming error somewhere... XXX)
700117bcae7SGarrett Wollman  */
701117bcae7SGarrett Wollman int
702f76fcf6dSJeffrey Hsu in_setsockaddr(so, nam, pcbinfo)
703117bcae7SGarrett Wollman 	struct socket *so;
70457bf258eSGarrett Wollman 	struct sockaddr **nam;
705f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
706df8bae1dSRodney W. Grimes {
707fdc984f7STor Egge 	int s;
708fdc984f7STor Egge 	register struct inpcb *inp;
70926ef6ac4SDon Lewis 	struct in_addr addr;
71026ef6ac4SDon Lewis 	in_port_t port;
71142fa505bSDavid Greenman 
712fdc984f7STor Egge 	s = splnet();
713f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
714fdc984f7STor Egge 	inp = sotoinpcb(so);
715db112f04STor Egge 	if (!inp) {
716f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
717db112f04STor Egge 		splx(s);
718ff079ca4SPeter Wemm 		return ECONNRESET;
719db112f04STor Egge 	}
720f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
72126ef6ac4SDon Lewis 	port = inp->inp_lport;
72226ef6ac4SDon Lewis 	addr = inp->inp_laddr;
723f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
724f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
725db112f04STor Egge 	splx(s);
72642fa505bSDavid Greenman 
72726ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
728117bcae7SGarrett Wollman 	return 0;
729df8bae1dSRodney W. Grimes }
730df8bae1dSRodney W. Grimes 
731f76fcf6dSJeffrey Hsu /*
732f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
733f76fcf6dSJeffrey Hsu  */
734117bcae7SGarrett Wollman int
735f76fcf6dSJeffrey Hsu in_setpeeraddr(so, nam, pcbinfo)
736117bcae7SGarrett Wollman 	struct socket *so;
73757bf258eSGarrett Wollman 	struct sockaddr **nam;
738f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
739df8bae1dSRodney W. Grimes {
740fdc984f7STor Egge 	int s;
741f76fcf6dSJeffrey Hsu 	register struct inpcb *inp;
74226ef6ac4SDon Lewis 	struct in_addr addr;
74326ef6ac4SDon Lewis 	in_port_t port;
74442fa505bSDavid Greenman 
745fdc984f7STor Egge 	s = splnet();
746f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
747fdc984f7STor Egge 	inp = sotoinpcb(so);
748db112f04STor Egge 	if (!inp) {
749f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(pcbinfo);
750db112f04STor Egge 		splx(s);
751ff079ca4SPeter Wemm 		return ECONNRESET;
752db112f04STor Egge 	}
753f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
75426ef6ac4SDon Lewis 	port = inp->inp_fport;
75526ef6ac4SDon Lewis 	addr = inp->inp_faddr;
756f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
757f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
758db112f04STor Egge 	splx(s);
75942fa505bSDavid Greenman 
76026ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
761117bcae7SGarrett Wollman 	return 0;
762df8bae1dSRodney W. Grimes }
763df8bae1dSRodney W. Grimes 
76426f9a767SRodney W. Grimes void
765f76fcf6dSJeffrey Hsu in_pcbnotifyall(pcbinfo, faddr, errno, notify)
766f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
767df8bae1dSRodney W. Grimes 	struct in_addr faddr;
768c693a045SJonathan Lemon 	int errno;
7693ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int);
770d1c54148SJesper Skriver {
771c693a045SJonathan Lemon 	struct inpcb *inp, *ninp;
772f76fcf6dSJeffrey Hsu 	struct inpcbhead *head;
773c693a045SJonathan Lemon 	int s;
774d1c54148SJesper Skriver 
775d1c54148SJesper Skriver 	s = splnet();
7763dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
777f76fcf6dSJeffrey Hsu 	head = pcbinfo->listhead;
778c693a045SJonathan Lemon 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
779f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
780c693a045SJonathan Lemon 		ninp = LIST_NEXT(inp, inp_list);
781d1c54148SJesper Skriver #ifdef INET6
782f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
783f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
784d1c54148SJesper Skriver 			continue;
785f76fcf6dSJeffrey Hsu 		}
786d1c54148SJesper Skriver #endif
787d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
788f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
789f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
790d1c54148SJesper Skriver 			continue;
791d1c54148SJesper Skriver 		}
7923dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
793f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
794f76fcf6dSJeffrey Hsu 	}
7953dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
796d1c54148SJesper Skriver 	splx(s);
797d1c54148SJesper Skriver }
798d1c54148SJesper Skriver 
799e43cc4aeSHajimu UMEMOTO void
800f76fcf6dSJeffrey Hsu in_pcbpurgeif0(pcbinfo, ifp)
801f76fcf6dSJeffrey Hsu 	struct inpcbinfo *pcbinfo;
802e43cc4aeSHajimu UMEMOTO 	struct ifnet *ifp;
803e43cc4aeSHajimu UMEMOTO {
804e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
805e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
806e43cc4aeSHajimu UMEMOTO 	int i, gap;
807e43cc4aeSHajimu UMEMOTO 
808f76fcf6dSJeffrey Hsu 	/* why no splnet here? XXX */
809f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
8103cfcc388SJeffrey Hsu 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
811f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
812e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
813e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
814e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
815e43cc4aeSHajimu UMEMOTO 			/*
816e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
817e43cc4aeSHajimu UMEMOTO 			 * detached.
818e43cc4aeSHajimu UMEMOTO 			 */
819e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
820e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
821e43cc4aeSHajimu UMEMOTO 
822e43cc4aeSHajimu UMEMOTO 			/*
823e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
824e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
825e43cc4aeSHajimu UMEMOTO 			 */
826e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
827e43cc4aeSHajimu UMEMOTO 			    i++) {
828e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
829e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
830e43cc4aeSHajimu UMEMOTO 					gap++;
831e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
832e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
833e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
834e43cc4aeSHajimu UMEMOTO 			}
835e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
836e43cc4aeSHajimu UMEMOTO 		}
837f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
838e43cc4aeSHajimu UMEMOTO 	}
8393cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
840e43cc4aeSHajimu UMEMOTO }
841e43cc4aeSHajimu UMEMOTO 
842df8bae1dSRodney W. Grimes /*
843df8bae1dSRodney W. Grimes  * Check for alternatives when higher level complains
844df8bae1dSRodney W. Grimes  * about service problems.  For now, invalidate cached
845df8bae1dSRodney W. Grimes  * routing information.  If the route was created dynamically
846df8bae1dSRodney W. Grimes  * (by a redirect), time to try a default gateway again.
847df8bae1dSRodney W. Grimes  */
84826f9a767SRodney W. Grimes void
849df8bae1dSRodney W. Grimes in_losing(inp)
850df8bae1dSRodney W. Grimes 	struct inpcb *inp;
851df8bae1dSRodney W. Grimes {
852df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
853df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
854df8bae1dSRodney W. Grimes 
855df8bae1dSRodney W. Grimes 	if ((rt = inp->inp_route.ro_rt)) {
856df8bae1dSRodney W. Grimes 		bzero((caddr_t)&info, sizeof(info));
8578071913dSRuslan Ermilov 		info.rti_flags = rt->rt_flags;
8588071913dSRuslan Ermilov 		info.rti_info[RTAX_DST] = rt_key(rt);
859df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
860df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
861df8bae1dSRodney W. Grimes 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
862df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_DYNAMIC)
8638071913dSRuslan Ermilov 			(void) rtrequest1(RTM_DELETE, &info, NULL);
8648071913dSRuslan Ermilov 		inp->inp_route.ro_rt = NULL;
8658bf82a92SRuslan Ermilov 		rtfree(rt);
866df8bae1dSRodney W. Grimes 		/*
867df8bae1dSRodney W. Grimes 		 * A new route can be allocated
868df8bae1dSRodney W. Grimes 		 * the next time output is attempted.
869df8bae1dSRodney W. Grimes 		 */
870df8bae1dSRodney W. Grimes 	}
871df8bae1dSRodney W. Grimes }
872df8bae1dSRodney W. Grimes 
873df8bae1dSRodney W. Grimes /*
874df8bae1dSRodney W. Grimes  * After a routing change, flush old routing
875df8bae1dSRodney W. Grimes  * and allocate a (hopefully) better one.
876df8bae1dSRodney W. Grimes  */
8773ce144eaSJeffrey Hsu struct inpcb *
878df8bae1dSRodney W. Grimes in_rtchange(inp, errno)
879df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
880df8bae1dSRodney W. Grimes 	int errno;
881df8bae1dSRodney W. Grimes {
882df8bae1dSRodney W. Grimes 	if (inp->inp_route.ro_rt) {
883df8bae1dSRodney W. Grimes 		rtfree(inp->inp_route.ro_rt);
884df8bae1dSRodney W. Grimes 		inp->inp_route.ro_rt = 0;
885df8bae1dSRodney W. Grimes 		/*
886df8bae1dSRodney W. Grimes 		 * A new route can be allocated the next time
887df8bae1dSRodney W. Grimes 		 * output is attempted.
888df8bae1dSRodney W. Grimes 		 */
889df8bae1dSRodney W. Grimes 	}
8903ce144eaSJeffrey Hsu 	return inp;
891df8bae1dSRodney W. Grimes }
892df8bae1dSRodney W. Grimes 
893c3229e05SDavid Greenman /*
894c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
895c3229e05SDavid Greenman  */
896df8bae1dSRodney W. Grimes struct inpcb *
897c3229e05SDavid Greenman in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
8986d6a026bSDavid Greenman 	struct inpcbinfo *pcbinfo;
899c3229e05SDavid Greenman 	struct in_addr laddr;
900c3229e05SDavid Greenman 	u_int lport_arg;
9016d6a026bSDavid Greenman 	int wild_okay;
902df8bae1dSRodney W. Grimes {
903f1d19042SArchie Cobbs 	register struct inpcb *inp;
904df8bae1dSRodney W. Grimes 	int matchwild = 3, wildcard;
905c3229e05SDavid Greenman 	u_short lport = lport_arg;
9067bc4aca7SDavid Greenman 
907c3229e05SDavid Greenman 	if (!wild_okay) {
908c3229e05SDavid Greenman 		struct inpcbhead *head;
909c3229e05SDavid Greenman 		/*
910c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
911c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
912c3229e05SDavid Greenman 		 */
913c3229e05SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
914fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
915cfa1ca9dSYoshinobu Inoue #ifdef INET6
916369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
917cfa1ca9dSYoshinobu Inoue 				continue;
918cfa1ca9dSYoshinobu Inoue #endif
919c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
920c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
921c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
922c3229e05SDavid Greenman 				/*
923c3229e05SDavid Greenman 				 * Found.
924c3229e05SDavid Greenman 				 */
925c3229e05SDavid Greenman 				return (inp);
926df8bae1dSRodney W. Grimes 			}
927c3229e05SDavid Greenman 		}
928c3229e05SDavid Greenman 		/*
929c3229e05SDavid Greenman 		 * Not found.
930c3229e05SDavid Greenman 		 */
931c3229e05SDavid Greenman 		return (NULL);
932c3229e05SDavid Greenman 	} else {
933c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
934c3229e05SDavid Greenman 		struct inpcbport *phd;
935c3229e05SDavid Greenman 		struct inpcb *match = NULL;
936c3229e05SDavid Greenman 		/*
937c3229e05SDavid Greenman 		 * Best fit PCB lookup.
938c3229e05SDavid Greenman 		 *
939c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
940c3229e05SDavid Greenman 		 * port hash list.
941c3229e05SDavid Greenman 		 */
942c3229e05SDavid Greenman 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
943c3229e05SDavid Greenman 		    pcbinfo->porthashmask)];
944fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
945c3229e05SDavid Greenman 			if (phd->phd_port == lport)
946c3229e05SDavid Greenman 				break;
947c3229e05SDavid Greenman 		}
948c3229e05SDavid Greenman 		if (phd != NULL) {
949c3229e05SDavid Greenman 			/*
950c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
951c3229e05SDavid Greenman 			 * fit.
952c3229e05SDavid Greenman 			 */
95337d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
954c3229e05SDavid Greenman 				wildcard = 0;
955cfa1ca9dSYoshinobu Inoue #ifdef INET6
956369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
957cfa1ca9dSYoshinobu Inoue 					continue;
958cfa1ca9dSYoshinobu Inoue #endif
959c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
960c3229e05SDavid Greenman 					wildcard++;
96115bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
96215bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
96315bd2b43SDavid Greenman 						wildcard++;
96415bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
96515bd2b43SDavid Greenman 						continue;
96615bd2b43SDavid Greenman 				} else {
96715bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
96815bd2b43SDavid Greenman 						wildcard++;
96915bd2b43SDavid Greenman 				}
970df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
971df8bae1dSRodney W. Grimes 					match = inp;
972df8bae1dSRodney W. Grimes 					matchwild = wildcard;
9733dbdc25cSDavid Greenman 					if (matchwild == 0) {
974df8bae1dSRodney W. Grimes 						break;
975df8bae1dSRodney W. Grimes 					}
976df8bae1dSRodney W. Grimes 				}
9773dbdc25cSDavid Greenman 			}
978c3229e05SDavid Greenman 		}
979df8bae1dSRodney W. Grimes 		return (match);
980df8bae1dSRodney W. Grimes 	}
981c3229e05SDavid Greenman }
98215bd2b43SDavid Greenman 
98315bd2b43SDavid Greenman /*
98415bd2b43SDavid Greenman  * Lookup PCB in hash list.
98515bd2b43SDavid Greenman  */
98615bd2b43SDavid Greenman struct inpcb *
987cfa1ca9dSYoshinobu Inoue in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
988cfa1ca9dSYoshinobu Inoue 		  ifp)
98915bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
99015bd2b43SDavid Greenman 	struct in_addr faddr, laddr;
99115bd2b43SDavid Greenman 	u_int fport_arg, lport_arg;
9926d6a026bSDavid Greenman 	int wildcard;
993cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
99415bd2b43SDavid Greenman {
99515bd2b43SDavid Greenman 	struct inpcbhead *head;
99615bd2b43SDavid Greenman 	register struct inpcb *inp;
99715bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
99815bd2b43SDavid Greenman 
99915bd2b43SDavid Greenman 	/*
100015bd2b43SDavid Greenman 	 * First look for an exact match.
100115bd2b43SDavid Greenman 	 */
1002ddd79a97SDavid Greenman 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1003fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1004cfa1ca9dSYoshinobu Inoue #ifdef INET6
1005369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1006cfa1ca9dSYoshinobu Inoue 			continue;
1007cfa1ca9dSYoshinobu Inoue #endif
10086d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1009ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1010ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1011c3229e05SDavid Greenman 		    inp->inp_lport == lport) {
1012c3229e05SDavid Greenman 			/*
1013c3229e05SDavid Greenman 			 * Found.
1014c3229e05SDavid Greenman 			 */
1015c3229e05SDavid Greenman 			return (inp);
1016c3229e05SDavid Greenman 		}
10176d6a026bSDavid Greenman 	}
10186d6a026bSDavid Greenman 	if (wildcard) {
10196d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
1020cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1021cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1022cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10236d6a026bSDavid Greenman 
1024ddd79a97SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1025fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1026cfa1ca9dSYoshinobu Inoue #ifdef INET6
1027369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1028cfa1ca9dSYoshinobu Inoue 				continue;
1029cfa1ca9dSYoshinobu Inoue #endif
10306d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1031c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1032cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
1033cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
1034cfa1ca9dSYoshinobu Inoue 					continue;
10356d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1036c3229e05SDavid Greenman 					return (inp);
1037cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1038cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1039cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1040cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
1041cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
1042cfa1ca9dSYoshinobu Inoue 					else
1043cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
10446d6a026bSDavid Greenman 					local_wild = inp;
10456d6a026bSDavid Greenman 				}
10466d6a026bSDavid Greenman 			}
1047cfa1ca9dSYoshinobu Inoue 		}
1048cfa1ca9dSYoshinobu Inoue #if defined(INET6)
1049cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
1050cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
1051cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
1052c3229e05SDavid Greenman 		return (local_wild);
10536d6a026bSDavid Greenman 	}
1054c3229e05SDavid Greenman 
1055c3229e05SDavid Greenman 	/*
1056c3229e05SDavid Greenman 	 * Not found.
1057c3229e05SDavid Greenman 	 */
10586d6a026bSDavid Greenman 	return (NULL);
105915bd2b43SDavid Greenman }
106015bd2b43SDavid Greenman 
10617bc4aca7SDavid Greenman /*
1062c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
10637bc4aca7SDavid Greenman  */
1064c3229e05SDavid Greenman int
106515bd2b43SDavid Greenman in_pcbinshash(inp)
106615bd2b43SDavid Greenman 	struct inpcb *inp;
106715bd2b43SDavid Greenman {
1068c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1069c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1070c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1071c3229e05SDavid Greenman 	struct inpcbport *phd;
1072cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
107315bd2b43SDavid Greenman 
1074cfa1ca9dSYoshinobu Inoue #ifdef INET6
1075cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1076cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1077cfa1ca9dSYoshinobu Inoue 	else
1078cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1079cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1080cfa1ca9dSYoshinobu Inoue 
1081cfa1ca9dSYoshinobu Inoue 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1082c3229e05SDavid Greenman 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
108315bd2b43SDavid Greenman 
1084c3229e05SDavid Greenman 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1085c3229e05SDavid Greenman 	    pcbinfo->porthashmask)];
1086c3229e05SDavid Greenman 
1087c3229e05SDavid Greenman 	/*
1088c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1089c3229e05SDavid Greenman 	 */
1090fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1091c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1092c3229e05SDavid Greenman 			break;
1093c3229e05SDavid Greenman 	}
1094c3229e05SDavid Greenman 	/*
1095c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1096c3229e05SDavid Greenman 	 */
1097c3229e05SDavid Greenman 	if (phd == NULL) {
1098c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1099c3229e05SDavid Greenman 		if (phd == NULL) {
1100c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1101c3229e05SDavid Greenman 		}
1102c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1103c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1104c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1105c3229e05SDavid Greenman 	}
1106c3229e05SDavid Greenman 	inp->inp_phd = phd;
1107c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1108c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1109c3229e05SDavid Greenman 	return (0);
111015bd2b43SDavid Greenman }
111115bd2b43SDavid Greenman 
1112c3229e05SDavid Greenman /*
1113c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1114c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1115c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1116c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1117c3229e05SDavid Greenman  */
111815bd2b43SDavid Greenman void
111915bd2b43SDavid Greenman in_pcbrehash(inp)
112015bd2b43SDavid Greenman 	struct inpcb *inp;
112115bd2b43SDavid Greenman {
112215bd2b43SDavid Greenman 	struct inpcbhead *head;
1123cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
112415bd2b43SDavid Greenman 
1125cfa1ca9dSYoshinobu Inoue #ifdef INET6
1126cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1127cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1128cfa1ca9dSYoshinobu Inoue 	else
1129cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1130cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1131cfa1ca9dSYoshinobu Inoue 
1132cfa1ca9dSYoshinobu Inoue 	head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1133ddd79a97SDavid Greenman 		inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
113415bd2b43SDavid Greenman 
1135c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
113615bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1137c3229e05SDavid Greenman }
1138c3229e05SDavid Greenman 
1139c3229e05SDavid Greenman /*
1140c3229e05SDavid Greenman  * Remove PCB from various lists.
1141c3229e05SDavid Greenman  */
114276429de4SYoshinobu Inoue void
1143c3229e05SDavid Greenman in_pcbremlists(inp)
1144c3229e05SDavid Greenman 	struct inpcb *inp;
1145c3229e05SDavid Greenman {
114698271db4SGarrett Wollman 	inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1147c3229e05SDavid Greenman 	if (inp->inp_lport) {
1148c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1149c3229e05SDavid Greenman 
1150c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1151c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1152fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1153c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1154c3229e05SDavid Greenman 			free(phd, M_PCB);
1155c3229e05SDavid Greenman 		}
1156c3229e05SDavid Greenman 	}
1157c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
11583d4d47f3SGarrett Wollman 	inp->inp_pcbinfo->ipi_count--;
115915bd2b43SDavid Greenman }
116075c13541SPoul-Henning Kamp 
116175c13541SPoul-Henning Kamp int
1162ad278afdSJohn Baldwin prison_xinpcb(struct thread *td, struct inpcb *inp)
116375c13541SPoul-Henning Kamp {
1164ad278afdSJohn Baldwin 	if (!jailed(td->td_ucred))
116575c13541SPoul-Henning Kamp 		return (0);
1166ad278afdSJohn Baldwin 	if (ntohl(inp->inp_laddr.s_addr) == prison_getip(td->td_ucred))
116775c13541SPoul-Henning Kamp 		return (0);
116875c13541SPoul-Henning Kamp 	return (1);
116975c13541SPoul-Henning Kamp }
1170