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. 19fbbd9655SWarner Losh * 3. 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 /* 215*cc487c16SGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 216*cc487c16SGleb Smirnoff * different name to the lock. But they all are disposed the same. 217*cc487c16SGleb Smirnoff */ 218*cc487c16SGleb Smirnoff static void 219*cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size) 220*cc487c16SGleb Smirnoff { 221*cc487c16SGleb Smirnoff struct inpcb *inp = mem; 222*cc487c16SGleb Smirnoff 223*cc487c16SGleb Smirnoff INP_LOCK_DESTROY(inp); 224*cc487c16SGleb Smirnoff } 225*cc487c16SGleb Smirnoff 226*cc487c16SGleb Smirnoff /* 2279bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 2289bcd427bSRobert Watson * arguments in time. 2299bcd427bSRobert Watson */ 2309bcd427bSRobert Watson void 2319bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 2329bcd427bSRobert Watson struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 233*cc487c16SGleb Smirnoff char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields) 2349bcd427bSRobert Watson { 2359bcd427bSRobert Watson 2369bcd427bSRobert Watson INP_INFO_LOCK_INIT(pcbinfo, name); 237fa046d87SRobert Watson INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 238ff9b006dSJulien Charbon INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist"); 2399bcd427bSRobert Watson #ifdef VIMAGE 2409bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 2419bcd427bSRobert Watson #endif 2429bcd427bSRobert Watson pcbinfo->ipi_listhead = listhead; 2439bcd427bSRobert Watson LIST_INIT(pcbinfo->ipi_listhead); 244fa046d87SRobert Watson pcbinfo->ipi_count = 0; 2459bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 2469bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 2479bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 2489bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 24952cd27cbSRobert Watson #ifdef PCBGROUP 25052cd27cbSRobert Watson in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 25152cd27cbSRobert Watson #endif 2529bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 253*cc487c16SGleb Smirnoff NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0); 2549bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 2556acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 2566acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 2579bcd427bSRobert Watson } 2589bcd427bSRobert Watson 2599bcd427bSRobert Watson /* 2609bcd427bSRobert Watson * Destroy an inpcbinfo. 2619bcd427bSRobert Watson */ 2629bcd427bSRobert Watson void 2639bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 2649bcd427bSRobert Watson { 2659bcd427bSRobert Watson 266fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 267fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 268fa046d87SRobert Watson 2699bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 2709bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 2719bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 27252cd27cbSRobert Watson #ifdef PCBGROUP 27352cd27cbSRobert Watson in_pcbgroup_destroy(pcbinfo); 27452cd27cbSRobert Watson #endif 2759bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 276ff9b006dSJulien Charbon INP_LIST_LOCK_DESTROY(pcbinfo); 277fa046d87SRobert Watson INP_HASH_LOCK_DESTROY(pcbinfo); 2789bcd427bSRobert Watson INP_INFO_LOCK_DESTROY(pcbinfo); 2799bcd427bSRobert Watson } 2809bcd427bSRobert Watson 2819bcd427bSRobert Watson /* 282c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 283d915b280SStephan Uphoff * On success return with the PCB locked. 284c3229e05SDavid Greenman */ 285df8bae1dSRodney W. Grimes int 286d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 287df8bae1dSRodney W. Grimes { 288136d4f1cSRobert Watson struct inpcb *inp; 28913cf67f3SHajimu UMEMOTO int error; 290a557af22SRobert Watson 291ff9b006dSJulien Charbon #ifdef INVARIANTS 292ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 293ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 294ff9b006dSJulien Charbon } else { 29559daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 296ff9b006dSJulien Charbon } 297ff9b006dSJulien Charbon #endif 298ff9b006dSJulien Charbon 299a557af22SRobert Watson error = 0; 300d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 301df8bae1dSRodney W. Grimes if (inp == NULL) 302df8bae1dSRodney W. Grimes return (ENOBUFS); 303d915b280SStephan Uphoff bzero(inp, inp_zero_size); 30415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 305df8bae1dSRodney W. Grimes inp->inp_socket = so; 30686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 3078b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 308a557af22SRobert Watson #ifdef MAC 30930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 310a557af22SRobert Watson if (error != 0) 311a557af22SRobert Watson goto out; 31230d239bcSRobert Watson mac_inpcb_create(so, inp); 313a557af22SRobert Watson #endif 314fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 315fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 3160bffde27SRobert Watson if (error != 0) { 3170bffde27SRobert Watson #ifdef MAC 3180bffde27SRobert Watson mac_inpcb_destroy(inp); 3190bffde27SRobert Watson #endif 320a557af22SRobert Watson goto out; 3210bffde27SRobert Watson } 322b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 323e3fd5ffdSRobert Watson #ifdef INET6 324340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 325340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 326603724d3SBjoern A. Zeeb if (V_ip6_v6only) 32733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 328340c35deSJonathan Lemon } 32975daea93SPaul Saab #endif 330ff9b006dSJulien Charbon INP_WLOCK(inp); 331ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 332712fc218SRobert Watson LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 3333d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 334df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 33533841545SHajimu UMEMOTO #ifdef INET6 336603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 33733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 33833841545SHajimu UMEMOTO #endif 339d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 34079bdc6e5SRobert Watson refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 3418c1960d5SMike Karels 3428c1960d5SMike Karels /* 3438c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 3448c1960d5SMike Karels * to be cleaned up. 3458c1960d5SMike Karels */ 3468c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 347ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 3487a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 349a557af22SRobert Watson out: 35086d02c5cSBjoern A. Zeeb if (error != 0) { 35186d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 352a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 35386d02c5cSBjoern A. Zeeb } 354a557af22SRobert Watson #endif 355a557af22SRobert Watson return (error); 356df8bae1dSRodney W. Grimes } 357df8bae1dSRodney W. Grimes 35867107f45SBjoern A. Zeeb #ifdef INET 359df8bae1dSRodney W. Grimes int 360136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 361df8bae1dSRodney W. Grimes { 3624b932371SIan Dowse int anonport, error; 3634b932371SIan Dowse 3648501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 365fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 36659daba27SSam Leffler 3674b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 3684b932371SIan Dowse return (EINVAL); 3695cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 3704b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 371b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 3724b932371SIan Dowse if (error) 3734b932371SIan Dowse return (error); 3744b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 3754b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 3764b932371SIan Dowse inp->inp_lport = 0; 3774b932371SIan Dowse return (EAGAIN); 3784b932371SIan Dowse } 3794b932371SIan Dowse if (anonport) 3804b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 3814b932371SIan Dowse return (0); 3824b932371SIan Dowse } 38367107f45SBjoern A. Zeeb #endif 3844b932371SIan Dowse 38539c8c62eSHiren Panchasara /* 38639c8c62eSHiren Panchasara * Select a local port (number) to use. 38739c8c62eSHiren Panchasara */ 388efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 389efc76f72SBjoern A. Zeeb int 3904616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 3914616026fSErmal Luçi struct ucred *cred, int lookupflags) 392efc76f72SBjoern A. Zeeb { 393efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 394efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 395efc76f72SBjoern A. Zeeb unsigned short *lastport; 396efc76f72SBjoern A. Zeeb int count, dorandom, error; 397efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 398efc76f72SBjoern A. Zeeb #ifdef INET 399efc76f72SBjoern A. Zeeb struct in_addr laddr; 400efc76f72SBjoern A. Zeeb #endif 401efc76f72SBjoern A. Zeeb 402efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 403efc76f72SBjoern A. Zeeb 404efc76f72SBjoern A. Zeeb /* 405efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 406efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 407efc76f72SBjoern A. Zeeb */ 408efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 409fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 410efc76f72SBjoern A. Zeeb 411efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 412efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 413efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 414efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 415efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 416efc76f72SBjoern A. Zeeb error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0); 417efc76f72SBjoern A. Zeeb if (error) 418efc76f72SBjoern A. Zeeb return (error); 419efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 420efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 421efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 422efc76f72SBjoern A. Zeeb } else { 423efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 424efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 425efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 426efc76f72SBjoern A. Zeeb } 427efc76f72SBjoern A. Zeeb /* 428e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 429efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 430efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 431efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 432efc76f72SBjoern A. Zeeb */ 433efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 434e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 435e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 436efc76f72SBjoern A. Zeeb dorandom = 1; 437efc76f72SBjoern A. Zeeb else 438efc76f72SBjoern A. Zeeb dorandom = 0; 439efc76f72SBjoern A. Zeeb /* 440efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 441efc76f72SBjoern A. Zeeb * we have the only port available. 442efc76f72SBjoern A. Zeeb */ 443efc76f72SBjoern A. Zeeb if (first == last) 444efc76f72SBjoern A. Zeeb dorandom = 0; 445e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 446e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 447efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 448efc76f72SBjoern A. Zeeb /* 449efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 450efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 451efc76f72SBjoern A. Zeeb * code path implementing all logic. 452efc76f72SBjoern A. Zeeb */ 453efc76f72SBjoern A. Zeeb if (first > last) { 454efc76f72SBjoern A. Zeeb aux = first; 455efc76f72SBjoern A. Zeeb first = last; 456efc76f72SBjoern A. Zeeb last = aux; 457efc76f72SBjoern A. Zeeb } 458efc76f72SBjoern A. Zeeb 459efc76f72SBjoern A. Zeeb #ifdef INET 460efc76f72SBjoern A. Zeeb /* Make the compiler happy. */ 461efc76f72SBjoern A. Zeeb laddr.s_addr = 0; 4624d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 463efc76f72SBjoern A. Zeeb KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 464efc76f72SBjoern A. Zeeb __func__, inp)); 465efc76f72SBjoern A. Zeeb laddr = *laddrp; 466efc76f72SBjoern A. Zeeb } 467efc76f72SBjoern A. Zeeb #endif 46867107f45SBjoern A. Zeeb tmpinp = NULL; /* Make compiler happy. */ 469efc76f72SBjoern A. Zeeb lport = *lportp; 470efc76f72SBjoern A. Zeeb 471efc76f72SBjoern A. Zeeb if (dorandom) 472efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 473efc76f72SBjoern A. Zeeb 474efc76f72SBjoern A. Zeeb count = last - first; 475efc76f72SBjoern A. Zeeb 476efc76f72SBjoern A. Zeeb do { 477efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 478efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 479efc76f72SBjoern A. Zeeb ++*lastport; 480efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 481efc76f72SBjoern A. Zeeb *lastport = first; 482efc76f72SBjoern A. Zeeb lport = htons(*lastport); 483efc76f72SBjoern A. Zeeb 484efc76f72SBjoern A. Zeeb #ifdef INET6 4854616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 486efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 48768e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 488efc76f72SBjoern A. Zeeb #endif 489efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 490efc76f72SBjoern A. Zeeb else 491efc76f72SBjoern A. Zeeb #endif 492efc76f72SBjoern A. Zeeb #ifdef INET 493efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 49468e0d7e0SRobert Watson lport, lookupflags, cred); 495efc76f72SBjoern A. Zeeb #endif 496efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 497efc76f72SBjoern A. Zeeb 498efc76f72SBjoern A. Zeeb #ifdef INET 4994d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 500efc76f72SBjoern A. Zeeb laddrp->s_addr = laddr.s_addr; 501efc76f72SBjoern A. Zeeb #endif 502efc76f72SBjoern A. Zeeb *lportp = lport; 503efc76f72SBjoern A. Zeeb 504efc76f72SBjoern A. Zeeb return (0); 505efc76f72SBjoern A. Zeeb } 506efdf104bSMikolaj Golub 507efdf104bSMikolaj Golub /* 508efdf104bSMikolaj Golub * Return cached socket options. 509efdf104bSMikolaj Golub */ 510efdf104bSMikolaj Golub short 511efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 512efdf104bSMikolaj Golub { 513efdf104bSMikolaj Golub short so_options; 514efdf104bSMikolaj Golub 515efdf104bSMikolaj Golub so_options = 0; 516efdf104bSMikolaj Golub 517efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 518efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 519efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 520efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 521efdf104bSMikolaj Golub return (so_options); 522efdf104bSMikolaj Golub } 523efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 524efc76f72SBjoern A. Zeeb 5254b932371SIan Dowse /* 5260a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 5270a100a6fSAdrian Chadd * 5280a100a6fSAdrian Chadd * ni points to the new inp. 5290a100a6fSAdrian Chadd * oi points to the exisitng inp. 5300a100a6fSAdrian Chadd * 5310a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 5320a100a6fSAdrian Chadd * whether the credentials match. 5330a100a6fSAdrian Chadd */ 534d5bb8bd3SAdrian Chadd int 5350a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 5360a100a6fSAdrian Chadd { 5370a100a6fSAdrian Chadd /* Check permissions match */ 5380a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5390a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 5400a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 5410a100a6fSAdrian Chadd return (0); 5420a100a6fSAdrian Chadd 5430a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 5440a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5450a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 5460a100a6fSAdrian Chadd return (0); 5470a100a6fSAdrian Chadd 5480a100a6fSAdrian Chadd /* 5490a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 5500a100a6fSAdrian Chadd * it is and it matches the checks. 5510a100a6fSAdrian Chadd */ 5520a100a6fSAdrian Chadd return (1); 5530a100a6fSAdrian Chadd } 5540a100a6fSAdrian Chadd 555d5bb8bd3SAdrian Chadd #ifdef INET 5560a100a6fSAdrian Chadd /* 5574b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 5584b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 5594b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 5604b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 5614b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 5624b932371SIan Dowse * 5634b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 5644b932371SIan Dowse */ 5654b932371SIan Dowse int 566136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 567136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 5684b932371SIan Dowse { 5694b932371SIan Dowse struct socket *so = inp->inp_socket; 57015bd2b43SDavid Greenman struct sockaddr_in *sin; 571c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 5724b932371SIan Dowse struct in_addr laddr; 573df8bae1dSRodney W. Grimes u_short lport = 0; 57468e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 575413628a7SBjoern A. Zeeb int error; 576df8bae1dSRodney W. Grimes 5778501a69cSRobert Watson /* 578fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 5798501a69cSRobert Watson */ 58059daba27SSam Leffler INP_LOCK_ASSERT(inp); 581fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 58259daba27SSam Leffler 583603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 584df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 5854b932371SIan Dowse laddr.s_addr = *laddrp; 5864b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 587df8bae1dSRodney W. Grimes return (EINVAL); 588c3229e05SDavid Greenman if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 58968e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 5904616026fSErmal Luçi if (nam == NULL) { 5917c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 5927c2f3cb9SJamie Gritton return (error); 5937c2f3cb9SJamie Gritton } else { 59457bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 59557bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 596df8bae1dSRodney W. Grimes return (EINVAL); 597df8bae1dSRodney W. Grimes #ifdef notdef 598df8bae1dSRodney W. Grimes /* 599df8bae1dSRodney W. Grimes * We should check the family, but old programs 600df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 601df8bae1dSRodney W. Grimes */ 602df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 603df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 604df8bae1dSRodney W. Grimes #endif 605b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 606b89e82ddSJamie Gritton if (error) 607b89e82ddSJamie Gritton return (error); 6084b932371SIan Dowse if (sin->sin_port != *lportp) { 6094b932371SIan Dowse /* Don't allow the port to change. */ 6104b932371SIan Dowse if (*lportp != 0) 6114b932371SIan Dowse return (EINVAL); 612df8bae1dSRodney W. Grimes lport = sin->sin_port; 6134b932371SIan Dowse } 6144b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 615df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 616df8bae1dSRodney W. Grimes /* 617df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 618df8bae1dSRodney W. Grimes * allow complete duplication of binding if 619df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 620df8bae1dSRodney W. Grimes * and a multicast address is bound on both 621df8bae1dSRodney W. Grimes * new and duplicated sockets. 622df8bae1dSRodney W. Grimes */ 623f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 624df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 625df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 626df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 62783103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 6284209e01aSAdrian Chadd /* 6294209e01aSAdrian Chadd * Is the address a local IP address? 630f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 6318696873dSAdrian Chadd * to any endpoint address, local or not. 6324209e01aSAdrian Chadd */ 633f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 6348896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 635df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 636df8bae1dSRodney W. Grimes } 6374b932371SIan Dowse laddr = sin->sin_addr; 638df8bae1dSRodney W. Grimes if (lport) { 639df8bae1dSRodney W. Grimes struct inpcb *t; 640ae0e7143SRobert Watson struct tcptw *tw; 641ae0e7143SRobert Watson 642df8bae1dSRodney W. Grimes /* GROSS */ 643603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 644603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 645acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 64632f9753cSRobert Watson 0)) 6472469dd60SGarrett Wollman return (EACCES); 648835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 64986d02c5cSBjoern A. Zeeb priv_check_cred(inp->inp_cred, 65032f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 651078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 652413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 653340c35deSJonathan Lemon /* 654340c35deSJonathan Lemon * XXX 655340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 656340c35deSJonathan Lemon */ 6574cc20ab1SSeigo Tanimura if (t && 6580a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 659ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 6604658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 6614658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 6624cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 66352b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 664fc06cd42SMikolaj Golub (t->inp_flags2 & INP_REUSEPORT) == 0) && 66586d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 66686d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 6674049a042SGuido van Rooij return (EADDRINUSE); 6680a100a6fSAdrian Chadd 6690a100a6fSAdrian Chadd /* 6700a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 6710a100a6fSAdrian Chadd * the credentials need to match and the 6720a100a6fSAdrian Chadd * original socket also has to have been bound 6730a100a6fSAdrian Chadd * with BINDMULTI. 6740a100a6fSAdrian Chadd */ 6750a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 6760a100a6fSAdrian Chadd return (EADDRINUSE); 6774049a042SGuido van Rooij } 678c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 67968e0d7e0SRobert Watson lport, lookupflags, cred); 680ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 681ae0e7143SRobert Watson /* 682ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 683ae0e7143SRobert Watson * state recycled, we treat the address as 684ae0e7143SRobert Watson * being in use (for now). This is better 685ae0e7143SRobert Watson * than a panic, but not desirable. 686ae0e7143SRobert Watson */ 687ec95b709SMikolaj Golub tw = intotw(t); 688ae0e7143SRobert Watson if (tw == NULL || 689ae0e7143SRobert Watson (reuseport & tw->tw_so_options) == 0) 690340c35deSJonathan Lemon return (EADDRINUSE); 6910a100a6fSAdrian Chadd } else if (t && 6920a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 6930a100a6fSAdrian Chadd (reuseport & inp_so_options(t)) == 0) { 694e3fd5ffdSRobert Watson #ifdef INET6 69533841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 696cfa1ca9dSYoshinobu Inoue INADDR_ANY || 697cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 698cfa1ca9dSYoshinobu Inoue INADDR_ANY || 699fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 700fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 701e3fd5ffdSRobert Watson #endif 702df8bae1dSRodney W. Grimes return (EADDRINUSE); 7030a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 7040a100a6fSAdrian Chadd return (EADDRINUSE); 705df8bae1dSRodney W. Grimes } 706cfa1ca9dSYoshinobu Inoue } 707df8bae1dSRodney W. Grimes } 7084b932371SIan Dowse if (*lportp != 0) 7094b932371SIan Dowse lport = *lportp; 71033b3ac06SPeter Wemm if (lport == 0) { 7114616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 712efc76f72SBjoern A. Zeeb if (error != 0) 713efc76f72SBjoern A. Zeeb return (error); 71433b3ac06SPeter Wemm 71533b3ac06SPeter Wemm } 7164b932371SIan Dowse *laddrp = laddr.s_addr; 7174b932371SIan Dowse *lportp = lport; 718df8bae1dSRodney W. Grimes return (0); 719df8bae1dSRodney W. Grimes } 720df8bae1dSRodney W. Grimes 721999f1343SGarrett Wollman /* 7225200e00eSIan Dowse * Connect from a socket to a specified address. 7235200e00eSIan Dowse * Both address and port must be specified in argument sin. 7245200e00eSIan Dowse * If don't have a local address for this socket yet, 7255200e00eSIan Dowse * then pick one. 726999f1343SGarrett Wollman */ 727999f1343SGarrett Wollman int 728d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 729d3c1f003SRobert Watson struct ucred *cred, struct mbuf *m) 730999f1343SGarrett Wollman { 7315200e00eSIan Dowse u_short lport, fport; 7325200e00eSIan Dowse in_addr_t laddr, faddr; 7335200e00eSIan Dowse int anonport, error; 734df8bae1dSRodney W. Grimes 7358501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 736fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 73727f74fd0SRobert Watson 7385200e00eSIan Dowse lport = inp->inp_lport; 7395200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 7405200e00eSIan Dowse anonport = (lport == 0); 7415200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 742b0330ed9SPawel Jakub Dawidek NULL, cred); 7435200e00eSIan Dowse if (error) 7445200e00eSIan Dowse return (error); 7455200e00eSIan Dowse 7465200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 7475200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 7485200e00eSIan Dowse inp->inp_lport = lport; 7495200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7505200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 7515200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 7525200e00eSIan Dowse inp->inp_lport = 0; 7535200e00eSIan Dowse return (EAGAIN); 7545200e00eSIan Dowse } 7555200e00eSIan Dowse } 7565200e00eSIan Dowse 7575200e00eSIan Dowse /* Commit the remaining changes. */ 7585200e00eSIan Dowse inp->inp_lport = lport; 7595200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7605200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 7615200e00eSIan Dowse inp->inp_fport = fport; 762d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 7632cb64cb2SGeorge V. Neville-Neil 7645200e00eSIan Dowse if (anonport) 7655200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 7665200e00eSIan Dowse return (0); 7675200e00eSIan Dowse } 7685200e00eSIan Dowse 769d3c1f003SRobert Watson int 770d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 771d3c1f003SRobert Watson { 772d3c1f003SRobert Watson 773d3c1f003SRobert Watson return (in_pcbconnect_mbuf(inp, nam, cred, NULL)); 774d3c1f003SRobert Watson } 775d3c1f003SRobert Watson 7765200e00eSIan Dowse /* 7770895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 7780895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 7790895aec3SBjoern A. Zeeb */ 780ae190832SSteven Hartland int 7810895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 7820895aec3SBjoern A. Zeeb struct ucred *cred) 7830895aec3SBjoern A. Zeeb { 7840895aec3SBjoern A. Zeeb struct ifaddr *ifa; 7850895aec3SBjoern A. Zeeb struct sockaddr *sa; 7860895aec3SBjoern A. Zeeb struct sockaddr_in *sin; 7870895aec3SBjoern A. Zeeb struct route sro; 7880895aec3SBjoern A. Zeeb int error; 7890895aec3SBjoern A. Zeeb 790413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 7910895aec3SBjoern A. Zeeb 792592bcae8SBjoern A. Zeeb /* 793592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 794592bcae8SBjoern A. Zeeb * if requested. 795592bcae8SBjoern A. Zeeb */ 796592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 797592bcae8SBjoern A. Zeeb return (0); 798592bcae8SBjoern A. Zeeb 7990895aec3SBjoern A. Zeeb error = 0; 8000895aec3SBjoern A. Zeeb bzero(&sro, sizeof(sro)); 8010895aec3SBjoern A. Zeeb 8020895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)&sro.ro_dst; 8030895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 8040895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 8050895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 8060895aec3SBjoern A. Zeeb 8070895aec3SBjoern A. Zeeb /* 8080895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 8090895aec3SBjoern A. Zeeb * else punt. 8100895aec3SBjoern A. Zeeb * 8110895aec3SBjoern A. Zeeb * Find out route to destination. 8120895aec3SBjoern A. Zeeb */ 8130895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 8146e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 8150895aec3SBjoern A. Zeeb 8160895aec3SBjoern A. Zeeb /* 8170895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 8180895aec3SBjoern A. Zeeb * the outgoing interface. 8190895aec3SBjoern A. Zeeb * 8200895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 8210895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 8220895aec3SBjoern A. Zeeb * the source address from. 8230895aec3SBjoern A. Zeeb */ 8240895aec3SBjoern A. Zeeb if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 8258c0fec80SRobert Watson struct in_ifaddr *ia; 8260895aec3SBjoern A. Zeeb struct ifnet *ifp; 8270895aec3SBjoern A. Zeeb 8284f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 82958a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 8300895aec3SBjoern A. Zeeb if (ia == NULL) 8314f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 83258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 8330895aec3SBjoern A. Zeeb if (ia == NULL) { 8340895aec3SBjoern A. Zeeb error = ENETUNREACH; 8350895aec3SBjoern A. Zeeb goto done; 8360895aec3SBjoern A. Zeeb } 8370895aec3SBjoern A. Zeeb 8380304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8390895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8408c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8410895aec3SBjoern A. Zeeb goto done; 8420895aec3SBjoern A. Zeeb } 8430895aec3SBjoern A. Zeeb 8440895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 8458c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8460895aec3SBjoern A. Zeeb ia = NULL; 847137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 8480895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8490895aec3SBjoern A. Zeeb 8500895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 8510895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 8520895aec3SBjoern A. Zeeb continue; 8530895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 854b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8550895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 8560895aec3SBjoern A. Zeeb break; 8570895aec3SBjoern A. Zeeb } 8580895aec3SBjoern A. Zeeb } 8590895aec3SBjoern A. Zeeb if (ia != NULL) { 8600895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 861137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8620895aec3SBjoern A. Zeeb goto done; 8630895aec3SBjoern A. Zeeb } 864137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8650895aec3SBjoern A. Zeeb 8660895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 867b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 8680895aec3SBjoern A. Zeeb goto done; 8690895aec3SBjoern A. Zeeb } 8700895aec3SBjoern A. Zeeb 8710895aec3SBjoern A. Zeeb /* 8720895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 8730895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 8740895aec3SBjoern A. Zeeb * In case of jails do those three steps: 8750895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 8760895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 8770895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 8780895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 8790895aec3SBjoern A. Zeeb */ 8800895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 8818c0fec80SRobert Watson struct in_ifaddr *ia; 8829317b04eSRobert Watson struct ifnet *ifp; 8830895aec3SBjoern A. Zeeb 8840895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 8850304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8860895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8870895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8880895aec3SBjoern A. Zeeb goto done; 8890895aec3SBjoern A. Zeeb } 8900895aec3SBjoern A. Zeeb 8910895aec3SBjoern A. Zeeb /* Jailed. */ 8920895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 8930895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 894b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8950895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8960895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8970895aec3SBjoern A. Zeeb goto done; 8980895aec3SBjoern A. Zeeb } 8990895aec3SBjoern A. Zeeb 9000895aec3SBjoern A. Zeeb /* 9010895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 9020895aec3SBjoern A. Zeeb * belonging to this jail. 9030895aec3SBjoern A. Zeeb */ 9048c0fec80SRobert Watson ia = NULL; 9059317b04eSRobert Watson ifp = sro.ro_rt->rt_ifp; 906137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 9079317b04eSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 9080895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 9090895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 9100895aec3SBjoern A. Zeeb continue; 9110895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 912b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 9130895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 9140895aec3SBjoern A. Zeeb break; 9150895aec3SBjoern A. Zeeb } 9160895aec3SBjoern A. Zeeb } 9170895aec3SBjoern A. Zeeb if (ia != NULL) { 9180895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 919137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9200895aec3SBjoern A. Zeeb goto done; 9210895aec3SBjoern A. Zeeb } 922137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9230895aec3SBjoern A. Zeeb 9240895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 925b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 9260895aec3SBjoern A. Zeeb goto done; 9270895aec3SBjoern A. Zeeb } 9280895aec3SBjoern A. Zeeb 9290895aec3SBjoern A. Zeeb /* 9300895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 9310895aec3SBjoern A. Zeeb * to ourselves is here. 9320895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 9330895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 9340895aec3SBjoern A. Zeeb * a loopback interface. 9350895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 9360895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 9370895aec3SBjoern A. Zeeb */ 9380895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 9390895aec3SBjoern A. Zeeb struct sockaddr_in sain; 9408c0fec80SRobert Watson struct in_ifaddr *ia; 9410895aec3SBjoern A. Zeeb 9420895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 9430895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 9440895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 9450895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 9460895aec3SBjoern A. Zeeb 94758a39d8cSAlan Somers ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain), 94858a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 9490895aec3SBjoern A. Zeeb if (ia == NULL) 9504f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0, 95158a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 952f0bb05fcSQing Li if (ia == NULL) 953f0bb05fcSQing Li ia = ifatoia(ifa_ifwithaddr(sintosa(&sain))); 9540895aec3SBjoern A. Zeeb 9550304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 9560895aec3SBjoern A. Zeeb if (ia == NULL) { 9570895aec3SBjoern A. Zeeb error = ENETUNREACH; 9580895aec3SBjoern A. Zeeb goto done; 9590895aec3SBjoern A. Zeeb } 9600895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 9618c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9620895aec3SBjoern A. Zeeb goto done; 9630895aec3SBjoern A. Zeeb } 9640895aec3SBjoern A. Zeeb 9650895aec3SBjoern A. Zeeb /* Jailed. */ 9660895aec3SBjoern A. Zeeb if (ia != NULL) { 9670895aec3SBjoern A. Zeeb struct ifnet *ifp; 9680895aec3SBjoern A. Zeeb 9690895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 9708c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9710895aec3SBjoern A. Zeeb ia = NULL; 972137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 9730895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 9740895aec3SBjoern A. Zeeb 9750895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 9760895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 9770895aec3SBjoern A. Zeeb continue; 9780895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 979b89e82ddSJamie Gritton if (prison_check_ip4(cred, 980b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 9810895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 9820895aec3SBjoern A. Zeeb break; 9830895aec3SBjoern A. Zeeb } 9840895aec3SBjoern A. Zeeb } 9850895aec3SBjoern A. Zeeb if (ia != NULL) { 9860895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 987137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9880895aec3SBjoern A. Zeeb goto done; 9890895aec3SBjoern A. Zeeb } 990137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9910895aec3SBjoern A. Zeeb } 9920895aec3SBjoern A. Zeeb 9930895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 994b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 9950895aec3SBjoern A. Zeeb goto done; 9960895aec3SBjoern A. Zeeb } 9970895aec3SBjoern A. Zeeb 9980895aec3SBjoern A. Zeeb done: 9990895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 10000895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 10010895aec3SBjoern A. Zeeb return (error); 10020895aec3SBjoern A. Zeeb } 10030895aec3SBjoern A. Zeeb 10040895aec3SBjoern A. Zeeb /* 10055200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 10065200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 10075200e00eSIan Dowse * address and port for the PCB; these are updated to the values 10085200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 10095200e00eSIan Dowse * the connect. 10105200e00eSIan Dowse * 10115200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 10125200e00eSIan Dowse * and port. These are not updated in the error case. 10135200e00eSIan Dowse * 10145200e00eSIan Dowse * If the operation fails because the connection already exists, 10155200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 10165200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 10175200e00eSIan Dowse * is set to NULL. 10185200e00eSIan Dowse */ 10195200e00eSIan Dowse int 1020136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1021136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1022136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 10235200e00eSIan Dowse { 1024cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 10255200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 10265200e00eSIan Dowse struct in_ifaddr *ia; 10275200e00eSIan Dowse struct inpcb *oinp; 1028b89e82ddSJamie Gritton struct in_addr laddr, faddr; 10295200e00eSIan Dowse u_short lport, fport; 10305200e00eSIan Dowse int error; 10315200e00eSIan Dowse 10328501a69cSRobert Watson /* 10338501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 10348501a69cSRobert Watson * lock is sufficient. 10358501a69cSRobert Watson */ 103627f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1037fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 103827f74fd0SRobert Watson 10395200e00eSIan Dowse if (oinpp != NULL) 10405200e00eSIan Dowse *oinpp = NULL; 104157bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1042df8bae1dSRodney W. Grimes return (EINVAL); 1043df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1044df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1045df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1046df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 10475200e00eSIan Dowse laddr.s_addr = *laddrp; 10485200e00eSIan Dowse lport = *lportp; 10495200e00eSIan Dowse faddr = sin->sin_addr; 10505200e00eSIan Dowse fport = sin->sin_port; 10510895aec3SBjoern A. Zeeb 1052603724d3SBjoern A. Zeeb if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 1053df8bae1dSRodney W. Grimes /* 1054df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1055df8bae1dSRodney W. Grimes * use the primary local address. 1056df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1057df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1058df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1059df8bae1dSRodney W. Grimes */ 1060413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1061cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1062413628a7SBjoern A. Zeeb faddr = 1063b89e82ddSJamie Gritton IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1064cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1065b89e82ddSJamie Gritton if (cred != NULL && 1066b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1067b89e82ddSJamie Gritton return (error); 10682d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1069cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 10702d9cfabaSRobert Watson if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 10712d9cfabaSRobert Watson IFF_BROADCAST) 10725200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 1073603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1074cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 10752d9cfabaSRobert Watson } 1076df8bae1dSRodney W. Grimes } 10775200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1078d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1079df8bae1dSRodney W. Grimes /* 1080df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1081d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1082df8bae1dSRodney W. Grimes * address of that interface as our source address. 1083df8bae1dSRodney W. Grimes */ 10845200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1085df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1086df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1087df8bae1dSRodney W. Grimes struct ifnet *ifp; 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1090df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1091df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1092cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1093e691be70SDaniel Eischen TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1094e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1095e691be70SDaniel Eischen (cred == NULL || 1096e691be70SDaniel Eischen prison_check_ip4(cred, 1097e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1098df8bae1dSRodney W. Grimes break; 1099e691be70SDaniel Eischen } 1100e691be70SDaniel Eischen if (ia == NULL) 1101d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1102e691be70SDaniel Eischen else { 11035200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1104d79fdd98SDaniel Eischen error = 0; 1105999f1343SGarrett Wollman } 1106cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1107d79fdd98SDaniel Eischen } 1108d79fdd98SDaniel Eischen } 110904215ed2SRandall Stewart if (error) 111004215ed2SRandall Stewart return (error); 11110895aec3SBjoern A. Zeeb } 1112fa046d87SRobert Watson oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1113fa046d87SRobert Watson laddr, lport, 0, NULL); 11145200e00eSIan Dowse if (oinp != NULL) { 11155200e00eSIan Dowse if (oinpp != NULL) 11165200e00eSIan Dowse *oinpp = oinp; 1117df8bae1dSRodney W. Grimes return (EADDRINUSE); 1118c3229e05SDavid Greenman } 11195200e00eSIan Dowse if (lport == 0) { 1120b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1121b0330ed9SPawel Jakub Dawidek cred); 11225a903f8dSPierre Beyssac if (error) 11235a903f8dSPierre Beyssac return (error); 11245a903f8dSPierre Beyssac } 11255200e00eSIan Dowse *laddrp = laddr.s_addr; 11265200e00eSIan Dowse *lportp = lport; 11275200e00eSIan Dowse *faddrp = faddr.s_addr; 11285200e00eSIan Dowse *fportp = fport; 1129df8bae1dSRodney W. Grimes return (0); 1130df8bae1dSRodney W. Grimes } 1131df8bae1dSRodney W. Grimes 113226f9a767SRodney W. Grimes void 1133136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1134df8bae1dSRodney W. Grimes { 11356b348152SRobert Watson 11368501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1137fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1138df8bae1dSRodney W. Grimes 1139df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1140df8bae1dSRodney W. Grimes inp->inp_fport = 0; 114115bd2b43SDavid Greenman in_pcbrehash(inp); 1142df8bae1dSRodney W. Grimes } 114383e521ecSBjoern A. Zeeb #endif /* INET */ 1144df8bae1dSRodney W. Grimes 11454c7c478dSRobert Watson /* 114628696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1147c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 114828696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 114928696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 11504c7c478dSRobert Watson */ 115126f9a767SRodney W. Grimes void 1152136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1153df8bae1dSRodney W. Grimes { 11544c7c478dSRobert Watson 1155a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1156c0a211c5SRobert Watson 1157f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1158f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1159f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1160f3e7afe2SHans Petter Selasky #endif 11614c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 11624c7c478dSRobert Watson inp->inp_socket = NULL; 11634c7c478dSRobert Watson } 11644c7c478dSRobert Watson 1165c0a211c5SRobert Watson /* 116679bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 116779bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 116879bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 116952cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 117052cd27cbSRobert Watson * via a pcbgroup. 117179bdc6e5SRobert Watson * 117279bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 117379bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 117479bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 117579bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 117679bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 117779bdc6e5SRobert Watson * inpcb. 117879bdc6e5SRobert Watson * 117979bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 118079bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 118179bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 118279bdc6e5SRobert Watson * using in_pcbrele(). 1183c0a211c5SRobert Watson */ 118479bdc6e5SRobert Watson void 118579bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 11864c7c478dSRobert Watson { 1187df8bae1dSRodney W. Grimes 118879bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 118979bdc6e5SRobert Watson 119079bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 119179bdc6e5SRobert Watson } 119279bdc6e5SRobert Watson 119379bdc6e5SRobert Watson /* 119479bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 119579bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 119679bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 119779bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 119879bdc6e5SRobert Watson * 119979bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 120079bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 120179bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 120279bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 120379bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 120479bdc6e5SRobert Watson */ 120579bdc6e5SRobert Watson int 120679bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 120779bdc6e5SRobert Watson { 120879bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 120979bdc6e5SRobert Watson 121079bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 121179bdc6e5SRobert Watson 121279bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 121379bdc6e5SRobert Watson 1214df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1215df4e91d3SGleb Smirnoff /* 1216df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1217df4e91d3SGleb Smirnoff * this isn't the last reference. 1218df4e91d3SGleb Smirnoff */ 1219df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1220df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1221df4e91d3SGleb Smirnoff return (1); 1222df4e91d3SGleb Smirnoff } 122379bdc6e5SRobert Watson return (0); 1224df4e91d3SGleb Smirnoff } 122579bdc6e5SRobert Watson 122679bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 122779bdc6e5SRobert Watson 122879bdc6e5SRobert Watson INP_RUNLOCK(inp); 122979bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 123079bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 123179bdc6e5SRobert Watson return (1); 123279bdc6e5SRobert Watson } 123379bdc6e5SRobert Watson 123479bdc6e5SRobert Watson int 123579bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 123679bdc6e5SRobert Watson { 123779bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 123879bdc6e5SRobert Watson 123979bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 124079bdc6e5SRobert Watson 124179bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 124279bdc6e5SRobert Watson 1243edd0e0b0SFabien Thomas if (refcount_release(&inp->inp_refcount) == 0) { 1244edd0e0b0SFabien Thomas /* 1245edd0e0b0SFabien Thomas * If the inpcb has been freed, let the caller know, even if 1246edd0e0b0SFabien Thomas * this isn't the last reference. 1247edd0e0b0SFabien Thomas */ 1248edd0e0b0SFabien Thomas if (inp->inp_flags2 & INP_FREED) { 1249edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1250edd0e0b0SFabien Thomas return (1); 1251edd0e0b0SFabien Thomas } 125279bdc6e5SRobert Watson return (0); 1253edd0e0b0SFabien Thomas } 125479bdc6e5SRobert Watson 125579bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 125679bdc6e5SRobert Watson 125779bdc6e5SRobert Watson INP_WUNLOCK(inp); 125879bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 125979bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 126079bdc6e5SRobert Watson return (1); 126179bdc6e5SRobert Watson } 126279bdc6e5SRobert Watson 126379bdc6e5SRobert Watson /* 126479bdc6e5SRobert Watson * Temporary wrapper. 126579bdc6e5SRobert Watson */ 126679bdc6e5SRobert Watson int 126779bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 126879bdc6e5SRobert Watson { 126979bdc6e5SRobert Watson 127079bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 127179bdc6e5SRobert Watson } 127279bdc6e5SRobert Watson 127379bdc6e5SRobert Watson /* 127479bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 127579bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 127679bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 127779bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 127879bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 127979bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 128079bdc6e5SRobert Watson * the pcbinfo lock is held. 128179bdc6e5SRobert Watson */ 128279bdc6e5SRobert Watson void 128379bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 128479bdc6e5SRobert Watson { 128579bdc6e5SRobert Watson struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 128679bdc6e5SRobert Watson 128779bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 128879bdc6e5SRobert Watson 1289ff9b006dSJulien Charbon #ifdef INVARIANTS 1290ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 1291079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(pcbinfo); 1292ff9b006dSJulien Charbon } else { 129379bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 1294ff9b006dSJulien Charbon } 1295ff9b006dSJulien Charbon #endif 129679bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 129779bdc6e5SRobert Watson 129879bdc6e5SRobert Watson /* XXXRW: Do as much as possible here. */ 1299fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 13006aee2fc5SBjoern A. Zeeb if (inp->inp_sp != NULL) 13016974bd9eSBjoern A. Zeeb ipsec_delete_pcbpolicy(inp); 130283e521ecSBjoern A. Zeeb #endif 1303ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 130479bdc6e5SRobert Watson inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1305c3229e05SDavid Greenman in_pcbremlists(inp); 1306ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 13076aee2fc5SBjoern A. Zeeb #ifdef INET6 13086aee2fc5SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 13096aee2fc5SBjoern A. Zeeb ip6_freepcbopts(inp->in6p_outputopts); 13101096332aSBruce M Simpson if (inp->in6p_moptions != NULL) 13116aee2fc5SBjoern A. Zeeb ip6_freemoptions(inp->in6p_moptions); 13126aee2fc5SBjoern A. Zeeb } 13136aee2fc5SBjoern A. Zeeb #endif 1314df8bae1dSRodney W. Grimes if (inp->inp_options) 1315df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 131667107f45SBjoern A. Zeeb #ifdef INET 131771498f30SBruce M Simpson if (inp->inp_moptions != NULL) 131871498f30SBruce M Simpson inp_freemoptions(inp->inp_moptions); 131967107f45SBjoern A. Zeeb #endif 1320cc94f0c2SGleb Smirnoff RO_RTFREE(&inp->inp_route); 13216d768226SGeorge V. Neville-Neil if (inp->inp_route.ro_lle) 13226d768226SGeorge V. Neville-Neil LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */ 132384cc0778SGeorge V. Neville-Neil 1324cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 1325df4e91d3SGleb Smirnoff inp->inp_flags2 |= INP_FREED; 132686d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 1327a557af22SRobert Watson #ifdef MAC 132830d239bcSRobert Watson mac_inpcb_destroy(inp); 1329a557af22SRobert Watson #endif 133079bdc6e5SRobert Watson if (!in_pcbrele_wlocked(inp)) 133128696211SRobert Watson INP_WUNLOCK(inp); 133228696211SRobert Watson } 133328696211SRobert Watson 133428696211SRobert Watson /* 1335c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1336c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1337c0a211c5SRobert Watson * 1338c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1339c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1340c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1341c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1342c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1343c0a211c5SRobert Watson * in_pcbdetach(). 1344c0a211c5SRobert Watson * 1345c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1346c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 134710702a28SRobert Watson */ 134810702a28SRobert Watson void 134910702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 135010702a28SRobert Watson { 135110702a28SRobert Watson 13528501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 135310702a28SRobert Watson 1354fa046d87SRobert Watson /* 1355fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1356fa046d87SRobert Watson * the hash lock...? 1357fa046d87SRobert Watson */ 1358ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1359111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 136010702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 136110702a28SRobert Watson 1362fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 136310702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 136410702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 136510702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 136610702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 136710702a28SRobert Watson free(phd, M_PCB); 136810702a28SRobert Watson } 1369fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1370111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 137152cd27cbSRobert Watson #ifdef PCBGROUP 137252cd27cbSRobert Watson in_pcbgroup_remove(inp); 137352cd27cbSRobert Watson #endif 137410702a28SRobert Watson } 137510702a28SRobert Watson } 137610702a28SRobert Watson 137767107f45SBjoern A. Zeeb #ifdef INET 137854d642bbSRobert Watson /* 137954d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 138054d642bbSRobert Watson */ 138126ef6ac4SDon Lewis struct sockaddr * 1382136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 138326ef6ac4SDon Lewis { 138426ef6ac4SDon Lewis struct sockaddr_in *sin; 138526ef6ac4SDon Lewis 13861ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1387a163d034SWarner Losh M_WAITOK | M_ZERO); 138826ef6ac4SDon Lewis sin->sin_family = AF_INET; 138926ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 139026ef6ac4SDon Lewis sin->sin_addr = *addr_p; 139126ef6ac4SDon Lewis sin->sin_port = port; 139226ef6ac4SDon Lewis 139326ef6ac4SDon Lewis return (struct sockaddr *)sin; 139426ef6ac4SDon Lewis } 139526ef6ac4SDon Lewis 1396117bcae7SGarrett Wollman int 139754d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1398df8bae1dSRodney W. Grimes { 1399136d4f1cSRobert Watson struct inpcb *inp; 140026ef6ac4SDon Lewis struct in_addr addr; 140126ef6ac4SDon Lewis in_port_t port; 140242fa505bSDavid Greenman 1403fdc984f7STor Egge inp = sotoinpcb(so); 140454d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 14056466b28aSRobert Watson 1406a69042a5SRobert Watson INP_RLOCK(inp); 140726ef6ac4SDon Lewis port = inp->inp_lport; 140826ef6ac4SDon Lewis addr = inp->inp_laddr; 1409a69042a5SRobert Watson INP_RUNLOCK(inp); 141042fa505bSDavid Greenman 141126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1412117bcae7SGarrett Wollman return 0; 1413df8bae1dSRodney W. Grimes } 1414df8bae1dSRodney W. Grimes 1415117bcae7SGarrett Wollman int 141654d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1417df8bae1dSRodney W. Grimes { 1418136d4f1cSRobert Watson struct inpcb *inp; 141926ef6ac4SDon Lewis struct in_addr addr; 142026ef6ac4SDon Lewis in_port_t port; 142142fa505bSDavid Greenman 1422fdc984f7STor Egge inp = sotoinpcb(so); 142354d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 14246466b28aSRobert Watson 1425a69042a5SRobert Watson INP_RLOCK(inp); 142626ef6ac4SDon Lewis port = inp->inp_fport; 142726ef6ac4SDon Lewis addr = inp->inp_faddr; 1428a69042a5SRobert Watson INP_RUNLOCK(inp); 142942fa505bSDavid Greenman 143026ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1431117bcae7SGarrett Wollman return 0; 1432df8bae1dSRodney W. Grimes } 1433df8bae1dSRodney W. Grimes 143426f9a767SRodney W. Grimes void 1435136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1436136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1437d1c54148SJesper Skriver { 1438f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1439d1c54148SJesper Skriver 14403dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1441f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 14428501a69cSRobert Watson INP_WLOCK(inp); 1443d1c54148SJesper Skriver #ifdef INET6 1444f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 14458501a69cSRobert Watson INP_WUNLOCK(inp); 1446d1c54148SJesper Skriver continue; 1447f76fcf6dSJeffrey Hsu } 1448d1c54148SJesper Skriver #endif 1449d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1450f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 14518501a69cSRobert Watson INP_WUNLOCK(inp); 1452d1c54148SJesper Skriver continue; 1453d1c54148SJesper Skriver } 14543dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 14558501a69cSRobert Watson INP_WUNLOCK(inp); 1456f76fcf6dSJeffrey Hsu } 14573dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1458d1c54148SJesper Skriver } 1459d1c54148SJesper Skriver 1460e43cc4aeSHajimu UMEMOTO void 1461136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1462e43cc4aeSHajimu UMEMOTO { 1463e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1464e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1465e43cc4aeSHajimu UMEMOTO int i, gap; 1466e43cc4aeSHajimu UMEMOTO 1467ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1468712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 14698501a69cSRobert Watson INP_WLOCK(inp); 1470e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1471e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1472e43cc4aeSHajimu UMEMOTO imo != NULL) { 1473e43cc4aeSHajimu UMEMOTO /* 1474e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1475e43cc4aeSHajimu UMEMOTO * detached. 1476e43cc4aeSHajimu UMEMOTO */ 1477e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1478e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1479e43cc4aeSHajimu UMEMOTO 1480e43cc4aeSHajimu UMEMOTO /* 1481e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1482e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1483e43cc4aeSHajimu UMEMOTO */ 1484e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1485e43cc4aeSHajimu UMEMOTO i++) { 1486e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1487e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 1488e43cc4aeSHajimu UMEMOTO gap++; 1489e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1490e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1491e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1492e43cc4aeSHajimu UMEMOTO } 1493e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1494e43cc4aeSHajimu UMEMOTO } 14958501a69cSRobert Watson INP_WUNLOCK(inp); 1496e43cc4aeSHajimu UMEMOTO } 1497ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1498e43cc4aeSHajimu UMEMOTO } 1499e43cc4aeSHajimu UMEMOTO 1500df8bae1dSRodney W. Grimes /* 1501fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1502fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1503c3229e05SDavid Greenman */ 1504d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1505df8bae1dSRodney W. Grimes struct inpcb * 1506136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 150768e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1508df8bae1dSRodney W. Grimes { 1509136d4f1cSRobert Watson struct inpcb *inp; 1510d5e8a67eSHajimu UMEMOTO #ifdef INET6 1511d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1512d5e8a67eSHajimu UMEMOTO #else 1513d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1514d5e8a67eSHajimu UMEMOTO #endif 1515d5e8a67eSHajimu UMEMOTO int wildcard; 15167bc4aca7SDavid Greenman 151768e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 151868e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 151968e0d7e0SRobert Watson 1520fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 15211b73ca0bSSam Leffler 152268e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1523c3229e05SDavid Greenman struct inpcbhead *head; 1524c3229e05SDavid Greenman /* 1525c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1526c3229e05SDavid Greenman * matches the local address and port we're looking for. 1527c3229e05SDavid Greenman */ 1528712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1529712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1530fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1531cfa1ca9dSYoshinobu Inoue #ifdef INET6 1532413628a7SBjoern A. Zeeb /* XXX inp locking */ 1533369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1534cfa1ca9dSYoshinobu Inoue continue; 1535cfa1ca9dSYoshinobu Inoue #endif 1536c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1537c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1538c3229e05SDavid Greenman inp->inp_lport == lport) { 1539c3229e05SDavid Greenman /* 1540413628a7SBjoern A. Zeeb * Found? 1541c3229e05SDavid Greenman */ 1542413628a7SBjoern A. Zeeb if (cred == NULL || 15430304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 15440304c731SJamie Gritton inp->inp_cred->cr_prison)) 1545c3229e05SDavid Greenman return (inp); 1546df8bae1dSRodney W. Grimes } 1547c3229e05SDavid Greenman } 1548c3229e05SDavid Greenman /* 1549c3229e05SDavid Greenman * Not found. 1550c3229e05SDavid Greenman */ 1551c3229e05SDavid Greenman return (NULL); 1552c3229e05SDavid Greenman } else { 1553c3229e05SDavid Greenman struct inpcbporthead *porthash; 1554c3229e05SDavid Greenman struct inpcbport *phd; 1555c3229e05SDavid Greenman struct inpcb *match = NULL; 1556c3229e05SDavid Greenman /* 1557c3229e05SDavid Greenman * Best fit PCB lookup. 1558c3229e05SDavid Greenman * 1559c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1560c3229e05SDavid Greenman * port hash list. 1561c3229e05SDavid Greenman */ 1562712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1563712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1564fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1565c3229e05SDavid Greenman if (phd->phd_port == lport) 1566c3229e05SDavid Greenman break; 1567c3229e05SDavid Greenman } 1568c3229e05SDavid Greenman if (phd != NULL) { 1569c3229e05SDavid Greenman /* 1570c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1571c3229e05SDavid Greenman * fit. 1572c3229e05SDavid Greenman */ 157337d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1574c3229e05SDavid Greenman wildcard = 0; 1575413628a7SBjoern A. Zeeb if (cred != NULL && 15760304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 15770304c731SJamie Gritton cred->cr_prison)) 1578413628a7SBjoern A. Zeeb continue; 1579cfa1ca9dSYoshinobu Inoue #ifdef INET6 1580413628a7SBjoern A. Zeeb /* XXX inp locking */ 1581369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1582cfa1ca9dSYoshinobu Inoue continue; 1583d5e8a67eSHajimu UMEMOTO /* 1584d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1585d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1586d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1587d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1588d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1589d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1590d5e8a67eSHajimu UMEMOTO * 1591d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1592d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1593d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1594d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1595d5e8a67eSHajimu UMEMOTO */ 1596d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1597d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1598cfa1ca9dSYoshinobu Inoue #endif 1599c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1600c3229e05SDavid Greenman wildcard++; 160115bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 160215bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 160315bd2b43SDavid Greenman wildcard++; 160415bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 160515bd2b43SDavid Greenman continue; 160615bd2b43SDavid Greenman } else { 160715bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 160815bd2b43SDavid Greenman wildcard++; 160915bd2b43SDavid Greenman } 1610df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1611df8bae1dSRodney W. Grimes match = inp; 1612df8bae1dSRodney W. Grimes matchwild = wildcard; 1613413628a7SBjoern A. Zeeb if (matchwild == 0) 1614df8bae1dSRodney W. Grimes break; 1615df8bae1dSRodney W. Grimes } 1616df8bae1dSRodney W. Grimes } 16173dbdc25cSDavid Greenman } 1618df8bae1dSRodney W. Grimes return (match); 1619df8bae1dSRodney W. Grimes } 1620c3229e05SDavid Greenman } 1621d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 162215bd2b43SDavid Greenman 162352cd27cbSRobert Watson #ifdef PCBGROUP 162452cd27cbSRobert Watson /* 162552cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 162652cd27cbSRobert Watson */ 162752cd27cbSRobert Watson static struct inpcb * 162852cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 162952cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 163052cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 163152cd27cbSRobert Watson { 163252cd27cbSRobert Watson struct inpcbhead *head; 163352cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 163452cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 163552cd27cbSRobert Watson 163652cd27cbSRobert Watson /* 163752cd27cbSRobert Watson * First look for an exact match. 163852cd27cbSRobert Watson */ 163952cd27cbSRobert Watson tmpinp = NULL; 164052cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 164152cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 164252cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 164352cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgrouphash) { 164452cd27cbSRobert Watson #ifdef INET6 164552cd27cbSRobert Watson /* XXX inp locking */ 164652cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 164752cd27cbSRobert Watson continue; 164852cd27cbSRobert Watson #endif 164952cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 165052cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 165152cd27cbSRobert Watson inp->inp_fport == fport && 165252cd27cbSRobert Watson inp->inp_lport == lport) { 165352cd27cbSRobert Watson /* 165452cd27cbSRobert Watson * XXX We should be able to directly return 165552cd27cbSRobert Watson * the inp here, without any checks. 165652cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 165752cd27cbSRobert Watson */ 165852cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 165952cd27cbSRobert Watson goto found; 166052cd27cbSRobert Watson if (tmpinp == NULL) 166152cd27cbSRobert Watson tmpinp = inp; 166252cd27cbSRobert Watson } 166352cd27cbSRobert Watson } 166452cd27cbSRobert Watson if (tmpinp != NULL) { 166552cd27cbSRobert Watson inp = tmpinp; 166652cd27cbSRobert Watson goto found; 166752cd27cbSRobert Watson } 166852cd27cbSRobert Watson 16690a100a6fSAdrian Chadd #ifdef RSS 16700a100a6fSAdrian Chadd /* 16710a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 16720a100a6fSAdrian Chadd * match for an RSS-local socket. 16730a100a6fSAdrian Chadd */ 16740a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 16750a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 16760a100a6fSAdrian Chadd #ifdef INET6 16770a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 16780a100a6fSAdrian Chadd #endif 16790a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 16800a100a6fSAdrian Chadd struct inpcbhead *head; 16810a100a6fSAdrian Chadd int injail; 16820a100a6fSAdrian Chadd 16830a100a6fSAdrian Chadd /* 16840a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 16850a100a6fSAdrian Chadd * 1. jailed, non-wild. 16860a100a6fSAdrian Chadd * 2. jailed, wild. 16870a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 16880a100a6fSAdrian Chadd * 4. non-jailed, wild. 16890a100a6fSAdrian Chadd */ 16900a100a6fSAdrian Chadd 16910a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 16920a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 16930a100a6fSAdrian Chadd LIST_FOREACH(inp, head, inp_pcbgrouphash) { 16940a100a6fSAdrian Chadd #ifdef INET6 16950a100a6fSAdrian Chadd /* XXX inp locking */ 16960a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 16970a100a6fSAdrian Chadd continue; 16980a100a6fSAdrian Chadd #endif 16990a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 17000a100a6fSAdrian Chadd inp->inp_lport != lport) 17010a100a6fSAdrian Chadd continue; 17020a100a6fSAdrian Chadd 17030a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 17040a100a6fSAdrian Chadd if (injail) { 17050a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 17060a100a6fSAdrian Chadd &laddr) != 0) 17070a100a6fSAdrian Chadd continue; 17080a100a6fSAdrian Chadd } else { 17090a100a6fSAdrian Chadd if (local_exact != NULL) 17100a100a6fSAdrian Chadd continue; 17110a100a6fSAdrian Chadd } 17120a100a6fSAdrian Chadd 17130a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 17140a100a6fSAdrian Chadd if (injail) 17150a100a6fSAdrian Chadd goto found; 17160a100a6fSAdrian Chadd else 17170a100a6fSAdrian Chadd local_exact = inp; 17180a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 17190a100a6fSAdrian Chadd #ifdef INET6 17200a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 17210a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 17220a100a6fSAdrian Chadd local_wild_mapped = inp; 17230a100a6fSAdrian Chadd else 17240a100a6fSAdrian Chadd #endif 17250a100a6fSAdrian Chadd if (injail) 17260a100a6fSAdrian Chadd jail_wild = inp; 17270a100a6fSAdrian Chadd else 17280a100a6fSAdrian Chadd local_wild = inp; 17290a100a6fSAdrian Chadd } 17300a100a6fSAdrian Chadd } /* LIST_FOREACH */ 17310a100a6fSAdrian Chadd 17320a100a6fSAdrian Chadd inp = jail_wild; 17330a100a6fSAdrian Chadd if (inp == NULL) 17340a100a6fSAdrian Chadd inp = local_exact; 17350a100a6fSAdrian Chadd if (inp == NULL) 17360a100a6fSAdrian Chadd inp = local_wild; 17370a100a6fSAdrian Chadd #ifdef INET6 17380a100a6fSAdrian Chadd if (inp == NULL) 17390a100a6fSAdrian Chadd inp = local_wild_mapped; 17400a100a6fSAdrian Chadd #endif 17410a100a6fSAdrian Chadd if (inp != NULL) 17420a100a6fSAdrian Chadd goto found; 17430a100a6fSAdrian Chadd } 17440a100a6fSAdrian Chadd #endif 17450a100a6fSAdrian Chadd 174652cd27cbSRobert Watson /* 174752cd27cbSRobert Watson * Then look for a wildcard match, if requested. 174852cd27cbSRobert Watson */ 174952cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 175052cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 175152cd27cbSRobert Watson #ifdef INET6 175252cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 175352cd27cbSRobert Watson #endif 175452cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 175552cd27cbSRobert Watson struct inpcbhead *head; 175652cd27cbSRobert Watson int injail; 175752cd27cbSRobert Watson 175852cd27cbSRobert Watson /* 175952cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 176052cd27cbSRobert Watson * 1. jailed, non-wild. 176152cd27cbSRobert Watson * 2. jailed, wild. 176252cd27cbSRobert Watson * 3. non-jailed, non-wild. 176352cd27cbSRobert Watson * 4. non-jailed, wild. 176452cd27cbSRobert Watson */ 176552cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 176652cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 176752cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 176852cd27cbSRobert Watson #ifdef INET6 176952cd27cbSRobert Watson /* XXX inp locking */ 177052cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 177152cd27cbSRobert Watson continue; 177252cd27cbSRobert Watson #endif 177352cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 177452cd27cbSRobert Watson inp->inp_lport != lport) 177552cd27cbSRobert Watson continue; 177652cd27cbSRobert Watson 177752cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 177852cd27cbSRobert Watson if (injail) { 177952cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 178052cd27cbSRobert Watson &laddr) != 0) 178152cd27cbSRobert Watson continue; 178252cd27cbSRobert Watson } else { 178352cd27cbSRobert Watson if (local_exact != NULL) 178452cd27cbSRobert Watson continue; 178552cd27cbSRobert Watson } 178652cd27cbSRobert Watson 178752cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 178852cd27cbSRobert Watson if (injail) 178952cd27cbSRobert Watson goto found; 179052cd27cbSRobert Watson else 179152cd27cbSRobert Watson local_exact = inp; 179252cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 179352cd27cbSRobert Watson #ifdef INET6 179452cd27cbSRobert Watson /* XXX inp locking, NULL check */ 179552cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 179652cd27cbSRobert Watson local_wild_mapped = inp; 179752cd27cbSRobert Watson else 179883e521ecSBjoern A. Zeeb #endif 179952cd27cbSRobert Watson if (injail) 180052cd27cbSRobert Watson jail_wild = inp; 180152cd27cbSRobert Watson else 180252cd27cbSRobert Watson local_wild = inp; 180352cd27cbSRobert Watson } 180452cd27cbSRobert Watson } /* LIST_FOREACH */ 180552cd27cbSRobert Watson inp = jail_wild; 180652cd27cbSRobert Watson if (inp == NULL) 180752cd27cbSRobert Watson inp = local_exact; 180852cd27cbSRobert Watson if (inp == NULL) 180952cd27cbSRobert Watson inp = local_wild; 181052cd27cbSRobert Watson #ifdef INET6 181152cd27cbSRobert Watson if (inp == NULL) 181252cd27cbSRobert Watson inp = local_wild_mapped; 181383e521ecSBjoern A. Zeeb #endif 181452cd27cbSRobert Watson if (inp != NULL) 181552cd27cbSRobert Watson goto found; 181652cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 181752cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 181852cd27cbSRobert Watson return (NULL); 181952cd27cbSRobert Watson 182052cd27cbSRobert Watson found: 182152cd27cbSRobert Watson in_pcbref(inp); 182252cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 182352cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 182452cd27cbSRobert Watson INP_WLOCK(inp); 182552cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 182652cd27cbSRobert Watson return (NULL); 182752cd27cbSRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 182852cd27cbSRobert Watson INP_RLOCK(inp); 182952cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 183052cd27cbSRobert Watson return (NULL); 183152cd27cbSRobert Watson } else 183252cd27cbSRobert Watson panic("%s: locking bug", __func__); 183352cd27cbSRobert Watson return (inp); 183452cd27cbSRobert Watson } 183552cd27cbSRobert Watson #endif /* PCBGROUP */ 183652cd27cbSRobert Watson 183715bd2b43SDavid Greenman /* 1838fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 1839fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 1840fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 184115bd2b43SDavid Greenman */ 1842fa046d87SRobert Watson static struct inpcb * 1843fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 184468e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 1845136d4f1cSRobert Watson struct ifnet *ifp) 184615bd2b43SDavid Greenman { 184715bd2b43SDavid Greenman struct inpcbhead *head; 1848413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 184915bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 185015bd2b43SDavid Greenman 185168e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 185268e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 185368e0d7e0SRobert Watson 1854fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 1855602cc7f1SRobert Watson 185615bd2b43SDavid Greenman /* 185715bd2b43SDavid Greenman * First look for an exact match. 185815bd2b43SDavid Greenman */ 1859413628a7SBjoern A. Zeeb tmpinp = NULL; 1860712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1861712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1862fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1863cfa1ca9dSYoshinobu Inoue #ifdef INET6 1864413628a7SBjoern A. Zeeb /* XXX inp locking */ 1865369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1866cfa1ca9dSYoshinobu Inoue continue; 1867cfa1ca9dSYoshinobu Inoue #endif 18686d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1869ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1870ca98b82cSDavid Greenman inp->inp_fport == fport && 1871413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1872413628a7SBjoern A. Zeeb /* 1873413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1874413628a7SBjoern A. Zeeb * the inp here, without any checks. 1875413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1876413628a7SBjoern A. Zeeb */ 18770304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 1878c3229e05SDavid Greenman return (inp); 1879413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1880413628a7SBjoern A. Zeeb tmpinp = inp; 1881c3229e05SDavid Greenman } 1882413628a7SBjoern A. Zeeb } 1883413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1884413628a7SBjoern A. Zeeb return (tmpinp); 1885e3fd5ffdSRobert Watson 1886e3fd5ffdSRobert Watson /* 1887e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1888e3fd5ffdSRobert Watson */ 188968e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1890413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1891e3fd5ffdSRobert Watson #ifdef INET6 1892cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1893e3fd5ffdSRobert Watson #endif 1894413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1895413628a7SBjoern A. Zeeb int injail; 1896413628a7SBjoern A. Zeeb 1897413628a7SBjoern A. Zeeb /* 1898413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 1899413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 1900413628a7SBjoern A. Zeeb * 2. jailed, wild. 1901413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 1902413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 1903413628a7SBjoern A. Zeeb */ 19046d6a026bSDavid Greenman 1905712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1906712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1907fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1908cfa1ca9dSYoshinobu Inoue #ifdef INET6 1909413628a7SBjoern A. Zeeb /* XXX inp locking */ 1910369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1911cfa1ca9dSYoshinobu Inoue continue; 1912cfa1ca9dSYoshinobu Inoue #endif 1913413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 1914413628a7SBjoern A. Zeeb inp->inp_lport != lport) 1915413628a7SBjoern A. Zeeb continue; 1916413628a7SBjoern A. Zeeb 19170304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 1918413628a7SBjoern A. Zeeb if (injail) { 1919b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 1920b89e82ddSJamie Gritton &laddr) != 0) 1921413628a7SBjoern A. Zeeb continue; 1922413628a7SBjoern A. Zeeb } else { 1923413628a7SBjoern A. Zeeb if (local_exact != NULL) 1924413628a7SBjoern A. Zeeb continue; 1925413628a7SBjoern A. Zeeb } 1926413628a7SBjoern A. Zeeb 1927413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 1928413628a7SBjoern A. Zeeb if (injail) 1929c3229e05SDavid Greenman return (inp); 1930413628a7SBjoern A. Zeeb else 1931413628a7SBjoern A. Zeeb local_exact = inp; 1932413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1933e3fd5ffdSRobert Watson #ifdef INET6 1934413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 19355cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 1936cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1937cfa1ca9dSYoshinobu Inoue else 193883e521ecSBjoern A. Zeeb #endif 1939413628a7SBjoern A. Zeeb if (injail) 1940413628a7SBjoern A. Zeeb jail_wild = inp; 1941413628a7SBjoern A. Zeeb else 19426d6a026bSDavid Greenman local_wild = inp; 19436d6a026bSDavid Greenman } 1944413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 1945413628a7SBjoern A. Zeeb if (jail_wild != NULL) 1946413628a7SBjoern A. Zeeb return (jail_wild); 1947413628a7SBjoern A. Zeeb if (local_exact != NULL) 1948413628a7SBjoern A. Zeeb return (local_exact); 1949413628a7SBjoern A. Zeeb if (local_wild != NULL) 1950c3229e05SDavid Greenman return (local_wild); 1951413628a7SBjoern A. Zeeb #ifdef INET6 1952413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 1953413628a7SBjoern A. Zeeb return (local_wild_mapped); 195483e521ecSBjoern A. Zeeb #endif 195568e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 1956413628a7SBjoern A. Zeeb 19576d6a026bSDavid Greenman return (NULL); 195815bd2b43SDavid Greenman } 1959fa046d87SRobert Watson 1960fa046d87SRobert Watson /* 1961fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 1962fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 1963fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 1964fa046d87SRobert Watson */ 1965fa046d87SRobert Watson static struct inpcb * 1966fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1967fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 1968fa046d87SRobert Watson struct ifnet *ifp) 1969fa046d87SRobert Watson { 1970fa046d87SRobert Watson struct inpcb *inp; 1971fa046d87SRobert Watson 1972fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 1973fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1974fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 1975fa046d87SRobert Watson if (inp != NULL) { 1976fa046d87SRobert Watson in_pcbref(inp); 1977fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1978fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 1979fa046d87SRobert Watson INP_WLOCK(inp); 1980fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) 1981fa046d87SRobert Watson return (NULL); 1982fa046d87SRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 1983fa046d87SRobert Watson INP_RLOCK(inp); 1984fa046d87SRobert Watson if (in_pcbrele_rlocked(inp)) 1985fa046d87SRobert Watson return (NULL); 1986fa046d87SRobert Watson } else 1987fa046d87SRobert Watson panic("%s: locking bug", __func__); 1988fa046d87SRobert Watson } else 1989fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1990fa046d87SRobert Watson return (inp); 1991fa046d87SRobert Watson } 1992fa046d87SRobert Watson 1993fa046d87SRobert Watson /* 1994d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1995d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 199652cd27cbSRobert Watson * 199752cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 1998fa046d87SRobert Watson */ 1999fa046d87SRobert Watson struct inpcb * 2000fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2001fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2002fa046d87SRobert Watson { 20037527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 200452cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 200552cd27cbSRobert Watson #endif 2006fa046d87SRobert Watson 2007fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2008fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2009fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2010fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 2011fa046d87SRobert Watson 20127527624eSRobert Watson /* 20137527624eSRobert Watson * When not using RSS, use connection groups in preference to the 20147527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 20157527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 20167527624eSRobert Watson * in software. 20177527624eSRobert Watson * 20187527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 20197527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 20207527624eSRobert Watson * in software. 20217527624eSRobert Watson */ 20227527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 202352cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 202452cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 202552cd27cbSRobert Watson fport); 202652cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 202752cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 202852cd27cbSRobert Watson } 202952cd27cbSRobert Watson #endif 2030fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2031fa046d87SRobert Watson lookupflags, ifp)); 2032fa046d87SRobert Watson } 2033d3c1f003SRobert Watson 2034d3c1f003SRobert Watson struct inpcb * 2035d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2036d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2037d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2038d3c1f003SRobert Watson { 203952cd27cbSRobert Watson #ifdef PCBGROUP 204052cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 204152cd27cbSRobert Watson #endif 2042d3c1f003SRobert Watson 2043d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2044d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2045d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2046d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2047d3c1f003SRobert Watson 204852cd27cbSRobert Watson #ifdef PCBGROUP 20497527624eSRobert Watson /* 20507527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 20517527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 20527527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 20537527624eSRobert Watson * using RSS. 20547527624eSRobert Watson * 20557527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 20567527624eSRobert Watson */ 20577527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 20587527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 205952cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 206052cd27cbSRobert Watson m->m_pkthdr.flowid); 206152cd27cbSRobert Watson if (pcbgroup != NULL) 206252cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 206352cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 20647527624eSRobert Watson #ifndef RSS 206552cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 206652cd27cbSRobert Watson fport); 206752cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 206852cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 20697527624eSRobert Watson #endif 207052cd27cbSRobert Watson } 207152cd27cbSRobert Watson #endif 2072d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2073d3c1f003SRobert Watson lookupflags, ifp)); 2074d3c1f003SRobert Watson } 207567107f45SBjoern A. Zeeb #endif /* INET */ 207615bd2b43SDavid Greenman 20777bc4aca7SDavid Greenman /* 2078c3229e05SDavid Greenman * Insert PCB onto various hash lists. 20797bc4aca7SDavid Greenman */ 208052cd27cbSRobert Watson static int 208152cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 208215bd2b43SDavid Greenman { 2083c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2084c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2085c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2086c3229e05SDavid Greenman struct inpcbport *phd; 2087cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 208815bd2b43SDavid Greenman 20898501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2090fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2091fa046d87SRobert Watson 2092111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2093111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2094602cc7f1SRobert Watson 2095cfa1ca9dSYoshinobu Inoue #ifdef INET6 2096cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 20971b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2098cfa1ca9dSYoshinobu Inoue else 209983e521ecSBjoern A. Zeeb #endif 2100cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2101cfa1ca9dSYoshinobu Inoue 2102712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2103712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 210415bd2b43SDavid Greenman 2105712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2106712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2107c3229e05SDavid Greenman 2108c3229e05SDavid Greenman /* 2109c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2110c3229e05SDavid Greenman */ 2111fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 2112c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2113c3229e05SDavid Greenman break; 2114c3229e05SDavid Greenman } 2115c3229e05SDavid Greenman /* 2116c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2117c3229e05SDavid Greenman */ 2118c3229e05SDavid Greenman if (phd == NULL) { 21191ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2120c3229e05SDavid Greenman if (phd == NULL) { 2121c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2122c3229e05SDavid Greenman } 2123c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2124c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 2125c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2126c3229e05SDavid Greenman } 2127c3229e05SDavid Greenman inp->inp_phd = phd; 2128c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2129c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2130111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 213152cd27cbSRobert Watson #ifdef PCBGROUP 213252cd27cbSRobert Watson if (do_pcbgroup_update) 213352cd27cbSRobert Watson in_pcbgroup_update(inp); 213452cd27cbSRobert Watson #endif 2135c3229e05SDavid Greenman return (0); 213615bd2b43SDavid Greenman } 213715bd2b43SDavid Greenman 2138c3229e05SDavid Greenman /* 213952cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 214052cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 214152cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 214252cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 214352cd27cbSRobert Watson * pcbgroup until later. 214452cd27cbSRobert Watson * 214552cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 214652cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 214752cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 214852cd27cbSRobert Watson */ 214952cd27cbSRobert Watson int 215052cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 215152cd27cbSRobert Watson { 215252cd27cbSRobert Watson 215352cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 215452cd27cbSRobert Watson } 215552cd27cbSRobert Watson 215652cd27cbSRobert Watson int 215752cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 215852cd27cbSRobert Watson { 215952cd27cbSRobert Watson 216052cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 216152cd27cbSRobert Watson } 216252cd27cbSRobert Watson 216352cd27cbSRobert Watson /* 2164c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2165c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2166c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2167c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2168c3229e05SDavid Greenman */ 216915bd2b43SDavid Greenman void 2170d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 217115bd2b43SDavid Greenman { 217259daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 217315bd2b43SDavid Greenman struct inpcbhead *head; 2174cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 217515bd2b43SDavid Greenman 21768501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2177fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2178fa046d87SRobert Watson 2179111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2180111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2181602cc7f1SRobert Watson 2182cfa1ca9dSYoshinobu Inoue #ifdef INET6 2183cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 21841b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2185cfa1ca9dSYoshinobu Inoue else 218683e521ecSBjoern A. Zeeb #endif 2187cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2188cfa1ca9dSYoshinobu Inoue 2189712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2190712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 219115bd2b43SDavid Greenman 2192c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 219315bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 219452cd27cbSRobert Watson 219552cd27cbSRobert Watson #ifdef PCBGROUP 219652cd27cbSRobert Watson if (m != NULL) 219752cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 219852cd27cbSRobert Watson else 219952cd27cbSRobert Watson in_pcbgroup_update(inp); 220052cd27cbSRobert Watson #endif 2201c3229e05SDavid Greenman } 2202c3229e05SDavid Greenman 2203d3c1f003SRobert Watson void 2204d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2205d3c1f003SRobert Watson { 2206d3c1f003SRobert Watson 2207d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2208d3c1f003SRobert Watson } 2209d3c1f003SRobert Watson 2210c3229e05SDavid Greenman /* 2211c3229e05SDavid Greenman * Remove PCB from various lists. 2212c3229e05SDavid Greenman */ 22136d888973SRobert Watson static void 2214136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2215c3229e05SDavid Greenman { 221659daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 221759daba27SSam Leffler 2218ff9b006dSJulien Charbon #ifdef INVARIANTS 2219ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 2220ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 2221ff9b006dSJulien Charbon } else { 222259daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 2223ff9b006dSJulien Charbon } 2224ff9b006dSJulien Charbon #endif 2225ff9b006dSJulien Charbon 22268501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2227ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 222859daba27SSam Leffler 222959daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2230111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2231c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2232c3229e05SDavid Greenman 2233fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 2234c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 2235c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 2236fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2237c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 2238c3229e05SDavid Greenman free(phd, M_PCB); 2239c3229e05SDavid Greenman } 2240fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2241111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2242c3229e05SDavid Greenman } 2243c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 224459daba27SSam Leffler pcbinfo->ipi_count--; 224552cd27cbSRobert Watson #ifdef PCBGROUP 224652cd27cbSRobert Watson in_pcbgroup_remove(inp); 224752cd27cbSRobert Watson #endif 224815bd2b43SDavid Greenman } 224975c13541SPoul-Henning Kamp 2250a557af22SRobert Watson /* 225184cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 225284cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 225384cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 225484cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 225584cc0778SGeorge V. Neville-Neil */ 225684cc0778SGeorge V. Neville-Neil void 225784cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 225884cc0778SGeorge V. Neville-Neil { 225984cc0778SGeorge V. Neville-Neil 2260cc94f0c2SGleb Smirnoff RO_RTFREE(&inp->inp_route); 22616d768226SGeorge V. Neville-Neil if (inp->inp_route.ro_lle) 22626d768226SGeorge V. Neville-Neil LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */ 226384cc0778SGeorge V. Neville-Neil return; 226484cc0778SGeorge V. Neville-Neil } 226584cc0778SGeorge V. Neville-Neil 226684cc0778SGeorge V. Neville-Neil /* 2267a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2268a557af22SRobert Watson * label change into the in_pcb for the socket. 2269a557af22SRobert Watson */ 2270a557af22SRobert Watson void 2271136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2272a557af22SRobert Watson { 2273a557af22SRobert Watson #ifdef MAC 2274a557af22SRobert Watson struct inpcb *inp; 2275a557af22SRobert Watson 22764c7c478dSRobert Watson inp = sotoinpcb(so); 22774c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2278602cc7f1SRobert Watson 22798501a69cSRobert Watson INP_WLOCK(inp); 2280310e7cebSRobert Watson SOCK_LOCK(so); 2281a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2282310e7cebSRobert Watson SOCK_UNLOCK(so); 22838501a69cSRobert Watson INP_WUNLOCK(inp); 2284a557af22SRobert Watson #endif 2285a557af22SRobert Watson } 22865f311da2SMike Silbersack 22875f311da2SMike Silbersack /* 2288ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2289ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2290ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2291ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2292ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 22935f311da2SMike Silbersack */ 2294aae49dd3SBjoern A. Zeeb static void 2295136d4f1cSRobert Watson ipport_tick(void *xtp) 22965f311da2SMike Silbersack { 22978b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2298ad3a630fSRobert Watson 22995ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 23008b615593SMarko Zec VNET_FOREACH(vnet_iter) { 23018b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 23028b615593SMarko Zec if (V_ipport_tcpallocs <= 23038b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2304603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2305603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2306ad3a630fSRobert Watson } else 2307603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2308603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 23098b615593SMarko Zec CURVNET_RESTORE(); 23108b615593SMarko Zec } 23115ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 23125f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 23135f311da2SMike Silbersack } 2314497057eeSRobert Watson 2315aae49dd3SBjoern A. Zeeb static void 2316aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2317aae49dd3SBjoern A. Zeeb { 2318aae49dd3SBjoern A. Zeeb 2319aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2320aae49dd3SBjoern A. Zeeb } 2321aae49dd3SBjoern A. Zeeb 2322aae49dd3SBjoern A. Zeeb /* 2323aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2324aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2325aae49dd3SBjoern A. Zeeb */ 2326aae49dd3SBjoern A. Zeeb static void 2327aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2328aae49dd3SBjoern A. Zeeb { 2329aae49dd3SBjoern A. Zeeb 2330aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2331fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2332aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2333aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2334aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2335aae49dd3SBjoern A. Zeeb } 2336aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2337aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2338aae49dd3SBjoern A. Zeeb 23393d585327SKip Macy void 23403d585327SKip Macy inp_wlock(struct inpcb *inp) 23413d585327SKip Macy { 23423d585327SKip Macy 23438501a69cSRobert Watson INP_WLOCK(inp); 23443d585327SKip Macy } 23453d585327SKip Macy 23463d585327SKip Macy void 23473d585327SKip Macy inp_wunlock(struct inpcb *inp) 23483d585327SKip Macy { 23493d585327SKip Macy 23508501a69cSRobert Watson INP_WUNLOCK(inp); 23513d585327SKip Macy } 23523d585327SKip Macy 23533d585327SKip Macy void 23543d585327SKip Macy inp_rlock(struct inpcb *inp) 23553d585327SKip Macy { 23563d585327SKip Macy 2357a69042a5SRobert Watson INP_RLOCK(inp); 23583d585327SKip Macy } 23593d585327SKip Macy 23603d585327SKip Macy void 23613d585327SKip Macy inp_runlock(struct inpcb *inp) 23623d585327SKip Macy { 23633d585327SKip Macy 2364a69042a5SRobert Watson INP_RUNLOCK(inp); 23653d585327SKip Macy } 23663d585327SKip Macy 2367c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 23683d585327SKip Macy void 2369e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 23703d585327SKip Macy { 23713d585327SKip Macy 23728501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 23733d585327SKip Macy } 23743d585327SKip Macy 23753d585327SKip Macy void 2376e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 23773d585327SKip Macy { 23783d585327SKip Macy 23793d585327SKip Macy INP_UNLOCK_ASSERT(inp); 23803d585327SKip Macy } 23813d585327SKip Macy #endif 23823d585327SKip Macy 23839378e437SKip Macy void 23849378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 23859378e437SKip Macy { 23869378e437SKip Macy struct inpcb *inp; 23879378e437SKip Macy 2388ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 238997021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 23909378e437SKip Macy INP_WLOCK(inp); 23919378e437SKip Macy func(inp, arg); 23929378e437SKip Macy INP_WUNLOCK(inp); 23939378e437SKip Macy } 2394ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 23959378e437SKip Macy } 23969378e437SKip Macy 23979378e437SKip Macy struct socket * 23989378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 23999378e437SKip Macy { 24009378e437SKip Macy 24019378e437SKip Macy INP_WLOCK_ASSERT(inp); 24029378e437SKip Macy return (inp->inp_socket); 24039378e437SKip Macy } 24049378e437SKip Macy 24059378e437SKip Macy struct tcpcb * 24069378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 24079378e437SKip Macy { 24089378e437SKip Macy 24099378e437SKip Macy INP_WLOCK_ASSERT(inp); 24109378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 24119378e437SKip Macy } 24129378e437SKip Macy 24139378e437SKip Macy int 24149378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 24159378e437SKip Macy { 24169378e437SKip Macy 24179378e437SKip Macy return (inp->inp_ip_tos); 24189378e437SKip Macy } 24199378e437SKip Macy 24209378e437SKip Macy void 24219378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 24229378e437SKip Macy { 24239378e437SKip Macy 24249378e437SKip Macy inp->inp_ip_tos = val; 24259378e437SKip Macy } 24269378e437SKip Macy 24279378e437SKip Macy void 2428df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 24299d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 24309378e437SKip Macy { 24319378e437SKip Macy 24329d29c635SKip Macy INP_LOCK_ASSERT(inp); 2433df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2434df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 24359378e437SKip Macy *lp = inp->inp_lport; 24369378e437SKip Macy *fp = inp->inp_fport; 24379378e437SKip Macy } 24389378e437SKip Macy 2439dd0e6c38SKip Macy struct inpcb * 2440dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2441dd0e6c38SKip Macy { 2442dd0e6c38SKip Macy 2443dd0e6c38SKip Macy return (sotoinpcb(so)); 2444dd0e6c38SKip Macy } 2445dd0e6c38SKip Macy 2446dd0e6c38SKip Macy struct tcpcb * 2447dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2448dd0e6c38SKip Macy { 2449dd0e6c38SKip Macy 2450dd0e6c38SKip Macy return (sototcpcb(so)); 2451dd0e6c38SKip Macy } 2452dd0e6c38SKip Macy 2453cc65eb4eSGleb Smirnoff /* 2454cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2455cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2456cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2457cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2458cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2459cc65eb4eSGleb Smirnoff * hidden from users. 2460cc65eb4eSGleb Smirnoff */ 2461cc65eb4eSGleb Smirnoff void 2462cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2463cc65eb4eSGleb Smirnoff { 2464cc65eb4eSGleb Smirnoff 2465cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2466cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2467cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2468cc65eb4eSGleb Smirnoff else 2469cc65eb4eSGleb Smirnoff bzero(&xi->xi_socket, sizeof(struct xsocket)); 2470cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2471cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 2472cc65eb4eSGleb Smirnoff xi->inp_ppcb = inp->inp_ppcb; 2473cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2474cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2475cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2476cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2477cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2478cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2479cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2480cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2481cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2482cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2483cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2484cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2485cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2486cc65eb4eSGleb Smirnoff } 2487cc65eb4eSGleb Smirnoff 2488497057eeSRobert Watson #ifdef DDB 2489497057eeSRobert Watson static void 2490497057eeSRobert Watson db_print_indent(int indent) 2491497057eeSRobert Watson { 2492497057eeSRobert Watson int i; 2493497057eeSRobert Watson 2494497057eeSRobert Watson for (i = 0; i < indent; i++) 2495497057eeSRobert Watson db_printf(" "); 2496497057eeSRobert Watson } 2497497057eeSRobert Watson 2498497057eeSRobert Watson static void 2499497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2500497057eeSRobert Watson { 2501497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2502497057eeSRobert Watson 2503497057eeSRobert Watson db_print_indent(indent); 2504497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2505497057eeSRobert Watson 2506497057eeSRobert Watson indent += 2; 2507497057eeSRobert Watson 250803dc38a4SRobert Watson #ifdef INET6 2509dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2510497057eeSRobert Watson /* IPv6. */ 2511497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2512497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 251383e521ecSBjoern A. Zeeb } else 251403dc38a4SRobert Watson #endif 251583e521ecSBjoern A. Zeeb { 2516497057eeSRobert Watson /* IPv4. */ 2517497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2518497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2519497057eeSRobert Watson } 2520497057eeSRobert Watson db_print_indent(indent); 2521497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2522497057eeSRobert Watson ntohs(inc->inc_lport)); 2523497057eeSRobert Watson db_print_indent(indent); 2524497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2525497057eeSRobert Watson ntohs(inc->inc_fport)); 2526497057eeSRobert Watson } 2527497057eeSRobert Watson 2528497057eeSRobert Watson static void 2529497057eeSRobert Watson db_print_inpflags(int inp_flags) 2530497057eeSRobert Watson { 2531497057eeSRobert Watson int comma; 2532497057eeSRobert Watson 2533497057eeSRobert Watson comma = 0; 2534497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2535497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2536497057eeSRobert Watson comma = 1; 2537497057eeSRobert Watson } 2538497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2539497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2540497057eeSRobert Watson comma = 1; 2541497057eeSRobert Watson } 2542497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2543497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2544497057eeSRobert Watson comma = 1; 2545497057eeSRobert Watson } 2546dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2547dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2548dce33a45SErmal Luçi comma = 1; 2549dce33a45SErmal Luçi } 2550497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2551497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2552497057eeSRobert Watson comma = 1; 2553497057eeSRobert Watson } 2554497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2555497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2556497057eeSRobert Watson comma = 1; 2557497057eeSRobert Watson } 2558497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2559497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2560497057eeSRobert Watson comma = 1; 2561497057eeSRobert Watson } 2562497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2563497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2564497057eeSRobert Watson comma = 1; 2565497057eeSRobert Watson } 2566497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2567497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2568497057eeSRobert Watson comma = 1; 2569497057eeSRobert Watson } 2570497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2571497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2572497057eeSRobert Watson comma = 1; 2573497057eeSRobert Watson } 2574497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2575497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2576497057eeSRobert Watson comma = 1; 2577497057eeSRobert Watson } 2578497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2579497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2580497057eeSRobert Watson comma = 1; 2581497057eeSRobert Watson } 25823cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 25833cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 25843cca425bSMichael Tuexen comma = 1; 25853cca425bSMichael Tuexen } 2586497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2587497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2588497057eeSRobert Watson comma = 1; 2589497057eeSRobert Watson } 2590497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2591497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2592497057eeSRobert Watson comma = 1; 2593497057eeSRobert Watson } 2594497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2595497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2596497057eeSRobert Watson comma = 1; 2597497057eeSRobert Watson } 2598497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2599497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2600497057eeSRobert Watson comma = 1; 2601497057eeSRobert Watson } 2602497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2603497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2604497057eeSRobert Watson comma = 1; 2605497057eeSRobert Watson } 2606497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2607497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2608497057eeSRobert Watson comma = 1; 2609497057eeSRobert Watson } 2610497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2611497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2612497057eeSRobert Watson comma = 1; 2613497057eeSRobert Watson } 2614497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2615497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2616497057eeSRobert Watson comma = 1; 2617497057eeSRobert Watson } 2618497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2619497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2620497057eeSRobert Watson comma = 1; 2621497057eeSRobert Watson } 2622ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2623ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2624ad71fe3cSRobert Watson comma = 1; 2625ad71fe3cSRobert Watson } 2626ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2627ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2628ad71fe3cSRobert Watson comma = 1; 2629ad71fe3cSRobert Watson } 2630ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2631ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2632ad71fe3cSRobert Watson comma = 1; 2633ad71fe3cSRobert Watson } 2634ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2635ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2636ad71fe3cSRobert Watson comma = 1; 2637ad71fe3cSRobert Watson } 2638497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2639497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2640497057eeSRobert Watson comma = 1; 2641497057eeSRobert Watson } 2642497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2643497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2644497057eeSRobert Watson comma = 1; 2645497057eeSRobert Watson } 2646497057eeSRobert Watson } 2647497057eeSRobert Watson 2648497057eeSRobert Watson static void 2649497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2650497057eeSRobert Watson { 2651497057eeSRobert Watson int comma; 2652497057eeSRobert Watson 2653497057eeSRobert Watson comma = 0; 2654497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2655497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2656497057eeSRobert Watson comma = 1; 2657497057eeSRobert Watson } 2658497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2659497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2660497057eeSRobert Watson comma = 1; 2661497057eeSRobert Watson } 2662497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2663497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2664497057eeSRobert Watson comma = 1; 2665497057eeSRobert Watson } 2666497057eeSRobert Watson } 2667497057eeSRobert Watson 26686d888973SRobert Watson static void 2669497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2670497057eeSRobert Watson { 2671497057eeSRobert Watson 2672497057eeSRobert Watson db_print_indent(indent); 2673497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2674497057eeSRobert Watson 2675497057eeSRobert Watson indent += 2; 2676497057eeSRobert Watson 2677497057eeSRobert Watson db_print_indent(indent); 2678497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2679497057eeSRobert Watson 2680497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2681497057eeSRobert Watson 2682497057eeSRobert Watson db_print_indent(indent); 2683497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2684497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2685497057eeSRobert Watson 2686497057eeSRobert Watson db_print_indent(indent); 2687497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2688497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2689497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2690497057eeSRobert Watson db_printf(")\n"); 2691497057eeSRobert Watson 2692497057eeSRobert Watson db_print_indent(indent); 2693497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2694497057eeSRobert Watson inp->inp_vflag); 2695497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2696497057eeSRobert Watson db_printf(")\n"); 2697497057eeSRobert Watson 2698497057eeSRobert Watson db_print_indent(indent); 2699497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2700497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2701497057eeSRobert Watson 2702497057eeSRobert Watson db_print_indent(indent); 2703497057eeSRobert Watson #ifdef INET6 2704497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2705497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2706497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2707497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2708497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2709497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2710497057eeSRobert Watson inp->in6p_hops); 2711497057eeSRobert Watson } else 2712497057eeSRobert Watson #endif 2713497057eeSRobert Watson { 2714497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2715497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2716497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2717497057eeSRobert Watson } 2718497057eeSRobert Watson 2719497057eeSRobert Watson db_print_indent(indent); 2720497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2721497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 2722497057eeSRobert Watson } 2723497057eeSRobert Watson 2724497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2725497057eeSRobert Watson { 2726497057eeSRobert Watson struct inpcb *inp; 2727497057eeSRobert Watson 2728497057eeSRobert Watson if (!have_addr) { 2729497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 2730497057eeSRobert Watson return; 2731497057eeSRobert Watson } 2732497057eeSRobert Watson inp = (struct inpcb *)addr; 2733497057eeSRobert Watson 2734497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 2735497057eeSRobert Watson } 273683e521ecSBjoern A. Zeeb #endif /* DDB */ 2737f3e7afe2SHans Petter Selasky 2738f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 2739f3e7afe2SHans Petter Selasky /* 2740f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 2741f3e7afe2SHans Petter Selasky * if any. 2742f3e7afe2SHans Petter Selasky */ 2743f3e7afe2SHans Petter Selasky int 2744f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 2745f3e7afe2SHans Petter Selasky { 2746f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 2747f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2748f3e7afe2SHans Petter Selasky }; 2749f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2750f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2751f3e7afe2SHans Petter Selasky int error; 2752f3e7afe2SHans Petter Selasky 2753f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2754f3e7afe2SHans Petter Selasky if (mst == NULL) 2755f3e7afe2SHans Petter Selasky return (EINVAL); 2756f3e7afe2SHans Petter Selasky 2757f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2758f3e7afe2SHans Petter Selasky if (ifp == NULL) 2759f3e7afe2SHans Petter Selasky return (EINVAL); 2760f3e7afe2SHans Petter Selasky 2761f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 2762f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2763f3e7afe2SHans Petter Selasky } else { 2764f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 2765f3e7afe2SHans Petter Selasky } 2766f3e7afe2SHans Petter Selasky return (error); 2767f3e7afe2SHans Petter Selasky } 2768f3e7afe2SHans Petter Selasky 2769f3e7afe2SHans Petter Selasky /* 2770f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 2771f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 2772f3e7afe2SHans Petter Selasky */ 2773f3e7afe2SHans Petter Selasky int 2774f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 2775f3e7afe2SHans Petter Selasky { 2776f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 2777f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2778f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2779f3e7afe2SHans Petter Selasky int error; 2780f3e7afe2SHans Petter Selasky 2781f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2782f3e7afe2SHans Petter Selasky if (mst == NULL) 2783f3e7afe2SHans Petter Selasky return (EINVAL); 2784f3e7afe2SHans Petter Selasky 2785f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2786f3e7afe2SHans Petter Selasky if (ifp == NULL) 2787f3e7afe2SHans Petter Selasky return (EINVAL); 2788f3e7afe2SHans Petter Selasky 2789f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 2790f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2791f3e7afe2SHans Petter Selasky } else { 2792f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 2793f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 2794f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 2795f3e7afe2SHans Petter Selasky } 2796f3e7afe2SHans Petter Selasky return (error); 2797f3e7afe2SHans Petter Selasky } 2798f3e7afe2SHans Petter Selasky 2799f3e7afe2SHans Petter Selasky /* 2800f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 2801f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 2802f3e7afe2SHans Petter Selasky */ 2803f3e7afe2SHans Petter Selasky int 2804f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 2805f3e7afe2SHans Petter Selasky uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate) 2806f3e7afe2SHans Petter Selasky { 2807f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 2808f3e7afe2SHans Petter Selasky .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT, 2809f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 2810f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 2811f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 2812f3e7afe2SHans Petter Selasky }; 2813f3e7afe2SHans Petter Selasky int error; 2814f3e7afe2SHans Petter Selasky 2815f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2816f3e7afe2SHans Petter Selasky 2817f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 2818f3e7afe2SHans Petter Selasky return (EINVAL); 2819f3e7afe2SHans Petter Selasky 2820f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 2821f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 2822f3e7afe2SHans Petter Selasky } else { 2823f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 2824f3e7afe2SHans Petter Selasky 2825f3e7afe2SHans Petter Selasky /* 2826f3e7afe2SHans Petter Selasky * At success increment the refcount on 2827f3e7afe2SHans Petter Selasky * the send tag's network interface: 2828f3e7afe2SHans Petter Selasky */ 2829f3e7afe2SHans Petter Selasky if (error == 0) 2830f3e7afe2SHans Petter Selasky if_ref(inp->inp_snd_tag->ifp); 2831f3e7afe2SHans Petter Selasky } 2832f3e7afe2SHans Petter Selasky return (error); 2833f3e7afe2SHans Petter Selasky } 2834f3e7afe2SHans Petter Selasky 2835f3e7afe2SHans Petter Selasky /* 2836f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 2837f3e7afe2SHans Petter Selasky * if any: 2838f3e7afe2SHans Petter Selasky */ 2839f3e7afe2SHans Petter Selasky void 2840f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 2841f3e7afe2SHans Petter Selasky { 2842f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 2843f3e7afe2SHans Petter Selasky struct ifnet *ifp; 2844f3e7afe2SHans Petter Selasky 2845f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 2846f3e7afe2SHans Petter Selasky 2847f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 2848f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 2849f3e7afe2SHans Petter Selasky 2850f3e7afe2SHans Petter Selasky if (mst == NULL) 2851f3e7afe2SHans Petter Selasky return; 2852f3e7afe2SHans Petter Selasky 2853f3e7afe2SHans Petter Selasky ifp = mst->ifp; 2854f3e7afe2SHans Petter Selasky if (ifp == NULL) 2855f3e7afe2SHans Petter Selasky return; 2856f3e7afe2SHans Petter Selasky 2857f3e7afe2SHans Petter Selasky /* 2858f3e7afe2SHans Petter Selasky * If the device was detached while we still had reference(s) 2859f3e7afe2SHans Petter Selasky * on the ifp, we assume if_snd_tag_free() was replaced with 2860f3e7afe2SHans Petter Selasky * stubs. 2861f3e7afe2SHans Petter Selasky */ 2862f3e7afe2SHans Petter Selasky ifp->if_snd_tag_free(mst); 2863f3e7afe2SHans Petter Selasky 2864f3e7afe2SHans Petter Selasky /* release reference count on network interface */ 2865f3e7afe2SHans Petter Selasky if_rele(ifp); 2866f3e7afe2SHans Petter Selasky } 2867f3e7afe2SHans Petter Selasky 2868f3e7afe2SHans Petter Selasky /* 2869f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 2870f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 2871f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 2872f3e7afe2SHans Petter Selasky */ 2873f3e7afe2SHans Petter Selasky void 2874f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 2875f3e7afe2SHans Petter Selasky { 2876f3e7afe2SHans Petter Selasky struct socket *socket; 2877f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 2878f3e7afe2SHans Petter Selasky bool did_upgrade; 2879f3e7afe2SHans Petter Selasky int error; 2880f3e7afe2SHans Petter Selasky 2881f3e7afe2SHans Petter Selasky if (inp == NULL) 2882f3e7afe2SHans Petter Selasky return; 2883f3e7afe2SHans Petter Selasky 2884f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 2885f3e7afe2SHans Petter Selasky if (socket == NULL) 2886f3e7afe2SHans Petter Selasky return; 2887f3e7afe2SHans Petter Selasky 2888f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 2889f3e7afe2SHans Petter Selasky /* 2890f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 2891f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 2892f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 2893f3e7afe2SHans Petter Selasky * write lock. 2894f3e7afe2SHans Petter Selasky */ 2895f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 2896f3e7afe2SHans Petter Selasky return; 2897f3e7afe2SHans Petter Selasky did_upgrade = 1; 2898f3e7afe2SHans Petter Selasky } else { 2899f3e7afe2SHans Petter Selasky did_upgrade = 0; 2900f3e7afe2SHans Petter Selasky } 2901f3e7afe2SHans Petter Selasky 2902f3e7afe2SHans Petter Selasky /* 2903f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 2904f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 2905f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 2906f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 2907f3e7afe2SHans Petter Selasky */ 2908f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 2909f3e7afe2SHans Petter Selasky 2910f3e7afe2SHans Petter Selasky /* 2911f3e7afe2SHans Petter Selasky * NOTE: When attaching to a network interface a reference is 2912f3e7afe2SHans Petter Selasky * made to ensure the network interface doesn't go away until 2913f3e7afe2SHans Petter Selasky * all ratelimit connections are gone. The network interface 2914f3e7afe2SHans Petter Selasky * pointers compared below represent valid network interfaces, 2915f3e7afe2SHans Petter Selasky * except when comparing towards NULL. 2916f3e7afe2SHans Petter Selasky */ 2917f3e7afe2SHans Petter Selasky if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 2918f3e7afe2SHans Petter Selasky error = 0; 2919f3e7afe2SHans Petter Selasky } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 2920f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 2921f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 2922f3e7afe2SHans Petter Selasky error = 0; 2923f3e7afe2SHans Petter Selasky } else if (inp->inp_snd_tag == NULL) { 2924f3e7afe2SHans Petter Selasky /* 2925f3e7afe2SHans Petter Selasky * In order to utilize packet pacing with RSS, we need 2926f3e7afe2SHans Petter Selasky * to wait until there is a valid RSS hash before we 2927f3e7afe2SHans Petter Selasky * can proceed: 2928f3e7afe2SHans Petter Selasky */ 2929f3e7afe2SHans Petter Selasky if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 2930f3e7afe2SHans Petter Selasky error = EAGAIN; 2931f3e7afe2SHans Petter Selasky } else { 2932f3e7afe2SHans Petter Selasky error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 2933f3e7afe2SHans Petter Selasky mb->m_pkthdr.flowid, max_pacing_rate); 2934f3e7afe2SHans Petter Selasky } 2935f3e7afe2SHans Petter Selasky } else { 2936f3e7afe2SHans Petter Selasky error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 2937f3e7afe2SHans Petter Selasky } 2938f3e7afe2SHans Petter Selasky if (error == 0 || error == EOPNOTSUPP) 2939f3e7afe2SHans Petter Selasky inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 2940f3e7afe2SHans Petter Selasky if (did_upgrade) 2941f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 2942f3e7afe2SHans Petter Selasky } 2943f3e7afe2SHans Petter Selasky 2944f3e7afe2SHans Petter Selasky /* 2945f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 2946f3e7afe2SHans Petter Selasky */ 2947f3e7afe2SHans Petter Selasky void 2948f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 2949f3e7afe2SHans Petter Selasky { 2950f3e7afe2SHans Petter Selasky struct socket *socket; 2951f3e7afe2SHans Petter Selasky bool did_upgrade; 2952f3e7afe2SHans Petter Selasky 2953f3e7afe2SHans Petter Selasky if (inp == NULL) 2954f3e7afe2SHans Petter Selasky return; 2955f3e7afe2SHans Petter Selasky 2956f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 2957f3e7afe2SHans Petter Selasky if (socket == NULL) 2958f3e7afe2SHans Petter Selasky return; 2959f3e7afe2SHans Petter Selasky 2960f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 2961f3e7afe2SHans Petter Selasky return; 2962f3e7afe2SHans Petter Selasky 2963f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 2964f3e7afe2SHans Petter Selasky /* 2965f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 2966f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 2967f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 2968f3e7afe2SHans Petter Selasky * write lock. 2969f3e7afe2SHans Petter Selasky */ 2970f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 2971f3e7afe2SHans Petter Selasky return; 2972f3e7afe2SHans Petter Selasky did_upgrade = 1; 2973f3e7afe2SHans Petter Selasky } else { 2974f3e7afe2SHans Petter Selasky did_upgrade = 0; 2975f3e7afe2SHans Petter Selasky } 2976f3e7afe2SHans Petter Selasky 2977f3e7afe2SHans Petter Selasky /* detach rate limiting */ 2978f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 2979f3e7afe2SHans Petter Selasky 2980f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 2981f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 2982f3e7afe2SHans Petter Selasky 2983f3e7afe2SHans Petter Selasky if (did_upgrade) 2984f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 2985f3e7afe2SHans Petter Selasky } 2986f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 2987