xref: /freebsd/sys/netinet/in_pcb.c (revision 712fc218a08cd427cbebc71de439e4cf1458619f)
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
32c3aac50fSPeter Wemm  * $FreeBSD$
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
35497057eeSRobert Watson #include "opt_ddb.h"
366a800098SYoshinobu Inoue #include "opt_ipsec.h"
37cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
38a557af22SRobert Watson #include "opt_mac.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>
48acd3428bSRobert Watson #include <sys/priv.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
5075c13541SPoul-Henning Kamp #include <sys/jail.h>
51101f9fc8SPeter Wemm #include <sys/kernel.h>
52101f9fc8SPeter Wemm #include <sys/sysctl.h>
538781d8e9SBruce Evans 
54497057eeSRobert Watson #ifdef DDB
55497057eeSRobert Watson #include <ddb/ddb.h>
56497057eeSRobert Watson #endif
57497057eeSRobert Watson 
5869c2d429SJeff Roberson #include <vm/uma.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <net/if.h>
61cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
62df8bae1dSRodney W. Grimes #include <net/route.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
68340c35deSJonathan Lemon #include <netinet/tcp_var.h>
695f311da2SMike Silbersack #include <netinet/udp.h>
705f311da2SMike Silbersack #include <netinet/udp_var.h>
71cfa1ca9dSYoshinobu Inoue #ifdef INET6
72cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
73cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
74cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
75cfa1ca9dSYoshinobu Inoue 
76cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
77cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
78cfa1ca9dSYoshinobu Inoue #include <netkey/key.h>
79cfa1ca9dSYoshinobu Inoue #endif /* IPSEC */
80df8bae1dSRodney W. Grimes 
81b9234fafSSam Leffler #ifdef FAST_IPSEC
82b9234fafSSam Leffler #if defined(IPSEC) || defined(IPSEC_ESP)
83b9234fafSSam Leffler #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
84b9234fafSSam Leffler #endif
85b9234fafSSam Leffler 
86b9234fafSSam Leffler #include <netipsec/ipsec.h>
87b9234fafSSam Leffler #include <netipsec/key.h>
88b9234fafSSam Leffler #endif /* FAST_IPSEC */
89b9234fafSSam Leffler 
90aed55708SRobert Watson #include <security/mac/mac_framework.h>
91aed55708SRobert Watson 
92101f9fc8SPeter Wemm /*
93101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
94101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
95101f9fc8SPeter Wemm  */
9682cd038dSYoshinobu Inoue int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
9782cd038dSYoshinobu Inoue int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
989e5a5ed4SMike Silbersack int	ipport_firstauto = IPPORT_HIFIRSTAUTO;		/* 49152 */
999e5a5ed4SMike Silbersack int	ipport_lastauto  = IPPORT_HILASTAUTO;		/* 65535 */
10082cd038dSYoshinobu Inoue int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
10182cd038dSYoshinobu Inoue int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
102101f9fc8SPeter Wemm 
103b0d22693SCrist J. Clark /*
104b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
105b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
106b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
107b0d22693SCrist J. Clark  */
108b0d22693SCrist J. Clark int	ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
109b0d22693SCrist J. Clark int	ipport_reservedlow = 0;
110b0d22693SCrist J. Clark 
1115f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
1125f311da2SMike Silbersack int	ipport_randomized = 1;	/* user controlled via sysctl */
1135f311da2SMike Silbersack int	ipport_randomcps = 10;	/* user controlled via sysctl */
1145f311da2SMike Silbersack int	ipport_randomtime = 45;	/* user controlled via sysctl */
1155f311da2SMike Silbersack int	ipport_stoprandom = 0;	/* toggled by ipport_tick */
1165f311da2SMike Silbersack int	ipport_tcpallocs;
1175f311da2SMike Silbersack int	ipport_tcplastcount;
1186ac48b74SMike Silbersack 
119bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
120bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
121bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
122bbd42ad0SPeter Wemm 
123bbd42ad0SPeter Wemm static int
12482d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
125bbd42ad0SPeter Wemm {
12630a4ab08SBruce Evans 	int error;
12730a4ab08SBruce Evans 
12830a4ab08SBruce Evans 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
12930a4ab08SBruce Evans 	if (error == 0) {
130bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
131bbd42ad0SPeter Wemm 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
13230a4ab08SBruce Evans 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
13330a4ab08SBruce Evans 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
13430a4ab08SBruce Evans 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
13530a4ab08SBruce Evans 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
136bbd42ad0SPeter Wemm 	}
13730a4ab08SBruce Evans 	return (error);
138bbd42ad0SPeter Wemm }
139bbd42ad0SPeter Wemm 
140bbd42ad0SPeter Wemm #undef RANGECHK
141bbd42ad0SPeter Wemm 
14233b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
14333b3ac06SPeter Wemm 
144bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
145bbd42ad0SPeter Wemm 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
146bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
147bbd42ad0SPeter Wemm 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
148bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
149bbd42ad0SPeter Wemm 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
150bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
151bbd42ad0SPeter Wemm 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
152bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
153bbd42ad0SPeter Wemm 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
154bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
155bbd42ad0SPeter Wemm 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
156b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
157b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
158b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
159b0d22693SCrist J. Clark 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
1606ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
1616ee79c59SMaxim Konovalov 	   &ipport_randomized, 0, "Enable random port allocation");
1626ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
1636ee79c59SMaxim Konovalov 	   &ipport_randomcps, 0, "Maximum number of random port "
1646ee79c59SMaxim Konovalov 	   "allocations before switching to a sequental one");
1656ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
1666ee79c59SMaxim Konovalov 	   &ipport_randomtime, 0, "Minimum time to keep sequental port "
1676ee79c59SMaxim Konovalov 	   "allocation before switching to a random one");
1680312fbe9SPoul-Henning Kamp 
169c3229e05SDavid Greenman /*
170c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
171c3229e05SDavid Greenman  *
172de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
173de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
174de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
175c3229e05SDavid Greenman  */
176c3229e05SDavid Greenman 
177c3229e05SDavid Greenman /*
178c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
179d915b280SStephan Uphoff  * On success return with the PCB locked.
180c3229e05SDavid Greenman  */
181df8bae1dSRodney W. Grimes int
182d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
183df8bae1dSRodney W. Grimes {
184136d4f1cSRobert Watson 	struct inpcb *inp;
18513cf67f3SHajimu UMEMOTO 	int error;
186a557af22SRobert Watson 
18759daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
188a557af22SRobert Watson 	error = 0;
189d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
190df8bae1dSRodney W. Grimes 	if (inp == NULL)
191df8bae1dSRodney W. Grimes 		return (ENOBUFS);
192d915b280SStephan Uphoff 	bzero(inp, inp_zero_size);
19315bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
194df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
195a557af22SRobert Watson #ifdef MAC
196a557af22SRobert Watson 	error = mac_init_inpcb(inp, M_NOWAIT);
197a557af22SRobert Watson 	if (error != 0)
198a557af22SRobert Watson 		goto out;
199310e7cebSRobert Watson 	SOCK_LOCK(so);
200a557af22SRobert Watson 	mac_create_inpcb_from_socket(so, inp);
201310e7cebSRobert Watson 	SOCK_UNLOCK(so);
202a557af22SRobert Watson #endif
2030f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
2040f9ade71SHajimu UMEMOTO #ifdef FAST_IPSEC
20513cf67f3SHajimu UMEMOTO 	error = ipsec_init_policy(so, &inp->inp_sp);
2060f9ade71SHajimu UMEMOTO #else
2070f9ade71SHajimu UMEMOTO 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
2080f9ade71SHajimu UMEMOTO #endif
209a557af22SRobert Watson 	if (error != 0)
210a557af22SRobert Watson 		goto out;
21113cf67f3SHajimu UMEMOTO #endif /*IPSEC*/
212e3fd5ffdSRobert Watson #ifdef INET6
213340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
214340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
215340c35deSJonathan Lemon 		if (ip6_v6only)
21633841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
217340c35deSJonathan Lemon 	}
21875daea93SPaul Saab #endif
219712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
2203d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
221df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
22233841545SHajimu UMEMOTO #ifdef INET6
22333841545SHajimu UMEMOTO 	if (ip6_auto_flowlabel)
22433841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
22533841545SHajimu UMEMOTO #endif
226d915b280SStephan Uphoff 	INP_LOCK(inp);
227d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
228d915b280SStephan Uphoff 
229a557af22SRobert Watson #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
230a557af22SRobert Watson out:
231a557af22SRobert Watson 	if (error != 0)
232a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
233a557af22SRobert Watson #endif
234a557af22SRobert Watson 	return (error);
235df8bae1dSRodney W. Grimes }
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes int
238136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
239df8bae1dSRodney W. Grimes {
2404b932371SIan Dowse 	int anonport, error;
2414b932371SIan Dowse 
2421b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
24359daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
24459daba27SSam Leffler 
2454b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
2464b932371SIan Dowse 		return (EINVAL);
2474b932371SIan Dowse 	anonport = inp->inp_lport == 0 && (nam == NULL ||
2484b932371SIan Dowse 	    ((struct sockaddr_in *)nam)->sin_port == 0);
2494b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
250b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
2514b932371SIan Dowse 	if (error)
2524b932371SIan Dowse 		return (error);
2534b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
2544b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
2554b932371SIan Dowse 		inp->inp_lport = 0;
2564b932371SIan Dowse 		return (EAGAIN);
2574b932371SIan Dowse 	}
2584b932371SIan Dowse 	if (anonport)
2594b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
2604b932371SIan Dowse 	return (0);
2614b932371SIan Dowse }
2624b932371SIan Dowse 
2634b932371SIan Dowse /*
2644b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
2654b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
2664b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
2674b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
2684b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
2694b932371SIan Dowse  *
2704b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
2714b932371SIan Dowse  */
2724b932371SIan Dowse int
273136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
274136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
2754b932371SIan Dowse {
2764b932371SIan Dowse 	struct socket *so = inp->inp_socket;
27737bd2b30SPeter Wemm 	unsigned short *lastport;
27815bd2b43SDavid Greenman 	struct sockaddr_in *sin;
279c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2804b932371SIan Dowse 	struct in_addr laddr;
281df8bae1dSRodney W. Grimes 	u_short lport = 0;
2824cc20ab1SSeigo Tanimura 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
28375c13541SPoul-Henning Kamp 	int error, prison = 0;
2845f311da2SMike Silbersack 	int dorandom;
285df8bae1dSRodney W. Grimes 
2861b73ca0bSSam Leffler 	INP_INFO_WLOCK_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,
343acd3428bSRobert Watson 			    SUSER_ALLOWJAIL))
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,
3496493245dSRobert Watson 			    PRIV_NETINET_REUSEPORT, SUSER_ALLOWJAIL) != 0) {
3504049a042SGuido van Rooij 				t = in_pcblookup_local(inp->inp_pcbinfo,
35175c13541SPoul-Henning Kamp 				    sin->sin_addr, lport,
35275c13541SPoul-Henning Kamp 				    prison ? 0 :  INPLOOKUP_WILDCARD);
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,
37275c13541SPoul-Henning Kamp 			    lport, prison ? 0 : wild);
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) {
4016ac48b74SMike Silbersack 		u_short first, last;
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,
414acd3428bSRobert Watson 			    PRIV_NETINET_RESERVEDPORT, SUSER_ALLOWJAIL);
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 		 * We split the two cases (up and down) so that the direction
45033b3ac06SPeter Wemm 		 * is not being tested on each round of the loop.
45133b3ac06SPeter Wemm 		 */
45233b3ac06SPeter Wemm 		if (first > last) {
45333b3ac06SPeter Wemm 			/*
45433b3ac06SPeter Wemm 			 * counting down
45533b3ac06SPeter Wemm 			 */
4565f311da2SMike Silbersack 			if (dorandom)
4576b2fc10bSMike Silbersack 				*lastport = first -
4586b2fc10bSMike Silbersack 					    (arc4random() % (first - last));
45933b3ac06SPeter Wemm 			count = first - last;
460174624e0SMike Silbersack 
461df8bae1dSRodney W. Grimes 			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;
46715bd2b43SDavid Greenman 				lport = htons(*lastport);
4684b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4694b932371SIan Dowse 			    wild));
47033b3ac06SPeter Wemm 		} else {
47133b3ac06SPeter Wemm 			/*
47233b3ac06SPeter Wemm 			 * counting up
47333b3ac06SPeter Wemm 			 */
4745f311da2SMike Silbersack 			if (dorandom)
4756b2fc10bSMike Silbersack 				*lastport = first +
4766b2fc10bSMike Silbersack 					    (arc4random() % (last - first));
47733b3ac06SPeter Wemm 			count = last - first;
478174624e0SMike Silbersack 
47933b3ac06SPeter Wemm 			do {
4806ac48b74SMike Silbersack 				if (count-- < 0)	/* completely used? */
481550b1518SWes Peters 					return (EADDRNOTAVAIL);
48233b3ac06SPeter Wemm 				++*lastport;
48333b3ac06SPeter Wemm 				if (*lastport < first || *lastport > last)
48433b3ac06SPeter Wemm 					*lastport = first;
48533b3ac06SPeter Wemm 				lport = htons(*lastport);
4864b932371SIan Dowse 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
4874b932371SIan Dowse 			    wild));
48833b3ac06SPeter Wemm 		}
48933b3ac06SPeter Wemm 	}
490b0330ed9SPawel Jakub Dawidek 	if (prison_ip(cred, 0, &laddr.s_addr))
491e4bdf25dSPoul-Henning Kamp 		return (EINVAL);
4924b932371SIan Dowse 	*laddrp = laddr.s_addr;
4934b932371SIan Dowse 	*lportp = lport;
494df8bae1dSRodney W. Grimes 	return (0);
495df8bae1dSRodney W. Grimes }
496df8bae1dSRodney W. Grimes 
497999f1343SGarrett Wollman /*
4985200e00eSIan Dowse  * Connect from a socket to a specified address.
4995200e00eSIan Dowse  * Both address and port must be specified in argument sin.
5005200e00eSIan Dowse  * If don't have a local address for this socket yet,
5015200e00eSIan Dowse  * then pick one.
502999f1343SGarrett Wollman  */
503999f1343SGarrett Wollman int
504136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
505999f1343SGarrett Wollman {
5065200e00eSIan Dowse 	u_short lport, fport;
5075200e00eSIan Dowse 	in_addr_t laddr, faddr;
5085200e00eSIan Dowse 	int anonport, error;
509df8bae1dSRodney W. Grimes 
51027f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
51127f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
51227f74fd0SRobert Watson 
5135200e00eSIan Dowse 	lport = inp->inp_lport;
5145200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
5155200e00eSIan Dowse 	anonport = (lport == 0);
5165200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
517b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
5185200e00eSIan Dowse 	if (error)
5195200e00eSIan Dowse 		return (error);
5205200e00eSIan Dowse 
5215200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
5225200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
5235200e00eSIan Dowse 		inp->inp_lport = lport;
5245200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
5255200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
5265200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
5275200e00eSIan Dowse 			inp->inp_lport = 0;
5285200e00eSIan Dowse 			return (EAGAIN);
5295200e00eSIan Dowse 		}
5305200e00eSIan Dowse 	}
5315200e00eSIan Dowse 
5325200e00eSIan Dowse 	/* Commit the remaining changes. */
5335200e00eSIan Dowse 	inp->inp_lport = lport;
5345200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
5355200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
5365200e00eSIan Dowse 	inp->inp_fport = fport;
5375200e00eSIan Dowse 	in_pcbrehash(inp);
5380f9ade71SHajimu UMEMOTO #ifdef IPSEC
5390f9ade71SHajimu UMEMOTO 	if (inp->inp_socket->so_type == SOCK_STREAM)
5400f9ade71SHajimu UMEMOTO 		ipsec_pcbconn(inp->inp_sp);
5410f9ade71SHajimu UMEMOTO #endif
5425200e00eSIan Dowse 	if (anonport)
5435200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
5445200e00eSIan Dowse 	return (0);
5455200e00eSIan Dowse }
5465200e00eSIan Dowse 
5475200e00eSIan Dowse /*
5485200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
5495200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
5505200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
5515200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
5525200e00eSIan Dowse  * the connect.
5535200e00eSIan Dowse  *
5545200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
5555200e00eSIan Dowse  * and port. These are not updated in the error case.
5565200e00eSIan Dowse  *
5575200e00eSIan Dowse  * If the operation fails because the connection already exists,
5585200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
5595200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
5605200e00eSIan Dowse  * is set to NULL.
5615200e00eSIan Dowse  */
5625200e00eSIan Dowse int
563136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
564136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
565136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
5665200e00eSIan Dowse {
5675200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
5685200e00eSIan Dowse 	struct in_ifaddr *ia;
5695200e00eSIan Dowse 	struct sockaddr_in sa;
570b0330ed9SPawel Jakub Dawidek 	struct ucred *socred;
5715200e00eSIan Dowse 	struct inpcb *oinp;
5725200e00eSIan Dowse 	struct in_addr laddr, faddr;
5735200e00eSIan Dowse 	u_short lport, fport;
5745200e00eSIan Dowse 	int error;
5755200e00eSIan Dowse 
57627f74fd0SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
57727f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
57827f74fd0SRobert Watson 
5795200e00eSIan Dowse 	if (oinpp != NULL)
5805200e00eSIan Dowse 		*oinpp = NULL;
58157bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
582df8bae1dSRodney W. Grimes 		return (EINVAL);
583df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
584df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
585df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
586df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5875200e00eSIan Dowse 	laddr.s_addr = *laddrp;
5885200e00eSIan Dowse 	lport = *lportp;
5895200e00eSIan Dowse 	faddr = sin->sin_addr;
5905200e00eSIan Dowse 	fport = sin->sin_port;
591b0330ed9SPawel Jakub Dawidek 	socred = inp->inp_socket->so_cred;
592b0330ed9SPawel Jakub Dawidek 	if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
5935200e00eSIan Dowse 		bzero(&sa, sizeof(sa));
594b0330ed9SPawel Jakub Dawidek 		sa.sin_addr.s_addr = htonl(prison_getip(socred));
5955200e00eSIan Dowse 		sa.sin_len = sizeof(sa);
5965200e00eSIan Dowse 		sa.sin_family = AF_INET;
5975200e00eSIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
598b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, cred);
5995200e00eSIan Dowse 		if (error)
6005200e00eSIan Dowse 			return (error);
6015200e00eSIan Dowse 	}
60259562606SGarrett Wollman 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
603df8bae1dSRodney W. Grimes 		/*
604df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
605df8bae1dSRodney W. Grimes 		 * use the primary local address.
606df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
607df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
608df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
609df8bae1dSRodney W. Grimes 		 */
6105200e00eSIan Dowse 		if (faddr.s_addr == INADDR_ANY)
6115200e00eSIan Dowse 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
6125200e00eSIan Dowse 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
6135200e00eSIan Dowse 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
6145200e00eSIan Dowse 		    IFF_BROADCAST))
6155200e00eSIan Dowse 			faddr = satosin(&TAILQ_FIRST(
6165200e00eSIan Dowse 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
617df8bae1dSRodney W. Grimes 	}
6185200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
619df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
620df8bae1dSRodney W. Grimes 		/*
62197d8d152SAndre Oppermann 		 * If route is known our src addr is taken from the i/f,
62297d8d152SAndre Oppermann 		 * else punt.
623cf744713SAndre Oppermann 		 *
624cf744713SAndre Oppermann 		 * Find out route to destination
625df8bae1dSRodney W. Grimes 		 */
626cf744713SAndre Oppermann 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
627cf744713SAndre Oppermann 			ia = ip_rtaddr(faddr);
628df8bae1dSRodney W. Grimes 		/*
629cf744713SAndre Oppermann 		 * If we found a route, use the address corresponding to
630cf744713SAndre Oppermann 		 * the outgoing interface.
631cf744713SAndre Oppermann 		 *
632cf744713SAndre Oppermann 		 * Otherwise assume faddr is reachable on a directly connected
633cf744713SAndre Oppermann 		 * network and try to find a corresponding interface to take
634cf744713SAndre Oppermann 		 * the source address from.
635df8bae1dSRodney W. Grimes 		 */
636df8bae1dSRodney W. Grimes 		if (ia == 0) {
6375200e00eSIan Dowse 			bzero(&sa, sizeof(sa));
6385200e00eSIan Dowse 			sa.sin_addr = faddr;
6395200e00eSIan Dowse 			sa.sin_len = sizeof(sa);
6405200e00eSIan Dowse 			sa.sin_family = AF_INET;
641df8bae1dSRodney W. Grimes 
6425200e00eSIan Dowse 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
643df8bae1dSRodney W. Grimes 			if (ia == 0)
6445200e00eSIan Dowse 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
645df8bae1dSRodney W. Grimes 			if (ia == 0)
646ef14c369SMaxim Konovalov 				return (ENETUNREACH);
647df8bae1dSRodney W. Grimes 		}
648df8bae1dSRodney W. Grimes 		/*
649df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
650df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
651df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
652df8bae1dSRodney W. Grimes 		 */
6535200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
654df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
655df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
656df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
659df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
660df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
66137d40066SPoul-Henning Kamp 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
662df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
663df8bae1dSRodney W. Grimes 						break;
664df8bae1dSRodney W. Grimes 				if (ia == 0)
665df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
666df8bae1dSRodney W. Grimes 			}
667df8bae1dSRodney W. Grimes 		}
6685200e00eSIan Dowse 		laddr = ia->ia_addr.sin_addr;
669999f1343SGarrett Wollman 	}
670999f1343SGarrett Wollman 
6715200e00eSIan Dowse 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
6725200e00eSIan Dowse 	    0, NULL);
6735200e00eSIan Dowse 	if (oinp != NULL) {
6745200e00eSIan Dowse 		if (oinpp != NULL)
6755200e00eSIan Dowse 			*oinpp = oinp;
676df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
677c3229e05SDavid Greenman 	}
6785200e00eSIan Dowse 	if (lport == 0) {
679b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
680b0330ed9SPawel Jakub Dawidek 		    cred);
6815a903f8dSPierre Beyssac 		if (error)
6825a903f8dSPierre Beyssac 			return (error);
6835a903f8dSPierre Beyssac 	}
6845200e00eSIan Dowse 	*laddrp = laddr.s_addr;
6855200e00eSIan Dowse 	*lportp = lport;
6865200e00eSIan Dowse 	*faddrp = faddr.s_addr;
6875200e00eSIan Dowse 	*fportp = fport;
688df8bae1dSRodney W. Grimes 	return (0);
689df8bae1dSRodney W. Grimes }
690df8bae1dSRodney W. Grimes 
69126f9a767SRodney W. Grimes void
692136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
693df8bae1dSRodney W. Grimes {
6946b348152SRobert Watson 
695fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
69659daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
697df8bae1dSRodney W. Grimes 
698df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
699df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
70015bd2b43SDavid Greenman 	in_pcbrehash(inp);
7010f9ade71SHajimu UMEMOTO #ifdef IPSEC
7020f9ade71SHajimu UMEMOTO 	ipsec_pcbdisconn(inp->inp_sp);
7030f9ade71SHajimu UMEMOTO #endif
704df8bae1dSRodney W. Grimes }
705df8bae1dSRodney W. Grimes 
7064c7c478dSRobert Watson /*
7074c7c478dSRobert Watson  * In the old world order, in_pcbdetach() served two functions: to detach the
7084c7c478dSRobert Watson  * pcb from the socket/potentially free the socket, and to free the pcb
7094c7c478dSRobert Watson  * itself.  In the new world order, the protocol code is responsible for
7104c7c478dSRobert Watson  * managing the relationship with the socket, and this code simply frees the
7114c7c478dSRobert Watson  * pcb.
7124c7c478dSRobert Watson  */
71326f9a767SRodney W. Grimes void
714136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
715df8bae1dSRodney W. Grimes {
7164c7c478dSRobert Watson 
7174c7c478dSRobert Watson 	KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
7184c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
7194c7c478dSRobert Watson 	inp->inp_socket = NULL;
7204c7c478dSRobert Watson }
7214c7c478dSRobert Watson 
7224c7c478dSRobert Watson void
7234c7c478dSRobert Watson in_pcbfree(struct inpcb *inp)
7244c7c478dSRobert Watson {
7253d4d47f3SGarrett Wollman 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
726df8bae1dSRodney W. Grimes 
7274c7c478dSRobert Watson 	KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
728fe6bfc37SRobert Watson 	INP_INFO_WLOCK_ASSERT(ipi);
72959daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
73059daba27SSam Leffler 
7310f9ade71SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
732cfa1ca9dSYoshinobu Inoue 	ipsec4_delete_pcbpolicy(inp);
733cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
7343d4d47f3SGarrett Wollman 	inp->inp_gencnt = ++ipi->ipi_gencnt;
735c3229e05SDavid Greenman 	in_pcbremlists(inp);
736df8bae1dSRodney W. Grimes 	if (inp->inp_options)
737df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
738df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
739cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
740d915b280SStephan Uphoff 
741a557af22SRobert Watson #ifdef MAC
742a557af22SRobert Watson 	mac_destroy_inpcb(inp);
743a557af22SRobert Watson #endif
744d915b280SStephan Uphoff 	INP_UNLOCK(inp);
74569c2d429SJeff Roberson 	uma_zfree(ipi->ipi_zone, inp);
746df8bae1dSRodney W. Grimes }
747df8bae1dSRodney W. Grimes 
74810702a28SRobert Watson /*
74910702a28SRobert Watson  * TCP needs to maintain its inpcb structure after the TCP connection has
75010702a28SRobert Watson  * been torn down.  However, it must be disconnected from the inpcb hashes as
75110702a28SRobert Watson  * it must not prevent binding of future connections to the same port/ip
75210702a28SRobert Watson  * combination by other inpcbs.
75310702a28SRobert Watson  */
75410702a28SRobert Watson void
75510702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
75610702a28SRobert Watson {
75710702a28SRobert Watson 
7587c5a8ab2SMarcel Moolenaar 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
75910702a28SRobert Watson 	INP_LOCK_ASSERT(inp);
76010702a28SRobert Watson 
76110702a28SRobert Watson 	inp->inp_vflag |= INP_DROPPED;
76210702a28SRobert Watson 	if (inp->inp_lport) {
76310702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
76410702a28SRobert Watson 
76510702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
76610702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
76710702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
76810702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
76910702a28SRobert Watson 			free(phd, M_PCB);
77010702a28SRobert Watson 		}
77110702a28SRobert Watson 		inp->inp_lport = 0;
77210702a28SRobert Watson 	}
77310702a28SRobert Watson }
77410702a28SRobert Watson 
77526ef6ac4SDon Lewis struct sockaddr *
776136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
77726ef6ac4SDon Lewis {
77826ef6ac4SDon Lewis 	struct sockaddr_in *sin;
77926ef6ac4SDon Lewis 
78026ef6ac4SDon Lewis 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
781a163d034SWarner Losh 		M_WAITOK | M_ZERO);
78226ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
78326ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
78426ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
78526ef6ac4SDon Lewis 	sin->sin_port = port;
78626ef6ac4SDon Lewis 
78726ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
78826ef6ac4SDon Lewis }
78926ef6ac4SDon Lewis 
790117bcae7SGarrett Wollman /*
791f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
792f76fcf6dSJeffrey Hsu  * The socket must have a valid
793117bcae7SGarrett Wollman  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
794117bcae7SGarrett Wollman  * except through a kernel programming error, so it is acceptable to panic
79557bf258eSGarrett Wollman  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
79657bf258eSGarrett Wollman  * because there actually /is/ a programming error somewhere... XXX)
797117bcae7SGarrett Wollman  */
798117bcae7SGarrett Wollman int
799136d4f1cSRobert Watson in_setsockaddr(struct socket *so, struct sockaddr **nam,
800136d4f1cSRobert Watson     struct inpcbinfo *pcbinfo)
801df8bae1dSRodney W. Grimes {
802136d4f1cSRobert Watson 	struct inpcb *inp;
80326ef6ac4SDon Lewis 	struct in_addr addr;
80426ef6ac4SDon Lewis 	in_port_t port;
80542fa505bSDavid Greenman 
806fdc984f7STor Egge 	inp = sotoinpcb(so);
8076466b28aSRobert Watson 	KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
8086466b28aSRobert Watson 
809f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
81026ef6ac4SDon Lewis 	port = inp->inp_lport;
81126ef6ac4SDon Lewis 	addr = inp->inp_laddr;
812f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
81342fa505bSDavid Greenman 
81426ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
815117bcae7SGarrett Wollman 	return 0;
816df8bae1dSRodney W. Grimes }
817df8bae1dSRodney W. Grimes 
818f76fcf6dSJeffrey Hsu /*
819f76fcf6dSJeffrey Hsu  * The wrapper function will pass down the pcbinfo for this function to lock.
820f76fcf6dSJeffrey Hsu  */
821117bcae7SGarrett Wollman int
822136d4f1cSRobert Watson in_setpeeraddr(struct socket *so, struct sockaddr **nam,
823136d4f1cSRobert Watson     struct inpcbinfo *pcbinfo)
824df8bae1dSRodney W. Grimes {
825136d4f1cSRobert Watson 	struct inpcb *inp;
82626ef6ac4SDon Lewis 	struct in_addr addr;
82726ef6ac4SDon Lewis 	in_port_t port;
82842fa505bSDavid Greenman 
829fdc984f7STor Egge 	inp = sotoinpcb(so);
8306466b28aSRobert Watson 	KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
8316466b28aSRobert Watson 
832f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
83326ef6ac4SDon Lewis 	port = inp->inp_fport;
83426ef6ac4SDon Lewis 	addr = inp->inp_faddr;
835f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
83642fa505bSDavid Greenman 
83726ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
838117bcae7SGarrett Wollman 	return 0;
839df8bae1dSRodney W. Grimes }
840df8bae1dSRodney W. Grimes 
84126f9a767SRodney W. Grimes void
842136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
843136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
844d1c54148SJesper Skriver {
845c693a045SJonathan Lemon 	struct inpcb *inp, *ninp;
846f76fcf6dSJeffrey Hsu 	struct inpcbhead *head;
847d1c54148SJesper Skriver 
8483dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
849712fc218SRobert Watson 	head = pcbinfo->ipi_listhead;
850c693a045SJonathan Lemon 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
851f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
852c693a045SJonathan Lemon 		ninp = LIST_NEXT(inp, inp_list);
853d1c54148SJesper Skriver #ifdef INET6
854f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
855f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
856d1c54148SJesper Skriver 			continue;
857f76fcf6dSJeffrey Hsu 		}
858d1c54148SJesper Skriver #endif
859d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
860f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
861f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
862d1c54148SJesper Skriver 			continue;
863d1c54148SJesper Skriver 		}
8643dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
865f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
866f76fcf6dSJeffrey Hsu 	}
8673dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
868d1c54148SJesper Skriver }
869d1c54148SJesper Skriver 
870e43cc4aeSHajimu UMEMOTO void
871136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
872e43cc4aeSHajimu UMEMOTO {
873e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
874e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
875e43cc4aeSHajimu UMEMOTO 	int i, gap;
876e43cc4aeSHajimu UMEMOTO 
877f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(pcbinfo);
878712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
879f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
880e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
881e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
882e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
883e43cc4aeSHajimu UMEMOTO 			/*
884e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
885e43cc4aeSHajimu UMEMOTO 			 * detached.
886e43cc4aeSHajimu UMEMOTO 			 */
887e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
888e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
889e43cc4aeSHajimu UMEMOTO 
890e43cc4aeSHajimu UMEMOTO 			/*
891e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
892e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
893e43cc4aeSHajimu UMEMOTO 			 */
894e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
895e43cc4aeSHajimu UMEMOTO 			    i++) {
896e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
897e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
898e43cc4aeSHajimu UMEMOTO 					gap++;
899e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
900e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
901e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
902e43cc4aeSHajimu UMEMOTO 			}
903e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
904e43cc4aeSHajimu UMEMOTO 		}
905f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
906e43cc4aeSHajimu UMEMOTO 	}
9073cfcc388SJeffrey Hsu 	INP_INFO_RUNLOCK(pcbinfo);
908e43cc4aeSHajimu UMEMOTO }
909e43cc4aeSHajimu UMEMOTO 
910df8bae1dSRodney W. Grimes /*
911c3229e05SDavid Greenman  * Lookup a PCB based on the local address and port.
912c3229e05SDavid Greenman  */
913d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
914df8bae1dSRodney W. Grimes struct inpcb *
915136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
916136d4f1cSRobert Watson     u_int lport_arg, int wild_okay)
917df8bae1dSRodney W. Grimes {
918136d4f1cSRobert Watson 	struct inpcb *inp;
919d5e8a67eSHajimu UMEMOTO #ifdef INET6
920d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
921d5e8a67eSHajimu UMEMOTO #else
922d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
923d5e8a67eSHajimu UMEMOTO #endif
924d5e8a67eSHajimu UMEMOTO 	int wildcard;
925c3229e05SDavid Greenman 	u_short lport = lport_arg;
9267bc4aca7SDavid Greenman 
9271b73ca0bSSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
9281b73ca0bSSam Leffler 
929c3229e05SDavid Greenman 	if (!wild_okay) {
930c3229e05SDavid Greenman 		struct inpcbhead *head;
931c3229e05SDavid Greenman 		/*
932c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
933c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
934c3229e05SDavid Greenman 		 */
935712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
936712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
937fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
938cfa1ca9dSYoshinobu Inoue #ifdef INET6
939369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
940cfa1ca9dSYoshinobu Inoue 				continue;
941cfa1ca9dSYoshinobu Inoue #endif
942c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
943c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
944c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
945c3229e05SDavid Greenman 				/*
946c3229e05SDavid Greenman 				 * Found.
947c3229e05SDavid Greenman 				 */
948c3229e05SDavid Greenman 				return (inp);
949df8bae1dSRodney W. Grimes 			}
950c3229e05SDavid Greenman 		}
951c3229e05SDavid Greenman 		/*
952c3229e05SDavid Greenman 		 * Not found.
953c3229e05SDavid Greenman 		 */
954c3229e05SDavid Greenman 		return (NULL);
955c3229e05SDavid Greenman 	} else {
956c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
957c3229e05SDavid Greenman 		struct inpcbport *phd;
958c3229e05SDavid Greenman 		struct inpcb *match = NULL;
959c3229e05SDavid Greenman 		/*
960c3229e05SDavid Greenman 		 * Best fit PCB lookup.
961c3229e05SDavid Greenman 		 *
962c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
963c3229e05SDavid Greenman 		 * port hash list.
964c3229e05SDavid Greenman 		 */
965712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
966712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
967fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
968c3229e05SDavid Greenman 			if (phd->phd_port == lport)
969c3229e05SDavid Greenman 				break;
970c3229e05SDavid Greenman 		}
971c3229e05SDavid Greenman 		if (phd != NULL) {
972c3229e05SDavid Greenman 			/*
973c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
974c3229e05SDavid Greenman 			 * fit.
975c3229e05SDavid Greenman 			 */
97637d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
977c3229e05SDavid Greenman 				wildcard = 0;
978cfa1ca9dSYoshinobu Inoue #ifdef INET6
979369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
980cfa1ca9dSYoshinobu Inoue 					continue;
981d5e8a67eSHajimu UMEMOTO 				/*
982d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
983d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
984d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
985d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
986d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
987d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
988d5e8a67eSHajimu UMEMOTO 				 *
989d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
990d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
991d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
992d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
993d5e8a67eSHajimu UMEMOTO 				 */
994d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
995d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
996cfa1ca9dSYoshinobu Inoue #endif
997c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
998c3229e05SDavid Greenman 					wildcard++;
99915bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
100015bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
100115bd2b43SDavid Greenman 						wildcard++;
100215bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
100315bd2b43SDavid Greenman 						continue;
100415bd2b43SDavid Greenman 				} else {
100515bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
100615bd2b43SDavid Greenman 						wildcard++;
100715bd2b43SDavid Greenman 				}
1008df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1009df8bae1dSRodney W. Grimes 					match = inp;
1010df8bae1dSRodney W. Grimes 					matchwild = wildcard;
10113dbdc25cSDavid Greenman 					if (matchwild == 0) {
1012df8bae1dSRodney W. Grimes 						break;
1013df8bae1dSRodney W. Grimes 					}
1014df8bae1dSRodney W. Grimes 				}
10153dbdc25cSDavid Greenman 			}
1016c3229e05SDavid Greenman 		}
1017df8bae1dSRodney W. Grimes 		return (match);
1018df8bae1dSRodney W. Grimes 	}
1019c3229e05SDavid Greenman }
1020d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
102115bd2b43SDavid Greenman 
102215bd2b43SDavid Greenman /*
102315bd2b43SDavid Greenman  * Lookup PCB in hash list.
102415bd2b43SDavid Greenman  */
102515bd2b43SDavid Greenman struct inpcb *
1026136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1027136d4f1cSRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1028136d4f1cSRobert Watson     struct ifnet *ifp)
102915bd2b43SDavid Greenman {
103015bd2b43SDavid Greenman 	struct inpcbhead *head;
1031136d4f1cSRobert Watson 	struct inpcb *inp;
103215bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
103315bd2b43SDavid Greenman 
103459daba27SSam Leffler 	INP_INFO_RLOCK_ASSERT(pcbinfo);
1035602cc7f1SRobert Watson 
103615bd2b43SDavid Greenman 	/*
103715bd2b43SDavid Greenman 	 * First look for an exact match.
103815bd2b43SDavid Greenman 	 */
1039712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1040712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1041fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1042cfa1ca9dSYoshinobu Inoue #ifdef INET6
1043369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1044cfa1ca9dSYoshinobu Inoue 			continue;
1045cfa1ca9dSYoshinobu Inoue #endif
10466d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1047ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1048ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1049e3fd5ffdSRobert Watson 		    inp->inp_lport == lport)
1050c3229e05SDavid Greenman 			return (inp);
1051c3229e05SDavid Greenman 	}
1052e3fd5ffdSRobert Watson 
1053e3fd5ffdSRobert Watson 	/*
1054e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1055e3fd5ffdSRobert Watson 	 */
10566d6a026bSDavid Greenman 	if (wildcard) {
10576d6a026bSDavid Greenman 		struct inpcb *local_wild = NULL;
1058e3fd5ffdSRobert Watson #ifdef INET6
1059cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1060e3fd5ffdSRobert Watson #endif
10616d6a026bSDavid Greenman 
1062712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1063712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1064fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1065cfa1ca9dSYoshinobu Inoue #ifdef INET6
1066369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1067cfa1ca9dSYoshinobu Inoue 				continue;
1068cfa1ca9dSYoshinobu Inoue #endif
10696d6a026bSDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1070c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1071cfa1ca9dSYoshinobu Inoue 				if (ifp && ifp->if_type == IFT_FAITH &&
1072cfa1ca9dSYoshinobu Inoue 				    (inp->inp_flags & INP_FAITH) == 0)
1073cfa1ca9dSYoshinobu Inoue 					continue;
10746d6a026bSDavid Greenman 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1075c3229e05SDavid Greenman 					return (inp);
1076cfa1ca9dSYoshinobu Inoue 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1077e3fd5ffdSRobert Watson #ifdef INET6
1078cfa1ca9dSYoshinobu Inoue 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1079cfa1ca9dSYoshinobu Inoue 							     AF_INET6))
1080cfa1ca9dSYoshinobu Inoue 						local_wild_mapped = inp;
1081cfa1ca9dSYoshinobu Inoue 					else
1082e3fd5ffdSRobert Watson #endif
10836d6a026bSDavid Greenman 						local_wild = inp;
10846d6a026bSDavid Greenman 				}
10856d6a026bSDavid Greenman 			}
1086cfa1ca9dSYoshinobu Inoue 		}
1087e3fd5ffdSRobert Watson #ifdef INET6
1088cfa1ca9dSYoshinobu Inoue 		if (local_wild == NULL)
1089cfa1ca9dSYoshinobu Inoue 			return (local_wild_mapped);
1090e3fd5ffdSRobert Watson #endif
1091c3229e05SDavid Greenman 		return (local_wild);
10926d6a026bSDavid Greenman 	}
10936d6a026bSDavid Greenman 	return (NULL);
109415bd2b43SDavid Greenman }
109515bd2b43SDavid Greenman 
10967bc4aca7SDavid Greenman /*
1097c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
10987bc4aca7SDavid Greenman  */
1099c3229e05SDavid Greenman int
1100136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp)
110115bd2b43SDavid Greenman {
1102c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
1103c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
1104c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1105c3229e05SDavid Greenman 	struct inpcbport *phd;
1106cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
110715bd2b43SDavid Greenman 
110859daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1109602cc7f1SRobert Watson 	INP_LOCK_ASSERT(inp);
1110602cc7f1SRobert Watson 
1111cfa1ca9dSYoshinobu Inoue #ifdef INET6
1112cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1113cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1114cfa1ca9dSYoshinobu Inoue 	else
1115cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1116cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1117cfa1ca9dSYoshinobu Inoue 
1118712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1119712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
112015bd2b43SDavid Greenman 
1121712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
1122712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1123c3229e05SDavid Greenman 
1124c3229e05SDavid Greenman 	/*
1125c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
1126c3229e05SDavid Greenman 	 */
1127fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1128c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
1129c3229e05SDavid Greenman 			break;
1130c3229e05SDavid Greenman 	}
1131c3229e05SDavid Greenman 	/*
1132c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
1133c3229e05SDavid Greenman 	 */
1134c3229e05SDavid Greenman 	if (phd == NULL) {
1135c3229e05SDavid Greenman 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1136c3229e05SDavid Greenman 		if (phd == NULL) {
1137c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
1138c3229e05SDavid Greenman 		}
1139c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
1140c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
1141c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1142c3229e05SDavid Greenman 	}
1143c3229e05SDavid Greenman 	inp->inp_phd = phd;
1144c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1145c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1146c3229e05SDavid Greenman 	return (0);
114715bd2b43SDavid Greenman }
114815bd2b43SDavid Greenman 
1149c3229e05SDavid Greenman /*
1150c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1151c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
1152c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
1153c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
1154c3229e05SDavid Greenman  */
115515bd2b43SDavid Greenman void
1156136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp)
115715bd2b43SDavid Greenman {
115859daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
115915bd2b43SDavid Greenman 	struct inpcbhead *head;
1160cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
116115bd2b43SDavid Greenman 
116259daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
11634c2bb15aSRobert Watson 	INP_LOCK_ASSERT(inp);
1164602cc7f1SRobert Watson 
1165cfa1ca9dSYoshinobu Inoue #ifdef INET6
1166cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
1167cfa1ca9dSYoshinobu Inoue 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1168cfa1ca9dSYoshinobu Inoue 	else
1169cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
1170cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
1171cfa1ca9dSYoshinobu Inoue 
1172712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1173712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
117415bd2b43SDavid Greenman 
1175c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
117615bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
1177c3229e05SDavid Greenman }
1178c3229e05SDavid Greenman 
1179c3229e05SDavid Greenman /*
1180c3229e05SDavid Greenman  * Remove PCB from various lists.
1181c3229e05SDavid Greenman  */
118276429de4SYoshinobu Inoue void
1183136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
1184c3229e05SDavid Greenman {
118559daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
118659daba27SSam Leffler 
118759daba27SSam Leffler 	INP_INFO_WLOCK_ASSERT(pcbinfo);
118859daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
118959daba27SSam Leffler 
119059daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1191c3229e05SDavid Greenman 	if (inp->inp_lport) {
1192c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
1193c3229e05SDavid Greenman 
1194c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
1195c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
1196fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1197c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
1198c3229e05SDavid Greenman 			free(phd, M_PCB);
1199c3229e05SDavid Greenman 		}
1200c3229e05SDavid Greenman 	}
1201c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
120259daba27SSam Leffler 	pcbinfo->ipi_count--;
120315bd2b43SDavid Greenman }
120475c13541SPoul-Henning Kamp 
1205a557af22SRobert Watson /*
1206a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
1207a557af22SRobert Watson  * label change into the in_pcb for the socket.
1208a557af22SRobert Watson  */
1209a557af22SRobert Watson void
1210136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
1211a557af22SRobert Watson {
1212a557af22SRobert Watson #ifdef MAC
1213a557af22SRobert Watson 	struct inpcb *inp;
1214a557af22SRobert Watson 
12154c7c478dSRobert Watson 	inp = sotoinpcb(so);
12164c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1217602cc7f1SRobert Watson 
1218a557af22SRobert Watson 	INP_LOCK(inp);
1219310e7cebSRobert Watson 	SOCK_LOCK(so);
1220a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
1221310e7cebSRobert Watson 	SOCK_UNLOCK(so);
1222a557af22SRobert Watson 	INP_UNLOCK(inp);
1223a557af22SRobert Watson #endif
1224a557af22SRobert Watson }
12255f311da2SMike Silbersack 
12265f311da2SMike Silbersack /*
1227ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
1228ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
1229ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
1230ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
1231ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
12325f311da2SMike Silbersack  */
12335f311da2SMike Silbersack void
1234136d4f1cSRobert Watson ipport_tick(void *xtp)
12355f311da2SMike Silbersack {
1236ad3a630fSRobert Watson 
1237ad3a630fSRobert Watson 	if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
12385f311da2SMike Silbersack 		if (ipport_stoprandom > 0)
12395f311da2SMike Silbersack 			ipport_stoprandom--;
1240ad3a630fSRobert Watson 	} else
1241ad3a630fSRobert Watson 		ipport_stoprandom = ipport_randomtime;
12425f311da2SMike Silbersack 	ipport_tcplastcount = ipport_tcpallocs;
12435f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
12445f311da2SMike Silbersack }
1245497057eeSRobert Watson 
1246497057eeSRobert Watson #ifdef DDB
1247497057eeSRobert Watson static void
1248497057eeSRobert Watson db_print_indent(int indent)
1249497057eeSRobert Watson {
1250497057eeSRobert Watson 	int i;
1251497057eeSRobert Watson 
1252497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1253497057eeSRobert Watson 		db_printf(" ");
1254497057eeSRobert Watson }
1255497057eeSRobert Watson 
1256497057eeSRobert Watson static void
1257497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1258497057eeSRobert Watson {
1259497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
1260497057eeSRobert Watson 
1261497057eeSRobert Watson 	db_print_indent(indent);
1262497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
1263497057eeSRobert Watson 
1264497057eeSRobert Watson 	indent += 2;
1265497057eeSRobert Watson 
126603dc38a4SRobert Watson #ifdef INET6
1267497057eeSRobert Watson 	if (inc->inc_flags == 1) {
1268497057eeSRobert Watson 		/* IPv6. */
1269497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1270497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1271497057eeSRobert Watson 	} else {
127203dc38a4SRobert Watson #endif
1273497057eeSRobert Watson 		/* IPv4. */
1274497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1275497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
127603dc38a4SRobert Watson #ifdef INET6
1277497057eeSRobert Watson 	}
127803dc38a4SRobert Watson #endif
1279497057eeSRobert Watson 	db_print_indent(indent);
1280497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1281497057eeSRobert Watson 	    ntohs(inc->inc_lport));
1282497057eeSRobert Watson 	db_print_indent(indent);
1283497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1284497057eeSRobert Watson 	    ntohs(inc->inc_fport));
1285497057eeSRobert Watson }
1286497057eeSRobert Watson 
1287497057eeSRobert Watson static void
1288497057eeSRobert Watson db_print_inpflags(int inp_flags)
1289497057eeSRobert Watson {
1290497057eeSRobert Watson 	int comma;
1291497057eeSRobert Watson 
1292497057eeSRobert Watson 	comma = 0;
1293497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
1294497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1295497057eeSRobert Watson 		comma = 1;
1296497057eeSRobert Watson 	}
1297497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
1298497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1299497057eeSRobert Watson 		comma = 1;
1300497057eeSRobert Watson 	}
1301497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
1302497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1303497057eeSRobert Watson 		comma = 1;
1304497057eeSRobert Watson 	}
1305497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
1306497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
1307497057eeSRobert Watson 		comma = 1;
1308497057eeSRobert Watson 	}
1309497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
1310497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1311497057eeSRobert Watson 		comma = 1;
1312497057eeSRobert Watson 	}
1313497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
1314497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
1315497057eeSRobert Watson 		comma = 1;
1316497057eeSRobert Watson 	}
1317497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
1318497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
1319497057eeSRobert Watson 		comma = 1;
1320497057eeSRobert Watson 	}
1321497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
1322497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
1323497057eeSRobert Watson 		comma = 1;
1324497057eeSRobert Watson 	}
1325497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
1326497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
1327497057eeSRobert Watson 		comma = 1;
1328497057eeSRobert Watson 	}
1329497057eeSRobert Watson 	if (inp_flags & INP_FAITH) {
1330497057eeSRobert Watson 		db_printf("%sINP_FAITH", comma ? ", " : "");
1331497057eeSRobert Watson 		comma = 1;
1332497057eeSRobert Watson 	}
1333497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
1334497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
1335497057eeSRobert Watson 		comma = 1;
1336497057eeSRobert Watson 	}
1337497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
1338497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1339497057eeSRobert Watson 		comma = 1;
1340497057eeSRobert Watson 	}
1341497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
1342497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1343497057eeSRobert Watson 		comma = 1;
1344497057eeSRobert Watson 	}
1345497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
1346497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1347497057eeSRobert Watson 		comma = 1;
1348497057eeSRobert Watson 	}
1349497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
1350497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1351497057eeSRobert Watson 		comma = 1;
1352497057eeSRobert Watson 	}
1353497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
1354497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1355497057eeSRobert Watson 		comma = 1;
1356497057eeSRobert Watson 	}
1357497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
1358497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1359497057eeSRobert Watson 		comma = 1;
1360497057eeSRobert Watson 	}
1361497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
1362497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1363497057eeSRobert Watson 		comma = 1;
1364497057eeSRobert Watson 	}
1365497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
1366497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1367497057eeSRobert Watson 		comma = 1;
1368497057eeSRobert Watson 	}
1369497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
1370497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1371497057eeSRobert Watson 		comma = 1;
1372497057eeSRobert Watson 	}
1373497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
1374497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1375497057eeSRobert Watson 		comma = 1;
1376497057eeSRobert Watson 	}
1377497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
1378497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1379497057eeSRobert Watson 		comma = 1;
1380497057eeSRobert Watson 	}
1381497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
1382497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
1383497057eeSRobert Watson 		comma = 1;
1384497057eeSRobert Watson 	}
1385497057eeSRobert Watson }
1386497057eeSRobert Watson 
1387497057eeSRobert Watson static void
1388497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
1389497057eeSRobert Watson {
1390497057eeSRobert Watson 	int comma;
1391497057eeSRobert Watson 
1392497057eeSRobert Watson 	comma = 0;
1393497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
1394497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
1395497057eeSRobert Watson 		comma  = 1;
1396497057eeSRobert Watson 	}
1397497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
1398497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
1399497057eeSRobert Watson 		comma  = 1;
1400497057eeSRobert Watson 	}
1401497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
1402497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1403497057eeSRobert Watson 		comma  = 1;
1404497057eeSRobert Watson 	}
1405497057eeSRobert Watson 	if (inp_vflag & INP_TIMEWAIT) {
1406497057eeSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1407497057eeSRobert Watson 		comma  = 1;
1408497057eeSRobert Watson 	}
1409497057eeSRobert Watson 	if (inp_vflag & INP_ONESBCAST) {
1410497057eeSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1411497057eeSRobert Watson 		comma  = 1;
1412497057eeSRobert Watson 	}
1413497057eeSRobert Watson 	if (inp_vflag & INP_DROPPED) {
1414497057eeSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
1415497057eeSRobert Watson 		comma  = 1;
1416497057eeSRobert Watson 	}
1417497057eeSRobert Watson 	if (inp_vflag & INP_SOCKREF) {
1418497057eeSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
1419497057eeSRobert Watson 		comma  = 1;
1420497057eeSRobert Watson 	}
1421497057eeSRobert Watson }
1422497057eeSRobert Watson 
1423497057eeSRobert Watson void
1424497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1425497057eeSRobert Watson {
1426497057eeSRobert Watson 
1427497057eeSRobert Watson 	db_print_indent(indent);
1428497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
1429497057eeSRobert Watson 
1430497057eeSRobert Watson 	indent += 2;
1431497057eeSRobert Watson 
1432497057eeSRobert Watson 	db_print_indent(indent);
1433497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1434497057eeSRobert Watson 
1435497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1436497057eeSRobert Watson 
1437497057eeSRobert Watson 	db_print_indent(indent);
1438497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1439497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1440497057eeSRobert Watson 
1441497057eeSRobert Watson 	db_print_indent(indent);
1442497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
1443497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
1444497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
1445497057eeSRobert Watson 	db_printf(")\n");
1446497057eeSRobert Watson 
1447497057eeSRobert Watson 	db_print_indent(indent);
1448497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1449497057eeSRobert Watson 	    inp->inp_vflag);
1450497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
1451497057eeSRobert Watson 	db_printf(")\n");
1452497057eeSRobert Watson 
1453497057eeSRobert Watson 	db_print_indent(indent);
1454497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1455497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1456497057eeSRobert Watson 
1457497057eeSRobert Watson 	db_print_indent(indent);
1458497057eeSRobert Watson #ifdef INET6
1459497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
1460497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
1461497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
1462497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
1463497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1464497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1465497057eeSRobert Watson 		    inp->in6p_hops);
1466497057eeSRobert Watson 	} else
1467497057eeSRobert Watson #endif
1468497057eeSRobert Watson 	{
1469497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1470497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1471497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
1472497057eeSRobert Watson 	}
1473497057eeSRobert Watson 
1474497057eeSRobert Watson 	db_print_indent(indent);
1475497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1476497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
1477497057eeSRobert Watson }
1478497057eeSRobert Watson 
1479497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1480497057eeSRobert Watson {
1481497057eeSRobert Watson 	struct inpcb *inp;
1482497057eeSRobert Watson 
1483497057eeSRobert Watson 	if (!have_addr) {
1484497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
1485497057eeSRobert Watson 		return;
1486497057eeSRobert Watson 	}
1487497057eeSRobert Watson 	inp = (struct inpcb *)addr;
1488497057eeSRobert Watson 
1489497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
1490497057eeSRobert Watson }
1491497057eeSRobert Watson #endif
1492