xref: /freebsd/sys/netinet/in_pcb.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
1c398230bSWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
42469dd60SGarrett Wollman  * Copyright (c) 1982, 1986, 1991, 1993, 1995
5497057eeSRobert Watson  *	The Regents of the University of California.
6111d57a6SRobert Watson  * Copyright (c) 2007-2009 Robert N. M. Watson
779bdc6e5SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8497057eeSRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
1079bdc6e5SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
1179bdc6e5SRobert Watson  * contract to Juniper Networks, Inc.
1279bdc6e5SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
372469dd60SGarrett Wollman  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
404b421e2dSMike Silbersack #include <sys/cdefs.h>
414b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
424b421e2dSMike Silbersack 
43497057eeSRobert Watson #include "opt_ddb.h"
446a800098SYoshinobu Inoue #include "opt_ipsec.h"
45efc76f72SBjoern A. Zeeb #include "opt_inet.h"
46cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
47f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
4852cd27cbSRobert Watson #include "opt_pcbgroup.h"
497527624eSRobert Watson #include "opt_rss.h"
50cfa1ca9dSYoshinobu Inoue 
51df8bae1dSRodney W. Grimes #include <sys/param.h>
52df8bae1dSRodney W. Grimes #include <sys/systm.h>
53cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
54df8bae1dSRodney W. Grimes #include <sys/malloc.h>
55df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
56aae49dd3SBjoern A. Zeeb #include <sys/callout.h>
57ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h>
58cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
59df8bae1dSRodney W. Grimes #include <sys/protosw.h>
60cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
61df8bae1dSRodney W. Grimes #include <sys/socket.h>
62df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
63f3e7afe2SHans Petter Selasky #include <sys/sockio.h>
64acd3428bSRobert Watson #include <sys/priv.h>
65df8bae1dSRodney W. Grimes #include <sys/proc.h>
6679bdc6e5SRobert Watson #include <sys/refcount.h>
6775c13541SPoul-Henning Kamp #include <sys/jail.h>
68101f9fc8SPeter Wemm #include <sys/kernel.h>
69101f9fc8SPeter Wemm #include <sys/sysctl.h>
708781d8e9SBruce Evans 
71497057eeSRobert Watson #ifdef DDB
72497057eeSRobert Watson #include <ddb/ddb.h>
73497057eeSRobert Watson #endif
74497057eeSRobert Watson 
7569c2d429SJeff Roberson #include <vm/uma.h>
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes #include <net/if.h>
7876039bc8SGleb Smirnoff #include <net/if_var.h>
79cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
806d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h>
81df8bae1dSRodney W. Grimes #include <net/route.h>
82b2bdc62aSAdrian Chadd #include <net/rss_config.h>
83530c0060SRobert Watson #include <net/vnet.h>
84df8bae1dSRodney W. Grimes 
8567107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
86df8bae1dSRodney W. Grimes #include <netinet/in.h>
87df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
88df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
89340c35deSJonathan Lemon #include <netinet/tcp_var.h>
905f311da2SMike Silbersack #include <netinet/udp.h>
915f311da2SMike Silbersack #include <netinet/udp_var.h>
9267107f45SBjoern A. Zeeb #endif
9367107f45SBjoern A. Zeeb #ifdef INET
9467107f45SBjoern A. Zeeb #include <netinet/in_var.h>
9567107f45SBjoern A. Zeeb #endif
96cfa1ca9dSYoshinobu Inoue #ifdef INET6
97cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
98efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
9967107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
10067107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
101cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
102cfa1ca9dSYoshinobu Inoue 
103fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
104b9234fafSSam Leffler 
105aed55708SRobert Watson #include <security/mac/mac_framework.h>
106aed55708SRobert Watson 
107aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
108aae49dd3SBjoern A. Zeeb 
109101f9fc8SPeter Wemm /*
110101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
111101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
112101f9fc8SPeter Wemm  */
113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
115eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
116eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
117eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
118eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
119101f9fc8SPeter Wemm 
120b0d22693SCrist J. Clark /*
121b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
122b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
123b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
124b0d22693SCrist J. Clark  */
125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
127b0d22693SCrist J. Clark 
1285f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
130eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
131eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
132eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
133eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1343e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount);
135eddfbb76SRobert Watson 
1361e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1376ac48b74SMike Silbersack 
138d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
139d2025bd0SBjoern A. Zeeb #ifdef INET
140fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
141fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
142fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
143fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
14467107f45SBjoern A. Zeeb 
145bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
146bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
147bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
148bbd42ad0SPeter Wemm 
149bbd42ad0SPeter Wemm static int
15082d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
151bbd42ad0SPeter Wemm {
15230a4ab08SBruce Evans 	int error;
15330a4ab08SBruce Evans 
154f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
15530a4ab08SBruce Evans 	if (error == 0) {
156603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
157603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
158603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
159603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
160603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
161603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
162bbd42ad0SPeter Wemm 	}
16330a4ab08SBruce Evans 	return (error);
164bbd42ad0SPeter Wemm }
165bbd42ad0SPeter Wemm 
166bbd42ad0SPeter Wemm #undef RANGECHK
167bbd42ad0SPeter Wemm 
1686472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
1696472ac3dSEd Schouten     "IP Ports");
17033b3ac06SPeter Wemm 
1716df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1726df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1736df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
1746df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1756df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1766df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
1776df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1786df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1796df8a710SGleb Smirnoff 	&VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
1806df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1816df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1826df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
1836df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
1846df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1856df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
1866df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
1876df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1886df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
1896df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
1906df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
1916df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
193eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
1946df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
1956df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
196eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
1986df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
199eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
2006ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
2026df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
203eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
2048b615593SMarko Zec 	"Minimum time to keep sequental port "
2056ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
20683e521ecSBjoern A. Zeeb #endif /* INET */
2070312fbe9SPoul-Henning Kamp 
208c3229e05SDavid Greenman /*
209c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
210c3229e05SDavid Greenman  *
211de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
212de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
213de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
214c3229e05SDavid Greenman  */
215c3229e05SDavid Greenman 
216c3229e05SDavid Greenman /*
217cc487c16SGleb Smirnoff  * Different protocols initialize their inpcbs differently - giving
218cc487c16SGleb Smirnoff  * different name to the lock.  But they all are disposed the same.
219cc487c16SGleb Smirnoff  */
220cc487c16SGleb Smirnoff static void
221cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size)
222cc487c16SGleb Smirnoff {
223cc487c16SGleb Smirnoff 	struct inpcb *inp = mem;
224cc487c16SGleb Smirnoff 
225cc487c16SGleb Smirnoff 	INP_LOCK_DESTROY(inp);
226cc487c16SGleb Smirnoff }
227cc487c16SGleb Smirnoff 
228cc487c16SGleb Smirnoff /*
2299bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
2309bcd427bSRobert Watson  * arguments in time.
2319bcd427bSRobert Watson  */
2329bcd427bSRobert Watson void
2339bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
2349bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
235cc487c16SGleb Smirnoff     char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields)
2369bcd427bSRobert Watson {
2379bcd427bSRobert Watson 
2389bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
239fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
240ff9b006dSJulien Charbon 	INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
2419bcd427bSRobert Watson #ifdef VIMAGE
2429bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
2439bcd427bSRobert Watson #endif
2449bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
2459bcd427bSRobert Watson 	LIST_INIT(pcbinfo->ipi_listhead);
246fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
2479bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
2489bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
2499bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
2509bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
25152cd27cbSRobert Watson #ifdef PCBGROUP
25252cd27cbSRobert Watson 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
25352cd27cbSRobert Watson #endif
2549bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
255cc487c16SGleb Smirnoff 	    NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0);
2569bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
2576acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
2586acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
2599bcd427bSRobert Watson }
2609bcd427bSRobert Watson 
2619bcd427bSRobert Watson /*
2629bcd427bSRobert Watson  * Destroy an inpcbinfo.
2639bcd427bSRobert Watson  */
2649bcd427bSRobert Watson void
2659bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
2669bcd427bSRobert Watson {
2679bcd427bSRobert Watson 
268fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
269fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
270fa046d87SRobert Watson 
2719bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
2729bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
2739bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
27452cd27cbSRobert Watson #ifdef PCBGROUP
27552cd27cbSRobert Watson 	in_pcbgroup_destroy(pcbinfo);
27652cd27cbSRobert Watson #endif
2779bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
278ff9b006dSJulien Charbon 	INP_LIST_LOCK_DESTROY(pcbinfo);
279fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
2809bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
2819bcd427bSRobert Watson }
2829bcd427bSRobert Watson 
2839bcd427bSRobert Watson /*
284c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
285d915b280SStephan Uphoff  * On success return with the PCB locked.
286c3229e05SDavid Greenman  */
287df8bae1dSRodney W. Grimes int
288d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
289df8bae1dSRodney W. Grimes {
290136d4f1cSRobert Watson 	struct inpcb *inp;
29113cf67f3SHajimu UMEMOTO 	int error;
292a557af22SRobert Watson 
293ff9b006dSJulien Charbon #ifdef INVARIANTS
294ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
295ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
296ff9b006dSJulien Charbon 	} else {
29759daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
298ff9b006dSJulien Charbon 	}
299ff9b006dSJulien Charbon #endif
300ff9b006dSJulien Charbon 
301a557af22SRobert Watson 	error = 0;
302d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
303df8bae1dSRodney W. Grimes 	if (inp == NULL)
304df8bae1dSRodney W. Grimes 		return (ENOBUFS);
3056a6cefacSGleb Smirnoff 	bzero(&inp->inp_start_zero, inp_zero_size);
30615bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
307df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
30886d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
3098b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
310a557af22SRobert Watson #ifdef MAC
31130d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
312a557af22SRobert Watson 	if (error != 0)
313a557af22SRobert Watson 		goto out;
31430d239bcSRobert Watson 	mac_inpcb_create(so, inp);
315a557af22SRobert Watson #endif
316fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
317fcf59617SAndrey V. Elsukov 	error = ipsec_init_pcbpolicy(inp);
3180bffde27SRobert Watson 	if (error != 0) {
3190bffde27SRobert Watson #ifdef MAC
3200bffde27SRobert Watson 		mac_inpcb_destroy(inp);
3210bffde27SRobert Watson #endif
322a557af22SRobert Watson 		goto out;
3230bffde27SRobert Watson 	}
324b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
325e3fd5ffdSRobert Watson #ifdef INET6
326340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
327340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
328603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
32933841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
330340c35deSJonathan Lemon 	}
33175daea93SPaul Saab #endif
332ff9b006dSJulien Charbon 	INP_WLOCK(inp);
333ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
334712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
3353d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
336df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
33733841545SHajimu UMEMOTO #ifdef INET6
338603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
33933841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
34033841545SHajimu UMEMOTO #endif
341d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
34279bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
3438c1960d5SMike Karels 
3448c1960d5SMike Karels 	/*
3458c1960d5SMike Karels 	 * Routes in inpcb's can cache L2 as well; they are guaranteed
3468c1960d5SMike Karels 	 * to be cleaned up.
3478c1960d5SMike Karels 	 */
3488c1960d5SMike Karels 	inp->inp_route.ro_flags = RT_LLE_CACHE;
349ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
3507a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC)
351a557af22SRobert Watson out:
35286d02c5cSBjoern A. Zeeb 	if (error != 0) {
35386d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
354a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
35586d02c5cSBjoern A. Zeeb 	}
356a557af22SRobert Watson #endif
357a557af22SRobert Watson 	return (error);
358df8bae1dSRodney W. Grimes }
359df8bae1dSRodney W. Grimes 
36067107f45SBjoern A. Zeeb #ifdef INET
361df8bae1dSRodney W. Grimes int
362136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
363df8bae1dSRodney W. Grimes {
3644b932371SIan Dowse 	int anonport, error;
3654b932371SIan Dowse 
3668501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
367fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
36859daba27SSam Leffler 
3694b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
3704b932371SIan Dowse 		return (EINVAL);
3715cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
3724b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
373b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
3744b932371SIan Dowse 	if (error)
3754b932371SIan Dowse 		return (error);
3764b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
3774b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
3784b932371SIan Dowse 		inp->inp_lport = 0;
3794b932371SIan Dowse 		return (EAGAIN);
3804b932371SIan Dowse 	}
3814b932371SIan Dowse 	if (anonport)
3824b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
3834b932371SIan Dowse 	return (0);
3844b932371SIan Dowse }
38567107f45SBjoern A. Zeeb #endif
3864b932371SIan Dowse 
38739c8c62eSHiren Panchasara /*
38839c8c62eSHiren Panchasara  * Select a local port (number) to use.
38939c8c62eSHiren Panchasara  */
390efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
391efc76f72SBjoern A. Zeeb int
3924616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
3934616026fSErmal Luçi     struct ucred *cred, int lookupflags)
394efc76f72SBjoern A. Zeeb {
395efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
396efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
397efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
398efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
399efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
400efc76f72SBjoern A. Zeeb #ifdef INET
401efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
402efc76f72SBjoern A. Zeeb #endif
403efc76f72SBjoern A. Zeeb 
404efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
405efc76f72SBjoern A. Zeeb 
406efc76f72SBjoern A. Zeeb 	/*
407efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
408efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
409efc76f72SBjoern A. Zeeb 	 */
410efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
411fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
412efc76f72SBjoern A. Zeeb 
413efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
414efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
415efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
416efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
417efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
418efc76f72SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
419efc76f72SBjoern A. Zeeb 		if (error)
420efc76f72SBjoern A. Zeeb 			return (error);
421efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
422efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
423efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
424efc76f72SBjoern A. Zeeb 	} else {
425efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
426efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
427efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
428efc76f72SBjoern A. Zeeb 	}
429efc76f72SBjoern A. Zeeb 	/*
430e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
431efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
432efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
433efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
434efc76f72SBjoern A. Zeeb 	 */
435efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
436e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
437e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
438efc76f72SBjoern A. Zeeb 		dorandom = 1;
439efc76f72SBjoern A. Zeeb 	else
440efc76f72SBjoern A. Zeeb 		dorandom = 0;
441efc76f72SBjoern A. Zeeb 	/*
442efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
443efc76f72SBjoern A. Zeeb 	 * we have the only port available.
444efc76f72SBjoern A. Zeeb 	 */
445efc76f72SBjoern A. Zeeb 	if (first == last)
446efc76f72SBjoern A. Zeeb 		dorandom = 0;
447e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
448e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
449efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
450efc76f72SBjoern A. Zeeb 	/*
451efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
452efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
453efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
454efc76f72SBjoern A. Zeeb 	 */
455efc76f72SBjoern A. Zeeb 	if (first > last) {
456efc76f72SBjoern A. Zeeb 		aux = first;
457efc76f72SBjoern A. Zeeb 		first = last;
458efc76f72SBjoern A. Zeeb 		last = aux;
459efc76f72SBjoern A. Zeeb 	}
460efc76f72SBjoern A. Zeeb 
461efc76f72SBjoern A. Zeeb #ifdef INET
462efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
463efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
4644d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
465efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
466efc76f72SBjoern A. Zeeb 		    __func__, inp));
467efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
468efc76f72SBjoern A. Zeeb 	}
469efc76f72SBjoern A. Zeeb #endif
47067107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
471efc76f72SBjoern A. Zeeb 	lport = *lportp;
472efc76f72SBjoern A. Zeeb 
473efc76f72SBjoern A. Zeeb 	if (dorandom)
474efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
475efc76f72SBjoern A. Zeeb 
476efc76f72SBjoern A. Zeeb 	count = last - first;
477efc76f72SBjoern A. Zeeb 
478efc76f72SBjoern A. Zeeb 	do {
479efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
480efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
481efc76f72SBjoern A. Zeeb 		++*lastport;
482efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
483efc76f72SBjoern A. Zeeb 			*lastport = first;
484efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
485efc76f72SBjoern A. Zeeb 
486efc76f72SBjoern A. Zeeb #ifdef INET6
4874616026fSErmal Luçi 		if ((inp->inp_vflag & INP_IPV6) != 0)
488efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
48968e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
490efc76f72SBjoern A. Zeeb #endif
491efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
492efc76f72SBjoern A. Zeeb 		else
493efc76f72SBjoern A. Zeeb #endif
494efc76f72SBjoern A. Zeeb #ifdef INET
495efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
49668e0d7e0SRobert Watson 			    lport, lookupflags, cred);
497efc76f72SBjoern A. Zeeb #endif
498efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
499efc76f72SBjoern A. Zeeb 
500efc76f72SBjoern A. Zeeb #ifdef INET
5014d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
502efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
503efc76f72SBjoern A. Zeeb #endif
504efc76f72SBjoern A. Zeeb 	*lportp = lport;
505efc76f72SBjoern A. Zeeb 
506efc76f72SBjoern A. Zeeb 	return (0);
507efc76f72SBjoern A. Zeeb }
508efdf104bSMikolaj Golub 
509efdf104bSMikolaj Golub /*
510efdf104bSMikolaj Golub  * Return cached socket options.
511efdf104bSMikolaj Golub  */
512efdf104bSMikolaj Golub short
513efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
514efdf104bSMikolaj Golub {
515efdf104bSMikolaj Golub    short so_options;
516efdf104bSMikolaj Golub 
517efdf104bSMikolaj Golub    so_options = 0;
518efdf104bSMikolaj Golub 
519efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
520efdf104bSMikolaj Golub 	   so_options |= SO_REUSEPORT;
521efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
522efdf104bSMikolaj Golub 	   so_options |= SO_REUSEADDR;
523efdf104bSMikolaj Golub    return (so_options);
524efdf104bSMikolaj Golub }
525efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
526efc76f72SBjoern A. Zeeb 
5274b932371SIan Dowse /*
5280a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
5290a100a6fSAdrian Chadd  *
5300a100a6fSAdrian Chadd  * ni points to the new inp.
5310a100a6fSAdrian Chadd  * oi points to the exisitng inp.
5320a100a6fSAdrian Chadd  *
5330a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
5340a100a6fSAdrian Chadd  * whether the credentials match.
5350a100a6fSAdrian Chadd  */
536d5bb8bd3SAdrian Chadd int
5370a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
5380a100a6fSAdrian Chadd {
5390a100a6fSAdrian Chadd 	/* Check permissions match */
5400a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5410a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
5420a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
5430a100a6fSAdrian Chadd 		return (0);
5440a100a6fSAdrian Chadd 
5450a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
5460a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5470a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
5480a100a6fSAdrian Chadd 		return (0);
5490a100a6fSAdrian Chadd 
5500a100a6fSAdrian Chadd 	/*
5510a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
5520a100a6fSAdrian Chadd 	 * it is and it matches the checks.
5530a100a6fSAdrian Chadd 	 */
5540a100a6fSAdrian Chadd 	return (1);
5550a100a6fSAdrian Chadd }
5560a100a6fSAdrian Chadd 
557d5bb8bd3SAdrian Chadd #ifdef INET
5580a100a6fSAdrian Chadd /*
5594b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
5604b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
5614b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
5624b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
5634b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
5644b932371SIan Dowse  *
5654b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
5664b932371SIan Dowse  */
5674b932371SIan Dowse int
568136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
569136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
5704b932371SIan Dowse {
5714b932371SIan Dowse 	struct socket *so = inp->inp_socket;
57215bd2b43SDavid Greenman 	struct sockaddr_in *sin;
573c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
5744b932371SIan Dowse 	struct in_addr laddr;
575df8bae1dSRodney W. Grimes 	u_short lport = 0;
57668e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
577413628a7SBjoern A. Zeeb 	int error;
578df8bae1dSRodney W. Grimes 
5798501a69cSRobert Watson 	/*
580fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
5818501a69cSRobert Watson 	 */
58259daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
583fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
58459daba27SSam Leffler 
585603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
586df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5874b932371SIan Dowse 	laddr.s_addr = *laddrp;
5884b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
589df8bae1dSRodney W. Grimes 		return (EINVAL);
590c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
59168e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
5924616026fSErmal Luçi 	if (nam == NULL) {
5937c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
5947c2f3cb9SJamie Gritton 			return (error);
5957c2f3cb9SJamie Gritton 	} else {
59657bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
59757bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
598df8bae1dSRodney W. Grimes 			return (EINVAL);
599df8bae1dSRodney W. Grimes #ifdef notdef
600df8bae1dSRodney W. Grimes 		/*
601df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
602df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
603df8bae1dSRodney W. Grimes 		 */
604df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
605df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
606df8bae1dSRodney W. Grimes #endif
607b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
608b89e82ddSJamie Gritton 		if (error)
609b89e82ddSJamie Gritton 			return (error);
6104b932371SIan Dowse 		if (sin->sin_port != *lportp) {
6114b932371SIan Dowse 			/* Don't allow the port to change. */
6124b932371SIan Dowse 			if (*lportp != 0)
6134b932371SIan Dowse 				return (EINVAL);
614df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
6154b932371SIan Dowse 		}
6164b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
617df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
618df8bae1dSRodney W. Grimes 			/*
619df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
620df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
621df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
622df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
623df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
624df8bae1dSRodney W. Grimes 			 */
625f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
626df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
627df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
628df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
62983103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
6304209e01aSAdrian Chadd 			/*
6314209e01aSAdrian Chadd 			 * Is the address a local IP address?
632f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
6338696873dSAdrian Chadd 			 * to any endpoint address, local or not.
6344209e01aSAdrian Chadd 			 */
635f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
6368896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
637df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
638df8bae1dSRodney W. Grimes 		}
6394b932371SIan Dowse 		laddr = sin->sin_addr;
640df8bae1dSRodney W. Grimes 		if (lport) {
641df8bae1dSRodney W. Grimes 			struct inpcb *t;
642ae0e7143SRobert Watson 			struct tcptw *tw;
643ae0e7143SRobert Watson 
644df8bae1dSRodney W. Grimes 			/* GROSS */
645603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
646603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
647acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
64832f9753cSRobert Watson 			    0))
6492469dd60SGarrett Wollman 				return (EACCES);
650835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
65186d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
65232f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
653078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
654413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
655340c35deSJonathan Lemon 	/*
656340c35deSJonathan Lemon 	 * XXX
657340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
658340c35deSJonathan Lemon 	 */
6594cc20ab1SSeigo Tanimura 				if (t &&
6600a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
661ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
6624658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
6634658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
6644cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
66552b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
666fc06cd42SMikolaj Golub 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
66786d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
66886d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
6694049a042SGuido van Rooij 					return (EADDRINUSE);
6700a100a6fSAdrian Chadd 
6710a100a6fSAdrian Chadd 				/*
6720a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
6730a100a6fSAdrian Chadd 				 * the credentials need to match and the
6740a100a6fSAdrian Chadd 				 * original socket also has to have been bound
6750a100a6fSAdrian Chadd 				 * with BINDMULTI.
6760a100a6fSAdrian Chadd 				 */
6770a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6780a100a6fSAdrian Chadd 					return (EADDRINUSE);
6794049a042SGuido van Rooij 			}
680c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
68168e0d7e0SRobert Watson 			    lport, lookupflags, cred);
682ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
683ae0e7143SRobert Watson 				/*
684ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
685ae0e7143SRobert Watson 				 * state recycled, we treat the address as
686ae0e7143SRobert Watson 				 * being in use (for now).  This is better
687ae0e7143SRobert Watson 				 * than a panic, but not desirable.
688ae0e7143SRobert Watson 				 */
689ec95b709SMikolaj Golub 				tw = intotw(t);
690ae0e7143SRobert Watson 				if (tw == NULL ||
691ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
692340c35deSJonathan Lemon 					return (EADDRINUSE);
6930a100a6fSAdrian Chadd 			} else if (t &&
6940a100a6fSAdrian Chadd 			    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
6950a100a6fSAdrian Chadd 			    (reuseport & inp_so_options(t)) == 0) {
696e3fd5ffdSRobert Watson #ifdef INET6
69733841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
698cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
699cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
700cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
701fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
702fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
703e3fd5ffdSRobert Watson #endif
704df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
7050a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
7060a100a6fSAdrian Chadd 					return (EADDRINUSE);
707df8bae1dSRodney W. Grimes 			}
708cfa1ca9dSYoshinobu Inoue 		}
709df8bae1dSRodney W. Grimes 	}
7104b932371SIan Dowse 	if (*lportp != 0)
7114b932371SIan Dowse 		lport = *lportp;
71233b3ac06SPeter Wemm 	if (lport == 0) {
7134616026fSErmal Luçi 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
714efc76f72SBjoern A. Zeeb 		if (error != 0)
715efc76f72SBjoern A. Zeeb 			return (error);
71633b3ac06SPeter Wemm 
71733b3ac06SPeter Wemm 	}
7184b932371SIan Dowse 	*laddrp = laddr.s_addr;
7194b932371SIan Dowse 	*lportp = lport;
720df8bae1dSRodney W. Grimes 	return (0);
721df8bae1dSRodney W. Grimes }
722df8bae1dSRodney W. Grimes 
723999f1343SGarrett Wollman /*
7245200e00eSIan Dowse  * Connect from a socket to a specified address.
7255200e00eSIan Dowse  * Both address and port must be specified in argument sin.
7265200e00eSIan Dowse  * If don't have a local address for this socket yet,
7275200e00eSIan Dowse  * then pick one.
728999f1343SGarrett Wollman  */
729999f1343SGarrett Wollman int
730d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
731d3c1f003SRobert Watson     struct ucred *cred, struct mbuf *m)
732999f1343SGarrett Wollman {
7335200e00eSIan Dowse 	u_short lport, fport;
7345200e00eSIan Dowse 	in_addr_t laddr, faddr;
7355200e00eSIan Dowse 	int anonport, error;
736df8bae1dSRodney W. Grimes 
7378501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
738fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
73927f74fd0SRobert Watson 
7405200e00eSIan Dowse 	lport = inp->inp_lport;
7415200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
7425200e00eSIan Dowse 	anonport = (lport == 0);
7435200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
744b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
7455200e00eSIan Dowse 	if (error)
7465200e00eSIan Dowse 		return (error);
7475200e00eSIan Dowse 
7485200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
7495200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
7505200e00eSIan Dowse 		inp->inp_lport = lport;
7515200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
7525200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
7535200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
7545200e00eSIan Dowse 			inp->inp_lport = 0;
7555200e00eSIan Dowse 			return (EAGAIN);
7565200e00eSIan Dowse 		}
7575200e00eSIan Dowse 	}
7585200e00eSIan Dowse 
7595200e00eSIan Dowse 	/* Commit the remaining changes. */
7605200e00eSIan Dowse 	inp->inp_lport = lport;
7615200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
7625200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
7635200e00eSIan Dowse 	inp->inp_fport = fport;
764d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, m);
7652cb64cb2SGeorge V. Neville-Neil 
7665200e00eSIan Dowse 	if (anonport)
7675200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
7685200e00eSIan Dowse 	return (0);
7695200e00eSIan Dowse }
7705200e00eSIan Dowse 
771d3c1f003SRobert Watson int
772d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
773d3c1f003SRobert Watson {
774d3c1f003SRobert Watson 
775d3c1f003SRobert Watson 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
776d3c1f003SRobert Watson }
777d3c1f003SRobert Watson 
7785200e00eSIan Dowse /*
7790895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
7800895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
7810895aec3SBjoern A. Zeeb  */
782ae190832SSteven Hartland int
7830895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
7840895aec3SBjoern A. Zeeb     struct ucred *cred)
7850895aec3SBjoern A. Zeeb {
7860895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
7870895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
7880895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
7890895aec3SBjoern A. Zeeb 	struct route sro;
7900895aec3SBjoern A. Zeeb 	int error;
7910895aec3SBjoern A. Zeeb 
792413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
7930895aec3SBjoern A. Zeeb 
794592bcae8SBjoern A. Zeeb 	/*
795592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
796592bcae8SBjoern A. Zeeb 	 * if requested.
797592bcae8SBjoern A. Zeeb 	 */
798592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
799592bcae8SBjoern A. Zeeb 		return (0);
800592bcae8SBjoern A. Zeeb 
8010895aec3SBjoern A. Zeeb 	error = 0;
8020895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
8030895aec3SBjoern A. Zeeb 
8040895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
8050895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
8060895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
8070895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
8080895aec3SBjoern A. Zeeb 
8090895aec3SBjoern A. Zeeb 	/*
8100895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
8110895aec3SBjoern A. Zeeb 	 * else punt.
8120895aec3SBjoern A. Zeeb 	 *
8130895aec3SBjoern A. Zeeb 	 * Find out route to destination.
8140895aec3SBjoern A. Zeeb 	 */
8150895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
8166e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
8170895aec3SBjoern A. Zeeb 
8180895aec3SBjoern A. Zeeb 	/*
8190895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
8200895aec3SBjoern A. Zeeb 	 * the outgoing interface.
8210895aec3SBjoern A. Zeeb 	 *
8220895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
8230895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
8240895aec3SBjoern A. Zeeb 	 * the source address from.
8250895aec3SBjoern A. Zeeb 	 */
8260895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
8278c0fec80SRobert Watson 		struct in_ifaddr *ia;
8280895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
8290895aec3SBjoern A. Zeeb 
8304f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
83158a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
8320895aec3SBjoern A. Zeeb 		if (ia == NULL)
8334f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
83458a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
8350895aec3SBjoern A. Zeeb 		if (ia == NULL) {
8360895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
8370895aec3SBjoern A. Zeeb 			goto done;
8380895aec3SBjoern A. Zeeb 		}
8390895aec3SBjoern A. Zeeb 
8400304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8410895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8428c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8430895aec3SBjoern A. Zeeb 			goto done;
8440895aec3SBjoern A. Zeeb 		}
8450895aec3SBjoern A. Zeeb 
8460895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
8478c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
8480895aec3SBjoern A. Zeeb 		ia = NULL;
849137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8500895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8510895aec3SBjoern A. Zeeb 
8520895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8530895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8540895aec3SBjoern A. Zeeb 				continue;
8550895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
856b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8570895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8580895aec3SBjoern A. Zeeb 				break;
8590895aec3SBjoern A. Zeeb 			}
8600895aec3SBjoern A. Zeeb 		}
8610895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8620895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
863137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8640895aec3SBjoern A. Zeeb 			goto done;
8650895aec3SBjoern A. Zeeb 		}
866137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8670895aec3SBjoern A. Zeeb 
8680895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
869b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8700895aec3SBjoern A. Zeeb 		goto done;
8710895aec3SBjoern A. Zeeb 	}
8720895aec3SBjoern A. Zeeb 
8730895aec3SBjoern A. Zeeb 	/*
8740895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
8750895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
8760895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
8770895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
8780895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
8790895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
8800895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
8810895aec3SBjoern A. Zeeb 	 */
8820895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
8838c0fec80SRobert Watson 		struct in_ifaddr *ia;
8849317b04eSRobert Watson 		struct ifnet *ifp;
8850895aec3SBjoern A. Zeeb 
8860895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
8870304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8880895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8890895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8900895aec3SBjoern A. Zeeb 			goto done;
8910895aec3SBjoern A. Zeeb 		}
8920895aec3SBjoern A. Zeeb 
8930895aec3SBjoern A. Zeeb 		/* Jailed. */
8940895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
8950895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
896b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8970895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8980895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8990895aec3SBjoern A. Zeeb 			goto done;
9000895aec3SBjoern A. Zeeb 		}
9010895aec3SBjoern A. Zeeb 
9020895aec3SBjoern A. Zeeb 		/*
9030895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
9040895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
9050895aec3SBjoern A. Zeeb 		 */
9068c0fec80SRobert Watson 		ia = NULL;
9079317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
908137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
9099317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9100895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
9110895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
9120895aec3SBjoern A. Zeeb 				continue;
9130895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
914b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
9150895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
9160895aec3SBjoern A. Zeeb 				break;
9170895aec3SBjoern A. Zeeb 			}
9180895aec3SBjoern A. Zeeb 		}
9190895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9200895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
921137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9220895aec3SBjoern A. Zeeb 			goto done;
9230895aec3SBjoern A. Zeeb 		}
924137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
9250895aec3SBjoern A. Zeeb 
9260895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
927b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9280895aec3SBjoern A. Zeeb 		goto done;
9290895aec3SBjoern A. Zeeb 	}
9300895aec3SBjoern A. Zeeb 
9310895aec3SBjoern A. Zeeb 	/*
9320895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
9330895aec3SBjoern A. Zeeb 	 * to ourselves is here.
9340895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
9350895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
9360895aec3SBjoern A. Zeeb 	 * a loopback interface.
9370895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
9380895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
9390895aec3SBjoern A. Zeeb 	 */
9400895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
9410895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
9428c0fec80SRobert Watson 		struct in_ifaddr *ia;
9430895aec3SBjoern A. Zeeb 
9440895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
9450895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
9460895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
9470895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
9480895aec3SBjoern A. Zeeb 
94958a39d8cSAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
95058a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
9510895aec3SBjoern A. Zeeb 		if (ia == NULL)
9524f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
95358a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
954f0bb05fcSQing Li 		if (ia == NULL)
955f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
9560895aec3SBjoern A. Zeeb 
9570304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
9580895aec3SBjoern A. Zeeb 			if (ia == NULL) {
9590895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
9600895aec3SBjoern A. Zeeb 				goto done;
9610895aec3SBjoern A. Zeeb 			}
9620895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
9638c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9640895aec3SBjoern A. Zeeb 			goto done;
9650895aec3SBjoern A. Zeeb 		}
9660895aec3SBjoern A. Zeeb 
9670895aec3SBjoern A. Zeeb 		/* Jailed. */
9680895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9690895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
9700895aec3SBjoern A. Zeeb 
9710895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
9728c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9730895aec3SBjoern A. Zeeb 			ia = NULL;
974137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
9750895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9760895aec3SBjoern A. Zeeb 
9770895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
9780895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
9790895aec3SBjoern A. Zeeb 					continue;
9800895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
981b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
982b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
9830895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
9840895aec3SBjoern A. Zeeb 					break;
9850895aec3SBjoern A. Zeeb 				}
9860895aec3SBjoern A. Zeeb 			}
9870895aec3SBjoern A. Zeeb 			if (ia != NULL) {
9880895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
989137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
9900895aec3SBjoern A. Zeeb 				goto done;
9910895aec3SBjoern A. Zeeb 			}
992137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9930895aec3SBjoern A. Zeeb 		}
9940895aec3SBjoern A. Zeeb 
9950895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
996b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9970895aec3SBjoern A. Zeeb 		goto done;
9980895aec3SBjoern A. Zeeb 	}
9990895aec3SBjoern A. Zeeb 
10000895aec3SBjoern A. Zeeb done:
10010895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
10020895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
10030895aec3SBjoern A. Zeeb 	return (error);
10040895aec3SBjoern A. Zeeb }
10050895aec3SBjoern A. Zeeb 
10060895aec3SBjoern A. Zeeb /*
10075200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
10085200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
10095200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
10105200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
10115200e00eSIan Dowse  * the connect.
10125200e00eSIan Dowse  *
10135200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
10145200e00eSIan Dowse  * and port. These are not updated in the error case.
10155200e00eSIan Dowse  *
10165200e00eSIan Dowse  * If the operation fails because the connection already exists,
10175200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
10185200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
10195200e00eSIan Dowse  * is set to NULL.
10205200e00eSIan Dowse  */
10215200e00eSIan Dowse int
1022136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1023136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1024136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
10255200e00eSIan Dowse {
1026cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
10275200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
10285200e00eSIan Dowse 	struct in_ifaddr *ia;
10295200e00eSIan Dowse 	struct inpcb *oinp;
1030b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
10315200e00eSIan Dowse 	u_short lport, fport;
10325200e00eSIan Dowse 	int error;
10335200e00eSIan Dowse 
10348501a69cSRobert Watson 	/*
10358501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
10368501a69cSRobert Watson 	 * lock is sufficient.
10378501a69cSRobert Watson 	 */
103827f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
1039fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
104027f74fd0SRobert Watson 
10415200e00eSIan Dowse 	if (oinpp != NULL)
10425200e00eSIan Dowse 		*oinpp = NULL;
104357bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
1044df8bae1dSRodney W. Grimes 		return (EINVAL);
1045df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
1046df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
1047df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1048df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
10495200e00eSIan Dowse 	laddr.s_addr = *laddrp;
10505200e00eSIan Dowse 	lport = *lportp;
10515200e00eSIan Dowse 	faddr = sin->sin_addr;
10525200e00eSIan Dowse 	fport = sin->sin_port;
10530895aec3SBjoern A. Zeeb 
1054603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1055df8bae1dSRodney W. Grimes 		/*
1056df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1057df8bae1dSRodney W. Grimes 		 * use the primary local address.
1058df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1059df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1060df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1061df8bae1dSRodney W. Grimes 		 */
1062413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
1063cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
1064413628a7SBjoern A. Zeeb 			faddr =
1065b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1066cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1067b89e82ddSJamie Gritton 			if (cred != NULL &&
1068b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1069b89e82ddSJamie Gritton 				return (error);
10702d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1071cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
10722d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
10732d9cfabaSRobert Watson 			    IFF_BROADCAST)
10745200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
1075603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1076cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
10772d9cfabaSRobert Watson 		}
1078df8bae1dSRodney W. Grimes 	}
10795200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1080d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1081df8bae1dSRodney W. Grimes 		/*
1082df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1083d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1084df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1085df8bae1dSRodney W. Grimes 		 */
10865200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1087df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1088df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1089df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1090df8bae1dSRodney W. Grimes 
1091df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1092df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1093df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
1094cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RLOCK(&in_ifa_tracker);
1095e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1096e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1097e691be70SDaniel Eischen 					    (cred == NULL ||
1098e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1099e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1100df8bae1dSRodney W. Grimes 						break;
1101e691be70SDaniel Eischen 				}
1102e691be70SDaniel Eischen 				if (ia == NULL)
1103d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1104e691be70SDaniel Eischen 				else {
11055200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1106d79fdd98SDaniel Eischen 					error = 0;
1107999f1343SGarrett Wollman 				}
1108cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1109d79fdd98SDaniel Eischen 			}
1110d79fdd98SDaniel Eischen 		}
111104215ed2SRandall Stewart 		if (error)
111204215ed2SRandall Stewart 			return (error);
11130895aec3SBjoern A. Zeeb 	}
1114fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1115fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
11165200e00eSIan Dowse 	if (oinp != NULL) {
11175200e00eSIan Dowse 		if (oinpp != NULL)
11185200e00eSIan Dowse 			*oinpp = oinp;
1119df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1120c3229e05SDavid Greenman 	}
11215200e00eSIan Dowse 	if (lport == 0) {
1122b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1123b0330ed9SPawel Jakub Dawidek 		    cred);
11245a903f8dSPierre Beyssac 		if (error)
11255a903f8dSPierre Beyssac 			return (error);
11265a903f8dSPierre Beyssac 	}
11275200e00eSIan Dowse 	*laddrp = laddr.s_addr;
11285200e00eSIan Dowse 	*lportp = lport;
11295200e00eSIan Dowse 	*faddrp = faddr.s_addr;
11305200e00eSIan Dowse 	*fportp = fport;
1131df8bae1dSRodney W. Grimes 	return (0);
1132df8bae1dSRodney W. Grimes }
1133df8bae1dSRodney W. Grimes 
113426f9a767SRodney W. Grimes void
1135136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1136df8bae1dSRodney W. Grimes {
11376b348152SRobert Watson 
11388501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1139fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1142df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
114315bd2b43SDavid Greenman 	in_pcbrehash(inp);
1144df8bae1dSRodney W. Grimes }
114583e521ecSBjoern A. Zeeb #endif /* INET */
1146df8bae1dSRodney W. Grimes 
11474c7c478dSRobert Watson /*
114828696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1149c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
115028696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
115128696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
11524c7c478dSRobert Watson  */
115326f9a767SRodney W. Grimes void
1154136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1155df8bae1dSRodney W. Grimes {
11564c7c478dSRobert Watson 
1157a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1158c0a211c5SRobert Watson 
1159f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1160f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag != NULL)
1161f3e7afe2SHans Petter Selasky 		in_pcbdetach_txrtlmt(inp);
1162f3e7afe2SHans Petter Selasky #endif
11634c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
11644c7c478dSRobert Watson 	inp->inp_socket = NULL;
11654c7c478dSRobert Watson }
11664c7c478dSRobert Watson 
1167c0a211c5SRobert Watson /*
116879bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
116979bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
117079bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
117152cd27cbSRobert Watson  * but where the inpcb lock may already held, or when acquiring a reference
117252cd27cbSRobert Watson  * via a pcbgroup.
117379bdc6e5SRobert Watson  *
117479bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
117579bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
117679bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
117779bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
117879bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
117979bdc6e5SRobert Watson  * inpcb.
118079bdc6e5SRobert Watson  *
118179bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
118279bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
118379bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
118479bdc6e5SRobert Watson  * using in_pcbrele().
1185c0a211c5SRobert Watson  */
118679bdc6e5SRobert Watson void
118779bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
11884c7c478dSRobert Watson {
1189df8bae1dSRodney W. Grimes 
119079bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
119179bdc6e5SRobert Watson 
119279bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
119379bdc6e5SRobert Watson }
119479bdc6e5SRobert Watson 
119579bdc6e5SRobert Watson /*
119679bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
119779bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
119879bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
119979bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
120079bdc6e5SRobert Watson  *
120179bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
120279bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
120379bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
120479bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
120579bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
120679bdc6e5SRobert Watson  */
120779bdc6e5SRobert Watson int
120879bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
120979bdc6e5SRobert Watson {
121079bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
121179bdc6e5SRobert Watson 
121279bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
121379bdc6e5SRobert Watson 
121479bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
121579bdc6e5SRobert Watson 
1216df4e91d3SGleb Smirnoff 	if (refcount_release(&inp->inp_refcount) == 0) {
1217df4e91d3SGleb Smirnoff 		/*
1218df4e91d3SGleb Smirnoff 		 * If the inpcb has been freed, let the caller know, even if
1219df4e91d3SGleb Smirnoff 		 * this isn't the last reference.
1220df4e91d3SGleb Smirnoff 		 */
1221df4e91d3SGleb Smirnoff 		if (inp->inp_flags2 & INP_FREED) {
1222df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1223df4e91d3SGleb Smirnoff 			return (1);
1224df4e91d3SGleb Smirnoff 		}
122579bdc6e5SRobert Watson 		return (0);
1226df4e91d3SGleb Smirnoff 	}
122779bdc6e5SRobert Watson 
122879bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
122979bdc6e5SRobert Watson 
123079bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
123179bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
123279bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
123379bdc6e5SRobert Watson 	return (1);
123479bdc6e5SRobert Watson }
123579bdc6e5SRobert Watson 
123679bdc6e5SRobert Watson int
123779bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
123879bdc6e5SRobert Watson {
123979bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
124079bdc6e5SRobert Watson 
124179bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
124279bdc6e5SRobert Watson 
124379bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
124479bdc6e5SRobert Watson 
1245edd0e0b0SFabien Thomas 	if (refcount_release(&inp->inp_refcount) == 0) {
1246edd0e0b0SFabien Thomas 		/*
1247edd0e0b0SFabien Thomas 		 * If the inpcb has been freed, let the caller know, even if
1248edd0e0b0SFabien Thomas 		 * this isn't the last reference.
1249edd0e0b0SFabien Thomas 		 */
1250edd0e0b0SFabien Thomas 		if (inp->inp_flags2 & INP_FREED) {
1251edd0e0b0SFabien Thomas 			INP_WUNLOCK(inp);
1252edd0e0b0SFabien Thomas 			return (1);
1253edd0e0b0SFabien Thomas 		}
125479bdc6e5SRobert Watson 		return (0);
1255edd0e0b0SFabien Thomas 	}
125679bdc6e5SRobert Watson 
125779bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
125879bdc6e5SRobert Watson 
125979bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
126079bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
126179bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
126279bdc6e5SRobert Watson 	return (1);
126379bdc6e5SRobert Watson }
126479bdc6e5SRobert Watson 
126579bdc6e5SRobert Watson /*
126679bdc6e5SRobert Watson  * Temporary wrapper.
126779bdc6e5SRobert Watson  */
126879bdc6e5SRobert Watson int
126979bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
127079bdc6e5SRobert Watson {
127179bdc6e5SRobert Watson 
127279bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
127379bdc6e5SRobert Watson }
127479bdc6e5SRobert Watson 
127579bdc6e5SRobert Watson /*
127679bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
127779bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
127879bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
127979bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
128079bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
128179bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
128279bdc6e5SRobert Watson  * the pcbinfo lock is held.
128379bdc6e5SRobert Watson  */
128479bdc6e5SRobert Watson void
128579bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
128679bdc6e5SRobert Watson {
128779bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
128879bdc6e5SRobert Watson 
128979bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
129079bdc6e5SRobert Watson 
1291ff9b006dSJulien Charbon #ifdef INVARIANTS
1292ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
1293079672cbSJulien Charbon 		INP_INFO_LOCK_ASSERT(pcbinfo);
1294ff9b006dSJulien Charbon 	} else {
129579bdc6e5SRobert Watson 		INP_INFO_WLOCK_ASSERT(pcbinfo);
1296ff9b006dSJulien Charbon 	}
1297ff9b006dSJulien Charbon #endif
129879bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
129979bdc6e5SRobert Watson 
130079bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1301fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13026aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
13036974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
130483e521ecSBjoern A. Zeeb #endif
1305ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
130679bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1307c3229e05SDavid Greenman 	in_pcbremlists(inp);
1308ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
13096aee2fc5SBjoern A. Zeeb #ifdef INET6
13106aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
13116aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
13121096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
13136aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
13146aee2fc5SBjoern A. Zeeb 	}
13156aee2fc5SBjoern A. Zeeb #endif
1316df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1317df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
131867107f45SBjoern A. Zeeb #ifdef INET
131971498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
132071498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
132167107f45SBjoern A. Zeeb #endif
1322cc94f0c2SGleb Smirnoff 	RO_RTFREE(&inp->inp_route);
13236d768226SGeorge V. Neville-Neil 	if (inp->inp_route.ro_lle)
13246d768226SGeorge V. Neville-Neil 		LLE_FREE(inp->inp_route.ro_lle);	/* zeros ro_lle */
132584cc0778SGeorge V. Neville-Neil 
1326cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1327df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
132886d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1329a557af22SRobert Watson #ifdef MAC
133030d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1331a557af22SRobert Watson #endif
133279bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
133328696211SRobert Watson 		INP_WUNLOCK(inp);
133428696211SRobert Watson }
133528696211SRobert Watson 
133628696211SRobert Watson /*
1337c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1338c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1339c0a211c5SRobert Watson  *
1340c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1341c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1342c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1343c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1344c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1345c0a211c5SRobert Watson  * in_pcbdetach().
1346c0a211c5SRobert Watson  *
1347c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1348c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
134910702a28SRobert Watson  */
135010702a28SRobert Watson void
135110702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
135210702a28SRobert Watson {
135310702a28SRobert Watson 
13548501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
135510702a28SRobert Watson 
1356fa046d87SRobert Watson 	/*
1357fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1358fa046d87SRobert Watson 	 * the hash lock...?
1359fa046d87SRobert Watson 	 */
1360ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1361111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
136210702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
136310702a28SRobert Watson 
1364fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
136510702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
136610702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
136710702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
136810702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
136910702a28SRobert Watson 			free(phd, M_PCB);
137010702a28SRobert Watson 		}
1371fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1372111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
137352cd27cbSRobert Watson #ifdef PCBGROUP
137452cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
137552cd27cbSRobert Watson #endif
137610702a28SRobert Watson 	}
137710702a28SRobert Watson }
137810702a28SRobert Watson 
137967107f45SBjoern A. Zeeb #ifdef INET
138054d642bbSRobert Watson /*
138154d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
138254d642bbSRobert Watson  */
138326ef6ac4SDon Lewis struct sockaddr *
1384136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
138526ef6ac4SDon Lewis {
138626ef6ac4SDon Lewis 	struct sockaddr_in *sin;
138726ef6ac4SDon Lewis 
13881ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1389a163d034SWarner Losh 		M_WAITOK | M_ZERO);
139026ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
139126ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
139226ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
139326ef6ac4SDon Lewis 	sin->sin_port = port;
139426ef6ac4SDon Lewis 
139526ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
139626ef6ac4SDon Lewis }
139726ef6ac4SDon Lewis 
1398117bcae7SGarrett Wollman int
139954d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1400df8bae1dSRodney W. Grimes {
1401136d4f1cSRobert Watson 	struct inpcb *inp;
140226ef6ac4SDon Lewis 	struct in_addr addr;
140326ef6ac4SDon Lewis 	in_port_t port;
140442fa505bSDavid Greenman 
1405fdc984f7STor Egge 	inp = sotoinpcb(so);
140654d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
14076466b28aSRobert Watson 
1408a69042a5SRobert Watson 	INP_RLOCK(inp);
140926ef6ac4SDon Lewis 	port = inp->inp_lport;
141026ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1411a69042a5SRobert Watson 	INP_RUNLOCK(inp);
141242fa505bSDavid Greenman 
141326ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1414117bcae7SGarrett Wollman 	return 0;
1415df8bae1dSRodney W. Grimes }
1416df8bae1dSRodney W. Grimes 
1417117bcae7SGarrett Wollman int
141854d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1419df8bae1dSRodney W. Grimes {
1420136d4f1cSRobert Watson 	struct inpcb *inp;
142126ef6ac4SDon Lewis 	struct in_addr addr;
142226ef6ac4SDon Lewis 	in_port_t port;
142342fa505bSDavid Greenman 
1424fdc984f7STor Egge 	inp = sotoinpcb(so);
142554d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
14266466b28aSRobert Watson 
1427a69042a5SRobert Watson 	INP_RLOCK(inp);
142826ef6ac4SDon Lewis 	port = inp->inp_fport;
142926ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1430a69042a5SRobert Watson 	INP_RUNLOCK(inp);
143142fa505bSDavid Greenman 
143226ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1433117bcae7SGarrett Wollman 	return 0;
1434df8bae1dSRodney W. Grimes }
1435df8bae1dSRodney W. Grimes 
143626f9a767SRodney W. Grimes void
1437136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1438136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1439d1c54148SJesper Skriver {
1440f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1441d1c54148SJesper Skriver 
14423dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1443f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
14448501a69cSRobert Watson 		INP_WLOCK(inp);
1445d1c54148SJesper Skriver #ifdef INET6
1446f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
14478501a69cSRobert Watson 			INP_WUNLOCK(inp);
1448d1c54148SJesper Skriver 			continue;
1449f76fcf6dSJeffrey Hsu 		}
1450d1c54148SJesper Skriver #endif
1451d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1452f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
14538501a69cSRobert Watson 			INP_WUNLOCK(inp);
1454d1c54148SJesper Skriver 			continue;
1455d1c54148SJesper Skriver 		}
14563dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
14578501a69cSRobert Watson 			INP_WUNLOCK(inp);
1458f76fcf6dSJeffrey Hsu 	}
14593dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1460d1c54148SJesper Skriver }
1461d1c54148SJesper Skriver 
1462e43cc4aeSHajimu UMEMOTO void
1463136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1464e43cc4aeSHajimu UMEMOTO {
1465e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1466e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1467e43cc4aeSHajimu UMEMOTO 	int i, gap;
1468e43cc4aeSHajimu UMEMOTO 
1469ff9b006dSJulien Charbon 	INP_INFO_WLOCK(pcbinfo);
1470712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
14718501a69cSRobert Watson 		INP_WLOCK(inp);
1472e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1473e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1474e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1475e43cc4aeSHajimu UMEMOTO 			/*
1476e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1477e43cc4aeSHajimu UMEMOTO 			 * detached.
1478e43cc4aeSHajimu UMEMOTO 			 */
1479e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1480e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1481e43cc4aeSHajimu UMEMOTO 
1482e43cc4aeSHajimu UMEMOTO 			/*
1483e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1484e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1485e43cc4aeSHajimu UMEMOTO 			 */
1486e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1487e43cc4aeSHajimu UMEMOTO 			    i++) {
1488e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1489e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1490e43cc4aeSHajimu UMEMOTO 					gap++;
1491e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1492e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1493e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1494e43cc4aeSHajimu UMEMOTO 			}
1495e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1496e43cc4aeSHajimu UMEMOTO 		}
14978501a69cSRobert Watson 		INP_WUNLOCK(inp);
1498e43cc4aeSHajimu UMEMOTO 	}
1499ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(pcbinfo);
1500e43cc4aeSHajimu UMEMOTO }
1501e43cc4aeSHajimu UMEMOTO 
1502df8bae1dSRodney W. Grimes /*
1503fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1504fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1505c3229e05SDavid Greenman  */
1506d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1507df8bae1dSRodney W. Grimes struct inpcb *
1508136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
150968e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1510df8bae1dSRodney W. Grimes {
1511136d4f1cSRobert Watson 	struct inpcb *inp;
1512d5e8a67eSHajimu UMEMOTO #ifdef INET6
1513d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1514d5e8a67eSHajimu UMEMOTO #else
1515d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1516d5e8a67eSHajimu UMEMOTO #endif
1517d5e8a67eSHajimu UMEMOTO 	int wildcard;
15187bc4aca7SDavid Greenman 
151968e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
152068e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
152168e0d7e0SRobert Watson 
1522fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
15231b73ca0bSSam Leffler 
152468e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1525c3229e05SDavid Greenman 		struct inpcbhead *head;
1526c3229e05SDavid Greenman 		/*
1527c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1528c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1529c3229e05SDavid Greenman 		 */
1530712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1531712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1532fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1533cfa1ca9dSYoshinobu Inoue #ifdef INET6
1534413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1535369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1536cfa1ca9dSYoshinobu Inoue 				continue;
1537cfa1ca9dSYoshinobu Inoue #endif
1538c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1539c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1540c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1541c3229e05SDavid Greenman 				/*
1542413628a7SBjoern A. Zeeb 				 * Found?
1543c3229e05SDavid Greenman 				 */
1544413628a7SBjoern A. Zeeb 				if (cred == NULL ||
15450304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
15460304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1547c3229e05SDavid Greenman 					return (inp);
1548df8bae1dSRodney W. Grimes 			}
1549c3229e05SDavid Greenman 		}
1550c3229e05SDavid Greenman 		/*
1551c3229e05SDavid Greenman 		 * Not found.
1552c3229e05SDavid Greenman 		 */
1553c3229e05SDavid Greenman 		return (NULL);
1554c3229e05SDavid Greenman 	} else {
1555c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1556c3229e05SDavid Greenman 		struct inpcbport *phd;
1557c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1558c3229e05SDavid Greenman 		/*
1559c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1560c3229e05SDavid Greenman 		 *
1561c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1562c3229e05SDavid Greenman 		 * port hash list.
1563c3229e05SDavid Greenman 		 */
1564712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1565712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1566fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1567c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1568c3229e05SDavid Greenman 				break;
1569c3229e05SDavid Greenman 		}
1570c3229e05SDavid Greenman 		if (phd != NULL) {
1571c3229e05SDavid Greenman 			/*
1572c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1573c3229e05SDavid Greenman 			 * fit.
1574c3229e05SDavid Greenman 			 */
157537d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1576c3229e05SDavid Greenman 				wildcard = 0;
1577413628a7SBjoern A. Zeeb 				if (cred != NULL &&
15780304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
15790304c731SJamie Gritton 					cred->cr_prison))
1580413628a7SBjoern A. Zeeb 					continue;
1581cfa1ca9dSYoshinobu Inoue #ifdef INET6
1582413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1583369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1584cfa1ca9dSYoshinobu Inoue 					continue;
1585d5e8a67eSHajimu UMEMOTO 				/*
1586d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1587d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1588d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1589d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1590d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1591d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1592d5e8a67eSHajimu UMEMOTO 				 *
1593d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1594d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1595d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1596d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1597d5e8a67eSHajimu UMEMOTO 				 */
1598d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1599d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1600cfa1ca9dSYoshinobu Inoue #endif
1601c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1602c3229e05SDavid Greenman 					wildcard++;
160315bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
160415bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
160515bd2b43SDavid Greenman 						wildcard++;
160615bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
160715bd2b43SDavid Greenman 						continue;
160815bd2b43SDavid Greenman 				} else {
160915bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
161015bd2b43SDavid Greenman 						wildcard++;
161115bd2b43SDavid Greenman 				}
1612df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1613df8bae1dSRodney W. Grimes 					match = inp;
1614df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1615413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1616df8bae1dSRodney W. Grimes 						break;
1617df8bae1dSRodney W. Grimes 				}
1618df8bae1dSRodney W. Grimes 			}
16193dbdc25cSDavid Greenman 		}
1620df8bae1dSRodney W. Grimes 		return (match);
1621df8bae1dSRodney W. Grimes 	}
1622c3229e05SDavid Greenman }
1623d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
162415bd2b43SDavid Greenman 
162552cd27cbSRobert Watson #ifdef PCBGROUP
162652cd27cbSRobert Watson /*
162752cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
162852cd27cbSRobert Watson  */
162952cd27cbSRobert Watson static struct inpcb *
163052cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
163152cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
163252cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
163352cd27cbSRobert Watson {
163452cd27cbSRobert Watson 	struct inpcbhead *head;
163552cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
163652cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
163752cd27cbSRobert Watson 
163852cd27cbSRobert Watson 	/*
163952cd27cbSRobert Watson 	 * First look for an exact match.
164052cd27cbSRobert Watson 	 */
164152cd27cbSRobert Watson 	tmpinp = NULL;
164252cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
164352cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
164452cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
164552cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
164652cd27cbSRobert Watson #ifdef INET6
164752cd27cbSRobert Watson 		/* XXX inp locking */
164852cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
164952cd27cbSRobert Watson 			continue;
165052cd27cbSRobert Watson #endif
165152cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
165252cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
165352cd27cbSRobert Watson 		    inp->inp_fport == fport &&
165452cd27cbSRobert Watson 		    inp->inp_lport == lport) {
165552cd27cbSRobert Watson 			/*
165652cd27cbSRobert Watson 			 * XXX We should be able to directly return
165752cd27cbSRobert Watson 			 * the inp here, without any checks.
165852cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
165952cd27cbSRobert Watson 			 */
166052cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
166152cd27cbSRobert Watson 				goto found;
166252cd27cbSRobert Watson 			if (tmpinp == NULL)
166352cd27cbSRobert Watson 				tmpinp = inp;
166452cd27cbSRobert Watson 		}
166552cd27cbSRobert Watson 	}
166652cd27cbSRobert Watson 	if (tmpinp != NULL) {
166752cd27cbSRobert Watson 		inp = tmpinp;
166852cd27cbSRobert Watson 		goto found;
166952cd27cbSRobert Watson 	}
167052cd27cbSRobert Watson 
16710a100a6fSAdrian Chadd #ifdef	RSS
16720a100a6fSAdrian Chadd 	/*
16730a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
16740a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
16750a100a6fSAdrian Chadd 	 */
16760a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
16770a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
16780a100a6fSAdrian Chadd #ifdef INET6
16790a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
16800a100a6fSAdrian Chadd #endif
16810a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
16820a100a6fSAdrian Chadd 		struct inpcbhead *head;
16830a100a6fSAdrian Chadd 		int injail;
16840a100a6fSAdrian Chadd 
16850a100a6fSAdrian Chadd 		/*
16860a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
16870a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
16880a100a6fSAdrian Chadd 		 *      2. jailed, wild.
16890a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
16900a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
16910a100a6fSAdrian Chadd 		 */
16920a100a6fSAdrian Chadd 
16930a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
16940a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
16950a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
16960a100a6fSAdrian Chadd #ifdef INET6
16970a100a6fSAdrian Chadd 			/* XXX inp locking */
16980a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
16990a100a6fSAdrian Chadd 				continue;
17000a100a6fSAdrian Chadd #endif
17010a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
17020a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
17030a100a6fSAdrian Chadd 				continue;
17040a100a6fSAdrian Chadd 
17050a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
17060a100a6fSAdrian Chadd 			if (injail) {
17070a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
17080a100a6fSAdrian Chadd 				    &laddr) != 0)
17090a100a6fSAdrian Chadd 					continue;
17100a100a6fSAdrian Chadd 			} else {
17110a100a6fSAdrian Chadd 				if (local_exact != NULL)
17120a100a6fSAdrian Chadd 					continue;
17130a100a6fSAdrian Chadd 			}
17140a100a6fSAdrian Chadd 
17150a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
17160a100a6fSAdrian Chadd 				if (injail)
17170a100a6fSAdrian Chadd 					goto found;
17180a100a6fSAdrian Chadd 				else
17190a100a6fSAdrian Chadd 					local_exact = inp;
17200a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
17210a100a6fSAdrian Chadd #ifdef INET6
17220a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
17230a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
17240a100a6fSAdrian Chadd 					local_wild_mapped = inp;
17250a100a6fSAdrian Chadd 				else
17260a100a6fSAdrian Chadd #endif
17270a100a6fSAdrian Chadd 					if (injail)
17280a100a6fSAdrian Chadd 						jail_wild = inp;
17290a100a6fSAdrian Chadd 					else
17300a100a6fSAdrian Chadd 						local_wild = inp;
17310a100a6fSAdrian Chadd 			}
17320a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
17330a100a6fSAdrian Chadd 
17340a100a6fSAdrian Chadd 		inp = jail_wild;
17350a100a6fSAdrian Chadd 		if (inp == NULL)
17360a100a6fSAdrian Chadd 			inp = local_exact;
17370a100a6fSAdrian Chadd 		if (inp == NULL)
17380a100a6fSAdrian Chadd 			inp = local_wild;
17390a100a6fSAdrian Chadd #ifdef INET6
17400a100a6fSAdrian Chadd 		if (inp == NULL)
17410a100a6fSAdrian Chadd 			inp = local_wild_mapped;
17420a100a6fSAdrian Chadd #endif
17430a100a6fSAdrian Chadd 		if (inp != NULL)
17440a100a6fSAdrian Chadd 			goto found;
17450a100a6fSAdrian Chadd 	}
17460a100a6fSAdrian Chadd #endif
17470a100a6fSAdrian Chadd 
174852cd27cbSRobert Watson 	/*
174952cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
175052cd27cbSRobert Watson 	 */
175152cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
175252cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
175352cd27cbSRobert Watson #ifdef INET6
175452cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
175552cd27cbSRobert Watson #endif
175652cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
175752cd27cbSRobert Watson 		struct inpcbhead *head;
175852cd27cbSRobert Watson 		int injail;
175952cd27cbSRobert Watson 
176052cd27cbSRobert Watson 		/*
176152cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
176252cd27cbSRobert Watson 		 *      1. jailed, non-wild.
176352cd27cbSRobert Watson 		 *      2. jailed, wild.
176452cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
176552cd27cbSRobert Watson 		 *      4. non-jailed, wild.
176652cd27cbSRobert Watson 		 */
176752cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
176852cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
176952cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
177052cd27cbSRobert Watson #ifdef INET6
177152cd27cbSRobert Watson 			/* XXX inp locking */
177252cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
177352cd27cbSRobert Watson 				continue;
177452cd27cbSRobert Watson #endif
177552cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
177652cd27cbSRobert Watson 			    inp->inp_lport != lport)
177752cd27cbSRobert Watson 				continue;
177852cd27cbSRobert Watson 
177952cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
178052cd27cbSRobert Watson 			if (injail) {
178152cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
178252cd27cbSRobert Watson 				    &laddr) != 0)
178352cd27cbSRobert Watson 					continue;
178452cd27cbSRobert Watson 			} else {
178552cd27cbSRobert Watson 				if (local_exact != NULL)
178652cd27cbSRobert Watson 					continue;
178752cd27cbSRobert Watson 			}
178852cd27cbSRobert Watson 
178952cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
179052cd27cbSRobert Watson 				if (injail)
179152cd27cbSRobert Watson 					goto found;
179252cd27cbSRobert Watson 				else
179352cd27cbSRobert Watson 					local_exact = inp;
179452cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
179552cd27cbSRobert Watson #ifdef INET6
179652cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
179752cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
179852cd27cbSRobert Watson 					local_wild_mapped = inp;
179952cd27cbSRobert Watson 				else
180083e521ecSBjoern A. Zeeb #endif
180152cd27cbSRobert Watson 					if (injail)
180252cd27cbSRobert Watson 						jail_wild = inp;
180352cd27cbSRobert Watson 					else
180452cd27cbSRobert Watson 						local_wild = inp;
180552cd27cbSRobert Watson 			}
180652cd27cbSRobert Watson 		} /* LIST_FOREACH */
180752cd27cbSRobert Watson 		inp = jail_wild;
180852cd27cbSRobert Watson 		if (inp == NULL)
180952cd27cbSRobert Watson 			inp = local_exact;
181052cd27cbSRobert Watson 		if (inp == NULL)
181152cd27cbSRobert Watson 			inp = local_wild;
181252cd27cbSRobert Watson #ifdef INET6
181352cd27cbSRobert Watson 		if (inp == NULL)
181452cd27cbSRobert Watson 			inp = local_wild_mapped;
181583e521ecSBjoern A. Zeeb #endif
181652cd27cbSRobert Watson 		if (inp != NULL)
181752cd27cbSRobert Watson 			goto found;
181852cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
181952cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
182052cd27cbSRobert Watson 	return (NULL);
182152cd27cbSRobert Watson 
182252cd27cbSRobert Watson found:
182352cd27cbSRobert Watson 	in_pcbref(inp);
182452cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
182552cd27cbSRobert Watson 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
182652cd27cbSRobert Watson 		INP_WLOCK(inp);
182752cd27cbSRobert Watson 		if (in_pcbrele_wlocked(inp))
182852cd27cbSRobert Watson 			return (NULL);
182952cd27cbSRobert Watson 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
183052cd27cbSRobert Watson 		INP_RLOCK(inp);
183152cd27cbSRobert Watson 		if (in_pcbrele_rlocked(inp))
183252cd27cbSRobert Watson 			return (NULL);
183352cd27cbSRobert Watson 	} else
183452cd27cbSRobert Watson 		panic("%s: locking bug", __func__);
183552cd27cbSRobert Watson 	return (inp);
183652cd27cbSRobert Watson }
183752cd27cbSRobert Watson #endif /* PCBGROUP */
183852cd27cbSRobert Watson 
183915bd2b43SDavid Greenman /*
1840fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1841fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1842fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
184315bd2b43SDavid Greenman  */
1844fa046d87SRobert Watson static struct inpcb *
1845fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
184668e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1847136d4f1cSRobert Watson     struct ifnet *ifp)
184815bd2b43SDavid Greenman {
184915bd2b43SDavid Greenman 	struct inpcbhead *head;
1850413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
185115bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
185215bd2b43SDavid Greenman 
185368e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
185468e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
185568e0d7e0SRobert Watson 
1856fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1857602cc7f1SRobert Watson 
185815bd2b43SDavid Greenman 	/*
185915bd2b43SDavid Greenman 	 * First look for an exact match.
186015bd2b43SDavid Greenman 	 */
1861413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1862712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1863712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1864fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1865cfa1ca9dSYoshinobu Inoue #ifdef INET6
1866413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1867369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1868cfa1ca9dSYoshinobu Inoue 			continue;
1869cfa1ca9dSYoshinobu Inoue #endif
18706d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1871ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1872ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1873413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1874413628a7SBjoern A. Zeeb 			/*
1875413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1876413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1877413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1878413628a7SBjoern A. Zeeb 			 */
18790304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1880c3229e05SDavid Greenman 				return (inp);
1881413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1882413628a7SBjoern A. Zeeb 				tmpinp = inp;
1883c3229e05SDavid Greenman 		}
1884413628a7SBjoern A. Zeeb 	}
1885413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1886413628a7SBjoern A. Zeeb 		return (tmpinp);
1887e3fd5ffdSRobert Watson 
1888e3fd5ffdSRobert Watson 	/*
1889e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1890e3fd5ffdSRobert Watson 	 */
189168e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1892413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1893e3fd5ffdSRobert Watson #ifdef INET6
1894cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1895e3fd5ffdSRobert Watson #endif
1896413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1897413628a7SBjoern A. Zeeb 		int injail;
1898413628a7SBjoern A. Zeeb 
1899413628a7SBjoern A. Zeeb 		/*
1900413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1901413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1902413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1903413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1904413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1905413628a7SBjoern A. Zeeb 		 */
19066d6a026bSDavid Greenman 
1907712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1908712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1909fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1910cfa1ca9dSYoshinobu Inoue #ifdef INET6
1911413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1912369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1913cfa1ca9dSYoshinobu Inoue 				continue;
1914cfa1ca9dSYoshinobu Inoue #endif
1915413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1916413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1917413628a7SBjoern A. Zeeb 				continue;
1918413628a7SBjoern A. Zeeb 
19190304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1920413628a7SBjoern A. Zeeb 			if (injail) {
1921b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1922b89e82ddSJamie Gritton 				    &laddr) != 0)
1923413628a7SBjoern A. Zeeb 					continue;
1924413628a7SBjoern A. Zeeb 			} else {
1925413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1926413628a7SBjoern A. Zeeb 					continue;
1927413628a7SBjoern A. Zeeb 			}
1928413628a7SBjoern A. Zeeb 
1929413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1930413628a7SBjoern A. Zeeb 				if (injail)
1931c3229e05SDavid Greenman 					return (inp);
1932413628a7SBjoern A. Zeeb 				else
1933413628a7SBjoern A. Zeeb 					local_exact = inp;
1934413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1935e3fd5ffdSRobert Watson #ifdef INET6
1936413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
19375cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1938cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1939cfa1ca9dSYoshinobu Inoue 				else
194083e521ecSBjoern A. Zeeb #endif
1941413628a7SBjoern A. Zeeb 					if (injail)
1942413628a7SBjoern A. Zeeb 						jail_wild = inp;
1943413628a7SBjoern A. Zeeb 					else
19446d6a026bSDavid Greenman 						local_wild = inp;
19456d6a026bSDavid Greenman 			}
1946413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1947413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1948413628a7SBjoern A. Zeeb 			return (jail_wild);
1949413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1950413628a7SBjoern A. Zeeb 			return (local_exact);
1951413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1952c3229e05SDavid Greenman 			return (local_wild);
1953413628a7SBjoern A. Zeeb #ifdef INET6
1954413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1955413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
195683e521ecSBjoern A. Zeeb #endif
195768e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1958413628a7SBjoern A. Zeeb 
19596d6a026bSDavid Greenman 	return (NULL);
196015bd2b43SDavid Greenman }
1961fa046d87SRobert Watson 
1962fa046d87SRobert Watson /*
1963fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1964fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1965fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1966fa046d87SRobert Watson  */
1967fa046d87SRobert Watson static struct inpcb *
1968fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1969fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1970fa046d87SRobert Watson     struct ifnet *ifp)
1971fa046d87SRobert Watson {
1972fa046d87SRobert Watson 	struct inpcb *inp;
1973fa046d87SRobert Watson 
1974fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1975fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1976fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1977fa046d87SRobert Watson 	if (inp != NULL) {
1978fa046d87SRobert Watson 		in_pcbref(inp);
1979fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1980fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1981fa046d87SRobert Watson 			INP_WLOCK(inp);
1982fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1983fa046d87SRobert Watson 				return (NULL);
1984fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1985fa046d87SRobert Watson 			INP_RLOCK(inp);
1986fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1987fa046d87SRobert Watson 				return (NULL);
1988fa046d87SRobert Watson 		} else
1989fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1990fa046d87SRobert Watson 	} else
1991fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1992fa046d87SRobert Watson 	return (inp);
1993fa046d87SRobert Watson }
1994fa046d87SRobert Watson 
1995fa046d87SRobert Watson /*
1996d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1997d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
199852cd27cbSRobert Watson  *
199952cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
2000fa046d87SRobert Watson  */
2001fa046d87SRobert Watson struct inpcb *
2002fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
2003fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
2004fa046d87SRobert Watson {
20057527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
200652cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
200752cd27cbSRobert Watson #endif
2008fa046d87SRobert Watson 
2009fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2010fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2011fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2012fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2013fa046d87SRobert Watson 
20147527624eSRobert Watson 	/*
20157527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
20167527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
20177527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
20187527624eSRobert Watson 	 * in software.
20197527624eSRobert Watson 	 *
20207527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
20217527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
20227527624eSRobert Watson 	 * in software.
20237527624eSRobert Watson 	 */
20247527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
202552cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
202652cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
202752cd27cbSRobert Watson 		    fport);
202852cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
202952cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
203052cd27cbSRobert Watson 	}
203152cd27cbSRobert Watson #endif
2032fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2033fa046d87SRobert Watson 	    lookupflags, ifp));
2034fa046d87SRobert Watson }
2035d3c1f003SRobert Watson 
2036d3c1f003SRobert Watson struct inpcb *
2037d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2038d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2039d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2040d3c1f003SRobert Watson {
204152cd27cbSRobert Watson #ifdef PCBGROUP
204252cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
204352cd27cbSRobert Watson #endif
2044d3c1f003SRobert Watson 
2045d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2046d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2047d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2048d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2049d3c1f003SRobert Watson 
205052cd27cbSRobert Watson #ifdef PCBGROUP
20517527624eSRobert Watson 	/*
20527527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
20537527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
20547527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
20557527624eSRobert Watson 	 * using RSS.
20567527624eSRobert Watson 	 *
20577527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
20587527624eSRobert Watson 	 */
20597527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
20607527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
206152cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
206252cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
206352cd27cbSRobert Watson 		if (pcbgroup != NULL)
206452cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
206552cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
20667527624eSRobert Watson #ifndef RSS
206752cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
206852cd27cbSRobert Watson 		    fport);
206952cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
207052cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
20717527624eSRobert Watson #endif
207252cd27cbSRobert Watson 	}
207352cd27cbSRobert Watson #endif
2074d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2075d3c1f003SRobert Watson 	    lookupflags, ifp));
2076d3c1f003SRobert Watson }
207767107f45SBjoern A. Zeeb #endif /* INET */
207815bd2b43SDavid Greenman 
20797bc4aca7SDavid Greenman /*
2080c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
20817bc4aca7SDavid Greenman  */
208252cd27cbSRobert Watson static int
208352cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
208415bd2b43SDavid Greenman {
2085c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2086c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2087c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2088c3229e05SDavid Greenman 	struct inpcbport *phd;
2089cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
209015bd2b43SDavid Greenman 
20918501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2092fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2093fa046d87SRobert Watson 
2094111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2095111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2096602cc7f1SRobert Watson 
2097cfa1ca9dSYoshinobu Inoue #ifdef INET6
2098cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
20991b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2100cfa1ca9dSYoshinobu Inoue 	else
210183e521ecSBjoern A. Zeeb #endif
2102cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2103cfa1ca9dSYoshinobu Inoue 
2104712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2105712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
210615bd2b43SDavid Greenman 
2107712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2108712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2109c3229e05SDavid Greenman 
2110c3229e05SDavid Greenman 	/*
2111c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2112c3229e05SDavid Greenman 	 */
2113fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2114c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2115c3229e05SDavid Greenman 			break;
2116c3229e05SDavid Greenman 	}
2117c3229e05SDavid Greenman 	/*
2118c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2119c3229e05SDavid Greenman 	 */
2120c3229e05SDavid Greenman 	if (phd == NULL) {
21211ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2122c3229e05SDavid Greenman 		if (phd == NULL) {
2123c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2124c3229e05SDavid Greenman 		}
2125c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2126c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2127c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2128c3229e05SDavid Greenman 	}
2129c3229e05SDavid Greenman 	inp->inp_phd = phd;
2130c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2131c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2132111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
213352cd27cbSRobert Watson #ifdef PCBGROUP
213452cd27cbSRobert Watson 	if (do_pcbgroup_update)
213552cd27cbSRobert Watson 		in_pcbgroup_update(inp);
213652cd27cbSRobert Watson #endif
2137c3229e05SDavid Greenman 	return (0);
213815bd2b43SDavid Greenman }
213915bd2b43SDavid Greenman 
2140c3229e05SDavid Greenman /*
214152cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
214252cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
214352cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
214452cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
214552cd27cbSRobert Watson  * pcbgroup until later.
214652cd27cbSRobert Watson  *
214752cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
214852cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
214952cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
215052cd27cbSRobert Watson  */
215152cd27cbSRobert Watson int
215252cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
215352cd27cbSRobert Watson {
215452cd27cbSRobert Watson 
215552cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
215652cd27cbSRobert Watson }
215752cd27cbSRobert Watson 
215852cd27cbSRobert Watson int
215952cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
216052cd27cbSRobert Watson {
216152cd27cbSRobert Watson 
216252cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
216352cd27cbSRobert Watson }
216452cd27cbSRobert Watson 
216552cd27cbSRobert Watson /*
2166c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2167c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2168c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2169c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2170c3229e05SDavid Greenman  */
217115bd2b43SDavid Greenman void
2172d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
217315bd2b43SDavid Greenman {
217459daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
217515bd2b43SDavid Greenman 	struct inpcbhead *head;
2176cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
217715bd2b43SDavid Greenman 
21788501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2179fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2180fa046d87SRobert Watson 
2181111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2182111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2183602cc7f1SRobert Watson 
2184cfa1ca9dSYoshinobu Inoue #ifdef INET6
2185cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
21861b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2187cfa1ca9dSYoshinobu Inoue 	else
218883e521ecSBjoern A. Zeeb #endif
2189cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2190cfa1ca9dSYoshinobu Inoue 
2191712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2192712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
219315bd2b43SDavid Greenman 
2194c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
219515bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
219652cd27cbSRobert Watson 
219752cd27cbSRobert Watson #ifdef PCBGROUP
219852cd27cbSRobert Watson 	if (m != NULL)
219952cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
220052cd27cbSRobert Watson 	else
220152cd27cbSRobert Watson 		in_pcbgroup_update(inp);
220252cd27cbSRobert Watson #endif
2203c3229e05SDavid Greenman }
2204c3229e05SDavid Greenman 
2205d3c1f003SRobert Watson void
2206d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2207d3c1f003SRobert Watson {
2208d3c1f003SRobert Watson 
2209d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2210d3c1f003SRobert Watson }
2211d3c1f003SRobert Watson 
2212c3229e05SDavid Greenman /*
2213c3229e05SDavid Greenman  * Remove PCB from various lists.
2214c3229e05SDavid Greenman  */
22156d888973SRobert Watson static void
2216136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2217c3229e05SDavid Greenman {
221859daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
221959daba27SSam Leffler 
2220ff9b006dSJulien Charbon #ifdef INVARIANTS
2221ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
2222ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
2223ff9b006dSJulien Charbon 	} else {
222459daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
2225ff9b006dSJulien Charbon 	}
2226ff9b006dSJulien Charbon #endif
2227ff9b006dSJulien Charbon 
22288501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2229ff9b006dSJulien Charbon 	INP_LIST_WLOCK_ASSERT(pcbinfo);
223059daba27SSam Leffler 
223159daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2232111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2233c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2234c3229e05SDavid Greenman 
2235fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2236c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2237c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2238fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2239c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2240c3229e05SDavid Greenman 			free(phd, M_PCB);
2241c3229e05SDavid Greenman 		}
2242fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2243111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2244c3229e05SDavid Greenman 	}
2245c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
224659daba27SSam Leffler 	pcbinfo->ipi_count--;
224752cd27cbSRobert Watson #ifdef PCBGROUP
224852cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
224952cd27cbSRobert Watson #endif
225015bd2b43SDavid Greenman }
225175c13541SPoul-Henning Kamp 
2252a557af22SRobert Watson /*
225384cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
225484cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
225584cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
225684cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
225784cc0778SGeorge V. Neville-Neil  */
225884cc0778SGeorge V. Neville-Neil void
225984cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
226084cc0778SGeorge V. Neville-Neil {
226184cc0778SGeorge V. Neville-Neil 
2262cc94f0c2SGleb Smirnoff 	RO_RTFREE(&inp->inp_route);
22636d768226SGeorge V. Neville-Neil 	if (inp->inp_route.ro_lle)
22646d768226SGeorge V. Neville-Neil 		LLE_FREE(inp->inp_route.ro_lle);	/* zeros ro_lle */
226584cc0778SGeorge V. Neville-Neil 	return;
226684cc0778SGeorge V. Neville-Neil }
226784cc0778SGeorge V. Neville-Neil 
226884cc0778SGeorge V. Neville-Neil /*
2269a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2270a557af22SRobert Watson  * label change into the in_pcb for the socket.
2271a557af22SRobert Watson  */
2272a557af22SRobert Watson void
2273136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2274a557af22SRobert Watson {
2275a557af22SRobert Watson #ifdef MAC
2276a557af22SRobert Watson 	struct inpcb *inp;
2277a557af22SRobert Watson 
22784c7c478dSRobert Watson 	inp = sotoinpcb(so);
22794c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2280602cc7f1SRobert Watson 
22818501a69cSRobert Watson 	INP_WLOCK(inp);
2282310e7cebSRobert Watson 	SOCK_LOCK(so);
2283a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2284310e7cebSRobert Watson 	SOCK_UNLOCK(so);
22858501a69cSRobert Watson 	INP_WUNLOCK(inp);
2286a557af22SRobert Watson #endif
2287a557af22SRobert Watson }
22885f311da2SMike Silbersack 
22895f311da2SMike Silbersack /*
2290ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2291ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2292ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2293ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2294ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
22955f311da2SMike Silbersack  */
2296aae49dd3SBjoern A. Zeeb static void
2297136d4f1cSRobert Watson ipport_tick(void *xtp)
22985f311da2SMike Silbersack {
22998b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2300ad3a630fSRobert Watson 
23015ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
23028b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
23038b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
23048b615593SMarko Zec 		if (V_ipport_tcpallocs <=
23058b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2306603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2307603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2308ad3a630fSRobert Watson 		} else
2309603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2310603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
23118b615593SMarko Zec 		CURVNET_RESTORE();
23128b615593SMarko Zec 	}
23135ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
23145f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
23155f311da2SMike Silbersack }
2316497057eeSRobert Watson 
2317aae49dd3SBjoern A. Zeeb static void
2318aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2319aae49dd3SBjoern A. Zeeb {
2320aae49dd3SBjoern A. Zeeb 
2321aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2322aae49dd3SBjoern A. Zeeb }
2323aae49dd3SBjoern A. Zeeb 
2324aae49dd3SBjoern A. Zeeb /*
2325aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2326aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2327aae49dd3SBjoern A. Zeeb  */
2328aae49dd3SBjoern A. Zeeb static void
2329aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2330aae49dd3SBjoern A. Zeeb {
2331aae49dd3SBjoern A. Zeeb 
2332aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2333fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2334aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2335aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2336aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2337aae49dd3SBjoern A. Zeeb }
2338aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2339aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2340aae49dd3SBjoern A. Zeeb 
23413d585327SKip Macy void
23423d585327SKip Macy inp_wlock(struct inpcb *inp)
23433d585327SKip Macy {
23443d585327SKip Macy 
23458501a69cSRobert Watson 	INP_WLOCK(inp);
23463d585327SKip Macy }
23473d585327SKip Macy 
23483d585327SKip Macy void
23493d585327SKip Macy inp_wunlock(struct inpcb *inp)
23503d585327SKip Macy {
23513d585327SKip Macy 
23528501a69cSRobert Watson 	INP_WUNLOCK(inp);
23533d585327SKip Macy }
23543d585327SKip Macy 
23553d585327SKip Macy void
23563d585327SKip Macy inp_rlock(struct inpcb *inp)
23573d585327SKip Macy {
23583d585327SKip Macy 
2359a69042a5SRobert Watson 	INP_RLOCK(inp);
23603d585327SKip Macy }
23613d585327SKip Macy 
23623d585327SKip Macy void
23633d585327SKip Macy inp_runlock(struct inpcb *inp)
23643d585327SKip Macy {
23653d585327SKip Macy 
2366a69042a5SRobert Watson 	INP_RUNLOCK(inp);
23673d585327SKip Macy }
23683d585327SKip Macy 
2369c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
23703d585327SKip Macy void
2371e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
23723d585327SKip Macy {
23733d585327SKip Macy 
23748501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
23753d585327SKip Macy }
23763d585327SKip Macy 
23773d585327SKip Macy void
2378e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
23793d585327SKip Macy {
23803d585327SKip Macy 
23813d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
23823d585327SKip Macy }
23833d585327SKip Macy #endif
23843d585327SKip Macy 
23859378e437SKip Macy void
23869378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
23879378e437SKip Macy {
23889378e437SKip Macy 	struct inpcb *inp;
23899378e437SKip Macy 
2390ff9b006dSJulien Charbon 	INP_INFO_WLOCK(&V_tcbinfo);
239197021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
23929378e437SKip Macy 		INP_WLOCK(inp);
23939378e437SKip Macy 		func(inp, arg);
23949378e437SKip Macy 		INP_WUNLOCK(inp);
23959378e437SKip Macy 	}
2396ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(&V_tcbinfo);
23979378e437SKip Macy }
23989378e437SKip Macy 
23999378e437SKip Macy struct socket *
24009378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
24019378e437SKip Macy {
24029378e437SKip Macy 
24039378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
24049378e437SKip Macy 	return (inp->inp_socket);
24059378e437SKip Macy }
24069378e437SKip Macy 
24079378e437SKip Macy struct tcpcb *
24089378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
24099378e437SKip Macy {
24109378e437SKip Macy 
24119378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
24129378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
24139378e437SKip Macy }
24149378e437SKip Macy 
24159378e437SKip Macy int
24169378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
24179378e437SKip Macy {
24189378e437SKip Macy 
24199378e437SKip Macy 	return (inp->inp_ip_tos);
24209378e437SKip Macy }
24219378e437SKip Macy 
24229378e437SKip Macy void
24239378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
24249378e437SKip Macy {
24259378e437SKip Macy 
24269378e437SKip Macy 	inp->inp_ip_tos = val;
24279378e437SKip Macy }
24289378e437SKip Macy 
24299378e437SKip Macy void
2430df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
24319d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
24329378e437SKip Macy {
24339378e437SKip Macy 
24349d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2435df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2436df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
24379378e437SKip Macy 	*lp = inp->inp_lport;
24389378e437SKip Macy 	*fp = inp->inp_fport;
24399378e437SKip Macy }
24409378e437SKip Macy 
2441dd0e6c38SKip Macy struct inpcb *
2442dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2443dd0e6c38SKip Macy {
2444dd0e6c38SKip Macy 
2445dd0e6c38SKip Macy 	return (sotoinpcb(so));
2446dd0e6c38SKip Macy }
2447dd0e6c38SKip Macy 
2448dd0e6c38SKip Macy struct tcpcb *
2449dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2450dd0e6c38SKip Macy {
2451dd0e6c38SKip Macy 
2452dd0e6c38SKip Macy 	return (sototcpcb(so));
2453dd0e6c38SKip Macy }
2454dd0e6c38SKip Macy 
2455cc65eb4eSGleb Smirnoff /*
2456cc65eb4eSGleb Smirnoff  * Create an external-format (``xinpcb'') structure using the information in
2457cc65eb4eSGleb Smirnoff  * the kernel-format in_pcb structure pointed to by inp.  This is done to
2458cc65eb4eSGleb Smirnoff  * reduce the spew of irrelevant information over this interface, to isolate
2459cc65eb4eSGleb Smirnoff  * user code from changes in the kernel structure, and potentially to provide
2460cc65eb4eSGleb Smirnoff  * information-hiding if we decide that some of this information should be
2461cc65eb4eSGleb Smirnoff  * hidden from users.
2462cc65eb4eSGleb Smirnoff  */
2463cc65eb4eSGleb Smirnoff void
2464cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi)
2465cc65eb4eSGleb Smirnoff {
2466cc65eb4eSGleb Smirnoff 
2467cc65eb4eSGleb Smirnoff 	xi->xi_len = sizeof(struct xinpcb);
2468cc65eb4eSGleb Smirnoff 	if (inp->inp_socket)
2469cc65eb4eSGleb Smirnoff 		sotoxsocket(inp->inp_socket, &xi->xi_socket);
2470cc65eb4eSGleb Smirnoff 	else
2471cc65eb4eSGleb Smirnoff 		bzero(&xi->xi_socket, sizeof(struct xsocket));
2472cc65eb4eSGleb Smirnoff 	bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo));
2473cc65eb4eSGleb Smirnoff 	xi->inp_gencnt = inp->inp_gencnt;
2474cc65eb4eSGleb Smirnoff 	xi->inp_ppcb = inp->inp_ppcb;
2475cc65eb4eSGleb Smirnoff 	xi->inp_flow = inp->inp_flow;
2476cc65eb4eSGleb Smirnoff 	xi->inp_flowid = inp->inp_flowid;
2477cc65eb4eSGleb Smirnoff 	xi->inp_flowtype = inp->inp_flowtype;
2478cc65eb4eSGleb Smirnoff 	xi->inp_flags = inp->inp_flags;
2479cc65eb4eSGleb Smirnoff 	xi->inp_flags2 = inp->inp_flags2;
2480cc65eb4eSGleb Smirnoff 	xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket;
2481cc65eb4eSGleb Smirnoff 	xi->in6p_cksum = inp->in6p_cksum;
2482cc65eb4eSGleb Smirnoff 	xi->in6p_hops = inp->in6p_hops;
2483cc65eb4eSGleb Smirnoff 	xi->inp_ip_tos = inp->inp_ip_tos;
2484cc65eb4eSGleb Smirnoff 	xi->inp_vflag = inp->inp_vflag;
2485cc65eb4eSGleb Smirnoff 	xi->inp_ip_ttl = inp->inp_ip_ttl;
2486cc65eb4eSGleb Smirnoff 	xi->inp_ip_p = inp->inp_ip_p;
2487cc65eb4eSGleb Smirnoff 	xi->inp_ip_minttl = inp->inp_ip_minttl;
2488cc65eb4eSGleb Smirnoff }
2489cc65eb4eSGleb Smirnoff 
2490497057eeSRobert Watson #ifdef DDB
2491497057eeSRobert Watson static void
2492497057eeSRobert Watson db_print_indent(int indent)
2493497057eeSRobert Watson {
2494497057eeSRobert Watson 	int i;
2495497057eeSRobert Watson 
2496497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2497497057eeSRobert Watson 		db_printf(" ");
2498497057eeSRobert Watson }
2499497057eeSRobert Watson 
2500497057eeSRobert Watson static void
2501497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2502497057eeSRobert Watson {
2503497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2504497057eeSRobert Watson 
2505497057eeSRobert Watson 	db_print_indent(indent);
2506497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2507497057eeSRobert Watson 
2508497057eeSRobert Watson 	indent += 2;
2509497057eeSRobert Watson 
251003dc38a4SRobert Watson #ifdef INET6
2511dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2512497057eeSRobert Watson 		/* IPv6. */
2513497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2514497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
251583e521ecSBjoern A. Zeeb 	} else
251603dc38a4SRobert Watson #endif
251783e521ecSBjoern A. Zeeb 	{
2518497057eeSRobert Watson 		/* IPv4. */
2519497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2520497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2521497057eeSRobert Watson 	}
2522497057eeSRobert Watson 	db_print_indent(indent);
2523497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2524497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2525497057eeSRobert Watson 	db_print_indent(indent);
2526497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2527497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2528497057eeSRobert Watson }
2529497057eeSRobert Watson 
2530497057eeSRobert Watson static void
2531497057eeSRobert Watson db_print_inpflags(int inp_flags)
2532497057eeSRobert Watson {
2533497057eeSRobert Watson 	int comma;
2534497057eeSRobert Watson 
2535497057eeSRobert Watson 	comma = 0;
2536497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2537497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2538497057eeSRobert Watson 		comma = 1;
2539497057eeSRobert Watson 	}
2540497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2541497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2542497057eeSRobert Watson 		comma = 1;
2543497057eeSRobert Watson 	}
2544497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2545497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2546497057eeSRobert Watson 		comma = 1;
2547497057eeSRobert Watson 	}
2548dce33a45SErmal Luçi 	if (inp_flags & INP_ORIGDSTADDR) {
2549dce33a45SErmal Luçi 		db_printf("%sINP_ORIGDSTADDR", comma ? ", " : "");
2550dce33a45SErmal Luçi 		comma = 1;
2551dce33a45SErmal Luçi 	}
2552497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2553497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2554497057eeSRobert Watson 		comma = 1;
2555497057eeSRobert Watson 	}
2556497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2557497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2558497057eeSRobert Watson 		comma = 1;
2559497057eeSRobert Watson 	}
2560497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2561497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2562497057eeSRobert Watson 		comma = 1;
2563497057eeSRobert Watson 	}
2564497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2565497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2566497057eeSRobert Watson 		comma = 1;
2567497057eeSRobert Watson 	}
2568497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2569497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2570497057eeSRobert Watson 		comma = 1;
2571497057eeSRobert Watson 	}
2572497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2573497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2574497057eeSRobert Watson 		comma = 1;
2575497057eeSRobert Watson 	}
2576497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2577497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2578497057eeSRobert Watson 		comma = 1;
2579497057eeSRobert Watson 	}
2580497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2581497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2582497057eeSRobert Watson 		comma = 1;
2583497057eeSRobert Watson 	}
25843cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
25853cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
25863cca425bSMichael Tuexen 		comma = 1;
25873cca425bSMichael Tuexen 	}
2588497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2589497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2590497057eeSRobert Watson 		comma = 1;
2591497057eeSRobert Watson 	}
2592497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2593497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2594497057eeSRobert Watson 		comma = 1;
2595497057eeSRobert Watson 	}
2596497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2597497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2598497057eeSRobert Watson 		comma = 1;
2599497057eeSRobert Watson 	}
2600497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2601497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2602497057eeSRobert Watson 		comma = 1;
2603497057eeSRobert Watson 	}
2604497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2605497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2606497057eeSRobert Watson 		comma = 1;
2607497057eeSRobert Watson 	}
2608497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2609497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2610497057eeSRobert Watson 		comma = 1;
2611497057eeSRobert Watson 	}
2612497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2613497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2614497057eeSRobert Watson 		comma = 1;
2615497057eeSRobert Watson 	}
2616497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2617497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2618497057eeSRobert Watson 		comma = 1;
2619497057eeSRobert Watson 	}
2620497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2621497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2622497057eeSRobert Watson 		comma = 1;
2623497057eeSRobert Watson 	}
2624ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2625ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2626ad71fe3cSRobert Watson 		comma  = 1;
2627ad71fe3cSRobert Watson 	}
2628ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2629ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2630ad71fe3cSRobert Watson 		comma  = 1;
2631ad71fe3cSRobert Watson 	}
2632ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2633ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2634ad71fe3cSRobert Watson 		comma  = 1;
2635ad71fe3cSRobert Watson 	}
2636ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2637ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2638ad71fe3cSRobert Watson 		comma  = 1;
2639ad71fe3cSRobert Watson 	}
2640497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2641497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2642497057eeSRobert Watson 		comma = 1;
2643497057eeSRobert Watson 	}
2644497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2645497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2646497057eeSRobert Watson 		comma = 1;
2647497057eeSRobert Watson 	}
2648497057eeSRobert Watson }
2649497057eeSRobert Watson 
2650497057eeSRobert Watson static void
2651497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2652497057eeSRobert Watson {
2653497057eeSRobert Watson 	int comma;
2654497057eeSRobert Watson 
2655497057eeSRobert Watson 	comma = 0;
2656497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2657497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2658497057eeSRobert Watson 		comma  = 1;
2659497057eeSRobert Watson 	}
2660497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2661497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2662497057eeSRobert Watson 		comma  = 1;
2663497057eeSRobert Watson 	}
2664497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2665497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2666497057eeSRobert Watson 		comma  = 1;
2667497057eeSRobert Watson 	}
2668497057eeSRobert Watson }
2669497057eeSRobert Watson 
26706d888973SRobert Watson static void
2671497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2672497057eeSRobert Watson {
2673497057eeSRobert Watson 
2674497057eeSRobert Watson 	db_print_indent(indent);
2675497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2676497057eeSRobert Watson 
2677497057eeSRobert Watson 	indent += 2;
2678497057eeSRobert Watson 
2679497057eeSRobert Watson 	db_print_indent(indent);
2680497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2681497057eeSRobert Watson 
2682497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2683497057eeSRobert Watson 
2684497057eeSRobert Watson 	db_print_indent(indent);
2685497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2686497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2687497057eeSRobert Watson 
2688497057eeSRobert Watson 	db_print_indent(indent);
2689497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2690497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2691497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2692497057eeSRobert Watson 	db_printf(")\n");
2693497057eeSRobert Watson 
2694497057eeSRobert Watson 	db_print_indent(indent);
2695497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2696497057eeSRobert Watson 	    inp->inp_vflag);
2697497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2698497057eeSRobert Watson 	db_printf(")\n");
2699497057eeSRobert Watson 
2700497057eeSRobert Watson 	db_print_indent(indent);
2701497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2702497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2703497057eeSRobert Watson 
2704497057eeSRobert Watson 	db_print_indent(indent);
2705497057eeSRobert Watson #ifdef INET6
2706497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2707497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2708497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2709497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2710497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2711497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2712497057eeSRobert Watson 		    inp->in6p_hops);
2713497057eeSRobert Watson 	} else
2714497057eeSRobert Watson #endif
2715497057eeSRobert Watson 	{
2716497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2717497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2718497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2719497057eeSRobert Watson 	}
2720497057eeSRobert Watson 
2721497057eeSRobert Watson 	db_print_indent(indent);
2722497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2723497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2724497057eeSRobert Watson }
2725497057eeSRobert Watson 
2726497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2727497057eeSRobert Watson {
2728497057eeSRobert Watson 	struct inpcb *inp;
2729497057eeSRobert Watson 
2730497057eeSRobert Watson 	if (!have_addr) {
2731497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2732497057eeSRobert Watson 		return;
2733497057eeSRobert Watson 	}
2734497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2735497057eeSRobert Watson 
2736497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2737497057eeSRobert Watson }
273883e521ecSBjoern A. Zeeb #endif /* DDB */
2739f3e7afe2SHans Petter Selasky 
2740f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
2741f3e7afe2SHans Petter Selasky /*
2742f3e7afe2SHans Petter Selasky  * Modify TX rate limit based on the existing "inp->inp_snd_tag",
2743f3e7afe2SHans Petter Selasky  * if any.
2744f3e7afe2SHans Petter Selasky  */
2745f3e7afe2SHans Petter Selasky int
2746f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate)
2747f3e7afe2SHans Petter Selasky {
2748f3e7afe2SHans Petter Selasky 	union if_snd_tag_modify_params params = {
2749f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
2750f3e7afe2SHans Petter Selasky 	};
2751f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2752f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2753f3e7afe2SHans Petter Selasky 	int error;
2754f3e7afe2SHans Petter Selasky 
2755f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2756f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2757f3e7afe2SHans Petter Selasky 		return (EINVAL);
2758f3e7afe2SHans Petter Selasky 
2759f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2760f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2761f3e7afe2SHans Petter Selasky 		return (EINVAL);
2762f3e7afe2SHans Petter Selasky 
2763f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_modify == NULL) {
2764f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2765f3e7afe2SHans Petter Selasky 	} else {
2766f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_modify(mst, &params);
2767f3e7afe2SHans Petter Selasky 	}
2768f3e7afe2SHans Petter Selasky 	return (error);
2769f3e7afe2SHans Petter Selasky }
2770f3e7afe2SHans Petter Selasky 
2771f3e7afe2SHans Petter Selasky /*
2772f3e7afe2SHans Petter Selasky  * Query existing TX rate limit based on the existing
2773f3e7afe2SHans Petter Selasky  * "inp->inp_snd_tag", if any.
2774f3e7afe2SHans Petter Selasky  */
2775f3e7afe2SHans Petter Selasky int
2776f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate)
2777f3e7afe2SHans Petter Selasky {
2778f3e7afe2SHans Petter Selasky 	union if_snd_tag_query_params params = { };
2779f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2780f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2781f3e7afe2SHans Petter Selasky 	int error;
2782f3e7afe2SHans Petter Selasky 
2783f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2784f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2785f3e7afe2SHans Petter Selasky 		return (EINVAL);
2786f3e7afe2SHans Petter Selasky 
2787f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2788f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2789f3e7afe2SHans Petter Selasky 		return (EINVAL);
2790f3e7afe2SHans Petter Selasky 
2791f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL) {
2792f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2793f3e7afe2SHans Petter Selasky 	} else {
2794f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_query(mst, &params);
2795f3e7afe2SHans Petter Selasky 		if (error == 0 &&  p_max_pacing_rate != NULL)
2796f3e7afe2SHans Petter Selasky 			*p_max_pacing_rate = params.rate_limit.max_rate;
2797f3e7afe2SHans Petter Selasky 	}
2798f3e7afe2SHans Petter Selasky 	return (error);
2799f3e7afe2SHans Petter Selasky }
2800f3e7afe2SHans Petter Selasky 
2801f3e7afe2SHans Petter Selasky /*
280295ed5015SHans Petter Selasky  * Query existing TX queue level based on the existing
280395ed5015SHans Petter Selasky  * "inp->inp_snd_tag", if any.
280495ed5015SHans Petter Selasky  */
280595ed5015SHans Petter Selasky int
280695ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level)
280795ed5015SHans Petter Selasky {
280895ed5015SHans Petter Selasky 	union if_snd_tag_query_params params = { };
280995ed5015SHans Petter Selasky 	struct m_snd_tag *mst;
281095ed5015SHans Petter Selasky 	struct ifnet *ifp;
281195ed5015SHans Petter Selasky 	int error;
281295ed5015SHans Petter Selasky 
281395ed5015SHans Petter Selasky 	mst = inp->inp_snd_tag;
281495ed5015SHans Petter Selasky 	if (mst == NULL)
281595ed5015SHans Petter Selasky 		return (EINVAL);
281695ed5015SHans Petter Selasky 
281795ed5015SHans Petter Selasky 	ifp = mst->ifp;
281895ed5015SHans Petter Selasky 	if (ifp == NULL)
281995ed5015SHans Petter Selasky 		return (EINVAL);
282095ed5015SHans Petter Selasky 
282195ed5015SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL)
282295ed5015SHans Petter Selasky 		return (EOPNOTSUPP);
282395ed5015SHans Petter Selasky 
282495ed5015SHans Petter Selasky 	error = ifp->if_snd_tag_query(mst, &params);
282595ed5015SHans Petter Selasky 	if (error == 0 &&  p_txqueue_level != NULL)
282695ed5015SHans Petter Selasky 		*p_txqueue_level = params.rate_limit.queue_level;
282795ed5015SHans Petter Selasky 	return (error);
282895ed5015SHans Petter Selasky }
282995ed5015SHans Petter Selasky 
283095ed5015SHans Petter Selasky /*
2831f3e7afe2SHans Petter Selasky  * Allocate a new TX rate limit send tag from the network interface
2832f3e7afe2SHans Petter Selasky  * given by the "ifp" argument and save it in "inp->inp_snd_tag":
2833f3e7afe2SHans Petter Selasky  */
2834f3e7afe2SHans Petter Selasky int
2835f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp,
2836f3e7afe2SHans Petter Selasky     uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate)
2837f3e7afe2SHans Petter Selasky {
2838f3e7afe2SHans Petter Selasky 	union if_snd_tag_alloc_params params = {
283995ed5015SHans Petter Selasky 		.rate_limit.hdr.type = (max_pacing_rate == -1U) ?
284095ed5015SHans Petter Selasky 		    IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT,
2841f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowid = flowid,
2842f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowtype = flowtype,
2843f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
2844f3e7afe2SHans Petter Selasky 	};
2845f3e7afe2SHans Petter Selasky 	int error;
2846f3e7afe2SHans Petter Selasky 
2847f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
2848f3e7afe2SHans Petter Selasky 
2849f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag != NULL)
2850f3e7afe2SHans Petter Selasky 		return (EINVAL);
2851f3e7afe2SHans Petter Selasky 
2852f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_alloc == NULL) {
2853f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2854f3e7afe2SHans Petter Selasky 	} else {
2855f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_alloc(ifp, &params, &inp->inp_snd_tag);
2856f3e7afe2SHans Petter Selasky 
2857f3e7afe2SHans Petter Selasky 		/*
2858f3e7afe2SHans Petter Selasky 		 * At success increment the refcount on
2859f3e7afe2SHans Petter Selasky 		 * the send tag's network interface:
2860f3e7afe2SHans Petter Selasky 		 */
2861f3e7afe2SHans Petter Selasky 		if (error == 0)
2862f3e7afe2SHans Petter Selasky 			if_ref(inp->inp_snd_tag->ifp);
2863f3e7afe2SHans Petter Selasky 	}
2864f3e7afe2SHans Petter Selasky 	return (error);
2865f3e7afe2SHans Petter Selasky }
2866f3e7afe2SHans Petter Selasky 
2867f3e7afe2SHans Petter Selasky /*
2868f3e7afe2SHans Petter Selasky  * Free an existing TX rate limit tag based on the "inp->inp_snd_tag",
2869f3e7afe2SHans Petter Selasky  * if any:
2870f3e7afe2SHans Petter Selasky  */
2871f3e7afe2SHans Petter Selasky void
2872f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp)
2873f3e7afe2SHans Petter Selasky {
2874f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2875f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2876f3e7afe2SHans Petter Selasky 
2877f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
2878f3e7afe2SHans Petter Selasky 
2879f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2880f3e7afe2SHans Petter Selasky 	inp->inp_snd_tag = NULL;
2881f3e7afe2SHans Petter Selasky 
2882f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2883f3e7afe2SHans Petter Selasky 		return;
2884f3e7afe2SHans Petter Selasky 
2885f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2886f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2887f3e7afe2SHans Petter Selasky 		return;
2888f3e7afe2SHans Petter Selasky 
2889f3e7afe2SHans Petter Selasky 	/*
2890f3e7afe2SHans Petter Selasky 	 * If the device was detached while we still had reference(s)
2891f3e7afe2SHans Petter Selasky 	 * on the ifp, we assume if_snd_tag_free() was replaced with
2892f3e7afe2SHans Petter Selasky 	 * stubs.
2893f3e7afe2SHans Petter Selasky 	 */
2894f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_free(mst);
2895f3e7afe2SHans Petter Selasky 
2896f3e7afe2SHans Petter Selasky 	/* release reference count on network interface */
2897f3e7afe2SHans Petter Selasky 	if_rele(ifp);
2898f3e7afe2SHans Petter Selasky }
2899f3e7afe2SHans Petter Selasky 
2900f3e7afe2SHans Petter Selasky /*
2901f3e7afe2SHans Petter Selasky  * This function should be called when the INP_RATE_LIMIT_CHANGED flag
2902f3e7afe2SHans Petter Selasky  * is set in the fast path and will attach/detach/modify the TX rate
2903f3e7afe2SHans Petter Selasky  * limit send tag based on the socket's so_max_pacing_rate value.
2904f3e7afe2SHans Petter Selasky  */
2905f3e7afe2SHans Petter Selasky void
2906f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb)
2907f3e7afe2SHans Petter Selasky {
2908f3e7afe2SHans Petter Selasky 	struct socket *socket;
2909f3e7afe2SHans Petter Selasky 	uint32_t max_pacing_rate;
2910f3e7afe2SHans Petter Selasky 	bool did_upgrade;
2911f3e7afe2SHans Petter Selasky 	int error;
2912f3e7afe2SHans Petter Selasky 
2913f3e7afe2SHans Petter Selasky 	if (inp == NULL)
2914f3e7afe2SHans Petter Selasky 		return;
2915f3e7afe2SHans Petter Selasky 
2916f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
2917f3e7afe2SHans Petter Selasky 	if (socket == NULL)
2918f3e7afe2SHans Petter Selasky 		return;
2919f3e7afe2SHans Petter Selasky 
2920f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
2921f3e7afe2SHans Petter Selasky 		/*
2922f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
2923f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
2924f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
2925f3e7afe2SHans Petter Selasky 		 * write lock.
2926f3e7afe2SHans Petter Selasky 		 */
2927f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
2928f3e7afe2SHans Petter Selasky 			return;
2929f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
2930f3e7afe2SHans Petter Selasky 	} else {
2931f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
2932f3e7afe2SHans Petter Selasky 	}
2933f3e7afe2SHans Petter Selasky 
2934f3e7afe2SHans Petter Selasky 	/*
2935f3e7afe2SHans Petter Selasky 	 * NOTE: The so_max_pacing_rate value is read unlocked,
2936f3e7afe2SHans Petter Selasky 	 * because atomic updates are not required since the variable
2937f3e7afe2SHans Petter Selasky 	 * is checked at every mbuf we send. It is assumed that the
2938f3e7afe2SHans Petter Selasky 	 * variable read itself will be atomic.
2939f3e7afe2SHans Petter Selasky 	 */
2940f3e7afe2SHans Petter Selasky 	max_pacing_rate = socket->so_max_pacing_rate;
2941f3e7afe2SHans Petter Selasky 
2942f3e7afe2SHans Petter Selasky 	/*
2943f3e7afe2SHans Petter Selasky 	 * NOTE: When attaching to a network interface a reference is
2944f3e7afe2SHans Petter Selasky 	 * made to ensure the network interface doesn't go away until
2945f3e7afe2SHans Petter Selasky 	 * all ratelimit connections are gone. The network interface
2946f3e7afe2SHans Petter Selasky 	 * pointers compared below represent valid network interfaces,
2947f3e7afe2SHans Petter Selasky 	 * except when comparing towards NULL.
2948f3e7afe2SHans Petter Selasky 	 */
2949f3e7afe2SHans Petter Selasky 	if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) {
2950f3e7afe2SHans Petter Selasky 		error = 0;
2951f3e7afe2SHans Petter Selasky 	} else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) {
2952f3e7afe2SHans Petter Selasky 		if (inp->inp_snd_tag != NULL)
2953f3e7afe2SHans Petter Selasky 			in_pcbdetach_txrtlmt(inp);
2954f3e7afe2SHans Petter Selasky 		error = 0;
2955f3e7afe2SHans Petter Selasky 	} else if (inp->inp_snd_tag == NULL) {
2956f3e7afe2SHans Petter Selasky 		/*
2957f3e7afe2SHans Petter Selasky 		 * In order to utilize packet pacing with RSS, we need
2958f3e7afe2SHans Petter Selasky 		 * to wait until there is a valid RSS hash before we
2959f3e7afe2SHans Petter Selasky 		 * can proceed:
2960f3e7afe2SHans Petter Selasky 		 */
2961f3e7afe2SHans Petter Selasky 		if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) {
2962f3e7afe2SHans Petter Selasky 			error = EAGAIN;
2963f3e7afe2SHans Petter Selasky 		} else {
2964f3e7afe2SHans Petter Selasky 			error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb),
2965f3e7afe2SHans Petter Selasky 			    mb->m_pkthdr.flowid, max_pacing_rate);
2966f3e7afe2SHans Petter Selasky 		}
2967f3e7afe2SHans Petter Selasky 	} else {
2968f3e7afe2SHans Petter Selasky 		error = in_pcbmodify_txrtlmt(inp, max_pacing_rate);
2969f3e7afe2SHans Petter Selasky 	}
2970f3e7afe2SHans Petter Selasky 	if (error == 0 || error == EOPNOTSUPP)
2971f3e7afe2SHans Petter Selasky 		inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
2972f3e7afe2SHans Petter Selasky 	if (did_upgrade)
2973f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
2974f3e7afe2SHans Petter Selasky }
2975f3e7afe2SHans Petter Selasky 
2976f3e7afe2SHans Petter Selasky /*
2977f3e7afe2SHans Petter Selasky  * Track route changes for TX rate limiting.
2978f3e7afe2SHans Petter Selasky  */
2979f3e7afe2SHans Petter Selasky void
2980f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp)
2981f3e7afe2SHans Petter Selasky {
2982f3e7afe2SHans Petter Selasky 	struct socket *socket;
2983f3e7afe2SHans Petter Selasky 	bool did_upgrade;
2984f3e7afe2SHans Petter Selasky 
2985f3e7afe2SHans Petter Selasky 	if (inp == NULL)
2986f3e7afe2SHans Petter Selasky 		return;
2987f3e7afe2SHans Petter Selasky 
2988f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
2989f3e7afe2SHans Petter Selasky 	if (socket == NULL)
2990f3e7afe2SHans Petter Selasky 		return;
2991f3e7afe2SHans Petter Selasky 
2992f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag == NULL)
2993f3e7afe2SHans Petter Selasky 		return;
2994f3e7afe2SHans Petter Selasky 
2995f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
2996f3e7afe2SHans Petter Selasky 		/*
2997f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
2998f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
2999f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3000f3e7afe2SHans Petter Selasky 		 * write lock.
3001f3e7afe2SHans Petter Selasky 		 */
3002f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3003f3e7afe2SHans Petter Selasky 			return;
3004f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3005f3e7afe2SHans Petter Selasky 	} else {
3006f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3007f3e7afe2SHans Petter Selasky 	}
3008f3e7afe2SHans Petter Selasky 
3009f3e7afe2SHans Petter Selasky 	/* detach rate limiting */
3010f3e7afe2SHans Petter Selasky 	in_pcbdetach_txrtlmt(inp);
3011f3e7afe2SHans Petter Selasky 
3012f3e7afe2SHans Petter Selasky 	/* make sure new mbuf send tag allocation is made */
3013f3e7afe2SHans Petter Selasky 	inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
3014f3e7afe2SHans Petter Selasky 
3015f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3016f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3017f3e7afe2SHans Petter Selasky }
3018f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */
3019