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 * 382469dd60SGarrett Wollman * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 414b421e2dSMike Silbersack #include <sys/cdefs.h> 424b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 434b421e2dSMike Silbersack 44497057eeSRobert Watson #include "opt_ddb.h" 456a800098SYoshinobu Inoue #include "opt_ipsec.h" 46efc76f72SBjoern A. Zeeb #include "opt_inet.h" 47cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 48f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 490c325f53SAlexander V. Chernikov #include "opt_route.h" 507527624eSRobert Watson #include "opt_rss.h" 51cfa1ca9dSYoshinobu Inoue 52df8bae1dSRodney W. Grimes #include <sys/param.h> 53a0577692SGleb Smirnoff #include <sys/hash.h> 54df8bae1dSRodney W. Grimes #include <sys/systm.h> 55a0577692SGleb Smirnoff #include <sys/libkern.h> 56cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 57df8bae1dSRodney W. Grimes #include <sys/malloc.h> 58df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 59ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h> 60cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 61df8bae1dSRodney W. Grimes #include <sys/protosw.h> 623ee9c3c4SRandall Stewart #include <sys/smp.h> 63df8bae1dSRodney W. Grimes #include <sys/socket.h> 64df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 65f3e7afe2SHans Petter Selasky #include <sys/sockio.h> 66acd3428bSRobert Watson #include <sys/priv.h> 67df8bae1dSRodney W. Grimes #include <sys/proc.h> 6879bdc6e5SRobert Watson #include <sys/refcount.h> 6975c13541SPoul-Henning Kamp #include <sys/jail.h> 70101f9fc8SPeter Wemm #include <sys/kernel.h> 71101f9fc8SPeter Wemm #include <sys/sysctl.h> 728781d8e9SBruce Evans 73497057eeSRobert Watson #ifdef DDB 74497057eeSRobert Watson #include <ddb/ddb.h> 75497057eeSRobert Watson #endif 76497057eeSRobert Watson 7769c2d429SJeff Roberson #include <vm/uma.h> 78a034518aSAndrew Gallatin #include <vm/vm.h> 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes #include <net/if.h> 8176039bc8SGleb Smirnoff #include <net/if_var.h> 823d0d5b21SJustin Hibbits #include <net/if_private.h> 83cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 846d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h> 85df8bae1dSRodney W. Grimes #include <net/route.h> 86b2bdc62aSAdrian Chadd #include <net/rss_config.h> 87530c0060SRobert Watson #include <net/vnet.h> 88df8bae1dSRodney W. Grimes 8967107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 90df8bae1dSRodney W. Grimes #include <netinet/in.h> 91df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 920f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h> 9373bebcc5SGleb Smirnoff #include <netinet/tcp.h> 9459854ecfSHans Petter Selasky #ifdef INET 9559854ecfSHans Petter Selasky #include <netinet/in_var.h> 969ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h> 9759854ecfSHans Petter Selasky #endif 98df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 99cfa1ca9dSYoshinobu Inoue #ifdef INET6 100cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 101efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 10267107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 10367107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 104cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1059ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h> 10659854ecfSHans Petter Selasky #endif 107cfa1ca9dSYoshinobu Inoue 108fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 109b9234fafSSam Leffler 110aed55708SRobert Watson #include <security/mac/mac_framework.h> 111aed55708SRobert Watson 1121a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 1131a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 114db0ac6deSCy Schubert #define INP_FREED 0x00000200 /* See in_pcb.h. */ 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, 142a034518aSAndrew Gallatin int lookupflags, struct ifnet *ifp, 143a034518aSAndrew Gallatin uint8_t numa_domain); 14467107f45SBjoern A. Zeeb 145bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 146bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 147bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 148bbd42ad0SPeter Wemm 149bbd42ad0SPeter Wemm static int 15082d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 151bbd42ad0SPeter Wemm { 15230a4ab08SBruce Evans int error; 15330a4ab08SBruce Evans 154f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 15530a4ab08SBruce Evans if (error == 0) { 156603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 157603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 158603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 159603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 160603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 161603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 162bbd42ad0SPeter Wemm } 16330a4ab08SBruce Evans return (error); 164bbd42ad0SPeter Wemm } 165bbd42ad0SPeter Wemm 166bbd42ad0SPeter Wemm #undef RANGECHK 167bbd42ad0SPeter Wemm 1687029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, 1697029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1706472ac3dSEd Schouten "IP Ports"); 17133b3ac06SPeter Wemm 1726df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1737029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1747029da5cSPawel Biernacki &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", 1757029da5cSPawel Biernacki ""); 1766df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1777029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1787029da5cSPawel Biernacki &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", 1797029da5cSPawel Biernacki ""); 1806df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1817029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1827029da5cSPawel Biernacki &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", 1837029da5cSPawel Biernacki ""); 1846df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1857029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1867029da5cSPawel Biernacki &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", 1877029da5cSPawel Biernacki ""); 1886df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1897029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1907029da5cSPawel Biernacki &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", 1917029da5cSPawel Biernacki ""); 1926df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 1937029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1947029da5cSPawel Biernacki &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", 1957029da5cSPawel Biernacki ""); 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 1976df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 1986df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 200eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 2026df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 203eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 20420abea66SRandall Stewart 20520abea66SRandall Stewart #ifdef RATELIMIT 2061a714ff2SRandall Stewart counter_u64_t rate_limit_new; 2071a714ff2SRandall Stewart counter_u64_t rate_limit_chg; 20820abea66SRandall Stewart counter_u64_t rate_limit_active; 20920abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail; 21020abea66SRandall Stewart counter_u64_t rate_limit_set_ok; 21120abea66SRandall Stewart 2127029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 21320abea66SRandall Stewart "IP Rate Limiting"); 21420abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD, 21520abea66SRandall Stewart &rate_limit_active, "Active rate limited connections"); 21620abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD, 21720abea66SRandall Stewart &rate_limit_alloc_fail, "Rate limited connection failures"); 21820abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD, 21920abea66SRandall Stewart &rate_limit_set_ok, "Rate limited setting succeeded"); 2201a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD, 2211a714ff2SRandall Stewart &rate_limit_new, "Total Rate limit new attempts"); 2221a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD, 2231a714ff2SRandall Stewart &rate_limit_chg, "Total Rate limited change attempts"); 2241a714ff2SRandall Stewart 22520abea66SRandall Stewart #endif /* RATELIMIT */ 22620abea66SRandall Stewart 22783e521ecSBjoern A. Zeeb #endif /* INET */ 2280312fbe9SPoul-Henning Kamp 229a0577692SGleb Smirnoff VNET_DEFINE(uint32_t, in_pcbhashseed); 230a0577692SGleb Smirnoff static void 231a0577692SGleb Smirnoff in_pcbhashseed_init(void) 232a0577692SGleb Smirnoff { 233a0577692SGleb Smirnoff 234a0577692SGleb Smirnoff V_in_pcbhashseed = arc4random(); 235a0577692SGleb Smirnoff } 236a0577692SGleb Smirnoff VNET_SYSINIT(in_pcbhashseed_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 237a0577692SGleb Smirnoff in_pcbhashseed_init, 0); 238a0577692SGleb Smirnoff 2393ba34b07SGleb Smirnoff static void in_pcbremhash(struct inpcb *); 2403ba34b07SGleb Smirnoff 241c3229e05SDavid Greenman /* 242c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 243c3229e05SDavid Greenman * 244de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 245de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 246de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 247c3229e05SDavid Greenman */ 248c3229e05SDavid Greenman 2491a43cff9SSean Bruno static struct inpcblbgroup * 250d93ec8cbSMark Johnston in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, struct ucred *cred, 251d93ec8cbSMark Johnston u_char vflag, uint16_t port, const union in_dependaddr *addr, int size, 252a034518aSAndrew Gallatin uint8_t numa_domain) 2531a43cff9SSean Bruno { 2541a43cff9SSean Bruno struct inpcblbgroup *grp; 2551a43cff9SSean Bruno size_t bytes; 2561a43cff9SSean Bruno 2571a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 2581a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 259d93ec8cbSMark Johnston if (grp == NULL) 2601a43cff9SSean Bruno return (NULL); 261d93ec8cbSMark Johnston grp->il_cred = crhold(cred); 2621a43cff9SSean Bruno grp->il_vflag = vflag; 2631a43cff9SSean Bruno grp->il_lport = port; 264a034518aSAndrew Gallatin grp->il_numa_domain = numa_domain; 2651a43cff9SSean Bruno grp->il_dependladdr = *addr; 2661a43cff9SSean Bruno grp->il_inpsiz = size; 26754af3d0dSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 2681a43cff9SSean Bruno return (grp); 2691a43cff9SSean Bruno } 2701a43cff9SSean Bruno 2711a43cff9SSean Bruno static void 27254af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx) 27354af3d0dSMark Johnston { 27454af3d0dSMark Johnston struct inpcblbgroup *grp; 27554af3d0dSMark Johnston 27654af3d0dSMark Johnston grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 277d93ec8cbSMark Johnston crfree(grp->il_cred); 27854af3d0dSMark Johnston free(grp, M_PCB); 27954af3d0dSMark Johnston } 28054af3d0dSMark Johnston 28154af3d0dSMark Johnston static void 2821a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 2831a43cff9SSean Bruno { 2841a43cff9SSean Bruno 28554af3d0dSMark Johnston CK_LIST_REMOVE(grp, il_list); 2862a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx); 2871a43cff9SSean Bruno } 2881a43cff9SSean Bruno 2891a43cff9SSean Bruno static struct inpcblbgroup * 2901a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 2911a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 2921a43cff9SSean Bruno { 2931a43cff9SSean Bruno struct inpcblbgroup *grp; 2941a43cff9SSean Bruno int i; 2951a43cff9SSean Bruno 296d93ec8cbSMark Johnston grp = in_pcblbgroup_alloc(hdr, old_grp->il_cred, old_grp->il_vflag, 297a034518aSAndrew Gallatin old_grp->il_lport, &old_grp->il_dependladdr, size, 298a034518aSAndrew Gallatin old_grp->il_numa_domain); 29979ee680bSMark Johnston if (grp == NULL) 3001a43cff9SSean Bruno return (NULL); 3011a43cff9SSean Bruno 3021a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 3031a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 3041a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 3051a43cff9SSean Bruno 3061a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 3071a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 3081a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 3091a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 3101a43cff9SSean Bruno return (grp); 3111a43cff9SSean Bruno } 3121a43cff9SSean Bruno 3131a43cff9SSean Bruno /* 3141a43cff9SSean Bruno * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 3151a43cff9SSean Bruno * and shrink group if possible. 3161a43cff9SSean Bruno */ 3171a43cff9SSean Bruno static void 3181a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 3191a43cff9SSean Bruno int i) 3201a43cff9SSean Bruno { 32179ee680bSMark Johnston struct inpcblbgroup *grp, *new_grp; 3221a43cff9SSean Bruno 32379ee680bSMark Johnston grp = *grpp; 3241a43cff9SSean Bruno for (; i + 1 < grp->il_inpcnt; ++i) 3251a43cff9SSean Bruno grp->il_inp[i] = grp->il_inp[i + 1]; 3261a43cff9SSean Bruno grp->il_inpcnt--; 3271a43cff9SSean Bruno 3281a43cff9SSean Bruno if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 32979ee680bSMark Johnston grp->il_inpcnt <= grp->il_inpsiz / 4) { 3301a43cff9SSean Bruno /* Shrink this group. */ 33179ee680bSMark Johnston new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 33279ee680bSMark Johnston if (new_grp != NULL) 3331a43cff9SSean Bruno *grpp = new_grp; 3341a43cff9SSean Bruno } 3351a43cff9SSean Bruno } 3361a43cff9SSean Bruno 3371a43cff9SSean Bruno /* 3381a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 3391a43cff9SSean Bruno */ 3401a43cff9SSean Bruno static int 341a034518aSAndrew Gallatin in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain) 3421a43cff9SSean Bruno { 343a7026c7fSMark Johnston const static struct timeval interval = { 60, 0 }; 344a7026c7fSMark Johnston static struct timeval lastprint; 3451a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3461a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3471a43cff9SSean Bruno struct inpcblbgroup *grp; 34879ee680bSMark Johnston uint32_t idx; 3491a43cff9SSean Bruno 3501a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 3511a43cff9SSean Bruno 3521a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 3531a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 3541a43cff9SSean Bruno 3551a43cff9SSean Bruno #ifdef INET6 3561a43cff9SSean Bruno /* 3571a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 3581a43cff9SSean Bruno */ 3591a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 3601a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 3611a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 3621a43cff9SSean Bruno return (0); 3631a43cff9SSean Bruno } 3641a43cff9SSean Bruno #endif 3651a43cff9SSean Bruno 3669d2877fcSMark Johnston idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask); 36779ee680bSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 36854af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 369d93ec8cbSMark Johnston if (grp->il_cred->cr_prison == inp->inp_cred->cr_prison && 370d93ec8cbSMark Johnston grp->il_vflag == inp->inp_vflag && 3711a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 372a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain && 3731a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 3741a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 375d93ec8cbSMark Johnston sizeof(grp->il_dependladdr)) == 0) { 3761a43cff9SSean Bruno break; 3771a43cff9SSean Bruno } 378d93ec8cbSMark Johnston } 3791a43cff9SSean Bruno if (grp == NULL) { 3801a43cff9SSean Bruno /* Create new load balance group. */ 381d93ec8cbSMark Johnston grp = in_pcblbgroup_alloc(hdr, inp->inp_cred, inp->inp_vflag, 3821a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 383a034518aSAndrew Gallatin INPCBLBGROUP_SIZMIN, numa_domain); 38479ee680bSMark Johnston if (grp == NULL) 3851a43cff9SSean Bruno return (ENOBUFS); 3861a43cff9SSean Bruno } else if (grp->il_inpcnt == grp->il_inpsiz) { 3871a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 388a7026c7fSMark Johnston if (ratecheck(&lastprint, &interval)) 3891a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 3901a43cff9SSean Bruno ntohs(grp->il_lport)); 3911a43cff9SSean Bruno return (0); 3921a43cff9SSean Bruno } 3931a43cff9SSean Bruno 3941a43cff9SSean Bruno /* Expand this local group. */ 3951a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 39679ee680bSMark Johnston if (grp == NULL) 3971a43cff9SSean Bruno return (ENOBUFS); 3981a43cff9SSean Bruno } 3991a43cff9SSean Bruno 4001a43cff9SSean Bruno KASSERT(grp->il_inpcnt < grp->il_inpsiz, 40179ee680bSMark Johnston ("invalid local group size %d and count %d", grp->il_inpsiz, 40279ee680bSMark Johnston grp->il_inpcnt)); 4031a43cff9SSean Bruno 4041a43cff9SSean Bruno grp->il_inp[grp->il_inpcnt] = inp; 4051a43cff9SSean Bruno grp->il_inpcnt++; 4061a43cff9SSean Bruno return (0); 4071a43cff9SSean Bruno } 4081a43cff9SSean Bruno 4091a43cff9SSean Bruno /* 4101a43cff9SSean Bruno * Remove PCB from load balance group. 4111a43cff9SSean Bruno */ 4121a43cff9SSean Bruno static void 4131a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 4141a43cff9SSean Bruno { 4151a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 4161a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 4171a43cff9SSean Bruno struct inpcblbgroup *grp; 4181a43cff9SSean Bruno int i; 4191a43cff9SSean Bruno 4201a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 4211a43cff9SSean Bruno 4221a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 4231a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 4241a43cff9SSean Bruno 4251a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 4269d2877fcSMark Johnston INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 42754af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 4281a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 4291a43cff9SSean Bruno if (grp->il_inp[i] != inp) 4301a43cff9SSean Bruno continue; 4311a43cff9SSean Bruno 4321a43cff9SSean Bruno if (grp->il_inpcnt == 1) { 4331a43cff9SSean Bruno /* We are the last, free this local group. */ 4341a43cff9SSean Bruno in_pcblbgroup_free(grp); 4351a43cff9SSean Bruno } else { 4361a43cff9SSean Bruno /* Pull up inpcbs, shrink group if possible. */ 4371a43cff9SSean Bruno in_pcblbgroup_reorder(hdr, &grp, i); 4381a43cff9SSean Bruno } 4391a43cff9SSean Bruno return; 4401a43cff9SSean Bruno } 4411a43cff9SSean Bruno } 4421a43cff9SSean Bruno } 4431a43cff9SSean Bruno 444a034518aSAndrew Gallatin int 445a034518aSAndrew Gallatin in_pcblbgroup_numa(struct inpcb *inp, int arg) 446a034518aSAndrew Gallatin { 447a034518aSAndrew Gallatin struct inpcbinfo *pcbinfo; 448a034518aSAndrew Gallatin struct inpcblbgrouphead *hdr; 449a034518aSAndrew Gallatin struct inpcblbgroup *grp; 450a034518aSAndrew Gallatin int err, i; 451a034518aSAndrew Gallatin uint8_t numa_domain; 452a034518aSAndrew Gallatin 453a034518aSAndrew Gallatin switch (arg) { 454a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_NODOM: 455a034518aSAndrew Gallatin numa_domain = M_NODOM; 456a034518aSAndrew Gallatin break; 457a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_CURDOM: 458a034518aSAndrew Gallatin numa_domain = PCPU_GET(domain); 459a034518aSAndrew Gallatin break; 460a034518aSAndrew Gallatin default: 461a034518aSAndrew Gallatin if (arg < 0 || arg >= vm_ndomains) 462a034518aSAndrew Gallatin return (EINVAL); 463a034518aSAndrew Gallatin numa_domain = arg; 464a034518aSAndrew Gallatin } 465a034518aSAndrew Gallatin 466a034518aSAndrew Gallatin err = 0; 467a034518aSAndrew Gallatin pcbinfo = inp->inp_pcbinfo; 468a034518aSAndrew Gallatin INP_WLOCK_ASSERT(inp); 469a034518aSAndrew Gallatin INP_HASH_WLOCK(pcbinfo); 470a034518aSAndrew Gallatin hdr = &pcbinfo->ipi_lbgrouphashbase[ 471a034518aSAndrew Gallatin INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 472a034518aSAndrew Gallatin CK_LIST_FOREACH(grp, hdr, il_list) { 473a034518aSAndrew Gallatin for (i = 0; i < grp->il_inpcnt; ++i) { 474a034518aSAndrew Gallatin if (grp->il_inp[i] != inp) 475a034518aSAndrew Gallatin continue; 476a034518aSAndrew Gallatin 477a034518aSAndrew Gallatin if (grp->il_numa_domain == numa_domain) { 478a034518aSAndrew Gallatin goto abort_with_hash_wlock; 479a034518aSAndrew Gallatin } 480a034518aSAndrew Gallatin 481a034518aSAndrew Gallatin /* Remove it from the old group. */ 482a034518aSAndrew Gallatin in_pcbremlbgrouphash(inp); 483a034518aSAndrew Gallatin 484a034518aSAndrew Gallatin /* Add it to the new group based on numa domain. */ 485a034518aSAndrew Gallatin in_pcbinslbgrouphash(inp, numa_domain); 486a034518aSAndrew Gallatin goto abort_with_hash_wlock; 487a034518aSAndrew Gallatin } 488a034518aSAndrew Gallatin } 489a034518aSAndrew Gallatin err = ENOENT; 490a034518aSAndrew Gallatin abort_with_hash_wlock: 491a034518aSAndrew Gallatin INP_HASH_WUNLOCK(pcbinfo); 492a034518aSAndrew Gallatin return (err); 493a034518aSAndrew Gallatin } 494a034518aSAndrew Gallatin 495db0ac6deSCy Schubert /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 496db0ac6deSCy Schubert CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb))); 497db0ac6deSCy Schubert 498cc487c16SGleb Smirnoff /* 499fec8a8c7SGleb Smirnoff * Initialize an inpcbinfo - a per-VNET instance of connections db. 5009bcd427bSRobert Watson */ 5019bcd427bSRobert Watson void 502fec8a8c7SGleb Smirnoff in_pcbinfo_init(struct inpcbinfo *pcbinfo, struct inpcbstorage *pcbstor, 503fec8a8c7SGleb Smirnoff u_int hash_nelements, u_int porthash_nelements) 5049bcd427bSRobert Watson { 5059bcd427bSRobert Watson 506fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_lock, pcbstor->ips_infolock_name, NULL, MTX_DEF); 507fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_hash_lock, pcbstor->ips_hashlock_name, 508fec8a8c7SGleb Smirnoff NULL, MTX_DEF); 5099bcd427bSRobert Watson #ifdef VIMAGE 5109bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 5119bcd427bSRobert Watson #endif 512db0ac6deSCy Schubert CK_LIST_INIT(&pcbinfo->ipi_listhead); 513fa046d87SRobert Watson pcbinfo->ipi_count = 0; 5149bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 5159bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 516db0ac6deSCy Schubert porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 5179bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 5189bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 5199d2877fcSMark Johnston pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 5201a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 521fec8a8c7SGleb Smirnoff pcbinfo->ipi_zone = pcbstor->ips_zone; 522fec8a8c7SGleb Smirnoff pcbinfo->ipi_portzone = pcbstor->ips_portzone; 523db0ac6deSCy Schubert pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone); 5249bcd427bSRobert Watson } 5259bcd427bSRobert Watson 5269bcd427bSRobert Watson /* 5279bcd427bSRobert Watson * Destroy an inpcbinfo. 5289bcd427bSRobert Watson */ 5299bcd427bSRobert Watson void 5309bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 5319bcd427bSRobert Watson { 5329bcd427bSRobert Watson 533fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 534fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 535fa046d87SRobert Watson 5369bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 5379bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 5389bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 5391a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 5401a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 541db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_hash_lock); 542db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_lock); 5439bcd427bSRobert Watson } 5449bcd427bSRobert Watson 5459bcd427bSRobert Watson /* 546fec8a8c7SGleb Smirnoff * Initialize a pcbstorage - per protocol zones to allocate inpcbs. 547fec8a8c7SGleb Smirnoff */ 548fec8a8c7SGleb Smirnoff static void inpcb_dtor(void *, int, void *); 549fec8a8c7SGleb Smirnoff static void inpcb_fini(void *, int); 550fec8a8c7SGleb Smirnoff void 551fec8a8c7SGleb Smirnoff in_pcbstorage_init(void *arg) 552fec8a8c7SGleb Smirnoff { 553fec8a8c7SGleb Smirnoff struct inpcbstorage *pcbstor = arg; 554fec8a8c7SGleb Smirnoff 555fec8a8c7SGleb Smirnoff pcbstor->ips_zone = uma_zcreate(pcbstor->ips_zone_name, 5560aa120d5SGleb Smirnoff pcbstor->ips_size, NULL, inpcb_dtor, pcbstor->ips_pcbinit, 557c4a4b263SAndrew Gallatin inpcb_fini, UMA_ALIGN_CACHE, UMA_ZONE_SMR); 558fec8a8c7SGleb Smirnoff pcbstor->ips_portzone = uma_zcreate(pcbstor->ips_portzone_name, 559fec8a8c7SGleb Smirnoff sizeof(struct inpcbport), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 560fec8a8c7SGleb Smirnoff uma_zone_set_smr(pcbstor->ips_portzone, 561fec8a8c7SGleb Smirnoff uma_zone_get_smr(pcbstor->ips_zone)); 562fec8a8c7SGleb Smirnoff } 563fec8a8c7SGleb Smirnoff 564fec8a8c7SGleb Smirnoff /* 565fec8a8c7SGleb Smirnoff * Destroy a pcbstorage - used by unloadable protocols. 566fec8a8c7SGleb Smirnoff */ 567fec8a8c7SGleb Smirnoff void 568fec8a8c7SGleb Smirnoff in_pcbstorage_destroy(void *arg) 569fec8a8c7SGleb Smirnoff { 570fec8a8c7SGleb Smirnoff struct inpcbstorage *pcbstor = arg; 571fec8a8c7SGleb Smirnoff 572fec8a8c7SGleb Smirnoff uma_zdestroy(pcbstor->ips_zone); 573fec8a8c7SGleb Smirnoff uma_zdestroy(pcbstor->ips_portzone); 574fec8a8c7SGleb Smirnoff } 575fec8a8c7SGleb Smirnoff 576fec8a8c7SGleb Smirnoff /* 577c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 578d915b280SStephan Uphoff * On success return with the PCB locked. 579c3229e05SDavid Greenman */ 580df8bae1dSRodney W. Grimes int 581d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 582df8bae1dSRodney W. Grimes { 583136d4f1cSRobert Watson struct inpcb *inp; 584fe5324acSJohn Baldwin #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 58513cf67f3SHajimu UMEMOTO int error; 586fe5324acSJohn Baldwin #endif 587a557af22SRobert Watson 588db0ac6deSCy Schubert inp = uma_zalloc_smr(pcbinfo->ipi_zone, M_NOWAIT); 589df8bae1dSRodney W. Grimes if (inp == NULL) 590df8bae1dSRodney W. Grimes return (ENOBUFS); 5916a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 59250575ce1SAndrew Gallatin #ifdef NUMA 59350575ce1SAndrew Gallatin inp->inp_numa_domain = M_NODOM; 59450575ce1SAndrew Gallatin #endif 59515bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 596df8bae1dSRodney W. Grimes inp->inp_socket = so; 59786d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 5988b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 599a557af22SRobert Watson #ifdef MAC 60030d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 601a557af22SRobert Watson if (error != 0) 602a557af22SRobert Watson goto out; 60330d239bcSRobert Watson mac_inpcb_create(so, inp); 604a557af22SRobert Watson #endif 605fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 606fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 6070bffde27SRobert Watson if (error != 0) { 6080bffde27SRobert Watson #ifdef MAC 6090bffde27SRobert Watson mac_inpcb_destroy(inp); 6100bffde27SRobert Watson #endif 611a557af22SRobert Watson goto out; 6120bffde27SRobert Watson } 613b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 614e3fd5ffdSRobert Watson #ifdef INET6 615340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 616c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV6PROTO | INP_IPV6; 617603724d3SBjoern A. Zeeb if (V_ip6_v6only) 61833841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 619c7a62c92SGleb Smirnoff #ifdef INET 620c7a62c92SGleb Smirnoff else 621c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 622c7a62c92SGleb Smirnoff #endif 623603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 62433841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 625c7a62c92SGleb Smirnoff inp->in6p_hops = -1; /* use kernel default */ 626c7a62c92SGleb Smirnoff } 627c7a62c92SGleb Smirnoff #endif 628c7a62c92SGleb Smirnoff #if defined(INET) && defined(INET6) 629c7a62c92SGleb Smirnoff else 630c7a62c92SGleb Smirnoff #endif 631c7a62c92SGleb Smirnoff #ifdef INET 632c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 63333841545SHajimu UMEMOTO #endif 6348c1960d5SMike Karels /* 6358c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 6368c1960d5SMike Karels * to be cleaned up. 6378c1960d5SMike Karels */ 6388c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 639db0ac6deSCy Schubert refcount_init(&inp->inp_refcount, 1); /* Reference from socket. */ 640db0ac6deSCy Schubert INP_WLOCK(inp); 641db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 642db0ac6deSCy Schubert pcbinfo->ipi_count++; 643db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 644db0ac6deSCy Schubert CK_LIST_INSERT_HEAD(&pcbinfo->ipi_listhead, inp, inp_list); 645db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 646db0ac6deSCy Schubert so->so_pcb = inp; 647db0ac6deSCy Schubert 648db0ac6deSCy Schubert return (0); 649db0ac6deSCy Schubert 6507a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 651a557af22SRobert Watson out: 652db0ac6deSCy Schubert uma_zfree_smr(pcbinfo->ipi_zone, inp); 653266f97b5SCy Schubert return (error); 654db0ac6deSCy Schubert #endif 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes 65767107f45SBjoern A. Zeeb #ifdef INET 658df8bae1dSRodney W. Grimes int 659136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 660df8bae1dSRodney W. Grimes { 6614b932371SIan Dowse int anonport, error; 6624b932371SIan Dowse 663f161d294SMark Johnston KASSERT(nam == NULL || nam->sa_family == AF_INET, 664f161d294SMark Johnston ("%s: invalid address family for %p", __func__, nam)); 665f161d294SMark Johnston KASSERT(nam == NULL || nam->sa_len == sizeof(struct sockaddr_in), 666f161d294SMark Johnston ("%s: invalid address length for %p", __func__, nam)); 6678501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 668fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 66959daba27SSam Leffler 6704b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 6714b932371SIan Dowse return (EINVAL); 6725cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 6734b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 674b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 6754b932371SIan Dowse if (error) 6764b932371SIan Dowse return (error); 6774b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 6784b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 6794b932371SIan Dowse inp->inp_lport = 0; 6804b932371SIan Dowse return (EAGAIN); 6814b932371SIan Dowse } 6824b932371SIan Dowse if (anonport) 6834b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 6844b932371SIan Dowse return (0); 6854b932371SIan Dowse } 68667107f45SBjoern A. Zeeb #endif 6874b932371SIan Dowse 688efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 68925102351SMike Karels /* 69025102351SMike Karels * Assign a local port like in_pcb_lport(), but also used with connect() 69125102351SMike Karels * and a foreign address and port. If fsa is non-NULL, choose a local port 69225102351SMike Karels * that is unused with those, otherwise one that is completely unused. 6932fdbcbeaSMike Karels * lsa can be NULL for IPv6. 69425102351SMike Karels */ 695efc76f72SBjoern A. Zeeb int 69625102351SMike Karels in_pcb_lport_dest(struct inpcb *inp, struct sockaddr *lsa, u_short *lportp, 69725102351SMike Karels struct sockaddr *fsa, u_short fport, struct ucred *cred, int lookupflags) 698efc76f72SBjoern A. Zeeb { 699efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 700efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 701efc76f72SBjoern A. Zeeb unsigned short *lastport; 70219acc506SGleb Smirnoff int count, error; 703efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 704efc76f72SBjoern A. Zeeb #ifdef INET 70525102351SMike Karels struct in_addr laddr, faddr; 70625102351SMike Karels #endif 70725102351SMike Karels #ifdef INET6 70825102351SMike Karels struct in6_addr *laddr6, *faddr6; 709efc76f72SBjoern A. Zeeb #endif 710efc76f72SBjoern A. Zeeb 711efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 712efc76f72SBjoern A. Zeeb 713efc76f72SBjoern A. Zeeb /* 714efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 715efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 716efc76f72SBjoern A. Zeeb */ 717efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 718fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 719efc76f72SBjoern A. Zeeb 720efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 721efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 722efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 723efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 724efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 725cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 726efc76f72SBjoern A. Zeeb if (error) 727efc76f72SBjoern A. Zeeb return (error); 728efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 729efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 730efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 731efc76f72SBjoern A. Zeeb } else { 732efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 733efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 734efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 735efc76f72SBjoern A. Zeeb } 73619acc506SGleb Smirnoff 737efc76f72SBjoern A. Zeeb /* 738efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 739efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 740efc76f72SBjoern A. Zeeb * code path implementing all logic. 741efc76f72SBjoern A. Zeeb */ 742efc76f72SBjoern A. Zeeb if (first > last) { 743efc76f72SBjoern A. Zeeb aux = first; 744efc76f72SBjoern A. Zeeb first = last; 745efc76f72SBjoern A. Zeeb last = aux; 746efc76f72SBjoern A. Zeeb } 747efc76f72SBjoern A. Zeeb 748efc76f72SBjoern A. Zeeb #ifdef INET 749637f317cSMike Karels laddr.s_addr = INADDR_ANY; /* used by INET6+INET below too */ 7504d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 7512fdbcbeaSMike Karels if (lsa != NULL) 75225102351SMike Karels laddr = ((struct sockaddr_in *)lsa)->sin_addr; 75325102351SMike Karels if (fsa != NULL) 75425102351SMike Karels faddr = ((struct sockaddr_in *)fsa)->sin_addr; 755efc76f72SBjoern A. Zeeb } 756efc76f72SBjoern A. Zeeb #endif 75725102351SMike Karels #ifdef INET6 7582fdbcbeaSMike Karels laddr6 = NULL; 7592fdbcbeaSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 7602fdbcbeaSMike Karels if (lsa != NULL) 76125102351SMike Karels laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 76225102351SMike Karels if (fsa != NULL) 76325102351SMike Karels faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 76425102351SMike Karels } 76525102351SMike Karels #endif 76625102351SMike Karels 76725102351SMike Karels tmpinp = NULL; 768efc76f72SBjoern A. Zeeb lport = *lportp; 769efc76f72SBjoern A. Zeeb 77019acc506SGleb Smirnoff if (V_ipport_randomized) 771efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 772efc76f72SBjoern A. Zeeb 773efc76f72SBjoern A. Zeeb count = last - first; 774efc76f72SBjoern A. Zeeb 775efc76f72SBjoern A. Zeeb do { 776efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 777efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 778efc76f72SBjoern A. Zeeb ++*lastport; 779efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 780efc76f72SBjoern A. Zeeb *lastport = first; 781efc76f72SBjoern A. Zeeb lport = htons(*lastport); 782efc76f72SBjoern A. Zeeb 78325102351SMike Karels if (fsa != NULL) { 78425102351SMike Karels #ifdef INET 78525102351SMike Karels if (lsa->sa_family == AF_INET) { 78625102351SMike Karels tmpinp = in_pcblookup_hash_locked(pcbinfo, 78725102351SMike Karels faddr, fport, laddr, lport, lookupflags, 788a034518aSAndrew Gallatin NULL, M_NODOM); 78925102351SMike Karels } 79025102351SMike Karels #endif 79125102351SMike Karels #ifdef INET6 79225102351SMike Karels if (lsa->sa_family == AF_INET6) { 79325102351SMike Karels tmpinp = in6_pcblookup_hash_locked(pcbinfo, 79425102351SMike Karels faddr6, fport, laddr6, lport, lookupflags, 795a034518aSAndrew Gallatin NULL, M_NODOM); 79625102351SMike Karels } 79725102351SMike Karels #endif 79825102351SMike Karels } else { 799efc76f72SBjoern A. Zeeb #ifdef INET6 800637f317cSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 801efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 80268e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 803637f317cSMike Karels #ifdef INET 804637f317cSMike Karels if (tmpinp == NULL && 805637f317cSMike Karels (inp->inp_vflag & INP_IPV4)) 806637f317cSMike Karels tmpinp = in_pcblookup_local(pcbinfo, 807637f317cSMike Karels laddr, lport, lookupflags, cred); 808637f317cSMike Karels #endif 809637f317cSMike Karels } 810efc76f72SBjoern A. Zeeb #endif 811efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 812efc76f72SBjoern A. Zeeb else 813efc76f72SBjoern A. Zeeb #endif 814efc76f72SBjoern A. Zeeb #ifdef INET 815efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 81668e0d7e0SRobert Watson lport, lookupflags, cred); 817efc76f72SBjoern A. Zeeb #endif 81825102351SMike Karels } 819efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 820efc76f72SBjoern A. Zeeb 821efc76f72SBjoern A. Zeeb *lportp = lport; 822efc76f72SBjoern A. Zeeb 823efc76f72SBjoern A. Zeeb return (0); 824efc76f72SBjoern A. Zeeb } 825efdf104bSMikolaj Golub 826efdf104bSMikolaj Golub /* 82725102351SMike Karels * Select a local port (number) to use. 82825102351SMike Karels */ 82925102351SMike Karels int 83025102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 83125102351SMike Karels struct ucred *cred, int lookupflags) 83225102351SMike Karels { 83325102351SMike Karels struct sockaddr_in laddr; 83425102351SMike Karels 83525102351SMike Karels if (laddrp) { 83625102351SMike Karels bzero(&laddr, sizeof(laddr)); 83725102351SMike Karels laddr.sin_family = AF_INET; 83825102351SMike Karels laddr.sin_addr = *laddrp; 83925102351SMike Karels } 84025102351SMike Karels return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 84125102351SMike Karels NULL, lportp, NULL, 0, cred, lookupflags)); 84225102351SMike Karels } 84325102351SMike Karels 84425102351SMike Karels /* 845efdf104bSMikolaj Golub * Return cached socket options. 846efdf104bSMikolaj Golub */ 8471a43cff9SSean Bruno int 848efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 849efdf104bSMikolaj Golub { 8501a43cff9SSean Bruno int so_options; 851efdf104bSMikolaj Golub 852efdf104bSMikolaj Golub so_options = 0; 853efdf104bSMikolaj Golub 8541a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 8551a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 856efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 857efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 858efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 859efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 860efdf104bSMikolaj Golub return (so_options); 861efdf104bSMikolaj Golub } 862efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 863efc76f72SBjoern A. Zeeb 8644b932371SIan Dowse /* 8650a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 8660a100a6fSAdrian Chadd * 8670a100a6fSAdrian Chadd * ni points to the new inp. 868942e8cabSGordon Bergling * oi points to the existing inp. 8690a100a6fSAdrian Chadd * 8700a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 8710a100a6fSAdrian Chadd * whether the credentials match. 8720a100a6fSAdrian Chadd */ 873d5bb8bd3SAdrian Chadd int 8740a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 8750a100a6fSAdrian Chadd { 8760a100a6fSAdrian Chadd /* Check permissions match */ 8770a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8780a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 8790a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 8800a100a6fSAdrian Chadd return (0); 8810a100a6fSAdrian Chadd 8820a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 8830a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8840a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 8850a100a6fSAdrian Chadd return (0); 8860a100a6fSAdrian Chadd 8870a100a6fSAdrian Chadd /* 8880a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 8890a100a6fSAdrian Chadd * it is and it matches the checks. 8900a100a6fSAdrian Chadd */ 8910a100a6fSAdrian Chadd return (1); 8920a100a6fSAdrian Chadd } 8930a100a6fSAdrian Chadd 894d5bb8bd3SAdrian Chadd #ifdef INET 8950a100a6fSAdrian Chadd /* 8964b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 8974b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 8984b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 8994b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 9004b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 9014b932371SIan Dowse * 9024b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 9034b932371SIan Dowse */ 9044b932371SIan Dowse int 905136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 906136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 9074b932371SIan Dowse { 9084b932371SIan Dowse struct socket *so = inp->inp_socket; 90915bd2b43SDavid Greenman struct sockaddr_in *sin; 910c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 9114b932371SIan Dowse struct in_addr laddr; 912df8bae1dSRodney W. Grimes u_short lport = 0; 91368e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 914413628a7SBjoern A. Zeeb int error; 915df8bae1dSRodney W. Grimes 9168501a69cSRobert Watson /* 9171a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 9181a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 9191a43cff9SSean Bruno */ 9201a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 9211a43cff9SSean Bruno 9221a43cff9SSean Bruno /* 923fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 9248501a69cSRobert Watson */ 92559daba27SSam Leffler INP_LOCK_ASSERT(inp); 926fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 92759daba27SSam Leffler 9284b932371SIan Dowse laddr.s_addr = *laddrp; 9294b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 930df8bae1dSRodney W. Grimes return (EINVAL); 9311a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 93268e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 9334616026fSErmal Luçi if (nam == NULL) { 9347c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 9357c2f3cb9SJamie Gritton return (error); 9367c2f3cb9SJamie Gritton } else { 93757bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 938f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 939f161d294SMark Johnston ("%s: invalid family for address %p", __func__, sin)); 940f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 941f161d294SMark Johnston ("%s: invalid length for address %p", __func__, sin)); 942f161d294SMark Johnston 943b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 944b89e82ddSJamie Gritton if (error) 945b89e82ddSJamie Gritton return (error); 9464b932371SIan Dowse if (sin->sin_port != *lportp) { 9474b932371SIan Dowse /* Don't allow the port to change. */ 9484b932371SIan Dowse if (*lportp != 0) 9494b932371SIan Dowse return (EINVAL); 950df8bae1dSRodney W. Grimes lport = sin->sin_port; 9514b932371SIan Dowse } 9524b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 953df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 954df8bae1dSRodney W. Grimes /* 955df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 956df8bae1dSRodney W. Grimes * allow complete duplication of binding if 957df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 958df8bae1dSRodney W. Grimes * and a multicast address is bound on both 959df8bae1dSRodney W. Grimes * new and duplicated sockets. 960df8bae1dSRodney W. Grimes */ 961f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 962df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 9631a43cff9SSean Bruno /* 9641a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 9651a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 9661a43cff9SSean Bruno */ 9671a43cff9SSean Bruno if ((so->so_options & 9681a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 9691a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 970df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 971df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 97283103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 9734209e01aSAdrian Chadd /* 9744209e01aSAdrian Chadd * Is the address a local IP address? 975f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 9768696873dSAdrian Chadd * to any endpoint address, local or not. 9774209e01aSAdrian Chadd */ 978f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 9798896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 980df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 981df8bae1dSRodney W. Grimes } 9824b932371SIan Dowse laddr = sin->sin_addr; 983df8bae1dSRodney W. Grimes if (lport) { 984df8bae1dSRodney W. Grimes struct inpcb *t; 985ae0e7143SRobert Watson 986df8bae1dSRodney W. Grimes /* GROSS */ 987603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 988603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 989cc426dd3SMateusz Guzik priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 9902469dd60SGarrett Wollman return (EACCES); 991835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 992cc426dd3SMateusz Guzik priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 993078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 994413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 995340c35deSJonathan Lemon /* 996340c35deSJonathan Lemon * XXX 997340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 998340c35deSJonathan Lemon */ 9994cc20ab1SSeigo Tanimura if (t && 10000a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 10014658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 10024658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 10034cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 100452b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 10051a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 10061a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 100786d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 100886d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 10094049a042SGuido van Rooij return (EADDRINUSE); 10100a100a6fSAdrian Chadd 10110a100a6fSAdrian Chadd /* 10120a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 10130a100a6fSAdrian Chadd * the credentials need to match and the 10140a100a6fSAdrian Chadd * original socket also has to have been bound 10150a100a6fSAdrian Chadd * with BINDMULTI. 10160a100a6fSAdrian Chadd */ 10170a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 10180a100a6fSAdrian Chadd return (EADDRINUSE); 10194049a042SGuido van Rooij } 1020c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 102168e0d7e0SRobert Watson lport, lookupflags, cred); 10220d744519SGleb Smirnoff if (t && ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 10231a43cff9SSean Bruno (reuseport & inp_so_options(t)) == 0 && 10241a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 1025e3fd5ffdSRobert Watson #ifdef INET6 102633841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 1027cfa1ca9dSYoshinobu Inoue INADDR_ANY || 1028cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 1029cfa1ca9dSYoshinobu Inoue INADDR_ANY || 1030fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 1031fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 1032e3fd5ffdSRobert Watson #endif 1033df8bae1dSRodney W. Grimes return (EADDRINUSE); 10340a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 10350a100a6fSAdrian Chadd return (EADDRINUSE); 1036df8bae1dSRodney W. Grimes } 1037cfa1ca9dSYoshinobu Inoue } 1038df8bae1dSRodney W. Grimes } 10394b932371SIan Dowse if (*lportp != 0) 10404b932371SIan Dowse lport = *lportp; 104133b3ac06SPeter Wemm if (lport == 0) { 10424616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 1043efc76f72SBjoern A. Zeeb if (error != 0) 1044efc76f72SBjoern A. Zeeb return (error); 104533b3ac06SPeter Wemm } 10464b932371SIan Dowse *laddrp = laddr.s_addr; 10474b932371SIan Dowse *lportp = lport; 1048df8bae1dSRodney W. Grimes return (0); 1049df8bae1dSRodney W. Grimes } 1050df8bae1dSRodney W. Grimes 1051999f1343SGarrett Wollman /* 10525200e00eSIan Dowse * Connect from a socket to a specified address. 10535200e00eSIan Dowse * Both address and port must be specified in argument sin. 10545200e00eSIan Dowse * If don't have a local address for this socket yet, 10555200e00eSIan Dowse * then pick one. 1056999f1343SGarrett Wollman */ 1057999f1343SGarrett Wollman int 1058a9d22cceSGleb Smirnoff in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred, 1059db0ac6deSCy Schubert bool rehash) 1060999f1343SGarrett Wollman { 10615200e00eSIan Dowse u_short lport, fport; 10625200e00eSIan Dowse in_addr_t laddr, faddr; 10635200e00eSIan Dowse int anonport, error; 1064df8bae1dSRodney W. Grimes 10658501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1066fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 106727f74fd0SRobert Watson 10685200e00eSIan Dowse lport = inp->inp_lport; 10695200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 10705200e00eSIan Dowse anonport = (lport == 0); 1071a9d22cceSGleb Smirnoff error = in_pcbconnect_setup(inp, sin, &laddr, &lport, &faddr, &fport, 10729e46ff4dSGleb Smirnoff cred); 10735200e00eSIan Dowse if (error) 10745200e00eSIan Dowse return (error); 10755200e00eSIan Dowse 10765200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 10775200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 1078fe1274eeSMichael Tuexen KASSERT(rehash == true, 1079fe1274eeSMichael Tuexen ("Rehashing required for unbound inps")); 10805200e00eSIan Dowse inp->inp_lport = lport; 10815200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10825200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 10835200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 10845200e00eSIan Dowse inp->inp_lport = 0; 10855200e00eSIan Dowse return (EAGAIN); 10865200e00eSIan Dowse } 10875200e00eSIan Dowse } 10885200e00eSIan Dowse 10895200e00eSIan Dowse /* Commit the remaining changes. */ 10905200e00eSIan Dowse inp->inp_lport = lport; 10915200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10925200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 10935200e00eSIan Dowse inp->inp_fport = fport; 1094fe1274eeSMichael Tuexen if (rehash) { 1095db0ac6deSCy Schubert in_pcbrehash(inp); 1096fe1274eeSMichael Tuexen } else { 1097db0ac6deSCy Schubert in_pcbinshash(inp); 1098fe1274eeSMichael Tuexen } 10992cb64cb2SGeorge V. Neville-Neil 11005200e00eSIan Dowse if (anonport) 11015200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 11025200e00eSIan Dowse return (0); 11035200e00eSIan Dowse } 11045200e00eSIan Dowse 11055200e00eSIan Dowse /* 11060895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 11070895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 11080895aec3SBjoern A. Zeeb */ 1109ae190832SSteven Hartland int 11100895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 11110895aec3SBjoern A. Zeeb struct ucred *cred) 11120895aec3SBjoern A. Zeeb { 11130895aec3SBjoern A. Zeeb struct ifaddr *ifa; 11140895aec3SBjoern A. Zeeb struct sockaddr *sa; 11159ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 11169ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 11170895aec3SBjoern A. Zeeb int error; 11180895aec3SBjoern A. Zeeb 1119c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1120413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1121ac1750ddSMark Johnston 1122592bcae8SBjoern A. Zeeb /* 1123592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1124592bcae8SBjoern A. Zeeb * if requested. 1125592bcae8SBjoern A. Zeeb */ 1126ac1750ddSMark Johnston if (!prison_saddrsel_ip4(cred, laddr)) 1127592bcae8SBjoern A. Zeeb return (0); 1128592bcae8SBjoern A. Zeeb 11290895aec3SBjoern A. Zeeb error = 0; 11300895aec3SBjoern A. Zeeb 11319ac7c6cfSAlexander V. Chernikov nh = NULL; 11329ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 11339ac7c6cfSAlexander V. Chernikov sin = &dst; 11340895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 11350895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 11360895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 11370895aec3SBjoern A. Zeeb 11380895aec3SBjoern A. Zeeb /* 11390895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 11400895aec3SBjoern A. Zeeb * else punt. 11410895aec3SBjoern A. Zeeb * 11420895aec3SBjoern A. Zeeb * Find out route to destination. 11430895aec3SBjoern A. Zeeb */ 11440895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 11459ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 11469ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 11470895aec3SBjoern A. Zeeb 11480895aec3SBjoern A. Zeeb /* 11490895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 11500895aec3SBjoern A. Zeeb * the outgoing interface. 11510895aec3SBjoern A. Zeeb * 11520895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 11530895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 11540895aec3SBjoern A. Zeeb * the source address from. 11550895aec3SBjoern A. Zeeb */ 11569ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 11578c0fec80SRobert Watson struct in_ifaddr *ia; 11580895aec3SBjoern A. Zeeb struct ifnet *ifp; 11590895aec3SBjoern A. Zeeb 11604f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 116158a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11624f6c66ccSMatt Macy if (ia == NULL) { 11634f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 116458a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11654f6c66ccSMatt Macy } 11660895aec3SBjoern A. Zeeb if (ia == NULL) { 11670895aec3SBjoern A. Zeeb error = ENETUNREACH; 11680895aec3SBjoern A. Zeeb goto done; 11690895aec3SBjoern A. Zeeb } 11700895aec3SBjoern A. Zeeb 1171ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 11720895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11730895aec3SBjoern A. Zeeb goto done; 11740895aec3SBjoern A. Zeeb } 11750895aec3SBjoern A. Zeeb 11760895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 11770895aec3SBjoern A. Zeeb ia = NULL; 1178d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11790895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11800895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11810895aec3SBjoern A. Zeeb continue; 11820895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1183b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11840895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11850895aec3SBjoern A. Zeeb break; 11860895aec3SBjoern A. Zeeb } 11870895aec3SBjoern A. Zeeb } 11880895aec3SBjoern A. Zeeb if (ia != NULL) { 11890895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11900895aec3SBjoern A. Zeeb goto done; 11910895aec3SBjoern A. Zeeb } 11920895aec3SBjoern A. Zeeb 11930895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1194b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11950895aec3SBjoern A. Zeeb goto done; 11960895aec3SBjoern A. Zeeb } 11970895aec3SBjoern A. Zeeb 11980895aec3SBjoern A. Zeeb /* 11990895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 12000895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 12010895aec3SBjoern A. Zeeb * In case of jails do those three steps: 12020895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 12030895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 12040895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 12050895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 12060895aec3SBjoern A. Zeeb */ 12079ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 12088c0fec80SRobert Watson struct in_ifaddr *ia; 12099317b04eSRobert Watson struct ifnet *ifp; 12100895aec3SBjoern A. Zeeb 12110895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 1212ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 12139ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 12140895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12150895aec3SBjoern A. Zeeb goto done; 12160895aec3SBjoern A. Zeeb } 12170895aec3SBjoern A. Zeeb 12180895aec3SBjoern A. Zeeb /* Jailed. */ 12190895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 12209ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1221b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12229ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 12230895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12240895aec3SBjoern A. Zeeb goto done; 12250895aec3SBjoern A. Zeeb } 12260895aec3SBjoern A. Zeeb 12270895aec3SBjoern A. Zeeb /* 12280895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 12290895aec3SBjoern A. Zeeb * belonging to this jail. 12300895aec3SBjoern A. Zeeb */ 12318c0fec80SRobert Watson ia = NULL; 12329ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1233d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12340895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12350895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12360895aec3SBjoern A. Zeeb continue; 12370895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1238b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12390895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12400895aec3SBjoern A. Zeeb break; 12410895aec3SBjoern A. Zeeb } 12420895aec3SBjoern A. Zeeb } 12430895aec3SBjoern A. Zeeb if (ia != NULL) { 12440895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12450895aec3SBjoern A. Zeeb goto done; 12460895aec3SBjoern A. Zeeb } 12470895aec3SBjoern A. Zeeb 12480895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1249b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12500895aec3SBjoern A. Zeeb goto done; 12510895aec3SBjoern A. Zeeb } 12520895aec3SBjoern A. Zeeb 12530895aec3SBjoern A. Zeeb /* 12540895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 12550895aec3SBjoern A. Zeeb * to ourselves is here. 12560895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 12570895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 12580895aec3SBjoern A. Zeeb * a loopback interface. 12590895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 12600895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 12610895aec3SBjoern A. Zeeb */ 12629ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 12638c0fec80SRobert Watson struct in_ifaddr *ia; 12640895aec3SBjoern A. Zeeb 12659ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 126658a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12670895aec3SBjoern A. Zeeb if (ia == NULL) 12689ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 126958a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1270f0bb05fcSQing Li if (ia == NULL) 12719ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 12720895aec3SBjoern A. Zeeb 1273ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 12740895aec3SBjoern A. Zeeb if (ia == NULL) { 12750895aec3SBjoern A. Zeeb error = ENETUNREACH; 12760895aec3SBjoern A. Zeeb goto done; 12770895aec3SBjoern A. Zeeb } 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 /* Jailed. */ 12830895aec3SBjoern A. Zeeb if (ia != NULL) { 12840895aec3SBjoern A. Zeeb struct ifnet *ifp; 12850895aec3SBjoern A. Zeeb 12860895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12870895aec3SBjoern A. Zeeb ia = NULL; 1288d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12890895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12900895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12910895aec3SBjoern A. Zeeb continue; 12920895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1293b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1294b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 12950895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12960895aec3SBjoern A. Zeeb break; 12970895aec3SBjoern A. Zeeb } 12980895aec3SBjoern A. Zeeb } 12990895aec3SBjoern A. Zeeb if (ia != NULL) { 13000895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13010895aec3SBjoern A. Zeeb goto done; 13020895aec3SBjoern A. Zeeb } 13030895aec3SBjoern A. Zeeb } 13040895aec3SBjoern A. Zeeb 13050895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1306b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 13070895aec3SBjoern A. Zeeb goto done; 13080895aec3SBjoern A. Zeeb } 13090895aec3SBjoern A. Zeeb 13100895aec3SBjoern A. Zeeb done: 13110895aec3SBjoern A. Zeeb return (error); 13120895aec3SBjoern A. Zeeb } 13130895aec3SBjoern A. Zeeb 13140895aec3SBjoern A. Zeeb /* 13155200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 13165200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 13175200e00eSIan Dowse * address and port for the PCB; these are updated to the values 13185200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 13195200e00eSIan Dowse * the connect. 13205200e00eSIan Dowse * 13215200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 13225200e00eSIan Dowse * and port. These are not updated in the error case. 13235200e00eSIan Dowse */ 13245200e00eSIan Dowse int 1325a9d22cceSGleb Smirnoff in_pcbconnect_setup(struct inpcb *inp, struct sockaddr_in *sin, 1326136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 13279e46ff4dSGleb Smirnoff struct ucred *cred) 13285200e00eSIan Dowse { 13295200e00eSIan Dowse struct in_ifaddr *ia; 1330b89e82ddSJamie Gritton struct in_addr laddr, faddr; 13315200e00eSIan Dowse u_short lport, fport; 13325200e00eSIan Dowse int error; 13335200e00eSIan Dowse 1334f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 1335f161d294SMark Johnston ("%s: invalid address family for %p", __func__, sin)); 1336f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 1337f161d294SMark Johnston ("%s: invalid address length for %p", __func__, sin)); 1338f161d294SMark Johnston 13398501a69cSRobert Watson /* 13408501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 13418501a69cSRobert Watson * lock is sufficient. 13428501a69cSRobert Watson */ 1343c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 134427f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1345fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 134627f74fd0SRobert Watson 1347df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1348df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 13495200e00eSIan Dowse laddr.s_addr = *laddrp; 13505200e00eSIan Dowse lport = *lportp; 13515200e00eSIan Dowse faddr = sin->sin_addr; 13525200e00eSIan Dowse fport = sin->sin_port; 13530c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH 13540c325f53SAlexander V. Chernikov if (CALC_FLOWID_OUTBOUND) { 13550c325f53SAlexander V. Chernikov uint32_t hash_val, hash_type; 13560895aec3SBjoern A. Zeeb 13570c325f53SAlexander V. Chernikov hash_val = fib4_calc_software_hash(laddr, faddr, 0, fport, 13580c325f53SAlexander V. Chernikov inp->inp_socket->so_proto->pr_protocol, &hash_type); 13590c325f53SAlexander V. Chernikov 13600c325f53SAlexander V. Chernikov inp->inp_flowid = hash_val; 13610c325f53SAlexander V. Chernikov inp->inp_flowtype = hash_type; 13620c325f53SAlexander V. Chernikov } 13630c325f53SAlexander V. Chernikov #endif 1364d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1365df8bae1dSRodney W. Grimes /* 1366df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1367df8bae1dSRodney W. Grimes * use the primary local address. 1368df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1369df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1370df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1371df8bae1dSRodney W. Grimes */ 1372413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1373413628a7SBjoern A. Zeeb faddr = 1374d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1375ac1750ddSMark Johnston if ((error = prison_get_ip4(cred, &faddr)) != 0) 1376b89e82ddSJamie Gritton return (error); 13772d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1378d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 13792d9cfabaSRobert Watson IFF_BROADCAST) 1380d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1381603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 13822d9cfabaSRobert Watson } 1383df8bae1dSRodney W. Grimes } 13845200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1385d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1386df8bae1dSRodney W. Grimes /* 1387df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1388d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1389df8bae1dSRodney W. Grimes * address of that interface as our source address. 1390df8bae1dSRodney W. Grimes */ 13915200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1392df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1393df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1394df8bae1dSRodney W. Grimes struct ifnet *ifp; 1395df8bae1dSRodney W. Grimes 1396df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1397df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1398df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1399d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1400ac1750ddSMark Johnston if (ia->ia_ifp == ifp && 1401e691be70SDaniel Eischen prison_check_ip4(cred, 1402ac1750ddSMark Johnston &ia->ia_addr.sin_addr) == 0) 1403df8bae1dSRodney W. Grimes break; 1404e691be70SDaniel Eischen } 1405e691be70SDaniel Eischen if (ia == NULL) 1406d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1407e691be70SDaniel Eischen else { 14085200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1409d79fdd98SDaniel Eischen error = 0; 1410999f1343SGarrett Wollman } 1411d79fdd98SDaniel Eischen } 1412d79fdd98SDaniel Eischen } 141304215ed2SRandall Stewart if (error) 141404215ed2SRandall Stewart return (error); 14150895aec3SBjoern A. Zeeb } 1416a034518aSAndrew Gallatin 141725102351SMike Karels if (lport != 0) { 14189e46ff4dSGleb Smirnoff if (in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 14199e46ff4dSGleb Smirnoff fport, laddr, lport, 0, NULL, M_NODOM) != NULL) 1420df8bae1dSRodney W. Grimes return (EADDRINUSE); 142125102351SMike Karels } else { 142225102351SMike Karels struct sockaddr_in lsin, fsin; 142325102351SMike Karels 142425102351SMike Karels bzero(&lsin, sizeof(lsin)); 142525102351SMike Karels bzero(&fsin, sizeof(fsin)); 142625102351SMike Karels lsin.sin_family = AF_INET; 142725102351SMike Karels lsin.sin_addr = laddr; 142825102351SMike Karels fsin.sin_family = AF_INET; 142925102351SMike Karels fsin.sin_addr = faddr; 143025102351SMike Karels error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin, 143125102351SMike Karels &lport, (struct sockaddr *)& fsin, fport, cred, 143225102351SMike Karels INPLOOKUP_WILDCARD); 14335a903f8dSPierre Beyssac if (error) 14345a903f8dSPierre Beyssac return (error); 14355a903f8dSPierre Beyssac } 14365200e00eSIan Dowse *laddrp = laddr.s_addr; 14375200e00eSIan Dowse *lportp = lport; 14385200e00eSIan Dowse *faddrp = faddr.s_addr; 14395200e00eSIan Dowse *fportp = fport; 1440df8bae1dSRodney W. Grimes return (0); 1441df8bae1dSRodney W. Grimes } 1442df8bae1dSRodney W. Grimes 144326f9a767SRodney W. Grimes void 1444136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1445df8bae1dSRodney W. Grimes { 14466b348152SRobert Watson 14478501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1448fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1449df8bae1dSRodney W. Grimes 14502589ec0fSMark Johnston inp->inp_laddr.s_addr = INADDR_ANY; 1451df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1452df8bae1dSRodney W. Grimes inp->inp_fport = 0; 145315bd2b43SDavid Greenman in_pcbrehash(inp); 1454df8bae1dSRodney W. Grimes } 145583e521ecSBjoern A. Zeeb #endif /* INET */ 1456df8bae1dSRodney W. Grimes 14574c7c478dSRobert Watson /* 145828696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1459c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 146028696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 146128696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 14624c7c478dSRobert Watson */ 146326f9a767SRodney W. Grimes void 1464136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1465df8bae1dSRodney W. Grimes { 14664c7c478dSRobert Watson 1467a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1468c0a211c5SRobert Watson 1469f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1470f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1471f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1472f3e7afe2SHans Petter Selasky #endif 14734c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 14744c7c478dSRobert Watson inp->inp_socket = NULL; 14754c7c478dSRobert Watson } 14764c7c478dSRobert Watson 1477c0a211c5SRobert Watson /* 1478db0ac6deSCy Schubert * inpcb hash lookups are protected by SMR section. 1479db0ac6deSCy Schubert * 1480db0ac6deSCy Schubert * Once desired pcb has been found, switching from SMR section to a pcb 1481db0ac6deSCy Schubert * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1482db0ac6deSCy Schubert * here because SMR is a critical section. 1483db0ac6deSCy Schubert * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1484db0ac6deSCy Schubert */ 1485db0ac6deSCy Schubert static inline void 1486db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1487db0ac6deSCy Schubert { 1488db0ac6deSCy Schubert 1489db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1490db0ac6deSCy Schubert rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1491db0ac6deSCy Schubert } 1492db0ac6deSCy Schubert 1493db0ac6deSCy Schubert static inline void 1494db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1495db0ac6deSCy Schubert { 1496db0ac6deSCy Schubert 1497db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1498db0ac6deSCy Schubert rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1499db0ac6deSCy Schubert } 1500db0ac6deSCy Schubert 1501db0ac6deSCy Schubert static inline int 1502db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1503db0ac6deSCy Schubert { 1504db0ac6deSCy Schubert 1505db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1506db0ac6deSCy Schubert rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1507db0ac6deSCy Schubert } 1508db0ac6deSCy Schubert 1509db0ac6deSCy Schubert static inline bool 1510db0ac6deSCy Schubert in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 1511db0ac6deSCy Schubert { 1512db0ac6deSCy Schubert 1513db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1514db0ac6deSCy Schubert in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 1515db0ac6deSCy Schubert } 1516db0ac6deSCy Schubert 1517f567d55fSGleb Smirnoff static inline bool 1518f567d55fSGleb Smirnoff _inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock, const int ignflags) 1519db0ac6deSCy Schubert { 1520db0ac6deSCy Schubert 1521db0ac6deSCy Schubert MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1522db0ac6deSCy Schubert SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1523db0ac6deSCy Schubert 1524db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1525f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1526db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1527db0ac6deSCy Schubert inp_unlock(inp, lock); 1528db0ac6deSCy Schubert return (false); 1529db0ac6deSCy Schubert } 1530db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1531db0ac6deSCy Schubert return (true); 1532db0ac6deSCy Schubert } 1533db0ac6deSCy Schubert 1534db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1535db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1536db0ac6deSCy Schubert inp_lock(inp, lock); 1537db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) 1538db0ac6deSCy Schubert return (false); 1539db0ac6deSCy Schubert /* 1540db0ac6deSCy Schubert * inp acquired through refcount & lock for sure didn't went 1541db0ac6deSCy Schubert * through uma_zfree(). However, it may have already went 1542db0ac6deSCy Schubert * through in_pcbfree() and has another reference, that 1543db0ac6deSCy Schubert * prevented its release by our in_pcbrele(). 1544db0ac6deSCy Schubert */ 1545f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1546db0ac6deSCy Schubert inp_unlock(inp, lock); 1547db0ac6deSCy Schubert return (false); 1548db0ac6deSCy Schubert } 1549db0ac6deSCy Schubert return (true); 1550db0ac6deSCy Schubert } else { 1551db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1552db0ac6deSCy Schubert return (false); 1553db0ac6deSCy Schubert } 1554db0ac6deSCy Schubert } 1555db0ac6deSCy Schubert 1556f567d55fSGleb Smirnoff bool 1557f567d55fSGleb Smirnoff inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1558f567d55fSGleb Smirnoff { 1559f567d55fSGleb Smirnoff 1560f567d55fSGleb Smirnoff /* 1561f567d55fSGleb Smirnoff * in_pcblookup() family of functions ignore not only freed entries, 1562f567d55fSGleb Smirnoff * that may be found due to lockless access to the hash, but dropped 1563f567d55fSGleb Smirnoff * entries, too. 1564f567d55fSGleb Smirnoff */ 1565f567d55fSGleb Smirnoff return (_inp_smr_lock(inp, lock, INP_FREED | INP_DROPPED)); 1566f567d55fSGleb Smirnoff } 1567f567d55fSGleb Smirnoff 1568db0ac6deSCy Schubert /* 1569db0ac6deSCy Schubert * inp_next() - inpcb hash/list traversal iterator 1570db0ac6deSCy Schubert * 1571db0ac6deSCy Schubert * Requires initialized struct inpcb_iterator for context. 1572db0ac6deSCy Schubert * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1573db0ac6deSCy Schubert * 1574db0ac6deSCy Schubert * - Iterator can have either write-lock or read-lock semantics, that can not 1575db0ac6deSCy Schubert * be changed later. 1576db0ac6deSCy Schubert * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1577db0ac6deSCy Schubert * a single hash slot. Note: only rip_input() does the latter. 1578db0ac6deSCy Schubert * - Iterator may have optional bool matching function. The matching function 1579db0ac6deSCy Schubert * will be executed for each inpcb in the SMR context, so it can not acquire 1580db0ac6deSCy Schubert * locks and can safely access only immutable fields of inpcb. 1581db0ac6deSCy Schubert * 1582db0ac6deSCy Schubert * A fresh initialized iterator has NULL inpcb in its context and that 1583db0ac6deSCy Schubert * means that inp_next() call would return the very first inpcb on the list 1584db0ac6deSCy Schubert * locked with desired semantic. In all following calls the context pointer 1585db0ac6deSCy Schubert * shall hold the current inpcb pointer. The KPI user is not supposed to 1586db0ac6deSCy Schubert * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1587db0ac6deSCy Schubert * and write NULL to its context. After end of traversal an iterator can be 1588db0ac6deSCy Schubert * reused. 1589db0ac6deSCy Schubert * 1590db0ac6deSCy Schubert * List traversals have the following features/constraints: 1591db0ac6deSCy Schubert * - New entries won't be seen, as they are always added to the head of a list. 1592db0ac6deSCy Schubert * - Removed entries won't stop traversal as long as they are not added to 1593db0ac6deSCy Schubert * a different list. This is violated by in_pcbrehash(). 1594db0ac6deSCy Schubert */ 1595db0ac6deSCy Schubert #define II_LIST_FIRST(ipi, hash) \ 1596db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1597db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1598db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_hashbase[(hash)])) 1599db0ac6deSCy Schubert #define II_LIST_NEXT(inp, hash) \ 1600db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1601db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_list) : \ 1602db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_hash)) 1603db0ac6deSCy Schubert #define II_LOCK_ASSERT(inp, lock) \ 1604db0ac6deSCy Schubert rw_assert(&(inp)->inp_lock, \ 1605db0ac6deSCy Schubert (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1606db0ac6deSCy Schubert struct inpcb * 1607db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii) 1608db0ac6deSCy Schubert { 1609db0ac6deSCy Schubert const struct inpcbinfo *ipi = ii->ipi; 1610db0ac6deSCy Schubert inp_match_t *match = ii->match; 1611db0ac6deSCy Schubert void *ctx = ii->ctx; 1612db0ac6deSCy Schubert inp_lookup_t lock = ii->lock; 1613db0ac6deSCy Schubert int hash = ii->hash; 1614db0ac6deSCy Schubert struct inpcb *inp; 1615db0ac6deSCy Schubert 1616db0ac6deSCy Schubert if (ii->inp == NULL) { /* First call. */ 1617db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1618db0ac6deSCy Schubert /* This is unrolled CK_LIST_FOREACH(). */ 1619db0ac6deSCy Schubert for (inp = II_LIST_FIRST(ipi, hash); 1620db0ac6deSCy Schubert inp != NULL; 1621db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash)) { 1622db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1623db0ac6deSCy Schubert continue; 1624f567d55fSGleb Smirnoff if (__predict_true(_inp_smr_lock(inp, lock, INP_FREED))) 1625db0ac6deSCy Schubert break; 1626db0ac6deSCy Schubert else { 1627db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1628db0ac6deSCy Schubert MPASS(inp != II_LIST_FIRST(ipi, hash)); 1629db0ac6deSCy Schubert inp = II_LIST_FIRST(ipi, hash); 1630430df2abSMichael Tuexen if (inp == NULL) 1631430df2abSMichael Tuexen break; 1632db0ac6deSCy Schubert } 1633db0ac6deSCy Schubert } 1634db0ac6deSCy Schubert 1635db0ac6deSCy Schubert if (inp == NULL) 1636db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1637db0ac6deSCy Schubert else 1638db0ac6deSCy Schubert ii->inp = inp; 1639db0ac6deSCy Schubert 1640db0ac6deSCy Schubert return (inp); 1641db0ac6deSCy Schubert } 1642db0ac6deSCy Schubert 1643db0ac6deSCy Schubert /* Not a first call. */ 1644db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1645db0ac6deSCy Schubert restart: 1646db0ac6deSCy Schubert inp = ii->inp; 1647db0ac6deSCy Schubert II_LOCK_ASSERT(inp, lock); 1648db0ac6deSCy Schubert next: 1649db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash); 1650db0ac6deSCy Schubert if (inp == NULL) { 1651db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1652db0ac6deSCy Schubert goto found; 1653db0ac6deSCy Schubert } 1654db0ac6deSCy Schubert 1655db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1656db0ac6deSCy Schubert goto next; 1657db0ac6deSCy Schubert 1658db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1659db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1660db0ac6deSCy Schubert /* 1661db0ac6deSCy Schubert * Entries are never inserted in middle of a list, thus 1662db0ac6deSCy Schubert * as long as we are in SMR, we can continue traversal. 1663db0ac6deSCy Schubert * Jump to 'restart' should yield in the same result, 1664db0ac6deSCy Schubert * but could produce unnecessary looping. Could this 1665db0ac6deSCy Schubert * looping be unbound? 1666db0ac6deSCy Schubert */ 1667db0ac6deSCy Schubert inp_unlock(inp, lock); 1668db0ac6deSCy Schubert goto next; 1669db0ac6deSCy Schubert } else { 1670db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1671db0ac6deSCy Schubert goto found; 1672db0ac6deSCy Schubert } 1673db0ac6deSCy Schubert } 1674db0ac6deSCy Schubert 1675db0ac6deSCy Schubert /* 1676db0ac6deSCy Schubert * Can't obtain lock immediately, thus going hard. Once we exit the 1677db0ac6deSCy Schubert * SMR section we can no longer jump to 'next', and our only stable 1678db0ac6deSCy Schubert * anchoring point is ii->inp, which we keep locked for this case, so 1679db0ac6deSCy Schubert * we jump to 'restart'. 1680db0ac6deSCy Schubert */ 1681db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1682db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1683db0ac6deSCy Schubert inp_lock(inp, lock); 1684db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) { 1685db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1686db0ac6deSCy Schubert goto restart; 1687db0ac6deSCy Schubert } 1688db0ac6deSCy Schubert /* 1689db0ac6deSCy Schubert * See comment in inp_smr_lock(). 1690db0ac6deSCy Schubert */ 1691db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1692db0ac6deSCy Schubert inp_unlock(inp, lock); 1693db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1694db0ac6deSCy Schubert goto restart; 1695db0ac6deSCy Schubert } 1696db0ac6deSCy Schubert } else 1697db0ac6deSCy Schubert goto next; 1698db0ac6deSCy Schubert 1699db0ac6deSCy Schubert found: 1700db0ac6deSCy Schubert inp_unlock(ii->inp, lock); 1701db0ac6deSCy Schubert ii->inp = inp; 1702db0ac6deSCy Schubert 1703db0ac6deSCy Schubert return (ii->inp); 1704db0ac6deSCy Schubert } 1705db0ac6deSCy Schubert 1706db0ac6deSCy Schubert /* 170779bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 1708db0ac6deSCy Schubert * stability of an inpcb pointer despite the inpcb lock being released or 1709db0ac6deSCy Schubert * SMR section exited. 171079bdc6e5SRobert Watson * 1711db0ac6deSCy Schubert * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1712c0a211c5SRobert Watson */ 171379bdc6e5SRobert Watson void 171479bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 17154c7c478dSRobert Watson { 1716db0ac6deSCy Schubert u_int old __diagused; 1717df8bae1dSRodney W. Grimes 1718db0ac6deSCy Schubert old = refcount_acquire(&inp->inp_refcount); 1719db0ac6deSCy Schubert KASSERT(old > 0, ("%s: refcount 0", __func__)); 172079bdc6e5SRobert Watson } 172179bdc6e5SRobert Watson 172279bdc6e5SRobert Watson /* 1723db0ac6deSCy Schubert * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1724db0ac6deSCy Schubert * freeing the pcb, if the reference was very last. 172579bdc6e5SRobert Watson */ 1726db0ac6deSCy Schubert bool 172779bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 172879bdc6e5SRobert Watson { 172979bdc6e5SRobert Watson 173079bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 173179bdc6e5SRobert Watson 1732db0ac6deSCy Schubert if (refcount_release(&inp->inp_refcount) == 0) 1733db0ac6deSCy Schubert return (false); 1734db0ac6deSCy Schubert 1735db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1736db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1737db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1738df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1739db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1740db0ac6deSCy Schubert return (true); 1741df4e91d3SGleb Smirnoff } 174279bdc6e5SRobert Watson 1743db0ac6deSCy Schubert bool 174479bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 174579bdc6e5SRobert Watson { 174679bdc6e5SRobert Watson 174779bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 174879bdc6e5SRobert Watson 1749db0ac6deSCy Schubert if (refcount_release(&inp->inp_refcount) == 0) 1750db0ac6deSCy Schubert return (false); 1751db0ac6deSCy Schubert 1752db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1753db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1754db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1755edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1756db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1757db0ac6deSCy Schubert return (true); 1758addf2b20SMatt Macy } 1759addf2b20SMatt Macy 176079bdc6e5SRobert Watson /* 176179bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 176279bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 176379bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 176479bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 1765db0ac6deSCy Schubert * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1766db0ac6deSCy Schubert * Almost all work, including removal from global lists, is done in this 1767db0ac6deSCy Schubert * context, where the pcbinfo lock is held. 176879bdc6e5SRobert Watson */ 176979bdc6e5SRobert Watson void 177079bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 177179bdc6e5SRobert Watson { 1772f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1773db0ac6deSCy Schubert #ifdef INET 1774db0ac6deSCy Schubert struct ip_moptions *imo; 1775db0ac6deSCy Schubert #endif 1776db0ac6deSCy Schubert #ifdef INET6 1777db0ac6deSCy Schubert struct ip6_moptions *im6o; 1778db0ac6deSCy Schubert #endif 177945a48d07SJonathan T. Looney 178079bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1781db0ac6deSCy Schubert KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1782db0ac6deSCy Schubert KASSERT((inp->inp_flags & INP_FREED) == 0, 1783db0ac6deSCy Schubert ("%s: called twice for pcb %p", __func__, inp)); 1784db0ac6deSCy Schubert 1785db0ac6deSCy Schubert inp->inp_flags |= INP_FREED; 1786db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 1787db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1788db0ac6deSCy Schubert pcbinfo->ipi_count--; 1789db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_list); 1790db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 1791db0ac6deSCy Schubert 17923ba34b07SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 17933ba34b07SGleb Smirnoff in_pcbremhash(inp); 1794db0ac6deSCy Schubert 1795fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1796db0ac6deSCy Schubert #ifdef MAC 1797db0ac6deSCy Schubert mac_inpcb_destroy(inp); 1798db0ac6deSCy Schubert #endif 1799db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1800db0ac6deSCy Schubert if (inp->inp_sp != NULL) 1801db0ac6deSCy Schubert ipsec_delete_pcbpolicy(inp); 1802db0ac6deSCy Schubert #endif 1803db0ac6deSCy Schubert #ifdef INET 1804db0ac6deSCy Schubert if (inp->inp_options) 1805db0ac6deSCy Schubert (void)m_free(inp->inp_options); 1806db0ac6deSCy Schubert imo = inp->inp_moptions; 1807db0ac6deSCy Schubert #endif 1808db0ac6deSCy Schubert #ifdef INET6 1809db0ac6deSCy Schubert if (inp->inp_vflag & INP_IPV6PROTO) { 1810db0ac6deSCy Schubert ip6_freepcbopts(inp->in6p_outputopts); 1811db0ac6deSCy Schubert im6o = inp->in6p_moptions; 1812db0ac6deSCy Schubert } else 1813db0ac6deSCy Schubert im6o = NULL; 1814db0ac6deSCy Schubert #endif 1815db0ac6deSCy Schubert 1816db0ac6deSCy Schubert if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1817cb6bb230SMatt Macy INP_WUNLOCK(inp); 1818db0ac6deSCy Schubert } 1819db0ac6deSCy Schubert #ifdef INET6 1820db0ac6deSCy Schubert ip6_freemoptions(im6o); 1821db0ac6deSCy Schubert #endif 1822db0ac6deSCy Schubert #ifdef INET 1823db0ac6deSCy Schubert inp_freemoptions(imo); 1824db0ac6deSCy Schubert #endif 1825eb93b99dSGleb Smirnoff /* Destruction is finalized in inpcb_dtor(). */ 1826eb93b99dSGleb Smirnoff } 1827eb93b99dSGleb Smirnoff 1828eb93b99dSGleb Smirnoff static void 1829eb93b99dSGleb Smirnoff inpcb_dtor(void *mem, int size, void *arg) 1830eb93b99dSGleb Smirnoff { 1831eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1832eb93b99dSGleb Smirnoff 1833eb93b99dSGleb Smirnoff crfree(inp->inp_cred); 1834eb93b99dSGleb Smirnoff #ifdef INVARIANTS 1835eb93b99dSGleb Smirnoff inp->inp_cred = NULL; 1836eb93b99dSGleb Smirnoff #endif 1837eb93b99dSGleb Smirnoff } 1838eb93b99dSGleb Smirnoff 1839eb93b99dSGleb Smirnoff /* 1840eb93b99dSGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 1841eb93b99dSGleb Smirnoff * different name to the lock. But they all are disposed the same. 1842eb93b99dSGleb Smirnoff */ 1843eb93b99dSGleb Smirnoff static void 1844eb93b99dSGleb Smirnoff inpcb_fini(void *mem, int size) 1845eb93b99dSGleb Smirnoff { 1846eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1847eb93b99dSGleb Smirnoff 1848eb93b99dSGleb Smirnoff INP_LOCK_DESTROY(inp); 184928696211SRobert Watson } 185028696211SRobert Watson 185128696211SRobert Watson /* 1852c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1853c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1854c0a211c5SRobert Watson * 1855c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1856c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1857c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1858c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1859c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1860c0a211c5SRobert Watson * in_pcbdetach(). 1861c0a211c5SRobert Watson * 1862c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1863c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 186410702a28SRobert Watson */ 186510702a28SRobert Watson void 186610702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 186710702a28SRobert Watson { 186810702a28SRobert Watson 18698501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 18706573d758SMatt Macy #ifdef INVARIANTS 18716573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 18726573d758SMatt Macy MPASS(inp->inp_refcount > 1); 18736573d758SMatt Macy #endif 187410702a28SRobert Watson 1875ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 18763ba34b07SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 18773ba34b07SGleb Smirnoff in_pcbremhash(inp); 187810702a28SRobert Watson } 187910702a28SRobert Watson 188067107f45SBjoern A. Zeeb #ifdef INET 188154d642bbSRobert Watson /* 188254d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 188354d642bbSRobert Watson */ 188426ef6ac4SDon Lewis struct sockaddr * 1885136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 188626ef6ac4SDon Lewis { 188726ef6ac4SDon Lewis struct sockaddr_in *sin; 188826ef6ac4SDon Lewis 18891ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1890a163d034SWarner Losh M_WAITOK | M_ZERO); 189126ef6ac4SDon Lewis sin->sin_family = AF_INET; 189226ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 189326ef6ac4SDon Lewis sin->sin_addr = *addr_p; 189426ef6ac4SDon Lewis sin->sin_port = port; 189526ef6ac4SDon Lewis 189626ef6ac4SDon Lewis return (struct sockaddr *)sin; 189726ef6ac4SDon Lewis } 189826ef6ac4SDon Lewis 1899117bcae7SGarrett Wollman int 190054d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1901df8bae1dSRodney W. Grimes { 1902136d4f1cSRobert Watson struct inpcb *inp; 190326ef6ac4SDon Lewis struct in_addr addr; 190426ef6ac4SDon Lewis in_port_t port; 190542fa505bSDavid Greenman 1906fdc984f7STor Egge inp = sotoinpcb(so); 190754d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 19086466b28aSRobert Watson 1909a69042a5SRobert Watson INP_RLOCK(inp); 191026ef6ac4SDon Lewis port = inp->inp_lport; 191126ef6ac4SDon Lewis addr = inp->inp_laddr; 1912a69042a5SRobert Watson INP_RUNLOCK(inp); 191342fa505bSDavid Greenman 191426ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1915117bcae7SGarrett Wollman return 0; 1916df8bae1dSRodney W. Grimes } 1917df8bae1dSRodney W. Grimes 1918117bcae7SGarrett Wollman int 191954d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1920df8bae1dSRodney W. Grimes { 1921136d4f1cSRobert Watson struct inpcb *inp; 192226ef6ac4SDon Lewis struct in_addr addr; 192326ef6ac4SDon Lewis in_port_t port; 192442fa505bSDavid Greenman 1925fdc984f7STor Egge inp = sotoinpcb(so); 192654d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 19276466b28aSRobert Watson 1928a69042a5SRobert Watson INP_RLOCK(inp); 192926ef6ac4SDon Lewis port = inp->inp_fport; 193026ef6ac4SDon Lewis addr = inp->inp_faddr; 1931a69042a5SRobert Watson INP_RUNLOCK(inp); 193242fa505bSDavid Greenman 193326ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1934117bcae7SGarrett Wollman return 0; 1935df8bae1dSRodney W. Grimes } 1936df8bae1dSRodney W. Grimes 193726f9a767SRodney W. Grimes void 1938136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1939136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1940d1c54148SJesper Skriver { 1941f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1942d1c54148SJesper Skriver 19433dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1944db0ac6deSCy Schubert CK_LIST_FOREACH_SAFE(inp, &pcbinfo->ipi_listhead, inp_list, inp_temp) { 19458501a69cSRobert Watson INP_WLOCK(inp); 1946d1c54148SJesper Skriver #ifdef INET6 1947f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 19488501a69cSRobert Watson INP_WUNLOCK(inp); 1949d1c54148SJesper Skriver continue; 1950f76fcf6dSJeffrey Hsu } 1951d1c54148SJesper Skriver #endif 1952d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1953f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 19548501a69cSRobert Watson INP_WUNLOCK(inp); 1955d1c54148SJesper Skriver continue; 1956d1c54148SJesper Skriver } 19573dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 19588501a69cSRobert Watson INP_WUNLOCK(inp); 1959f76fcf6dSJeffrey Hsu } 19603dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1961d1c54148SJesper Skriver } 1962d1c54148SJesper Skriver 1963db0ac6deSCy Schubert static bool 1964db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 1965db0ac6deSCy Schubert { 1966db0ac6deSCy Schubert 1967db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 1968db0ac6deSCy Schubert return (true); 1969db0ac6deSCy Schubert else 1970db0ac6deSCy Schubert return (false); 1971db0ac6deSCy Schubert } 1972db0ac6deSCy Schubert 1973e43cc4aeSHajimu UMEMOTO void 1974136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1975e43cc4aeSHajimu UMEMOTO { 1976db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 1977db0ac6deSCy Schubert inp_v4_multi_match, NULL); 1978e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 197959854ecfSHans Petter Selasky struct in_multi *inm; 198059854ecfSHans Petter Selasky struct in_mfilter *imf; 1981e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1982e43cc4aeSHajimu UMEMOTO 1983db0ac6deSCy Schubert IN_MULTI_LOCK_ASSERT(); 1984db0ac6deSCy Schubert 1985db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 1986db0ac6deSCy Schubert INP_WLOCK_ASSERT(inp); 1987db0ac6deSCy Schubert 1988e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1989e43cc4aeSHajimu UMEMOTO /* 1990e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1991e43cc4aeSHajimu UMEMOTO * detached. 1992e43cc4aeSHajimu UMEMOTO */ 1993e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1994e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1995e43cc4aeSHajimu UMEMOTO 1996e43cc4aeSHajimu UMEMOTO /* 1997e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1998e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1999cb6bb230SMatt Macy * 2000cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 2001e43cc4aeSHajimu UMEMOTO */ 200259854ecfSHans Petter Selasky restart: 200359854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 200459854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 200559854ecfSHans Petter Selasky continue; 200659854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 200759854ecfSHans Petter Selasky continue; 200859854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 200959854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 201059854ecfSHans Petter Selasky ip_mfilter_free(imf); 201159854ecfSHans Petter Selasky goto restart; 2012e43cc4aeSHajimu UMEMOTO } 2013e43cc4aeSHajimu UMEMOTO } 2014e43cc4aeSHajimu UMEMOTO } 2015e43cc4aeSHajimu UMEMOTO 2016df8bae1dSRodney W. Grimes /* 2017fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 2018fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 2019c3229e05SDavid Greenman */ 2020d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 2021df8bae1dSRodney W. Grimes struct inpcb * 2022136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 202368e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 2024df8bae1dSRodney W. Grimes { 2025136d4f1cSRobert Watson struct inpcb *inp; 2026d5e8a67eSHajimu UMEMOTO #ifdef INET6 2027d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 2028d5e8a67eSHajimu UMEMOTO #else 2029d5e8a67eSHajimu UMEMOTO int matchwild = 3; 2030d5e8a67eSHajimu UMEMOTO #endif 2031d5e8a67eSHajimu UMEMOTO int wildcard; 20327bc4aca7SDavid Greenman 203368e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 203468e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2035fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 20361b73ca0bSSam Leffler 203768e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 2038c3229e05SDavid Greenman struct inpcbhead *head; 2039c3229e05SDavid Greenman /* 2040c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 2041c3229e05SDavid Greenman * matches the local address and port we're looking for. 2042c3229e05SDavid Greenman */ 2043a0577692SGleb Smirnoff head = &pcbinfo->ipi_hashbase[INP_PCBHASH_WILD(lport, 2044a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 2045b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2046cfa1ca9dSYoshinobu Inoue #ifdef INET6 2047413628a7SBjoern A. Zeeb /* XXX inp locking */ 2048369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2049cfa1ca9dSYoshinobu Inoue continue; 2050cfa1ca9dSYoshinobu Inoue #endif 2051c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 2052c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2053c3229e05SDavid Greenman inp->inp_lport == lport) { 2054c3229e05SDavid Greenman /* 2055413628a7SBjoern A. Zeeb * Found? 2056c3229e05SDavid Greenman */ 2057ac1750ddSMark Johnston if (prison_equal_ip4(cred->cr_prison, 20580304c731SJamie Gritton inp->inp_cred->cr_prison)) 2059c3229e05SDavid Greenman return (inp); 2060df8bae1dSRodney W. Grimes } 2061c3229e05SDavid Greenman } 2062c3229e05SDavid Greenman /* 2063c3229e05SDavid Greenman * Not found. 2064c3229e05SDavid Greenman */ 2065c3229e05SDavid Greenman return (NULL); 2066c3229e05SDavid Greenman } else { 2067c3229e05SDavid Greenman struct inpcbporthead *porthash; 2068c3229e05SDavid Greenman struct inpcbport *phd; 2069c3229e05SDavid Greenman struct inpcb *match = NULL; 2070c3229e05SDavid Greenman /* 2071c3229e05SDavid Greenman * Best fit PCB lookup. 2072c3229e05SDavid Greenman * 2073c3229e05SDavid Greenman * First see if this local port is in use by looking on the 2074c3229e05SDavid Greenman * port hash list. 2075c3229e05SDavid Greenman */ 2076712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 2077712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 2078b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 2079c3229e05SDavid Greenman if (phd->phd_port == lport) 2080c3229e05SDavid Greenman break; 2081c3229e05SDavid Greenman } 2082c3229e05SDavid Greenman if (phd != NULL) { 2083c3229e05SDavid Greenman /* 2084c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 2085c3229e05SDavid Greenman * fit. 2086c3229e05SDavid Greenman */ 2087b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 2088c3229e05SDavid Greenman wildcard = 0; 2089ac1750ddSMark Johnston if (!prison_equal_ip4(inp->inp_cred->cr_prison, 20900304c731SJamie Gritton cred->cr_prison)) 2091413628a7SBjoern A. Zeeb continue; 2092cfa1ca9dSYoshinobu Inoue #ifdef INET6 2093413628a7SBjoern A. Zeeb /* XXX inp locking */ 2094369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2095cfa1ca9dSYoshinobu Inoue continue; 2096d5e8a67eSHajimu UMEMOTO /* 2097d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 2098d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 2099d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 2100d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 2101d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 2102d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 2103d5e8a67eSHajimu UMEMOTO * 2104d5e8a67eSHajimu UMEMOTO * Note that the case only happens 2105d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 2106d5e8a67eSHajimu UMEMOTO * the condition that the use of the 2107d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 2108d5e8a67eSHajimu UMEMOTO */ 2109d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 2110d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2111cfa1ca9dSYoshinobu Inoue #endif 2112c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 2113c3229e05SDavid Greenman wildcard++; 211415bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 211515bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 211615bd2b43SDavid Greenman wildcard++; 211715bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 211815bd2b43SDavid Greenman continue; 211915bd2b43SDavid Greenman } else { 212015bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 212115bd2b43SDavid Greenman wildcard++; 212215bd2b43SDavid Greenman } 2123df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 2124df8bae1dSRodney W. Grimes match = inp; 2125df8bae1dSRodney W. Grimes matchwild = wildcard; 2126413628a7SBjoern A. Zeeb if (matchwild == 0) 2127df8bae1dSRodney W. Grimes break; 2128df8bae1dSRodney W. Grimes } 2129df8bae1dSRodney W. Grimes } 21303dbdc25cSDavid Greenman } 2131df8bae1dSRodney W. Grimes return (match); 2132df8bae1dSRodney W. Grimes } 2133c3229e05SDavid Greenman } 2134d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 213515bd2b43SDavid Greenman 2136d93ec8cbSMark Johnston static bool 2137d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain) 2138d93ec8cbSMark Johnston { 2139d93ec8cbSMark Johnston return (domain == M_NODOM || domain == grp->il_numa_domain); 2140d93ec8cbSMark Johnston } 2141d93ec8cbSMark Johnston 21421a43cff9SSean Bruno static struct inpcb * 21431a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 21441a43cff9SSean Bruno const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr, 2145d93ec8cbSMark Johnston uint16_t fport, int lookupflags, int domain) 21461a43cff9SSean Bruno { 21471a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 21481a43cff9SSean Bruno struct inpcblbgroup *grp; 2149d93ec8cbSMark Johnston struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 21501a43cff9SSean Bruno 21511a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 21521a43cff9SSean Bruno 21539d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 21549d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 21551a43cff9SSean Bruno 21561a43cff9SSean Bruno /* 2157d93ec8cbSMark Johnston * Search for an LB group match based on the following criteria: 2158d93ec8cbSMark Johnston * - prefer jailed groups to non-jailed groups 2159d93ec8cbSMark Johnston * - prefer exact source address matches to wildcard matches 2160d93ec8cbSMark Johnston * - prefer groups bound to the specified NUMA domain 21611a43cff9SSean Bruno */ 2162d93ec8cbSMark Johnston jail_exact = jail_wild = local_exact = local_wild = NULL; 216354af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 2164d93ec8cbSMark Johnston bool injail; 2165d93ec8cbSMark Johnston 21661a43cff9SSean Bruno #ifdef INET6 21671a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 21681a43cff9SSean Bruno continue; 21691a43cff9SSean Bruno #endif 21708be02ee4SMark Johnston if (grp->il_lport != lport) 21718be02ee4SMark Johnston continue; 21721a43cff9SSean Bruno 2173d93ec8cbSMark Johnston injail = prison_flag(grp->il_cred, PR_IP4) != 0; 2174d93ec8cbSMark Johnston if (injail && prison_check_ip4_locked(grp->il_cred->cr_prison, 2175d93ec8cbSMark Johnston laddr) != 0) 2176d93ec8cbSMark Johnston continue; 2177a034518aSAndrew Gallatin 2178d93ec8cbSMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) { 2179d93ec8cbSMark Johnston if (injail) { 2180d93ec8cbSMark Johnston jail_exact = grp; 2181d93ec8cbSMark Johnston if (in_pcblookup_lb_numa_match(grp, domain)) 2182d93ec8cbSMark Johnston /* This is a perfect match. */ 2183d93ec8cbSMark Johnston goto out; 2184d93ec8cbSMark Johnston } else if (local_exact == NULL || 2185d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) { 2186d93ec8cbSMark Johnston local_exact = grp; 2187d93ec8cbSMark Johnston } 2188d93ec8cbSMark Johnston } else if (grp->il_laddr.s_addr == INADDR_ANY && 2189d93ec8cbSMark Johnston (lookupflags & INPLOOKUP_WILDCARD) != 0) { 2190d93ec8cbSMark Johnston if (injail) { 2191d93ec8cbSMark Johnston if (jail_wild == NULL || 2192d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) 2193d93ec8cbSMark Johnston jail_wild = grp; 2194d93ec8cbSMark Johnston } else if (local_wild == NULL || 2195d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) { 2196d93ec8cbSMark Johnston local_wild = grp; 2197d93ec8cbSMark Johnston } 2198d93ec8cbSMark Johnston } 2199d93ec8cbSMark Johnston } 2200d93ec8cbSMark Johnston 2201d93ec8cbSMark Johnston if (jail_exact != NULL) 2202d93ec8cbSMark Johnston grp = jail_exact; 2203d93ec8cbSMark Johnston else if (jail_wild != NULL) 2204d93ec8cbSMark Johnston grp = jail_wild; 2205d93ec8cbSMark Johnston else if (local_exact != NULL) 2206d93ec8cbSMark Johnston grp = local_exact; 2207d93ec8cbSMark Johnston else 2208d93ec8cbSMark Johnston grp = local_wild; 2209d93ec8cbSMark Johnston if (grp == NULL) 2210d93ec8cbSMark Johnston return (NULL); 2211d93ec8cbSMark Johnston out: 2212d93ec8cbSMark Johnston return (grp->il_inp[INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) % 2213d93ec8cbSMark Johnston grp->il_inpcnt]); 22141a43cff9SSean Bruno } 22151a43cff9SSean Bruno 221615bd2b43SDavid Greenman /* 2217fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2218db0ac6deSCy Schubert * that the caller has either locked the hash list, which usually happens 2219db0ac6deSCy Schubert * for bind(2) operations, or is in SMR section, which happens when sorting 2220db0ac6deSCy Schubert * out incoming packets. 222115bd2b43SDavid Greenman */ 2222fa046d87SRobert Watson static struct inpcb * 2223fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 222468e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2225a034518aSAndrew Gallatin struct ifnet *ifp, uint8_t numa_domain) 222615bd2b43SDavid Greenman { 222715bd2b43SDavid Greenman struct inpcbhead *head; 2228*220d8921SGleb Smirnoff struct inpcb *inp; 222915bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 223015bd2b43SDavid Greenman 223168e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 223268e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2233b0ccf53fSMark Johnston KASSERT(faddr.s_addr != INADDR_ANY, 2234b0ccf53fSMark Johnston ("%s: invalid foreign address", __func__)); 2235b0ccf53fSMark Johnston KASSERT(laddr.s_addr != INADDR_ANY, 2236b0ccf53fSMark Johnston ("%s: invalid local address", __func__)); 2237d797164aSGleb Smirnoff INP_HASH_LOCK_ASSERT(pcbinfo); 2238d797164aSGleb Smirnoff 223915bd2b43SDavid Greenman /* 224015bd2b43SDavid Greenman * First look for an exact match. 224115bd2b43SDavid Greenman */ 2242a0577692SGleb Smirnoff head = &pcbinfo->ipi_hashbase[INP_PCBHASH(&faddr, lport, fport, 2243712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 2244b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2245cfa1ca9dSYoshinobu Inoue #ifdef INET6 2246413628a7SBjoern A. Zeeb /* XXX inp locking */ 2247369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2248cfa1ca9dSYoshinobu Inoue continue; 2249cfa1ca9dSYoshinobu Inoue #endif 22506d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2251ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2252ca98b82cSDavid Greenman inp->inp_fport == fport && 2253*220d8921SGleb Smirnoff inp->inp_lport == lport) 2254c3229e05SDavid Greenman return (inp); 2255c3229e05SDavid Greenman } 2256e3fd5ffdSRobert Watson 2257e3fd5ffdSRobert Watson /* 2258e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 2259e3fd5ffdSRobert Watson */ 226068e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2261413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 2262e3fd5ffdSRobert Watson #ifdef INET6 2263cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 2264e3fd5ffdSRobert Watson #endif 2265413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 2266413628a7SBjoern A. Zeeb int injail; 2267413628a7SBjoern A. Zeeb 2268413628a7SBjoern A. Zeeb /* 2269d93ec8cbSMark Johnston * First see if an LB group matches the request before scanning 2270d93ec8cbSMark Johnston * all sockets on this port. 2271d93ec8cbSMark Johnston */ 2272d93ec8cbSMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr, 2273d93ec8cbSMark Johnston fport, lookupflags, numa_domain); 2274d93ec8cbSMark Johnston if (inp != NULL) 2275d93ec8cbSMark Johnston return (inp); 2276d93ec8cbSMark Johnston 2277d93ec8cbSMark Johnston /* 2278413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2279413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2280413628a7SBjoern A. Zeeb * 2. jailed, wild. 2281413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2282413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2283413628a7SBjoern A. Zeeb */ 22846d6a026bSDavid Greenman 2285a0577692SGleb Smirnoff head = &pcbinfo->ipi_hashbase[INP_PCBHASH_WILD(lport, 2286a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 2287b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2288cfa1ca9dSYoshinobu Inoue #ifdef INET6 2289413628a7SBjoern A. Zeeb /* XXX inp locking */ 2290369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2291cfa1ca9dSYoshinobu Inoue continue; 2292cfa1ca9dSYoshinobu Inoue #endif 2293413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2294413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2295413628a7SBjoern A. Zeeb continue; 2296413628a7SBjoern A. Zeeb 22970304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2298413628a7SBjoern A. Zeeb if (injail) { 2299185e659cSGleb Smirnoff if (prison_check_ip4_locked( 2300185e659cSGleb Smirnoff inp->inp_cred->cr_prison, &laddr) != 0) 2301413628a7SBjoern A. Zeeb continue; 2302413628a7SBjoern A. Zeeb } else { 2303413628a7SBjoern A. Zeeb if (local_exact != NULL) 2304413628a7SBjoern A. Zeeb continue; 2305413628a7SBjoern A. Zeeb } 2306413628a7SBjoern A. Zeeb 2307413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2308413628a7SBjoern A. Zeeb if (injail) 2309c3229e05SDavid Greenman return (inp); 2310413628a7SBjoern A. Zeeb else 2311413628a7SBjoern A. Zeeb local_exact = inp; 2312413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2313e3fd5ffdSRobert Watson #ifdef INET6 2314413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23155cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2316cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2317cfa1ca9dSYoshinobu Inoue else 231883e521ecSBjoern A. Zeeb #endif 2319413628a7SBjoern A. Zeeb if (injail) 2320413628a7SBjoern A. Zeeb jail_wild = inp; 2321413628a7SBjoern A. Zeeb else 23226d6a026bSDavid Greenman local_wild = inp; 23236d6a026bSDavid Greenman } 2324413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2325413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2326413628a7SBjoern A. Zeeb return (jail_wild); 2327413628a7SBjoern A. Zeeb if (local_exact != NULL) 2328413628a7SBjoern A. Zeeb return (local_exact); 2329413628a7SBjoern A. Zeeb if (local_wild != NULL) 2330c3229e05SDavid Greenman return (local_wild); 2331413628a7SBjoern A. Zeeb #ifdef INET6 2332413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2333413628a7SBjoern A. Zeeb return (local_wild_mapped); 233483e521ecSBjoern A. Zeeb #endif 233568e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2336413628a7SBjoern A. Zeeb 23376d6a026bSDavid Greenman return (NULL); 233815bd2b43SDavid Greenman } 2339fa046d87SRobert Watson 2340fa046d87SRobert Watson /* 2341fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2342fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2343fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2344fa046d87SRobert Watson */ 2345fa046d87SRobert Watson static struct inpcb * 2346fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2347fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2348a034518aSAndrew Gallatin struct ifnet *ifp, uint8_t numa_domain) 2349fa046d87SRobert Watson { 2350fa046d87SRobert Watson struct inpcb *inp; 2351fa046d87SRobert Watson 2352675e2618SMark Johnston KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2353675e2618SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 2354675e2618SMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2355675e2618SMark Johnston ("%s: LOCKPCB not set", __func__)); 2356675e2618SMark Johnston 2357db0ac6deSCy Schubert smr_enter(pcbinfo->ipi_smr); 2358fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 235908d9c920SGleb Smirnoff lookupflags & INPLOOKUP_WILDCARD, ifp, numa_domain); 2360fa046d87SRobert Watson if (inp != NULL) { 2361db0ac6deSCy Schubert if (__predict_false(inp_smr_lock(inp, 2362db0ac6deSCy Schubert (lookupflags & INPLOOKUP_LOCKMASK)) == false)) 2363266f97b5SCy Schubert inp = NULL; 2364db0ac6deSCy Schubert } else 2365db0ac6deSCy Schubert smr_exit(pcbinfo->ipi_smr); 2366d797164aSGleb Smirnoff 2367fa046d87SRobert Watson return (inp); 2368fa046d87SRobert Watson } 2369fa046d87SRobert Watson 2370fa046d87SRobert Watson /* 2371d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2372d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 2373fa046d87SRobert Watson */ 2374fa046d87SRobert Watson struct inpcb * 2375fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2376fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2377fa046d87SRobert Watson { 2378fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2379a034518aSAndrew Gallatin lookupflags, ifp, M_NODOM)); 2380fa046d87SRobert Watson } 2381d3c1f003SRobert Watson 2382d3c1f003SRobert Watson struct inpcb * 2383d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2384d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2385d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2386d3c1f003SRobert Watson { 2387d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2388a034518aSAndrew Gallatin lookupflags, ifp, m->m_pkthdr.numa_domain)); 2389d3c1f003SRobert Watson } 239067107f45SBjoern A. Zeeb #endif /* INET */ 239115bd2b43SDavid Greenman 23927bc4aca7SDavid Greenman /* 2393c3229e05SDavid Greenman * Insert PCB onto various hash lists. 23947bc4aca7SDavid Greenman */ 2395db0ac6deSCy Schubert int 2396db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp) 239715bd2b43SDavid Greenman { 2398c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2399c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2400c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2401c3229e05SDavid Greenman struct inpcbport *phd; 240215bd2b43SDavid Greenman 24038501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2404fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2405fa046d87SRobert Watson 2406111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2407111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2408602cc7f1SRobert Watson 2409cfa1ca9dSYoshinobu Inoue #ifdef INET6 2410cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 2411a0577692SGleb Smirnoff pcbhash = &pcbinfo->ipi_hashbase[INP6_PCBHASH(&inp->in6p_faddr, 2412a0577692SGleb Smirnoff inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 2413cfa1ca9dSYoshinobu Inoue else 241483e521ecSBjoern A. Zeeb #endif 2415a0577692SGleb Smirnoff pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(&inp->inp_faddr, 2416712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 241715bd2b43SDavid Greenman 2418712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2419712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2420c3229e05SDavid Greenman 2421c3229e05SDavid Greenman /* 24221a43cff9SSean Bruno * Add entry to load balance group. 24231a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 24241a43cff9SSean Bruno */ 2425a152dd86SMark Johnston if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) { 2426a152dd86SMark Johnston int error = in_pcbinslbgrouphash(inp, M_NODOM); 2427a152dd86SMark Johnston if (error != 0) 2428a152dd86SMark Johnston return (error); 24291a43cff9SSean Bruno } 24301a43cff9SSean Bruno 24311a43cff9SSean Bruno /* 2432c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2433c3229e05SDavid Greenman */ 2434b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2435c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2436c3229e05SDavid Greenman break; 2437c3229e05SDavid Greenman } 2438a152dd86SMark Johnston 2439c3229e05SDavid Greenman /* 2440c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2441c3229e05SDavid Greenman */ 2442c3229e05SDavid Greenman if (phd == NULL) { 2443db0ac6deSCy Schubert phd = uma_zalloc_smr(pcbinfo->ipi_portzone, M_NOWAIT); 2444c3229e05SDavid Greenman if (phd == NULL) { 2445a152dd86SMark Johnston if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 2446a152dd86SMark Johnston in_pcbremlbgrouphash(inp); 2447a152dd86SMark Johnston return (ENOMEM); 2448c3229e05SDavid Greenman } 2449c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2450b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2451b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2452c3229e05SDavid Greenman } 2453c3229e05SDavid Greenman inp->inp_phd = phd; 2454b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2455b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2456111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 2457db0ac6deSCy Schubert 2458c3229e05SDavid Greenman return (0); 245915bd2b43SDavid Greenman } 246015bd2b43SDavid Greenman 24613ba34b07SGleb Smirnoff static void 24623ba34b07SGleb Smirnoff in_pcbremhash(struct inpcb *inp) 24633ba34b07SGleb Smirnoff { 24643ba34b07SGleb Smirnoff struct inpcbport *phd = inp->inp_phd; 24653ba34b07SGleb Smirnoff 24663ba34b07SGleb Smirnoff INP_WLOCK_ASSERT(inp); 24673ba34b07SGleb Smirnoff MPASS(inp->inp_flags & INP_INHASHLIST); 24683ba34b07SGleb Smirnoff 24693ba34b07SGleb Smirnoff INP_HASH_WLOCK(inp->inp_pcbinfo); 2470d93ec8cbSMark Johnston if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 24713ba34b07SGleb Smirnoff in_pcbremlbgrouphash(inp); 24723ba34b07SGleb Smirnoff CK_LIST_REMOVE(inp, inp_hash); 24733ba34b07SGleb Smirnoff CK_LIST_REMOVE(inp, inp_portlist); 24743ba34b07SGleb Smirnoff if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 24753ba34b07SGleb Smirnoff CK_LIST_REMOVE(phd, phd_hash); 24763ba34b07SGleb Smirnoff uma_zfree_smr(inp->inp_pcbinfo->ipi_portzone, phd); 24773ba34b07SGleb Smirnoff } 24783ba34b07SGleb Smirnoff INP_HASH_WUNLOCK(inp->inp_pcbinfo); 24793ba34b07SGleb Smirnoff inp->inp_flags &= ~INP_INHASHLIST; 24803ba34b07SGleb Smirnoff } 24813ba34b07SGleb Smirnoff 248252cd27cbSRobert Watson /* 2483c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2484c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2485c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2486c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2487db0ac6deSCy Schubert * 2488db0ac6deSCy Schubert * XXXGL: a race between this function and SMR-protected hash iterator 2489db0ac6deSCy Schubert * will lead to iterator traversing a possibly wrong hash list. However, 2490db0ac6deSCy Schubert * this race should have been here since change from rwlock to epoch. 2491c3229e05SDavid Greenman */ 249215bd2b43SDavid Greenman void 2493db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp) 249415bd2b43SDavid Greenman { 249559daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 249615bd2b43SDavid Greenman struct inpcbhead *head; 249715bd2b43SDavid Greenman 24988501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2499fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2500fa046d87SRobert Watson 2501111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2502111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2503602cc7f1SRobert Watson 2504cfa1ca9dSYoshinobu Inoue #ifdef INET6 2505cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 2506a0577692SGleb Smirnoff head = &pcbinfo->ipi_hashbase[INP6_PCBHASH(&inp->in6p_faddr, 2507a0577692SGleb Smirnoff inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 2508cfa1ca9dSYoshinobu Inoue else 250983e521ecSBjoern A. Zeeb #endif 2510a0577692SGleb Smirnoff head = &pcbinfo->ipi_hashbase[INP_PCBHASH(&inp->inp_faddr, 2511712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 251215bd2b43SDavid Greenman 2513b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2514b872626dSMatt Macy CK_LIST_INSERT_HEAD(head, inp, inp_hash); 2515c3229e05SDavid Greenman } 2516c3229e05SDavid Greenman 2517c3229e05SDavid Greenman /* 251884cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 251984cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 252084cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 252184cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 252284cc0778SGeorge V. Neville-Neil */ 252384cc0778SGeorge V. Neville-Neil void 252484cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 252584cc0778SGeorge V. Neville-Neil { 252684cc0778SGeorge V. Neville-Neil 2527fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 252884cc0778SGeorge V. Neville-Neil return; 252984cc0778SGeorge V. Neville-Neil } 253084cc0778SGeorge V. Neville-Neil 253184cc0778SGeorge V. Neville-Neil /* 2532a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2533a557af22SRobert Watson * label change into the in_pcb for the socket. 2534a557af22SRobert Watson */ 2535a557af22SRobert Watson void 2536136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2537a557af22SRobert Watson { 2538a557af22SRobert Watson #ifdef MAC 2539a557af22SRobert Watson struct inpcb *inp; 2540a557af22SRobert Watson 25414c7c478dSRobert Watson inp = sotoinpcb(so); 25424c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2543602cc7f1SRobert Watson 25448501a69cSRobert Watson INP_WLOCK(inp); 2545310e7cebSRobert Watson SOCK_LOCK(so); 2546a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2547310e7cebSRobert Watson SOCK_UNLOCK(so); 25488501a69cSRobert Watson INP_WUNLOCK(inp); 2549a557af22SRobert Watson #endif 2550a557af22SRobert Watson } 25515f311da2SMike Silbersack 25523d585327SKip Macy void 25533d585327SKip Macy inp_wlock(struct inpcb *inp) 25543d585327SKip Macy { 25553d585327SKip Macy 25568501a69cSRobert Watson INP_WLOCK(inp); 25573d585327SKip Macy } 25583d585327SKip Macy 25593d585327SKip Macy void 25603d585327SKip Macy inp_wunlock(struct inpcb *inp) 25613d585327SKip Macy { 25623d585327SKip Macy 25638501a69cSRobert Watson INP_WUNLOCK(inp); 25643d585327SKip Macy } 25653d585327SKip Macy 25663d585327SKip Macy void 25673d585327SKip Macy inp_rlock(struct inpcb *inp) 25683d585327SKip Macy { 25693d585327SKip Macy 2570a69042a5SRobert Watson INP_RLOCK(inp); 25713d585327SKip Macy } 25723d585327SKip Macy 25733d585327SKip Macy void 25743d585327SKip Macy inp_runlock(struct inpcb *inp) 25753d585327SKip Macy { 25763d585327SKip Macy 2577a69042a5SRobert Watson INP_RUNLOCK(inp); 25783d585327SKip Macy } 25793d585327SKip Macy 2580c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 25813d585327SKip Macy void 2582e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 25833d585327SKip Macy { 25843d585327SKip Macy 25858501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 25863d585327SKip Macy } 25873d585327SKip Macy 25883d585327SKip Macy void 2589e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 25903d585327SKip Macy { 25913d585327SKip Macy 25923d585327SKip Macy INP_UNLOCK_ASSERT(inp); 25933d585327SKip Macy } 25943d585327SKip Macy #endif 25953d585327SKip Macy 25969378e437SKip Macy void 259724cf7a8dSGleb Smirnoff inp_apply_all(struct inpcbinfo *pcbinfo, 259824cf7a8dSGleb Smirnoff void (*func)(struct inpcb *, void *), void *arg) 25999378e437SKip Macy { 260024cf7a8dSGleb Smirnoff struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2601db0ac6deSCy Schubert INPLOOKUP_WLOCKPCB); 26029378e437SKip Macy struct inpcb *inp; 26039378e437SKip Macy 2604db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) 26059378e437SKip Macy func(inp, arg); 26069378e437SKip Macy } 26079378e437SKip Macy 26089378e437SKip Macy struct socket * 26099378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 26109378e437SKip Macy { 26119378e437SKip Macy 26129378e437SKip Macy INP_WLOCK_ASSERT(inp); 26139378e437SKip Macy return (inp->inp_socket); 26149378e437SKip Macy } 26159378e437SKip Macy 26169378e437SKip Macy struct tcpcb * 26179378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 26189378e437SKip Macy { 26199378e437SKip Macy 26209378e437SKip Macy INP_WLOCK_ASSERT(inp); 26219378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 26229378e437SKip Macy } 26239378e437SKip Macy 26249378e437SKip Macy int 26259378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 26269378e437SKip Macy { 26279378e437SKip Macy 26289378e437SKip Macy return (inp->inp_ip_tos); 26299378e437SKip Macy } 26309378e437SKip Macy 26319378e437SKip Macy void 26329378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 26339378e437SKip Macy { 26349378e437SKip Macy 26359378e437SKip Macy inp->inp_ip_tos = val; 26369378e437SKip Macy } 26379378e437SKip Macy 26389378e437SKip Macy void 2639df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 26409d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 26419378e437SKip Macy { 26429378e437SKip Macy 26439d29c635SKip Macy INP_LOCK_ASSERT(inp); 2644df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2645df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 26469378e437SKip Macy *lp = inp->inp_lport; 26479378e437SKip Macy *fp = inp->inp_fport; 26489378e437SKip Macy } 26499378e437SKip Macy 2650dd0e6c38SKip Macy struct inpcb * 2651dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2652dd0e6c38SKip Macy { 2653dd0e6c38SKip Macy 2654dd0e6c38SKip Macy return (sotoinpcb(so)); 2655dd0e6c38SKip Macy } 2656dd0e6c38SKip Macy 2657cc65eb4eSGleb Smirnoff /* 2658cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2659cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2660cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2661cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2662cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2663cc65eb4eSGleb Smirnoff * hidden from users. 2664cc65eb4eSGleb Smirnoff */ 2665cc65eb4eSGleb Smirnoff void 2666cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2667cc65eb4eSGleb Smirnoff { 2668cc65eb4eSGleb Smirnoff 266979db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2670cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2671cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2672cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2673cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2674cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 26753a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2676cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2677cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2678cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2679cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2680cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2681cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2682cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2683cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2684cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2685cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2686cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2687cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2688cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2689cc65eb4eSGleb Smirnoff } 2690cc65eb4eSGleb Smirnoff 2691a35bdd44SMichael Tuexen int 2692a35bdd44SMichael Tuexen sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, 2693a35bdd44SMichael Tuexen int (*ctloutput_set)(struct inpcb *, struct sockopt *)) 2694a35bdd44SMichael Tuexen { 2695a35bdd44SMichael Tuexen struct sockopt sopt; 2696a35bdd44SMichael Tuexen struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2697a35bdd44SMichael Tuexen INPLOOKUP_WLOCKPCB); 2698a35bdd44SMichael Tuexen struct inpcb *inp; 2699a35bdd44SMichael Tuexen struct sockopt_parameters *params; 2700a35bdd44SMichael Tuexen struct socket *so; 2701a35bdd44SMichael Tuexen int error; 2702a35bdd44SMichael Tuexen char buf[1024]; 2703a35bdd44SMichael Tuexen 2704a35bdd44SMichael Tuexen if (req->oldptr != NULL || req->oldlen != 0) 2705a35bdd44SMichael Tuexen return (EINVAL); 2706a35bdd44SMichael Tuexen if (req->newptr == NULL) 2707a35bdd44SMichael Tuexen return (EPERM); 2708a35bdd44SMichael Tuexen if (req->newlen > sizeof(buf)) 2709a35bdd44SMichael Tuexen return (ENOMEM); 2710a35bdd44SMichael Tuexen error = SYSCTL_IN(req, buf, req->newlen); 2711a35bdd44SMichael Tuexen if (error != 0) 2712a35bdd44SMichael Tuexen return (error); 2713a35bdd44SMichael Tuexen if (req->newlen < sizeof(struct sockopt_parameters)) 2714a35bdd44SMichael Tuexen return (EINVAL); 2715a35bdd44SMichael Tuexen params = (struct sockopt_parameters *)buf; 2716a35bdd44SMichael Tuexen sopt.sopt_level = params->sop_level; 2717a35bdd44SMichael Tuexen sopt.sopt_name = params->sop_optname; 2718a35bdd44SMichael Tuexen sopt.sopt_dir = SOPT_SET; 2719a35bdd44SMichael Tuexen sopt.sopt_val = params->sop_optval; 2720a35bdd44SMichael Tuexen sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters); 2721a35bdd44SMichael Tuexen sopt.sopt_td = NULL; 2722a0aeb1ceSMichael Tuexen #ifdef INET6 2723a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) { 2724a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_laddr)) 2725a35bdd44SMichael Tuexen params->sop_inc.inc6_laddr.s6_addr16[1] = 2726a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2727a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_faddr)) 2728a35bdd44SMichael Tuexen params->sop_inc.inc6_faddr.s6_addr16[1] = 2729a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2730a35bdd44SMichael Tuexen } 2731a0aeb1ceSMichael Tuexen #endif 2732a35bdd44SMichael Tuexen if (params->sop_inc.inc_lport != htons(0)) { 2733a35bdd44SMichael Tuexen if (params->sop_inc.inc_fport == htons(0)) 2734a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH_WILD(params->sop_inc.inc_lport, 2735a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2736a35bdd44SMichael Tuexen else 2737a0aeb1ceSMichael Tuexen #ifdef INET6 2738a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) 2739a35bdd44SMichael Tuexen inpi.hash = INP6_PCBHASH( 2740a35bdd44SMichael Tuexen ¶ms->sop_inc.inc6_faddr, 2741a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2742a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2743a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2744a35bdd44SMichael Tuexen else 2745a0aeb1ceSMichael Tuexen #endif 2746a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH( 2747a35bdd44SMichael Tuexen ¶ms->sop_inc.inc_faddr, 2748a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2749a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2750a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2751a35bdd44SMichael Tuexen } 2752a35bdd44SMichael Tuexen while ((inp = inp_next(&inpi)) != NULL) 2753a35bdd44SMichael Tuexen if (inp->inp_gencnt == params->sop_id) { 275453af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 2755a35bdd44SMichael Tuexen INP_WUNLOCK(inp); 2756a35bdd44SMichael Tuexen return (ECONNRESET); 2757a35bdd44SMichael Tuexen } 2758a35bdd44SMichael Tuexen so = inp->inp_socket; 2759a35bdd44SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 2760a35bdd44SMichael Tuexen soref(so); 2761a35bdd44SMichael Tuexen error = (*ctloutput_set)(inp, &sopt); 2762a35bdd44SMichael Tuexen sorele(so); 2763a35bdd44SMichael Tuexen break; 2764a35bdd44SMichael Tuexen } 2765a35bdd44SMichael Tuexen if (inp == NULL) 2766a35bdd44SMichael Tuexen error = ESRCH; 2767a35bdd44SMichael Tuexen return (error); 2768a35bdd44SMichael Tuexen } 2769a35bdd44SMichael Tuexen 2770497057eeSRobert Watson #ifdef DDB 2771497057eeSRobert Watson static void 2772497057eeSRobert Watson db_print_indent(int indent) 2773497057eeSRobert Watson { 2774497057eeSRobert Watson int i; 2775497057eeSRobert Watson 2776497057eeSRobert Watson for (i = 0; i < indent; i++) 2777497057eeSRobert Watson db_printf(" "); 2778497057eeSRobert Watson } 2779497057eeSRobert Watson 2780497057eeSRobert Watson static void 2781497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2782497057eeSRobert Watson { 2783497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2784497057eeSRobert Watson 2785497057eeSRobert Watson db_print_indent(indent); 2786497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2787497057eeSRobert Watson 2788497057eeSRobert Watson indent += 2; 2789497057eeSRobert Watson 279003dc38a4SRobert Watson #ifdef INET6 2791dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2792497057eeSRobert Watson /* IPv6. */ 2793497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2794497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 279583e521ecSBjoern A. Zeeb } else 279603dc38a4SRobert Watson #endif 279783e521ecSBjoern A. Zeeb { 2798497057eeSRobert Watson /* IPv4. */ 2799497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2800497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2801497057eeSRobert Watson } 2802497057eeSRobert Watson db_print_indent(indent); 2803497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2804497057eeSRobert Watson ntohs(inc->inc_lport)); 2805497057eeSRobert Watson db_print_indent(indent); 2806497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2807497057eeSRobert Watson ntohs(inc->inc_fport)); 2808497057eeSRobert Watson } 2809497057eeSRobert Watson 2810497057eeSRobert Watson static void 2811497057eeSRobert Watson db_print_inpflags(int inp_flags) 2812497057eeSRobert Watson { 2813497057eeSRobert Watson int comma; 2814497057eeSRobert Watson 2815497057eeSRobert Watson comma = 0; 2816497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2817497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2818497057eeSRobert Watson comma = 1; 2819497057eeSRobert Watson } 2820497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2821497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2822497057eeSRobert Watson comma = 1; 2823497057eeSRobert Watson } 2824497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2825497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2826497057eeSRobert Watson comma = 1; 2827497057eeSRobert Watson } 2828dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2829dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2830dce33a45SErmal Luçi comma = 1; 2831dce33a45SErmal Luçi } 2832497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2833497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2834497057eeSRobert Watson comma = 1; 2835497057eeSRobert Watson } 2836497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2837497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2838497057eeSRobert Watson comma = 1; 2839497057eeSRobert Watson } 2840497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2841497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2842497057eeSRobert Watson comma = 1; 2843497057eeSRobert Watson } 2844497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2845497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2846497057eeSRobert Watson comma = 1; 2847497057eeSRobert Watson } 2848497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2849497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2850497057eeSRobert Watson comma = 1; 2851497057eeSRobert Watson } 2852497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2853497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2854497057eeSRobert Watson comma = 1; 2855497057eeSRobert Watson } 2856497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2857497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2858497057eeSRobert Watson comma = 1; 2859497057eeSRobert Watson } 2860497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2861497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2862497057eeSRobert Watson comma = 1; 2863497057eeSRobert Watson } 28643cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 28653cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 28663cca425bSMichael Tuexen comma = 1; 28673cca425bSMichael Tuexen } 2868497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2869497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2870497057eeSRobert Watson comma = 1; 2871497057eeSRobert Watson } 2872497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2873497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2874497057eeSRobert Watson comma = 1; 2875497057eeSRobert Watson } 2876497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2877497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2878497057eeSRobert Watson comma = 1; 2879497057eeSRobert Watson } 2880497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2881497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2882497057eeSRobert Watson comma = 1; 2883497057eeSRobert Watson } 2884497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2885497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2886497057eeSRobert Watson comma = 1; 2887497057eeSRobert Watson } 2888497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2889497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2890497057eeSRobert Watson comma = 1; 2891497057eeSRobert Watson } 2892497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2893497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2894497057eeSRobert Watson comma = 1; 2895497057eeSRobert Watson } 2896497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2897497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2898497057eeSRobert Watson comma = 1; 2899497057eeSRobert Watson } 2900497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2901497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2902497057eeSRobert Watson comma = 1; 2903497057eeSRobert Watson } 2904ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2905ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2906ad71fe3cSRobert Watson comma = 1; 2907ad71fe3cSRobert Watson } 2908ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2909ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2910ad71fe3cSRobert Watson comma = 1; 2911ad71fe3cSRobert Watson } 2912ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2913ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2914ad71fe3cSRobert Watson comma = 1; 2915ad71fe3cSRobert Watson } 2916497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2917497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2918497057eeSRobert Watson comma = 1; 2919497057eeSRobert Watson } 2920497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2921497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2922497057eeSRobert Watson comma = 1; 2923497057eeSRobert Watson } 2924497057eeSRobert Watson } 2925497057eeSRobert Watson 2926497057eeSRobert Watson static void 2927497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2928497057eeSRobert Watson { 2929497057eeSRobert Watson int comma; 2930497057eeSRobert Watson 2931497057eeSRobert Watson comma = 0; 2932497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2933497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2934497057eeSRobert Watson comma = 1; 2935497057eeSRobert Watson } 2936497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2937497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2938497057eeSRobert Watson comma = 1; 2939497057eeSRobert Watson } 2940497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2941497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2942497057eeSRobert Watson comma = 1; 2943497057eeSRobert Watson } 2944497057eeSRobert Watson } 2945497057eeSRobert Watson 29466d888973SRobert Watson static void 2947497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2948497057eeSRobert Watson { 2949497057eeSRobert Watson 2950497057eeSRobert Watson db_print_indent(indent); 2951497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2952497057eeSRobert Watson 2953497057eeSRobert Watson indent += 2; 2954497057eeSRobert Watson 2955497057eeSRobert Watson db_print_indent(indent); 2956497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2957497057eeSRobert Watson 2958497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2959497057eeSRobert Watson 2960497057eeSRobert Watson db_print_indent(indent); 2961497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2962497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2963497057eeSRobert Watson 2964497057eeSRobert Watson db_print_indent(indent); 2965497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2966497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2967497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2968497057eeSRobert Watson db_printf(")\n"); 2969497057eeSRobert Watson 2970497057eeSRobert Watson db_print_indent(indent); 2971497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2972497057eeSRobert Watson inp->inp_vflag); 2973497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2974497057eeSRobert Watson db_printf(")\n"); 2975497057eeSRobert Watson 2976497057eeSRobert Watson db_print_indent(indent); 2977497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2978497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2979497057eeSRobert Watson 2980497057eeSRobert Watson db_print_indent(indent); 2981497057eeSRobert Watson #ifdef INET6 2982497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2983497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2984497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2985497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2986497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2987497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2988497057eeSRobert Watson inp->in6p_hops); 2989497057eeSRobert Watson } else 2990497057eeSRobert Watson #endif 2991497057eeSRobert Watson { 2992497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2993497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2994497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2995497057eeSRobert Watson } 2996497057eeSRobert Watson 2997497057eeSRobert Watson db_print_indent(indent); 2998497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2999497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3000497057eeSRobert Watson } 3001497057eeSRobert Watson 3002497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3003497057eeSRobert Watson { 3004497057eeSRobert Watson struct inpcb *inp; 3005497057eeSRobert Watson 3006497057eeSRobert Watson if (!have_addr) { 3007497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3008497057eeSRobert Watson return; 3009497057eeSRobert Watson } 3010497057eeSRobert Watson inp = (struct inpcb *)addr; 3011497057eeSRobert Watson 3012497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3013497057eeSRobert Watson } 301483e521ecSBjoern A. Zeeb #endif /* DDB */ 3015f3e7afe2SHans Petter Selasky 3016f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3017f3e7afe2SHans Petter Selasky /* 3018f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3019f3e7afe2SHans Petter Selasky * if any. 3020f3e7afe2SHans Petter Selasky */ 3021f3e7afe2SHans Petter Selasky int 3022f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3023f3e7afe2SHans Petter Selasky { 3024f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3025f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 302620abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3027f3e7afe2SHans Petter Selasky }; 3028f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3029f3e7afe2SHans Petter Selasky int error; 3030f3e7afe2SHans Petter Selasky 3031f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3032f3e7afe2SHans Petter Selasky if (mst == NULL) 3033f3e7afe2SHans Petter Selasky return (EINVAL); 3034f3e7afe2SHans Petter Selasky 3035c782ea8bSJohn Baldwin if (mst->sw->snd_tag_modify == NULL) { 3036f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3037f3e7afe2SHans Petter Selasky } else { 3038c782ea8bSJohn Baldwin error = mst->sw->snd_tag_modify(mst, ¶ms); 3039f3e7afe2SHans Petter Selasky } 3040f3e7afe2SHans Petter Selasky return (error); 3041f3e7afe2SHans Petter Selasky } 3042f3e7afe2SHans Petter Selasky 3043f3e7afe2SHans Petter Selasky /* 3044f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3045f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3046f3e7afe2SHans Petter Selasky */ 3047f3e7afe2SHans Petter Selasky int 3048f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3049f3e7afe2SHans Petter Selasky { 3050f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3051f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3052f3e7afe2SHans Petter Selasky int error; 3053f3e7afe2SHans Petter Selasky 3054f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3055f3e7afe2SHans Petter Selasky if (mst == NULL) 3056f3e7afe2SHans Petter Selasky return (EINVAL); 3057f3e7afe2SHans Petter Selasky 3058c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) { 3059f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3060f3e7afe2SHans Petter Selasky } else { 3061c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 3062f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3063f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3064f3e7afe2SHans Petter Selasky } 3065f3e7afe2SHans Petter Selasky return (error); 3066f3e7afe2SHans Petter Selasky } 3067f3e7afe2SHans Petter Selasky 3068f3e7afe2SHans Petter Selasky /* 306995ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 307095ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 307195ed5015SHans Petter Selasky */ 307295ed5015SHans Petter Selasky int 307395ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 307495ed5015SHans Petter Selasky { 307595ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 307695ed5015SHans Petter Selasky struct m_snd_tag *mst; 307795ed5015SHans Petter Selasky int error; 307895ed5015SHans Petter Selasky 307995ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 308095ed5015SHans Petter Selasky if (mst == NULL) 308195ed5015SHans Petter Selasky return (EINVAL); 308295ed5015SHans Petter Selasky 3083c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) 308495ed5015SHans Petter Selasky return (EOPNOTSUPP); 308595ed5015SHans Petter Selasky 3086c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 308795ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 308895ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 308995ed5015SHans Petter Selasky return (error); 309095ed5015SHans Petter Selasky } 309195ed5015SHans Petter Selasky 309295ed5015SHans Petter Selasky /* 3093f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3094f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3095f3e7afe2SHans Petter Selasky */ 3096f3e7afe2SHans Petter Selasky int 3097f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 309820abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 309920abea66SRandall Stewart 3100f3e7afe2SHans Petter Selasky { 3101f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 310295ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 310395ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3104f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3105f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 310698085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3107f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 310820abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3109f3e7afe2SHans Petter Selasky }; 3110f3e7afe2SHans Petter Selasky int error; 3111f3e7afe2SHans Petter Selasky 3112f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3113f3e7afe2SHans Petter Selasky 311485d8d30fSHans Petter Selasky /* 311585d8d30fSHans Petter Selasky * If there is already a send tag, or the INP is being torn 311685d8d30fSHans Petter Selasky * down, allocating a new send tag is not allowed. Else send 311785d8d30fSHans Petter Selasky * tags may leak. 311885d8d30fSHans Petter Selasky */ 311953af6903SGleb Smirnoff if (*st != NULL || (inp->inp_flags & INP_DROPPED) != 0) 3120f3e7afe2SHans Petter Selasky return (EINVAL); 3121f3e7afe2SHans Petter Selasky 312236e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, st); 3123903c4ee6SXin LI #ifdef INET 312420abea66SRandall Stewart if (error == 0) { 312520abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 312620abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 312736e0a362SJohn Baldwin } else if (error != EOPNOTSUPP) 312820abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3129903c4ee6SXin LI #endif 3130f3e7afe2SHans Petter Selasky return (error); 3131f3e7afe2SHans Petter Selasky } 3132f3e7afe2SHans Petter Selasky 313320abea66SRandall Stewart void 313498d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst) 313520abea66SRandall Stewart { 313620abea66SRandall Stewart 313798d7a8d9SJohn Baldwin m_snd_tag_rele(mst); 3138903c4ee6SXin LI #ifdef INET 313920abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3140903c4ee6SXin LI #endif 314120abea66SRandall Stewart } 314220abea66SRandall Stewart 3143f3e7afe2SHans Petter Selasky /* 3144f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3145f3e7afe2SHans Petter Selasky * if any: 3146f3e7afe2SHans Petter Selasky */ 3147f3e7afe2SHans Petter Selasky void 3148f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3149f3e7afe2SHans Petter Selasky { 3150f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3151f3e7afe2SHans Petter Selasky 3152f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3153f3e7afe2SHans Petter Selasky 3154f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3155f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3156f3e7afe2SHans Petter Selasky 3157f3e7afe2SHans Petter Selasky if (mst == NULL) 3158f3e7afe2SHans Petter Selasky return; 3159f3e7afe2SHans Petter Selasky 3160fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3161093e7231SHans Petter Selasky #ifdef INET 3162093e7231SHans Petter Selasky counter_u64_add(rate_limit_active, -1); 3163093e7231SHans Petter Selasky #endif 3164f3e7afe2SHans Petter Selasky } 3165f3e7afe2SHans Petter Selasky 316620abea66SRandall Stewart int 316720abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 316820abea66SRandall Stewart { 316920abea66SRandall Stewart int error; 317020abea66SRandall Stewart 317120abea66SRandall Stewart /* 317220abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 317320abea66SRandall Stewart * a route change, first drop the existing tag. Set the 317420abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 317520abea66SRandall Stewart * tag if we fail to allocate one this time. 317620abea66SRandall Stewart */ 317720abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 317820abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 317920abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 318020abea66SRandall Stewart } 318120abea66SRandall Stewart 318220abea66SRandall Stewart /* 318320abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 318420abea66SRandall Stewart * made to ensure the network interface doesn't go away until 318520abea66SRandall Stewart * all ratelimit connections are gone. The network interface 318620abea66SRandall Stewart * pointers compared below represent valid network interfaces, 318720abea66SRandall Stewart * except when comparing towards NULL. 318820abea66SRandall Stewart */ 318920abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 319020abea66SRandall Stewart error = 0; 319120abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 319220abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 319320abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 319420abea66SRandall Stewart error = 0; 319520abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 319620abea66SRandall Stewart /* 319720abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 319820abea66SRandall Stewart * to wait until there is a valid RSS hash before we 319920abea66SRandall Stewart * can proceed: 320020abea66SRandall Stewart */ 320120abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 320220abea66SRandall Stewart error = EAGAIN; 320320abea66SRandall Stewart } else { 320420abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 320520abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 320620abea66SRandall Stewart } 320720abea66SRandall Stewart } else { 320820abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 320920abea66SRandall Stewart } 321020abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 321120abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 321220abea66SRandall Stewart 321320abea66SRandall Stewart return (error); 321420abea66SRandall Stewart } 321520abea66SRandall Stewart 3216f3e7afe2SHans Petter Selasky /* 3217f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3218f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3219f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3220f3e7afe2SHans Petter Selasky */ 3221f3e7afe2SHans Petter Selasky void 3222f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3223f3e7afe2SHans Petter Selasky { 3224f3e7afe2SHans Petter Selasky struct socket *socket; 3225f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3226f3e7afe2SHans Petter Selasky bool did_upgrade; 3227f3e7afe2SHans Petter Selasky 3228f3e7afe2SHans Petter Selasky if (inp == NULL) 3229f3e7afe2SHans Petter Selasky return; 3230f3e7afe2SHans Petter Selasky 3231f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3232f3e7afe2SHans Petter Selasky if (socket == NULL) 3233f3e7afe2SHans Petter Selasky return; 3234f3e7afe2SHans Petter Selasky 3235f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3236f3e7afe2SHans Petter Selasky /* 3237f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3238f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3239f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3240f3e7afe2SHans Petter Selasky * write lock. 3241f3e7afe2SHans Petter Selasky */ 3242f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3243f3e7afe2SHans Petter Selasky return; 3244f3e7afe2SHans Petter Selasky did_upgrade = 1; 3245f3e7afe2SHans Petter Selasky } else { 3246f3e7afe2SHans Petter Selasky did_upgrade = 0; 3247f3e7afe2SHans Petter Selasky } 3248f3e7afe2SHans Petter Selasky 3249f3e7afe2SHans Petter Selasky /* 3250f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3251f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3252f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3253f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3254f3e7afe2SHans Petter Selasky */ 3255f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3256f3e7afe2SHans Petter Selasky 3257bab34d63SJohn Baldwin in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3258fb3bc596SJohn Baldwin 3259f3e7afe2SHans Petter Selasky if (did_upgrade) 3260f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3261f3e7afe2SHans Petter Selasky } 3262f3e7afe2SHans Petter Selasky 3263f3e7afe2SHans Petter Selasky /* 3264f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3265f3e7afe2SHans Petter Selasky */ 3266f3e7afe2SHans Petter Selasky void 3267f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3268f3e7afe2SHans Petter Selasky { 3269f3e7afe2SHans Petter Selasky bool did_upgrade; 3270f3e7afe2SHans Petter Selasky 3271f3e7afe2SHans Petter Selasky if (inp == NULL) 3272f3e7afe2SHans Petter Selasky return; 3273f3e7afe2SHans Petter Selasky 3274f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3275f3e7afe2SHans Petter Selasky return; 3276f3e7afe2SHans Petter Selasky 3277f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3278f3e7afe2SHans Petter Selasky /* 3279f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3280f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3281f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3282f3e7afe2SHans Petter Selasky * write lock. 3283f3e7afe2SHans Petter Selasky */ 3284f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3285f3e7afe2SHans Petter Selasky return; 3286f3e7afe2SHans Petter Selasky did_upgrade = 1; 3287f3e7afe2SHans Petter Selasky } else { 3288f3e7afe2SHans Petter Selasky did_upgrade = 0; 3289f3e7afe2SHans Petter Selasky } 3290f3e7afe2SHans Petter Selasky 3291f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3292f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3293f3e7afe2SHans Petter Selasky 3294f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3295f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3296f3e7afe2SHans Petter Selasky 3297f3e7afe2SHans Petter Selasky if (did_upgrade) 3298f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3299f3e7afe2SHans Petter Selasky } 330020abea66SRandall Stewart 3301903c4ee6SXin LI #ifdef INET 330220abea66SRandall Stewart static void 330320abea66SRandall Stewart rl_init(void *st) 330420abea66SRandall Stewart { 33051a714ff2SRandall Stewart rate_limit_new = counter_u64_alloc(M_WAITOK); 33061a714ff2SRandall Stewart rate_limit_chg = counter_u64_alloc(M_WAITOK); 330720abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 330820abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 330920abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 331020abea66SRandall Stewart } 331120abea66SRandall Stewart 331220abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3313903c4ee6SXin LI #endif 3314f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3315