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) 4381a43cff9SSean Bruno return (ENOBUFS); 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) 4521a43cff9SSean Bruno return (ENOBUFS); 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); 7354b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 7364b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 7374b932371SIan Dowse inp->inp_lport = 0; 738bbd0084bSMark Johnston inp->inp_flags &= ~INP_BOUNDFIB; 7394b932371SIan Dowse return (EAGAIN); 7404b932371SIan Dowse } 7414b932371SIan Dowse if (anonport) 7424b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 7434b932371SIan Dowse return (0); 7444b932371SIan Dowse } 74567107f45SBjoern A. Zeeb #endif 7464b932371SIan Dowse 747efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 74825102351SMike Karels /* 74925102351SMike Karels * Assign a local port like in_pcb_lport(), but also used with connect() 75025102351SMike Karels * and a foreign address and port. If fsa is non-NULL, choose a local port 75125102351SMike Karels * that is unused with those, otherwise one that is completely unused. 7522fdbcbeaSMike Karels * lsa can be NULL for IPv6. 75325102351SMike Karels */ 754efc76f72SBjoern A. Zeeb int 755bafe022bSGleb Smirnoff in_pcb_lport_dest(const struct inpcb *inp, struct sockaddr *lsa, 756bafe022bSGleb Smirnoff u_short *lportp, struct sockaddr *fsa, u_short fport, struct ucred *cred, 757bafe022bSGleb Smirnoff int lookupflags) 758efc76f72SBjoern A. Zeeb { 759efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 760efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 761efc76f72SBjoern A. Zeeb unsigned short *lastport; 76219acc506SGleb Smirnoff int count, error; 763efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 764efc76f72SBjoern A. Zeeb #ifdef INET 76525102351SMike Karels struct in_addr laddr, faddr; 76625102351SMike Karels #endif 76725102351SMike Karels #ifdef INET6 76825102351SMike Karels struct in6_addr *laddr6, *faddr6; 769efc76f72SBjoern A. Zeeb #endif 770efc76f72SBjoern A. Zeeb 771efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 772efc76f72SBjoern A. Zeeb 773efc76f72SBjoern A. Zeeb /* 774efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 775efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 776efc76f72SBjoern A. Zeeb */ 777efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 778fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 779efc76f72SBjoern A. Zeeb 780efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 781efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 782efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 783efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 784efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 785cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 786efc76f72SBjoern A. Zeeb if (error) 787efc76f72SBjoern A. Zeeb return (error); 788efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 789efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 790efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 791efc76f72SBjoern A. Zeeb } else { 792efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 793efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 794efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 795efc76f72SBjoern A. Zeeb } 79619acc506SGleb Smirnoff 797efc76f72SBjoern A. Zeeb /* 798efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 799efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 800efc76f72SBjoern A. Zeeb * code path implementing all logic. 801efc76f72SBjoern A. Zeeb */ 802efc76f72SBjoern A. Zeeb if (first > last) { 803efc76f72SBjoern A. Zeeb aux = first; 804efc76f72SBjoern A. Zeeb first = last; 805efc76f72SBjoern A. Zeeb last = aux; 806efc76f72SBjoern A. Zeeb } 807efc76f72SBjoern A. Zeeb 808efc76f72SBjoern A. Zeeb #ifdef INET 809637f317cSMike Karels laddr.s_addr = INADDR_ANY; /* used by INET6+INET below too */ 8104d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 8112fdbcbeaSMike Karels if (lsa != NULL) 81225102351SMike Karels laddr = ((struct sockaddr_in *)lsa)->sin_addr; 81325102351SMike Karels if (fsa != NULL) 81425102351SMike Karels faddr = ((struct sockaddr_in *)fsa)->sin_addr; 815efc76f72SBjoern A. Zeeb } 816efc76f72SBjoern A. Zeeb #endif 81725102351SMike Karels #ifdef INET6 8182fdbcbeaSMike Karels laddr6 = NULL; 8192fdbcbeaSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 8202fdbcbeaSMike Karels if (lsa != NULL) 82125102351SMike Karels laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 82225102351SMike Karels if (fsa != NULL) 82325102351SMike Karels faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 82425102351SMike Karels } 82525102351SMike Karels #endif 82625102351SMike Karels 82725102351SMike Karels tmpinp = NULL; 828efc76f72SBjoern A. Zeeb lport = *lportp; 829efc76f72SBjoern A. Zeeb 83019acc506SGleb Smirnoff if (V_ipport_randomized) 831efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 832efc76f72SBjoern A. Zeeb 833efc76f72SBjoern A. Zeeb count = last - first; 834efc76f72SBjoern A. Zeeb 835efc76f72SBjoern A. Zeeb do { 836efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 837efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 838efc76f72SBjoern A. Zeeb ++*lastport; 839efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 840efc76f72SBjoern A. Zeeb *lastport = first; 841efc76f72SBjoern A. Zeeb lport = htons(*lastport); 842efc76f72SBjoern A. Zeeb 84325102351SMike Karels if (fsa != NULL) { 84425102351SMike Karels #ifdef INET 84525102351SMike Karels if (lsa->sa_family == AF_INET) { 84625102351SMike Karels tmpinp = in_pcblookup_hash_locked(pcbinfo, 84725102351SMike Karels faddr, fport, laddr, lport, lookupflags, 848da806e8dSMark Johnston M_NODOM, RT_ALL_FIBS); 84925102351SMike Karels } 85025102351SMike Karels #endif 85125102351SMike Karels #ifdef INET6 85225102351SMike Karels if (lsa->sa_family == AF_INET6) { 85325102351SMike Karels tmpinp = in6_pcblookup_hash_locked(pcbinfo, 85425102351SMike Karels faddr6, fport, laddr6, lport, lookupflags, 855da806e8dSMark Johnston M_NODOM, RT_ALL_FIBS); 85625102351SMike Karels } 85725102351SMike Karels #endif 85825102351SMike Karels } else { 859efc76f72SBjoern A. Zeeb #ifdef INET6 860637f317cSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 861efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 8629a413162SMark Johnston &inp->in6p_laddr, lport, RT_ALL_FIBS, 8639a413162SMark Johnston lookupflags, cred); 864637f317cSMike Karels #ifdef INET 865637f317cSMike Karels if (tmpinp == NULL && 866637f317cSMike Karels (inp->inp_vflag & INP_IPV4)) 867637f317cSMike Karels tmpinp = in_pcblookup_local(pcbinfo, 8689a413162SMark Johnston laddr, lport, RT_ALL_FIBS, 8699a413162SMark Johnston lookupflags, cred); 870637f317cSMike Karels #endif 871637f317cSMike Karels } 872efc76f72SBjoern A. Zeeb #endif 873efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 874efc76f72SBjoern A. Zeeb else 875efc76f72SBjoern A. Zeeb #endif 876efc76f72SBjoern A. Zeeb #ifdef INET 877efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 8789a413162SMark Johnston lport, RT_ALL_FIBS, lookupflags, cred); 879efc76f72SBjoern A. Zeeb #endif 88025102351SMike Karels } 881efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 882efc76f72SBjoern A. Zeeb 883efc76f72SBjoern A. Zeeb *lportp = lport; 884efc76f72SBjoern A. Zeeb 885efc76f72SBjoern A. Zeeb return (0); 886efc76f72SBjoern A. Zeeb } 887efdf104bSMikolaj Golub 888efdf104bSMikolaj Golub /* 88925102351SMike Karels * Select a local port (number) to use. 89025102351SMike Karels */ 89125102351SMike Karels int 89225102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 89325102351SMike Karels struct ucred *cred, int lookupflags) 89425102351SMike Karels { 89525102351SMike Karels struct sockaddr_in laddr; 89625102351SMike Karels 89725102351SMike Karels if (laddrp) { 89825102351SMike Karels bzero(&laddr, sizeof(laddr)); 89925102351SMike Karels laddr.sin_family = AF_INET; 90025102351SMike Karels laddr.sin_addr = *laddrp; 90125102351SMike Karels } 90225102351SMike Karels return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 90325102351SMike Karels NULL, lportp, NULL, 0, cred, lookupflags)); 90425102351SMike Karels } 905efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 906efc76f72SBjoern A. Zeeb 907d5bb8bd3SAdrian Chadd #ifdef INET 9080a100a6fSAdrian Chadd /* 90901f8ce83SMark Johnston * Determine whether the inpcb can be bound to the specified address/port tuple. 91001f8ce83SMark Johnston */ 91101f8ce83SMark Johnston static int 91201f8ce83SMark Johnston in_pcbbind_avail(struct inpcb *inp, const struct in_addr laddr, 913bbd0084bSMark Johnston const u_short lport, const int fib, int sooptions, int lookupflags, 914bbd0084bSMark Johnston struct ucred *cred) 91501f8ce83SMark Johnston { 91601f8ce83SMark Johnston int reuseport, reuseport_lb; 91701f8ce83SMark Johnston 91801f8ce83SMark Johnston INP_LOCK_ASSERT(inp); 91901f8ce83SMark Johnston INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 92001f8ce83SMark Johnston 92101f8ce83SMark Johnston reuseport = (sooptions & SO_REUSEPORT); 92201f8ce83SMark Johnston reuseport_lb = (sooptions & SO_REUSEPORT_LB); 92301f8ce83SMark Johnston 92401f8ce83SMark Johnston if (IN_MULTICAST(ntohl(laddr.s_addr))) { 92501f8ce83SMark Johnston /* 92601f8ce83SMark Johnston * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 92701f8ce83SMark Johnston * allow complete duplication of binding if 92801f8ce83SMark Johnston * SO_REUSEPORT is set, or if SO_REUSEADDR is set 92901f8ce83SMark Johnston * and a multicast address is bound on both 93001f8ce83SMark Johnston * new and duplicated sockets. 93101f8ce83SMark Johnston */ 93201f8ce83SMark Johnston if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0) 93301f8ce83SMark Johnston reuseport = SO_REUSEADDR | SO_REUSEPORT; 93401f8ce83SMark Johnston /* 93501f8ce83SMark Johnston * XXX: How to deal with SO_REUSEPORT_LB here? 93601f8ce83SMark Johnston * Treat same as SO_REUSEPORT for now. 93701f8ce83SMark Johnston */ 93801f8ce83SMark Johnston if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0) 93901f8ce83SMark Johnston reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB; 94001f8ce83SMark Johnston } else if (!in_nullhost(laddr)) { 94101f8ce83SMark Johnston struct sockaddr_in sin; 94201f8ce83SMark Johnston 94301f8ce83SMark Johnston memset(&sin, 0, sizeof(sin)); 94401f8ce83SMark Johnston sin.sin_family = AF_INET; 94501f8ce83SMark Johnston sin.sin_len = sizeof(sin); 94601f8ce83SMark Johnston sin.sin_addr = laddr; 94701f8ce83SMark Johnston 94801f8ce83SMark Johnston /* 94901f8ce83SMark Johnston * Is the address a local IP address? 95001f8ce83SMark Johnston * If INP_BINDANY is set, then the socket may be bound 95101f8ce83SMark Johnston * to any endpoint address, local or not. 95201f8ce83SMark Johnston */ 95301f8ce83SMark Johnston if ((inp->inp_flags & INP_BINDANY) == 0 && 95401f8ce83SMark Johnston ifa_ifwithaddr_check((const struct sockaddr *)&sin) == 0) 95501f8ce83SMark Johnston return (EADDRNOTAVAIL); 95601f8ce83SMark Johnston } 95701f8ce83SMark Johnston 95801f8ce83SMark Johnston if (lport != 0) { 95901f8ce83SMark Johnston struct inpcb *t; 96001f8ce83SMark Johnston 96101f8ce83SMark Johnston if (ntohs(lport) <= V_ipport_reservedhigh && 96201f8ce83SMark Johnston ntohs(lport) >= V_ipport_reservedlow && 96301f8ce83SMark Johnston priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 96401f8ce83SMark Johnston return (EACCES); 96501f8ce83SMark Johnston 96601f8ce83SMark Johnston if (!IN_MULTICAST(ntohl(laddr.s_addr)) && 96701f8ce83SMark Johnston priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 968c9756953SMark Johnston /* 969c9756953SMark Johnston * If a socket owned by a different user is already 970c9756953SMark Johnston * bound to this port, fail. In particular, SO_REUSE* 971c9756953SMark Johnston * can only be used to share a port among sockets owned 972c9756953SMark Johnston * by the same user. 973c9756953SMark Johnston * 974c9756953SMark Johnston * However, we can share a port with a connected socket 975c9756953SMark Johnston * which has a unique 4-tuple. 976c9756953SMark Johnston */ 97701f8ce83SMark Johnston t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport, 9789a413162SMark Johnston RT_ALL_FIBS, INPLOOKUP_WILDCARD, cred); 97901f8ce83SMark Johnston if (t != NULL && 98001f8ce83SMark Johnston (inp->inp_socket->so_type != SOCK_STREAM || 98101f8ce83SMark Johnston in_nullhost(t->inp_faddr)) && 98201f8ce83SMark Johnston (inp->inp_cred->cr_uid != t->inp_cred->cr_uid)) 98301f8ce83SMark Johnston return (EADDRINUSE); 98401f8ce83SMark Johnston } 985bbd0084bSMark Johnston t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport, fib, 986bbd0084bSMark Johnston lookupflags, cred); 98701f8ce83SMark Johnston if (t != NULL && ((reuseport | reuseport_lb) & 98801f8ce83SMark Johnston t->inp_socket->so_options) == 0) { 98901f8ce83SMark Johnston #ifdef INET6 99001f8ce83SMark Johnston if (!in_nullhost(laddr) || 99101f8ce83SMark Johnston !in_nullhost(t->inp_laddr) || 99201f8ce83SMark Johnston (inp->inp_vflag & INP_IPV6PROTO) == 0 || 99301f8ce83SMark Johnston (t->inp_vflag & INP_IPV6PROTO) == 0) 99401f8ce83SMark Johnston #endif 99501f8ce83SMark Johnston return (EADDRINUSE); 99601f8ce83SMark Johnston } 99701f8ce83SMark Johnston } 99801f8ce83SMark Johnston return (0); 99901f8ce83SMark Johnston } 100001f8ce83SMark Johnston 100101f8ce83SMark Johnston /* 10024b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 10034b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 10044b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 10054b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 10064b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 10074b932371SIan Dowse * 10084b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 10094b932371SIan Dowse */ 10104b932371SIan Dowse int 101196871af0SGleb Smirnoff in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, 1012bbd0084bSMark Johnston u_short *lportp, int flags, struct ucred *cred) 10134b932371SIan Dowse { 10144b932371SIan Dowse struct socket *so = inp->inp_socket; 10154b932371SIan Dowse struct in_addr laddr; 1016df8bae1dSRodney W. Grimes u_short lport = 0; 1017bbd0084bSMark Johnston int error, fib, lookupflags, sooptions; 1018df8bae1dSRodney W. Grimes 10198501a69cSRobert Watson /* 1020fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 10218501a69cSRobert Watson */ 102259daba27SSam Leffler INP_LOCK_ASSERT(inp); 1023ffb3d384SMark Johnston INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 102459daba27SSam Leffler 10254b932371SIan Dowse laddr.s_addr = *laddrp; 102696871af0SGleb Smirnoff if (sin != NULL && laddr.s_addr != INADDR_ANY) 1027df8bae1dSRodney W. Grimes return (EINVAL); 102801f8ce83SMark Johnston 102901f8ce83SMark Johnston lookupflags = 0; 103001f8ce83SMark Johnston sooptions = atomic_load_int(&so->so_options); 103101f8ce83SMark Johnston if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0) 103268e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 103396871af0SGleb Smirnoff if (sin == NULL) { 10347c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 10357c2f3cb9SJamie Gritton return (error); 10367c2f3cb9SJamie Gritton } else { 1037f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 1038f161d294SMark Johnston ("%s: invalid family for address %p", __func__, sin)); 1039f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 1040f161d294SMark Johnston ("%s: invalid length for address %p", __func__, sin)); 1041f161d294SMark Johnston 1042b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 1043b89e82ddSJamie Gritton if (error) 1044b89e82ddSJamie Gritton return (error); 10454b932371SIan Dowse if (sin->sin_port != *lportp) { 10464b932371SIan Dowse /* Don't allow the port to change. */ 10474b932371SIan Dowse if (*lportp != 0) 10484b932371SIan Dowse return (EINVAL); 1049df8bae1dSRodney W. Grimes lport = sin->sin_port; 10504b932371SIan Dowse } 105145a77bf2SMark Johnston laddr = sin->sin_addr; 105245a77bf2SMark Johnston 1053bbd0084bSMark Johnston fib = (flags & INPBIND_FIB) != 0 ? inp->inp_inc.inc_fibnum : 1054bbd0084bSMark Johnston RT_ALL_FIBS; 1055bbd0084bSMark Johnston 105601f8ce83SMark Johnston /* See if this address/port combo is available. */ 1057bbd0084bSMark Johnston error = in_pcbbind_avail(inp, laddr, lport, fib, sooptions, 105845a77bf2SMark Johnston lookupflags, cred); 105901f8ce83SMark Johnston if (error != 0) 106001f8ce83SMark Johnston return (error); 1061df8bae1dSRodney W. Grimes } 10624b932371SIan Dowse if (*lportp != 0) 10634b932371SIan Dowse lport = *lportp; 106433b3ac06SPeter Wemm if (lport == 0) { 10654616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 1066efc76f72SBjoern A. Zeeb if (error != 0) 1067efc76f72SBjoern A. Zeeb return (error); 106833b3ac06SPeter Wemm } 10694b932371SIan Dowse *laddrp = laddr.s_addr; 10704b932371SIan Dowse *lportp = lport; 1071bbd0084bSMark Johnston if ((flags & INPBIND_FIB) != 0) 1072bbd0084bSMark Johnston inp->inp_flags |= INP_BOUNDFIB; 1073df8bae1dSRodney W. Grimes return (0); 1074df8bae1dSRodney W. Grimes } 1075df8bae1dSRodney W. Grimes 1076999f1343SGarrett Wollman /* 10775200e00eSIan Dowse * Connect from a socket to a specified address. 10785200e00eSIan Dowse * Both address and port must be specified in argument sin. 10795200e00eSIan Dowse * If don't have a local address for this socket yet, 10805200e00eSIan Dowse * then pick one. 1081999f1343SGarrett Wollman */ 1082999f1343SGarrett Wollman int 10830b4539eeSGleb Smirnoff in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred) 1084999f1343SGarrett Wollman { 10852af953b1SGleb Smirnoff struct in_addr laddr, faddr; 10862af953b1SGleb Smirnoff u_short lport; 10872af953b1SGleb Smirnoff int error; 10882af953b1SGleb Smirnoff bool anonport; 1089df8bae1dSRodney W. Grimes 10908501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1091fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1092fdb987beSMark Johnston KASSERT(in_nullhost(inp->inp_faddr), 1093fdb987beSMark Johnston ("%s: inp is already connected", __func__)); 10942af953b1SGleb Smirnoff KASSERT(sin->sin_family == AF_INET, 10952af953b1SGleb Smirnoff ("%s: invalid address family for %p", __func__, sin)); 10962af953b1SGleb Smirnoff KASSERT(sin->sin_len == sizeof(*sin), 10972af953b1SGleb Smirnoff ("%s: invalid address length for %p", __func__, sin)); 109827f74fd0SRobert Watson 10992af953b1SGleb Smirnoff if (sin->sin_port == 0) 11002af953b1SGleb Smirnoff return (EADDRNOTAVAIL); 11012af953b1SGleb Smirnoff 11022af953b1SGleb Smirnoff anonport = (inp->inp_lport == 0); 11032af953b1SGleb Smirnoff 11042af953b1SGleb Smirnoff if (__predict_false(in_broadcast(sin->sin_addr))) { 11052af953b1SGleb Smirnoff if (!V_connect_inaddr_wild || CK_STAILQ_EMPTY(&V_in_ifaddrhead)) 11062af953b1SGleb Smirnoff return (ENETUNREACH); 11072af953b1SGleb Smirnoff /* 11082af953b1SGleb Smirnoff * If the destination address is INADDR_ANY, use the primary 11092af953b1SGleb Smirnoff * local address. If the supplied address is INADDR_BROADCAST, 11102af953b1SGleb Smirnoff * and the primary interface supports broadcast, choose the 11112af953b1SGleb Smirnoff * broadcast address for that interface. 11122af953b1SGleb Smirnoff */ 11132af953b1SGleb Smirnoff if (in_nullhost(sin->sin_addr)) { 11142af953b1SGleb Smirnoff faddr = 11152af953b1SGleb Smirnoff IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 11162af953b1SGleb Smirnoff if ((error = prison_get_ip4(cred, &faddr)) != 0) 11172af953b1SGleb Smirnoff return (error); 11182af953b1SGleb Smirnoff } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) { 11192af953b1SGleb Smirnoff if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags 11202af953b1SGleb Smirnoff & IFF_BROADCAST) 11212af953b1SGleb Smirnoff faddr = satosin(&CK_STAILQ_FIRST( 11222af953b1SGleb Smirnoff &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 11232af953b1SGleb Smirnoff else 11242af953b1SGleb Smirnoff faddr = sin->sin_addr; 11252af953b1SGleb Smirnoff } 11262af953b1SGleb Smirnoff } else 11272af953b1SGleb Smirnoff faddr = sin->sin_addr; 11282af953b1SGleb Smirnoff 11292af953b1SGleb Smirnoff if (in_nullhost(inp->inp_laddr)) { 11302af953b1SGleb Smirnoff error = in_pcbladdr(inp, &faddr, &laddr, cred); 11312af953b1SGleb Smirnoff /* 11322af953b1SGleb Smirnoff * If the destination address is multicast and an outgoing 11332af953b1SGleb Smirnoff * interface has been set as a multicast option, prefer the 11342af953b1SGleb Smirnoff * address of that interface as our source address. 11352af953b1SGleb Smirnoff */ 11362af953b1SGleb Smirnoff if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 11372af953b1SGleb Smirnoff inp->inp_moptions != NULL && 11382af953b1SGleb Smirnoff inp->inp_moptions->imo_multicast_ifp != NULL) { 11392af953b1SGleb Smirnoff struct ifnet *ifp = 11402af953b1SGleb Smirnoff inp->inp_moptions->imo_multicast_ifp; 11412af953b1SGleb Smirnoff struct in_ifaddr *ia; 11422af953b1SGleb Smirnoff 11432af953b1SGleb Smirnoff CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 11442af953b1SGleb Smirnoff if (ia->ia_ifp == ifp && 11452af953b1SGleb Smirnoff prison_check_ip4(cred, 11462af953b1SGleb Smirnoff &ia->ia_addr.sin_addr) == 0) 11472af953b1SGleb Smirnoff break; 11482af953b1SGleb Smirnoff } 11492af953b1SGleb Smirnoff if (ia == NULL) 11502af953b1SGleb Smirnoff return (EADDRNOTAVAIL); 11512af953b1SGleb Smirnoff laddr = ia->ia_addr.sin_addr; 11522af953b1SGleb Smirnoff error = 0; 11532af953b1SGleb Smirnoff } 11545200e00eSIan Dowse if (error) 11555200e00eSIan Dowse return (error); 11562af953b1SGleb Smirnoff } else 11572af953b1SGleb Smirnoff laddr = inp->inp_laddr; 11585200e00eSIan Dowse 11592af953b1SGleb Smirnoff if (anonport) { 11602af953b1SGleb Smirnoff struct sockaddr_in lsin = { 11612af953b1SGleb Smirnoff .sin_family = AF_INET, 11622af953b1SGleb Smirnoff .sin_addr = laddr, 11632af953b1SGleb Smirnoff }; 11642af953b1SGleb Smirnoff struct sockaddr_in fsin = { 11652af953b1SGleb Smirnoff .sin_family = AF_INET, 11662af953b1SGleb Smirnoff .sin_addr = faddr, 11672af953b1SGleb Smirnoff }; 11682af953b1SGleb Smirnoff 11692af953b1SGleb Smirnoff error = in_pcb_lport_dest(inp, (struct sockaddr *)&lsin, 11702af953b1SGleb Smirnoff &lport, (struct sockaddr *)&fsin, sin->sin_port, cred, 11712af953b1SGleb Smirnoff INPLOOKUP_WILDCARD); 11722af953b1SGleb Smirnoff if (error) 11732af953b1SGleb Smirnoff return (error); 11742af953b1SGleb Smirnoff } else if (in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 11752af953b1SGleb Smirnoff sin->sin_port, laddr, inp->inp_lport, 0, M_NODOM, RT_ALL_FIBS) != 11762af953b1SGleb Smirnoff NULL) 11772af953b1SGleb Smirnoff return (EADDRINUSE); 11782af953b1SGleb Smirnoff else 11792af953b1SGleb Smirnoff lport = inp->inp_lport; 11802af953b1SGleb Smirnoff 1181*452187b6SGleb Smirnoff MPASS(!in_nullhost(inp->inp_laddr) || inp->inp_lport != 0 || 1182*452187b6SGleb Smirnoff !(inp->inp_flags & INP_INHASHLIST)); 1183*452187b6SGleb Smirnoff 11842af953b1SGleb Smirnoff inp->inp_faddr = faddr; 11852af953b1SGleb Smirnoff inp->inp_fport = sin->sin_port; 1186*452187b6SGleb Smirnoff inp->inp_laddr = laddr; 1187*452187b6SGleb Smirnoff inp->inp_lport = lport; 1188fdb987beSMark Johnston 1189*452187b6SGleb Smirnoff if ((inp->inp_flags & INP_INHASHLIST) == 0) { 1190*452187b6SGleb Smirnoff error = in_pcbinshash(inp); 1191*452187b6SGleb Smirnoff MPASS(error == 0); 1192*452187b6SGleb Smirnoff } else 1193db0ac6deSCy Schubert in_pcbrehash(inp); 119424e5c2eeSGleb Smirnoff #ifdef ROUTE_MPATH 119524e5c2eeSGleb Smirnoff if (CALC_FLOWID_OUTBOUND) { 119624e5c2eeSGleb Smirnoff uint32_t hash_val, hash_type; 11972cb64cb2SGeorge V. Neville-Neil 119824e5c2eeSGleb Smirnoff hash_val = fib4_calc_software_hash(inp->inp_laddr, 11992af953b1SGleb Smirnoff inp->inp_faddr, 0, sin->sin_port, 120024e5c2eeSGleb Smirnoff inp->inp_socket->so_proto->pr_protocol, &hash_type); 120124e5c2eeSGleb Smirnoff 120224e5c2eeSGleb Smirnoff inp->inp_flowid = hash_val; 120324e5c2eeSGleb Smirnoff inp->inp_flowtype = hash_type; 120424e5c2eeSGleb Smirnoff } 120524e5c2eeSGleb Smirnoff #endif 12065200e00eSIan Dowse if (anonport) 12075200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 12085200e00eSIan Dowse return (0); 12095200e00eSIan Dowse } 12105200e00eSIan Dowse 12115200e00eSIan Dowse /* 12120895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 12130895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 12140895aec3SBjoern A. Zeeb */ 1215ae190832SSteven Hartland int 1216bafe022bSGleb Smirnoff in_pcbladdr(const struct inpcb *inp, struct in_addr *faddr, 1217bafe022bSGleb Smirnoff struct in_addr *laddr, struct ucred *cred) 12180895aec3SBjoern A. Zeeb { 12190895aec3SBjoern A. Zeeb struct ifaddr *ifa; 12200895aec3SBjoern A. Zeeb struct sockaddr *sa; 12219ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 12229ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 12230895aec3SBjoern A. Zeeb int error; 12240895aec3SBjoern A. Zeeb 1225c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1226413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1227ac1750ddSMark Johnston 1228592bcae8SBjoern A. Zeeb /* 1229592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1230592bcae8SBjoern A. Zeeb * if requested. 1231592bcae8SBjoern A. Zeeb */ 1232ac1750ddSMark Johnston if (!prison_saddrsel_ip4(cred, laddr)) 1233592bcae8SBjoern A. Zeeb return (0); 1234592bcae8SBjoern A. Zeeb 12350895aec3SBjoern A. Zeeb error = 0; 12360895aec3SBjoern A. Zeeb 12379ac7c6cfSAlexander V. Chernikov nh = NULL; 12389ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 12399ac7c6cfSAlexander V. Chernikov sin = &dst; 12400895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 12410895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 12420895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 12430895aec3SBjoern A. Zeeb 12440895aec3SBjoern A. Zeeb /* 12450895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 12460895aec3SBjoern A. Zeeb * else punt. 12470895aec3SBjoern A. Zeeb * 12480895aec3SBjoern A. Zeeb * Find out route to destination. 12490895aec3SBjoern A. Zeeb */ 12500895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 12519ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 12529ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 12530895aec3SBjoern A. Zeeb 12540895aec3SBjoern A. Zeeb /* 12550895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 12560895aec3SBjoern A. Zeeb * the outgoing interface. 12570895aec3SBjoern A. Zeeb * 12580895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 12590895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 12600895aec3SBjoern A. Zeeb * the source address from. 12610895aec3SBjoern A. Zeeb */ 12629ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 12638c0fec80SRobert Watson struct in_ifaddr *ia; 12640895aec3SBjoern A. Zeeb struct ifnet *ifp; 12650895aec3SBjoern A. Zeeb 12664f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 126758a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12684f6c66ccSMatt Macy if (ia == NULL) { 12694f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 127058a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12714f6c66ccSMatt Macy } 12720895aec3SBjoern A. Zeeb if (ia == NULL) { 12730895aec3SBjoern A. Zeeb error = ENETUNREACH; 12740895aec3SBjoern A. Zeeb goto done; 12750895aec3SBjoern A. Zeeb } 12760895aec3SBjoern A. Zeeb 1277ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 12780895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12790895aec3SBjoern A. Zeeb goto done; 12800895aec3SBjoern A. Zeeb } 12810895aec3SBjoern A. Zeeb 12820895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12830895aec3SBjoern A. Zeeb ia = NULL; 1284d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12850895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12860895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12870895aec3SBjoern A. Zeeb continue; 12880895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1289b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12900895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12910895aec3SBjoern A. Zeeb break; 12920895aec3SBjoern A. Zeeb } 12930895aec3SBjoern A. Zeeb } 12940895aec3SBjoern A. Zeeb if (ia != NULL) { 12950895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12960895aec3SBjoern A. Zeeb goto done; 12970895aec3SBjoern A. Zeeb } 12980895aec3SBjoern A. Zeeb 12990895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1300b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 13010895aec3SBjoern A. Zeeb goto done; 13020895aec3SBjoern A. Zeeb } 13030895aec3SBjoern A. Zeeb 13040895aec3SBjoern A. Zeeb /* 13050895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 13060895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 13070895aec3SBjoern A. Zeeb * In case of jails do those three steps: 13080895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 13090895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 13100895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 13110895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 13120895aec3SBjoern A. Zeeb */ 13139ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 13148c0fec80SRobert Watson struct in_ifaddr *ia; 13159317b04eSRobert Watson struct ifnet *ifp; 13160895aec3SBjoern A. Zeeb 13170895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 1318ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 13199ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 13200895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13210895aec3SBjoern A. Zeeb goto done; 13220895aec3SBjoern A. Zeeb } 13230895aec3SBjoern A. Zeeb 13240895aec3SBjoern A. Zeeb /* Jailed. */ 13250895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 13269ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1327b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 13289ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 13290895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13300895aec3SBjoern A. Zeeb goto done; 13310895aec3SBjoern A. Zeeb } 13320895aec3SBjoern A. Zeeb 13330895aec3SBjoern A. Zeeb /* 13340895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 13350895aec3SBjoern A. Zeeb * belonging to this jail. 13360895aec3SBjoern A. Zeeb */ 13378c0fec80SRobert Watson ia = NULL; 13389ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1339d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 13400895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 13410895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 13420895aec3SBjoern A. Zeeb continue; 13430895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1344b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 13450895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 13460895aec3SBjoern A. Zeeb break; 13470895aec3SBjoern A. Zeeb } 13480895aec3SBjoern A. Zeeb } 13490895aec3SBjoern A. Zeeb if (ia != NULL) { 13500895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13510895aec3SBjoern A. Zeeb goto done; 13520895aec3SBjoern A. Zeeb } 13530895aec3SBjoern A. Zeeb 13540895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1355b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 13560895aec3SBjoern A. Zeeb goto done; 13570895aec3SBjoern A. Zeeb } 13580895aec3SBjoern A. Zeeb 13590895aec3SBjoern A. Zeeb /* 13600895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 13610895aec3SBjoern A. Zeeb * to ourselves is here. 13620895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 13630895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 13640895aec3SBjoern A. Zeeb * a loopback interface. 13650895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 13660895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 13670895aec3SBjoern A. Zeeb */ 13689ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 13698c0fec80SRobert Watson struct in_ifaddr *ia; 13700895aec3SBjoern A. Zeeb 13719ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 137258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 13730895aec3SBjoern A. Zeeb if (ia == NULL) 13749ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 137558a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1376f0bb05fcSQing Li if (ia == NULL) 13779ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 13780895aec3SBjoern A. Zeeb 1379ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 13800895aec3SBjoern A. Zeeb if (ia == NULL) { 13810895aec3SBjoern A. Zeeb error = ENETUNREACH; 13820895aec3SBjoern A. Zeeb goto done; 13830895aec3SBjoern A. Zeeb } 13840895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13850895aec3SBjoern A. Zeeb goto done; 13860895aec3SBjoern A. Zeeb } 13870895aec3SBjoern A. Zeeb 13880895aec3SBjoern A. Zeeb /* Jailed. */ 13890895aec3SBjoern A. Zeeb if (ia != NULL) { 13900895aec3SBjoern A. Zeeb struct ifnet *ifp; 13910895aec3SBjoern A. Zeeb 13920895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 13930895aec3SBjoern A. Zeeb ia = NULL; 1394d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 13950895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 13960895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 13970895aec3SBjoern A. Zeeb continue; 13980895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1399b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1400b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 14010895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 14020895aec3SBjoern A. Zeeb break; 14030895aec3SBjoern A. Zeeb } 14040895aec3SBjoern A. Zeeb } 14050895aec3SBjoern A. Zeeb if (ia != NULL) { 14060895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 14070895aec3SBjoern A. Zeeb goto done; 14080895aec3SBjoern A. Zeeb } 14090895aec3SBjoern A. Zeeb } 14100895aec3SBjoern A. Zeeb 14110895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1412b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 14130895aec3SBjoern A. Zeeb goto done; 14140895aec3SBjoern A. Zeeb } 14150895aec3SBjoern A. Zeeb 14160895aec3SBjoern A. Zeeb done: 1417713264f6SMark Johnston if (error == 0 && laddr->s_addr == INADDR_ANY) 1418713264f6SMark Johnston return (EHOSTUNREACH); 14190895aec3SBjoern A. Zeeb return (error); 14200895aec3SBjoern A. Zeeb } 14210895aec3SBjoern A. Zeeb 142226f9a767SRodney W. Grimes void 1423136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1424df8bae1dSRodney W. Grimes { 14256b348152SRobert Watson 14268501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1427fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1428fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 1429fdb987beSMark Johnston ("%s: inp %p was already disconnected", __func__, inp)); 1430df8bae1dSRodney W. Grimes 1431fdb987beSMark Johnston in_pcbremhash_locked(inp); 1432fdb987beSMark Johnston 1433fdb987beSMark Johnston /* See the comment in in_pcbinshash(). */ 1434fdb987beSMark Johnston inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr); 14352589ec0fSMark Johnston inp->inp_laddr.s_addr = INADDR_ANY; 1436df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1437df8bae1dSRodney W. Grimes inp->inp_fport = 0; 1438df8bae1dSRodney W. Grimes } 143983e521ecSBjoern A. Zeeb #endif /* INET */ 1440df8bae1dSRodney W. Grimes 14417cbb6b6eSMark Johnston void 14427cbb6b6eSMark Johnston in_pcblisten(struct inpcb *inp) 14437cbb6b6eSMark Johnston { 14447cbb6b6eSMark Johnston struct inpcblbgroup *grp; 14457cbb6b6eSMark Johnston 14467cbb6b6eSMark Johnston INP_WLOCK_ASSERT(inp); 14477cbb6b6eSMark Johnston 14487cbb6b6eSMark Johnston if ((inp->inp_flags & INP_INLBGROUP) != 0) { 14497cbb6b6eSMark Johnston struct inpcbinfo *pcbinfo; 14507cbb6b6eSMark Johnston 14517cbb6b6eSMark Johnston pcbinfo = inp->inp_pcbinfo; 14527cbb6b6eSMark Johnston INP_HASH_WLOCK(pcbinfo); 14537cbb6b6eSMark Johnston grp = in_pcblbgroup_find(inp); 14547cbb6b6eSMark Johnston LIST_REMOVE(inp, inp_lbgroup_list); 14558b3d2c19SMark Johnston grp->il_pendcnt--; 14567cbb6b6eSMark Johnston in_pcblbgroup_insert(grp, inp); 14577cbb6b6eSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 14587cbb6b6eSMark Johnston } 14597cbb6b6eSMark Johnston } 14607cbb6b6eSMark Johnston 14614c7c478dSRobert Watson /* 1462db0ac6deSCy Schubert * inpcb hash lookups are protected by SMR section. 1463db0ac6deSCy Schubert * 1464db0ac6deSCy Schubert * Once desired pcb has been found, switching from SMR section to a pcb 1465db0ac6deSCy Schubert * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1466db0ac6deSCy Schubert * here because SMR is a critical section. 1467db0ac6deSCy Schubert * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1468db0ac6deSCy Schubert */ 14697b92493aSMark Johnston void 1470db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1471db0ac6deSCy Schubert { 1472db0ac6deSCy Schubert 1473db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1474db0ac6deSCy Schubert rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1475db0ac6deSCy Schubert } 1476db0ac6deSCy Schubert 14777b92493aSMark Johnston void 1478db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1479db0ac6deSCy Schubert { 1480db0ac6deSCy Schubert 1481db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1482db0ac6deSCy Schubert rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1483db0ac6deSCy Schubert } 1484db0ac6deSCy Schubert 14857b92493aSMark Johnston int 1486db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1487db0ac6deSCy Schubert { 1488db0ac6deSCy Schubert 1489db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1490db0ac6deSCy Schubert rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1491db0ac6deSCy Schubert } 1492db0ac6deSCy Schubert 1493db0ac6deSCy Schubert static inline bool 1494f567d55fSGleb Smirnoff _inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock, const int ignflags) 1495db0ac6deSCy Schubert { 1496db0ac6deSCy Schubert 1497db0ac6deSCy Schubert MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1498db0ac6deSCy Schubert SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1499db0ac6deSCy Schubert 1500db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1501f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1502db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1503db0ac6deSCy Schubert inp_unlock(inp, lock); 1504db0ac6deSCy Schubert return (false); 1505db0ac6deSCy Schubert } 1506db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1507db0ac6deSCy Schubert return (true); 1508db0ac6deSCy Schubert } 1509db0ac6deSCy Schubert 1510db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1511db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1512db0ac6deSCy Schubert inp_lock(inp, lock); 1513db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) 1514db0ac6deSCy Schubert return (false); 1515db0ac6deSCy Schubert /* 1516db0ac6deSCy Schubert * inp acquired through refcount & lock for sure didn't went 1517db0ac6deSCy Schubert * through uma_zfree(). However, it may have already went 1518db0ac6deSCy Schubert * through in_pcbfree() and has another reference, that 1519db0ac6deSCy Schubert * prevented its release by our in_pcbrele(). 1520db0ac6deSCy Schubert */ 1521f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1522db0ac6deSCy Schubert inp_unlock(inp, lock); 1523db0ac6deSCy Schubert return (false); 1524db0ac6deSCy Schubert } 1525db0ac6deSCy Schubert return (true); 1526db0ac6deSCy Schubert } else { 1527db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1528db0ac6deSCy Schubert return (false); 1529db0ac6deSCy Schubert } 1530db0ac6deSCy Schubert } 1531db0ac6deSCy Schubert 1532f567d55fSGleb Smirnoff bool 1533f567d55fSGleb Smirnoff inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1534f567d55fSGleb Smirnoff { 1535f567d55fSGleb Smirnoff 1536f567d55fSGleb Smirnoff /* 1537f567d55fSGleb Smirnoff * in_pcblookup() family of functions ignore not only freed entries, 1538f567d55fSGleb Smirnoff * that may be found due to lockless access to the hash, but dropped 1539f567d55fSGleb Smirnoff * entries, too. 1540f567d55fSGleb Smirnoff */ 1541f567d55fSGleb Smirnoff return (_inp_smr_lock(inp, lock, INP_FREED | INP_DROPPED)); 1542f567d55fSGleb Smirnoff } 1543f567d55fSGleb Smirnoff 1544db0ac6deSCy Schubert /* 1545db0ac6deSCy Schubert * inp_next() - inpcb hash/list traversal iterator 1546db0ac6deSCy Schubert * 1547db0ac6deSCy Schubert * Requires initialized struct inpcb_iterator for context. 1548db0ac6deSCy Schubert * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1549db0ac6deSCy Schubert * 1550db0ac6deSCy Schubert * - Iterator can have either write-lock or read-lock semantics, that can not 1551db0ac6deSCy Schubert * be changed later. 1552db0ac6deSCy Schubert * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1553db0ac6deSCy Schubert * a single hash slot. Note: only rip_input() does the latter. 1554db0ac6deSCy Schubert * - Iterator may have optional bool matching function. The matching function 1555db0ac6deSCy Schubert * will be executed for each inpcb in the SMR context, so it can not acquire 1556db0ac6deSCy Schubert * locks and can safely access only immutable fields of inpcb. 1557db0ac6deSCy Schubert * 1558db0ac6deSCy Schubert * A fresh initialized iterator has NULL inpcb in its context and that 1559db0ac6deSCy Schubert * means that inp_next() call would return the very first inpcb on the list 1560db0ac6deSCy Schubert * locked with desired semantic. In all following calls the context pointer 1561db0ac6deSCy Schubert * shall hold the current inpcb pointer. The KPI user is not supposed to 1562db0ac6deSCy Schubert * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1563db0ac6deSCy Schubert * and write NULL to its context. After end of traversal an iterator can be 1564db0ac6deSCy Schubert * reused. 1565db0ac6deSCy Schubert * 1566db0ac6deSCy Schubert * List traversals have the following features/constraints: 1567db0ac6deSCy Schubert * - New entries won't be seen, as they are always added to the head of a list. 1568db0ac6deSCy Schubert * - Removed entries won't stop traversal as long as they are not added to 1569db0ac6deSCy Schubert * a different list. This is violated by in_pcbrehash(). 1570db0ac6deSCy Schubert */ 1571db0ac6deSCy Schubert #define II_LIST_FIRST(ipi, hash) \ 1572db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1573db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1574fdb987beSMark Johnston CK_LIST_FIRST(&(ipi)->ipi_hash_exact[(hash)])) 1575db0ac6deSCy Schubert #define II_LIST_NEXT(inp, hash) \ 1576db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1577db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_list) : \ 1578fdb987beSMark Johnston CK_LIST_NEXT((inp), inp_hash_exact)) 1579db0ac6deSCy Schubert #define II_LOCK_ASSERT(inp, lock) \ 1580db0ac6deSCy Schubert rw_assert(&(inp)->inp_lock, \ 1581db0ac6deSCy Schubert (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1582db0ac6deSCy Schubert struct inpcb * 1583db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii) 1584db0ac6deSCy Schubert { 1585db0ac6deSCy Schubert const struct inpcbinfo *ipi = ii->ipi; 1586db0ac6deSCy Schubert inp_match_t *match = ii->match; 1587db0ac6deSCy Schubert void *ctx = ii->ctx; 1588db0ac6deSCy Schubert inp_lookup_t lock = ii->lock; 1589db0ac6deSCy Schubert int hash = ii->hash; 1590db0ac6deSCy Schubert struct inpcb *inp; 1591db0ac6deSCy Schubert 1592db0ac6deSCy Schubert if (ii->inp == NULL) { /* First call. */ 1593db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1594db0ac6deSCy Schubert /* This is unrolled CK_LIST_FOREACH(). */ 1595db0ac6deSCy Schubert for (inp = II_LIST_FIRST(ipi, hash); 1596db0ac6deSCy Schubert inp != NULL; 1597db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash)) { 1598db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1599db0ac6deSCy Schubert continue; 1600f567d55fSGleb Smirnoff if (__predict_true(_inp_smr_lock(inp, lock, INP_FREED))) 1601db0ac6deSCy Schubert break; 1602db0ac6deSCy Schubert else { 1603db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1604db0ac6deSCy Schubert MPASS(inp != II_LIST_FIRST(ipi, hash)); 1605db0ac6deSCy Schubert inp = II_LIST_FIRST(ipi, hash); 1606430df2abSMichael Tuexen if (inp == NULL) 1607430df2abSMichael Tuexen break; 1608db0ac6deSCy Schubert } 1609db0ac6deSCy Schubert } 1610db0ac6deSCy Schubert 1611db0ac6deSCy Schubert if (inp == NULL) 1612db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1613db0ac6deSCy Schubert else 1614db0ac6deSCy Schubert ii->inp = inp; 1615db0ac6deSCy Schubert 1616db0ac6deSCy Schubert return (inp); 1617db0ac6deSCy Schubert } 1618db0ac6deSCy Schubert 1619db0ac6deSCy Schubert /* Not a first call. */ 1620db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1621db0ac6deSCy Schubert restart: 1622db0ac6deSCy Schubert inp = ii->inp; 1623db0ac6deSCy Schubert II_LOCK_ASSERT(inp, lock); 1624db0ac6deSCy Schubert next: 1625db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash); 1626db0ac6deSCy Schubert if (inp == NULL) { 1627db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1628db0ac6deSCy Schubert goto found; 1629db0ac6deSCy Schubert } 1630db0ac6deSCy Schubert 1631db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1632db0ac6deSCy Schubert goto next; 1633db0ac6deSCy Schubert 1634db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1635db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1636db0ac6deSCy Schubert /* 1637db0ac6deSCy Schubert * Entries are never inserted in middle of a list, thus 1638db0ac6deSCy Schubert * as long as we are in SMR, we can continue traversal. 1639db0ac6deSCy Schubert * Jump to 'restart' should yield in the same result, 1640db0ac6deSCy Schubert * but could produce unnecessary looping. Could this 1641db0ac6deSCy Schubert * looping be unbound? 1642db0ac6deSCy Schubert */ 1643db0ac6deSCy Schubert inp_unlock(inp, lock); 1644db0ac6deSCy Schubert goto next; 1645db0ac6deSCy Schubert } else { 1646db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1647db0ac6deSCy Schubert goto found; 1648db0ac6deSCy Schubert } 1649db0ac6deSCy Schubert } 1650db0ac6deSCy Schubert 1651db0ac6deSCy Schubert /* 1652db0ac6deSCy Schubert * Can't obtain lock immediately, thus going hard. Once we exit the 1653db0ac6deSCy Schubert * SMR section we can no longer jump to 'next', and our only stable 1654db0ac6deSCy Schubert * anchoring point is ii->inp, which we keep locked for this case, so 1655db0ac6deSCy Schubert * we jump to 'restart'. 1656db0ac6deSCy Schubert */ 1657db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1658db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1659db0ac6deSCy Schubert inp_lock(inp, lock); 1660db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) { 1661db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1662db0ac6deSCy Schubert goto restart; 1663db0ac6deSCy Schubert } 1664db0ac6deSCy Schubert /* 1665db0ac6deSCy Schubert * See comment in inp_smr_lock(). 1666db0ac6deSCy Schubert */ 1667db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1668db0ac6deSCy Schubert inp_unlock(inp, lock); 1669db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1670db0ac6deSCy Schubert goto restart; 1671db0ac6deSCy Schubert } 1672db0ac6deSCy Schubert } else 1673db0ac6deSCy Schubert goto next; 1674db0ac6deSCy Schubert 1675db0ac6deSCy Schubert found: 1676db0ac6deSCy Schubert inp_unlock(ii->inp, lock); 1677db0ac6deSCy Schubert ii->inp = inp; 1678db0ac6deSCy Schubert 1679db0ac6deSCy Schubert return (ii->inp); 1680db0ac6deSCy Schubert } 1681db0ac6deSCy Schubert 1682db0ac6deSCy Schubert /* 168379bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 1684db0ac6deSCy Schubert * stability of an inpcb pointer despite the inpcb lock being released or 1685db0ac6deSCy Schubert * SMR section exited. 168679bdc6e5SRobert Watson * 1687db0ac6deSCy Schubert * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1688c0a211c5SRobert Watson */ 168979bdc6e5SRobert Watson void 169079bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 16914c7c478dSRobert Watson { 1692db0ac6deSCy Schubert u_int old __diagused; 1693df8bae1dSRodney W. Grimes 1694db0ac6deSCy Schubert old = refcount_acquire(&inp->inp_refcount); 1695db0ac6deSCy Schubert KASSERT(old > 0, ("%s: refcount 0", __func__)); 169679bdc6e5SRobert Watson } 169779bdc6e5SRobert Watson 169879bdc6e5SRobert Watson /* 1699db0ac6deSCy Schubert * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1700db0ac6deSCy Schubert * freeing the pcb, if the reference was very last. 170179bdc6e5SRobert Watson */ 1702db0ac6deSCy Schubert bool 170379bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 170479bdc6e5SRobert Watson { 170579bdc6e5SRobert Watson 170679bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 170779bdc6e5SRobert Watson 1708c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1709db0ac6deSCy Schubert return (false); 1710db0ac6deSCy Schubert 1711db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1712db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 17135fd1a67eSMark Johnston crfree(inp->inp_cred); 17145fd1a67eSMark Johnston #ifdef INVARIANTS 17155fd1a67eSMark Johnston inp->inp_cred = NULL; 17165fd1a67eSMark Johnston #endif 1717df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1718db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1719db0ac6deSCy Schubert return (true); 1720df4e91d3SGleb Smirnoff } 172179bdc6e5SRobert Watson 1722db0ac6deSCy Schubert bool 172379bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 172479bdc6e5SRobert Watson { 172579bdc6e5SRobert Watson 172679bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 172779bdc6e5SRobert Watson 1728c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1729db0ac6deSCy Schubert return (false); 1730db0ac6deSCy Schubert 1731db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1732db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 17335fd1a67eSMark Johnston crfree(inp->inp_cred); 17345fd1a67eSMark Johnston #ifdef INVARIANTS 17355fd1a67eSMark Johnston inp->inp_cred = NULL; 17365fd1a67eSMark Johnston #endif 1737edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1738db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1739db0ac6deSCy Schubert return (true); 1740addf2b20SMatt Macy } 1741addf2b20SMatt Macy 17427b92493aSMark Johnston bool 17437b92493aSMark Johnston in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 17447b92493aSMark Johnston { 17457b92493aSMark Johnston 17467b92493aSMark Johnston return (lock == INPLOOKUP_RLOCKPCB ? 17477b92493aSMark Johnston in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 17487b92493aSMark Johnston } 17497b92493aSMark Johnston 175079bdc6e5SRobert Watson /* 175179bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 175279bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 175379bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 175479bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 1755db0ac6deSCy Schubert * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1756db0ac6deSCy Schubert * Almost all work, including removal from global lists, is done in this 1757db0ac6deSCy Schubert * context, where the pcbinfo lock is held. 175879bdc6e5SRobert Watson */ 175979bdc6e5SRobert Watson void 176079bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 176179bdc6e5SRobert Watson { 1762f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1763db0ac6deSCy Schubert #ifdef INET 1764db0ac6deSCy Schubert struct ip_moptions *imo; 1765db0ac6deSCy Schubert #endif 1766db0ac6deSCy Schubert #ifdef INET6 1767db0ac6deSCy Schubert struct ip6_moptions *im6o; 1768db0ac6deSCy Schubert #endif 176945a48d07SJonathan T. Looney 177079bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1771a13039e2SGleb Smirnoff KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1772db0ac6deSCy Schubert KASSERT((inp->inp_flags & INP_FREED) == 0, 1773db0ac6deSCy Schubert ("%s: called twice for pcb %p", __func__, inp)); 1774db0ac6deSCy Schubert 1775a13039e2SGleb Smirnoff /* 1776a13039e2SGleb Smirnoff * in_pcblookup_local() and in6_pcblookup_local() may return an inpcb 1777a13039e2SGleb Smirnoff * from the hash without acquiring inpcb lock, they rely on the hash 1778a13039e2SGleb Smirnoff * lock, thus in_pcbremhash() should be the first action. 1779a13039e2SGleb Smirnoff */ 1780a13039e2SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 1781a13039e2SGleb Smirnoff in_pcbremhash(inp); 1782db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 1783db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1784db0ac6deSCy Schubert pcbinfo->ipi_count--; 1785db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_list); 1786db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 1787db0ac6deSCy Schubert 1788a13039e2SGleb Smirnoff #ifdef RATELIMIT 1789a13039e2SGleb Smirnoff if (inp->inp_snd_tag != NULL) 1790a13039e2SGleb Smirnoff in_pcbdetach_txrtlmt(inp); 1791a13039e2SGleb Smirnoff #endif 1792a13039e2SGleb Smirnoff inp->inp_flags |= INP_FREED; 1793a13039e2SGleb Smirnoff inp->inp_socket->so_pcb = NULL; 1794a13039e2SGleb Smirnoff inp->inp_socket = NULL; 1795db0ac6deSCy Schubert 1796fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1797db0ac6deSCy Schubert #ifdef MAC 1798db0ac6deSCy Schubert mac_inpcb_destroy(inp); 1799db0ac6deSCy Schubert #endif 1800db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1801db0ac6deSCy Schubert if (inp->inp_sp != NULL) 1802db0ac6deSCy Schubert ipsec_delete_pcbpolicy(inp); 1803db0ac6deSCy Schubert #endif 1804db0ac6deSCy Schubert #ifdef INET 1805db0ac6deSCy Schubert if (inp->inp_options) 1806db0ac6deSCy Schubert (void)m_free(inp->inp_options); 18074a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->inp_options); 1808db0ac6deSCy Schubert imo = inp->inp_moptions; 18094a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->inp_moptions); 1810db0ac6deSCy Schubert #endif 1811db0ac6deSCy Schubert #ifdef INET6 1812db0ac6deSCy Schubert if (inp->inp_vflag & INP_IPV6PROTO) { 1813db0ac6deSCy Schubert ip6_freepcbopts(inp->in6p_outputopts); 18144a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->in6p_outputopts); 1815db0ac6deSCy Schubert im6o = inp->in6p_moptions; 18164a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->in6p_moptions); 1817db0ac6deSCy Schubert } else 1818db0ac6deSCy Schubert im6o = NULL; 1819db0ac6deSCy Schubert #endif 1820db0ac6deSCy Schubert 1821db0ac6deSCy Schubert if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1822cb6bb230SMatt Macy INP_WUNLOCK(inp); 1823db0ac6deSCy Schubert } 1824db0ac6deSCy Schubert #ifdef INET6 1825db0ac6deSCy Schubert ip6_freemoptions(im6o); 1826db0ac6deSCy Schubert #endif 1827db0ac6deSCy Schubert #ifdef INET 1828db0ac6deSCy Schubert inp_freemoptions(imo); 1829db0ac6deSCy Schubert #endif 1830eb93b99dSGleb Smirnoff } 1831eb93b99dSGleb Smirnoff 1832eb93b99dSGleb Smirnoff /* 1833eb93b99dSGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 1834eb93b99dSGleb Smirnoff * different name to the lock. But they all are disposed the same. 1835eb93b99dSGleb Smirnoff */ 1836eb93b99dSGleb Smirnoff static void 1837eb93b99dSGleb Smirnoff inpcb_fini(void *mem, int size) 1838eb93b99dSGleb Smirnoff { 1839eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1840eb93b99dSGleb Smirnoff 1841eb93b99dSGleb Smirnoff INP_LOCK_DESTROY(inp); 184228696211SRobert Watson } 184328696211SRobert Watson 184428696211SRobert Watson /* 1845c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1846c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1847c0a211c5SRobert Watson * 1848c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1849c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1850c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1851c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1852c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1853c0a211c5SRobert Watson * in_pcbdetach(). 1854c0a211c5SRobert Watson * 1855c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1856bbbd7aabSGleb Smirnoff * in_pcbpurgeif0()? 185710702a28SRobert Watson */ 185810702a28SRobert Watson void 185910702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 186010702a28SRobert Watson { 186110702a28SRobert Watson 18628501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 186310702a28SRobert Watson 1864ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 18653ba34b07SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 18663ba34b07SGleb Smirnoff in_pcbremhash(inp); 186710702a28SRobert Watson } 186810702a28SRobert Watson 186967107f45SBjoern A. Zeeb #ifdef INET 187054d642bbSRobert Watson /* 187154d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 187254d642bbSRobert Watson */ 1873117bcae7SGarrett Wollman int 18740fac350cSGleb Smirnoff in_getsockaddr(struct socket *so, struct sockaddr *sa) 1875df8bae1dSRodney W. Grimes { 1876136d4f1cSRobert Watson struct inpcb *inp; 187742fa505bSDavid Greenman 1878fdc984f7STor Egge inp = sotoinpcb(so); 187954d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 18806466b28aSRobert Watson 18810fac350cSGleb Smirnoff *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 18820fac350cSGleb Smirnoff .sin_len = sizeof(struct sockaddr_in), 18830fac350cSGleb Smirnoff .sin_family = AF_INET, 18840fac350cSGleb Smirnoff .sin_port = inp->inp_lport, 18850fac350cSGleb Smirnoff .sin_addr = inp->inp_laddr, 18860fac350cSGleb Smirnoff }; 188742fa505bSDavid Greenman 18880fac350cSGleb Smirnoff return (0); 1889df8bae1dSRodney W. Grimes } 1890df8bae1dSRodney W. Grimes 1891117bcae7SGarrett Wollman int 18920fac350cSGleb Smirnoff in_getpeeraddr(struct socket *so, struct sockaddr *sa) 1893df8bae1dSRodney W. Grimes { 1894136d4f1cSRobert Watson struct inpcb *inp; 189542fa505bSDavid Greenman 1896fdc984f7STor Egge inp = sotoinpcb(so); 189754d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 18986466b28aSRobert Watson 18990fac350cSGleb Smirnoff *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 19000fac350cSGleb Smirnoff .sin_len = sizeof(struct sockaddr_in), 19010fac350cSGleb Smirnoff .sin_family = AF_INET, 19020fac350cSGleb Smirnoff .sin_port = inp->inp_fport, 19030fac350cSGleb Smirnoff .sin_addr = inp->inp_faddr, 19040fac350cSGleb Smirnoff }; 190542fa505bSDavid Greenman 19060fac350cSGleb Smirnoff return (0); 1907df8bae1dSRodney W. Grimes } 1908df8bae1dSRodney W. Grimes 1909db0ac6deSCy Schubert static bool 1910db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 1911db0ac6deSCy Schubert { 1912db0ac6deSCy Schubert 1913db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 1914db0ac6deSCy Schubert return (true); 1915db0ac6deSCy Schubert else 1916db0ac6deSCy Schubert return (false); 1917db0ac6deSCy Schubert } 1918db0ac6deSCy Schubert 1919e43cc4aeSHajimu UMEMOTO void 1920136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1921e43cc4aeSHajimu UMEMOTO { 1922db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 1923db0ac6deSCy Schubert inp_v4_multi_match, NULL); 1924e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 192559854ecfSHans Petter Selasky struct in_multi *inm; 192659854ecfSHans Petter Selasky struct in_mfilter *imf; 1927e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1928e43cc4aeSHajimu UMEMOTO 1929db0ac6deSCy Schubert IN_MULTI_LOCK_ASSERT(); 1930db0ac6deSCy Schubert 1931db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 1932db0ac6deSCy Schubert INP_WLOCK_ASSERT(inp); 1933db0ac6deSCy Schubert 1934e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1935e43cc4aeSHajimu UMEMOTO /* 1936e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1937e43cc4aeSHajimu UMEMOTO * detached. 1938e43cc4aeSHajimu UMEMOTO */ 1939e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1940e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1941e43cc4aeSHajimu UMEMOTO 1942e43cc4aeSHajimu UMEMOTO /* 1943e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1944e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1945cb6bb230SMatt Macy * 1946cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1947e43cc4aeSHajimu UMEMOTO */ 194859854ecfSHans Petter Selasky restart: 194959854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 195059854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 195159854ecfSHans Petter Selasky continue; 195259854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 195359854ecfSHans Petter Selasky continue; 195459854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 195559854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 195659854ecfSHans Petter Selasky ip_mfilter_free(imf); 195759854ecfSHans Petter Selasky goto restart; 1958e43cc4aeSHajimu UMEMOTO } 1959e43cc4aeSHajimu UMEMOTO } 1960e43cc4aeSHajimu UMEMOTO } 1961e43cc4aeSHajimu UMEMOTO 1962df8bae1dSRodney W. Grimes /* 1963fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1964fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1965c3229e05SDavid Greenman */ 1966d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1967df8bae1dSRodney W. Grimes struct inpcb * 1968136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 19699a413162SMark Johnston u_short lport, int fib, int lookupflags, struct ucred *cred) 1970df8bae1dSRodney W. Grimes { 1971136d4f1cSRobert Watson struct inpcb *inp; 1972d5e8a67eSHajimu UMEMOTO #ifdef INET6 1973d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1974d5e8a67eSHajimu UMEMOTO #else 1975d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1976d5e8a67eSHajimu UMEMOTO #endif 1977d5e8a67eSHajimu UMEMOTO int wildcard; 19787bc4aca7SDavid Greenman 197968e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 198068e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 19819a413162SMark Johnston KASSERT(fib == RT_ALL_FIBS || (fib >= 0 && fib < V_rt_numfibs), 19829a413162SMark Johnston ("%s: invalid fib %d", __func__, fib)); 19839a413162SMark Johnston 1984fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 19851b73ca0bSSam Leffler 198668e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1987c3229e05SDavid Greenman struct inpcbhead *head; 1988c3229e05SDavid Greenman /* 1989c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1990c3229e05SDavid Greenman * matches the local address and port we're looking for. 1991c3229e05SDavid Greenman */ 1992fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1993a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 1994fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1995cfa1ca9dSYoshinobu Inoue #ifdef INET6 1996413628a7SBjoern A. Zeeb /* XXX inp locking */ 1997369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1998cfa1ca9dSYoshinobu Inoue continue; 1999cfa1ca9dSYoshinobu Inoue #endif 2000c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 2001c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 20029a413162SMark Johnston inp->inp_lport == lport && (fib == RT_ALL_FIBS || 20039a413162SMark Johnston inp->inp_inc.inc_fibnum == fib)) { 2004c3229e05SDavid Greenman /* 2005413628a7SBjoern A. Zeeb * Found? 2006c3229e05SDavid Greenman */ 2007ac1750ddSMark Johnston if (prison_equal_ip4(cred->cr_prison, 20080304c731SJamie Gritton inp->inp_cred->cr_prison)) 2009c3229e05SDavid Greenman return (inp); 2010df8bae1dSRodney W. Grimes } 2011c3229e05SDavid Greenman } 2012c3229e05SDavid Greenman /* 2013c3229e05SDavid Greenman * Not found. 2014c3229e05SDavid Greenman */ 2015c3229e05SDavid Greenman return (NULL); 2016c3229e05SDavid Greenman } else { 20175f539170SGleb Smirnoff struct inpcbhead *porthash; 2018c3229e05SDavid Greenman struct inpcb *match = NULL; 20195f539170SGleb Smirnoff 2020c3229e05SDavid Greenman /* 2021c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 2022c3229e05SDavid Greenman * fit. 2023c3229e05SDavid Greenman */ 20245f539170SGleb Smirnoff porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 20255f539170SGleb Smirnoff pcbinfo->ipi_porthashmask)]; 20265f539170SGleb Smirnoff CK_LIST_FOREACH(inp, porthash, inp_portlist) { 20275f539170SGleb Smirnoff if (inp->inp_lport != lport) 20285f539170SGleb Smirnoff continue; 2029ac1750ddSMark Johnston if (!prison_equal_ip4(inp->inp_cred->cr_prison, 20300304c731SJamie Gritton cred->cr_prison)) 2031413628a7SBjoern A. Zeeb continue; 20329a413162SMark Johnston if (fib != RT_ALL_FIBS && 20339a413162SMark Johnston inp->inp_inc.inc_fibnum != fib) 20349a413162SMark Johnston continue; 20355f539170SGleb Smirnoff wildcard = 0; 2036cfa1ca9dSYoshinobu Inoue #ifdef INET6 2037413628a7SBjoern A. Zeeb /* XXX inp locking */ 2038369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2039cfa1ca9dSYoshinobu Inoue continue; 2040d5e8a67eSHajimu UMEMOTO /* 20415f539170SGleb Smirnoff * We never select the PCB that has INP_IPV6 flag and 20425f539170SGleb Smirnoff * is bound to :: if we have another PCB which is bound 20435f539170SGleb Smirnoff * to 0.0.0.0. If a PCB has the INP_IPV6 flag, then we 20445f539170SGleb Smirnoff * set its cost higher than IPv4 only PCBs. 2045d5e8a67eSHajimu UMEMOTO * 20465f539170SGleb Smirnoff * Note that the case only happens when a socket is 20475f539170SGleb Smirnoff * bound to ::, under the condition that the use of the 2048d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 2049d5e8a67eSHajimu UMEMOTO */ 2050d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 2051d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2052cfa1ca9dSYoshinobu Inoue #endif 2053c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 2054c3229e05SDavid Greenman wildcard++; 205515bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 205615bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 205715bd2b43SDavid Greenman wildcard++; 205815bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 205915bd2b43SDavid Greenman continue; 206015bd2b43SDavid Greenman } else { 206115bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 206215bd2b43SDavid Greenman wildcard++; 206315bd2b43SDavid Greenman } 2064df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 2065df8bae1dSRodney W. Grimes match = inp; 2066df8bae1dSRodney W. Grimes matchwild = wildcard; 2067413628a7SBjoern A. Zeeb if (matchwild == 0) 2068df8bae1dSRodney W. Grimes break; 2069df8bae1dSRodney W. Grimes } 2070df8bae1dSRodney W. Grimes } 2071df8bae1dSRodney W. Grimes return (match); 2072df8bae1dSRodney W. Grimes } 2073c3229e05SDavid Greenman } 2074d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 207515bd2b43SDavid Greenman 2076d93ec8cbSMark Johnston static bool 2077da806e8dSMark Johnston in_pcblookup_lb_match(const struct inpcblbgroup *grp, int domain, int fib) 2078d93ec8cbSMark Johnston { 2079da806e8dSMark Johnston return ((domain == M_NODOM || domain == grp->il_numa_domain) && 2080da806e8dSMark Johnston (fib == RT_ALL_FIBS || fib == grp->il_fibnum)); 2081d93ec8cbSMark Johnston } 2082d93ec8cbSMark Johnston 20831a43cff9SSean Bruno static struct inpcb * 20841a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 20854130ea61SMark Johnston const struct in_addr *faddr, uint16_t fport, const struct in_addr *laddr, 2086da806e8dSMark Johnston uint16_t lport, int domain, int fib) 20871a43cff9SSean Bruno { 20881a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 20891a43cff9SSean Bruno struct inpcblbgroup *grp; 2090d93ec8cbSMark Johnston struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 2091a600aabeSMark Johnston struct inpcb *inp; 2092a600aabeSMark Johnston u_int count; 20931a43cff9SSean Bruno 20941a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 2095a600aabeSMark Johnston NET_EPOCH_ASSERT(); 20961a43cff9SSean Bruno 20979d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 20989d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 20991a43cff9SSean Bruno 21001a43cff9SSean Bruno /* 2101d93ec8cbSMark Johnston * Search for an LB group match based on the following criteria: 2102d93ec8cbSMark Johnston * - prefer jailed groups to non-jailed groups 2103d93ec8cbSMark Johnston * - prefer exact source address matches to wildcard matches 2104d93ec8cbSMark Johnston * - prefer groups bound to the specified NUMA domain 21051a43cff9SSean Bruno */ 2106d93ec8cbSMark Johnston jail_exact = jail_wild = local_exact = local_wild = NULL; 210754af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 2108d93ec8cbSMark Johnston bool injail; 2109d93ec8cbSMark Johnston 21101a43cff9SSean Bruno #ifdef INET6 21111a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 21121a43cff9SSean Bruno continue; 21131a43cff9SSean Bruno #endif 21148be02ee4SMark Johnston if (grp->il_lport != lport) 21158be02ee4SMark Johnston continue; 21161a43cff9SSean Bruno 2117d93ec8cbSMark Johnston injail = prison_flag(grp->il_cred, PR_IP4) != 0; 2118d93ec8cbSMark Johnston if (injail && prison_check_ip4_locked(grp->il_cred->cr_prison, 2119d93ec8cbSMark Johnston laddr) != 0) 2120d93ec8cbSMark Johnston continue; 2121a034518aSAndrew Gallatin 2122d93ec8cbSMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) { 2123d93ec8cbSMark Johnston if (injail) { 2124d93ec8cbSMark Johnston jail_exact = grp; 2125da806e8dSMark Johnston if (in_pcblookup_lb_match(grp, domain, fib)) 2126d93ec8cbSMark Johnston /* This is a perfect match. */ 2127d93ec8cbSMark Johnston goto out; 2128d93ec8cbSMark Johnston } else if (local_exact == NULL || 2129da806e8dSMark Johnston in_pcblookup_lb_match(grp, domain, fib)) { 2130d93ec8cbSMark Johnston local_exact = grp; 2131d93ec8cbSMark Johnston } 21324130ea61SMark Johnston } else if (grp->il_laddr.s_addr == INADDR_ANY) { 2133d93ec8cbSMark Johnston if (injail) { 2134d93ec8cbSMark Johnston if (jail_wild == NULL || 2135da806e8dSMark Johnston in_pcblookup_lb_match(grp, domain, fib)) 2136d93ec8cbSMark Johnston jail_wild = grp; 2137d93ec8cbSMark Johnston } else if (local_wild == NULL || 2138da806e8dSMark Johnston in_pcblookup_lb_match(grp, domain, fib)) { 2139d93ec8cbSMark Johnston local_wild = grp; 2140d93ec8cbSMark Johnston } 2141d93ec8cbSMark Johnston } 2142d93ec8cbSMark Johnston } 2143d93ec8cbSMark Johnston 2144d93ec8cbSMark Johnston if (jail_exact != NULL) 2145d93ec8cbSMark Johnston grp = jail_exact; 2146d93ec8cbSMark Johnston else if (jail_wild != NULL) 2147d93ec8cbSMark Johnston grp = jail_wild; 2148d93ec8cbSMark Johnston else if (local_exact != NULL) 2149d93ec8cbSMark Johnston grp = local_exact; 2150d93ec8cbSMark Johnston else 2151d93ec8cbSMark Johnston grp = local_wild; 2152d93ec8cbSMark Johnston if (grp == NULL) 2153d93ec8cbSMark Johnston return (NULL); 2154a600aabeSMark Johnston 2155d93ec8cbSMark Johnston out: 2156a600aabeSMark Johnston /* 2157a600aabeSMark Johnston * Synchronize with in_pcblbgroup_insert(). 2158a600aabeSMark Johnston */ 2159a600aabeSMark Johnston count = atomic_load_acq_int(&grp->il_inpcnt); 2160a600aabeSMark Johnston if (count == 0) 2161a600aabeSMark Johnston return (NULL); 2162a600aabeSMark Johnston inp = grp->il_inp[INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) % count]; 2163a600aabeSMark Johnston KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 2164a600aabeSMark Johnston return (inp); 21651a43cff9SSean Bruno } 21661a43cff9SSean Bruno 21673e98dcb3SMark Johnston static bool 21683e98dcb3SMark Johnston in_pcblookup_exact_match(const struct inpcb *inp, struct in_addr faddr, 21694130ea61SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 217015bd2b43SDavid Greenman { 2171cfa1ca9dSYoshinobu Inoue #ifdef INET6 2172413628a7SBjoern A. Zeeb /* XXX inp locking */ 2173369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 21743e98dcb3SMark Johnston return (false); 2175cfa1ca9dSYoshinobu Inoue #endif 21766d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2177ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2178ca98b82cSDavid Greenman inp->inp_fport == fport && 2179220d8921SGleb Smirnoff inp->inp_lport == lport) 21803e98dcb3SMark Johnston return (true); 21813e98dcb3SMark Johnston return (false); 21823e98dcb3SMark Johnston } 21833e98dcb3SMark Johnston 21843e98dcb3SMark Johnston static struct inpcb * 21853e98dcb3SMark Johnston in_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in_addr faddr, 21863e98dcb3SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 21873e98dcb3SMark Johnston { 21883e98dcb3SMark Johnston struct inpcbhead *head; 21893e98dcb3SMark Johnston struct inpcb *inp; 21903e98dcb3SMark Johnston 21913e98dcb3SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 21923e98dcb3SMark Johnston 21933e98dcb3SMark Johnston head = &pcbinfo->ipi_hash_exact[INP_PCBHASH(&faddr, lport, fport, 21943e98dcb3SMark Johnston pcbinfo->ipi_hashmask)]; 21953e98dcb3SMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_exact) { 21963e98dcb3SMark Johnston if (in_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) 2197c3229e05SDavid Greenman return (inp); 2198c3229e05SDavid Greenman } 21993e98dcb3SMark Johnston return (NULL); 22003e98dcb3SMark Johnston } 22013e98dcb3SMark Johnston 22023e98dcb3SMark Johnston typedef enum { 22033e98dcb3SMark Johnston INPLOOKUP_MATCH_NONE = 0, 22043e98dcb3SMark Johnston INPLOOKUP_MATCH_WILD = 1, 22053e98dcb3SMark Johnston INPLOOKUP_MATCH_LADDR = 2, 22063e98dcb3SMark Johnston } inp_lookup_match_t; 22073e98dcb3SMark Johnston 22083e98dcb3SMark Johnston static inp_lookup_match_t 22093e98dcb3SMark Johnston in_pcblookup_wild_match(const struct inpcb *inp, struct in_addr laddr, 2210da806e8dSMark Johnston u_short lport, int fib) 22113e98dcb3SMark Johnston { 22123e98dcb3SMark Johnston #ifdef INET6 22133e98dcb3SMark Johnston /* XXX inp locking */ 22143e98dcb3SMark Johnston if ((inp->inp_vflag & INP_IPV4) == 0) 22153e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 22163e98dcb3SMark Johnston #endif 22173e98dcb3SMark Johnston if (inp->inp_faddr.s_addr != INADDR_ANY || inp->inp_lport != lport) 22183e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 2219da806e8dSMark Johnston if (fib != RT_ALL_FIBS && inp->inp_inc.inc_fibnum != fib) 2220da806e8dSMark Johnston return (INPLOOKUP_MATCH_NONE); 22213e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == INADDR_ANY) 22223e98dcb3SMark Johnston return (INPLOOKUP_MATCH_WILD); 22233e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == laddr.s_addr) 22243e98dcb3SMark Johnston return (INPLOOKUP_MATCH_LADDR); 22253e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 22264130ea61SMark Johnston } 2227e3fd5ffdSRobert Watson 22287b92493aSMark Johnston #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1) 22297b92493aSMark Johnston 22307b92493aSMark Johnston static struct inpcb * 223121d7ac8cSMark Johnston in_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo, struct in_addr laddr, 2232da806e8dSMark Johnston u_short lport, int fib, const inp_lookup_t lockflags) 22337b92493aSMark Johnston { 22347b92493aSMark Johnston struct inpcbhead *head; 22357b92493aSMark Johnston struct inpcb *inp; 22367b92493aSMark Johnston 22377b92493aSMark Johnston KASSERT(SMR_ENTERED(pcbinfo->ipi_smr), 22387b92493aSMark Johnston ("%s: not in SMR read section", __func__)); 22397b92493aSMark Johnston 22407b92493aSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 22417b92493aSMark Johnston pcbinfo->ipi_hashmask)]; 22427b92493aSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 22437b92493aSMark Johnston inp_lookup_match_t match; 22447b92493aSMark Johnston 2245da806e8dSMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport, fib); 22467b92493aSMark Johnston if (match == INPLOOKUP_MATCH_NONE) 22477b92493aSMark Johnston continue; 22487b92493aSMark Johnston 22497b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 2250da806e8dSMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport, fib); 2251a306ed50SMark Johnston if (match != INPLOOKUP_MATCH_NONE && 2252a306ed50SMark Johnston prison_check_ip4_locked(inp->inp_cred->cr_prison, 2253a306ed50SMark Johnston &laddr) == 0) 22547b92493aSMark Johnston return (inp); 22557b92493aSMark Johnston inp_unlock(inp, lockflags); 22567b92493aSMark Johnston } 22577b92493aSMark Johnston 22587b92493aSMark Johnston /* 22597b92493aSMark Johnston * The matching socket disappeared out from under us. Fall back 22607b92493aSMark Johnston * to a serialized lookup. 22617b92493aSMark Johnston */ 22627b92493aSMark Johnston return (INP_LOOKUP_AGAIN); 22637b92493aSMark Johnston } 22647b92493aSMark Johnston return (NULL); 22657b92493aSMark Johnston } 22667b92493aSMark Johnston 22674130ea61SMark Johnston static struct inpcb * 226821d7ac8cSMark Johnston in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr laddr, 2269da806e8dSMark Johnston u_short lport, int fib) 22704130ea61SMark Johnston { 22714130ea61SMark Johnston struct inpcbhead *head; 22724130ea61SMark Johnston struct inpcb *inp, *local_wild, *local_exact, *jail_wild; 2273e3fd5ffdSRobert Watson #ifdef INET6 22744130ea61SMark Johnston struct inpcb *local_wild_mapped; 2275e3fd5ffdSRobert Watson #endif 2276413628a7SBjoern A. Zeeb 22774130ea61SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 2278d93ec8cbSMark Johnston 2279d93ec8cbSMark Johnston /* 2280413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2281413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2282413628a7SBjoern A. Zeeb * 2. jailed, wild. 2283413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2284413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2285413628a7SBjoern A. Zeeb */ 2286fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 2287a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 22884130ea61SMark Johnston local_wild = local_exact = jail_wild = NULL; 22894130ea61SMark Johnston #ifdef INET6 22904130ea61SMark Johnston local_wild_mapped = NULL; 22914130ea61SMark Johnston #endif 2292fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 22933e98dcb3SMark Johnston inp_lookup_match_t match; 22944130ea61SMark Johnston bool injail; 22954130ea61SMark Johnston 2296da806e8dSMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport, fib); 22973e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_NONE) 2298413628a7SBjoern A. Zeeb continue; 2299413628a7SBjoern A. Zeeb 23004130ea61SMark Johnston injail = prison_flag(inp->inp_cred, PR_IP4) != 0; 2301413628a7SBjoern A. Zeeb if (injail) { 23024130ea61SMark Johnston if (prison_check_ip4_locked(inp->inp_cred->cr_prison, 23034130ea61SMark Johnston &laddr) != 0) 2304413628a7SBjoern A. Zeeb continue; 2305413628a7SBjoern A. Zeeb } else { 2306413628a7SBjoern A. Zeeb if (local_exact != NULL) 2307413628a7SBjoern A. Zeeb continue; 2308413628a7SBjoern A. Zeeb } 2309413628a7SBjoern A. Zeeb 23103e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_LADDR) { 2311413628a7SBjoern A. Zeeb if (injail) 2312c3229e05SDavid Greenman return (inp); 2313413628a7SBjoern A. Zeeb local_exact = inp; 23143e98dcb3SMark Johnston } else { 2315e3fd5ffdSRobert Watson #ifdef INET6 2316413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23175cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2318cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2319cfa1ca9dSYoshinobu Inoue else 232083e521ecSBjoern A. Zeeb #endif 2321413628a7SBjoern A. Zeeb if (injail) 2322413628a7SBjoern A. Zeeb jail_wild = inp; 2323413628a7SBjoern A. Zeeb else 23246d6a026bSDavid Greenman local_wild = inp; 23256d6a026bSDavid Greenman } 23264130ea61SMark Johnston } 2327413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2328413628a7SBjoern A. Zeeb return (jail_wild); 2329413628a7SBjoern A. Zeeb if (local_exact != NULL) 2330413628a7SBjoern A. Zeeb return (local_exact); 2331413628a7SBjoern A. Zeeb if (local_wild != NULL) 2332c3229e05SDavid Greenman return (local_wild); 2333413628a7SBjoern A. Zeeb #ifdef INET6 2334413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2335413628a7SBjoern A. Zeeb return (local_wild_mapped); 233683e521ecSBjoern A. Zeeb #endif 23376d6a026bSDavid Greenman return (NULL); 233815bd2b43SDavid Greenman } 2339fa046d87SRobert Watson 2340fa046d87SRobert Watson /* 23414130ea61SMark Johnston * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 23424130ea61SMark Johnston * that the caller has either locked the hash list, which usually happens 23434130ea61SMark Johnston * for bind(2) operations, or is in SMR section, which happens when sorting 23444130ea61SMark Johnston * out incoming packets. 2345fa046d87SRobert Watson */ 2346fa046d87SRobert Watson static struct inpcb * 23474130ea61SMark Johnston in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 23484130ea61SMark Johnston u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2349da806e8dSMark Johnston uint8_t numa_domain, int fib) 23504130ea61SMark Johnston { 23514130ea61SMark Johnston struct inpcb *inp; 23524130ea61SMark Johnston const u_short fport = fport_arg, lport = lport_arg; 23534130ea61SMark Johnston 2354da806e8dSMark Johnston KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD | INPLOOKUP_FIB)) == 0, 23554130ea61SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 23564130ea61SMark Johnston KASSERT(faddr.s_addr != INADDR_ANY, 23574130ea61SMark Johnston ("%s: invalid foreign address", __func__)); 23584130ea61SMark Johnston KASSERT(laddr.s_addr != INADDR_ANY, 23594130ea61SMark Johnston ("%s: invalid local address", __func__)); 23607b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(pcbinfo); 23614130ea61SMark Johnston 23624130ea61SMark Johnston inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 23634130ea61SMark Johnston if (inp != NULL) 23644130ea61SMark Johnston return (inp); 23654130ea61SMark Johnston 23664130ea61SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 23677b92493aSMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 2368da806e8dSMark Johnston &laddr, lport, numa_domain, fib); 23694130ea61SMark Johnston if (inp == NULL) { 237021d7ac8cSMark Johnston inp = in_pcblookup_hash_wild_locked(pcbinfo, laddr, 2371da806e8dSMark Johnston lport, fib); 23724130ea61SMark Johnston } 23734130ea61SMark Johnston } 23744130ea61SMark Johnston 23754130ea61SMark Johnston return (inp); 23764130ea61SMark Johnston } 23774130ea61SMark Johnston 23784130ea61SMark Johnston static struct inpcb * 23797b92493aSMark Johnston in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2380fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2381da806e8dSMark Johnston uint8_t numa_domain, int fib) 2382fa046d87SRobert Watson { 2383fa046d87SRobert Watson struct inpcb *inp; 23847b92493aSMark Johnston const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 23857b92493aSMark Johnston 23867b92493aSMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 23877b92493aSMark Johnston ("%s: LOCKPCB not set", __func__)); 23887b92493aSMark Johnston 23897b92493aSMark Johnston INP_HASH_WLOCK(pcbinfo); 23907b92493aSMark Johnston inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2391da806e8dSMark Johnston lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain, fib); 23927b92493aSMark Johnston if (inp != NULL && !inp_trylock(inp, lockflags)) { 23937b92493aSMark Johnston in_pcbref(inp); 23947b92493aSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 23957b92493aSMark Johnston inp_lock(inp, lockflags); 23967b92493aSMark Johnston if (in_pcbrele(inp, lockflags)) 23977b92493aSMark Johnston /* XXX-MJ or retry until we get a negative match? */ 23987b92493aSMark Johnston inp = NULL; 23997b92493aSMark Johnston } else { 24007b92493aSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 24017b92493aSMark Johnston } 24027b92493aSMark Johnston return (inp); 24037b92493aSMark Johnston } 24047b92493aSMark Johnston 24057b92493aSMark Johnston static struct inpcb * 24067b92493aSMark Johnston in_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr, 24077b92493aSMark Johnston u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2408da806e8dSMark Johnston uint8_t numa_domain, int fib) 24097b92493aSMark Johnston { 24107b92493aSMark Johnston struct inpcb *inp; 24117b92493aSMark Johnston const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 24127b92493aSMark Johnston const u_short fport = fport_arg, lport = lport_arg; 2413fa046d87SRobert Watson 2414675e2618SMark Johnston KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2415675e2618SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 2416675e2618SMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2417675e2618SMark Johnston ("%s: LOCKPCB not set", __func__)); 2418675e2618SMark Johnston 2419db0ac6deSCy Schubert smr_enter(pcbinfo->ipi_smr); 24207b92493aSMark Johnston inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 2421fa046d87SRobert Watson if (inp != NULL) { 24227b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 24237b92493aSMark Johnston /* 24247b92493aSMark Johnston * Revalidate the 4-tuple, the socket could have been 24257b92493aSMark Johnston * disconnected. 24267b92493aSMark Johnston */ 24277b92493aSMark Johnston if (__predict_true(in_pcblookup_exact_match(inp, 24287b92493aSMark Johnston faddr, fport, laddr, lport))) 24297b92493aSMark Johnston return (inp); 24307b92493aSMark Johnston inp_unlock(inp, lockflags); 24317b92493aSMark Johnston } 24327b92493aSMark Johnston 24337b92493aSMark Johnston /* 24347b92493aSMark Johnston * We failed to lock the inpcb, or its connection state changed 24357b92493aSMark Johnston * out from under us. Fall back to a precise search. 24367b92493aSMark Johnston */ 24377b92493aSMark Johnston return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2438da806e8dSMark Johnston lookupflags, numa_domain, fib)); 24397b92493aSMark Johnston } 24407b92493aSMark Johnston 24417b92493aSMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 24427b92493aSMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 2443da806e8dSMark Johnston &laddr, lport, numa_domain, fib); 24447b92493aSMark Johnston if (inp != NULL) { 24457b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 24467b92493aSMark Johnston if (__predict_true(in_pcblookup_wild_match(inp, 2447da806e8dSMark Johnston laddr, lport, fib) != INPLOOKUP_MATCH_NONE)) 24487b92493aSMark Johnston return (inp); 24497b92493aSMark Johnston inp_unlock(inp, lockflags); 24507b92493aSMark Johnston } 24517b92493aSMark Johnston inp = INP_LOOKUP_AGAIN; 24527b92493aSMark Johnston } else { 245321d7ac8cSMark Johnston inp = in_pcblookup_hash_wild_smr(pcbinfo, laddr, lport, 2454da806e8dSMark Johnston fib, lockflags); 24557b92493aSMark Johnston } 24567b92493aSMark Johnston if (inp == INP_LOOKUP_AGAIN) { 24577b92493aSMark Johnston return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, 2458da806e8dSMark Johnston lport, lookupflags, numa_domain, fib)); 24597b92493aSMark Johnston } 24607b92493aSMark Johnston } 24617b92493aSMark Johnston 24627b92493aSMark Johnston if (inp == NULL) 2463db0ac6deSCy Schubert smr_exit(pcbinfo->ipi_smr); 2464d797164aSGleb Smirnoff 2465fa046d87SRobert Watson return (inp); 2466fa046d87SRobert Watson } 2467fa046d87SRobert Watson 2468fa046d87SRobert Watson /* 2469d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2470d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 2471fa046d87SRobert Watson */ 2472fa046d87SRobert Watson struct inpcb * 2473fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 24744130ea61SMark Johnston struct in_addr laddr, u_int lport, int lookupflags, 2475da806e8dSMark Johnston struct ifnet *ifp) 2476fa046d87SRobert Watson { 2477da806e8dSMark Johnston int fib; 2478da806e8dSMark Johnston 2479da806e8dSMark Johnston fib = (lookupflags & INPLOOKUP_FIB) ? if_getfib(ifp) : RT_ALL_FIBS; 24804130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 2481da806e8dSMark Johnston lookupflags, M_NODOM, fib)); 2482fa046d87SRobert Watson } 2483d3c1f003SRobert Watson 2484d3c1f003SRobert Watson struct inpcb * 2485d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2486d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 24874130ea61SMark Johnston struct ifnet *ifp __unused, struct mbuf *m) 2488d3c1f003SRobert Watson { 2489da806e8dSMark Johnston int fib; 2490da806e8dSMark Johnston 2491da806e8dSMark Johnston M_ASSERTPKTHDR(m); 2492da806e8dSMark Johnston fib = (lookupflags & INPLOOKUP_FIB) ? M_GETFIB(m) : RT_ALL_FIBS; 24934130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 2494da806e8dSMark Johnston lookupflags, m->m_pkthdr.numa_domain, fib)); 2495d3c1f003SRobert Watson } 249667107f45SBjoern A. Zeeb #endif /* INET */ 249715bd2b43SDavid Greenman 24987b92493aSMark Johnston static bool 24997b92493aSMark Johnston in_pcbjailed(const struct inpcb *inp, unsigned int flag) 25007b92493aSMark Johnston { 25017b92493aSMark Johnston return (prison_flag(inp->inp_cred, flag) != 0); 25027b92493aSMark Johnston } 25037b92493aSMark Johnston 25047b92493aSMark Johnston /* 25057b92493aSMark Johnston * Insert the PCB into a hash chain using ordering rules which ensure that 25067b92493aSMark Johnston * in_pcblookup_hash_wild_*() always encounter the highest-ranking PCB first. 25077b92493aSMark Johnston * 25087b92493aSMark Johnston * Specifically, keep jailed PCBs in front of non-jailed PCBs, and keep PCBs 250993998d16SMark Johnston * with exact local addresses ahead of wildcard PCBs. Unbound v4-mapped v6 PCBs 251093998d16SMark Johnston * always appear last no matter whether they are jailed. 25117b92493aSMark Johnston */ 25127b92493aSMark Johnston static void 25137b92493aSMark Johnston _in_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 25147b92493aSMark Johnston { 25157b92493aSMark Johnston struct inpcb *last; 25167b92493aSMark Johnston bool bound, injail; 25177b92493aSMark Johnston 251893998d16SMark Johnston INP_LOCK_ASSERT(inp); 25197b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 25207b92493aSMark Johnston 25217b92493aSMark Johnston last = NULL; 25227b92493aSMark Johnston bound = inp->inp_laddr.s_addr != INADDR_ANY; 252393998d16SMark Johnston if (!bound && (inp->inp_vflag & INP_IPV6PROTO) != 0) { 252493998d16SMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 252593998d16SMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 252693998d16SMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 252793998d16SMark Johnston return; 252893998d16SMark Johnston } 252993998d16SMark Johnston } 253093998d16SMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 253193998d16SMark Johnston return; 253293998d16SMark Johnston } 253393998d16SMark Johnston 25347b92493aSMark Johnston injail = in_pcbjailed(inp, PR_IP4); 25357b92493aSMark Johnston if (!injail) { 25367b92493aSMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 253793998d16SMark Johnston if (!in_pcbjailed(last, PR_IP4)) 25387b92493aSMark Johnston break; 25397b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25407b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25417b92493aSMark Johnston return; 25427b92493aSMark Johnston } 25437b92493aSMark Johnston } 25447b92493aSMark Johnston } else if (!CK_LIST_EMPTY(pcbhash) && 25457b92493aSMark Johnston !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP4)) { 25467b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25477b92493aSMark Johnston return; 25487b92493aSMark Johnston } 25497b92493aSMark Johnston if (!bound) { 25507b92493aSMark Johnston CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 25517b92493aSMark Johnston if (last->inp_laddr.s_addr == INADDR_ANY) 25527b92493aSMark Johnston break; 25537b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25547b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25557b92493aSMark Johnston return; 25567b92493aSMark Johnston } 25577b92493aSMark Johnston } 25587b92493aSMark Johnston } 25597b92493aSMark Johnston if (last == NULL) 25607b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25617b92493aSMark Johnston else 25627b92493aSMark Johnston CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 25637b92493aSMark Johnston } 25647b92493aSMark Johnston 25657b92493aSMark Johnston #ifdef INET6 25667b92493aSMark Johnston /* 25677b92493aSMark Johnston * See the comment above _in_pcbinshash_wild(). 25687b92493aSMark Johnston */ 25697b92493aSMark Johnston static void 25707b92493aSMark Johnston _in6_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 25717b92493aSMark Johnston { 25727b92493aSMark Johnston struct inpcb *last; 25737b92493aSMark Johnston bool bound, injail; 25747b92493aSMark Johnston 257593998d16SMark Johnston INP_LOCK_ASSERT(inp); 25767b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 25777b92493aSMark Johnston 25787b92493aSMark Johnston last = NULL; 25797b92493aSMark Johnston bound = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr); 25807b92493aSMark Johnston injail = in_pcbjailed(inp, PR_IP6); 25817b92493aSMark Johnston if (!injail) { 25827b92493aSMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 258393998d16SMark Johnston if (!in_pcbjailed(last, PR_IP6)) 25847b92493aSMark Johnston break; 25857b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25867b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25877b92493aSMark Johnston return; 25887b92493aSMark Johnston } 25897b92493aSMark Johnston } 25907b92493aSMark Johnston } else if (!CK_LIST_EMPTY(pcbhash) && 25917b92493aSMark Johnston !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP6)) { 25927b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25937b92493aSMark Johnston return; 25947b92493aSMark Johnston } 25957b92493aSMark Johnston if (!bound) { 25967b92493aSMark Johnston CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 25977b92493aSMark Johnston if (IN6_IS_ADDR_UNSPECIFIED(&last->in6p_laddr)) 25987b92493aSMark Johnston break; 25997b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 26007b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 26017b92493aSMark Johnston return; 26027b92493aSMark Johnston } 26037b92493aSMark Johnston } 26047b92493aSMark Johnston } 26057b92493aSMark Johnston if (last == NULL) 26067b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 26077b92493aSMark Johnston else 26087b92493aSMark Johnston CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 26097b92493aSMark Johnston } 26107b92493aSMark Johnston #endif 26117b92493aSMark Johnston 26127bc4aca7SDavid Greenman /* 2613c3229e05SDavid Greenman * Insert PCB onto various hash lists. 26147bc4aca7SDavid Greenman */ 2615db0ac6deSCy Schubert int 2616db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp) 261715bd2b43SDavid Greenman { 26185f539170SGleb Smirnoff struct inpcbhead *pcbhash, *pcbporthash; 2619c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2620fdb987beSMark Johnston uint32_t hash; 2621fdb987beSMark Johnston bool connected; 262215bd2b43SDavid Greenman 26238501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2624fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2625111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2626111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2627602cc7f1SRobert Watson 2628cfa1ca9dSYoshinobu Inoue #ifdef INET6 2629fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2630fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2631fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2632fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2633fdb987beSMark Johnston } else 263483e521ecSBjoern A. Zeeb #endif 2635fdb987beSMark Johnston { 2636fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2637fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2638fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2639fdb987beSMark Johnston } 2640fdb987beSMark Johnston 2641fdb987beSMark Johnston if (connected) 2642fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_exact[hash]; 2643fdb987beSMark Johnston else 2644fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_wild[hash]; 264515bd2b43SDavid Greenman 2646712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2647712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2648c3229e05SDavid Greenman 2649c3229e05SDavid Greenman /* 26501a43cff9SSean Bruno * Add entry to load balance group. 26511a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 26521a43cff9SSean Bruno */ 2653e3ba0d6aSGleb Smirnoff if ((inp->inp_socket->so_options & SO_REUSEPORT_LB) != 0) { 2654a152dd86SMark Johnston int error = in_pcbinslbgrouphash(inp, M_NODOM); 2655a152dd86SMark Johnston if (error != 0) 2656a152dd86SMark Johnston return (error); 26571a43cff9SSean Bruno } 26581a43cff9SSean Bruno 26591a43cff9SSean Bruno /* 2660fdb987beSMark Johnston * The PCB may have been disconnected in the past. Before we can safely 2661fdb987beSMark Johnston * make it visible in the hash table, we must wait for all readers which 2662fdb987beSMark Johnston * may be traversing this PCB to finish. 2663fdb987beSMark Johnston */ 2664fdb987beSMark Johnston if (inp->inp_smr != SMR_SEQ_INVALID) { 2665fdb987beSMark Johnston smr_wait(pcbinfo->ipi_smr, inp->inp_smr); 2666fdb987beSMark Johnston inp->inp_smr = SMR_SEQ_INVALID; 2667fdb987beSMark Johnston } 2668fdb987beSMark Johnston 2669fdb987beSMark Johnston if (connected) 2670fdb987beSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_exact); 26717b92493aSMark Johnston else { 26727b92493aSMark Johnston #ifdef INET6 26737b92493aSMark Johnston if ((inp->inp_vflag & INP_IPV6) != 0) 26747b92493aSMark Johnston _in6_pcbinshash_wild(pcbhash, inp); 2675fdb987beSMark Johnston else 26767b92493aSMark Johnston #endif 26777b92493aSMark Johnston _in_pcbinshash_wild(pcbhash, inp); 26787b92493aSMark Johnston } 26795f539170SGleb Smirnoff CK_LIST_INSERT_HEAD(pcbporthash, inp, inp_portlist); 2680111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 2681db0ac6deSCy Schubert 2682c3229e05SDavid Greenman return (0); 268315bd2b43SDavid Greenman } 268415bd2b43SDavid Greenman 2685fdb987beSMark Johnston void 2686fdb987beSMark Johnston in_pcbremhash_locked(struct inpcb *inp) 26873ba34b07SGleb Smirnoff { 26883ba34b07SGleb Smirnoff 26893ba34b07SGleb Smirnoff INP_WLOCK_ASSERT(inp); 2690fdb987beSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 26913ba34b07SGleb Smirnoff MPASS(inp->inp_flags & INP_INHASHLIST); 26923ba34b07SGleb Smirnoff 2693a43e7a96SGleb Smirnoff if ((inp->inp_flags & INP_INLBGROUP) != 0) 26943ba34b07SGleb Smirnoff in_pcbremlbgrouphash(inp); 2695fdb987beSMark Johnston #ifdef INET6 2696fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2697fdb987beSMark Johnston if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 2698fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2699fdb987beSMark Johnston else 2700fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2701fdb987beSMark Johnston } else 2702fdb987beSMark Johnston #endif 2703fdb987beSMark Johnston { 2704fdb987beSMark Johnston if (in_nullhost(inp->inp_faddr)) 2705fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2706fdb987beSMark Johnston else 2707fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2708fdb987beSMark Johnston } 27093ba34b07SGleb Smirnoff CK_LIST_REMOVE(inp, inp_portlist); 27103ba34b07SGleb Smirnoff inp->inp_flags &= ~INP_INHASHLIST; 27113ba34b07SGleb Smirnoff } 27123ba34b07SGleb Smirnoff 2713fdb987beSMark Johnston static void 2714fdb987beSMark Johnston in_pcbremhash(struct inpcb *inp) 2715fdb987beSMark Johnston { 2716fdb987beSMark Johnston INP_HASH_WLOCK(inp->inp_pcbinfo); 2717fdb987beSMark Johnston in_pcbremhash_locked(inp); 2718fdb987beSMark Johnston INP_HASH_WUNLOCK(inp->inp_pcbinfo); 2719fdb987beSMark Johnston } 2720fdb987beSMark Johnston 272152cd27cbSRobert Watson /* 2722c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2723c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2724c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2725c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2726c3229e05SDavid Greenman */ 272715bd2b43SDavid Greenman void 2728db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp) 272915bd2b43SDavid Greenman { 273059daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 273115bd2b43SDavid Greenman struct inpcbhead *head; 2732fdb987beSMark Johnston uint32_t hash; 2733fdb987beSMark Johnston bool connected; 273415bd2b43SDavid Greenman 27358501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2736fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2737111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2738fdb987beSMark Johnston ("%s: !INP_INHASHLIST", __func__)); 2739fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 2740fdb987beSMark Johnston ("%s: inp was disconnected", __func__)); 2741602cc7f1SRobert Watson 2742cfa1ca9dSYoshinobu Inoue #ifdef INET6 2743fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2744fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2745fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2746fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2747fdb987beSMark Johnston } else 274883e521ecSBjoern A. Zeeb #endif 2749fdb987beSMark Johnston { 2750fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2751fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2752fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2753fdb987beSMark Johnston } 275415bd2b43SDavid Greenman 2755fdb987beSMark Johnston /* 2756fdb987beSMark Johnston * When rehashing, the caller must ensure that either the new or the old 2757fdb987beSMark Johnston * foreign address was unspecified. 2758fdb987beSMark Johnston */ 2759fdb987beSMark Johnston if (connected) 2760fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2761fdb987beSMark Johnston else 2762fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2763fdb987beSMark Johnston 2764fdb987beSMark Johnston if (connected) { 2765fdb987beSMark Johnston head = &pcbinfo->ipi_hash_exact[hash]; 2766fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_exact); 2767fdb987beSMark Johnston } else { 2768fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[hash]; 2769fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_wild); 2770fdb987beSMark Johnston } 2771c3229e05SDavid Greenman } 2772c3229e05SDavid Greenman 2773c3229e05SDavid Greenman /* 277484cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 277584cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 277684cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 277784cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 277884cc0778SGeorge V. Neville-Neil */ 277984cc0778SGeorge V. Neville-Neil void 278084cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 278184cc0778SGeorge V. Neville-Neil { 278284cc0778SGeorge V. Neville-Neil 2783fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 278484cc0778SGeorge V. Neville-Neil return; 278584cc0778SGeorge V. Neville-Neil } 278684cc0778SGeorge V. Neville-Neil 278784cc0778SGeorge V. Neville-Neil /* 2788a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2789a557af22SRobert Watson * label change into the in_pcb for the socket. 2790a557af22SRobert Watson */ 2791a557af22SRobert Watson void 2792136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2793a557af22SRobert Watson { 2794a557af22SRobert Watson #ifdef MAC 2795a557af22SRobert Watson struct inpcb *inp; 2796a557af22SRobert Watson 27974c7c478dSRobert Watson inp = sotoinpcb(so); 27984c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2799602cc7f1SRobert Watson 28008501a69cSRobert Watson INP_WLOCK(inp); 2801310e7cebSRobert Watson SOCK_LOCK(so); 2802a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2803310e7cebSRobert Watson SOCK_UNLOCK(so); 28048501a69cSRobert Watson INP_WUNLOCK(inp); 2805a557af22SRobert Watson #endif 2806a557af22SRobert Watson } 28075f311da2SMike Silbersack 28083d585327SKip Macy void 28093d585327SKip Macy inp_wlock(struct inpcb *inp) 28103d585327SKip Macy { 28113d585327SKip Macy 28128501a69cSRobert Watson INP_WLOCK(inp); 28133d585327SKip Macy } 28143d585327SKip Macy 28153d585327SKip Macy void 28163d585327SKip Macy inp_wunlock(struct inpcb *inp) 28173d585327SKip Macy { 28183d585327SKip Macy 28198501a69cSRobert Watson INP_WUNLOCK(inp); 28203d585327SKip Macy } 28213d585327SKip Macy 28223d585327SKip Macy void 28233d585327SKip Macy inp_rlock(struct inpcb *inp) 28243d585327SKip Macy { 28253d585327SKip Macy 2826a69042a5SRobert Watson INP_RLOCK(inp); 28273d585327SKip Macy } 28283d585327SKip Macy 28293d585327SKip Macy void 28303d585327SKip Macy inp_runlock(struct inpcb *inp) 28313d585327SKip Macy { 28323d585327SKip Macy 2833a69042a5SRobert Watson INP_RUNLOCK(inp); 28343d585327SKip Macy } 28353d585327SKip Macy 2836c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 28373d585327SKip Macy void 2838e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 28393d585327SKip Macy { 28403d585327SKip Macy 28418501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 28423d585327SKip Macy } 28433d585327SKip Macy 28443d585327SKip Macy void 2845e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 28463d585327SKip Macy { 28473d585327SKip Macy 28483d585327SKip Macy INP_UNLOCK_ASSERT(inp); 28493d585327SKip Macy } 28503d585327SKip Macy #endif 28513d585327SKip Macy 28529378e437SKip Macy void 285324cf7a8dSGleb Smirnoff inp_apply_all(struct inpcbinfo *pcbinfo, 285424cf7a8dSGleb Smirnoff void (*func)(struct inpcb *, void *), void *arg) 28559378e437SKip Macy { 285624cf7a8dSGleb Smirnoff struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2857db0ac6deSCy Schubert INPLOOKUP_WLOCKPCB); 28589378e437SKip Macy struct inpcb *inp; 28599378e437SKip Macy 2860db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) 28619378e437SKip Macy func(inp, arg); 28629378e437SKip Macy } 28639378e437SKip Macy 28649378e437SKip Macy struct socket * 28659378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 28669378e437SKip Macy { 28679378e437SKip Macy 28689378e437SKip Macy INP_WLOCK_ASSERT(inp); 28699378e437SKip Macy return (inp->inp_socket); 28709378e437SKip Macy } 28719378e437SKip Macy 28729378e437SKip Macy void 2873df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 28749d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 28759378e437SKip Macy { 28769378e437SKip Macy 28779d29c635SKip Macy INP_LOCK_ASSERT(inp); 2878df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2879df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 28809378e437SKip Macy *lp = inp->inp_lport; 28819378e437SKip Macy *fp = inp->inp_fport; 28829378e437SKip Macy } 28839378e437SKip Macy 2884cc65eb4eSGleb Smirnoff /* 2885cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2886cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2887cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2888cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2889cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2890cc65eb4eSGleb Smirnoff * hidden from users. 2891cc65eb4eSGleb Smirnoff */ 2892cc65eb4eSGleb Smirnoff void 2893cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2894cc65eb4eSGleb Smirnoff { 2895cc65eb4eSGleb Smirnoff 289679db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2897cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2898cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2899cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2900cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2901cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 2902cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2903cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2904cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2905cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2906cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2907cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2908cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2909cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2910cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2911cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2912cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2913cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2914cc65eb4eSGleb Smirnoff } 2915cc65eb4eSGleb Smirnoff 2916a35bdd44SMichael Tuexen int 2917a35bdd44SMichael Tuexen sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, 2918a35bdd44SMichael Tuexen int (*ctloutput_set)(struct inpcb *, struct sockopt *)) 2919a35bdd44SMichael Tuexen { 2920a35bdd44SMichael Tuexen struct sockopt sopt; 2921a35bdd44SMichael Tuexen struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2922a35bdd44SMichael Tuexen INPLOOKUP_WLOCKPCB); 2923a35bdd44SMichael Tuexen struct inpcb *inp; 2924a35bdd44SMichael Tuexen struct sockopt_parameters *params; 2925a35bdd44SMichael Tuexen struct socket *so; 2926a35bdd44SMichael Tuexen int error; 2927a35bdd44SMichael Tuexen char buf[1024]; 2928a35bdd44SMichael Tuexen 2929a35bdd44SMichael Tuexen if (req->oldptr != NULL || req->oldlen != 0) 2930a35bdd44SMichael Tuexen return (EINVAL); 2931a35bdd44SMichael Tuexen if (req->newptr == NULL) 2932a35bdd44SMichael Tuexen return (EPERM); 2933a35bdd44SMichael Tuexen if (req->newlen > sizeof(buf)) 2934a35bdd44SMichael Tuexen return (ENOMEM); 2935a35bdd44SMichael Tuexen error = SYSCTL_IN(req, buf, req->newlen); 2936a35bdd44SMichael Tuexen if (error != 0) 2937a35bdd44SMichael Tuexen return (error); 2938a35bdd44SMichael Tuexen if (req->newlen < sizeof(struct sockopt_parameters)) 2939a35bdd44SMichael Tuexen return (EINVAL); 2940a35bdd44SMichael Tuexen params = (struct sockopt_parameters *)buf; 2941a35bdd44SMichael Tuexen sopt.sopt_level = params->sop_level; 2942a35bdd44SMichael Tuexen sopt.sopt_name = params->sop_optname; 2943a35bdd44SMichael Tuexen sopt.sopt_dir = SOPT_SET; 2944a35bdd44SMichael Tuexen sopt.sopt_val = params->sop_optval; 2945a35bdd44SMichael Tuexen sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters); 2946a35bdd44SMichael Tuexen sopt.sopt_td = NULL; 2947a0aeb1ceSMichael Tuexen #ifdef INET6 2948a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) { 2949a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_laddr)) 2950a35bdd44SMichael Tuexen params->sop_inc.inc6_laddr.s6_addr16[1] = 2951a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2952a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_faddr)) 2953a35bdd44SMichael Tuexen params->sop_inc.inc6_faddr.s6_addr16[1] = 2954a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2955a35bdd44SMichael Tuexen } 2956a0aeb1ceSMichael Tuexen #endif 295713720136SMichael Tuexen if (params->sop_inc.inc_lport != htons(0) && 295813720136SMichael Tuexen params->sop_inc.inc_fport != htons(0)) { 2959a0aeb1ceSMichael Tuexen #ifdef INET6 2960a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) 2961a35bdd44SMichael Tuexen inpi.hash = INP6_PCBHASH( 2962a35bdd44SMichael Tuexen ¶ms->sop_inc.inc6_faddr, 2963a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2964a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2965a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2966a35bdd44SMichael Tuexen else 2967a0aeb1ceSMichael Tuexen #endif 2968a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH( 2969a35bdd44SMichael Tuexen ¶ms->sop_inc.inc_faddr, 2970a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2971a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2972a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2973a35bdd44SMichael Tuexen } 2974a35bdd44SMichael Tuexen while ((inp = inp_next(&inpi)) != NULL) 2975a35bdd44SMichael Tuexen if (inp->inp_gencnt == params->sop_id) { 297653af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 2977a35bdd44SMichael Tuexen INP_WUNLOCK(inp); 2978a35bdd44SMichael Tuexen return (ECONNRESET); 2979a35bdd44SMichael Tuexen } 2980a35bdd44SMichael Tuexen so = inp->inp_socket; 2981a35bdd44SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 2982a35bdd44SMichael Tuexen soref(so); 2983bed7633bSMichael Tuexen if (params->sop_level == SOL_SOCKET) { 2984bed7633bSMichael Tuexen INP_WUNLOCK(inp); 2985bed7633bSMichael Tuexen error = sosetopt(so, &sopt); 2986bed7633bSMichael Tuexen } else 2987a35bdd44SMichael Tuexen error = (*ctloutput_set)(inp, &sopt); 2988a35bdd44SMichael Tuexen sorele(so); 2989a35bdd44SMichael Tuexen break; 2990a35bdd44SMichael Tuexen } 2991a35bdd44SMichael Tuexen if (inp == NULL) 2992a35bdd44SMichael Tuexen error = ESRCH; 2993a35bdd44SMichael Tuexen return (error); 2994a35bdd44SMichael Tuexen } 2995a35bdd44SMichael Tuexen 2996497057eeSRobert Watson #ifdef DDB 2997497057eeSRobert Watson static void 2998497057eeSRobert Watson db_print_indent(int indent) 2999497057eeSRobert Watson { 3000497057eeSRobert Watson int i; 3001497057eeSRobert Watson 3002497057eeSRobert Watson for (i = 0; i < indent; i++) 3003497057eeSRobert Watson db_printf(" "); 3004497057eeSRobert Watson } 3005497057eeSRobert Watson 3006497057eeSRobert Watson static void 3007497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 3008497057eeSRobert Watson { 3009497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 3010497057eeSRobert Watson 3011497057eeSRobert Watson db_print_indent(indent); 3012497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 3013497057eeSRobert Watson 3014497057eeSRobert Watson indent += 2; 3015497057eeSRobert Watson 301603dc38a4SRobert Watson #ifdef INET6 3017dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3018497057eeSRobert Watson /* IPv6. */ 3019497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 3020497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 302183e521ecSBjoern A. Zeeb } else 302203dc38a4SRobert Watson #endif 302383e521ecSBjoern A. Zeeb { 3024497057eeSRobert Watson /* IPv4. */ 3025497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 3026497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 3027497057eeSRobert Watson } 3028497057eeSRobert Watson db_print_indent(indent); 3029497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 3030497057eeSRobert Watson ntohs(inc->inc_lport)); 3031497057eeSRobert Watson db_print_indent(indent); 3032497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 3033497057eeSRobert Watson ntohs(inc->inc_fport)); 3034497057eeSRobert Watson } 3035497057eeSRobert Watson 3036497057eeSRobert Watson static void 3037497057eeSRobert Watson db_print_inpflags(int inp_flags) 3038497057eeSRobert Watson { 3039497057eeSRobert Watson int comma; 3040497057eeSRobert Watson 3041497057eeSRobert Watson comma = 0; 3042497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 3043497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 3044497057eeSRobert Watson comma = 1; 3045497057eeSRobert Watson } 3046497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 3047497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 3048497057eeSRobert Watson comma = 1; 3049497057eeSRobert Watson } 3050497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 3051497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 3052497057eeSRobert Watson comma = 1; 3053497057eeSRobert Watson } 3054dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 3055dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 3056dce33a45SErmal Luçi comma = 1; 3057dce33a45SErmal Luçi } 3058497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 3059497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 3060497057eeSRobert Watson comma = 1; 3061497057eeSRobert Watson } 3062497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 3063497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 3064497057eeSRobert Watson comma = 1; 3065497057eeSRobert Watson } 3066497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 3067497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 3068497057eeSRobert Watson comma = 1; 3069497057eeSRobert Watson } 3070497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 3071497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 3072497057eeSRobert Watson comma = 1; 3073497057eeSRobert Watson } 3074497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 3075497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 3076497057eeSRobert Watson comma = 1; 3077497057eeSRobert Watson } 3078497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 3079497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 3080497057eeSRobert Watson comma = 1; 3081497057eeSRobert Watson } 3082497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 3083497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 3084497057eeSRobert Watson comma = 1; 3085497057eeSRobert Watson } 3086497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 3087497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 3088497057eeSRobert Watson comma = 1; 3089497057eeSRobert Watson } 30903cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 30913cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 30923cca425bSMichael Tuexen comma = 1; 30933cca425bSMichael Tuexen } 3094497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 3095497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3096497057eeSRobert Watson comma = 1; 3097497057eeSRobert Watson } 3098497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 3099497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3100497057eeSRobert Watson comma = 1; 3101497057eeSRobert Watson } 3102497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 3103497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3104497057eeSRobert Watson comma = 1; 3105497057eeSRobert Watson } 3106497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 3107497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3108497057eeSRobert Watson comma = 1; 3109497057eeSRobert Watson } 3110497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 3111497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3112497057eeSRobert Watson comma = 1; 3113497057eeSRobert Watson } 3114497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 3115497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3116497057eeSRobert Watson comma = 1; 3117497057eeSRobert Watson } 3118497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 3119497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3120497057eeSRobert Watson comma = 1; 3121497057eeSRobert Watson } 3122497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 3123497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3124497057eeSRobert Watson comma = 1; 3125497057eeSRobert Watson } 3126497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 3127497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3128497057eeSRobert Watson comma = 1; 3129497057eeSRobert Watson } 3130ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 3131ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3132ad71fe3cSRobert Watson comma = 1; 3133ad71fe3cSRobert Watson } 3134ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 3135ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 3136ad71fe3cSRobert Watson comma = 1; 3137ad71fe3cSRobert Watson } 3138ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 3139ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3140ad71fe3cSRobert Watson comma = 1; 3141ad71fe3cSRobert Watson } 3142497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 3143497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3144497057eeSRobert Watson comma = 1; 3145497057eeSRobert Watson } 3146497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 3147497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 3148497057eeSRobert Watson comma = 1; 3149497057eeSRobert Watson } 3150497057eeSRobert Watson } 3151497057eeSRobert Watson 3152497057eeSRobert Watson static void 3153497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 3154497057eeSRobert Watson { 3155497057eeSRobert Watson int comma; 3156497057eeSRobert Watson 3157497057eeSRobert Watson comma = 0; 3158497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3159497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3160497057eeSRobert Watson comma = 1; 3161497057eeSRobert Watson } 3162497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3163497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3164497057eeSRobert Watson comma = 1; 3165497057eeSRobert Watson } 3166497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3167497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3168497057eeSRobert Watson comma = 1; 3169497057eeSRobert Watson } 3170497057eeSRobert Watson } 3171497057eeSRobert Watson 31726d888973SRobert Watson static void 3173497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3174497057eeSRobert Watson { 3175497057eeSRobert Watson 3176497057eeSRobert Watson db_print_indent(indent); 3177497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3178497057eeSRobert Watson 3179497057eeSRobert Watson indent += 2; 3180497057eeSRobert Watson 3181497057eeSRobert Watson db_print_indent(indent); 3182497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3183497057eeSRobert Watson 3184497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3185497057eeSRobert Watson 3186497057eeSRobert Watson db_print_indent(indent); 3187497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3188497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3189497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3190497057eeSRobert Watson db_printf(")\n"); 3191497057eeSRobert Watson 3192497057eeSRobert Watson db_print_indent(indent); 3193497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3194497057eeSRobert Watson inp->inp_vflag); 3195497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3196497057eeSRobert Watson db_printf(")\n"); 3197497057eeSRobert Watson 3198497057eeSRobert Watson db_print_indent(indent); 3199497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3200497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3201497057eeSRobert Watson 3202497057eeSRobert Watson db_print_indent(indent); 3203497057eeSRobert Watson #ifdef INET6 3204497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3205497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3206497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3207497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3208497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3209497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3210497057eeSRobert Watson inp->in6p_hops); 3211497057eeSRobert Watson } else 3212497057eeSRobert Watson #endif 3213497057eeSRobert Watson { 3214497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3215497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3216497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3217497057eeSRobert Watson } 3218497057eeSRobert Watson 3219497057eeSRobert Watson db_print_indent(indent); 32205f539170SGleb Smirnoff db_printf("inp_gencnt: %ju\n", (uintmax_t)inp->inp_gencnt); 3221497057eeSRobert Watson } 3222497057eeSRobert Watson 3223497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3224497057eeSRobert Watson { 3225497057eeSRobert Watson struct inpcb *inp; 3226497057eeSRobert Watson 3227497057eeSRobert Watson if (!have_addr) { 3228497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3229497057eeSRobert Watson return; 3230497057eeSRobert Watson } 3231497057eeSRobert Watson inp = (struct inpcb *)addr; 3232497057eeSRobert Watson 3233497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3234497057eeSRobert Watson } 323583e521ecSBjoern A. Zeeb #endif /* DDB */ 3236f3e7afe2SHans Petter Selasky 3237f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3238f3e7afe2SHans Petter Selasky /* 3239f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3240f3e7afe2SHans Petter Selasky * if any. 3241f3e7afe2SHans Petter Selasky */ 3242f3e7afe2SHans Petter Selasky int 3243f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3244f3e7afe2SHans Petter Selasky { 3245f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3246f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 324720abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3248f3e7afe2SHans Petter Selasky }; 3249f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3250f3e7afe2SHans Petter Selasky int error; 3251f3e7afe2SHans Petter Selasky 3252f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3253f3e7afe2SHans Petter Selasky if (mst == NULL) 3254f3e7afe2SHans Petter Selasky return (EINVAL); 3255f3e7afe2SHans Petter Selasky 3256c782ea8bSJohn Baldwin if (mst->sw->snd_tag_modify == NULL) { 3257f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3258f3e7afe2SHans Petter Selasky } else { 3259c782ea8bSJohn Baldwin error = mst->sw->snd_tag_modify(mst, ¶ms); 3260f3e7afe2SHans Petter Selasky } 3261f3e7afe2SHans Petter Selasky return (error); 3262f3e7afe2SHans Petter Selasky } 3263f3e7afe2SHans Petter Selasky 3264f3e7afe2SHans Petter Selasky /* 3265f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3266f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3267f3e7afe2SHans Petter Selasky */ 3268f3e7afe2SHans Petter Selasky int 3269f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3270f3e7afe2SHans Petter Selasky { 3271f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3272f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3273f3e7afe2SHans Petter Selasky int error; 3274f3e7afe2SHans Petter Selasky 3275f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3276f3e7afe2SHans Petter Selasky if (mst == NULL) 3277f3e7afe2SHans Petter Selasky return (EINVAL); 3278f3e7afe2SHans Petter Selasky 3279c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) { 3280f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3281f3e7afe2SHans Petter Selasky } else { 3282c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 3283f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3284f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3285f3e7afe2SHans Petter Selasky } 3286f3e7afe2SHans Petter Selasky return (error); 3287f3e7afe2SHans Petter Selasky } 3288f3e7afe2SHans Petter Selasky 3289f3e7afe2SHans Petter Selasky /* 329095ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 329195ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 329295ed5015SHans Petter Selasky */ 329395ed5015SHans Petter Selasky int 329495ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 329595ed5015SHans Petter Selasky { 329695ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 329795ed5015SHans Petter Selasky struct m_snd_tag *mst; 329895ed5015SHans Petter Selasky int error; 329995ed5015SHans Petter Selasky 330095ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 330195ed5015SHans Petter Selasky if (mst == NULL) 330295ed5015SHans Petter Selasky return (EINVAL); 330395ed5015SHans Petter Selasky 3304c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) 330595ed5015SHans Petter Selasky return (EOPNOTSUPP); 330695ed5015SHans Petter Selasky 3307c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 330895ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 330995ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 331095ed5015SHans Petter Selasky return (error); 331195ed5015SHans Petter Selasky } 331295ed5015SHans Petter Selasky 331395ed5015SHans Petter Selasky /* 3314f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3315f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3316f3e7afe2SHans Petter Selasky */ 3317f3e7afe2SHans Petter Selasky int 3318f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 331920abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 332020abea66SRandall Stewart 3321f3e7afe2SHans Petter Selasky { 3322f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 332395ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 332495ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3325f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3326f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 332798085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3328f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 332920abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3330f3e7afe2SHans Petter Selasky }; 3331f3e7afe2SHans Petter Selasky int error; 3332f3e7afe2SHans Petter Selasky 3333f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3334f3e7afe2SHans Petter Selasky 333585d8d30fSHans Petter Selasky /* 333685d8d30fSHans Petter Selasky * If there is already a send tag, or the INP is being torn 333785d8d30fSHans Petter Selasky * down, allocating a new send tag is not allowed. Else send 333885d8d30fSHans Petter Selasky * tags may leak. 333985d8d30fSHans Petter Selasky */ 334053af6903SGleb Smirnoff if (*st != NULL || (inp->inp_flags & INP_DROPPED) != 0) 3341f3e7afe2SHans Petter Selasky return (EINVAL); 3342f3e7afe2SHans Petter Selasky 334336e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, st); 3344903c4ee6SXin LI #ifdef INET 334520abea66SRandall Stewart if (error == 0) { 334620abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 334720abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 334836e0a362SJohn Baldwin } else if (error != EOPNOTSUPP) 334920abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3350903c4ee6SXin LI #endif 3351f3e7afe2SHans Petter Selasky return (error); 3352f3e7afe2SHans Petter Selasky } 3353f3e7afe2SHans Petter Selasky 335420abea66SRandall Stewart void 335598d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst) 335620abea66SRandall Stewart { 335720abea66SRandall Stewart 335898d7a8d9SJohn Baldwin m_snd_tag_rele(mst); 3359903c4ee6SXin LI #ifdef INET 336020abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3361903c4ee6SXin LI #endif 336220abea66SRandall Stewart } 336320abea66SRandall Stewart 3364f3e7afe2SHans Petter Selasky /* 3365f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3366f3e7afe2SHans Petter Selasky * if any: 3367f3e7afe2SHans Petter Selasky */ 3368f3e7afe2SHans Petter Selasky void 3369f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3370f3e7afe2SHans Petter Selasky { 3371f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3372f3e7afe2SHans Petter Selasky 3373f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3374f3e7afe2SHans Petter Selasky 3375f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3376f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3377f3e7afe2SHans Petter Selasky 3378f3e7afe2SHans Petter Selasky if (mst == NULL) 3379f3e7afe2SHans Petter Selasky return; 3380f3e7afe2SHans Petter Selasky 3381fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3382093e7231SHans Petter Selasky #ifdef INET 3383093e7231SHans Petter Selasky counter_u64_add(rate_limit_active, -1); 3384093e7231SHans Petter Selasky #endif 3385f3e7afe2SHans Petter Selasky } 3386f3e7afe2SHans Petter Selasky 338720abea66SRandall Stewart int 338820abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 338920abea66SRandall Stewart { 339020abea66SRandall Stewart int error; 339120abea66SRandall Stewart 339220abea66SRandall Stewart /* 339320abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 339420abea66SRandall Stewart * a route change, first drop the existing tag. Set the 339520abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 339620abea66SRandall Stewart * tag if we fail to allocate one this time. 339720abea66SRandall Stewart */ 339820abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 339920abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 340020abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 340120abea66SRandall Stewart } 340220abea66SRandall Stewart 340320abea66SRandall Stewart /* 340420abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 340520abea66SRandall Stewart * made to ensure the network interface doesn't go away until 340620abea66SRandall Stewart * all ratelimit connections are gone. The network interface 340720abea66SRandall Stewart * pointers compared below represent valid network interfaces, 340820abea66SRandall Stewart * except when comparing towards NULL. 340920abea66SRandall Stewart */ 341020abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 341120abea66SRandall Stewart error = 0; 341220abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 341320abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 341420abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 341520abea66SRandall Stewart error = 0; 341620abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 341720abea66SRandall Stewart /* 341820abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 341920abea66SRandall Stewart * to wait until there is a valid RSS hash before we 342020abea66SRandall Stewart * can proceed: 342120abea66SRandall Stewart */ 342220abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 342320abea66SRandall Stewart error = EAGAIN; 342420abea66SRandall Stewart } else { 342520abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 342620abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 342720abea66SRandall Stewart } 342820abea66SRandall Stewart } else { 342920abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 343020abea66SRandall Stewart } 343120abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 343220abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 343320abea66SRandall Stewart 343420abea66SRandall Stewart return (error); 343520abea66SRandall Stewart } 343620abea66SRandall Stewart 3437f3e7afe2SHans Petter Selasky /* 3438f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3439f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3440f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3441f3e7afe2SHans Petter Selasky */ 3442f3e7afe2SHans Petter Selasky void 3443f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3444f3e7afe2SHans Petter Selasky { 3445f3e7afe2SHans Petter Selasky struct socket *socket; 3446f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3447f3e7afe2SHans Petter Selasky bool did_upgrade; 3448f3e7afe2SHans Petter Selasky 3449f3e7afe2SHans Petter Selasky if (inp == NULL) 3450f3e7afe2SHans Petter Selasky return; 3451f3e7afe2SHans Petter Selasky 3452f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3453f3e7afe2SHans Petter Selasky if (socket == NULL) 3454f3e7afe2SHans Petter Selasky return; 3455f3e7afe2SHans Petter Selasky 3456f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3457f3e7afe2SHans Petter Selasky /* 3458f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3459f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3460f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3461f3e7afe2SHans Petter Selasky * write lock. 3462f3e7afe2SHans Petter Selasky */ 3463f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3464f3e7afe2SHans Petter Selasky return; 3465f3e7afe2SHans Petter Selasky did_upgrade = 1; 3466f3e7afe2SHans Petter Selasky } else { 3467f3e7afe2SHans Petter Selasky did_upgrade = 0; 3468f3e7afe2SHans Petter Selasky } 3469f3e7afe2SHans Petter Selasky 3470f3e7afe2SHans Petter Selasky /* 3471f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3472f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3473f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3474f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3475f3e7afe2SHans Petter Selasky */ 3476f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3477f3e7afe2SHans Petter Selasky 3478bab34d63SJohn Baldwin in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3479fb3bc596SJohn Baldwin 3480f3e7afe2SHans Petter Selasky if (did_upgrade) 3481f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3482f3e7afe2SHans Petter Selasky } 3483f3e7afe2SHans Petter Selasky 3484f3e7afe2SHans Petter Selasky /* 3485f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3486f3e7afe2SHans Petter Selasky */ 3487f3e7afe2SHans Petter Selasky void 3488f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3489f3e7afe2SHans Petter Selasky { 3490f3e7afe2SHans Petter Selasky bool did_upgrade; 3491f3e7afe2SHans Petter Selasky 3492f3e7afe2SHans Petter Selasky if (inp == NULL) 3493f3e7afe2SHans Petter Selasky return; 3494f3e7afe2SHans Petter Selasky 3495f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3496f3e7afe2SHans Petter Selasky return; 3497f3e7afe2SHans Petter Selasky 3498f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3499f3e7afe2SHans Petter Selasky /* 3500f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3501f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3502f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3503f3e7afe2SHans Petter Selasky * write lock. 3504f3e7afe2SHans Petter Selasky */ 3505f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3506f3e7afe2SHans Petter Selasky return; 3507f3e7afe2SHans Petter Selasky did_upgrade = 1; 3508f3e7afe2SHans Petter Selasky } else { 3509f3e7afe2SHans Petter Selasky did_upgrade = 0; 3510f3e7afe2SHans Petter Selasky } 3511f3e7afe2SHans Petter Selasky 3512f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3513f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3514f3e7afe2SHans Petter Selasky 3515f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3516f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3517f3e7afe2SHans Petter Selasky 3518f3e7afe2SHans Petter Selasky if (did_upgrade) 3519f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3520f3e7afe2SHans Petter Selasky } 352120abea66SRandall Stewart 3522903c4ee6SXin LI #ifdef INET 352320abea66SRandall Stewart static void 352420abea66SRandall Stewart rl_init(void *st) 352520abea66SRandall Stewart { 35261a714ff2SRandall Stewart rate_limit_new = counter_u64_alloc(M_WAITOK); 35271a714ff2SRandall Stewart rate_limit_chg = counter_u64_alloc(M_WAITOK); 352820abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 352920abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 353020abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 353120abea66SRandall Stewart } 353220abea66SRandall Stewart 353320abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3534903c4ee6SXin LI #endif 3535f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3536