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 589*d7c5a620SMatt 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); 854*d7c5a620SMatt 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); 913*d7c5a620SMatt 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); 979*d7c5a620SMatt 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 1058*d7c5a620SMatt 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 = 1069*d7c5a620SMatt 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); 1076*d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 10772d9cfabaSRobert Watson IFF_BROADCAST) 1078*d7c5a620SMatt 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); 1099*d7c5a620SMatt 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 131779bdc6e5SRobert Watson /* 131879bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 131979bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 132079bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 132179bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 132279bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 132379bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 132479bdc6e5SRobert Watson * the pcbinfo lock is held. 132579bdc6e5SRobert Watson */ 132679bdc6e5SRobert Watson void 132779bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 132879bdc6e5SRobert Watson { 132979bdc6e5SRobert Watson struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 133079bdc6e5SRobert Watson 1331f3e1324bSStephen Hurd #ifdef INET6 1332f3e1324bSStephen Hurd struct ip6_moptions *im6o = NULL; 1333f3e1324bSStephen Hurd #endif 1334f3e1324bSStephen Hurd #ifdef INET 1335f3e1324bSStephen Hurd struct ip_moptions *imo = NULL; 1336f3e1324bSStephen Hurd #endif 133779bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 133879bdc6e5SRobert Watson 133945a48d07SJonathan T. Looney KASSERT((inp->inp_flags2 & INP_FREED) == 0, 134045a48d07SJonathan T. Looney ("%s: called twice for pcb %p", __func__, inp)); 134145a48d07SJonathan T. Looney if (inp->inp_flags2 & INP_FREED) { 134245a48d07SJonathan T. Looney INP_WUNLOCK(inp); 134345a48d07SJonathan T. Looney return; 134445a48d07SJonathan T. Looney } 134545a48d07SJonathan T. Looney 1346ff9b006dSJulien Charbon #ifdef INVARIANTS 1347ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 1348079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(pcbinfo); 1349ff9b006dSJulien Charbon } else { 135079bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 1351ff9b006dSJulien Charbon } 1352ff9b006dSJulien Charbon #endif 135379bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 135479bdc6e5SRobert Watson 1355f3e1324bSStephen Hurd #ifdef INET 1356f3e1324bSStephen Hurd imo = inp->inp_moptions; 1357f3e1324bSStephen Hurd inp->inp_moptions = NULL; 1358f3e1324bSStephen Hurd #endif 135979bdc6e5SRobert Watson /* XXXRW: Do as much as possible here. */ 1360fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 13616aee2fc5SBjoern A. Zeeb if (inp->inp_sp != NULL) 13626974bd9eSBjoern A. Zeeb ipsec_delete_pcbpolicy(inp); 136383e521ecSBjoern A. Zeeb #endif 1364ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 136579bdc6e5SRobert Watson inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1366c3229e05SDavid Greenman in_pcbremlists(inp); 1367ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 13686aee2fc5SBjoern A. Zeeb #ifdef INET6 13696aee2fc5SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 13706aee2fc5SBjoern A. Zeeb ip6_freepcbopts(inp->in6p_outputopts); 1371f3e1324bSStephen Hurd im6o = inp->in6p_moptions; 1372f3e1324bSStephen Hurd inp->in6p_moptions = NULL; 13736aee2fc5SBjoern A. Zeeb } 13746aee2fc5SBjoern A. Zeeb #endif 1375df8bae1dSRodney W. Grimes if (inp->inp_options) 1376df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 1377fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 137884cc0778SGeorge V. Neville-Neil 1379cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 1380df4e91d3SGleb Smirnoff inp->inp_flags2 |= INP_FREED; 138186d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 1382a557af22SRobert Watson #ifdef MAC 138330d239bcSRobert Watson mac_inpcb_destroy(inp); 1384a557af22SRobert Watson #endif 138579bdc6e5SRobert Watson if (!in_pcbrele_wlocked(inp)) 138628696211SRobert Watson INP_WUNLOCK(inp); 1387f3e1324bSStephen Hurd #if defined(INET) && defined(INET6) 1388f3e1324bSStephen Hurd if (imo == NULL && im6o == NULL) 1389f3e1324bSStephen Hurd return; 1390f3e1324bSStephen Hurd #endif 1391f3e1324bSStephen Hurd #ifdef INET6 139228c00100SMatt Macy ip6_freemoptions(im6o, pcbinfo); 1393f3e1324bSStephen Hurd #endif 1394f3e1324bSStephen Hurd #ifdef INET 139528c00100SMatt Macy inp_freemoptions(imo, pcbinfo); 1396f3e1324bSStephen Hurd #endif 139728696211SRobert Watson } 139828696211SRobert Watson 139928696211SRobert Watson /* 1400c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1401c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1402c0a211c5SRobert Watson * 1403c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1404c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1405c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1406c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1407c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1408c0a211c5SRobert Watson * in_pcbdetach(). 1409c0a211c5SRobert Watson * 1410c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1411c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 141210702a28SRobert Watson */ 141310702a28SRobert Watson void 141410702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 141510702a28SRobert Watson { 141610702a28SRobert Watson 14178501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 141810702a28SRobert Watson 1419fa046d87SRobert Watson /* 1420fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1421fa046d87SRobert Watson * the hash lock...? 1422fa046d87SRobert Watson */ 1423ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1424111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 142510702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 142610702a28SRobert Watson 1427fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 142810702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 142910702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 143010702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 143110702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 143210702a28SRobert Watson free(phd, M_PCB); 143310702a28SRobert Watson } 1434fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1435111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 143652cd27cbSRobert Watson #ifdef PCBGROUP 143752cd27cbSRobert Watson in_pcbgroup_remove(inp); 143852cd27cbSRobert Watson #endif 143910702a28SRobert Watson } 144010702a28SRobert Watson } 144110702a28SRobert Watson 144267107f45SBjoern A. Zeeb #ifdef INET 144354d642bbSRobert Watson /* 144454d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 144554d642bbSRobert Watson */ 144626ef6ac4SDon Lewis struct sockaddr * 1447136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 144826ef6ac4SDon Lewis { 144926ef6ac4SDon Lewis struct sockaddr_in *sin; 145026ef6ac4SDon Lewis 14511ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1452a163d034SWarner Losh M_WAITOK | M_ZERO); 145326ef6ac4SDon Lewis sin->sin_family = AF_INET; 145426ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 145526ef6ac4SDon Lewis sin->sin_addr = *addr_p; 145626ef6ac4SDon Lewis sin->sin_port = port; 145726ef6ac4SDon Lewis 145826ef6ac4SDon Lewis return (struct sockaddr *)sin; 145926ef6ac4SDon Lewis } 146026ef6ac4SDon Lewis 1461117bcae7SGarrett Wollman int 146254d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1463df8bae1dSRodney W. Grimes { 1464136d4f1cSRobert Watson struct inpcb *inp; 146526ef6ac4SDon Lewis struct in_addr addr; 146626ef6ac4SDon Lewis in_port_t port; 146742fa505bSDavid Greenman 1468fdc984f7STor Egge inp = sotoinpcb(so); 146954d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 14706466b28aSRobert Watson 1471a69042a5SRobert Watson INP_RLOCK(inp); 147226ef6ac4SDon Lewis port = inp->inp_lport; 147326ef6ac4SDon Lewis addr = inp->inp_laddr; 1474a69042a5SRobert Watson INP_RUNLOCK(inp); 147542fa505bSDavid Greenman 147626ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1477117bcae7SGarrett Wollman return 0; 1478df8bae1dSRodney W. Grimes } 1479df8bae1dSRodney W. Grimes 1480117bcae7SGarrett Wollman int 148154d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1482df8bae1dSRodney W. Grimes { 1483136d4f1cSRobert Watson struct inpcb *inp; 148426ef6ac4SDon Lewis struct in_addr addr; 148526ef6ac4SDon Lewis in_port_t port; 148642fa505bSDavid Greenman 1487fdc984f7STor Egge inp = sotoinpcb(so); 148854d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 14896466b28aSRobert Watson 1490a69042a5SRobert Watson INP_RLOCK(inp); 149126ef6ac4SDon Lewis port = inp->inp_fport; 149226ef6ac4SDon Lewis addr = inp->inp_faddr; 1493a69042a5SRobert Watson INP_RUNLOCK(inp); 149442fa505bSDavid Greenman 149526ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1496117bcae7SGarrett Wollman return 0; 1497df8bae1dSRodney W. Grimes } 1498df8bae1dSRodney W. Grimes 149926f9a767SRodney W. Grimes void 1500136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1501136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1502d1c54148SJesper Skriver { 1503f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1504d1c54148SJesper Skriver 15053dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1506f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 15078501a69cSRobert Watson INP_WLOCK(inp); 1508d1c54148SJesper Skriver #ifdef INET6 1509f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 15108501a69cSRobert Watson INP_WUNLOCK(inp); 1511d1c54148SJesper Skriver continue; 1512f76fcf6dSJeffrey Hsu } 1513d1c54148SJesper Skriver #endif 1514d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1515f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 15168501a69cSRobert Watson INP_WUNLOCK(inp); 1517d1c54148SJesper Skriver continue; 1518d1c54148SJesper Skriver } 15193dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 15208501a69cSRobert Watson INP_WUNLOCK(inp); 1521f76fcf6dSJeffrey Hsu } 15223dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1523d1c54148SJesper Skriver } 1524d1c54148SJesper Skriver 1525e43cc4aeSHajimu UMEMOTO void 1526136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1527e43cc4aeSHajimu UMEMOTO { 1528e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1529e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1530e43cc4aeSHajimu UMEMOTO int i, gap; 1531e43cc4aeSHajimu UMEMOTO 1532ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1533712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 15348501a69cSRobert Watson INP_WLOCK(inp); 1535e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1536e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1537e43cc4aeSHajimu UMEMOTO imo != NULL) { 1538e43cc4aeSHajimu UMEMOTO /* 1539e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1540e43cc4aeSHajimu UMEMOTO * detached. 1541e43cc4aeSHajimu UMEMOTO */ 1542e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1543e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1544e43cc4aeSHajimu UMEMOTO 1545e43cc4aeSHajimu UMEMOTO /* 1546e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1547e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1548e43cc4aeSHajimu UMEMOTO */ 1549e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1550e43cc4aeSHajimu UMEMOTO i++) { 1551e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1552f3e1324bSStephen Hurd IN_MULTI_LOCK_ASSERT(); 1553f3e1324bSStephen Hurd in_leavegroup_locked(imo->imo_membership[i], NULL); 1554e43cc4aeSHajimu UMEMOTO gap++; 1555e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1556e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1557e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1558e43cc4aeSHajimu UMEMOTO } 1559e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1560e43cc4aeSHajimu UMEMOTO } 15618501a69cSRobert Watson INP_WUNLOCK(inp); 1562e43cc4aeSHajimu UMEMOTO } 1563ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1564e43cc4aeSHajimu UMEMOTO } 1565e43cc4aeSHajimu UMEMOTO 1566df8bae1dSRodney W. Grimes /* 1567fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1568fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1569c3229e05SDavid Greenman */ 1570d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1571df8bae1dSRodney W. Grimes struct inpcb * 1572136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 157368e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1574df8bae1dSRodney W. Grimes { 1575136d4f1cSRobert Watson struct inpcb *inp; 1576d5e8a67eSHajimu UMEMOTO #ifdef INET6 1577d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1578d5e8a67eSHajimu UMEMOTO #else 1579d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1580d5e8a67eSHajimu UMEMOTO #endif 1581d5e8a67eSHajimu UMEMOTO int wildcard; 15827bc4aca7SDavid Greenman 158368e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 158468e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 158568e0d7e0SRobert Watson 1586fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 15871b73ca0bSSam Leffler 158868e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1589c3229e05SDavid Greenman struct inpcbhead *head; 1590c3229e05SDavid Greenman /* 1591c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1592c3229e05SDavid Greenman * matches the local address and port we're looking for. 1593c3229e05SDavid Greenman */ 1594712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1595712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1596fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1597cfa1ca9dSYoshinobu Inoue #ifdef INET6 1598413628a7SBjoern A. Zeeb /* XXX inp locking */ 1599369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1600cfa1ca9dSYoshinobu Inoue continue; 1601cfa1ca9dSYoshinobu Inoue #endif 1602c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1603c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1604c3229e05SDavid Greenman inp->inp_lport == lport) { 1605c3229e05SDavid Greenman /* 1606413628a7SBjoern A. Zeeb * Found? 1607c3229e05SDavid Greenman */ 1608413628a7SBjoern A. Zeeb if (cred == NULL || 16090304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 16100304c731SJamie Gritton inp->inp_cred->cr_prison)) 1611c3229e05SDavid Greenman return (inp); 1612df8bae1dSRodney W. Grimes } 1613c3229e05SDavid Greenman } 1614c3229e05SDavid Greenman /* 1615c3229e05SDavid Greenman * Not found. 1616c3229e05SDavid Greenman */ 1617c3229e05SDavid Greenman return (NULL); 1618c3229e05SDavid Greenman } else { 1619c3229e05SDavid Greenman struct inpcbporthead *porthash; 1620c3229e05SDavid Greenman struct inpcbport *phd; 1621c3229e05SDavid Greenman struct inpcb *match = NULL; 1622c3229e05SDavid Greenman /* 1623c3229e05SDavid Greenman * Best fit PCB lookup. 1624c3229e05SDavid Greenman * 1625c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1626c3229e05SDavid Greenman * port hash list. 1627c3229e05SDavid Greenman */ 1628712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1629712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1630fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1631c3229e05SDavid Greenman if (phd->phd_port == lport) 1632c3229e05SDavid Greenman break; 1633c3229e05SDavid Greenman } 1634c3229e05SDavid Greenman if (phd != NULL) { 1635c3229e05SDavid Greenman /* 1636c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1637c3229e05SDavid Greenman * fit. 1638c3229e05SDavid Greenman */ 163937d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1640c3229e05SDavid Greenman wildcard = 0; 1641413628a7SBjoern A. Zeeb if (cred != NULL && 16420304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 16430304c731SJamie Gritton cred->cr_prison)) 1644413628a7SBjoern A. Zeeb continue; 1645cfa1ca9dSYoshinobu Inoue #ifdef INET6 1646413628a7SBjoern A. Zeeb /* XXX inp locking */ 1647369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1648cfa1ca9dSYoshinobu Inoue continue; 1649d5e8a67eSHajimu UMEMOTO /* 1650d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1651d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1652d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1653d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1654d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1655d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1656d5e8a67eSHajimu UMEMOTO * 1657d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1658d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1659d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1660d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1661d5e8a67eSHajimu UMEMOTO */ 1662d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1663d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1664cfa1ca9dSYoshinobu Inoue #endif 1665c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1666c3229e05SDavid Greenman wildcard++; 166715bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 166815bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 166915bd2b43SDavid Greenman wildcard++; 167015bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 167115bd2b43SDavid Greenman continue; 167215bd2b43SDavid Greenman } else { 167315bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 167415bd2b43SDavid Greenman wildcard++; 167515bd2b43SDavid Greenman } 1676df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1677df8bae1dSRodney W. Grimes match = inp; 1678df8bae1dSRodney W. Grimes matchwild = wildcard; 1679413628a7SBjoern A. Zeeb if (matchwild == 0) 1680df8bae1dSRodney W. Grimes break; 1681df8bae1dSRodney W. Grimes } 1682df8bae1dSRodney W. Grimes } 16833dbdc25cSDavid Greenman } 1684df8bae1dSRodney W. Grimes return (match); 1685df8bae1dSRodney W. Grimes } 1686c3229e05SDavid Greenman } 1687d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 168815bd2b43SDavid Greenman 168952cd27cbSRobert Watson #ifdef PCBGROUP 169052cd27cbSRobert Watson /* 169152cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 169252cd27cbSRobert Watson */ 169352cd27cbSRobert Watson static struct inpcb * 169452cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 169552cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 169652cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 169752cd27cbSRobert Watson { 169852cd27cbSRobert Watson struct inpcbhead *head; 169952cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 170052cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 17017fb2986fSJonathan T. Looney bool locked; 170252cd27cbSRobert Watson 170352cd27cbSRobert Watson /* 170452cd27cbSRobert Watson * First look for an exact match. 170552cd27cbSRobert Watson */ 170652cd27cbSRobert Watson tmpinp = NULL; 170752cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 170852cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 170952cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 171052cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgrouphash) { 171152cd27cbSRobert Watson #ifdef INET6 171252cd27cbSRobert Watson /* XXX inp locking */ 171352cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 171452cd27cbSRobert Watson continue; 171552cd27cbSRobert Watson #endif 171652cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 171752cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 171852cd27cbSRobert Watson inp->inp_fport == fport && 171952cd27cbSRobert Watson inp->inp_lport == lport) { 172052cd27cbSRobert Watson /* 172152cd27cbSRobert Watson * XXX We should be able to directly return 172252cd27cbSRobert Watson * the inp here, without any checks. 172352cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 172452cd27cbSRobert Watson */ 172552cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 172652cd27cbSRobert Watson goto found; 172752cd27cbSRobert Watson if (tmpinp == NULL) 172852cd27cbSRobert Watson tmpinp = inp; 172952cd27cbSRobert Watson } 173052cd27cbSRobert Watson } 173152cd27cbSRobert Watson if (tmpinp != NULL) { 173252cd27cbSRobert Watson inp = tmpinp; 173352cd27cbSRobert Watson goto found; 173452cd27cbSRobert Watson } 173552cd27cbSRobert Watson 17360a100a6fSAdrian Chadd #ifdef RSS 17370a100a6fSAdrian Chadd /* 17380a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 17390a100a6fSAdrian Chadd * match for an RSS-local socket. 17400a100a6fSAdrian Chadd */ 17410a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 17420a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 17430a100a6fSAdrian Chadd #ifdef INET6 17440a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 17450a100a6fSAdrian Chadd #endif 17460a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 17470a100a6fSAdrian Chadd struct inpcbhead *head; 17480a100a6fSAdrian Chadd int injail; 17490a100a6fSAdrian Chadd 17500a100a6fSAdrian Chadd /* 17510a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 17520a100a6fSAdrian Chadd * 1. jailed, non-wild. 17530a100a6fSAdrian Chadd * 2. jailed, wild. 17540a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 17550a100a6fSAdrian Chadd * 4. non-jailed, wild. 17560a100a6fSAdrian Chadd */ 17570a100a6fSAdrian Chadd 17580a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 17590a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 17600a100a6fSAdrian Chadd LIST_FOREACH(inp, head, inp_pcbgrouphash) { 17610a100a6fSAdrian Chadd #ifdef INET6 17620a100a6fSAdrian Chadd /* XXX inp locking */ 17630a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 17640a100a6fSAdrian Chadd continue; 17650a100a6fSAdrian Chadd #endif 17660a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 17670a100a6fSAdrian Chadd inp->inp_lport != lport) 17680a100a6fSAdrian Chadd continue; 17690a100a6fSAdrian Chadd 17700a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 17710a100a6fSAdrian Chadd if (injail) { 17720a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 17730a100a6fSAdrian Chadd &laddr) != 0) 17740a100a6fSAdrian Chadd continue; 17750a100a6fSAdrian Chadd } else { 17760a100a6fSAdrian Chadd if (local_exact != NULL) 17770a100a6fSAdrian Chadd continue; 17780a100a6fSAdrian Chadd } 17790a100a6fSAdrian Chadd 17800a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 17810a100a6fSAdrian Chadd if (injail) 17820a100a6fSAdrian Chadd goto found; 17830a100a6fSAdrian Chadd else 17840a100a6fSAdrian Chadd local_exact = inp; 17850a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 17860a100a6fSAdrian Chadd #ifdef INET6 17870a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 17880a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 17890a100a6fSAdrian Chadd local_wild_mapped = inp; 17900a100a6fSAdrian Chadd else 17910a100a6fSAdrian Chadd #endif 17920a100a6fSAdrian Chadd if (injail) 17930a100a6fSAdrian Chadd jail_wild = inp; 17940a100a6fSAdrian Chadd else 17950a100a6fSAdrian Chadd local_wild = inp; 17960a100a6fSAdrian Chadd } 17970a100a6fSAdrian Chadd } /* LIST_FOREACH */ 17980a100a6fSAdrian Chadd 17990a100a6fSAdrian Chadd inp = jail_wild; 18000a100a6fSAdrian Chadd if (inp == NULL) 18010a100a6fSAdrian Chadd inp = local_exact; 18020a100a6fSAdrian Chadd if (inp == NULL) 18030a100a6fSAdrian Chadd inp = local_wild; 18040a100a6fSAdrian Chadd #ifdef INET6 18050a100a6fSAdrian Chadd if (inp == NULL) 18060a100a6fSAdrian Chadd inp = local_wild_mapped; 18070a100a6fSAdrian Chadd #endif 18080a100a6fSAdrian Chadd if (inp != NULL) 18090a100a6fSAdrian Chadd goto found; 18100a100a6fSAdrian Chadd } 18110a100a6fSAdrian Chadd #endif 18120a100a6fSAdrian Chadd 181352cd27cbSRobert Watson /* 181452cd27cbSRobert Watson * Then look for a wildcard match, if requested. 181552cd27cbSRobert Watson */ 181652cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 181752cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 181852cd27cbSRobert Watson #ifdef INET6 181952cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 182052cd27cbSRobert Watson #endif 182152cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 182252cd27cbSRobert Watson struct inpcbhead *head; 182352cd27cbSRobert Watson int injail; 182452cd27cbSRobert Watson 182552cd27cbSRobert Watson /* 182652cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 182752cd27cbSRobert Watson * 1. jailed, non-wild. 182852cd27cbSRobert Watson * 2. jailed, wild. 182952cd27cbSRobert Watson * 3. non-jailed, non-wild. 183052cd27cbSRobert Watson * 4. non-jailed, wild. 183152cd27cbSRobert Watson */ 183252cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 183352cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 183452cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 183552cd27cbSRobert Watson #ifdef INET6 183652cd27cbSRobert Watson /* XXX inp locking */ 183752cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 183852cd27cbSRobert Watson continue; 183952cd27cbSRobert Watson #endif 184052cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 184152cd27cbSRobert Watson inp->inp_lport != lport) 184252cd27cbSRobert Watson continue; 184352cd27cbSRobert Watson 184452cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 184552cd27cbSRobert Watson if (injail) { 184652cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 184752cd27cbSRobert Watson &laddr) != 0) 184852cd27cbSRobert Watson continue; 184952cd27cbSRobert Watson } else { 185052cd27cbSRobert Watson if (local_exact != NULL) 185152cd27cbSRobert Watson continue; 185252cd27cbSRobert Watson } 185352cd27cbSRobert Watson 185452cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 185552cd27cbSRobert Watson if (injail) 185652cd27cbSRobert Watson goto found; 185752cd27cbSRobert Watson else 185852cd27cbSRobert Watson local_exact = inp; 185952cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 186052cd27cbSRobert Watson #ifdef INET6 186152cd27cbSRobert Watson /* XXX inp locking, NULL check */ 186252cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 186352cd27cbSRobert Watson local_wild_mapped = inp; 186452cd27cbSRobert Watson else 186583e521ecSBjoern A. Zeeb #endif 186652cd27cbSRobert Watson if (injail) 186752cd27cbSRobert Watson jail_wild = inp; 186852cd27cbSRobert Watson else 186952cd27cbSRobert Watson local_wild = inp; 187052cd27cbSRobert Watson } 187152cd27cbSRobert Watson } /* LIST_FOREACH */ 187252cd27cbSRobert Watson inp = jail_wild; 187352cd27cbSRobert Watson if (inp == NULL) 187452cd27cbSRobert Watson inp = local_exact; 187552cd27cbSRobert Watson if (inp == NULL) 187652cd27cbSRobert Watson inp = local_wild; 187752cd27cbSRobert Watson #ifdef INET6 187852cd27cbSRobert Watson if (inp == NULL) 187952cd27cbSRobert Watson inp = local_wild_mapped; 188083e521ecSBjoern A. Zeeb #endif 188152cd27cbSRobert Watson if (inp != NULL) 188252cd27cbSRobert Watson goto found; 188352cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 188452cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 188552cd27cbSRobert Watson return (NULL); 188652cd27cbSRobert Watson 188752cd27cbSRobert Watson found: 18887fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 1889b9a9e8e9SNavdeep Parhar locked = INP_TRY_WLOCK(inp); 18907fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 1891b9a9e8e9SNavdeep Parhar locked = INP_TRY_RLOCK(inp); 18927fb2986fSJonathan T. Looney else 18937fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 18947fb2986fSJonathan T. Looney if (!locked) 189552cd27cbSRobert Watson in_pcbref(inp); 189652cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 18977fb2986fSJonathan T. Looney if (!locked) { 189852cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 189952cd27cbSRobert Watson INP_WLOCK(inp); 190052cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 190152cd27cbSRobert Watson return (NULL); 19027fb2986fSJonathan T. Looney } else { 190352cd27cbSRobert Watson INP_RLOCK(inp); 190452cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 190552cd27cbSRobert Watson return (NULL); 19067fb2986fSJonathan T. Looney } 19077fb2986fSJonathan T. Looney } 19087fb2986fSJonathan T. Looney #ifdef INVARIANTS 19097fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 19107fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 19117fb2986fSJonathan T. Looney else 19127fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 19137fb2986fSJonathan T. Looney #endif 191452cd27cbSRobert Watson return (inp); 191552cd27cbSRobert Watson } 191652cd27cbSRobert Watson #endif /* PCBGROUP */ 191752cd27cbSRobert Watson 191815bd2b43SDavid Greenman /* 1919fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 1920fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 1921fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 192215bd2b43SDavid Greenman */ 1923fa046d87SRobert Watson static struct inpcb * 1924fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 192568e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 1926136d4f1cSRobert Watson struct ifnet *ifp) 192715bd2b43SDavid Greenman { 192815bd2b43SDavid Greenman struct inpcbhead *head; 1929413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 193015bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 193115bd2b43SDavid Greenman 193268e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 193368e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 193468e0d7e0SRobert Watson 1935fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 1936602cc7f1SRobert Watson 193715bd2b43SDavid Greenman /* 193815bd2b43SDavid Greenman * First look for an exact match. 193915bd2b43SDavid Greenman */ 1940413628a7SBjoern A. Zeeb tmpinp = NULL; 1941712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1942712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1943fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1944cfa1ca9dSYoshinobu Inoue #ifdef INET6 1945413628a7SBjoern A. Zeeb /* XXX inp locking */ 1946369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1947cfa1ca9dSYoshinobu Inoue continue; 1948cfa1ca9dSYoshinobu Inoue #endif 19496d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1950ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1951ca98b82cSDavid Greenman inp->inp_fport == fport && 1952413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1953413628a7SBjoern A. Zeeb /* 1954413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1955413628a7SBjoern A. Zeeb * the inp here, without any checks. 1956413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1957413628a7SBjoern A. Zeeb */ 19580304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 1959c3229e05SDavid Greenman return (inp); 1960413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1961413628a7SBjoern A. Zeeb tmpinp = inp; 1962c3229e05SDavid Greenman } 1963413628a7SBjoern A. Zeeb } 1964413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1965413628a7SBjoern A. Zeeb return (tmpinp); 1966e3fd5ffdSRobert Watson 1967e3fd5ffdSRobert Watson /* 1968e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1969e3fd5ffdSRobert Watson */ 197068e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1971413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1972e3fd5ffdSRobert Watson #ifdef INET6 1973cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1974e3fd5ffdSRobert Watson #endif 1975413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1976413628a7SBjoern A. Zeeb int injail; 1977413628a7SBjoern A. Zeeb 1978413628a7SBjoern A. Zeeb /* 1979413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 1980413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 1981413628a7SBjoern A. Zeeb * 2. jailed, wild. 1982413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 1983413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 1984413628a7SBjoern A. Zeeb */ 19856d6a026bSDavid Greenman 1986712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1987712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1988fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1989cfa1ca9dSYoshinobu Inoue #ifdef INET6 1990413628a7SBjoern A. Zeeb /* XXX inp locking */ 1991369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1992cfa1ca9dSYoshinobu Inoue continue; 1993cfa1ca9dSYoshinobu Inoue #endif 1994413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 1995413628a7SBjoern A. Zeeb inp->inp_lport != lport) 1996413628a7SBjoern A. Zeeb continue; 1997413628a7SBjoern A. Zeeb 19980304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 1999413628a7SBjoern A. Zeeb if (injail) { 2000b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2001b89e82ddSJamie Gritton &laddr) != 0) 2002413628a7SBjoern A. Zeeb continue; 2003413628a7SBjoern A. Zeeb } else { 2004413628a7SBjoern A. Zeeb if (local_exact != NULL) 2005413628a7SBjoern A. Zeeb continue; 2006413628a7SBjoern A. Zeeb } 2007413628a7SBjoern A. Zeeb 2008413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2009413628a7SBjoern A. Zeeb if (injail) 2010c3229e05SDavid Greenman return (inp); 2011413628a7SBjoern A. Zeeb else 2012413628a7SBjoern A. Zeeb local_exact = inp; 2013413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2014e3fd5ffdSRobert Watson #ifdef INET6 2015413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 20165cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2017cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2018cfa1ca9dSYoshinobu Inoue else 201983e521ecSBjoern A. Zeeb #endif 2020413628a7SBjoern A. Zeeb if (injail) 2021413628a7SBjoern A. Zeeb jail_wild = inp; 2022413628a7SBjoern A. Zeeb else 20236d6a026bSDavid Greenman local_wild = inp; 20246d6a026bSDavid Greenman } 2025413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2026413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2027413628a7SBjoern A. Zeeb return (jail_wild); 2028413628a7SBjoern A. Zeeb if (local_exact != NULL) 2029413628a7SBjoern A. Zeeb return (local_exact); 2030413628a7SBjoern A. Zeeb if (local_wild != NULL) 2031c3229e05SDavid Greenman return (local_wild); 2032413628a7SBjoern A. Zeeb #ifdef INET6 2033413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2034413628a7SBjoern A. Zeeb return (local_wild_mapped); 203583e521ecSBjoern A. Zeeb #endif 203668e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2037413628a7SBjoern A. Zeeb 20386d6a026bSDavid Greenman return (NULL); 203915bd2b43SDavid Greenman } 2040fa046d87SRobert Watson 2041fa046d87SRobert Watson /* 2042fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2043fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2044fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2045fa046d87SRobert Watson */ 2046fa046d87SRobert Watson static struct inpcb * 2047fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2048fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2049fa046d87SRobert Watson struct ifnet *ifp) 2050fa046d87SRobert Watson { 2051fa046d87SRobert Watson struct inpcb *inp; 20527fb2986fSJonathan T. Looney bool locked; 2053fa046d87SRobert Watson 2054fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 2055fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2056fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 2057fa046d87SRobert Watson if (inp != NULL) { 20587fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 20597fb2986fSJonathan T. Looney locked = INP_TRY_WLOCK(inp); 20607fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 20617fb2986fSJonathan T. Looney locked = INP_TRY_RLOCK(inp); 20627fb2986fSJonathan T. Looney else 20637fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 20647fb2986fSJonathan T. Looney if (!locked) 2065fa046d87SRobert Watson in_pcbref(inp); 2066fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 20677fb2986fSJonathan T. Looney if (!locked) { 2068fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 2069fa046d87SRobert Watson INP_WLOCK(inp); 2070fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) 2071fa046d87SRobert Watson return (NULL); 20727fb2986fSJonathan T. Looney } else { 2073fa046d87SRobert Watson INP_RLOCK(inp); 2074fa046d87SRobert Watson if (in_pcbrele_rlocked(inp)) 2075fa046d87SRobert Watson return (NULL); 20767fb2986fSJonathan T. Looney } 20777fb2986fSJonathan T. Looney } 20787fb2986fSJonathan T. Looney #ifdef INVARIANTS 20797fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 20807fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 20817fb2986fSJonathan T. Looney else 20827fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 20837fb2986fSJonathan T. Looney #endif 2084fa046d87SRobert Watson } else 2085fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 2086fa046d87SRobert Watson return (inp); 2087fa046d87SRobert Watson } 2088fa046d87SRobert Watson 2089fa046d87SRobert Watson /* 2090d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2091d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 209252cd27cbSRobert Watson * 209352cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 2094fa046d87SRobert Watson */ 2095fa046d87SRobert Watson struct inpcb * 2096fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2097fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2098fa046d87SRobert Watson { 20997527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 210052cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 210152cd27cbSRobert Watson #endif 2102fa046d87SRobert Watson 2103fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2104fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2105fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2106fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 2107fa046d87SRobert Watson 21087527624eSRobert Watson /* 21097527624eSRobert Watson * When not using RSS, use connection groups in preference to the 21107527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 21117527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 21127527624eSRobert Watson * in software. 21137527624eSRobert Watson * 21147527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 21157527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 21167527624eSRobert Watson * in software. 21177527624eSRobert Watson */ 21187527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 211952cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 212052cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 212152cd27cbSRobert Watson fport); 212252cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 212352cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 212452cd27cbSRobert Watson } 212552cd27cbSRobert Watson #endif 2126fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2127fa046d87SRobert Watson lookupflags, ifp)); 2128fa046d87SRobert Watson } 2129d3c1f003SRobert Watson 2130d3c1f003SRobert Watson struct inpcb * 2131d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2132d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2133d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2134d3c1f003SRobert Watson { 213552cd27cbSRobert Watson #ifdef PCBGROUP 213652cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 213752cd27cbSRobert Watson #endif 2138d3c1f003SRobert Watson 2139d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2140d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2141d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2142d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2143d3c1f003SRobert Watson 214452cd27cbSRobert Watson #ifdef PCBGROUP 21457527624eSRobert Watson /* 21467527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 21477527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 21487527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 21497527624eSRobert Watson * using RSS. 21507527624eSRobert Watson * 21517527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 21527527624eSRobert Watson */ 21537527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 21547527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 215552cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 215652cd27cbSRobert Watson m->m_pkthdr.flowid); 215752cd27cbSRobert Watson if (pcbgroup != NULL) 215852cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 215952cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 21607527624eSRobert Watson #ifndef RSS 216152cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 216252cd27cbSRobert Watson fport); 216352cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 216452cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 21657527624eSRobert Watson #endif 216652cd27cbSRobert Watson } 216752cd27cbSRobert Watson #endif 2168d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2169d3c1f003SRobert Watson lookupflags, ifp)); 2170d3c1f003SRobert Watson } 217167107f45SBjoern A. Zeeb #endif /* INET */ 217215bd2b43SDavid Greenman 21737bc4aca7SDavid Greenman /* 2174c3229e05SDavid Greenman * Insert PCB onto various hash lists. 21757bc4aca7SDavid Greenman */ 217652cd27cbSRobert Watson static int 217752cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 217815bd2b43SDavid Greenman { 2179c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2180c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2181c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2182c3229e05SDavid Greenman struct inpcbport *phd; 2183cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 218415bd2b43SDavid Greenman 21858501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2186fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2187fa046d87SRobert Watson 2188111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2189111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2190602cc7f1SRobert Watson 2191cfa1ca9dSYoshinobu Inoue #ifdef INET6 2192cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 21931b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2194cfa1ca9dSYoshinobu Inoue else 219583e521ecSBjoern A. Zeeb #endif 2196cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2197cfa1ca9dSYoshinobu Inoue 2198712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2199712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 220015bd2b43SDavid Greenman 2201712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2202712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2203c3229e05SDavid Greenman 2204c3229e05SDavid Greenman /* 2205c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2206c3229e05SDavid Greenman */ 2207fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 2208c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2209c3229e05SDavid Greenman break; 2210c3229e05SDavid Greenman } 2211c3229e05SDavid Greenman /* 2212c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2213c3229e05SDavid Greenman */ 2214c3229e05SDavid Greenman if (phd == NULL) { 22151ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2216c3229e05SDavid Greenman if (phd == NULL) { 2217c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2218c3229e05SDavid Greenman } 2219c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2220c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 2221c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2222c3229e05SDavid Greenman } 2223c3229e05SDavid Greenman inp->inp_phd = phd; 2224c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2225c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2226111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 222752cd27cbSRobert Watson #ifdef PCBGROUP 222852cd27cbSRobert Watson if (do_pcbgroup_update) 222952cd27cbSRobert Watson in_pcbgroup_update(inp); 223052cd27cbSRobert Watson #endif 2231c3229e05SDavid Greenman return (0); 223215bd2b43SDavid Greenman } 223315bd2b43SDavid Greenman 2234c3229e05SDavid Greenman /* 223552cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 223652cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 223752cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 223852cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 223952cd27cbSRobert Watson * pcbgroup until later. 224052cd27cbSRobert Watson * 224152cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 224252cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 224352cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 224452cd27cbSRobert Watson */ 224552cd27cbSRobert Watson int 224652cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 224752cd27cbSRobert Watson { 224852cd27cbSRobert Watson 224952cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 225052cd27cbSRobert Watson } 225152cd27cbSRobert Watson 225252cd27cbSRobert Watson int 225352cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 225452cd27cbSRobert Watson { 225552cd27cbSRobert Watson 225652cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 225752cd27cbSRobert Watson } 225852cd27cbSRobert Watson 225952cd27cbSRobert Watson /* 2260c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2261c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2262c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2263c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2264c3229e05SDavid Greenman */ 226515bd2b43SDavid Greenman void 2266d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 226715bd2b43SDavid Greenman { 226859daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 226915bd2b43SDavid Greenman struct inpcbhead *head; 2270cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 227115bd2b43SDavid Greenman 22728501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2273fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2274fa046d87SRobert Watson 2275111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2276111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2277602cc7f1SRobert Watson 2278cfa1ca9dSYoshinobu Inoue #ifdef INET6 2279cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 22801b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2281cfa1ca9dSYoshinobu Inoue else 228283e521ecSBjoern A. Zeeb #endif 2283cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2284cfa1ca9dSYoshinobu Inoue 2285712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2286712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 228715bd2b43SDavid Greenman 2288c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 228915bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 229052cd27cbSRobert Watson 229152cd27cbSRobert Watson #ifdef PCBGROUP 229252cd27cbSRobert Watson if (m != NULL) 229352cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 229452cd27cbSRobert Watson else 229552cd27cbSRobert Watson in_pcbgroup_update(inp); 229652cd27cbSRobert Watson #endif 2297c3229e05SDavid Greenman } 2298c3229e05SDavid Greenman 2299d3c1f003SRobert Watson void 2300d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2301d3c1f003SRobert Watson { 2302d3c1f003SRobert Watson 2303d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2304d3c1f003SRobert Watson } 2305d3c1f003SRobert Watson 2306c3229e05SDavid Greenman /* 2307c3229e05SDavid Greenman * Remove PCB from various lists. 2308c3229e05SDavid Greenman */ 23096d888973SRobert Watson static void 2310136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2311c3229e05SDavid Greenman { 231259daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 231359daba27SSam Leffler 2314ff9b006dSJulien Charbon #ifdef INVARIANTS 2315ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 2316ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 2317ff9b006dSJulien Charbon } else { 231859daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 2319ff9b006dSJulien Charbon } 2320ff9b006dSJulien Charbon #endif 2321ff9b006dSJulien Charbon 23228501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2323ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 232459daba27SSam Leffler 232559daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2326111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2327c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2328c3229e05SDavid Greenman 2329fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 2330c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 2331c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 2332fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2333c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 2334c3229e05SDavid Greenman free(phd, M_PCB); 2335c3229e05SDavid Greenman } 2336fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2337111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2338c3229e05SDavid Greenman } 2339c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 234059daba27SSam Leffler pcbinfo->ipi_count--; 234152cd27cbSRobert Watson #ifdef PCBGROUP 234252cd27cbSRobert Watson in_pcbgroup_remove(inp); 234352cd27cbSRobert Watson #endif 234415bd2b43SDavid Greenman } 234575c13541SPoul-Henning Kamp 2346a557af22SRobert Watson /* 234784cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 234884cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 234984cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 235084cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 235184cc0778SGeorge V. Neville-Neil */ 235284cc0778SGeorge V. Neville-Neil void 235384cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 235484cc0778SGeorge V. Neville-Neil { 235584cc0778SGeorge V. Neville-Neil 2356fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 235784cc0778SGeorge V. Neville-Neil return; 235884cc0778SGeorge V. Neville-Neil } 235984cc0778SGeorge V. Neville-Neil 236084cc0778SGeorge V. Neville-Neil /* 2361a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2362a557af22SRobert Watson * label change into the in_pcb for the socket. 2363a557af22SRobert Watson */ 2364a557af22SRobert Watson void 2365136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2366a557af22SRobert Watson { 2367a557af22SRobert Watson #ifdef MAC 2368a557af22SRobert Watson struct inpcb *inp; 2369a557af22SRobert Watson 23704c7c478dSRobert Watson inp = sotoinpcb(so); 23714c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2372602cc7f1SRobert Watson 23738501a69cSRobert Watson INP_WLOCK(inp); 2374310e7cebSRobert Watson SOCK_LOCK(so); 2375a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2376310e7cebSRobert Watson SOCK_UNLOCK(so); 23778501a69cSRobert Watson INP_WUNLOCK(inp); 2378a557af22SRobert Watson #endif 2379a557af22SRobert Watson } 23805f311da2SMike Silbersack 23815f311da2SMike Silbersack /* 2382ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2383ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2384ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2385ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2386ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 23875f311da2SMike Silbersack */ 2388aae49dd3SBjoern A. Zeeb static void 2389136d4f1cSRobert Watson ipport_tick(void *xtp) 23905f311da2SMike Silbersack { 23918b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2392ad3a630fSRobert Watson 23935ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 23948b615593SMarko Zec VNET_FOREACH(vnet_iter) { 23958b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 23968b615593SMarko Zec if (V_ipport_tcpallocs <= 23978b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2398603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2399603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2400ad3a630fSRobert Watson } else 2401603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2402603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 24038b615593SMarko Zec CURVNET_RESTORE(); 24048b615593SMarko Zec } 24055ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 24065f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 24075f311da2SMike Silbersack } 2408497057eeSRobert Watson 2409aae49dd3SBjoern A. Zeeb static void 2410aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2411aae49dd3SBjoern A. Zeeb { 2412aae49dd3SBjoern A. Zeeb 2413aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2414aae49dd3SBjoern A. Zeeb } 2415aae49dd3SBjoern A. Zeeb 2416aae49dd3SBjoern A. Zeeb /* 2417aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2418aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2419aae49dd3SBjoern A. Zeeb */ 2420aae49dd3SBjoern A. Zeeb static void 2421aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2422aae49dd3SBjoern A. Zeeb { 2423aae49dd3SBjoern A. Zeeb 2424aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2425fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2426aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2427aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2428aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2429aae49dd3SBjoern A. Zeeb } 2430aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2431aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2432aae49dd3SBjoern A. Zeeb 24333d585327SKip Macy void 24343d585327SKip Macy inp_wlock(struct inpcb *inp) 24353d585327SKip Macy { 24363d585327SKip Macy 24378501a69cSRobert Watson INP_WLOCK(inp); 24383d585327SKip Macy } 24393d585327SKip Macy 24403d585327SKip Macy void 24413d585327SKip Macy inp_wunlock(struct inpcb *inp) 24423d585327SKip Macy { 24433d585327SKip Macy 24448501a69cSRobert Watson INP_WUNLOCK(inp); 24453d585327SKip Macy } 24463d585327SKip Macy 24473d585327SKip Macy void 24483d585327SKip Macy inp_rlock(struct inpcb *inp) 24493d585327SKip Macy { 24503d585327SKip Macy 2451a69042a5SRobert Watson INP_RLOCK(inp); 24523d585327SKip Macy } 24533d585327SKip Macy 24543d585327SKip Macy void 24553d585327SKip Macy inp_runlock(struct inpcb *inp) 24563d585327SKip Macy { 24573d585327SKip Macy 2458a69042a5SRobert Watson INP_RUNLOCK(inp); 24593d585327SKip Macy } 24603d585327SKip Macy 2461c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 24623d585327SKip Macy void 2463e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 24643d585327SKip Macy { 24653d585327SKip Macy 24668501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 24673d585327SKip Macy } 24683d585327SKip Macy 24693d585327SKip Macy void 2470e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 24713d585327SKip Macy { 24723d585327SKip Macy 24733d585327SKip Macy INP_UNLOCK_ASSERT(inp); 24743d585327SKip Macy } 24753d585327SKip Macy #endif 24763d585327SKip Macy 24779378e437SKip Macy void 24789378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 24799378e437SKip Macy { 24809378e437SKip Macy struct inpcb *inp; 24819378e437SKip Macy 2482ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 248397021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 24849378e437SKip Macy INP_WLOCK(inp); 24859378e437SKip Macy func(inp, arg); 24869378e437SKip Macy INP_WUNLOCK(inp); 24879378e437SKip Macy } 2488ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 24899378e437SKip Macy } 24909378e437SKip Macy 24919378e437SKip Macy struct socket * 24929378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 24939378e437SKip Macy { 24949378e437SKip Macy 24959378e437SKip Macy INP_WLOCK_ASSERT(inp); 24969378e437SKip Macy return (inp->inp_socket); 24979378e437SKip Macy } 24989378e437SKip Macy 24999378e437SKip Macy struct tcpcb * 25009378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 25019378e437SKip Macy { 25029378e437SKip Macy 25039378e437SKip Macy INP_WLOCK_ASSERT(inp); 25049378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 25059378e437SKip Macy } 25069378e437SKip Macy 25079378e437SKip Macy int 25089378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 25099378e437SKip Macy { 25109378e437SKip Macy 25119378e437SKip Macy return (inp->inp_ip_tos); 25129378e437SKip Macy } 25139378e437SKip Macy 25149378e437SKip Macy void 25159378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 25169378e437SKip Macy { 25179378e437SKip Macy 25189378e437SKip Macy inp->inp_ip_tos = val; 25199378e437SKip Macy } 25209378e437SKip Macy 25219378e437SKip Macy void 2522df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 25239d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 25249378e437SKip Macy { 25259378e437SKip Macy 25269d29c635SKip Macy INP_LOCK_ASSERT(inp); 2527df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2528df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 25299378e437SKip Macy *lp = inp->inp_lport; 25309378e437SKip Macy *fp = inp->inp_fport; 25319378e437SKip Macy } 25329378e437SKip Macy 2533dd0e6c38SKip Macy struct inpcb * 2534dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2535dd0e6c38SKip Macy { 2536dd0e6c38SKip Macy 2537dd0e6c38SKip Macy return (sotoinpcb(so)); 2538dd0e6c38SKip Macy } 2539dd0e6c38SKip Macy 2540dd0e6c38SKip Macy struct tcpcb * 2541dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2542dd0e6c38SKip Macy { 2543dd0e6c38SKip Macy 2544dd0e6c38SKip Macy return (sototcpcb(so)); 2545dd0e6c38SKip Macy } 2546dd0e6c38SKip Macy 2547cc65eb4eSGleb Smirnoff /* 2548cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2549cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2550cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2551cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2552cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2553cc65eb4eSGleb Smirnoff * hidden from users. 2554cc65eb4eSGleb Smirnoff */ 2555cc65eb4eSGleb Smirnoff void 2556cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2557cc65eb4eSGleb Smirnoff { 2558cc65eb4eSGleb Smirnoff 2559cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2560cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2561cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2562cc65eb4eSGleb Smirnoff else 2563cc65eb4eSGleb Smirnoff bzero(&xi->xi_socket, sizeof(struct xsocket)); 2564cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2565cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 2566cc65eb4eSGleb Smirnoff xi->inp_ppcb = inp->inp_ppcb; 2567cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2568cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2569cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2570cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2571cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2572cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2573cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2574cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2575cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2576cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2577cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2578cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2579cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2580cc65eb4eSGleb Smirnoff } 2581cc65eb4eSGleb Smirnoff 2582497057eeSRobert Watson #ifdef DDB 2583497057eeSRobert Watson static void 2584497057eeSRobert Watson db_print_indent(int indent) 2585497057eeSRobert Watson { 2586497057eeSRobert Watson int i; 2587497057eeSRobert Watson 2588497057eeSRobert Watson for (i = 0; i < indent; i++) 2589497057eeSRobert Watson db_printf(" "); 2590497057eeSRobert Watson } 2591497057eeSRobert Watson 2592497057eeSRobert Watson static void 2593497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2594497057eeSRobert Watson { 2595497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2596497057eeSRobert Watson 2597497057eeSRobert Watson db_print_indent(indent); 2598497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2599497057eeSRobert Watson 2600497057eeSRobert Watson indent += 2; 2601497057eeSRobert Watson 260203dc38a4SRobert Watson #ifdef INET6 2603dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2604497057eeSRobert Watson /* IPv6. */ 2605497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2606497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 260783e521ecSBjoern A. Zeeb } else 260803dc38a4SRobert Watson #endif 260983e521ecSBjoern A. Zeeb { 2610497057eeSRobert Watson /* IPv4. */ 2611497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2612497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2613497057eeSRobert Watson } 2614497057eeSRobert Watson db_print_indent(indent); 2615497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2616497057eeSRobert Watson ntohs(inc->inc_lport)); 2617497057eeSRobert Watson db_print_indent(indent); 2618497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2619497057eeSRobert Watson ntohs(inc->inc_fport)); 2620497057eeSRobert Watson } 2621497057eeSRobert Watson 2622497057eeSRobert Watson static void 2623497057eeSRobert Watson db_print_inpflags(int inp_flags) 2624497057eeSRobert Watson { 2625497057eeSRobert Watson int comma; 2626497057eeSRobert Watson 2627497057eeSRobert Watson comma = 0; 2628497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2629497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2630497057eeSRobert Watson comma = 1; 2631497057eeSRobert Watson } 2632497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2633497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2634497057eeSRobert Watson comma = 1; 2635497057eeSRobert Watson } 2636497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2637497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2638497057eeSRobert Watson comma = 1; 2639497057eeSRobert Watson } 2640dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2641dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2642dce33a45SErmal Luçi comma = 1; 2643dce33a45SErmal Luçi } 2644497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2645497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2646497057eeSRobert Watson comma = 1; 2647497057eeSRobert Watson } 2648497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2649497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2650497057eeSRobert Watson comma = 1; 2651497057eeSRobert Watson } 2652497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2653497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2654497057eeSRobert Watson comma = 1; 2655497057eeSRobert Watson } 2656497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2657497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2658497057eeSRobert Watson comma = 1; 2659497057eeSRobert Watson } 2660497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2661497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2662497057eeSRobert Watson comma = 1; 2663497057eeSRobert Watson } 2664497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2665497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2666497057eeSRobert Watson comma = 1; 2667497057eeSRobert Watson } 2668497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2669497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2670497057eeSRobert Watson comma = 1; 2671497057eeSRobert Watson } 2672497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2673497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2674497057eeSRobert Watson comma = 1; 2675497057eeSRobert Watson } 26763cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 26773cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 26783cca425bSMichael Tuexen comma = 1; 26793cca425bSMichael Tuexen } 2680497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2681497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2682497057eeSRobert Watson comma = 1; 2683497057eeSRobert Watson } 2684497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2685497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2686497057eeSRobert Watson comma = 1; 2687497057eeSRobert Watson } 2688497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2689497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2690497057eeSRobert Watson comma = 1; 2691497057eeSRobert Watson } 2692497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2693497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2694497057eeSRobert Watson comma = 1; 2695497057eeSRobert Watson } 2696497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2697497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2698497057eeSRobert Watson comma = 1; 2699497057eeSRobert Watson } 2700497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2701497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2702497057eeSRobert Watson comma = 1; 2703497057eeSRobert Watson } 2704497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2705497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2706497057eeSRobert Watson comma = 1; 2707497057eeSRobert Watson } 2708497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2709497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2710497057eeSRobert Watson comma = 1; 2711497057eeSRobert Watson } 2712497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2713497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2714497057eeSRobert Watson comma = 1; 2715497057eeSRobert Watson } 2716ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2717ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2718ad71fe3cSRobert Watson comma = 1; 2719ad71fe3cSRobert Watson } 2720ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2721ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2722ad71fe3cSRobert Watson comma = 1; 2723ad71fe3cSRobert Watson } 2724ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2725ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2726ad71fe3cSRobert Watson comma = 1; 2727ad71fe3cSRobert Watson } 2728ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2729ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2730ad71fe3cSRobert Watson comma = 1; 2731ad71fe3cSRobert Watson } 2732497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2733497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2734497057eeSRobert Watson comma = 1; 2735497057eeSRobert Watson } 2736497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2737497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2738497057eeSRobert Watson comma = 1; 2739497057eeSRobert Watson } 2740497057eeSRobert Watson } 2741497057eeSRobert Watson 2742497057eeSRobert Watson static void 2743497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2744497057eeSRobert Watson { 2745497057eeSRobert Watson int comma; 2746497057eeSRobert Watson 2747497057eeSRobert Watson comma = 0; 2748497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2749497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2750497057eeSRobert Watson comma = 1; 2751497057eeSRobert Watson } 2752497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2753497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2754497057eeSRobert Watson comma = 1; 2755497057eeSRobert Watson } 2756497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2757497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2758497057eeSRobert Watson comma = 1; 2759497057eeSRobert Watson } 2760497057eeSRobert Watson } 2761497057eeSRobert Watson 27626d888973SRobert Watson static void 2763497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2764497057eeSRobert Watson { 2765497057eeSRobert Watson 2766497057eeSRobert Watson db_print_indent(indent); 2767497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2768497057eeSRobert Watson 2769497057eeSRobert Watson indent += 2; 2770497057eeSRobert Watson 2771497057eeSRobert Watson db_print_indent(indent); 2772497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2773497057eeSRobert Watson 2774497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2775497057eeSRobert Watson 2776497057eeSRobert Watson db_print_indent(indent); 2777497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2778497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2779497057eeSRobert Watson 2780497057eeSRobert Watson db_print_indent(indent); 2781497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2782497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2783497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2784497057eeSRobert Watson db_printf(")\n"); 2785497057eeSRobert Watson 2786497057eeSRobert Watson db_print_indent(indent); 2787497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2788497057eeSRobert Watson inp->inp_vflag); 2789497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2790497057eeSRobert Watson db_printf(")\n"); 2791497057eeSRobert Watson 2792497057eeSRobert Watson db_print_indent(indent); 2793497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2794497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2795497057eeSRobert Watson 2796497057eeSRobert Watson db_print_indent(indent); 2797497057eeSRobert Watson #ifdef INET6 2798497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2799497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2800497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2801497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2802497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2803497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2804497057eeSRobert Watson inp->in6p_hops); 2805497057eeSRobert Watson } else 2806497057eeSRobert Watson #endif 2807497057eeSRobert Watson { 2808497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2809497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2810497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2811497057eeSRobert Watson } 2812497057eeSRobert Watson 2813497057eeSRobert Watson db_print_indent(indent); 2814497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2815497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 2816497057eeSRobert Watson } 2817497057eeSRobert Watson 2818497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2819497057eeSRobert Watson { 2820497057eeSRobert Watson struct inpcb *inp; 2821497057eeSRobert Watson 2822497057eeSRobert Watson if (!have_addr) { 2823497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 2824497057eeSRobert Watson return; 2825497057eeSRobert Watson } 2826497057eeSRobert Watson inp = (struct inpcb *)addr; 2827497057eeSRobert Watson 2828497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 2829497057eeSRobert Watson } 283083e521ecSBjoern A. Zeeb #endif /* DDB */ 2831f3e7afe2SHans Petter Selasky 2832f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 2833f3e7afe2SHans Petter Selasky /* 2834f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 2835f3e7afe2SHans Petter Selasky * if any. 2836f3e7afe2SHans Petter Selasky */ 2837f3e7afe2SHans Petter Selasky int 2838f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 2839f3e7afe2SHans Petter Selasky { 2840f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 2841f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2842f3e7afe2SHans Petter Selasky }; 2843f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2844f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2845f3e7afe2SHans Petter Selasky int error; 2846f3e7afe2SHans Petter Selasky 2847f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2848f3e7afe2SHans Petter Selasky if (mst == NULL) 2849f3e7afe2SHans Petter Selasky return (EINVAL); 2850f3e7afe2SHans Petter Selasky 2851f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2852f3e7afe2SHans Petter Selasky if (ifp == NULL) 2853f3e7afe2SHans Petter Selasky return (EINVAL); 2854f3e7afe2SHans Petter Selasky 2855f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 2856f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2857f3e7afe2SHans Petter Selasky } else { 2858f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 2859f3e7afe2SHans Petter Selasky } 2860f3e7afe2SHans Petter Selasky return (error); 2861f3e7afe2SHans Petter Selasky } 2862f3e7afe2SHans Petter Selasky 2863f3e7afe2SHans Petter Selasky /* 2864f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 2865f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 2866f3e7afe2SHans Petter Selasky */ 2867f3e7afe2SHans Petter Selasky int 2868f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 2869f3e7afe2SHans Petter Selasky { 2870f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 2871f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2872f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2873f3e7afe2SHans Petter Selasky int error; 2874f3e7afe2SHans Petter Selasky 2875f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2876f3e7afe2SHans Petter Selasky if (mst == NULL) 2877f3e7afe2SHans Petter Selasky return (EINVAL); 2878f3e7afe2SHans Petter Selasky 2879f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2880f3e7afe2SHans Petter Selasky if (ifp == NULL) 2881f3e7afe2SHans Petter Selasky return (EINVAL); 2882f3e7afe2SHans Petter Selasky 2883f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 2884f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2885f3e7afe2SHans Petter Selasky } else { 2886f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 2887f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 2888f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 2889f3e7afe2SHans Petter Selasky } 2890f3e7afe2SHans Petter Selasky return (error); 2891f3e7afe2SHans Petter Selasky } 2892f3e7afe2SHans Petter Selasky 2893f3e7afe2SHans Petter Selasky /* 289495ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 289595ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 289695ed5015SHans Petter Selasky */ 289795ed5015SHans Petter Selasky int 289895ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 289995ed5015SHans Petter Selasky { 290095ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 290195ed5015SHans Petter Selasky struct m_snd_tag *mst; 290295ed5015SHans Petter Selasky struct ifnet *ifp; 290395ed5015SHans Petter Selasky int error; 290495ed5015SHans Petter Selasky 290595ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 290695ed5015SHans Petter Selasky if (mst == NULL) 290795ed5015SHans Petter Selasky return (EINVAL); 290895ed5015SHans Petter Selasky 290995ed5015SHans Petter Selasky ifp = mst->ifp; 291095ed5015SHans Petter Selasky if (ifp == NULL) 291195ed5015SHans Petter Selasky return (EINVAL); 291295ed5015SHans Petter Selasky 291395ed5015SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) 291495ed5015SHans Petter Selasky return (EOPNOTSUPP); 291595ed5015SHans Petter Selasky 291695ed5015SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 291795ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 291895ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 291995ed5015SHans Petter Selasky return (error); 292095ed5015SHans Petter Selasky } 292195ed5015SHans Petter Selasky 292295ed5015SHans Petter Selasky /* 2923f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 2924f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 2925f3e7afe2SHans Petter Selasky */ 2926f3e7afe2SHans Petter Selasky int 2927f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 2928f3e7afe2SHans Petter Selasky uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate) 2929f3e7afe2SHans Petter Selasky { 2930f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 293195ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 293295ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 2933f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 2934f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 2935f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2936f3e7afe2SHans Petter Selasky }; 2937f3e7afe2SHans Petter Selasky int error; 2938f3e7afe2SHans Petter Selasky 2939f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2940f3e7afe2SHans Petter Selasky 2941f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 2942f3e7afe2SHans Petter Selasky return (EINVAL); 2943f3e7afe2SHans Petter Selasky 2944f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 2945f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2946f3e7afe2SHans Petter Selasky } else { 2947f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 2948f3e7afe2SHans Petter Selasky 2949f3e7afe2SHans Petter Selasky /* 2950f3e7afe2SHans Petter Selasky * At success increment the refcount on 2951f3e7afe2SHans Petter Selasky * the send tag's network interface: 2952f3e7afe2SHans Petter Selasky */ 2953f3e7afe2SHans Petter Selasky if (error == 0) 2954f3e7afe2SHans Petter Selasky if_ref(inp->inp_snd_tag->ifp); 2955f3e7afe2SHans Petter Selasky } 2956f3e7afe2SHans Petter Selasky return (error); 2957f3e7afe2SHans Petter Selasky } 2958f3e7afe2SHans Petter Selasky 2959f3e7afe2SHans Petter Selasky /* 2960f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 2961f3e7afe2SHans Petter Selasky * if any: 2962f3e7afe2SHans Petter Selasky */ 2963f3e7afe2SHans Petter Selasky void 2964f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 2965f3e7afe2SHans Petter Selasky { 2966f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2967f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2968f3e7afe2SHans Petter Selasky 2969f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2970f3e7afe2SHans Petter Selasky 2971f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2972f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 2973f3e7afe2SHans Petter Selasky 2974f3e7afe2SHans Petter Selasky if (mst == NULL) 2975f3e7afe2SHans Petter Selasky return; 2976f3e7afe2SHans Petter Selasky 2977f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2978f3e7afe2SHans Petter Selasky if (ifp == NULL) 2979f3e7afe2SHans Petter Selasky return; 2980f3e7afe2SHans Petter Selasky 2981f3e7afe2SHans Petter Selasky /* 2982f3e7afe2SHans Petter Selasky * If the device was detached while we still had reference(s) 2983f3e7afe2SHans Petter Selasky * on the ifp, we assume if_snd_tag_free() was replaced with 2984f3e7afe2SHans Petter Selasky * stubs. 2985f3e7afe2SHans Petter Selasky */ 2986f3e7afe2SHans Petter Selasky ifp->if_snd_tag_free(mst); 2987f3e7afe2SHans Petter Selasky 2988f3e7afe2SHans Petter Selasky /* release reference count on network interface */ 2989f3e7afe2SHans Petter Selasky if_rele(ifp); 2990f3e7afe2SHans Petter Selasky } 2991f3e7afe2SHans Petter Selasky 2992f3e7afe2SHans Petter Selasky /* 2993f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 2994f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 2995f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 2996f3e7afe2SHans Petter Selasky */ 2997f3e7afe2SHans Petter Selasky void 2998f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 2999f3e7afe2SHans Petter Selasky { 3000f3e7afe2SHans Petter Selasky struct socket *socket; 3001f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3002f3e7afe2SHans Petter Selasky bool did_upgrade; 3003f3e7afe2SHans Petter Selasky int error; 3004f3e7afe2SHans Petter Selasky 3005f3e7afe2SHans Petter Selasky if (inp == NULL) 3006f3e7afe2SHans Petter Selasky return; 3007f3e7afe2SHans Petter Selasky 3008f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3009f3e7afe2SHans Petter Selasky if (socket == NULL) 3010f3e7afe2SHans Petter Selasky return; 3011f3e7afe2SHans Petter Selasky 3012f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3013f3e7afe2SHans Petter Selasky /* 3014f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3015f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3016f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3017f3e7afe2SHans Petter Selasky * write lock. 3018f3e7afe2SHans Petter Selasky */ 3019f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3020f3e7afe2SHans Petter Selasky return; 3021f3e7afe2SHans Petter Selasky did_upgrade = 1; 3022f3e7afe2SHans Petter Selasky } else { 3023f3e7afe2SHans Petter Selasky did_upgrade = 0; 3024f3e7afe2SHans Petter Selasky } 3025f3e7afe2SHans Petter Selasky 3026f3e7afe2SHans Petter Selasky /* 3027f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3028f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3029f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3030f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3031f3e7afe2SHans Petter Selasky */ 3032f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3033f3e7afe2SHans Petter Selasky 3034f3e7afe2SHans Petter Selasky /* 3035f3e7afe2SHans Petter Selasky * NOTE: When attaching to a network interface a reference is 3036f3e7afe2SHans Petter Selasky * made to ensure the network interface doesn't go away until 3037f3e7afe2SHans Petter Selasky * all ratelimit connections are gone. The network interface 3038f3e7afe2SHans Petter Selasky * pointers compared below represent valid network interfaces, 3039f3e7afe2SHans Petter Selasky * except when comparing towards NULL. 3040f3e7afe2SHans Petter Selasky */ 3041f3e7afe2SHans Petter Selasky if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 3042f3e7afe2SHans Petter Selasky error = 0; 3043f3e7afe2SHans Petter Selasky } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 3044f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 3045f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3046f3e7afe2SHans Petter Selasky error = 0; 3047f3e7afe2SHans Petter Selasky } else if (inp->inp_snd_tag == NULL) { 3048f3e7afe2SHans Petter Selasky /* 3049f3e7afe2SHans Petter Selasky * In order to utilize packet pacing with RSS, we need 3050f3e7afe2SHans Petter Selasky * to wait until there is a valid RSS hash before we 3051f3e7afe2SHans Petter Selasky * can proceed: 3052f3e7afe2SHans Petter Selasky */ 3053f3e7afe2SHans Petter Selasky if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 3054f3e7afe2SHans Petter Selasky error = EAGAIN; 3055f3e7afe2SHans Petter Selasky } else { 3056f3e7afe2SHans Petter Selasky error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 3057f3e7afe2SHans Petter Selasky mb->m_pkthdr.flowid, max_pacing_rate); 3058f3e7afe2SHans Petter Selasky } 3059f3e7afe2SHans Petter Selasky } else { 3060f3e7afe2SHans Petter Selasky error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 3061f3e7afe2SHans Petter Selasky } 3062f3e7afe2SHans Petter Selasky if (error == 0 || error == EOPNOTSUPP) 3063f3e7afe2SHans Petter Selasky inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 3064f3e7afe2SHans Petter Selasky if (did_upgrade) 3065f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3066f3e7afe2SHans Petter Selasky } 3067f3e7afe2SHans Petter Selasky 3068f3e7afe2SHans Petter Selasky /* 3069f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3070f3e7afe2SHans Petter Selasky */ 3071f3e7afe2SHans Petter Selasky void 3072f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3073f3e7afe2SHans Petter Selasky { 3074f3e7afe2SHans Petter Selasky struct socket *socket; 3075f3e7afe2SHans Petter Selasky bool did_upgrade; 3076f3e7afe2SHans Petter Selasky 3077f3e7afe2SHans Petter Selasky if (inp == NULL) 3078f3e7afe2SHans Petter Selasky return; 3079f3e7afe2SHans Petter Selasky 3080f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3081f3e7afe2SHans Petter Selasky if (socket == NULL) 3082f3e7afe2SHans Petter Selasky return; 3083f3e7afe2SHans Petter Selasky 3084f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3085f3e7afe2SHans Petter Selasky return; 3086f3e7afe2SHans Petter Selasky 3087f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3088f3e7afe2SHans Petter Selasky /* 3089f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3090f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3091f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3092f3e7afe2SHans Petter Selasky * write lock. 3093f3e7afe2SHans Petter Selasky */ 3094f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3095f3e7afe2SHans Petter Selasky return; 3096f3e7afe2SHans Petter Selasky did_upgrade = 1; 3097f3e7afe2SHans Petter Selasky } else { 3098f3e7afe2SHans Petter Selasky did_upgrade = 0; 3099f3e7afe2SHans Petter Selasky } 3100f3e7afe2SHans Petter Selasky 3101f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3102f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3103f3e7afe2SHans Petter Selasky 3104f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3105f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3106f3e7afe2SHans Petter Selasky 3107f3e7afe2SHans Petter Selasky if (did_upgrade) 3108f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3109f3e7afe2SHans Petter Selasky } 3110f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3111