xref: /freebsd/sys/netinet/in_pcb.c (revision fc21c53f63e8f45a5fec4f7dceb7995cc2fe27ab)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro 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
1322*fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
132384cc0778SGeorge V. Neville-Neil 
1324cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1325df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
132686d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1327a557af22SRobert Watson #ifdef MAC
132830d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1329a557af22SRobert Watson #endif
133079bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
133128696211SRobert Watson 		INP_WUNLOCK(inp);
133228696211SRobert Watson }
133328696211SRobert Watson 
133428696211SRobert Watson /*
1335c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1336c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1337c0a211c5SRobert Watson  *
1338c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1339c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1340c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1341c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1342c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1343c0a211c5SRobert Watson  * in_pcbdetach().
1344c0a211c5SRobert Watson  *
1345c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1346c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
134710702a28SRobert Watson  */
134810702a28SRobert Watson void
134910702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
135010702a28SRobert Watson {
135110702a28SRobert Watson 
13528501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
135310702a28SRobert Watson 
1354fa046d87SRobert Watson 	/*
1355fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1356fa046d87SRobert Watson 	 * the hash lock...?
1357fa046d87SRobert Watson 	 */
1358ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1359111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
136010702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
136110702a28SRobert Watson 
1362fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
136310702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
136410702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
136510702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
136610702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
136710702a28SRobert Watson 			free(phd, M_PCB);
136810702a28SRobert Watson 		}
1369fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1370111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
137152cd27cbSRobert Watson #ifdef PCBGROUP
137252cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
137352cd27cbSRobert Watson #endif
137410702a28SRobert Watson 	}
137510702a28SRobert Watson }
137610702a28SRobert Watson 
137767107f45SBjoern A. Zeeb #ifdef INET
137854d642bbSRobert Watson /*
137954d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
138054d642bbSRobert Watson  */
138126ef6ac4SDon Lewis struct sockaddr *
1382136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
138326ef6ac4SDon Lewis {
138426ef6ac4SDon Lewis 	struct sockaddr_in *sin;
138526ef6ac4SDon Lewis 
13861ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1387a163d034SWarner Losh 		M_WAITOK | M_ZERO);
138826ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
138926ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
139026ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
139126ef6ac4SDon Lewis 	sin->sin_port = port;
139226ef6ac4SDon Lewis 
139326ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
139426ef6ac4SDon Lewis }
139526ef6ac4SDon Lewis 
1396117bcae7SGarrett Wollman int
139754d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1398df8bae1dSRodney W. Grimes {
1399136d4f1cSRobert Watson 	struct inpcb *inp;
140026ef6ac4SDon Lewis 	struct in_addr addr;
140126ef6ac4SDon Lewis 	in_port_t port;
140242fa505bSDavid Greenman 
1403fdc984f7STor Egge 	inp = sotoinpcb(so);
140454d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
14056466b28aSRobert Watson 
1406a69042a5SRobert Watson 	INP_RLOCK(inp);
140726ef6ac4SDon Lewis 	port = inp->inp_lport;
140826ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1409a69042a5SRobert Watson 	INP_RUNLOCK(inp);
141042fa505bSDavid Greenman 
141126ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1412117bcae7SGarrett Wollman 	return 0;
1413df8bae1dSRodney W. Grimes }
1414df8bae1dSRodney W. Grimes 
1415117bcae7SGarrett Wollman int
141654d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1417df8bae1dSRodney W. Grimes {
1418136d4f1cSRobert Watson 	struct inpcb *inp;
141926ef6ac4SDon Lewis 	struct in_addr addr;
142026ef6ac4SDon Lewis 	in_port_t port;
142142fa505bSDavid Greenman 
1422fdc984f7STor Egge 	inp = sotoinpcb(so);
142354d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
14246466b28aSRobert Watson 
1425a69042a5SRobert Watson 	INP_RLOCK(inp);
142626ef6ac4SDon Lewis 	port = inp->inp_fport;
142726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1428a69042a5SRobert Watson 	INP_RUNLOCK(inp);
142942fa505bSDavid Greenman 
143026ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1431117bcae7SGarrett Wollman 	return 0;
1432df8bae1dSRodney W. Grimes }
1433df8bae1dSRodney W. Grimes 
143426f9a767SRodney W. Grimes void
1435136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1436136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1437d1c54148SJesper Skriver {
1438f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1439d1c54148SJesper Skriver 
14403dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1441f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
14428501a69cSRobert Watson 		INP_WLOCK(inp);
1443d1c54148SJesper Skriver #ifdef INET6
1444f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
14458501a69cSRobert Watson 			INP_WUNLOCK(inp);
1446d1c54148SJesper Skriver 			continue;
1447f76fcf6dSJeffrey Hsu 		}
1448d1c54148SJesper Skriver #endif
1449d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1450f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
14518501a69cSRobert Watson 			INP_WUNLOCK(inp);
1452d1c54148SJesper Skriver 			continue;
1453d1c54148SJesper Skriver 		}
14543dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
14558501a69cSRobert Watson 			INP_WUNLOCK(inp);
1456f76fcf6dSJeffrey Hsu 	}
14573dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1458d1c54148SJesper Skriver }
1459d1c54148SJesper Skriver 
1460e43cc4aeSHajimu UMEMOTO void
1461136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1462e43cc4aeSHajimu UMEMOTO {
1463e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1464e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1465e43cc4aeSHajimu UMEMOTO 	int i, gap;
1466e43cc4aeSHajimu UMEMOTO 
1467ff9b006dSJulien Charbon 	INP_INFO_WLOCK(pcbinfo);
1468712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
14698501a69cSRobert Watson 		INP_WLOCK(inp);
1470e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1471e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1472e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1473e43cc4aeSHajimu UMEMOTO 			/*
1474e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1475e43cc4aeSHajimu UMEMOTO 			 * detached.
1476e43cc4aeSHajimu UMEMOTO 			 */
1477e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1478e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1479e43cc4aeSHajimu UMEMOTO 
1480e43cc4aeSHajimu UMEMOTO 			/*
1481e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1482e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1483e43cc4aeSHajimu UMEMOTO 			 */
1484e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1485e43cc4aeSHajimu UMEMOTO 			    i++) {
1486e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1487e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1488e43cc4aeSHajimu UMEMOTO 					gap++;
1489e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1490e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1491e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1492e43cc4aeSHajimu UMEMOTO 			}
1493e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1494e43cc4aeSHajimu UMEMOTO 		}
14958501a69cSRobert Watson 		INP_WUNLOCK(inp);
1496e43cc4aeSHajimu UMEMOTO 	}
1497ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(pcbinfo);
1498e43cc4aeSHajimu UMEMOTO }
1499e43cc4aeSHajimu UMEMOTO 
1500df8bae1dSRodney W. Grimes /*
1501fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1502fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1503c3229e05SDavid Greenman  */
1504d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1505df8bae1dSRodney W. Grimes struct inpcb *
1506136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
150768e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1508df8bae1dSRodney W. Grimes {
1509136d4f1cSRobert Watson 	struct inpcb *inp;
1510d5e8a67eSHajimu UMEMOTO #ifdef INET6
1511d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1512d5e8a67eSHajimu UMEMOTO #else
1513d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1514d5e8a67eSHajimu UMEMOTO #endif
1515d5e8a67eSHajimu UMEMOTO 	int wildcard;
15167bc4aca7SDavid Greenman 
151768e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
151868e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
151968e0d7e0SRobert Watson 
1520fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
15211b73ca0bSSam Leffler 
152268e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1523c3229e05SDavid Greenman 		struct inpcbhead *head;
1524c3229e05SDavid Greenman 		/*
1525c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1526c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1527c3229e05SDavid Greenman 		 */
1528712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1529712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1530fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1531cfa1ca9dSYoshinobu Inoue #ifdef INET6
1532413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1533369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1534cfa1ca9dSYoshinobu Inoue 				continue;
1535cfa1ca9dSYoshinobu Inoue #endif
1536c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1537c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1538c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1539c3229e05SDavid Greenman 				/*
1540413628a7SBjoern A. Zeeb 				 * Found?
1541c3229e05SDavid Greenman 				 */
1542413628a7SBjoern A. Zeeb 				if (cred == NULL ||
15430304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
15440304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1545c3229e05SDavid Greenman 					return (inp);
1546df8bae1dSRodney W. Grimes 			}
1547c3229e05SDavid Greenman 		}
1548c3229e05SDavid Greenman 		/*
1549c3229e05SDavid Greenman 		 * Not found.
1550c3229e05SDavid Greenman 		 */
1551c3229e05SDavid Greenman 		return (NULL);
1552c3229e05SDavid Greenman 	} else {
1553c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1554c3229e05SDavid Greenman 		struct inpcbport *phd;
1555c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1556c3229e05SDavid Greenman 		/*
1557c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1558c3229e05SDavid Greenman 		 *
1559c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1560c3229e05SDavid Greenman 		 * port hash list.
1561c3229e05SDavid Greenman 		 */
1562712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1563712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1564fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1565c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1566c3229e05SDavid Greenman 				break;
1567c3229e05SDavid Greenman 		}
1568c3229e05SDavid Greenman 		if (phd != NULL) {
1569c3229e05SDavid Greenman 			/*
1570c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1571c3229e05SDavid Greenman 			 * fit.
1572c3229e05SDavid Greenman 			 */
157337d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1574c3229e05SDavid Greenman 				wildcard = 0;
1575413628a7SBjoern A. Zeeb 				if (cred != NULL &&
15760304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
15770304c731SJamie Gritton 					cred->cr_prison))
1578413628a7SBjoern A. Zeeb 					continue;
1579cfa1ca9dSYoshinobu Inoue #ifdef INET6
1580413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1581369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1582cfa1ca9dSYoshinobu Inoue 					continue;
1583d5e8a67eSHajimu UMEMOTO 				/*
1584d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1585d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1586d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1587d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1588d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1589d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1590d5e8a67eSHajimu UMEMOTO 				 *
1591d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1592d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1593d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1594d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1595d5e8a67eSHajimu UMEMOTO 				 */
1596d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1597d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1598cfa1ca9dSYoshinobu Inoue #endif
1599c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1600c3229e05SDavid Greenman 					wildcard++;
160115bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
160215bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
160315bd2b43SDavid Greenman 						wildcard++;
160415bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
160515bd2b43SDavid Greenman 						continue;
160615bd2b43SDavid Greenman 				} else {
160715bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
160815bd2b43SDavid Greenman 						wildcard++;
160915bd2b43SDavid Greenman 				}
1610df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1611df8bae1dSRodney W. Grimes 					match = inp;
1612df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1613413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1614df8bae1dSRodney W. Grimes 						break;
1615df8bae1dSRodney W. Grimes 				}
1616df8bae1dSRodney W. Grimes 			}
16173dbdc25cSDavid Greenman 		}
1618df8bae1dSRodney W. Grimes 		return (match);
1619df8bae1dSRodney W. Grimes 	}
1620c3229e05SDavid Greenman }
1621d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
162215bd2b43SDavid Greenman 
162352cd27cbSRobert Watson #ifdef PCBGROUP
162452cd27cbSRobert Watson /*
162552cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
162652cd27cbSRobert Watson  */
162752cd27cbSRobert Watson static struct inpcb *
162852cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
162952cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
163052cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
163152cd27cbSRobert Watson {
163252cd27cbSRobert Watson 	struct inpcbhead *head;
163352cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
163452cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
163552cd27cbSRobert Watson 
163652cd27cbSRobert Watson 	/*
163752cd27cbSRobert Watson 	 * First look for an exact match.
163852cd27cbSRobert Watson 	 */
163952cd27cbSRobert Watson 	tmpinp = NULL;
164052cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
164152cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
164252cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
164352cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
164452cd27cbSRobert Watson #ifdef INET6
164552cd27cbSRobert Watson 		/* XXX inp locking */
164652cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
164752cd27cbSRobert Watson 			continue;
164852cd27cbSRobert Watson #endif
164952cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
165052cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
165152cd27cbSRobert Watson 		    inp->inp_fport == fport &&
165252cd27cbSRobert Watson 		    inp->inp_lport == lport) {
165352cd27cbSRobert Watson 			/*
165452cd27cbSRobert Watson 			 * XXX We should be able to directly return
165552cd27cbSRobert Watson 			 * the inp here, without any checks.
165652cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
165752cd27cbSRobert Watson 			 */
165852cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
165952cd27cbSRobert Watson 				goto found;
166052cd27cbSRobert Watson 			if (tmpinp == NULL)
166152cd27cbSRobert Watson 				tmpinp = inp;
166252cd27cbSRobert Watson 		}
166352cd27cbSRobert Watson 	}
166452cd27cbSRobert Watson 	if (tmpinp != NULL) {
166552cd27cbSRobert Watson 		inp = tmpinp;
166652cd27cbSRobert Watson 		goto found;
166752cd27cbSRobert Watson 	}
166852cd27cbSRobert Watson 
16690a100a6fSAdrian Chadd #ifdef	RSS
16700a100a6fSAdrian Chadd 	/*
16710a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
16720a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
16730a100a6fSAdrian Chadd 	 */
16740a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
16750a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
16760a100a6fSAdrian Chadd #ifdef INET6
16770a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
16780a100a6fSAdrian Chadd #endif
16790a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
16800a100a6fSAdrian Chadd 		struct inpcbhead *head;
16810a100a6fSAdrian Chadd 		int injail;
16820a100a6fSAdrian Chadd 
16830a100a6fSAdrian Chadd 		/*
16840a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
16850a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
16860a100a6fSAdrian Chadd 		 *      2. jailed, wild.
16870a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
16880a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
16890a100a6fSAdrian Chadd 		 */
16900a100a6fSAdrian Chadd 
16910a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
16920a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
16930a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
16940a100a6fSAdrian Chadd #ifdef INET6
16950a100a6fSAdrian Chadd 			/* XXX inp locking */
16960a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
16970a100a6fSAdrian Chadd 				continue;
16980a100a6fSAdrian Chadd #endif
16990a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
17000a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
17010a100a6fSAdrian Chadd 				continue;
17020a100a6fSAdrian Chadd 
17030a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
17040a100a6fSAdrian Chadd 			if (injail) {
17050a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
17060a100a6fSAdrian Chadd 				    &laddr) != 0)
17070a100a6fSAdrian Chadd 					continue;
17080a100a6fSAdrian Chadd 			} else {
17090a100a6fSAdrian Chadd 				if (local_exact != NULL)
17100a100a6fSAdrian Chadd 					continue;
17110a100a6fSAdrian Chadd 			}
17120a100a6fSAdrian Chadd 
17130a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
17140a100a6fSAdrian Chadd 				if (injail)
17150a100a6fSAdrian Chadd 					goto found;
17160a100a6fSAdrian Chadd 				else
17170a100a6fSAdrian Chadd 					local_exact = inp;
17180a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
17190a100a6fSAdrian Chadd #ifdef INET6
17200a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
17210a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
17220a100a6fSAdrian Chadd 					local_wild_mapped = inp;
17230a100a6fSAdrian Chadd 				else
17240a100a6fSAdrian Chadd #endif
17250a100a6fSAdrian Chadd 					if (injail)
17260a100a6fSAdrian Chadd 						jail_wild = inp;
17270a100a6fSAdrian Chadd 					else
17280a100a6fSAdrian Chadd 						local_wild = inp;
17290a100a6fSAdrian Chadd 			}
17300a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
17310a100a6fSAdrian Chadd 
17320a100a6fSAdrian Chadd 		inp = jail_wild;
17330a100a6fSAdrian Chadd 		if (inp == NULL)
17340a100a6fSAdrian Chadd 			inp = local_exact;
17350a100a6fSAdrian Chadd 		if (inp == NULL)
17360a100a6fSAdrian Chadd 			inp = local_wild;
17370a100a6fSAdrian Chadd #ifdef INET6
17380a100a6fSAdrian Chadd 		if (inp == NULL)
17390a100a6fSAdrian Chadd 			inp = local_wild_mapped;
17400a100a6fSAdrian Chadd #endif
17410a100a6fSAdrian Chadd 		if (inp != NULL)
17420a100a6fSAdrian Chadd 			goto found;
17430a100a6fSAdrian Chadd 	}
17440a100a6fSAdrian Chadd #endif
17450a100a6fSAdrian Chadd 
174652cd27cbSRobert Watson 	/*
174752cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
174852cd27cbSRobert Watson 	 */
174952cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
175052cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
175152cd27cbSRobert Watson #ifdef INET6
175252cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
175352cd27cbSRobert Watson #endif
175452cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
175552cd27cbSRobert Watson 		struct inpcbhead *head;
175652cd27cbSRobert Watson 		int injail;
175752cd27cbSRobert Watson 
175852cd27cbSRobert Watson 		/*
175952cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
176052cd27cbSRobert Watson 		 *      1. jailed, non-wild.
176152cd27cbSRobert Watson 		 *      2. jailed, wild.
176252cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
176352cd27cbSRobert Watson 		 *      4. non-jailed, wild.
176452cd27cbSRobert Watson 		 */
176552cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
176652cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
176752cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
176852cd27cbSRobert Watson #ifdef INET6
176952cd27cbSRobert Watson 			/* XXX inp locking */
177052cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
177152cd27cbSRobert Watson 				continue;
177252cd27cbSRobert Watson #endif
177352cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
177452cd27cbSRobert Watson 			    inp->inp_lport != lport)
177552cd27cbSRobert Watson 				continue;
177652cd27cbSRobert Watson 
177752cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
177852cd27cbSRobert Watson 			if (injail) {
177952cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
178052cd27cbSRobert Watson 				    &laddr) != 0)
178152cd27cbSRobert Watson 					continue;
178252cd27cbSRobert Watson 			} else {
178352cd27cbSRobert Watson 				if (local_exact != NULL)
178452cd27cbSRobert Watson 					continue;
178552cd27cbSRobert Watson 			}
178652cd27cbSRobert Watson 
178752cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
178852cd27cbSRobert Watson 				if (injail)
178952cd27cbSRobert Watson 					goto found;
179052cd27cbSRobert Watson 				else
179152cd27cbSRobert Watson 					local_exact = inp;
179252cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
179352cd27cbSRobert Watson #ifdef INET6
179452cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
179552cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
179652cd27cbSRobert Watson 					local_wild_mapped = inp;
179752cd27cbSRobert Watson 				else
179883e521ecSBjoern A. Zeeb #endif
179952cd27cbSRobert Watson 					if (injail)
180052cd27cbSRobert Watson 						jail_wild = inp;
180152cd27cbSRobert Watson 					else
180252cd27cbSRobert Watson 						local_wild = inp;
180352cd27cbSRobert Watson 			}
180452cd27cbSRobert Watson 		} /* LIST_FOREACH */
180552cd27cbSRobert Watson 		inp = jail_wild;
180652cd27cbSRobert Watson 		if (inp == NULL)
180752cd27cbSRobert Watson 			inp = local_exact;
180852cd27cbSRobert Watson 		if (inp == NULL)
180952cd27cbSRobert Watson 			inp = local_wild;
181052cd27cbSRobert Watson #ifdef INET6
181152cd27cbSRobert Watson 		if (inp == NULL)
181252cd27cbSRobert Watson 			inp = local_wild_mapped;
181383e521ecSBjoern A. Zeeb #endif
181452cd27cbSRobert Watson 		if (inp != NULL)
181552cd27cbSRobert Watson 			goto found;
181652cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
181752cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
181852cd27cbSRobert Watson 	return (NULL);
181952cd27cbSRobert Watson 
182052cd27cbSRobert Watson found:
182152cd27cbSRobert Watson 	in_pcbref(inp);
182252cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
182352cd27cbSRobert Watson 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
182452cd27cbSRobert Watson 		INP_WLOCK(inp);
182552cd27cbSRobert Watson 		if (in_pcbrele_wlocked(inp))
182652cd27cbSRobert Watson 			return (NULL);
182752cd27cbSRobert Watson 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
182852cd27cbSRobert Watson 		INP_RLOCK(inp);
182952cd27cbSRobert Watson 		if (in_pcbrele_rlocked(inp))
183052cd27cbSRobert Watson 			return (NULL);
183152cd27cbSRobert Watson 	} else
183252cd27cbSRobert Watson 		panic("%s: locking bug", __func__);
183352cd27cbSRobert Watson 	return (inp);
183452cd27cbSRobert Watson }
183552cd27cbSRobert Watson #endif /* PCBGROUP */
183652cd27cbSRobert Watson 
183715bd2b43SDavid Greenman /*
1838fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1839fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1840fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
184115bd2b43SDavid Greenman  */
1842fa046d87SRobert Watson static struct inpcb *
1843fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
184468e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1845136d4f1cSRobert Watson     struct ifnet *ifp)
184615bd2b43SDavid Greenman {
184715bd2b43SDavid Greenman 	struct inpcbhead *head;
1848413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
184915bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
185015bd2b43SDavid Greenman 
185168e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
185268e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
185368e0d7e0SRobert Watson 
1854fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1855602cc7f1SRobert Watson 
185615bd2b43SDavid Greenman 	/*
185715bd2b43SDavid Greenman 	 * First look for an exact match.
185815bd2b43SDavid Greenman 	 */
1859413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1860712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1861712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1862fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1863cfa1ca9dSYoshinobu Inoue #ifdef INET6
1864413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1865369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1866cfa1ca9dSYoshinobu Inoue 			continue;
1867cfa1ca9dSYoshinobu Inoue #endif
18686d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1869ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1870ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1871413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1872413628a7SBjoern A. Zeeb 			/*
1873413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1874413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1875413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1876413628a7SBjoern A. Zeeb 			 */
18770304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1878c3229e05SDavid Greenman 				return (inp);
1879413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1880413628a7SBjoern A. Zeeb 				tmpinp = inp;
1881c3229e05SDavid Greenman 		}
1882413628a7SBjoern A. Zeeb 	}
1883413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1884413628a7SBjoern A. Zeeb 		return (tmpinp);
1885e3fd5ffdSRobert Watson 
1886e3fd5ffdSRobert Watson 	/*
1887e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1888e3fd5ffdSRobert Watson 	 */
188968e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1890413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1891e3fd5ffdSRobert Watson #ifdef INET6
1892cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1893e3fd5ffdSRobert Watson #endif
1894413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1895413628a7SBjoern A. Zeeb 		int injail;
1896413628a7SBjoern A. Zeeb 
1897413628a7SBjoern A. Zeeb 		/*
1898413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1899413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1900413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1901413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1902413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1903413628a7SBjoern A. Zeeb 		 */
19046d6a026bSDavid Greenman 
1905712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1906712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1907fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1908cfa1ca9dSYoshinobu Inoue #ifdef INET6
1909413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1910369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1911cfa1ca9dSYoshinobu Inoue 				continue;
1912cfa1ca9dSYoshinobu Inoue #endif
1913413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1914413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1915413628a7SBjoern A. Zeeb 				continue;
1916413628a7SBjoern A. Zeeb 
19170304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1918413628a7SBjoern A. Zeeb 			if (injail) {
1919b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1920b89e82ddSJamie Gritton 				    &laddr) != 0)
1921413628a7SBjoern A. Zeeb 					continue;
1922413628a7SBjoern A. Zeeb 			} else {
1923413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1924413628a7SBjoern A. Zeeb 					continue;
1925413628a7SBjoern A. Zeeb 			}
1926413628a7SBjoern A. Zeeb 
1927413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1928413628a7SBjoern A. Zeeb 				if (injail)
1929c3229e05SDavid Greenman 					return (inp);
1930413628a7SBjoern A. Zeeb 				else
1931413628a7SBjoern A. Zeeb 					local_exact = inp;
1932413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1933e3fd5ffdSRobert Watson #ifdef INET6
1934413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
19355cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
1936cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
1937cfa1ca9dSYoshinobu Inoue 				else
193883e521ecSBjoern A. Zeeb #endif
1939413628a7SBjoern A. Zeeb 					if (injail)
1940413628a7SBjoern A. Zeeb 						jail_wild = inp;
1941413628a7SBjoern A. Zeeb 					else
19426d6a026bSDavid Greenman 						local_wild = inp;
19436d6a026bSDavid Greenman 			}
1944413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
1945413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
1946413628a7SBjoern A. Zeeb 			return (jail_wild);
1947413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
1948413628a7SBjoern A. Zeeb 			return (local_exact);
1949413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
1950c3229e05SDavid Greenman 			return (local_wild);
1951413628a7SBjoern A. Zeeb #ifdef INET6
1952413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
1953413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
195483e521ecSBjoern A. Zeeb #endif
195568e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1956413628a7SBjoern A. Zeeb 
19576d6a026bSDavid Greenman 	return (NULL);
195815bd2b43SDavid Greenman }
1959fa046d87SRobert Watson 
1960fa046d87SRobert Watson /*
1961fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1962fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
1963fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
1964fa046d87SRobert Watson  */
1965fa046d87SRobert Watson static struct inpcb *
1966fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1967fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1968fa046d87SRobert Watson     struct ifnet *ifp)
1969fa046d87SRobert Watson {
1970fa046d87SRobert Watson 	struct inpcb *inp;
1971fa046d87SRobert Watson 
1972fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
1973fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1974fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1975fa046d87SRobert Watson 	if (inp != NULL) {
1976fa046d87SRobert Watson 		in_pcbref(inp);
1977fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1978fa046d87SRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1979fa046d87SRobert Watson 			INP_WLOCK(inp);
1980fa046d87SRobert Watson 			if (in_pcbrele_wlocked(inp))
1981fa046d87SRobert Watson 				return (NULL);
1982fa046d87SRobert Watson 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1983fa046d87SRobert Watson 			INP_RLOCK(inp);
1984fa046d87SRobert Watson 			if (in_pcbrele_rlocked(inp))
1985fa046d87SRobert Watson 				return (NULL);
1986fa046d87SRobert Watson 		} else
1987fa046d87SRobert Watson 			panic("%s: locking bug", __func__);
1988fa046d87SRobert Watson 	} else
1989fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
1990fa046d87SRobert Watson 	return (inp);
1991fa046d87SRobert Watson }
1992fa046d87SRobert Watson 
1993fa046d87SRobert Watson /*
1994d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1995d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
199652cd27cbSRobert Watson  *
199752cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
1998fa046d87SRobert Watson  */
1999fa046d87SRobert Watson struct inpcb *
2000fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
2001fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
2002fa046d87SRobert Watson {
20037527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
200452cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
200552cd27cbSRobert Watson #endif
2006fa046d87SRobert Watson 
2007fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2008fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2009fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2010fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2011fa046d87SRobert Watson 
20127527624eSRobert Watson 	/*
20137527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
20147527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
20157527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
20167527624eSRobert Watson 	 * in software.
20177527624eSRobert Watson 	 *
20187527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
20197527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
20207527624eSRobert Watson 	 * in software.
20217527624eSRobert Watson 	 */
20227527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
202352cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
202452cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
202552cd27cbSRobert Watson 		    fport);
202652cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
202752cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
202852cd27cbSRobert Watson 	}
202952cd27cbSRobert Watson #endif
2030fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2031fa046d87SRobert Watson 	    lookupflags, ifp));
2032fa046d87SRobert Watson }
2033d3c1f003SRobert Watson 
2034d3c1f003SRobert Watson struct inpcb *
2035d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2036d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2037d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2038d3c1f003SRobert Watson {
203952cd27cbSRobert Watson #ifdef PCBGROUP
204052cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
204152cd27cbSRobert Watson #endif
2042d3c1f003SRobert Watson 
2043d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2044d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2045d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2046d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2047d3c1f003SRobert Watson 
204852cd27cbSRobert Watson #ifdef PCBGROUP
20497527624eSRobert Watson 	/*
20507527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
20517527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
20527527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
20537527624eSRobert Watson 	 * using RSS.
20547527624eSRobert Watson 	 *
20557527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
20567527624eSRobert Watson 	 */
20577527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
20587527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
205952cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
206052cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
206152cd27cbSRobert Watson 		if (pcbgroup != NULL)
206252cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
206352cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
20647527624eSRobert Watson #ifndef RSS
206552cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
206652cd27cbSRobert Watson 		    fport);
206752cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
206852cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
20697527624eSRobert Watson #endif
207052cd27cbSRobert Watson 	}
207152cd27cbSRobert Watson #endif
2072d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2073d3c1f003SRobert Watson 	    lookupflags, ifp));
2074d3c1f003SRobert Watson }
207567107f45SBjoern A. Zeeb #endif /* INET */
207615bd2b43SDavid Greenman 
20777bc4aca7SDavid Greenman /*
2078c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
20797bc4aca7SDavid Greenman  */
208052cd27cbSRobert Watson static int
208152cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
208215bd2b43SDavid Greenman {
2083c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2084c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2085c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2086c3229e05SDavid Greenman 	struct inpcbport *phd;
2087cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
208815bd2b43SDavid Greenman 
20898501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2090fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2091fa046d87SRobert Watson 
2092111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2093111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2094602cc7f1SRobert Watson 
2095cfa1ca9dSYoshinobu Inoue #ifdef INET6
2096cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
20971b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2098cfa1ca9dSYoshinobu Inoue 	else
209983e521ecSBjoern A. Zeeb #endif
2100cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2101cfa1ca9dSYoshinobu Inoue 
2102712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2103712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
210415bd2b43SDavid Greenman 
2105712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2106712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2107c3229e05SDavid Greenman 
2108c3229e05SDavid Greenman 	/*
2109c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2110c3229e05SDavid Greenman 	 */
2111fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2112c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2113c3229e05SDavid Greenman 			break;
2114c3229e05SDavid Greenman 	}
2115c3229e05SDavid Greenman 	/*
2116c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2117c3229e05SDavid Greenman 	 */
2118c3229e05SDavid Greenman 	if (phd == NULL) {
21191ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2120c3229e05SDavid Greenman 		if (phd == NULL) {
2121c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2122c3229e05SDavid Greenman 		}
2123c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2124c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2125c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2126c3229e05SDavid Greenman 	}
2127c3229e05SDavid Greenman 	inp->inp_phd = phd;
2128c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2129c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2130111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
213152cd27cbSRobert Watson #ifdef PCBGROUP
213252cd27cbSRobert Watson 	if (do_pcbgroup_update)
213352cd27cbSRobert Watson 		in_pcbgroup_update(inp);
213452cd27cbSRobert Watson #endif
2135c3229e05SDavid Greenman 	return (0);
213615bd2b43SDavid Greenman }
213715bd2b43SDavid Greenman 
2138c3229e05SDavid Greenman /*
213952cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
214052cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
214152cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
214252cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
214352cd27cbSRobert Watson  * pcbgroup until later.
214452cd27cbSRobert Watson  *
214552cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
214652cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
214752cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
214852cd27cbSRobert Watson  */
214952cd27cbSRobert Watson int
215052cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
215152cd27cbSRobert Watson {
215252cd27cbSRobert Watson 
215352cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
215452cd27cbSRobert Watson }
215552cd27cbSRobert Watson 
215652cd27cbSRobert Watson int
215752cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
215852cd27cbSRobert Watson {
215952cd27cbSRobert Watson 
216052cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
216152cd27cbSRobert Watson }
216252cd27cbSRobert Watson 
216352cd27cbSRobert Watson /*
2164c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2165c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2166c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2167c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2168c3229e05SDavid Greenman  */
216915bd2b43SDavid Greenman void
2170d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
217115bd2b43SDavid Greenman {
217259daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
217315bd2b43SDavid Greenman 	struct inpcbhead *head;
2174cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
217515bd2b43SDavid Greenman 
21768501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2177fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2178fa046d87SRobert Watson 
2179111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2180111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2181602cc7f1SRobert Watson 
2182cfa1ca9dSYoshinobu Inoue #ifdef INET6
2183cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
21841b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2185cfa1ca9dSYoshinobu Inoue 	else
218683e521ecSBjoern A. Zeeb #endif
2187cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2188cfa1ca9dSYoshinobu Inoue 
2189712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2190712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
219115bd2b43SDavid Greenman 
2192c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
219315bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
219452cd27cbSRobert Watson 
219552cd27cbSRobert Watson #ifdef PCBGROUP
219652cd27cbSRobert Watson 	if (m != NULL)
219752cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
219852cd27cbSRobert Watson 	else
219952cd27cbSRobert Watson 		in_pcbgroup_update(inp);
220052cd27cbSRobert Watson #endif
2201c3229e05SDavid Greenman }
2202c3229e05SDavid Greenman 
2203d3c1f003SRobert Watson void
2204d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2205d3c1f003SRobert Watson {
2206d3c1f003SRobert Watson 
2207d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2208d3c1f003SRobert Watson }
2209d3c1f003SRobert Watson 
2210c3229e05SDavid Greenman /*
2211c3229e05SDavid Greenman  * Remove PCB from various lists.
2212c3229e05SDavid Greenman  */
22136d888973SRobert Watson static void
2214136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2215c3229e05SDavid Greenman {
221659daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
221759daba27SSam Leffler 
2218ff9b006dSJulien Charbon #ifdef INVARIANTS
2219ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
2220ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
2221ff9b006dSJulien Charbon 	} else {
222259daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
2223ff9b006dSJulien Charbon 	}
2224ff9b006dSJulien Charbon #endif
2225ff9b006dSJulien Charbon 
22268501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2227ff9b006dSJulien Charbon 	INP_LIST_WLOCK_ASSERT(pcbinfo);
222859daba27SSam Leffler 
222959daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2230111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2231c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2232c3229e05SDavid Greenman 
2233fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2234c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2235c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2236fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2237c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2238c3229e05SDavid Greenman 			free(phd, M_PCB);
2239c3229e05SDavid Greenman 		}
2240fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2241111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2242c3229e05SDavid Greenman 	}
2243c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
224459daba27SSam Leffler 	pcbinfo->ipi_count--;
224552cd27cbSRobert Watson #ifdef PCBGROUP
224652cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
224752cd27cbSRobert Watson #endif
224815bd2b43SDavid Greenman }
224975c13541SPoul-Henning Kamp 
2250a557af22SRobert Watson /*
225184cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
225284cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
225384cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
225484cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
225584cc0778SGeorge V. Neville-Neil  */
225684cc0778SGeorge V. Neville-Neil void
225784cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
225884cc0778SGeorge V. Neville-Neil {
225984cc0778SGeorge V. Neville-Neil 
2260*fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
226184cc0778SGeorge V. Neville-Neil 	return;
226284cc0778SGeorge V. Neville-Neil }
226384cc0778SGeorge V. Neville-Neil 
226484cc0778SGeorge V. Neville-Neil /*
2265a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2266a557af22SRobert Watson  * label change into the in_pcb for the socket.
2267a557af22SRobert Watson  */
2268a557af22SRobert Watson void
2269136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2270a557af22SRobert Watson {
2271a557af22SRobert Watson #ifdef MAC
2272a557af22SRobert Watson 	struct inpcb *inp;
2273a557af22SRobert Watson 
22744c7c478dSRobert Watson 	inp = sotoinpcb(so);
22754c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2276602cc7f1SRobert Watson 
22778501a69cSRobert Watson 	INP_WLOCK(inp);
2278310e7cebSRobert Watson 	SOCK_LOCK(so);
2279a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2280310e7cebSRobert Watson 	SOCK_UNLOCK(so);
22818501a69cSRobert Watson 	INP_WUNLOCK(inp);
2282a557af22SRobert Watson #endif
2283a557af22SRobert Watson }
22845f311da2SMike Silbersack 
22855f311da2SMike Silbersack /*
2286ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2287ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2288ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2289ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2290ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
22915f311da2SMike Silbersack  */
2292aae49dd3SBjoern A. Zeeb static void
2293136d4f1cSRobert Watson ipport_tick(void *xtp)
22945f311da2SMike Silbersack {
22958b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2296ad3a630fSRobert Watson 
22975ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
22988b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
22998b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
23008b615593SMarko Zec 		if (V_ipport_tcpallocs <=
23018b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2302603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2303603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2304ad3a630fSRobert Watson 		} else
2305603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2306603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
23078b615593SMarko Zec 		CURVNET_RESTORE();
23088b615593SMarko Zec 	}
23095ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
23105f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
23115f311da2SMike Silbersack }
2312497057eeSRobert Watson 
2313aae49dd3SBjoern A. Zeeb static void
2314aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2315aae49dd3SBjoern A. Zeeb {
2316aae49dd3SBjoern A. Zeeb 
2317aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2318aae49dd3SBjoern A. Zeeb }
2319aae49dd3SBjoern A. Zeeb 
2320aae49dd3SBjoern A. Zeeb /*
2321aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2322aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2323aae49dd3SBjoern A. Zeeb  */
2324aae49dd3SBjoern A. Zeeb static void
2325aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2326aae49dd3SBjoern A. Zeeb {
2327aae49dd3SBjoern A. Zeeb 
2328aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2329fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2330aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2331aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2332aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2333aae49dd3SBjoern A. Zeeb }
2334aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2335aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2336aae49dd3SBjoern A. Zeeb 
23373d585327SKip Macy void
23383d585327SKip Macy inp_wlock(struct inpcb *inp)
23393d585327SKip Macy {
23403d585327SKip Macy 
23418501a69cSRobert Watson 	INP_WLOCK(inp);
23423d585327SKip Macy }
23433d585327SKip Macy 
23443d585327SKip Macy void
23453d585327SKip Macy inp_wunlock(struct inpcb *inp)
23463d585327SKip Macy {
23473d585327SKip Macy 
23488501a69cSRobert Watson 	INP_WUNLOCK(inp);
23493d585327SKip Macy }
23503d585327SKip Macy 
23513d585327SKip Macy void
23523d585327SKip Macy inp_rlock(struct inpcb *inp)
23533d585327SKip Macy {
23543d585327SKip Macy 
2355a69042a5SRobert Watson 	INP_RLOCK(inp);
23563d585327SKip Macy }
23573d585327SKip Macy 
23583d585327SKip Macy void
23593d585327SKip Macy inp_runlock(struct inpcb *inp)
23603d585327SKip Macy {
23613d585327SKip Macy 
2362a69042a5SRobert Watson 	INP_RUNLOCK(inp);
23633d585327SKip Macy }
23643d585327SKip Macy 
2365c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
23663d585327SKip Macy void
2367e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
23683d585327SKip Macy {
23693d585327SKip Macy 
23708501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
23713d585327SKip Macy }
23723d585327SKip Macy 
23733d585327SKip Macy void
2374e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
23753d585327SKip Macy {
23763d585327SKip Macy 
23773d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
23783d585327SKip Macy }
23793d585327SKip Macy #endif
23803d585327SKip Macy 
23819378e437SKip Macy void
23829378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
23839378e437SKip Macy {
23849378e437SKip Macy 	struct inpcb *inp;
23859378e437SKip Macy 
2386ff9b006dSJulien Charbon 	INP_INFO_WLOCK(&V_tcbinfo);
238797021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
23889378e437SKip Macy 		INP_WLOCK(inp);
23899378e437SKip Macy 		func(inp, arg);
23909378e437SKip Macy 		INP_WUNLOCK(inp);
23919378e437SKip Macy 	}
2392ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(&V_tcbinfo);
23939378e437SKip Macy }
23949378e437SKip Macy 
23959378e437SKip Macy struct socket *
23969378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
23979378e437SKip Macy {
23989378e437SKip Macy 
23999378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
24009378e437SKip Macy 	return (inp->inp_socket);
24019378e437SKip Macy }
24029378e437SKip Macy 
24039378e437SKip Macy struct tcpcb *
24049378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
24059378e437SKip Macy {
24069378e437SKip Macy 
24079378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
24089378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
24099378e437SKip Macy }
24109378e437SKip Macy 
24119378e437SKip Macy int
24129378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
24139378e437SKip Macy {
24149378e437SKip Macy 
24159378e437SKip Macy 	return (inp->inp_ip_tos);
24169378e437SKip Macy }
24179378e437SKip Macy 
24189378e437SKip Macy void
24199378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
24209378e437SKip Macy {
24219378e437SKip Macy 
24229378e437SKip Macy 	inp->inp_ip_tos = val;
24239378e437SKip Macy }
24249378e437SKip Macy 
24259378e437SKip Macy void
2426df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
24279d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
24289378e437SKip Macy {
24299378e437SKip Macy 
24309d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2431df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2432df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
24339378e437SKip Macy 	*lp = inp->inp_lport;
24349378e437SKip Macy 	*fp = inp->inp_fport;
24359378e437SKip Macy }
24369378e437SKip Macy 
2437dd0e6c38SKip Macy struct inpcb *
2438dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2439dd0e6c38SKip Macy {
2440dd0e6c38SKip Macy 
2441dd0e6c38SKip Macy 	return (sotoinpcb(so));
2442dd0e6c38SKip Macy }
2443dd0e6c38SKip Macy 
2444dd0e6c38SKip Macy struct tcpcb *
2445dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2446dd0e6c38SKip Macy {
2447dd0e6c38SKip Macy 
2448dd0e6c38SKip Macy 	return (sototcpcb(so));
2449dd0e6c38SKip Macy }
2450dd0e6c38SKip Macy 
2451cc65eb4eSGleb Smirnoff /*
2452cc65eb4eSGleb Smirnoff  * Create an external-format (``xinpcb'') structure using the information in
2453cc65eb4eSGleb Smirnoff  * the kernel-format in_pcb structure pointed to by inp.  This is done to
2454cc65eb4eSGleb Smirnoff  * reduce the spew of irrelevant information over this interface, to isolate
2455cc65eb4eSGleb Smirnoff  * user code from changes in the kernel structure, and potentially to provide
2456cc65eb4eSGleb Smirnoff  * information-hiding if we decide that some of this information should be
2457cc65eb4eSGleb Smirnoff  * hidden from users.
2458cc65eb4eSGleb Smirnoff  */
2459cc65eb4eSGleb Smirnoff void
2460cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi)
2461cc65eb4eSGleb Smirnoff {
2462cc65eb4eSGleb Smirnoff 
2463cc65eb4eSGleb Smirnoff 	xi->xi_len = sizeof(struct xinpcb);
2464cc65eb4eSGleb Smirnoff 	if (inp->inp_socket)
2465cc65eb4eSGleb Smirnoff 		sotoxsocket(inp->inp_socket, &xi->xi_socket);
2466cc65eb4eSGleb Smirnoff 	else
2467cc65eb4eSGleb Smirnoff 		bzero(&xi->xi_socket, sizeof(struct xsocket));
2468cc65eb4eSGleb Smirnoff 	bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo));
2469cc65eb4eSGleb Smirnoff 	xi->inp_gencnt = inp->inp_gencnt;
2470cc65eb4eSGleb Smirnoff 	xi->inp_ppcb = inp->inp_ppcb;
2471cc65eb4eSGleb Smirnoff 	xi->inp_flow = inp->inp_flow;
2472cc65eb4eSGleb Smirnoff 	xi->inp_flowid = inp->inp_flowid;
2473cc65eb4eSGleb Smirnoff 	xi->inp_flowtype = inp->inp_flowtype;
2474cc65eb4eSGleb Smirnoff 	xi->inp_flags = inp->inp_flags;
2475cc65eb4eSGleb Smirnoff 	xi->inp_flags2 = inp->inp_flags2;
2476cc65eb4eSGleb Smirnoff 	xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket;
2477cc65eb4eSGleb Smirnoff 	xi->in6p_cksum = inp->in6p_cksum;
2478cc65eb4eSGleb Smirnoff 	xi->in6p_hops = inp->in6p_hops;
2479cc65eb4eSGleb Smirnoff 	xi->inp_ip_tos = inp->inp_ip_tos;
2480cc65eb4eSGleb Smirnoff 	xi->inp_vflag = inp->inp_vflag;
2481cc65eb4eSGleb Smirnoff 	xi->inp_ip_ttl = inp->inp_ip_ttl;
2482cc65eb4eSGleb Smirnoff 	xi->inp_ip_p = inp->inp_ip_p;
2483cc65eb4eSGleb Smirnoff 	xi->inp_ip_minttl = inp->inp_ip_minttl;
2484cc65eb4eSGleb Smirnoff }
2485cc65eb4eSGleb Smirnoff 
2486497057eeSRobert Watson #ifdef DDB
2487497057eeSRobert Watson static void
2488497057eeSRobert Watson db_print_indent(int indent)
2489497057eeSRobert Watson {
2490497057eeSRobert Watson 	int i;
2491497057eeSRobert Watson 
2492497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2493497057eeSRobert Watson 		db_printf(" ");
2494497057eeSRobert Watson }
2495497057eeSRobert Watson 
2496497057eeSRobert Watson static void
2497497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2498497057eeSRobert Watson {
2499497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2500497057eeSRobert Watson 
2501497057eeSRobert Watson 	db_print_indent(indent);
2502497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2503497057eeSRobert Watson 
2504497057eeSRobert Watson 	indent += 2;
2505497057eeSRobert Watson 
250603dc38a4SRobert Watson #ifdef INET6
2507dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2508497057eeSRobert Watson 		/* IPv6. */
2509497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2510497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
251183e521ecSBjoern A. Zeeb 	} else
251203dc38a4SRobert Watson #endif
251383e521ecSBjoern A. Zeeb 	{
2514497057eeSRobert Watson 		/* IPv4. */
2515497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2516497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2517497057eeSRobert Watson 	}
2518497057eeSRobert Watson 	db_print_indent(indent);
2519497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2520497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2521497057eeSRobert Watson 	db_print_indent(indent);
2522497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2523497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2524497057eeSRobert Watson }
2525497057eeSRobert Watson 
2526497057eeSRobert Watson static void
2527497057eeSRobert Watson db_print_inpflags(int inp_flags)
2528497057eeSRobert Watson {
2529497057eeSRobert Watson 	int comma;
2530497057eeSRobert Watson 
2531497057eeSRobert Watson 	comma = 0;
2532497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2533497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2534497057eeSRobert Watson 		comma = 1;
2535497057eeSRobert Watson 	}
2536497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2537497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2538497057eeSRobert Watson 		comma = 1;
2539497057eeSRobert Watson 	}
2540497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2541497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2542497057eeSRobert Watson 		comma = 1;
2543497057eeSRobert Watson 	}
2544dce33a45SErmal Luçi 	if (inp_flags & INP_ORIGDSTADDR) {
2545dce33a45SErmal Luçi 		db_printf("%sINP_ORIGDSTADDR", comma ? ", " : "");
2546dce33a45SErmal Luçi 		comma = 1;
2547dce33a45SErmal Luçi 	}
2548497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2549497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2550497057eeSRobert Watson 		comma = 1;
2551497057eeSRobert Watson 	}
2552497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2553497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2554497057eeSRobert Watson 		comma = 1;
2555497057eeSRobert Watson 	}
2556497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2557497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2558497057eeSRobert Watson 		comma = 1;
2559497057eeSRobert Watson 	}
2560497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2561497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2562497057eeSRobert Watson 		comma = 1;
2563497057eeSRobert Watson 	}
2564497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2565497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2566497057eeSRobert Watson 		comma = 1;
2567497057eeSRobert Watson 	}
2568497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2569497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2570497057eeSRobert Watson 		comma = 1;
2571497057eeSRobert Watson 	}
2572497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2573497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2574497057eeSRobert Watson 		comma = 1;
2575497057eeSRobert Watson 	}
2576497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2577497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2578497057eeSRobert Watson 		comma = 1;
2579497057eeSRobert Watson 	}
25803cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
25813cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
25823cca425bSMichael Tuexen 		comma = 1;
25833cca425bSMichael Tuexen 	}
2584497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2585497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2586497057eeSRobert Watson 		comma = 1;
2587497057eeSRobert Watson 	}
2588497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2589497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2590497057eeSRobert Watson 		comma = 1;
2591497057eeSRobert Watson 	}
2592497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2593497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2594497057eeSRobert Watson 		comma = 1;
2595497057eeSRobert Watson 	}
2596497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2597497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2598497057eeSRobert Watson 		comma = 1;
2599497057eeSRobert Watson 	}
2600497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2601497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2602497057eeSRobert Watson 		comma = 1;
2603497057eeSRobert Watson 	}
2604497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2605497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2606497057eeSRobert Watson 		comma = 1;
2607497057eeSRobert Watson 	}
2608497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2609497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2610497057eeSRobert Watson 		comma = 1;
2611497057eeSRobert Watson 	}
2612497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2613497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2614497057eeSRobert Watson 		comma = 1;
2615497057eeSRobert Watson 	}
2616497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2617497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2618497057eeSRobert Watson 		comma = 1;
2619497057eeSRobert Watson 	}
2620ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2621ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2622ad71fe3cSRobert Watson 		comma  = 1;
2623ad71fe3cSRobert Watson 	}
2624ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2625ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2626ad71fe3cSRobert Watson 		comma  = 1;
2627ad71fe3cSRobert Watson 	}
2628ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2629ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2630ad71fe3cSRobert Watson 		comma  = 1;
2631ad71fe3cSRobert Watson 	}
2632ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2633ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2634ad71fe3cSRobert Watson 		comma  = 1;
2635ad71fe3cSRobert Watson 	}
2636497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2637497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2638497057eeSRobert Watson 		comma = 1;
2639497057eeSRobert Watson 	}
2640497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2641497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2642497057eeSRobert Watson 		comma = 1;
2643497057eeSRobert Watson 	}
2644497057eeSRobert Watson }
2645497057eeSRobert Watson 
2646497057eeSRobert Watson static void
2647497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2648497057eeSRobert Watson {
2649497057eeSRobert Watson 	int comma;
2650497057eeSRobert Watson 
2651497057eeSRobert Watson 	comma = 0;
2652497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2653497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2654497057eeSRobert Watson 		comma  = 1;
2655497057eeSRobert Watson 	}
2656497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2657497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2658497057eeSRobert Watson 		comma  = 1;
2659497057eeSRobert Watson 	}
2660497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2661497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2662497057eeSRobert Watson 		comma  = 1;
2663497057eeSRobert Watson 	}
2664497057eeSRobert Watson }
2665497057eeSRobert Watson 
26666d888973SRobert Watson static void
2667497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2668497057eeSRobert Watson {
2669497057eeSRobert Watson 
2670497057eeSRobert Watson 	db_print_indent(indent);
2671497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2672497057eeSRobert Watson 
2673497057eeSRobert Watson 	indent += 2;
2674497057eeSRobert Watson 
2675497057eeSRobert Watson 	db_print_indent(indent);
2676497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2677497057eeSRobert Watson 
2678497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2679497057eeSRobert Watson 
2680497057eeSRobert Watson 	db_print_indent(indent);
2681497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2682497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2683497057eeSRobert Watson 
2684497057eeSRobert Watson 	db_print_indent(indent);
2685497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2686497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2687497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2688497057eeSRobert Watson 	db_printf(")\n");
2689497057eeSRobert Watson 
2690497057eeSRobert Watson 	db_print_indent(indent);
2691497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2692497057eeSRobert Watson 	    inp->inp_vflag);
2693497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2694497057eeSRobert Watson 	db_printf(")\n");
2695497057eeSRobert Watson 
2696497057eeSRobert Watson 	db_print_indent(indent);
2697497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2698497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2699497057eeSRobert Watson 
2700497057eeSRobert Watson 	db_print_indent(indent);
2701497057eeSRobert Watson #ifdef INET6
2702497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2703497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2704497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2705497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2706497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2707497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2708497057eeSRobert Watson 		    inp->in6p_hops);
2709497057eeSRobert Watson 	} else
2710497057eeSRobert Watson #endif
2711497057eeSRobert Watson 	{
2712497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2713497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2714497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2715497057eeSRobert Watson 	}
2716497057eeSRobert Watson 
2717497057eeSRobert Watson 	db_print_indent(indent);
2718497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2719497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2720497057eeSRobert Watson }
2721497057eeSRobert Watson 
2722497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2723497057eeSRobert Watson {
2724497057eeSRobert Watson 	struct inpcb *inp;
2725497057eeSRobert Watson 
2726497057eeSRobert Watson 	if (!have_addr) {
2727497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2728497057eeSRobert Watson 		return;
2729497057eeSRobert Watson 	}
2730497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2731497057eeSRobert Watson 
2732497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2733497057eeSRobert Watson }
273483e521ecSBjoern A. Zeeb #endif /* DDB */
2735f3e7afe2SHans Petter Selasky 
2736f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
2737f3e7afe2SHans Petter Selasky /*
2738f3e7afe2SHans Petter Selasky  * Modify TX rate limit based on the existing "inp->inp_snd_tag",
2739f3e7afe2SHans Petter Selasky  * if any.
2740f3e7afe2SHans Petter Selasky  */
2741f3e7afe2SHans Petter Selasky int
2742f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate)
2743f3e7afe2SHans Petter Selasky {
2744f3e7afe2SHans Petter Selasky 	union if_snd_tag_modify_params params = {
2745f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
2746f3e7afe2SHans Petter Selasky 	};
2747f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2748f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2749f3e7afe2SHans Petter Selasky 	int error;
2750f3e7afe2SHans Petter Selasky 
2751f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2752f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2753f3e7afe2SHans Petter Selasky 		return (EINVAL);
2754f3e7afe2SHans Petter Selasky 
2755f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2756f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2757f3e7afe2SHans Petter Selasky 		return (EINVAL);
2758f3e7afe2SHans Petter Selasky 
2759f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_modify == NULL) {
2760f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2761f3e7afe2SHans Petter Selasky 	} else {
2762f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_modify(mst, &params);
2763f3e7afe2SHans Petter Selasky 	}
2764f3e7afe2SHans Petter Selasky 	return (error);
2765f3e7afe2SHans Petter Selasky }
2766f3e7afe2SHans Petter Selasky 
2767f3e7afe2SHans Petter Selasky /*
2768f3e7afe2SHans Petter Selasky  * Query existing TX rate limit based on the existing
2769f3e7afe2SHans Petter Selasky  * "inp->inp_snd_tag", if any.
2770f3e7afe2SHans Petter Selasky  */
2771f3e7afe2SHans Petter Selasky int
2772f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate)
2773f3e7afe2SHans Petter Selasky {
2774f3e7afe2SHans Petter Selasky 	union if_snd_tag_query_params params = { };
2775f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2776f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2777f3e7afe2SHans Petter Selasky 	int error;
2778f3e7afe2SHans Petter Selasky 
2779f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2780f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2781f3e7afe2SHans Petter Selasky 		return (EINVAL);
2782f3e7afe2SHans Petter Selasky 
2783f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2784f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2785f3e7afe2SHans Petter Selasky 		return (EINVAL);
2786f3e7afe2SHans Petter Selasky 
2787f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL) {
2788f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2789f3e7afe2SHans Petter Selasky 	} else {
2790f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_query(mst, &params);
2791f3e7afe2SHans Petter Selasky 		if (error == 0 &&  p_max_pacing_rate != NULL)
2792f3e7afe2SHans Petter Selasky 			*p_max_pacing_rate = params.rate_limit.max_rate;
2793f3e7afe2SHans Petter Selasky 	}
2794f3e7afe2SHans Petter Selasky 	return (error);
2795f3e7afe2SHans Petter Selasky }
2796f3e7afe2SHans Petter Selasky 
2797f3e7afe2SHans Petter Selasky /*
279895ed5015SHans Petter Selasky  * Query existing TX queue level based on the existing
279995ed5015SHans Petter Selasky  * "inp->inp_snd_tag", if any.
280095ed5015SHans Petter Selasky  */
280195ed5015SHans Petter Selasky int
280295ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level)
280395ed5015SHans Petter Selasky {
280495ed5015SHans Petter Selasky 	union if_snd_tag_query_params params = { };
280595ed5015SHans Petter Selasky 	struct m_snd_tag *mst;
280695ed5015SHans Petter Selasky 	struct ifnet *ifp;
280795ed5015SHans Petter Selasky 	int error;
280895ed5015SHans Petter Selasky 
280995ed5015SHans Petter Selasky 	mst = inp->inp_snd_tag;
281095ed5015SHans Petter Selasky 	if (mst == NULL)
281195ed5015SHans Petter Selasky 		return (EINVAL);
281295ed5015SHans Petter Selasky 
281395ed5015SHans Petter Selasky 	ifp = mst->ifp;
281495ed5015SHans Petter Selasky 	if (ifp == NULL)
281595ed5015SHans Petter Selasky 		return (EINVAL);
281695ed5015SHans Petter Selasky 
281795ed5015SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL)
281895ed5015SHans Petter Selasky 		return (EOPNOTSUPP);
281995ed5015SHans Petter Selasky 
282095ed5015SHans Petter Selasky 	error = ifp->if_snd_tag_query(mst, &params);
282195ed5015SHans Petter Selasky 	if (error == 0 &&  p_txqueue_level != NULL)
282295ed5015SHans Petter Selasky 		*p_txqueue_level = params.rate_limit.queue_level;
282395ed5015SHans Petter Selasky 	return (error);
282495ed5015SHans Petter Selasky }
282595ed5015SHans Petter Selasky 
282695ed5015SHans Petter Selasky /*
2827f3e7afe2SHans Petter Selasky  * Allocate a new TX rate limit send tag from the network interface
2828f3e7afe2SHans Petter Selasky  * given by the "ifp" argument and save it in "inp->inp_snd_tag":
2829f3e7afe2SHans Petter Selasky  */
2830f3e7afe2SHans Petter Selasky int
2831f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp,
2832f3e7afe2SHans Petter Selasky     uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate)
2833f3e7afe2SHans Petter Selasky {
2834f3e7afe2SHans Petter Selasky 	union if_snd_tag_alloc_params params = {
283595ed5015SHans Petter Selasky 		.rate_limit.hdr.type = (max_pacing_rate == -1U) ?
283695ed5015SHans Petter Selasky 		    IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT,
2837f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowid = flowid,
2838f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowtype = flowtype,
2839f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
2840f3e7afe2SHans Petter Selasky 	};
2841f3e7afe2SHans Petter Selasky 	int error;
2842f3e7afe2SHans Petter Selasky 
2843f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
2844f3e7afe2SHans Petter Selasky 
2845f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag != NULL)
2846f3e7afe2SHans Petter Selasky 		return (EINVAL);
2847f3e7afe2SHans Petter Selasky 
2848f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_alloc == NULL) {
2849f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2850f3e7afe2SHans Petter Selasky 	} else {
2851f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_alloc(ifp, &params, &inp->inp_snd_tag);
2852f3e7afe2SHans Petter Selasky 
2853f3e7afe2SHans Petter Selasky 		/*
2854f3e7afe2SHans Petter Selasky 		 * At success increment the refcount on
2855f3e7afe2SHans Petter Selasky 		 * the send tag's network interface:
2856f3e7afe2SHans Petter Selasky 		 */
2857f3e7afe2SHans Petter Selasky 		if (error == 0)
2858f3e7afe2SHans Petter Selasky 			if_ref(inp->inp_snd_tag->ifp);
2859f3e7afe2SHans Petter Selasky 	}
2860f3e7afe2SHans Petter Selasky 	return (error);
2861f3e7afe2SHans Petter Selasky }
2862f3e7afe2SHans Petter Selasky 
2863f3e7afe2SHans Petter Selasky /*
2864f3e7afe2SHans Petter Selasky  * Free an existing TX rate limit tag based on the "inp->inp_snd_tag",
2865f3e7afe2SHans Petter Selasky  * if any:
2866f3e7afe2SHans Petter Selasky  */
2867f3e7afe2SHans Petter Selasky void
2868f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp)
2869f3e7afe2SHans Petter Selasky {
2870f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2871f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2872f3e7afe2SHans Petter Selasky 
2873f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
2874f3e7afe2SHans Petter Selasky 
2875f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2876f3e7afe2SHans Petter Selasky 	inp->inp_snd_tag = NULL;
2877f3e7afe2SHans Petter Selasky 
2878f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2879f3e7afe2SHans Petter Selasky 		return;
2880f3e7afe2SHans Petter Selasky 
2881f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2882f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2883f3e7afe2SHans Petter Selasky 		return;
2884f3e7afe2SHans Petter Selasky 
2885f3e7afe2SHans Petter Selasky 	/*
2886f3e7afe2SHans Petter Selasky 	 * If the device was detached while we still had reference(s)
2887f3e7afe2SHans Petter Selasky 	 * on the ifp, we assume if_snd_tag_free() was replaced with
2888f3e7afe2SHans Petter Selasky 	 * stubs.
2889f3e7afe2SHans Petter Selasky 	 */
2890f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_free(mst);
2891f3e7afe2SHans Petter Selasky 
2892f3e7afe2SHans Petter Selasky 	/* release reference count on network interface */
2893f3e7afe2SHans Petter Selasky 	if_rele(ifp);
2894f3e7afe2SHans Petter Selasky }
2895f3e7afe2SHans Petter Selasky 
2896f3e7afe2SHans Petter Selasky /*
2897f3e7afe2SHans Petter Selasky  * This function should be called when the INP_RATE_LIMIT_CHANGED flag
2898f3e7afe2SHans Petter Selasky  * is set in the fast path and will attach/detach/modify the TX rate
2899f3e7afe2SHans Petter Selasky  * limit send tag based on the socket's so_max_pacing_rate value.
2900f3e7afe2SHans Petter Selasky  */
2901f3e7afe2SHans Petter Selasky void
2902f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb)
2903f3e7afe2SHans Petter Selasky {
2904f3e7afe2SHans Petter Selasky 	struct socket *socket;
2905f3e7afe2SHans Petter Selasky 	uint32_t max_pacing_rate;
2906f3e7afe2SHans Petter Selasky 	bool did_upgrade;
2907f3e7afe2SHans Petter Selasky 	int error;
2908f3e7afe2SHans Petter Selasky 
2909f3e7afe2SHans Petter Selasky 	if (inp == NULL)
2910f3e7afe2SHans Petter Selasky 		return;
2911f3e7afe2SHans Petter Selasky 
2912f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
2913f3e7afe2SHans Petter Selasky 	if (socket == NULL)
2914f3e7afe2SHans Petter Selasky 		return;
2915f3e7afe2SHans Petter Selasky 
2916f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
2917f3e7afe2SHans Petter Selasky 		/*
2918f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
2919f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
2920f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
2921f3e7afe2SHans Petter Selasky 		 * write lock.
2922f3e7afe2SHans Petter Selasky 		 */
2923f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
2924f3e7afe2SHans Petter Selasky 			return;
2925f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
2926f3e7afe2SHans Petter Selasky 	} else {
2927f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
2928f3e7afe2SHans Petter Selasky 	}
2929f3e7afe2SHans Petter Selasky 
2930f3e7afe2SHans Petter Selasky 	/*
2931f3e7afe2SHans Petter Selasky 	 * NOTE: The so_max_pacing_rate value is read unlocked,
2932f3e7afe2SHans Petter Selasky 	 * because atomic updates are not required since the variable
2933f3e7afe2SHans Petter Selasky 	 * is checked at every mbuf we send. It is assumed that the
2934f3e7afe2SHans Petter Selasky 	 * variable read itself will be atomic.
2935f3e7afe2SHans Petter Selasky 	 */
2936f3e7afe2SHans Petter Selasky 	max_pacing_rate = socket->so_max_pacing_rate;
2937f3e7afe2SHans Petter Selasky 
2938f3e7afe2SHans Petter Selasky 	/*
2939f3e7afe2SHans Petter Selasky 	 * NOTE: When attaching to a network interface a reference is
2940f3e7afe2SHans Petter Selasky 	 * made to ensure the network interface doesn't go away until
2941f3e7afe2SHans Petter Selasky 	 * all ratelimit connections are gone. The network interface
2942f3e7afe2SHans Petter Selasky 	 * pointers compared below represent valid network interfaces,
2943f3e7afe2SHans Petter Selasky 	 * except when comparing towards NULL.
2944f3e7afe2SHans Petter Selasky 	 */
2945f3e7afe2SHans Petter Selasky 	if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) {
2946f3e7afe2SHans Petter Selasky 		error = 0;
2947f3e7afe2SHans Petter Selasky 	} else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) {
2948f3e7afe2SHans Petter Selasky 		if (inp->inp_snd_tag != NULL)
2949f3e7afe2SHans Petter Selasky 			in_pcbdetach_txrtlmt(inp);
2950f3e7afe2SHans Petter Selasky 		error = 0;
2951f3e7afe2SHans Petter Selasky 	} else if (inp->inp_snd_tag == NULL) {
2952f3e7afe2SHans Petter Selasky 		/*
2953f3e7afe2SHans Petter Selasky 		 * In order to utilize packet pacing with RSS, we need
2954f3e7afe2SHans Petter Selasky 		 * to wait until there is a valid RSS hash before we
2955f3e7afe2SHans Petter Selasky 		 * can proceed:
2956f3e7afe2SHans Petter Selasky 		 */
2957f3e7afe2SHans Petter Selasky 		if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) {
2958f3e7afe2SHans Petter Selasky 			error = EAGAIN;
2959f3e7afe2SHans Petter Selasky 		} else {
2960f3e7afe2SHans Petter Selasky 			error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb),
2961f3e7afe2SHans Petter Selasky 			    mb->m_pkthdr.flowid, max_pacing_rate);
2962f3e7afe2SHans Petter Selasky 		}
2963f3e7afe2SHans Petter Selasky 	} else {
2964f3e7afe2SHans Petter Selasky 		error = in_pcbmodify_txrtlmt(inp, max_pacing_rate);
2965f3e7afe2SHans Petter Selasky 	}
2966f3e7afe2SHans Petter Selasky 	if (error == 0 || error == EOPNOTSUPP)
2967f3e7afe2SHans Petter Selasky 		inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
2968f3e7afe2SHans Petter Selasky 	if (did_upgrade)
2969f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
2970f3e7afe2SHans Petter Selasky }
2971f3e7afe2SHans Petter Selasky 
2972f3e7afe2SHans Petter Selasky /*
2973f3e7afe2SHans Petter Selasky  * Track route changes for TX rate limiting.
2974f3e7afe2SHans Petter Selasky  */
2975f3e7afe2SHans Petter Selasky void
2976f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp)
2977f3e7afe2SHans Petter Selasky {
2978f3e7afe2SHans Petter Selasky 	struct socket *socket;
2979f3e7afe2SHans Petter Selasky 	bool did_upgrade;
2980f3e7afe2SHans Petter Selasky 
2981f3e7afe2SHans Petter Selasky 	if (inp == NULL)
2982f3e7afe2SHans Petter Selasky 		return;
2983f3e7afe2SHans Petter Selasky 
2984f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
2985f3e7afe2SHans Petter Selasky 	if (socket == NULL)
2986f3e7afe2SHans Petter Selasky 		return;
2987f3e7afe2SHans Petter Selasky 
2988f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag == NULL)
2989f3e7afe2SHans Petter Selasky 		return;
2990f3e7afe2SHans Petter Selasky 
2991f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
2992f3e7afe2SHans Petter Selasky 		/*
2993f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
2994f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
2995f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
2996f3e7afe2SHans Petter Selasky 		 * write lock.
2997f3e7afe2SHans Petter Selasky 		 */
2998f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
2999f3e7afe2SHans Petter Selasky 			return;
3000f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3001f3e7afe2SHans Petter Selasky 	} else {
3002f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3003f3e7afe2SHans Petter Selasky 	}
3004f3e7afe2SHans Petter Selasky 
3005f3e7afe2SHans Petter Selasky 	/* detach rate limiting */
3006f3e7afe2SHans Petter Selasky 	in_pcbdetach_txrtlmt(inp);
3007f3e7afe2SHans Petter Selasky 
3008f3e7afe2SHans Petter Selasky 	/* make sure new mbuf send tag allocation is made */
3009f3e7afe2SHans Petter Selasky 	inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
3010f3e7afe2SHans Petter Selasky 
3011f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3012f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3013f3e7afe2SHans Petter Selasky }
3014f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */
3015