xref: /freebsd/sys/netinet/in_pcb.c (revision 3ee9c3c4ebf1d1edb3337cef1a840bcfe383ceb8)
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>
61*3ee9c3c4SRandall Stewart #include <sys/smp.h>
62df8bae1dSRodney W. Grimes #include <sys/socket.h>
63df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
64f3e7afe2SHans Petter Selasky #include <sys/sockio.h>
65acd3428bSRobert Watson #include <sys/priv.h>
66df8bae1dSRodney W. Grimes #include <sys/proc.h>
6779bdc6e5SRobert Watson #include <sys/refcount.h>
6875c13541SPoul-Henning Kamp #include <sys/jail.h>
69101f9fc8SPeter Wemm #include <sys/kernel.h>
70101f9fc8SPeter Wemm #include <sys/sysctl.h>
718781d8e9SBruce Evans 
72497057eeSRobert Watson #ifdef DDB
73497057eeSRobert Watson #include <ddb/ddb.h>
74497057eeSRobert Watson #endif
75497057eeSRobert Watson 
7669c2d429SJeff Roberson #include <vm/uma.h>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <net/if.h>
7976039bc8SGleb Smirnoff #include <net/if_var.h>
80cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
816d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h>
82df8bae1dSRodney W. Grimes #include <net/route.h>
83b2bdc62aSAdrian Chadd #include <net/rss_config.h>
84530c0060SRobert Watson #include <net/vnet.h>
85df8bae1dSRodney W. Grimes 
8667107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6)
87df8bae1dSRodney W. Grimes #include <netinet/in.h>
88df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
89df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
90340c35deSJonathan Lemon #include <netinet/tcp_var.h>
91*3ee9c3c4SRandall Stewart #ifdef TCPHPTS
92*3ee9c3c4SRandall Stewart #include <netinet/tcp_hpts.h>
93*3ee9c3c4SRandall Stewart #endif
945f311da2SMike Silbersack #include <netinet/udp.h>
955f311da2SMike Silbersack #include <netinet/udp_var.h>
9667107f45SBjoern A. Zeeb #endif
9767107f45SBjoern A. Zeeb #ifdef INET
9867107f45SBjoern A. Zeeb #include <netinet/in_var.h>
9967107f45SBjoern A. Zeeb #endif
100cfa1ca9dSYoshinobu Inoue #ifdef INET6
101cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
102efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
10367107f45SBjoern A. Zeeb #include <netinet6/in6_var.h>
10467107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h>
105cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
106cfa1ca9dSYoshinobu Inoue 
107fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
108b9234fafSSam Leffler 
109aed55708SRobert Watson #include <security/mac/mac_framework.h>
110aed55708SRobert Watson 
111aae49dd3SBjoern A. Zeeb static struct callout	ipport_tick_callout;
112aae49dd3SBjoern A. Zeeb 
113101f9fc8SPeter Wemm /*
114101f9fc8SPeter Wemm  * These configure the range of local port addresses assigned to
115101f9fc8SPeter Wemm  * "unspecified" outgoing connections/packets/whatever.
116101f9fc8SPeter Wemm  */
117eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
118eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
119eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
120eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
123101f9fc8SPeter Wemm 
124b0d22693SCrist J. Clark /*
125b0d22693SCrist J. Clark  * Reserved ports accessible only to root. There are significant
126b0d22693SCrist J. Clark  * security considerations that must be accounted for when changing these,
127b0d22693SCrist J. Clark  * but the security benefits can be great. Please be careful.
128b0d22693SCrist J. Clark  */
129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
130eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow);
131b0d22693SCrist J. Clark 
1325f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */
133eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
134eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
135eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
136eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
137eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs);
1383e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount);
139eddfbb76SRobert Watson 
1401e77c105SRobert Watson #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
1416ac48b74SMike Silbersack 
142d2025bd0SBjoern A. Zeeb static void	in_pcbremlists(struct inpcb *inp);
143d2025bd0SBjoern A. Zeeb #ifdef INET
144fa046d87SRobert Watson static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
145fa046d87SRobert Watson 			    struct in_addr faddr, u_int fport_arg,
146fa046d87SRobert Watson 			    struct in_addr laddr, u_int lport_arg,
147fa046d87SRobert Watson 			    int lookupflags, struct ifnet *ifp);
14867107f45SBjoern A. Zeeb 
149bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \
150bbd42ad0SPeter Wemm 	if ((var) < (min)) { (var) = (min); } \
151bbd42ad0SPeter Wemm 	else if ((var) > (max)) { (var) = (max); }
152bbd42ad0SPeter Wemm 
153bbd42ad0SPeter Wemm static int
15482d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
155bbd42ad0SPeter Wemm {
15630a4ab08SBruce Evans 	int error;
15730a4ab08SBruce Evans 
158f6dfe47aSMarko Zec 	error = sysctl_handle_int(oidp, arg1, arg2, req);
15930a4ab08SBruce Evans 	if (error == 0) {
160603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
161603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
162603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
163603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
164603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
165603724d3SBjoern A. Zeeb 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
166bbd42ad0SPeter Wemm 	}
16730a4ab08SBruce Evans 	return (error);
168bbd42ad0SPeter Wemm }
169bbd42ad0SPeter Wemm 
170bbd42ad0SPeter Wemm #undef RANGECHK
171bbd42ad0SPeter Wemm 
1726472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
1736472ac3dSEd Schouten     "IP Ports");
17433b3ac06SPeter Wemm 
1756df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
1766df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1776df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
1786df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
1796df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1806df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
1816df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
1826df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1836df8a710SGleb Smirnoff 	&VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
1846df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
1856df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1866df8a710SGleb Smirnoff 	&VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
1876df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
1886df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1896df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
1906df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
1916df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
1926df8a710SGleb Smirnoff 	&VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
1946df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
1956df8a710SGleb Smirnoff 	&VNET_NAME(ipport_reservedhigh), 0, "");
1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
197eddfbb76SRobert Watson 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
1996df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
200eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
2026df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
203eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
2046ee79c59SMaxim Konovalov 	"allocations before switching to a sequental one");
2056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
2066df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW,
207eddfbb76SRobert Watson 	&VNET_NAME(ipport_randomtime), 0,
2088b615593SMarko Zec 	"Minimum time to keep sequental port "
2096ee79c59SMaxim Konovalov 	"allocation before switching to a random one");
21083e521ecSBjoern A. Zeeb #endif /* INET */
2110312fbe9SPoul-Henning Kamp 
212c3229e05SDavid Greenman /*
213c3229e05SDavid Greenman  * in_pcb.c: manage the Protocol Control Blocks.
214c3229e05SDavid Greenman  *
215de35559fSRobert Watson  * NOTE: It is assumed that most of these functions will be called with
216de35559fSRobert Watson  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
217de35559fSRobert Watson  * functions often modify hash chains or addresses in pcbs.
218c3229e05SDavid Greenman  */
219c3229e05SDavid Greenman 
220c3229e05SDavid Greenman /*
221cc487c16SGleb Smirnoff  * Different protocols initialize their inpcbs differently - giving
222cc487c16SGleb Smirnoff  * different name to the lock.  But they all are disposed the same.
223cc487c16SGleb Smirnoff  */
224cc487c16SGleb Smirnoff static void
225cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size)
226cc487c16SGleb Smirnoff {
227cc487c16SGleb Smirnoff 	struct inpcb *inp = mem;
228cc487c16SGleb Smirnoff 
229cc487c16SGleb Smirnoff 	INP_LOCK_DESTROY(inp);
230cc487c16SGleb Smirnoff }
231cc487c16SGleb Smirnoff 
232cc487c16SGleb Smirnoff /*
2339bcd427bSRobert Watson  * Initialize an inpcbinfo -- we should be able to reduce the number of
2349bcd427bSRobert Watson  * arguments in time.
2359bcd427bSRobert Watson  */
2369bcd427bSRobert Watson void
2379bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
2389bcd427bSRobert Watson     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
239cc487c16SGleb Smirnoff     char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields)
2409bcd427bSRobert Watson {
2419bcd427bSRobert Watson 
2429bcd427bSRobert Watson 	INP_INFO_LOCK_INIT(pcbinfo, name);
243fa046d87SRobert Watson 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
244ff9b006dSJulien Charbon 	INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
2459bcd427bSRobert Watson #ifdef VIMAGE
2469bcd427bSRobert Watson 	pcbinfo->ipi_vnet = curvnet;
2479bcd427bSRobert Watson #endif
2489bcd427bSRobert Watson 	pcbinfo->ipi_listhead = listhead;
2499bcd427bSRobert Watson 	LIST_INIT(pcbinfo->ipi_listhead);
250fa046d87SRobert Watson 	pcbinfo->ipi_count = 0;
2519bcd427bSRobert Watson 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
2529bcd427bSRobert Watson 	    &pcbinfo->ipi_hashmask);
2539bcd427bSRobert Watson 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
2549bcd427bSRobert Watson 	    &pcbinfo->ipi_porthashmask);
25552cd27cbSRobert Watson #ifdef PCBGROUP
25652cd27cbSRobert Watson 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
25752cd27cbSRobert Watson #endif
2589bcd427bSRobert Watson 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
259cc487c16SGleb Smirnoff 	    NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0);
2609bcd427bSRobert Watson 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
2616acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(pcbinfo->ipi_zone,
2626acd596eSPawel Jakub Dawidek 	    "kern.ipc.maxsockets limit reached");
2639bcd427bSRobert Watson }
2649bcd427bSRobert Watson 
2659bcd427bSRobert Watson /*
2669bcd427bSRobert Watson  * Destroy an inpcbinfo.
2679bcd427bSRobert Watson  */
2689bcd427bSRobert Watson void
2699bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
2709bcd427bSRobert Watson {
2719bcd427bSRobert Watson 
272fa046d87SRobert Watson 	KASSERT(pcbinfo->ipi_count == 0,
273fa046d87SRobert Watson 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
274fa046d87SRobert Watson 
2759bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
2769bcd427bSRobert Watson 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
2779bcd427bSRobert Watson 	    pcbinfo->ipi_porthashmask);
27852cd27cbSRobert Watson #ifdef PCBGROUP
27952cd27cbSRobert Watson 	in_pcbgroup_destroy(pcbinfo);
28052cd27cbSRobert Watson #endif
2819bcd427bSRobert Watson 	uma_zdestroy(pcbinfo->ipi_zone);
282ff9b006dSJulien Charbon 	INP_LIST_LOCK_DESTROY(pcbinfo);
283fa046d87SRobert Watson 	INP_HASH_LOCK_DESTROY(pcbinfo);
2849bcd427bSRobert Watson 	INP_INFO_LOCK_DESTROY(pcbinfo);
2859bcd427bSRobert Watson }
2869bcd427bSRobert Watson 
2879bcd427bSRobert Watson /*
288c3229e05SDavid Greenman  * Allocate a PCB and associate it with the socket.
289d915b280SStephan Uphoff  * On success return with the PCB locked.
290c3229e05SDavid Greenman  */
291df8bae1dSRodney W. Grimes int
292d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
293df8bae1dSRodney W. Grimes {
294136d4f1cSRobert Watson 	struct inpcb *inp;
29513cf67f3SHajimu UMEMOTO 	int error;
296a557af22SRobert Watson 
297ff9b006dSJulien Charbon #ifdef INVARIANTS
298ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
299ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
300ff9b006dSJulien Charbon 	} else {
30159daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
302ff9b006dSJulien Charbon 	}
303ff9b006dSJulien Charbon #endif
304ff9b006dSJulien Charbon 
305a557af22SRobert Watson 	error = 0;
306d915b280SStephan Uphoff 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
307df8bae1dSRodney W. Grimes 	if (inp == NULL)
308df8bae1dSRodney W. Grimes 		return (ENOBUFS);
3096a6cefacSGleb Smirnoff 	bzero(&inp->inp_start_zero, inp_zero_size);
31015bd2b43SDavid Greenman 	inp->inp_pcbinfo = pcbinfo;
311df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
31286d02c5cSBjoern A. Zeeb 	inp->inp_cred = crhold(so->so_cred);
3138b07e49aSJulian Elischer 	inp->inp_inc.inc_fibnum = so->so_fibnum;
314a557af22SRobert Watson #ifdef MAC
31530d239bcSRobert Watson 	error = mac_inpcb_init(inp, M_NOWAIT);
316a557af22SRobert Watson 	if (error != 0)
317a557af22SRobert Watson 		goto out;
31830d239bcSRobert Watson 	mac_inpcb_create(so, inp);
319a557af22SRobert Watson #endif
320fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
321fcf59617SAndrey V. Elsukov 	error = ipsec_init_pcbpolicy(inp);
3220bffde27SRobert Watson 	if (error != 0) {
3230bffde27SRobert Watson #ifdef MAC
3240bffde27SRobert Watson 		mac_inpcb_destroy(inp);
3250bffde27SRobert Watson #endif
326a557af22SRobert Watson 		goto out;
3270bffde27SRobert Watson 	}
328b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
329e3fd5ffdSRobert Watson #ifdef INET6
330340c35deSJonathan Lemon 	if (INP_SOCKAF(so) == AF_INET6) {
331340c35deSJonathan Lemon 		inp->inp_vflag |= INP_IPV6PROTO;
332603724d3SBjoern A. Zeeb 		if (V_ip6_v6only)
33333841545SHajimu UMEMOTO 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
334340c35deSJonathan Lemon 	}
33575daea93SPaul Saab #endif
336ff9b006dSJulien Charbon 	INP_WLOCK(inp);
337ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
338712fc218SRobert Watson 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
3393d4d47f3SGarrett Wollman 	pcbinfo->ipi_count++;
340df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
34133841545SHajimu UMEMOTO #ifdef INET6
342603724d3SBjoern A. Zeeb 	if (V_ip6_auto_flowlabel)
34333841545SHajimu UMEMOTO 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
34433841545SHajimu UMEMOTO #endif
345d915b280SStephan Uphoff 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
34679bdc6e5SRobert Watson 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
3478c1960d5SMike Karels 
3488c1960d5SMike Karels 	/*
3498c1960d5SMike Karels 	 * Routes in inpcb's can cache L2 as well; they are guaranteed
3508c1960d5SMike Karels 	 * to be cleaned up.
3518c1960d5SMike Karels 	 */
3528c1960d5SMike Karels 	inp->inp_route.ro_flags = RT_LLE_CACHE;
353ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
3547a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC)
355a557af22SRobert Watson out:
35686d02c5cSBjoern A. Zeeb 	if (error != 0) {
35786d02c5cSBjoern A. Zeeb 		crfree(inp->inp_cred);
358a557af22SRobert Watson 		uma_zfree(pcbinfo->ipi_zone, inp);
35986d02c5cSBjoern A. Zeeb 	}
360a557af22SRobert Watson #endif
361a557af22SRobert Watson 	return (error);
362df8bae1dSRodney W. Grimes }
363df8bae1dSRodney W. Grimes 
36467107f45SBjoern A. Zeeb #ifdef INET
365df8bae1dSRodney W. Grimes int
366136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
367df8bae1dSRodney W. Grimes {
3684b932371SIan Dowse 	int anonport, error;
3694b932371SIan Dowse 
3708501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
371fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
37259daba27SSam Leffler 
3734b932371SIan Dowse 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
3744b932371SIan Dowse 		return (EINVAL);
3755cd3dcaaSNavdeep Parhar 	anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
3764b932371SIan Dowse 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
377b0330ed9SPawel Jakub Dawidek 	    &inp->inp_lport, cred);
3784b932371SIan Dowse 	if (error)
3794b932371SIan Dowse 		return (error);
3804b932371SIan Dowse 	if (in_pcbinshash(inp) != 0) {
3814b932371SIan Dowse 		inp->inp_laddr.s_addr = INADDR_ANY;
3824b932371SIan Dowse 		inp->inp_lport = 0;
3834b932371SIan Dowse 		return (EAGAIN);
3844b932371SIan Dowse 	}
3854b932371SIan Dowse 	if (anonport)
3864b932371SIan Dowse 		inp->inp_flags |= INP_ANONPORT;
3874b932371SIan Dowse 	return (0);
3884b932371SIan Dowse }
38967107f45SBjoern A. Zeeb #endif
3904b932371SIan Dowse 
39139c8c62eSHiren Panchasara /*
39239c8c62eSHiren Panchasara  * Select a local port (number) to use.
39339c8c62eSHiren Panchasara  */
394efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6)
395efc76f72SBjoern A. Zeeb int
3964616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
3974616026fSErmal Luçi     struct ucred *cred, int lookupflags)
398efc76f72SBjoern A. Zeeb {
399efc76f72SBjoern A. Zeeb 	struct inpcbinfo *pcbinfo;
400efc76f72SBjoern A. Zeeb 	struct inpcb *tmpinp;
401efc76f72SBjoern A. Zeeb 	unsigned short *lastport;
402efc76f72SBjoern A. Zeeb 	int count, dorandom, error;
403efc76f72SBjoern A. Zeeb 	u_short aux, first, last, lport;
404efc76f72SBjoern A. Zeeb #ifdef INET
405efc76f72SBjoern A. Zeeb 	struct in_addr laddr;
406efc76f72SBjoern A. Zeeb #endif
407efc76f72SBjoern A. Zeeb 
408efc76f72SBjoern A. Zeeb 	pcbinfo = inp->inp_pcbinfo;
409efc76f72SBjoern A. Zeeb 
410efc76f72SBjoern A. Zeeb 	/*
411efc76f72SBjoern A. Zeeb 	 * Because no actual state changes occur here, a global write lock on
412efc76f72SBjoern A. Zeeb 	 * the pcbinfo isn't required.
413efc76f72SBjoern A. Zeeb 	 */
414efc76f72SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
415fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
416efc76f72SBjoern A. Zeeb 
417efc76f72SBjoern A. Zeeb 	if (inp->inp_flags & INP_HIGHPORT) {
418efc76f72SBjoern A. Zeeb 		first = V_ipport_hifirstauto;	/* sysctl */
419efc76f72SBjoern A. Zeeb 		last  = V_ipport_hilastauto;
420efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lasthi;
421efc76f72SBjoern A. Zeeb 	} else if (inp->inp_flags & INP_LOWPORT) {
422efc76f72SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
423efc76f72SBjoern A. Zeeb 		if (error)
424efc76f72SBjoern A. Zeeb 			return (error);
425efc76f72SBjoern A. Zeeb 		first = V_ipport_lowfirstauto;	/* 1023 */
426efc76f72SBjoern A. Zeeb 		last  = V_ipport_lowlastauto;	/* 600 */
427efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastlow;
428efc76f72SBjoern A. Zeeb 	} else {
429efc76f72SBjoern A. Zeeb 		first = V_ipport_firstauto;	/* sysctl */
430efc76f72SBjoern A. Zeeb 		last  = V_ipport_lastauto;
431efc76f72SBjoern A. Zeeb 		lastport = &pcbinfo->ipi_lastport;
432efc76f72SBjoern A. Zeeb 	}
433efc76f72SBjoern A. Zeeb 	/*
434e06e816fSKevin Lo 	 * For UDP(-Lite), use random port allocation as long as the user
435efc76f72SBjoern A. Zeeb 	 * allows it.  For TCP (and as of yet unknown) connections,
436efc76f72SBjoern A. Zeeb 	 * use random port allocation only if the user allows it AND
437efc76f72SBjoern A. Zeeb 	 * ipport_tick() allows it.
438efc76f72SBjoern A. Zeeb 	 */
439efc76f72SBjoern A. Zeeb 	if (V_ipport_randomized &&
440e06e816fSKevin Lo 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
441e06e816fSKevin Lo 		pcbinfo == &V_ulitecbinfo))
442efc76f72SBjoern A. Zeeb 		dorandom = 1;
443efc76f72SBjoern A. Zeeb 	else
444efc76f72SBjoern A. Zeeb 		dorandom = 0;
445efc76f72SBjoern A. Zeeb 	/*
446efc76f72SBjoern A. Zeeb 	 * It makes no sense to do random port allocation if
447efc76f72SBjoern A. Zeeb 	 * we have the only port available.
448efc76f72SBjoern A. Zeeb 	 */
449efc76f72SBjoern A. Zeeb 	if (first == last)
450efc76f72SBjoern A. Zeeb 		dorandom = 0;
451e06e816fSKevin Lo 	/* Make sure to not include UDP(-Lite) packets in the count. */
452e06e816fSKevin Lo 	if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
453efc76f72SBjoern A. Zeeb 		V_ipport_tcpallocs++;
454efc76f72SBjoern A. Zeeb 	/*
455efc76f72SBjoern A. Zeeb 	 * Instead of having two loops further down counting up or down
456efc76f72SBjoern A. Zeeb 	 * make sure that first is always <= last and go with only one
457efc76f72SBjoern A. Zeeb 	 * code path implementing all logic.
458efc76f72SBjoern A. Zeeb 	 */
459efc76f72SBjoern A. Zeeb 	if (first > last) {
460efc76f72SBjoern A. Zeeb 		aux = first;
461efc76f72SBjoern A. Zeeb 		first = last;
462efc76f72SBjoern A. Zeeb 		last = aux;
463efc76f72SBjoern A. Zeeb 	}
464efc76f72SBjoern A. Zeeb 
465efc76f72SBjoern A. Zeeb #ifdef INET
466efc76f72SBjoern A. Zeeb 	/* Make the compiler happy. */
467efc76f72SBjoern A. Zeeb 	laddr.s_addr = 0;
4684d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
469efc76f72SBjoern A. Zeeb 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
470efc76f72SBjoern A. Zeeb 		    __func__, inp));
471efc76f72SBjoern A. Zeeb 		laddr = *laddrp;
472efc76f72SBjoern A. Zeeb 	}
473efc76f72SBjoern A. Zeeb #endif
47467107f45SBjoern A. Zeeb 	tmpinp = NULL;	/* Make compiler happy. */
475efc76f72SBjoern A. Zeeb 	lport = *lportp;
476efc76f72SBjoern A. Zeeb 
477efc76f72SBjoern A. Zeeb 	if (dorandom)
478efc76f72SBjoern A. Zeeb 		*lastport = first + (arc4random() % (last - first));
479efc76f72SBjoern A. Zeeb 
480efc76f72SBjoern A. Zeeb 	count = last - first;
481efc76f72SBjoern A. Zeeb 
482efc76f72SBjoern A. Zeeb 	do {
483efc76f72SBjoern A. Zeeb 		if (count-- < 0)	/* completely used? */
484efc76f72SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
485efc76f72SBjoern A. Zeeb 		++*lastport;
486efc76f72SBjoern A. Zeeb 		if (*lastport < first || *lastport > last)
487efc76f72SBjoern A. Zeeb 			*lastport = first;
488efc76f72SBjoern A. Zeeb 		lport = htons(*lastport);
489efc76f72SBjoern A. Zeeb 
490efc76f72SBjoern A. Zeeb #ifdef INET6
4914616026fSErmal Luçi 		if ((inp->inp_vflag & INP_IPV6) != 0)
492efc76f72SBjoern A. Zeeb 			tmpinp = in6_pcblookup_local(pcbinfo,
49368e0d7e0SRobert Watson 			    &inp->in6p_laddr, lport, lookupflags, cred);
494efc76f72SBjoern A. Zeeb #endif
495efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6)
496efc76f72SBjoern A. Zeeb 		else
497efc76f72SBjoern A. Zeeb #endif
498efc76f72SBjoern A. Zeeb #ifdef INET
499efc76f72SBjoern A. Zeeb 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
50068e0d7e0SRobert Watson 			    lport, lookupflags, cred);
501efc76f72SBjoern A. Zeeb #endif
502efc76f72SBjoern A. Zeeb 	} while (tmpinp != NULL);
503efc76f72SBjoern A. Zeeb 
504efc76f72SBjoern A. Zeeb #ifdef INET
5054d457387SBjoern A. Zeeb 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
506efc76f72SBjoern A. Zeeb 		laddrp->s_addr = laddr.s_addr;
507efc76f72SBjoern A. Zeeb #endif
508efc76f72SBjoern A. Zeeb 	*lportp = lport;
509efc76f72SBjoern A. Zeeb 
510efc76f72SBjoern A. Zeeb 	return (0);
511efc76f72SBjoern A. Zeeb }
512efdf104bSMikolaj Golub 
513efdf104bSMikolaj Golub /*
514efdf104bSMikolaj Golub  * Return cached socket options.
515efdf104bSMikolaj Golub  */
516efdf104bSMikolaj Golub short
517efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp)
518efdf104bSMikolaj Golub {
519efdf104bSMikolaj Golub    short so_options;
520efdf104bSMikolaj Golub 
521efdf104bSMikolaj Golub    so_options = 0;
522efdf104bSMikolaj Golub 
523efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
524efdf104bSMikolaj Golub 	   so_options |= SO_REUSEPORT;
525efdf104bSMikolaj Golub    if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
526efdf104bSMikolaj Golub 	   so_options |= SO_REUSEADDR;
527efdf104bSMikolaj Golub    return (so_options);
528efdf104bSMikolaj Golub }
529efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */
530efc76f72SBjoern A. Zeeb 
5314b932371SIan Dowse /*
5320a100a6fSAdrian Chadd  * Check if a new BINDMULTI socket is allowed to be created.
5330a100a6fSAdrian Chadd  *
5340a100a6fSAdrian Chadd  * ni points to the new inp.
5350a100a6fSAdrian Chadd  * oi points to the exisitng inp.
5360a100a6fSAdrian Chadd  *
5370a100a6fSAdrian Chadd  * This checks whether the existing inp also has BINDMULTI and
5380a100a6fSAdrian Chadd  * whether the credentials match.
5390a100a6fSAdrian Chadd  */
540d5bb8bd3SAdrian Chadd int
5410a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
5420a100a6fSAdrian Chadd {
5430a100a6fSAdrian Chadd 	/* Check permissions match */
5440a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5450a100a6fSAdrian Chadd 	    (ni->inp_cred->cr_uid !=
5460a100a6fSAdrian Chadd 	    oi->inp_cred->cr_uid))
5470a100a6fSAdrian Chadd 		return (0);
5480a100a6fSAdrian Chadd 
5490a100a6fSAdrian Chadd 	/* Check the existing inp has BINDMULTI set */
5500a100a6fSAdrian Chadd 	if ((ni->inp_flags2 & INP_BINDMULTI) &&
5510a100a6fSAdrian Chadd 	    ((oi->inp_flags2 & INP_BINDMULTI) == 0))
5520a100a6fSAdrian Chadd 		return (0);
5530a100a6fSAdrian Chadd 
5540a100a6fSAdrian Chadd 	/*
5550a100a6fSAdrian Chadd 	 * We're okay - either INP_BINDMULTI isn't set on ni, or
5560a100a6fSAdrian Chadd 	 * it is and it matches the checks.
5570a100a6fSAdrian Chadd 	 */
5580a100a6fSAdrian Chadd 	return (1);
5590a100a6fSAdrian Chadd }
5600a100a6fSAdrian Chadd 
561d5bb8bd3SAdrian Chadd #ifdef INET
5620a100a6fSAdrian Chadd /*
5634b932371SIan Dowse  * Set up a bind operation on a PCB, performing port allocation
5644b932371SIan Dowse  * as required, but do not actually modify the PCB. Callers can
5654b932371SIan Dowse  * either complete the bind by setting inp_laddr/inp_lport and
5664b932371SIan Dowse  * calling in_pcbinshash(), or they can just use the resulting
5674b932371SIan Dowse  * port and address to authorise the sending of a once-off packet.
5684b932371SIan Dowse  *
5694b932371SIan Dowse  * On error, the values of *laddrp and *lportp are not changed.
5704b932371SIan Dowse  */
5714b932371SIan Dowse int
572136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
573136d4f1cSRobert Watson     u_short *lportp, struct ucred *cred)
5744b932371SIan Dowse {
5754b932371SIan Dowse 	struct socket *so = inp->inp_socket;
57615bd2b43SDavid Greenman 	struct sockaddr_in *sin;
577c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
5784b932371SIan Dowse 	struct in_addr laddr;
579df8bae1dSRodney W. Grimes 	u_short lport = 0;
58068e0d7e0SRobert Watson 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
581413628a7SBjoern A. Zeeb 	int error;
582df8bae1dSRodney W. Grimes 
5838501a69cSRobert Watson 	/*
584fa046d87SRobert Watson 	 * No state changes, so read locks are sufficient here.
5858501a69cSRobert Watson 	 */
58659daba27SSam Leffler 	INP_LOCK_ASSERT(inp);
587fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
58859daba27SSam Leffler 
589603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
590df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
5914b932371SIan Dowse 	laddr.s_addr = *laddrp;
5924b932371SIan Dowse 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
593df8bae1dSRodney W. Grimes 		return (EINVAL);
594c3229e05SDavid Greenman 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
59568e0d7e0SRobert Watson 		lookupflags = INPLOOKUP_WILDCARD;
5964616026fSErmal Luçi 	if (nam == NULL) {
5977c2f3cb9SJamie Gritton 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
5987c2f3cb9SJamie Gritton 			return (error);
5997c2f3cb9SJamie Gritton 	} else {
60057bf258eSGarrett Wollman 		sin = (struct sockaddr_in *)nam;
60157bf258eSGarrett Wollman 		if (nam->sa_len != sizeof (*sin))
602df8bae1dSRodney W. Grimes 			return (EINVAL);
603df8bae1dSRodney W. Grimes #ifdef notdef
604df8bae1dSRodney W. Grimes 		/*
605df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
606df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
607df8bae1dSRodney W. Grimes 		 */
608df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
609df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
610df8bae1dSRodney W. Grimes #endif
611b89e82ddSJamie Gritton 		error = prison_local_ip4(cred, &sin->sin_addr);
612b89e82ddSJamie Gritton 		if (error)
613b89e82ddSJamie Gritton 			return (error);
6144b932371SIan Dowse 		if (sin->sin_port != *lportp) {
6154b932371SIan Dowse 			/* Don't allow the port to change. */
6164b932371SIan Dowse 			if (*lportp != 0)
6174b932371SIan Dowse 				return (EINVAL);
618df8bae1dSRodney W. Grimes 			lport = sin->sin_port;
6194b932371SIan Dowse 		}
6204b932371SIan Dowse 		/* NB: lport is left as 0 if the port isn't being changed. */
621df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
622df8bae1dSRodney W. Grimes 			/*
623df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
624df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
625df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
626df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
627df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
628df8bae1dSRodney W. Grimes 			 */
629f122b319SMikolaj Golub 			if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
630df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
631df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
632df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
63383103a73SAndrew R. Reiter 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
6344209e01aSAdrian Chadd 			/*
6354209e01aSAdrian Chadd 			 * Is the address a local IP address?
636f44270e7SPawel Jakub Dawidek 			 * If INP_BINDANY is set, then the socket may be bound
6378696873dSAdrian Chadd 			 * to any endpoint address, local or not.
6384209e01aSAdrian Chadd 			 */
639f44270e7SPawel Jakub Dawidek 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
6408896f83aSRobert Watson 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
641df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
642df8bae1dSRodney W. Grimes 		}
6434b932371SIan Dowse 		laddr = sin->sin_addr;
644df8bae1dSRodney W. Grimes 		if (lport) {
645df8bae1dSRodney W. Grimes 			struct inpcb *t;
646ae0e7143SRobert Watson 			struct tcptw *tw;
647ae0e7143SRobert Watson 
648df8bae1dSRodney W. Grimes 			/* GROSS */
649603724d3SBjoern A. Zeeb 			if (ntohs(lport) <= V_ipport_reservedhigh &&
650603724d3SBjoern A. Zeeb 			    ntohs(lport) >= V_ipport_reservedlow &&
651acd3428bSRobert Watson 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
65232f9753cSRobert Watson 			    0))
6532469dd60SGarrett Wollman 				return (EACCES);
654835d4b89SPawel Jakub Dawidek 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
65586d02c5cSBjoern A. Zeeb 			    priv_check_cred(inp->inp_cred,
65632f9753cSRobert Watson 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
657078b7042SBjoern A. Zeeb 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
658413628a7SBjoern A. Zeeb 				    lport, INPLOOKUP_WILDCARD, cred);
659340c35deSJonathan Lemon 	/*
660340c35deSJonathan Lemon 	 * XXX
661340c35deSJonathan Lemon 	 * This entire block sorely needs a rewrite.
662340c35deSJonathan Lemon 	 */
6634cc20ab1SSeigo Tanimura 				if (t &&
6640a100a6fSAdrian Chadd 				    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
665ad71fe3cSRobert Watson 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
6664658dc83SYaroslav Tykhiy 				    (so->so_type != SOCK_STREAM ||
6674658dc83SYaroslav Tykhiy 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
6684cc20ab1SSeigo Tanimura 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
66952b65dbeSBill Fenner 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
670fc06cd42SMikolaj Golub 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
67186d02c5cSBjoern A. Zeeb 				    (inp->inp_cred->cr_uid !=
67286d02c5cSBjoern A. Zeeb 				     t->inp_cred->cr_uid))
6734049a042SGuido van Rooij 					return (EADDRINUSE);
6740a100a6fSAdrian Chadd 
6750a100a6fSAdrian Chadd 				/*
6760a100a6fSAdrian Chadd 				 * If the socket is a BINDMULTI socket, then
6770a100a6fSAdrian Chadd 				 * the credentials need to match and the
6780a100a6fSAdrian Chadd 				 * original socket also has to have been bound
6790a100a6fSAdrian Chadd 				 * with BINDMULTI.
6800a100a6fSAdrian Chadd 				 */
6810a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
6820a100a6fSAdrian Chadd 					return (EADDRINUSE);
6834049a042SGuido van Rooij 			}
684c3229e05SDavid Greenman 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
68568e0d7e0SRobert Watson 			    lport, lookupflags, cred);
686ad71fe3cSRobert Watson 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
687ae0e7143SRobert Watson 				/*
688ae0e7143SRobert Watson 				 * XXXRW: If an incpb has had its timewait
689ae0e7143SRobert Watson 				 * state recycled, we treat the address as
690ae0e7143SRobert Watson 				 * being in use (for now).  This is better
691ae0e7143SRobert Watson 				 * than a panic, but not desirable.
692ae0e7143SRobert Watson 				 */
693ec95b709SMikolaj Golub 				tw = intotw(t);
694ae0e7143SRobert Watson 				if (tw == NULL ||
695ae0e7143SRobert Watson 				    (reuseport & tw->tw_so_options) == 0)
696340c35deSJonathan Lemon 					return (EADDRINUSE);
6970a100a6fSAdrian Chadd 			} else if (t &&
6980a100a6fSAdrian Chadd 			    ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
6990a100a6fSAdrian Chadd 			    (reuseport & inp_so_options(t)) == 0) {
700e3fd5ffdSRobert Watson #ifdef INET6
70133841545SHajimu UMEMOTO 				if (ntohl(sin->sin_addr.s_addr) !=
702cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
703cfa1ca9dSYoshinobu Inoue 				    ntohl(t->inp_laddr.s_addr) !=
704cfa1ca9dSYoshinobu Inoue 				    INADDR_ANY ||
705fc06cd42SMikolaj Golub 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
706fc06cd42SMikolaj Golub 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
707e3fd5ffdSRobert Watson #endif
708df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
7090a100a6fSAdrian Chadd 				if (t && (! in_pcbbind_check_bindmulti(inp, t)))
7100a100a6fSAdrian Chadd 					return (EADDRINUSE);
711df8bae1dSRodney W. Grimes 			}
712cfa1ca9dSYoshinobu Inoue 		}
713df8bae1dSRodney W. Grimes 	}
7144b932371SIan Dowse 	if (*lportp != 0)
7154b932371SIan Dowse 		lport = *lportp;
71633b3ac06SPeter Wemm 	if (lport == 0) {
7174616026fSErmal Luçi 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
718efc76f72SBjoern A. Zeeb 		if (error != 0)
719efc76f72SBjoern A. Zeeb 			return (error);
72033b3ac06SPeter Wemm 
72133b3ac06SPeter Wemm 	}
7224b932371SIan Dowse 	*laddrp = laddr.s_addr;
7234b932371SIan Dowse 	*lportp = lport;
724df8bae1dSRodney W. Grimes 	return (0);
725df8bae1dSRodney W. Grimes }
726df8bae1dSRodney W. Grimes 
727999f1343SGarrett Wollman /*
7285200e00eSIan Dowse  * Connect from a socket to a specified address.
7295200e00eSIan Dowse  * Both address and port must be specified in argument sin.
7305200e00eSIan Dowse  * If don't have a local address for this socket yet,
7315200e00eSIan Dowse  * then pick one.
732999f1343SGarrett Wollman  */
733999f1343SGarrett Wollman int
734d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
735d3c1f003SRobert Watson     struct ucred *cred, struct mbuf *m)
736999f1343SGarrett Wollman {
7375200e00eSIan Dowse 	u_short lport, fport;
7385200e00eSIan Dowse 	in_addr_t laddr, faddr;
7395200e00eSIan Dowse 	int anonport, error;
740df8bae1dSRodney W. Grimes 
7418501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
742fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
74327f74fd0SRobert Watson 
7445200e00eSIan Dowse 	lport = inp->inp_lport;
7455200e00eSIan Dowse 	laddr = inp->inp_laddr.s_addr;
7465200e00eSIan Dowse 	anonport = (lport == 0);
7475200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
748b0330ed9SPawel Jakub Dawidek 	    NULL, cred);
7495200e00eSIan Dowse 	if (error)
7505200e00eSIan Dowse 		return (error);
7515200e00eSIan Dowse 
7525200e00eSIan Dowse 	/* Do the initial binding of the local address if required. */
7535200e00eSIan Dowse 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
7545200e00eSIan Dowse 		inp->inp_lport = lport;
7555200e00eSIan Dowse 		inp->inp_laddr.s_addr = laddr;
7565200e00eSIan Dowse 		if (in_pcbinshash(inp) != 0) {
7575200e00eSIan Dowse 			inp->inp_laddr.s_addr = INADDR_ANY;
7585200e00eSIan Dowse 			inp->inp_lport = 0;
7595200e00eSIan Dowse 			return (EAGAIN);
7605200e00eSIan Dowse 		}
7615200e00eSIan Dowse 	}
7625200e00eSIan Dowse 
7635200e00eSIan Dowse 	/* Commit the remaining changes. */
7645200e00eSIan Dowse 	inp->inp_lport = lport;
7655200e00eSIan Dowse 	inp->inp_laddr.s_addr = laddr;
7665200e00eSIan Dowse 	inp->inp_faddr.s_addr = faddr;
7675200e00eSIan Dowse 	inp->inp_fport = fport;
768d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, m);
7692cb64cb2SGeorge V. Neville-Neil 
7705200e00eSIan Dowse 	if (anonport)
7715200e00eSIan Dowse 		inp->inp_flags |= INP_ANONPORT;
7725200e00eSIan Dowse 	return (0);
7735200e00eSIan Dowse }
7745200e00eSIan Dowse 
775d3c1f003SRobert Watson int
776d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
777d3c1f003SRobert Watson {
778d3c1f003SRobert Watson 
779d3c1f003SRobert Watson 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
780d3c1f003SRobert Watson }
781d3c1f003SRobert Watson 
7825200e00eSIan Dowse /*
7830895aec3SBjoern A. Zeeb  * Do proper source address selection on an unbound socket in case
7840895aec3SBjoern A. Zeeb  * of connect. Take jails into account as well.
7850895aec3SBjoern A. Zeeb  */
786ae190832SSteven Hartland int
7870895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
7880895aec3SBjoern A. Zeeb     struct ucred *cred)
7890895aec3SBjoern A. Zeeb {
7900895aec3SBjoern A. Zeeb 	struct ifaddr *ifa;
7910895aec3SBjoern A. Zeeb 	struct sockaddr *sa;
7920895aec3SBjoern A. Zeeb 	struct sockaddr_in *sin;
7930895aec3SBjoern A. Zeeb 	struct route sro;
7940895aec3SBjoern A. Zeeb 	int error;
7950895aec3SBjoern A. Zeeb 
796413628a7SBjoern A. Zeeb 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
7970895aec3SBjoern A. Zeeb 
798592bcae8SBjoern A. Zeeb 	/*
799592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
800592bcae8SBjoern A. Zeeb 	 * if requested.
801592bcae8SBjoern A. Zeeb 	 */
802592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
803592bcae8SBjoern A. Zeeb 		return (0);
804592bcae8SBjoern A. Zeeb 
8050895aec3SBjoern A. Zeeb 	error = 0;
8060895aec3SBjoern A. Zeeb 	bzero(&sro, sizeof(sro));
8070895aec3SBjoern A. Zeeb 
8080895aec3SBjoern A. Zeeb 	sin = (struct sockaddr_in *)&sro.ro_dst;
8090895aec3SBjoern A. Zeeb 	sin->sin_family = AF_INET;
8100895aec3SBjoern A. Zeeb 	sin->sin_len = sizeof(struct sockaddr_in);
8110895aec3SBjoern A. Zeeb 	sin->sin_addr.s_addr = faddr->s_addr;
8120895aec3SBjoern A. Zeeb 
8130895aec3SBjoern A. Zeeb 	/*
8140895aec3SBjoern A. Zeeb 	 * If route is known our src addr is taken from the i/f,
8150895aec3SBjoern A. Zeeb 	 * else punt.
8160895aec3SBjoern A. Zeeb 	 *
8170895aec3SBjoern A. Zeeb 	 * Find out route to destination.
8180895aec3SBjoern A. Zeeb 	 */
8190895aec3SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
8206e6b3f7cSQing Li 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
8210895aec3SBjoern A. Zeeb 
8220895aec3SBjoern A. Zeeb 	/*
8230895aec3SBjoern A. Zeeb 	 * If we found a route, use the address corresponding to
8240895aec3SBjoern A. Zeeb 	 * the outgoing interface.
8250895aec3SBjoern A. Zeeb 	 *
8260895aec3SBjoern A. Zeeb 	 * Otherwise assume faddr is reachable on a directly connected
8270895aec3SBjoern A. Zeeb 	 * network and try to find a corresponding interface to take
8280895aec3SBjoern A. Zeeb 	 * the source address from.
8290895aec3SBjoern A. Zeeb 	 */
8300895aec3SBjoern A. Zeeb 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
8318c0fec80SRobert Watson 		struct in_ifaddr *ia;
8320895aec3SBjoern A. Zeeb 		struct ifnet *ifp;
8330895aec3SBjoern A. Zeeb 
8344f8585e0SAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
83558a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
8360895aec3SBjoern A. Zeeb 		if (ia == NULL)
8374f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
83858a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
8390895aec3SBjoern A. Zeeb 		if (ia == NULL) {
8400895aec3SBjoern A. Zeeb 			error = ENETUNREACH;
8410895aec3SBjoern A. Zeeb 			goto done;
8420895aec3SBjoern A. Zeeb 		}
8430895aec3SBjoern A. Zeeb 
8440304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8450895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8468c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
8470895aec3SBjoern A. Zeeb 			goto done;
8480895aec3SBjoern A. Zeeb 		}
8490895aec3SBjoern A. Zeeb 
8500895aec3SBjoern A. Zeeb 		ifp = ia->ia_ifp;
8518c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
8520895aec3SBjoern A. Zeeb 		ia = NULL;
853137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
8540895aec3SBjoern A. Zeeb 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
8550895aec3SBjoern A. Zeeb 
8560895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
8570895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
8580895aec3SBjoern A. Zeeb 				continue;
8590895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
860b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
8610895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
8620895aec3SBjoern A. Zeeb 				break;
8630895aec3SBjoern A. Zeeb 			}
8640895aec3SBjoern A. Zeeb 		}
8650895aec3SBjoern A. Zeeb 		if (ia != NULL) {
8660895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
867137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
8680895aec3SBjoern A. Zeeb 			goto done;
8690895aec3SBjoern A. Zeeb 		}
870137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
8710895aec3SBjoern A. Zeeb 
8720895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
873b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
8740895aec3SBjoern A. Zeeb 		goto done;
8750895aec3SBjoern A. Zeeb 	}
8760895aec3SBjoern A. Zeeb 
8770895aec3SBjoern A. Zeeb 	/*
8780895aec3SBjoern A. Zeeb 	 * If the outgoing interface on the route found is not
8790895aec3SBjoern A. Zeeb 	 * a loopback interface, use the address from that interface.
8800895aec3SBjoern A. Zeeb 	 * In case of jails do those three steps:
8810895aec3SBjoern A. Zeeb 	 * 1. check if the interface address belongs to the jail. If so use it.
8820895aec3SBjoern A. Zeeb 	 * 2. check if we have any address on the outgoing interface
8830895aec3SBjoern A. Zeeb 	 *    belonging to this jail. If so use it.
8840895aec3SBjoern A. Zeeb 	 * 3. as a last resort return the 'default' jail address.
8850895aec3SBjoern A. Zeeb 	 */
8860895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
8878c0fec80SRobert Watson 		struct in_ifaddr *ia;
8889317b04eSRobert Watson 		struct ifnet *ifp;
8890895aec3SBjoern A. Zeeb 
8900895aec3SBjoern A. Zeeb 		/* If not jailed, use the default returned. */
8910304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
8920895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
8930895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
8940895aec3SBjoern A. Zeeb 			goto done;
8950895aec3SBjoern A. Zeeb 		}
8960895aec3SBjoern A. Zeeb 
8970895aec3SBjoern A. Zeeb 		/* Jailed. */
8980895aec3SBjoern A. Zeeb 		/* 1. Check if the iface address belongs to the jail. */
8990895aec3SBjoern A. Zeeb 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
900b89e82ddSJamie Gritton 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
9010895aec3SBjoern A. Zeeb 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
9020895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
9030895aec3SBjoern A. Zeeb 			goto done;
9040895aec3SBjoern A. Zeeb 		}
9050895aec3SBjoern A. Zeeb 
9060895aec3SBjoern A. Zeeb 		/*
9070895aec3SBjoern A. Zeeb 		 * 2. Check if we have any address on the outgoing interface
9080895aec3SBjoern A. Zeeb 		 *    belonging to this jail.
9090895aec3SBjoern A. Zeeb 		 */
9108c0fec80SRobert Watson 		ia = NULL;
9119317b04eSRobert Watson 		ifp = sro.ro_rt->rt_ifp;
912137f91e8SJohn Baldwin 		IF_ADDR_RLOCK(ifp);
9139317b04eSRobert Watson 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9140895aec3SBjoern A. Zeeb 			sa = ifa->ifa_addr;
9150895aec3SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
9160895aec3SBjoern A. Zeeb 				continue;
9170895aec3SBjoern A. Zeeb 			sin = (struct sockaddr_in *)sa;
918b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
9190895aec3SBjoern A. Zeeb 				ia = (struct in_ifaddr *)ifa;
9200895aec3SBjoern A. Zeeb 				break;
9210895aec3SBjoern A. Zeeb 			}
9220895aec3SBjoern A. Zeeb 		}
9230895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9240895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
925137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9260895aec3SBjoern A. Zeeb 			goto done;
9270895aec3SBjoern A. Zeeb 		}
928137f91e8SJohn Baldwin 		IF_ADDR_RUNLOCK(ifp);
9290895aec3SBjoern A. Zeeb 
9300895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
931b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
9320895aec3SBjoern A. Zeeb 		goto done;
9330895aec3SBjoern A. Zeeb 	}
9340895aec3SBjoern A. Zeeb 
9350895aec3SBjoern A. Zeeb 	/*
9360895aec3SBjoern A. Zeeb 	 * The outgoing interface is marked with 'loopback net', so a route
9370895aec3SBjoern A. Zeeb 	 * to ourselves is here.
9380895aec3SBjoern A. Zeeb 	 * Try to find the interface of the destination address and then
9390895aec3SBjoern A. Zeeb 	 * take the address from there. That interface is not necessarily
9400895aec3SBjoern A. Zeeb 	 * a loopback interface.
9410895aec3SBjoern A. Zeeb 	 * In case of jails, check that it is an address of the jail
9420895aec3SBjoern A. Zeeb 	 * and if we cannot find, fall back to the 'default' jail address.
9430895aec3SBjoern A. Zeeb 	 */
9440895aec3SBjoern A. Zeeb 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
9450895aec3SBjoern A. Zeeb 		struct sockaddr_in sain;
9468c0fec80SRobert Watson 		struct in_ifaddr *ia;
9470895aec3SBjoern A. Zeeb 
9480895aec3SBjoern A. Zeeb 		bzero(&sain, sizeof(struct sockaddr_in));
9490895aec3SBjoern A. Zeeb 		sain.sin_family = AF_INET;
9500895aec3SBjoern A. Zeeb 		sain.sin_len = sizeof(struct sockaddr_in);
9510895aec3SBjoern A. Zeeb 		sain.sin_addr.s_addr = faddr->s_addr;
9520895aec3SBjoern A. Zeeb 
95358a39d8cSAlan Somers 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
95458a39d8cSAlan Somers 					inp->inp_socket->so_fibnum));
9550895aec3SBjoern A. Zeeb 		if (ia == NULL)
9564f8585e0SAlan Somers 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
95758a39d8cSAlan Somers 						inp->inp_socket->so_fibnum));
958f0bb05fcSQing Li 		if (ia == NULL)
959f0bb05fcSQing Li 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
9600895aec3SBjoern A. Zeeb 
9610304c731SJamie Gritton 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
9620895aec3SBjoern A. Zeeb 			if (ia == NULL) {
9630895aec3SBjoern A. Zeeb 				error = ENETUNREACH;
9640895aec3SBjoern A. Zeeb 				goto done;
9650895aec3SBjoern A. Zeeb 			}
9660895aec3SBjoern A. Zeeb 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
9678c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9680895aec3SBjoern A. Zeeb 			goto done;
9690895aec3SBjoern A. Zeeb 		}
9700895aec3SBjoern A. Zeeb 
9710895aec3SBjoern A. Zeeb 		/* Jailed. */
9720895aec3SBjoern A. Zeeb 		if (ia != NULL) {
9730895aec3SBjoern A. Zeeb 			struct ifnet *ifp;
9740895aec3SBjoern A. Zeeb 
9750895aec3SBjoern A. Zeeb 			ifp = ia->ia_ifp;
9768c0fec80SRobert Watson 			ifa_free(&ia->ia_ifa);
9770895aec3SBjoern A. Zeeb 			ia = NULL;
978137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
9790895aec3SBjoern A. Zeeb 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9800895aec3SBjoern A. Zeeb 
9810895aec3SBjoern A. Zeeb 				sa = ifa->ifa_addr;
9820895aec3SBjoern A. Zeeb 				if (sa->sa_family != AF_INET)
9830895aec3SBjoern A. Zeeb 					continue;
9840895aec3SBjoern A. Zeeb 				sin = (struct sockaddr_in *)sa;
985b89e82ddSJamie Gritton 				if (prison_check_ip4(cred,
986b89e82ddSJamie Gritton 				    &sin->sin_addr) == 0) {
9870895aec3SBjoern A. Zeeb 					ia = (struct in_ifaddr *)ifa;
9880895aec3SBjoern A. Zeeb 					break;
9890895aec3SBjoern A. Zeeb 				}
9900895aec3SBjoern A. Zeeb 			}
9910895aec3SBjoern A. Zeeb 			if (ia != NULL) {
9920895aec3SBjoern A. Zeeb 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
993137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
9940895aec3SBjoern A. Zeeb 				goto done;
9950895aec3SBjoern A. Zeeb 			}
996137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
9970895aec3SBjoern A. Zeeb 		}
9980895aec3SBjoern A. Zeeb 
9990895aec3SBjoern A. Zeeb 		/* 3. As a last resort return the 'default' jail address. */
1000b89e82ddSJamie Gritton 		error = prison_get_ip4(cred, laddr);
10010895aec3SBjoern A. Zeeb 		goto done;
10020895aec3SBjoern A. Zeeb 	}
10030895aec3SBjoern A. Zeeb 
10040895aec3SBjoern A. Zeeb done:
10050895aec3SBjoern A. Zeeb 	if (sro.ro_rt != NULL)
10060895aec3SBjoern A. Zeeb 		RTFREE(sro.ro_rt);
10070895aec3SBjoern A. Zeeb 	return (error);
10080895aec3SBjoern A. Zeeb }
10090895aec3SBjoern A. Zeeb 
10100895aec3SBjoern A. Zeeb /*
10115200e00eSIan Dowse  * Set up for a connect from a socket to the specified address.
10125200e00eSIan Dowse  * On entry, *laddrp and *lportp should contain the current local
10135200e00eSIan Dowse  * address and port for the PCB; these are updated to the values
10145200e00eSIan Dowse  * that should be placed in inp_laddr and inp_lport to complete
10155200e00eSIan Dowse  * the connect.
10165200e00eSIan Dowse  *
10175200e00eSIan Dowse  * On success, *faddrp and *fportp will be set to the remote address
10185200e00eSIan Dowse  * and port. These are not updated in the error case.
10195200e00eSIan Dowse  *
10205200e00eSIan Dowse  * If the operation fails because the connection already exists,
10215200e00eSIan Dowse  * *oinpp will be set to the PCB of that connection so that the
10225200e00eSIan Dowse  * caller can decide to override it. In all other cases, *oinpp
10235200e00eSIan Dowse  * is set to NULL.
10245200e00eSIan Dowse  */
10255200e00eSIan Dowse int
1026136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1027136d4f1cSRobert Watson     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1028136d4f1cSRobert Watson     struct inpcb **oinpp, struct ucred *cred)
10295200e00eSIan Dowse {
1030cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
10315200e00eSIan Dowse 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
10325200e00eSIan Dowse 	struct in_ifaddr *ia;
10335200e00eSIan Dowse 	struct inpcb *oinp;
1034b89e82ddSJamie Gritton 	struct in_addr laddr, faddr;
10355200e00eSIan Dowse 	u_short lport, fport;
10365200e00eSIan Dowse 	int error;
10375200e00eSIan Dowse 
10388501a69cSRobert Watson 	/*
10398501a69cSRobert Watson 	 * Because a global state change doesn't actually occur here, a read
10408501a69cSRobert Watson 	 * lock is sufficient.
10418501a69cSRobert Watson 	 */
104227f74fd0SRobert Watson 	INP_LOCK_ASSERT(inp);
1043fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
104427f74fd0SRobert Watson 
10455200e00eSIan Dowse 	if (oinpp != NULL)
10465200e00eSIan Dowse 		*oinpp = NULL;
104757bf258eSGarrett Wollman 	if (nam->sa_len != sizeof (*sin))
1048df8bae1dSRodney W. Grimes 		return (EINVAL);
1049df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
1050df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
1051df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
1052df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
10535200e00eSIan Dowse 	laddr.s_addr = *laddrp;
10545200e00eSIan Dowse 	lport = *lportp;
10555200e00eSIan Dowse 	faddr = sin->sin_addr;
10565200e00eSIan Dowse 	fport = sin->sin_port;
10570895aec3SBjoern A. Zeeb 
1058603724d3SBjoern A. Zeeb 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1059df8bae1dSRodney W. Grimes 		/*
1060df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
1061df8bae1dSRodney W. Grimes 		 * use the primary local address.
1062df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
1063df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
1064df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
1065df8bae1dSRodney W. Grimes 		 */
1066413628a7SBjoern A. Zeeb 		if (faddr.s_addr == INADDR_ANY) {
1067cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
1068413628a7SBjoern A. Zeeb 			faddr =
1069b89e82ddSJamie Gritton 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1070cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1071b89e82ddSJamie Gritton 			if (cred != NULL &&
1072b89e82ddSJamie Gritton 			    (error = prison_get_ip4(cred, &faddr)) != 0)
1073b89e82ddSJamie Gritton 				return (error);
10742d9cfabaSRobert Watson 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1075cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RLOCK(&in_ifa_tracker);
10762d9cfabaSRobert Watson 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
10772d9cfabaSRobert Watson 			    IFF_BROADCAST)
10785200e00eSIan Dowse 				faddr = satosin(&TAILQ_FIRST(
1079603724d3SBjoern A. Zeeb 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1080cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
10812d9cfabaSRobert Watson 		}
1082df8bae1dSRodney W. Grimes 	}
10835200e00eSIan Dowse 	if (laddr.s_addr == INADDR_ANY) {
1084d79fdd98SDaniel Eischen 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
1085df8bae1dSRodney W. Grimes 		/*
1086df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
1087d79fdd98SDaniel Eischen 		 * interface has been set as a multicast option, prefer the
1088df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
1089df8bae1dSRodney W. Grimes 		 */
10905200e00eSIan Dowse 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1091df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
1092df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
1093df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1094df8bae1dSRodney W. Grimes 
1095df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
1096df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
1097df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
1098cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RLOCK(&in_ifa_tracker);
1099e691be70SDaniel Eischen 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1100e691be70SDaniel Eischen 					if ((ia->ia_ifp == ifp) &&
1101e691be70SDaniel Eischen 					    (cred == NULL ||
1102e691be70SDaniel Eischen 					    prison_check_ip4(cred,
1103e691be70SDaniel Eischen 					    &ia->ia_addr.sin_addr) == 0))
1104df8bae1dSRodney W. Grimes 						break;
1105e691be70SDaniel Eischen 				}
1106e691be70SDaniel Eischen 				if (ia == NULL)
1107d79fdd98SDaniel Eischen 					error = EADDRNOTAVAIL;
1108e691be70SDaniel Eischen 				else {
11095200e00eSIan Dowse 					laddr = ia->ia_addr.sin_addr;
1110d79fdd98SDaniel Eischen 					error = 0;
1111999f1343SGarrett Wollman 				}
1112cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1113d79fdd98SDaniel Eischen 			}
1114d79fdd98SDaniel Eischen 		}
111504215ed2SRandall Stewart 		if (error)
111604215ed2SRandall Stewart 			return (error);
11170895aec3SBjoern A. Zeeb 	}
1118fa046d87SRobert Watson 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1119fa046d87SRobert Watson 	    laddr, lport, 0, NULL);
11205200e00eSIan Dowse 	if (oinp != NULL) {
11215200e00eSIan Dowse 		if (oinpp != NULL)
11225200e00eSIan Dowse 			*oinpp = oinp;
1123df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
1124c3229e05SDavid Greenman 	}
11255200e00eSIan Dowse 	if (lport == 0) {
1126b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1127b0330ed9SPawel Jakub Dawidek 		    cred);
11285a903f8dSPierre Beyssac 		if (error)
11295a903f8dSPierre Beyssac 			return (error);
11305a903f8dSPierre Beyssac 	}
11315200e00eSIan Dowse 	*laddrp = laddr.s_addr;
11325200e00eSIan Dowse 	*lportp = lport;
11335200e00eSIan Dowse 	*faddrp = faddr.s_addr;
11345200e00eSIan Dowse 	*fportp = fport;
1135df8bae1dSRodney W. Grimes 	return (0);
1136df8bae1dSRodney W. Grimes }
1137df8bae1dSRodney W. Grimes 
113826f9a767SRodney W. Grimes void
1139136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp)
1140df8bae1dSRodney W. Grimes {
11416b348152SRobert Watson 
11428501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1143fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1144df8bae1dSRodney W. Grimes 
1145df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
1146df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
114715bd2b43SDavid Greenman 	in_pcbrehash(inp);
1148df8bae1dSRodney W. Grimes }
114983e521ecSBjoern A. Zeeb #endif /* INET */
1150df8bae1dSRodney W. Grimes 
11514c7c478dSRobert Watson /*
115228696211SRobert Watson  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1153c0a211c5SRobert Watson  * For most protocols, this will be invoked immediately prior to calling
115428696211SRobert Watson  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
115528696211SRobert Watson  * socket, in which case in_pcbfree() is deferred.
11564c7c478dSRobert Watson  */
115726f9a767SRodney W. Grimes void
1158136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp)
1159df8bae1dSRodney W. Grimes {
11604c7c478dSRobert Watson 
1161a7df09e8SBjoern A. Zeeb 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1162c0a211c5SRobert Watson 
1163f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1164f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag != NULL)
1165f3e7afe2SHans Petter Selasky 		in_pcbdetach_txrtlmt(inp);
1166f3e7afe2SHans Petter Selasky #endif
11674c7c478dSRobert Watson 	inp->inp_socket->so_pcb = NULL;
11684c7c478dSRobert Watson 	inp->inp_socket = NULL;
11694c7c478dSRobert Watson }
11704c7c478dSRobert Watson 
1171c0a211c5SRobert Watson /*
117279bdc6e5SRobert Watson  * in_pcbref() bumps the reference count on an inpcb in order to maintain
117379bdc6e5SRobert Watson  * stability of an inpcb pointer despite the inpcb lock being released.  This
117479bdc6e5SRobert Watson  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
117552cd27cbSRobert Watson  * but where the inpcb lock may already held, or when acquiring a reference
117652cd27cbSRobert Watson  * via a pcbgroup.
117779bdc6e5SRobert Watson  *
117879bdc6e5SRobert Watson  * in_pcbref() should be used only to provide brief memory stability, and
117979bdc6e5SRobert Watson  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
118079bdc6e5SRobert Watson  * garbage collect the inpcb if it has been in_pcbfree()'d from another
118179bdc6e5SRobert Watson  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
118279bdc6e5SRobert Watson  * lock and rele are the *only* safe operations that may be performed on the
118379bdc6e5SRobert Watson  * inpcb.
118479bdc6e5SRobert Watson  *
118579bdc6e5SRobert Watson  * While the inpcb will not be freed, releasing the inpcb lock means that the
118679bdc6e5SRobert Watson  * connection's state may change, so the caller should be careful to
118779bdc6e5SRobert Watson  * revalidate any cached state on reacquiring the lock.  Drop the reference
118879bdc6e5SRobert Watson  * using in_pcbrele().
1189c0a211c5SRobert Watson  */
119079bdc6e5SRobert Watson void
119179bdc6e5SRobert Watson in_pcbref(struct inpcb *inp)
11924c7c478dSRobert Watson {
1193df8bae1dSRodney W. Grimes 
119479bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
119579bdc6e5SRobert Watson 
119679bdc6e5SRobert Watson 	refcount_acquire(&inp->inp_refcount);
119779bdc6e5SRobert Watson }
119879bdc6e5SRobert Watson 
119979bdc6e5SRobert Watson /*
120079bdc6e5SRobert Watson  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
120179bdc6e5SRobert Watson  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
120279bdc6e5SRobert Watson  * return a flag indicating whether or not the inpcb remains valid.  If it is
120379bdc6e5SRobert Watson  * valid, we return with the inpcb lock held.
120479bdc6e5SRobert Watson  *
120579bdc6e5SRobert Watson  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
120679bdc6e5SRobert Watson  * reference on an inpcb.  Historically more work was done here (actually, in
120779bdc6e5SRobert Watson  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
120879bdc6e5SRobert Watson  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
120979bdc6e5SRobert Watson  * about memory stability (and continued use of the write lock).
121079bdc6e5SRobert Watson  */
121179bdc6e5SRobert Watson int
121279bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp)
121379bdc6e5SRobert Watson {
121479bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
121579bdc6e5SRobert Watson 
121679bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
121779bdc6e5SRobert Watson 
121879bdc6e5SRobert Watson 	INP_RLOCK_ASSERT(inp);
121979bdc6e5SRobert Watson 
1220df4e91d3SGleb Smirnoff 	if (refcount_release(&inp->inp_refcount) == 0) {
1221df4e91d3SGleb Smirnoff 		/*
1222df4e91d3SGleb Smirnoff 		 * If the inpcb has been freed, let the caller know, even if
1223df4e91d3SGleb Smirnoff 		 * this isn't the last reference.
1224df4e91d3SGleb Smirnoff 		 */
1225df4e91d3SGleb Smirnoff 		if (inp->inp_flags2 & INP_FREED) {
1226df4e91d3SGleb Smirnoff 			INP_RUNLOCK(inp);
1227df4e91d3SGleb Smirnoff 			return (1);
1228df4e91d3SGleb Smirnoff 		}
122979bdc6e5SRobert Watson 		return (0);
1230df4e91d3SGleb Smirnoff 	}
123179bdc6e5SRobert Watson 
123279bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1233*3ee9c3c4SRandall Stewart #ifdef TCPHPTS
1234*3ee9c3c4SRandall Stewart 	if (inp->inp_in_hpts || inp->inp_in_input) {
1235*3ee9c3c4SRandall Stewart 		struct tcp_hpts_entry *hpts;
1236*3ee9c3c4SRandall Stewart 		/*
1237*3ee9c3c4SRandall Stewart 		 * We should not be on the hpts at
1238*3ee9c3c4SRandall Stewart 		 * this point in any form. we must
1239*3ee9c3c4SRandall Stewart 		 * get the lock to be sure.
1240*3ee9c3c4SRandall Stewart 		 */
1241*3ee9c3c4SRandall Stewart 		hpts = tcp_hpts_lock(inp);
1242*3ee9c3c4SRandall Stewart 		if (inp->inp_in_hpts)
1243*3ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on hpts",
1244*3ee9c3c4SRandall Stewart 			      hpts, inp);
1245*3ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
1246*3ee9c3c4SRandall Stewart 		hpts = tcp_input_lock(inp);
1247*3ee9c3c4SRandall Stewart 		if (inp->inp_in_input)
1248*3ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on input hpts",
1249*3ee9c3c4SRandall Stewart 			      hpts, inp);
1250*3ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
1251*3ee9c3c4SRandall Stewart 	}
1252*3ee9c3c4SRandall Stewart #endif
125379bdc6e5SRobert Watson 	INP_RUNLOCK(inp);
125479bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
125579bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
125679bdc6e5SRobert Watson 	return (1);
125779bdc6e5SRobert Watson }
125879bdc6e5SRobert Watson 
125979bdc6e5SRobert Watson int
126079bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp)
126179bdc6e5SRobert Watson {
126279bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo;
126379bdc6e5SRobert Watson 
126479bdc6e5SRobert Watson 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
126579bdc6e5SRobert Watson 
126679bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
126779bdc6e5SRobert Watson 
1268edd0e0b0SFabien Thomas 	if (refcount_release(&inp->inp_refcount) == 0) {
1269edd0e0b0SFabien Thomas 		/*
1270edd0e0b0SFabien Thomas 		 * If the inpcb has been freed, let the caller know, even if
1271edd0e0b0SFabien Thomas 		 * this isn't the last reference.
1272edd0e0b0SFabien Thomas 		 */
1273edd0e0b0SFabien Thomas 		if (inp->inp_flags2 & INP_FREED) {
1274edd0e0b0SFabien Thomas 			INP_WUNLOCK(inp);
1275edd0e0b0SFabien Thomas 			return (1);
1276edd0e0b0SFabien Thomas 		}
127779bdc6e5SRobert Watson 		return (0);
1278edd0e0b0SFabien Thomas 	}
127979bdc6e5SRobert Watson 
128079bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1281*3ee9c3c4SRandall Stewart #ifdef TCPHPTS
1282*3ee9c3c4SRandall Stewart 	if (inp->inp_in_hpts || inp->inp_in_input) {
1283*3ee9c3c4SRandall Stewart 		struct tcp_hpts_entry *hpts;
1284*3ee9c3c4SRandall Stewart 		/*
1285*3ee9c3c4SRandall Stewart 		 * We should not be on the hpts at
1286*3ee9c3c4SRandall Stewart 		 * this point in any form. we must
1287*3ee9c3c4SRandall Stewart 		 * get the lock to be sure.
1288*3ee9c3c4SRandall Stewart 		 */
1289*3ee9c3c4SRandall Stewart 		hpts = tcp_hpts_lock(inp);
1290*3ee9c3c4SRandall Stewart 		if (inp->inp_in_hpts)
1291*3ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on hpts",
1292*3ee9c3c4SRandall Stewart 			      hpts, inp);
1293*3ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
1294*3ee9c3c4SRandall Stewart 		hpts = tcp_input_lock(inp);
1295*3ee9c3c4SRandall Stewart 		if (inp->inp_in_input)
1296*3ee9c3c4SRandall Stewart 			panic("Hpts:%p inp:%p at free still on input hpts",
1297*3ee9c3c4SRandall Stewart 			      hpts, inp);
1298*3ee9c3c4SRandall Stewart 		mtx_unlock(&hpts->p_mtx);
1299*3ee9c3c4SRandall Stewart 	}
1300*3ee9c3c4SRandall Stewart #endif
130179bdc6e5SRobert Watson 	INP_WUNLOCK(inp);
130279bdc6e5SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
130379bdc6e5SRobert Watson 	uma_zfree(pcbinfo->ipi_zone, inp);
130479bdc6e5SRobert Watson 	return (1);
130579bdc6e5SRobert Watson }
130679bdc6e5SRobert Watson 
130779bdc6e5SRobert Watson /*
130879bdc6e5SRobert Watson  * Temporary wrapper.
130979bdc6e5SRobert Watson  */
131079bdc6e5SRobert Watson int
131179bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp)
131279bdc6e5SRobert Watson {
131379bdc6e5SRobert Watson 
131479bdc6e5SRobert Watson 	return (in_pcbrele_wlocked(inp));
131579bdc6e5SRobert Watson }
131679bdc6e5SRobert Watson 
131779bdc6e5SRobert Watson /*
131879bdc6e5SRobert Watson  * Unconditionally schedule an inpcb to be freed by decrementing its
131979bdc6e5SRobert Watson  * reference count, which should occur only after the inpcb has been detached
132079bdc6e5SRobert Watson  * from its socket.  If another thread holds a temporary reference (acquired
132179bdc6e5SRobert Watson  * using in_pcbref()) then the free is deferred until that reference is
132279bdc6e5SRobert Watson  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
132379bdc6e5SRobert Watson  * work, including removal from global lists, is done in this context, where
132479bdc6e5SRobert Watson  * the pcbinfo lock is held.
132579bdc6e5SRobert Watson  */
132679bdc6e5SRobert Watson void
132779bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp)
132879bdc6e5SRobert Watson {
132979bdc6e5SRobert Watson 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
133079bdc6e5SRobert Watson 
133179bdc6e5SRobert Watson 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
133279bdc6e5SRobert Watson 
133345a48d07SJonathan T. Looney 	KASSERT((inp->inp_flags2 & INP_FREED) == 0,
133445a48d07SJonathan T. Looney 	    ("%s: called twice for pcb %p", __func__, inp));
133545a48d07SJonathan T. Looney 	if (inp->inp_flags2 & INP_FREED) {
133645a48d07SJonathan T. Looney 		INP_WUNLOCK(inp);
133745a48d07SJonathan T. Looney 		return;
133845a48d07SJonathan T. Looney 	}
133945a48d07SJonathan T. Looney 
1340ff9b006dSJulien Charbon #ifdef INVARIANTS
1341ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
1342079672cbSJulien Charbon 		INP_INFO_LOCK_ASSERT(pcbinfo);
1343ff9b006dSJulien Charbon 	} else {
134479bdc6e5SRobert Watson 		INP_INFO_WLOCK_ASSERT(pcbinfo);
1345ff9b006dSJulien Charbon 	}
1346ff9b006dSJulien Charbon #endif
134779bdc6e5SRobert Watson 	INP_WLOCK_ASSERT(inp);
134879bdc6e5SRobert Watson 
134979bdc6e5SRobert Watson 	/* XXXRW: Do as much as possible here. */
1350fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13516aee2fc5SBjoern A. Zeeb 	if (inp->inp_sp != NULL)
13526974bd9eSBjoern A. Zeeb 		ipsec_delete_pcbpolicy(inp);
135383e521ecSBjoern A. Zeeb #endif
1354ff9b006dSJulien Charbon 	INP_LIST_WLOCK(pcbinfo);
135579bdc6e5SRobert Watson 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1356c3229e05SDavid Greenman 	in_pcbremlists(inp);
1357ff9b006dSJulien Charbon 	INP_LIST_WUNLOCK(pcbinfo);
13586aee2fc5SBjoern A. Zeeb #ifdef INET6
13596aee2fc5SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
13606aee2fc5SBjoern A. Zeeb 		ip6_freepcbopts(inp->in6p_outputopts);
13611096332aSBruce M Simpson 		if (inp->in6p_moptions != NULL)
13626aee2fc5SBjoern A. Zeeb 			ip6_freemoptions(inp->in6p_moptions);
13636aee2fc5SBjoern A. Zeeb 	}
13646aee2fc5SBjoern A. Zeeb #endif
1365df8bae1dSRodney W. Grimes 	if (inp->inp_options)
1366df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
136767107f45SBjoern A. Zeeb #ifdef INET
136871498f30SBruce M Simpson 	if (inp->inp_moptions != NULL)
136971498f30SBruce M Simpson 		inp_freemoptions(inp->inp_moptions);
137067107f45SBjoern A. Zeeb #endif
1371fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
137284cc0778SGeorge V. Neville-Neil 
1373cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag = 0;
1374df4e91d3SGleb Smirnoff 	inp->inp_flags2 |= INP_FREED;
137586d02c5cSBjoern A. Zeeb 	crfree(inp->inp_cred);
1376a557af22SRobert Watson #ifdef MAC
137730d239bcSRobert Watson 	mac_inpcb_destroy(inp);
1378a557af22SRobert Watson #endif
137979bdc6e5SRobert Watson 	if (!in_pcbrele_wlocked(inp))
138028696211SRobert Watson 		INP_WUNLOCK(inp);
138128696211SRobert Watson }
138228696211SRobert Watson 
138328696211SRobert Watson /*
1384c0a211c5SRobert Watson  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1385c0a211c5SRobert Watson  * port reservation, and preventing it from being returned by inpcb lookups.
1386c0a211c5SRobert Watson  *
1387c0a211c5SRobert Watson  * It is used by TCP to mark an inpcb as unused and avoid future packet
1388c0a211c5SRobert Watson  * delivery or event notification when a socket remains open but TCP has
1389c0a211c5SRobert Watson  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1390c0a211c5SRobert Watson  * or a RST on the wire, and allows the port binding to be reused while still
1391c0a211c5SRobert Watson  * maintaining the invariant that so_pcb always points to a valid inpcb until
1392c0a211c5SRobert Watson  * in_pcbdetach().
1393c0a211c5SRobert Watson  *
1394c0a211c5SRobert Watson  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1395c0a211c5SRobert Watson  * in_pcbnotifyall() and in_pcbpurgeif0()?
139610702a28SRobert Watson  */
139710702a28SRobert Watson void
139810702a28SRobert Watson in_pcbdrop(struct inpcb *inp)
139910702a28SRobert Watson {
140010702a28SRobert Watson 
14018501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
140210702a28SRobert Watson 
1403fa046d87SRobert Watson 	/*
1404fa046d87SRobert Watson 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1405fa046d87SRobert Watson 	 * the hash lock...?
1406fa046d87SRobert Watson 	 */
1407ad71fe3cSRobert Watson 	inp->inp_flags |= INP_DROPPED;
1408111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
140910702a28SRobert Watson 		struct inpcbport *phd = inp->inp_phd;
141010702a28SRobert Watson 
1411fa046d87SRobert Watson 		INP_HASH_WLOCK(inp->inp_pcbinfo);
141210702a28SRobert Watson 		LIST_REMOVE(inp, inp_hash);
141310702a28SRobert Watson 		LIST_REMOVE(inp, inp_portlist);
141410702a28SRobert Watson 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
141510702a28SRobert Watson 			LIST_REMOVE(phd, phd_hash);
141610702a28SRobert Watson 			free(phd, M_PCB);
141710702a28SRobert Watson 		}
1418fa046d87SRobert Watson 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1419111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
142052cd27cbSRobert Watson #ifdef PCBGROUP
142152cd27cbSRobert Watson 		in_pcbgroup_remove(inp);
142252cd27cbSRobert Watson #endif
142310702a28SRobert Watson 	}
142410702a28SRobert Watson }
142510702a28SRobert Watson 
142667107f45SBjoern A. Zeeb #ifdef INET
142754d642bbSRobert Watson /*
142854d642bbSRobert Watson  * Common routines to return the socket addresses associated with inpcbs.
142954d642bbSRobert Watson  */
143026ef6ac4SDon Lewis struct sockaddr *
1431136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p)
143226ef6ac4SDon Lewis {
143326ef6ac4SDon Lewis 	struct sockaddr_in *sin;
143426ef6ac4SDon Lewis 
14351ede983cSDag-Erling Smørgrav 	sin = malloc(sizeof *sin, M_SONAME,
1436a163d034SWarner Losh 		M_WAITOK | M_ZERO);
143726ef6ac4SDon Lewis 	sin->sin_family = AF_INET;
143826ef6ac4SDon Lewis 	sin->sin_len = sizeof(*sin);
143926ef6ac4SDon Lewis 	sin->sin_addr = *addr_p;
144026ef6ac4SDon Lewis 	sin->sin_port = port;
144126ef6ac4SDon Lewis 
144226ef6ac4SDon Lewis 	return (struct sockaddr *)sin;
144326ef6ac4SDon Lewis }
144426ef6ac4SDon Lewis 
1445117bcae7SGarrett Wollman int
144654d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam)
1447df8bae1dSRodney W. Grimes {
1448136d4f1cSRobert Watson 	struct inpcb *inp;
144926ef6ac4SDon Lewis 	struct in_addr addr;
145026ef6ac4SDon Lewis 	in_port_t port;
145142fa505bSDavid Greenman 
1452fdc984f7STor Egge 	inp = sotoinpcb(so);
145354d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
14546466b28aSRobert Watson 
1455a69042a5SRobert Watson 	INP_RLOCK(inp);
145626ef6ac4SDon Lewis 	port = inp->inp_lport;
145726ef6ac4SDon Lewis 	addr = inp->inp_laddr;
1458a69042a5SRobert Watson 	INP_RUNLOCK(inp);
145942fa505bSDavid Greenman 
146026ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1461117bcae7SGarrett Wollman 	return 0;
1462df8bae1dSRodney W. Grimes }
1463df8bae1dSRodney W. Grimes 
1464117bcae7SGarrett Wollman int
146554d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1466df8bae1dSRodney W. Grimes {
1467136d4f1cSRobert Watson 	struct inpcb *inp;
146826ef6ac4SDon Lewis 	struct in_addr addr;
146926ef6ac4SDon Lewis 	in_port_t port;
147042fa505bSDavid Greenman 
1471fdc984f7STor Egge 	inp = sotoinpcb(so);
147254d642bbSRobert Watson 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
14736466b28aSRobert Watson 
1474a69042a5SRobert Watson 	INP_RLOCK(inp);
147526ef6ac4SDon Lewis 	port = inp->inp_fport;
147626ef6ac4SDon Lewis 	addr = inp->inp_faddr;
1477a69042a5SRobert Watson 	INP_RUNLOCK(inp);
147842fa505bSDavid Greenman 
147926ef6ac4SDon Lewis 	*nam = in_sockaddr(port, &addr);
1480117bcae7SGarrett Wollman 	return 0;
1481df8bae1dSRodney W. Grimes }
1482df8bae1dSRodney W. Grimes 
148326f9a767SRodney W. Grimes void
1484136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1485136d4f1cSRobert Watson     struct inpcb *(*notify)(struct inpcb *, int))
1486d1c54148SJesper Skriver {
1487f457d580SRobert Watson 	struct inpcb *inp, *inp_temp;
1488d1c54148SJesper Skriver 
14893dc7ebf9SJeffrey Hsu 	INP_INFO_WLOCK(pcbinfo);
1490f457d580SRobert Watson 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
14918501a69cSRobert Watson 		INP_WLOCK(inp);
1492d1c54148SJesper Skriver #ifdef INET6
1493f76fcf6dSJeffrey Hsu 		if ((inp->inp_vflag & INP_IPV4) == 0) {
14948501a69cSRobert Watson 			INP_WUNLOCK(inp);
1495d1c54148SJesper Skriver 			continue;
1496f76fcf6dSJeffrey Hsu 		}
1497d1c54148SJesper Skriver #endif
1498d1c54148SJesper Skriver 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1499f76fcf6dSJeffrey Hsu 		    inp->inp_socket == NULL) {
15008501a69cSRobert Watson 			INP_WUNLOCK(inp);
1501d1c54148SJesper Skriver 			continue;
1502d1c54148SJesper Skriver 		}
15033dc7ebf9SJeffrey Hsu 		if ((*notify)(inp, errno))
15048501a69cSRobert Watson 			INP_WUNLOCK(inp);
1505f76fcf6dSJeffrey Hsu 	}
15063dc7ebf9SJeffrey Hsu 	INP_INFO_WUNLOCK(pcbinfo);
1507d1c54148SJesper Skriver }
1508d1c54148SJesper Skriver 
1509e43cc4aeSHajimu UMEMOTO void
1510136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1511e43cc4aeSHajimu UMEMOTO {
1512e43cc4aeSHajimu UMEMOTO 	struct inpcb *inp;
1513e43cc4aeSHajimu UMEMOTO 	struct ip_moptions *imo;
1514e43cc4aeSHajimu UMEMOTO 	int i, gap;
1515e43cc4aeSHajimu UMEMOTO 
1516ff9b006dSJulien Charbon 	INP_INFO_WLOCK(pcbinfo);
1517712fc218SRobert Watson 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
15188501a69cSRobert Watson 		INP_WLOCK(inp);
1519e43cc4aeSHajimu UMEMOTO 		imo = inp->inp_moptions;
1520e43cc4aeSHajimu UMEMOTO 		if ((inp->inp_vflag & INP_IPV4) &&
1521e43cc4aeSHajimu UMEMOTO 		    imo != NULL) {
1522e43cc4aeSHajimu UMEMOTO 			/*
1523e43cc4aeSHajimu UMEMOTO 			 * Unselect the outgoing interface if it is being
1524e43cc4aeSHajimu UMEMOTO 			 * detached.
1525e43cc4aeSHajimu UMEMOTO 			 */
1526e43cc4aeSHajimu UMEMOTO 			if (imo->imo_multicast_ifp == ifp)
1527e43cc4aeSHajimu UMEMOTO 				imo->imo_multicast_ifp = NULL;
1528e43cc4aeSHajimu UMEMOTO 
1529e43cc4aeSHajimu UMEMOTO 			/*
1530e43cc4aeSHajimu UMEMOTO 			 * Drop multicast group membership if we joined
1531e43cc4aeSHajimu UMEMOTO 			 * through the interface being detached.
1532e43cc4aeSHajimu UMEMOTO 			 */
1533e43cc4aeSHajimu UMEMOTO 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1534e43cc4aeSHajimu UMEMOTO 			    i++) {
1535e43cc4aeSHajimu UMEMOTO 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1536e43cc4aeSHajimu UMEMOTO 					in_delmulti(imo->imo_membership[i]);
1537e43cc4aeSHajimu UMEMOTO 					gap++;
1538e43cc4aeSHajimu UMEMOTO 				} else if (gap != 0)
1539e43cc4aeSHajimu UMEMOTO 					imo->imo_membership[i - gap] =
1540e43cc4aeSHajimu UMEMOTO 					    imo->imo_membership[i];
1541e43cc4aeSHajimu UMEMOTO 			}
1542e43cc4aeSHajimu UMEMOTO 			imo->imo_num_memberships -= gap;
1543e43cc4aeSHajimu UMEMOTO 		}
15448501a69cSRobert Watson 		INP_WUNLOCK(inp);
1545e43cc4aeSHajimu UMEMOTO 	}
1546ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(pcbinfo);
1547e43cc4aeSHajimu UMEMOTO }
1548e43cc4aeSHajimu UMEMOTO 
1549df8bae1dSRodney W. Grimes /*
1550fa046d87SRobert Watson  * Lookup a PCB based on the local address and port.  Caller must hold the
1551fa046d87SRobert Watson  * hash lock.  No inpcb locks or references are acquired.
1552c3229e05SDavid Greenman  */
1553d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST	3
1554df8bae1dSRodney W. Grimes struct inpcb *
1555136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
155668e0d7e0SRobert Watson     u_short lport, int lookupflags, struct ucred *cred)
1557df8bae1dSRodney W. Grimes {
1558136d4f1cSRobert Watson 	struct inpcb *inp;
1559d5e8a67eSHajimu UMEMOTO #ifdef INET6
1560d5e8a67eSHajimu UMEMOTO 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1561d5e8a67eSHajimu UMEMOTO #else
1562d5e8a67eSHajimu UMEMOTO 	int matchwild = 3;
1563d5e8a67eSHajimu UMEMOTO #endif
1564d5e8a67eSHajimu UMEMOTO 	int wildcard;
15657bc4aca7SDavid Greenman 
156668e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
156768e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
156868e0d7e0SRobert Watson 
1569fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
15701b73ca0bSSam Leffler 
157168e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1572c3229e05SDavid Greenman 		struct inpcbhead *head;
1573c3229e05SDavid Greenman 		/*
1574c3229e05SDavid Greenman 		 * Look for an unconnected (wildcard foreign addr) PCB that
1575c3229e05SDavid Greenman 		 * matches the local address and port we're looking for.
1576c3229e05SDavid Greenman 		 */
1577712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1578712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1579fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1580cfa1ca9dSYoshinobu Inoue #ifdef INET6
1581413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1582369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1583cfa1ca9dSYoshinobu Inoue 				continue;
1584cfa1ca9dSYoshinobu Inoue #endif
1585c3229e05SDavid Greenman 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1586c3229e05SDavid Greenman 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1587c3229e05SDavid Greenman 			    inp->inp_lport == lport) {
1588c3229e05SDavid Greenman 				/*
1589413628a7SBjoern A. Zeeb 				 * Found?
1590c3229e05SDavid Greenman 				 */
1591413628a7SBjoern A. Zeeb 				if (cred == NULL ||
15920304c731SJamie Gritton 				    prison_equal_ip4(cred->cr_prison,
15930304c731SJamie Gritton 					inp->inp_cred->cr_prison))
1594c3229e05SDavid Greenman 					return (inp);
1595df8bae1dSRodney W. Grimes 			}
1596c3229e05SDavid Greenman 		}
1597c3229e05SDavid Greenman 		/*
1598c3229e05SDavid Greenman 		 * Not found.
1599c3229e05SDavid Greenman 		 */
1600c3229e05SDavid Greenman 		return (NULL);
1601c3229e05SDavid Greenman 	} else {
1602c3229e05SDavid Greenman 		struct inpcbporthead *porthash;
1603c3229e05SDavid Greenman 		struct inpcbport *phd;
1604c3229e05SDavid Greenman 		struct inpcb *match = NULL;
1605c3229e05SDavid Greenman 		/*
1606c3229e05SDavid Greenman 		 * Best fit PCB lookup.
1607c3229e05SDavid Greenman 		 *
1608c3229e05SDavid Greenman 		 * First see if this local port is in use by looking on the
1609c3229e05SDavid Greenman 		 * port hash list.
1610c3229e05SDavid Greenman 		 */
1611712fc218SRobert Watson 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1612712fc218SRobert Watson 		    pcbinfo->ipi_porthashmask)];
1613fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(phd, porthash, phd_hash) {
1614c3229e05SDavid Greenman 			if (phd->phd_port == lport)
1615c3229e05SDavid Greenman 				break;
1616c3229e05SDavid Greenman 		}
1617c3229e05SDavid Greenman 		if (phd != NULL) {
1618c3229e05SDavid Greenman 			/*
1619c3229e05SDavid Greenman 			 * Port is in use by one or more PCBs. Look for best
1620c3229e05SDavid Greenman 			 * fit.
1621c3229e05SDavid Greenman 			 */
162237d40066SPoul-Henning Kamp 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1623c3229e05SDavid Greenman 				wildcard = 0;
1624413628a7SBjoern A. Zeeb 				if (cred != NULL &&
16250304c731SJamie Gritton 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
16260304c731SJamie Gritton 					cred->cr_prison))
1627413628a7SBjoern A. Zeeb 					continue;
1628cfa1ca9dSYoshinobu Inoue #ifdef INET6
1629413628a7SBjoern A. Zeeb 				/* XXX inp locking */
1630369dc8ceSEivind Eklund 				if ((inp->inp_vflag & INP_IPV4) == 0)
1631cfa1ca9dSYoshinobu Inoue 					continue;
1632d5e8a67eSHajimu UMEMOTO 				/*
1633d5e8a67eSHajimu UMEMOTO 				 * We never select the PCB that has
1634d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag and is bound to :: if
1635d5e8a67eSHajimu UMEMOTO 				 * we have another PCB which is bound
1636d5e8a67eSHajimu UMEMOTO 				 * to 0.0.0.0.  If a PCB has the
1637d5e8a67eSHajimu UMEMOTO 				 * INP_IPV6 flag, then we set its cost
1638d5e8a67eSHajimu UMEMOTO 				 * higher than IPv4 only PCBs.
1639d5e8a67eSHajimu UMEMOTO 				 *
1640d5e8a67eSHajimu UMEMOTO 				 * Note that the case only happens
1641d5e8a67eSHajimu UMEMOTO 				 * when a socket is bound to ::, under
1642d5e8a67eSHajimu UMEMOTO 				 * the condition that the use of the
1643d5e8a67eSHajimu UMEMOTO 				 * mapped address is allowed.
1644d5e8a67eSHajimu UMEMOTO 				 */
1645d5e8a67eSHajimu UMEMOTO 				if ((inp->inp_vflag & INP_IPV6) != 0)
1646d5e8a67eSHajimu UMEMOTO 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1647cfa1ca9dSYoshinobu Inoue #endif
1648c3229e05SDavid Greenman 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1649c3229e05SDavid Greenman 					wildcard++;
165015bd2b43SDavid Greenman 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
165115bd2b43SDavid Greenman 					if (laddr.s_addr == INADDR_ANY)
165215bd2b43SDavid Greenman 						wildcard++;
165315bd2b43SDavid Greenman 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
165415bd2b43SDavid Greenman 						continue;
165515bd2b43SDavid Greenman 				} else {
165615bd2b43SDavid Greenman 					if (laddr.s_addr != INADDR_ANY)
165715bd2b43SDavid Greenman 						wildcard++;
165815bd2b43SDavid Greenman 				}
1659df8bae1dSRodney W. Grimes 				if (wildcard < matchwild) {
1660df8bae1dSRodney W. Grimes 					match = inp;
1661df8bae1dSRodney W. Grimes 					matchwild = wildcard;
1662413628a7SBjoern A. Zeeb 					if (matchwild == 0)
1663df8bae1dSRodney W. Grimes 						break;
1664df8bae1dSRodney W. Grimes 				}
1665df8bae1dSRodney W. Grimes 			}
16663dbdc25cSDavid Greenman 		}
1667df8bae1dSRodney W. Grimes 		return (match);
1668df8bae1dSRodney W. Grimes 	}
1669c3229e05SDavid Greenman }
1670d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST
167115bd2b43SDavid Greenman 
167252cd27cbSRobert Watson #ifdef PCBGROUP
167352cd27cbSRobert Watson /*
167452cd27cbSRobert Watson  * Lookup PCB in hash list, using pcbgroup tables.
167552cd27cbSRobert Watson  */
167652cd27cbSRobert Watson static struct inpcb *
167752cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
167852cd27cbSRobert Watson     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
167952cd27cbSRobert Watson     u_int lport_arg, int lookupflags, struct ifnet *ifp)
168052cd27cbSRobert Watson {
168152cd27cbSRobert Watson 	struct inpcbhead *head;
168252cd27cbSRobert Watson 	struct inpcb *inp, *tmpinp;
168352cd27cbSRobert Watson 	u_short fport = fport_arg, lport = lport_arg;
16847fb2986fSJonathan T. Looney 	bool locked;
168552cd27cbSRobert Watson 
168652cd27cbSRobert Watson 	/*
168752cd27cbSRobert Watson 	 * First look for an exact match.
168852cd27cbSRobert Watson 	 */
168952cd27cbSRobert Watson 	tmpinp = NULL;
169052cd27cbSRobert Watson 	INP_GROUP_LOCK(pcbgroup);
169152cd27cbSRobert Watson 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
169252cd27cbSRobert Watson 	    pcbgroup->ipg_hashmask)];
169352cd27cbSRobert Watson 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
169452cd27cbSRobert Watson #ifdef INET6
169552cd27cbSRobert Watson 		/* XXX inp locking */
169652cd27cbSRobert Watson 		if ((inp->inp_vflag & INP_IPV4) == 0)
169752cd27cbSRobert Watson 			continue;
169852cd27cbSRobert Watson #endif
169952cd27cbSRobert Watson 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
170052cd27cbSRobert Watson 		    inp->inp_laddr.s_addr == laddr.s_addr &&
170152cd27cbSRobert Watson 		    inp->inp_fport == fport &&
170252cd27cbSRobert Watson 		    inp->inp_lport == lport) {
170352cd27cbSRobert Watson 			/*
170452cd27cbSRobert Watson 			 * XXX We should be able to directly return
170552cd27cbSRobert Watson 			 * the inp here, without any checks.
170652cd27cbSRobert Watson 			 * Well unless both bound with SO_REUSEPORT?
170752cd27cbSRobert Watson 			 */
170852cd27cbSRobert Watson 			if (prison_flag(inp->inp_cred, PR_IP4))
170952cd27cbSRobert Watson 				goto found;
171052cd27cbSRobert Watson 			if (tmpinp == NULL)
171152cd27cbSRobert Watson 				tmpinp = inp;
171252cd27cbSRobert Watson 		}
171352cd27cbSRobert Watson 	}
171452cd27cbSRobert Watson 	if (tmpinp != NULL) {
171552cd27cbSRobert Watson 		inp = tmpinp;
171652cd27cbSRobert Watson 		goto found;
171752cd27cbSRobert Watson 	}
171852cd27cbSRobert Watson 
17190a100a6fSAdrian Chadd #ifdef	RSS
17200a100a6fSAdrian Chadd 	/*
17210a100a6fSAdrian Chadd 	 * For incoming connections, we may wish to do a wildcard
17220a100a6fSAdrian Chadd 	 * match for an RSS-local socket.
17230a100a6fSAdrian Chadd 	 */
17240a100a6fSAdrian Chadd 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
17250a100a6fSAdrian Chadd 		struct inpcb *local_wild = NULL, *local_exact = NULL;
17260a100a6fSAdrian Chadd #ifdef INET6
17270a100a6fSAdrian Chadd 		struct inpcb *local_wild_mapped = NULL;
17280a100a6fSAdrian Chadd #endif
17290a100a6fSAdrian Chadd 		struct inpcb *jail_wild = NULL;
17300a100a6fSAdrian Chadd 		struct inpcbhead *head;
17310a100a6fSAdrian Chadd 		int injail;
17320a100a6fSAdrian Chadd 
17330a100a6fSAdrian Chadd 		/*
17340a100a6fSAdrian Chadd 		 * Order of socket selection - we always prefer jails.
17350a100a6fSAdrian Chadd 		 *      1. jailed, non-wild.
17360a100a6fSAdrian Chadd 		 *      2. jailed, wild.
17370a100a6fSAdrian Chadd 		 *      3. non-jailed, non-wild.
17380a100a6fSAdrian Chadd 		 *      4. non-jailed, wild.
17390a100a6fSAdrian Chadd 		 */
17400a100a6fSAdrian Chadd 
17410a100a6fSAdrian Chadd 		head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
17420a100a6fSAdrian Chadd 		    lport, 0, pcbgroup->ipg_hashmask)];
17430a100a6fSAdrian Chadd 		LIST_FOREACH(inp, head, inp_pcbgrouphash) {
17440a100a6fSAdrian Chadd #ifdef INET6
17450a100a6fSAdrian Chadd 			/* XXX inp locking */
17460a100a6fSAdrian Chadd 			if ((inp->inp_vflag & INP_IPV4) == 0)
17470a100a6fSAdrian Chadd 				continue;
17480a100a6fSAdrian Chadd #endif
17490a100a6fSAdrian Chadd 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
17500a100a6fSAdrian Chadd 			    inp->inp_lport != lport)
17510a100a6fSAdrian Chadd 				continue;
17520a100a6fSAdrian Chadd 
17530a100a6fSAdrian Chadd 			injail = prison_flag(inp->inp_cred, PR_IP4);
17540a100a6fSAdrian Chadd 			if (injail) {
17550a100a6fSAdrian Chadd 				if (prison_check_ip4(inp->inp_cred,
17560a100a6fSAdrian Chadd 				    &laddr) != 0)
17570a100a6fSAdrian Chadd 					continue;
17580a100a6fSAdrian Chadd 			} else {
17590a100a6fSAdrian Chadd 				if (local_exact != NULL)
17600a100a6fSAdrian Chadd 					continue;
17610a100a6fSAdrian Chadd 			}
17620a100a6fSAdrian Chadd 
17630a100a6fSAdrian Chadd 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
17640a100a6fSAdrian Chadd 				if (injail)
17650a100a6fSAdrian Chadd 					goto found;
17660a100a6fSAdrian Chadd 				else
17670a100a6fSAdrian Chadd 					local_exact = inp;
17680a100a6fSAdrian Chadd 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
17690a100a6fSAdrian Chadd #ifdef INET6
17700a100a6fSAdrian Chadd 				/* XXX inp locking, NULL check */
17710a100a6fSAdrian Chadd 				if (inp->inp_vflag & INP_IPV6PROTO)
17720a100a6fSAdrian Chadd 					local_wild_mapped = inp;
17730a100a6fSAdrian Chadd 				else
17740a100a6fSAdrian Chadd #endif
17750a100a6fSAdrian Chadd 					if (injail)
17760a100a6fSAdrian Chadd 						jail_wild = inp;
17770a100a6fSAdrian Chadd 					else
17780a100a6fSAdrian Chadd 						local_wild = inp;
17790a100a6fSAdrian Chadd 			}
17800a100a6fSAdrian Chadd 		} /* LIST_FOREACH */
17810a100a6fSAdrian Chadd 
17820a100a6fSAdrian Chadd 		inp = jail_wild;
17830a100a6fSAdrian Chadd 		if (inp == NULL)
17840a100a6fSAdrian Chadd 			inp = local_exact;
17850a100a6fSAdrian Chadd 		if (inp == NULL)
17860a100a6fSAdrian Chadd 			inp = local_wild;
17870a100a6fSAdrian Chadd #ifdef INET6
17880a100a6fSAdrian Chadd 		if (inp == NULL)
17890a100a6fSAdrian Chadd 			inp = local_wild_mapped;
17900a100a6fSAdrian Chadd #endif
17910a100a6fSAdrian Chadd 		if (inp != NULL)
17920a100a6fSAdrian Chadd 			goto found;
17930a100a6fSAdrian Chadd 	}
17940a100a6fSAdrian Chadd #endif
17950a100a6fSAdrian Chadd 
179652cd27cbSRobert Watson 	/*
179752cd27cbSRobert Watson 	 * Then look for a wildcard match, if requested.
179852cd27cbSRobert Watson 	 */
179952cd27cbSRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
180052cd27cbSRobert Watson 		struct inpcb *local_wild = NULL, *local_exact = NULL;
180152cd27cbSRobert Watson #ifdef INET6
180252cd27cbSRobert Watson 		struct inpcb *local_wild_mapped = NULL;
180352cd27cbSRobert Watson #endif
180452cd27cbSRobert Watson 		struct inpcb *jail_wild = NULL;
180552cd27cbSRobert Watson 		struct inpcbhead *head;
180652cd27cbSRobert Watson 		int injail;
180752cd27cbSRobert Watson 
180852cd27cbSRobert Watson 		/*
180952cd27cbSRobert Watson 		 * Order of socket selection - we always prefer jails.
181052cd27cbSRobert Watson 		 *      1. jailed, non-wild.
181152cd27cbSRobert Watson 		 *      2. jailed, wild.
181252cd27cbSRobert Watson 		 *      3. non-jailed, non-wild.
181352cd27cbSRobert Watson 		 *      4. non-jailed, wild.
181452cd27cbSRobert Watson 		 */
181552cd27cbSRobert Watson 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
181652cd27cbSRobert Watson 		    0, pcbinfo->ipi_wildmask)];
181752cd27cbSRobert Watson 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
181852cd27cbSRobert Watson #ifdef INET6
181952cd27cbSRobert Watson 			/* XXX inp locking */
182052cd27cbSRobert Watson 			if ((inp->inp_vflag & INP_IPV4) == 0)
182152cd27cbSRobert Watson 				continue;
182252cd27cbSRobert Watson #endif
182352cd27cbSRobert Watson 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
182452cd27cbSRobert Watson 			    inp->inp_lport != lport)
182552cd27cbSRobert Watson 				continue;
182652cd27cbSRobert Watson 
182752cd27cbSRobert Watson 			injail = prison_flag(inp->inp_cred, PR_IP4);
182852cd27cbSRobert Watson 			if (injail) {
182952cd27cbSRobert Watson 				if (prison_check_ip4(inp->inp_cred,
183052cd27cbSRobert Watson 				    &laddr) != 0)
183152cd27cbSRobert Watson 					continue;
183252cd27cbSRobert Watson 			} else {
183352cd27cbSRobert Watson 				if (local_exact != NULL)
183452cd27cbSRobert Watson 					continue;
183552cd27cbSRobert Watson 			}
183652cd27cbSRobert Watson 
183752cd27cbSRobert Watson 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
183852cd27cbSRobert Watson 				if (injail)
183952cd27cbSRobert Watson 					goto found;
184052cd27cbSRobert Watson 				else
184152cd27cbSRobert Watson 					local_exact = inp;
184252cd27cbSRobert Watson 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
184352cd27cbSRobert Watson #ifdef INET6
184452cd27cbSRobert Watson 				/* XXX inp locking, NULL check */
184552cd27cbSRobert Watson 				if (inp->inp_vflag & INP_IPV6PROTO)
184652cd27cbSRobert Watson 					local_wild_mapped = inp;
184752cd27cbSRobert Watson 				else
184883e521ecSBjoern A. Zeeb #endif
184952cd27cbSRobert Watson 					if (injail)
185052cd27cbSRobert Watson 						jail_wild = inp;
185152cd27cbSRobert Watson 					else
185252cd27cbSRobert Watson 						local_wild = inp;
185352cd27cbSRobert Watson 			}
185452cd27cbSRobert Watson 		} /* LIST_FOREACH */
185552cd27cbSRobert Watson 		inp = jail_wild;
185652cd27cbSRobert Watson 		if (inp == NULL)
185752cd27cbSRobert Watson 			inp = local_exact;
185852cd27cbSRobert Watson 		if (inp == NULL)
185952cd27cbSRobert Watson 			inp = local_wild;
186052cd27cbSRobert Watson #ifdef INET6
186152cd27cbSRobert Watson 		if (inp == NULL)
186252cd27cbSRobert Watson 			inp = local_wild_mapped;
186383e521ecSBjoern A. Zeeb #endif
186452cd27cbSRobert Watson 		if (inp != NULL)
186552cd27cbSRobert Watson 			goto found;
186652cd27cbSRobert Watson 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
186752cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
186852cd27cbSRobert Watson 	return (NULL);
186952cd27cbSRobert Watson 
187052cd27cbSRobert Watson found:
18717fb2986fSJonathan T. Looney 	if (lookupflags & INPLOOKUP_WLOCKPCB)
1872b9a9e8e9SNavdeep Parhar 		locked = INP_TRY_WLOCK(inp);
18737fb2986fSJonathan T. Looney 	else if (lookupflags & INPLOOKUP_RLOCKPCB)
1874b9a9e8e9SNavdeep Parhar 		locked = INP_TRY_RLOCK(inp);
18757fb2986fSJonathan T. Looney 	else
18767fb2986fSJonathan T. Looney 		panic("%s: locking bug", __func__);
18777fb2986fSJonathan T. Looney 	if (!locked)
187852cd27cbSRobert Watson 		in_pcbref(inp);
187952cd27cbSRobert Watson 	INP_GROUP_UNLOCK(pcbgroup);
18807fb2986fSJonathan T. Looney 	if (!locked) {
188152cd27cbSRobert Watson 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
188252cd27cbSRobert Watson 			INP_WLOCK(inp);
188352cd27cbSRobert Watson 			if (in_pcbrele_wlocked(inp))
188452cd27cbSRobert Watson 				return (NULL);
18857fb2986fSJonathan T. Looney 		} else {
188652cd27cbSRobert Watson 			INP_RLOCK(inp);
188752cd27cbSRobert Watson 			if (in_pcbrele_rlocked(inp))
188852cd27cbSRobert Watson 				return (NULL);
18897fb2986fSJonathan T. Looney 		}
18907fb2986fSJonathan T. Looney 	}
18917fb2986fSJonathan T. Looney #ifdef INVARIANTS
18927fb2986fSJonathan T. Looney 	if (lookupflags & INPLOOKUP_WLOCKPCB)
18937fb2986fSJonathan T. Looney 		INP_WLOCK_ASSERT(inp);
18947fb2986fSJonathan T. Looney 	else
18957fb2986fSJonathan T. Looney 		INP_RLOCK_ASSERT(inp);
18967fb2986fSJonathan T. Looney #endif
189752cd27cbSRobert Watson 	return (inp);
189852cd27cbSRobert Watson }
189952cd27cbSRobert Watson #endif /* PCBGROUP */
190052cd27cbSRobert Watson 
190115bd2b43SDavid Greenman /*
1902fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1903fa046d87SRobert Watson  * that the caller has locked the hash list, and will not perform any further
1904fa046d87SRobert Watson  * locking or reference operations on either the hash list or the connection.
190515bd2b43SDavid Greenman  */
1906fa046d87SRobert Watson static struct inpcb *
1907fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
190868e0d7e0SRobert Watson     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1909136d4f1cSRobert Watson     struct ifnet *ifp)
191015bd2b43SDavid Greenman {
191115bd2b43SDavid Greenman 	struct inpcbhead *head;
1912413628a7SBjoern A. Zeeb 	struct inpcb *inp, *tmpinp;
191315bd2b43SDavid Greenman 	u_short fport = fport_arg, lport = lport_arg;
191415bd2b43SDavid Greenman 
191568e0d7e0SRobert Watson 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
191668e0d7e0SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
191768e0d7e0SRobert Watson 
1918fa046d87SRobert Watson 	INP_HASH_LOCK_ASSERT(pcbinfo);
1919602cc7f1SRobert Watson 
192015bd2b43SDavid Greenman 	/*
192115bd2b43SDavid Greenman 	 * First look for an exact match.
192215bd2b43SDavid Greenman 	 */
1923413628a7SBjoern A. Zeeb 	tmpinp = NULL;
1924712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1925712fc218SRobert Watson 	    pcbinfo->ipi_hashmask)];
1926fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(inp, head, inp_hash) {
1927cfa1ca9dSYoshinobu Inoue #ifdef INET6
1928413628a7SBjoern A. Zeeb 		/* XXX inp locking */
1929369dc8ceSEivind Eklund 		if ((inp->inp_vflag & INP_IPV4) == 0)
1930cfa1ca9dSYoshinobu Inoue 			continue;
1931cfa1ca9dSYoshinobu Inoue #endif
19326d6a026bSDavid Greenman 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1933ca98b82cSDavid Greenman 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1934ca98b82cSDavid Greenman 		    inp->inp_fport == fport &&
1935413628a7SBjoern A. Zeeb 		    inp->inp_lport == lport) {
1936413628a7SBjoern A. Zeeb 			/*
1937413628a7SBjoern A. Zeeb 			 * XXX We should be able to directly return
1938413628a7SBjoern A. Zeeb 			 * the inp here, without any checks.
1939413628a7SBjoern A. Zeeb 			 * Well unless both bound with SO_REUSEPORT?
1940413628a7SBjoern A. Zeeb 			 */
19410304c731SJamie Gritton 			if (prison_flag(inp->inp_cred, PR_IP4))
1942c3229e05SDavid Greenman 				return (inp);
1943413628a7SBjoern A. Zeeb 			if (tmpinp == NULL)
1944413628a7SBjoern A. Zeeb 				tmpinp = inp;
1945c3229e05SDavid Greenman 		}
1946413628a7SBjoern A. Zeeb 	}
1947413628a7SBjoern A. Zeeb 	if (tmpinp != NULL)
1948413628a7SBjoern A. Zeeb 		return (tmpinp);
1949e3fd5ffdSRobert Watson 
1950e3fd5ffdSRobert Watson 	/*
1951e3fd5ffdSRobert Watson 	 * Then look for a wildcard match, if requested.
1952e3fd5ffdSRobert Watson 	 */
195368e0d7e0SRobert Watson 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1954413628a7SBjoern A. Zeeb 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1955e3fd5ffdSRobert Watson #ifdef INET6
1956cfa1ca9dSYoshinobu Inoue 		struct inpcb *local_wild_mapped = NULL;
1957e3fd5ffdSRobert Watson #endif
1958413628a7SBjoern A. Zeeb 		struct inpcb *jail_wild = NULL;
1959413628a7SBjoern A. Zeeb 		int injail;
1960413628a7SBjoern A. Zeeb 
1961413628a7SBjoern A. Zeeb 		/*
1962413628a7SBjoern A. Zeeb 		 * Order of socket selection - we always prefer jails.
1963413628a7SBjoern A. Zeeb 		 *      1. jailed, non-wild.
1964413628a7SBjoern A. Zeeb 		 *      2. jailed, wild.
1965413628a7SBjoern A. Zeeb 		 *      3. non-jailed, non-wild.
1966413628a7SBjoern A. Zeeb 		 *      4. non-jailed, wild.
1967413628a7SBjoern A. Zeeb 		 */
19686d6a026bSDavid Greenman 
1969712fc218SRobert Watson 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1970712fc218SRobert Watson 		    0, pcbinfo->ipi_hashmask)];
1971fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(inp, head, inp_hash) {
1972cfa1ca9dSYoshinobu Inoue #ifdef INET6
1973413628a7SBjoern A. Zeeb 			/* XXX inp locking */
1974369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
1975cfa1ca9dSYoshinobu Inoue 				continue;
1976cfa1ca9dSYoshinobu Inoue #endif
1977413628a7SBjoern A. Zeeb 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1978413628a7SBjoern A. Zeeb 			    inp->inp_lport != lport)
1979413628a7SBjoern A. Zeeb 				continue;
1980413628a7SBjoern A. Zeeb 
19810304c731SJamie Gritton 			injail = prison_flag(inp->inp_cred, PR_IP4);
1982413628a7SBjoern A. Zeeb 			if (injail) {
1983b89e82ddSJamie Gritton 				if (prison_check_ip4(inp->inp_cred,
1984b89e82ddSJamie Gritton 				    &laddr) != 0)
1985413628a7SBjoern A. Zeeb 					continue;
1986413628a7SBjoern A. Zeeb 			} else {
1987413628a7SBjoern A. Zeeb 				if (local_exact != NULL)
1988413628a7SBjoern A. Zeeb 					continue;
1989413628a7SBjoern A. Zeeb 			}
1990413628a7SBjoern A. Zeeb 
1991413628a7SBjoern A. Zeeb 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1992413628a7SBjoern A. Zeeb 				if (injail)
1993c3229e05SDavid Greenman 					return (inp);
1994413628a7SBjoern A. Zeeb 				else
1995413628a7SBjoern A. Zeeb 					local_exact = inp;
1996413628a7SBjoern A. Zeeb 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1997e3fd5ffdSRobert Watson #ifdef INET6
1998413628a7SBjoern A. Zeeb 				/* XXX inp locking, NULL check */
19995cd54324SBjoern A. Zeeb 				if (inp->inp_vflag & INP_IPV6PROTO)
2000cfa1ca9dSYoshinobu Inoue 					local_wild_mapped = inp;
2001cfa1ca9dSYoshinobu Inoue 				else
200283e521ecSBjoern A. Zeeb #endif
2003413628a7SBjoern A. Zeeb 					if (injail)
2004413628a7SBjoern A. Zeeb 						jail_wild = inp;
2005413628a7SBjoern A. Zeeb 					else
20066d6a026bSDavid Greenman 						local_wild = inp;
20076d6a026bSDavid Greenman 			}
2008413628a7SBjoern A. Zeeb 		} /* LIST_FOREACH */
2009413628a7SBjoern A. Zeeb 		if (jail_wild != NULL)
2010413628a7SBjoern A. Zeeb 			return (jail_wild);
2011413628a7SBjoern A. Zeeb 		if (local_exact != NULL)
2012413628a7SBjoern A. Zeeb 			return (local_exact);
2013413628a7SBjoern A. Zeeb 		if (local_wild != NULL)
2014c3229e05SDavid Greenman 			return (local_wild);
2015413628a7SBjoern A. Zeeb #ifdef INET6
2016413628a7SBjoern A. Zeeb 		if (local_wild_mapped != NULL)
2017413628a7SBjoern A. Zeeb 			return (local_wild_mapped);
201883e521ecSBjoern A. Zeeb #endif
201968e0d7e0SRobert Watson 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
2020413628a7SBjoern A. Zeeb 
20216d6a026bSDavid Greenman 	return (NULL);
202215bd2b43SDavid Greenman }
2023fa046d87SRobert Watson 
2024fa046d87SRobert Watson /*
2025fa046d87SRobert Watson  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
2026fa046d87SRobert Watson  * hash list lock, and will return the inpcb locked (i.e., requires
2027fa046d87SRobert Watson  * INPLOOKUP_LOCKPCB).
2028fa046d87SRobert Watson  */
2029fa046d87SRobert Watson static struct inpcb *
2030fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2031fa046d87SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2032fa046d87SRobert Watson     struct ifnet *ifp)
2033fa046d87SRobert Watson {
2034fa046d87SRobert Watson 	struct inpcb *inp;
20357fb2986fSJonathan T. Looney 	bool locked;
2036fa046d87SRobert Watson 
2037fa046d87SRobert Watson 	INP_HASH_RLOCK(pcbinfo);
2038fa046d87SRobert Watson 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
2039fa046d87SRobert Watson 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
2040fa046d87SRobert Watson 	if (inp != NULL) {
20417fb2986fSJonathan T. Looney 		if (lookupflags & INPLOOKUP_WLOCKPCB)
20427fb2986fSJonathan T. Looney 			locked = INP_TRY_WLOCK(inp);
20437fb2986fSJonathan T. Looney 		else if (lookupflags & INPLOOKUP_RLOCKPCB)
20447fb2986fSJonathan T. Looney 			locked = INP_TRY_RLOCK(inp);
20457fb2986fSJonathan T. Looney 		else
20467fb2986fSJonathan T. Looney 			panic("%s: locking bug", __func__);
20477fb2986fSJonathan T. Looney 		if (!locked)
2048fa046d87SRobert Watson 			in_pcbref(inp);
2049fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
20507fb2986fSJonathan T. Looney 		if (!locked) {
2051fa046d87SRobert Watson 			if (lookupflags & INPLOOKUP_WLOCKPCB) {
2052fa046d87SRobert Watson 				INP_WLOCK(inp);
2053fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp))
2054fa046d87SRobert Watson 					return (NULL);
20557fb2986fSJonathan T. Looney 			} else {
2056fa046d87SRobert Watson 				INP_RLOCK(inp);
2057fa046d87SRobert Watson 				if (in_pcbrele_rlocked(inp))
2058fa046d87SRobert Watson 					return (NULL);
20597fb2986fSJonathan T. Looney 			}
20607fb2986fSJonathan T. Looney 		}
20617fb2986fSJonathan T. Looney #ifdef INVARIANTS
20627fb2986fSJonathan T. Looney 		if (lookupflags & INPLOOKUP_WLOCKPCB)
20637fb2986fSJonathan T. Looney 			INP_WLOCK_ASSERT(inp);
20647fb2986fSJonathan T. Looney 		else
20657fb2986fSJonathan T. Looney 			INP_RLOCK_ASSERT(inp);
20667fb2986fSJonathan T. Looney #endif
2067fa046d87SRobert Watson 	} else
2068fa046d87SRobert Watson 		INP_HASH_RUNLOCK(pcbinfo);
2069fa046d87SRobert Watson 	return (inp);
2070fa046d87SRobert Watson }
2071fa046d87SRobert Watson 
2072fa046d87SRobert Watson /*
2073d3c1f003SRobert Watson  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
2074d3c1f003SRobert Watson  * from which a pre-calculated hash value may be extracted.
207552cd27cbSRobert Watson  *
207652cd27cbSRobert Watson  * Possibly more of this logic should be in in_pcbgroup.c.
2077fa046d87SRobert Watson  */
2078fa046d87SRobert Watson struct inpcb *
2079fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
2080fa046d87SRobert Watson     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
2081fa046d87SRobert Watson {
20827527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
208352cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
208452cd27cbSRobert Watson #endif
2085fa046d87SRobert Watson 
2086fa046d87SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2087fa046d87SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2088fa046d87SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2089fa046d87SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2090fa046d87SRobert Watson 
20917527624eSRobert Watson 	/*
20927527624eSRobert Watson 	 * When not using RSS, use connection groups in preference to the
20937527624eSRobert Watson 	 * reservation table when looking up 4-tuples.  When using RSS, just
20947527624eSRobert Watson 	 * use the reservation table, due to the cost of the Toeplitz hash
20957527624eSRobert Watson 	 * in software.
20967527624eSRobert Watson 	 *
20977527624eSRobert Watson 	 * XXXRW: This policy belongs in the pcbgroup code, as in principle
20987527624eSRobert Watson 	 * we could be doing RSS with a non-Toeplitz hash that is affordable
20997527624eSRobert Watson 	 * in software.
21007527624eSRobert Watson 	 */
21017527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS)
210252cd27cbSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo)) {
210352cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
210452cd27cbSRobert Watson 		    fport);
210552cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
210652cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
210752cd27cbSRobert Watson 	}
210852cd27cbSRobert Watson #endif
2109fa046d87SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2110fa046d87SRobert Watson 	    lookupflags, ifp));
2111fa046d87SRobert Watson }
2112d3c1f003SRobert Watson 
2113d3c1f003SRobert Watson struct inpcb *
2114d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2115d3c1f003SRobert Watson     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2116d3c1f003SRobert Watson     struct ifnet *ifp, struct mbuf *m)
2117d3c1f003SRobert Watson {
211852cd27cbSRobert Watson #ifdef PCBGROUP
211952cd27cbSRobert Watson 	struct inpcbgroup *pcbgroup;
212052cd27cbSRobert Watson #endif
2121d3c1f003SRobert Watson 
2122d3c1f003SRobert Watson 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2123d3c1f003SRobert Watson 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
2124d3c1f003SRobert Watson 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2125d3c1f003SRobert Watson 	    ("%s: LOCKPCB not set", __func__));
2126d3c1f003SRobert Watson 
212752cd27cbSRobert Watson #ifdef PCBGROUP
21287527624eSRobert Watson 	/*
21297527624eSRobert Watson 	 * If we can use a hardware-generated hash to look up the connection
21307527624eSRobert Watson 	 * group, use that connection group to find the inpcb.  Otherwise
21317527624eSRobert Watson 	 * fall back on a software hash -- or the reservation table if we're
21327527624eSRobert Watson 	 * using RSS.
21337527624eSRobert Watson 	 *
21347527624eSRobert Watson 	 * XXXRW: As above, that policy belongs in the pcbgroup code.
21357527624eSRobert Watson 	 */
21367527624eSRobert Watson 	if (in_pcbgroup_enabled(pcbinfo) &&
21377527624eSRobert Watson 	    !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
213852cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
213952cd27cbSRobert Watson 		    m->m_pkthdr.flowid);
214052cd27cbSRobert Watson 		if (pcbgroup != NULL)
214152cd27cbSRobert Watson 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
214252cd27cbSRobert Watson 			    fport, laddr, lport, lookupflags, ifp));
21437527624eSRobert Watson #ifndef RSS
214452cd27cbSRobert Watson 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
214552cd27cbSRobert Watson 		    fport);
214652cd27cbSRobert Watson 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
214752cd27cbSRobert Watson 		    laddr, lport, lookupflags, ifp));
21487527624eSRobert Watson #endif
214952cd27cbSRobert Watson 	}
215052cd27cbSRobert Watson #endif
2151d3c1f003SRobert Watson 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2152d3c1f003SRobert Watson 	    lookupflags, ifp));
2153d3c1f003SRobert Watson }
215467107f45SBjoern A. Zeeb #endif /* INET */
215515bd2b43SDavid Greenman 
21567bc4aca7SDavid Greenman /*
2157c3229e05SDavid Greenman  * Insert PCB onto various hash lists.
21587bc4aca7SDavid Greenman  */
215952cd27cbSRobert Watson static int
216052cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
216115bd2b43SDavid Greenman {
2162c3229e05SDavid Greenman 	struct inpcbhead *pcbhash;
2163c3229e05SDavid Greenman 	struct inpcbporthead *pcbporthash;
2164c3229e05SDavid Greenman 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2165c3229e05SDavid Greenman 	struct inpcbport *phd;
2166cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
216715bd2b43SDavid Greenman 
21688501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2169fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2170fa046d87SRobert Watson 
2171111d57a6SRobert Watson 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2172111d57a6SRobert Watson 	    ("in_pcbinshash: INP_INHASHLIST"));
2173602cc7f1SRobert Watson 
2174cfa1ca9dSYoshinobu Inoue #ifdef INET6
2175cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
21761b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2177cfa1ca9dSYoshinobu Inoue 	else
217883e521ecSBjoern A. Zeeb #endif
2179cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2180cfa1ca9dSYoshinobu Inoue 
2181712fc218SRobert Watson 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2182712fc218SRobert Watson 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
218315bd2b43SDavid Greenman 
2184712fc218SRobert Watson 	pcbporthash = &pcbinfo->ipi_porthashbase[
2185712fc218SRobert Watson 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2186c3229e05SDavid Greenman 
2187c3229e05SDavid Greenman 	/*
2188c3229e05SDavid Greenman 	 * Go through port list and look for a head for this lport.
2189c3229e05SDavid Greenman 	 */
2190fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
2191c3229e05SDavid Greenman 		if (phd->phd_port == inp->inp_lport)
2192c3229e05SDavid Greenman 			break;
2193c3229e05SDavid Greenman 	}
2194c3229e05SDavid Greenman 	/*
2195c3229e05SDavid Greenman 	 * If none exists, malloc one and tack it on.
2196c3229e05SDavid Greenman 	 */
2197c3229e05SDavid Greenman 	if (phd == NULL) {
21981ede983cSDag-Erling Smørgrav 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2199c3229e05SDavid Greenman 		if (phd == NULL) {
2200c3229e05SDavid Greenman 			return (ENOBUFS); /* XXX */
2201c3229e05SDavid Greenman 		}
2202c3229e05SDavid Greenman 		phd->phd_port = inp->inp_lport;
2203c3229e05SDavid Greenman 		LIST_INIT(&phd->phd_pcblist);
2204c3229e05SDavid Greenman 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2205c3229e05SDavid Greenman 	}
2206c3229e05SDavid Greenman 	inp->inp_phd = phd;
2207c3229e05SDavid Greenman 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2208c3229e05SDavid Greenman 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2209111d57a6SRobert Watson 	inp->inp_flags |= INP_INHASHLIST;
221052cd27cbSRobert Watson #ifdef PCBGROUP
221152cd27cbSRobert Watson 	if (do_pcbgroup_update)
221252cd27cbSRobert Watson 		in_pcbgroup_update(inp);
221352cd27cbSRobert Watson #endif
2214c3229e05SDavid Greenman 	return (0);
221515bd2b43SDavid Greenman }
221615bd2b43SDavid Greenman 
2217c3229e05SDavid Greenman /*
221852cd27cbSRobert Watson  * For now, there are two public interfaces to insert an inpcb into the hash
221952cd27cbSRobert Watson  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
222052cd27cbSRobert Watson  * is used only in the TCP syncache, where in_pcbinshash is called before the
222152cd27cbSRobert Watson  * full 4-tuple is set for the inpcb, and we don't want to install in the
222252cd27cbSRobert Watson  * pcbgroup until later.
222352cd27cbSRobert Watson  *
222452cd27cbSRobert Watson  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
222552cd27cbSRobert Watson  * connection groups, and partially initialised inpcbs should not be exposed
222652cd27cbSRobert Watson  * to either reservation hash tables or pcbgroups.
222752cd27cbSRobert Watson  */
222852cd27cbSRobert Watson int
222952cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp)
223052cd27cbSRobert Watson {
223152cd27cbSRobert Watson 
223252cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 1));
223352cd27cbSRobert Watson }
223452cd27cbSRobert Watson 
223552cd27cbSRobert Watson int
223652cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp)
223752cd27cbSRobert Watson {
223852cd27cbSRobert Watson 
223952cd27cbSRobert Watson 	return (in_pcbinshash_internal(inp, 0));
224052cd27cbSRobert Watson }
224152cd27cbSRobert Watson 
224252cd27cbSRobert Watson /*
2243c3229e05SDavid Greenman  * Move PCB to the proper hash bucket when { faddr, fport } have  been
2244c3229e05SDavid Greenman  * changed. NOTE: This does not handle the case of the lport changing (the
2245c3229e05SDavid Greenman  * hashed port list would have to be updated as well), so the lport must
2246c3229e05SDavid Greenman  * not change after in_pcbinshash() has been called.
2247c3229e05SDavid Greenman  */
224815bd2b43SDavid Greenman void
2249d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
225015bd2b43SDavid Greenman {
225159daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
225215bd2b43SDavid Greenman 	struct inpcbhead *head;
2253cfa1ca9dSYoshinobu Inoue 	u_int32_t hashkey_faddr;
225415bd2b43SDavid Greenman 
22558501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2256fa046d87SRobert Watson 	INP_HASH_WLOCK_ASSERT(pcbinfo);
2257fa046d87SRobert Watson 
2258111d57a6SRobert Watson 	KASSERT(inp->inp_flags & INP_INHASHLIST,
2259111d57a6SRobert Watson 	    ("in_pcbrehash: !INP_INHASHLIST"));
2260602cc7f1SRobert Watson 
2261cfa1ca9dSYoshinobu Inoue #ifdef INET6
2262cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6)
22631b44e5ffSAndrey V. Elsukov 		hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2264cfa1ca9dSYoshinobu Inoue 	else
226583e521ecSBjoern A. Zeeb #endif
2266cfa1ca9dSYoshinobu Inoue 	hashkey_faddr = inp->inp_faddr.s_addr;
2267cfa1ca9dSYoshinobu Inoue 
2268712fc218SRobert Watson 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2269712fc218SRobert Watson 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
227015bd2b43SDavid Greenman 
2271c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_hash);
227215bd2b43SDavid Greenman 	LIST_INSERT_HEAD(head, inp, inp_hash);
227352cd27cbSRobert Watson 
227452cd27cbSRobert Watson #ifdef PCBGROUP
227552cd27cbSRobert Watson 	if (m != NULL)
227652cd27cbSRobert Watson 		in_pcbgroup_update_mbuf(inp, m);
227752cd27cbSRobert Watson 	else
227852cd27cbSRobert Watson 		in_pcbgroup_update(inp);
227952cd27cbSRobert Watson #endif
2280c3229e05SDavid Greenman }
2281c3229e05SDavid Greenman 
2282d3c1f003SRobert Watson void
2283d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp)
2284d3c1f003SRobert Watson {
2285d3c1f003SRobert Watson 
2286d3c1f003SRobert Watson 	in_pcbrehash_mbuf(inp, NULL);
2287d3c1f003SRobert Watson }
2288d3c1f003SRobert Watson 
2289c3229e05SDavid Greenman /*
2290c3229e05SDavid Greenman  * Remove PCB from various lists.
2291c3229e05SDavid Greenman  */
22926d888973SRobert Watson static void
2293136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp)
2294c3229e05SDavid Greenman {
229559daba27SSam Leffler 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
229659daba27SSam Leffler 
2297ff9b006dSJulien Charbon #ifdef INVARIANTS
2298ff9b006dSJulien Charbon 	if (pcbinfo == &V_tcbinfo) {
2299ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(pcbinfo);
2300ff9b006dSJulien Charbon 	} else {
230159daba27SSam Leffler 		INP_INFO_WLOCK_ASSERT(pcbinfo);
2302ff9b006dSJulien Charbon 	}
2303ff9b006dSJulien Charbon #endif
2304ff9b006dSJulien Charbon 
23058501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2306ff9b006dSJulien Charbon 	INP_LIST_WLOCK_ASSERT(pcbinfo);
230759daba27SSam Leffler 
230859daba27SSam Leffler 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2309111d57a6SRobert Watson 	if (inp->inp_flags & INP_INHASHLIST) {
2310c3229e05SDavid Greenman 		struct inpcbport *phd = inp->inp_phd;
2311c3229e05SDavid Greenman 
2312fa046d87SRobert Watson 		INP_HASH_WLOCK(pcbinfo);
2313c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_hash);
2314c3229e05SDavid Greenman 		LIST_REMOVE(inp, inp_portlist);
2315fc2ffbe6SPoul-Henning Kamp 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2316c3229e05SDavid Greenman 			LIST_REMOVE(phd, phd_hash);
2317c3229e05SDavid Greenman 			free(phd, M_PCB);
2318c3229e05SDavid Greenman 		}
2319fa046d87SRobert Watson 		INP_HASH_WUNLOCK(pcbinfo);
2320111d57a6SRobert Watson 		inp->inp_flags &= ~INP_INHASHLIST;
2321c3229e05SDavid Greenman 	}
2322c3229e05SDavid Greenman 	LIST_REMOVE(inp, inp_list);
232359daba27SSam Leffler 	pcbinfo->ipi_count--;
232452cd27cbSRobert Watson #ifdef PCBGROUP
232552cd27cbSRobert Watson 	in_pcbgroup_remove(inp);
232652cd27cbSRobert Watson #endif
232715bd2b43SDavid Greenman }
232875c13541SPoul-Henning Kamp 
2329a557af22SRobert Watson /*
233084cc0778SGeorge V. Neville-Neil  * Check for alternatives when higher level complains
233184cc0778SGeorge V. Neville-Neil  * about service problems.  For now, invalidate cached
233284cc0778SGeorge V. Neville-Neil  * routing information.  If the route was created dynamically
233384cc0778SGeorge V. Neville-Neil  * (by a redirect), time to try a default gateway again.
233484cc0778SGeorge V. Neville-Neil  */
233584cc0778SGeorge V. Neville-Neil void
233684cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp)
233784cc0778SGeorge V. Neville-Neil {
233884cc0778SGeorge V. Neville-Neil 
2339fc21c53fSRyan Stone 	RO_INVALIDATE_CACHE(&inp->inp_route);
234084cc0778SGeorge V. Neville-Neil 	return;
234184cc0778SGeorge V. Neville-Neil }
234284cc0778SGeorge V. Neville-Neil 
234384cc0778SGeorge V. Neville-Neil /*
2344a557af22SRobert Watson  * A set label operation has occurred at the socket layer, propagate the
2345a557af22SRobert Watson  * label change into the in_pcb for the socket.
2346a557af22SRobert Watson  */
2347a557af22SRobert Watson void
2348136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so)
2349a557af22SRobert Watson {
2350a557af22SRobert Watson #ifdef MAC
2351a557af22SRobert Watson 	struct inpcb *inp;
2352a557af22SRobert Watson 
23534c7c478dSRobert Watson 	inp = sotoinpcb(so);
23544c7c478dSRobert Watson 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2355602cc7f1SRobert Watson 
23568501a69cSRobert Watson 	INP_WLOCK(inp);
2357310e7cebSRobert Watson 	SOCK_LOCK(so);
2358a557af22SRobert Watson 	mac_inpcb_sosetlabel(so, inp);
2359310e7cebSRobert Watson 	SOCK_UNLOCK(so);
23608501a69cSRobert Watson 	INP_WUNLOCK(inp);
2361a557af22SRobert Watson #endif
2362a557af22SRobert Watson }
23635f311da2SMike Silbersack 
23645f311da2SMike Silbersack /*
2365ad3a630fSRobert Watson  * ipport_tick runs once per second, determining if random port allocation
2366ad3a630fSRobert Watson  * should be continued.  If more than ipport_randomcps ports have been
2367ad3a630fSRobert Watson  * allocated in the last second, then we return to sequential port
2368ad3a630fSRobert Watson  * allocation. We return to random allocation only once we drop below
2369ad3a630fSRobert Watson  * ipport_randomcps for at least ipport_randomtime seconds.
23705f311da2SMike Silbersack  */
2371aae49dd3SBjoern A. Zeeb static void
2372136d4f1cSRobert Watson ipport_tick(void *xtp)
23735f311da2SMike Silbersack {
23748b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
2375ad3a630fSRobert Watson 
23765ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
23778b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
23788b615593SMarko Zec 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
23798b615593SMarko Zec 		if (V_ipport_tcpallocs <=
23808b615593SMarko Zec 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2381603724d3SBjoern A. Zeeb 			if (V_ipport_stoprandom > 0)
2382603724d3SBjoern A. Zeeb 				V_ipport_stoprandom--;
2383ad3a630fSRobert Watson 		} else
2384603724d3SBjoern A. Zeeb 			V_ipport_stoprandom = V_ipport_randomtime;
2385603724d3SBjoern A. Zeeb 		V_ipport_tcplastcount = V_ipport_tcpallocs;
23868b615593SMarko Zec 		CURVNET_RESTORE();
23878b615593SMarko Zec 	}
23885ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
23895f311da2SMike Silbersack 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
23905f311da2SMike Silbersack }
2391497057eeSRobert Watson 
2392aae49dd3SBjoern A. Zeeb static void
2393aae49dd3SBjoern A. Zeeb ip_fini(void *xtp)
2394aae49dd3SBjoern A. Zeeb {
2395aae49dd3SBjoern A. Zeeb 
2396aae49dd3SBjoern A. Zeeb 	callout_stop(&ipport_tick_callout);
2397aae49dd3SBjoern A. Zeeb }
2398aae49dd3SBjoern A. Zeeb 
2399aae49dd3SBjoern A. Zeeb /*
2400aae49dd3SBjoern A. Zeeb  * The ipport_callout should start running at about the time we attach the
2401aae49dd3SBjoern A. Zeeb  * inet or inet6 domains.
2402aae49dd3SBjoern A. Zeeb  */
2403aae49dd3SBjoern A. Zeeb static void
2404aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused)
2405aae49dd3SBjoern A. Zeeb {
2406aae49dd3SBjoern A. Zeeb 
2407aae49dd3SBjoern A. Zeeb 	/* Start ipport_tick. */
2408fd90e2edSJung-uk Kim 	callout_init(&ipport_tick_callout, 1);
2409aae49dd3SBjoern A. Zeeb 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2410aae49dd3SBjoern A. Zeeb 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2411aae49dd3SBjoern A. Zeeb 		SHUTDOWN_PRI_DEFAULT);
2412aae49dd3SBjoern A. Zeeb }
2413aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2414aae49dd3SBjoern A. Zeeb     ipport_tick_init, NULL);
2415aae49dd3SBjoern A. Zeeb 
24163d585327SKip Macy void
24173d585327SKip Macy inp_wlock(struct inpcb *inp)
24183d585327SKip Macy {
24193d585327SKip Macy 
24208501a69cSRobert Watson 	INP_WLOCK(inp);
24213d585327SKip Macy }
24223d585327SKip Macy 
24233d585327SKip Macy void
24243d585327SKip Macy inp_wunlock(struct inpcb *inp)
24253d585327SKip Macy {
24263d585327SKip Macy 
24278501a69cSRobert Watson 	INP_WUNLOCK(inp);
24283d585327SKip Macy }
24293d585327SKip Macy 
24303d585327SKip Macy void
24313d585327SKip Macy inp_rlock(struct inpcb *inp)
24323d585327SKip Macy {
24333d585327SKip Macy 
2434a69042a5SRobert Watson 	INP_RLOCK(inp);
24353d585327SKip Macy }
24363d585327SKip Macy 
24373d585327SKip Macy void
24383d585327SKip Macy inp_runlock(struct inpcb *inp)
24393d585327SKip Macy {
24403d585327SKip Macy 
2441a69042a5SRobert Watson 	INP_RUNLOCK(inp);
24423d585327SKip Macy }
24433d585327SKip Macy 
2444c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
24453d585327SKip Macy void
2446e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp)
24473d585327SKip Macy {
24483d585327SKip Macy 
24498501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
24503d585327SKip Macy }
24513d585327SKip Macy 
24523d585327SKip Macy void
2453e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp)
24543d585327SKip Macy {
24553d585327SKip Macy 
24563d585327SKip Macy 	INP_UNLOCK_ASSERT(inp);
24573d585327SKip Macy }
24583d585327SKip Macy #endif
24593d585327SKip Macy 
24609378e437SKip Macy void
24619378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
24629378e437SKip Macy {
24639378e437SKip Macy 	struct inpcb *inp;
24649378e437SKip Macy 
2465ff9b006dSJulien Charbon 	INP_INFO_WLOCK(&V_tcbinfo);
246697021c24SMarko Zec 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
24679378e437SKip Macy 		INP_WLOCK(inp);
24689378e437SKip Macy 		func(inp, arg);
24699378e437SKip Macy 		INP_WUNLOCK(inp);
24709378e437SKip Macy 	}
2471ff9b006dSJulien Charbon 	INP_INFO_WUNLOCK(&V_tcbinfo);
24729378e437SKip Macy }
24739378e437SKip Macy 
24749378e437SKip Macy struct socket *
24759378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp)
24769378e437SKip Macy {
24779378e437SKip Macy 
24789378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
24799378e437SKip Macy 	return (inp->inp_socket);
24809378e437SKip Macy }
24819378e437SKip Macy 
24829378e437SKip Macy struct tcpcb *
24839378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp)
24849378e437SKip Macy {
24859378e437SKip Macy 
24869378e437SKip Macy 	INP_WLOCK_ASSERT(inp);
24879378e437SKip Macy 	return ((struct tcpcb *)inp->inp_ppcb);
24889378e437SKip Macy }
24899378e437SKip Macy 
24909378e437SKip Macy int
24919378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp)
24929378e437SKip Macy {
24939378e437SKip Macy 
24949378e437SKip Macy 	return (inp->inp_ip_tos);
24959378e437SKip Macy }
24969378e437SKip Macy 
24979378e437SKip Macy void
24989378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val)
24999378e437SKip Macy {
25009378e437SKip Macy 
25019378e437SKip Macy 	inp->inp_ip_tos = val;
25029378e437SKip Macy }
25039378e437SKip Macy 
25049378e437SKip Macy void
2505df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
25069d29c635SKip Macy     uint32_t *faddr, uint16_t *fp)
25079378e437SKip Macy {
25089378e437SKip Macy 
25099d29c635SKip Macy 	INP_LOCK_ASSERT(inp);
2510df9cf830STai-hwa Liang 	*laddr = inp->inp_laddr.s_addr;
2511df9cf830STai-hwa Liang 	*faddr = inp->inp_faddr.s_addr;
25129378e437SKip Macy 	*lp = inp->inp_lport;
25139378e437SKip Macy 	*fp = inp->inp_fport;
25149378e437SKip Macy }
25159378e437SKip Macy 
2516dd0e6c38SKip Macy struct inpcb *
2517dd0e6c38SKip Macy so_sotoinpcb(struct socket *so)
2518dd0e6c38SKip Macy {
2519dd0e6c38SKip Macy 
2520dd0e6c38SKip Macy 	return (sotoinpcb(so));
2521dd0e6c38SKip Macy }
2522dd0e6c38SKip Macy 
2523dd0e6c38SKip Macy struct tcpcb *
2524dd0e6c38SKip Macy so_sototcpcb(struct socket *so)
2525dd0e6c38SKip Macy {
2526dd0e6c38SKip Macy 
2527dd0e6c38SKip Macy 	return (sototcpcb(so));
2528dd0e6c38SKip Macy }
2529dd0e6c38SKip Macy 
2530cc65eb4eSGleb Smirnoff /*
2531cc65eb4eSGleb Smirnoff  * Create an external-format (``xinpcb'') structure using the information in
2532cc65eb4eSGleb Smirnoff  * the kernel-format in_pcb structure pointed to by inp.  This is done to
2533cc65eb4eSGleb Smirnoff  * reduce the spew of irrelevant information over this interface, to isolate
2534cc65eb4eSGleb Smirnoff  * user code from changes in the kernel structure, and potentially to provide
2535cc65eb4eSGleb Smirnoff  * information-hiding if we decide that some of this information should be
2536cc65eb4eSGleb Smirnoff  * hidden from users.
2537cc65eb4eSGleb Smirnoff  */
2538cc65eb4eSGleb Smirnoff void
2539cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi)
2540cc65eb4eSGleb Smirnoff {
2541cc65eb4eSGleb Smirnoff 
2542cc65eb4eSGleb Smirnoff 	xi->xi_len = sizeof(struct xinpcb);
2543cc65eb4eSGleb Smirnoff 	if (inp->inp_socket)
2544cc65eb4eSGleb Smirnoff 		sotoxsocket(inp->inp_socket, &xi->xi_socket);
2545cc65eb4eSGleb Smirnoff 	else
2546cc65eb4eSGleb Smirnoff 		bzero(&xi->xi_socket, sizeof(struct xsocket));
2547cc65eb4eSGleb Smirnoff 	bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo));
2548cc65eb4eSGleb Smirnoff 	xi->inp_gencnt = inp->inp_gencnt;
2549cc65eb4eSGleb Smirnoff 	xi->inp_ppcb = inp->inp_ppcb;
2550cc65eb4eSGleb Smirnoff 	xi->inp_flow = inp->inp_flow;
2551cc65eb4eSGleb Smirnoff 	xi->inp_flowid = inp->inp_flowid;
2552cc65eb4eSGleb Smirnoff 	xi->inp_flowtype = inp->inp_flowtype;
2553cc65eb4eSGleb Smirnoff 	xi->inp_flags = inp->inp_flags;
2554cc65eb4eSGleb Smirnoff 	xi->inp_flags2 = inp->inp_flags2;
2555cc65eb4eSGleb Smirnoff 	xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket;
2556cc65eb4eSGleb Smirnoff 	xi->in6p_cksum = inp->in6p_cksum;
2557cc65eb4eSGleb Smirnoff 	xi->in6p_hops = inp->in6p_hops;
2558cc65eb4eSGleb Smirnoff 	xi->inp_ip_tos = inp->inp_ip_tos;
2559cc65eb4eSGleb Smirnoff 	xi->inp_vflag = inp->inp_vflag;
2560cc65eb4eSGleb Smirnoff 	xi->inp_ip_ttl = inp->inp_ip_ttl;
2561cc65eb4eSGleb Smirnoff 	xi->inp_ip_p = inp->inp_ip_p;
2562cc65eb4eSGleb Smirnoff 	xi->inp_ip_minttl = inp->inp_ip_minttl;
2563cc65eb4eSGleb Smirnoff }
2564cc65eb4eSGleb Smirnoff 
2565497057eeSRobert Watson #ifdef DDB
2566497057eeSRobert Watson static void
2567497057eeSRobert Watson db_print_indent(int indent)
2568497057eeSRobert Watson {
2569497057eeSRobert Watson 	int i;
2570497057eeSRobert Watson 
2571497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2572497057eeSRobert Watson 		db_printf(" ");
2573497057eeSRobert Watson }
2574497057eeSRobert Watson 
2575497057eeSRobert Watson static void
2576497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2577497057eeSRobert Watson {
2578497057eeSRobert Watson 	char faddr_str[48], laddr_str[48];
2579497057eeSRobert Watson 
2580497057eeSRobert Watson 	db_print_indent(indent);
2581497057eeSRobert Watson 	db_printf("%s at %p\n", name, inc);
2582497057eeSRobert Watson 
2583497057eeSRobert Watson 	indent += 2;
2584497057eeSRobert Watson 
258503dc38a4SRobert Watson #ifdef INET6
2586dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
2587497057eeSRobert Watson 		/* IPv6. */
2588497057eeSRobert Watson 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2589497057eeSRobert Watson 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
259083e521ecSBjoern A. Zeeb 	} else
259103dc38a4SRobert Watson #endif
259283e521ecSBjoern A. Zeeb 	{
2593497057eeSRobert Watson 		/* IPv4. */
2594497057eeSRobert Watson 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2595497057eeSRobert Watson 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2596497057eeSRobert Watson 	}
2597497057eeSRobert Watson 	db_print_indent(indent);
2598497057eeSRobert Watson 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2599497057eeSRobert Watson 	    ntohs(inc->inc_lport));
2600497057eeSRobert Watson 	db_print_indent(indent);
2601497057eeSRobert Watson 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2602497057eeSRobert Watson 	    ntohs(inc->inc_fport));
2603497057eeSRobert Watson }
2604497057eeSRobert Watson 
2605497057eeSRobert Watson static void
2606497057eeSRobert Watson db_print_inpflags(int inp_flags)
2607497057eeSRobert Watson {
2608497057eeSRobert Watson 	int comma;
2609497057eeSRobert Watson 
2610497057eeSRobert Watson 	comma = 0;
2611497057eeSRobert Watson 	if (inp_flags & INP_RECVOPTS) {
2612497057eeSRobert Watson 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2613497057eeSRobert Watson 		comma = 1;
2614497057eeSRobert Watson 	}
2615497057eeSRobert Watson 	if (inp_flags & INP_RECVRETOPTS) {
2616497057eeSRobert Watson 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2617497057eeSRobert Watson 		comma = 1;
2618497057eeSRobert Watson 	}
2619497057eeSRobert Watson 	if (inp_flags & INP_RECVDSTADDR) {
2620497057eeSRobert Watson 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2621497057eeSRobert Watson 		comma = 1;
2622497057eeSRobert Watson 	}
2623dce33a45SErmal Luçi 	if (inp_flags & INP_ORIGDSTADDR) {
2624dce33a45SErmal Luçi 		db_printf("%sINP_ORIGDSTADDR", comma ? ", " : "");
2625dce33a45SErmal Luçi 		comma = 1;
2626dce33a45SErmal Luçi 	}
2627497057eeSRobert Watson 	if (inp_flags & INP_HDRINCL) {
2628497057eeSRobert Watson 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2629497057eeSRobert Watson 		comma = 1;
2630497057eeSRobert Watson 	}
2631497057eeSRobert Watson 	if (inp_flags & INP_HIGHPORT) {
2632497057eeSRobert Watson 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2633497057eeSRobert Watson 		comma = 1;
2634497057eeSRobert Watson 	}
2635497057eeSRobert Watson 	if (inp_flags & INP_LOWPORT) {
2636497057eeSRobert Watson 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2637497057eeSRobert Watson 		comma = 1;
2638497057eeSRobert Watson 	}
2639497057eeSRobert Watson 	if (inp_flags & INP_ANONPORT) {
2640497057eeSRobert Watson 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2641497057eeSRobert Watson 		comma = 1;
2642497057eeSRobert Watson 	}
2643497057eeSRobert Watson 	if (inp_flags & INP_RECVIF) {
2644497057eeSRobert Watson 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2645497057eeSRobert Watson 		comma = 1;
2646497057eeSRobert Watson 	}
2647497057eeSRobert Watson 	if (inp_flags & INP_MTUDISC) {
2648497057eeSRobert Watson 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2649497057eeSRobert Watson 		comma = 1;
2650497057eeSRobert Watson 	}
2651497057eeSRobert Watson 	if (inp_flags & INP_RECVTTL) {
2652497057eeSRobert Watson 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2653497057eeSRobert Watson 		comma = 1;
2654497057eeSRobert Watson 	}
2655497057eeSRobert Watson 	if (inp_flags & INP_DONTFRAG) {
2656497057eeSRobert Watson 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2657497057eeSRobert Watson 		comma = 1;
2658497057eeSRobert Watson 	}
26593cca425bSMichael Tuexen 	if (inp_flags & INP_RECVTOS) {
26603cca425bSMichael Tuexen 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
26613cca425bSMichael Tuexen 		comma = 1;
26623cca425bSMichael Tuexen 	}
2663497057eeSRobert Watson 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2664497057eeSRobert Watson 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2665497057eeSRobert Watson 		comma = 1;
2666497057eeSRobert Watson 	}
2667497057eeSRobert Watson 	if (inp_flags & IN6P_PKTINFO) {
2668497057eeSRobert Watson 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2669497057eeSRobert Watson 		comma = 1;
2670497057eeSRobert Watson 	}
2671497057eeSRobert Watson 	if (inp_flags & IN6P_HOPLIMIT) {
2672497057eeSRobert Watson 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2673497057eeSRobert Watson 		comma = 1;
2674497057eeSRobert Watson 	}
2675497057eeSRobert Watson 	if (inp_flags & IN6P_HOPOPTS) {
2676497057eeSRobert Watson 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2677497057eeSRobert Watson 		comma = 1;
2678497057eeSRobert Watson 	}
2679497057eeSRobert Watson 	if (inp_flags & IN6P_DSTOPTS) {
2680497057eeSRobert Watson 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2681497057eeSRobert Watson 		comma = 1;
2682497057eeSRobert Watson 	}
2683497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDR) {
2684497057eeSRobert Watson 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2685497057eeSRobert Watson 		comma = 1;
2686497057eeSRobert Watson 	}
2687497057eeSRobert Watson 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2688497057eeSRobert Watson 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2689497057eeSRobert Watson 		comma = 1;
2690497057eeSRobert Watson 	}
2691497057eeSRobert Watson 	if (inp_flags & IN6P_TCLASS) {
2692497057eeSRobert Watson 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2693497057eeSRobert Watson 		comma = 1;
2694497057eeSRobert Watson 	}
2695497057eeSRobert Watson 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2696497057eeSRobert Watson 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2697497057eeSRobert Watson 		comma = 1;
2698497057eeSRobert Watson 	}
2699ad71fe3cSRobert Watson 	if (inp_flags & INP_TIMEWAIT) {
2700ad71fe3cSRobert Watson 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2701ad71fe3cSRobert Watson 		comma  = 1;
2702ad71fe3cSRobert Watson 	}
2703ad71fe3cSRobert Watson 	if (inp_flags & INP_ONESBCAST) {
2704ad71fe3cSRobert Watson 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2705ad71fe3cSRobert Watson 		comma  = 1;
2706ad71fe3cSRobert Watson 	}
2707ad71fe3cSRobert Watson 	if (inp_flags & INP_DROPPED) {
2708ad71fe3cSRobert Watson 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2709ad71fe3cSRobert Watson 		comma  = 1;
2710ad71fe3cSRobert Watson 	}
2711ad71fe3cSRobert Watson 	if (inp_flags & INP_SOCKREF) {
2712ad71fe3cSRobert Watson 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2713ad71fe3cSRobert Watson 		comma  = 1;
2714ad71fe3cSRobert Watson 	}
2715497057eeSRobert Watson 	if (inp_flags & IN6P_RFC2292) {
2716497057eeSRobert Watson 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2717497057eeSRobert Watson 		comma = 1;
2718497057eeSRobert Watson 	}
2719497057eeSRobert Watson 	if (inp_flags & IN6P_MTU) {
2720497057eeSRobert Watson 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2721497057eeSRobert Watson 		comma = 1;
2722497057eeSRobert Watson 	}
2723497057eeSRobert Watson }
2724497057eeSRobert Watson 
2725497057eeSRobert Watson static void
2726497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag)
2727497057eeSRobert Watson {
2728497057eeSRobert Watson 	int comma;
2729497057eeSRobert Watson 
2730497057eeSRobert Watson 	comma = 0;
2731497057eeSRobert Watson 	if (inp_vflag & INP_IPV4) {
2732497057eeSRobert Watson 		db_printf("%sINP_IPV4", comma ? ", " : "");
2733497057eeSRobert Watson 		comma  = 1;
2734497057eeSRobert Watson 	}
2735497057eeSRobert Watson 	if (inp_vflag & INP_IPV6) {
2736497057eeSRobert Watson 		db_printf("%sINP_IPV6", comma ? ", " : "");
2737497057eeSRobert Watson 		comma  = 1;
2738497057eeSRobert Watson 	}
2739497057eeSRobert Watson 	if (inp_vflag & INP_IPV6PROTO) {
2740497057eeSRobert Watson 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2741497057eeSRobert Watson 		comma  = 1;
2742497057eeSRobert Watson 	}
2743497057eeSRobert Watson }
2744497057eeSRobert Watson 
27456d888973SRobert Watson static void
2746497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2747497057eeSRobert Watson {
2748497057eeSRobert Watson 
2749497057eeSRobert Watson 	db_print_indent(indent);
2750497057eeSRobert Watson 	db_printf("%s at %p\n", name, inp);
2751497057eeSRobert Watson 
2752497057eeSRobert Watson 	indent += 2;
2753497057eeSRobert Watson 
2754497057eeSRobert Watson 	db_print_indent(indent);
2755497057eeSRobert Watson 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2756497057eeSRobert Watson 
2757497057eeSRobert Watson 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2758497057eeSRobert Watson 
2759497057eeSRobert Watson 	db_print_indent(indent);
2760497057eeSRobert Watson 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2761497057eeSRobert Watson 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2762497057eeSRobert Watson 
2763497057eeSRobert Watson 	db_print_indent(indent);
2764497057eeSRobert Watson 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2765497057eeSRobert Watson 	   inp->inp_label, inp->inp_flags);
2766497057eeSRobert Watson 	db_print_inpflags(inp->inp_flags);
2767497057eeSRobert Watson 	db_printf(")\n");
2768497057eeSRobert Watson 
2769497057eeSRobert Watson 	db_print_indent(indent);
2770497057eeSRobert Watson 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2771497057eeSRobert Watson 	    inp->inp_vflag);
2772497057eeSRobert Watson 	db_print_inpvflag(inp->inp_vflag);
2773497057eeSRobert Watson 	db_printf(")\n");
2774497057eeSRobert Watson 
2775497057eeSRobert Watson 	db_print_indent(indent);
2776497057eeSRobert Watson 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2777497057eeSRobert Watson 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2778497057eeSRobert Watson 
2779497057eeSRobert Watson 	db_print_indent(indent);
2780497057eeSRobert Watson #ifdef INET6
2781497057eeSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
2782497057eeSRobert Watson 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2783497057eeSRobert Watson 		    "in6p_moptions: %p\n", inp->in6p_options,
2784497057eeSRobert Watson 		    inp->in6p_outputopts, inp->in6p_moptions);
2785497057eeSRobert Watson 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2786497057eeSRobert Watson 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2787497057eeSRobert Watson 		    inp->in6p_hops);
2788497057eeSRobert Watson 	} else
2789497057eeSRobert Watson #endif
2790497057eeSRobert Watson 	{
2791497057eeSRobert Watson 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2792497057eeSRobert Watson 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2793497057eeSRobert Watson 		    inp->inp_options, inp->inp_moptions);
2794497057eeSRobert Watson 	}
2795497057eeSRobert Watson 
2796497057eeSRobert Watson 	db_print_indent(indent);
2797497057eeSRobert Watson 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2798497057eeSRobert Watson 	    (uintmax_t)inp->inp_gencnt);
2799497057eeSRobert Watson }
2800497057eeSRobert Watson 
2801497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2802497057eeSRobert Watson {
2803497057eeSRobert Watson 	struct inpcb *inp;
2804497057eeSRobert Watson 
2805497057eeSRobert Watson 	if (!have_addr) {
2806497057eeSRobert Watson 		db_printf("usage: show inpcb <addr>\n");
2807497057eeSRobert Watson 		return;
2808497057eeSRobert Watson 	}
2809497057eeSRobert Watson 	inp = (struct inpcb *)addr;
2810497057eeSRobert Watson 
2811497057eeSRobert Watson 	db_print_inpcb(inp, "inpcb", 0);
2812497057eeSRobert Watson }
281383e521ecSBjoern A. Zeeb #endif /* DDB */
2814f3e7afe2SHans Petter Selasky 
2815f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
2816f3e7afe2SHans Petter Selasky /*
2817f3e7afe2SHans Petter Selasky  * Modify TX rate limit based on the existing "inp->inp_snd_tag",
2818f3e7afe2SHans Petter Selasky  * if any.
2819f3e7afe2SHans Petter Selasky  */
2820f3e7afe2SHans Petter Selasky int
2821f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate)
2822f3e7afe2SHans Petter Selasky {
2823f3e7afe2SHans Petter Selasky 	union if_snd_tag_modify_params params = {
2824f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
2825f3e7afe2SHans Petter Selasky 	};
2826f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2827f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2828f3e7afe2SHans Petter Selasky 	int error;
2829f3e7afe2SHans Petter Selasky 
2830f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2831f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2832f3e7afe2SHans Petter Selasky 		return (EINVAL);
2833f3e7afe2SHans Petter Selasky 
2834f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2835f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2836f3e7afe2SHans Petter Selasky 		return (EINVAL);
2837f3e7afe2SHans Petter Selasky 
2838f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_modify == NULL) {
2839f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2840f3e7afe2SHans Petter Selasky 	} else {
2841f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_modify(mst, &params);
2842f3e7afe2SHans Petter Selasky 	}
2843f3e7afe2SHans Petter Selasky 	return (error);
2844f3e7afe2SHans Petter Selasky }
2845f3e7afe2SHans Petter Selasky 
2846f3e7afe2SHans Petter Selasky /*
2847f3e7afe2SHans Petter Selasky  * Query existing TX rate limit based on the existing
2848f3e7afe2SHans Petter Selasky  * "inp->inp_snd_tag", if any.
2849f3e7afe2SHans Petter Selasky  */
2850f3e7afe2SHans Petter Selasky int
2851f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate)
2852f3e7afe2SHans Petter Selasky {
2853f3e7afe2SHans Petter Selasky 	union if_snd_tag_query_params params = { };
2854f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2855f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2856f3e7afe2SHans Petter Selasky 	int error;
2857f3e7afe2SHans Petter Selasky 
2858f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2859f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2860f3e7afe2SHans Petter Selasky 		return (EINVAL);
2861f3e7afe2SHans Petter Selasky 
2862f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2863f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2864f3e7afe2SHans Petter Selasky 		return (EINVAL);
2865f3e7afe2SHans Petter Selasky 
2866f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL) {
2867f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2868f3e7afe2SHans Petter Selasky 	} else {
2869f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_query(mst, &params);
2870f3e7afe2SHans Petter Selasky 		if (error == 0 &&  p_max_pacing_rate != NULL)
2871f3e7afe2SHans Petter Selasky 			*p_max_pacing_rate = params.rate_limit.max_rate;
2872f3e7afe2SHans Petter Selasky 	}
2873f3e7afe2SHans Petter Selasky 	return (error);
2874f3e7afe2SHans Petter Selasky }
2875f3e7afe2SHans Petter Selasky 
2876f3e7afe2SHans Petter Selasky /*
287795ed5015SHans Petter Selasky  * Query existing TX queue level based on the existing
287895ed5015SHans Petter Selasky  * "inp->inp_snd_tag", if any.
287995ed5015SHans Petter Selasky  */
288095ed5015SHans Petter Selasky int
288195ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level)
288295ed5015SHans Petter Selasky {
288395ed5015SHans Petter Selasky 	union if_snd_tag_query_params params = { };
288495ed5015SHans Petter Selasky 	struct m_snd_tag *mst;
288595ed5015SHans Petter Selasky 	struct ifnet *ifp;
288695ed5015SHans Petter Selasky 	int error;
288795ed5015SHans Petter Selasky 
288895ed5015SHans Petter Selasky 	mst = inp->inp_snd_tag;
288995ed5015SHans Petter Selasky 	if (mst == NULL)
289095ed5015SHans Petter Selasky 		return (EINVAL);
289195ed5015SHans Petter Selasky 
289295ed5015SHans Petter Selasky 	ifp = mst->ifp;
289395ed5015SHans Petter Selasky 	if (ifp == NULL)
289495ed5015SHans Petter Selasky 		return (EINVAL);
289595ed5015SHans Petter Selasky 
289695ed5015SHans Petter Selasky 	if (ifp->if_snd_tag_query == NULL)
289795ed5015SHans Petter Selasky 		return (EOPNOTSUPP);
289895ed5015SHans Petter Selasky 
289995ed5015SHans Petter Selasky 	error = ifp->if_snd_tag_query(mst, &params);
290095ed5015SHans Petter Selasky 	if (error == 0 &&  p_txqueue_level != NULL)
290195ed5015SHans Petter Selasky 		*p_txqueue_level = params.rate_limit.queue_level;
290295ed5015SHans Petter Selasky 	return (error);
290395ed5015SHans Petter Selasky }
290495ed5015SHans Petter Selasky 
290595ed5015SHans Petter Selasky /*
2906f3e7afe2SHans Petter Selasky  * Allocate a new TX rate limit send tag from the network interface
2907f3e7afe2SHans Petter Selasky  * given by the "ifp" argument and save it in "inp->inp_snd_tag":
2908f3e7afe2SHans Petter Selasky  */
2909f3e7afe2SHans Petter Selasky int
2910f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp,
2911f3e7afe2SHans Petter Selasky     uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate)
2912f3e7afe2SHans Petter Selasky {
2913f3e7afe2SHans Petter Selasky 	union if_snd_tag_alloc_params params = {
291495ed5015SHans Petter Selasky 		.rate_limit.hdr.type = (max_pacing_rate == -1U) ?
291595ed5015SHans Petter Selasky 		    IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT,
2916f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowid = flowid,
2917f3e7afe2SHans Petter Selasky 		.rate_limit.hdr.flowtype = flowtype,
2918f3e7afe2SHans Petter Selasky 		.rate_limit.max_rate = max_pacing_rate,
2919f3e7afe2SHans Petter Selasky 	};
2920f3e7afe2SHans Petter Selasky 	int error;
2921f3e7afe2SHans Petter Selasky 
2922f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
2923f3e7afe2SHans Petter Selasky 
2924f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag != NULL)
2925f3e7afe2SHans Petter Selasky 		return (EINVAL);
2926f3e7afe2SHans Petter Selasky 
2927f3e7afe2SHans Petter Selasky 	if (ifp->if_snd_tag_alloc == NULL) {
2928f3e7afe2SHans Petter Selasky 		error = EOPNOTSUPP;
2929f3e7afe2SHans Petter Selasky 	} else {
2930f3e7afe2SHans Petter Selasky 		error = ifp->if_snd_tag_alloc(ifp, &params, &inp->inp_snd_tag);
2931f3e7afe2SHans Petter Selasky 
2932f3e7afe2SHans Petter Selasky 		/*
2933f3e7afe2SHans Petter Selasky 		 * At success increment the refcount on
2934f3e7afe2SHans Petter Selasky 		 * the send tag's network interface:
2935f3e7afe2SHans Petter Selasky 		 */
2936f3e7afe2SHans Petter Selasky 		if (error == 0)
2937f3e7afe2SHans Petter Selasky 			if_ref(inp->inp_snd_tag->ifp);
2938f3e7afe2SHans Petter Selasky 	}
2939f3e7afe2SHans Petter Selasky 	return (error);
2940f3e7afe2SHans Petter Selasky }
2941f3e7afe2SHans Petter Selasky 
2942f3e7afe2SHans Petter Selasky /*
2943f3e7afe2SHans Petter Selasky  * Free an existing TX rate limit tag based on the "inp->inp_snd_tag",
2944f3e7afe2SHans Petter Selasky  * if any:
2945f3e7afe2SHans Petter Selasky  */
2946f3e7afe2SHans Petter Selasky void
2947f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp)
2948f3e7afe2SHans Petter Selasky {
2949f3e7afe2SHans Petter Selasky 	struct m_snd_tag *mst;
2950f3e7afe2SHans Petter Selasky 	struct ifnet *ifp;
2951f3e7afe2SHans Petter Selasky 
2952f3e7afe2SHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
2953f3e7afe2SHans Petter Selasky 
2954f3e7afe2SHans Petter Selasky 	mst = inp->inp_snd_tag;
2955f3e7afe2SHans Petter Selasky 	inp->inp_snd_tag = NULL;
2956f3e7afe2SHans Petter Selasky 
2957f3e7afe2SHans Petter Selasky 	if (mst == NULL)
2958f3e7afe2SHans Petter Selasky 		return;
2959f3e7afe2SHans Petter Selasky 
2960f3e7afe2SHans Petter Selasky 	ifp = mst->ifp;
2961f3e7afe2SHans Petter Selasky 	if (ifp == NULL)
2962f3e7afe2SHans Petter Selasky 		return;
2963f3e7afe2SHans Petter Selasky 
2964f3e7afe2SHans Petter Selasky 	/*
2965f3e7afe2SHans Petter Selasky 	 * If the device was detached while we still had reference(s)
2966f3e7afe2SHans Petter Selasky 	 * on the ifp, we assume if_snd_tag_free() was replaced with
2967f3e7afe2SHans Petter Selasky 	 * stubs.
2968f3e7afe2SHans Petter Selasky 	 */
2969f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_free(mst);
2970f3e7afe2SHans Petter Selasky 
2971f3e7afe2SHans Petter Selasky 	/* release reference count on network interface */
2972f3e7afe2SHans Petter Selasky 	if_rele(ifp);
2973f3e7afe2SHans Petter Selasky }
2974f3e7afe2SHans Petter Selasky 
2975f3e7afe2SHans Petter Selasky /*
2976f3e7afe2SHans Petter Selasky  * This function should be called when the INP_RATE_LIMIT_CHANGED flag
2977f3e7afe2SHans Petter Selasky  * is set in the fast path and will attach/detach/modify the TX rate
2978f3e7afe2SHans Petter Selasky  * limit send tag based on the socket's so_max_pacing_rate value.
2979f3e7afe2SHans Petter Selasky  */
2980f3e7afe2SHans Petter Selasky void
2981f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb)
2982f3e7afe2SHans Petter Selasky {
2983f3e7afe2SHans Petter Selasky 	struct socket *socket;
2984f3e7afe2SHans Petter Selasky 	uint32_t max_pacing_rate;
2985f3e7afe2SHans Petter Selasky 	bool did_upgrade;
2986f3e7afe2SHans Petter Selasky 	int error;
2987f3e7afe2SHans Petter Selasky 
2988f3e7afe2SHans Petter Selasky 	if (inp == NULL)
2989f3e7afe2SHans Petter Selasky 		return;
2990f3e7afe2SHans Petter Selasky 
2991f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
2992f3e7afe2SHans Petter Selasky 	if (socket == NULL)
2993f3e7afe2SHans Petter Selasky 		return;
2994f3e7afe2SHans Petter Selasky 
2995f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
2996f3e7afe2SHans Petter Selasky 		/*
2997f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
2998f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
2999f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3000f3e7afe2SHans Petter Selasky 		 * write lock.
3001f3e7afe2SHans Petter Selasky 		 */
3002f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3003f3e7afe2SHans Petter Selasky 			return;
3004f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3005f3e7afe2SHans Petter Selasky 	} else {
3006f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3007f3e7afe2SHans Petter Selasky 	}
3008f3e7afe2SHans Petter Selasky 
3009f3e7afe2SHans Petter Selasky 	/*
3010f3e7afe2SHans Petter Selasky 	 * NOTE: The so_max_pacing_rate value is read unlocked,
3011f3e7afe2SHans Petter Selasky 	 * because atomic updates are not required since the variable
3012f3e7afe2SHans Petter Selasky 	 * is checked at every mbuf we send. It is assumed that the
3013f3e7afe2SHans Petter Selasky 	 * variable read itself will be atomic.
3014f3e7afe2SHans Petter Selasky 	 */
3015f3e7afe2SHans Petter Selasky 	max_pacing_rate = socket->so_max_pacing_rate;
3016f3e7afe2SHans Petter Selasky 
3017f3e7afe2SHans Petter Selasky 	/*
3018f3e7afe2SHans Petter Selasky 	 * NOTE: When attaching to a network interface a reference is
3019f3e7afe2SHans Petter Selasky 	 * made to ensure the network interface doesn't go away until
3020f3e7afe2SHans Petter Selasky 	 * all ratelimit connections are gone. The network interface
3021f3e7afe2SHans Petter Selasky 	 * pointers compared below represent valid network interfaces,
3022f3e7afe2SHans Petter Selasky 	 * except when comparing towards NULL.
3023f3e7afe2SHans Petter Selasky 	 */
3024f3e7afe2SHans Petter Selasky 	if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) {
3025f3e7afe2SHans Petter Selasky 		error = 0;
3026f3e7afe2SHans Petter Selasky 	} else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) {
3027f3e7afe2SHans Petter Selasky 		if (inp->inp_snd_tag != NULL)
3028f3e7afe2SHans Petter Selasky 			in_pcbdetach_txrtlmt(inp);
3029f3e7afe2SHans Petter Selasky 		error = 0;
3030f3e7afe2SHans Petter Selasky 	} else if (inp->inp_snd_tag == NULL) {
3031f3e7afe2SHans Petter Selasky 		/*
3032f3e7afe2SHans Petter Selasky 		 * In order to utilize packet pacing with RSS, we need
3033f3e7afe2SHans Petter Selasky 		 * to wait until there is a valid RSS hash before we
3034f3e7afe2SHans Petter Selasky 		 * can proceed:
3035f3e7afe2SHans Petter Selasky 		 */
3036f3e7afe2SHans Petter Selasky 		if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) {
3037f3e7afe2SHans Petter Selasky 			error = EAGAIN;
3038f3e7afe2SHans Petter Selasky 		} else {
3039f3e7afe2SHans Petter Selasky 			error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb),
3040f3e7afe2SHans Petter Selasky 			    mb->m_pkthdr.flowid, max_pacing_rate);
3041f3e7afe2SHans Petter Selasky 		}
3042f3e7afe2SHans Petter Selasky 	} else {
3043f3e7afe2SHans Petter Selasky 		error = in_pcbmodify_txrtlmt(inp, max_pacing_rate);
3044f3e7afe2SHans Petter Selasky 	}
3045f3e7afe2SHans Petter Selasky 	if (error == 0 || error == EOPNOTSUPP)
3046f3e7afe2SHans Petter Selasky 		inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
3047f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3048f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3049f3e7afe2SHans Petter Selasky }
3050f3e7afe2SHans Petter Selasky 
3051f3e7afe2SHans Petter Selasky /*
3052f3e7afe2SHans Petter Selasky  * Track route changes for TX rate limiting.
3053f3e7afe2SHans Petter Selasky  */
3054f3e7afe2SHans Petter Selasky void
3055f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp)
3056f3e7afe2SHans Petter Selasky {
3057f3e7afe2SHans Petter Selasky 	struct socket *socket;
3058f3e7afe2SHans Petter Selasky 	bool did_upgrade;
3059f3e7afe2SHans Petter Selasky 
3060f3e7afe2SHans Petter Selasky 	if (inp == NULL)
3061f3e7afe2SHans Petter Selasky 		return;
3062f3e7afe2SHans Petter Selasky 
3063f3e7afe2SHans Petter Selasky 	socket = inp->inp_socket;
3064f3e7afe2SHans Petter Selasky 	if (socket == NULL)
3065f3e7afe2SHans Petter Selasky 		return;
3066f3e7afe2SHans Petter Selasky 
3067f3e7afe2SHans Petter Selasky 	if (inp->inp_snd_tag == NULL)
3068f3e7afe2SHans Petter Selasky 		return;
3069f3e7afe2SHans Petter Selasky 
3070f3e7afe2SHans Petter Selasky 	if (!INP_WLOCKED(inp)) {
3071f3e7afe2SHans Petter Selasky 		/*
3072f3e7afe2SHans Petter Selasky 		 * NOTE: If the write locking fails, we need to bail
3073f3e7afe2SHans Petter Selasky 		 * out and use the non-ratelimited ring for the
3074f3e7afe2SHans Petter Selasky 		 * transmit until there is a new chance to get the
3075f3e7afe2SHans Petter Selasky 		 * write lock.
3076f3e7afe2SHans Petter Selasky 		 */
3077f3e7afe2SHans Petter Selasky 		if (!INP_TRY_UPGRADE(inp))
3078f3e7afe2SHans Petter Selasky 			return;
3079f3e7afe2SHans Petter Selasky 		did_upgrade = 1;
3080f3e7afe2SHans Petter Selasky 	} else {
3081f3e7afe2SHans Petter Selasky 		did_upgrade = 0;
3082f3e7afe2SHans Petter Selasky 	}
3083f3e7afe2SHans Petter Selasky 
3084f3e7afe2SHans Petter Selasky 	/* detach rate limiting */
3085f3e7afe2SHans Petter Selasky 	in_pcbdetach_txrtlmt(inp);
3086f3e7afe2SHans Petter Selasky 
3087f3e7afe2SHans Petter Selasky 	/* make sure new mbuf send tag allocation is made */
3088f3e7afe2SHans Petter Selasky 	inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
3089f3e7afe2SHans Petter Selasky 
3090f3e7afe2SHans Petter Selasky 	if (did_upgrade)
3091f3e7afe2SHans Petter Selasky 		INP_DOWNGRADE(inp);
3092f3e7afe2SHans Petter Selasky }
3093f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */
3094