1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 42469dd60SGarrett Wollman * Copyright (c) 1982, 1986, 1991, 1993, 1995 5497057eeSRobert Watson * The Regents of the University of California. 6111d57a6SRobert Watson * Copyright (c) 2007-2009 Robert N. M. Watson 779bdc6e5SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 85ebea466SGleb Smirnoff * Copyright (c) 2021-2022 Gleb Smirnoff <glebius@FreeBSD.org> 9497057eeSRobert Watson * All rights reserved. 10df8bae1dSRodney W. Grimes * 1179bdc6e5SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 1279bdc6e5SRobert Watson * contract to Juniper Networks, Inc. 1379bdc6e5SRobert Watson * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 394b421e2dSMike Silbersack #include <sys/cdefs.h> 40497057eeSRobert Watson #include "opt_ddb.h" 416a800098SYoshinobu Inoue #include "opt_ipsec.h" 42efc76f72SBjoern A. Zeeb #include "opt_inet.h" 43cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 44f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 450c325f53SAlexander V. Chernikov #include "opt_route.h" 467527624eSRobert Watson #include "opt_rss.h" 47cfa1ca9dSYoshinobu Inoue 48df8bae1dSRodney W. Grimes #include <sys/param.h> 49a0577692SGleb Smirnoff #include <sys/hash.h> 50df8bae1dSRodney W. Grimes #include <sys/systm.h> 51a0577692SGleb Smirnoff #include <sys/libkern.h> 52cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 53df8bae1dSRodney W. Grimes #include <sys/malloc.h> 54df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 55ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h> 56cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 57fdb987beSMark Johnston #include <sys/proc.h> 58df8bae1dSRodney W. Grimes #include <sys/protosw.h> 593ee9c3c4SRandall Stewart #include <sys/smp.h> 60fdb987beSMark Johnston #include <sys/smr.h> 61df8bae1dSRodney W. Grimes #include <sys/socket.h> 62df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 63f3e7afe2SHans Petter Selasky #include <sys/sockio.h> 64acd3428bSRobert Watson #include <sys/priv.h> 65df8bae1dSRodney W. Grimes #include <sys/proc.h> 6679bdc6e5SRobert Watson #include <sys/refcount.h> 6775c13541SPoul-Henning Kamp #include <sys/jail.h> 68101f9fc8SPeter Wemm #include <sys/kernel.h> 69101f9fc8SPeter Wemm #include <sys/sysctl.h> 708781d8e9SBruce Evans 71497057eeSRobert Watson #ifdef DDB 72497057eeSRobert Watson #include <ddb/ddb.h> 73497057eeSRobert Watson #endif 74497057eeSRobert Watson 7569c2d429SJeff Roberson #include <vm/uma.h> 76a034518aSAndrew Gallatin #include <vm/vm.h> 77df8bae1dSRodney W. Grimes 78df8bae1dSRodney W. Grimes #include <net/if.h> 7976039bc8SGleb Smirnoff #include <net/if_var.h> 803d0d5b21SJustin Hibbits #include <net/if_private.h> 81cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 826d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h> 83df8bae1dSRodney W. Grimes #include <net/route.h> 84b2bdc62aSAdrian Chadd #include <net/rss_config.h> 85530c0060SRobert Watson #include <net/vnet.h> 86df8bae1dSRodney W. Grimes 8767107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 88df8bae1dSRodney W. Grimes #include <netinet/in.h> 89df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 900f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h> 9173bebcc5SGleb Smirnoff #include <netinet/tcp.h> 9259854ecfSHans Petter Selasky #ifdef INET 9359854ecfSHans Petter Selasky #include <netinet/in_var.h> 949ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h> 9559854ecfSHans Petter Selasky #endif 96df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 97cfa1ca9dSYoshinobu Inoue #ifdef INET6 98cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 99efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 10067107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 10167107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 102cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1039ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h> 10459854ecfSHans Petter Selasky #endif 105cfa1ca9dSYoshinobu Inoue 106fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 107b9234fafSSam Leffler 108aed55708SRobert Watson #include <security/mac/mac_framework.h> 109aed55708SRobert Watson 1101a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 1111a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 112a43e7a96SGleb Smirnoff 113a43e7a96SGleb Smirnoff #define INP_FREED 0x00000200 /* Went through in_pcbfree(). */ 114a43e7a96SGleb Smirnoff #define INP_INLBGROUP 0x01000000 /* Inserted into inpcblbgroup. */ 1151a43cff9SSean Bruno 116101f9fc8SPeter Wemm /* 117101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 118101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 119101f9fc8SPeter Wemm */ 120eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 126101f9fc8SPeter Wemm 127b0d22693SCrist J. Clark /* 128b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 129b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 130b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 131b0d22693SCrist J. Clark */ 132eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 133eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 134b0d22693SCrist J. Clark 13519acc506SGleb Smirnoff /* Enable random ephemeral port allocation by default. */ 13619acc506SGleb Smirnoff VNET_DEFINE(int, ipport_randomized) = 1; 1376ac48b74SMike Silbersack 138d2025bd0SBjoern A. Zeeb #ifdef INET 139fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 140fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 141fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 142da806e8dSMark Johnston int lookupflags, uint8_t numa_domain, int fib); 14367107f45SBjoern A. Zeeb 144bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 145bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 146bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 147bbd42ad0SPeter Wemm 148bbd42ad0SPeter Wemm static int 14982d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 150bbd42ad0SPeter Wemm { 15130a4ab08SBruce Evans int error; 15230a4ab08SBruce Evans 153f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 15430a4ab08SBruce Evans if (error == 0) { 155603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 156603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 157603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 158603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 159603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 160603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 161bbd42ad0SPeter Wemm } 16230a4ab08SBruce Evans return (error); 163bbd42ad0SPeter Wemm } 164bbd42ad0SPeter Wemm 165bbd42ad0SPeter Wemm #undef RANGECHK 166bbd42ad0SPeter Wemm 1677029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, 1687029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1696472ac3dSEd Schouten "IP Ports"); 17033b3ac06SPeter Wemm 1716df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1727029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1737029da5cSPawel Biernacki &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", 1747029da5cSPawel Biernacki ""); 1756df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1767029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1777029da5cSPawel Biernacki &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", 1787029da5cSPawel Biernacki ""); 1796df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1807029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1817029da5cSPawel Biernacki &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", 1827029da5cSPawel Biernacki ""); 1836df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1847029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1857029da5cSPawel Biernacki &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", 1867029da5cSPawel Biernacki ""); 1876df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1887029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1897029da5cSPawel Biernacki &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", 1907029da5cSPawel Biernacki ""); 1916df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 1927029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1937029da5cSPawel Biernacki &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", 1947029da5cSPawel Biernacki ""); 1956df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 1966df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 1976df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 199eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 2016df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 202eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 20320abea66SRandall Stewart 20420abea66SRandall Stewart #ifdef RATELIMIT 2051a714ff2SRandall Stewart counter_u64_t rate_limit_new; 2061a714ff2SRandall Stewart counter_u64_t rate_limit_chg; 20720abea66SRandall Stewart counter_u64_t rate_limit_active; 20820abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail; 20920abea66SRandall Stewart counter_u64_t rate_limit_set_ok; 21020abea66SRandall Stewart 2117029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 21220abea66SRandall Stewart "IP Rate Limiting"); 21320abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD, 21420abea66SRandall Stewart &rate_limit_active, "Active rate limited connections"); 21520abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD, 21620abea66SRandall Stewart &rate_limit_alloc_fail, "Rate limited connection failures"); 21720abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD, 21820abea66SRandall Stewart &rate_limit_set_ok, "Rate limited setting succeeded"); 2191a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD, 2201a714ff2SRandall Stewart &rate_limit_new, "Total Rate limit new attempts"); 2211a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD, 2221a714ff2SRandall Stewart &rate_limit_chg, "Total Rate limited change attempts"); 22320abea66SRandall Stewart #endif /* RATELIMIT */ 22420abea66SRandall Stewart 22583e521ecSBjoern A. Zeeb #endif /* INET */ 2260312fbe9SPoul-Henning Kamp 227a0577692SGleb Smirnoff VNET_DEFINE(uint32_t, in_pcbhashseed); 228a0577692SGleb Smirnoff static void 229a0577692SGleb Smirnoff in_pcbhashseed_init(void) 230a0577692SGleb Smirnoff { 231a0577692SGleb Smirnoff 232a0577692SGleb Smirnoff V_in_pcbhashseed = arc4random(); 233a0577692SGleb Smirnoff } 234a0577692SGleb Smirnoff VNET_SYSINIT(in_pcbhashseed_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 235ec6e7677SZhenlei Huang in_pcbhashseed_init, NULL); 236a0577692SGleb Smirnoff 2373b62f335SKristof Provost #ifdef INET 238417b35a9SMark Johnston VNET_DEFINE_STATIC(int, connect_inaddr_wild) = 1; 239417b35a9SMark Johnston #define V_connect_inaddr_wild VNET(connect_inaddr_wild) 240417b35a9SMark Johnston SYSCTL_INT(_net_inet_ip, OID_AUTO, connect_inaddr_wild, 241417b35a9SMark Johnston CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(connect_inaddr_wild), 0, 242417b35a9SMark Johnston "Allow connecting to INADDR_ANY or INADDR_BROADCAST for connect(2)"); 2433b62f335SKristof Provost #endif 244417b35a9SMark Johnston 2453ba34b07SGleb Smirnoff static void in_pcbremhash(struct inpcb *); 2463ba34b07SGleb Smirnoff 247c3229e05SDavid Greenman /* 248c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 249c3229e05SDavid Greenman * 250de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 251de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 252de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 253c3229e05SDavid Greenman */ 254c3229e05SDavid Greenman 2551a43cff9SSean Bruno static struct inpcblbgroup * 256a600aabeSMark Johnston in_pcblbgroup_alloc(struct ucred *cred, u_char vflag, uint16_t port, 257bbd0084bSMark Johnston const union in_dependaddr *addr, int size, uint8_t numa_domain, int fib) 2581a43cff9SSean Bruno { 2591a43cff9SSean Bruno struct inpcblbgroup *grp; 2601a43cff9SSean Bruno size_t bytes; 2611a43cff9SSean Bruno 2621a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 2631a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 264d93ec8cbSMark Johnston if (grp == NULL) 2651a43cff9SSean Bruno return (NULL); 2667cbb6b6eSMark Johnston LIST_INIT(&grp->il_pending); 267d93ec8cbSMark Johnston grp->il_cred = crhold(cred); 2681a43cff9SSean Bruno grp->il_vflag = vflag; 2691a43cff9SSean Bruno grp->il_lport = port; 270a034518aSAndrew Gallatin grp->il_numa_domain = numa_domain; 271bbd0084bSMark Johnston grp->il_fibnum = fib; 2721a43cff9SSean Bruno grp->il_dependladdr = *addr; 2731a43cff9SSean Bruno grp->il_inpsiz = size; 2741a43cff9SSean Bruno return (grp); 2751a43cff9SSean Bruno } 2761a43cff9SSean Bruno 2771a43cff9SSean Bruno static void 27854af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx) 27954af3d0dSMark Johnston { 28054af3d0dSMark Johnston struct inpcblbgroup *grp; 28154af3d0dSMark Johnston 28254af3d0dSMark Johnston grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 283d93ec8cbSMark Johnston crfree(grp->il_cred); 28454af3d0dSMark Johnston free(grp, M_PCB); 28554af3d0dSMark Johnston } 28654af3d0dSMark Johnston 28754af3d0dSMark Johnston static void 2881a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 2891a43cff9SSean Bruno { 2907cbb6b6eSMark Johnston KASSERT(LIST_EMPTY(&grp->il_pending), 2917cbb6b6eSMark Johnston ("local group %p still has pending inps", grp)); 2921a43cff9SSean Bruno 29354af3d0dSMark Johnston CK_LIST_REMOVE(grp, il_list); 2942a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx); 2951a43cff9SSean Bruno } 2961a43cff9SSean Bruno 2977cbb6b6eSMark Johnston static struct inpcblbgroup * 2987cbb6b6eSMark Johnston in_pcblbgroup_find(struct inpcb *inp) 2997cbb6b6eSMark Johnston { 3007cbb6b6eSMark Johnston struct inpcbinfo *pcbinfo; 3017cbb6b6eSMark Johnston struct inpcblbgroup *grp; 3027cbb6b6eSMark Johnston struct inpcblbgrouphead *hdr; 3037cbb6b6eSMark Johnston 3047cbb6b6eSMark Johnston INP_LOCK_ASSERT(inp); 3057cbb6b6eSMark Johnston 3067cbb6b6eSMark Johnston pcbinfo = inp->inp_pcbinfo; 3077cbb6b6eSMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 3087cbb6b6eSMark Johnston 3097cbb6b6eSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 3107cbb6b6eSMark Johnston INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 3117cbb6b6eSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 3127cbb6b6eSMark Johnston struct inpcb *inp1; 3137cbb6b6eSMark Johnston 3147cbb6b6eSMark Johnston for (unsigned int i = 0; i < grp->il_inpcnt; i++) { 3157cbb6b6eSMark Johnston if (inp == grp->il_inp[i]) 3167cbb6b6eSMark Johnston goto found; 3177cbb6b6eSMark Johnston } 3187cbb6b6eSMark Johnston LIST_FOREACH(inp1, &grp->il_pending, inp_lbgroup_list) { 3197cbb6b6eSMark Johnston if (inp == inp1) 3207cbb6b6eSMark Johnston goto found; 3217cbb6b6eSMark Johnston } 3227cbb6b6eSMark Johnston } 3237cbb6b6eSMark Johnston found: 3247cbb6b6eSMark Johnston return (grp); 3257cbb6b6eSMark Johnston } 3267cbb6b6eSMark Johnston 327a600aabeSMark Johnston static void 328a600aabeSMark Johnston in_pcblbgroup_insert(struct inpcblbgroup *grp, struct inpcb *inp) 329a600aabeSMark Johnston { 330a600aabeSMark Johnston KASSERT(grp->il_inpcnt < grp->il_inpsiz, 331a600aabeSMark Johnston ("invalid local group size %d and count %d", grp->il_inpsiz, 332a600aabeSMark Johnston grp->il_inpcnt)); 333a600aabeSMark Johnston INP_WLOCK_ASSERT(inp); 334a600aabeSMark Johnston 3357cbb6b6eSMark Johnston if (inp->inp_socket->so_proto->pr_listen != pr_listen_notsupp && 3367cbb6b6eSMark Johnston !SOLISTENING(inp->inp_socket)) { 3377cbb6b6eSMark Johnston /* 3387cbb6b6eSMark Johnston * If this is a TCP socket, it should not be visible to lbgroup 3397cbb6b6eSMark Johnston * lookups until listen() has been called. 3407cbb6b6eSMark Johnston */ 3417cbb6b6eSMark Johnston LIST_INSERT_HEAD(&grp->il_pending, inp, inp_lbgroup_list); 3428b3d2c19SMark Johnston grp->il_pendcnt++; 3437cbb6b6eSMark Johnston } else { 344a600aabeSMark Johnston grp->il_inp[grp->il_inpcnt] = inp; 345a600aabeSMark Johnston 346a600aabeSMark Johnston /* 3477cbb6b6eSMark Johnston * Synchronize with in_pcblookup_lbgroup(): make sure that we 3487cbb6b6eSMark Johnston * don't expose a null slot to the lookup path. 349a600aabeSMark Johnston */ 350a600aabeSMark Johnston atomic_store_rel_int(&grp->il_inpcnt, grp->il_inpcnt + 1); 351a600aabeSMark Johnston } 352a600aabeSMark Johnston 3537cbb6b6eSMark Johnston inp->inp_flags |= INP_INLBGROUP; 3547cbb6b6eSMark Johnston } 3557cbb6b6eSMark Johnston 3561a43cff9SSean Bruno static struct inpcblbgroup * 3571a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 3581a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 3591a43cff9SSean Bruno { 3601a43cff9SSean Bruno struct inpcblbgroup *grp; 3611a43cff9SSean Bruno int i; 3621a43cff9SSean Bruno 363a600aabeSMark Johnston grp = in_pcblbgroup_alloc(old_grp->il_cred, old_grp->il_vflag, 364a034518aSAndrew Gallatin old_grp->il_lport, &old_grp->il_dependladdr, size, 365bbd0084bSMark Johnston old_grp->il_numa_domain, old_grp->il_fibnum); 36679ee680bSMark Johnston if (grp == NULL) 3671a43cff9SSean Bruno return (NULL); 3681a43cff9SSean Bruno 3691a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 3701a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 3711a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 3721a43cff9SSean Bruno 3731a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 3741a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 3751a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 376a600aabeSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 3777cbb6b6eSMark Johnston LIST_SWAP(&old_grp->il_pending, &grp->il_pending, inpcb, 3787cbb6b6eSMark Johnston inp_lbgroup_list); 3798b3d2c19SMark Johnston grp->il_pendcnt = old_grp->il_pendcnt; 3808b3d2c19SMark Johnston old_grp->il_pendcnt = 0; 3811a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 3821a43cff9SSean Bruno return (grp); 3831a43cff9SSean Bruno } 3841a43cff9SSean Bruno 3851a43cff9SSean Bruno /* 3861a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 3871a43cff9SSean Bruno */ 3881a43cff9SSean Bruno static int 389a034518aSAndrew Gallatin in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain) 3901a43cff9SSean Bruno { 391a7026c7fSMark Johnston const static struct timeval interval = { 60, 0 }; 392a7026c7fSMark Johnston static struct timeval lastprint; 3931a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3941a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3951a43cff9SSean Bruno struct inpcblbgroup *grp; 39679ee680bSMark Johnston uint32_t idx; 397bbd0084bSMark Johnston int fib; 3981a43cff9SSean Bruno 3991a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 4001a43cff9SSean Bruno 4011a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 4021a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 4031a43cff9SSean Bruno 404bbd0084bSMark Johnston fib = (inp->inp_flags & INP_BOUNDFIB) != 0 ? 405bbd0084bSMark Johnston inp->inp_inc.inc_fibnum : RT_ALL_FIBS; 406bbd0084bSMark Johnston 4071a43cff9SSean Bruno #ifdef INET6 4081a43cff9SSean Bruno /* 4091a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 4101a43cff9SSean Bruno */ 4111a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 4121a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 4131a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 4141a43cff9SSean Bruno return (0); 4151a43cff9SSean Bruno } 4161a43cff9SSean Bruno #endif 4171a43cff9SSean Bruno 4189d2877fcSMark Johnston idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask); 41979ee680bSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 42054af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 421d93ec8cbSMark Johnston if (grp->il_cred->cr_prison == inp->inp_cred->cr_prison && 422d93ec8cbSMark Johnston grp->il_vflag == inp->inp_vflag && 4231a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 424a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain && 425bbd0084bSMark Johnston grp->il_fibnum == fib && 4261a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 4271a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 428d93ec8cbSMark Johnston sizeof(grp->il_dependladdr)) == 0) { 4291a43cff9SSean Bruno break; 4301a43cff9SSean Bruno } 431d93ec8cbSMark Johnston } 4321a43cff9SSean Bruno if (grp == NULL) { 4331a43cff9SSean Bruno /* Create new load balance group. */ 434a600aabeSMark Johnston grp = in_pcblbgroup_alloc(inp->inp_cred, inp->inp_vflag, 4351a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 436bbd0084bSMark Johnston INPCBLBGROUP_SIZMIN, numa_domain, fib); 43779ee680bSMark Johnston if (grp == NULL) 438*136c5e17SGleb Smirnoff return (ENOMEM); 439a600aabeSMark Johnston in_pcblbgroup_insert(grp, inp); 440a600aabeSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 4418b3d2c19SMark Johnston } else if (grp->il_inpcnt + grp->il_pendcnt == grp->il_inpsiz) { 4421a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 443a7026c7fSMark Johnston if (ratecheck(&lastprint, &interval)) 4441a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 4451a43cff9SSean Bruno ntohs(grp->il_lport)); 4461a43cff9SSean Bruno return (0); 4471a43cff9SSean Bruno } 4481a43cff9SSean Bruno 4491a43cff9SSean Bruno /* Expand this local group. */ 4501a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 45179ee680bSMark Johnston if (grp == NULL) 452*136c5e17SGleb Smirnoff return (ENOMEM); 453a600aabeSMark Johnston in_pcblbgroup_insert(grp, inp); 454a600aabeSMark Johnston } else { 455a600aabeSMark Johnston in_pcblbgroup_insert(grp, inp); 4561a43cff9SSean Bruno } 4571a43cff9SSean Bruno return (0); 4581a43cff9SSean Bruno } 4591a43cff9SSean Bruno 4601a43cff9SSean Bruno /* 4611a43cff9SSean Bruno * Remove PCB from load balance group. 4621a43cff9SSean Bruno */ 4631a43cff9SSean Bruno static void 4641a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 4651a43cff9SSean Bruno { 4661a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 4671a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 4681a43cff9SSean Bruno struct inpcblbgroup *grp; 4697cbb6b6eSMark Johnston struct inpcb *inp1; 4701a43cff9SSean Bruno int i; 4711a43cff9SSean Bruno 4721a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 4731a43cff9SSean Bruno 4741a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 475a43e7a96SGleb Smirnoff MPASS(inp->inp_flags & INP_INLBGROUP); 4761a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 4771a43cff9SSean Bruno 4781a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 4799d2877fcSMark Johnston INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 48054af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 4811a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 4821a43cff9SSean Bruno if (grp->il_inp[i] != inp) 4831a43cff9SSean Bruno continue; 4841a43cff9SSean Bruno 4857cbb6b6eSMark Johnston if (grp->il_inpcnt == 1 && 4867cbb6b6eSMark Johnston LIST_EMPTY(&grp->il_pending)) { 4871a43cff9SSean Bruno /* We are the last, free this local group. */ 4881a43cff9SSean Bruno in_pcblbgroup_free(grp); 4891a43cff9SSean Bruno } else { 490a600aabeSMark Johnston grp->il_inp[i] = 491a600aabeSMark Johnston grp->il_inp[grp->il_inpcnt - 1]; 492a600aabeSMark Johnston 493a600aabeSMark Johnston /* 494a600aabeSMark Johnston * Synchronize with in_pcblookup_lbgroup(). 495a600aabeSMark Johnston */ 496a600aabeSMark Johnston atomic_store_rel_int(&grp->il_inpcnt, 497a600aabeSMark Johnston grp->il_inpcnt - 1); 4981a43cff9SSean Bruno } 499a43e7a96SGleb Smirnoff inp->inp_flags &= ~INP_INLBGROUP; 5001a43cff9SSean Bruno return; 5011a43cff9SSean Bruno } 5027cbb6b6eSMark Johnston LIST_FOREACH(inp1, &grp->il_pending, inp_lbgroup_list) { 5037cbb6b6eSMark Johnston if (inp == inp1) { 5047cbb6b6eSMark Johnston LIST_REMOVE(inp, inp_lbgroup_list); 5058b3d2c19SMark Johnston grp->il_pendcnt--; 5067cbb6b6eSMark Johnston inp->inp_flags &= ~INP_INLBGROUP; 5077cbb6b6eSMark Johnston return; 5081a43cff9SSean Bruno } 5097cbb6b6eSMark Johnston } 5107cbb6b6eSMark Johnston } 5117cbb6b6eSMark Johnston __assert_unreachable(); 5121a43cff9SSean Bruno } 5131a43cff9SSean Bruno 514a034518aSAndrew Gallatin int 515a034518aSAndrew Gallatin in_pcblbgroup_numa(struct inpcb *inp, int arg) 516a034518aSAndrew Gallatin { 517a034518aSAndrew Gallatin struct inpcbinfo *pcbinfo; 5187cbb6b6eSMark Johnston int error; 519a034518aSAndrew Gallatin uint8_t numa_domain; 520a034518aSAndrew Gallatin 521a034518aSAndrew Gallatin switch (arg) { 522a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_NODOM: 523a034518aSAndrew Gallatin numa_domain = M_NODOM; 524a034518aSAndrew Gallatin break; 525a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_CURDOM: 526a034518aSAndrew Gallatin numa_domain = PCPU_GET(domain); 527a034518aSAndrew Gallatin break; 528a034518aSAndrew Gallatin default: 529a034518aSAndrew Gallatin if (arg < 0 || arg >= vm_ndomains) 530a034518aSAndrew Gallatin return (EINVAL); 531a034518aSAndrew Gallatin numa_domain = arg; 532a034518aSAndrew Gallatin } 533a034518aSAndrew Gallatin 534a034518aSAndrew Gallatin pcbinfo = inp->inp_pcbinfo; 535a034518aSAndrew Gallatin INP_WLOCK_ASSERT(inp); 536a034518aSAndrew Gallatin INP_HASH_WLOCK(pcbinfo); 5377cbb6b6eSMark Johnston if (in_pcblbgroup_find(inp) != NULL) { 538a034518aSAndrew Gallatin /* Remove it from the old group. */ 539a034518aSAndrew Gallatin in_pcbremlbgrouphash(inp); 540a034518aSAndrew Gallatin /* Add it to the new group based on numa domain. */ 541a034518aSAndrew Gallatin in_pcbinslbgrouphash(inp, numa_domain); 5427cbb6b6eSMark Johnston error = 0; 5437cbb6b6eSMark Johnston } else { 5447cbb6b6eSMark Johnston error = ENOENT; 545a034518aSAndrew Gallatin } 546a034518aSAndrew Gallatin INP_HASH_WUNLOCK(pcbinfo); 5477cbb6b6eSMark Johnston return (error); 548a034518aSAndrew Gallatin } 549a034518aSAndrew Gallatin 550db0ac6deSCy Schubert /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 551db0ac6deSCy Schubert CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb))); 552db0ac6deSCy Schubert 553cc487c16SGleb Smirnoff /* 554fec8a8c7SGleb Smirnoff * Initialize an inpcbinfo - a per-VNET instance of connections db. 5559bcd427bSRobert Watson */ 5569bcd427bSRobert Watson void 557fec8a8c7SGleb Smirnoff in_pcbinfo_init(struct inpcbinfo *pcbinfo, struct inpcbstorage *pcbstor, 558fec8a8c7SGleb Smirnoff u_int hash_nelements, u_int porthash_nelements) 5599bcd427bSRobert Watson { 5609bcd427bSRobert Watson 561fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_lock, pcbstor->ips_infolock_name, NULL, MTX_DEF); 562fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_hash_lock, pcbstor->ips_hashlock_name, 563fec8a8c7SGleb Smirnoff NULL, MTX_DEF); 5649bcd427bSRobert Watson #ifdef VIMAGE 5659bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 5669bcd427bSRobert Watson #endif 567db0ac6deSCy Schubert CK_LIST_INIT(&pcbinfo->ipi_listhead); 568fa046d87SRobert Watson pcbinfo->ipi_count = 0; 569fdb987beSMark Johnston pcbinfo->ipi_hash_exact = hashinit(hash_nelements, M_PCB, 570fdb987beSMark Johnston &pcbinfo->ipi_hashmask); 571fdb987beSMark Johnston pcbinfo->ipi_hash_wild = hashinit(hash_nelements, M_PCB, 5729bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 573db0ac6deSCy Schubert porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 5749bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 5759bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 5769d2877fcSMark Johnston pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 5771a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 578fec8a8c7SGleb Smirnoff pcbinfo->ipi_zone = pcbstor->ips_zone; 579db0ac6deSCy Schubert pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone); 5809bcd427bSRobert Watson } 5819bcd427bSRobert Watson 5829bcd427bSRobert Watson /* 5839bcd427bSRobert Watson * Destroy an inpcbinfo. 5849bcd427bSRobert Watson */ 5859bcd427bSRobert Watson void 5869bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 5879bcd427bSRobert Watson { 5889bcd427bSRobert Watson 589fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 590fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 591fa046d87SRobert Watson 592fdb987beSMark Johnston hashdestroy(pcbinfo->ipi_hash_exact, M_PCB, pcbinfo->ipi_hashmask); 593fdb987beSMark Johnston hashdestroy(pcbinfo->ipi_hash_wild, M_PCB, pcbinfo->ipi_hashmask); 5949bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 5959bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 5961a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 5971a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 598db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_hash_lock); 599db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_lock); 6009bcd427bSRobert Watson } 6019bcd427bSRobert Watson 6029bcd427bSRobert Watson /* 603fec8a8c7SGleb Smirnoff * Initialize a pcbstorage - per protocol zones to allocate inpcbs. 604fec8a8c7SGleb Smirnoff */ 605fec8a8c7SGleb Smirnoff static void inpcb_fini(void *, int); 606fec8a8c7SGleb Smirnoff void 607fec8a8c7SGleb Smirnoff in_pcbstorage_init(void *arg) 608fec8a8c7SGleb Smirnoff { 609fec8a8c7SGleb Smirnoff struct inpcbstorage *pcbstor = arg; 610fec8a8c7SGleb Smirnoff 611fec8a8c7SGleb Smirnoff pcbstor->ips_zone = uma_zcreate(pcbstor->ips_zone_name, 6125fd1a67eSMark Johnston pcbstor->ips_size, NULL, NULL, pcbstor->ips_pcbinit, 613c4a4b263SAndrew Gallatin inpcb_fini, UMA_ALIGN_CACHE, UMA_ZONE_SMR); 614fec8a8c7SGleb Smirnoff } 615fec8a8c7SGleb Smirnoff 616fec8a8c7SGleb Smirnoff /* 617fec8a8c7SGleb Smirnoff * Destroy a pcbstorage - used by unloadable protocols. 618fec8a8c7SGleb Smirnoff */ 619fec8a8c7SGleb Smirnoff void 620fec8a8c7SGleb Smirnoff in_pcbstorage_destroy(void *arg) 621fec8a8c7SGleb Smirnoff { 622fec8a8c7SGleb Smirnoff struct inpcbstorage *pcbstor = arg; 623fec8a8c7SGleb Smirnoff 624fec8a8c7SGleb Smirnoff uma_zdestroy(pcbstor->ips_zone); 625fec8a8c7SGleb Smirnoff } 626fec8a8c7SGleb Smirnoff 627fec8a8c7SGleb Smirnoff /* 628c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 629d915b280SStephan Uphoff * On success return with the PCB locked. 630c3229e05SDavid Greenman */ 631df8bae1dSRodney W. Grimes int 632d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 633df8bae1dSRodney W. Grimes { 634136d4f1cSRobert Watson struct inpcb *inp; 635fe5324acSJohn Baldwin #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 63613cf67f3SHajimu UMEMOTO int error; 637fe5324acSJohn Baldwin #endif 638a557af22SRobert Watson 639db0ac6deSCy Schubert inp = uma_zalloc_smr(pcbinfo->ipi_zone, M_NOWAIT); 640df8bae1dSRodney W. Grimes if (inp == NULL) 641df8bae1dSRodney W. Grimes return (ENOBUFS); 6426a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 64350575ce1SAndrew Gallatin #ifdef NUMA 64450575ce1SAndrew Gallatin inp->inp_numa_domain = M_NODOM; 64550575ce1SAndrew Gallatin #endif 64615bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 647df8bae1dSRodney W. Grimes inp->inp_socket = so; 64886d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 6498b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 650a557af22SRobert Watson #ifdef MAC 65130d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 652a557af22SRobert Watson if (error != 0) 653a557af22SRobert Watson goto out; 65430d239bcSRobert Watson mac_inpcb_create(so, inp); 655a557af22SRobert Watson #endif 656fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 657fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 6580bffde27SRobert Watson if (error != 0) { 6590bffde27SRobert Watson #ifdef MAC 6600bffde27SRobert Watson mac_inpcb_destroy(inp); 6610bffde27SRobert Watson #endif 662a557af22SRobert Watson goto out; 6630bffde27SRobert Watson } 664b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 665e3fd5ffdSRobert Watson #ifdef INET6 666340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 667c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV6PROTO | INP_IPV6; 668603724d3SBjoern A. Zeeb if (V_ip6_v6only) 66933841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 670c7a62c92SGleb Smirnoff #ifdef INET 671c7a62c92SGleb Smirnoff else 672c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 673c7a62c92SGleb Smirnoff #endif 674603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 67533841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 676c7a62c92SGleb Smirnoff inp->in6p_hops = -1; /* use kernel default */ 677c7a62c92SGleb Smirnoff } 678c7a62c92SGleb Smirnoff #endif 679c7a62c92SGleb Smirnoff #if defined(INET) && defined(INET6) 680c7a62c92SGleb Smirnoff else 681c7a62c92SGleb Smirnoff #endif 682c7a62c92SGleb Smirnoff #ifdef INET 683c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 68433841545SHajimu UMEMOTO #endif 685fdb987beSMark Johnston inp->inp_smr = SMR_SEQ_INVALID; 686fdb987beSMark Johnston 6878c1960d5SMike Karels /* 6888c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 6898c1960d5SMike Karels * to be cleaned up. 6908c1960d5SMike Karels */ 6918c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 692db0ac6deSCy Schubert refcount_init(&inp->inp_refcount, 1); /* Reference from socket. */ 693db0ac6deSCy Schubert INP_WLOCK(inp); 694db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 695db0ac6deSCy Schubert pcbinfo->ipi_count++; 696db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 697db0ac6deSCy Schubert CK_LIST_INSERT_HEAD(&pcbinfo->ipi_listhead, inp, inp_list); 698db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 699db0ac6deSCy Schubert so->so_pcb = inp; 700db0ac6deSCy Schubert 701db0ac6deSCy Schubert return (0); 702db0ac6deSCy Schubert 7037a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 704a557af22SRobert Watson out: 70530cf0fbfSRichard Scheffenegger crfree(inp->inp_cred); 70630cf0fbfSRichard Scheffenegger #ifdef INVARIANTS 70730cf0fbfSRichard Scheffenegger inp->inp_cred = NULL; 70830cf0fbfSRichard Scheffenegger #endif 709db0ac6deSCy Schubert uma_zfree_smr(pcbinfo->ipi_zone, inp); 710266f97b5SCy Schubert return (error); 711db0ac6deSCy Schubert #endif 712df8bae1dSRodney W. Grimes } 713df8bae1dSRodney W. Grimes 71467107f45SBjoern A. Zeeb #ifdef INET 715df8bae1dSRodney W. Grimes int 716bbd0084bSMark Johnston in_pcbbind(struct inpcb *inp, struct sockaddr_in *sin, int flags, 717bbd0084bSMark Johnston struct ucred *cred) 718df8bae1dSRodney W. Grimes { 7194b932371SIan Dowse int anonport, error; 7204b932371SIan Dowse 72196871af0SGleb Smirnoff KASSERT(sin == NULL || sin->sin_family == AF_INET, 72296871af0SGleb Smirnoff ("%s: invalid address family for %p", __func__, sin)); 72396871af0SGleb Smirnoff KASSERT(sin == NULL || sin->sin_len == sizeof(struct sockaddr_in), 72496871af0SGleb Smirnoff ("%s: invalid address length for %p", __func__, sin)); 7258501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 726fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 72759daba27SSam Leffler 7284b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 7294b932371SIan Dowse return (EINVAL); 73096871af0SGleb Smirnoff anonport = sin == NULL || sin->sin_port == 0; 73196871af0SGleb Smirnoff error = in_pcbbind_setup(inp, sin, &inp->inp_laddr.s_addr, 732bbd0084bSMark Johnston &inp->inp_lport, flags, cred); 7334b932371SIan Dowse if (error) 7344b932371SIan Dowse return (error); 735*136c5e17SGleb Smirnoff if (__predict_false((error = in_pcbinshash(inp)) != 0)) { 736*136c5e17SGleb Smirnoff MPASS(inp->inp_socket->so_options & SO_REUSEPORT_LB); 7374b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 7384b932371SIan Dowse inp->inp_lport = 0; 739bbd0084bSMark Johnston inp->inp_flags &= ~INP_BOUNDFIB; 740*136c5e17SGleb Smirnoff return (error); 7414b932371SIan Dowse } 7424b932371SIan Dowse if (anonport) 7434b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 7444b932371SIan Dowse return (0); 7454b932371SIan Dowse } 74667107f45SBjoern A. Zeeb #endif 7474b932371SIan Dowse 748efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 74925102351SMike Karels /* 75025102351SMike Karels * Assign a local port like in_pcb_lport(), but also used with connect() 75125102351SMike Karels * and a foreign address and port. If fsa is non-NULL, choose a local port 75225102351SMike Karels * that is unused with those, otherwise one that is completely unused. 7532fdbcbeaSMike Karels * lsa can be NULL for IPv6. 75425102351SMike Karels */ 755efc76f72SBjoern A. Zeeb int 756bafe022bSGleb Smirnoff in_pcb_lport_dest(const struct inpcb *inp, struct sockaddr *lsa, 757bafe022bSGleb Smirnoff u_short *lportp, struct sockaddr *fsa, u_short fport, struct ucred *cred, 758bafe022bSGleb Smirnoff int lookupflags) 759efc76f72SBjoern A. Zeeb { 760efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 761efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 762efc76f72SBjoern A. Zeeb unsigned short *lastport; 76319acc506SGleb Smirnoff int count, error; 764efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 765efc76f72SBjoern A. Zeeb #ifdef INET 76625102351SMike Karels struct in_addr laddr, faddr; 76725102351SMike Karels #endif 76825102351SMike Karels #ifdef INET6 76925102351SMike Karels struct in6_addr *laddr6, *faddr6; 770efc76f72SBjoern A. Zeeb #endif 771efc76f72SBjoern A. Zeeb 772efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 773efc76f72SBjoern A. Zeeb 774efc76f72SBjoern A. Zeeb /* 775efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 776efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 777efc76f72SBjoern A. Zeeb */ 778efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 779fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 780efc76f72SBjoern A. Zeeb 781efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 782efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 783efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 784efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 785efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 786cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 787efc76f72SBjoern A. Zeeb if (error) 788efc76f72SBjoern A. Zeeb return (error); 789efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 790efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 791efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 792efc76f72SBjoern A. Zeeb } else { 793efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 794efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 795efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 796efc76f72SBjoern A. Zeeb } 79719acc506SGleb Smirnoff 798efc76f72SBjoern A. Zeeb /* 799efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 800efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 801efc76f72SBjoern A. Zeeb * code path implementing all logic. 802efc76f72SBjoern A. Zeeb */ 803efc76f72SBjoern A. Zeeb if (first > last) { 804efc76f72SBjoern A. Zeeb aux = first; 805efc76f72SBjoern A. Zeeb first = last; 806efc76f72SBjoern A. Zeeb last = aux; 807efc76f72SBjoern A. Zeeb } 808efc76f72SBjoern A. Zeeb 809efc76f72SBjoern A. Zeeb #ifdef INET 810637f317cSMike Karels laddr.s_addr = INADDR_ANY; /* used by INET6+INET below too */ 8114d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 8122fdbcbeaSMike Karels if (lsa != NULL) 81325102351SMike Karels laddr = ((struct sockaddr_in *)lsa)->sin_addr; 81425102351SMike Karels if (fsa != NULL) 81525102351SMike Karels faddr = ((struct sockaddr_in *)fsa)->sin_addr; 816efc76f72SBjoern A. Zeeb } 817efc76f72SBjoern A. Zeeb #endif 81825102351SMike Karels #ifdef INET6 8192fdbcbeaSMike Karels laddr6 = NULL; 8202fdbcbeaSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 8212fdbcbeaSMike Karels if (lsa != NULL) 82225102351SMike Karels laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 82325102351SMike Karels if (fsa != NULL) 82425102351SMike Karels faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 82525102351SMike Karels } 82625102351SMike Karels #endif 82725102351SMike Karels 82825102351SMike Karels tmpinp = NULL; 829efc76f72SBjoern A. Zeeb lport = *lportp; 830efc76f72SBjoern A. Zeeb 83119acc506SGleb Smirnoff if (V_ipport_randomized) 832efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 833efc76f72SBjoern A. Zeeb 834efc76f72SBjoern A. Zeeb count = last - first; 835efc76f72SBjoern A. Zeeb 836efc76f72SBjoern A. Zeeb do { 837efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 838efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 839efc76f72SBjoern A. Zeeb ++*lastport; 840efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 841efc76f72SBjoern A. Zeeb *lastport = first; 842efc76f72SBjoern A. Zeeb lport = htons(*lastport); 843efc76f72SBjoern A. Zeeb 84425102351SMike Karels if (fsa != NULL) { 84525102351SMike Karels #ifdef INET 84625102351SMike Karels if (lsa->sa_family == AF_INET) { 84725102351SMike Karels tmpinp = in_pcblookup_hash_locked(pcbinfo, 84825102351SMike Karels faddr, fport, laddr, lport, lookupflags, 849da806e8dSMark Johnston M_NODOM, RT_ALL_FIBS); 85025102351SMike Karels } 85125102351SMike Karels #endif 85225102351SMike Karels #ifdef INET6 85325102351SMike Karels if (lsa->sa_family == AF_INET6) { 85425102351SMike Karels tmpinp = in6_pcblookup_hash_locked(pcbinfo, 85525102351SMike Karels faddr6, fport, laddr6, lport, lookupflags, 856da806e8dSMark Johnston M_NODOM, RT_ALL_FIBS); 85725102351SMike Karels } 85825102351SMike Karels #endif 85925102351SMike Karels } else { 860efc76f72SBjoern A. Zeeb #ifdef INET6 861637f317cSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 862efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 8639a413162SMark Johnston &inp->in6p_laddr, lport, RT_ALL_FIBS, 8649a413162SMark Johnston lookupflags, cred); 865637f317cSMike Karels #ifdef INET 866637f317cSMike Karels if (tmpinp == NULL && 867637f317cSMike Karels (inp->inp_vflag & INP_IPV4)) 868637f317cSMike Karels tmpinp = in_pcblookup_local(pcbinfo, 8699a413162SMark Johnston laddr, lport, RT_ALL_FIBS, 8709a413162SMark Johnston lookupflags, cred); 871637f317cSMike Karels #endif 872637f317cSMike Karels } 873efc76f72SBjoern A. Zeeb #endif 874efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 875efc76f72SBjoern A. Zeeb else 876efc76f72SBjoern A. Zeeb #endif 877efc76f72SBjoern A. Zeeb #ifdef INET 878efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 8799a413162SMark Johnston lport, RT_ALL_FIBS, lookupflags, cred); 880efc76f72SBjoern A. Zeeb #endif 88125102351SMike Karels } 882efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 883efc76f72SBjoern A. Zeeb 884efc76f72SBjoern A. Zeeb *lportp = lport; 885efc76f72SBjoern A. Zeeb 886efc76f72SBjoern A. Zeeb return (0); 887efc76f72SBjoern A. Zeeb } 888efdf104bSMikolaj Golub 889efdf104bSMikolaj Golub /* 89025102351SMike Karels * Select a local port (number) to use. 89125102351SMike Karels */ 89225102351SMike Karels int 89325102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 89425102351SMike Karels struct ucred *cred, int lookupflags) 89525102351SMike Karels { 89625102351SMike Karels struct sockaddr_in laddr; 89725102351SMike Karels 89825102351SMike Karels if (laddrp) { 89925102351SMike Karels bzero(&laddr, sizeof(laddr)); 90025102351SMike Karels laddr.sin_family = AF_INET; 90125102351SMike Karels laddr.sin_addr = *laddrp; 90225102351SMike Karels } 90325102351SMike Karels return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 90425102351SMike Karels NULL, lportp, NULL, 0, cred, lookupflags)); 90525102351SMike Karels } 906efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 907efc76f72SBjoern A. Zeeb 908d5bb8bd3SAdrian Chadd #ifdef INET 9090a100a6fSAdrian Chadd /* 91001f8ce83SMark Johnston * Determine whether the inpcb can be bound to the specified address/port tuple. 91101f8ce83SMark Johnston */ 91201f8ce83SMark Johnston static int 91301f8ce83SMark Johnston in_pcbbind_avail(struct inpcb *inp, const struct in_addr laddr, 914bbd0084bSMark Johnston const u_short lport, const int fib, int sooptions, int lookupflags, 915bbd0084bSMark Johnston struct ucred *cred) 91601f8ce83SMark Johnston { 91701f8ce83SMark Johnston int reuseport, reuseport_lb; 91801f8ce83SMark Johnston 91901f8ce83SMark Johnston INP_LOCK_ASSERT(inp); 92001f8ce83SMark Johnston INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 92101f8ce83SMark Johnston 92201f8ce83SMark Johnston reuseport = (sooptions & SO_REUSEPORT); 92301f8ce83SMark Johnston reuseport_lb = (sooptions & SO_REUSEPORT_LB); 92401f8ce83SMark Johnston 92501f8ce83SMark Johnston if (IN_MULTICAST(ntohl(laddr.s_addr))) { 92601f8ce83SMark Johnston /* 92701f8ce83SMark Johnston * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 92801f8ce83SMark Johnston * allow complete duplication of binding if 92901f8ce83SMark Johnston * SO_REUSEPORT is set, or if SO_REUSEADDR is set 93001f8ce83SMark Johnston * and a multicast address is bound on both 93101f8ce83SMark Johnston * new and duplicated sockets. 93201f8ce83SMark Johnston */ 93301f8ce83SMark Johnston if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0) 93401f8ce83SMark Johnston reuseport = SO_REUSEADDR | SO_REUSEPORT; 93501f8ce83SMark Johnston /* 93601f8ce83SMark Johnston * XXX: How to deal with SO_REUSEPORT_LB here? 93701f8ce83SMark Johnston * Treat same as SO_REUSEPORT for now. 93801f8ce83SMark Johnston */ 93901f8ce83SMark Johnston if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0) 94001f8ce83SMark Johnston reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB; 94101f8ce83SMark Johnston } else if (!in_nullhost(laddr)) { 94201f8ce83SMark Johnston struct sockaddr_in sin; 94301f8ce83SMark Johnston 94401f8ce83SMark Johnston memset(&sin, 0, sizeof(sin)); 94501f8ce83SMark Johnston sin.sin_family = AF_INET; 94601f8ce83SMark Johnston sin.sin_len = sizeof(sin); 94701f8ce83SMark Johnston sin.sin_addr = laddr; 94801f8ce83SMark Johnston 94901f8ce83SMark Johnston /* 95001f8ce83SMark Johnston * Is the address a local IP address? 95101f8ce83SMark Johnston * If INP_BINDANY is set, then the socket may be bound 95201f8ce83SMark Johnston * to any endpoint address, local or not. 95301f8ce83SMark Johnston */ 95401f8ce83SMark Johnston if ((inp->inp_flags & INP_BINDANY) == 0 && 95501f8ce83SMark Johnston ifa_ifwithaddr_check((const struct sockaddr *)&sin) == 0) 95601f8ce83SMark Johnston return (EADDRNOTAVAIL); 95701f8ce83SMark Johnston } 95801f8ce83SMark Johnston 95901f8ce83SMark Johnston if (lport != 0) { 96001f8ce83SMark Johnston struct inpcb *t; 96101f8ce83SMark Johnston 96201f8ce83SMark Johnston if (ntohs(lport) <= V_ipport_reservedhigh && 96301f8ce83SMark Johnston ntohs(lport) >= V_ipport_reservedlow && 96401f8ce83SMark Johnston priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 96501f8ce83SMark Johnston return (EACCES); 96601f8ce83SMark Johnston 96701f8ce83SMark Johnston if (!IN_MULTICAST(ntohl(laddr.s_addr)) && 96801f8ce83SMark Johnston priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 969c9756953SMark Johnston /* 970c9756953SMark Johnston * If a socket owned by a different user is already 971c9756953SMark Johnston * bound to this port, fail. In particular, SO_REUSE* 972c9756953SMark Johnston * can only be used to share a port among sockets owned 973c9756953SMark Johnston * by the same user. 974c9756953SMark Johnston * 975c9756953SMark Johnston * However, we can share a port with a connected socket 976c9756953SMark Johnston * which has a unique 4-tuple. 977c9756953SMark Johnston */ 97801f8ce83SMark Johnston t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport, 9799a413162SMark Johnston RT_ALL_FIBS, INPLOOKUP_WILDCARD, cred); 98001f8ce83SMark Johnston if (t != NULL && 98101f8ce83SMark Johnston (inp->inp_socket->so_type != SOCK_STREAM || 98201f8ce83SMark Johnston in_nullhost(t->inp_faddr)) && 98301f8ce83SMark Johnston (inp->inp_cred->cr_uid != t->inp_cred->cr_uid)) 98401f8ce83SMark Johnston return (EADDRINUSE); 98501f8ce83SMark Johnston } 986bbd0084bSMark Johnston t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport, fib, 987bbd0084bSMark Johnston lookupflags, cred); 98801f8ce83SMark Johnston if (t != NULL && ((reuseport | reuseport_lb) & 98901f8ce83SMark Johnston t->inp_socket->so_options) == 0) { 99001f8ce83SMark Johnston #ifdef INET6 99101f8ce83SMark Johnston if (!in_nullhost(laddr) || 99201f8ce83SMark Johnston !in_nullhost(t->inp_laddr) || 99301f8ce83SMark Johnston (inp->inp_vflag & INP_IPV6PROTO) == 0 || 99401f8ce83SMark Johnston (t->inp_vflag & INP_IPV6PROTO) == 0) 99501f8ce83SMark Johnston #endif 99601f8ce83SMark Johnston return (EADDRINUSE); 99701f8ce83SMark Johnston } 99801f8ce83SMark Johnston } 99901f8ce83SMark Johnston return (0); 100001f8ce83SMark Johnston } 100101f8ce83SMark Johnston 100201f8ce83SMark Johnston /* 10034b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 10044b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 10054b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 10064b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 10074b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 10084b932371SIan Dowse * 10094b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 10104b932371SIan Dowse */ 10114b932371SIan Dowse int 101296871af0SGleb Smirnoff in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, 1013bbd0084bSMark Johnston u_short *lportp, int flags, struct ucred *cred) 10144b932371SIan Dowse { 10154b932371SIan Dowse struct socket *so = inp->inp_socket; 10164b932371SIan Dowse struct in_addr laddr; 1017df8bae1dSRodney W. Grimes u_short lport = 0; 1018bbd0084bSMark Johnston int error, fib, lookupflags, sooptions; 1019df8bae1dSRodney W. Grimes 10208501a69cSRobert Watson /* 1021fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 10228501a69cSRobert Watson */ 102359daba27SSam Leffler INP_LOCK_ASSERT(inp); 1024ffb3d384SMark Johnston INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 102559daba27SSam Leffler 10264b932371SIan Dowse laddr.s_addr = *laddrp; 102796871af0SGleb Smirnoff if (sin != NULL && laddr.s_addr != INADDR_ANY) 1028df8bae1dSRodney W. Grimes return (EINVAL); 102901f8ce83SMark Johnston 103001f8ce83SMark Johnston lookupflags = 0; 103101f8ce83SMark Johnston sooptions = atomic_load_int(&so->so_options); 103201f8ce83SMark Johnston if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0) 103368e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 103496871af0SGleb Smirnoff if (sin == NULL) { 10357c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 10367c2f3cb9SJamie Gritton return (error); 10377c2f3cb9SJamie Gritton } else { 1038f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 1039f161d294SMark Johnston ("%s: invalid family for address %p", __func__, sin)); 1040f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 1041f161d294SMark Johnston ("%s: invalid length for address %p", __func__, sin)); 1042f161d294SMark Johnston 1043b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 1044b89e82ddSJamie Gritton if (error) 1045b89e82ddSJamie Gritton return (error); 10464b932371SIan Dowse if (sin->sin_port != *lportp) { 10474b932371SIan Dowse /* Don't allow the port to change. */ 10484b932371SIan Dowse if (*lportp != 0) 10494b932371SIan Dowse return (EINVAL); 1050df8bae1dSRodney W. Grimes lport = sin->sin_port; 10514b932371SIan Dowse } 105245a77bf2SMark Johnston laddr = sin->sin_addr; 105345a77bf2SMark Johnston 1054bbd0084bSMark Johnston fib = (flags & INPBIND_FIB) != 0 ? inp->inp_inc.inc_fibnum : 1055bbd0084bSMark Johnston RT_ALL_FIBS; 1056bbd0084bSMark Johnston 105701f8ce83SMark Johnston /* See if this address/port combo is available. */ 1058bbd0084bSMark Johnston error = in_pcbbind_avail(inp, laddr, lport, fib, sooptions, 105945a77bf2SMark Johnston lookupflags, cred); 106001f8ce83SMark Johnston if (error != 0) 106101f8ce83SMark Johnston return (error); 1062df8bae1dSRodney W. Grimes } 10634b932371SIan Dowse if (*lportp != 0) 10644b932371SIan Dowse lport = *lportp; 106533b3ac06SPeter Wemm if (lport == 0) { 10664616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 1067efc76f72SBjoern A. Zeeb if (error != 0) 1068efc76f72SBjoern A. Zeeb return (error); 106933b3ac06SPeter Wemm } 10704b932371SIan Dowse *laddrp = laddr.s_addr; 10714b932371SIan Dowse *lportp = lport; 1072bbd0084bSMark Johnston if ((flags & INPBIND_FIB) != 0) 1073bbd0084bSMark Johnston inp->inp_flags |= INP_BOUNDFIB; 1074df8bae1dSRodney W. Grimes return (0); 1075df8bae1dSRodney W. Grimes } 1076df8bae1dSRodney W. Grimes 1077999f1343SGarrett Wollman /* 10785200e00eSIan Dowse * Connect from a socket to a specified address. 10795200e00eSIan Dowse * Both address and port must be specified in argument sin. 10805200e00eSIan Dowse * If don't have a local address for this socket yet, 10815200e00eSIan Dowse * then pick one. 1082999f1343SGarrett Wollman */ 1083999f1343SGarrett Wollman int 10840b4539eeSGleb Smirnoff in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred) 1085999f1343SGarrett Wollman { 10862af953b1SGleb Smirnoff struct in_addr laddr, faddr; 10872af953b1SGleb Smirnoff u_short lport; 10882af953b1SGleb Smirnoff int error; 10892af953b1SGleb Smirnoff bool anonport; 1090df8bae1dSRodney W. Grimes 10918501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1092fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1093fdb987beSMark Johnston KASSERT(in_nullhost(inp->inp_faddr), 1094fdb987beSMark Johnston ("%s: inp is already connected", __func__)); 10952af953b1SGleb Smirnoff KASSERT(sin->sin_family == AF_INET, 10962af953b1SGleb Smirnoff ("%s: invalid address family for %p", __func__, sin)); 10972af953b1SGleb Smirnoff KASSERT(sin->sin_len == sizeof(*sin), 10982af953b1SGleb Smirnoff ("%s: invalid address length for %p", __func__, sin)); 109927f74fd0SRobert Watson 11002af953b1SGleb Smirnoff if (sin->sin_port == 0) 11012af953b1SGleb Smirnoff return (EADDRNOTAVAIL); 11022af953b1SGleb Smirnoff 11032af953b1SGleb Smirnoff anonport = (inp->inp_lport == 0); 11042af953b1SGleb Smirnoff 11052af953b1SGleb Smirnoff if (__predict_false(in_broadcast(sin->sin_addr))) { 11062af953b1SGleb Smirnoff if (!V_connect_inaddr_wild || CK_STAILQ_EMPTY(&V_in_ifaddrhead)) 11072af953b1SGleb Smirnoff return (ENETUNREACH); 11082af953b1SGleb Smirnoff /* 11092af953b1SGleb Smirnoff * If the destination address is INADDR_ANY, use the primary 11102af953b1SGleb Smirnoff * local address. If the supplied address is INADDR_BROADCAST, 11112af953b1SGleb Smirnoff * and the primary interface supports broadcast, choose the 11122af953b1SGleb Smirnoff * broadcast address for that interface. 11132af953b1SGleb Smirnoff */ 11142af953b1SGleb Smirnoff if (in_nullhost(sin->sin_addr)) { 11152af953b1SGleb Smirnoff faddr = 11162af953b1SGleb Smirnoff IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 11172af953b1SGleb Smirnoff if ((error = prison_get_ip4(cred, &faddr)) != 0) 11182af953b1SGleb Smirnoff return (error); 11192af953b1SGleb Smirnoff } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) { 11202af953b1SGleb Smirnoff if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags 11212af953b1SGleb Smirnoff & IFF_BROADCAST) 11222af953b1SGleb Smirnoff faddr = satosin(&CK_STAILQ_FIRST( 11232af953b1SGleb Smirnoff &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 11242af953b1SGleb Smirnoff else 11252af953b1SGleb Smirnoff faddr = sin->sin_addr; 11262af953b1SGleb Smirnoff } 11272af953b1SGleb Smirnoff } else 11282af953b1SGleb Smirnoff faddr = sin->sin_addr; 11292af953b1SGleb Smirnoff 11302af953b1SGleb Smirnoff if (in_nullhost(inp->inp_laddr)) { 11312af953b1SGleb Smirnoff error = in_pcbladdr(inp, &faddr, &laddr, cred); 11322af953b1SGleb Smirnoff /* 11332af953b1SGleb Smirnoff * If the destination address is multicast and an outgoing 11342af953b1SGleb Smirnoff * interface has been set as a multicast option, prefer the 11352af953b1SGleb Smirnoff * address of that interface as our source address. 11362af953b1SGleb Smirnoff */ 11372af953b1SGleb Smirnoff if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 11382af953b1SGleb Smirnoff inp->inp_moptions != NULL && 11392af953b1SGleb Smirnoff inp->inp_moptions->imo_multicast_ifp != NULL) { 11402af953b1SGleb Smirnoff struct ifnet *ifp = 11412af953b1SGleb Smirnoff inp->inp_moptions->imo_multicast_ifp; 11422af953b1SGleb Smirnoff struct in_ifaddr *ia; 11432af953b1SGleb Smirnoff 11442af953b1SGleb Smirnoff CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 11452af953b1SGleb Smirnoff if (ia->ia_ifp == ifp && 11462af953b1SGleb Smirnoff prison_check_ip4(cred, 11472af953b1SGleb Smirnoff &ia->ia_addr.sin_addr) == 0) 11482af953b1SGleb Smirnoff break; 11492af953b1SGleb Smirnoff } 11502af953b1SGleb Smirnoff if (ia == NULL) 11512af953b1SGleb Smirnoff return (EADDRNOTAVAIL); 11522af953b1SGleb Smirnoff laddr = ia->ia_addr.sin_addr; 11532af953b1SGleb Smirnoff error = 0; 11542af953b1SGleb Smirnoff } 11555200e00eSIan Dowse if (error) 11565200e00eSIan Dowse return (error); 11572af953b1SGleb Smirnoff } else 11582af953b1SGleb Smirnoff laddr = inp->inp_laddr; 11595200e00eSIan Dowse 11602af953b1SGleb Smirnoff if (anonport) { 11612af953b1SGleb Smirnoff struct sockaddr_in lsin = { 11622af953b1SGleb Smirnoff .sin_family = AF_INET, 11632af953b1SGleb Smirnoff .sin_addr = laddr, 11642af953b1SGleb Smirnoff }; 11652af953b1SGleb Smirnoff struct sockaddr_in fsin = { 11662af953b1SGleb Smirnoff .sin_family = AF_INET, 11672af953b1SGleb Smirnoff .sin_addr = faddr, 11682af953b1SGleb Smirnoff }; 11692af953b1SGleb Smirnoff 11702af953b1SGleb Smirnoff error = in_pcb_lport_dest(inp, (struct sockaddr *)&lsin, 11712af953b1SGleb Smirnoff &lport, (struct sockaddr *)&fsin, sin->sin_port, cred, 11722af953b1SGleb Smirnoff INPLOOKUP_WILDCARD); 11732af953b1SGleb Smirnoff if (error) 11742af953b1SGleb Smirnoff return (error); 11752af953b1SGleb Smirnoff } else if (in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 11762af953b1SGleb Smirnoff sin->sin_port, laddr, inp->inp_lport, 0, M_NODOM, RT_ALL_FIBS) != 11772af953b1SGleb Smirnoff NULL) 11782af953b1SGleb Smirnoff return (EADDRINUSE); 11792af953b1SGleb Smirnoff else 11802af953b1SGleb Smirnoff lport = inp->inp_lport; 11812af953b1SGleb Smirnoff 1182452187b6SGleb Smirnoff MPASS(!in_nullhost(inp->inp_laddr) || inp->inp_lport != 0 || 1183452187b6SGleb Smirnoff !(inp->inp_flags & INP_INHASHLIST)); 1184452187b6SGleb Smirnoff 11852af953b1SGleb Smirnoff inp->inp_faddr = faddr; 11862af953b1SGleb Smirnoff inp->inp_fport = sin->sin_port; 1187452187b6SGleb Smirnoff inp->inp_laddr = laddr; 1188452187b6SGleb Smirnoff inp->inp_lport = lport; 1189fdb987beSMark Johnston 1190452187b6SGleb Smirnoff if ((inp->inp_flags & INP_INHASHLIST) == 0) { 1191452187b6SGleb Smirnoff error = in_pcbinshash(inp); 1192452187b6SGleb Smirnoff MPASS(error == 0); 1193452187b6SGleb Smirnoff } else 1194db0ac6deSCy Schubert in_pcbrehash(inp); 119524e5c2eeSGleb Smirnoff #ifdef ROUTE_MPATH 119624e5c2eeSGleb Smirnoff if (CALC_FLOWID_OUTBOUND) { 119724e5c2eeSGleb Smirnoff uint32_t hash_val, hash_type; 11982cb64cb2SGeorge V. Neville-Neil 119924e5c2eeSGleb Smirnoff hash_val = fib4_calc_software_hash(inp->inp_laddr, 12002af953b1SGleb Smirnoff inp->inp_faddr, 0, sin->sin_port, 120124e5c2eeSGleb Smirnoff inp->inp_socket->so_proto->pr_protocol, &hash_type); 120224e5c2eeSGleb Smirnoff 120324e5c2eeSGleb Smirnoff inp->inp_flowid = hash_val; 120424e5c2eeSGleb Smirnoff inp->inp_flowtype = hash_type; 120524e5c2eeSGleb Smirnoff } 120624e5c2eeSGleb Smirnoff #endif 12075200e00eSIan Dowse if (anonport) 12085200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 12095200e00eSIan Dowse return (0); 12105200e00eSIan Dowse } 12115200e00eSIan Dowse 12125200e00eSIan Dowse /* 12130895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 12140895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 12150895aec3SBjoern A. Zeeb */ 1216ae190832SSteven Hartland int 1217bafe022bSGleb Smirnoff in_pcbladdr(const struct inpcb *inp, struct in_addr *faddr, 1218bafe022bSGleb Smirnoff struct in_addr *laddr, struct ucred *cred) 12190895aec3SBjoern A. Zeeb { 12200895aec3SBjoern A. Zeeb struct ifaddr *ifa; 12210895aec3SBjoern A. Zeeb struct sockaddr *sa; 12229ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 12239ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 12240895aec3SBjoern A. Zeeb int error; 12250895aec3SBjoern A. Zeeb 1226c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1227413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1228ac1750ddSMark Johnston 1229592bcae8SBjoern A. Zeeb /* 1230592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1231592bcae8SBjoern A. Zeeb * if requested. 1232592bcae8SBjoern A. Zeeb */ 1233ac1750ddSMark Johnston if (!prison_saddrsel_ip4(cred, laddr)) 1234592bcae8SBjoern A. Zeeb return (0); 1235592bcae8SBjoern A. Zeeb 12360895aec3SBjoern A. Zeeb error = 0; 12370895aec3SBjoern A. Zeeb 12389ac7c6cfSAlexander V. Chernikov nh = NULL; 12399ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 12409ac7c6cfSAlexander V. Chernikov sin = &dst; 12410895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 12420895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 12430895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 12440895aec3SBjoern A. Zeeb 12450895aec3SBjoern A. Zeeb /* 12460895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 12470895aec3SBjoern A. Zeeb * else punt. 12480895aec3SBjoern A. Zeeb * 12490895aec3SBjoern A. Zeeb * Find out route to destination. 12500895aec3SBjoern A. Zeeb */ 12510895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 12529ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 12539ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 12540895aec3SBjoern A. Zeeb 12550895aec3SBjoern A. Zeeb /* 12560895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 12570895aec3SBjoern A. Zeeb * the outgoing interface. 12580895aec3SBjoern A. Zeeb * 12590895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 12600895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 12610895aec3SBjoern A. Zeeb * the source address from. 12620895aec3SBjoern A. Zeeb */ 12639ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 12648c0fec80SRobert Watson struct in_ifaddr *ia; 12650895aec3SBjoern A. Zeeb struct ifnet *ifp; 12660895aec3SBjoern A. Zeeb 12674f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 126858a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12694f6c66ccSMatt Macy if (ia == NULL) { 12704f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 127158a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12724f6c66ccSMatt Macy } 12730895aec3SBjoern A. Zeeb if (ia == NULL) { 12740895aec3SBjoern A. Zeeb error = ENETUNREACH; 12750895aec3SBjoern A. Zeeb goto done; 12760895aec3SBjoern A. Zeeb } 12770895aec3SBjoern A. Zeeb 1278ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 12790895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12800895aec3SBjoern A. Zeeb goto done; 12810895aec3SBjoern A. Zeeb } 12820895aec3SBjoern A. Zeeb 12830895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12840895aec3SBjoern A. Zeeb ia = NULL; 1285d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12860895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12870895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12880895aec3SBjoern A. Zeeb continue; 12890895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1290b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12910895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12920895aec3SBjoern A. Zeeb break; 12930895aec3SBjoern A. Zeeb } 12940895aec3SBjoern A. Zeeb } 12950895aec3SBjoern A. Zeeb if (ia != NULL) { 12960895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12970895aec3SBjoern A. Zeeb goto done; 12980895aec3SBjoern A. Zeeb } 12990895aec3SBjoern A. Zeeb 13000895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1301b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 13020895aec3SBjoern A. Zeeb goto done; 13030895aec3SBjoern A. Zeeb } 13040895aec3SBjoern A. Zeeb 13050895aec3SBjoern A. Zeeb /* 13060895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 13070895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 13080895aec3SBjoern A. Zeeb * In case of jails do those three steps: 13090895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 13100895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 13110895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 13120895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 13130895aec3SBjoern A. Zeeb */ 13149ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 13158c0fec80SRobert Watson struct in_ifaddr *ia; 13169317b04eSRobert Watson struct ifnet *ifp; 13170895aec3SBjoern A. Zeeb 13180895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 1319ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 13209ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 13210895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13220895aec3SBjoern A. Zeeb goto done; 13230895aec3SBjoern A. Zeeb } 13240895aec3SBjoern A. Zeeb 13250895aec3SBjoern A. Zeeb /* Jailed. */ 13260895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 13279ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1328b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 13299ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 13300895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13310895aec3SBjoern A. Zeeb goto done; 13320895aec3SBjoern A. Zeeb } 13330895aec3SBjoern A. Zeeb 13340895aec3SBjoern A. Zeeb /* 13350895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 13360895aec3SBjoern A. Zeeb * belonging to this jail. 13370895aec3SBjoern A. Zeeb */ 13388c0fec80SRobert Watson ia = NULL; 13399ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1340d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 13410895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 13420895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 13430895aec3SBjoern A. Zeeb continue; 13440895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1345b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 13460895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 13470895aec3SBjoern A. Zeeb break; 13480895aec3SBjoern A. Zeeb } 13490895aec3SBjoern A. Zeeb } 13500895aec3SBjoern A. Zeeb if (ia != NULL) { 13510895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13520895aec3SBjoern A. Zeeb goto done; 13530895aec3SBjoern A. Zeeb } 13540895aec3SBjoern A. Zeeb 13550895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1356b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 13570895aec3SBjoern A. Zeeb goto done; 13580895aec3SBjoern A. Zeeb } 13590895aec3SBjoern A. Zeeb 13600895aec3SBjoern A. Zeeb /* 13610895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 13620895aec3SBjoern A. Zeeb * to ourselves is here. 13630895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 13640895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 13650895aec3SBjoern A. Zeeb * a loopback interface. 13660895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 13670895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 13680895aec3SBjoern A. Zeeb */ 13699ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 13708c0fec80SRobert Watson struct in_ifaddr *ia; 13710895aec3SBjoern A. Zeeb 13729ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 137358a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 13740895aec3SBjoern A. Zeeb if (ia == NULL) 13759ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 137658a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1377f0bb05fcSQing Li if (ia == NULL) 13789ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 13790895aec3SBjoern A. Zeeb 1380ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 13810895aec3SBjoern A. Zeeb if (ia == NULL) { 13820895aec3SBjoern A. Zeeb error = ENETUNREACH; 13830895aec3SBjoern A. Zeeb goto done; 13840895aec3SBjoern A. Zeeb } 13850895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13860895aec3SBjoern A. Zeeb goto done; 13870895aec3SBjoern A. Zeeb } 13880895aec3SBjoern A. Zeeb 13890895aec3SBjoern A. Zeeb /* Jailed. */ 13900895aec3SBjoern A. Zeeb if (ia != NULL) { 13910895aec3SBjoern A. Zeeb struct ifnet *ifp; 13920895aec3SBjoern A. Zeeb 13930895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 13940895aec3SBjoern A. Zeeb ia = NULL; 1395d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 13960895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 13970895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 13980895aec3SBjoern A. Zeeb continue; 13990895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1400b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1401b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 14020895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 14030895aec3SBjoern A. Zeeb break; 14040895aec3SBjoern A. Zeeb } 14050895aec3SBjoern A. Zeeb } 14060895aec3SBjoern A. Zeeb if (ia != NULL) { 14070895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 14080895aec3SBjoern A. Zeeb goto done; 14090895aec3SBjoern A. Zeeb } 14100895aec3SBjoern A. Zeeb } 14110895aec3SBjoern A. Zeeb 14120895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1413b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 14140895aec3SBjoern A. Zeeb goto done; 14150895aec3SBjoern A. Zeeb } 14160895aec3SBjoern A. Zeeb 14170895aec3SBjoern A. Zeeb done: 1418713264f6SMark Johnston if (error == 0 && laddr->s_addr == INADDR_ANY) 1419713264f6SMark Johnston return (EHOSTUNREACH); 14200895aec3SBjoern A. Zeeb return (error); 14210895aec3SBjoern A. Zeeb } 14220895aec3SBjoern A. Zeeb 142326f9a767SRodney W. Grimes void 1424136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1425df8bae1dSRodney W. Grimes { 14266b348152SRobert Watson 14278501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1428fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1429fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 1430fdb987beSMark Johnston ("%s: inp %p was already disconnected", __func__, inp)); 1431df8bae1dSRodney W. Grimes 1432fdb987beSMark Johnston in_pcbremhash_locked(inp); 1433fdb987beSMark Johnston 1434fdb987beSMark Johnston /* See the comment in in_pcbinshash(). */ 1435fdb987beSMark Johnston inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr); 14362589ec0fSMark Johnston inp->inp_laddr.s_addr = INADDR_ANY; 1437df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1438df8bae1dSRodney W. Grimes inp->inp_fport = 0; 1439df8bae1dSRodney W. Grimes } 144083e521ecSBjoern A. Zeeb #endif /* INET */ 1441df8bae1dSRodney W. Grimes 14427cbb6b6eSMark Johnston void 14437cbb6b6eSMark Johnston in_pcblisten(struct inpcb *inp) 14447cbb6b6eSMark Johnston { 14457cbb6b6eSMark Johnston struct inpcblbgroup *grp; 14467cbb6b6eSMark Johnston 14477cbb6b6eSMark Johnston INP_WLOCK_ASSERT(inp); 14487cbb6b6eSMark Johnston 14497cbb6b6eSMark Johnston if ((inp->inp_flags & INP_INLBGROUP) != 0) { 14507cbb6b6eSMark Johnston struct inpcbinfo *pcbinfo; 14517cbb6b6eSMark Johnston 14527cbb6b6eSMark Johnston pcbinfo = inp->inp_pcbinfo; 14537cbb6b6eSMark Johnston INP_HASH_WLOCK(pcbinfo); 14547cbb6b6eSMark Johnston grp = in_pcblbgroup_find(inp); 14557cbb6b6eSMark Johnston LIST_REMOVE(inp, inp_lbgroup_list); 14568b3d2c19SMark Johnston grp->il_pendcnt--; 14577cbb6b6eSMark Johnston in_pcblbgroup_insert(grp, inp); 14587cbb6b6eSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 14597cbb6b6eSMark Johnston } 14607cbb6b6eSMark Johnston } 14617cbb6b6eSMark Johnston 14624c7c478dSRobert Watson /* 1463db0ac6deSCy Schubert * inpcb hash lookups are protected by SMR section. 1464db0ac6deSCy Schubert * 1465db0ac6deSCy Schubert * Once desired pcb has been found, switching from SMR section to a pcb 1466db0ac6deSCy Schubert * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1467db0ac6deSCy Schubert * here because SMR is a critical section. 1468db0ac6deSCy Schubert * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1469db0ac6deSCy Schubert */ 14707b92493aSMark Johnston void 1471db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1472db0ac6deSCy Schubert { 1473db0ac6deSCy Schubert 1474db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1475db0ac6deSCy Schubert rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1476db0ac6deSCy Schubert } 1477db0ac6deSCy Schubert 14787b92493aSMark Johnston void 1479db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1480db0ac6deSCy Schubert { 1481db0ac6deSCy Schubert 1482db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1483db0ac6deSCy Schubert rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1484db0ac6deSCy Schubert } 1485db0ac6deSCy Schubert 14867b92493aSMark Johnston int 1487db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1488db0ac6deSCy Schubert { 1489db0ac6deSCy Schubert 1490db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1491db0ac6deSCy Schubert rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1492db0ac6deSCy Schubert } 1493db0ac6deSCy Schubert 1494db0ac6deSCy Schubert static inline bool 1495f567d55fSGleb Smirnoff _inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock, const int ignflags) 1496db0ac6deSCy Schubert { 1497db0ac6deSCy Schubert 1498db0ac6deSCy Schubert MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1499db0ac6deSCy Schubert SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1500db0ac6deSCy Schubert 1501db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1502f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1503db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1504db0ac6deSCy Schubert inp_unlock(inp, lock); 1505db0ac6deSCy Schubert return (false); 1506db0ac6deSCy Schubert } 1507db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1508db0ac6deSCy Schubert return (true); 1509db0ac6deSCy Schubert } 1510db0ac6deSCy Schubert 1511db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1512db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1513db0ac6deSCy Schubert inp_lock(inp, lock); 1514db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) 1515db0ac6deSCy Schubert return (false); 1516db0ac6deSCy Schubert /* 1517db0ac6deSCy Schubert * inp acquired through refcount & lock for sure didn't went 1518db0ac6deSCy Schubert * through uma_zfree(). However, it may have already went 1519db0ac6deSCy Schubert * through in_pcbfree() and has another reference, that 1520db0ac6deSCy Schubert * prevented its release by our in_pcbrele(). 1521db0ac6deSCy Schubert */ 1522f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1523db0ac6deSCy Schubert inp_unlock(inp, lock); 1524db0ac6deSCy Schubert return (false); 1525db0ac6deSCy Schubert } 1526db0ac6deSCy Schubert return (true); 1527db0ac6deSCy Schubert } else { 1528db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1529db0ac6deSCy Schubert return (false); 1530db0ac6deSCy Schubert } 1531db0ac6deSCy Schubert } 1532db0ac6deSCy Schubert 1533f567d55fSGleb Smirnoff bool 1534f567d55fSGleb Smirnoff inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1535f567d55fSGleb Smirnoff { 1536f567d55fSGleb Smirnoff 1537f567d55fSGleb Smirnoff /* 1538f567d55fSGleb Smirnoff * in_pcblookup() family of functions ignore not only freed entries, 1539f567d55fSGleb Smirnoff * that may be found due to lockless access to the hash, but dropped 1540f567d55fSGleb Smirnoff * entries, too. 1541f567d55fSGleb Smirnoff */ 1542f567d55fSGleb Smirnoff return (_inp_smr_lock(inp, lock, INP_FREED | INP_DROPPED)); 1543f567d55fSGleb Smirnoff } 1544f567d55fSGleb Smirnoff 1545db0ac6deSCy Schubert /* 1546db0ac6deSCy Schubert * inp_next() - inpcb hash/list traversal iterator 1547db0ac6deSCy Schubert * 1548db0ac6deSCy Schubert * Requires initialized struct inpcb_iterator for context. 1549db0ac6deSCy Schubert * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1550db0ac6deSCy Schubert * 1551db0ac6deSCy Schubert * - Iterator can have either write-lock or read-lock semantics, that can not 1552db0ac6deSCy Schubert * be changed later. 1553db0ac6deSCy Schubert * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1554db0ac6deSCy Schubert * a single hash slot. Note: only rip_input() does the latter. 1555db0ac6deSCy Schubert * - Iterator may have optional bool matching function. The matching function 1556db0ac6deSCy Schubert * will be executed for each inpcb in the SMR context, so it can not acquire 1557db0ac6deSCy Schubert * locks and can safely access only immutable fields of inpcb. 1558db0ac6deSCy Schubert * 1559db0ac6deSCy Schubert * A fresh initialized iterator has NULL inpcb in its context and that 1560db0ac6deSCy Schubert * means that inp_next() call would return the very first inpcb on the list 1561db0ac6deSCy Schubert * locked with desired semantic. In all following calls the context pointer 1562db0ac6deSCy Schubert * shall hold the current inpcb pointer. The KPI user is not supposed to 1563db0ac6deSCy Schubert * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1564db0ac6deSCy Schubert * and write NULL to its context. After end of traversal an iterator can be 1565db0ac6deSCy Schubert * reused. 1566db0ac6deSCy Schubert * 1567db0ac6deSCy Schubert * List traversals have the following features/constraints: 1568db0ac6deSCy Schubert * - New entries won't be seen, as they are always added to the head of a list. 1569db0ac6deSCy Schubert * - Removed entries won't stop traversal as long as they are not added to 1570db0ac6deSCy Schubert * a different list. This is violated by in_pcbrehash(). 1571db0ac6deSCy Schubert */ 1572db0ac6deSCy Schubert #define II_LIST_FIRST(ipi, hash) \ 1573db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1574db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1575fdb987beSMark Johnston CK_LIST_FIRST(&(ipi)->ipi_hash_exact[(hash)])) 1576db0ac6deSCy Schubert #define II_LIST_NEXT(inp, hash) \ 1577db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1578db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_list) : \ 1579fdb987beSMark Johnston CK_LIST_NEXT((inp), inp_hash_exact)) 1580db0ac6deSCy Schubert #define II_LOCK_ASSERT(inp, lock) \ 1581db0ac6deSCy Schubert rw_assert(&(inp)->inp_lock, \ 1582db0ac6deSCy Schubert (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1583db0ac6deSCy Schubert struct inpcb * 1584db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii) 1585db0ac6deSCy Schubert { 1586db0ac6deSCy Schubert const struct inpcbinfo *ipi = ii->ipi; 1587db0ac6deSCy Schubert inp_match_t *match = ii->match; 1588db0ac6deSCy Schubert void *ctx = ii->ctx; 1589db0ac6deSCy Schubert inp_lookup_t lock = ii->lock; 1590db0ac6deSCy Schubert int hash = ii->hash; 1591db0ac6deSCy Schubert struct inpcb *inp; 1592db0ac6deSCy Schubert 1593db0ac6deSCy Schubert if (ii->inp == NULL) { /* First call. */ 1594db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1595db0ac6deSCy Schubert /* This is unrolled CK_LIST_FOREACH(). */ 1596db0ac6deSCy Schubert for (inp = II_LIST_FIRST(ipi, hash); 1597db0ac6deSCy Schubert inp != NULL; 1598db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash)) { 1599db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1600db0ac6deSCy Schubert continue; 1601f567d55fSGleb Smirnoff if (__predict_true(_inp_smr_lock(inp, lock, INP_FREED))) 1602db0ac6deSCy Schubert break; 1603db0ac6deSCy Schubert else { 1604db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1605db0ac6deSCy Schubert MPASS(inp != II_LIST_FIRST(ipi, hash)); 1606db0ac6deSCy Schubert inp = II_LIST_FIRST(ipi, hash); 1607430df2abSMichael Tuexen if (inp == NULL) 1608430df2abSMichael Tuexen break; 1609db0ac6deSCy Schubert } 1610db0ac6deSCy Schubert } 1611db0ac6deSCy Schubert 1612db0ac6deSCy Schubert if (inp == NULL) 1613db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1614db0ac6deSCy Schubert else 1615db0ac6deSCy Schubert ii->inp = inp; 1616db0ac6deSCy Schubert 1617db0ac6deSCy Schubert return (inp); 1618db0ac6deSCy Schubert } 1619db0ac6deSCy Schubert 1620db0ac6deSCy Schubert /* Not a first call. */ 1621db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1622db0ac6deSCy Schubert restart: 1623db0ac6deSCy Schubert inp = ii->inp; 1624db0ac6deSCy Schubert II_LOCK_ASSERT(inp, lock); 1625db0ac6deSCy Schubert next: 1626db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash); 1627db0ac6deSCy Schubert if (inp == NULL) { 1628db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1629db0ac6deSCy Schubert goto found; 1630db0ac6deSCy Schubert } 1631db0ac6deSCy Schubert 1632db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1633db0ac6deSCy Schubert goto next; 1634db0ac6deSCy Schubert 1635db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1636db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1637db0ac6deSCy Schubert /* 1638db0ac6deSCy Schubert * Entries are never inserted in middle of a list, thus 1639db0ac6deSCy Schubert * as long as we are in SMR, we can continue traversal. 1640db0ac6deSCy Schubert * Jump to 'restart' should yield in the same result, 1641db0ac6deSCy Schubert * but could produce unnecessary looping. Could this 1642db0ac6deSCy Schubert * looping be unbound? 1643db0ac6deSCy Schubert */ 1644db0ac6deSCy Schubert inp_unlock(inp, lock); 1645db0ac6deSCy Schubert goto next; 1646db0ac6deSCy Schubert } else { 1647db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1648db0ac6deSCy Schubert goto found; 1649db0ac6deSCy Schubert } 1650db0ac6deSCy Schubert } 1651db0ac6deSCy Schubert 1652db0ac6deSCy Schubert /* 1653db0ac6deSCy Schubert * Can't obtain lock immediately, thus going hard. Once we exit the 1654db0ac6deSCy Schubert * SMR section we can no longer jump to 'next', and our only stable 1655db0ac6deSCy Schubert * anchoring point is ii->inp, which we keep locked for this case, so 1656db0ac6deSCy Schubert * we jump to 'restart'. 1657db0ac6deSCy Schubert */ 1658db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1659db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1660db0ac6deSCy Schubert inp_lock(inp, lock); 1661db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) { 1662db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1663db0ac6deSCy Schubert goto restart; 1664db0ac6deSCy Schubert } 1665db0ac6deSCy Schubert /* 1666db0ac6deSCy Schubert * See comment in inp_smr_lock(). 1667db0ac6deSCy Schubert */ 1668db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1669db0ac6deSCy Schubert inp_unlock(inp, lock); 1670db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1671db0ac6deSCy Schubert goto restart; 1672db0ac6deSCy Schubert } 1673db0ac6deSCy Schubert } else 1674db0ac6deSCy Schubert goto next; 1675db0ac6deSCy Schubert 1676db0ac6deSCy Schubert found: 1677db0ac6deSCy Schubert inp_unlock(ii->inp, lock); 1678db0ac6deSCy Schubert ii->inp = inp; 1679db0ac6deSCy Schubert 1680db0ac6deSCy Schubert return (ii->inp); 1681db0ac6deSCy Schubert } 1682db0ac6deSCy Schubert 1683db0ac6deSCy Schubert /* 168479bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 1685db0ac6deSCy Schubert * stability of an inpcb pointer despite the inpcb lock being released or 1686db0ac6deSCy Schubert * SMR section exited. 168779bdc6e5SRobert Watson * 1688db0ac6deSCy Schubert * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1689c0a211c5SRobert Watson */ 169079bdc6e5SRobert Watson void 169179bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 16924c7c478dSRobert Watson { 1693db0ac6deSCy Schubert u_int old __diagused; 1694df8bae1dSRodney W. Grimes 1695db0ac6deSCy Schubert old = refcount_acquire(&inp->inp_refcount); 1696db0ac6deSCy Schubert KASSERT(old > 0, ("%s: refcount 0", __func__)); 169779bdc6e5SRobert Watson } 169879bdc6e5SRobert Watson 169979bdc6e5SRobert Watson /* 1700db0ac6deSCy Schubert * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1701db0ac6deSCy Schubert * freeing the pcb, if the reference was very last. 170279bdc6e5SRobert Watson */ 1703db0ac6deSCy Schubert bool 170479bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 170579bdc6e5SRobert Watson { 170679bdc6e5SRobert Watson 170779bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 170879bdc6e5SRobert Watson 1709c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1710db0ac6deSCy Schubert return (false); 1711db0ac6deSCy Schubert 1712db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1713db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 17145fd1a67eSMark Johnston crfree(inp->inp_cred); 17155fd1a67eSMark Johnston #ifdef INVARIANTS 17165fd1a67eSMark Johnston inp->inp_cred = NULL; 17175fd1a67eSMark Johnston #endif 1718df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1719db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1720db0ac6deSCy Schubert return (true); 1721df4e91d3SGleb Smirnoff } 172279bdc6e5SRobert Watson 1723db0ac6deSCy Schubert bool 172479bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 172579bdc6e5SRobert Watson { 172679bdc6e5SRobert Watson 172779bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 172879bdc6e5SRobert Watson 1729c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1730db0ac6deSCy Schubert return (false); 1731db0ac6deSCy Schubert 1732db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1733db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 17345fd1a67eSMark Johnston crfree(inp->inp_cred); 17355fd1a67eSMark Johnston #ifdef INVARIANTS 17365fd1a67eSMark Johnston inp->inp_cred = NULL; 17375fd1a67eSMark Johnston #endif 1738edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1739db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1740db0ac6deSCy Schubert return (true); 1741addf2b20SMatt Macy } 1742addf2b20SMatt Macy 17437b92493aSMark Johnston bool 17447b92493aSMark Johnston in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 17457b92493aSMark Johnston { 17467b92493aSMark Johnston 17477b92493aSMark Johnston return (lock == INPLOOKUP_RLOCKPCB ? 17487b92493aSMark Johnston in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 17497b92493aSMark Johnston } 17507b92493aSMark Johnston 175179bdc6e5SRobert Watson /* 175279bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 175379bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 175479bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 175579bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 1756db0ac6deSCy Schubert * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1757db0ac6deSCy Schubert * Almost all work, including removal from global lists, is done in this 1758db0ac6deSCy Schubert * context, where the pcbinfo lock is held. 175979bdc6e5SRobert Watson */ 176079bdc6e5SRobert Watson void 176179bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 176279bdc6e5SRobert Watson { 1763f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1764db0ac6deSCy Schubert #ifdef INET 1765db0ac6deSCy Schubert struct ip_moptions *imo; 1766db0ac6deSCy Schubert #endif 1767db0ac6deSCy Schubert #ifdef INET6 1768db0ac6deSCy Schubert struct ip6_moptions *im6o; 1769db0ac6deSCy Schubert #endif 177045a48d07SJonathan T. Looney 177179bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1772a13039e2SGleb Smirnoff KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1773db0ac6deSCy Schubert KASSERT((inp->inp_flags & INP_FREED) == 0, 1774db0ac6deSCy Schubert ("%s: called twice for pcb %p", __func__, inp)); 1775db0ac6deSCy Schubert 1776a13039e2SGleb Smirnoff /* 1777a13039e2SGleb Smirnoff * in_pcblookup_local() and in6_pcblookup_local() may return an inpcb 1778a13039e2SGleb Smirnoff * from the hash without acquiring inpcb lock, they rely on the hash 1779a13039e2SGleb Smirnoff * lock, thus in_pcbremhash() should be the first action. 1780a13039e2SGleb Smirnoff */ 1781a13039e2SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 1782a13039e2SGleb Smirnoff in_pcbremhash(inp); 1783db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 1784db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1785db0ac6deSCy Schubert pcbinfo->ipi_count--; 1786db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_list); 1787db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 1788db0ac6deSCy Schubert 1789a13039e2SGleb Smirnoff #ifdef RATELIMIT 1790a13039e2SGleb Smirnoff if (inp->inp_snd_tag != NULL) 1791a13039e2SGleb Smirnoff in_pcbdetach_txrtlmt(inp); 1792a13039e2SGleb Smirnoff #endif 1793a13039e2SGleb Smirnoff inp->inp_flags |= INP_FREED; 1794a13039e2SGleb Smirnoff inp->inp_socket->so_pcb = NULL; 1795a13039e2SGleb Smirnoff inp->inp_socket = NULL; 1796db0ac6deSCy Schubert 1797fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1798db0ac6deSCy Schubert #ifdef MAC 1799db0ac6deSCy Schubert mac_inpcb_destroy(inp); 1800db0ac6deSCy Schubert #endif 1801db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1802db0ac6deSCy Schubert if (inp->inp_sp != NULL) 1803db0ac6deSCy Schubert ipsec_delete_pcbpolicy(inp); 1804db0ac6deSCy Schubert #endif 1805db0ac6deSCy Schubert #ifdef INET 1806db0ac6deSCy Schubert if (inp->inp_options) 1807db0ac6deSCy Schubert (void)m_free(inp->inp_options); 18084a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->inp_options); 1809db0ac6deSCy Schubert imo = inp->inp_moptions; 18104a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->inp_moptions); 1811db0ac6deSCy Schubert #endif 1812db0ac6deSCy Schubert #ifdef INET6 1813db0ac6deSCy Schubert if (inp->inp_vflag & INP_IPV6PROTO) { 1814db0ac6deSCy Schubert ip6_freepcbopts(inp->in6p_outputopts); 18154a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->in6p_outputopts); 1816db0ac6deSCy Schubert im6o = inp->in6p_moptions; 18174a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->in6p_moptions); 1818db0ac6deSCy Schubert } else 1819db0ac6deSCy Schubert im6o = NULL; 1820db0ac6deSCy Schubert #endif 1821db0ac6deSCy Schubert 1822db0ac6deSCy Schubert if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1823cb6bb230SMatt Macy INP_WUNLOCK(inp); 1824db0ac6deSCy Schubert } 1825db0ac6deSCy Schubert #ifdef INET6 1826db0ac6deSCy Schubert ip6_freemoptions(im6o); 1827db0ac6deSCy Schubert #endif 1828db0ac6deSCy Schubert #ifdef INET 1829db0ac6deSCy Schubert inp_freemoptions(imo); 1830db0ac6deSCy Schubert #endif 1831eb93b99dSGleb Smirnoff } 1832eb93b99dSGleb Smirnoff 1833eb93b99dSGleb Smirnoff /* 1834eb93b99dSGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 1835eb93b99dSGleb Smirnoff * different name to the lock. But they all are disposed the same. 1836eb93b99dSGleb Smirnoff */ 1837eb93b99dSGleb Smirnoff static void 1838eb93b99dSGleb Smirnoff inpcb_fini(void *mem, int size) 1839eb93b99dSGleb Smirnoff { 1840eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1841eb93b99dSGleb Smirnoff 1842eb93b99dSGleb Smirnoff INP_LOCK_DESTROY(inp); 184328696211SRobert Watson } 184428696211SRobert Watson 184528696211SRobert Watson /* 1846c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1847c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1848c0a211c5SRobert Watson * 1849c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1850c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1851c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1852c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1853c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1854c0a211c5SRobert Watson * in_pcbdetach(). 1855c0a211c5SRobert Watson * 1856c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1857bbbd7aabSGleb Smirnoff * in_pcbpurgeif0()? 185810702a28SRobert Watson */ 185910702a28SRobert Watson void 186010702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 186110702a28SRobert Watson { 186210702a28SRobert Watson 18638501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 186410702a28SRobert Watson 1865ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 18663ba34b07SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 18673ba34b07SGleb Smirnoff in_pcbremhash(inp); 186810702a28SRobert Watson } 186910702a28SRobert Watson 187067107f45SBjoern A. Zeeb #ifdef INET 187154d642bbSRobert Watson /* 187254d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 187354d642bbSRobert Watson */ 1874117bcae7SGarrett Wollman int 18750fac350cSGleb Smirnoff in_getsockaddr(struct socket *so, struct sockaddr *sa) 1876df8bae1dSRodney W. Grimes { 1877136d4f1cSRobert Watson struct inpcb *inp; 187842fa505bSDavid Greenman 1879fdc984f7STor Egge inp = sotoinpcb(so); 188054d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 18816466b28aSRobert Watson 18820fac350cSGleb Smirnoff *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 18830fac350cSGleb Smirnoff .sin_len = sizeof(struct sockaddr_in), 18840fac350cSGleb Smirnoff .sin_family = AF_INET, 18850fac350cSGleb Smirnoff .sin_port = inp->inp_lport, 18860fac350cSGleb Smirnoff .sin_addr = inp->inp_laddr, 18870fac350cSGleb Smirnoff }; 188842fa505bSDavid Greenman 18890fac350cSGleb Smirnoff return (0); 1890df8bae1dSRodney W. Grimes } 1891df8bae1dSRodney W. Grimes 1892117bcae7SGarrett Wollman int 18930fac350cSGleb Smirnoff in_getpeeraddr(struct socket *so, struct sockaddr *sa) 1894df8bae1dSRodney W. Grimes { 1895136d4f1cSRobert Watson struct inpcb *inp; 189642fa505bSDavid Greenman 1897fdc984f7STor Egge inp = sotoinpcb(so); 189854d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 18996466b28aSRobert Watson 19000fac350cSGleb Smirnoff *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 19010fac350cSGleb Smirnoff .sin_len = sizeof(struct sockaddr_in), 19020fac350cSGleb Smirnoff .sin_family = AF_INET, 19030fac350cSGleb Smirnoff .sin_port = inp->inp_fport, 19040fac350cSGleb Smirnoff .sin_addr = inp->inp_faddr, 19050fac350cSGleb Smirnoff }; 190642fa505bSDavid Greenman 19070fac350cSGleb Smirnoff return (0); 1908df8bae1dSRodney W. Grimes } 1909df8bae1dSRodney W. Grimes 1910db0ac6deSCy Schubert static bool 1911db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 1912db0ac6deSCy Schubert { 1913db0ac6deSCy Schubert 1914db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 1915db0ac6deSCy Schubert return (true); 1916db0ac6deSCy Schubert else 1917db0ac6deSCy Schubert return (false); 1918db0ac6deSCy Schubert } 1919db0ac6deSCy Schubert 1920e43cc4aeSHajimu UMEMOTO void 1921136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1922e43cc4aeSHajimu UMEMOTO { 1923db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 1924db0ac6deSCy Schubert inp_v4_multi_match, NULL); 1925e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 192659854ecfSHans Petter Selasky struct in_multi *inm; 192759854ecfSHans Petter Selasky struct in_mfilter *imf; 1928e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1929e43cc4aeSHajimu UMEMOTO 1930db0ac6deSCy Schubert IN_MULTI_LOCK_ASSERT(); 1931db0ac6deSCy Schubert 1932db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 1933db0ac6deSCy Schubert INP_WLOCK_ASSERT(inp); 1934db0ac6deSCy Schubert 1935e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1936e43cc4aeSHajimu UMEMOTO /* 1937e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1938e43cc4aeSHajimu UMEMOTO * detached. 1939e43cc4aeSHajimu UMEMOTO */ 1940e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1941e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1942e43cc4aeSHajimu UMEMOTO 1943e43cc4aeSHajimu UMEMOTO /* 1944e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1945e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1946cb6bb230SMatt Macy * 1947cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1948e43cc4aeSHajimu UMEMOTO */ 194959854ecfSHans Petter Selasky restart: 195059854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 195159854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 195259854ecfSHans Petter Selasky continue; 195359854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 195459854ecfSHans Petter Selasky continue; 195559854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 195659854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 195759854ecfSHans Petter Selasky ip_mfilter_free(imf); 195859854ecfSHans Petter Selasky goto restart; 1959e43cc4aeSHajimu UMEMOTO } 1960e43cc4aeSHajimu UMEMOTO } 1961e43cc4aeSHajimu UMEMOTO } 1962e43cc4aeSHajimu UMEMOTO 1963df8bae1dSRodney W. Grimes /* 1964fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1965fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1966c3229e05SDavid Greenman */ 1967d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1968df8bae1dSRodney W. Grimes struct inpcb * 1969136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 19709a413162SMark Johnston u_short lport, int fib, int lookupflags, struct ucred *cred) 1971df8bae1dSRodney W. Grimes { 1972136d4f1cSRobert Watson struct inpcb *inp; 1973d5e8a67eSHajimu UMEMOTO #ifdef INET6 1974d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1975d5e8a67eSHajimu UMEMOTO #else 1976d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1977d5e8a67eSHajimu UMEMOTO #endif 1978d5e8a67eSHajimu UMEMOTO int wildcard; 19797bc4aca7SDavid Greenman 198068e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 198168e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 19829a413162SMark Johnston KASSERT(fib == RT_ALL_FIBS || (fib >= 0 && fib < V_rt_numfibs), 19839a413162SMark Johnston ("%s: invalid fib %d", __func__, fib)); 19849a413162SMark Johnston 1985fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 19861b73ca0bSSam Leffler 198768e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1988c3229e05SDavid Greenman struct inpcbhead *head; 1989c3229e05SDavid Greenman /* 1990c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1991c3229e05SDavid Greenman * matches the local address and port we're looking for. 1992c3229e05SDavid Greenman */ 1993fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1994a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 1995fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1996cfa1ca9dSYoshinobu Inoue #ifdef INET6 1997413628a7SBjoern A. Zeeb /* XXX inp locking */ 1998369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1999cfa1ca9dSYoshinobu Inoue continue; 2000cfa1ca9dSYoshinobu Inoue #endif 2001c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 2002c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 20039a413162SMark Johnston inp->inp_lport == lport && (fib == RT_ALL_FIBS || 20049a413162SMark Johnston inp->inp_inc.inc_fibnum == fib)) { 2005c3229e05SDavid Greenman /* 2006413628a7SBjoern A. Zeeb * Found? 2007c3229e05SDavid Greenman */ 2008ac1750ddSMark Johnston if (prison_equal_ip4(cred->cr_prison, 20090304c731SJamie Gritton inp->inp_cred->cr_prison)) 2010c3229e05SDavid Greenman return (inp); 2011df8bae1dSRodney W. Grimes } 2012c3229e05SDavid Greenman } 2013c3229e05SDavid Greenman /* 2014c3229e05SDavid Greenman * Not found. 2015c3229e05SDavid Greenman */ 2016c3229e05SDavid Greenman return (NULL); 2017c3229e05SDavid Greenman } else { 20185f539170SGleb Smirnoff struct inpcbhead *porthash; 2019c3229e05SDavid Greenman struct inpcb *match = NULL; 20205f539170SGleb Smirnoff 2021c3229e05SDavid Greenman /* 2022c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 2023c3229e05SDavid Greenman * fit. 2024c3229e05SDavid Greenman */ 20255f539170SGleb Smirnoff porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 20265f539170SGleb Smirnoff pcbinfo->ipi_porthashmask)]; 20275f539170SGleb Smirnoff CK_LIST_FOREACH(inp, porthash, inp_portlist) { 20285f539170SGleb Smirnoff if (inp->inp_lport != lport) 20295f539170SGleb Smirnoff continue; 2030ac1750ddSMark Johnston if (!prison_equal_ip4(inp->inp_cred->cr_prison, 20310304c731SJamie Gritton cred->cr_prison)) 2032413628a7SBjoern A. Zeeb continue; 20339a413162SMark Johnston if (fib != RT_ALL_FIBS && 20349a413162SMark Johnston inp->inp_inc.inc_fibnum != fib) 20359a413162SMark Johnston continue; 20365f539170SGleb Smirnoff wildcard = 0; 2037cfa1ca9dSYoshinobu Inoue #ifdef INET6 2038413628a7SBjoern A. Zeeb /* XXX inp locking */ 2039369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2040cfa1ca9dSYoshinobu Inoue continue; 2041d5e8a67eSHajimu UMEMOTO /* 20425f539170SGleb Smirnoff * We never select the PCB that has INP_IPV6 flag and 20435f539170SGleb Smirnoff * is bound to :: if we have another PCB which is bound 20445f539170SGleb Smirnoff * to 0.0.0.0. If a PCB has the INP_IPV6 flag, then we 20455f539170SGleb Smirnoff * set its cost higher than IPv4 only PCBs. 2046d5e8a67eSHajimu UMEMOTO * 20475f539170SGleb Smirnoff * Note that the case only happens when a socket is 20485f539170SGleb Smirnoff * bound to ::, under the condition that the use of the 2049d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 2050d5e8a67eSHajimu UMEMOTO */ 2051d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 2052d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2053cfa1ca9dSYoshinobu Inoue #endif 2054c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 2055c3229e05SDavid Greenman wildcard++; 205615bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 205715bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 205815bd2b43SDavid Greenman wildcard++; 205915bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 206015bd2b43SDavid Greenman continue; 206115bd2b43SDavid Greenman } else { 206215bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 206315bd2b43SDavid Greenman wildcard++; 206415bd2b43SDavid Greenman } 2065df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 2066df8bae1dSRodney W. Grimes match = inp; 2067df8bae1dSRodney W. Grimes matchwild = wildcard; 2068413628a7SBjoern A. Zeeb if (matchwild == 0) 2069df8bae1dSRodney W. Grimes break; 2070df8bae1dSRodney W. Grimes } 2071df8bae1dSRodney W. Grimes } 2072df8bae1dSRodney W. Grimes return (match); 2073df8bae1dSRodney W. Grimes } 2074c3229e05SDavid Greenman } 2075d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 207615bd2b43SDavid Greenman 2077d93ec8cbSMark Johnston static bool 2078da806e8dSMark Johnston in_pcblookup_lb_match(const struct inpcblbgroup *grp, int domain, int fib) 2079d93ec8cbSMark Johnston { 2080da806e8dSMark Johnston return ((domain == M_NODOM || domain == grp->il_numa_domain) && 2081da806e8dSMark Johnston (fib == RT_ALL_FIBS || fib == grp->il_fibnum)); 2082d93ec8cbSMark Johnston } 2083d93ec8cbSMark Johnston 20841a43cff9SSean Bruno static struct inpcb * 20851a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 20864130ea61SMark Johnston const struct in_addr *faddr, uint16_t fport, const struct in_addr *laddr, 2087da806e8dSMark Johnston uint16_t lport, int domain, int fib) 20881a43cff9SSean Bruno { 20891a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 20901a43cff9SSean Bruno struct inpcblbgroup *grp; 2091d93ec8cbSMark Johnston struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 2092a600aabeSMark Johnston struct inpcb *inp; 2093a600aabeSMark Johnston u_int count; 20941a43cff9SSean Bruno 20951a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 2096a600aabeSMark Johnston NET_EPOCH_ASSERT(); 20971a43cff9SSean Bruno 20989d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 20999d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 21001a43cff9SSean Bruno 21011a43cff9SSean Bruno /* 2102d93ec8cbSMark Johnston * Search for an LB group match based on the following criteria: 2103d93ec8cbSMark Johnston * - prefer jailed groups to non-jailed groups 2104d93ec8cbSMark Johnston * - prefer exact source address matches to wildcard matches 2105d93ec8cbSMark Johnston * - prefer groups bound to the specified NUMA domain 21061a43cff9SSean Bruno */ 2107d93ec8cbSMark Johnston jail_exact = jail_wild = local_exact = local_wild = NULL; 210854af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 2109d93ec8cbSMark Johnston bool injail; 2110d93ec8cbSMark Johnston 21111a43cff9SSean Bruno #ifdef INET6 21121a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 21131a43cff9SSean Bruno continue; 21141a43cff9SSean Bruno #endif 21158be02ee4SMark Johnston if (grp->il_lport != lport) 21168be02ee4SMark Johnston continue; 21171a43cff9SSean Bruno 2118d93ec8cbSMark Johnston injail = prison_flag(grp->il_cred, PR_IP4) != 0; 2119d93ec8cbSMark Johnston if (injail && prison_check_ip4_locked(grp->il_cred->cr_prison, 2120d93ec8cbSMark Johnston laddr) != 0) 2121d93ec8cbSMark Johnston continue; 2122a034518aSAndrew Gallatin 2123d93ec8cbSMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) { 2124d93ec8cbSMark Johnston if (injail) { 2125d93ec8cbSMark Johnston jail_exact = grp; 2126da806e8dSMark Johnston if (in_pcblookup_lb_match(grp, domain, fib)) 2127d93ec8cbSMark Johnston /* This is a perfect match. */ 2128d93ec8cbSMark Johnston goto out; 2129d93ec8cbSMark Johnston } else if (local_exact == NULL || 2130da806e8dSMark Johnston in_pcblookup_lb_match(grp, domain, fib)) { 2131d93ec8cbSMark Johnston local_exact = grp; 2132d93ec8cbSMark Johnston } 21334130ea61SMark Johnston } else if (grp->il_laddr.s_addr == INADDR_ANY) { 2134d93ec8cbSMark Johnston if (injail) { 2135d93ec8cbSMark Johnston if (jail_wild == NULL || 2136da806e8dSMark Johnston in_pcblookup_lb_match(grp, domain, fib)) 2137d93ec8cbSMark Johnston jail_wild = grp; 2138d93ec8cbSMark Johnston } else if (local_wild == NULL || 2139da806e8dSMark Johnston in_pcblookup_lb_match(grp, domain, fib)) { 2140d93ec8cbSMark Johnston local_wild = grp; 2141d93ec8cbSMark Johnston } 2142d93ec8cbSMark Johnston } 2143d93ec8cbSMark Johnston } 2144d93ec8cbSMark Johnston 2145d93ec8cbSMark Johnston if (jail_exact != NULL) 2146d93ec8cbSMark Johnston grp = jail_exact; 2147d93ec8cbSMark Johnston else if (jail_wild != NULL) 2148d93ec8cbSMark Johnston grp = jail_wild; 2149d93ec8cbSMark Johnston else if (local_exact != NULL) 2150d93ec8cbSMark Johnston grp = local_exact; 2151d93ec8cbSMark Johnston else 2152d93ec8cbSMark Johnston grp = local_wild; 2153d93ec8cbSMark Johnston if (grp == NULL) 2154d93ec8cbSMark Johnston return (NULL); 2155a600aabeSMark Johnston 2156d93ec8cbSMark Johnston out: 2157a600aabeSMark Johnston /* 2158a600aabeSMark Johnston * Synchronize with in_pcblbgroup_insert(). 2159a600aabeSMark Johnston */ 2160a600aabeSMark Johnston count = atomic_load_acq_int(&grp->il_inpcnt); 2161a600aabeSMark Johnston if (count == 0) 2162a600aabeSMark Johnston return (NULL); 2163a600aabeSMark Johnston inp = grp->il_inp[INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) % count]; 2164a600aabeSMark Johnston KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 2165a600aabeSMark Johnston return (inp); 21661a43cff9SSean Bruno } 21671a43cff9SSean Bruno 21683e98dcb3SMark Johnston static bool 21693e98dcb3SMark Johnston in_pcblookup_exact_match(const struct inpcb *inp, struct in_addr faddr, 21704130ea61SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 217115bd2b43SDavid Greenman { 2172cfa1ca9dSYoshinobu Inoue #ifdef INET6 2173413628a7SBjoern A. Zeeb /* XXX inp locking */ 2174369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 21753e98dcb3SMark Johnston return (false); 2176cfa1ca9dSYoshinobu Inoue #endif 21776d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2178ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2179ca98b82cSDavid Greenman inp->inp_fport == fport && 2180220d8921SGleb Smirnoff inp->inp_lport == lport) 21813e98dcb3SMark Johnston return (true); 21823e98dcb3SMark Johnston return (false); 21833e98dcb3SMark Johnston } 21843e98dcb3SMark Johnston 21853e98dcb3SMark Johnston static struct inpcb * 21863e98dcb3SMark Johnston in_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in_addr faddr, 21873e98dcb3SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 21883e98dcb3SMark Johnston { 21893e98dcb3SMark Johnston struct inpcbhead *head; 21903e98dcb3SMark Johnston struct inpcb *inp; 21913e98dcb3SMark Johnston 21923e98dcb3SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 21933e98dcb3SMark Johnston 21943e98dcb3SMark Johnston head = &pcbinfo->ipi_hash_exact[INP_PCBHASH(&faddr, lport, fport, 21953e98dcb3SMark Johnston pcbinfo->ipi_hashmask)]; 21963e98dcb3SMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_exact) { 21973e98dcb3SMark Johnston if (in_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) 2198c3229e05SDavid Greenman return (inp); 2199c3229e05SDavid Greenman } 22003e98dcb3SMark Johnston return (NULL); 22013e98dcb3SMark Johnston } 22023e98dcb3SMark Johnston 22033e98dcb3SMark Johnston typedef enum { 22043e98dcb3SMark Johnston INPLOOKUP_MATCH_NONE = 0, 22053e98dcb3SMark Johnston INPLOOKUP_MATCH_WILD = 1, 22063e98dcb3SMark Johnston INPLOOKUP_MATCH_LADDR = 2, 22073e98dcb3SMark Johnston } inp_lookup_match_t; 22083e98dcb3SMark Johnston 22093e98dcb3SMark Johnston static inp_lookup_match_t 22103e98dcb3SMark Johnston in_pcblookup_wild_match(const struct inpcb *inp, struct in_addr laddr, 2211da806e8dSMark Johnston u_short lport, int fib) 22123e98dcb3SMark Johnston { 22133e98dcb3SMark Johnston #ifdef INET6 22143e98dcb3SMark Johnston /* XXX inp locking */ 22153e98dcb3SMark Johnston if ((inp->inp_vflag & INP_IPV4) == 0) 22163e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 22173e98dcb3SMark Johnston #endif 22183e98dcb3SMark Johnston if (inp->inp_faddr.s_addr != INADDR_ANY || inp->inp_lport != lport) 22193e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 2220da806e8dSMark Johnston if (fib != RT_ALL_FIBS && inp->inp_inc.inc_fibnum != fib) 2221da806e8dSMark Johnston return (INPLOOKUP_MATCH_NONE); 22223e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == INADDR_ANY) 22233e98dcb3SMark Johnston return (INPLOOKUP_MATCH_WILD); 22243e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == laddr.s_addr) 22253e98dcb3SMark Johnston return (INPLOOKUP_MATCH_LADDR); 22263e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 22274130ea61SMark Johnston } 2228e3fd5ffdSRobert Watson 22297b92493aSMark Johnston #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1) 22307b92493aSMark Johnston 22317b92493aSMark Johnston static struct inpcb * 223221d7ac8cSMark Johnston in_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo, struct in_addr laddr, 2233da806e8dSMark Johnston u_short lport, int fib, const inp_lookup_t lockflags) 22347b92493aSMark Johnston { 22357b92493aSMark Johnston struct inpcbhead *head; 22367b92493aSMark Johnston struct inpcb *inp; 22377b92493aSMark Johnston 22387b92493aSMark Johnston KASSERT(SMR_ENTERED(pcbinfo->ipi_smr), 22397b92493aSMark Johnston ("%s: not in SMR read section", __func__)); 22407b92493aSMark Johnston 22417b92493aSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 22427b92493aSMark Johnston pcbinfo->ipi_hashmask)]; 22437b92493aSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 22447b92493aSMark Johnston inp_lookup_match_t match; 22457b92493aSMark Johnston 2246da806e8dSMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport, fib); 22477b92493aSMark Johnston if (match == INPLOOKUP_MATCH_NONE) 22487b92493aSMark Johnston continue; 22497b92493aSMark Johnston 22507b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 2251da806e8dSMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport, fib); 2252a306ed50SMark Johnston if (match != INPLOOKUP_MATCH_NONE && 2253a306ed50SMark Johnston prison_check_ip4_locked(inp->inp_cred->cr_prison, 2254a306ed50SMark Johnston &laddr) == 0) 22557b92493aSMark Johnston return (inp); 22567b92493aSMark Johnston inp_unlock(inp, lockflags); 22577b92493aSMark Johnston } 22587b92493aSMark Johnston 22597b92493aSMark Johnston /* 22607b92493aSMark Johnston * The matching socket disappeared out from under us. Fall back 22617b92493aSMark Johnston * to a serialized lookup. 22627b92493aSMark Johnston */ 22637b92493aSMark Johnston return (INP_LOOKUP_AGAIN); 22647b92493aSMark Johnston } 22657b92493aSMark Johnston return (NULL); 22667b92493aSMark Johnston } 22677b92493aSMark Johnston 22684130ea61SMark Johnston static struct inpcb * 226921d7ac8cSMark Johnston in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr laddr, 2270da806e8dSMark Johnston u_short lport, int fib) 22714130ea61SMark Johnston { 22724130ea61SMark Johnston struct inpcbhead *head; 22734130ea61SMark Johnston struct inpcb *inp, *local_wild, *local_exact, *jail_wild; 2274e3fd5ffdSRobert Watson #ifdef INET6 22754130ea61SMark Johnston struct inpcb *local_wild_mapped; 2276e3fd5ffdSRobert Watson #endif 2277413628a7SBjoern A. Zeeb 22784130ea61SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 2279d93ec8cbSMark Johnston 2280d93ec8cbSMark Johnston /* 2281413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2282413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2283413628a7SBjoern A. Zeeb * 2. jailed, wild. 2284413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2285413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2286413628a7SBjoern A. Zeeb */ 2287fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 2288a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 22894130ea61SMark Johnston local_wild = local_exact = jail_wild = NULL; 22904130ea61SMark Johnston #ifdef INET6 22914130ea61SMark Johnston local_wild_mapped = NULL; 22924130ea61SMark Johnston #endif 2293fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 22943e98dcb3SMark Johnston inp_lookup_match_t match; 22954130ea61SMark Johnston bool injail; 22964130ea61SMark Johnston 2297da806e8dSMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport, fib); 22983e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_NONE) 2299413628a7SBjoern A. Zeeb continue; 2300413628a7SBjoern A. Zeeb 23014130ea61SMark Johnston injail = prison_flag(inp->inp_cred, PR_IP4) != 0; 2302413628a7SBjoern A. Zeeb if (injail) { 23034130ea61SMark Johnston if (prison_check_ip4_locked(inp->inp_cred->cr_prison, 23044130ea61SMark Johnston &laddr) != 0) 2305413628a7SBjoern A. Zeeb continue; 2306413628a7SBjoern A. Zeeb } else { 2307413628a7SBjoern A. Zeeb if (local_exact != NULL) 2308413628a7SBjoern A. Zeeb continue; 2309413628a7SBjoern A. Zeeb } 2310413628a7SBjoern A. Zeeb 23113e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_LADDR) { 2312413628a7SBjoern A. Zeeb if (injail) 2313c3229e05SDavid Greenman return (inp); 2314413628a7SBjoern A. Zeeb local_exact = inp; 23153e98dcb3SMark Johnston } else { 2316e3fd5ffdSRobert Watson #ifdef INET6 2317413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23185cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2319cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2320cfa1ca9dSYoshinobu Inoue else 232183e521ecSBjoern A. Zeeb #endif 2322413628a7SBjoern A. Zeeb if (injail) 2323413628a7SBjoern A. Zeeb jail_wild = inp; 2324413628a7SBjoern A. Zeeb else 23256d6a026bSDavid Greenman local_wild = inp; 23266d6a026bSDavid Greenman } 23274130ea61SMark Johnston } 2328413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2329413628a7SBjoern A. Zeeb return (jail_wild); 2330413628a7SBjoern A. Zeeb if (local_exact != NULL) 2331413628a7SBjoern A. Zeeb return (local_exact); 2332413628a7SBjoern A. Zeeb if (local_wild != NULL) 2333c3229e05SDavid Greenman return (local_wild); 2334413628a7SBjoern A. Zeeb #ifdef INET6 2335413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2336413628a7SBjoern A. Zeeb return (local_wild_mapped); 233783e521ecSBjoern A. Zeeb #endif 23386d6a026bSDavid Greenman return (NULL); 233915bd2b43SDavid Greenman } 2340fa046d87SRobert Watson 2341fa046d87SRobert Watson /* 23424130ea61SMark Johnston * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 23434130ea61SMark Johnston * that the caller has either locked the hash list, which usually happens 23444130ea61SMark Johnston * for bind(2) operations, or is in SMR section, which happens when sorting 23454130ea61SMark Johnston * out incoming packets. 2346fa046d87SRobert Watson */ 2347fa046d87SRobert Watson static struct inpcb * 23484130ea61SMark Johnston in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 23494130ea61SMark Johnston u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2350da806e8dSMark Johnston uint8_t numa_domain, int fib) 23514130ea61SMark Johnston { 23524130ea61SMark Johnston struct inpcb *inp; 23534130ea61SMark Johnston const u_short fport = fport_arg, lport = lport_arg; 23544130ea61SMark Johnston 2355da806e8dSMark Johnston KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD | INPLOOKUP_FIB)) == 0, 23564130ea61SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 23574130ea61SMark Johnston KASSERT(faddr.s_addr != INADDR_ANY, 23584130ea61SMark Johnston ("%s: invalid foreign address", __func__)); 23594130ea61SMark Johnston KASSERT(laddr.s_addr != INADDR_ANY, 23604130ea61SMark Johnston ("%s: invalid local address", __func__)); 23617b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(pcbinfo); 23624130ea61SMark Johnston 23634130ea61SMark Johnston inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 23644130ea61SMark Johnston if (inp != NULL) 23654130ea61SMark Johnston return (inp); 23664130ea61SMark Johnston 23674130ea61SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 23687b92493aSMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 2369da806e8dSMark Johnston &laddr, lport, numa_domain, fib); 23704130ea61SMark Johnston if (inp == NULL) { 237121d7ac8cSMark Johnston inp = in_pcblookup_hash_wild_locked(pcbinfo, laddr, 2372da806e8dSMark Johnston lport, fib); 23734130ea61SMark Johnston } 23744130ea61SMark Johnston } 23754130ea61SMark Johnston 23764130ea61SMark Johnston return (inp); 23774130ea61SMark Johnston } 23784130ea61SMark Johnston 23794130ea61SMark Johnston static struct inpcb * 23807b92493aSMark Johnston in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2381fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2382da806e8dSMark Johnston uint8_t numa_domain, int fib) 2383fa046d87SRobert Watson { 2384fa046d87SRobert Watson struct inpcb *inp; 23857b92493aSMark Johnston const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 23867b92493aSMark Johnston 23877b92493aSMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 23887b92493aSMark Johnston ("%s: LOCKPCB not set", __func__)); 23897b92493aSMark Johnston 23907b92493aSMark Johnston INP_HASH_WLOCK(pcbinfo); 23917b92493aSMark Johnston inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2392da806e8dSMark Johnston lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain, fib); 23937b92493aSMark Johnston if (inp != NULL && !inp_trylock(inp, lockflags)) { 23947b92493aSMark Johnston in_pcbref(inp); 23957b92493aSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 23967b92493aSMark Johnston inp_lock(inp, lockflags); 23977b92493aSMark Johnston if (in_pcbrele(inp, lockflags)) 23987b92493aSMark Johnston /* XXX-MJ or retry until we get a negative match? */ 23997b92493aSMark Johnston inp = NULL; 24007b92493aSMark Johnston } else { 24017b92493aSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 24027b92493aSMark Johnston } 24037b92493aSMark Johnston return (inp); 24047b92493aSMark Johnston } 24057b92493aSMark Johnston 24067b92493aSMark Johnston static struct inpcb * 24077b92493aSMark Johnston in_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr, 24087b92493aSMark Johnston u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2409da806e8dSMark Johnston uint8_t numa_domain, int fib) 24107b92493aSMark Johnston { 24117b92493aSMark Johnston struct inpcb *inp; 24127b92493aSMark Johnston const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 24137b92493aSMark Johnston const u_short fport = fport_arg, lport = lport_arg; 2414fa046d87SRobert Watson 2415675e2618SMark Johnston KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2416675e2618SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 2417675e2618SMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2418675e2618SMark Johnston ("%s: LOCKPCB not set", __func__)); 2419675e2618SMark Johnston 2420db0ac6deSCy Schubert smr_enter(pcbinfo->ipi_smr); 24217b92493aSMark Johnston inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 2422fa046d87SRobert Watson if (inp != NULL) { 24237b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 24247b92493aSMark Johnston /* 24257b92493aSMark Johnston * Revalidate the 4-tuple, the socket could have been 24267b92493aSMark Johnston * disconnected. 24277b92493aSMark Johnston */ 24287b92493aSMark Johnston if (__predict_true(in_pcblookup_exact_match(inp, 24297b92493aSMark Johnston faddr, fport, laddr, lport))) 24307b92493aSMark Johnston return (inp); 24317b92493aSMark Johnston inp_unlock(inp, lockflags); 24327b92493aSMark Johnston } 24337b92493aSMark Johnston 24347b92493aSMark Johnston /* 24357b92493aSMark Johnston * We failed to lock the inpcb, or its connection state changed 24367b92493aSMark Johnston * out from under us. Fall back to a precise search. 24377b92493aSMark Johnston */ 24387b92493aSMark Johnston return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2439da806e8dSMark Johnston lookupflags, numa_domain, fib)); 24407b92493aSMark Johnston } 24417b92493aSMark Johnston 24427b92493aSMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 24437b92493aSMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 2444da806e8dSMark Johnston &laddr, lport, numa_domain, fib); 24457b92493aSMark Johnston if (inp != NULL) { 24467b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 24477b92493aSMark Johnston if (__predict_true(in_pcblookup_wild_match(inp, 2448da806e8dSMark Johnston laddr, lport, fib) != INPLOOKUP_MATCH_NONE)) 24497b92493aSMark Johnston return (inp); 24507b92493aSMark Johnston inp_unlock(inp, lockflags); 24517b92493aSMark Johnston } 24527b92493aSMark Johnston inp = INP_LOOKUP_AGAIN; 24537b92493aSMark Johnston } else { 245421d7ac8cSMark Johnston inp = in_pcblookup_hash_wild_smr(pcbinfo, laddr, lport, 2455da806e8dSMark Johnston fib, lockflags); 24567b92493aSMark Johnston } 24577b92493aSMark Johnston if (inp == INP_LOOKUP_AGAIN) { 24587b92493aSMark Johnston return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, 2459da806e8dSMark Johnston lport, lookupflags, numa_domain, fib)); 24607b92493aSMark Johnston } 24617b92493aSMark Johnston } 24627b92493aSMark Johnston 24637b92493aSMark Johnston if (inp == NULL) 2464db0ac6deSCy Schubert smr_exit(pcbinfo->ipi_smr); 2465d797164aSGleb Smirnoff 2466fa046d87SRobert Watson return (inp); 2467fa046d87SRobert Watson } 2468fa046d87SRobert Watson 2469fa046d87SRobert Watson /* 2470d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2471d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 2472fa046d87SRobert Watson */ 2473fa046d87SRobert Watson struct inpcb * 2474fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 24754130ea61SMark Johnston struct in_addr laddr, u_int lport, int lookupflags, 2476da806e8dSMark Johnston struct ifnet *ifp) 2477fa046d87SRobert Watson { 2478da806e8dSMark Johnston int fib; 2479da806e8dSMark Johnston 2480da806e8dSMark Johnston fib = (lookupflags & INPLOOKUP_FIB) ? if_getfib(ifp) : RT_ALL_FIBS; 24814130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 2482da806e8dSMark Johnston lookupflags, M_NODOM, fib)); 2483fa046d87SRobert Watson } 2484d3c1f003SRobert Watson 2485d3c1f003SRobert Watson struct inpcb * 2486d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2487d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 24884130ea61SMark Johnston struct ifnet *ifp __unused, struct mbuf *m) 2489d3c1f003SRobert Watson { 2490da806e8dSMark Johnston int fib; 2491da806e8dSMark Johnston 2492da806e8dSMark Johnston M_ASSERTPKTHDR(m); 2493da806e8dSMark Johnston fib = (lookupflags & INPLOOKUP_FIB) ? M_GETFIB(m) : RT_ALL_FIBS; 24944130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 2495da806e8dSMark Johnston lookupflags, m->m_pkthdr.numa_domain, fib)); 2496d3c1f003SRobert Watson } 249767107f45SBjoern A. Zeeb #endif /* INET */ 249815bd2b43SDavid Greenman 24997b92493aSMark Johnston static bool 25007b92493aSMark Johnston in_pcbjailed(const struct inpcb *inp, unsigned int flag) 25017b92493aSMark Johnston { 25027b92493aSMark Johnston return (prison_flag(inp->inp_cred, flag) != 0); 25037b92493aSMark Johnston } 25047b92493aSMark Johnston 25057b92493aSMark Johnston /* 25067b92493aSMark Johnston * Insert the PCB into a hash chain using ordering rules which ensure that 25077b92493aSMark Johnston * in_pcblookup_hash_wild_*() always encounter the highest-ranking PCB first. 25087b92493aSMark Johnston * 25097b92493aSMark Johnston * Specifically, keep jailed PCBs in front of non-jailed PCBs, and keep PCBs 251093998d16SMark Johnston * with exact local addresses ahead of wildcard PCBs. Unbound v4-mapped v6 PCBs 251193998d16SMark Johnston * always appear last no matter whether they are jailed. 25127b92493aSMark Johnston */ 25137b92493aSMark Johnston static void 25147b92493aSMark Johnston _in_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 25157b92493aSMark Johnston { 25167b92493aSMark Johnston struct inpcb *last; 25177b92493aSMark Johnston bool bound, injail; 25187b92493aSMark Johnston 251993998d16SMark Johnston INP_LOCK_ASSERT(inp); 25207b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 25217b92493aSMark Johnston 25227b92493aSMark Johnston last = NULL; 25237b92493aSMark Johnston bound = inp->inp_laddr.s_addr != INADDR_ANY; 252493998d16SMark Johnston if (!bound && (inp->inp_vflag & INP_IPV6PROTO) != 0) { 252593998d16SMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 252693998d16SMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 252793998d16SMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 252893998d16SMark Johnston return; 252993998d16SMark Johnston } 253093998d16SMark Johnston } 253193998d16SMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 253293998d16SMark Johnston return; 253393998d16SMark Johnston } 253493998d16SMark Johnston 25357b92493aSMark Johnston injail = in_pcbjailed(inp, PR_IP4); 25367b92493aSMark Johnston if (!injail) { 25377b92493aSMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 253893998d16SMark Johnston if (!in_pcbjailed(last, PR_IP4)) 25397b92493aSMark Johnston break; 25407b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25417b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25427b92493aSMark Johnston return; 25437b92493aSMark Johnston } 25447b92493aSMark Johnston } 25457b92493aSMark Johnston } else if (!CK_LIST_EMPTY(pcbhash) && 25467b92493aSMark Johnston !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP4)) { 25477b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25487b92493aSMark Johnston return; 25497b92493aSMark Johnston } 25507b92493aSMark Johnston if (!bound) { 25517b92493aSMark Johnston CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 25527b92493aSMark Johnston if (last->inp_laddr.s_addr == INADDR_ANY) 25537b92493aSMark Johnston break; 25547b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25557b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25567b92493aSMark Johnston return; 25577b92493aSMark Johnston } 25587b92493aSMark Johnston } 25597b92493aSMark Johnston } 25607b92493aSMark Johnston if (last == NULL) 25617b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25627b92493aSMark Johnston else 25637b92493aSMark Johnston CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 25647b92493aSMark Johnston } 25657b92493aSMark Johnston 25667b92493aSMark Johnston #ifdef INET6 25677b92493aSMark Johnston /* 25687b92493aSMark Johnston * See the comment above _in_pcbinshash_wild(). 25697b92493aSMark Johnston */ 25707b92493aSMark Johnston static void 25717b92493aSMark Johnston _in6_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 25727b92493aSMark Johnston { 25737b92493aSMark Johnston struct inpcb *last; 25747b92493aSMark Johnston bool bound, injail; 25757b92493aSMark Johnston 257693998d16SMark Johnston INP_LOCK_ASSERT(inp); 25777b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 25787b92493aSMark Johnston 25797b92493aSMark Johnston last = NULL; 25807b92493aSMark Johnston bound = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr); 25817b92493aSMark Johnston injail = in_pcbjailed(inp, PR_IP6); 25827b92493aSMark Johnston if (!injail) { 25837b92493aSMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 258493998d16SMark Johnston if (!in_pcbjailed(last, PR_IP6)) 25857b92493aSMark Johnston break; 25867b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25877b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25887b92493aSMark Johnston return; 25897b92493aSMark Johnston } 25907b92493aSMark Johnston } 25917b92493aSMark Johnston } else if (!CK_LIST_EMPTY(pcbhash) && 25927b92493aSMark Johnston !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP6)) { 25937b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25947b92493aSMark Johnston return; 25957b92493aSMark Johnston } 25967b92493aSMark Johnston if (!bound) { 25977b92493aSMark Johnston CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 25987b92493aSMark Johnston if (IN6_IS_ADDR_UNSPECIFIED(&last->in6p_laddr)) 25997b92493aSMark Johnston break; 26007b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 26017b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 26027b92493aSMark Johnston return; 26037b92493aSMark Johnston } 26047b92493aSMark Johnston } 26057b92493aSMark Johnston } 26067b92493aSMark Johnston if (last == NULL) 26077b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 26087b92493aSMark Johnston else 26097b92493aSMark Johnston CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 26107b92493aSMark Johnston } 26117b92493aSMark Johnston #endif 26127b92493aSMark Johnston 26137bc4aca7SDavid Greenman /* 2614c3229e05SDavid Greenman * Insert PCB onto various hash lists. 2615*136c5e17SGleb Smirnoff * 2616*136c5e17SGleb Smirnoff * With normal sockets this function shall not fail, so it could return void. 2617*136c5e17SGleb Smirnoff * But for SO_REUSEPORT_LB it may need to allocate memory with locks held, 2618*136c5e17SGleb Smirnoff * that's the only condition when it can fail. 26197bc4aca7SDavid Greenman */ 2620db0ac6deSCy Schubert int 2621db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp) 262215bd2b43SDavid Greenman { 26235f539170SGleb Smirnoff struct inpcbhead *pcbhash, *pcbporthash; 2624c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2625fdb987beSMark Johnston uint32_t hash; 2626fdb987beSMark Johnston bool connected; 262715bd2b43SDavid Greenman 26288501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2629fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2630111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2631111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2632602cc7f1SRobert Watson 2633cfa1ca9dSYoshinobu Inoue #ifdef INET6 2634fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2635fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2636fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2637fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2638fdb987beSMark Johnston } else 263983e521ecSBjoern A. Zeeb #endif 2640fdb987beSMark Johnston { 2641fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2642fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2643fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2644fdb987beSMark Johnston } 2645fdb987beSMark Johnston 2646fdb987beSMark Johnston if (connected) 2647fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_exact[hash]; 2648fdb987beSMark Johnston else 2649fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_wild[hash]; 265015bd2b43SDavid Greenman 2651712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2652712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2653c3229e05SDavid Greenman 2654c3229e05SDavid Greenman /* 26551a43cff9SSean Bruno * Add entry to load balance group. 26561a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 26571a43cff9SSean Bruno */ 2658e3ba0d6aSGleb Smirnoff if ((inp->inp_socket->so_options & SO_REUSEPORT_LB) != 0) { 2659a152dd86SMark Johnston int error = in_pcbinslbgrouphash(inp, M_NODOM); 2660a152dd86SMark Johnston if (error != 0) 2661a152dd86SMark Johnston return (error); 26621a43cff9SSean Bruno } 26631a43cff9SSean Bruno 26641a43cff9SSean Bruno /* 2665fdb987beSMark Johnston * The PCB may have been disconnected in the past. Before we can safely 2666fdb987beSMark Johnston * make it visible in the hash table, we must wait for all readers which 2667fdb987beSMark Johnston * may be traversing this PCB to finish. 2668fdb987beSMark Johnston */ 2669fdb987beSMark Johnston if (inp->inp_smr != SMR_SEQ_INVALID) { 2670fdb987beSMark Johnston smr_wait(pcbinfo->ipi_smr, inp->inp_smr); 2671fdb987beSMark Johnston inp->inp_smr = SMR_SEQ_INVALID; 2672fdb987beSMark Johnston } 2673fdb987beSMark Johnston 2674fdb987beSMark Johnston if (connected) 2675fdb987beSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_exact); 26767b92493aSMark Johnston else { 26777b92493aSMark Johnston #ifdef INET6 26787b92493aSMark Johnston if ((inp->inp_vflag & INP_IPV6) != 0) 26797b92493aSMark Johnston _in6_pcbinshash_wild(pcbhash, inp); 2680fdb987beSMark Johnston else 26817b92493aSMark Johnston #endif 26827b92493aSMark Johnston _in_pcbinshash_wild(pcbhash, inp); 26837b92493aSMark Johnston } 26845f539170SGleb Smirnoff CK_LIST_INSERT_HEAD(pcbporthash, inp, inp_portlist); 2685111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 2686db0ac6deSCy Schubert 2687c3229e05SDavid Greenman return (0); 268815bd2b43SDavid Greenman } 268915bd2b43SDavid Greenman 2690fdb987beSMark Johnston void 2691fdb987beSMark Johnston in_pcbremhash_locked(struct inpcb *inp) 26923ba34b07SGleb Smirnoff { 26933ba34b07SGleb Smirnoff 26943ba34b07SGleb Smirnoff INP_WLOCK_ASSERT(inp); 2695fdb987beSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 26963ba34b07SGleb Smirnoff MPASS(inp->inp_flags & INP_INHASHLIST); 26973ba34b07SGleb Smirnoff 2698a43e7a96SGleb Smirnoff if ((inp->inp_flags & INP_INLBGROUP) != 0) 26993ba34b07SGleb Smirnoff in_pcbremlbgrouphash(inp); 2700fdb987beSMark Johnston #ifdef INET6 2701fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2702fdb987beSMark Johnston if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 2703fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2704fdb987beSMark Johnston else 2705fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2706fdb987beSMark Johnston } else 2707fdb987beSMark Johnston #endif 2708fdb987beSMark Johnston { 2709fdb987beSMark Johnston if (in_nullhost(inp->inp_faddr)) 2710fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2711fdb987beSMark Johnston else 2712fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2713fdb987beSMark Johnston } 27143ba34b07SGleb Smirnoff CK_LIST_REMOVE(inp, inp_portlist); 27153ba34b07SGleb Smirnoff inp->inp_flags &= ~INP_INHASHLIST; 27163ba34b07SGleb Smirnoff } 27173ba34b07SGleb Smirnoff 2718fdb987beSMark Johnston static void 2719fdb987beSMark Johnston in_pcbremhash(struct inpcb *inp) 2720fdb987beSMark Johnston { 2721fdb987beSMark Johnston INP_HASH_WLOCK(inp->inp_pcbinfo); 2722fdb987beSMark Johnston in_pcbremhash_locked(inp); 2723fdb987beSMark Johnston INP_HASH_WUNLOCK(inp->inp_pcbinfo); 2724fdb987beSMark Johnston } 2725fdb987beSMark Johnston 272652cd27cbSRobert Watson /* 2727c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2728c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2729c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2730c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2731c3229e05SDavid Greenman */ 273215bd2b43SDavid Greenman void 2733db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp) 273415bd2b43SDavid Greenman { 273559daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 273615bd2b43SDavid Greenman struct inpcbhead *head; 2737fdb987beSMark Johnston uint32_t hash; 2738fdb987beSMark Johnston bool connected; 273915bd2b43SDavid Greenman 27408501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2741fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2742111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2743fdb987beSMark Johnston ("%s: !INP_INHASHLIST", __func__)); 2744fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 2745fdb987beSMark Johnston ("%s: inp was disconnected", __func__)); 2746602cc7f1SRobert Watson 2747cfa1ca9dSYoshinobu Inoue #ifdef INET6 2748fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2749fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2750fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2751fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2752fdb987beSMark Johnston } else 275383e521ecSBjoern A. Zeeb #endif 2754fdb987beSMark Johnston { 2755fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2756fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2757fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2758fdb987beSMark Johnston } 275915bd2b43SDavid Greenman 2760fdb987beSMark Johnston /* 2761fdb987beSMark Johnston * When rehashing, the caller must ensure that either the new or the old 2762fdb987beSMark Johnston * foreign address was unspecified. 2763fdb987beSMark Johnston */ 2764fdb987beSMark Johnston if (connected) 2765fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2766fdb987beSMark Johnston else 2767fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2768fdb987beSMark Johnston 2769fdb987beSMark Johnston if (connected) { 2770fdb987beSMark Johnston head = &pcbinfo->ipi_hash_exact[hash]; 2771fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_exact); 2772fdb987beSMark Johnston } else { 2773fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[hash]; 2774fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_wild); 2775fdb987beSMark Johnston } 2776c3229e05SDavid Greenman } 2777c3229e05SDavid Greenman 2778c3229e05SDavid Greenman /* 277984cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 278084cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 278184cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 278284cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 278384cc0778SGeorge V. Neville-Neil */ 278484cc0778SGeorge V. Neville-Neil void 278584cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 278684cc0778SGeorge V. Neville-Neil { 278784cc0778SGeorge V. Neville-Neil 2788fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 278984cc0778SGeorge V. Neville-Neil return; 279084cc0778SGeorge V. Neville-Neil } 279184cc0778SGeorge V. Neville-Neil 279284cc0778SGeorge V. Neville-Neil /* 2793a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2794a557af22SRobert Watson * label change into the in_pcb for the socket. 2795a557af22SRobert Watson */ 2796a557af22SRobert Watson void 2797136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2798a557af22SRobert Watson { 2799a557af22SRobert Watson #ifdef MAC 2800a557af22SRobert Watson struct inpcb *inp; 2801a557af22SRobert Watson 28024c7c478dSRobert Watson inp = sotoinpcb(so); 28034c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2804602cc7f1SRobert Watson 28058501a69cSRobert Watson INP_WLOCK(inp); 2806310e7cebSRobert Watson SOCK_LOCK(so); 2807a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2808310e7cebSRobert Watson SOCK_UNLOCK(so); 28098501a69cSRobert Watson INP_WUNLOCK(inp); 2810a557af22SRobert Watson #endif 2811a557af22SRobert Watson } 28125f311da2SMike Silbersack 28133d585327SKip Macy void 28143d585327SKip Macy inp_wlock(struct inpcb *inp) 28153d585327SKip Macy { 28163d585327SKip Macy 28178501a69cSRobert Watson INP_WLOCK(inp); 28183d585327SKip Macy } 28193d585327SKip Macy 28203d585327SKip Macy void 28213d585327SKip Macy inp_wunlock(struct inpcb *inp) 28223d585327SKip Macy { 28233d585327SKip Macy 28248501a69cSRobert Watson INP_WUNLOCK(inp); 28253d585327SKip Macy } 28263d585327SKip Macy 28273d585327SKip Macy void 28283d585327SKip Macy inp_rlock(struct inpcb *inp) 28293d585327SKip Macy { 28303d585327SKip Macy 2831a69042a5SRobert Watson INP_RLOCK(inp); 28323d585327SKip Macy } 28333d585327SKip Macy 28343d585327SKip Macy void 28353d585327SKip Macy inp_runlock(struct inpcb *inp) 28363d585327SKip Macy { 28373d585327SKip Macy 2838a69042a5SRobert Watson INP_RUNLOCK(inp); 28393d585327SKip Macy } 28403d585327SKip Macy 2841c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 28423d585327SKip Macy void 2843e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 28443d585327SKip Macy { 28453d585327SKip Macy 28468501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 28473d585327SKip Macy } 28483d585327SKip Macy 28493d585327SKip Macy void 2850e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 28513d585327SKip Macy { 28523d585327SKip Macy 28533d585327SKip Macy INP_UNLOCK_ASSERT(inp); 28543d585327SKip Macy } 28553d585327SKip Macy #endif 28563d585327SKip Macy 28579378e437SKip Macy void 285824cf7a8dSGleb Smirnoff inp_apply_all(struct inpcbinfo *pcbinfo, 285924cf7a8dSGleb Smirnoff void (*func)(struct inpcb *, void *), void *arg) 28609378e437SKip Macy { 286124cf7a8dSGleb Smirnoff struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2862db0ac6deSCy Schubert INPLOOKUP_WLOCKPCB); 28639378e437SKip Macy struct inpcb *inp; 28649378e437SKip Macy 2865db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) 28669378e437SKip Macy func(inp, arg); 28679378e437SKip Macy } 28689378e437SKip Macy 28699378e437SKip Macy struct socket * 28709378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 28719378e437SKip Macy { 28729378e437SKip Macy 28739378e437SKip Macy INP_WLOCK_ASSERT(inp); 28749378e437SKip Macy return (inp->inp_socket); 28759378e437SKip Macy } 28769378e437SKip Macy 28779378e437SKip Macy void 2878df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 28799d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 28809378e437SKip Macy { 28819378e437SKip Macy 28829d29c635SKip Macy INP_LOCK_ASSERT(inp); 2883df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2884df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 28859378e437SKip Macy *lp = inp->inp_lport; 28869378e437SKip Macy *fp = inp->inp_fport; 28879378e437SKip Macy } 28889378e437SKip Macy 2889cc65eb4eSGleb Smirnoff /* 2890cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2891cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2892cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2893cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2894cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2895cc65eb4eSGleb Smirnoff * hidden from users. 2896cc65eb4eSGleb Smirnoff */ 2897cc65eb4eSGleb Smirnoff void 2898cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2899cc65eb4eSGleb Smirnoff { 2900cc65eb4eSGleb Smirnoff 290179db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2902cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2903cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2904cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2905cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2906cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 2907cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2908cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2909cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2910cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2911cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2912cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2913cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2914cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2915cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2916cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2917cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2918cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2919cc65eb4eSGleb Smirnoff } 2920cc65eb4eSGleb Smirnoff 2921a35bdd44SMichael Tuexen int 2922a35bdd44SMichael Tuexen sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, 2923a35bdd44SMichael Tuexen int (*ctloutput_set)(struct inpcb *, struct sockopt *)) 2924a35bdd44SMichael Tuexen { 2925a35bdd44SMichael Tuexen struct sockopt sopt; 2926a35bdd44SMichael Tuexen struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2927a35bdd44SMichael Tuexen INPLOOKUP_WLOCKPCB); 2928a35bdd44SMichael Tuexen struct inpcb *inp; 2929a35bdd44SMichael Tuexen struct sockopt_parameters *params; 2930a35bdd44SMichael Tuexen struct socket *so; 2931a35bdd44SMichael Tuexen int error; 2932a35bdd44SMichael Tuexen char buf[1024]; 2933a35bdd44SMichael Tuexen 2934a35bdd44SMichael Tuexen if (req->oldptr != NULL || req->oldlen != 0) 2935a35bdd44SMichael Tuexen return (EINVAL); 2936a35bdd44SMichael Tuexen if (req->newptr == NULL) 2937a35bdd44SMichael Tuexen return (EPERM); 2938a35bdd44SMichael Tuexen if (req->newlen > sizeof(buf)) 2939a35bdd44SMichael Tuexen return (ENOMEM); 2940a35bdd44SMichael Tuexen error = SYSCTL_IN(req, buf, req->newlen); 2941a35bdd44SMichael Tuexen if (error != 0) 2942a35bdd44SMichael Tuexen return (error); 2943a35bdd44SMichael Tuexen if (req->newlen < sizeof(struct sockopt_parameters)) 2944a35bdd44SMichael Tuexen return (EINVAL); 2945a35bdd44SMichael Tuexen params = (struct sockopt_parameters *)buf; 2946a35bdd44SMichael Tuexen sopt.sopt_level = params->sop_level; 2947a35bdd44SMichael Tuexen sopt.sopt_name = params->sop_optname; 2948a35bdd44SMichael Tuexen sopt.sopt_dir = SOPT_SET; 2949a35bdd44SMichael Tuexen sopt.sopt_val = params->sop_optval; 2950a35bdd44SMichael Tuexen sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters); 2951a35bdd44SMichael Tuexen sopt.sopt_td = NULL; 2952a0aeb1ceSMichael Tuexen #ifdef INET6 2953a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) { 2954a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_laddr)) 2955a35bdd44SMichael Tuexen params->sop_inc.inc6_laddr.s6_addr16[1] = 2956a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2957a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_faddr)) 2958a35bdd44SMichael Tuexen params->sop_inc.inc6_faddr.s6_addr16[1] = 2959a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2960a35bdd44SMichael Tuexen } 2961a0aeb1ceSMichael Tuexen #endif 296213720136SMichael Tuexen if (params->sop_inc.inc_lport != htons(0) && 296313720136SMichael Tuexen params->sop_inc.inc_fport != htons(0)) { 2964a0aeb1ceSMichael Tuexen #ifdef INET6 2965a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) 2966a35bdd44SMichael Tuexen inpi.hash = INP6_PCBHASH( 2967a35bdd44SMichael Tuexen ¶ms->sop_inc.inc6_faddr, 2968a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2969a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2970a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2971a35bdd44SMichael Tuexen else 2972a0aeb1ceSMichael Tuexen #endif 2973a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH( 2974a35bdd44SMichael Tuexen ¶ms->sop_inc.inc_faddr, 2975a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2976a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2977a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2978a35bdd44SMichael Tuexen } 2979a35bdd44SMichael Tuexen while ((inp = inp_next(&inpi)) != NULL) 2980a35bdd44SMichael Tuexen if (inp->inp_gencnt == params->sop_id) { 298153af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 2982a35bdd44SMichael Tuexen INP_WUNLOCK(inp); 2983a35bdd44SMichael Tuexen return (ECONNRESET); 2984a35bdd44SMichael Tuexen } 2985a35bdd44SMichael Tuexen so = inp->inp_socket; 2986a35bdd44SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 2987a35bdd44SMichael Tuexen soref(so); 2988bed7633bSMichael Tuexen if (params->sop_level == SOL_SOCKET) { 2989bed7633bSMichael Tuexen INP_WUNLOCK(inp); 2990bed7633bSMichael Tuexen error = sosetopt(so, &sopt); 2991bed7633bSMichael Tuexen } else 2992a35bdd44SMichael Tuexen error = (*ctloutput_set)(inp, &sopt); 2993a35bdd44SMichael Tuexen sorele(so); 2994a35bdd44SMichael Tuexen break; 2995a35bdd44SMichael Tuexen } 2996a35bdd44SMichael Tuexen if (inp == NULL) 2997a35bdd44SMichael Tuexen error = ESRCH; 2998a35bdd44SMichael Tuexen return (error); 2999a35bdd44SMichael Tuexen } 3000a35bdd44SMichael Tuexen 3001497057eeSRobert Watson #ifdef DDB 3002497057eeSRobert Watson static void 3003497057eeSRobert Watson db_print_indent(int indent) 3004497057eeSRobert Watson { 3005497057eeSRobert Watson int i; 3006497057eeSRobert Watson 3007497057eeSRobert Watson for (i = 0; i < indent; i++) 3008497057eeSRobert Watson db_printf(" "); 3009497057eeSRobert Watson } 3010497057eeSRobert Watson 3011497057eeSRobert Watson static void 3012497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 3013497057eeSRobert Watson { 3014497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 3015497057eeSRobert Watson 3016497057eeSRobert Watson db_print_indent(indent); 3017497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 3018497057eeSRobert Watson 3019497057eeSRobert Watson indent += 2; 3020497057eeSRobert Watson 302103dc38a4SRobert Watson #ifdef INET6 3022dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3023497057eeSRobert Watson /* IPv6. */ 3024497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 3025497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 302683e521ecSBjoern A. Zeeb } else 302703dc38a4SRobert Watson #endif 302883e521ecSBjoern A. Zeeb { 3029497057eeSRobert Watson /* IPv4. */ 3030497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 3031497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 3032497057eeSRobert Watson } 3033497057eeSRobert Watson db_print_indent(indent); 3034497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 3035497057eeSRobert Watson ntohs(inc->inc_lport)); 3036497057eeSRobert Watson db_print_indent(indent); 3037497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 3038497057eeSRobert Watson ntohs(inc->inc_fport)); 3039497057eeSRobert Watson } 3040497057eeSRobert Watson 3041497057eeSRobert Watson static void 3042497057eeSRobert Watson db_print_inpflags(int inp_flags) 3043497057eeSRobert Watson { 3044497057eeSRobert Watson int comma; 3045497057eeSRobert Watson 3046497057eeSRobert Watson comma = 0; 3047497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 3048497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 3049497057eeSRobert Watson comma = 1; 3050497057eeSRobert Watson } 3051497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 3052497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 3053497057eeSRobert Watson comma = 1; 3054497057eeSRobert Watson } 3055497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 3056497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 3057497057eeSRobert Watson comma = 1; 3058497057eeSRobert Watson } 3059dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 3060dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 3061dce33a45SErmal Luçi comma = 1; 3062dce33a45SErmal Luçi } 3063497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 3064497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 3065497057eeSRobert Watson comma = 1; 3066497057eeSRobert Watson } 3067497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 3068497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 3069497057eeSRobert Watson comma = 1; 3070497057eeSRobert Watson } 3071497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 3072497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 3073497057eeSRobert Watson comma = 1; 3074497057eeSRobert Watson } 3075497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 3076497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 3077497057eeSRobert Watson comma = 1; 3078497057eeSRobert Watson } 3079497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 3080497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 3081497057eeSRobert Watson comma = 1; 3082497057eeSRobert Watson } 3083497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 3084497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 3085497057eeSRobert Watson comma = 1; 3086497057eeSRobert Watson } 3087497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 3088497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 3089497057eeSRobert Watson comma = 1; 3090497057eeSRobert Watson } 3091497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 3092497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 3093497057eeSRobert Watson comma = 1; 3094497057eeSRobert Watson } 30953cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 30963cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 30973cca425bSMichael Tuexen comma = 1; 30983cca425bSMichael Tuexen } 3099497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 3100497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3101497057eeSRobert Watson comma = 1; 3102497057eeSRobert Watson } 3103497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 3104497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3105497057eeSRobert Watson comma = 1; 3106497057eeSRobert Watson } 3107497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 3108497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3109497057eeSRobert Watson comma = 1; 3110497057eeSRobert Watson } 3111497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 3112497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3113497057eeSRobert Watson comma = 1; 3114497057eeSRobert Watson } 3115497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 3116497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3117497057eeSRobert Watson comma = 1; 3118497057eeSRobert Watson } 3119497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 3120497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3121497057eeSRobert Watson comma = 1; 3122497057eeSRobert Watson } 3123497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 3124497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3125497057eeSRobert Watson comma = 1; 3126497057eeSRobert Watson } 3127497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 3128497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3129497057eeSRobert Watson comma = 1; 3130497057eeSRobert Watson } 3131497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 3132497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3133497057eeSRobert Watson comma = 1; 3134497057eeSRobert Watson } 3135ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 3136ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3137ad71fe3cSRobert Watson comma = 1; 3138ad71fe3cSRobert Watson } 3139ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 3140ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 3141ad71fe3cSRobert Watson comma = 1; 3142ad71fe3cSRobert Watson } 3143ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 3144ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3145ad71fe3cSRobert Watson comma = 1; 3146ad71fe3cSRobert Watson } 3147497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 3148497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3149497057eeSRobert Watson comma = 1; 3150497057eeSRobert Watson } 3151497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 3152497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 3153497057eeSRobert Watson comma = 1; 3154497057eeSRobert Watson } 3155497057eeSRobert Watson } 3156497057eeSRobert Watson 3157497057eeSRobert Watson static void 3158497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 3159497057eeSRobert Watson { 3160497057eeSRobert Watson int comma; 3161497057eeSRobert Watson 3162497057eeSRobert Watson comma = 0; 3163497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3164497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3165497057eeSRobert Watson comma = 1; 3166497057eeSRobert Watson } 3167497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3168497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3169497057eeSRobert Watson comma = 1; 3170497057eeSRobert Watson } 3171497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3172497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3173497057eeSRobert Watson comma = 1; 3174497057eeSRobert Watson } 3175497057eeSRobert Watson } 3176497057eeSRobert Watson 31776d888973SRobert Watson static void 3178497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3179497057eeSRobert Watson { 3180497057eeSRobert Watson 3181497057eeSRobert Watson db_print_indent(indent); 3182497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3183497057eeSRobert Watson 3184497057eeSRobert Watson indent += 2; 3185497057eeSRobert Watson 3186497057eeSRobert Watson db_print_indent(indent); 3187497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3188497057eeSRobert Watson 3189497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3190497057eeSRobert Watson 3191497057eeSRobert Watson db_print_indent(indent); 3192497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3193497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3194497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3195497057eeSRobert Watson db_printf(")\n"); 3196497057eeSRobert Watson 3197497057eeSRobert Watson db_print_indent(indent); 3198497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3199497057eeSRobert Watson inp->inp_vflag); 3200497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3201497057eeSRobert Watson db_printf(")\n"); 3202497057eeSRobert Watson 3203497057eeSRobert Watson db_print_indent(indent); 3204497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3205497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3206497057eeSRobert Watson 3207497057eeSRobert Watson db_print_indent(indent); 3208497057eeSRobert Watson #ifdef INET6 3209497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3210497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3211497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3212497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3213497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3214497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3215497057eeSRobert Watson inp->in6p_hops); 3216497057eeSRobert Watson } else 3217497057eeSRobert Watson #endif 3218497057eeSRobert Watson { 3219497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3220497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3221497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3222497057eeSRobert Watson } 3223497057eeSRobert Watson 3224497057eeSRobert Watson db_print_indent(indent); 32255f539170SGleb Smirnoff db_printf("inp_gencnt: %ju\n", (uintmax_t)inp->inp_gencnt); 3226497057eeSRobert Watson } 3227497057eeSRobert Watson 3228497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3229497057eeSRobert Watson { 3230497057eeSRobert Watson struct inpcb *inp; 3231497057eeSRobert Watson 3232497057eeSRobert Watson if (!have_addr) { 3233497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3234497057eeSRobert Watson return; 3235497057eeSRobert Watson } 3236497057eeSRobert Watson inp = (struct inpcb *)addr; 3237497057eeSRobert Watson 3238497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3239497057eeSRobert Watson } 324083e521ecSBjoern A. Zeeb #endif /* DDB */ 3241f3e7afe2SHans Petter Selasky 3242f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3243f3e7afe2SHans Petter Selasky /* 3244f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3245f3e7afe2SHans Petter Selasky * if any. 3246f3e7afe2SHans Petter Selasky */ 3247f3e7afe2SHans Petter Selasky int 3248f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3249f3e7afe2SHans Petter Selasky { 3250f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3251f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 325220abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3253f3e7afe2SHans Petter Selasky }; 3254f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3255f3e7afe2SHans Petter Selasky int error; 3256f3e7afe2SHans Petter Selasky 3257f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3258f3e7afe2SHans Petter Selasky if (mst == NULL) 3259f3e7afe2SHans Petter Selasky return (EINVAL); 3260f3e7afe2SHans Petter Selasky 3261c782ea8bSJohn Baldwin if (mst->sw->snd_tag_modify == NULL) { 3262f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3263f3e7afe2SHans Petter Selasky } else { 3264c782ea8bSJohn Baldwin error = mst->sw->snd_tag_modify(mst, ¶ms); 3265f3e7afe2SHans Petter Selasky } 3266f3e7afe2SHans Petter Selasky return (error); 3267f3e7afe2SHans Petter Selasky } 3268f3e7afe2SHans Petter Selasky 3269f3e7afe2SHans Petter Selasky /* 3270f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3271f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3272f3e7afe2SHans Petter Selasky */ 3273f3e7afe2SHans Petter Selasky int 3274f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3275f3e7afe2SHans Petter Selasky { 3276f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3277f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3278f3e7afe2SHans Petter Selasky int error; 3279f3e7afe2SHans Petter Selasky 3280f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3281f3e7afe2SHans Petter Selasky if (mst == NULL) 3282f3e7afe2SHans Petter Selasky return (EINVAL); 3283f3e7afe2SHans Petter Selasky 3284c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) { 3285f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3286f3e7afe2SHans Petter Selasky } else { 3287c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 3288f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3289f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3290f3e7afe2SHans Petter Selasky } 3291f3e7afe2SHans Petter Selasky return (error); 3292f3e7afe2SHans Petter Selasky } 3293f3e7afe2SHans Petter Selasky 3294f3e7afe2SHans Petter Selasky /* 329595ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 329695ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 329795ed5015SHans Petter Selasky */ 329895ed5015SHans Petter Selasky int 329995ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 330095ed5015SHans Petter Selasky { 330195ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 330295ed5015SHans Petter Selasky struct m_snd_tag *mst; 330395ed5015SHans Petter Selasky int error; 330495ed5015SHans Petter Selasky 330595ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 330695ed5015SHans Petter Selasky if (mst == NULL) 330795ed5015SHans Petter Selasky return (EINVAL); 330895ed5015SHans Petter Selasky 3309c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) 331095ed5015SHans Petter Selasky return (EOPNOTSUPP); 331195ed5015SHans Petter Selasky 3312c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 331395ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 331495ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 331595ed5015SHans Petter Selasky return (error); 331695ed5015SHans Petter Selasky } 331795ed5015SHans Petter Selasky 331895ed5015SHans Petter Selasky /* 3319f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3320f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3321f3e7afe2SHans Petter Selasky */ 3322f3e7afe2SHans Petter Selasky int 3323f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 332420abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 332520abea66SRandall Stewart 3326f3e7afe2SHans Petter Selasky { 3327f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 332895ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 332995ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3330f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3331f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 333298085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3333f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 333420abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3335f3e7afe2SHans Petter Selasky }; 3336f3e7afe2SHans Petter Selasky int error; 3337f3e7afe2SHans Petter Selasky 3338f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3339f3e7afe2SHans Petter Selasky 334085d8d30fSHans Petter Selasky /* 334185d8d30fSHans Petter Selasky * If there is already a send tag, or the INP is being torn 334285d8d30fSHans Petter Selasky * down, allocating a new send tag is not allowed. Else send 334385d8d30fSHans Petter Selasky * tags may leak. 334485d8d30fSHans Petter Selasky */ 334553af6903SGleb Smirnoff if (*st != NULL || (inp->inp_flags & INP_DROPPED) != 0) 3346f3e7afe2SHans Petter Selasky return (EINVAL); 3347f3e7afe2SHans Petter Selasky 334836e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, st); 3349903c4ee6SXin LI #ifdef INET 335020abea66SRandall Stewart if (error == 0) { 335120abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 335220abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 335336e0a362SJohn Baldwin } else if (error != EOPNOTSUPP) 335420abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3355903c4ee6SXin LI #endif 3356f3e7afe2SHans Petter Selasky return (error); 3357f3e7afe2SHans Petter Selasky } 3358f3e7afe2SHans Petter Selasky 335920abea66SRandall Stewart void 336098d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst) 336120abea66SRandall Stewart { 336220abea66SRandall Stewart 336398d7a8d9SJohn Baldwin m_snd_tag_rele(mst); 3364903c4ee6SXin LI #ifdef INET 336520abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3366903c4ee6SXin LI #endif 336720abea66SRandall Stewart } 336820abea66SRandall Stewart 3369f3e7afe2SHans Petter Selasky /* 3370f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3371f3e7afe2SHans Petter Selasky * if any: 3372f3e7afe2SHans Petter Selasky */ 3373f3e7afe2SHans Petter Selasky void 3374f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3375f3e7afe2SHans Petter Selasky { 3376f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3377f3e7afe2SHans Petter Selasky 3378f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3379f3e7afe2SHans Petter Selasky 3380f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3381f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3382f3e7afe2SHans Petter Selasky 3383f3e7afe2SHans Petter Selasky if (mst == NULL) 3384f3e7afe2SHans Petter Selasky return; 3385f3e7afe2SHans Petter Selasky 3386fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3387093e7231SHans Petter Selasky #ifdef INET 3388093e7231SHans Petter Selasky counter_u64_add(rate_limit_active, -1); 3389093e7231SHans Petter Selasky #endif 3390f3e7afe2SHans Petter Selasky } 3391f3e7afe2SHans Petter Selasky 339220abea66SRandall Stewart int 339320abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 339420abea66SRandall Stewart { 339520abea66SRandall Stewart int error; 339620abea66SRandall Stewart 339720abea66SRandall Stewart /* 339820abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 339920abea66SRandall Stewart * a route change, first drop the existing tag. Set the 340020abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 340120abea66SRandall Stewart * tag if we fail to allocate one this time. 340220abea66SRandall Stewart */ 340320abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 340420abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 340520abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 340620abea66SRandall Stewart } 340720abea66SRandall Stewart 340820abea66SRandall Stewart /* 340920abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 341020abea66SRandall Stewart * made to ensure the network interface doesn't go away until 341120abea66SRandall Stewart * all ratelimit connections are gone. The network interface 341220abea66SRandall Stewart * pointers compared below represent valid network interfaces, 341320abea66SRandall Stewart * except when comparing towards NULL. 341420abea66SRandall Stewart */ 341520abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 341620abea66SRandall Stewart error = 0; 341720abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 341820abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 341920abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 342020abea66SRandall Stewart error = 0; 342120abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 342220abea66SRandall Stewart /* 342320abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 342420abea66SRandall Stewart * to wait until there is a valid RSS hash before we 342520abea66SRandall Stewart * can proceed: 342620abea66SRandall Stewart */ 342720abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 342820abea66SRandall Stewart error = EAGAIN; 342920abea66SRandall Stewart } else { 343020abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 343120abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 343220abea66SRandall Stewart } 343320abea66SRandall Stewart } else { 343420abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 343520abea66SRandall Stewart } 343620abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 343720abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 343820abea66SRandall Stewart 343920abea66SRandall Stewart return (error); 344020abea66SRandall Stewart } 344120abea66SRandall Stewart 3442f3e7afe2SHans Petter Selasky /* 3443f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3444f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3445f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3446f3e7afe2SHans Petter Selasky */ 3447f3e7afe2SHans Petter Selasky void 3448f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3449f3e7afe2SHans Petter Selasky { 3450f3e7afe2SHans Petter Selasky struct socket *socket; 3451f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3452f3e7afe2SHans Petter Selasky bool did_upgrade; 3453f3e7afe2SHans Petter Selasky 3454f3e7afe2SHans Petter Selasky if (inp == NULL) 3455f3e7afe2SHans Petter Selasky return; 3456f3e7afe2SHans Petter Selasky 3457f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3458f3e7afe2SHans Petter Selasky if (socket == NULL) 3459f3e7afe2SHans Petter Selasky return; 3460f3e7afe2SHans Petter Selasky 3461f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3462f3e7afe2SHans Petter Selasky /* 3463f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3464f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3465f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3466f3e7afe2SHans Petter Selasky * write lock. 3467f3e7afe2SHans Petter Selasky */ 3468f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3469f3e7afe2SHans Petter Selasky return; 3470f3e7afe2SHans Petter Selasky did_upgrade = 1; 3471f3e7afe2SHans Petter Selasky } else { 3472f3e7afe2SHans Petter Selasky did_upgrade = 0; 3473f3e7afe2SHans Petter Selasky } 3474f3e7afe2SHans Petter Selasky 3475f3e7afe2SHans Petter Selasky /* 3476f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3477f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3478f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3479f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3480f3e7afe2SHans Petter Selasky */ 3481f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3482f3e7afe2SHans Petter Selasky 3483bab34d63SJohn Baldwin in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3484fb3bc596SJohn Baldwin 3485f3e7afe2SHans Petter Selasky if (did_upgrade) 3486f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3487f3e7afe2SHans Petter Selasky } 3488f3e7afe2SHans Petter Selasky 3489f3e7afe2SHans Petter Selasky /* 3490f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3491f3e7afe2SHans Petter Selasky */ 3492f3e7afe2SHans Petter Selasky void 3493f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3494f3e7afe2SHans Petter Selasky { 3495f3e7afe2SHans Petter Selasky bool did_upgrade; 3496f3e7afe2SHans Petter Selasky 3497f3e7afe2SHans Petter Selasky if (inp == NULL) 3498f3e7afe2SHans Petter Selasky return; 3499f3e7afe2SHans Petter Selasky 3500f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3501f3e7afe2SHans Petter Selasky return; 3502f3e7afe2SHans Petter Selasky 3503f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3504f3e7afe2SHans Petter Selasky /* 3505f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3506f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3507f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3508f3e7afe2SHans Petter Selasky * write lock. 3509f3e7afe2SHans Petter Selasky */ 3510f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3511f3e7afe2SHans Petter Selasky return; 3512f3e7afe2SHans Petter Selasky did_upgrade = 1; 3513f3e7afe2SHans Petter Selasky } else { 3514f3e7afe2SHans Petter Selasky did_upgrade = 0; 3515f3e7afe2SHans Petter Selasky } 3516f3e7afe2SHans Petter Selasky 3517f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3518f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3519f3e7afe2SHans Petter Selasky 3520f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3521f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3522f3e7afe2SHans Petter Selasky 3523f3e7afe2SHans Petter Selasky if (did_upgrade) 3524f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3525f3e7afe2SHans Petter Selasky } 352620abea66SRandall Stewart 3527903c4ee6SXin LI #ifdef INET 352820abea66SRandall Stewart static void 352920abea66SRandall Stewart rl_init(void *st) 353020abea66SRandall Stewart { 35311a714ff2SRandall Stewart rate_limit_new = counter_u64_alloc(M_WAITOK); 35321a714ff2SRandall Stewart rate_limit_chg = counter_u64_alloc(M_WAITOK); 353320abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 353420abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 353520abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 353620abea66SRandall Stewart } 353720abea66SRandall Stewart 353820abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3539903c4ee6SXin LI #endif 3540f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3541