1c398230bSWarner Losh /*- 22469dd60SGarrett Wollman * Copyright (c) 1982, 1986, 1991, 1993, 1995 3497057eeSRobert Watson * The Regents of the University of California. 4111d57a6SRobert Watson * Copyright (c) 2007-2009 Robert N. M. Watson 579bdc6e5SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 6497057eeSRobert Watson * All rights reserved. 7df8bae1dSRodney W. Grimes * 879bdc6e5SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 979bdc6e5SRobert Watson * contract to Juniper Networks, Inc. 1079bdc6e5SRobert Watson * 11df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 12df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 13df8bae1dSRodney W. Grimes * are met: 14df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 16df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 18df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 19df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 20df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 21df8bae1dSRodney W. Grimes * without specific prior written permission. 22df8bae1dSRodney W. Grimes * 23df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33df8bae1dSRodney W. Grimes * SUCH DAMAGE. 34df8bae1dSRodney W. Grimes * 352469dd60SGarrett Wollman * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 36df8bae1dSRodney W. Grimes */ 37df8bae1dSRodney W. Grimes 384b421e2dSMike Silbersack #include <sys/cdefs.h> 394b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 404b421e2dSMike Silbersack 41497057eeSRobert Watson #include "opt_ddb.h" 426a800098SYoshinobu Inoue #include "opt_ipsec.h" 43efc76f72SBjoern A. Zeeb #include "opt_inet.h" 44cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 45f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 4652cd27cbSRobert Watson #include "opt_pcbgroup.h" 477527624eSRobert Watson #include "opt_rss.h" 48cfa1ca9dSYoshinobu Inoue 49df8bae1dSRodney W. Grimes #include <sys/param.h> 50df8bae1dSRodney W. Grimes #include <sys/systm.h> 51cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 52df8bae1dSRodney W. Grimes #include <sys/malloc.h> 53df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 54aae49dd3SBjoern A. Zeeb #include <sys/callout.h> 55ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h> 56cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 57df8bae1dSRodney W. Grimes #include <sys/protosw.h> 58cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 59df8bae1dSRodney W. Grimes #include <sys/socket.h> 60df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 61f3e7afe2SHans Petter Selasky #include <sys/sockio.h> 62acd3428bSRobert Watson #include <sys/priv.h> 63df8bae1dSRodney W. Grimes #include <sys/proc.h> 6479bdc6e5SRobert Watson #include <sys/refcount.h> 6575c13541SPoul-Henning Kamp #include <sys/jail.h> 66101f9fc8SPeter Wemm #include <sys/kernel.h> 67101f9fc8SPeter Wemm #include <sys/sysctl.h> 688781d8e9SBruce Evans 69497057eeSRobert Watson #ifdef DDB 70497057eeSRobert Watson #include <ddb/ddb.h> 71497057eeSRobert Watson #endif 72497057eeSRobert Watson 7369c2d429SJeff Roberson #include <vm/uma.h> 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes #include <net/if.h> 7676039bc8SGleb Smirnoff #include <net/if_var.h> 77cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 786d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h> 79df8bae1dSRodney W. Grimes #include <net/route.h> 80b2bdc62aSAdrian Chadd #include <net/rss_config.h> 81530c0060SRobert Watson #include <net/vnet.h> 82df8bae1dSRodney W. Grimes 8367107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 84df8bae1dSRodney W. Grimes #include <netinet/in.h> 85df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 87340c35deSJonathan Lemon #include <netinet/tcp_var.h> 885f311da2SMike Silbersack #include <netinet/udp.h> 895f311da2SMike Silbersack #include <netinet/udp_var.h> 9067107f45SBjoern A. Zeeb #endif 9167107f45SBjoern A. Zeeb #ifdef INET 9267107f45SBjoern A. Zeeb #include <netinet/in_var.h> 9367107f45SBjoern A. Zeeb #endif 94cfa1ca9dSYoshinobu Inoue #ifdef INET6 95cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 96efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 9767107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 9867107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 99cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 100cfa1ca9dSYoshinobu Inoue 101fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 102b9234fafSSam Leffler 103aed55708SRobert Watson #include <security/mac/mac_framework.h> 104aed55708SRobert Watson 105aae49dd3SBjoern A. Zeeb static struct callout ipport_tick_callout; 106aae49dd3SBjoern A. Zeeb 107101f9fc8SPeter Wemm /* 108101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 109101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 110101f9fc8SPeter Wemm */ 111eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 112eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 115eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 116eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 117101f9fc8SPeter Wemm 118b0d22693SCrist J. Clark /* 119b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 120b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 121b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 122b0d22693SCrist J. Clark */ 123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 125b0d22693SCrist J. Clark 1265f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */ 128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */ 129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */ 130eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */ 131eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs); 1323e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount); 133eddfbb76SRobert Watson 1341e77c105SRobert Watson #define V_ipport_tcplastcount VNET(ipport_tcplastcount) 1356ac48b74SMike Silbersack 136d2025bd0SBjoern A. Zeeb static void in_pcbremlists(struct inpcb *inp); 137d2025bd0SBjoern A. Zeeb #ifdef INET 138fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 139fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 140fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 141fa046d87SRobert Watson int lookupflags, struct ifnet *ifp); 14267107f45SBjoern A. Zeeb 143bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 144bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 145bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 146bbd42ad0SPeter Wemm 147bbd42ad0SPeter Wemm static int 14882d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 149bbd42ad0SPeter Wemm { 15030a4ab08SBruce Evans int error; 15130a4ab08SBruce Evans 152f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 15330a4ab08SBruce Evans if (error == 0) { 154603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 155603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 156603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 157603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 158603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 159603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 160bbd42ad0SPeter Wemm } 16130a4ab08SBruce Evans return (error); 162bbd42ad0SPeter Wemm } 163bbd42ad0SPeter Wemm 164bbd42ad0SPeter Wemm #undef RANGECHK 165bbd42ad0SPeter Wemm 1666472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, 1676472ac3dSEd Schouten "IP Ports"); 16833b3ac06SPeter Wemm 1696df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1706df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1716df8a710SGleb Smirnoff &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", ""); 1726df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1736df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1746df8a710SGleb Smirnoff &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", ""); 1756df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1766df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1776df8a710SGleb Smirnoff &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", ""); 1786df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1796df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1806df8a710SGleb Smirnoff &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", ""); 1816df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1826df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1836df8a710SGleb Smirnoff &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", ""); 1846df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 1856df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1866df8a710SGleb Smirnoff &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", ""); 1876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 1886df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 1896df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 191eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 1936df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 194eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 1956df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, 1966df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 197eddfbb76SRobert Watson &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port " 1986ee79c59SMaxim Konovalov "allocations before switching to a sequental one"); 1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, 2006df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 201eddfbb76SRobert Watson &VNET_NAME(ipport_randomtime), 0, 2028b615593SMarko Zec "Minimum time to keep sequental port " 2036ee79c59SMaxim Konovalov "allocation before switching to a random one"); 20483e521ecSBjoern A. Zeeb #endif /* INET */ 2050312fbe9SPoul-Henning Kamp 206c3229e05SDavid Greenman /* 207c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 208c3229e05SDavid Greenman * 209de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 210de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 211de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 212c3229e05SDavid Greenman */ 213c3229e05SDavid Greenman 214c3229e05SDavid Greenman /* 2159bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 2169bcd427bSRobert Watson * arguments in time. 2179bcd427bSRobert Watson */ 2189bcd427bSRobert Watson void 2199bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 2209bcd427bSRobert Watson struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 2219bcd427bSRobert Watson char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini, 22252cd27cbSRobert Watson uint32_t inpcbzone_flags, u_int hashfields) 2239bcd427bSRobert Watson { 2249bcd427bSRobert Watson 2259bcd427bSRobert Watson INP_INFO_LOCK_INIT(pcbinfo, name); 226fa046d87SRobert Watson INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 227ff9b006dSJulien Charbon INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist"); 2289bcd427bSRobert Watson #ifdef VIMAGE 2299bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 2309bcd427bSRobert Watson #endif 2319bcd427bSRobert Watson pcbinfo->ipi_listhead = listhead; 2329bcd427bSRobert Watson LIST_INIT(pcbinfo->ipi_listhead); 233fa046d87SRobert Watson pcbinfo->ipi_count = 0; 2349bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 2359bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 2369bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 2379bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 23852cd27cbSRobert Watson #ifdef PCBGROUP 23952cd27cbSRobert Watson in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 24052cd27cbSRobert Watson #endif 2419bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 2429bcd427bSRobert Watson NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR, 2439bcd427bSRobert Watson inpcbzone_flags); 2449bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 2456acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 2466acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 2479bcd427bSRobert Watson } 2489bcd427bSRobert Watson 2499bcd427bSRobert Watson /* 2509bcd427bSRobert Watson * Destroy an inpcbinfo. 2519bcd427bSRobert Watson */ 2529bcd427bSRobert Watson void 2539bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 2549bcd427bSRobert Watson { 2559bcd427bSRobert Watson 256fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 257fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 258fa046d87SRobert Watson 2599bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 2609bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 2619bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 26252cd27cbSRobert Watson #ifdef PCBGROUP 26352cd27cbSRobert Watson in_pcbgroup_destroy(pcbinfo); 26452cd27cbSRobert Watson #endif 2659bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 266ff9b006dSJulien Charbon INP_LIST_LOCK_DESTROY(pcbinfo); 267fa046d87SRobert Watson INP_HASH_LOCK_DESTROY(pcbinfo); 2689bcd427bSRobert Watson INP_INFO_LOCK_DESTROY(pcbinfo); 2699bcd427bSRobert Watson } 2709bcd427bSRobert Watson 2719bcd427bSRobert Watson /* 272c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 273d915b280SStephan Uphoff * On success return with the PCB locked. 274c3229e05SDavid Greenman */ 275df8bae1dSRodney W. Grimes int 276d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 277df8bae1dSRodney W. Grimes { 278136d4f1cSRobert Watson struct inpcb *inp; 27913cf67f3SHajimu UMEMOTO int error; 280a557af22SRobert Watson 281ff9b006dSJulien Charbon #ifdef INVARIANTS 282ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 283ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 284ff9b006dSJulien Charbon } else { 28559daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 286ff9b006dSJulien Charbon } 287ff9b006dSJulien Charbon #endif 288ff9b006dSJulien Charbon 289a557af22SRobert Watson error = 0; 290d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 291df8bae1dSRodney W. Grimes if (inp == NULL) 292df8bae1dSRodney W. Grimes return (ENOBUFS); 293d915b280SStephan Uphoff bzero(inp, inp_zero_size); 29415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 295df8bae1dSRodney W. Grimes inp->inp_socket = so; 29686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 2978b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 298a557af22SRobert Watson #ifdef MAC 29930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 300a557af22SRobert Watson if (error != 0) 301a557af22SRobert Watson goto out; 30230d239bcSRobert Watson mac_inpcb_create(so, inp); 303a557af22SRobert Watson #endif 304fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 305fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 3060bffde27SRobert Watson if (error != 0) { 3070bffde27SRobert Watson #ifdef MAC 3080bffde27SRobert Watson mac_inpcb_destroy(inp); 3090bffde27SRobert Watson #endif 310a557af22SRobert Watson goto out; 3110bffde27SRobert Watson } 312b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 313e3fd5ffdSRobert Watson #ifdef INET6 314340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 315340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 316603724d3SBjoern A. Zeeb if (V_ip6_v6only) 31733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 318340c35deSJonathan Lemon } 31975daea93SPaul Saab #endif 320ff9b006dSJulien Charbon INP_WLOCK(inp); 321ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 322712fc218SRobert Watson LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 3233d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 324df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 32533841545SHajimu UMEMOTO #ifdef INET6 326603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 32733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 32833841545SHajimu UMEMOTO #endif 329d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 33079bdc6e5SRobert Watson refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 331ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 332b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC) 333a557af22SRobert Watson out: 33486d02c5cSBjoern A. Zeeb if (error != 0) { 33586d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 336a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 33786d02c5cSBjoern A. Zeeb } 338a557af22SRobert Watson #endif 339a557af22SRobert Watson return (error); 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes 34267107f45SBjoern A. Zeeb #ifdef INET 343df8bae1dSRodney W. Grimes int 344136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 345df8bae1dSRodney W. Grimes { 3464b932371SIan Dowse int anonport, error; 3474b932371SIan Dowse 3488501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 349fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 35059daba27SSam Leffler 3514b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 3524b932371SIan Dowse return (EINVAL); 3535cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 3544b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 355b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 3564b932371SIan Dowse if (error) 3574b932371SIan Dowse return (error); 3584b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 3594b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 3604b932371SIan Dowse inp->inp_lport = 0; 3614b932371SIan Dowse return (EAGAIN); 3624b932371SIan Dowse } 3634b932371SIan Dowse if (anonport) 3644b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 3654b932371SIan Dowse return (0); 3664b932371SIan Dowse } 36767107f45SBjoern A. Zeeb #endif 3684b932371SIan Dowse 36939c8c62eSHiren Panchasara /* 37039c8c62eSHiren Panchasara * Select a local port (number) to use. 37139c8c62eSHiren Panchasara */ 372efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 373efc76f72SBjoern A. Zeeb int 374*4616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 375*4616026fSErmal Luçi struct ucred *cred, int lookupflags) 376efc76f72SBjoern A. Zeeb { 377efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 378efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 379efc76f72SBjoern A. Zeeb unsigned short *lastport; 380efc76f72SBjoern A. Zeeb int count, dorandom, error; 381efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 382efc76f72SBjoern A. Zeeb #ifdef INET 383efc76f72SBjoern A. Zeeb struct in_addr laddr; 384efc76f72SBjoern A. Zeeb #endif 385efc76f72SBjoern A. Zeeb 386efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 387efc76f72SBjoern A. Zeeb 388efc76f72SBjoern A. Zeeb /* 389efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 390efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 391efc76f72SBjoern A. Zeeb */ 392efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 393fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 394efc76f72SBjoern A. Zeeb 395efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 396efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 397efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 398efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 399efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 400efc76f72SBjoern A. Zeeb error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0); 401efc76f72SBjoern A. Zeeb if (error) 402efc76f72SBjoern A. Zeeb return (error); 403efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 404efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 405efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 406efc76f72SBjoern A. Zeeb } else { 407efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 408efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 409efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 410efc76f72SBjoern A. Zeeb } 411efc76f72SBjoern A. Zeeb /* 412e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 413efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 414efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 415efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 416efc76f72SBjoern A. Zeeb */ 417efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 418e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 419e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 420efc76f72SBjoern A. Zeeb dorandom = 1; 421efc76f72SBjoern A. Zeeb else 422efc76f72SBjoern A. Zeeb dorandom = 0; 423efc76f72SBjoern A. Zeeb /* 424efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 425efc76f72SBjoern A. Zeeb * we have the only port available. 426efc76f72SBjoern A. Zeeb */ 427efc76f72SBjoern A. Zeeb if (first == last) 428efc76f72SBjoern A. Zeeb dorandom = 0; 429e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 430e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 431efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 432efc76f72SBjoern A. Zeeb /* 433efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 434efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 435efc76f72SBjoern A. Zeeb * code path implementing all logic. 436efc76f72SBjoern A. Zeeb */ 437efc76f72SBjoern A. Zeeb if (first > last) { 438efc76f72SBjoern A. Zeeb aux = first; 439efc76f72SBjoern A. Zeeb first = last; 440efc76f72SBjoern A. Zeeb last = aux; 441efc76f72SBjoern A. Zeeb } 442efc76f72SBjoern A. Zeeb 443efc76f72SBjoern A. Zeeb #ifdef INET 444efc76f72SBjoern A. Zeeb /* Make the compiler happy. */ 445efc76f72SBjoern A. Zeeb laddr.s_addr = 0; 4464d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 447efc76f72SBjoern A. Zeeb KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 448efc76f72SBjoern A. Zeeb __func__, inp)); 449efc76f72SBjoern A. Zeeb laddr = *laddrp; 450efc76f72SBjoern A. Zeeb } 451efc76f72SBjoern A. Zeeb #endif 45267107f45SBjoern A. Zeeb tmpinp = NULL; /* Make compiler happy. */ 453efc76f72SBjoern A. Zeeb lport = *lportp; 454efc76f72SBjoern A. Zeeb 455efc76f72SBjoern A. Zeeb if (dorandom) 456efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 457efc76f72SBjoern A. Zeeb 458efc76f72SBjoern A. Zeeb count = last - first; 459efc76f72SBjoern A. Zeeb 460efc76f72SBjoern A. Zeeb do { 461efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 462efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 463efc76f72SBjoern A. Zeeb ++*lastport; 464efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 465efc76f72SBjoern A. Zeeb *lastport = first; 466efc76f72SBjoern A. Zeeb lport = htons(*lastport); 467efc76f72SBjoern A. Zeeb 468efc76f72SBjoern A. Zeeb #ifdef INET6 469*4616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 470efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 47168e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 472efc76f72SBjoern A. Zeeb #endif 473efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 474efc76f72SBjoern A. Zeeb else 475efc76f72SBjoern A. Zeeb #endif 476efc76f72SBjoern A. Zeeb #ifdef INET 477efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 47868e0d7e0SRobert Watson lport, lookupflags, cred); 479efc76f72SBjoern A. Zeeb #endif 480efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 481efc76f72SBjoern A. Zeeb 482efc76f72SBjoern A. Zeeb #ifdef INET 4834d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 484efc76f72SBjoern A. Zeeb laddrp->s_addr = laddr.s_addr; 485efc76f72SBjoern A. Zeeb #endif 486efc76f72SBjoern A. Zeeb *lportp = lport; 487efc76f72SBjoern A. Zeeb 488efc76f72SBjoern A. Zeeb return (0); 489efc76f72SBjoern A. Zeeb } 490efdf104bSMikolaj Golub 491efdf104bSMikolaj Golub /* 492efdf104bSMikolaj Golub * Return cached socket options. 493efdf104bSMikolaj Golub */ 494efdf104bSMikolaj Golub short 495efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 496efdf104bSMikolaj Golub { 497efdf104bSMikolaj Golub short so_options; 498efdf104bSMikolaj Golub 499efdf104bSMikolaj Golub so_options = 0; 500efdf104bSMikolaj Golub 501efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 502efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 503efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 504efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 505efdf104bSMikolaj Golub return (so_options); 506efdf104bSMikolaj Golub } 507efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 508efc76f72SBjoern A. Zeeb 5094b932371SIan Dowse /* 5100a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 5110a100a6fSAdrian Chadd * 5120a100a6fSAdrian Chadd * ni points to the new inp. 5130a100a6fSAdrian Chadd * oi points to the exisitng inp. 5140a100a6fSAdrian Chadd * 5150a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 5160a100a6fSAdrian Chadd * whether the credentials match. 5170a100a6fSAdrian Chadd */ 518d5bb8bd3SAdrian Chadd int 5190a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 5200a100a6fSAdrian Chadd { 5210a100a6fSAdrian Chadd /* Check permissions match */ 5220a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5230a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 5240a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 5250a100a6fSAdrian Chadd return (0); 5260a100a6fSAdrian Chadd 5270a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 5280a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5290a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 5300a100a6fSAdrian Chadd return (0); 5310a100a6fSAdrian Chadd 5320a100a6fSAdrian Chadd /* 5330a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 5340a100a6fSAdrian Chadd * it is and it matches the checks. 5350a100a6fSAdrian Chadd */ 5360a100a6fSAdrian Chadd return (1); 5370a100a6fSAdrian Chadd } 5380a100a6fSAdrian Chadd 539d5bb8bd3SAdrian Chadd #ifdef INET 5400a100a6fSAdrian Chadd /* 5414b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 5424b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 5434b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 5444b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 5454b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 5464b932371SIan Dowse * 5474b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 5484b932371SIan Dowse */ 5494b932371SIan Dowse int 550136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 551136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 5524b932371SIan Dowse { 5534b932371SIan Dowse struct socket *so = inp->inp_socket; 55415bd2b43SDavid Greenman struct sockaddr_in *sin; 555c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 5564b932371SIan Dowse struct in_addr laddr; 557df8bae1dSRodney W. Grimes u_short lport = 0; 55868e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 559413628a7SBjoern A. Zeeb int error; 560df8bae1dSRodney W. Grimes 5618501a69cSRobert Watson /* 562fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 5638501a69cSRobert Watson */ 56459daba27SSam Leffler INP_LOCK_ASSERT(inp); 565fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 56659daba27SSam Leffler 567603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 568df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 5694b932371SIan Dowse laddr.s_addr = *laddrp; 5704b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 571df8bae1dSRodney W. Grimes return (EINVAL); 572c3229e05SDavid Greenman if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 57368e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 574*4616026fSErmal Luçi if (nam == NULL) { 5757c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 5767c2f3cb9SJamie Gritton return (error); 5777c2f3cb9SJamie Gritton } else { 57857bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 57957bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 580df8bae1dSRodney W. Grimes return (EINVAL); 581df8bae1dSRodney W. Grimes #ifdef notdef 582df8bae1dSRodney W. Grimes /* 583df8bae1dSRodney W. Grimes * We should check the family, but old programs 584df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 585df8bae1dSRodney W. Grimes */ 586df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 587df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 588df8bae1dSRodney W. Grimes #endif 589b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 590b89e82ddSJamie Gritton if (error) 591b89e82ddSJamie Gritton return (error); 5924b932371SIan Dowse if (sin->sin_port != *lportp) { 5934b932371SIan Dowse /* Don't allow the port to change. */ 5944b932371SIan Dowse if (*lportp != 0) 5954b932371SIan Dowse return (EINVAL); 596df8bae1dSRodney W. Grimes lport = sin->sin_port; 5974b932371SIan Dowse } 5984b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 599df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 600df8bae1dSRodney W. Grimes /* 601df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 602df8bae1dSRodney W. Grimes * allow complete duplication of binding if 603df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 604df8bae1dSRodney W. Grimes * and a multicast address is bound on both 605df8bae1dSRodney W. Grimes * new and duplicated sockets. 606df8bae1dSRodney W. Grimes */ 607f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 608df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 609df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 610df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 61183103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 6124209e01aSAdrian Chadd /* 6134209e01aSAdrian Chadd * Is the address a local IP address? 614f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 6158696873dSAdrian Chadd * to any endpoint address, local or not. 6164209e01aSAdrian Chadd */ 617f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 6188896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 619df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 620df8bae1dSRodney W. Grimes } 6214b932371SIan Dowse laddr = sin->sin_addr; 622df8bae1dSRodney W. Grimes if (lport) { 623df8bae1dSRodney W. Grimes struct inpcb *t; 624ae0e7143SRobert Watson struct tcptw *tw; 625ae0e7143SRobert Watson 626df8bae1dSRodney W. Grimes /* GROSS */ 627603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 628603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 629acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 63032f9753cSRobert Watson 0)) 6312469dd60SGarrett Wollman return (EACCES); 632835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 63386d02c5cSBjoern A. Zeeb priv_check_cred(inp->inp_cred, 63432f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 635078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 636413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 637340c35deSJonathan Lemon /* 638340c35deSJonathan Lemon * XXX 639340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 640340c35deSJonathan Lemon */ 6414cc20ab1SSeigo Tanimura if (t && 6420a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 643ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 6444658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 6454658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 6464cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 64752b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 648fc06cd42SMikolaj Golub (t->inp_flags2 & INP_REUSEPORT) == 0) && 64986d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 65086d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 6514049a042SGuido van Rooij return (EADDRINUSE); 6520a100a6fSAdrian Chadd 6530a100a6fSAdrian Chadd /* 6540a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 6550a100a6fSAdrian Chadd * the credentials need to match and the 6560a100a6fSAdrian Chadd * original socket also has to have been bound 6570a100a6fSAdrian Chadd * with BINDMULTI. 6580a100a6fSAdrian Chadd */ 6590a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 6600a100a6fSAdrian Chadd return (EADDRINUSE); 6614049a042SGuido van Rooij } 662c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 66368e0d7e0SRobert Watson lport, lookupflags, cred); 664ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 665ae0e7143SRobert Watson /* 666ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 667ae0e7143SRobert Watson * state recycled, we treat the address as 668ae0e7143SRobert Watson * being in use (for now). This is better 669ae0e7143SRobert Watson * than a panic, but not desirable. 670ae0e7143SRobert Watson */ 671ec95b709SMikolaj Golub tw = intotw(t); 672ae0e7143SRobert Watson if (tw == NULL || 673ae0e7143SRobert Watson (reuseport & tw->tw_so_options) == 0) 674340c35deSJonathan Lemon return (EADDRINUSE); 6750a100a6fSAdrian Chadd } else if (t && 6760a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 6770a100a6fSAdrian Chadd (reuseport & inp_so_options(t)) == 0) { 678e3fd5ffdSRobert Watson #ifdef INET6 67933841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 680cfa1ca9dSYoshinobu Inoue INADDR_ANY || 681cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 682cfa1ca9dSYoshinobu Inoue INADDR_ANY || 683fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 684fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 685e3fd5ffdSRobert Watson #endif 686df8bae1dSRodney W. Grimes return (EADDRINUSE); 6870a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 6880a100a6fSAdrian Chadd return (EADDRINUSE); 689df8bae1dSRodney W. Grimes } 690cfa1ca9dSYoshinobu Inoue } 691df8bae1dSRodney W. Grimes } 6924b932371SIan Dowse if (*lportp != 0) 6934b932371SIan Dowse lport = *lportp; 69433b3ac06SPeter Wemm if (lport == 0) { 695*4616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 696efc76f72SBjoern A. Zeeb if (error != 0) 697efc76f72SBjoern A. Zeeb return (error); 69833b3ac06SPeter Wemm 69933b3ac06SPeter Wemm } 7004b932371SIan Dowse *laddrp = laddr.s_addr; 7014b932371SIan Dowse *lportp = lport; 702df8bae1dSRodney W. Grimes return (0); 703df8bae1dSRodney W. Grimes } 704df8bae1dSRodney W. Grimes 705999f1343SGarrett Wollman /* 7065200e00eSIan Dowse * Connect from a socket to a specified address. 7075200e00eSIan Dowse * Both address and port must be specified in argument sin. 7085200e00eSIan Dowse * If don't have a local address for this socket yet, 7095200e00eSIan Dowse * then pick one. 710999f1343SGarrett Wollman */ 711999f1343SGarrett Wollman int 712d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 713d3c1f003SRobert Watson struct ucred *cred, struct mbuf *m) 714999f1343SGarrett Wollman { 7155200e00eSIan Dowse u_short lport, fport; 7165200e00eSIan Dowse in_addr_t laddr, faddr; 7175200e00eSIan Dowse int anonport, error; 718df8bae1dSRodney W. Grimes 7198501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 720fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 72127f74fd0SRobert Watson 7225200e00eSIan Dowse lport = inp->inp_lport; 7235200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 7245200e00eSIan Dowse anonport = (lport == 0); 7255200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 726b0330ed9SPawel Jakub Dawidek NULL, cred); 7275200e00eSIan Dowse if (error) 7285200e00eSIan Dowse return (error); 7295200e00eSIan Dowse 7305200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 7315200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 7325200e00eSIan Dowse inp->inp_lport = lport; 7335200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7345200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 7355200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 7365200e00eSIan Dowse inp->inp_lport = 0; 7375200e00eSIan Dowse return (EAGAIN); 7385200e00eSIan Dowse } 7395200e00eSIan Dowse } 7405200e00eSIan Dowse 7415200e00eSIan Dowse /* Commit the remaining changes. */ 7425200e00eSIan Dowse inp->inp_lport = lport; 7435200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7445200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 7455200e00eSIan Dowse inp->inp_fport = fport; 746d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 7472cb64cb2SGeorge V. Neville-Neil 7485200e00eSIan Dowse if (anonport) 7495200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 7505200e00eSIan Dowse return (0); 7515200e00eSIan Dowse } 7525200e00eSIan Dowse 753d3c1f003SRobert Watson int 754d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 755d3c1f003SRobert Watson { 756d3c1f003SRobert Watson 757d3c1f003SRobert Watson return (in_pcbconnect_mbuf(inp, nam, cred, NULL)); 758d3c1f003SRobert Watson } 759d3c1f003SRobert Watson 7605200e00eSIan Dowse /* 7610895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 7620895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 7630895aec3SBjoern A. Zeeb */ 764ae190832SSteven Hartland int 7650895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 7660895aec3SBjoern A. Zeeb struct ucred *cred) 7670895aec3SBjoern A. Zeeb { 7680895aec3SBjoern A. Zeeb struct ifaddr *ifa; 7690895aec3SBjoern A. Zeeb struct sockaddr *sa; 7700895aec3SBjoern A. Zeeb struct sockaddr_in *sin; 7710895aec3SBjoern A. Zeeb struct route sro; 7720895aec3SBjoern A. Zeeb int error; 7730895aec3SBjoern A. Zeeb 774413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 7750895aec3SBjoern A. Zeeb 776592bcae8SBjoern A. Zeeb /* 777592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 778592bcae8SBjoern A. Zeeb * if requested. 779592bcae8SBjoern A. Zeeb */ 780592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 781592bcae8SBjoern A. Zeeb return (0); 782592bcae8SBjoern A. Zeeb 7830895aec3SBjoern A. Zeeb error = 0; 7840895aec3SBjoern A. Zeeb bzero(&sro, sizeof(sro)); 7850895aec3SBjoern A. Zeeb 7860895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)&sro.ro_dst; 7870895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 7880895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 7890895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 7900895aec3SBjoern A. Zeeb 7910895aec3SBjoern A. Zeeb /* 7920895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 7930895aec3SBjoern A. Zeeb * else punt. 7940895aec3SBjoern A. Zeeb * 7950895aec3SBjoern A. Zeeb * Find out route to destination. 7960895aec3SBjoern A. Zeeb */ 7970895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 7986e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 7990895aec3SBjoern A. Zeeb 8000895aec3SBjoern A. Zeeb /* 8010895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 8020895aec3SBjoern A. Zeeb * the outgoing interface. 8030895aec3SBjoern A. Zeeb * 8040895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 8050895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 8060895aec3SBjoern A. Zeeb * the source address from. 8070895aec3SBjoern A. Zeeb */ 8080895aec3SBjoern A. Zeeb if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 8098c0fec80SRobert Watson struct in_ifaddr *ia; 8100895aec3SBjoern A. Zeeb struct ifnet *ifp; 8110895aec3SBjoern A. Zeeb 8124f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 81358a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 8140895aec3SBjoern A. Zeeb if (ia == NULL) 8154f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 81658a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 8170895aec3SBjoern A. Zeeb if (ia == NULL) { 8180895aec3SBjoern A. Zeeb error = ENETUNREACH; 8190895aec3SBjoern A. Zeeb goto done; 8200895aec3SBjoern A. Zeeb } 8210895aec3SBjoern A. Zeeb 8220304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8230895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8248c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8250895aec3SBjoern A. Zeeb goto done; 8260895aec3SBjoern A. Zeeb } 8270895aec3SBjoern A. Zeeb 8280895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 8298c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8300895aec3SBjoern A. Zeeb ia = NULL; 831137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 8320895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8330895aec3SBjoern A. Zeeb 8340895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 8350895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 8360895aec3SBjoern A. Zeeb continue; 8370895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 838b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8390895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 8400895aec3SBjoern A. Zeeb break; 8410895aec3SBjoern A. Zeeb } 8420895aec3SBjoern A. Zeeb } 8430895aec3SBjoern A. Zeeb if (ia != NULL) { 8440895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 845137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8460895aec3SBjoern A. Zeeb goto done; 8470895aec3SBjoern A. Zeeb } 848137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8490895aec3SBjoern A. Zeeb 8500895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 851b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 8520895aec3SBjoern A. Zeeb goto done; 8530895aec3SBjoern A. Zeeb } 8540895aec3SBjoern A. Zeeb 8550895aec3SBjoern A. Zeeb /* 8560895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 8570895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 8580895aec3SBjoern A. Zeeb * In case of jails do those three steps: 8590895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 8600895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 8610895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 8620895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 8630895aec3SBjoern A. Zeeb */ 8640895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 8658c0fec80SRobert Watson struct in_ifaddr *ia; 8669317b04eSRobert Watson struct ifnet *ifp; 8670895aec3SBjoern A. Zeeb 8680895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 8690304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8700895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8710895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8720895aec3SBjoern A. Zeeb goto done; 8730895aec3SBjoern A. Zeeb } 8740895aec3SBjoern A. Zeeb 8750895aec3SBjoern A. Zeeb /* Jailed. */ 8760895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 8770895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 878b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8790895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8800895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8810895aec3SBjoern A. Zeeb goto done; 8820895aec3SBjoern A. Zeeb } 8830895aec3SBjoern A. Zeeb 8840895aec3SBjoern A. Zeeb /* 8850895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 8860895aec3SBjoern A. Zeeb * belonging to this jail. 8870895aec3SBjoern A. Zeeb */ 8888c0fec80SRobert Watson ia = NULL; 8899317b04eSRobert Watson ifp = sro.ro_rt->rt_ifp; 890137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 8919317b04eSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8920895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 8930895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 8940895aec3SBjoern A. Zeeb continue; 8950895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 896b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8970895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 8980895aec3SBjoern A. Zeeb break; 8990895aec3SBjoern A. Zeeb } 9000895aec3SBjoern A. Zeeb } 9010895aec3SBjoern A. Zeeb if (ia != NULL) { 9020895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 903137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9040895aec3SBjoern A. Zeeb goto done; 9050895aec3SBjoern A. Zeeb } 906137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9070895aec3SBjoern A. Zeeb 9080895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 909b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 9100895aec3SBjoern A. Zeeb goto done; 9110895aec3SBjoern A. Zeeb } 9120895aec3SBjoern A. Zeeb 9130895aec3SBjoern A. Zeeb /* 9140895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 9150895aec3SBjoern A. Zeeb * to ourselves is here. 9160895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 9170895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 9180895aec3SBjoern A. Zeeb * a loopback interface. 9190895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 9200895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 9210895aec3SBjoern A. Zeeb */ 9220895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 9230895aec3SBjoern A. Zeeb struct sockaddr_in sain; 9248c0fec80SRobert Watson struct in_ifaddr *ia; 9250895aec3SBjoern A. Zeeb 9260895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 9270895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 9280895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 9290895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 9300895aec3SBjoern A. Zeeb 93158a39d8cSAlan Somers ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain), 93258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 9330895aec3SBjoern A. Zeeb if (ia == NULL) 9344f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0, 93558a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 936f0bb05fcSQing Li if (ia == NULL) 937f0bb05fcSQing Li ia = ifatoia(ifa_ifwithaddr(sintosa(&sain))); 9380895aec3SBjoern A. Zeeb 9390304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 9400895aec3SBjoern A. Zeeb if (ia == NULL) { 9410895aec3SBjoern A. Zeeb error = ENETUNREACH; 9420895aec3SBjoern A. Zeeb goto done; 9430895aec3SBjoern A. Zeeb } 9440895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 9458c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9460895aec3SBjoern A. Zeeb goto done; 9470895aec3SBjoern A. Zeeb } 9480895aec3SBjoern A. Zeeb 9490895aec3SBjoern A. Zeeb /* Jailed. */ 9500895aec3SBjoern A. Zeeb if (ia != NULL) { 9510895aec3SBjoern A. Zeeb struct ifnet *ifp; 9520895aec3SBjoern A. Zeeb 9530895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 9548c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9550895aec3SBjoern A. Zeeb ia = NULL; 956137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 9570895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 9580895aec3SBjoern A. Zeeb 9590895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 9600895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 9610895aec3SBjoern A. Zeeb continue; 9620895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 963b89e82ddSJamie Gritton if (prison_check_ip4(cred, 964b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 9650895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 9660895aec3SBjoern A. Zeeb break; 9670895aec3SBjoern A. Zeeb } 9680895aec3SBjoern A. Zeeb } 9690895aec3SBjoern A. Zeeb if (ia != NULL) { 9700895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 971137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9720895aec3SBjoern A. Zeeb goto done; 9730895aec3SBjoern A. Zeeb } 974137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9750895aec3SBjoern A. Zeeb } 9760895aec3SBjoern A. Zeeb 9770895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 978b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 9790895aec3SBjoern A. Zeeb goto done; 9800895aec3SBjoern A. Zeeb } 9810895aec3SBjoern A. Zeeb 9820895aec3SBjoern A. Zeeb done: 9830895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 9840895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 9850895aec3SBjoern A. Zeeb return (error); 9860895aec3SBjoern A. Zeeb } 9870895aec3SBjoern A. Zeeb 9880895aec3SBjoern A. Zeeb /* 9895200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 9905200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 9915200e00eSIan Dowse * address and port for the PCB; these are updated to the values 9925200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 9935200e00eSIan Dowse * the connect. 9945200e00eSIan Dowse * 9955200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 9965200e00eSIan Dowse * and port. These are not updated in the error case. 9975200e00eSIan Dowse * 9985200e00eSIan Dowse * If the operation fails because the connection already exists, 9995200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 10005200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 10015200e00eSIan Dowse * is set to NULL. 10025200e00eSIan Dowse */ 10035200e00eSIan Dowse int 1004136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1005136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1006136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 10075200e00eSIan Dowse { 1008cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 10095200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 10105200e00eSIan Dowse struct in_ifaddr *ia; 10115200e00eSIan Dowse struct inpcb *oinp; 1012b89e82ddSJamie Gritton struct in_addr laddr, faddr; 10135200e00eSIan Dowse u_short lport, fport; 10145200e00eSIan Dowse int error; 10155200e00eSIan Dowse 10168501a69cSRobert Watson /* 10178501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 10188501a69cSRobert Watson * lock is sufficient. 10198501a69cSRobert Watson */ 102027f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1021fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 102227f74fd0SRobert Watson 10235200e00eSIan Dowse if (oinpp != NULL) 10245200e00eSIan Dowse *oinpp = NULL; 102557bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1026df8bae1dSRodney W. Grimes return (EINVAL); 1027df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1028df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1029df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1030df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 10315200e00eSIan Dowse laddr.s_addr = *laddrp; 10325200e00eSIan Dowse lport = *lportp; 10335200e00eSIan Dowse faddr = sin->sin_addr; 10345200e00eSIan Dowse fport = sin->sin_port; 10350895aec3SBjoern A. Zeeb 1036603724d3SBjoern A. Zeeb if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 1037df8bae1dSRodney W. Grimes /* 1038df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1039df8bae1dSRodney W. Grimes * use the primary local address. 1040df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1041df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1042df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1043df8bae1dSRodney W. Grimes */ 1044413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1045cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1046413628a7SBjoern A. Zeeb faddr = 1047b89e82ddSJamie Gritton IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1048cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1049b89e82ddSJamie Gritton if (cred != NULL && 1050b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1051b89e82ddSJamie Gritton return (error); 10522d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1053cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 10542d9cfabaSRobert Watson if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 10552d9cfabaSRobert Watson IFF_BROADCAST) 10565200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 1057603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1058cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 10592d9cfabaSRobert Watson } 1060df8bae1dSRodney W. Grimes } 10615200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1062d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1063df8bae1dSRodney W. Grimes /* 1064df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1065d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1066df8bae1dSRodney W. Grimes * address of that interface as our source address. 1067df8bae1dSRodney W. Grimes */ 10685200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1069df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1070df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1071df8bae1dSRodney W. Grimes struct ifnet *ifp; 1072df8bae1dSRodney W. Grimes 1073df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1074df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1075df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1076cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1077e691be70SDaniel Eischen TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1078e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1079e691be70SDaniel Eischen (cred == NULL || 1080e691be70SDaniel Eischen prison_check_ip4(cred, 1081e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1082df8bae1dSRodney W. Grimes break; 1083e691be70SDaniel Eischen } 1084e691be70SDaniel Eischen if (ia == NULL) 1085d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1086e691be70SDaniel Eischen else { 10875200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1088d79fdd98SDaniel Eischen error = 0; 1089999f1343SGarrett Wollman } 1090cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1091d79fdd98SDaniel Eischen } 1092d79fdd98SDaniel Eischen } 109304215ed2SRandall Stewart if (error) 109404215ed2SRandall Stewart return (error); 10950895aec3SBjoern A. Zeeb } 1096fa046d87SRobert Watson oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1097fa046d87SRobert Watson laddr, lport, 0, NULL); 10985200e00eSIan Dowse if (oinp != NULL) { 10995200e00eSIan Dowse if (oinpp != NULL) 11005200e00eSIan Dowse *oinpp = oinp; 1101df8bae1dSRodney W. Grimes return (EADDRINUSE); 1102c3229e05SDavid Greenman } 11035200e00eSIan Dowse if (lport == 0) { 1104b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1105b0330ed9SPawel Jakub Dawidek cred); 11065a903f8dSPierre Beyssac if (error) 11075a903f8dSPierre Beyssac return (error); 11085a903f8dSPierre Beyssac } 11095200e00eSIan Dowse *laddrp = laddr.s_addr; 11105200e00eSIan Dowse *lportp = lport; 11115200e00eSIan Dowse *faddrp = faddr.s_addr; 11125200e00eSIan Dowse *fportp = fport; 1113df8bae1dSRodney W. Grimes return (0); 1114df8bae1dSRodney W. Grimes } 1115df8bae1dSRodney W. Grimes 111626f9a767SRodney W. Grimes void 1117136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1118df8bae1dSRodney W. Grimes { 11196b348152SRobert Watson 11208501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1121fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1122df8bae1dSRodney W. Grimes 1123df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1124df8bae1dSRodney W. Grimes inp->inp_fport = 0; 112515bd2b43SDavid Greenman in_pcbrehash(inp); 1126df8bae1dSRodney W. Grimes } 112783e521ecSBjoern A. Zeeb #endif /* INET */ 1128df8bae1dSRodney W. Grimes 11294c7c478dSRobert Watson /* 113028696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1131c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 113228696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 113328696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 11344c7c478dSRobert Watson */ 113526f9a767SRodney W. Grimes void 1136136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1137df8bae1dSRodney W. Grimes { 11384c7c478dSRobert Watson 1139a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1140c0a211c5SRobert Watson 1141f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1142f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1143f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1144f3e7afe2SHans Petter Selasky #endif 11454c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 11464c7c478dSRobert Watson inp->inp_socket = NULL; 11474c7c478dSRobert Watson } 11484c7c478dSRobert Watson 1149c0a211c5SRobert Watson /* 115079bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 115179bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 115279bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 115352cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 115452cd27cbSRobert Watson * via a pcbgroup. 115579bdc6e5SRobert Watson * 115679bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 115779bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 115879bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 115979bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 116079bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 116179bdc6e5SRobert Watson * inpcb. 116279bdc6e5SRobert Watson * 116379bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 116479bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 116579bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 116679bdc6e5SRobert Watson * using in_pcbrele(). 1167c0a211c5SRobert Watson */ 116879bdc6e5SRobert Watson void 116979bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 11704c7c478dSRobert Watson { 1171df8bae1dSRodney W. Grimes 117279bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 117379bdc6e5SRobert Watson 117479bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 117579bdc6e5SRobert Watson } 117679bdc6e5SRobert Watson 117779bdc6e5SRobert Watson /* 117879bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 117979bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 118079bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 118179bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 118279bdc6e5SRobert Watson * 118379bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 118479bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 118579bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 118679bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 118779bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 118879bdc6e5SRobert Watson */ 118979bdc6e5SRobert Watson int 119079bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 119179bdc6e5SRobert Watson { 119279bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 119379bdc6e5SRobert Watson 119479bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 119579bdc6e5SRobert Watson 119679bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 119779bdc6e5SRobert Watson 1198df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1199df4e91d3SGleb Smirnoff /* 1200df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1201df4e91d3SGleb Smirnoff * this isn't the last reference. 1202df4e91d3SGleb Smirnoff */ 1203df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1204df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1205df4e91d3SGleb Smirnoff return (1); 1206df4e91d3SGleb Smirnoff } 120779bdc6e5SRobert Watson return (0); 1208df4e91d3SGleb Smirnoff } 120979bdc6e5SRobert Watson 121079bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 121179bdc6e5SRobert Watson 121279bdc6e5SRobert Watson INP_RUNLOCK(inp); 121379bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 121479bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 121579bdc6e5SRobert Watson return (1); 121679bdc6e5SRobert Watson } 121779bdc6e5SRobert Watson 121879bdc6e5SRobert Watson int 121979bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 122079bdc6e5SRobert Watson { 122179bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 122279bdc6e5SRobert Watson 122379bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 122479bdc6e5SRobert Watson 122579bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 122679bdc6e5SRobert Watson 1227edd0e0b0SFabien Thomas if (refcount_release(&inp->inp_refcount) == 0) { 1228edd0e0b0SFabien Thomas /* 1229edd0e0b0SFabien Thomas * If the inpcb has been freed, let the caller know, even if 1230edd0e0b0SFabien Thomas * this isn't the last reference. 1231edd0e0b0SFabien Thomas */ 1232edd0e0b0SFabien Thomas if (inp->inp_flags2 & INP_FREED) { 1233edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1234edd0e0b0SFabien Thomas return (1); 1235edd0e0b0SFabien Thomas } 123679bdc6e5SRobert Watson return (0); 1237edd0e0b0SFabien Thomas } 123879bdc6e5SRobert Watson 123979bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 124079bdc6e5SRobert Watson 124179bdc6e5SRobert Watson INP_WUNLOCK(inp); 124279bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 124379bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 124479bdc6e5SRobert Watson return (1); 124579bdc6e5SRobert Watson } 124679bdc6e5SRobert Watson 124779bdc6e5SRobert Watson /* 124879bdc6e5SRobert Watson * Temporary wrapper. 124979bdc6e5SRobert Watson */ 125079bdc6e5SRobert Watson int 125179bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 125279bdc6e5SRobert Watson { 125379bdc6e5SRobert Watson 125479bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 125579bdc6e5SRobert Watson } 125679bdc6e5SRobert Watson 125779bdc6e5SRobert Watson /* 125879bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 125979bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 126079bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 126179bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 126279bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 126379bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 126479bdc6e5SRobert Watson * the pcbinfo lock is held. 126579bdc6e5SRobert Watson */ 126679bdc6e5SRobert Watson void 126779bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 126879bdc6e5SRobert Watson { 126979bdc6e5SRobert Watson struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 127079bdc6e5SRobert Watson 127179bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 127279bdc6e5SRobert Watson 1273ff9b006dSJulien Charbon #ifdef INVARIANTS 1274ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 1275079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(pcbinfo); 1276ff9b006dSJulien Charbon } else { 127779bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 1278ff9b006dSJulien Charbon } 1279ff9b006dSJulien Charbon #endif 128079bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 128179bdc6e5SRobert Watson 128279bdc6e5SRobert Watson /* XXXRW: Do as much as possible here. */ 1283fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 12846aee2fc5SBjoern A. Zeeb if (inp->inp_sp != NULL) 12856974bd9eSBjoern A. Zeeb ipsec_delete_pcbpolicy(inp); 128683e521ecSBjoern A. Zeeb #endif 1287ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 128879bdc6e5SRobert Watson inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1289c3229e05SDavid Greenman in_pcbremlists(inp); 1290ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 12916aee2fc5SBjoern A. Zeeb #ifdef INET6 12926aee2fc5SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 12936aee2fc5SBjoern A. Zeeb ip6_freepcbopts(inp->in6p_outputopts); 12941096332aSBruce M Simpson if (inp->in6p_moptions != NULL) 12956aee2fc5SBjoern A. Zeeb ip6_freemoptions(inp->in6p_moptions); 12966aee2fc5SBjoern A. Zeeb } 12976aee2fc5SBjoern A. Zeeb #endif 1298df8bae1dSRodney W. Grimes if (inp->inp_options) 1299df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 130067107f45SBjoern A. Zeeb #ifdef INET 130171498f30SBruce M Simpson if (inp->inp_moptions != NULL) 130271498f30SBruce M Simpson inp_freemoptions(inp->inp_moptions); 130367107f45SBjoern A. Zeeb #endif 1304cc94f0c2SGleb Smirnoff RO_RTFREE(&inp->inp_route); 13056d768226SGeorge V. Neville-Neil if (inp->inp_route.ro_lle) 13066d768226SGeorge V. Neville-Neil LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */ 130784cc0778SGeorge V. Neville-Neil 1308cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 1309df4e91d3SGleb Smirnoff inp->inp_flags2 |= INP_FREED; 131086d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 1311a557af22SRobert Watson #ifdef MAC 131230d239bcSRobert Watson mac_inpcb_destroy(inp); 1313a557af22SRobert Watson #endif 131479bdc6e5SRobert Watson if (!in_pcbrele_wlocked(inp)) 131528696211SRobert Watson INP_WUNLOCK(inp); 131628696211SRobert Watson } 131728696211SRobert Watson 131828696211SRobert Watson /* 1319c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1320c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1321c0a211c5SRobert Watson * 1322c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1323c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1324c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1325c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1326c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1327c0a211c5SRobert Watson * in_pcbdetach(). 1328c0a211c5SRobert Watson * 1329c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1330c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 133110702a28SRobert Watson */ 133210702a28SRobert Watson void 133310702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 133410702a28SRobert Watson { 133510702a28SRobert Watson 13368501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 133710702a28SRobert Watson 1338fa046d87SRobert Watson /* 1339fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1340fa046d87SRobert Watson * the hash lock...? 1341fa046d87SRobert Watson */ 1342ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1343111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 134410702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 134510702a28SRobert Watson 1346fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 134710702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 134810702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 134910702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 135010702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 135110702a28SRobert Watson free(phd, M_PCB); 135210702a28SRobert Watson } 1353fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1354111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 135552cd27cbSRobert Watson #ifdef PCBGROUP 135652cd27cbSRobert Watson in_pcbgroup_remove(inp); 135752cd27cbSRobert Watson #endif 135810702a28SRobert Watson } 135910702a28SRobert Watson } 136010702a28SRobert Watson 136167107f45SBjoern A. Zeeb #ifdef INET 136254d642bbSRobert Watson /* 136354d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 136454d642bbSRobert Watson */ 136526ef6ac4SDon Lewis struct sockaddr * 1366136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 136726ef6ac4SDon Lewis { 136826ef6ac4SDon Lewis struct sockaddr_in *sin; 136926ef6ac4SDon Lewis 13701ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1371a163d034SWarner Losh M_WAITOK | M_ZERO); 137226ef6ac4SDon Lewis sin->sin_family = AF_INET; 137326ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 137426ef6ac4SDon Lewis sin->sin_addr = *addr_p; 137526ef6ac4SDon Lewis sin->sin_port = port; 137626ef6ac4SDon Lewis 137726ef6ac4SDon Lewis return (struct sockaddr *)sin; 137826ef6ac4SDon Lewis } 137926ef6ac4SDon Lewis 1380117bcae7SGarrett Wollman int 138154d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1382df8bae1dSRodney W. Grimes { 1383136d4f1cSRobert Watson struct inpcb *inp; 138426ef6ac4SDon Lewis struct in_addr addr; 138526ef6ac4SDon Lewis in_port_t port; 138642fa505bSDavid Greenman 1387fdc984f7STor Egge inp = sotoinpcb(so); 138854d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 13896466b28aSRobert Watson 1390a69042a5SRobert Watson INP_RLOCK(inp); 139126ef6ac4SDon Lewis port = inp->inp_lport; 139226ef6ac4SDon Lewis addr = inp->inp_laddr; 1393a69042a5SRobert Watson INP_RUNLOCK(inp); 139442fa505bSDavid Greenman 139526ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1396117bcae7SGarrett Wollman return 0; 1397df8bae1dSRodney W. Grimes } 1398df8bae1dSRodney W. Grimes 1399117bcae7SGarrett Wollman int 140054d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1401df8bae1dSRodney W. Grimes { 1402136d4f1cSRobert Watson struct inpcb *inp; 140326ef6ac4SDon Lewis struct in_addr addr; 140426ef6ac4SDon Lewis in_port_t port; 140542fa505bSDavid Greenman 1406fdc984f7STor Egge inp = sotoinpcb(so); 140754d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 14086466b28aSRobert Watson 1409a69042a5SRobert Watson INP_RLOCK(inp); 141026ef6ac4SDon Lewis port = inp->inp_fport; 141126ef6ac4SDon Lewis addr = inp->inp_faddr; 1412a69042a5SRobert Watson INP_RUNLOCK(inp); 141342fa505bSDavid Greenman 141426ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1415117bcae7SGarrett Wollman return 0; 1416df8bae1dSRodney W. Grimes } 1417df8bae1dSRodney W. Grimes 141826f9a767SRodney W. Grimes void 1419136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1420136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1421d1c54148SJesper Skriver { 1422f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1423d1c54148SJesper Skriver 14243dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1425f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 14268501a69cSRobert Watson INP_WLOCK(inp); 1427d1c54148SJesper Skriver #ifdef INET6 1428f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 14298501a69cSRobert Watson INP_WUNLOCK(inp); 1430d1c54148SJesper Skriver continue; 1431f76fcf6dSJeffrey Hsu } 1432d1c54148SJesper Skriver #endif 1433d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1434f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 14358501a69cSRobert Watson INP_WUNLOCK(inp); 1436d1c54148SJesper Skriver continue; 1437d1c54148SJesper Skriver } 14383dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 14398501a69cSRobert Watson INP_WUNLOCK(inp); 1440f76fcf6dSJeffrey Hsu } 14413dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1442d1c54148SJesper Skriver } 1443d1c54148SJesper Skriver 1444e43cc4aeSHajimu UMEMOTO void 1445136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1446e43cc4aeSHajimu UMEMOTO { 1447e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1448e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1449e43cc4aeSHajimu UMEMOTO int i, gap; 1450e43cc4aeSHajimu UMEMOTO 1451ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1452712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 14538501a69cSRobert Watson INP_WLOCK(inp); 1454e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1455e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1456e43cc4aeSHajimu UMEMOTO imo != NULL) { 1457e43cc4aeSHajimu UMEMOTO /* 1458e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1459e43cc4aeSHajimu UMEMOTO * detached. 1460e43cc4aeSHajimu UMEMOTO */ 1461e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1462e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1463e43cc4aeSHajimu UMEMOTO 1464e43cc4aeSHajimu UMEMOTO /* 1465e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1466e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1467e43cc4aeSHajimu UMEMOTO */ 1468e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1469e43cc4aeSHajimu UMEMOTO i++) { 1470e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1471e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 1472e43cc4aeSHajimu UMEMOTO gap++; 1473e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1474e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1475e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1476e43cc4aeSHajimu UMEMOTO } 1477e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1478e43cc4aeSHajimu UMEMOTO } 14798501a69cSRobert Watson INP_WUNLOCK(inp); 1480e43cc4aeSHajimu UMEMOTO } 1481ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1482e43cc4aeSHajimu UMEMOTO } 1483e43cc4aeSHajimu UMEMOTO 1484df8bae1dSRodney W. Grimes /* 1485fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1486fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1487c3229e05SDavid Greenman */ 1488d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1489df8bae1dSRodney W. Grimes struct inpcb * 1490136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 149168e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1492df8bae1dSRodney W. Grimes { 1493136d4f1cSRobert Watson struct inpcb *inp; 1494d5e8a67eSHajimu UMEMOTO #ifdef INET6 1495d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1496d5e8a67eSHajimu UMEMOTO #else 1497d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1498d5e8a67eSHajimu UMEMOTO #endif 1499d5e8a67eSHajimu UMEMOTO int wildcard; 15007bc4aca7SDavid Greenman 150168e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 150268e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 150368e0d7e0SRobert Watson 1504fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 15051b73ca0bSSam Leffler 150668e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1507c3229e05SDavid Greenman struct inpcbhead *head; 1508c3229e05SDavid Greenman /* 1509c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1510c3229e05SDavid Greenman * matches the local address and port we're looking for. 1511c3229e05SDavid Greenman */ 1512712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1513712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1514fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1515cfa1ca9dSYoshinobu Inoue #ifdef INET6 1516413628a7SBjoern A. Zeeb /* XXX inp locking */ 1517369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1518cfa1ca9dSYoshinobu Inoue continue; 1519cfa1ca9dSYoshinobu Inoue #endif 1520c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1521c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1522c3229e05SDavid Greenman inp->inp_lport == lport) { 1523c3229e05SDavid Greenman /* 1524413628a7SBjoern A. Zeeb * Found? 1525c3229e05SDavid Greenman */ 1526413628a7SBjoern A. Zeeb if (cred == NULL || 15270304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 15280304c731SJamie Gritton inp->inp_cred->cr_prison)) 1529c3229e05SDavid Greenman return (inp); 1530df8bae1dSRodney W. Grimes } 1531c3229e05SDavid Greenman } 1532c3229e05SDavid Greenman /* 1533c3229e05SDavid Greenman * Not found. 1534c3229e05SDavid Greenman */ 1535c3229e05SDavid Greenman return (NULL); 1536c3229e05SDavid Greenman } else { 1537c3229e05SDavid Greenman struct inpcbporthead *porthash; 1538c3229e05SDavid Greenman struct inpcbport *phd; 1539c3229e05SDavid Greenman struct inpcb *match = NULL; 1540c3229e05SDavid Greenman /* 1541c3229e05SDavid Greenman * Best fit PCB lookup. 1542c3229e05SDavid Greenman * 1543c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1544c3229e05SDavid Greenman * port hash list. 1545c3229e05SDavid Greenman */ 1546712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1547712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1548fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1549c3229e05SDavid Greenman if (phd->phd_port == lport) 1550c3229e05SDavid Greenman break; 1551c3229e05SDavid Greenman } 1552c3229e05SDavid Greenman if (phd != NULL) { 1553c3229e05SDavid Greenman /* 1554c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1555c3229e05SDavid Greenman * fit. 1556c3229e05SDavid Greenman */ 155737d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1558c3229e05SDavid Greenman wildcard = 0; 1559413628a7SBjoern A. Zeeb if (cred != NULL && 15600304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 15610304c731SJamie Gritton cred->cr_prison)) 1562413628a7SBjoern A. Zeeb continue; 1563cfa1ca9dSYoshinobu Inoue #ifdef INET6 1564413628a7SBjoern A. Zeeb /* XXX inp locking */ 1565369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1566cfa1ca9dSYoshinobu Inoue continue; 1567d5e8a67eSHajimu UMEMOTO /* 1568d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1569d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1570d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1571d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1572d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1573d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1574d5e8a67eSHajimu UMEMOTO * 1575d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1576d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1577d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1578d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1579d5e8a67eSHajimu UMEMOTO */ 1580d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1581d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1582cfa1ca9dSYoshinobu Inoue #endif 1583c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1584c3229e05SDavid Greenman wildcard++; 158515bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 158615bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 158715bd2b43SDavid Greenman wildcard++; 158815bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 158915bd2b43SDavid Greenman continue; 159015bd2b43SDavid Greenman } else { 159115bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 159215bd2b43SDavid Greenman wildcard++; 159315bd2b43SDavid Greenman } 1594df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1595df8bae1dSRodney W. Grimes match = inp; 1596df8bae1dSRodney W. Grimes matchwild = wildcard; 1597413628a7SBjoern A. Zeeb if (matchwild == 0) 1598df8bae1dSRodney W. Grimes break; 1599df8bae1dSRodney W. Grimes } 1600df8bae1dSRodney W. Grimes } 16013dbdc25cSDavid Greenman } 1602df8bae1dSRodney W. Grimes return (match); 1603df8bae1dSRodney W. Grimes } 1604c3229e05SDavid Greenman } 1605d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 160615bd2b43SDavid Greenman 160752cd27cbSRobert Watson #ifdef PCBGROUP 160852cd27cbSRobert Watson /* 160952cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 161052cd27cbSRobert Watson */ 161152cd27cbSRobert Watson static struct inpcb * 161252cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 161352cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 161452cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 161552cd27cbSRobert Watson { 161652cd27cbSRobert Watson struct inpcbhead *head; 161752cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 161852cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 161952cd27cbSRobert Watson 162052cd27cbSRobert Watson /* 162152cd27cbSRobert Watson * First look for an exact match. 162252cd27cbSRobert Watson */ 162352cd27cbSRobert Watson tmpinp = NULL; 162452cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 162552cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 162652cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 162752cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgrouphash) { 162852cd27cbSRobert Watson #ifdef INET6 162952cd27cbSRobert Watson /* XXX inp locking */ 163052cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 163152cd27cbSRobert Watson continue; 163252cd27cbSRobert Watson #endif 163352cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 163452cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 163552cd27cbSRobert Watson inp->inp_fport == fport && 163652cd27cbSRobert Watson inp->inp_lport == lport) { 163752cd27cbSRobert Watson /* 163852cd27cbSRobert Watson * XXX We should be able to directly return 163952cd27cbSRobert Watson * the inp here, without any checks. 164052cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 164152cd27cbSRobert Watson */ 164252cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 164352cd27cbSRobert Watson goto found; 164452cd27cbSRobert Watson if (tmpinp == NULL) 164552cd27cbSRobert Watson tmpinp = inp; 164652cd27cbSRobert Watson } 164752cd27cbSRobert Watson } 164852cd27cbSRobert Watson if (tmpinp != NULL) { 164952cd27cbSRobert Watson inp = tmpinp; 165052cd27cbSRobert Watson goto found; 165152cd27cbSRobert Watson } 165252cd27cbSRobert Watson 16530a100a6fSAdrian Chadd #ifdef RSS 16540a100a6fSAdrian Chadd /* 16550a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 16560a100a6fSAdrian Chadd * match for an RSS-local socket. 16570a100a6fSAdrian Chadd */ 16580a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 16590a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 16600a100a6fSAdrian Chadd #ifdef INET6 16610a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 16620a100a6fSAdrian Chadd #endif 16630a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 16640a100a6fSAdrian Chadd struct inpcbhead *head; 16650a100a6fSAdrian Chadd int injail; 16660a100a6fSAdrian Chadd 16670a100a6fSAdrian Chadd /* 16680a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 16690a100a6fSAdrian Chadd * 1. jailed, non-wild. 16700a100a6fSAdrian Chadd * 2. jailed, wild. 16710a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 16720a100a6fSAdrian Chadd * 4. non-jailed, wild. 16730a100a6fSAdrian Chadd */ 16740a100a6fSAdrian Chadd 16750a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 16760a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 16770a100a6fSAdrian Chadd LIST_FOREACH(inp, head, inp_pcbgrouphash) { 16780a100a6fSAdrian Chadd #ifdef INET6 16790a100a6fSAdrian Chadd /* XXX inp locking */ 16800a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 16810a100a6fSAdrian Chadd continue; 16820a100a6fSAdrian Chadd #endif 16830a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 16840a100a6fSAdrian Chadd inp->inp_lport != lport) 16850a100a6fSAdrian Chadd continue; 16860a100a6fSAdrian Chadd 16870a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 16880a100a6fSAdrian Chadd if (injail) { 16890a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 16900a100a6fSAdrian Chadd &laddr) != 0) 16910a100a6fSAdrian Chadd continue; 16920a100a6fSAdrian Chadd } else { 16930a100a6fSAdrian Chadd if (local_exact != NULL) 16940a100a6fSAdrian Chadd continue; 16950a100a6fSAdrian Chadd } 16960a100a6fSAdrian Chadd 16970a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 16980a100a6fSAdrian Chadd if (injail) 16990a100a6fSAdrian Chadd goto found; 17000a100a6fSAdrian Chadd else 17010a100a6fSAdrian Chadd local_exact = inp; 17020a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 17030a100a6fSAdrian Chadd #ifdef INET6 17040a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 17050a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 17060a100a6fSAdrian Chadd local_wild_mapped = inp; 17070a100a6fSAdrian Chadd else 17080a100a6fSAdrian Chadd #endif 17090a100a6fSAdrian Chadd if (injail) 17100a100a6fSAdrian Chadd jail_wild = inp; 17110a100a6fSAdrian Chadd else 17120a100a6fSAdrian Chadd local_wild = inp; 17130a100a6fSAdrian Chadd } 17140a100a6fSAdrian Chadd } /* LIST_FOREACH */ 17150a100a6fSAdrian Chadd 17160a100a6fSAdrian Chadd inp = jail_wild; 17170a100a6fSAdrian Chadd if (inp == NULL) 17180a100a6fSAdrian Chadd inp = local_exact; 17190a100a6fSAdrian Chadd if (inp == NULL) 17200a100a6fSAdrian Chadd inp = local_wild; 17210a100a6fSAdrian Chadd #ifdef INET6 17220a100a6fSAdrian Chadd if (inp == NULL) 17230a100a6fSAdrian Chadd inp = local_wild_mapped; 17240a100a6fSAdrian Chadd #endif 17250a100a6fSAdrian Chadd if (inp != NULL) 17260a100a6fSAdrian Chadd goto found; 17270a100a6fSAdrian Chadd } 17280a100a6fSAdrian Chadd #endif 17290a100a6fSAdrian Chadd 173052cd27cbSRobert Watson /* 173152cd27cbSRobert Watson * Then look for a wildcard match, if requested. 173252cd27cbSRobert Watson */ 173352cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 173452cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 173552cd27cbSRobert Watson #ifdef INET6 173652cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 173752cd27cbSRobert Watson #endif 173852cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 173952cd27cbSRobert Watson struct inpcbhead *head; 174052cd27cbSRobert Watson int injail; 174152cd27cbSRobert Watson 174252cd27cbSRobert Watson /* 174352cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 174452cd27cbSRobert Watson * 1. jailed, non-wild. 174552cd27cbSRobert Watson * 2. jailed, wild. 174652cd27cbSRobert Watson * 3. non-jailed, non-wild. 174752cd27cbSRobert Watson * 4. non-jailed, wild. 174852cd27cbSRobert Watson */ 174952cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 175052cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 175152cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 175252cd27cbSRobert Watson #ifdef INET6 175352cd27cbSRobert Watson /* XXX inp locking */ 175452cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 175552cd27cbSRobert Watson continue; 175652cd27cbSRobert Watson #endif 175752cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 175852cd27cbSRobert Watson inp->inp_lport != lport) 175952cd27cbSRobert Watson continue; 176052cd27cbSRobert Watson 176152cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 176252cd27cbSRobert Watson if (injail) { 176352cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 176452cd27cbSRobert Watson &laddr) != 0) 176552cd27cbSRobert Watson continue; 176652cd27cbSRobert Watson } else { 176752cd27cbSRobert Watson if (local_exact != NULL) 176852cd27cbSRobert Watson continue; 176952cd27cbSRobert Watson } 177052cd27cbSRobert Watson 177152cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 177252cd27cbSRobert Watson if (injail) 177352cd27cbSRobert Watson goto found; 177452cd27cbSRobert Watson else 177552cd27cbSRobert Watson local_exact = inp; 177652cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 177752cd27cbSRobert Watson #ifdef INET6 177852cd27cbSRobert Watson /* XXX inp locking, NULL check */ 177952cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 178052cd27cbSRobert Watson local_wild_mapped = inp; 178152cd27cbSRobert Watson else 178283e521ecSBjoern A. Zeeb #endif 178352cd27cbSRobert Watson if (injail) 178452cd27cbSRobert Watson jail_wild = inp; 178552cd27cbSRobert Watson else 178652cd27cbSRobert Watson local_wild = inp; 178752cd27cbSRobert Watson } 178852cd27cbSRobert Watson } /* LIST_FOREACH */ 178952cd27cbSRobert Watson inp = jail_wild; 179052cd27cbSRobert Watson if (inp == NULL) 179152cd27cbSRobert Watson inp = local_exact; 179252cd27cbSRobert Watson if (inp == NULL) 179352cd27cbSRobert Watson inp = local_wild; 179452cd27cbSRobert Watson #ifdef INET6 179552cd27cbSRobert Watson if (inp == NULL) 179652cd27cbSRobert Watson inp = local_wild_mapped; 179783e521ecSBjoern A. Zeeb #endif 179852cd27cbSRobert Watson if (inp != NULL) 179952cd27cbSRobert Watson goto found; 180052cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 180152cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 180252cd27cbSRobert Watson return (NULL); 180352cd27cbSRobert Watson 180452cd27cbSRobert Watson found: 180552cd27cbSRobert Watson in_pcbref(inp); 180652cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 180752cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 180852cd27cbSRobert Watson INP_WLOCK(inp); 180952cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 181052cd27cbSRobert Watson return (NULL); 181152cd27cbSRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 181252cd27cbSRobert Watson INP_RLOCK(inp); 181352cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 181452cd27cbSRobert Watson return (NULL); 181552cd27cbSRobert Watson } else 181652cd27cbSRobert Watson panic("%s: locking bug", __func__); 181752cd27cbSRobert Watson return (inp); 181852cd27cbSRobert Watson } 181952cd27cbSRobert Watson #endif /* PCBGROUP */ 182052cd27cbSRobert Watson 182115bd2b43SDavid Greenman /* 1822fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 1823fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 1824fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 182515bd2b43SDavid Greenman */ 1826fa046d87SRobert Watson static struct inpcb * 1827fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 182868e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 1829136d4f1cSRobert Watson struct ifnet *ifp) 183015bd2b43SDavid Greenman { 183115bd2b43SDavid Greenman struct inpcbhead *head; 1832413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 183315bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 183415bd2b43SDavid Greenman 183568e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 183668e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 183768e0d7e0SRobert Watson 1838fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 1839602cc7f1SRobert Watson 184015bd2b43SDavid Greenman /* 184115bd2b43SDavid Greenman * First look for an exact match. 184215bd2b43SDavid Greenman */ 1843413628a7SBjoern A. Zeeb tmpinp = NULL; 1844712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1845712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1846fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1847cfa1ca9dSYoshinobu Inoue #ifdef INET6 1848413628a7SBjoern A. Zeeb /* XXX inp locking */ 1849369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1850cfa1ca9dSYoshinobu Inoue continue; 1851cfa1ca9dSYoshinobu Inoue #endif 18526d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1853ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1854ca98b82cSDavid Greenman inp->inp_fport == fport && 1855413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1856413628a7SBjoern A. Zeeb /* 1857413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1858413628a7SBjoern A. Zeeb * the inp here, without any checks. 1859413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1860413628a7SBjoern A. Zeeb */ 18610304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 1862c3229e05SDavid Greenman return (inp); 1863413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1864413628a7SBjoern A. Zeeb tmpinp = inp; 1865c3229e05SDavid Greenman } 1866413628a7SBjoern A. Zeeb } 1867413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1868413628a7SBjoern A. Zeeb return (tmpinp); 1869e3fd5ffdSRobert Watson 1870e3fd5ffdSRobert Watson /* 1871e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1872e3fd5ffdSRobert Watson */ 187368e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1874413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1875e3fd5ffdSRobert Watson #ifdef INET6 1876cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1877e3fd5ffdSRobert Watson #endif 1878413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1879413628a7SBjoern A. Zeeb int injail; 1880413628a7SBjoern A. Zeeb 1881413628a7SBjoern A. Zeeb /* 1882413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 1883413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 1884413628a7SBjoern A. Zeeb * 2. jailed, wild. 1885413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 1886413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 1887413628a7SBjoern A. Zeeb */ 18886d6a026bSDavid Greenman 1889712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1890712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1891fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1892cfa1ca9dSYoshinobu Inoue #ifdef INET6 1893413628a7SBjoern A. Zeeb /* XXX inp locking */ 1894369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1895cfa1ca9dSYoshinobu Inoue continue; 1896cfa1ca9dSYoshinobu Inoue #endif 1897413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 1898413628a7SBjoern A. Zeeb inp->inp_lport != lport) 1899413628a7SBjoern A. Zeeb continue; 1900413628a7SBjoern A. Zeeb 19010304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 1902413628a7SBjoern A. Zeeb if (injail) { 1903b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 1904b89e82ddSJamie Gritton &laddr) != 0) 1905413628a7SBjoern A. Zeeb continue; 1906413628a7SBjoern A. Zeeb } else { 1907413628a7SBjoern A. Zeeb if (local_exact != NULL) 1908413628a7SBjoern A. Zeeb continue; 1909413628a7SBjoern A. Zeeb } 1910413628a7SBjoern A. Zeeb 1911413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 1912413628a7SBjoern A. Zeeb if (injail) 1913c3229e05SDavid Greenman return (inp); 1914413628a7SBjoern A. Zeeb else 1915413628a7SBjoern A. Zeeb local_exact = inp; 1916413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1917e3fd5ffdSRobert Watson #ifdef INET6 1918413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 19195cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 1920cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1921cfa1ca9dSYoshinobu Inoue else 192283e521ecSBjoern A. Zeeb #endif 1923413628a7SBjoern A. Zeeb if (injail) 1924413628a7SBjoern A. Zeeb jail_wild = inp; 1925413628a7SBjoern A. Zeeb else 19266d6a026bSDavid Greenman local_wild = inp; 19276d6a026bSDavid Greenman } 1928413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 1929413628a7SBjoern A. Zeeb if (jail_wild != NULL) 1930413628a7SBjoern A. Zeeb return (jail_wild); 1931413628a7SBjoern A. Zeeb if (local_exact != NULL) 1932413628a7SBjoern A. Zeeb return (local_exact); 1933413628a7SBjoern A. Zeeb if (local_wild != NULL) 1934c3229e05SDavid Greenman return (local_wild); 1935413628a7SBjoern A. Zeeb #ifdef INET6 1936413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 1937413628a7SBjoern A. Zeeb return (local_wild_mapped); 193883e521ecSBjoern A. Zeeb #endif 193968e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 1940413628a7SBjoern A. Zeeb 19416d6a026bSDavid Greenman return (NULL); 194215bd2b43SDavid Greenman } 1943fa046d87SRobert Watson 1944fa046d87SRobert Watson /* 1945fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 1946fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 1947fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 1948fa046d87SRobert Watson */ 1949fa046d87SRobert Watson static struct inpcb * 1950fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1951fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 1952fa046d87SRobert Watson struct ifnet *ifp) 1953fa046d87SRobert Watson { 1954fa046d87SRobert Watson struct inpcb *inp; 1955fa046d87SRobert Watson 1956fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 1957fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1958fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 1959fa046d87SRobert Watson if (inp != NULL) { 1960fa046d87SRobert Watson in_pcbref(inp); 1961fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1962fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 1963fa046d87SRobert Watson INP_WLOCK(inp); 1964fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) 1965fa046d87SRobert Watson return (NULL); 1966fa046d87SRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 1967fa046d87SRobert Watson INP_RLOCK(inp); 1968fa046d87SRobert Watson if (in_pcbrele_rlocked(inp)) 1969fa046d87SRobert Watson return (NULL); 1970fa046d87SRobert Watson } else 1971fa046d87SRobert Watson panic("%s: locking bug", __func__); 1972fa046d87SRobert Watson } else 1973fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1974fa046d87SRobert Watson return (inp); 1975fa046d87SRobert Watson } 1976fa046d87SRobert Watson 1977fa046d87SRobert Watson /* 1978d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1979d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 198052cd27cbSRobert Watson * 198152cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 1982fa046d87SRobert Watson */ 1983fa046d87SRobert Watson struct inpcb * 1984fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 1985fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 1986fa046d87SRobert Watson { 19877527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 198852cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 198952cd27cbSRobert Watson #endif 1990fa046d87SRobert Watson 1991fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1992fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 1993fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1994fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 1995fa046d87SRobert Watson 19967527624eSRobert Watson /* 19977527624eSRobert Watson * When not using RSS, use connection groups in preference to the 19987527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 19997527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 20007527624eSRobert Watson * in software. 20017527624eSRobert Watson * 20027527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 20037527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 20047527624eSRobert Watson * in software. 20057527624eSRobert Watson */ 20067527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 200752cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 200852cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 200952cd27cbSRobert Watson fport); 201052cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 201152cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 201252cd27cbSRobert Watson } 201352cd27cbSRobert Watson #endif 2014fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2015fa046d87SRobert Watson lookupflags, ifp)); 2016fa046d87SRobert Watson } 2017d3c1f003SRobert Watson 2018d3c1f003SRobert Watson struct inpcb * 2019d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2020d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2021d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2022d3c1f003SRobert Watson { 202352cd27cbSRobert Watson #ifdef PCBGROUP 202452cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 202552cd27cbSRobert Watson #endif 2026d3c1f003SRobert Watson 2027d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2028d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2029d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2030d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2031d3c1f003SRobert Watson 203252cd27cbSRobert Watson #ifdef PCBGROUP 20337527624eSRobert Watson /* 20347527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 20357527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 20367527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 20377527624eSRobert Watson * using RSS. 20387527624eSRobert Watson * 20397527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 20407527624eSRobert Watson */ 20417527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 20427527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 204352cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 204452cd27cbSRobert Watson m->m_pkthdr.flowid); 204552cd27cbSRobert Watson if (pcbgroup != NULL) 204652cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 204752cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 20487527624eSRobert Watson #ifndef RSS 204952cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 205052cd27cbSRobert Watson fport); 205152cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 205252cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 20537527624eSRobert Watson #endif 205452cd27cbSRobert Watson } 205552cd27cbSRobert Watson #endif 2056d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2057d3c1f003SRobert Watson lookupflags, ifp)); 2058d3c1f003SRobert Watson } 205967107f45SBjoern A. Zeeb #endif /* INET */ 206015bd2b43SDavid Greenman 20617bc4aca7SDavid Greenman /* 2062c3229e05SDavid Greenman * Insert PCB onto various hash lists. 20637bc4aca7SDavid Greenman */ 206452cd27cbSRobert Watson static int 206552cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 206615bd2b43SDavid Greenman { 2067c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2068c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2069c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2070c3229e05SDavid Greenman struct inpcbport *phd; 2071cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 207215bd2b43SDavid Greenman 20738501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2074fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2075fa046d87SRobert Watson 2076111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2077111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2078602cc7f1SRobert Watson 2079cfa1ca9dSYoshinobu Inoue #ifdef INET6 2080cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 20811b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2082cfa1ca9dSYoshinobu Inoue else 208383e521ecSBjoern A. Zeeb #endif 2084cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2085cfa1ca9dSYoshinobu Inoue 2086712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2087712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 208815bd2b43SDavid Greenman 2089712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2090712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2091c3229e05SDavid Greenman 2092c3229e05SDavid Greenman /* 2093c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2094c3229e05SDavid Greenman */ 2095fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 2096c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2097c3229e05SDavid Greenman break; 2098c3229e05SDavid Greenman } 2099c3229e05SDavid Greenman /* 2100c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2101c3229e05SDavid Greenman */ 2102c3229e05SDavid Greenman if (phd == NULL) { 21031ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2104c3229e05SDavid Greenman if (phd == NULL) { 2105c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2106c3229e05SDavid Greenman } 2107c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2108c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 2109c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2110c3229e05SDavid Greenman } 2111c3229e05SDavid Greenman inp->inp_phd = phd; 2112c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2113c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2114111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 211552cd27cbSRobert Watson #ifdef PCBGROUP 211652cd27cbSRobert Watson if (do_pcbgroup_update) 211752cd27cbSRobert Watson in_pcbgroup_update(inp); 211852cd27cbSRobert Watson #endif 2119c3229e05SDavid Greenman return (0); 212015bd2b43SDavid Greenman } 212115bd2b43SDavid Greenman 2122c3229e05SDavid Greenman /* 212352cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 212452cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 212552cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 212652cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 212752cd27cbSRobert Watson * pcbgroup until later. 212852cd27cbSRobert Watson * 212952cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 213052cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 213152cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 213252cd27cbSRobert Watson */ 213352cd27cbSRobert Watson int 213452cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 213552cd27cbSRobert Watson { 213652cd27cbSRobert Watson 213752cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 213852cd27cbSRobert Watson } 213952cd27cbSRobert Watson 214052cd27cbSRobert Watson int 214152cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 214252cd27cbSRobert Watson { 214352cd27cbSRobert Watson 214452cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 214552cd27cbSRobert Watson } 214652cd27cbSRobert Watson 214752cd27cbSRobert Watson /* 2148c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2149c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2150c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2151c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2152c3229e05SDavid Greenman */ 215315bd2b43SDavid Greenman void 2154d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 215515bd2b43SDavid Greenman { 215659daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 215715bd2b43SDavid Greenman struct inpcbhead *head; 2158cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 215915bd2b43SDavid Greenman 21608501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2161fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2162fa046d87SRobert Watson 2163111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2164111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2165602cc7f1SRobert Watson 2166cfa1ca9dSYoshinobu Inoue #ifdef INET6 2167cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 21681b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2169cfa1ca9dSYoshinobu Inoue else 217083e521ecSBjoern A. Zeeb #endif 2171cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2172cfa1ca9dSYoshinobu Inoue 2173712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2174712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 217515bd2b43SDavid Greenman 2176c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 217715bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 217852cd27cbSRobert Watson 217952cd27cbSRobert Watson #ifdef PCBGROUP 218052cd27cbSRobert Watson if (m != NULL) 218152cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 218252cd27cbSRobert Watson else 218352cd27cbSRobert Watson in_pcbgroup_update(inp); 218452cd27cbSRobert Watson #endif 2185c3229e05SDavid Greenman } 2186c3229e05SDavid Greenman 2187d3c1f003SRobert Watson void 2188d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2189d3c1f003SRobert Watson { 2190d3c1f003SRobert Watson 2191d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2192d3c1f003SRobert Watson } 2193d3c1f003SRobert Watson 2194c3229e05SDavid Greenman /* 2195c3229e05SDavid Greenman * Remove PCB from various lists. 2196c3229e05SDavid Greenman */ 21976d888973SRobert Watson static void 2198136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2199c3229e05SDavid Greenman { 220059daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 220159daba27SSam Leffler 2202ff9b006dSJulien Charbon #ifdef INVARIANTS 2203ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 2204ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 2205ff9b006dSJulien Charbon } else { 220659daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 2207ff9b006dSJulien Charbon } 2208ff9b006dSJulien Charbon #endif 2209ff9b006dSJulien Charbon 22108501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2211ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 221259daba27SSam Leffler 221359daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2214111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2215c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2216c3229e05SDavid Greenman 2217fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 2218c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 2219c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 2220fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2221c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 2222c3229e05SDavid Greenman free(phd, M_PCB); 2223c3229e05SDavid Greenman } 2224fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2225111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2226c3229e05SDavid Greenman } 2227c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 222859daba27SSam Leffler pcbinfo->ipi_count--; 222952cd27cbSRobert Watson #ifdef PCBGROUP 223052cd27cbSRobert Watson in_pcbgroup_remove(inp); 223152cd27cbSRobert Watson #endif 223215bd2b43SDavid Greenman } 223375c13541SPoul-Henning Kamp 2234a557af22SRobert Watson /* 223584cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 223684cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 223784cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 223884cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 223984cc0778SGeorge V. Neville-Neil */ 224084cc0778SGeorge V. Neville-Neil void 224184cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 224284cc0778SGeorge V. Neville-Neil { 224384cc0778SGeorge V. Neville-Neil 2244cc94f0c2SGleb Smirnoff RO_RTFREE(&inp->inp_route); 22456d768226SGeorge V. Neville-Neil if (inp->inp_route.ro_lle) 22466d768226SGeorge V. Neville-Neil LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */ 224784cc0778SGeorge V. Neville-Neil return; 224884cc0778SGeorge V. Neville-Neil } 224984cc0778SGeorge V. Neville-Neil 225084cc0778SGeorge V. Neville-Neil /* 2251a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2252a557af22SRobert Watson * label change into the in_pcb for the socket. 2253a557af22SRobert Watson */ 2254a557af22SRobert Watson void 2255136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2256a557af22SRobert Watson { 2257a557af22SRobert Watson #ifdef MAC 2258a557af22SRobert Watson struct inpcb *inp; 2259a557af22SRobert Watson 22604c7c478dSRobert Watson inp = sotoinpcb(so); 22614c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2262602cc7f1SRobert Watson 22638501a69cSRobert Watson INP_WLOCK(inp); 2264310e7cebSRobert Watson SOCK_LOCK(so); 2265a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2266310e7cebSRobert Watson SOCK_UNLOCK(so); 22678501a69cSRobert Watson INP_WUNLOCK(inp); 2268a557af22SRobert Watson #endif 2269a557af22SRobert Watson } 22705f311da2SMike Silbersack 22715f311da2SMike Silbersack /* 2272ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2273ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2274ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2275ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2276ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 22775f311da2SMike Silbersack */ 2278aae49dd3SBjoern A. Zeeb static void 2279136d4f1cSRobert Watson ipport_tick(void *xtp) 22805f311da2SMike Silbersack { 22818b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2282ad3a630fSRobert Watson 22835ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 22848b615593SMarko Zec VNET_FOREACH(vnet_iter) { 22858b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 22868b615593SMarko Zec if (V_ipport_tcpallocs <= 22878b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2288603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2289603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2290ad3a630fSRobert Watson } else 2291603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2292603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 22938b615593SMarko Zec CURVNET_RESTORE(); 22948b615593SMarko Zec } 22955ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 22965f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 22975f311da2SMike Silbersack } 2298497057eeSRobert Watson 2299aae49dd3SBjoern A. Zeeb static void 2300aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2301aae49dd3SBjoern A. Zeeb { 2302aae49dd3SBjoern A. Zeeb 2303aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2304aae49dd3SBjoern A. Zeeb } 2305aae49dd3SBjoern A. Zeeb 2306aae49dd3SBjoern A. Zeeb /* 2307aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2308aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2309aae49dd3SBjoern A. Zeeb */ 2310aae49dd3SBjoern A. Zeeb static void 2311aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2312aae49dd3SBjoern A. Zeeb { 2313aae49dd3SBjoern A. Zeeb 2314aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2315fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2316aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2317aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2318aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2319aae49dd3SBjoern A. Zeeb } 2320aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2321aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2322aae49dd3SBjoern A. Zeeb 23233d585327SKip Macy void 23243d585327SKip Macy inp_wlock(struct inpcb *inp) 23253d585327SKip Macy { 23263d585327SKip Macy 23278501a69cSRobert Watson INP_WLOCK(inp); 23283d585327SKip Macy } 23293d585327SKip Macy 23303d585327SKip Macy void 23313d585327SKip Macy inp_wunlock(struct inpcb *inp) 23323d585327SKip Macy { 23333d585327SKip Macy 23348501a69cSRobert Watson INP_WUNLOCK(inp); 23353d585327SKip Macy } 23363d585327SKip Macy 23373d585327SKip Macy void 23383d585327SKip Macy inp_rlock(struct inpcb *inp) 23393d585327SKip Macy { 23403d585327SKip Macy 2341a69042a5SRobert Watson INP_RLOCK(inp); 23423d585327SKip Macy } 23433d585327SKip Macy 23443d585327SKip Macy void 23453d585327SKip Macy inp_runlock(struct inpcb *inp) 23463d585327SKip Macy { 23473d585327SKip Macy 2348a69042a5SRobert Watson INP_RUNLOCK(inp); 23493d585327SKip Macy } 23503d585327SKip Macy 23513d585327SKip Macy #ifdef INVARIANTS 23523d585327SKip Macy void 2353e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 23543d585327SKip Macy { 23553d585327SKip Macy 23568501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 23573d585327SKip Macy } 23583d585327SKip Macy 23593d585327SKip Macy void 2360e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 23613d585327SKip Macy { 23623d585327SKip Macy 23633d585327SKip Macy INP_UNLOCK_ASSERT(inp); 23643d585327SKip Macy } 23653d585327SKip Macy #endif 23663d585327SKip Macy 23679378e437SKip Macy void 23689378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 23699378e437SKip Macy { 23709378e437SKip Macy struct inpcb *inp; 23719378e437SKip Macy 2372ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 237397021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 23749378e437SKip Macy INP_WLOCK(inp); 23759378e437SKip Macy func(inp, arg); 23769378e437SKip Macy INP_WUNLOCK(inp); 23779378e437SKip Macy } 2378ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 23799378e437SKip Macy } 23809378e437SKip Macy 23819378e437SKip Macy struct socket * 23829378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 23839378e437SKip Macy { 23849378e437SKip Macy 23859378e437SKip Macy INP_WLOCK_ASSERT(inp); 23869378e437SKip Macy return (inp->inp_socket); 23879378e437SKip Macy } 23889378e437SKip Macy 23899378e437SKip Macy struct tcpcb * 23909378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 23919378e437SKip Macy { 23929378e437SKip Macy 23939378e437SKip Macy INP_WLOCK_ASSERT(inp); 23949378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 23959378e437SKip Macy } 23969378e437SKip Macy 23979378e437SKip Macy int 23989378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 23999378e437SKip Macy { 24009378e437SKip Macy 24019378e437SKip Macy return (inp->inp_ip_tos); 24029378e437SKip Macy } 24039378e437SKip Macy 24049378e437SKip Macy void 24059378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 24069378e437SKip Macy { 24079378e437SKip Macy 24089378e437SKip Macy inp->inp_ip_tos = val; 24099378e437SKip Macy } 24109378e437SKip Macy 24119378e437SKip Macy void 2412df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 24139d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 24149378e437SKip Macy { 24159378e437SKip Macy 24169d29c635SKip Macy INP_LOCK_ASSERT(inp); 2417df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2418df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 24199378e437SKip Macy *lp = inp->inp_lport; 24209378e437SKip Macy *fp = inp->inp_fport; 24219378e437SKip Macy } 24229378e437SKip Macy 2423dd0e6c38SKip Macy struct inpcb * 2424dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2425dd0e6c38SKip Macy { 2426dd0e6c38SKip Macy 2427dd0e6c38SKip Macy return (sotoinpcb(so)); 2428dd0e6c38SKip Macy } 2429dd0e6c38SKip Macy 2430dd0e6c38SKip Macy struct tcpcb * 2431dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2432dd0e6c38SKip Macy { 2433dd0e6c38SKip Macy 2434dd0e6c38SKip Macy return (sototcpcb(so)); 2435dd0e6c38SKip Macy } 2436dd0e6c38SKip Macy 2437497057eeSRobert Watson #ifdef DDB 2438497057eeSRobert Watson static void 2439497057eeSRobert Watson db_print_indent(int indent) 2440497057eeSRobert Watson { 2441497057eeSRobert Watson int i; 2442497057eeSRobert Watson 2443497057eeSRobert Watson for (i = 0; i < indent; i++) 2444497057eeSRobert Watson db_printf(" "); 2445497057eeSRobert Watson } 2446497057eeSRobert Watson 2447497057eeSRobert Watson static void 2448497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2449497057eeSRobert Watson { 2450497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2451497057eeSRobert Watson 2452497057eeSRobert Watson db_print_indent(indent); 2453497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2454497057eeSRobert Watson 2455497057eeSRobert Watson indent += 2; 2456497057eeSRobert Watson 245703dc38a4SRobert Watson #ifdef INET6 2458dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2459497057eeSRobert Watson /* IPv6. */ 2460497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2461497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 246283e521ecSBjoern A. Zeeb } else 246303dc38a4SRobert Watson #endif 246483e521ecSBjoern A. Zeeb { 2465497057eeSRobert Watson /* IPv4. */ 2466497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2467497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2468497057eeSRobert Watson } 2469497057eeSRobert Watson db_print_indent(indent); 2470497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2471497057eeSRobert Watson ntohs(inc->inc_lport)); 2472497057eeSRobert Watson db_print_indent(indent); 2473497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2474497057eeSRobert Watson ntohs(inc->inc_fport)); 2475497057eeSRobert Watson } 2476497057eeSRobert Watson 2477497057eeSRobert Watson static void 2478497057eeSRobert Watson db_print_inpflags(int inp_flags) 2479497057eeSRobert Watson { 2480497057eeSRobert Watson int comma; 2481497057eeSRobert Watson 2482497057eeSRobert Watson comma = 0; 2483497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2484497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2485497057eeSRobert Watson comma = 1; 2486497057eeSRobert Watson } 2487497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2488497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2489497057eeSRobert Watson comma = 1; 2490497057eeSRobert Watson } 2491497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2492497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2493497057eeSRobert Watson comma = 1; 2494497057eeSRobert Watson } 2495ed55edceSErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2496ed55edceSErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2497ed55edceSErmal Luçi comma = 1; 2498ed55edceSErmal Luçi } 2499497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2500497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2501497057eeSRobert Watson comma = 1; 2502497057eeSRobert Watson } 2503497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2504497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2505497057eeSRobert Watson comma = 1; 2506497057eeSRobert Watson } 2507497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2508497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2509497057eeSRobert Watson comma = 1; 2510497057eeSRobert Watson } 2511497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2512497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2513497057eeSRobert Watson comma = 1; 2514497057eeSRobert Watson } 2515497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2516497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2517497057eeSRobert Watson comma = 1; 2518497057eeSRobert Watson } 2519497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2520497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2521497057eeSRobert Watson comma = 1; 2522497057eeSRobert Watson } 2523497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2524497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2525497057eeSRobert Watson comma = 1; 2526497057eeSRobert Watson } 2527497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2528497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2529497057eeSRobert Watson comma = 1; 2530497057eeSRobert Watson } 25313cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 25323cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 25333cca425bSMichael Tuexen comma = 1; 25343cca425bSMichael Tuexen } 2535497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2536497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2537497057eeSRobert Watson comma = 1; 2538497057eeSRobert Watson } 2539497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2540497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2541497057eeSRobert Watson comma = 1; 2542497057eeSRobert Watson } 2543497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2544497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2545497057eeSRobert Watson comma = 1; 2546497057eeSRobert Watson } 2547497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2548497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2549497057eeSRobert Watson comma = 1; 2550497057eeSRobert Watson } 2551497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2552497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2553497057eeSRobert Watson comma = 1; 2554497057eeSRobert Watson } 2555497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2556497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2557497057eeSRobert Watson comma = 1; 2558497057eeSRobert Watson } 2559497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2560497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2561497057eeSRobert Watson comma = 1; 2562497057eeSRobert Watson } 2563497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2564497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2565497057eeSRobert Watson comma = 1; 2566497057eeSRobert Watson } 2567497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2568497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2569497057eeSRobert Watson comma = 1; 2570497057eeSRobert Watson } 2571ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2572ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2573ad71fe3cSRobert Watson comma = 1; 2574ad71fe3cSRobert Watson } 2575ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2576ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2577ad71fe3cSRobert Watson comma = 1; 2578ad71fe3cSRobert Watson } 2579ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2580ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2581ad71fe3cSRobert Watson comma = 1; 2582ad71fe3cSRobert Watson } 2583ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2584ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2585ad71fe3cSRobert Watson comma = 1; 2586ad71fe3cSRobert Watson } 2587497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2588497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2589497057eeSRobert Watson comma = 1; 2590497057eeSRobert Watson } 2591497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2592497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2593497057eeSRobert Watson comma = 1; 2594497057eeSRobert Watson } 2595497057eeSRobert Watson } 2596497057eeSRobert Watson 2597497057eeSRobert Watson static void 2598497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2599497057eeSRobert Watson { 2600497057eeSRobert Watson int comma; 2601497057eeSRobert Watson 2602497057eeSRobert Watson comma = 0; 2603497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2604497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2605497057eeSRobert Watson comma = 1; 2606497057eeSRobert Watson } 2607497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2608497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2609497057eeSRobert Watson comma = 1; 2610497057eeSRobert Watson } 2611497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2612497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2613497057eeSRobert Watson comma = 1; 2614497057eeSRobert Watson } 2615497057eeSRobert Watson } 2616497057eeSRobert Watson 26176d888973SRobert Watson static void 2618497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2619497057eeSRobert Watson { 2620497057eeSRobert Watson 2621497057eeSRobert Watson db_print_indent(indent); 2622497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2623497057eeSRobert Watson 2624497057eeSRobert Watson indent += 2; 2625497057eeSRobert Watson 2626497057eeSRobert Watson db_print_indent(indent); 2627497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2628497057eeSRobert Watson 2629497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2630497057eeSRobert Watson 2631497057eeSRobert Watson db_print_indent(indent); 2632497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2633497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2634497057eeSRobert Watson 2635497057eeSRobert Watson db_print_indent(indent); 2636497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2637497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2638497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2639497057eeSRobert Watson db_printf(")\n"); 2640497057eeSRobert Watson 2641497057eeSRobert Watson db_print_indent(indent); 2642497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2643497057eeSRobert Watson inp->inp_vflag); 2644497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2645497057eeSRobert Watson db_printf(")\n"); 2646497057eeSRobert Watson 2647497057eeSRobert Watson db_print_indent(indent); 2648497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2649497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2650497057eeSRobert Watson 2651497057eeSRobert Watson db_print_indent(indent); 2652497057eeSRobert Watson #ifdef INET6 2653497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2654497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2655497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2656497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2657497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2658497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2659497057eeSRobert Watson inp->in6p_hops); 2660497057eeSRobert Watson } else 2661497057eeSRobert Watson #endif 2662497057eeSRobert Watson { 2663497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2664497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2665497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2666497057eeSRobert Watson } 2667497057eeSRobert Watson 2668497057eeSRobert Watson db_print_indent(indent); 2669497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2670497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 2671497057eeSRobert Watson } 2672497057eeSRobert Watson 2673497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2674497057eeSRobert Watson { 2675497057eeSRobert Watson struct inpcb *inp; 2676497057eeSRobert Watson 2677497057eeSRobert Watson if (!have_addr) { 2678497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 2679497057eeSRobert Watson return; 2680497057eeSRobert Watson } 2681497057eeSRobert Watson inp = (struct inpcb *)addr; 2682497057eeSRobert Watson 2683497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 2684497057eeSRobert Watson } 268583e521ecSBjoern A. Zeeb #endif /* DDB */ 2686f3e7afe2SHans Petter Selasky 2687f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 2688f3e7afe2SHans Petter Selasky /* 2689f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 2690f3e7afe2SHans Petter Selasky * if any. 2691f3e7afe2SHans Petter Selasky */ 2692f3e7afe2SHans Petter Selasky int 2693f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 2694f3e7afe2SHans Petter Selasky { 2695f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 2696f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2697f3e7afe2SHans Petter Selasky }; 2698f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2699f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2700f3e7afe2SHans Petter Selasky int error; 2701f3e7afe2SHans Petter Selasky 2702f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2703f3e7afe2SHans Petter Selasky if (mst == NULL) 2704f3e7afe2SHans Petter Selasky return (EINVAL); 2705f3e7afe2SHans Petter Selasky 2706f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2707f3e7afe2SHans Petter Selasky if (ifp == NULL) 2708f3e7afe2SHans Petter Selasky return (EINVAL); 2709f3e7afe2SHans Petter Selasky 2710f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 2711f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2712f3e7afe2SHans Petter Selasky } else { 2713f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 2714f3e7afe2SHans Petter Selasky } 2715f3e7afe2SHans Petter Selasky return (error); 2716f3e7afe2SHans Petter Selasky } 2717f3e7afe2SHans Petter Selasky 2718f3e7afe2SHans Petter Selasky /* 2719f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 2720f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 2721f3e7afe2SHans Petter Selasky */ 2722f3e7afe2SHans Petter Selasky int 2723f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 2724f3e7afe2SHans Petter Selasky { 2725f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 2726f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2727f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2728f3e7afe2SHans Petter Selasky int error; 2729f3e7afe2SHans Petter Selasky 2730f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2731f3e7afe2SHans Petter Selasky if (mst == NULL) 2732f3e7afe2SHans Petter Selasky return (EINVAL); 2733f3e7afe2SHans Petter Selasky 2734f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2735f3e7afe2SHans Petter Selasky if (ifp == NULL) 2736f3e7afe2SHans Petter Selasky return (EINVAL); 2737f3e7afe2SHans Petter Selasky 2738f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 2739f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2740f3e7afe2SHans Petter Selasky } else { 2741f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 2742f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 2743f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 2744f3e7afe2SHans Petter Selasky } 2745f3e7afe2SHans Petter Selasky return (error); 2746f3e7afe2SHans Petter Selasky } 2747f3e7afe2SHans Petter Selasky 2748f3e7afe2SHans Petter Selasky /* 2749f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 2750f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 2751f3e7afe2SHans Petter Selasky */ 2752f3e7afe2SHans Petter Selasky int 2753f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 2754f3e7afe2SHans Petter Selasky uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate) 2755f3e7afe2SHans Petter Selasky { 2756f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 2757f3e7afe2SHans Petter Selasky .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT, 2758f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 2759f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 2760f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2761f3e7afe2SHans Petter Selasky }; 2762f3e7afe2SHans Petter Selasky int error; 2763f3e7afe2SHans Petter Selasky 2764f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2765f3e7afe2SHans Petter Selasky 2766f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 2767f3e7afe2SHans Petter Selasky return (EINVAL); 2768f3e7afe2SHans Petter Selasky 2769f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 2770f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2771f3e7afe2SHans Petter Selasky } else { 2772f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 2773f3e7afe2SHans Petter Selasky 2774f3e7afe2SHans Petter Selasky /* 2775f3e7afe2SHans Petter Selasky * At success increment the refcount on 2776f3e7afe2SHans Petter Selasky * the send tag's network interface: 2777f3e7afe2SHans Petter Selasky */ 2778f3e7afe2SHans Petter Selasky if (error == 0) 2779f3e7afe2SHans Petter Selasky if_ref(inp->inp_snd_tag->ifp); 2780f3e7afe2SHans Petter Selasky } 2781f3e7afe2SHans Petter Selasky return (error); 2782f3e7afe2SHans Petter Selasky } 2783f3e7afe2SHans Petter Selasky 2784f3e7afe2SHans Petter Selasky /* 2785f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 2786f3e7afe2SHans Petter Selasky * if any: 2787f3e7afe2SHans Petter Selasky */ 2788f3e7afe2SHans Petter Selasky void 2789f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 2790f3e7afe2SHans Petter Selasky { 2791f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2792f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2793f3e7afe2SHans Petter Selasky 2794f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2795f3e7afe2SHans Petter Selasky 2796f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2797f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 2798f3e7afe2SHans Petter Selasky 2799f3e7afe2SHans Petter Selasky if (mst == NULL) 2800f3e7afe2SHans Petter Selasky return; 2801f3e7afe2SHans Petter Selasky 2802f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2803f3e7afe2SHans Petter Selasky if (ifp == NULL) 2804f3e7afe2SHans Petter Selasky return; 2805f3e7afe2SHans Petter Selasky 2806f3e7afe2SHans Petter Selasky /* 2807f3e7afe2SHans Petter Selasky * If the device was detached while we still had reference(s) 2808f3e7afe2SHans Petter Selasky * on the ifp, we assume if_snd_tag_free() was replaced with 2809f3e7afe2SHans Petter Selasky * stubs. 2810f3e7afe2SHans Petter Selasky */ 2811f3e7afe2SHans Petter Selasky ifp->if_snd_tag_free(mst); 2812f3e7afe2SHans Petter Selasky 2813f3e7afe2SHans Petter Selasky /* release reference count on network interface */ 2814f3e7afe2SHans Petter Selasky if_rele(ifp); 2815f3e7afe2SHans Petter Selasky } 2816f3e7afe2SHans Petter Selasky 2817f3e7afe2SHans Petter Selasky /* 2818f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 2819f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 2820f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 2821f3e7afe2SHans Petter Selasky */ 2822f3e7afe2SHans Petter Selasky void 2823f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 2824f3e7afe2SHans Petter Selasky { 2825f3e7afe2SHans Petter Selasky struct socket *socket; 2826f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 2827f3e7afe2SHans Petter Selasky bool did_upgrade; 2828f3e7afe2SHans Petter Selasky int error; 2829f3e7afe2SHans Petter Selasky 2830f3e7afe2SHans Petter Selasky if (inp == NULL) 2831f3e7afe2SHans Petter Selasky return; 2832f3e7afe2SHans Petter Selasky 2833f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 2834f3e7afe2SHans Petter Selasky if (socket == NULL) 2835f3e7afe2SHans Petter Selasky return; 2836f3e7afe2SHans Petter Selasky 2837f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 2838f3e7afe2SHans Petter Selasky /* 2839f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 2840f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 2841f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 2842f3e7afe2SHans Petter Selasky * write lock. 2843f3e7afe2SHans Petter Selasky */ 2844f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 2845f3e7afe2SHans Petter Selasky return; 2846f3e7afe2SHans Petter Selasky did_upgrade = 1; 2847f3e7afe2SHans Petter Selasky } else { 2848f3e7afe2SHans Petter Selasky did_upgrade = 0; 2849f3e7afe2SHans Petter Selasky } 2850f3e7afe2SHans Petter Selasky 2851f3e7afe2SHans Petter Selasky /* 2852f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 2853f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 2854f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 2855f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 2856f3e7afe2SHans Petter Selasky */ 2857f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 2858f3e7afe2SHans Petter Selasky 2859f3e7afe2SHans Petter Selasky /* 2860f3e7afe2SHans Petter Selasky * NOTE: When attaching to a network interface a reference is 2861f3e7afe2SHans Petter Selasky * made to ensure the network interface doesn't go away until 2862f3e7afe2SHans Petter Selasky * all ratelimit connections are gone. The network interface 2863f3e7afe2SHans Petter Selasky * pointers compared below represent valid network interfaces, 2864f3e7afe2SHans Petter Selasky * except when comparing towards NULL. 2865f3e7afe2SHans Petter Selasky */ 2866f3e7afe2SHans Petter Selasky if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 2867f3e7afe2SHans Petter Selasky error = 0; 2868f3e7afe2SHans Petter Selasky } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 2869f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 2870f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 2871f3e7afe2SHans Petter Selasky error = 0; 2872f3e7afe2SHans Petter Selasky } else if (inp->inp_snd_tag == NULL) { 2873f3e7afe2SHans Petter Selasky /* 2874f3e7afe2SHans Petter Selasky * In order to utilize packet pacing with RSS, we need 2875f3e7afe2SHans Petter Selasky * to wait until there is a valid RSS hash before we 2876f3e7afe2SHans Petter Selasky * can proceed: 2877f3e7afe2SHans Petter Selasky */ 2878f3e7afe2SHans Petter Selasky if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 2879f3e7afe2SHans Petter Selasky error = EAGAIN; 2880f3e7afe2SHans Petter Selasky } else { 2881f3e7afe2SHans Petter Selasky error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 2882f3e7afe2SHans Petter Selasky mb->m_pkthdr.flowid, max_pacing_rate); 2883f3e7afe2SHans Petter Selasky } 2884f3e7afe2SHans Petter Selasky } else { 2885f3e7afe2SHans Petter Selasky error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 2886f3e7afe2SHans Petter Selasky } 2887f3e7afe2SHans Petter Selasky if (error == 0 || error == EOPNOTSUPP) 2888f3e7afe2SHans Petter Selasky inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 2889f3e7afe2SHans Petter Selasky if (did_upgrade) 2890f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 2891f3e7afe2SHans Petter Selasky } 2892f3e7afe2SHans Petter Selasky 2893f3e7afe2SHans Petter Selasky /* 2894f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 2895f3e7afe2SHans Petter Selasky */ 2896f3e7afe2SHans Petter Selasky void 2897f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 2898f3e7afe2SHans Petter Selasky { 2899f3e7afe2SHans Petter Selasky struct socket *socket; 2900f3e7afe2SHans Petter Selasky bool did_upgrade; 2901f3e7afe2SHans Petter Selasky 2902f3e7afe2SHans Petter Selasky if (inp == NULL) 2903f3e7afe2SHans Petter Selasky return; 2904f3e7afe2SHans Petter Selasky 2905f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 2906f3e7afe2SHans Petter Selasky if (socket == NULL) 2907f3e7afe2SHans Petter Selasky return; 2908f3e7afe2SHans Petter Selasky 2909f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 2910f3e7afe2SHans Petter Selasky return; 2911f3e7afe2SHans Petter Selasky 2912f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 2913f3e7afe2SHans Petter Selasky /* 2914f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 2915f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 2916f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 2917f3e7afe2SHans Petter Selasky * write lock. 2918f3e7afe2SHans Petter Selasky */ 2919f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 2920f3e7afe2SHans Petter Selasky return; 2921f3e7afe2SHans Petter Selasky did_upgrade = 1; 2922f3e7afe2SHans Petter Selasky } else { 2923f3e7afe2SHans Petter Selasky did_upgrade = 0; 2924f3e7afe2SHans Petter Selasky } 2925f3e7afe2SHans Petter Selasky 2926f3e7afe2SHans Petter Selasky /* detach rate limiting */ 2927f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 2928f3e7afe2SHans Petter Selasky 2929f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 2930f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 2931f3e7afe2SHans Petter Selasky 2932f3e7afe2SHans Petter Selasky if (did_upgrade) 2933f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 2934f3e7afe2SHans Petter Selasky } 2935f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 2936