xref: /freebsd/sys/netinet/in_pcb.c (revision 9d29c635daa69d261a81651ebefd589fdceb64a7)
1c398230bSWarner Losh /*-
22469dd60SGarrett Wollman  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3497057eeSRobert Watson  *	The Regents of the University of California.
4497057eeSRobert Watson  * Copyright (c) 2007 Robert N. M. Watson
5497057eeSRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
312469dd60SGarrett Wollman  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
37497057eeSRobert Watson #include "opt_ddb.h"
386a800098SYoshinobu Inoue #include "opt_ipsec.h"
39cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
40a557af22SRobert Watson #include "opt_mac.h"
41cfa1ca9dSYoshinobu Inoue 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
49df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
50acd3428bSRobert Watson #include <sys/priv.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
5275c13541SPoul-Henning Kamp #include <sys/jail.h>
53101f9fc8SPeter Wemm #include <sys/kernel.h>
54101f9fc8SPeter Wemm #include <sys/sysctl.h>
558781d8e9SBruce Evans 
56497057eeSRobert Watson #ifdef DDB
57497057eeSRobert Watson #include <ddb/ddb.h>
58497057eeSRobert Watson #endif
59497057eeSRobert Watson 
6069c2d429SJeff Roberson #include <vm/uma.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <net/if.h>
63cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
64df8bae1dSRodney W. Grimes #include <net/route.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <netinet/in.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
68df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
70340c35deSJonathan Lemon #include <netinet/tcp_var.h>
715f311da2SMike Silbersack #include <netinet/udp.h>
725f311da2SMike Silbersack #include <netinet/udp_var.h>
73cfa1ca9dSYoshinobu Inoue #ifdef INET6
74cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
75cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
76cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
77cfa1ca9dSYoshinobu Inoue 
78df8bae1dSRodney W. Grimes 
79b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
80b9234fafSSam Leffler #include <netipsec/ipsec.h>
81b9234fafSSam Leffler #include <netipsec/key.h>
82b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
83b9234fafSSam Leffler 
84aed55708SRobert Watson #include <security/mac/mac_framework.h>
85aed55708SRobert Watson 
86101f9fc8SPeter Wemm /*
87101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
88101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
89101f9fc8SPeter Wemm  */
9082cd038dSYoshinobu Inoue int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
9182cd038dSYoshinobu Inoue int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
921cf6e4f5SRui Paulo int	ipport_firstauto = IPPORT_EPHEMERALFIRST;	/* 10000 */
931cf6e4f5SRui Paulo int	ipport_lastauto  = IPPORT_EPHEMERALLAST;	/* 65535 */
9482cd038dSYoshinobu Inoue int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
9582cd038dSYoshinobu Inoue int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
96101f9fc8SPeter Wemm 
97b0d22693SCrist J. Clark /*
98b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
99b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
100b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
101b0d22693SCrist J. Clark  */
102b0d22693SCrist J. Clark int	ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
103b0d22693SCrist J. Clark int	ipport_reservedlow = 0;
104b0d22693SCrist J. Clark 
1055f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
1065f311da2SMike Silbersack int	ipport_randomized = 1;	/* user controlled via sysctl */
1075f311da2SMike Silbersack int	ipport_randomcps = 10;	/* user controlled via sysctl */
1085f311da2SMike Silbersack int	ipport_randomtime = 45;	/* user controlled via sysctl */
1095f311da2SMike Silbersack int	ipport_stoprandom = 0;	/* toggled by ipport_tick */
1105f311da2SMike Silbersack int	ipport_tcpallocs;
1115f311da2SMike Silbersack int	ipport_tcplastcount;
1126ac48b74SMike Silbersack 
113bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
114bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
115bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
116bbd42ad0SPeter Wemm 
117bbd42ad0SPeter Wemm static int
11882d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
119bbd42ad0SPeter Wemm {
12030a4ab08SBruce Evans 	int error;
12130a4ab08SBruce Evans 
12230a4ab08SBruce Evans 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
12330a4ab08SBruce Evans 	if (error == 0) {
124bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
125bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
12630a4ab08SBruce Evans 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
12730a4ab08SBruce Evans 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
12830a4ab08SBruce Evans 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
12930a4ab08SBruce Evans 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
130bbd42ad0SPeter Wemm 	}
13130a4ab08SBruce Evans 	return (error);
132bbd42ad0SPeter Wemm }
133bbd42ad0SPeter Wemm 
134bbd42ad0SPeter Wemm #undef RANGECHK
135bbd42ad0SPeter Wemm 
13633b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
13733b3ac06SPeter Wemm 
138bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
139bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
140bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
141bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
142bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
143bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
144bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
145bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
146bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
147bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
148bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
149bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
150b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
151b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
152b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
153b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
1546ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
1556ee79c59SMaxim Konovalov 	   &ipport_randomized, 0, "Enable random port allocation");
1566ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
1576ee79c59SMaxim Konovalov 	   &ipport_randomcps, 0, "Maximum number of random port "
1586ee79c59SMaxim Konovalov 	   "allocations before switching to a sequental one");
1596ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
1606ee79c59SMaxim Konovalov 	   &ipport_randomtime, 0, "Minimum time to keep sequental port "
1616ee79c59SMaxim Konovalov 	   "allocation before switching to a random one");
1620312fbe9SPoul-Henning Kamp 
163c3229e05SDavid Greenman /*
164c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
165c3229e05SDavid Greenman  *
166de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
167de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
168de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
169c3229e05SDavid Greenman  */
170c3229e05SDavid Greenman 
171c3229e05SDavid Greenman /*
172c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
173d915b280SStephan Uphoff  * On success return with the PCB locked.
174c3229e05SDavid Greenman  */
175df8bae1dSRodney W. Grimes int
176d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
177df8bae1dSRodney W. Grimes {
178136d4f1cSRobert Watson 	struct inpcb *inp;
17913cf67f3SHajimu UMEMOTO 	int error;
180a557af22SRobert Watson 
18159daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
182a557af22SRobert Watson 	error = 0;
183d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
184df8bae1dSRodney W. Grimes 	if (inp == NULL)
185df8bae1dSRodney W. Grimes 		return (ENOBUFS);
186d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
18715bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
188df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
1898b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
190a557af22SRobert Watson #ifdef MAC
19130d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
192a557af22SRobert Watson 	if (error != 0)
193a557af22SRobert Watson 		goto out;
194310e7cebSRobert Watson 	SOCK_LOCK(so);
19530d239bcSRobert Watson 	mac_inpcb_create(so, inp);
196310e7cebSRobert Watson 	SOCK_UNLOCK(so);
197a557af22SRobert Watson #endif
1982cb64cb2SGeorge V. Neville-Neil 
199b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
20013cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
2010bffde27SRobert Watson 	if (error != 0) {
2020bffde27SRobert Watson #ifdef MAC
2030bffde27SRobert Watson 		mac_inpcb_destroy(inp);
2040bffde27SRobert Watson #endif
205a557af22SRobert Watson 		goto out;
2060bffde27SRobert Watson 	}
207b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
208e3fd5ffdSRobert Watson #ifdef INET6
209340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
210340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
211340c35deSJonathan Lemon 		if (ip6_v6only)
21233841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
213340c35deSJonathan Lemon 	}
21475daea93SPaul Saab #endif
215712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
2163d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
217df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
21833841545SHajimu UMEMOTO #ifdef INET6
21933841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
22033841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
22133841545SHajimu UMEMOTO #endif
2228501a69cSRobert Watson 	INP_WLOCK(inp);
223d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
224d915b280SStephan Uphoff 
225b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC)
226a557af22SRobert Watson out:
227a557af22SRobert Watson 	if (error != 0)
228a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
229a557af22SRobert Watson #endif
230a557af22SRobert Watson 	return (error);
231df8bae1dSRodney W. Grimes }
232df8bae1dSRodney W. Grimes 
233df8bae1dSRodney W. Grimes int
234136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
235df8bae1dSRodney W. Grimes {
2364b932371SIan Dowse 	int anonport, error;
2374b932371SIan Dowse 
2381b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
2398501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
24059daba27SSam Leffler 
2414b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2424b932371SIan Dowse 		return (EINVAL);
2434b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2444b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2454b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
246b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
2474b932371SIan Dowse 	if (error)
2484b932371SIan Dowse 		return (error);
2494b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2504b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2514b932371SIan Dowse 		inp->inp_lport = 0;
2524b932371SIan Dowse 		return (EAGAIN);
2534b932371SIan Dowse 	}
2544b932371SIan Dowse 	if (anonport)
2554b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2564b932371SIan Dowse 	return (0);
2574b932371SIan Dowse }
2584b932371SIan Dowse 
2594b932371SIan Dowse /*
2604b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2614b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2624b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2634b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2644b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2654b932371SIan Dowse  *
2664b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2674b932371SIan Dowse  */
2684b932371SIan Dowse int
269136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
270136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
2714b932371SIan Dowse {
2724b932371SIan Dowse 	struct socket *so = inp->inp_socket;
27337bd2b30SPeter Wemm 	unsigned short *lastport;
27415bd2b43SDavid Greenman 	struct sockaddr_in *sin;
275c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2764b932371SIan Dowse 	struct in_addr laddr;
277df8bae1dSRodney W. Grimes 	u_short lport = 0;
2784cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
27975c13541SPoul-Henning Kamp 	int error, prison = 0;
2805f311da2SMike Silbersack 	int dorandom;
281df8bae1dSRodney W. Grimes 
2828501a69cSRobert Watson 	/*
2838501a69cSRobert Watson 	 * Because no actual state changes occur here, a write global write
2848501a69cSRobert Watson 	 * lock on the pcbinfo isn't required.
2858501a69cSRobert Watson 	 */
2868501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
28759daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
28859daba27SSam Leffler 
28959562606SGarrett Wollman 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
290df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
2914b932371SIan Dowse 	laddr.s_addr = *laddrp;
2924b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
293df8bae1dSRodney W. Grimes 		return (EINVAL);
294c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
295421d8aa6SBjoern A. Zeeb 		wild = INPLOOKUP_WILDCARD;
296df8bae1dSRodney W. Grimes 	if (nam) {
29757bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
29857bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
299df8bae1dSRodney W. Grimes 			return (EINVAL);
300df8bae1dSRodney W. Grimes #ifdef notdef
301df8bae1dSRodney W. Grimes 		/*
302df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
303df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
304df8bae1dSRodney W. Grimes 		 */
305df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
306df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
307df8bae1dSRodney W. Grimes #endif
308e4bdf25dSPoul-Henning Kamp 		if (sin->sin_addr.s_addr != INADDR_ANY)
309b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
31075c13541SPoul-Henning Kamp 				return(EINVAL);
3114b932371SIan Dowse 		if (sin->sin_port != *lportp) {
3124b932371SIan Dowse 			/* Don't allow the port to change. */
3134b932371SIan Dowse 			if (*lportp != 0)
3144b932371SIan Dowse 				return (EINVAL);
315df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
3164b932371SIan Dowse 		}
3174b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
318df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
319df8bae1dSRodney W. Grimes 			/*
320df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
321df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
322df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
323df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
324df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
325df8bae1dSRodney W. Grimes 			 */
326df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
327df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
328df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
329df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
33083103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
331df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
332df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
333df8bae1dSRodney W. Grimes 		}
3344b932371SIan Dowse 		laddr = sin->sin_addr;
335df8bae1dSRodney W. Grimes 		if (lport) {
336df8bae1dSRodney W. Grimes 			struct inpcb *t;
337ae0e7143SRobert Watson 			struct tcptw *tw;
338ae0e7143SRobert Watson 
339df8bae1dSRodney W. Grimes 			/* GROSS */
340b0d22693SCrist J. Clark 			if (ntohs(lport) <= ipport_reservedhigh &&
341b0d22693SCrist J. Clark 			    ntohs(lport) >= ipport_reservedlow &&
342acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
34332f9753cSRobert Watson 			    0))
3442469dd60SGarrett Wollman 				return (EACCES);
345b0330ed9SPawel Jakub Dawidek 			if (jailed(cred))
34675c13541SPoul-Henning Kamp 				prison = 1;
347835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
3486493245dSRobert Watson 			    priv_check_cred(so->so_cred,
34932f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
350078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
351078b7042SBjoern A. Zeeb 				    lport, prison ? 0 : INPLOOKUP_WILDCARD,
352078b7042SBjoern A. Zeeb 				    cred);
353340c35deSJonathan Lemon 	/*
354340c35deSJonathan Lemon 	 * XXX
355340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
356340c35deSJonathan Lemon 	 */
3574cc20ab1SSeigo Tanimura 				if (t &&
3584658dc83SYaroslav Tykhiy 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
3594658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
3604658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
3614cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
36252b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
36352b65dbeSBill Fenner 				     (t->inp_socket->so_options &
36452b65dbeSBill Fenner 					 SO_REUSEPORT) == 0) &&
3652f9a2132SBrian Feldman 				    (so->so_cred->cr_uid !=
366a4eb4405SYaroslav Tykhiy 				     t->inp_socket->so_cred->cr_uid))
3674049a042SGuido van Rooij 					return (EADDRINUSE);
3684049a042SGuido van Rooij 			}
369b0330ed9SPawel Jakub Dawidek 			if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
370970680faSPoul-Henning Kamp 				return (EADDRNOTAVAIL);
371c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
372078b7042SBjoern A. Zeeb 			    lport, prison ? 0 : wild, cred);
373340c35deSJonathan Lemon 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
374ae0e7143SRobert Watson 				/*
375ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
376ae0e7143SRobert Watson 				 * state recycled, we treat the address as
377ae0e7143SRobert Watson 				 * being in use (for now).  This is better
378ae0e7143SRobert Watson 				 * than a panic, but not desirable.
379ae0e7143SRobert Watson 				 */
380ae0e7143SRobert Watson 				tw = intotw(inp);
381ae0e7143SRobert Watson 				if (tw == NULL ||
382ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
383340c35deSJonathan Lemon 					return (EADDRINUSE);
384ae0e7143SRobert Watson 			} else if (t &&
3854cc20ab1SSeigo Tanimura 			    (reuseport & t->inp_socket->so_options) == 0) {
386e3fd5ffdSRobert Watson #ifdef INET6
38733841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
388cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
389cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
390cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
391cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(so) ==
392cfa1ca9dSYoshinobu Inoue 				    INP_SOCKAF(t->inp_socket))
393e3fd5ffdSRobert Watson #endif
394df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
395df8bae1dSRodney W. Grimes 			}
396cfa1ca9dSYoshinobu Inoue 		}
397df8bae1dSRodney W. Grimes 	}
3984b932371SIan Dowse 	if (*lportp != 0)
3994b932371SIan Dowse 		lport = *lportp;
40033b3ac06SPeter Wemm 	if (lport == 0) {
4011cf6e4f5SRui Paulo 		u_short first, last, aux;
402174624e0SMike Silbersack 		int count;
40333b3ac06SPeter Wemm 
4044b932371SIan Dowse 		if (laddr.s_addr != INADDR_ANY)
405b0330ed9SPawel Jakub Dawidek 			if (prison_ip(cred, 0, &laddr.s_addr))
40675c13541SPoul-Henning Kamp 				return (EINVAL);
407321a2846SPoul-Henning Kamp 
40833b3ac06SPeter Wemm 		if (inp->inp_flags & INP_HIGHPORT) {
40933b3ac06SPeter Wemm 			first = ipport_hifirstauto;	/* sysctl */
41033b3ac06SPeter Wemm 			last  = ipport_hilastauto;
411712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lasthi;
41233b3ac06SPeter Wemm 		} else if (inp->inp_flags & INP_LOWPORT) {
413acd3428bSRobert Watson 			error = priv_check_cred(cred,
41432f9753cSRobert Watson 			    PRIV_NETINET_RESERVEDPORT, 0);
415acd3428bSRobert Watson 			if (error)
416a29f300eSGarrett Wollman 				return error;
417bbd42ad0SPeter Wemm 			first = ipport_lowfirstauto;	/* 1023 */
418bbd42ad0SPeter Wemm 			last  = ipport_lowlastauto;	/* 600 */
419712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lastlow;
42033b3ac06SPeter Wemm 		} else {
42133b3ac06SPeter Wemm 			first = ipport_firstauto;	/* sysctl */
42233b3ac06SPeter Wemm 			last  = ipport_lastauto;
423712fc218SRobert Watson 			lastport = &pcbinfo->ipi_lastport;
42433b3ac06SPeter Wemm 		}
42533b3ac06SPeter Wemm 		/*
4265f311da2SMike Silbersack 		 * For UDP, use random port allocation as long as the user
4275f311da2SMike Silbersack 		 * allows it.  For TCP (and as of yet unknown) connections,
4285f311da2SMike Silbersack 		 * use random port allocation only if the user allows it AND
42929f2a6ecSMaxim Konovalov 		 * ipport_tick() allows it.
4305f311da2SMike Silbersack 		 */
4315f311da2SMike Silbersack 		if (ipport_randomized &&
4325f311da2SMike Silbersack 			(!ipport_stoprandom || pcbinfo == &udbinfo))
4335f311da2SMike Silbersack 			dorandom = 1;
4345f311da2SMike Silbersack 		else
4355f311da2SMike Silbersack 			dorandom = 0;
436e99971bfSMaxim Konovalov 		/*
437e99971bfSMaxim Konovalov 		 * It makes no sense to do random port allocation if
438e99971bfSMaxim Konovalov 		 * we have the only port available.
439e99971bfSMaxim Konovalov 		 */
440e99971bfSMaxim Konovalov 		if (first == last)
441e99971bfSMaxim Konovalov 			dorandom = 0;
4425f311da2SMike Silbersack 		/* Make sure to not include UDP packets in the count. */
4435f311da2SMike Silbersack 		if (pcbinfo != &udbinfo)
4445f311da2SMike Silbersack 			ipport_tcpallocs++;
4455f311da2SMike Silbersack 		/*
44633b3ac06SPeter Wemm 		 * Simple check to ensure all ports are not used up causing
44733b3ac06SPeter Wemm 		 * a deadlock here.
44833b3ac06SPeter Wemm 		 */
44933b3ac06SPeter Wemm 		if (first > last) {
4501cf6e4f5SRui Paulo 			aux = first;
4511cf6e4f5SRui Paulo 			first = last;
4521cf6e4f5SRui Paulo 			last = aux;
4531cf6e4f5SRui Paulo 		}
454174624e0SMike Silbersack 
4555f311da2SMike Silbersack 		if (dorandom)
4566b2fc10bSMike Silbersack 			*lastport = first +
4576b2fc10bSMike Silbersack 				    (arc4random() % (last - first));
4581cf6e4f5SRui Paulo 
45933b3ac06SPeter Wemm 		count = last - first;
460174624e0SMike Silbersack 
46133b3ac06SPeter Wemm 		do {
4626ac48b74SMike Silbersack 			if (count-- < 0)	/* completely used? */
463550b1518SWes Peters 				return (EADDRNOTAVAIL);
46433b3ac06SPeter Wemm 			++*lastport;
46533b3ac06SPeter Wemm 			if (*lastport < first || *lastport > last)
46633b3ac06SPeter Wemm 				*lastport = first;
46733b3ac06SPeter Wemm 			lport = htons(*lastport);
468078b7042SBjoern A. Zeeb 		} while (in_pcblookup_local(pcbinfo, laddr,
469078b7042SBjoern A. Zeeb 		    lport, wild, cred));
47033b3ac06SPeter Wemm 	}
471b0330ed9SPawel Jakub Dawidek 	if (prison_ip(cred, 0, &laddr.s_addr))
472e4bdf25dSPoul-Henning Kamp 		return (EINVAL);
4734b932371SIan Dowse 	*laddrp = laddr.s_addr;
4744b932371SIan Dowse 	*lportp = lport;
475df8bae1dSRodney W. Grimes 	return (0);
476df8bae1dSRodney W. Grimes }
477df8bae1dSRodney W. Grimes 
478999f1343SGarrett Wollman /*
4795200e00eSIan Dowse  * Connect from a socket to a specified address.
4805200e00eSIan Dowse  * Both address and port must be specified in argument sin.
4815200e00eSIan Dowse  * If don't have a local address for this socket yet,
4825200e00eSIan Dowse  * then pick one.
483999f1343SGarrett Wollman  */
484999f1343SGarrett Wollman int
485136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
486999f1343SGarrett Wollman {
4875200e00eSIan Dowse 	u_short lport, fport;
4885200e00eSIan Dowse 	in_addr_t laddr, faddr;
4895200e00eSIan Dowse 	int anonport, error;
490df8bae1dSRodney W. Grimes 
49127f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
4928501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
49327f74fd0SRobert Watson 
4945200e00eSIan Dowse 	lport = inp->inp_lport;
4955200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
4965200e00eSIan Dowse 	anonport = (lport == 0);
4975200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
498b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
4995200e00eSIan Dowse 	if (error)
5005200e00eSIan Dowse 		return (error);
5015200e00eSIan Dowse 
5025200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
5035200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
5045200e00eSIan Dowse 		inp->inp_lport = lport;
5055200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
5065200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
5075200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
5085200e00eSIan Dowse 			inp->inp_lport = 0;
5095200e00eSIan Dowse 			return (EAGAIN);
5105200e00eSIan Dowse 		}
5115200e00eSIan Dowse 	}
5125200e00eSIan Dowse 
5135200e00eSIan Dowse 	/* Commit the remaining changes. */
5145200e00eSIan Dowse 	inp->inp_lport = lport;
5155200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
5165200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
5175200e00eSIan Dowse 	inp->inp_fport = fport;
5185200e00eSIan Dowse 	in_pcbrehash(inp);
5192cb64cb2SGeorge V. Neville-Neil 
5205200e00eSIan Dowse 	if (anonport)
5215200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
5225200e00eSIan Dowse 	return (0);
5235200e00eSIan Dowse }
5245200e00eSIan Dowse 
5255200e00eSIan Dowse /*
5265200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
5275200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
5285200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
5295200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
5305200e00eSIan Dowse  * the connect.
5315200e00eSIan Dowse  *
5325200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
5335200e00eSIan Dowse  * and port. These are not updated in the error case.
5345200e00eSIan Dowse  *
5355200e00eSIan Dowse  * If the operation fails because the connection already exists,
5365200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
5375200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
5385200e00eSIan Dowse  * is set to NULL.
5395200e00eSIan Dowse  */
5405200e00eSIan Dowse int
541136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
542136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
543136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
5445200e00eSIan Dowse {
5455200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
5465200e00eSIan Dowse 	struct in_ifaddr *ia;
5475200e00eSIan Dowse 	struct sockaddr_in sa;
548b0330ed9SPawel Jakub Dawidek 	struct ucred *socred;
5495200e00eSIan Dowse 	struct inpcb *oinp;
5505200e00eSIan Dowse 	struct in_addr laddr, faddr;
5515200e00eSIan Dowse 	u_short lport, fport;
5525200e00eSIan Dowse 	int error;
5535200e00eSIan Dowse 
5548501a69cSRobert Watson 	/*
5558501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
5568501a69cSRobert Watson 	 * lock is sufficient.
5578501a69cSRobert Watson 	 */
5588501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
55927f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
56027f74fd0SRobert Watson 
5615200e00eSIan Dowse 	if (oinpp != NULL)
5625200e00eSIan Dowse 		*oinpp = NULL;
56357bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
564df8bae1dSRodney W. Grimes 		return (EINVAL);
565df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
566df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
567df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
568df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5695200e00eSIan Dowse 	laddr.s_addr = *laddrp;
5705200e00eSIan Dowse 	lport = *lportp;
5715200e00eSIan Dowse 	faddr = sin->sin_addr;
5725200e00eSIan Dowse 	fport = sin->sin_port;
573b0330ed9SPawel Jakub Dawidek 	socred = inp->inp_socket->so_cred;
574b0330ed9SPawel Jakub Dawidek 	if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
5755200e00eSIan Dowse 		bzero(&sa, sizeof(sa));
576b0330ed9SPawel Jakub Dawidek 		sa.sin_addr.s_addr = htonl(prison_getip(socred));
5775200e00eSIan Dowse 		sa.sin_len = sizeof(sa);
5785200e00eSIan Dowse 		sa.sin_family = AF_INET;
5795200e00eSIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
580b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, cred);
5815200e00eSIan Dowse 		if (error)
5825200e00eSIan Dowse 			return (error);
5835200e00eSIan Dowse 	}
58459562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
585df8bae1dSRodney W. Grimes 		/*
586df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
587df8bae1dSRodney W. Grimes 		 * use the primary local address.
588df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
589df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
590df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
591df8bae1dSRodney W. Grimes 		 */
5925200e00eSIan Dowse 		if (faddr.s_addr == INADDR_ANY)
5935200e00eSIan Dowse 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
5945200e00eSIan Dowse 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
5955200e00eSIan Dowse 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
5965200e00eSIan Dowse 		    IFF_BROADCAST))
5975200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
5985200e00eSIan Dowse 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
599df8bae1dSRodney W. Grimes 	}
6005200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
6018699ea08SBjoern A. Zeeb 		ia = NULL;
602df8bae1dSRodney W. Grimes 		/*
60397d8d152SAndre Oppermann 		 * If route is known our src addr is taken from the i/f,
60497d8d152SAndre Oppermann 		 * else punt.
605cf744713SAndre Oppermann 		 *
606cf744713SAndre Oppermann 		 * Find out route to destination
607df8bae1dSRodney W. Grimes 		 */
608cf744713SAndre Oppermann 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
6098b07e49aSJulian Elischer 			ia = ip_rtaddr(faddr, inp->inp_inc.inc_fibnum);
610df8bae1dSRodney W. Grimes 		/*
611cf744713SAndre Oppermann 		 * If we found a route, use the address corresponding to
612cf744713SAndre Oppermann 		 * the outgoing interface.
613cf744713SAndre Oppermann 		 *
614cf744713SAndre Oppermann 		 * Otherwise assume faddr is reachable on a directly connected
615cf744713SAndre Oppermann 		 * network and try to find a corresponding interface to take
616cf744713SAndre Oppermann 		 * the source address from.
617df8bae1dSRodney W. Grimes 		 */
6188699ea08SBjoern A. Zeeb 		if (ia == NULL) {
6195200e00eSIan Dowse 			bzero(&sa, sizeof(sa));
6205200e00eSIan Dowse 			sa.sin_addr = faddr;
6215200e00eSIan Dowse 			sa.sin_len = sizeof(sa);
6225200e00eSIan Dowse 			sa.sin_family = AF_INET;
623df8bae1dSRodney W. Grimes 
6245200e00eSIan Dowse 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
6258699ea08SBjoern A. Zeeb 			if (ia == NULL)
6265200e00eSIan Dowse 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
6278699ea08SBjoern A. Zeeb 			if (ia == NULL)
628ef14c369SMaxim Konovalov 				return (ENETUNREACH);
629df8bae1dSRodney W. Grimes 		}
630df8bae1dSRodney W. Grimes 		/*
631df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
632df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
633df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
634df8bae1dSRodney W. Grimes 		 */
6355200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
636df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
637df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
638df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
641df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
642df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
64337d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
644df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
645df8bae1dSRodney W. Grimes 						break;
6468699ea08SBjoern A. Zeeb 				if (ia == NULL)
647df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
648df8bae1dSRodney W. Grimes 			}
649df8bae1dSRodney W. Grimes 		}
6505200e00eSIan Dowse 		laddr = ia->ia_addr.sin_addr;
651999f1343SGarrett Wollman 	}
652999f1343SGarrett Wollman 
6535200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
6545200e00eSIan Dowse 	    0, NULL);
6555200e00eSIan Dowse 	if (oinp != NULL) {
6565200e00eSIan Dowse 		if (oinpp != NULL)
6575200e00eSIan Dowse 			*oinpp = oinp;
658df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
659c3229e05SDavid Greenman 	}
6605200e00eSIan Dowse 	if (lport == 0) {
661b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
662b0330ed9SPawel Jakub Dawidek 		    cred);
6635a903f8dSPierre Beyssac 		if (error)
6645a903f8dSPierre Beyssac 			return (error);
6655a903f8dSPierre Beyssac 	}
6665200e00eSIan Dowse 	*laddrp = laddr.s_addr;
6675200e00eSIan Dowse 	*lportp = lport;
6685200e00eSIan Dowse 	*faddrp = faddr.s_addr;
6695200e00eSIan Dowse 	*fportp = fport;
670df8bae1dSRodney W. Grimes 	return (0);
671df8bae1dSRodney W. Grimes }
672df8bae1dSRodney W. Grimes 
67326f9a767SRodney W. Grimes void
674136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
675df8bae1dSRodney W. Grimes {
6766b348152SRobert Watson 
677fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
6788501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
679df8bae1dSRodney W. Grimes 
680df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
681df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
68215bd2b43SDavid Greenman 	in_pcbrehash(inp);
683df8bae1dSRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
6854c7c478dSRobert Watson /*
6864c7c478dSRobert Watson  * In the old world order, in_pcbdetach() served two functions: to detach the
6874c7c478dSRobert Watson  * pcb from the socket/potentially free the socket, and to free the pcb
6884c7c478dSRobert Watson  * itself.  In the new world order, the protocol code is responsible for
6894c7c478dSRobert Watson  * managing the relationship with the socket, and this code simply frees the
6904c7c478dSRobert Watson  * pcb.
6914c7c478dSRobert Watson  */
69226f9a767SRodney W. Grimes void
693136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
694df8bae1dSRodney W. Grimes {
6954c7c478dSRobert Watson 
6964c7c478dSRobert Watson 	KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
6974c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
6984c7c478dSRobert Watson 	inp->inp_socket = NULL;
6994c7c478dSRobert Watson }
7004c7c478dSRobert Watson 
7014c7c478dSRobert Watson void
7024c7c478dSRobert Watson in_pcbfree(struct inpcb *inp)
7034c7c478dSRobert Watson {
7043d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
705df8bae1dSRodney W. Grimes 
7064c7c478dSRobert Watson 	KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
7078501a69cSRobert Watson 
708fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
7098501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
71059daba27SSam Leffler 
711b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
712cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
713b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
7143d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
715c3229e05SDavid Greenman 	in_pcbremlists(inp);
716df8bae1dSRodney W. Grimes 	if (inp->inp_options)
717df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
71871498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
71971498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
720cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
721d915b280SStephan Uphoff 
722a557af22SRobert Watson #ifdef MAC
72330d239bcSRobert Watson 	mac_inpcb_destroy(inp);
724a557af22SRobert Watson #endif
7258501a69cSRobert Watson 	INP_WUNLOCK(inp);
72669c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
727df8bae1dSRodney W. Grimes }
728df8bae1dSRodney W. Grimes 
72910702a28SRobert Watson /*
73010702a28SRobert Watson  * TCP needs to maintain its inpcb structure after the TCP connection has
73110702a28SRobert Watson  * been torn down.  However, it must be disconnected from the inpcb hashes as
73210702a28SRobert Watson  * it must not prevent binding of future connections to the same port/ip
73310702a28SRobert Watson  * combination by other inpcbs.
73410702a28SRobert Watson  */
73510702a28SRobert Watson void
73610702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
73710702a28SRobert Watson {
73810702a28SRobert Watson 
7397c5a8ab2SMarcel Moolenaar 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
7408501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
74110702a28SRobert Watson 
74210702a28SRobert Watson 	inp->inp_vflag |= INP_DROPPED;
74310702a28SRobert Watson 	if (inp->inp_lport) {
74410702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
74510702a28SRobert Watson 
74610702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
74710702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
74810702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
74910702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
75010702a28SRobert Watson 			free(phd, M_PCB);
75110702a28SRobert Watson 		}
75210702a28SRobert Watson 		inp->inp_lport = 0;
75310702a28SRobert Watson 	}
75410702a28SRobert Watson }
75510702a28SRobert Watson 
75654d642bbSRobert Watson /*
75754d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
75854d642bbSRobert Watson  */
75926ef6ac4SDon Lewis struct sockaddr *
760136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
76126ef6ac4SDon Lewis {
76226ef6ac4SDon Lewis 	struct sockaddr_in *sin;
76326ef6ac4SDon Lewis 
76426ef6ac4SDon Lewis 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
765a163d034SWarner Losh 		M_WAITOK | M_ZERO);
76626ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
76726ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
76826ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
76926ef6ac4SDon Lewis 	sin->sin_port = port;
77026ef6ac4SDon Lewis 
77126ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
77226ef6ac4SDon Lewis }
77326ef6ac4SDon Lewis 
774117bcae7SGarrett Wollman int
77554d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
776df8bae1dSRodney W. Grimes {
777136d4f1cSRobert Watson 	struct inpcb *inp;
77826ef6ac4SDon Lewis 	struct in_addr addr;
77926ef6ac4SDon Lewis 	in_port_t port;
78042fa505bSDavid Greenman 
781fdc984f7STor Egge 	inp = sotoinpcb(so);
78254d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
7836466b28aSRobert Watson 
784a69042a5SRobert Watson 	INP_RLOCK(inp);
78526ef6ac4SDon Lewis 	port = inp->inp_lport;
78626ef6ac4SDon Lewis 	addr = inp->inp_laddr;
787a69042a5SRobert Watson 	INP_RUNLOCK(inp);
78842fa505bSDavid Greenman 
78926ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
790117bcae7SGarrett Wollman 	return 0;
791df8bae1dSRodney W. Grimes }
792df8bae1dSRodney W. Grimes 
793117bcae7SGarrett Wollman int
79454d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
795df8bae1dSRodney W. Grimes {
796136d4f1cSRobert Watson 	struct inpcb *inp;
79726ef6ac4SDon Lewis 	struct in_addr addr;
79826ef6ac4SDon Lewis 	in_port_t port;
79942fa505bSDavid Greenman 
800fdc984f7STor Egge 	inp = sotoinpcb(so);
80154d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
8026466b28aSRobert Watson 
803a69042a5SRobert Watson 	INP_RLOCK(inp);
80426ef6ac4SDon Lewis 	port = inp->inp_fport;
80526ef6ac4SDon Lewis 	addr = inp->inp_faddr;
806a69042a5SRobert Watson 	INP_RUNLOCK(inp);
80742fa505bSDavid Greenman 
80826ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
809117bcae7SGarrett Wollman 	return 0;
810df8bae1dSRodney W. Grimes }
811df8bae1dSRodney W. Grimes 
81226f9a767SRodney W. Grimes void
813136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
814136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
815d1c54148SJesper Skriver {
816f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
817d1c54148SJesper Skriver 
8183dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
819f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
8208501a69cSRobert Watson 		INP_WLOCK(inp);
821d1c54148SJesper Skriver #ifdef INET6
822f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
8238501a69cSRobert Watson 			INP_WUNLOCK(inp);
824d1c54148SJesper Skriver 			continue;
825f76fcf6dSJeffrey Hsu 		}
826d1c54148SJesper Skriver #endif
827d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
828f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
8298501a69cSRobert Watson 			INP_WUNLOCK(inp);
830d1c54148SJesper Skriver 			continue;
831d1c54148SJesper Skriver 		}
8323dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
8338501a69cSRobert Watson 			INP_WUNLOCK(inp);
834f76fcf6dSJeffrey Hsu 	}
8353dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
836d1c54148SJesper Skriver }
837d1c54148SJesper Skriver 
838e43cc4aeSHajimu UMEMOTO void
839136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
840e43cc4aeSHajimu UMEMOTO {
841e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
842e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
843e43cc4aeSHajimu UMEMOTO 	int i, gap;
844e43cc4aeSHajimu UMEMOTO 
845f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
846712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
8478501a69cSRobert Watson 		INP_WLOCK(inp);
848e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
849e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
850e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
851e43cc4aeSHajimu UMEMOTO 			/*
852e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
853e43cc4aeSHajimu UMEMOTO 			 * detached.
854e43cc4aeSHajimu UMEMOTO 			 */
855e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
856e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
857e43cc4aeSHajimu UMEMOTO 
858e43cc4aeSHajimu UMEMOTO 			/*
859e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
860e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
861e43cc4aeSHajimu UMEMOTO 			 */
862e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
863e43cc4aeSHajimu UMEMOTO 			    i++) {
864e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
865e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
866e43cc4aeSHajimu UMEMOTO 					gap++;
867e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
868e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
869e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
870e43cc4aeSHajimu UMEMOTO 			}
871e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
872e43cc4aeSHajimu UMEMOTO 		}
8738501a69cSRobert Watson 		INP_WUNLOCK(inp);
874e43cc4aeSHajimu UMEMOTO 	}
8753cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
876e43cc4aeSHajimu UMEMOTO }
877e43cc4aeSHajimu UMEMOTO 
878df8bae1dSRodney W. Grimes /*
879c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
880c3229e05SDavid Greenman  */
881d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
882df8bae1dSRodney W. Grimes struct inpcb *
883136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
884078b7042SBjoern A. Zeeb     u_short lport, int wild_okay, struct ucred *cred)
885df8bae1dSRodney W. Grimes {
886136d4f1cSRobert Watson 	struct inpcb *inp;
887d5e8a67eSHajimu UMEMOTO #ifdef INET6
888d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
889d5e8a67eSHajimu UMEMOTO #else
890d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
891d5e8a67eSHajimu UMEMOTO #endif
892d5e8a67eSHajimu UMEMOTO 	int wildcard;
8937bc4aca7SDavid Greenman 
8948501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
8951b73ca0bSSam Leffler 
896c3229e05SDavid Greenman 	if (!wild_okay) {
897c3229e05SDavid Greenman 		struct inpcbhead *head;
898c3229e05SDavid Greenman 		/*
899c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
900c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
901c3229e05SDavid Greenman 		 */
902712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
903712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
904fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
905cfa1ca9dSYoshinobu Inoue #ifdef INET6
906369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
907cfa1ca9dSYoshinobu Inoue 				continue;
908cfa1ca9dSYoshinobu Inoue #endif
909c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
910c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
911c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
912c3229e05SDavid Greenman 				/*
913c3229e05SDavid Greenman 				 * Found.
914c3229e05SDavid Greenman 				 */
915c3229e05SDavid Greenman 				return (inp);
916df8bae1dSRodney W. Grimes 			}
917c3229e05SDavid Greenman 		}
918c3229e05SDavid Greenman 		/*
919c3229e05SDavid Greenman 		 * Not found.
920c3229e05SDavid Greenman 		 */
921c3229e05SDavid Greenman 		return (NULL);
922c3229e05SDavid Greenman 	} else {
923c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
924c3229e05SDavid Greenman 		struct inpcbport *phd;
925c3229e05SDavid Greenman 		struct inpcb *match = NULL;
926c3229e05SDavid Greenman 		/*
927c3229e05SDavid Greenman 		 * Best fit PCB lookup.
928c3229e05SDavid Greenman 		 *
929c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
930c3229e05SDavid Greenman 		 * port hash list.
931c3229e05SDavid Greenman 		 */
932712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
933712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
934fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
935c3229e05SDavid Greenman 			if (phd->phd_port == lport)
936c3229e05SDavid Greenman 				break;
937c3229e05SDavid Greenman 		}
938c3229e05SDavid Greenman 		if (phd != NULL) {
939c3229e05SDavid Greenman 			/*
940c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
941c3229e05SDavid Greenman 			 * fit.
942c3229e05SDavid Greenman 			 */
94337d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
944c3229e05SDavid Greenman 				wildcard = 0;
945cfa1ca9dSYoshinobu Inoue #ifdef INET6
946369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
947cfa1ca9dSYoshinobu Inoue 					continue;
948d5e8a67eSHajimu UMEMOTO 				/*
949d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
950d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
951d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
952d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
953d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
954d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
955d5e8a67eSHajimu UMEMOTO 				 *
956d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
957d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
958d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
959d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
960d5e8a67eSHajimu UMEMOTO 				 */
961d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
962d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
963cfa1ca9dSYoshinobu Inoue #endif
964c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
965c3229e05SDavid Greenman 					wildcard++;
96615bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
96715bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
96815bd2b43SDavid Greenman 						wildcard++;
96915bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
97015bd2b43SDavid Greenman 						continue;
97115bd2b43SDavid Greenman 				} else {
97215bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
97315bd2b43SDavid Greenman 						wildcard++;
97415bd2b43SDavid Greenman 				}
975df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
976df8bae1dSRodney W. Grimes 					match = inp;
977df8bae1dSRodney W. Grimes 					matchwild = wildcard;
9783dbdc25cSDavid Greenman 					if (matchwild == 0) {
979df8bae1dSRodney W. Grimes 						break;
980df8bae1dSRodney W. Grimes 					}
981df8bae1dSRodney W. Grimes 				}
9823dbdc25cSDavid Greenman 			}
983c3229e05SDavid Greenman 		}
984df8bae1dSRodney W. Grimes 		return (match);
985df8bae1dSRodney W. Grimes 	}
986c3229e05SDavid Greenman }
987d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
98815bd2b43SDavid Greenman 
98915bd2b43SDavid Greenman /*
99015bd2b43SDavid Greenman  * Lookup PCB in hash list.
99115bd2b43SDavid Greenman  */
99215bd2b43SDavid Greenman struct inpcb *
993136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
994136d4f1cSRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
995136d4f1cSRobert Watson     struct ifnet *ifp)
99615bd2b43SDavid Greenman {
99715bd2b43SDavid Greenman 	struct inpcbhead *head;
998136d4f1cSRobert Watson 	struct inpcb *inp;
99915bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
100015bd2b43SDavid Greenman 
10018501a69cSRobert Watson 	INP_INFO_LOCK_ASSERT(pcbinfo);
1002602cc7f1SRobert Watson 
100315bd2b43SDavid Greenman 	/*
100415bd2b43SDavid Greenman 	 * First look for an exact match.
100515bd2b43SDavid Greenman 	 */
1006712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1007712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1008fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1009cfa1ca9dSYoshinobu Inoue #ifdef INET6
1010369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1011cfa1ca9dSYoshinobu Inoue 			continue;
1012cfa1ca9dSYoshinobu Inoue #endif
10136d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1014ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1015ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1016e3fd5ffdSRobert Watson 		    inp->inp_lport == lport)
1017c3229e05SDavid Greenman 			return (inp);
1018c3229e05SDavid Greenman 	}
1019e3fd5ffdSRobert Watson 
1020e3fd5ffdSRobert Watson 	/*
1021e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1022e3fd5ffdSRobert Watson 	 */
10236d6a026bSDavid Greenman 	if (wildcard) {
10246d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
1025e3fd5ffdSRobert Watson #ifdef INET6
1026cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1027e3fd5ffdSRobert Watson #endif
10286d6a026bSDavid Greenman 
1029712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1030712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1031fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1032cfa1ca9dSYoshinobu Inoue #ifdef INET6
1033369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1034cfa1ca9dSYoshinobu Inoue 				continue;
1035cfa1ca9dSYoshinobu Inoue #endif
10366d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1037c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1038cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
1039cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
1040cfa1ca9dSYoshinobu Inoue 					continue;
10416d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1042c3229e05SDavid Greenman 					return (inp);
1043cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1044e3fd5ffdSRobert Watson #ifdef INET6
1045cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1046cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
1047cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
1048cfa1ca9dSYoshinobu Inoue 					else
1049e3fd5ffdSRobert Watson #endif
10506d6a026bSDavid Greenman 						local_wild = inp;
10516d6a026bSDavid Greenman 				}
10526d6a026bSDavid Greenman 			}
1053cfa1ca9dSYoshinobu Inoue 		}
1054e3fd5ffdSRobert Watson #ifdef INET6
1055cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
1056cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
1057e3fd5ffdSRobert Watson #endif
1058c3229e05SDavid Greenman 		return (local_wild);
10596d6a026bSDavid Greenman 	}
10606d6a026bSDavid Greenman 	return (NULL);
106115bd2b43SDavid Greenman }
106215bd2b43SDavid Greenman 
10637bc4aca7SDavid Greenman /*
1064c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
10657bc4aca7SDavid Greenman  */
1066c3229e05SDavid Greenman int
1067136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
106815bd2b43SDavid Greenman {
1069c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1070c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1071c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1072c3229e05SDavid Greenman 	struct inpcbport *phd;
1073cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
107415bd2b43SDavid Greenman 
107559daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
10768501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1077602cc7f1SRobert Watson 
1078cfa1ca9dSYoshinobu Inoue #ifdef INET6
1079cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1080cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1081cfa1ca9dSYoshinobu Inoue 	else
1082cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1083cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1084cfa1ca9dSYoshinobu Inoue 
1085712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1086712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
108715bd2b43SDavid Greenman 
1088712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
1089712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1090c3229e05SDavid Greenman 
1091c3229e05SDavid Greenman 	/*
1092c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1093c3229e05SDavid Greenman 	 */
1094fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1095c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1096c3229e05SDavid Greenman 			break;
1097c3229e05SDavid Greenman 	}
1098c3229e05SDavid Greenman 	/*
1099c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1100c3229e05SDavid Greenman 	 */
1101c3229e05SDavid Greenman 	if (phd == NULL) {
1102c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1103c3229e05SDavid Greenman 		if (phd == NULL) {
1104c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1105c3229e05SDavid Greenman 		}
1106c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1107c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1108c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1109c3229e05SDavid Greenman 	}
1110c3229e05SDavid Greenman 	inp->inp_phd = phd;
1111c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1112c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1113c3229e05SDavid Greenman 	return (0);
111415bd2b43SDavid Greenman }
111515bd2b43SDavid Greenman 
1116c3229e05SDavid Greenman /*
1117c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1118c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1119c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1120c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1121c3229e05SDavid Greenman  */
112215bd2b43SDavid Greenman void
1123136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp)
112415bd2b43SDavid Greenman {
112559daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
112615bd2b43SDavid Greenman 	struct inpcbhead *head;
1127cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
112815bd2b43SDavid Greenman 
112959daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
11308501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1131602cc7f1SRobert Watson 
1132cfa1ca9dSYoshinobu Inoue #ifdef INET6
1133cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1134cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1135cfa1ca9dSYoshinobu Inoue 	else
1136cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1137cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1138cfa1ca9dSYoshinobu Inoue 
1139712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1140712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
114115bd2b43SDavid Greenman 
1142c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
114315bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1144c3229e05SDavid Greenman }
1145c3229e05SDavid Greenman 
1146c3229e05SDavid Greenman /*
1147c3229e05SDavid Greenman  * Remove PCB from various lists.
1148c3229e05SDavid Greenman  */
114976429de4SYoshinobu Inoue void
1150136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1151c3229e05SDavid Greenman {
115259daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
115359daba27SSam Leffler 
115459daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
11558501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
115659daba27SSam Leffler 
115759daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1158c3229e05SDavid Greenman 	if (inp->inp_lport) {
1159c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1160c3229e05SDavid Greenman 
1161c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1162c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1163fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1164c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1165c3229e05SDavid Greenman 			free(phd, M_PCB);
1166c3229e05SDavid Greenman 		}
1167c3229e05SDavid Greenman 	}
1168c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
116959daba27SSam Leffler 	pcbinfo->ipi_count--;
117015bd2b43SDavid Greenman }
117175c13541SPoul-Henning Kamp 
1172a557af22SRobert Watson /*
1173a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1174a557af22SRobert Watson  * label change into the in_pcb for the socket.
1175a557af22SRobert Watson  */
1176a557af22SRobert Watson void
1177136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1178a557af22SRobert Watson {
1179a557af22SRobert Watson #ifdef MAC
1180a557af22SRobert Watson 	struct inpcb *inp;
1181a557af22SRobert Watson 
11824c7c478dSRobert Watson 	inp = sotoinpcb(so);
11834c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1184602cc7f1SRobert Watson 
11858501a69cSRobert Watson 	INP_WLOCK(inp);
1186310e7cebSRobert Watson 	SOCK_LOCK(so);
1187a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1188310e7cebSRobert Watson 	SOCK_UNLOCK(so);
11898501a69cSRobert Watson 	INP_WUNLOCK(inp);
1190a557af22SRobert Watson #endif
1191a557af22SRobert Watson }
11925f311da2SMike Silbersack 
11935f311da2SMike Silbersack /*
1194ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
1195ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
1196ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
1197ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
1198ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
11995f311da2SMike Silbersack  */
12005f311da2SMike Silbersack void
1201136d4f1cSRobert Watson ipport_tick(void *xtp)
12025f311da2SMike Silbersack {
1203ad3a630fSRobert Watson 
1204ad3a630fSRobert Watson 	if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
12055f311da2SMike Silbersack 		if (ipport_stoprandom > 0)
12065f311da2SMike Silbersack 			ipport_stoprandom--;
1207ad3a630fSRobert Watson 	} else
1208ad3a630fSRobert Watson 		ipport_stoprandom = ipport_randomtime;
12095f311da2SMike Silbersack 	ipport_tcplastcount = ipport_tcpallocs;
12105f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
12115f311da2SMike Silbersack }
1212497057eeSRobert Watson 
12133d585327SKip Macy void
12143d585327SKip Macy inp_wlock(struct inpcb *inp)
12153d585327SKip Macy {
12163d585327SKip Macy 
12178501a69cSRobert Watson 	INP_WLOCK(inp);
12183d585327SKip Macy }
12193d585327SKip Macy 
12203d585327SKip Macy void
12213d585327SKip Macy inp_wunlock(struct inpcb *inp)
12223d585327SKip Macy {
12233d585327SKip Macy 
12248501a69cSRobert Watson 	INP_WUNLOCK(inp);
12253d585327SKip Macy }
12263d585327SKip Macy 
12273d585327SKip Macy void
12283d585327SKip Macy inp_rlock(struct inpcb *inp)
12293d585327SKip Macy {
12303d585327SKip Macy 
1231a69042a5SRobert Watson 	INP_RLOCK(inp);
12323d585327SKip Macy }
12333d585327SKip Macy 
12343d585327SKip Macy void
12353d585327SKip Macy inp_runlock(struct inpcb *inp)
12363d585327SKip Macy {
12373d585327SKip Macy 
1238a69042a5SRobert Watson 	INP_RUNLOCK(inp);
12393d585327SKip Macy }
12403d585327SKip Macy 
12413d585327SKip Macy #ifdef INVARIANTS
12423d585327SKip Macy void
1243e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
12443d585327SKip Macy {
12453d585327SKip Macy 
12468501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
12473d585327SKip Macy }
12483d585327SKip Macy 
12493d585327SKip Macy void
1250e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
12513d585327SKip Macy {
12523d585327SKip Macy 
12533d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
12543d585327SKip Macy }
12553d585327SKip Macy #endif
12563d585327SKip Macy 
12579378e437SKip Macy void
12589378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
12599378e437SKip Macy {
12609378e437SKip Macy 	struct inpcb *inp;
12619378e437SKip Macy 
12629378e437SKip Macy 	INP_INFO_RLOCK(&tcbinfo);
12639378e437SKip Macy 	LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) {
12649378e437SKip Macy 		INP_WLOCK(inp);
12659378e437SKip Macy 		func(inp, arg);
12669378e437SKip Macy 		INP_WUNLOCK(inp);
12679378e437SKip Macy 	}
12689378e437SKip Macy 	INP_INFO_RUNLOCK(&tcbinfo);
12699378e437SKip Macy }
12709378e437SKip Macy 
12719378e437SKip Macy struct socket *
12729378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
12739378e437SKip Macy {
12749378e437SKip Macy 
12759378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
12769378e437SKip Macy 	return (inp->inp_socket);
12779378e437SKip Macy }
12789378e437SKip Macy 
12799378e437SKip Macy struct tcpcb *
12809378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
12819378e437SKip Macy {
12829378e437SKip Macy 
12839378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
12849378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
12859378e437SKip Macy }
12869378e437SKip Macy 
12879378e437SKip Macy int
12889378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
12899378e437SKip Macy {
12909378e437SKip Macy 
12919378e437SKip Macy 	return (inp->inp_ip_tos);
12929378e437SKip Macy }
12939378e437SKip Macy 
12949378e437SKip Macy void
12959378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
12969378e437SKip Macy {
12979378e437SKip Macy 
12989378e437SKip Macy 	inp->inp_ip_tos = val;
12999378e437SKip Macy }
13009378e437SKip Macy 
13019378e437SKip Macy void
13029d29c635SKip Macy inp_4tuple_get(const struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
13039d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
13049378e437SKip Macy {
13059378e437SKip Macy 
13069d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
13079d29c635SKip Macy 	*laddr = inp->inp_laddr;
13089d29c635SKip Macy 	*faddr = inp->inp_faddr;
13099378e437SKip Macy 	*lp = inp->inp_lport;
13109378e437SKip Macy 	*fp = inp->inp_fport;
13119378e437SKip Macy }
13129378e437SKip Macy 
1313dd0e6c38SKip Macy struct inpcb *
1314dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
1315dd0e6c38SKip Macy {
1316dd0e6c38SKip Macy 
1317dd0e6c38SKip Macy 	return (sotoinpcb(so));
1318dd0e6c38SKip Macy }
1319dd0e6c38SKip Macy 
1320dd0e6c38SKip Macy struct tcpcb *
1321dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
1322dd0e6c38SKip Macy {
1323dd0e6c38SKip Macy 
1324dd0e6c38SKip Macy 	return (sototcpcb(so));
1325dd0e6c38SKip Macy }
1326dd0e6c38SKip Macy 
1327497057eeSRobert Watson #ifdef DDB
1328497057eeSRobert Watson static void
1329497057eeSRobert Watson db_print_indent(int indent)
1330497057eeSRobert Watson {
1331497057eeSRobert Watson 	int i;
1332497057eeSRobert Watson 
1333497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1334497057eeSRobert Watson 		db_printf(" ");
1335497057eeSRobert Watson }
1336497057eeSRobert Watson 
1337497057eeSRobert Watson static void
1338497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1339497057eeSRobert Watson {
1340497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
1341497057eeSRobert Watson 
1342497057eeSRobert Watson 	db_print_indent(indent);
1343497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
1344497057eeSRobert Watson 
1345497057eeSRobert Watson 	indent += 2;
1346497057eeSRobert Watson 
134703dc38a4SRobert Watson #ifdef INET6
1348497057eeSRobert Watson 	if (inc->inc_flags == 1) {
1349497057eeSRobert Watson 		/* IPv6. */
1350497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1351497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1352497057eeSRobert Watson 	} else {
135303dc38a4SRobert Watson #endif
1354497057eeSRobert Watson 		/* IPv4. */
1355497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1356497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
135703dc38a4SRobert Watson #ifdef INET6
1358497057eeSRobert Watson 	}
135903dc38a4SRobert Watson #endif
1360497057eeSRobert Watson 	db_print_indent(indent);
1361497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1362497057eeSRobert Watson 	    ntohs(inc->inc_lport));
1363497057eeSRobert Watson 	db_print_indent(indent);
1364497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1365497057eeSRobert Watson 	    ntohs(inc->inc_fport));
1366497057eeSRobert Watson }
1367497057eeSRobert Watson 
1368497057eeSRobert Watson static void
1369497057eeSRobert Watson db_print_inpflags(int inp_flags)
1370497057eeSRobert Watson {
1371497057eeSRobert Watson 	int comma;
1372497057eeSRobert Watson 
1373497057eeSRobert Watson 	comma = 0;
1374497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
1375497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1376497057eeSRobert Watson 		comma = 1;
1377497057eeSRobert Watson 	}
1378497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
1379497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1380497057eeSRobert Watson 		comma = 1;
1381497057eeSRobert Watson 	}
1382497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
1383497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1384497057eeSRobert Watson 		comma = 1;
1385497057eeSRobert Watson 	}
1386497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
1387497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
1388497057eeSRobert Watson 		comma = 1;
1389497057eeSRobert Watson 	}
1390497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
1391497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1392497057eeSRobert Watson 		comma = 1;
1393497057eeSRobert Watson 	}
1394497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
1395497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
1396497057eeSRobert Watson 		comma = 1;
1397497057eeSRobert Watson 	}
1398497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
1399497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
1400497057eeSRobert Watson 		comma = 1;
1401497057eeSRobert Watson 	}
1402497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
1403497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
1404497057eeSRobert Watson 		comma = 1;
1405497057eeSRobert Watson 	}
1406497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
1407497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
1408497057eeSRobert Watson 		comma = 1;
1409497057eeSRobert Watson 	}
1410497057eeSRobert Watson 	if (inp_flags & INP_FAITH) {
1411497057eeSRobert Watson 		db_printf("%sINP_FAITH", comma ? ", " : "");
1412497057eeSRobert Watson 		comma = 1;
1413497057eeSRobert Watson 	}
1414497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
1415497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
1416497057eeSRobert Watson 		comma = 1;
1417497057eeSRobert Watson 	}
1418497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
1419497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1420497057eeSRobert Watson 		comma = 1;
1421497057eeSRobert Watson 	}
1422497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
1423497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1424497057eeSRobert Watson 		comma = 1;
1425497057eeSRobert Watson 	}
1426497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
1427497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1428497057eeSRobert Watson 		comma = 1;
1429497057eeSRobert Watson 	}
1430497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
1431497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1432497057eeSRobert Watson 		comma = 1;
1433497057eeSRobert Watson 	}
1434497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
1435497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1436497057eeSRobert Watson 		comma = 1;
1437497057eeSRobert Watson 	}
1438497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
1439497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1440497057eeSRobert Watson 		comma = 1;
1441497057eeSRobert Watson 	}
1442497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
1443497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1444497057eeSRobert Watson 		comma = 1;
1445497057eeSRobert Watson 	}
1446497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
1447497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1448497057eeSRobert Watson 		comma = 1;
1449497057eeSRobert Watson 	}
1450497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
1451497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1452497057eeSRobert Watson 		comma = 1;
1453497057eeSRobert Watson 	}
1454497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
1455497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1456497057eeSRobert Watson 		comma = 1;
1457497057eeSRobert Watson 	}
1458497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
1459497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1460497057eeSRobert Watson 		comma = 1;
1461497057eeSRobert Watson 	}
1462497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
1463497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
1464497057eeSRobert Watson 		comma = 1;
1465497057eeSRobert Watson 	}
1466497057eeSRobert Watson }
1467497057eeSRobert Watson 
1468497057eeSRobert Watson static void
1469497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
1470497057eeSRobert Watson {
1471497057eeSRobert Watson 	int comma;
1472497057eeSRobert Watson 
1473497057eeSRobert Watson 	comma = 0;
1474497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
1475497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
1476497057eeSRobert Watson 		comma  = 1;
1477497057eeSRobert Watson 	}
1478497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
1479497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
1480497057eeSRobert Watson 		comma  = 1;
1481497057eeSRobert Watson 	}
1482497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
1483497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1484497057eeSRobert Watson 		comma  = 1;
1485497057eeSRobert Watson 	}
1486497057eeSRobert Watson 	if (inp_vflag & INP_TIMEWAIT) {
1487497057eeSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1488497057eeSRobert Watson 		comma  = 1;
1489497057eeSRobert Watson 	}
1490497057eeSRobert Watson 	if (inp_vflag & INP_ONESBCAST) {
1491497057eeSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1492497057eeSRobert Watson 		comma  = 1;
1493497057eeSRobert Watson 	}
1494497057eeSRobert Watson 	if (inp_vflag & INP_DROPPED) {
1495497057eeSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
1496497057eeSRobert Watson 		comma  = 1;
1497497057eeSRobert Watson 	}
1498497057eeSRobert Watson 	if (inp_vflag & INP_SOCKREF) {
1499497057eeSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
1500497057eeSRobert Watson 		comma  = 1;
1501497057eeSRobert Watson 	}
1502497057eeSRobert Watson }
1503497057eeSRobert Watson 
1504497057eeSRobert Watson void
1505497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1506497057eeSRobert Watson {
1507497057eeSRobert Watson 
1508497057eeSRobert Watson 	db_print_indent(indent);
1509497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
1510497057eeSRobert Watson 
1511497057eeSRobert Watson 	indent += 2;
1512497057eeSRobert Watson 
1513497057eeSRobert Watson 	db_print_indent(indent);
1514497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1515497057eeSRobert Watson 
1516497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1517497057eeSRobert Watson 
1518497057eeSRobert Watson 	db_print_indent(indent);
1519497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1520497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1521497057eeSRobert Watson 
1522497057eeSRobert Watson 	db_print_indent(indent);
1523497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
1524497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
1525497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
1526497057eeSRobert Watson 	db_printf(")\n");
1527497057eeSRobert Watson 
1528497057eeSRobert Watson 	db_print_indent(indent);
1529497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1530497057eeSRobert Watson 	    inp->inp_vflag);
1531497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
1532497057eeSRobert Watson 	db_printf(")\n");
1533497057eeSRobert Watson 
1534497057eeSRobert Watson 	db_print_indent(indent);
1535497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1536497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1537497057eeSRobert Watson 
1538497057eeSRobert Watson 	db_print_indent(indent);
1539497057eeSRobert Watson #ifdef INET6
1540497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
1541497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
1542497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
1543497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
1544497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1545497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1546497057eeSRobert Watson 		    inp->in6p_hops);
1547497057eeSRobert Watson 	} else
1548497057eeSRobert Watson #endif
1549497057eeSRobert Watson 	{
1550497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1551497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1552497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
1553497057eeSRobert Watson 	}
1554497057eeSRobert Watson 
1555497057eeSRobert Watson 	db_print_indent(indent);
1556497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1557497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
1558497057eeSRobert Watson }
1559497057eeSRobert Watson 
1560497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1561497057eeSRobert Watson {
1562497057eeSRobert Watson 	struct inpcb *inp;
1563497057eeSRobert Watson 
1564497057eeSRobert Watson 	if (!have_addr) {
1565497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
1566497057eeSRobert Watson 		return;
1567497057eeSRobert Watson 	}
1568497057eeSRobert Watson 	inp = (struct inpcb *)addr;
1569497057eeSRobert Watson 
1570497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
1571497057eeSRobert Watson }
1572497057eeSRobert Watson #endif
1573