xref: /freebsd/sys/netinet/in_pcb.c (revision 69c2d429c13b29b1cb66d1990d969d1314f5960f)
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>
65cfa1ca9dSYoshinobu Inoue #ifdef INET6
66cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
67cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
68cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
69cfa1ca9dSYoshinobu Inoue 
70cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
71cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
72cfa1ca9dSYoshinobu Inoue #include <netkey/key.h>
73cfa1ca9dSYoshinobu Inoue #endif /* IPSEC */
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes struct	in_addr zeroin_addr;
76df8bae1dSRodney W. Grimes 
77101f9fc8SPeter Wemm /*
78101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
79101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
80101f9fc8SPeter Wemm  */
8182cd038dSYoshinobu Inoue int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
8282cd038dSYoshinobu Inoue int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
8382cd038dSYoshinobu Inoue int	ipport_firstauto = IPPORT_RESERVED;		/* 1024 */
8482cd038dSYoshinobu Inoue int	ipport_lastauto  = IPPORT_USERRESERVED;		/* 5000 */
8582cd038dSYoshinobu Inoue int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
8682cd038dSYoshinobu Inoue int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
87101f9fc8SPeter Wemm 
88bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
89bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
90bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
91bbd42ad0SPeter Wemm 
92bbd42ad0SPeter Wemm static int
9382d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
94bbd42ad0SPeter Wemm {
95bbd42ad0SPeter Wemm 	int error = sysctl_handle_int(oidp,
96bbd42ad0SPeter Wemm 		oidp->oid_arg1, oidp->oid_arg2, req);
97bbd42ad0SPeter Wemm 	if (!error) {
98bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
99bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
100bbd42ad0SPeter Wemm 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
101bbd42ad0SPeter Wemm 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
102bbd42ad0SPeter Wemm 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
103bbd42ad0SPeter Wemm 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
104bbd42ad0SPeter Wemm 	}
105bbd42ad0SPeter Wemm 	return error;
106bbd42ad0SPeter Wemm }
107bbd42ad0SPeter Wemm 
108bbd42ad0SPeter Wemm #undef RANGECHK
109bbd42ad0SPeter Wemm 
11033b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
11133b3ac06SPeter Wemm 
112bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
113bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
114bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
115bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
116bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
117bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
118bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
119bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
120bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
121bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
122bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
123bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
1240312fbe9SPoul-Henning Kamp 
125c3229e05SDavid Greenman /*
126c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
127c3229e05SDavid Greenman  *
128c3229e05SDavid Greenman  * NOTE: It is assumed that most of these functions will be called at
129c3229e05SDavid Greenman  * splnet(). XXX - There are, unfortunately, a few exceptions to this
130c3229e05SDavid Greenman  * rule that should be fixed.
131c3229e05SDavid Greenman  */
132c3229e05SDavid Greenman 
133c3229e05SDavid Greenman /*
134c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
135c3229e05SDavid Greenman  */
136df8bae1dSRodney W. Grimes int
137b40ce416SJulian Elischer in_pcballoc(so, pcbinfo, td)
138df8bae1dSRodney W. Grimes 	struct socket *so;
13915bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
140b40ce416SJulian Elischer 	struct thread *td;
141df8bae1dSRodney W. Grimes {
142df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
14313cf67f3SHajimu UMEMOTO #ifdef IPSEC
14413cf67f3SHajimu UMEMOTO 	int error;
14513cf67f3SHajimu UMEMOTO #endif
146df8bae1dSRodney W. Grimes 
14769c2d429SJeff Roberson 	inp = uma_zalloc(pcbinfo->ipi_zone, M_WAITOK);
148df8bae1dSRodney W. Grimes 	if (inp == NULL)
149df8bae1dSRodney W. Grimes 		return (ENOBUFS);
150df8bae1dSRodney W. Grimes 	bzero((caddr_t)inp, sizeof(*inp));
1513d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
15215bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
153df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
15413cf67f3SHajimu UMEMOTO #ifdef IPSEC
15513cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
15613cf67f3SHajimu UMEMOTO 	if (error != 0) {
15769c2d429SJeff Roberson 		uma_zfree(pcbinfo->ipi_zone, inp);
15813cf67f3SHajimu UMEMOTO 		return error;
15913cf67f3SHajimu UMEMOTO 	}
16013cf67f3SHajimu UMEMOTO #endif /*IPSEC*/
16175daea93SPaul Saab #if defined(INET6)
16233841545SHajimu UMEMOTO 	if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on)
16333841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_IPV6_V6ONLY;
16475daea93SPaul Saab #endif
16515bd2b43SDavid Greenman 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
1663d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
167df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
16833841545SHajimu UMEMOTO #ifdef INET6
16933841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
17033841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
17133841545SHajimu UMEMOTO #endif
172df8bae1dSRodney W. Grimes 	return (0);
173df8bae1dSRodney W. Grimes }
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes int
176b40ce416SJulian Elischer in_pcbbind(inp, nam, td)
177df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
17857bf258eSGarrett Wollman 	struct sockaddr *nam;
179b40ce416SJulian Elischer 	struct thread *td;
180df8bae1dSRodney W. Grimes {
181b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
182df8bae1dSRodney W. Grimes 	register struct socket *so = inp->inp_socket;
18337bd2b30SPeter Wemm 	unsigned short *lastport;
18415bd2b43SDavid Greenman 	struct sockaddr_in *sin;
185c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
186df8bae1dSRodney W. Grimes 	u_short lport = 0;
187df8bae1dSRodney W. Grimes 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
18875c13541SPoul-Henning Kamp 	int error, prison = 0;
189df8bae1dSRodney W. Grimes 
19059562606SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
191df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
192df8bae1dSRodney W. Grimes 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
193df8bae1dSRodney W. Grimes 		return (EINVAL);
194c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
1956d6a026bSDavid Greenman 		wild = 1;
196df8bae1dSRodney W. Grimes 	if (nam) {
19757bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
19857bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
199df8bae1dSRodney W. Grimes 			return (EINVAL);
200df8bae1dSRodney W. Grimes #ifdef notdef
201df8bae1dSRodney W. Grimes 		/*
202df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
203df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
204df8bae1dSRodney W. Grimes 		 */
205df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
206df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
207df8bae1dSRodney W. Grimes #endif
208e4bdf25dSPoul-Henning Kamp 		if (sin->sin_addr.s_addr != INADDR_ANY)
209a854ed98SJohn Baldwin 			if (prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr))
21075c13541SPoul-Henning Kamp 				return(EINVAL);
211df8bae1dSRodney W. Grimes 		lport = sin->sin_port;
212df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
213df8bae1dSRodney W. Grimes 			/*
214df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
215df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
216df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
217df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
218df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
219df8bae1dSRodney W. Grimes 			 */
220df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
221df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
222df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
223df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
22483103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
225df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
226df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
227df8bae1dSRodney W. Grimes 		}
228df8bae1dSRodney W. Grimes 		if (lport) {
229df8bae1dSRodney W. Grimes 			struct inpcb *t;
230df8bae1dSRodney W. Grimes 			/* GROSS */
23157bf258eSGarrett Wollman 			if (ntohs(lport) < IPPORT_RESERVED && p &&
23275c13541SPoul-Henning Kamp 			    suser_xxx(0, p, PRISON_ROOT))
2332469dd60SGarrett Wollman 				return (EACCES);
234a854ed98SJohn Baldwin 			if (td && jailed(td->td_ucred))
23575c13541SPoul-Henning Kamp 				prison = 1;
2362f9a2132SBrian Feldman 			if (so->so_cred->cr_uid != 0 &&
23752b65dbeSBill Fenner 			    !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
2384049a042SGuido van Rooij 				t = in_pcblookup_local(inp->inp_pcbinfo,
23975c13541SPoul-Henning Kamp 				    sin->sin_addr, lport,
24075c13541SPoul-Henning Kamp 				    prison ? 0 :  INPLOOKUP_WILDCARD);
24152b65dbeSBill Fenner 				if (t &&
24252b65dbeSBill Fenner 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
24352b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
24452b65dbeSBill Fenner 				     (t->inp_socket->so_options &
24552b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
2462f9a2132SBrian Feldman 				    (so->so_cred->cr_uid !=
247cfa1ca9dSYoshinobu Inoue 				     t->inp_socket->so_cred->cr_uid)) {
248cfa1ca9dSYoshinobu Inoue #if defined(INET6)
24933841545SHajimu UMEMOTO 					if (ntohl(sin->sin_addr.s_addr) !=
250cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
251cfa1ca9dSYoshinobu Inoue 					    ntohl(t->inp_laddr.s_addr) !=
252cfa1ca9dSYoshinobu Inoue 					    INADDR_ANY ||
253cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(so) ==
254cfa1ca9dSYoshinobu Inoue 					    INP_SOCKAF(t->inp_socket))
255cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
2564049a042SGuido van Rooij 					return (EADDRINUSE);
2574049a042SGuido van Rooij 				}
258cfa1ca9dSYoshinobu Inoue 			}
259970680faSPoul-Henning Kamp 			if (prison &&
260a854ed98SJohn Baldwin 			    prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr))
261970680faSPoul-Henning Kamp 				return (EADDRNOTAVAIL);
262c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
26375c13541SPoul-Henning Kamp 			    lport, prison ? 0 : wild);
264cfa1ca9dSYoshinobu Inoue 			if (t &&
265cfa1ca9dSYoshinobu Inoue 			    (reuseport & t->inp_socket->so_options) == 0) {
266cfa1ca9dSYoshinobu Inoue #if defined(INET6)
26733841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
268cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
269cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
270cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
271cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
272cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
273cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
274df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
275df8bae1dSRodney W. Grimes 			}
276cfa1ca9dSYoshinobu Inoue 		}
277df8bae1dSRodney W. Grimes 		inp->inp_laddr = sin->sin_addr;
278df8bae1dSRodney W. Grimes 	}
27933b3ac06SPeter Wemm 	if (lport == 0) {
28033b3ac06SPeter Wemm 		ushort first, last;
28133b3ac06SPeter Wemm 		int count;
28233b3ac06SPeter Wemm 
283e4bdf25dSPoul-Henning Kamp 		if (inp->inp_laddr.s_addr != INADDR_ANY)
284a854ed98SJohn Baldwin 			if (prison_ip(td->td_ucred, 0, &inp->inp_laddr.s_addr )) {
285503d3c02SPoul-Henning Kamp 				inp->inp_laddr.s_addr = INADDR_ANY;
28675c13541SPoul-Henning Kamp 				return (EINVAL);
287503d3c02SPoul-Henning Kamp 			}
288321a2846SPoul-Henning Kamp 		inp->inp_flags |= INP_ANONPORT;
289321a2846SPoul-Henning Kamp 
29033b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
29133b3ac06SPeter Wemm 			first = ipport_hifirstauto;	/* sysctl */
29233b3ac06SPeter Wemm 			last  = ipport_hilastauto;
293c3229e05SDavid Greenman 			lastport = &pcbinfo->lasthi;
29433b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
295503d3c02SPoul-Henning Kamp 			if (p && (error = suser_xxx(0, p, PRISON_ROOT))) {
296503d3c02SPoul-Henning Kamp 				inp->inp_laddr.s_addr = INADDR_ANY;
297a29f300eSGarrett Wollman 				return error;
298503d3c02SPoul-Henning Kamp 			}
299bbd42ad0SPeter Wemm 			first = ipport_lowfirstauto;	/* 1023 */
300bbd42ad0SPeter Wemm 			last  = ipport_lowlastauto;	/* 600 */
301c3229e05SDavid Greenman 			lastport = &pcbinfo->lastlow;
30233b3ac06SPeter Wemm 		} else {
30333b3ac06SPeter Wemm 			first = ipport_firstauto;	/* sysctl */
30433b3ac06SPeter Wemm 			last  = ipport_lastauto;
305c3229e05SDavid Greenman 			lastport = &pcbinfo->lastport;
30633b3ac06SPeter Wemm 		}
30733b3ac06SPeter Wemm 		/*
30833b3ac06SPeter Wemm 		 * Simple check to ensure all ports are not used up causing
30933b3ac06SPeter Wemm 		 * a deadlock here.
31033b3ac06SPeter Wemm 		 *
31133b3ac06SPeter Wemm 		 * We split the two cases (up and down) so that the direction
31233b3ac06SPeter Wemm 		 * is not being tested on each round of the loop.
31333b3ac06SPeter Wemm 		 */
31433b3ac06SPeter Wemm 		if (first > last) {
31533b3ac06SPeter Wemm 			/*
31633b3ac06SPeter Wemm 			 * counting down
31733b3ac06SPeter Wemm 			 */
31833b3ac06SPeter Wemm 			count = first - last;
31933b3ac06SPeter Wemm 
320df8bae1dSRodney W. Grimes 			do {
321c3229e05SDavid Greenman 				if (count-- < 0) {	/* completely used? */
322c3229e05SDavid Greenman 					inp->inp_laddr.s_addr = INADDR_ANY;
323550b1518SWes Peters 					return (EADDRNOTAVAIL);
324c3229e05SDavid Greenman 				}
32533b3ac06SPeter Wemm 				--*lastport;
32633b3ac06SPeter Wemm 				if (*lastport > first || *lastport < last)
32733b3ac06SPeter Wemm 					*lastport = first;
32815bd2b43SDavid Greenman 				lport = htons(*lastport);
329c3229e05SDavid Greenman 			} while (in_pcblookup_local(pcbinfo,
330c3229e05SDavid Greenman 				 inp->inp_laddr, lport, wild));
33133b3ac06SPeter Wemm 		} else {
33233b3ac06SPeter Wemm 			/*
33333b3ac06SPeter Wemm 			 * counting up
33433b3ac06SPeter Wemm 			 */
33533b3ac06SPeter Wemm 			count = last - first;
33633b3ac06SPeter Wemm 
33733b3ac06SPeter Wemm 			do {
338c3229e05SDavid Greenman 				if (count-- < 0) {	/* completely used? */
339c3229e05SDavid Greenman 					/*
340c3229e05SDavid Greenman 					 * Undo any address bind that may have
341c3229e05SDavid Greenman 					 * occurred above.
342c3229e05SDavid Greenman 					 */
343c3229e05SDavid Greenman 					inp->inp_laddr.s_addr = INADDR_ANY;
344550b1518SWes Peters 					return (EADDRNOTAVAIL);
345c3229e05SDavid Greenman 				}
34633b3ac06SPeter Wemm 				++*lastport;
34733b3ac06SPeter Wemm 				if (*lastport < first || *lastport > last)
34833b3ac06SPeter Wemm 					*lastport = first;
34933b3ac06SPeter Wemm 				lport = htons(*lastport);
350c3229e05SDavid Greenman 			} while (in_pcblookup_local(pcbinfo,
351c3229e05SDavid Greenman 				 inp->inp_laddr, lport, wild));
35233b3ac06SPeter Wemm 		}
35333b3ac06SPeter Wemm 	}
354df8bae1dSRodney W. Grimes 	inp->inp_lport = lport;
355a854ed98SJohn Baldwin 	if (prison_ip(td->td_ucred, 0, &inp->inp_laddr.s_addr)) {
356503d3c02SPoul-Henning Kamp 		inp->inp_laddr.s_addr = INADDR_ANY;
357503d3c02SPoul-Henning Kamp 		inp->inp_lport = 0;
358e4bdf25dSPoul-Henning Kamp 		return (EINVAL);
359503d3c02SPoul-Henning Kamp 	}
360c3229e05SDavid Greenman 	if (in_pcbinshash(inp) != 0) {
361c3229e05SDavid Greenman 		inp->inp_laddr.s_addr = INADDR_ANY;
362c3229e05SDavid Greenman 		inp->inp_lport = 0;
363c3229e05SDavid Greenman 		return (EAGAIN);
364c3229e05SDavid Greenman 	}
365df8bae1dSRodney W. Grimes 	return (0);
366df8bae1dSRodney W. Grimes }
367df8bae1dSRodney W. Grimes 
368999f1343SGarrett Wollman /*
369999f1343SGarrett Wollman  *   Transform old in_pcbconnect() into an inner subroutine for new
370999f1343SGarrett Wollman  *   in_pcbconnect(): Do some validity-checking on the remote
371999f1343SGarrett Wollman  *   address (in mbuf 'nam') and then determine local host address
372999f1343SGarrett Wollman  *   (i.e., which interface) to use to access that remote host.
373999f1343SGarrett Wollman  *
374999f1343SGarrett Wollman  *   This preserves definition of in_pcbconnect(), while supporting a
375999f1343SGarrett Wollman  *   slightly different version for T/TCP.  (This is more than
376999f1343SGarrett Wollman  *   a bit of a kludge, but cleaning up the internal interfaces would
377999f1343SGarrett Wollman  *   have forced minor changes in every protocol).
378999f1343SGarrett Wollman  */
379999f1343SGarrett Wollman 
380999f1343SGarrett Wollman int
381999f1343SGarrett Wollman in_pcbladdr(inp, nam, plocal_sin)
382999f1343SGarrett Wollman 	register struct inpcb *inp;
38357bf258eSGarrett Wollman 	struct sockaddr *nam;
384999f1343SGarrett Wollman 	struct sockaddr_in **plocal_sin;
385999f1343SGarrett Wollman {
386df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
38757bf258eSGarrett Wollman 	register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
388df8bae1dSRodney W. Grimes 
38957bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
390df8bae1dSRodney W. Grimes 		return (EINVAL);
391df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
392df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
393df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
394df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
39559562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
396df8bae1dSRodney W. Grimes 		/*
397df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
398df8bae1dSRodney W. Grimes 		 * use the primary local address.
399df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
400df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
401df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
402df8bae1dSRodney W. Grimes 		 */
403df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY)
404fc2ffbe6SPoul-Henning Kamp 		    sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
405df8bae1dSRodney W. Grimes 		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
406fc2ffbe6SPoul-Henning Kamp 		  (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
407fc2ffbe6SPoul-Henning Kamp 		    sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
408df8bae1dSRodney W. Grimes 	}
409df8bae1dSRodney W. Grimes 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
410df8bae1dSRodney W. Grimes 		register struct route *ro;
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
413df8bae1dSRodney W. Grimes 		/*
414df8bae1dSRodney W. Grimes 		 * If route is known or can be allocated now,
415df8bae1dSRodney W. Grimes 		 * our src addr is taken from the i/f, else punt.
416a4a6e773SHajimu UMEMOTO 		 * Note that we should check the address family of the cached
417a4a6e773SHajimu UMEMOTO 		 * destination, in case of sharing the cache with IPv6.
418df8bae1dSRodney W. Grimes 		 */
419df8bae1dSRodney W. Grimes 		ro = &inp->inp_route;
420df8bae1dSRodney W. Grimes 		if (ro->ro_rt &&
421a4a6e773SHajimu UMEMOTO 		    (ro->ro_dst.sa_family != AF_INET ||
422a4a6e773SHajimu UMEMOTO 		     satosin(&ro->ro_dst)->sin_addr.s_addr !=
423df8bae1dSRodney W. Grimes 		     sin->sin_addr.s_addr ||
424df8bae1dSRodney W. Grimes 		     inp->inp_socket->so_options & SO_DONTROUTE)) {
425df8bae1dSRodney W. Grimes 			RTFREE(ro->ro_rt);
426df8bae1dSRodney W. Grimes 			ro->ro_rt = (struct rtentry *)0;
427df8bae1dSRodney W. Grimes 		}
428df8bae1dSRodney W. Grimes 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
429df8bae1dSRodney W. Grimes 		    (ro->ro_rt == (struct rtentry *)0 ||
430df8bae1dSRodney W. Grimes 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
431df8bae1dSRodney W. Grimes 			/* No route yet, so try to acquire one */
432a4a6e773SHajimu UMEMOTO 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
433df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_family = AF_INET;
434df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
435df8bae1dSRodney W. Grimes 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
436df8bae1dSRodney W. Grimes 				sin->sin_addr;
437df8bae1dSRodney W. Grimes 			rtalloc(ro);
438df8bae1dSRodney W. Grimes 		}
439df8bae1dSRodney W. Grimes 		/*
440df8bae1dSRodney W. Grimes 		 * If we found a route, use the address
441df8bae1dSRodney W. Grimes 		 * corresponding to the outgoing interface
442df8bae1dSRodney W. Grimes 		 * unless it is the loopback (in case a route
443df8bae1dSRodney W. Grimes 		 * to our address on another net goes to loopback).
444df8bae1dSRodney W. Grimes 		 */
445df8bae1dSRodney W. Grimes 		if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
446df8bae1dSRodney W. Grimes 			ia = ifatoia(ro->ro_rt->rt_ifa);
447df8bae1dSRodney W. Grimes 		if (ia == 0) {
448df8bae1dSRodney W. Grimes 			u_short fport = sin->sin_port;
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes 			sin->sin_port = 0;
451df8bae1dSRodney W. Grimes 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
452df8bae1dSRodney W. Grimes 			if (ia == 0)
453df8bae1dSRodney W. Grimes 				ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
454df8bae1dSRodney W. Grimes 			sin->sin_port = fport;
455df8bae1dSRodney W. Grimes 			if (ia == 0)
456fc2ffbe6SPoul-Henning Kamp 				ia = TAILQ_FIRST(&in_ifaddrhead);
457df8bae1dSRodney W. Grimes 			if (ia == 0)
458df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
459df8bae1dSRodney W. Grimes 		}
460df8bae1dSRodney W. Grimes 		/*
461df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
462df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
463df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
464df8bae1dSRodney W. Grimes 		 */
465df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
466df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
467df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
468df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
471df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
472df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
47337d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
474df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
475df8bae1dSRodney W. Grimes 						break;
476df8bae1dSRodney W. Grimes 				if (ia == 0)
477df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
478df8bae1dSRodney W. Grimes 			}
479df8bae1dSRodney W. Grimes 		}
480999f1343SGarrett Wollman 	/*
481999f1343SGarrett Wollman 	 * Don't do pcblookup call here; return interface in plocal_sin
482999f1343SGarrett Wollman 	 * and exit to caller, that will do the lookup.
483999f1343SGarrett Wollman 	 */
484999f1343SGarrett Wollman 		*plocal_sin = &ia->ia_addr;
485999f1343SGarrett Wollman 
486999f1343SGarrett Wollman 	}
487999f1343SGarrett Wollman 	return(0);
488999f1343SGarrett Wollman }
489999f1343SGarrett Wollman 
490999f1343SGarrett Wollman /*
491999f1343SGarrett Wollman  * Outer subroutine:
492999f1343SGarrett Wollman  * Connect from a socket to a specified address.
493999f1343SGarrett Wollman  * Both address and port must be specified in argument sin.
494999f1343SGarrett Wollman  * If don't have a local address for this socket yet,
495999f1343SGarrett Wollman  * then pick one.
496999f1343SGarrett Wollman  */
497999f1343SGarrett Wollman int
498b40ce416SJulian Elischer in_pcbconnect(inp, nam, td)
499999f1343SGarrett Wollman 	register struct inpcb *inp;
50057bf258eSGarrett Wollman 	struct sockaddr *nam;
501b40ce416SJulian Elischer 	struct thread *td;
502999f1343SGarrett Wollman {
503999f1343SGarrett Wollman 	struct sockaddr_in *ifaddr;
504e4bdf25dSPoul-Henning Kamp 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
505e4bdf25dSPoul-Henning Kamp 	struct sockaddr_in sa;
50691421ba2SRobert Watson 	struct ucred *cred;
507999f1343SGarrett Wollman 	int error;
508999f1343SGarrett Wollman 
50991421ba2SRobert Watson 	cred = inp->inp_socket->so_cred;
51091421ba2SRobert Watson 	if (inp->inp_laddr.s_addr == INADDR_ANY && jailed(cred)) {
511e4bdf25dSPoul-Henning Kamp 		bzero(&sa, sizeof (sa));
51201137630SRobert Watson 		sa.sin_addr.s_addr = htonl(prison_getip(cred));
513e4bdf25dSPoul-Henning Kamp 		sa.sin_len=sizeof (sa);
514e4bdf25dSPoul-Henning Kamp 		sa.sin_family = AF_INET;
515b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
516e4bdf25dSPoul-Henning Kamp 		if (error)
517e4bdf25dSPoul-Henning Kamp 			return (error);
518e4bdf25dSPoul-Henning Kamp 	}
519999f1343SGarrett Wollman 	/*
520999f1343SGarrett Wollman 	 *   Call inner routine, to assign local interface address.
521999f1343SGarrett Wollman 	 */
522831a80b0SMatthew Dillon 	if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
523999f1343SGarrett Wollman 		return(error);
524999f1343SGarrett Wollman 
525c3229e05SDavid Greenman 	if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
526df8bae1dSRodney W. Grimes 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
527cfa1ca9dSYoshinobu Inoue 	    inp->inp_lport, 0, NULL) != NULL) {
528df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
529c3229e05SDavid Greenman 	}
530df8bae1dSRodney W. Grimes 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
5315a903f8dSPierre Beyssac 		if (inp->inp_lport == 0) {
532b40ce416SJulian Elischer 			error = in_pcbbind(inp, (struct sockaddr *)0, td);
5335a903f8dSPierre Beyssac 			if (error)
5345a903f8dSPierre Beyssac 				return (error);
5355a903f8dSPierre Beyssac 		}
536df8bae1dSRodney W. Grimes 		inp->inp_laddr = ifaddr->sin_addr;
537df8bae1dSRodney W. Grimes 	}
538df8bae1dSRodney W. Grimes 	inp->inp_faddr = sin->sin_addr;
539df8bae1dSRodney W. Grimes 	inp->inp_fport = sin->sin_port;
54015bd2b43SDavid Greenman 	in_pcbrehash(inp);
541df8bae1dSRodney W. Grimes 	return (0);
542df8bae1dSRodney W. Grimes }
543df8bae1dSRodney W. Grimes 
54426f9a767SRodney W. Grimes void
545df8bae1dSRodney W. Grimes in_pcbdisconnect(inp)
546df8bae1dSRodney W. Grimes 	struct inpcb *inp;
547df8bae1dSRodney W. Grimes {
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
550df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
55115bd2b43SDavid Greenman 	in_pcbrehash(inp);
552df8bae1dSRodney W. Grimes 	if (inp->inp_socket->so_state & SS_NOFDREF)
553df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
554df8bae1dSRodney W. Grimes }
555df8bae1dSRodney W. Grimes 
55626f9a767SRodney W. Grimes void
557df8bae1dSRodney W. Grimes in_pcbdetach(inp)
558df8bae1dSRodney W. Grimes 	struct inpcb *inp;
559df8bae1dSRodney W. Grimes {
560df8bae1dSRodney W. Grimes 	struct socket *so = inp->inp_socket;
5613d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
562df8bae1dSRodney W. Grimes 
563cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
564cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
565cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
5663d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
567c3229e05SDavid Greenman 	in_pcbremlists(inp);
568df8bae1dSRodney W. Grimes 	so->so_pcb = 0;
569b1e4abd2SMatthew Dillon 	sotryfree(so);
570df8bae1dSRodney W. Grimes 	if (inp->inp_options)
571df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
572be2ac88cSJonathan Lemon 	if (inp->inp_route.ro_rt)
573be2ac88cSJonathan Lemon 		rtfree(inp->inp_route.ro_rt);
574df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
575cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
57669c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
577df8bae1dSRodney W. Grimes }
578df8bae1dSRodney W. Grimes 
579117bcae7SGarrett Wollman /*
580117bcae7SGarrett Wollman  * The calling convention of in_setsockaddr() and in_setpeeraddr() was
581117bcae7SGarrett Wollman  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
582117bcae7SGarrett Wollman  * in struct pr_usrreqs, so that protocols can just reference then directly
583117bcae7SGarrett Wollman  * without the need for a wrapper function.  The socket must have a valid
584117bcae7SGarrett Wollman  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
585117bcae7SGarrett Wollman  * except through a kernel programming error, so it is acceptable to panic
58657bf258eSGarrett Wollman  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
58757bf258eSGarrett Wollman  * because there actually /is/ a programming error somewhere... XXX)
588117bcae7SGarrett Wollman  */
589117bcae7SGarrett Wollman int
590117bcae7SGarrett Wollman in_setsockaddr(so, nam)
591117bcae7SGarrett Wollman 	struct socket *so;
59257bf258eSGarrett Wollman 	struct sockaddr **nam;
593df8bae1dSRodney W. Grimes {
594fdc984f7STor Egge 	int s;
595fdc984f7STor Egge 	register struct inpcb *inp;
596df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
597df8bae1dSRodney W. Grimes 
598c3229e05SDavid Greenman 	/*
599c3229e05SDavid Greenman 	 * Do the malloc first in case it blocks.
600c3229e05SDavid Greenman 	 */
6017cc0979fSDavid Malone 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
6027cc0979fSDavid Malone 		M_WAITOK | M_ZERO);
60342fa505bSDavid Greenman 	sin->sin_family = AF_INET;
60442fa505bSDavid Greenman 	sin->sin_len = sizeof(*sin);
60542fa505bSDavid Greenman 
606fdc984f7STor Egge 	s = splnet();
607fdc984f7STor Egge 	inp = sotoinpcb(so);
608db112f04STor Egge 	if (!inp) {
609db112f04STor Egge 		splx(s);
61042fa505bSDavid Greenman 		free(sin, M_SONAME);
611ff079ca4SPeter Wemm 		return ECONNRESET;
612db112f04STor Egge 	}
613df8bae1dSRodney W. Grimes 	sin->sin_port = inp->inp_lport;
614df8bae1dSRodney W. Grimes 	sin->sin_addr = inp->inp_laddr;
615db112f04STor Egge 	splx(s);
61642fa505bSDavid Greenman 
61742fa505bSDavid Greenman 	*nam = (struct sockaddr *)sin;
618117bcae7SGarrett Wollman 	return 0;
619df8bae1dSRodney W. Grimes }
620df8bae1dSRodney W. Grimes 
621117bcae7SGarrett Wollman int
622117bcae7SGarrett Wollman in_setpeeraddr(so, nam)
623117bcae7SGarrett Wollman 	struct socket *so;
62457bf258eSGarrett Wollman 	struct sockaddr **nam;
625df8bae1dSRodney W. Grimes {
626fdc984f7STor Egge 	int s;
627fdc984f7STor Egge 	struct inpcb *inp;
628df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
629df8bae1dSRodney W. Grimes 
630c3229e05SDavid Greenman 	/*
631c3229e05SDavid Greenman 	 * Do the malloc first in case it blocks.
632c3229e05SDavid Greenman 	 */
6337cc0979fSDavid Malone 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
6347cc0979fSDavid Malone 		M_WAITOK | M_ZERO);
63542fa505bSDavid Greenman 	sin->sin_family = AF_INET;
63642fa505bSDavid Greenman 	sin->sin_len = sizeof(*sin);
63742fa505bSDavid Greenman 
638fdc984f7STor Egge 	s = splnet();
639fdc984f7STor Egge 	inp = sotoinpcb(so);
640db112f04STor Egge 	if (!inp) {
641db112f04STor Egge 		splx(s);
64242fa505bSDavid Greenman 		free(sin, M_SONAME);
643ff079ca4SPeter Wemm 		return ECONNRESET;
644db112f04STor Egge 	}
645df8bae1dSRodney W. Grimes 	sin->sin_port = inp->inp_fport;
646df8bae1dSRodney W. Grimes 	sin->sin_addr = inp->inp_faddr;
647db112f04STor Egge 	splx(s);
64842fa505bSDavid Greenman 
64942fa505bSDavid Greenman 	*nam = (struct sockaddr *)sin;
650117bcae7SGarrett Wollman 	return 0;
651df8bae1dSRodney W. Grimes }
652df8bae1dSRodney W. Grimes 
65326f9a767SRodney W. Grimes void
654c693a045SJonathan Lemon in_pcbnotifyall(head, faddr, errno, notify)
65515bd2b43SDavid Greenman 	struct inpcbhead *head;
656df8bae1dSRodney W. Grimes 	struct in_addr faddr;
657c693a045SJonathan Lemon 	int errno;
6584d77a549SAlfred Perlstein 	void (*notify)(struct inpcb *, int);
659d1c54148SJesper Skriver {
660c693a045SJonathan Lemon 	struct inpcb *inp, *ninp;
661c693a045SJonathan Lemon 	int s;
662d1c54148SJesper Skriver 
663d1c54148SJesper Skriver 	s = splnet();
664c693a045SJonathan Lemon 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
665c693a045SJonathan Lemon 		ninp = LIST_NEXT(inp, inp_list);
666d1c54148SJesper Skriver #ifdef INET6
667c693a045SJonathan Lemon 		if ((inp->inp_vflag & INP_IPV4) == 0)
668d1c54148SJesper Skriver 			continue;
669d1c54148SJesper Skriver #endif
670d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
671c693a045SJonathan Lemon 		    inp->inp_socket == NULL)
672d1c54148SJesper Skriver 				continue;
673c693a045SJonathan Lemon 		(*notify)(inp, errno);
674d1c54148SJesper Skriver 	}
675d1c54148SJesper Skriver 	splx(s);
676d1c54148SJesper Skriver }
677d1c54148SJesper Skriver 
678e43cc4aeSHajimu UMEMOTO void
679e43cc4aeSHajimu UMEMOTO in_pcbpurgeif0(head, ifp)
680e43cc4aeSHajimu UMEMOTO 	struct inpcb *head;
681e43cc4aeSHajimu UMEMOTO 	struct ifnet *ifp;
682e43cc4aeSHajimu UMEMOTO {
683e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
684e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
685e43cc4aeSHajimu UMEMOTO 	int i, gap;
686e43cc4aeSHajimu UMEMOTO 
687e43cc4aeSHajimu UMEMOTO 	for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
688e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
689e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
690e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
691e43cc4aeSHajimu UMEMOTO 			/*
692e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
693e43cc4aeSHajimu UMEMOTO 			 * detached.
694e43cc4aeSHajimu UMEMOTO 			 */
695e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
696e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
697e43cc4aeSHajimu UMEMOTO 
698e43cc4aeSHajimu UMEMOTO 			/*
699e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
700e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
701e43cc4aeSHajimu UMEMOTO 			 */
702e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
703e43cc4aeSHajimu UMEMOTO 			    i++) {
704e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
705e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
706e43cc4aeSHajimu UMEMOTO 					gap++;
707e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
708e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
709e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
710e43cc4aeSHajimu UMEMOTO 			}
711e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
712e43cc4aeSHajimu UMEMOTO 		}
713e43cc4aeSHajimu UMEMOTO 	}
714e43cc4aeSHajimu UMEMOTO }
715e43cc4aeSHajimu UMEMOTO 
716df8bae1dSRodney W. Grimes /*
717df8bae1dSRodney W. Grimes  * Check for alternatives when higher level complains
718df8bae1dSRodney W. Grimes  * about service problems.  For now, invalidate cached
719df8bae1dSRodney W. Grimes  * routing information.  If the route was created dynamically
720df8bae1dSRodney W. Grimes  * (by a redirect), time to try a default gateway again.
721df8bae1dSRodney W. Grimes  */
72226f9a767SRodney W. Grimes void
723df8bae1dSRodney W. Grimes in_losing(inp)
724df8bae1dSRodney W. Grimes 	struct inpcb *inp;
725df8bae1dSRodney W. Grimes {
726df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
727df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
728df8bae1dSRodney W. Grimes 
729df8bae1dSRodney W. Grimes 	if ((rt = inp->inp_route.ro_rt)) {
730df8bae1dSRodney W. Grimes 		bzero((caddr_t)&info, sizeof(info));
7318071913dSRuslan Ermilov 		info.rti_flags = rt->rt_flags;
7328071913dSRuslan Ermilov 		info.rti_info[RTAX_DST] = rt_key(rt);
733df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
734df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
735df8bae1dSRodney W. Grimes 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
736df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_DYNAMIC)
7378071913dSRuslan Ermilov 			(void) rtrequest1(RTM_DELETE, &info, NULL);
7388071913dSRuslan Ermilov 		inp->inp_route.ro_rt = NULL;
7398bf82a92SRuslan Ermilov 		rtfree(rt);
740df8bae1dSRodney W. Grimes 		/*
741df8bae1dSRodney W. Grimes 		 * A new route can be allocated
742df8bae1dSRodney W. Grimes 		 * the next time output is attempted.
743df8bae1dSRodney W. Grimes 		 */
744df8bae1dSRodney W. Grimes 	}
745df8bae1dSRodney W. Grimes }
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes /*
748df8bae1dSRodney W. Grimes  * After a routing change, flush old routing
749df8bae1dSRodney W. Grimes  * and allocate a (hopefully) better one.
750df8bae1dSRodney W. Grimes  */
751d1c54148SJesper Skriver void
752df8bae1dSRodney W. Grimes in_rtchange(inp, errno)
753df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
754df8bae1dSRodney W. Grimes 	int errno;
755df8bae1dSRodney W. Grimes {
756df8bae1dSRodney W. Grimes 	if (inp->inp_route.ro_rt) {
757df8bae1dSRodney W. Grimes 		rtfree(inp->inp_route.ro_rt);
758df8bae1dSRodney W. Grimes 		inp->inp_route.ro_rt = 0;
759df8bae1dSRodney W. Grimes 		/*
760df8bae1dSRodney W. Grimes 		 * A new route can be allocated the next time
761df8bae1dSRodney W. Grimes 		 * output is attempted.
762df8bae1dSRodney W. Grimes 		 */
763df8bae1dSRodney W. Grimes 	}
764df8bae1dSRodney W. Grimes }
765df8bae1dSRodney W. Grimes 
766c3229e05SDavid Greenman /*
767c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
768c3229e05SDavid Greenman  */
769df8bae1dSRodney W. Grimes struct inpcb *
770c3229e05SDavid Greenman in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
7716d6a026bSDavid Greenman 	struct inpcbinfo *pcbinfo;
772c3229e05SDavid Greenman 	struct in_addr laddr;
773c3229e05SDavid Greenman 	u_int lport_arg;
7746d6a026bSDavid Greenman 	int wild_okay;
775df8bae1dSRodney W. Grimes {
776f1d19042SArchie Cobbs 	register struct inpcb *inp;
777df8bae1dSRodney W. Grimes 	int matchwild = 3, wildcard;
778c3229e05SDavid Greenman 	u_short lport = lport_arg;
7797bc4aca7SDavid Greenman 
780c3229e05SDavid Greenman 	if (!wild_okay) {
781c3229e05SDavid Greenman 		struct inpcbhead *head;
782c3229e05SDavid Greenman 		/*
783c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
784c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
785c3229e05SDavid Greenman 		 */
786c3229e05SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
787fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
788cfa1ca9dSYoshinobu Inoue #ifdef INET6
789369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
790cfa1ca9dSYoshinobu Inoue 				continue;
791cfa1ca9dSYoshinobu Inoue #endif
792c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
793c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
794c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
795c3229e05SDavid Greenman 				/*
796c3229e05SDavid Greenman 				 * Found.
797c3229e05SDavid Greenman 				 */
798c3229e05SDavid Greenman 				return (inp);
799df8bae1dSRodney W. Grimes 			}
800c3229e05SDavid Greenman 		}
801c3229e05SDavid Greenman 		/*
802c3229e05SDavid Greenman 		 * Not found.
803c3229e05SDavid Greenman 		 */
804c3229e05SDavid Greenman 		return (NULL);
805c3229e05SDavid Greenman 	} else {
806c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
807c3229e05SDavid Greenman 		struct inpcbport *phd;
808c3229e05SDavid Greenman 		struct inpcb *match = NULL;
809c3229e05SDavid Greenman 		/*
810c3229e05SDavid Greenman 		 * Best fit PCB lookup.
811c3229e05SDavid Greenman 		 *
812c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
813c3229e05SDavid Greenman 		 * port hash list.
814c3229e05SDavid Greenman 		 */
815c3229e05SDavid Greenman 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
816c3229e05SDavid Greenman 		    pcbinfo->porthashmask)];
817fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
818c3229e05SDavid Greenman 			if (phd->phd_port == lport)
819c3229e05SDavid Greenman 				break;
820c3229e05SDavid Greenman 		}
821c3229e05SDavid Greenman 		if (phd != NULL) {
822c3229e05SDavid Greenman 			/*
823c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
824c3229e05SDavid Greenman 			 * fit.
825c3229e05SDavid Greenman 			 */
82637d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
827c3229e05SDavid Greenman 				wildcard = 0;
828cfa1ca9dSYoshinobu Inoue #ifdef INET6
829369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
830cfa1ca9dSYoshinobu Inoue 					continue;
831cfa1ca9dSYoshinobu Inoue #endif
832c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
833c3229e05SDavid Greenman 					wildcard++;
83415bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
83515bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
83615bd2b43SDavid Greenman 						wildcard++;
83715bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
83815bd2b43SDavid Greenman 						continue;
83915bd2b43SDavid Greenman 				} else {
84015bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
84115bd2b43SDavid Greenman 						wildcard++;
84215bd2b43SDavid Greenman 				}
843df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
844df8bae1dSRodney W. Grimes 					match = inp;
845df8bae1dSRodney W. Grimes 					matchwild = wildcard;
8463dbdc25cSDavid Greenman 					if (matchwild == 0) {
847df8bae1dSRodney W. Grimes 						break;
848df8bae1dSRodney W. Grimes 					}
849df8bae1dSRodney W. Grimes 				}
8503dbdc25cSDavid Greenman 			}
851c3229e05SDavid Greenman 		}
852df8bae1dSRodney W. Grimes 		return (match);
853df8bae1dSRodney W. Grimes 	}
854c3229e05SDavid Greenman }
85515bd2b43SDavid Greenman 
85615bd2b43SDavid Greenman /*
85715bd2b43SDavid Greenman  * Lookup PCB in hash list.
85815bd2b43SDavid Greenman  */
85915bd2b43SDavid Greenman struct inpcb *
860cfa1ca9dSYoshinobu Inoue in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
861cfa1ca9dSYoshinobu Inoue 		  ifp)
86215bd2b43SDavid Greenman 	struct inpcbinfo *pcbinfo;
86315bd2b43SDavid Greenman 	struct in_addr faddr, laddr;
86415bd2b43SDavid Greenman 	u_int fport_arg, lport_arg;
8656d6a026bSDavid Greenman 	int wildcard;
866cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
86715bd2b43SDavid Greenman {
86815bd2b43SDavid Greenman 	struct inpcbhead *head;
86915bd2b43SDavid Greenman 	register struct inpcb *inp;
87015bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
87115bd2b43SDavid Greenman 
87215bd2b43SDavid Greenman 	/*
87315bd2b43SDavid Greenman 	 * First look for an exact match.
87415bd2b43SDavid Greenman 	 */
875ddd79a97SDavid Greenman 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
876fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
877cfa1ca9dSYoshinobu Inoue #ifdef INET6
878369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
879cfa1ca9dSYoshinobu Inoue 			continue;
880cfa1ca9dSYoshinobu Inoue #endif
8816d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
882ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
883ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
884c3229e05SDavid Greenman 		    inp->inp_lport == lport) {
885c3229e05SDavid Greenman 			/*
886c3229e05SDavid Greenman 			 * Found.
887c3229e05SDavid Greenman 			 */
888c3229e05SDavid Greenman 			return (inp);
889c3229e05SDavid Greenman 		}
8906d6a026bSDavid Greenman 	}
8916d6a026bSDavid Greenman 	if (wildcard) {
8926d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
893cfa1ca9dSYoshinobu Inoue #if defined(INET6)
894cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
895cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
8966d6a026bSDavid Greenman 
897ddd79a97SDavid Greenman 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
898fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
899cfa1ca9dSYoshinobu Inoue #ifdef INET6
900369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
901cfa1ca9dSYoshinobu Inoue 				continue;
902cfa1ca9dSYoshinobu Inoue #endif
9036d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
904c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
905cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
906cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
907cfa1ca9dSYoshinobu Inoue 					continue;
9086d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
909c3229e05SDavid Greenman 					return (inp);
910cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
911cfa1ca9dSYoshinobu Inoue #if defined(INET6)
912cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
913cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
914cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
915cfa1ca9dSYoshinobu Inoue 					else
916cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
9176d6a026bSDavid Greenman 					local_wild = inp;
9186d6a026bSDavid Greenman 				}
9196d6a026bSDavid Greenman 			}
920cfa1ca9dSYoshinobu Inoue 		}
921cfa1ca9dSYoshinobu Inoue #if defined(INET6)
922cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
923cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
924cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */
925c3229e05SDavid Greenman 		return (local_wild);
9266d6a026bSDavid Greenman 	}
927c3229e05SDavid Greenman 
928c3229e05SDavid Greenman 	/*
929c3229e05SDavid Greenman 	 * Not found.
930c3229e05SDavid Greenman 	 */
9316d6a026bSDavid Greenman 	return (NULL);
93215bd2b43SDavid Greenman }
93315bd2b43SDavid Greenman 
9347bc4aca7SDavid Greenman /*
935c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
9367bc4aca7SDavid Greenman  */
937c3229e05SDavid Greenman int
93815bd2b43SDavid Greenman in_pcbinshash(inp)
93915bd2b43SDavid Greenman 	struct inpcb *inp;
94015bd2b43SDavid Greenman {
941c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
942c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
943c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
944c3229e05SDavid Greenman 	struct inpcbport *phd;
945cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
94615bd2b43SDavid Greenman 
947cfa1ca9dSYoshinobu Inoue #ifdef INET6
948cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
949cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
950cfa1ca9dSYoshinobu Inoue 	else
951cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
952cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
953cfa1ca9dSYoshinobu Inoue 
954cfa1ca9dSYoshinobu Inoue 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
955c3229e05SDavid Greenman 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
95615bd2b43SDavid Greenman 
957c3229e05SDavid Greenman 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
958c3229e05SDavid Greenman 	    pcbinfo->porthashmask)];
959c3229e05SDavid Greenman 
960c3229e05SDavid Greenman 	/*
961c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
962c3229e05SDavid Greenman 	 */
963fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
964c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
965c3229e05SDavid Greenman 			break;
966c3229e05SDavid Greenman 	}
967c3229e05SDavid Greenman 	/*
968c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
969c3229e05SDavid Greenman 	 */
970c3229e05SDavid Greenman 	if (phd == NULL) {
971c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
972c3229e05SDavid Greenman 		if (phd == NULL) {
973c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
974c3229e05SDavid Greenman 		}
975c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
976c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
977c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
978c3229e05SDavid Greenman 	}
979c3229e05SDavid Greenman 	inp->inp_phd = phd;
980c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
981c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
982c3229e05SDavid Greenman 	return (0);
98315bd2b43SDavid Greenman }
98415bd2b43SDavid Greenman 
985c3229e05SDavid Greenman /*
986c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
987c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
988c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
989c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
990c3229e05SDavid Greenman  */
99115bd2b43SDavid Greenman void
99215bd2b43SDavid Greenman in_pcbrehash(inp)
99315bd2b43SDavid Greenman 	struct inpcb *inp;
99415bd2b43SDavid Greenman {
99515bd2b43SDavid Greenman 	struct inpcbhead *head;
996cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
99715bd2b43SDavid Greenman 
998cfa1ca9dSYoshinobu Inoue #ifdef INET6
999cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1000cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1001cfa1ca9dSYoshinobu Inoue 	else
1002cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1003cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1004cfa1ca9dSYoshinobu Inoue 
1005cfa1ca9dSYoshinobu Inoue 	head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1006ddd79a97SDavid Greenman 		inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
100715bd2b43SDavid Greenman 
1008c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
100915bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1010c3229e05SDavid Greenman }
1011c3229e05SDavid Greenman 
1012c3229e05SDavid Greenman /*
1013c3229e05SDavid Greenman  * Remove PCB from various lists.
1014c3229e05SDavid Greenman  */
101576429de4SYoshinobu Inoue void
1016c3229e05SDavid Greenman in_pcbremlists(inp)
1017c3229e05SDavid Greenman 	struct inpcb *inp;
1018c3229e05SDavid Greenman {
101998271db4SGarrett Wollman 	inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1020c3229e05SDavid Greenman 	if (inp->inp_lport) {
1021c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1022c3229e05SDavid Greenman 
1023c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1024c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1025fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1026c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1027c3229e05SDavid Greenman 			free(phd, M_PCB);
1028c3229e05SDavid Greenman 		}
1029c3229e05SDavid Greenman 	}
1030c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
10313d4d47f3SGarrett Wollman 	inp->inp_pcbinfo->ipi_count--;
103215bd2b43SDavid Greenman }
103375c13541SPoul-Henning Kamp 
103475c13541SPoul-Henning Kamp int
103575c13541SPoul-Henning Kamp prison_xinpcb(struct proc *p, struct inpcb *inp)
103675c13541SPoul-Henning Kamp {
103791421ba2SRobert Watson 	if (!jailed(p->p_ucred))
103875c13541SPoul-Henning Kamp 		return (0);
103901137630SRobert Watson 	if (ntohl(inp->inp_laddr.s_addr) == prison_getip(p->p_ucred))
104075c13541SPoul-Henning Kamp 		return (0);
104175c13541SPoul-Henning Kamp 	return (1);
104275c13541SPoul-Henning Kamp }
1043