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> 613ee9c3c4SRandall 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> 913ee9c3c4SRandall Stewart #ifdef TCPHPTS 923ee9c3c4SRandall Stewart #include <netinet/tcp_hpts.h> 933ee9c3c4SRandall 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 */ 5167875017cSSean Bruno short 517efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 518efdf104bSMikolaj Golub { 5197875017cSSean Bruno 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 589d7c5a620SMatt Macy if (CK_STAILQ_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); 5947875017cSSean Bruno 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 || 6707875017cSSean Bruno (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 || 6957875017cSSean Bruno (reuseport & tw->tw_so_options) == 0) 696340c35deSJonathan Lemon return (EADDRINUSE); 6970a100a6fSAdrian Chadd } else if (t && 6980a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 6997875017cSSean Bruno (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); 854d7c5a620SMatt Macy CK_STAILQ_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); 913d7c5a620SMatt Macy CK_STAILQ_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); 979d7c5a620SMatt Macy CK_STAILQ_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 1058d7c5a620SMatt Macy if (!CK_STAILQ_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 = 1069d7c5a620SMatt Macy IA_SIN(CK_STAILQ_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); 1076d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 10772d9cfabaSRobert Watson IFF_BROADCAST) 1078d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_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); 1099d7c5a620SMatt Macy CK_STAILQ_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__)); 12333ee9c3c4SRandall Stewart #ifdef TCPHPTS 12343ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 12353ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 12363ee9c3c4SRandall Stewart /* 12373ee9c3c4SRandall Stewart * We should not be on the hpts at 12383ee9c3c4SRandall Stewart * this point in any form. we must 12393ee9c3c4SRandall Stewart * get the lock to be sure. 12403ee9c3c4SRandall Stewart */ 12413ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 12423ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 12433ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 12443ee9c3c4SRandall Stewart hpts, inp); 12453ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 12463ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 12473ee9c3c4SRandall Stewart if (inp->inp_in_input) 12483ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 12493ee9c3c4SRandall Stewart hpts, inp); 12503ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 12513ee9c3c4SRandall Stewart } 12523ee9c3c4SRandall 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__)); 12813ee9c3c4SRandall Stewart #ifdef TCPHPTS 12823ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 12833ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 12843ee9c3c4SRandall Stewart /* 12853ee9c3c4SRandall Stewart * We should not be on the hpts at 12863ee9c3c4SRandall Stewart * this point in any form. we must 12873ee9c3c4SRandall Stewart * get the lock to be sure. 12883ee9c3c4SRandall Stewart */ 12893ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 12903ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 12913ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 12923ee9c3c4SRandall Stewart hpts, inp); 12933ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 12943ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 12953ee9c3c4SRandall Stewart if (inp->inp_in_input) 12963ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 12973ee9c3c4SRandall Stewart hpts, inp); 12983ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 12993ee9c3c4SRandall Stewart } 13003ee9c3c4SRandall 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 1317ddece765SMatt Macy void 1318ddece765SMatt Macy in_pcblist_rele_rlocked(epoch_context_t ctx) 1319ddece765SMatt Macy { 1320ddece765SMatt Macy struct in_pcblist *il; 1321ddece765SMatt Macy struct inpcb *inp; 1322ddece765SMatt Macy struct inpcbinfo *pcbinfo; 1323ddece765SMatt Macy int i, n; 1324ddece765SMatt Macy 1325ddece765SMatt Macy il = __containerof(ctx, struct in_pcblist, il_epoch_ctx); 1326ddece765SMatt Macy pcbinfo = il->il_pcbinfo; 1327ddece765SMatt Macy n = il->il_count; 1328ddece765SMatt Macy INP_INFO_WLOCK(pcbinfo); 1329ddece765SMatt Macy for (i = 0; i < n; i++) { 1330ddece765SMatt Macy inp = il->il_inp_list[i]; 1331ddece765SMatt Macy INP_RLOCK(inp); 1332ddece765SMatt Macy if (!in_pcbrele_rlocked(inp)) 1333ddece765SMatt Macy INP_RUNLOCK(inp); 1334ddece765SMatt Macy } 1335ddece765SMatt Macy INP_INFO_WUNLOCK(pcbinfo); 1336ddece765SMatt Macy free(il, M_TEMP); 1337ddece765SMatt Macy } 1338ddece765SMatt Macy 133979bdc6e5SRobert Watson /* 134079bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 134179bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 134279bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 134379bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 134479bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 134579bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 134679bdc6e5SRobert Watson * the pcbinfo lock is held. 134779bdc6e5SRobert Watson */ 134879bdc6e5SRobert Watson void 134979bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 135079bdc6e5SRobert Watson { 1351*f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1352*f42a83f2SMatt Macy 13531a3d880cSMatt Macy #ifdef INET6 13541a3d880cSMatt Macy struct ip6_moptions *im6o = NULL; 13551a3d880cSMatt Macy #endif 13561a3d880cSMatt Macy #ifdef INET 13571a3d880cSMatt Macy struct ip_moptions *imo = NULL; 13581a3d880cSMatt Macy #endif 135979bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1360*f42a83f2SMatt Macy 136145a48d07SJonathan T. Looney KASSERT((inp->inp_flags2 & INP_FREED) == 0, 136245a48d07SJonathan T. Looney ("%s: called twice for pcb %p", __func__, inp)); 136345a48d07SJonathan T. Looney if (inp->inp_flags2 & INP_FREED) { 136445a48d07SJonathan T. Looney INP_WUNLOCK(inp); 136545a48d07SJonathan T. Looney return; 136645a48d07SJonathan T. Looney } 136745a48d07SJonathan T. Looney 1368ff9b006dSJulien Charbon #ifdef INVARIANTS 1369ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 1370079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(pcbinfo); 1371ff9b006dSJulien Charbon } else { 137279bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 1373ff9b006dSJulien Charbon } 1374ff9b006dSJulien Charbon #endif 137579bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1376*f42a83f2SMatt Macy 13771a3d880cSMatt Macy #ifdef INET 13781a3d880cSMatt Macy imo = inp->inp_moptions; 13791a3d880cSMatt Macy inp->inp_moptions = NULL; 13801a3d880cSMatt Macy #endif 1381*f42a83f2SMatt Macy /* XXXRW: Do as much as possible here. */ 1382*f42a83f2SMatt Macy #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1383*f42a83f2SMatt Macy if (inp->inp_sp != NULL) 1384*f42a83f2SMatt Macy ipsec_delete_pcbpolicy(inp); 1385*f42a83f2SMatt Macy #endif 1386*f42a83f2SMatt Macy INP_LIST_WLOCK(pcbinfo); 1387*f42a83f2SMatt Macy inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1388*f42a83f2SMatt Macy in_pcbremlists(inp); 1389*f42a83f2SMatt Macy INP_LIST_WUNLOCK(pcbinfo); 13901a3d880cSMatt Macy #ifdef INET6 13911a3d880cSMatt Macy if (inp->inp_vflag & INP_IPV6PROTO) { 13921a3d880cSMatt Macy ip6_freepcbopts(inp->in6p_outputopts); 13931a3d880cSMatt Macy im6o = inp->in6p_moptions; 13941a3d880cSMatt Macy inp->in6p_moptions = NULL; 13951a3d880cSMatt Macy } 13961a3d880cSMatt Macy #endif 1397*f42a83f2SMatt Macy if (inp->inp_options) 1398*f42a83f2SMatt Macy (void)m_free(inp->inp_options); 1399fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1400*f42a83f2SMatt Macy 1401*f42a83f2SMatt Macy inp->inp_vflag = 0; 1402*f42a83f2SMatt Macy inp->inp_flags2 |= INP_FREED; 1403*f42a83f2SMatt Macy crfree(inp->inp_cred); 1404*f42a83f2SMatt Macy #ifdef MAC 1405*f42a83f2SMatt Macy mac_inpcb_destroy(inp); 1406*f42a83f2SMatt Macy #endif 1407*f42a83f2SMatt Macy #ifdef INET6 1408*f42a83f2SMatt Macy ip6_freemoptions(im6o); 1409*f42a83f2SMatt Macy #endif 1410*f42a83f2SMatt Macy #ifdef INET 1411*f42a83f2SMatt Macy inp_freemoptions(imo); 1412*f42a83f2SMatt Macy #endif 1413*f42a83f2SMatt Macy if (!in_pcbrele_wlocked(inp)) 1414cb6bb230SMatt Macy INP_WUNLOCK(inp); 141528696211SRobert Watson } 141628696211SRobert Watson 141728696211SRobert Watson /* 1418c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1419c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1420c0a211c5SRobert Watson * 1421c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1422c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1423c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1424c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1425c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1426c0a211c5SRobert Watson * in_pcbdetach(). 1427c0a211c5SRobert Watson * 1428c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1429c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 143010702a28SRobert Watson */ 143110702a28SRobert Watson void 143210702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 143310702a28SRobert Watson { 143410702a28SRobert Watson 14358501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 143610702a28SRobert Watson 1437fa046d87SRobert Watson /* 1438fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1439fa046d87SRobert Watson * the hash lock...? 1440fa046d87SRobert Watson */ 1441ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1442111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 144310702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 144410702a28SRobert Watson 1445fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 144610702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 144710702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 144810702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 144910702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 145010702a28SRobert Watson free(phd, M_PCB); 145110702a28SRobert Watson } 1452fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1453111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 145452cd27cbSRobert Watson #ifdef PCBGROUP 145552cd27cbSRobert Watson in_pcbgroup_remove(inp); 145652cd27cbSRobert Watson #endif 145710702a28SRobert Watson } 145810702a28SRobert Watson } 145910702a28SRobert Watson 146067107f45SBjoern A. Zeeb #ifdef INET 146154d642bbSRobert Watson /* 146254d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 146354d642bbSRobert Watson */ 146426ef6ac4SDon Lewis struct sockaddr * 1465136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 146626ef6ac4SDon Lewis { 146726ef6ac4SDon Lewis struct sockaddr_in *sin; 146826ef6ac4SDon Lewis 14691ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1470a163d034SWarner Losh M_WAITOK | M_ZERO); 147126ef6ac4SDon Lewis sin->sin_family = AF_INET; 147226ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 147326ef6ac4SDon Lewis sin->sin_addr = *addr_p; 147426ef6ac4SDon Lewis sin->sin_port = port; 147526ef6ac4SDon Lewis 147626ef6ac4SDon Lewis return (struct sockaddr *)sin; 147726ef6ac4SDon Lewis } 147826ef6ac4SDon Lewis 1479117bcae7SGarrett Wollman int 148054d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1481df8bae1dSRodney W. Grimes { 1482136d4f1cSRobert Watson struct inpcb *inp; 148326ef6ac4SDon Lewis struct in_addr addr; 148426ef6ac4SDon Lewis in_port_t port; 148542fa505bSDavid Greenman 1486fdc984f7STor Egge inp = sotoinpcb(so); 148754d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 14886466b28aSRobert Watson 1489a69042a5SRobert Watson INP_RLOCK(inp); 149026ef6ac4SDon Lewis port = inp->inp_lport; 149126ef6ac4SDon Lewis addr = inp->inp_laddr; 1492a69042a5SRobert Watson INP_RUNLOCK(inp); 149342fa505bSDavid Greenman 149426ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1495117bcae7SGarrett Wollman return 0; 1496df8bae1dSRodney W. Grimes } 1497df8bae1dSRodney W. Grimes 1498117bcae7SGarrett Wollman int 149954d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1500df8bae1dSRodney W. Grimes { 1501136d4f1cSRobert Watson struct inpcb *inp; 150226ef6ac4SDon Lewis struct in_addr addr; 150326ef6ac4SDon Lewis in_port_t port; 150442fa505bSDavid Greenman 1505fdc984f7STor Egge inp = sotoinpcb(so); 150654d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 15076466b28aSRobert Watson 1508a69042a5SRobert Watson INP_RLOCK(inp); 150926ef6ac4SDon Lewis port = inp->inp_fport; 151026ef6ac4SDon Lewis addr = inp->inp_faddr; 1511a69042a5SRobert Watson INP_RUNLOCK(inp); 151242fa505bSDavid Greenman 151326ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1514117bcae7SGarrett Wollman return 0; 1515df8bae1dSRodney W. Grimes } 1516df8bae1dSRodney W. Grimes 151726f9a767SRodney W. Grimes void 1518136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1519136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1520d1c54148SJesper Skriver { 1521f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1522d1c54148SJesper Skriver 15233dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1524f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 15258501a69cSRobert Watson INP_WLOCK(inp); 1526d1c54148SJesper Skriver #ifdef INET6 1527f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 15288501a69cSRobert Watson INP_WUNLOCK(inp); 1529d1c54148SJesper Skriver continue; 1530f76fcf6dSJeffrey Hsu } 1531d1c54148SJesper Skriver #endif 1532d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1533f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 15348501a69cSRobert Watson INP_WUNLOCK(inp); 1535d1c54148SJesper Skriver continue; 1536d1c54148SJesper Skriver } 15373dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 15388501a69cSRobert Watson INP_WUNLOCK(inp); 1539f76fcf6dSJeffrey Hsu } 15403dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1541d1c54148SJesper Skriver } 1542d1c54148SJesper Skriver 1543e43cc4aeSHajimu UMEMOTO void 1544136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1545e43cc4aeSHajimu UMEMOTO { 1546e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1547e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1548e43cc4aeSHajimu UMEMOTO int i, gap; 1549e43cc4aeSHajimu UMEMOTO 1550ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1551712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 15528501a69cSRobert Watson INP_WLOCK(inp); 1553e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1554e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1555e43cc4aeSHajimu UMEMOTO imo != NULL) { 1556e43cc4aeSHajimu UMEMOTO /* 1557e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1558e43cc4aeSHajimu UMEMOTO * detached. 1559e43cc4aeSHajimu UMEMOTO */ 1560e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1561e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1562e43cc4aeSHajimu UMEMOTO 1563e43cc4aeSHajimu UMEMOTO /* 1564e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1565e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1566cb6bb230SMatt Macy * 1567cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1568e43cc4aeSHajimu UMEMOTO */ 1569e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1570e43cc4aeSHajimu UMEMOTO i++) { 1571e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1572f3e1324bSStephen Hurd IN_MULTI_LOCK_ASSERT(); 1573f3e1324bSStephen Hurd in_leavegroup_locked(imo->imo_membership[i], NULL); 1574e43cc4aeSHajimu UMEMOTO gap++; 1575e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1576e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1577e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1578e43cc4aeSHajimu UMEMOTO } 1579e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1580e43cc4aeSHajimu UMEMOTO } 15818501a69cSRobert Watson INP_WUNLOCK(inp); 1582e43cc4aeSHajimu UMEMOTO } 1583ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1584e43cc4aeSHajimu UMEMOTO } 1585e43cc4aeSHajimu UMEMOTO 1586df8bae1dSRodney W. Grimes /* 1587fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1588fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1589c3229e05SDavid Greenman */ 1590d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1591df8bae1dSRodney W. Grimes struct inpcb * 1592136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 159368e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1594df8bae1dSRodney W. Grimes { 1595136d4f1cSRobert Watson struct inpcb *inp; 1596d5e8a67eSHajimu UMEMOTO #ifdef INET6 1597d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1598d5e8a67eSHajimu UMEMOTO #else 1599d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1600d5e8a67eSHajimu UMEMOTO #endif 1601d5e8a67eSHajimu UMEMOTO int wildcard; 16027bc4aca7SDavid Greenman 160368e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 160468e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 160568e0d7e0SRobert Watson 1606fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 16071b73ca0bSSam Leffler 160868e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1609c3229e05SDavid Greenman struct inpcbhead *head; 1610c3229e05SDavid Greenman /* 1611c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1612c3229e05SDavid Greenman * matches the local address and port we're looking for. 1613c3229e05SDavid Greenman */ 1614712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1615712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1616fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1617cfa1ca9dSYoshinobu Inoue #ifdef INET6 1618413628a7SBjoern A. Zeeb /* XXX inp locking */ 1619369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1620cfa1ca9dSYoshinobu Inoue continue; 1621cfa1ca9dSYoshinobu Inoue #endif 1622c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1623c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1624c3229e05SDavid Greenman inp->inp_lport == lport) { 1625c3229e05SDavid Greenman /* 1626413628a7SBjoern A. Zeeb * Found? 1627c3229e05SDavid Greenman */ 1628413628a7SBjoern A. Zeeb if (cred == NULL || 16290304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 16300304c731SJamie Gritton inp->inp_cred->cr_prison)) 1631c3229e05SDavid Greenman return (inp); 1632df8bae1dSRodney W. Grimes } 1633c3229e05SDavid Greenman } 1634c3229e05SDavid Greenman /* 1635c3229e05SDavid Greenman * Not found. 1636c3229e05SDavid Greenman */ 1637c3229e05SDavid Greenman return (NULL); 1638c3229e05SDavid Greenman } else { 1639c3229e05SDavid Greenman struct inpcbporthead *porthash; 1640c3229e05SDavid Greenman struct inpcbport *phd; 1641c3229e05SDavid Greenman struct inpcb *match = NULL; 1642c3229e05SDavid Greenman /* 1643c3229e05SDavid Greenman * Best fit PCB lookup. 1644c3229e05SDavid Greenman * 1645c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1646c3229e05SDavid Greenman * port hash list. 1647c3229e05SDavid Greenman */ 1648712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1649712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1650fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1651c3229e05SDavid Greenman if (phd->phd_port == lport) 1652c3229e05SDavid Greenman break; 1653c3229e05SDavid Greenman } 1654c3229e05SDavid Greenman if (phd != NULL) { 1655c3229e05SDavid Greenman /* 1656c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1657c3229e05SDavid Greenman * fit. 1658c3229e05SDavid Greenman */ 165937d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1660c3229e05SDavid Greenman wildcard = 0; 1661413628a7SBjoern A. Zeeb if (cred != NULL && 16620304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 16630304c731SJamie Gritton cred->cr_prison)) 1664413628a7SBjoern A. Zeeb continue; 1665cfa1ca9dSYoshinobu Inoue #ifdef INET6 1666413628a7SBjoern A. Zeeb /* XXX inp locking */ 1667369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1668cfa1ca9dSYoshinobu Inoue continue; 1669d5e8a67eSHajimu UMEMOTO /* 1670d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1671d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1672d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1673d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1674d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1675d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1676d5e8a67eSHajimu UMEMOTO * 1677d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1678d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1679d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1680d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1681d5e8a67eSHajimu UMEMOTO */ 1682d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1683d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1684cfa1ca9dSYoshinobu Inoue #endif 1685c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1686c3229e05SDavid Greenman wildcard++; 168715bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 168815bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 168915bd2b43SDavid Greenman wildcard++; 169015bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 169115bd2b43SDavid Greenman continue; 169215bd2b43SDavid Greenman } else { 169315bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 169415bd2b43SDavid Greenman wildcard++; 169515bd2b43SDavid Greenman } 1696df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1697df8bae1dSRodney W. Grimes match = inp; 1698df8bae1dSRodney W. Grimes matchwild = wildcard; 1699413628a7SBjoern A. Zeeb if (matchwild == 0) 1700df8bae1dSRodney W. Grimes break; 1701df8bae1dSRodney W. Grimes } 1702df8bae1dSRodney W. Grimes } 17033dbdc25cSDavid Greenman } 1704df8bae1dSRodney W. Grimes return (match); 1705df8bae1dSRodney W. Grimes } 1706c3229e05SDavid Greenman } 1707d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 170815bd2b43SDavid Greenman 170952cd27cbSRobert Watson #ifdef PCBGROUP 171052cd27cbSRobert Watson /* 171152cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 171252cd27cbSRobert Watson */ 171352cd27cbSRobert Watson static struct inpcb * 171452cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 171552cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 171652cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 171752cd27cbSRobert Watson { 171852cd27cbSRobert Watson struct inpcbhead *head; 171952cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 172052cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 17217fb2986fSJonathan T. Looney bool locked; 172252cd27cbSRobert Watson 172352cd27cbSRobert Watson /* 172452cd27cbSRobert Watson * First look for an exact match. 172552cd27cbSRobert Watson */ 172652cd27cbSRobert Watson tmpinp = NULL; 172752cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 172852cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 172952cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 173052cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgrouphash) { 173152cd27cbSRobert Watson #ifdef INET6 173252cd27cbSRobert Watson /* XXX inp locking */ 173352cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 173452cd27cbSRobert Watson continue; 173552cd27cbSRobert Watson #endif 173652cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 173752cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 173852cd27cbSRobert Watson inp->inp_fport == fport && 173952cd27cbSRobert Watson inp->inp_lport == lport) { 174052cd27cbSRobert Watson /* 174152cd27cbSRobert Watson * XXX We should be able to directly return 174252cd27cbSRobert Watson * the inp here, without any checks. 174352cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 174452cd27cbSRobert Watson */ 174552cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 174652cd27cbSRobert Watson goto found; 174752cd27cbSRobert Watson if (tmpinp == NULL) 174852cd27cbSRobert Watson tmpinp = inp; 174952cd27cbSRobert Watson } 175052cd27cbSRobert Watson } 175152cd27cbSRobert Watson if (tmpinp != NULL) { 175252cd27cbSRobert Watson inp = tmpinp; 175352cd27cbSRobert Watson goto found; 175452cd27cbSRobert Watson } 175552cd27cbSRobert Watson 17560a100a6fSAdrian Chadd #ifdef RSS 17570a100a6fSAdrian Chadd /* 17580a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 17590a100a6fSAdrian Chadd * match for an RSS-local socket. 17600a100a6fSAdrian Chadd */ 17610a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 17620a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 17630a100a6fSAdrian Chadd #ifdef INET6 17640a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 17650a100a6fSAdrian Chadd #endif 17660a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 17670a100a6fSAdrian Chadd struct inpcbhead *head; 17680a100a6fSAdrian Chadd int injail; 17690a100a6fSAdrian Chadd 17700a100a6fSAdrian Chadd /* 17710a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 17720a100a6fSAdrian Chadd * 1. jailed, non-wild. 17730a100a6fSAdrian Chadd * 2. jailed, wild. 17740a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 17750a100a6fSAdrian Chadd * 4. non-jailed, wild. 17760a100a6fSAdrian Chadd */ 17770a100a6fSAdrian Chadd 17780a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 17790a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 17800a100a6fSAdrian Chadd LIST_FOREACH(inp, head, inp_pcbgrouphash) { 17810a100a6fSAdrian Chadd #ifdef INET6 17820a100a6fSAdrian Chadd /* XXX inp locking */ 17830a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 17840a100a6fSAdrian Chadd continue; 17850a100a6fSAdrian Chadd #endif 17860a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 17870a100a6fSAdrian Chadd inp->inp_lport != lport) 17880a100a6fSAdrian Chadd continue; 17890a100a6fSAdrian Chadd 17900a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 17910a100a6fSAdrian Chadd if (injail) { 17920a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 17930a100a6fSAdrian Chadd &laddr) != 0) 17940a100a6fSAdrian Chadd continue; 17950a100a6fSAdrian Chadd } else { 17960a100a6fSAdrian Chadd if (local_exact != NULL) 17970a100a6fSAdrian Chadd continue; 17980a100a6fSAdrian Chadd } 17990a100a6fSAdrian Chadd 18000a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 18010a100a6fSAdrian Chadd if (injail) 18020a100a6fSAdrian Chadd goto found; 18030a100a6fSAdrian Chadd else 18040a100a6fSAdrian Chadd local_exact = inp; 18050a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 18060a100a6fSAdrian Chadd #ifdef INET6 18070a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 18080a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 18090a100a6fSAdrian Chadd local_wild_mapped = inp; 18100a100a6fSAdrian Chadd else 18110a100a6fSAdrian Chadd #endif 18120a100a6fSAdrian Chadd if (injail) 18130a100a6fSAdrian Chadd jail_wild = inp; 18140a100a6fSAdrian Chadd else 18150a100a6fSAdrian Chadd local_wild = inp; 18160a100a6fSAdrian Chadd } 18170a100a6fSAdrian Chadd } /* LIST_FOREACH */ 18180a100a6fSAdrian Chadd 18190a100a6fSAdrian Chadd inp = jail_wild; 18200a100a6fSAdrian Chadd if (inp == NULL) 18210a100a6fSAdrian Chadd inp = local_exact; 18220a100a6fSAdrian Chadd if (inp == NULL) 18230a100a6fSAdrian Chadd inp = local_wild; 18240a100a6fSAdrian Chadd #ifdef INET6 18250a100a6fSAdrian Chadd if (inp == NULL) 18260a100a6fSAdrian Chadd inp = local_wild_mapped; 18270a100a6fSAdrian Chadd #endif 18280a100a6fSAdrian Chadd if (inp != NULL) 18290a100a6fSAdrian Chadd goto found; 18300a100a6fSAdrian Chadd } 18310a100a6fSAdrian Chadd #endif 18320a100a6fSAdrian Chadd 183352cd27cbSRobert Watson /* 183452cd27cbSRobert Watson * Then look for a wildcard match, if requested. 183552cd27cbSRobert Watson */ 183652cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 183752cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 183852cd27cbSRobert Watson #ifdef INET6 183952cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 184052cd27cbSRobert Watson #endif 184152cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 184252cd27cbSRobert Watson struct inpcbhead *head; 184352cd27cbSRobert Watson int injail; 184452cd27cbSRobert Watson 184552cd27cbSRobert Watson /* 184652cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 184752cd27cbSRobert Watson * 1. jailed, non-wild. 184852cd27cbSRobert Watson * 2. jailed, wild. 184952cd27cbSRobert Watson * 3. non-jailed, non-wild. 185052cd27cbSRobert Watson * 4. non-jailed, wild. 185152cd27cbSRobert Watson */ 185252cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 185352cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 185452cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 185552cd27cbSRobert Watson #ifdef INET6 185652cd27cbSRobert Watson /* XXX inp locking */ 185752cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 185852cd27cbSRobert Watson continue; 185952cd27cbSRobert Watson #endif 186052cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 186152cd27cbSRobert Watson inp->inp_lport != lport) 186252cd27cbSRobert Watson continue; 186352cd27cbSRobert Watson 186452cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 186552cd27cbSRobert Watson if (injail) { 186652cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 186752cd27cbSRobert Watson &laddr) != 0) 186852cd27cbSRobert Watson continue; 186952cd27cbSRobert Watson } else { 187052cd27cbSRobert Watson if (local_exact != NULL) 187152cd27cbSRobert Watson continue; 187252cd27cbSRobert Watson } 187352cd27cbSRobert Watson 187452cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 187552cd27cbSRobert Watson if (injail) 187652cd27cbSRobert Watson goto found; 187752cd27cbSRobert Watson else 187852cd27cbSRobert Watson local_exact = inp; 187952cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 188052cd27cbSRobert Watson #ifdef INET6 188152cd27cbSRobert Watson /* XXX inp locking, NULL check */ 188252cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 188352cd27cbSRobert Watson local_wild_mapped = inp; 188452cd27cbSRobert Watson else 188583e521ecSBjoern A. Zeeb #endif 188652cd27cbSRobert Watson if (injail) 188752cd27cbSRobert Watson jail_wild = inp; 188852cd27cbSRobert Watson else 188952cd27cbSRobert Watson local_wild = inp; 189052cd27cbSRobert Watson } 189152cd27cbSRobert Watson } /* LIST_FOREACH */ 189252cd27cbSRobert Watson inp = jail_wild; 189352cd27cbSRobert Watson if (inp == NULL) 189452cd27cbSRobert Watson inp = local_exact; 189552cd27cbSRobert Watson if (inp == NULL) 189652cd27cbSRobert Watson inp = local_wild; 189752cd27cbSRobert Watson #ifdef INET6 189852cd27cbSRobert Watson if (inp == NULL) 189952cd27cbSRobert Watson inp = local_wild_mapped; 190083e521ecSBjoern A. Zeeb #endif 190152cd27cbSRobert Watson if (inp != NULL) 190252cd27cbSRobert Watson goto found; 190352cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 190452cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 190552cd27cbSRobert Watson return (NULL); 190652cd27cbSRobert Watson 190752cd27cbSRobert Watson found: 19087fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 1909b9a9e8e9SNavdeep Parhar locked = INP_TRY_WLOCK(inp); 19107fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 1911b9a9e8e9SNavdeep Parhar locked = INP_TRY_RLOCK(inp); 19127fb2986fSJonathan T. Looney else 19137fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 19147fb2986fSJonathan T. Looney if (!locked) 191552cd27cbSRobert Watson in_pcbref(inp); 191652cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 19177fb2986fSJonathan T. Looney if (!locked) { 191852cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 191952cd27cbSRobert Watson INP_WLOCK(inp); 192052cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 192152cd27cbSRobert Watson return (NULL); 19227fb2986fSJonathan T. Looney } else { 192352cd27cbSRobert Watson INP_RLOCK(inp); 192452cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 192552cd27cbSRobert Watson return (NULL); 19267fb2986fSJonathan T. Looney } 19277fb2986fSJonathan T. Looney } 19287fb2986fSJonathan T. Looney #ifdef INVARIANTS 19297fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 19307fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 19317fb2986fSJonathan T. Looney else 19327fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 19337fb2986fSJonathan T. Looney #endif 193452cd27cbSRobert Watson return (inp); 193552cd27cbSRobert Watson } 193652cd27cbSRobert Watson #endif /* PCBGROUP */ 193752cd27cbSRobert Watson 193815bd2b43SDavid Greenman /* 1939fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 1940fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 1941fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 194215bd2b43SDavid Greenman */ 1943fa046d87SRobert Watson static struct inpcb * 1944fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 194568e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 1946136d4f1cSRobert Watson struct ifnet *ifp) 194715bd2b43SDavid Greenman { 194815bd2b43SDavid Greenman struct inpcbhead *head; 1949413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 195015bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 195115bd2b43SDavid Greenman 195268e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 195368e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 195468e0d7e0SRobert Watson 1955fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 1956602cc7f1SRobert Watson 195715bd2b43SDavid Greenman /* 195815bd2b43SDavid Greenman * First look for an exact match. 195915bd2b43SDavid Greenman */ 1960413628a7SBjoern A. Zeeb tmpinp = NULL; 1961712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1962712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1963fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1964cfa1ca9dSYoshinobu Inoue #ifdef INET6 1965413628a7SBjoern A. Zeeb /* XXX inp locking */ 1966369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1967cfa1ca9dSYoshinobu Inoue continue; 1968cfa1ca9dSYoshinobu Inoue #endif 19696d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1970ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1971ca98b82cSDavid Greenman inp->inp_fport == fport && 1972413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1973413628a7SBjoern A. Zeeb /* 1974413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1975413628a7SBjoern A. Zeeb * the inp here, without any checks. 1976413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1977413628a7SBjoern A. Zeeb */ 19780304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 1979c3229e05SDavid Greenman return (inp); 1980413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1981413628a7SBjoern A. Zeeb tmpinp = inp; 1982c3229e05SDavid Greenman } 1983413628a7SBjoern A. Zeeb } 1984413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1985413628a7SBjoern A. Zeeb return (tmpinp); 1986e3fd5ffdSRobert Watson 1987e3fd5ffdSRobert Watson /* 1988e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1989e3fd5ffdSRobert Watson */ 199068e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1991413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1992e3fd5ffdSRobert Watson #ifdef INET6 1993cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1994e3fd5ffdSRobert Watson #endif 1995413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1996413628a7SBjoern A. Zeeb int injail; 1997413628a7SBjoern A. Zeeb 1998413628a7SBjoern A. Zeeb /* 1999413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2000413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2001413628a7SBjoern A. Zeeb * 2. jailed, wild. 2002413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2003413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2004413628a7SBjoern A. Zeeb */ 20056d6a026bSDavid Greenman 2006712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2007712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2008fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 2009cfa1ca9dSYoshinobu Inoue #ifdef INET6 2010413628a7SBjoern A. Zeeb /* XXX inp locking */ 2011369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2012cfa1ca9dSYoshinobu Inoue continue; 2013cfa1ca9dSYoshinobu Inoue #endif 2014413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2015413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2016413628a7SBjoern A. Zeeb continue; 2017413628a7SBjoern A. Zeeb 20180304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2019413628a7SBjoern A. Zeeb if (injail) { 2020b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2021b89e82ddSJamie Gritton &laddr) != 0) 2022413628a7SBjoern A. Zeeb continue; 2023413628a7SBjoern A. Zeeb } else { 2024413628a7SBjoern A. Zeeb if (local_exact != NULL) 2025413628a7SBjoern A. Zeeb continue; 2026413628a7SBjoern A. Zeeb } 2027413628a7SBjoern A. Zeeb 2028413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2029413628a7SBjoern A. Zeeb if (injail) 2030c3229e05SDavid Greenman return (inp); 2031413628a7SBjoern A. Zeeb else 2032413628a7SBjoern A. Zeeb local_exact = inp; 2033413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2034e3fd5ffdSRobert Watson #ifdef INET6 2035413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 20365cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2037cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2038cfa1ca9dSYoshinobu Inoue else 203983e521ecSBjoern A. Zeeb #endif 2040413628a7SBjoern A. Zeeb if (injail) 2041413628a7SBjoern A. Zeeb jail_wild = inp; 2042413628a7SBjoern A. Zeeb else 20436d6a026bSDavid Greenman local_wild = inp; 20446d6a026bSDavid Greenman } 2045413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2046413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2047413628a7SBjoern A. Zeeb return (jail_wild); 2048413628a7SBjoern A. Zeeb if (local_exact != NULL) 2049413628a7SBjoern A. Zeeb return (local_exact); 2050413628a7SBjoern A. Zeeb if (local_wild != NULL) 2051c3229e05SDavid Greenman return (local_wild); 2052413628a7SBjoern A. Zeeb #ifdef INET6 2053413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2054413628a7SBjoern A. Zeeb return (local_wild_mapped); 205583e521ecSBjoern A. Zeeb #endif 205668e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2057413628a7SBjoern A. Zeeb 20586d6a026bSDavid Greenman return (NULL); 205915bd2b43SDavid Greenman } 2060fa046d87SRobert Watson 2061fa046d87SRobert Watson /* 2062fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2063fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2064fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2065fa046d87SRobert Watson */ 2066fa046d87SRobert Watson static struct inpcb * 2067fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2068fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2069fa046d87SRobert Watson struct ifnet *ifp) 2070fa046d87SRobert Watson { 2071fa046d87SRobert Watson struct inpcb *inp; 20727fb2986fSJonathan T. Looney bool locked; 2073fa046d87SRobert Watson 2074fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 2075fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2076fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 2077fa046d87SRobert Watson if (inp != NULL) { 20787fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 20797fb2986fSJonathan T. Looney locked = INP_TRY_WLOCK(inp); 20807fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 20817fb2986fSJonathan T. Looney locked = INP_TRY_RLOCK(inp); 20827fb2986fSJonathan T. Looney else 20837fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 20847fb2986fSJonathan T. Looney if (!locked) 2085fa046d87SRobert Watson in_pcbref(inp); 2086fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 20877fb2986fSJonathan T. Looney if (!locked) { 2088fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 2089fa046d87SRobert Watson INP_WLOCK(inp); 2090fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) 2091fa046d87SRobert Watson return (NULL); 20927fb2986fSJonathan T. Looney } else { 2093fa046d87SRobert Watson INP_RLOCK(inp); 2094fa046d87SRobert Watson if (in_pcbrele_rlocked(inp)) 2095fa046d87SRobert Watson return (NULL); 20967fb2986fSJonathan T. Looney } 20977fb2986fSJonathan T. Looney } 20987fb2986fSJonathan T. Looney #ifdef INVARIANTS 20997fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 21007fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 21017fb2986fSJonathan T. Looney else 21027fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 21037fb2986fSJonathan T. Looney #endif 2104fa046d87SRobert Watson } else 2105fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 2106fa046d87SRobert Watson return (inp); 2107fa046d87SRobert Watson } 2108fa046d87SRobert Watson 2109fa046d87SRobert Watson /* 2110d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2111d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 211252cd27cbSRobert Watson * 211352cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 2114fa046d87SRobert Watson */ 2115fa046d87SRobert Watson struct inpcb * 2116fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2117fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2118fa046d87SRobert Watson { 21197527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 212052cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 212152cd27cbSRobert Watson #endif 2122fa046d87SRobert Watson 2123fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2124fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2125fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2126fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 2127fa046d87SRobert Watson 21287527624eSRobert Watson /* 21297527624eSRobert Watson * When not using RSS, use connection groups in preference to the 21307527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 21317527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 21327527624eSRobert Watson * in software. 21337527624eSRobert Watson * 21347527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 21357527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 21367527624eSRobert Watson * in software. 21377527624eSRobert Watson */ 21387527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 213952cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 214052cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 214152cd27cbSRobert Watson fport); 214252cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 214352cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 214452cd27cbSRobert Watson } 214552cd27cbSRobert Watson #endif 2146fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2147fa046d87SRobert Watson lookupflags, ifp)); 2148fa046d87SRobert Watson } 2149d3c1f003SRobert Watson 2150d3c1f003SRobert Watson struct inpcb * 2151d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2152d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2153d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2154d3c1f003SRobert Watson { 215552cd27cbSRobert Watson #ifdef PCBGROUP 215652cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 215752cd27cbSRobert Watson #endif 2158d3c1f003SRobert Watson 2159d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2160d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2161d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2162d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2163d3c1f003SRobert Watson 216452cd27cbSRobert Watson #ifdef PCBGROUP 21657527624eSRobert Watson /* 21667527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 21677527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 21687527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 21697527624eSRobert Watson * using RSS. 21707527624eSRobert Watson * 21717527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 21727527624eSRobert Watson */ 21737527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 21747527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 217552cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 217652cd27cbSRobert Watson m->m_pkthdr.flowid); 217752cd27cbSRobert Watson if (pcbgroup != NULL) 217852cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 217952cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 21807527624eSRobert Watson #ifndef RSS 218152cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 218252cd27cbSRobert Watson fport); 218352cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 218452cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 21857527624eSRobert Watson #endif 218652cd27cbSRobert Watson } 218752cd27cbSRobert Watson #endif 2188d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2189d3c1f003SRobert Watson lookupflags, ifp)); 2190d3c1f003SRobert Watson } 219167107f45SBjoern A. Zeeb #endif /* INET */ 219215bd2b43SDavid Greenman 21937bc4aca7SDavid Greenman /* 2194c3229e05SDavid Greenman * Insert PCB onto various hash lists. 21957bc4aca7SDavid Greenman */ 219652cd27cbSRobert Watson static int 219752cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 219815bd2b43SDavid Greenman { 2199c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2200c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2201c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2202c3229e05SDavid Greenman struct inpcbport *phd; 2203cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 220415bd2b43SDavid Greenman 22058501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2206fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2207fa046d87SRobert Watson 2208111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2209111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2210602cc7f1SRobert Watson 2211cfa1ca9dSYoshinobu Inoue #ifdef INET6 2212cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 22131b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2214cfa1ca9dSYoshinobu Inoue else 221583e521ecSBjoern A. Zeeb #endif 2216cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2217cfa1ca9dSYoshinobu Inoue 2218712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2219712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 222015bd2b43SDavid Greenman 2221712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2222712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2223c3229e05SDavid Greenman 2224c3229e05SDavid Greenman /* 2225c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2226c3229e05SDavid Greenman */ 2227fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 2228c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2229c3229e05SDavid Greenman break; 2230c3229e05SDavid Greenman } 2231c3229e05SDavid Greenman /* 2232c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2233c3229e05SDavid Greenman */ 2234c3229e05SDavid Greenman if (phd == NULL) { 22351ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2236c3229e05SDavid Greenman if (phd == NULL) { 2237c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2238c3229e05SDavid Greenman } 2239c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2240c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 2241c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2242c3229e05SDavid Greenman } 2243c3229e05SDavid Greenman inp->inp_phd = phd; 2244c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2245c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2246111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 224752cd27cbSRobert Watson #ifdef PCBGROUP 224852cd27cbSRobert Watson if (do_pcbgroup_update) 224952cd27cbSRobert Watson in_pcbgroup_update(inp); 225052cd27cbSRobert Watson #endif 2251c3229e05SDavid Greenman return (0); 225215bd2b43SDavid Greenman } 225315bd2b43SDavid Greenman 2254c3229e05SDavid Greenman /* 225552cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 225652cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 225752cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 225852cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 225952cd27cbSRobert Watson * pcbgroup until later. 226052cd27cbSRobert Watson * 226152cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 226252cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 226352cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 226452cd27cbSRobert Watson */ 226552cd27cbSRobert Watson int 226652cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 226752cd27cbSRobert Watson { 226852cd27cbSRobert Watson 226952cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 227052cd27cbSRobert Watson } 227152cd27cbSRobert Watson 227252cd27cbSRobert Watson int 227352cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 227452cd27cbSRobert Watson { 227552cd27cbSRobert Watson 227652cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 227752cd27cbSRobert Watson } 227852cd27cbSRobert Watson 227952cd27cbSRobert Watson /* 2280c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2281c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2282c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2283c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2284c3229e05SDavid Greenman */ 228515bd2b43SDavid Greenman void 2286d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 228715bd2b43SDavid Greenman { 228859daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 228915bd2b43SDavid Greenman struct inpcbhead *head; 2290cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 229115bd2b43SDavid Greenman 22928501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2293fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2294fa046d87SRobert Watson 2295111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2296111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2297602cc7f1SRobert Watson 2298cfa1ca9dSYoshinobu Inoue #ifdef INET6 2299cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 23001b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2301cfa1ca9dSYoshinobu Inoue else 230283e521ecSBjoern A. Zeeb #endif 2303cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2304cfa1ca9dSYoshinobu Inoue 2305712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2306712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 230715bd2b43SDavid Greenman 2308c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 230915bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 231052cd27cbSRobert Watson 231152cd27cbSRobert Watson #ifdef PCBGROUP 231252cd27cbSRobert Watson if (m != NULL) 231352cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 231452cd27cbSRobert Watson else 231552cd27cbSRobert Watson in_pcbgroup_update(inp); 231652cd27cbSRobert Watson #endif 2317c3229e05SDavid Greenman } 2318c3229e05SDavid Greenman 2319d3c1f003SRobert Watson void 2320d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2321d3c1f003SRobert Watson { 2322d3c1f003SRobert Watson 2323d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2324d3c1f003SRobert Watson } 2325d3c1f003SRobert Watson 2326c3229e05SDavid Greenman /* 2327c3229e05SDavid Greenman * Remove PCB from various lists. 2328c3229e05SDavid Greenman */ 23296d888973SRobert Watson static void 2330136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2331c3229e05SDavid Greenman { 233259daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 233359daba27SSam Leffler 2334ff9b006dSJulien Charbon #ifdef INVARIANTS 2335ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 2336ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 2337ff9b006dSJulien Charbon } else { 233859daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 2339ff9b006dSJulien Charbon } 2340ff9b006dSJulien Charbon #endif 2341ff9b006dSJulien Charbon 23428501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2343ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 234459daba27SSam Leffler 234559daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2346111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2347c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2348c3229e05SDavid Greenman 2349fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 2350c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 2351c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 2352fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2353c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 2354c3229e05SDavid Greenman free(phd, M_PCB); 2355c3229e05SDavid Greenman } 2356fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2357111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2358c3229e05SDavid Greenman } 2359c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 236059daba27SSam Leffler pcbinfo->ipi_count--; 236152cd27cbSRobert Watson #ifdef PCBGROUP 236252cd27cbSRobert Watson in_pcbgroup_remove(inp); 236352cd27cbSRobert Watson #endif 236415bd2b43SDavid Greenman } 236575c13541SPoul-Henning Kamp 2366a557af22SRobert Watson /* 236784cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 236884cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 236984cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 237084cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 237184cc0778SGeorge V. Neville-Neil */ 237284cc0778SGeorge V. Neville-Neil void 237384cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 237484cc0778SGeorge V. Neville-Neil { 237584cc0778SGeorge V. Neville-Neil 2376fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 237784cc0778SGeorge V. Neville-Neil return; 237884cc0778SGeorge V. Neville-Neil } 237984cc0778SGeorge V. Neville-Neil 238084cc0778SGeorge V. Neville-Neil /* 2381a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2382a557af22SRobert Watson * label change into the in_pcb for the socket. 2383a557af22SRobert Watson */ 2384a557af22SRobert Watson void 2385136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2386a557af22SRobert Watson { 2387a557af22SRobert Watson #ifdef MAC 2388a557af22SRobert Watson struct inpcb *inp; 2389a557af22SRobert Watson 23904c7c478dSRobert Watson inp = sotoinpcb(so); 23914c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2392602cc7f1SRobert Watson 23938501a69cSRobert Watson INP_WLOCK(inp); 2394310e7cebSRobert Watson SOCK_LOCK(so); 2395a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2396310e7cebSRobert Watson SOCK_UNLOCK(so); 23978501a69cSRobert Watson INP_WUNLOCK(inp); 2398a557af22SRobert Watson #endif 2399a557af22SRobert Watson } 24005f311da2SMike Silbersack 24015f311da2SMike Silbersack /* 2402ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2403ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2404ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2405ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2406ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 24075f311da2SMike Silbersack */ 2408aae49dd3SBjoern A. Zeeb static void 2409136d4f1cSRobert Watson ipport_tick(void *xtp) 24105f311da2SMike Silbersack { 24118b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2412ad3a630fSRobert Watson 24135ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 24148b615593SMarko Zec VNET_FOREACH(vnet_iter) { 24158b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 24168b615593SMarko Zec if (V_ipport_tcpallocs <= 24178b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2418603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2419603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2420ad3a630fSRobert Watson } else 2421603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2422603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 24238b615593SMarko Zec CURVNET_RESTORE(); 24248b615593SMarko Zec } 24255ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 24265f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 24275f311da2SMike Silbersack } 2428497057eeSRobert Watson 2429aae49dd3SBjoern A. Zeeb static void 2430aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2431aae49dd3SBjoern A. Zeeb { 2432aae49dd3SBjoern A. Zeeb 2433aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2434aae49dd3SBjoern A. Zeeb } 2435aae49dd3SBjoern A. Zeeb 2436aae49dd3SBjoern A. Zeeb /* 2437aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2438aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2439aae49dd3SBjoern A. Zeeb */ 2440aae49dd3SBjoern A. Zeeb static void 2441aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2442aae49dd3SBjoern A. Zeeb { 2443aae49dd3SBjoern A. Zeeb 2444aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2445fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2446aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2447aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2448aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2449aae49dd3SBjoern A. Zeeb } 2450aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2451aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2452aae49dd3SBjoern A. Zeeb 24533d585327SKip Macy void 24543d585327SKip Macy inp_wlock(struct inpcb *inp) 24553d585327SKip Macy { 24563d585327SKip Macy 24578501a69cSRobert Watson INP_WLOCK(inp); 24583d585327SKip Macy } 24593d585327SKip Macy 24603d585327SKip Macy void 24613d585327SKip Macy inp_wunlock(struct inpcb *inp) 24623d585327SKip Macy { 24633d585327SKip Macy 24648501a69cSRobert Watson INP_WUNLOCK(inp); 24653d585327SKip Macy } 24663d585327SKip Macy 24673d585327SKip Macy void 24683d585327SKip Macy inp_rlock(struct inpcb *inp) 24693d585327SKip Macy { 24703d585327SKip Macy 2471a69042a5SRobert Watson INP_RLOCK(inp); 24723d585327SKip Macy } 24733d585327SKip Macy 24743d585327SKip Macy void 24753d585327SKip Macy inp_runlock(struct inpcb *inp) 24763d585327SKip Macy { 24773d585327SKip Macy 2478a69042a5SRobert Watson INP_RUNLOCK(inp); 24793d585327SKip Macy } 24803d585327SKip Macy 2481c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 24823d585327SKip Macy void 2483e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 24843d585327SKip Macy { 24853d585327SKip Macy 24868501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 24873d585327SKip Macy } 24883d585327SKip Macy 24893d585327SKip Macy void 2490e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 24913d585327SKip Macy { 24923d585327SKip Macy 24933d585327SKip Macy INP_UNLOCK_ASSERT(inp); 24943d585327SKip Macy } 24953d585327SKip Macy #endif 24963d585327SKip Macy 24979378e437SKip Macy void 24989378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 24999378e437SKip Macy { 25009378e437SKip Macy struct inpcb *inp; 25019378e437SKip Macy 2502ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 250397021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 25049378e437SKip Macy INP_WLOCK(inp); 25059378e437SKip Macy func(inp, arg); 25069378e437SKip Macy INP_WUNLOCK(inp); 25079378e437SKip Macy } 2508ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 25099378e437SKip Macy } 25109378e437SKip Macy 25119378e437SKip Macy struct socket * 25129378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 25139378e437SKip Macy { 25149378e437SKip Macy 25159378e437SKip Macy INP_WLOCK_ASSERT(inp); 25169378e437SKip Macy return (inp->inp_socket); 25179378e437SKip Macy } 25189378e437SKip Macy 25199378e437SKip Macy struct tcpcb * 25209378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 25219378e437SKip Macy { 25229378e437SKip Macy 25239378e437SKip Macy INP_WLOCK_ASSERT(inp); 25249378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 25259378e437SKip Macy } 25269378e437SKip Macy 25279378e437SKip Macy int 25289378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 25299378e437SKip Macy { 25309378e437SKip Macy 25319378e437SKip Macy return (inp->inp_ip_tos); 25329378e437SKip Macy } 25339378e437SKip Macy 25349378e437SKip Macy void 25359378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 25369378e437SKip Macy { 25379378e437SKip Macy 25389378e437SKip Macy inp->inp_ip_tos = val; 25399378e437SKip Macy } 25409378e437SKip Macy 25419378e437SKip Macy void 2542df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 25439d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 25449378e437SKip Macy { 25459378e437SKip Macy 25469d29c635SKip Macy INP_LOCK_ASSERT(inp); 2547df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2548df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 25499378e437SKip Macy *lp = inp->inp_lport; 25509378e437SKip Macy *fp = inp->inp_fport; 25519378e437SKip Macy } 25529378e437SKip Macy 2553dd0e6c38SKip Macy struct inpcb * 2554dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2555dd0e6c38SKip Macy { 2556dd0e6c38SKip Macy 2557dd0e6c38SKip Macy return (sotoinpcb(so)); 2558dd0e6c38SKip Macy } 2559dd0e6c38SKip Macy 2560dd0e6c38SKip Macy struct tcpcb * 2561dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2562dd0e6c38SKip Macy { 2563dd0e6c38SKip Macy 2564dd0e6c38SKip Macy return (sototcpcb(so)); 2565dd0e6c38SKip Macy } 2566dd0e6c38SKip Macy 2567cc65eb4eSGleb Smirnoff /* 2568cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2569cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2570cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2571cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2572cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2573cc65eb4eSGleb Smirnoff * hidden from users. 2574cc65eb4eSGleb Smirnoff */ 2575cc65eb4eSGleb Smirnoff void 2576cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2577cc65eb4eSGleb Smirnoff { 2578cc65eb4eSGleb Smirnoff 2579cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2580cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2581cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2582cc65eb4eSGleb Smirnoff else 2583cc65eb4eSGleb Smirnoff bzero(&xi->xi_socket, sizeof(struct xsocket)); 2584cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2585cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 2586cc65eb4eSGleb Smirnoff xi->inp_ppcb = inp->inp_ppcb; 2587cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2588cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2589cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2590cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2591cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2592cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2593cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2594cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2595cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2596cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2597cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2598cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2599cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2600cc65eb4eSGleb Smirnoff } 2601cc65eb4eSGleb Smirnoff 2602497057eeSRobert Watson #ifdef DDB 2603497057eeSRobert Watson static void 2604497057eeSRobert Watson db_print_indent(int indent) 2605497057eeSRobert Watson { 2606497057eeSRobert Watson int i; 2607497057eeSRobert Watson 2608497057eeSRobert Watson for (i = 0; i < indent; i++) 2609497057eeSRobert Watson db_printf(" "); 2610497057eeSRobert Watson } 2611497057eeSRobert Watson 2612497057eeSRobert Watson static void 2613497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2614497057eeSRobert Watson { 2615497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2616497057eeSRobert Watson 2617497057eeSRobert Watson db_print_indent(indent); 2618497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2619497057eeSRobert Watson 2620497057eeSRobert Watson indent += 2; 2621497057eeSRobert Watson 262203dc38a4SRobert Watson #ifdef INET6 2623dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2624497057eeSRobert Watson /* IPv6. */ 2625497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2626497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 262783e521ecSBjoern A. Zeeb } else 262803dc38a4SRobert Watson #endif 262983e521ecSBjoern A. Zeeb { 2630497057eeSRobert Watson /* IPv4. */ 2631497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2632497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2633497057eeSRobert Watson } 2634497057eeSRobert Watson db_print_indent(indent); 2635497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2636497057eeSRobert Watson ntohs(inc->inc_lport)); 2637497057eeSRobert Watson db_print_indent(indent); 2638497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2639497057eeSRobert Watson ntohs(inc->inc_fport)); 2640497057eeSRobert Watson } 2641497057eeSRobert Watson 2642497057eeSRobert Watson static void 2643497057eeSRobert Watson db_print_inpflags(int inp_flags) 2644497057eeSRobert Watson { 2645497057eeSRobert Watson int comma; 2646497057eeSRobert Watson 2647497057eeSRobert Watson comma = 0; 2648497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2649497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2650497057eeSRobert Watson comma = 1; 2651497057eeSRobert Watson } 2652497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2653497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2654497057eeSRobert Watson comma = 1; 2655497057eeSRobert Watson } 2656497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2657497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2658497057eeSRobert Watson comma = 1; 2659497057eeSRobert Watson } 2660dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2661dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2662dce33a45SErmal Luçi comma = 1; 2663dce33a45SErmal Luçi } 2664497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2665497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2666497057eeSRobert Watson comma = 1; 2667497057eeSRobert Watson } 2668497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2669497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2670497057eeSRobert Watson comma = 1; 2671497057eeSRobert Watson } 2672497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2673497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2674497057eeSRobert Watson comma = 1; 2675497057eeSRobert Watson } 2676497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2677497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2678497057eeSRobert Watson comma = 1; 2679497057eeSRobert Watson } 2680497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2681497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2682497057eeSRobert Watson comma = 1; 2683497057eeSRobert Watson } 2684497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2685497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2686497057eeSRobert Watson comma = 1; 2687497057eeSRobert Watson } 2688497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2689497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2690497057eeSRobert Watson comma = 1; 2691497057eeSRobert Watson } 2692497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2693497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2694497057eeSRobert Watson comma = 1; 2695497057eeSRobert Watson } 26963cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 26973cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 26983cca425bSMichael Tuexen comma = 1; 26993cca425bSMichael Tuexen } 2700497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2701497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2702497057eeSRobert Watson comma = 1; 2703497057eeSRobert Watson } 2704497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2705497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2706497057eeSRobert Watson comma = 1; 2707497057eeSRobert Watson } 2708497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2709497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2710497057eeSRobert Watson comma = 1; 2711497057eeSRobert Watson } 2712497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2713497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2714497057eeSRobert Watson comma = 1; 2715497057eeSRobert Watson } 2716497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2717497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2718497057eeSRobert Watson comma = 1; 2719497057eeSRobert Watson } 2720497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2721497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2722497057eeSRobert Watson comma = 1; 2723497057eeSRobert Watson } 2724497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2725497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2726497057eeSRobert Watson comma = 1; 2727497057eeSRobert Watson } 2728497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2729497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2730497057eeSRobert Watson comma = 1; 2731497057eeSRobert Watson } 2732497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2733497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2734497057eeSRobert Watson comma = 1; 2735497057eeSRobert Watson } 2736ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2737ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2738ad71fe3cSRobert Watson comma = 1; 2739ad71fe3cSRobert Watson } 2740ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2741ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2742ad71fe3cSRobert Watson comma = 1; 2743ad71fe3cSRobert Watson } 2744ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2745ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2746ad71fe3cSRobert Watson comma = 1; 2747ad71fe3cSRobert Watson } 2748ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2749ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2750ad71fe3cSRobert Watson comma = 1; 2751ad71fe3cSRobert Watson } 2752497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2753497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2754497057eeSRobert Watson comma = 1; 2755497057eeSRobert Watson } 2756497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2757497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2758497057eeSRobert Watson comma = 1; 2759497057eeSRobert Watson } 2760497057eeSRobert Watson } 2761497057eeSRobert Watson 2762497057eeSRobert Watson static void 2763497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2764497057eeSRobert Watson { 2765497057eeSRobert Watson int comma; 2766497057eeSRobert Watson 2767497057eeSRobert Watson comma = 0; 2768497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2769497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2770497057eeSRobert Watson comma = 1; 2771497057eeSRobert Watson } 2772497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2773497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2774497057eeSRobert Watson comma = 1; 2775497057eeSRobert Watson } 2776497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2777497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2778497057eeSRobert Watson comma = 1; 2779497057eeSRobert Watson } 2780497057eeSRobert Watson } 2781497057eeSRobert Watson 27826d888973SRobert Watson static void 2783497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2784497057eeSRobert Watson { 2785497057eeSRobert Watson 2786497057eeSRobert Watson db_print_indent(indent); 2787497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2788497057eeSRobert Watson 2789497057eeSRobert Watson indent += 2; 2790497057eeSRobert Watson 2791497057eeSRobert Watson db_print_indent(indent); 2792497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2793497057eeSRobert Watson 2794497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2795497057eeSRobert Watson 2796497057eeSRobert Watson db_print_indent(indent); 2797497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2798497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2799497057eeSRobert Watson 2800497057eeSRobert Watson db_print_indent(indent); 2801497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2802497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2803497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2804497057eeSRobert Watson db_printf(")\n"); 2805497057eeSRobert Watson 2806497057eeSRobert Watson db_print_indent(indent); 2807497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2808497057eeSRobert Watson inp->inp_vflag); 2809497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2810497057eeSRobert Watson db_printf(")\n"); 2811497057eeSRobert Watson 2812497057eeSRobert Watson db_print_indent(indent); 2813497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2814497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2815497057eeSRobert Watson 2816497057eeSRobert Watson db_print_indent(indent); 2817497057eeSRobert Watson #ifdef INET6 2818497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2819497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2820497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2821497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2822497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2823497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2824497057eeSRobert Watson inp->in6p_hops); 2825497057eeSRobert Watson } else 2826497057eeSRobert Watson #endif 2827497057eeSRobert Watson { 2828497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2829497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2830497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2831497057eeSRobert Watson } 2832497057eeSRobert Watson 2833497057eeSRobert Watson db_print_indent(indent); 2834497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2835497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 2836497057eeSRobert Watson } 2837497057eeSRobert Watson 2838497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2839497057eeSRobert Watson { 2840497057eeSRobert Watson struct inpcb *inp; 2841497057eeSRobert Watson 2842497057eeSRobert Watson if (!have_addr) { 2843497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 2844497057eeSRobert Watson return; 2845497057eeSRobert Watson } 2846497057eeSRobert Watson inp = (struct inpcb *)addr; 2847497057eeSRobert Watson 2848497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 2849497057eeSRobert Watson } 285083e521ecSBjoern A. Zeeb #endif /* DDB */ 2851f3e7afe2SHans Petter Selasky 2852f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 2853f3e7afe2SHans Petter Selasky /* 2854f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 2855f3e7afe2SHans Petter Selasky * if any. 2856f3e7afe2SHans Petter Selasky */ 2857f3e7afe2SHans Petter Selasky int 2858f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 2859f3e7afe2SHans Petter Selasky { 2860f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 2861f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2862f3e7afe2SHans Petter Selasky }; 2863f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2864f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2865f3e7afe2SHans Petter Selasky int error; 2866f3e7afe2SHans Petter Selasky 2867f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2868f3e7afe2SHans Petter Selasky if (mst == NULL) 2869f3e7afe2SHans Petter Selasky return (EINVAL); 2870f3e7afe2SHans Petter Selasky 2871f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2872f3e7afe2SHans Petter Selasky if (ifp == NULL) 2873f3e7afe2SHans Petter Selasky return (EINVAL); 2874f3e7afe2SHans Petter Selasky 2875f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 2876f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2877f3e7afe2SHans Petter Selasky } else { 2878f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 2879f3e7afe2SHans Petter Selasky } 2880f3e7afe2SHans Petter Selasky return (error); 2881f3e7afe2SHans Petter Selasky } 2882f3e7afe2SHans Petter Selasky 2883f3e7afe2SHans Petter Selasky /* 2884f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 2885f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 2886f3e7afe2SHans Petter Selasky */ 2887f3e7afe2SHans Petter Selasky int 2888f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 2889f3e7afe2SHans Petter Selasky { 2890f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 2891f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2892f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2893f3e7afe2SHans Petter Selasky int error; 2894f3e7afe2SHans Petter Selasky 2895f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2896f3e7afe2SHans Petter Selasky if (mst == NULL) 2897f3e7afe2SHans Petter Selasky return (EINVAL); 2898f3e7afe2SHans Petter Selasky 2899f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2900f3e7afe2SHans Petter Selasky if (ifp == NULL) 2901f3e7afe2SHans Petter Selasky return (EINVAL); 2902f3e7afe2SHans Petter Selasky 2903f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 2904f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2905f3e7afe2SHans Petter Selasky } else { 2906f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 2907f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 2908f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 2909f3e7afe2SHans Petter Selasky } 2910f3e7afe2SHans Petter Selasky return (error); 2911f3e7afe2SHans Petter Selasky } 2912f3e7afe2SHans Petter Selasky 2913f3e7afe2SHans Petter Selasky /* 291495ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 291595ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 291695ed5015SHans Petter Selasky */ 291795ed5015SHans Petter Selasky int 291895ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 291995ed5015SHans Petter Selasky { 292095ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 292195ed5015SHans Petter Selasky struct m_snd_tag *mst; 292295ed5015SHans Petter Selasky struct ifnet *ifp; 292395ed5015SHans Petter Selasky int error; 292495ed5015SHans Petter Selasky 292595ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 292695ed5015SHans Petter Selasky if (mst == NULL) 292795ed5015SHans Petter Selasky return (EINVAL); 292895ed5015SHans Petter Selasky 292995ed5015SHans Petter Selasky ifp = mst->ifp; 293095ed5015SHans Petter Selasky if (ifp == NULL) 293195ed5015SHans Petter Selasky return (EINVAL); 293295ed5015SHans Petter Selasky 293395ed5015SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) 293495ed5015SHans Petter Selasky return (EOPNOTSUPP); 293595ed5015SHans Petter Selasky 293695ed5015SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 293795ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 293895ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 293995ed5015SHans Petter Selasky return (error); 294095ed5015SHans Petter Selasky } 294195ed5015SHans Petter Selasky 294295ed5015SHans Petter Selasky /* 2943f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 2944f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 2945f3e7afe2SHans Petter Selasky */ 2946f3e7afe2SHans Petter Selasky int 2947f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 2948f3e7afe2SHans Petter Selasky uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate) 2949f3e7afe2SHans Petter Selasky { 2950f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 295195ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 295295ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 2953f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 2954f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 2955f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2956f3e7afe2SHans Petter Selasky }; 2957f3e7afe2SHans Petter Selasky int error; 2958f3e7afe2SHans Petter Selasky 2959f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2960f3e7afe2SHans Petter Selasky 2961f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 2962f3e7afe2SHans Petter Selasky return (EINVAL); 2963f3e7afe2SHans Petter Selasky 2964f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 2965f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2966f3e7afe2SHans Petter Selasky } else { 2967f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 2968f3e7afe2SHans Petter Selasky 2969f3e7afe2SHans Petter Selasky /* 2970f3e7afe2SHans Petter Selasky * At success increment the refcount on 2971f3e7afe2SHans Petter Selasky * the send tag's network interface: 2972f3e7afe2SHans Petter Selasky */ 2973f3e7afe2SHans Petter Selasky if (error == 0) 2974f3e7afe2SHans Petter Selasky if_ref(inp->inp_snd_tag->ifp); 2975f3e7afe2SHans Petter Selasky } 2976f3e7afe2SHans Petter Selasky return (error); 2977f3e7afe2SHans Petter Selasky } 2978f3e7afe2SHans Petter Selasky 2979f3e7afe2SHans Petter Selasky /* 2980f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 2981f3e7afe2SHans Petter Selasky * if any: 2982f3e7afe2SHans Petter Selasky */ 2983f3e7afe2SHans Petter Selasky void 2984f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 2985f3e7afe2SHans Petter Selasky { 2986f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2987f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2988f3e7afe2SHans Petter Selasky 2989f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2990f3e7afe2SHans Petter Selasky 2991f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2992f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 2993f3e7afe2SHans Petter Selasky 2994f3e7afe2SHans Petter Selasky if (mst == NULL) 2995f3e7afe2SHans Petter Selasky return; 2996f3e7afe2SHans Petter Selasky 2997f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2998f3e7afe2SHans Petter Selasky if (ifp == NULL) 2999f3e7afe2SHans Petter Selasky return; 3000f3e7afe2SHans Petter Selasky 3001f3e7afe2SHans Petter Selasky /* 3002f3e7afe2SHans Petter Selasky * If the device was detached while we still had reference(s) 3003f3e7afe2SHans Petter Selasky * on the ifp, we assume if_snd_tag_free() was replaced with 3004f3e7afe2SHans Petter Selasky * stubs. 3005f3e7afe2SHans Petter Selasky */ 3006f3e7afe2SHans Petter Selasky ifp->if_snd_tag_free(mst); 3007f3e7afe2SHans Petter Selasky 3008f3e7afe2SHans Petter Selasky /* release reference count on network interface */ 3009f3e7afe2SHans Petter Selasky if_rele(ifp); 3010f3e7afe2SHans Petter Selasky } 3011f3e7afe2SHans Petter Selasky 3012f3e7afe2SHans Petter Selasky /* 3013f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3014f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3015f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3016f3e7afe2SHans Petter Selasky */ 3017f3e7afe2SHans Petter Selasky void 3018f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3019f3e7afe2SHans Petter Selasky { 3020f3e7afe2SHans Petter Selasky struct socket *socket; 3021f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3022f3e7afe2SHans Petter Selasky bool did_upgrade; 3023f3e7afe2SHans Petter Selasky int error; 3024f3e7afe2SHans Petter Selasky 3025f3e7afe2SHans Petter Selasky if (inp == NULL) 3026f3e7afe2SHans Petter Selasky return; 3027f3e7afe2SHans Petter Selasky 3028f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3029f3e7afe2SHans Petter Selasky if (socket == NULL) 3030f3e7afe2SHans Petter Selasky return; 3031f3e7afe2SHans Petter Selasky 3032f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3033f3e7afe2SHans Petter Selasky /* 3034f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3035f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3036f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3037f3e7afe2SHans Petter Selasky * write lock. 3038f3e7afe2SHans Petter Selasky */ 3039f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3040f3e7afe2SHans Petter Selasky return; 3041f3e7afe2SHans Petter Selasky did_upgrade = 1; 3042f3e7afe2SHans Petter Selasky } else { 3043f3e7afe2SHans Petter Selasky did_upgrade = 0; 3044f3e7afe2SHans Petter Selasky } 3045f3e7afe2SHans Petter Selasky 3046f3e7afe2SHans Petter Selasky /* 3047f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3048f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3049f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3050f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3051f3e7afe2SHans Petter Selasky */ 3052f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3053f3e7afe2SHans Petter Selasky 3054f3e7afe2SHans Petter Selasky /* 3055f3e7afe2SHans Petter Selasky * NOTE: When attaching to a network interface a reference is 3056f3e7afe2SHans Petter Selasky * made to ensure the network interface doesn't go away until 3057f3e7afe2SHans Petter Selasky * all ratelimit connections are gone. The network interface 3058f3e7afe2SHans Petter Selasky * pointers compared below represent valid network interfaces, 3059f3e7afe2SHans Petter Selasky * except when comparing towards NULL. 3060f3e7afe2SHans Petter Selasky */ 3061f3e7afe2SHans Petter Selasky if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 3062f3e7afe2SHans Petter Selasky error = 0; 3063f3e7afe2SHans Petter Selasky } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 3064f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 3065f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3066f3e7afe2SHans Petter Selasky error = 0; 3067f3e7afe2SHans Petter Selasky } else if (inp->inp_snd_tag == NULL) { 3068f3e7afe2SHans Petter Selasky /* 3069f3e7afe2SHans Petter Selasky * In order to utilize packet pacing with RSS, we need 3070f3e7afe2SHans Petter Selasky * to wait until there is a valid RSS hash before we 3071f3e7afe2SHans Petter Selasky * can proceed: 3072f3e7afe2SHans Petter Selasky */ 3073f3e7afe2SHans Petter Selasky if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 3074f3e7afe2SHans Petter Selasky error = EAGAIN; 3075f3e7afe2SHans Petter Selasky } else { 3076f3e7afe2SHans Petter Selasky error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 3077f3e7afe2SHans Petter Selasky mb->m_pkthdr.flowid, max_pacing_rate); 3078f3e7afe2SHans Petter Selasky } 3079f3e7afe2SHans Petter Selasky } else { 3080f3e7afe2SHans Petter Selasky error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 3081f3e7afe2SHans Petter Selasky } 3082f3e7afe2SHans Petter Selasky if (error == 0 || error == EOPNOTSUPP) 3083f3e7afe2SHans Petter Selasky inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 3084f3e7afe2SHans Petter Selasky if (did_upgrade) 3085f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3086f3e7afe2SHans Petter Selasky } 3087f3e7afe2SHans Petter Selasky 3088f3e7afe2SHans Petter Selasky /* 3089f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3090f3e7afe2SHans Petter Selasky */ 3091f3e7afe2SHans Petter Selasky void 3092f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3093f3e7afe2SHans Petter Selasky { 3094f3e7afe2SHans Petter Selasky struct socket *socket; 3095f3e7afe2SHans Petter Selasky bool did_upgrade; 3096f3e7afe2SHans Petter Selasky 3097f3e7afe2SHans Petter Selasky if (inp == NULL) 3098f3e7afe2SHans Petter Selasky return; 3099f3e7afe2SHans Petter Selasky 3100f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3101f3e7afe2SHans Petter Selasky if (socket == NULL) 3102f3e7afe2SHans Petter Selasky return; 3103f3e7afe2SHans Petter Selasky 3104f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3105f3e7afe2SHans Petter Selasky return; 3106f3e7afe2SHans Petter Selasky 3107f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3108f3e7afe2SHans Petter Selasky /* 3109f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3110f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3111f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3112f3e7afe2SHans Petter Selasky * write lock. 3113f3e7afe2SHans Petter Selasky */ 3114f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3115f3e7afe2SHans Petter Selasky return; 3116f3e7afe2SHans Petter Selasky did_upgrade = 1; 3117f3e7afe2SHans Petter Selasky } else { 3118f3e7afe2SHans Petter Selasky did_upgrade = 0; 3119f3e7afe2SHans Petter Selasky } 3120f3e7afe2SHans Petter Selasky 3121f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3122f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3123f3e7afe2SHans Petter Selasky 3124f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3125f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3126f3e7afe2SHans Petter Selasky 3127f3e7afe2SHans Petter Selasky if (did_upgrade) 3128f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3129f3e7afe2SHans Petter Selasky } 3130f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3131