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> 61fdb987beSMark Johnston #include <sys/proc.h> 62df8bae1dSRodney W. Grimes #include <sys/protosw.h> 633ee9c3c4SRandall Stewart #include <sys/smp.h> 64fdb987beSMark Johnston #include <sys/smr.h> 65df8bae1dSRodney W. Grimes #include <sys/socket.h> 66df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 67f3e7afe2SHans Petter Selasky #include <sys/sockio.h> 68acd3428bSRobert Watson #include <sys/priv.h> 69df8bae1dSRodney W. Grimes #include <sys/proc.h> 7079bdc6e5SRobert Watson #include <sys/refcount.h> 7175c13541SPoul-Henning Kamp #include <sys/jail.h> 72101f9fc8SPeter Wemm #include <sys/kernel.h> 73101f9fc8SPeter Wemm #include <sys/sysctl.h> 748781d8e9SBruce Evans 75497057eeSRobert Watson #ifdef DDB 76497057eeSRobert Watson #include <ddb/ddb.h> 77497057eeSRobert Watson #endif 78497057eeSRobert Watson 7969c2d429SJeff Roberson #include <vm/uma.h> 80a034518aSAndrew Gallatin #include <vm/vm.h> 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes #include <net/if.h> 8376039bc8SGleb Smirnoff #include <net/if_var.h> 843d0d5b21SJustin Hibbits #include <net/if_private.h> 85cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 866d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h> 87df8bae1dSRodney W. Grimes #include <net/route.h> 88b2bdc62aSAdrian Chadd #include <net/rss_config.h> 89530c0060SRobert Watson #include <net/vnet.h> 90df8bae1dSRodney W. Grimes 9167107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 92df8bae1dSRodney W. Grimes #include <netinet/in.h> 93df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 940f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h> 9573bebcc5SGleb Smirnoff #include <netinet/tcp.h> 9659854ecfSHans Petter Selasky #ifdef INET 9759854ecfSHans Petter Selasky #include <netinet/in_var.h> 989ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h> 9959854ecfSHans Petter Selasky #endif 100df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 101cfa1ca9dSYoshinobu Inoue #ifdef INET6 102cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 103efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 10467107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 10567107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 106cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1079ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h> 10859854ecfSHans Petter Selasky #endif 109cfa1ca9dSYoshinobu Inoue 110fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 111b9234fafSSam Leffler 112aed55708SRobert Watson #include <security/mac/mac_framework.h> 113aed55708SRobert Watson 1141a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 1151a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 116db0ac6deSCy Schubert #define INP_FREED 0x00000200 /* See in_pcb.h. */ 1171a43cff9SSean Bruno 118101f9fc8SPeter Wemm /* 119101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 120101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 121101f9fc8SPeter Wemm */ 122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 128101f9fc8SPeter Wemm 129b0d22693SCrist J. Clark /* 130b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 131b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 132b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 133b0d22693SCrist J. Clark */ 134eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 135eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 136b0d22693SCrist J. Clark 13719acc506SGleb Smirnoff /* Enable random ephemeral port allocation by default. */ 13819acc506SGleb Smirnoff VNET_DEFINE(int, ipport_randomized) = 1; 1396ac48b74SMike Silbersack 140d2025bd0SBjoern A. Zeeb #ifdef INET 141fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 142fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 143fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 1444130ea61SMark Johnston int lookupflags, uint8_t numa_domain); 14567107f45SBjoern A. Zeeb 146bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 147bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 148bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 149bbd42ad0SPeter Wemm 150bbd42ad0SPeter Wemm static int 15182d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 152bbd42ad0SPeter Wemm { 15330a4ab08SBruce Evans int error; 15430a4ab08SBruce Evans 155f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 15630a4ab08SBruce Evans if (error == 0) { 157603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 158603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 159603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 160603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 161603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 162603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 163bbd42ad0SPeter Wemm } 16430a4ab08SBruce Evans return (error); 165bbd42ad0SPeter Wemm } 166bbd42ad0SPeter Wemm 167bbd42ad0SPeter Wemm #undef RANGECHK 168bbd42ad0SPeter Wemm 1697029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, 1707029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1716472ac3dSEd Schouten "IP Ports"); 17233b3ac06SPeter Wemm 1736df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1747029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1757029da5cSPawel Biernacki &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", 1767029da5cSPawel Biernacki ""); 1776df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1787029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1797029da5cSPawel Biernacki &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", 1807029da5cSPawel Biernacki ""); 1816df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1827029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1837029da5cSPawel Biernacki &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", 1847029da5cSPawel Biernacki ""); 1856df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1867029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1877029da5cSPawel Biernacki &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", 1887029da5cSPawel Biernacki ""); 1896df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1907029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1917029da5cSPawel Biernacki &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", 1927029da5cSPawel Biernacki ""); 1936df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 1947029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1957029da5cSPawel Biernacki &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", 1967029da5cSPawel Biernacki ""); 1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 1986df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 1996df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 201eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 2026df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 2036df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 204eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 20520abea66SRandall Stewart 20620abea66SRandall Stewart #ifdef RATELIMIT 2071a714ff2SRandall Stewart counter_u64_t rate_limit_new; 2081a714ff2SRandall Stewart counter_u64_t rate_limit_chg; 20920abea66SRandall Stewart counter_u64_t rate_limit_active; 21020abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail; 21120abea66SRandall Stewart counter_u64_t rate_limit_set_ok; 21220abea66SRandall Stewart 2137029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 21420abea66SRandall Stewart "IP Rate Limiting"); 21520abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD, 21620abea66SRandall Stewart &rate_limit_active, "Active rate limited connections"); 21720abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD, 21820abea66SRandall Stewart &rate_limit_alloc_fail, "Rate limited connection failures"); 21920abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD, 22020abea66SRandall Stewart &rate_limit_set_ok, "Rate limited setting succeeded"); 2211a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD, 2221a714ff2SRandall Stewart &rate_limit_new, "Total Rate limit new attempts"); 2231a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD, 2241a714ff2SRandall Stewart &rate_limit_chg, "Total Rate limited change attempts"); 2251a714ff2SRandall Stewart 22620abea66SRandall Stewart #endif /* RATELIMIT */ 22720abea66SRandall Stewart 22883e521ecSBjoern A. Zeeb #endif /* INET */ 2290312fbe9SPoul-Henning Kamp 230a0577692SGleb Smirnoff VNET_DEFINE(uint32_t, in_pcbhashseed); 231a0577692SGleb Smirnoff static void 232a0577692SGleb Smirnoff in_pcbhashseed_init(void) 233a0577692SGleb Smirnoff { 234a0577692SGleb Smirnoff 235a0577692SGleb Smirnoff V_in_pcbhashseed = arc4random(); 236a0577692SGleb Smirnoff } 237a0577692SGleb Smirnoff VNET_SYSINIT(in_pcbhashseed_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 238a0577692SGleb Smirnoff in_pcbhashseed_init, 0); 239a0577692SGleb Smirnoff 2403ba34b07SGleb Smirnoff static void in_pcbremhash(struct inpcb *); 2413ba34b07SGleb Smirnoff 242c3229e05SDavid Greenman /* 243c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 244c3229e05SDavid Greenman * 245de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 246de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 247de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 248c3229e05SDavid Greenman */ 249c3229e05SDavid Greenman 2501a43cff9SSean Bruno static struct inpcblbgroup * 251d93ec8cbSMark Johnston in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, struct ucred *cred, 252d93ec8cbSMark Johnston u_char vflag, uint16_t port, const union in_dependaddr *addr, int size, 253a034518aSAndrew Gallatin uint8_t numa_domain) 2541a43cff9SSean Bruno { 2551a43cff9SSean Bruno struct inpcblbgroup *grp; 2561a43cff9SSean Bruno size_t bytes; 2571a43cff9SSean Bruno 2581a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 2591a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 260d93ec8cbSMark Johnston if (grp == NULL) 2611a43cff9SSean Bruno return (NULL); 262d93ec8cbSMark Johnston grp->il_cred = crhold(cred); 2631a43cff9SSean Bruno grp->il_vflag = vflag; 2641a43cff9SSean Bruno grp->il_lport = port; 265a034518aSAndrew Gallatin grp->il_numa_domain = numa_domain; 2661a43cff9SSean Bruno grp->il_dependladdr = *addr; 2671a43cff9SSean Bruno grp->il_inpsiz = size; 26854af3d0dSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 2691a43cff9SSean Bruno return (grp); 2701a43cff9SSean Bruno } 2711a43cff9SSean Bruno 2721a43cff9SSean Bruno static void 27354af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx) 27454af3d0dSMark Johnston { 27554af3d0dSMark Johnston struct inpcblbgroup *grp; 27654af3d0dSMark Johnston 27754af3d0dSMark Johnston grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 278d93ec8cbSMark Johnston crfree(grp->il_cred); 27954af3d0dSMark Johnston free(grp, M_PCB); 28054af3d0dSMark Johnston } 28154af3d0dSMark Johnston 28254af3d0dSMark Johnston static void 2831a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 2841a43cff9SSean Bruno { 2851a43cff9SSean Bruno 28654af3d0dSMark Johnston CK_LIST_REMOVE(grp, il_list); 2872a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx); 2881a43cff9SSean Bruno } 2891a43cff9SSean Bruno 2901a43cff9SSean Bruno static struct inpcblbgroup * 2911a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 2921a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 2931a43cff9SSean Bruno { 2941a43cff9SSean Bruno struct inpcblbgroup *grp; 2951a43cff9SSean Bruno int i; 2961a43cff9SSean Bruno 297d93ec8cbSMark Johnston grp = in_pcblbgroup_alloc(hdr, old_grp->il_cred, old_grp->il_vflag, 298a034518aSAndrew Gallatin old_grp->il_lport, &old_grp->il_dependladdr, size, 299a034518aSAndrew Gallatin old_grp->il_numa_domain); 30079ee680bSMark Johnston if (grp == NULL) 3011a43cff9SSean Bruno return (NULL); 3021a43cff9SSean Bruno 3031a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 3041a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 3051a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 3061a43cff9SSean Bruno 3071a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 3081a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 3091a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 3101a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 3111a43cff9SSean Bruno return (grp); 3121a43cff9SSean Bruno } 3131a43cff9SSean Bruno 3141a43cff9SSean Bruno /* 3151a43cff9SSean Bruno * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 3161a43cff9SSean Bruno * and shrink group if possible. 3171a43cff9SSean Bruno */ 3181a43cff9SSean Bruno static void 3191a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 3201a43cff9SSean Bruno int i) 3211a43cff9SSean Bruno { 32279ee680bSMark Johnston struct inpcblbgroup *grp, *new_grp; 3231a43cff9SSean Bruno 32479ee680bSMark Johnston grp = *grpp; 3251a43cff9SSean Bruno for (; i + 1 < grp->il_inpcnt; ++i) 3261a43cff9SSean Bruno grp->il_inp[i] = grp->il_inp[i + 1]; 3271a43cff9SSean Bruno grp->il_inpcnt--; 3281a43cff9SSean Bruno 3291a43cff9SSean Bruno if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 33079ee680bSMark Johnston grp->il_inpcnt <= grp->il_inpsiz / 4) { 3311a43cff9SSean Bruno /* Shrink this group. */ 33279ee680bSMark Johnston new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 33379ee680bSMark Johnston if (new_grp != NULL) 3341a43cff9SSean Bruno *grpp = new_grp; 3351a43cff9SSean Bruno } 3361a43cff9SSean Bruno } 3371a43cff9SSean Bruno 3381a43cff9SSean Bruno /* 3391a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 3401a43cff9SSean Bruno */ 3411a43cff9SSean Bruno static int 342a034518aSAndrew Gallatin in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain) 3431a43cff9SSean Bruno { 344a7026c7fSMark Johnston const static struct timeval interval = { 60, 0 }; 345a7026c7fSMark Johnston static struct timeval lastprint; 3461a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3471a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3481a43cff9SSean Bruno struct inpcblbgroup *grp; 34979ee680bSMark Johnston uint32_t idx; 3501a43cff9SSean Bruno 3511a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 3521a43cff9SSean Bruno 3531a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 3541a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 3551a43cff9SSean Bruno 3561a43cff9SSean Bruno #ifdef INET6 3571a43cff9SSean Bruno /* 3581a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 3591a43cff9SSean Bruno */ 3601a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 3611a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 3621a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 3631a43cff9SSean Bruno return (0); 3641a43cff9SSean Bruno } 3651a43cff9SSean Bruno #endif 3661a43cff9SSean Bruno 3679d2877fcSMark Johnston idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask); 36879ee680bSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 36954af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 370d93ec8cbSMark Johnston if (grp->il_cred->cr_prison == inp->inp_cred->cr_prison && 371d93ec8cbSMark Johnston grp->il_vflag == inp->inp_vflag && 3721a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 373a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain && 3741a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 3751a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 376d93ec8cbSMark Johnston sizeof(grp->il_dependladdr)) == 0) { 3771a43cff9SSean Bruno break; 3781a43cff9SSean Bruno } 379d93ec8cbSMark Johnston } 3801a43cff9SSean Bruno if (grp == NULL) { 3811a43cff9SSean Bruno /* Create new load balance group. */ 382d93ec8cbSMark Johnston grp = in_pcblbgroup_alloc(hdr, inp->inp_cred, inp->inp_vflag, 3831a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 384a034518aSAndrew Gallatin INPCBLBGROUP_SIZMIN, numa_domain); 38579ee680bSMark Johnston if (grp == NULL) 3861a43cff9SSean Bruno return (ENOBUFS); 3871a43cff9SSean Bruno } else if (grp->il_inpcnt == grp->il_inpsiz) { 3881a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 389a7026c7fSMark Johnston if (ratecheck(&lastprint, &interval)) 3901a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 3911a43cff9SSean Bruno ntohs(grp->il_lport)); 3921a43cff9SSean Bruno return (0); 3931a43cff9SSean Bruno } 3941a43cff9SSean Bruno 3951a43cff9SSean Bruno /* Expand this local group. */ 3961a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 39779ee680bSMark Johnston if (grp == NULL) 3981a43cff9SSean Bruno return (ENOBUFS); 3991a43cff9SSean Bruno } 4001a43cff9SSean Bruno 4011a43cff9SSean Bruno KASSERT(grp->il_inpcnt < grp->il_inpsiz, 40279ee680bSMark Johnston ("invalid local group size %d and count %d", grp->il_inpsiz, 40379ee680bSMark Johnston grp->il_inpcnt)); 4041a43cff9SSean Bruno 4051a43cff9SSean Bruno grp->il_inp[grp->il_inpcnt] = inp; 4061a43cff9SSean Bruno grp->il_inpcnt++; 4071a43cff9SSean Bruno return (0); 4081a43cff9SSean Bruno } 4091a43cff9SSean Bruno 4101a43cff9SSean Bruno /* 4111a43cff9SSean Bruno * Remove PCB from load balance group. 4121a43cff9SSean Bruno */ 4131a43cff9SSean Bruno static void 4141a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 4151a43cff9SSean Bruno { 4161a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 4171a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 4181a43cff9SSean Bruno struct inpcblbgroup *grp; 4191a43cff9SSean Bruno int i; 4201a43cff9SSean Bruno 4211a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 4221a43cff9SSean Bruno 4231a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 4241a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 4251a43cff9SSean Bruno 4261a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 4279d2877fcSMark Johnston INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 42854af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 4291a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 4301a43cff9SSean Bruno if (grp->il_inp[i] != inp) 4311a43cff9SSean Bruno continue; 4321a43cff9SSean Bruno 4331a43cff9SSean Bruno if (grp->il_inpcnt == 1) { 4341a43cff9SSean Bruno /* We are the last, free this local group. */ 4351a43cff9SSean Bruno in_pcblbgroup_free(grp); 4361a43cff9SSean Bruno } else { 4371a43cff9SSean Bruno /* Pull up inpcbs, shrink group if possible. */ 4381a43cff9SSean Bruno in_pcblbgroup_reorder(hdr, &grp, i); 4391a43cff9SSean Bruno } 4401a43cff9SSean Bruno return; 4411a43cff9SSean Bruno } 4421a43cff9SSean Bruno } 4431a43cff9SSean Bruno } 4441a43cff9SSean Bruno 445a034518aSAndrew Gallatin int 446a034518aSAndrew Gallatin in_pcblbgroup_numa(struct inpcb *inp, int arg) 447a034518aSAndrew Gallatin { 448a034518aSAndrew Gallatin struct inpcbinfo *pcbinfo; 449a034518aSAndrew Gallatin struct inpcblbgrouphead *hdr; 450a034518aSAndrew Gallatin struct inpcblbgroup *grp; 451a034518aSAndrew Gallatin int err, i; 452a034518aSAndrew Gallatin uint8_t numa_domain; 453a034518aSAndrew Gallatin 454a034518aSAndrew Gallatin switch (arg) { 455a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_NODOM: 456a034518aSAndrew Gallatin numa_domain = M_NODOM; 457a034518aSAndrew Gallatin break; 458a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_CURDOM: 459a034518aSAndrew Gallatin numa_domain = PCPU_GET(domain); 460a034518aSAndrew Gallatin break; 461a034518aSAndrew Gallatin default: 462a034518aSAndrew Gallatin if (arg < 0 || arg >= vm_ndomains) 463a034518aSAndrew Gallatin return (EINVAL); 464a034518aSAndrew Gallatin numa_domain = arg; 465a034518aSAndrew Gallatin } 466a034518aSAndrew Gallatin 467a034518aSAndrew Gallatin err = 0; 468a034518aSAndrew Gallatin pcbinfo = inp->inp_pcbinfo; 469a034518aSAndrew Gallatin INP_WLOCK_ASSERT(inp); 470a034518aSAndrew Gallatin INP_HASH_WLOCK(pcbinfo); 471a034518aSAndrew Gallatin hdr = &pcbinfo->ipi_lbgrouphashbase[ 472a034518aSAndrew Gallatin INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 473a034518aSAndrew Gallatin CK_LIST_FOREACH(grp, hdr, il_list) { 474a034518aSAndrew Gallatin for (i = 0; i < grp->il_inpcnt; ++i) { 475a034518aSAndrew Gallatin if (grp->il_inp[i] != inp) 476a034518aSAndrew Gallatin continue; 477a034518aSAndrew Gallatin 478a034518aSAndrew Gallatin if (grp->il_numa_domain == numa_domain) { 479a034518aSAndrew Gallatin goto abort_with_hash_wlock; 480a034518aSAndrew Gallatin } 481a034518aSAndrew Gallatin 482a034518aSAndrew Gallatin /* Remove it from the old group. */ 483a034518aSAndrew Gallatin in_pcbremlbgrouphash(inp); 484a034518aSAndrew Gallatin 485a034518aSAndrew Gallatin /* Add it to the new group based on numa domain. */ 486a034518aSAndrew Gallatin in_pcbinslbgrouphash(inp, numa_domain); 487a034518aSAndrew Gallatin goto abort_with_hash_wlock; 488a034518aSAndrew Gallatin } 489a034518aSAndrew Gallatin } 490a034518aSAndrew Gallatin err = ENOENT; 491a034518aSAndrew Gallatin abort_with_hash_wlock: 492a034518aSAndrew Gallatin INP_HASH_WUNLOCK(pcbinfo); 493a034518aSAndrew Gallatin return (err); 494a034518aSAndrew Gallatin } 495a034518aSAndrew Gallatin 496db0ac6deSCy Schubert /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 497db0ac6deSCy Schubert CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb))); 498db0ac6deSCy Schubert 499cc487c16SGleb Smirnoff /* 500fec8a8c7SGleb Smirnoff * Initialize an inpcbinfo - a per-VNET instance of connections db. 5019bcd427bSRobert Watson */ 5029bcd427bSRobert Watson void 503fec8a8c7SGleb Smirnoff in_pcbinfo_init(struct inpcbinfo *pcbinfo, struct inpcbstorage *pcbstor, 504fec8a8c7SGleb Smirnoff u_int hash_nelements, u_int porthash_nelements) 5059bcd427bSRobert Watson { 5069bcd427bSRobert Watson 507fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_lock, pcbstor->ips_infolock_name, NULL, MTX_DEF); 508fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_hash_lock, pcbstor->ips_hashlock_name, 509fec8a8c7SGleb Smirnoff NULL, MTX_DEF); 5109bcd427bSRobert Watson #ifdef VIMAGE 5119bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 5129bcd427bSRobert Watson #endif 513db0ac6deSCy Schubert CK_LIST_INIT(&pcbinfo->ipi_listhead); 514fa046d87SRobert Watson pcbinfo->ipi_count = 0; 515fdb987beSMark Johnston pcbinfo->ipi_hash_exact = hashinit(hash_nelements, M_PCB, 516fdb987beSMark Johnston &pcbinfo->ipi_hashmask); 517fdb987beSMark Johnston pcbinfo->ipi_hash_wild = hashinit(hash_nelements, M_PCB, 5189bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 519db0ac6deSCy Schubert porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 5209bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 5219bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 5229d2877fcSMark Johnston pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 5231a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 524fec8a8c7SGleb Smirnoff pcbinfo->ipi_zone = pcbstor->ips_zone; 525fec8a8c7SGleb Smirnoff pcbinfo->ipi_portzone = pcbstor->ips_portzone; 526db0ac6deSCy Schubert pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone); 5279bcd427bSRobert Watson } 5289bcd427bSRobert Watson 5299bcd427bSRobert Watson /* 5309bcd427bSRobert Watson * Destroy an inpcbinfo. 5319bcd427bSRobert Watson */ 5329bcd427bSRobert Watson void 5339bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 5349bcd427bSRobert Watson { 5359bcd427bSRobert Watson 536fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 537fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 538fa046d87SRobert Watson 539fdb987beSMark Johnston hashdestroy(pcbinfo->ipi_hash_exact, M_PCB, pcbinfo->ipi_hashmask); 540fdb987beSMark Johnston hashdestroy(pcbinfo->ipi_hash_wild, M_PCB, pcbinfo->ipi_hashmask); 5419bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 5429bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 5431a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 5441a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 545db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_hash_lock); 546db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_lock); 5479bcd427bSRobert Watson } 5489bcd427bSRobert Watson 5499bcd427bSRobert Watson /* 550fec8a8c7SGleb Smirnoff * Initialize a pcbstorage - per protocol zones to allocate inpcbs. 551fec8a8c7SGleb Smirnoff */ 552fec8a8c7SGleb Smirnoff static void inpcb_dtor(void *, int, void *); 553fec8a8c7SGleb Smirnoff static void inpcb_fini(void *, int); 554fec8a8c7SGleb Smirnoff void 555fec8a8c7SGleb Smirnoff in_pcbstorage_init(void *arg) 556fec8a8c7SGleb Smirnoff { 557fec8a8c7SGleb Smirnoff struct inpcbstorage *pcbstor = arg; 558fec8a8c7SGleb Smirnoff 559fec8a8c7SGleb Smirnoff pcbstor->ips_zone = uma_zcreate(pcbstor->ips_zone_name, 5600aa120d5SGleb Smirnoff pcbstor->ips_size, NULL, inpcb_dtor, pcbstor->ips_pcbinit, 561c4a4b263SAndrew Gallatin inpcb_fini, UMA_ALIGN_CACHE, UMA_ZONE_SMR); 562fec8a8c7SGleb Smirnoff pcbstor->ips_portzone = uma_zcreate(pcbstor->ips_portzone_name, 563fec8a8c7SGleb Smirnoff sizeof(struct inpcbport), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 564fec8a8c7SGleb Smirnoff uma_zone_set_smr(pcbstor->ips_portzone, 565fec8a8c7SGleb Smirnoff uma_zone_get_smr(pcbstor->ips_zone)); 566fec8a8c7SGleb Smirnoff } 567fec8a8c7SGleb Smirnoff 568fec8a8c7SGleb Smirnoff /* 569fec8a8c7SGleb Smirnoff * Destroy a pcbstorage - used by unloadable protocols. 570fec8a8c7SGleb Smirnoff */ 571fec8a8c7SGleb Smirnoff void 572fec8a8c7SGleb Smirnoff in_pcbstorage_destroy(void *arg) 573fec8a8c7SGleb Smirnoff { 574fec8a8c7SGleb Smirnoff struct inpcbstorage *pcbstor = arg; 575fec8a8c7SGleb Smirnoff 576fec8a8c7SGleb Smirnoff uma_zdestroy(pcbstor->ips_zone); 577fec8a8c7SGleb Smirnoff uma_zdestroy(pcbstor->ips_portzone); 578fec8a8c7SGleb Smirnoff } 579fec8a8c7SGleb Smirnoff 580fec8a8c7SGleb Smirnoff /* 581c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 582d915b280SStephan Uphoff * On success return with the PCB locked. 583c3229e05SDavid Greenman */ 584df8bae1dSRodney W. Grimes int 585d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 586df8bae1dSRodney W. Grimes { 587136d4f1cSRobert Watson struct inpcb *inp; 588fe5324acSJohn Baldwin #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 58913cf67f3SHajimu UMEMOTO int error; 590fe5324acSJohn Baldwin #endif 591a557af22SRobert Watson 592db0ac6deSCy Schubert inp = uma_zalloc_smr(pcbinfo->ipi_zone, M_NOWAIT); 593df8bae1dSRodney W. Grimes if (inp == NULL) 594df8bae1dSRodney W. Grimes return (ENOBUFS); 5956a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 59650575ce1SAndrew Gallatin #ifdef NUMA 59750575ce1SAndrew Gallatin inp->inp_numa_domain = M_NODOM; 59850575ce1SAndrew Gallatin #endif 59915bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 600df8bae1dSRodney W. Grimes inp->inp_socket = so; 60186d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 6028b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 603a557af22SRobert Watson #ifdef MAC 60430d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 605a557af22SRobert Watson if (error != 0) 606a557af22SRobert Watson goto out; 60730d239bcSRobert Watson mac_inpcb_create(so, inp); 608a557af22SRobert Watson #endif 609fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 610fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 6110bffde27SRobert Watson if (error != 0) { 6120bffde27SRobert Watson #ifdef MAC 6130bffde27SRobert Watson mac_inpcb_destroy(inp); 6140bffde27SRobert Watson #endif 615a557af22SRobert Watson goto out; 6160bffde27SRobert Watson } 617b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 618e3fd5ffdSRobert Watson #ifdef INET6 619340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 620c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV6PROTO | INP_IPV6; 621603724d3SBjoern A. Zeeb if (V_ip6_v6only) 62233841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 623c7a62c92SGleb Smirnoff #ifdef INET 624c7a62c92SGleb Smirnoff else 625c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 626c7a62c92SGleb Smirnoff #endif 627603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 62833841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 629c7a62c92SGleb Smirnoff inp->in6p_hops = -1; /* use kernel default */ 630c7a62c92SGleb Smirnoff } 631c7a62c92SGleb Smirnoff #endif 632c7a62c92SGleb Smirnoff #if defined(INET) && defined(INET6) 633c7a62c92SGleb Smirnoff else 634c7a62c92SGleb Smirnoff #endif 635c7a62c92SGleb Smirnoff #ifdef INET 636c7a62c92SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 63733841545SHajimu UMEMOTO #endif 638fdb987beSMark Johnston inp->inp_smr = SMR_SEQ_INVALID; 639fdb987beSMark Johnston 6408c1960d5SMike Karels /* 6418c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 6428c1960d5SMike Karels * to be cleaned up. 6438c1960d5SMike Karels */ 6448c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 645db0ac6deSCy Schubert refcount_init(&inp->inp_refcount, 1); /* Reference from socket. */ 646db0ac6deSCy Schubert INP_WLOCK(inp); 647db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 648db0ac6deSCy Schubert pcbinfo->ipi_count++; 649db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 650db0ac6deSCy Schubert CK_LIST_INSERT_HEAD(&pcbinfo->ipi_listhead, inp, inp_list); 651db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 652db0ac6deSCy Schubert so->so_pcb = inp; 653db0ac6deSCy Schubert 654db0ac6deSCy Schubert return (0); 655db0ac6deSCy Schubert 6567a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 657a557af22SRobert Watson out: 658db0ac6deSCy Schubert uma_zfree_smr(pcbinfo->ipi_zone, inp); 659266f97b5SCy Schubert return (error); 660db0ac6deSCy Schubert #endif 661df8bae1dSRodney W. Grimes } 662df8bae1dSRodney W. Grimes 66367107f45SBjoern A. Zeeb #ifdef INET 664df8bae1dSRodney W. Grimes int 66596871af0SGleb Smirnoff in_pcbbind(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred) 666df8bae1dSRodney W. Grimes { 6674b932371SIan Dowse int anonport, error; 6684b932371SIan Dowse 66996871af0SGleb Smirnoff KASSERT(sin == NULL || sin->sin_family == AF_INET, 67096871af0SGleb Smirnoff ("%s: invalid address family for %p", __func__, sin)); 67196871af0SGleb Smirnoff KASSERT(sin == NULL || sin->sin_len == sizeof(struct sockaddr_in), 67296871af0SGleb Smirnoff ("%s: invalid address length for %p", __func__, sin)); 6738501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 674fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 67559daba27SSam Leffler 6764b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 6774b932371SIan Dowse return (EINVAL); 67896871af0SGleb Smirnoff anonport = sin == NULL || sin->sin_port == 0; 67996871af0SGleb Smirnoff error = in_pcbbind_setup(inp, sin, &inp->inp_laddr.s_addr, 680b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 6814b932371SIan Dowse if (error) 6824b932371SIan Dowse return (error); 6834b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 6844b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 6854b932371SIan Dowse inp->inp_lport = 0; 6864b932371SIan Dowse return (EAGAIN); 6874b932371SIan Dowse } 6884b932371SIan Dowse if (anonport) 6894b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 6904b932371SIan Dowse return (0); 6914b932371SIan Dowse } 69267107f45SBjoern A. Zeeb #endif 6934b932371SIan Dowse 694efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 69525102351SMike Karels /* 69625102351SMike Karels * Assign a local port like in_pcb_lport(), but also used with connect() 69725102351SMike Karels * and a foreign address and port. If fsa is non-NULL, choose a local port 69825102351SMike Karels * that is unused with those, otherwise one that is completely unused. 6992fdbcbeaSMike Karels * lsa can be NULL for IPv6. 70025102351SMike Karels */ 701efc76f72SBjoern A. Zeeb int 70225102351SMike Karels in_pcb_lport_dest(struct inpcb *inp, struct sockaddr *lsa, u_short *lportp, 70325102351SMike Karels struct sockaddr *fsa, u_short fport, struct ucred *cred, int lookupflags) 704efc76f72SBjoern A. Zeeb { 705efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 706efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 707efc76f72SBjoern A. Zeeb unsigned short *lastport; 70819acc506SGleb Smirnoff int count, error; 709efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 710efc76f72SBjoern A. Zeeb #ifdef INET 71125102351SMike Karels struct in_addr laddr, faddr; 71225102351SMike Karels #endif 71325102351SMike Karels #ifdef INET6 71425102351SMike Karels struct in6_addr *laddr6, *faddr6; 715efc76f72SBjoern A. Zeeb #endif 716efc76f72SBjoern A. Zeeb 717efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 718efc76f72SBjoern A. Zeeb 719efc76f72SBjoern A. Zeeb /* 720efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 721efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 722efc76f72SBjoern A. Zeeb */ 723efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 724fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 725efc76f72SBjoern A. Zeeb 726efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 727efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 728efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 729efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 730efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 731cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 732efc76f72SBjoern A. Zeeb if (error) 733efc76f72SBjoern A. Zeeb return (error); 734efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 735efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 736efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 737efc76f72SBjoern A. Zeeb } else { 738efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 739efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 740efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 741efc76f72SBjoern A. Zeeb } 74219acc506SGleb Smirnoff 743efc76f72SBjoern A. Zeeb /* 744efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 745efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 746efc76f72SBjoern A. Zeeb * code path implementing all logic. 747efc76f72SBjoern A. Zeeb */ 748efc76f72SBjoern A. Zeeb if (first > last) { 749efc76f72SBjoern A. Zeeb aux = first; 750efc76f72SBjoern A. Zeeb first = last; 751efc76f72SBjoern A. Zeeb last = aux; 752efc76f72SBjoern A. Zeeb } 753efc76f72SBjoern A. Zeeb 754efc76f72SBjoern A. Zeeb #ifdef INET 755637f317cSMike Karels laddr.s_addr = INADDR_ANY; /* used by INET6+INET below too */ 7564d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 7572fdbcbeaSMike Karels if (lsa != NULL) 75825102351SMike Karels laddr = ((struct sockaddr_in *)lsa)->sin_addr; 75925102351SMike Karels if (fsa != NULL) 76025102351SMike Karels faddr = ((struct sockaddr_in *)fsa)->sin_addr; 761efc76f72SBjoern A. Zeeb } 762efc76f72SBjoern A. Zeeb #endif 76325102351SMike Karels #ifdef INET6 7642fdbcbeaSMike Karels laddr6 = NULL; 7652fdbcbeaSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 7662fdbcbeaSMike Karels if (lsa != NULL) 76725102351SMike Karels laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 76825102351SMike Karels if (fsa != NULL) 76925102351SMike Karels faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 77025102351SMike Karels } 77125102351SMike Karels #endif 77225102351SMike Karels 77325102351SMike Karels tmpinp = NULL; 774efc76f72SBjoern A. Zeeb lport = *lportp; 775efc76f72SBjoern A. Zeeb 77619acc506SGleb Smirnoff if (V_ipport_randomized) 777efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 778efc76f72SBjoern A. Zeeb 779efc76f72SBjoern A. Zeeb count = last - first; 780efc76f72SBjoern A. Zeeb 781efc76f72SBjoern A. Zeeb do { 782efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 783efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 784efc76f72SBjoern A. Zeeb ++*lastport; 785efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 786efc76f72SBjoern A. Zeeb *lastport = first; 787efc76f72SBjoern A. Zeeb lport = htons(*lastport); 788efc76f72SBjoern A. Zeeb 78925102351SMike Karels if (fsa != NULL) { 79025102351SMike Karels #ifdef INET 79125102351SMike Karels if (lsa->sa_family == AF_INET) { 79225102351SMike Karels tmpinp = in_pcblookup_hash_locked(pcbinfo, 79325102351SMike Karels faddr, fport, laddr, lport, lookupflags, 7944130ea61SMark Johnston M_NODOM); 79525102351SMike Karels } 79625102351SMike Karels #endif 79725102351SMike Karels #ifdef INET6 79825102351SMike Karels if (lsa->sa_family == AF_INET6) { 79925102351SMike Karels tmpinp = in6_pcblookup_hash_locked(pcbinfo, 80025102351SMike Karels faddr6, fport, laddr6, lport, lookupflags, 8014130ea61SMark Johnston M_NODOM); 80225102351SMike Karels } 80325102351SMike Karels #endif 80425102351SMike Karels } else { 805efc76f72SBjoern A. Zeeb #ifdef INET6 806637f317cSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 807efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 80868e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 809637f317cSMike Karels #ifdef INET 810637f317cSMike Karels if (tmpinp == NULL && 811637f317cSMike Karels (inp->inp_vflag & INP_IPV4)) 812637f317cSMike Karels tmpinp = in_pcblookup_local(pcbinfo, 813637f317cSMike Karels laddr, lport, lookupflags, cred); 814637f317cSMike Karels #endif 815637f317cSMike Karels } 816efc76f72SBjoern A. Zeeb #endif 817efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 818efc76f72SBjoern A. Zeeb else 819efc76f72SBjoern A. Zeeb #endif 820efc76f72SBjoern A. Zeeb #ifdef INET 821efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 82268e0d7e0SRobert Watson lport, lookupflags, cred); 823efc76f72SBjoern A. Zeeb #endif 82425102351SMike Karels } 825efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 826efc76f72SBjoern A. Zeeb 827efc76f72SBjoern A. Zeeb *lportp = lport; 828efc76f72SBjoern A. Zeeb 829efc76f72SBjoern A. Zeeb return (0); 830efc76f72SBjoern A. Zeeb } 831efdf104bSMikolaj Golub 832efdf104bSMikolaj Golub /* 83325102351SMike Karels * Select a local port (number) to use. 83425102351SMike Karels */ 83525102351SMike Karels int 83625102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 83725102351SMike Karels struct ucred *cred, int lookupflags) 83825102351SMike Karels { 83925102351SMike Karels struct sockaddr_in laddr; 84025102351SMike Karels 84125102351SMike Karels if (laddrp) { 84225102351SMike Karels bzero(&laddr, sizeof(laddr)); 84325102351SMike Karels laddr.sin_family = AF_INET; 84425102351SMike Karels laddr.sin_addr = *laddrp; 84525102351SMike Karels } 84625102351SMike Karels return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 84725102351SMike Karels NULL, lportp, NULL, 0, cred, lookupflags)); 84825102351SMike Karels } 84925102351SMike Karels 85025102351SMike Karels /* 851efdf104bSMikolaj Golub * Return cached socket options. 852efdf104bSMikolaj Golub */ 8531a43cff9SSean Bruno int 854efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 855efdf104bSMikolaj Golub { 8561a43cff9SSean Bruno int so_options; 857efdf104bSMikolaj Golub 858efdf104bSMikolaj Golub so_options = 0; 859efdf104bSMikolaj Golub 8601a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 8611a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 862efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 863efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 864efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 865efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 866efdf104bSMikolaj Golub return (so_options); 867efdf104bSMikolaj Golub } 868efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 869efc76f72SBjoern A. Zeeb 870d5bb8bd3SAdrian Chadd #ifdef INET 8710a100a6fSAdrian Chadd /* 8724b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 8734b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 8744b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 8754b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 8764b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 8774b932371SIan Dowse * 8784b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 8794b932371SIan Dowse */ 8804b932371SIan Dowse int 88196871af0SGleb Smirnoff in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, 882136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 8834b932371SIan Dowse { 8844b932371SIan Dowse struct socket *so = inp->inp_socket; 885c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 8864b932371SIan Dowse struct in_addr laddr; 887df8bae1dSRodney W. Grimes u_short lport = 0; 88868e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 889413628a7SBjoern A. Zeeb int error; 890df8bae1dSRodney W. Grimes 8918501a69cSRobert Watson /* 8921a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 8931a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 8941a43cff9SSean Bruno */ 8951a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 8961a43cff9SSean Bruno 8971a43cff9SSean Bruno /* 898fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 8998501a69cSRobert Watson */ 90059daba27SSam Leffler INP_LOCK_ASSERT(inp); 901fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 90259daba27SSam Leffler 9034b932371SIan Dowse laddr.s_addr = *laddrp; 90496871af0SGleb Smirnoff if (sin != NULL && laddr.s_addr != INADDR_ANY) 905df8bae1dSRodney W. Grimes return (EINVAL); 9061a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 90768e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 90896871af0SGleb Smirnoff if (sin == NULL) { 9097c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 9107c2f3cb9SJamie Gritton return (error); 9117c2f3cb9SJamie Gritton } else { 912f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 913f161d294SMark Johnston ("%s: invalid family for address %p", __func__, sin)); 914f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 915f161d294SMark Johnston ("%s: invalid length for address %p", __func__, sin)); 916f161d294SMark Johnston 917b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 918b89e82ddSJamie Gritton if (error) 919b89e82ddSJamie Gritton return (error); 9204b932371SIan Dowse if (sin->sin_port != *lportp) { 9214b932371SIan Dowse /* Don't allow the port to change. */ 9224b932371SIan Dowse if (*lportp != 0) 9234b932371SIan Dowse return (EINVAL); 924df8bae1dSRodney W. Grimes lport = sin->sin_port; 9254b932371SIan Dowse } 9264b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 927df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 928df8bae1dSRodney W. Grimes /* 929df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 930df8bae1dSRodney W. Grimes * allow complete duplication of binding if 931df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 932df8bae1dSRodney W. Grimes * and a multicast address is bound on both 933df8bae1dSRodney W. Grimes * new and duplicated sockets. 934df8bae1dSRodney W. Grimes */ 935f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 936df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 9371a43cff9SSean Bruno /* 9381a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 9391a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 9401a43cff9SSean Bruno */ 9411a43cff9SSean Bruno if ((so->so_options & 9421a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 9431a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 944df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 945df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 94683103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 9474209e01aSAdrian Chadd /* 9484209e01aSAdrian Chadd * Is the address a local IP address? 949f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 9508696873dSAdrian Chadd * to any endpoint address, local or not. 9514209e01aSAdrian Chadd */ 952f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 9538896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 954df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 955df8bae1dSRodney W. Grimes } 9564b932371SIan Dowse laddr = sin->sin_addr; 957df8bae1dSRodney W. Grimes if (lport) { 958df8bae1dSRodney W. Grimes struct inpcb *t; 959ae0e7143SRobert Watson 960df8bae1dSRodney W. Grimes /* GROSS */ 961603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 962603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 963cc426dd3SMateusz Guzik priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 9642469dd60SGarrett Wollman return (EACCES); 965835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 966cc426dd3SMateusz Guzik priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 967078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 968413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 969340c35deSJonathan Lemon /* 970340c35deSJonathan Lemon * XXX 971340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 972340c35deSJonathan Lemon */ 9733aff4ccdSMark Johnston if (t != NULL && 9744658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 9754658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 9764cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 97752b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 9781a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 9791a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 98086d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 98186d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 9824049a042SGuido van Rooij return (EADDRINUSE); 9834049a042SGuido van Rooij } 984c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 98568e0d7e0SRobert Watson lport, lookupflags, cred); 9863aff4ccdSMark Johnston if (t != NULL && (reuseport & inp_so_options(t)) == 0 && 9871a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 988e3fd5ffdSRobert Watson #ifdef INET6 98933841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 990cfa1ca9dSYoshinobu Inoue INADDR_ANY || 991cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 992cfa1ca9dSYoshinobu Inoue INADDR_ANY || 993fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 994fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 995e3fd5ffdSRobert Watson #endif 996df8bae1dSRodney W. Grimes return (EADDRINUSE); 997df8bae1dSRodney W. Grimes } 998cfa1ca9dSYoshinobu Inoue } 999df8bae1dSRodney W. Grimes } 10004b932371SIan Dowse if (*lportp != 0) 10014b932371SIan Dowse lport = *lportp; 100233b3ac06SPeter Wemm if (lport == 0) { 10034616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 1004efc76f72SBjoern A. Zeeb if (error != 0) 1005efc76f72SBjoern A. Zeeb return (error); 100633b3ac06SPeter Wemm } 10074b932371SIan Dowse *laddrp = laddr.s_addr; 10084b932371SIan Dowse *lportp = lport; 1009df8bae1dSRodney W. Grimes return (0); 1010df8bae1dSRodney W. Grimes } 1011df8bae1dSRodney W. Grimes 1012999f1343SGarrett Wollman /* 10135200e00eSIan Dowse * Connect from a socket to a specified address. 10145200e00eSIan Dowse * Both address and port must be specified in argument sin. 10155200e00eSIan Dowse * If don't have a local address for this socket yet, 10165200e00eSIan Dowse * then pick one. 1017999f1343SGarrett Wollman */ 1018999f1343SGarrett Wollman int 1019a9d22cceSGleb Smirnoff in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred, 1020fdb987beSMark Johnston bool rehash __unused) 1021999f1343SGarrett Wollman { 10225200e00eSIan Dowse u_short lport, fport; 10235200e00eSIan Dowse in_addr_t laddr, faddr; 10245200e00eSIan Dowse int anonport, error; 1025df8bae1dSRodney W. Grimes 10268501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1027fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1028fdb987beSMark Johnston KASSERT(in_nullhost(inp->inp_faddr), 1029fdb987beSMark Johnston ("%s: inp is already connected", __func__)); 103027f74fd0SRobert Watson 10315200e00eSIan Dowse lport = inp->inp_lport; 10325200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 10335200e00eSIan Dowse anonport = (lport == 0); 1034a9d22cceSGleb Smirnoff error = in_pcbconnect_setup(inp, sin, &laddr, &lport, &faddr, &fport, 10359e46ff4dSGleb Smirnoff cred); 10365200e00eSIan Dowse if (error) 10375200e00eSIan Dowse return (error); 10385200e00eSIan Dowse 1039fdb987beSMark Johnston inp->inp_faddr.s_addr = faddr; 1040fdb987beSMark Johnston inp->inp_fport = fport; 1041fdb987beSMark Johnston 10425200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 10435200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 10445200e00eSIan Dowse inp->inp_lport = lport; 10455200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10465200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 1047fdb987beSMark Johnston inp->inp_laddr.s_addr = inp->inp_faddr.s_addr = 1048fdb987beSMark Johnston INADDR_ANY; 1049fdb987beSMark Johnston inp->inp_lport = inp->inp_fport = 0; 10505200e00eSIan Dowse return (EAGAIN); 10515200e00eSIan Dowse } 1052fdb987beSMark Johnston } else { 10535200e00eSIan Dowse inp->inp_lport = lport; 10545200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 1055fdb987beSMark Johnston if ((inp->inp_flags & INP_INHASHLIST) != 0) 1056db0ac6deSCy Schubert in_pcbrehash(inp); 1057fdb987beSMark Johnston else 1058db0ac6deSCy Schubert in_pcbinshash(inp); 1059fe1274eeSMichael Tuexen } 10602cb64cb2SGeorge V. Neville-Neil 10615200e00eSIan Dowse if (anonport) 10625200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 10635200e00eSIan Dowse return (0); 10645200e00eSIan Dowse } 10655200e00eSIan Dowse 10665200e00eSIan Dowse /* 10670895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 10680895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 10690895aec3SBjoern A. Zeeb */ 1070ae190832SSteven Hartland int 10710895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 10720895aec3SBjoern A. Zeeb struct ucred *cred) 10730895aec3SBjoern A. Zeeb { 10740895aec3SBjoern A. Zeeb struct ifaddr *ifa; 10750895aec3SBjoern A. Zeeb struct sockaddr *sa; 10769ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 10779ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 10780895aec3SBjoern A. Zeeb int error; 10790895aec3SBjoern A. Zeeb 1080c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1081413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1082ac1750ddSMark Johnston 1083592bcae8SBjoern A. Zeeb /* 1084592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1085592bcae8SBjoern A. Zeeb * if requested. 1086592bcae8SBjoern A. Zeeb */ 1087ac1750ddSMark Johnston if (!prison_saddrsel_ip4(cred, laddr)) 1088592bcae8SBjoern A. Zeeb return (0); 1089592bcae8SBjoern A. Zeeb 10900895aec3SBjoern A. Zeeb error = 0; 10910895aec3SBjoern A. Zeeb 10929ac7c6cfSAlexander V. Chernikov nh = NULL; 10939ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 10949ac7c6cfSAlexander V. Chernikov sin = &dst; 10950895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 10960895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 10970895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 10980895aec3SBjoern A. Zeeb 10990895aec3SBjoern A. Zeeb /* 11000895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 11010895aec3SBjoern A. Zeeb * else punt. 11020895aec3SBjoern A. Zeeb * 11030895aec3SBjoern A. Zeeb * Find out route to destination. 11040895aec3SBjoern A. Zeeb */ 11050895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 11069ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 11079ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 11080895aec3SBjoern A. Zeeb 11090895aec3SBjoern A. Zeeb /* 11100895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 11110895aec3SBjoern A. Zeeb * the outgoing interface. 11120895aec3SBjoern A. Zeeb * 11130895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 11140895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 11150895aec3SBjoern A. Zeeb * the source address from. 11160895aec3SBjoern A. Zeeb */ 11179ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 11188c0fec80SRobert Watson struct in_ifaddr *ia; 11190895aec3SBjoern A. Zeeb struct ifnet *ifp; 11200895aec3SBjoern A. Zeeb 11214f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 112258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11234f6c66ccSMatt Macy if (ia == NULL) { 11244f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 112558a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11264f6c66ccSMatt Macy } 11270895aec3SBjoern A. Zeeb if (ia == NULL) { 11280895aec3SBjoern A. Zeeb error = ENETUNREACH; 11290895aec3SBjoern A. Zeeb goto done; 11300895aec3SBjoern A. Zeeb } 11310895aec3SBjoern A. Zeeb 1132ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 11330895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11340895aec3SBjoern A. Zeeb goto done; 11350895aec3SBjoern A. Zeeb } 11360895aec3SBjoern A. Zeeb 11370895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 11380895aec3SBjoern A. Zeeb ia = NULL; 1139d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11400895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11410895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11420895aec3SBjoern A. Zeeb continue; 11430895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1144b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11450895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11460895aec3SBjoern A. Zeeb break; 11470895aec3SBjoern A. Zeeb } 11480895aec3SBjoern A. Zeeb } 11490895aec3SBjoern A. Zeeb if (ia != NULL) { 11500895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11510895aec3SBjoern A. Zeeb goto done; 11520895aec3SBjoern A. Zeeb } 11530895aec3SBjoern A. Zeeb 11540895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1155b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11560895aec3SBjoern A. Zeeb goto done; 11570895aec3SBjoern A. Zeeb } 11580895aec3SBjoern A. Zeeb 11590895aec3SBjoern A. Zeeb /* 11600895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 11610895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 11620895aec3SBjoern A. Zeeb * In case of jails do those three steps: 11630895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 11640895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 11650895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 11660895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 11670895aec3SBjoern A. Zeeb */ 11689ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 11698c0fec80SRobert Watson struct in_ifaddr *ia; 11709317b04eSRobert Watson struct ifnet *ifp; 11710895aec3SBjoern A. Zeeb 11720895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 1173ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 11749ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11750895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11760895aec3SBjoern A. Zeeb goto done; 11770895aec3SBjoern A. Zeeb } 11780895aec3SBjoern A. Zeeb 11790895aec3SBjoern A. Zeeb /* Jailed. */ 11800895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 11819ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1182b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11839ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11840895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11850895aec3SBjoern A. Zeeb goto done; 11860895aec3SBjoern A. Zeeb } 11870895aec3SBjoern A. Zeeb 11880895aec3SBjoern A. Zeeb /* 11890895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 11900895aec3SBjoern A. Zeeb * belonging to this jail. 11910895aec3SBjoern A. Zeeb */ 11928c0fec80SRobert Watson ia = NULL; 11939ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1194d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11950895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11960895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11970895aec3SBjoern A. Zeeb continue; 11980895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1199b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12000895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12010895aec3SBjoern A. Zeeb break; 12020895aec3SBjoern A. Zeeb } 12030895aec3SBjoern A. Zeeb } 12040895aec3SBjoern A. Zeeb if (ia != NULL) { 12050895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12060895aec3SBjoern A. Zeeb goto done; 12070895aec3SBjoern A. Zeeb } 12080895aec3SBjoern A. Zeeb 12090895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1210b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12110895aec3SBjoern A. Zeeb goto done; 12120895aec3SBjoern A. Zeeb } 12130895aec3SBjoern A. Zeeb 12140895aec3SBjoern A. Zeeb /* 12150895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 12160895aec3SBjoern A. Zeeb * to ourselves is here. 12170895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 12180895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 12190895aec3SBjoern A. Zeeb * a loopback interface. 12200895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 12210895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 12220895aec3SBjoern A. Zeeb */ 12239ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 12248c0fec80SRobert Watson struct in_ifaddr *ia; 12250895aec3SBjoern A. Zeeb 12269ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 122758a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12280895aec3SBjoern A. Zeeb if (ia == NULL) 12299ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 123058a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1231f0bb05fcSQing Li if (ia == NULL) 12329ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 12330895aec3SBjoern A. Zeeb 1234ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 12350895aec3SBjoern A. Zeeb if (ia == NULL) { 12360895aec3SBjoern A. Zeeb error = ENETUNREACH; 12370895aec3SBjoern A. Zeeb goto done; 12380895aec3SBjoern A. Zeeb } 12390895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12400895aec3SBjoern A. Zeeb goto done; 12410895aec3SBjoern A. Zeeb } 12420895aec3SBjoern A. Zeeb 12430895aec3SBjoern A. Zeeb /* Jailed. */ 12440895aec3SBjoern A. Zeeb if (ia != NULL) { 12450895aec3SBjoern A. Zeeb struct ifnet *ifp; 12460895aec3SBjoern A. Zeeb 12470895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12480895aec3SBjoern A. Zeeb ia = NULL; 1249d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12500895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12510895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12520895aec3SBjoern A. Zeeb continue; 12530895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1254b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1255b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 12560895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12570895aec3SBjoern A. Zeeb break; 12580895aec3SBjoern A. Zeeb } 12590895aec3SBjoern A. Zeeb } 12600895aec3SBjoern A. Zeeb if (ia != NULL) { 12610895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12620895aec3SBjoern A. Zeeb goto done; 12630895aec3SBjoern A. Zeeb } 12640895aec3SBjoern A. Zeeb } 12650895aec3SBjoern A. Zeeb 12660895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1267b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12680895aec3SBjoern A. Zeeb goto done; 12690895aec3SBjoern A. Zeeb } 12700895aec3SBjoern A. Zeeb 12710895aec3SBjoern A. Zeeb done: 1272713264f6SMark Johnston if (error == 0 && laddr->s_addr == INADDR_ANY) 1273713264f6SMark Johnston return (EHOSTUNREACH); 12740895aec3SBjoern A. Zeeb return (error); 12750895aec3SBjoern A. Zeeb } 12760895aec3SBjoern A. Zeeb 12770895aec3SBjoern A. Zeeb /* 12785200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 12795200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 12805200e00eSIan Dowse * address and port for the PCB; these are updated to the values 12815200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 12825200e00eSIan Dowse * the connect. 12835200e00eSIan Dowse * 12845200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 12855200e00eSIan Dowse * and port. These are not updated in the error case. 12865200e00eSIan Dowse */ 12875200e00eSIan Dowse int 1288a9d22cceSGleb Smirnoff in_pcbconnect_setup(struct inpcb *inp, struct sockaddr_in *sin, 1289136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 12909e46ff4dSGleb Smirnoff struct ucred *cred) 12915200e00eSIan Dowse { 12925200e00eSIan Dowse struct in_ifaddr *ia; 1293b89e82ddSJamie Gritton struct in_addr laddr, faddr; 12945200e00eSIan Dowse u_short lport, fport; 12955200e00eSIan Dowse int error; 12965200e00eSIan Dowse 1297f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 1298f161d294SMark Johnston ("%s: invalid address family for %p", __func__, sin)); 1299f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 1300f161d294SMark Johnston ("%s: invalid address length for %p", __func__, sin)); 1301f161d294SMark Johnston 13028501a69cSRobert Watson /* 13038501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 13048501a69cSRobert Watson * lock is sufficient. 13058501a69cSRobert Watson */ 1306c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 130727f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1308fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 130927f74fd0SRobert Watson 1310df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1311df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 13125200e00eSIan Dowse laddr.s_addr = *laddrp; 13135200e00eSIan Dowse lport = *lportp; 13145200e00eSIan Dowse faddr = sin->sin_addr; 13155200e00eSIan Dowse fport = sin->sin_port; 13160c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH 13170c325f53SAlexander V. Chernikov if (CALC_FLOWID_OUTBOUND) { 13180c325f53SAlexander V. Chernikov uint32_t hash_val, hash_type; 13190895aec3SBjoern A. Zeeb 13200c325f53SAlexander V. Chernikov hash_val = fib4_calc_software_hash(laddr, faddr, 0, fport, 13210c325f53SAlexander V. Chernikov inp->inp_socket->so_proto->pr_protocol, &hash_type); 13220c325f53SAlexander V. Chernikov 13230c325f53SAlexander V. Chernikov inp->inp_flowid = hash_val; 13240c325f53SAlexander V. Chernikov inp->inp_flowtype = hash_type; 13250c325f53SAlexander V. Chernikov } 13260c325f53SAlexander V. Chernikov #endif 1327d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1328df8bae1dSRodney W. Grimes /* 1329df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1330df8bae1dSRodney W. Grimes * use the primary local address. 1331df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1332df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1333df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1334df8bae1dSRodney W. Grimes */ 1335413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1336413628a7SBjoern A. Zeeb faddr = 1337d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1338ac1750ddSMark Johnston if ((error = prison_get_ip4(cred, &faddr)) != 0) 1339b89e82ddSJamie Gritton return (error); 13402d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1341d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 13422d9cfabaSRobert Watson IFF_BROADCAST) 1343d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1344603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 13452d9cfabaSRobert Watson } 1346df8bae1dSRodney W. Grimes } 13475200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1348d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1349df8bae1dSRodney W. Grimes /* 1350df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1351d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1352df8bae1dSRodney W. Grimes * address of that interface as our source address. 1353df8bae1dSRodney W. Grimes */ 13545200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1355df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1356df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1357df8bae1dSRodney W. Grimes struct ifnet *ifp; 1358df8bae1dSRodney W. Grimes 1359df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1360df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1361df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1362d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1363ac1750ddSMark Johnston if (ia->ia_ifp == ifp && 1364e691be70SDaniel Eischen prison_check_ip4(cred, 1365ac1750ddSMark Johnston &ia->ia_addr.sin_addr) == 0) 1366df8bae1dSRodney W. Grimes break; 1367e691be70SDaniel Eischen } 1368e691be70SDaniel Eischen if (ia == NULL) 1369d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1370e691be70SDaniel Eischen else { 13715200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1372d79fdd98SDaniel Eischen error = 0; 1373999f1343SGarrett Wollman } 1374d79fdd98SDaniel Eischen } 1375d79fdd98SDaniel Eischen } 137604215ed2SRandall Stewart if (error) 137704215ed2SRandall Stewart return (error); 13780895aec3SBjoern A. Zeeb } 1379a034518aSAndrew Gallatin 138025102351SMike Karels if (lport != 0) { 13819e46ff4dSGleb Smirnoff if (in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 13824130ea61SMark Johnston fport, laddr, lport, 0, M_NODOM) != NULL) 1383df8bae1dSRodney W. Grimes return (EADDRINUSE); 138425102351SMike Karels } else { 138525102351SMike Karels struct sockaddr_in lsin, fsin; 138625102351SMike Karels 138725102351SMike Karels bzero(&lsin, sizeof(lsin)); 138825102351SMike Karels bzero(&fsin, sizeof(fsin)); 138925102351SMike Karels lsin.sin_family = AF_INET; 139025102351SMike Karels lsin.sin_addr = laddr; 139125102351SMike Karels fsin.sin_family = AF_INET; 139225102351SMike Karels fsin.sin_addr = faddr; 139325102351SMike Karels error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin, 139425102351SMike Karels &lport, (struct sockaddr *)& fsin, fport, cred, 139525102351SMike Karels INPLOOKUP_WILDCARD); 13965a903f8dSPierre Beyssac if (error) 13975a903f8dSPierre Beyssac return (error); 13985a903f8dSPierre Beyssac } 13995200e00eSIan Dowse *laddrp = laddr.s_addr; 14005200e00eSIan Dowse *lportp = lport; 14015200e00eSIan Dowse *faddrp = faddr.s_addr; 14025200e00eSIan Dowse *fportp = fport; 1403df8bae1dSRodney W. Grimes return (0); 1404df8bae1dSRodney W. Grimes } 1405df8bae1dSRodney W. Grimes 140626f9a767SRodney W. Grimes void 1407136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1408df8bae1dSRodney W. Grimes { 14096b348152SRobert Watson 14108501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1411fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1412fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 1413fdb987beSMark Johnston ("%s: inp %p was already disconnected", __func__, inp)); 1414df8bae1dSRodney W. Grimes 1415fdb987beSMark Johnston in_pcbremhash_locked(inp); 1416fdb987beSMark Johnston 1417fdb987beSMark Johnston /* See the comment in in_pcbinshash(). */ 1418fdb987beSMark Johnston inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr); 14192589ec0fSMark Johnston inp->inp_laddr.s_addr = INADDR_ANY; 1420df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1421df8bae1dSRodney W. Grimes inp->inp_fport = 0; 1422df8bae1dSRodney W. Grimes } 142383e521ecSBjoern A. Zeeb #endif /* INET */ 1424df8bae1dSRodney W. Grimes 14254c7c478dSRobert Watson /* 142628696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1427c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 142828696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 142928696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 14304c7c478dSRobert Watson */ 143126f9a767SRodney W. Grimes void 1432136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1433df8bae1dSRodney W. Grimes { 14344c7c478dSRobert Watson 1435a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1436c0a211c5SRobert Watson 1437f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1438f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1439f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1440f3e7afe2SHans Petter Selasky #endif 14414c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 14424c7c478dSRobert Watson inp->inp_socket = NULL; 14434c7c478dSRobert Watson } 14444c7c478dSRobert Watson 1445c0a211c5SRobert Watson /* 1446db0ac6deSCy Schubert * inpcb hash lookups are protected by SMR section. 1447db0ac6deSCy Schubert * 1448db0ac6deSCy Schubert * Once desired pcb has been found, switching from SMR section to a pcb 1449db0ac6deSCy Schubert * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1450db0ac6deSCy Schubert * here because SMR is a critical section. 1451db0ac6deSCy Schubert * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1452db0ac6deSCy Schubert */ 1453db0ac6deSCy Schubert static inline void 1454db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1455db0ac6deSCy Schubert { 1456db0ac6deSCy Schubert 1457db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1458db0ac6deSCy Schubert rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1459db0ac6deSCy Schubert } 1460db0ac6deSCy Schubert 1461db0ac6deSCy Schubert static inline void 1462db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1463db0ac6deSCy Schubert { 1464db0ac6deSCy Schubert 1465db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1466db0ac6deSCy Schubert rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1467db0ac6deSCy Schubert } 1468db0ac6deSCy Schubert 1469db0ac6deSCy Schubert static inline int 1470db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1471db0ac6deSCy Schubert { 1472db0ac6deSCy Schubert 1473db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1474db0ac6deSCy Schubert rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1475db0ac6deSCy Schubert } 1476db0ac6deSCy Schubert 1477db0ac6deSCy Schubert static inline bool 1478db0ac6deSCy Schubert in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 1479db0ac6deSCy Schubert { 1480db0ac6deSCy Schubert 1481db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1482db0ac6deSCy Schubert in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 1483db0ac6deSCy Schubert } 1484db0ac6deSCy Schubert 1485f567d55fSGleb Smirnoff static inline bool 1486f567d55fSGleb Smirnoff _inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock, const int ignflags) 1487db0ac6deSCy Schubert { 1488db0ac6deSCy Schubert 1489db0ac6deSCy Schubert MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1490db0ac6deSCy Schubert SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1491db0ac6deSCy Schubert 1492db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1493f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1494db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1495db0ac6deSCy Schubert inp_unlock(inp, lock); 1496db0ac6deSCy Schubert return (false); 1497db0ac6deSCy Schubert } 1498db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1499db0ac6deSCy Schubert return (true); 1500db0ac6deSCy Schubert } 1501db0ac6deSCy Schubert 1502db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1503db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1504db0ac6deSCy Schubert inp_lock(inp, lock); 1505db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) 1506db0ac6deSCy Schubert return (false); 1507db0ac6deSCy Schubert /* 1508db0ac6deSCy Schubert * inp acquired through refcount & lock for sure didn't went 1509db0ac6deSCy Schubert * through uma_zfree(). However, it may have already went 1510db0ac6deSCy Schubert * through in_pcbfree() and has another reference, that 1511db0ac6deSCy Schubert * prevented its release by our in_pcbrele(). 1512db0ac6deSCy Schubert */ 1513f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1514db0ac6deSCy Schubert inp_unlock(inp, lock); 1515db0ac6deSCy Schubert return (false); 1516db0ac6deSCy Schubert } 1517db0ac6deSCy Schubert return (true); 1518db0ac6deSCy Schubert } else { 1519db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1520db0ac6deSCy Schubert return (false); 1521db0ac6deSCy Schubert } 1522db0ac6deSCy Schubert } 1523db0ac6deSCy Schubert 1524f567d55fSGleb Smirnoff bool 1525f567d55fSGleb Smirnoff inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1526f567d55fSGleb Smirnoff { 1527f567d55fSGleb Smirnoff 1528f567d55fSGleb Smirnoff /* 1529f567d55fSGleb Smirnoff * in_pcblookup() family of functions ignore not only freed entries, 1530f567d55fSGleb Smirnoff * that may be found due to lockless access to the hash, but dropped 1531f567d55fSGleb Smirnoff * entries, too. 1532f567d55fSGleb Smirnoff */ 1533f567d55fSGleb Smirnoff return (_inp_smr_lock(inp, lock, INP_FREED | INP_DROPPED)); 1534f567d55fSGleb Smirnoff } 1535f567d55fSGleb Smirnoff 1536db0ac6deSCy Schubert /* 1537db0ac6deSCy Schubert * inp_next() - inpcb hash/list traversal iterator 1538db0ac6deSCy Schubert * 1539db0ac6deSCy Schubert * Requires initialized struct inpcb_iterator for context. 1540db0ac6deSCy Schubert * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1541db0ac6deSCy Schubert * 1542db0ac6deSCy Schubert * - Iterator can have either write-lock or read-lock semantics, that can not 1543db0ac6deSCy Schubert * be changed later. 1544db0ac6deSCy Schubert * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1545db0ac6deSCy Schubert * a single hash slot. Note: only rip_input() does the latter. 1546db0ac6deSCy Schubert * - Iterator may have optional bool matching function. The matching function 1547db0ac6deSCy Schubert * will be executed for each inpcb in the SMR context, so it can not acquire 1548db0ac6deSCy Schubert * locks and can safely access only immutable fields of inpcb. 1549db0ac6deSCy Schubert * 1550db0ac6deSCy Schubert * A fresh initialized iterator has NULL inpcb in its context and that 1551db0ac6deSCy Schubert * means that inp_next() call would return the very first inpcb on the list 1552db0ac6deSCy Schubert * locked with desired semantic. In all following calls the context pointer 1553db0ac6deSCy Schubert * shall hold the current inpcb pointer. The KPI user is not supposed to 1554db0ac6deSCy Schubert * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1555db0ac6deSCy Schubert * and write NULL to its context. After end of traversal an iterator can be 1556db0ac6deSCy Schubert * reused. 1557db0ac6deSCy Schubert * 1558db0ac6deSCy Schubert * List traversals have the following features/constraints: 1559db0ac6deSCy Schubert * - New entries won't be seen, as they are always added to the head of a list. 1560db0ac6deSCy Schubert * - Removed entries won't stop traversal as long as they are not added to 1561db0ac6deSCy Schubert * a different list. This is violated by in_pcbrehash(). 1562db0ac6deSCy Schubert */ 1563db0ac6deSCy Schubert #define II_LIST_FIRST(ipi, hash) \ 1564db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1565db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1566fdb987beSMark Johnston CK_LIST_FIRST(&(ipi)->ipi_hash_exact[(hash)])) 1567db0ac6deSCy Schubert #define II_LIST_NEXT(inp, hash) \ 1568db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1569db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_list) : \ 1570fdb987beSMark Johnston CK_LIST_NEXT((inp), inp_hash_exact)) 1571db0ac6deSCy Schubert #define II_LOCK_ASSERT(inp, lock) \ 1572db0ac6deSCy Schubert rw_assert(&(inp)->inp_lock, \ 1573db0ac6deSCy Schubert (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1574db0ac6deSCy Schubert struct inpcb * 1575db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii) 1576db0ac6deSCy Schubert { 1577db0ac6deSCy Schubert const struct inpcbinfo *ipi = ii->ipi; 1578db0ac6deSCy Schubert inp_match_t *match = ii->match; 1579db0ac6deSCy Schubert void *ctx = ii->ctx; 1580db0ac6deSCy Schubert inp_lookup_t lock = ii->lock; 1581db0ac6deSCy Schubert int hash = ii->hash; 1582db0ac6deSCy Schubert struct inpcb *inp; 1583db0ac6deSCy Schubert 1584db0ac6deSCy Schubert if (ii->inp == NULL) { /* First call. */ 1585db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1586db0ac6deSCy Schubert /* This is unrolled CK_LIST_FOREACH(). */ 1587db0ac6deSCy Schubert for (inp = II_LIST_FIRST(ipi, hash); 1588db0ac6deSCy Schubert inp != NULL; 1589db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash)) { 1590db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1591db0ac6deSCy Schubert continue; 1592f567d55fSGleb Smirnoff if (__predict_true(_inp_smr_lock(inp, lock, INP_FREED))) 1593db0ac6deSCy Schubert break; 1594db0ac6deSCy Schubert else { 1595db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1596db0ac6deSCy Schubert MPASS(inp != II_LIST_FIRST(ipi, hash)); 1597db0ac6deSCy Schubert inp = II_LIST_FIRST(ipi, hash); 1598430df2abSMichael Tuexen if (inp == NULL) 1599430df2abSMichael Tuexen break; 1600db0ac6deSCy Schubert } 1601db0ac6deSCy Schubert } 1602db0ac6deSCy Schubert 1603db0ac6deSCy Schubert if (inp == NULL) 1604db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1605db0ac6deSCy Schubert else 1606db0ac6deSCy Schubert ii->inp = inp; 1607db0ac6deSCy Schubert 1608db0ac6deSCy Schubert return (inp); 1609db0ac6deSCy Schubert } 1610db0ac6deSCy Schubert 1611db0ac6deSCy Schubert /* Not a first call. */ 1612db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1613db0ac6deSCy Schubert restart: 1614db0ac6deSCy Schubert inp = ii->inp; 1615db0ac6deSCy Schubert II_LOCK_ASSERT(inp, lock); 1616db0ac6deSCy Schubert next: 1617db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash); 1618db0ac6deSCy Schubert if (inp == NULL) { 1619db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1620db0ac6deSCy Schubert goto found; 1621db0ac6deSCy Schubert } 1622db0ac6deSCy Schubert 1623db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1624db0ac6deSCy Schubert goto next; 1625db0ac6deSCy Schubert 1626db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1627db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1628db0ac6deSCy Schubert /* 1629db0ac6deSCy Schubert * Entries are never inserted in middle of a list, thus 1630db0ac6deSCy Schubert * as long as we are in SMR, we can continue traversal. 1631db0ac6deSCy Schubert * Jump to 'restart' should yield in the same result, 1632db0ac6deSCy Schubert * but could produce unnecessary looping. Could this 1633db0ac6deSCy Schubert * looping be unbound? 1634db0ac6deSCy Schubert */ 1635db0ac6deSCy Schubert inp_unlock(inp, lock); 1636db0ac6deSCy Schubert goto next; 1637db0ac6deSCy Schubert } else { 1638db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1639db0ac6deSCy Schubert goto found; 1640db0ac6deSCy Schubert } 1641db0ac6deSCy Schubert } 1642db0ac6deSCy Schubert 1643db0ac6deSCy Schubert /* 1644db0ac6deSCy Schubert * Can't obtain lock immediately, thus going hard. Once we exit the 1645db0ac6deSCy Schubert * SMR section we can no longer jump to 'next', and our only stable 1646db0ac6deSCy Schubert * anchoring point is ii->inp, which we keep locked for this case, so 1647db0ac6deSCy Schubert * we jump to 'restart'. 1648db0ac6deSCy Schubert */ 1649db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1650db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1651db0ac6deSCy Schubert inp_lock(inp, lock); 1652db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) { 1653db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1654db0ac6deSCy Schubert goto restart; 1655db0ac6deSCy Schubert } 1656db0ac6deSCy Schubert /* 1657db0ac6deSCy Schubert * See comment in inp_smr_lock(). 1658db0ac6deSCy Schubert */ 1659db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1660db0ac6deSCy Schubert inp_unlock(inp, lock); 1661db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1662db0ac6deSCy Schubert goto restart; 1663db0ac6deSCy Schubert } 1664db0ac6deSCy Schubert } else 1665db0ac6deSCy Schubert goto next; 1666db0ac6deSCy Schubert 1667db0ac6deSCy Schubert found: 1668db0ac6deSCy Schubert inp_unlock(ii->inp, lock); 1669db0ac6deSCy Schubert ii->inp = inp; 1670db0ac6deSCy Schubert 1671db0ac6deSCy Schubert return (ii->inp); 1672db0ac6deSCy Schubert } 1673db0ac6deSCy Schubert 1674db0ac6deSCy Schubert /* 167579bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 1676db0ac6deSCy Schubert * stability of an inpcb pointer despite the inpcb lock being released or 1677db0ac6deSCy Schubert * SMR section exited. 167879bdc6e5SRobert Watson * 1679db0ac6deSCy Schubert * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1680c0a211c5SRobert Watson */ 168179bdc6e5SRobert Watson void 168279bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 16834c7c478dSRobert Watson { 1684db0ac6deSCy Schubert u_int old __diagused; 1685df8bae1dSRodney W. Grimes 1686db0ac6deSCy Schubert old = refcount_acquire(&inp->inp_refcount); 1687db0ac6deSCy Schubert KASSERT(old > 0, ("%s: refcount 0", __func__)); 168879bdc6e5SRobert Watson } 168979bdc6e5SRobert Watson 169079bdc6e5SRobert Watson /* 1691db0ac6deSCy Schubert * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1692db0ac6deSCy Schubert * freeing the pcb, if the reference was very last. 169379bdc6e5SRobert Watson */ 1694db0ac6deSCy Schubert bool 169579bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 169679bdc6e5SRobert Watson { 169779bdc6e5SRobert Watson 169879bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 169979bdc6e5SRobert Watson 1700c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1701db0ac6deSCy Schubert return (false); 1702db0ac6deSCy Schubert 1703db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1704db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1705db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1706df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1707db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1708db0ac6deSCy Schubert return (true); 1709df4e91d3SGleb Smirnoff } 171079bdc6e5SRobert Watson 1711db0ac6deSCy Schubert bool 171279bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 171379bdc6e5SRobert Watson { 171479bdc6e5SRobert Watson 171579bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 171679bdc6e5SRobert Watson 1717c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1718db0ac6deSCy Schubert return (false); 1719db0ac6deSCy Schubert 1720db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1721db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1722db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1723edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1724db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1725db0ac6deSCy Schubert return (true); 1726addf2b20SMatt Macy } 1727addf2b20SMatt Macy 172879bdc6e5SRobert Watson /* 172979bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 173079bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 173179bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 173279bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 1733db0ac6deSCy Schubert * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1734db0ac6deSCy Schubert * Almost all work, including removal from global lists, is done in this 1735db0ac6deSCy Schubert * context, where the pcbinfo lock is held. 173679bdc6e5SRobert Watson */ 173779bdc6e5SRobert Watson void 173879bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 173979bdc6e5SRobert Watson { 1740f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1741db0ac6deSCy Schubert #ifdef INET 1742db0ac6deSCy Schubert struct ip_moptions *imo; 1743db0ac6deSCy Schubert #endif 1744db0ac6deSCy Schubert #ifdef INET6 1745db0ac6deSCy Schubert struct ip6_moptions *im6o; 1746db0ac6deSCy Schubert #endif 174745a48d07SJonathan T. Looney 174879bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1749db0ac6deSCy Schubert KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1750db0ac6deSCy Schubert KASSERT((inp->inp_flags & INP_FREED) == 0, 1751db0ac6deSCy Schubert ("%s: called twice for pcb %p", __func__, inp)); 1752db0ac6deSCy Schubert 1753db0ac6deSCy Schubert inp->inp_flags |= INP_FREED; 1754db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 1755db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1756db0ac6deSCy Schubert pcbinfo->ipi_count--; 1757db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_list); 1758db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 1759db0ac6deSCy Schubert 17603ba34b07SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 17613ba34b07SGleb Smirnoff in_pcbremhash(inp); 1762db0ac6deSCy Schubert 1763fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1764db0ac6deSCy Schubert #ifdef MAC 1765db0ac6deSCy Schubert mac_inpcb_destroy(inp); 1766db0ac6deSCy Schubert #endif 1767db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1768db0ac6deSCy Schubert if (inp->inp_sp != NULL) 1769db0ac6deSCy Schubert ipsec_delete_pcbpolicy(inp); 1770db0ac6deSCy Schubert #endif 1771db0ac6deSCy Schubert #ifdef INET 1772db0ac6deSCy Schubert if (inp->inp_options) 1773db0ac6deSCy Schubert (void)m_free(inp->inp_options); 1774db0ac6deSCy Schubert imo = inp->inp_moptions; 1775db0ac6deSCy Schubert #endif 1776db0ac6deSCy Schubert #ifdef INET6 1777db0ac6deSCy Schubert if (inp->inp_vflag & INP_IPV6PROTO) { 1778db0ac6deSCy Schubert ip6_freepcbopts(inp->in6p_outputopts); 1779db0ac6deSCy Schubert im6o = inp->in6p_moptions; 1780db0ac6deSCy Schubert } else 1781db0ac6deSCy Schubert im6o = NULL; 1782db0ac6deSCy Schubert #endif 1783db0ac6deSCy Schubert 1784db0ac6deSCy Schubert if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1785cb6bb230SMatt Macy INP_WUNLOCK(inp); 1786db0ac6deSCy Schubert } 1787db0ac6deSCy Schubert #ifdef INET6 1788db0ac6deSCy Schubert ip6_freemoptions(im6o); 1789db0ac6deSCy Schubert #endif 1790db0ac6deSCy Schubert #ifdef INET 1791db0ac6deSCy Schubert inp_freemoptions(imo); 1792db0ac6deSCy Schubert #endif 1793eb93b99dSGleb Smirnoff /* Destruction is finalized in inpcb_dtor(). */ 1794eb93b99dSGleb Smirnoff } 1795eb93b99dSGleb Smirnoff 1796eb93b99dSGleb Smirnoff static void 1797eb93b99dSGleb Smirnoff inpcb_dtor(void *mem, int size, void *arg) 1798eb93b99dSGleb Smirnoff { 1799eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1800eb93b99dSGleb Smirnoff 1801eb93b99dSGleb Smirnoff crfree(inp->inp_cred); 1802eb93b99dSGleb Smirnoff #ifdef INVARIANTS 1803eb93b99dSGleb Smirnoff inp->inp_cred = NULL; 1804eb93b99dSGleb Smirnoff #endif 1805eb93b99dSGleb Smirnoff } 1806eb93b99dSGleb Smirnoff 1807eb93b99dSGleb Smirnoff /* 1808eb93b99dSGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 1809eb93b99dSGleb Smirnoff * different name to the lock. But they all are disposed the same. 1810eb93b99dSGleb Smirnoff */ 1811eb93b99dSGleb Smirnoff static void 1812eb93b99dSGleb Smirnoff inpcb_fini(void *mem, int size) 1813eb93b99dSGleb Smirnoff { 1814eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1815eb93b99dSGleb Smirnoff 1816eb93b99dSGleb Smirnoff INP_LOCK_DESTROY(inp); 181728696211SRobert Watson } 181828696211SRobert Watson 181928696211SRobert Watson /* 1820c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1821c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1822c0a211c5SRobert Watson * 1823c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1824c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1825c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1826c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1827c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1828c0a211c5SRobert Watson * in_pcbdetach(). 1829c0a211c5SRobert Watson * 1830c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1831c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 183210702a28SRobert Watson */ 183310702a28SRobert Watson void 183410702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 183510702a28SRobert Watson { 183610702a28SRobert Watson 18378501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 18386573d758SMatt Macy #ifdef INVARIANTS 18396573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 18406573d758SMatt Macy MPASS(inp->inp_refcount > 1); 18416573d758SMatt Macy #endif 184210702a28SRobert Watson 1843ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 18443ba34b07SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 18453ba34b07SGleb Smirnoff in_pcbremhash(inp); 184610702a28SRobert Watson } 184710702a28SRobert Watson 184867107f45SBjoern A. Zeeb #ifdef INET 184954d642bbSRobert Watson /* 185054d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 185154d642bbSRobert Watson */ 185226ef6ac4SDon Lewis struct sockaddr * 1853136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 185426ef6ac4SDon Lewis { 185526ef6ac4SDon Lewis struct sockaddr_in *sin; 185626ef6ac4SDon Lewis 18571ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1858a163d034SWarner Losh M_WAITOK | M_ZERO); 185926ef6ac4SDon Lewis sin->sin_family = AF_INET; 186026ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 186126ef6ac4SDon Lewis sin->sin_addr = *addr_p; 186226ef6ac4SDon Lewis sin->sin_port = port; 186326ef6ac4SDon Lewis 186426ef6ac4SDon Lewis return (struct sockaddr *)sin; 186526ef6ac4SDon Lewis } 186626ef6ac4SDon Lewis 1867117bcae7SGarrett Wollman int 186854d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1869df8bae1dSRodney W. Grimes { 1870136d4f1cSRobert Watson struct inpcb *inp; 187126ef6ac4SDon Lewis struct in_addr addr; 187226ef6ac4SDon Lewis in_port_t port; 187342fa505bSDavid Greenman 1874fdc984f7STor Egge inp = sotoinpcb(so); 187554d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 18766466b28aSRobert Watson 1877a69042a5SRobert Watson INP_RLOCK(inp); 187826ef6ac4SDon Lewis port = inp->inp_lport; 187926ef6ac4SDon Lewis addr = inp->inp_laddr; 1880a69042a5SRobert Watson INP_RUNLOCK(inp); 188142fa505bSDavid Greenman 188226ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1883117bcae7SGarrett Wollman return 0; 1884df8bae1dSRodney W. Grimes } 1885df8bae1dSRodney W. Grimes 1886117bcae7SGarrett Wollman int 188754d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1888df8bae1dSRodney W. Grimes { 1889136d4f1cSRobert Watson struct inpcb *inp; 189026ef6ac4SDon Lewis struct in_addr addr; 189126ef6ac4SDon Lewis in_port_t port; 189242fa505bSDavid Greenman 1893fdc984f7STor Egge inp = sotoinpcb(so); 189454d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 18956466b28aSRobert Watson 1896a69042a5SRobert Watson INP_RLOCK(inp); 189726ef6ac4SDon Lewis port = inp->inp_fport; 189826ef6ac4SDon Lewis addr = inp->inp_faddr; 1899a69042a5SRobert Watson INP_RUNLOCK(inp); 190042fa505bSDavid Greenman 190126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1902117bcae7SGarrett Wollman return 0; 1903df8bae1dSRodney W. Grimes } 1904df8bae1dSRodney W. Grimes 190526f9a767SRodney W. Grimes void 1906136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1907136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1908d1c54148SJesper Skriver { 1909f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1910d1c54148SJesper Skriver 19113dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1912db0ac6deSCy Schubert CK_LIST_FOREACH_SAFE(inp, &pcbinfo->ipi_listhead, inp_list, inp_temp) { 19138501a69cSRobert Watson INP_WLOCK(inp); 1914d1c54148SJesper Skriver #ifdef INET6 1915f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 19168501a69cSRobert Watson INP_WUNLOCK(inp); 1917d1c54148SJesper Skriver continue; 1918f76fcf6dSJeffrey Hsu } 1919d1c54148SJesper Skriver #endif 1920d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1921f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 19228501a69cSRobert Watson INP_WUNLOCK(inp); 1923d1c54148SJesper Skriver continue; 1924d1c54148SJesper Skriver } 19253dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 19268501a69cSRobert Watson INP_WUNLOCK(inp); 1927f76fcf6dSJeffrey Hsu } 19283dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1929d1c54148SJesper Skriver } 1930d1c54148SJesper Skriver 1931db0ac6deSCy Schubert static bool 1932db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 1933db0ac6deSCy Schubert { 1934db0ac6deSCy Schubert 1935db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 1936db0ac6deSCy Schubert return (true); 1937db0ac6deSCy Schubert else 1938db0ac6deSCy Schubert return (false); 1939db0ac6deSCy Schubert } 1940db0ac6deSCy Schubert 1941e43cc4aeSHajimu UMEMOTO void 1942136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1943e43cc4aeSHajimu UMEMOTO { 1944db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 1945db0ac6deSCy Schubert inp_v4_multi_match, NULL); 1946e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 194759854ecfSHans Petter Selasky struct in_multi *inm; 194859854ecfSHans Petter Selasky struct in_mfilter *imf; 1949e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1950e43cc4aeSHajimu UMEMOTO 1951db0ac6deSCy Schubert IN_MULTI_LOCK_ASSERT(); 1952db0ac6deSCy Schubert 1953db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 1954db0ac6deSCy Schubert INP_WLOCK_ASSERT(inp); 1955db0ac6deSCy Schubert 1956e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1957e43cc4aeSHajimu UMEMOTO /* 1958e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1959e43cc4aeSHajimu UMEMOTO * detached. 1960e43cc4aeSHajimu UMEMOTO */ 1961e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1962e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1963e43cc4aeSHajimu UMEMOTO 1964e43cc4aeSHajimu UMEMOTO /* 1965e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1966e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1967cb6bb230SMatt Macy * 1968cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1969e43cc4aeSHajimu UMEMOTO */ 197059854ecfSHans Petter Selasky restart: 197159854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 197259854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 197359854ecfSHans Petter Selasky continue; 197459854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 197559854ecfSHans Petter Selasky continue; 197659854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 197759854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 197859854ecfSHans Petter Selasky ip_mfilter_free(imf); 197959854ecfSHans Petter Selasky goto restart; 1980e43cc4aeSHajimu UMEMOTO } 1981e43cc4aeSHajimu UMEMOTO } 1982e43cc4aeSHajimu UMEMOTO } 1983e43cc4aeSHajimu UMEMOTO 1984df8bae1dSRodney W. Grimes /* 1985fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1986fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1987c3229e05SDavid Greenman */ 1988d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1989df8bae1dSRodney W. Grimes struct inpcb * 1990136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 199168e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1992df8bae1dSRodney W. Grimes { 1993136d4f1cSRobert Watson struct inpcb *inp; 1994d5e8a67eSHajimu UMEMOTO #ifdef INET6 1995d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1996d5e8a67eSHajimu UMEMOTO #else 1997d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1998d5e8a67eSHajimu UMEMOTO #endif 1999d5e8a67eSHajimu UMEMOTO int wildcard; 20007bc4aca7SDavid Greenman 200168e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 200268e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2003fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 20041b73ca0bSSam Leffler 200568e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 2006c3229e05SDavid Greenman struct inpcbhead *head; 2007c3229e05SDavid Greenman /* 2008c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 2009c3229e05SDavid Greenman * matches the local address and port we're looking for. 2010c3229e05SDavid Greenman */ 2011fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 2012a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 2013fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 2014cfa1ca9dSYoshinobu Inoue #ifdef INET6 2015413628a7SBjoern A. Zeeb /* XXX inp locking */ 2016369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2017cfa1ca9dSYoshinobu Inoue continue; 2018cfa1ca9dSYoshinobu Inoue #endif 2019c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 2020c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2021c3229e05SDavid Greenman inp->inp_lport == lport) { 2022c3229e05SDavid Greenman /* 2023413628a7SBjoern A. Zeeb * Found? 2024c3229e05SDavid Greenman */ 2025ac1750ddSMark Johnston if (prison_equal_ip4(cred->cr_prison, 20260304c731SJamie Gritton inp->inp_cred->cr_prison)) 2027c3229e05SDavid Greenman return (inp); 2028df8bae1dSRodney W. Grimes } 2029c3229e05SDavid Greenman } 2030c3229e05SDavid Greenman /* 2031c3229e05SDavid Greenman * Not found. 2032c3229e05SDavid Greenman */ 2033c3229e05SDavid Greenman return (NULL); 2034c3229e05SDavid Greenman } else { 2035c3229e05SDavid Greenman struct inpcbporthead *porthash; 2036c3229e05SDavid Greenman struct inpcbport *phd; 2037c3229e05SDavid Greenman struct inpcb *match = NULL; 2038c3229e05SDavid Greenman /* 2039c3229e05SDavid Greenman * Best fit PCB lookup. 2040c3229e05SDavid Greenman * 2041c3229e05SDavid Greenman * First see if this local port is in use by looking on the 2042c3229e05SDavid Greenman * port hash list. 2043c3229e05SDavid Greenman */ 2044712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 2045712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 2046b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 2047c3229e05SDavid Greenman if (phd->phd_port == lport) 2048c3229e05SDavid Greenman break; 2049c3229e05SDavid Greenman } 2050c3229e05SDavid Greenman if (phd != NULL) { 2051c3229e05SDavid Greenman /* 2052c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 2053c3229e05SDavid Greenman * fit. 2054c3229e05SDavid Greenman */ 2055b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 2056c3229e05SDavid Greenman wildcard = 0; 2057ac1750ddSMark Johnston if (!prison_equal_ip4(inp->inp_cred->cr_prison, 20580304c731SJamie Gritton cred->cr_prison)) 2059413628a7SBjoern A. Zeeb continue; 2060cfa1ca9dSYoshinobu Inoue #ifdef INET6 2061413628a7SBjoern A. Zeeb /* XXX inp locking */ 2062369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2063cfa1ca9dSYoshinobu Inoue continue; 2064d5e8a67eSHajimu UMEMOTO /* 2065d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 2066d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 2067d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 2068d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 2069d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 2070d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 2071d5e8a67eSHajimu UMEMOTO * 2072d5e8a67eSHajimu UMEMOTO * Note that the case only happens 2073d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 2074d5e8a67eSHajimu UMEMOTO * the condition that the use of the 2075d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 2076d5e8a67eSHajimu UMEMOTO */ 2077d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 2078d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2079cfa1ca9dSYoshinobu Inoue #endif 2080c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 2081c3229e05SDavid Greenman wildcard++; 208215bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 208315bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 208415bd2b43SDavid Greenman wildcard++; 208515bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 208615bd2b43SDavid Greenman continue; 208715bd2b43SDavid Greenman } else { 208815bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 208915bd2b43SDavid Greenman wildcard++; 209015bd2b43SDavid Greenman } 2091df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 2092df8bae1dSRodney W. Grimes match = inp; 2093df8bae1dSRodney W. Grimes matchwild = wildcard; 2094413628a7SBjoern A. Zeeb if (matchwild == 0) 2095df8bae1dSRodney W. Grimes break; 2096df8bae1dSRodney W. Grimes } 2097df8bae1dSRodney W. Grimes } 20983dbdc25cSDavid Greenman } 2099df8bae1dSRodney W. Grimes return (match); 2100df8bae1dSRodney W. Grimes } 2101c3229e05SDavid Greenman } 2102d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 210315bd2b43SDavid Greenman 2104d93ec8cbSMark Johnston static bool 2105d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain) 2106d93ec8cbSMark Johnston { 2107d93ec8cbSMark Johnston return (domain == M_NODOM || domain == grp->il_numa_domain); 2108d93ec8cbSMark Johnston } 2109d93ec8cbSMark Johnston 21101a43cff9SSean Bruno static struct inpcb * 21111a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 21124130ea61SMark Johnston const struct in_addr *faddr, uint16_t fport, const struct in_addr *laddr, 21134130ea61SMark Johnston uint16_t lport, int domain) 21141a43cff9SSean Bruno { 21151a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 21161a43cff9SSean Bruno struct inpcblbgroup *grp; 2117d93ec8cbSMark Johnston struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 21181a43cff9SSean Bruno 21191a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 21201a43cff9SSean Bruno 21219d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 21229d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 21231a43cff9SSean Bruno 21241a43cff9SSean Bruno /* 2125d93ec8cbSMark Johnston * Search for an LB group match based on the following criteria: 2126d93ec8cbSMark Johnston * - prefer jailed groups to non-jailed groups 2127d93ec8cbSMark Johnston * - prefer exact source address matches to wildcard matches 2128d93ec8cbSMark Johnston * - prefer groups bound to the specified NUMA domain 21291a43cff9SSean Bruno */ 2130d93ec8cbSMark Johnston jail_exact = jail_wild = local_exact = local_wild = NULL; 213154af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 2132d93ec8cbSMark Johnston bool injail; 2133d93ec8cbSMark Johnston 21341a43cff9SSean Bruno #ifdef INET6 21351a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 21361a43cff9SSean Bruno continue; 21371a43cff9SSean Bruno #endif 21388be02ee4SMark Johnston if (grp->il_lport != lport) 21398be02ee4SMark Johnston continue; 21401a43cff9SSean Bruno 2141d93ec8cbSMark Johnston injail = prison_flag(grp->il_cred, PR_IP4) != 0; 2142d93ec8cbSMark Johnston if (injail && prison_check_ip4_locked(grp->il_cred->cr_prison, 2143d93ec8cbSMark Johnston laddr) != 0) 2144d93ec8cbSMark Johnston continue; 2145a034518aSAndrew Gallatin 2146d93ec8cbSMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) { 2147d93ec8cbSMark Johnston if (injail) { 2148d93ec8cbSMark Johnston jail_exact = grp; 2149d93ec8cbSMark Johnston if (in_pcblookup_lb_numa_match(grp, domain)) 2150d93ec8cbSMark Johnston /* This is a perfect match. */ 2151d93ec8cbSMark Johnston goto out; 2152d93ec8cbSMark Johnston } else if (local_exact == NULL || 2153d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) { 2154d93ec8cbSMark Johnston local_exact = grp; 2155d93ec8cbSMark Johnston } 21564130ea61SMark Johnston } else if (grp->il_laddr.s_addr == INADDR_ANY) { 2157d93ec8cbSMark Johnston if (injail) { 2158d93ec8cbSMark Johnston if (jail_wild == NULL || 2159d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) 2160d93ec8cbSMark Johnston jail_wild = grp; 2161d93ec8cbSMark Johnston } else if (local_wild == NULL || 2162d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) { 2163d93ec8cbSMark Johnston local_wild = grp; 2164d93ec8cbSMark Johnston } 2165d93ec8cbSMark Johnston } 2166d93ec8cbSMark Johnston } 2167d93ec8cbSMark Johnston 2168d93ec8cbSMark Johnston if (jail_exact != NULL) 2169d93ec8cbSMark Johnston grp = jail_exact; 2170d93ec8cbSMark Johnston else if (jail_wild != NULL) 2171d93ec8cbSMark Johnston grp = jail_wild; 2172d93ec8cbSMark Johnston else if (local_exact != NULL) 2173d93ec8cbSMark Johnston grp = local_exact; 2174d93ec8cbSMark Johnston else 2175d93ec8cbSMark Johnston grp = local_wild; 2176d93ec8cbSMark Johnston if (grp == NULL) 2177d93ec8cbSMark Johnston return (NULL); 2178d93ec8cbSMark Johnston out: 2179d93ec8cbSMark Johnston return (grp->il_inp[INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) % 2180d93ec8cbSMark Johnston grp->il_inpcnt]); 21811a43cff9SSean Bruno } 21821a43cff9SSean Bruno 2183*3e98dcb3SMark Johnston static bool 2184*3e98dcb3SMark Johnston in_pcblookup_exact_match(const struct inpcb *inp, struct in_addr faddr, 21854130ea61SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 218615bd2b43SDavid Greenman { 2187cfa1ca9dSYoshinobu Inoue #ifdef INET6 2188413628a7SBjoern A. Zeeb /* XXX inp locking */ 2189369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2190*3e98dcb3SMark Johnston return (false); 2191cfa1ca9dSYoshinobu Inoue #endif 21926d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2193ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2194ca98b82cSDavid Greenman inp->inp_fport == fport && 2195220d8921SGleb Smirnoff inp->inp_lport == lport) 2196*3e98dcb3SMark Johnston return (true); 2197*3e98dcb3SMark Johnston return (false); 2198*3e98dcb3SMark Johnston } 2199*3e98dcb3SMark Johnston 2200*3e98dcb3SMark Johnston static struct inpcb * 2201*3e98dcb3SMark Johnston in_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2202*3e98dcb3SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 2203*3e98dcb3SMark Johnston { 2204*3e98dcb3SMark Johnston struct inpcbhead *head; 2205*3e98dcb3SMark Johnston struct inpcb *inp; 2206*3e98dcb3SMark Johnston 2207*3e98dcb3SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 2208*3e98dcb3SMark Johnston 2209*3e98dcb3SMark Johnston head = &pcbinfo->ipi_hash_exact[INP_PCBHASH(&faddr, lport, fport, 2210*3e98dcb3SMark Johnston pcbinfo->ipi_hashmask)]; 2211*3e98dcb3SMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_exact) { 2212*3e98dcb3SMark Johnston if (in_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) 2213c3229e05SDavid Greenman return (inp); 2214c3229e05SDavid Greenman } 2215*3e98dcb3SMark Johnston return (NULL); 2216*3e98dcb3SMark Johnston } 2217*3e98dcb3SMark Johnston 2218*3e98dcb3SMark Johnston typedef enum { 2219*3e98dcb3SMark Johnston INPLOOKUP_MATCH_NONE = 0, 2220*3e98dcb3SMark Johnston INPLOOKUP_MATCH_WILD = 1, 2221*3e98dcb3SMark Johnston INPLOOKUP_MATCH_LADDR = 2, 2222*3e98dcb3SMark Johnston } inp_lookup_match_t; 2223*3e98dcb3SMark Johnston 2224*3e98dcb3SMark Johnston static inp_lookup_match_t 2225*3e98dcb3SMark Johnston in_pcblookup_wild_match(const struct inpcb *inp, struct in_addr laddr, 2226*3e98dcb3SMark Johnston u_short lport) 2227*3e98dcb3SMark Johnston { 2228*3e98dcb3SMark Johnston #ifdef INET6 2229*3e98dcb3SMark Johnston /* XXX inp locking */ 2230*3e98dcb3SMark Johnston if ((inp->inp_vflag & INP_IPV4) == 0) 2231*3e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 2232*3e98dcb3SMark Johnston #endif 2233*3e98dcb3SMark Johnston if (inp->inp_faddr.s_addr != INADDR_ANY || inp->inp_lport != lport) 2234*3e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 2235*3e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == INADDR_ANY) 2236*3e98dcb3SMark Johnston return (INPLOOKUP_MATCH_WILD); 2237*3e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == laddr.s_addr) 2238*3e98dcb3SMark Johnston return (INPLOOKUP_MATCH_LADDR); 2239*3e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 22404130ea61SMark Johnston } 2241e3fd5ffdSRobert Watson 22424130ea61SMark Johnston static struct inpcb * 22434130ea61SMark Johnston in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 22444130ea61SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 22454130ea61SMark Johnston { 22464130ea61SMark Johnston struct inpcbhead *head; 22474130ea61SMark Johnston struct inpcb *inp, *local_wild, *local_exact, *jail_wild; 2248e3fd5ffdSRobert Watson #ifdef INET6 22494130ea61SMark Johnston struct inpcb *local_wild_mapped; 2250e3fd5ffdSRobert Watson #endif 2251413628a7SBjoern A. Zeeb 22524130ea61SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 2253d93ec8cbSMark Johnston 2254d93ec8cbSMark Johnston /* 2255413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2256413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2257413628a7SBjoern A. Zeeb * 2. jailed, wild. 2258413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2259413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2260413628a7SBjoern A. Zeeb */ 2261fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 2262a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 22634130ea61SMark Johnston local_wild = local_exact = jail_wild = NULL; 22644130ea61SMark Johnston #ifdef INET6 22654130ea61SMark Johnston local_wild_mapped = NULL; 22664130ea61SMark Johnston #endif 2267fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 2268*3e98dcb3SMark Johnston inp_lookup_match_t match; 22694130ea61SMark Johnston bool injail; 22704130ea61SMark Johnston 2271*3e98dcb3SMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport); 2272*3e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_NONE) 2273413628a7SBjoern A. Zeeb continue; 2274413628a7SBjoern A. Zeeb 22754130ea61SMark Johnston injail = prison_flag(inp->inp_cred, PR_IP4) != 0; 2276413628a7SBjoern A. Zeeb if (injail) { 22774130ea61SMark Johnston if (prison_check_ip4_locked(inp->inp_cred->cr_prison, 22784130ea61SMark Johnston &laddr) != 0) 2279413628a7SBjoern A. Zeeb continue; 2280413628a7SBjoern A. Zeeb } else { 2281413628a7SBjoern A. Zeeb if (local_exact != NULL) 2282413628a7SBjoern A. Zeeb continue; 2283413628a7SBjoern A. Zeeb } 2284413628a7SBjoern A. Zeeb 2285*3e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_LADDR) { 2286413628a7SBjoern A. Zeeb if (injail) 2287c3229e05SDavid Greenman return (inp); 2288413628a7SBjoern A. Zeeb local_exact = inp; 2289*3e98dcb3SMark Johnston } else { 2290e3fd5ffdSRobert Watson #ifdef INET6 2291413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 22925cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2293cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2294cfa1ca9dSYoshinobu Inoue else 229583e521ecSBjoern A. Zeeb #endif 2296413628a7SBjoern A. Zeeb if (injail) 2297413628a7SBjoern A. Zeeb jail_wild = inp; 2298413628a7SBjoern A. Zeeb else 22996d6a026bSDavid Greenman local_wild = inp; 23006d6a026bSDavid Greenman } 23014130ea61SMark Johnston } 2302413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2303413628a7SBjoern A. Zeeb return (jail_wild); 2304413628a7SBjoern A. Zeeb if (local_exact != NULL) 2305413628a7SBjoern A. Zeeb return (local_exact); 2306413628a7SBjoern A. Zeeb if (local_wild != NULL) 2307c3229e05SDavid Greenman return (local_wild); 2308413628a7SBjoern A. Zeeb #ifdef INET6 2309413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2310413628a7SBjoern A. Zeeb return (local_wild_mapped); 231183e521ecSBjoern A. Zeeb #endif 23126d6a026bSDavid Greenman return (NULL); 231315bd2b43SDavid Greenman } 2314fa046d87SRobert Watson 2315fa046d87SRobert Watson /* 23164130ea61SMark Johnston * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 23174130ea61SMark Johnston * that the caller has either locked the hash list, which usually happens 23184130ea61SMark Johnston * for bind(2) operations, or is in SMR section, which happens when sorting 23194130ea61SMark Johnston * out incoming packets. 2320fa046d87SRobert Watson */ 2321fa046d87SRobert Watson static struct inpcb * 23224130ea61SMark Johnston in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 23234130ea61SMark Johnston u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 23244130ea61SMark Johnston uint8_t numa_domain) 23254130ea61SMark Johnston { 23264130ea61SMark Johnston struct inpcb *inp; 23274130ea61SMark Johnston const u_short fport = fport_arg, lport = lport_arg; 23284130ea61SMark Johnston 23294130ea61SMark Johnston KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0, 23304130ea61SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 23314130ea61SMark Johnston KASSERT(faddr.s_addr != INADDR_ANY, 23324130ea61SMark Johnston ("%s: invalid foreign address", __func__)); 23334130ea61SMark Johnston KASSERT(laddr.s_addr != INADDR_ANY, 23344130ea61SMark Johnston ("%s: invalid local address", __func__)); 23354130ea61SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 23364130ea61SMark Johnston 23374130ea61SMark Johnston inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 23384130ea61SMark Johnston if (inp != NULL) 23394130ea61SMark Johnston return (inp); 23404130ea61SMark Johnston 23414130ea61SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 23424130ea61SMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, &laddr, 23434130ea61SMark Johnston lport, numa_domain); 23444130ea61SMark Johnston if (inp == NULL) { 23454130ea61SMark Johnston inp = in_pcblookup_hash_wild_locked(pcbinfo, faddr, 23464130ea61SMark Johnston fport, laddr, lport); 23474130ea61SMark Johnston } 23484130ea61SMark Johnston } 23494130ea61SMark Johnston 23504130ea61SMark Johnston return (inp); 23514130ea61SMark Johnston } 23524130ea61SMark Johnston 23534130ea61SMark Johnston static struct inpcb * 23544130ea61SMark Johnston in_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2355fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 23564130ea61SMark Johnston uint8_t numa_domain) 2357fa046d87SRobert Watson { 2358fa046d87SRobert Watson struct inpcb *inp; 2359fa046d87SRobert Watson 2360675e2618SMark Johnston KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2361675e2618SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 2362675e2618SMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2363675e2618SMark Johnston ("%s: LOCKPCB not set", __func__)); 2364675e2618SMark Johnston 2365db0ac6deSCy Schubert smr_enter(pcbinfo->ipi_smr); 2366fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 23674130ea61SMark Johnston lookupflags & INPLOOKUP_WILDCARD, numa_domain); 2368fa046d87SRobert Watson if (inp != NULL) { 2369db0ac6deSCy Schubert if (__predict_false(inp_smr_lock(inp, 2370db0ac6deSCy Schubert (lookupflags & INPLOOKUP_LOCKMASK)) == false)) 2371266f97b5SCy Schubert inp = NULL; 2372db0ac6deSCy Schubert } else 2373db0ac6deSCy Schubert smr_exit(pcbinfo->ipi_smr); 2374d797164aSGleb Smirnoff 2375fa046d87SRobert Watson return (inp); 2376fa046d87SRobert Watson } 2377fa046d87SRobert Watson 2378fa046d87SRobert Watson /* 2379d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2380d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 2381fa046d87SRobert Watson */ 2382fa046d87SRobert Watson struct inpcb * 2383fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 23844130ea61SMark Johnston struct in_addr laddr, u_int lport, int lookupflags, 23854130ea61SMark Johnston struct ifnet *ifp __unused) 2386fa046d87SRobert Watson { 23874130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 23884130ea61SMark Johnston lookupflags, M_NODOM)); 2389fa046d87SRobert Watson } 2390d3c1f003SRobert Watson 2391d3c1f003SRobert Watson struct inpcb * 2392d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2393d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 23944130ea61SMark Johnston struct ifnet *ifp __unused, struct mbuf *m) 2395d3c1f003SRobert Watson { 23964130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 23974130ea61SMark Johnston lookupflags, m->m_pkthdr.numa_domain)); 2398d3c1f003SRobert Watson } 239967107f45SBjoern A. Zeeb #endif /* INET */ 240015bd2b43SDavid Greenman 24017bc4aca7SDavid Greenman /* 2402c3229e05SDavid Greenman * Insert PCB onto various hash lists. 24037bc4aca7SDavid Greenman */ 2404db0ac6deSCy Schubert int 2405db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp) 240615bd2b43SDavid Greenman { 2407c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2408c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2409c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2410c3229e05SDavid Greenman struct inpcbport *phd; 2411fdb987beSMark Johnston uint32_t hash; 2412fdb987beSMark Johnston bool connected; 241315bd2b43SDavid Greenman 24148501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2415fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2416111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2417111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2418602cc7f1SRobert Watson 2419cfa1ca9dSYoshinobu Inoue #ifdef INET6 2420fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2421fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2422fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2423fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2424fdb987beSMark Johnston } else 242583e521ecSBjoern A. Zeeb #endif 2426fdb987beSMark Johnston { 2427fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2428fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2429fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2430fdb987beSMark Johnston } 2431fdb987beSMark Johnston 2432fdb987beSMark Johnston if (connected) 2433fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_exact[hash]; 2434fdb987beSMark Johnston else 2435fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_wild[hash]; 243615bd2b43SDavid Greenman 2437712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2438712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2439c3229e05SDavid Greenman 2440c3229e05SDavid Greenman /* 24411a43cff9SSean Bruno * Add entry to load balance group. 24421a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 24431a43cff9SSean Bruno */ 2444a152dd86SMark Johnston if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) { 2445a152dd86SMark Johnston int error = in_pcbinslbgrouphash(inp, M_NODOM); 2446a152dd86SMark Johnston if (error != 0) 2447a152dd86SMark Johnston return (error); 24481a43cff9SSean Bruno } 24491a43cff9SSean Bruno 24501a43cff9SSean Bruno /* 2451c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2452c3229e05SDavid Greenman */ 2453b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2454c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2455c3229e05SDavid Greenman break; 2456c3229e05SDavid Greenman } 2457a152dd86SMark Johnston 2458c3229e05SDavid Greenman /* 2459c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2460c3229e05SDavid Greenman */ 2461c3229e05SDavid Greenman if (phd == NULL) { 2462db0ac6deSCy Schubert phd = uma_zalloc_smr(pcbinfo->ipi_portzone, M_NOWAIT); 2463c3229e05SDavid Greenman if (phd == NULL) { 2464a152dd86SMark Johnston if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 2465a152dd86SMark Johnston in_pcbremlbgrouphash(inp); 2466a152dd86SMark Johnston return (ENOMEM); 2467c3229e05SDavid Greenman } 2468c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2469b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2470b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2471c3229e05SDavid Greenman } 2472c3229e05SDavid Greenman inp->inp_phd = phd; 2473b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2474fdb987beSMark Johnston 2475fdb987beSMark Johnston /* 2476fdb987beSMark Johnston * The PCB may have been disconnected in the past. Before we can safely 2477fdb987beSMark Johnston * make it visible in the hash table, we must wait for all readers which 2478fdb987beSMark Johnston * may be traversing this PCB to finish. 2479fdb987beSMark Johnston */ 2480fdb987beSMark Johnston if (inp->inp_smr != SMR_SEQ_INVALID) { 2481fdb987beSMark Johnston smr_wait(pcbinfo->ipi_smr, inp->inp_smr); 2482fdb987beSMark Johnston inp->inp_smr = SMR_SEQ_INVALID; 2483fdb987beSMark Johnston } 2484fdb987beSMark Johnston 2485fdb987beSMark Johnston if (connected) 2486fdb987beSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_exact); 2487fdb987beSMark Johnston else 2488fdb987beSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 2489111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 2490db0ac6deSCy Schubert 2491c3229e05SDavid Greenman return (0); 249215bd2b43SDavid Greenman } 249315bd2b43SDavid Greenman 2494fdb987beSMark Johnston void 2495fdb987beSMark Johnston in_pcbremhash_locked(struct inpcb *inp) 24963ba34b07SGleb Smirnoff { 24973ba34b07SGleb Smirnoff struct inpcbport *phd = inp->inp_phd; 24983ba34b07SGleb Smirnoff 24993ba34b07SGleb Smirnoff INP_WLOCK_ASSERT(inp); 2500fdb987beSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 25013ba34b07SGleb Smirnoff MPASS(inp->inp_flags & INP_INHASHLIST); 25023ba34b07SGleb Smirnoff 2503d93ec8cbSMark Johnston if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 25043ba34b07SGleb Smirnoff in_pcbremlbgrouphash(inp); 2505fdb987beSMark Johnston #ifdef INET6 2506fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2507fdb987beSMark Johnston if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 2508fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2509fdb987beSMark Johnston else 2510fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2511fdb987beSMark Johnston } else 2512fdb987beSMark Johnston #endif 2513fdb987beSMark Johnston { 2514fdb987beSMark Johnston if (in_nullhost(inp->inp_faddr)) 2515fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2516fdb987beSMark Johnston else 2517fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2518fdb987beSMark Johnston } 25193ba34b07SGleb Smirnoff CK_LIST_REMOVE(inp, inp_portlist); 25203ba34b07SGleb Smirnoff if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 25213ba34b07SGleb Smirnoff CK_LIST_REMOVE(phd, phd_hash); 25223ba34b07SGleb Smirnoff uma_zfree_smr(inp->inp_pcbinfo->ipi_portzone, phd); 25233ba34b07SGleb Smirnoff } 25243ba34b07SGleb Smirnoff inp->inp_flags &= ~INP_INHASHLIST; 25253ba34b07SGleb Smirnoff } 25263ba34b07SGleb Smirnoff 2527fdb987beSMark Johnston static void 2528fdb987beSMark Johnston in_pcbremhash(struct inpcb *inp) 2529fdb987beSMark Johnston { 2530fdb987beSMark Johnston INP_HASH_WLOCK(inp->inp_pcbinfo); 2531fdb987beSMark Johnston in_pcbremhash_locked(inp); 2532fdb987beSMark Johnston INP_HASH_WUNLOCK(inp->inp_pcbinfo); 2533fdb987beSMark Johnston } 2534fdb987beSMark Johnston 253552cd27cbSRobert Watson /* 2536c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2537c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2538c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2539c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2540c3229e05SDavid Greenman */ 254115bd2b43SDavid Greenman void 2542db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp) 254315bd2b43SDavid Greenman { 254459daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 254515bd2b43SDavid Greenman struct inpcbhead *head; 2546fdb987beSMark Johnston uint32_t hash; 2547fdb987beSMark Johnston bool connected; 254815bd2b43SDavid Greenman 25498501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2550fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2551111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2552fdb987beSMark Johnston ("%s: !INP_INHASHLIST", __func__)); 2553fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 2554fdb987beSMark Johnston ("%s: inp was disconnected", __func__)); 2555602cc7f1SRobert Watson 2556cfa1ca9dSYoshinobu Inoue #ifdef INET6 2557fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2558fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2559fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2560fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2561fdb987beSMark Johnston } else 256283e521ecSBjoern A. Zeeb #endif 2563fdb987beSMark Johnston { 2564fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2565fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2566fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2567fdb987beSMark Johnston } 256815bd2b43SDavid Greenman 2569fdb987beSMark Johnston /* 2570fdb987beSMark Johnston * When rehashing, the caller must ensure that either the new or the old 2571fdb987beSMark Johnston * foreign address was unspecified. 2572fdb987beSMark Johnston */ 2573fdb987beSMark Johnston if (connected) 2574fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2575fdb987beSMark Johnston else 2576fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2577fdb987beSMark Johnston 2578fdb987beSMark Johnston if (connected) { 2579fdb987beSMark Johnston head = &pcbinfo->ipi_hash_exact[hash]; 2580fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_exact); 2581fdb987beSMark Johnston } else { 2582fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[hash]; 2583fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_wild); 2584fdb987beSMark Johnston } 2585c3229e05SDavid Greenman } 2586c3229e05SDavid Greenman 2587c3229e05SDavid Greenman /* 258884cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 258984cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 259084cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 259184cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 259284cc0778SGeorge V. Neville-Neil */ 259384cc0778SGeorge V. Neville-Neil void 259484cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 259584cc0778SGeorge V. Neville-Neil { 259684cc0778SGeorge V. Neville-Neil 2597fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 259884cc0778SGeorge V. Neville-Neil return; 259984cc0778SGeorge V. Neville-Neil } 260084cc0778SGeorge V. Neville-Neil 260184cc0778SGeorge V. Neville-Neil /* 2602a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2603a557af22SRobert Watson * label change into the in_pcb for the socket. 2604a557af22SRobert Watson */ 2605a557af22SRobert Watson void 2606136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2607a557af22SRobert Watson { 2608a557af22SRobert Watson #ifdef MAC 2609a557af22SRobert Watson struct inpcb *inp; 2610a557af22SRobert Watson 26114c7c478dSRobert Watson inp = sotoinpcb(so); 26124c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2613602cc7f1SRobert Watson 26148501a69cSRobert Watson INP_WLOCK(inp); 2615310e7cebSRobert Watson SOCK_LOCK(so); 2616a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2617310e7cebSRobert Watson SOCK_UNLOCK(so); 26188501a69cSRobert Watson INP_WUNLOCK(inp); 2619a557af22SRobert Watson #endif 2620a557af22SRobert Watson } 26215f311da2SMike Silbersack 26223d585327SKip Macy void 26233d585327SKip Macy inp_wlock(struct inpcb *inp) 26243d585327SKip Macy { 26253d585327SKip Macy 26268501a69cSRobert Watson INP_WLOCK(inp); 26273d585327SKip Macy } 26283d585327SKip Macy 26293d585327SKip Macy void 26303d585327SKip Macy inp_wunlock(struct inpcb *inp) 26313d585327SKip Macy { 26323d585327SKip Macy 26338501a69cSRobert Watson INP_WUNLOCK(inp); 26343d585327SKip Macy } 26353d585327SKip Macy 26363d585327SKip Macy void 26373d585327SKip Macy inp_rlock(struct inpcb *inp) 26383d585327SKip Macy { 26393d585327SKip Macy 2640a69042a5SRobert Watson INP_RLOCK(inp); 26413d585327SKip Macy } 26423d585327SKip Macy 26433d585327SKip Macy void 26443d585327SKip Macy inp_runlock(struct inpcb *inp) 26453d585327SKip Macy { 26463d585327SKip Macy 2647a69042a5SRobert Watson INP_RUNLOCK(inp); 26483d585327SKip Macy } 26493d585327SKip Macy 2650c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 26513d585327SKip Macy void 2652e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 26533d585327SKip Macy { 26543d585327SKip Macy 26558501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 26563d585327SKip Macy } 26573d585327SKip Macy 26583d585327SKip Macy void 2659e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 26603d585327SKip Macy { 26613d585327SKip Macy 26623d585327SKip Macy INP_UNLOCK_ASSERT(inp); 26633d585327SKip Macy } 26643d585327SKip Macy #endif 26653d585327SKip Macy 26669378e437SKip Macy void 266724cf7a8dSGleb Smirnoff inp_apply_all(struct inpcbinfo *pcbinfo, 266824cf7a8dSGleb Smirnoff void (*func)(struct inpcb *, void *), void *arg) 26699378e437SKip Macy { 267024cf7a8dSGleb Smirnoff struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2671db0ac6deSCy Schubert INPLOOKUP_WLOCKPCB); 26729378e437SKip Macy struct inpcb *inp; 26739378e437SKip Macy 2674db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) 26759378e437SKip Macy func(inp, arg); 26769378e437SKip Macy } 26779378e437SKip Macy 26789378e437SKip Macy struct socket * 26799378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 26809378e437SKip Macy { 26819378e437SKip Macy 26829378e437SKip Macy INP_WLOCK_ASSERT(inp); 26839378e437SKip Macy return (inp->inp_socket); 26849378e437SKip Macy } 26859378e437SKip Macy 26869378e437SKip Macy struct tcpcb * 26879378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 26889378e437SKip Macy { 26899378e437SKip Macy 26909378e437SKip Macy INP_WLOCK_ASSERT(inp); 26919378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 26929378e437SKip Macy } 26939378e437SKip Macy 26949378e437SKip Macy int 26959378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 26969378e437SKip Macy { 26979378e437SKip Macy 26989378e437SKip Macy return (inp->inp_ip_tos); 26999378e437SKip Macy } 27009378e437SKip Macy 27019378e437SKip Macy void 27029378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 27039378e437SKip Macy { 27049378e437SKip Macy 27059378e437SKip Macy inp->inp_ip_tos = val; 27069378e437SKip Macy } 27079378e437SKip Macy 27089378e437SKip Macy void 2709df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 27109d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 27119378e437SKip Macy { 27129378e437SKip Macy 27139d29c635SKip Macy INP_LOCK_ASSERT(inp); 2714df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2715df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 27169378e437SKip Macy *lp = inp->inp_lport; 27179378e437SKip Macy *fp = inp->inp_fport; 27189378e437SKip Macy } 27199378e437SKip Macy 2720dd0e6c38SKip Macy struct inpcb * 2721dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2722dd0e6c38SKip Macy { 2723dd0e6c38SKip Macy 2724dd0e6c38SKip Macy return (sotoinpcb(so)); 2725dd0e6c38SKip Macy } 2726dd0e6c38SKip Macy 2727cc65eb4eSGleb Smirnoff /* 2728cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2729cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2730cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2731cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2732cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2733cc65eb4eSGleb Smirnoff * hidden from users. 2734cc65eb4eSGleb Smirnoff */ 2735cc65eb4eSGleb Smirnoff void 2736cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2737cc65eb4eSGleb Smirnoff { 2738cc65eb4eSGleb Smirnoff 273979db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2740cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2741cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2742cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2743cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2744cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 27453a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2746cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2747cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2748cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2749cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2750cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2751cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2752cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2753cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2754cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2755cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2756cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2757cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2758cc65eb4eSGleb Smirnoff } 2759cc65eb4eSGleb Smirnoff 2760a35bdd44SMichael Tuexen int 2761a35bdd44SMichael Tuexen sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, 2762a35bdd44SMichael Tuexen int (*ctloutput_set)(struct inpcb *, struct sockopt *)) 2763a35bdd44SMichael Tuexen { 2764a35bdd44SMichael Tuexen struct sockopt sopt; 2765a35bdd44SMichael Tuexen struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2766a35bdd44SMichael Tuexen INPLOOKUP_WLOCKPCB); 2767a35bdd44SMichael Tuexen struct inpcb *inp; 2768a35bdd44SMichael Tuexen struct sockopt_parameters *params; 2769a35bdd44SMichael Tuexen struct socket *so; 2770a35bdd44SMichael Tuexen int error; 2771a35bdd44SMichael Tuexen char buf[1024]; 2772a35bdd44SMichael Tuexen 2773a35bdd44SMichael Tuexen if (req->oldptr != NULL || req->oldlen != 0) 2774a35bdd44SMichael Tuexen return (EINVAL); 2775a35bdd44SMichael Tuexen if (req->newptr == NULL) 2776a35bdd44SMichael Tuexen return (EPERM); 2777a35bdd44SMichael Tuexen if (req->newlen > sizeof(buf)) 2778a35bdd44SMichael Tuexen return (ENOMEM); 2779a35bdd44SMichael Tuexen error = SYSCTL_IN(req, buf, req->newlen); 2780a35bdd44SMichael Tuexen if (error != 0) 2781a35bdd44SMichael Tuexen return (error); 2782a35bdd44SMichael Tuexen if (req->newlen < sizeof(struct sockopt_parameters)) 2783a35bdd44SMichael Tuexen return (EINVAL); 2784a35bdd44SMichael Tuexen params = (struct sockopt_parameters *)buf; 2785a35bdd44SMichael Tuexen sopt.sopt_level = params->sop_level; 2786a35bdd44SMichael Tuexen sopt.sopt_name = params->sop_optname; 2787a35bdd44SMichael Tuexen sopt.sopt_dir = SOPT_SET; 2788a35bdd44SMichael Tuexen sopt.sopt_val = params->sop_optval; 2789a35bdd44SMichael Tuexen sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters); 2790a35bdd44SMichael Tuexen sopt.sopt_td = NULL; 2791a0aeb1ceSMichael Tuexen #ifdef INET6 2792a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) { 2793a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_laddr)) 2794a35bdd44SMichael Tuexen params->sop_inc.inc6_laddr.s6_addr16[1] = 2795a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2796a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_faddr)) 2797a35bdd44SMichael Tuexen params->sop_inc.inc6_faddr.s6_addr16[1] = 2798a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2799a35bdd44SMichael Tuexen } 2800a0aeb1ceSMichael Tuexen #endif 2801a35bdd44SMichael Tuexen if (params->sop_inc.inc_lport != htons(0)) { 2802a35bdd44SMichael Tuexen if (params->sop_inc.inc_fport == htons(0)) 2803a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH_WILD(params->sop_inc.inc_lport, 2804a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2805a35bdd44SMichael Tuexen else 2806a0aeb1ceSMichael Tuexen #ifdef INET6 2807a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) 2808a35bdd44SMichael Tuexen inpi.hash = INP6_PCBHASH( 2809a35bdd44SMichael Tuexen ¶ms->sop_inc.inc6_faddr, 2810a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2811a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2812a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2813a35bdd44SMichael Tuexen else 2814a0aeb1ceSMichael Tuexen #endif 2815a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH( 2816a35bdd44SMichael Tuexen ¶ms->sop_inc.inc_faddr, 2817a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2818a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2819a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2820a35bdd44SMichael Tuexen } 2821a35bdd44SMichael Tuexen while ((inp = inp_next(&inpi)) != NULL) 2822a35bdd44SMichael Tuexen if (inp->inp_gencnt == params->sop_id) { 282353af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 2824a35bdd44SMichael Tuexen INP_WUNLOCK(inp); 2825a35bdd44SMichael Tuexen return (ECONNRESET); 2826a35bdd44SMichael Tuexen } 2827a35bdd44SMichael Tuexen so = inp->inp_socket; 2828a35bdd44SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 2829a35bdd44SMichael Tuexen soref(so); 2830a35bdd44SMichael Tuexen error = (*ctloutput_set)(inp, &sopt); 2831a35bdd44SMichael Tuexen sorele(so); 2832a35bdd44SMichael Tuexen break; 2833a35bdd44SMichael Tuexen } 2834a35bdd44SMichael Tuexen if (inp == NULL) 2835a35bdd44SMichael Tuexen error = ESRCH; 2836a35bdd44SMichael Tuexen return (error); 2837a35bdd44SMichael Tuexen } 2838a35bdd44SMichael Tuexen 2839497057eeSRobert Watson #ifdef DDB 2840497057eeSRobert Watson static void 2841497057eeSRobert Watson db_print_indent(int indent) 2842497057eeSRobert Watson { 2843497057eeSRobert Watson int i; 2844497057eeSRobert Watson 2845497057eeSRobert Watson for (i = 0; i < indent; i++) 2846497057eeSRobert Watson db_printf(" "); 2847497057eeSRobert Watson } 2848497057eeSRobert Watson 2849497057eeSRobert Watson static void 2850497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2851497057eeSRobert Watson { 2852497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2853497057eeSRobert Watson 2854497057eeSRobert Watson db_print_indent(indent); 2855497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2856497057eeSRobert Watson 2857497057eeSRobert Watson indent += 2; 2858497057eeSRobert Watson 285903dc38a4SRobert Watson #ifdef INET6 2860dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2861497057eeSRobert Watson /* IPv6. */ 2862497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2863497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 286483e521ecSBjoern A. Zeeb } else 286503dc38a4SRobert Watson #endif 286683e521ecSBjoern A. Zeeb { 2867497057eeSRobert Watson /* IPv4. */ 2868497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2869497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2870497057eeSRobert Watson } 2871497057eeSRobert Watson db_print_indent(indent); 2872497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2873497057eeSRobert Watson ntohs(inc->inc_lport)); 2874497057eeSRobert Watson db_print_indent(indent); 2875497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2876497057eeSRobert Watson ntohs(inc->inc_fport)); 2877497057eeSRobert Watson } 2878497057eeSRobert Watson 2879497057eeSRobert Watson static void 2880497057eeSRobert Watson db_print_inpflags(int inp_flags) 2881497057eeSRobert Watson { 2882497057eeSRobert Watson int comma; 2883497057eeSRobert Watson 2884497057eeSRobert Watson comma = 0; 2885497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2886497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2887497057eeSRobert Watson comma = 1; 2888497057eeSRobert Watson } 2889497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2890497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2891497057eeSRobert Watson comma = 1; 2892497057eeSRobert Watson } 2893497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2894497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2895497057eeSRobert Watson comma = 1; 2896497057eeSRobert Watson } 2897dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2898dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2899dce33a45SErmal Luçi comma = 1; 2900dce33a45SErmal Luçi } 2901497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2902497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2903497057eeSRobert Watson comma = 1; 2904497057eeSRobert Watson } 2905497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2906497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2907497057eeSRobert Watson comma = 1; 2908497057eeSRobert Watson } 2909497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2910497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2911497057eeSRobert Watson comma = 1; 2912497057eeSRobert Watson } 2913497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2914497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2915497057eeSRobert Watson comma = 1; 2916497057eeSRobert Watson } 2917497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2918497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2919497057eeSRobert Watson comma = 1; 2920497057eeSRobert Watson } 2921497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2922497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2923497057eeSRobert Watson comma = 1; 2924497057eeSRobert Watson } 2925497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2926497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2927497057eeSRobert Watson comma = 1; 2928497057eeSRobert Watson } 2929497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2930497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2931497057eeSRobert Watson comma = 1; 2932497057eeSRobert Watson } 29333cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 29343cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 29353cca425bSMichael Tuexen comma = 1; 29363cca425bSMichael Tuexen } 2937497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2938497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2939497057eeSRobert Watson comma = 1; 2940497057eeSRobert Watson } 2941497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2942497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2943497057eeSRobert Watson comma = 1; 2944497057eeSRobert Watson } 2945497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2946497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2947497057eeSRobert Watson comma = 1; 2948497057eeSRobert Watson } 2949497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2950497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2951497057eeSRobert Watson comma = 1; 2952497057eeSRobert Watson } 2953497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2954497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2955497057eeSRobert Watson comma = 1; 2956497057eeSRobert Watson } 2957497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2958497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2959497057eeSRobert Watson comma = 1; 2960497057eeSRobert Watson } 2961497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2962497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2963497057eeSRobert Watson comma = 1; 2964497057eeSRobert Watson } 2965497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2966497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2967497057eeSRobert Watson comma = 1; 2968497057eeSRobert Watson } 2969497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2970497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2971497057eeSRobert Watson comma = 1; 2972497057eeSRobert Watson } 2973ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2974ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2975ad71fe3cSRobert Watson comma = 1; 2976ad71fe3cSRobert Watson } 2977ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2978ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2979ad71fe3cSRobert Watson comma = 1; 2980ad71fe3cSRobert Watson } 2981ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2982ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2983ad71fe3cSRobert Watson comma = 1; 2984ad71fe3cSRobert Watson } 2985497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2986497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2987497057eeSRobert Watson comma = 1; 2988497057eeSRobert Watson } 2989497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2990497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2991497057eeSRobert Watson comma = 1; 2992497057eeSRobert Watson } 2993497057eeSRobert Watson } 2994497057eeSRobert Watson 2995497057eeSRobert Watson static void 2996497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2997497057eeSRobert Watson { 2998497057eeSRobert Watson int comma; 2999497057eeSRobert Watson 3000497057eeSRobert Watson comma = 0; 3001497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3002497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3003497057eeSRobert Watson comma = 1; 3004497057eeSRobert Watson } 3005497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3006497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3007497057eeSRobert Watson comma = 1; 3008497057eeSRobert Watson } 3009497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3010497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3011497057eeSRobert Watson comma = 1; 3012497057eeSRobert Watson } 3013497057eeSRobert Watson } 3014497057eeSRobert Watson 30156d888973SRobert Watson static void 3016497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3017497057eeSRobert Watson { 3018497057eeSRobert Watson 3019497057eeSRobert Watson db_print_indent(indent); 3020497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3021497057eeSRobert Watson 3022497057eeSRobert Watson indent += 2; 3023497057eeSRobert Watson 3024497057eeSRobert Watson db_print_indent(indent); 3025497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3026497057eeSRobert Watson 3027497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3028497057eeSRobert Watson 3029497057eeSRobert Watson db_print_indent(indent); 3030497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 3031497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 3032497057eeSRobert Watson 3033497057eeSRobert Watson db_print_indent(indent); 3034497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3035497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3036497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3037497057eeSRobert Watson db_printf(")\n"); 3038497057eeSRobert Watson 3039497057eeSRobert Watson db_print_indent(indent); 3040497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3041497057eeSRobert Watson inp->inp_vflag); 3042497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3043497057eeSRobert Watson db_printf(")\n"); 3044497057eeSRobert Watson 3045497057eeSRobert Watson db_print_indent(indent); 3046497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3047497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3048497057eeSRobert Watson 3049497057eeSRobert Watson db_print_indent(indent); 3050497057eeSRobert Watson #ifdef INET6 3051497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3052497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3053497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3054497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3055497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3056497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3057497057eeSRobert Watson inp->in6p_hops); 3058497057eeSRobert Watson } else 3059497057eeSRobert Watson #endif 3060497057eeSRobert Watson { 3061497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3062497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3063497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3064497057eeSRobert Watson } 3065497057eeSRobert Watson 3066497057eeSRobert Watson db_print_indent(indent); 3067497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3068497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3069497057eeSRobert Watson } 3070497057eeSRobert Watson 3071497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3072497057eeSRobert Watson { 3073497057eeSRobert Watson struct inpcb *inp; 3074497057eeSRobert Watson 3075497057eeSRobert Watson if (!have_addr) { 3076497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3077497057eeSRobert Watson return; 3078497057eeSRobert Watson } 3079497057eeSRobert Watson inp = (struct inpcb *)addr; 3080497057eeSRobert Watson 3081497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3082497057eeSRobert Watson } 308383e521ecSBjoern A. Zeeb #endif /* DDB */ 3084f3e7afe2SHans Petter Selasky 3085f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3086f3e7afe2SHans Petter Selasky /* 3087f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3088f3e7afe2SHans Petter Selasky * if any. 3089f3e7afe2SHans Petter Selasky */ 3090f3e7afe2SHans Petter Selasky int 3091f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3092f3e7afe2SHans Petter Selasky { 3093f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3094f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 309520abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3096f3e7afe2SHans Petter Selasky }; 3097f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3098f3e7afe2SHans Petter Selasky int error; 3099f3e7afe2SHans Petter Selasky 3100f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3101f3e7afe2SHans Petter Selasky if (mst == NULL) 3102f3e7afe2SHans Petter Selasky return (EINVAL); 3103f3e7afe2SHans Petter Selasky 3104c782ea8bSJohn Baldwin if (mst->sw->snd_tag_modify == NULL) { 3105f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3106f3e7afe2SHans Petter Selasky } else { 3107c782ea8bSJohn Baldwin error = mst->sw->snd_tag_modify(mst, ¶ms); 3108f3e7afe2SHans Petter Selasky } 3109f3e7afe2SHans Petter Selasky return (error); 3110f3e7afe2SHans Petter Selasky } 3111f3e7afe2SHans Petter Selasky 3112f3e7afe2SHans Petter Selasky /* 3113f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3114f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3115f3e7afe2SHans Petter Selasky */ 3116f3e7afe2SHans Petter Selasky int 3117f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3118f3e7afe2SHans Petter Selasky { 3119f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3120f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3121f3e7afe2SHans Petter Selasky int error; 3122f3e7afe2SHans Petter Selasky 3123f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3124f3e7afe2SHans Petter Selasky if (mst == NULL) 3125f3e7afe2SHans Petter Selasky return (EINVAL); 3126f3e7afe2SHans Petter Selasky 3127c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) { 3128f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3129f3e7afe2SHans Petter Selasky } else { 3130c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 3131f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3132f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3133f3e7afe2SHans Petter Selasky } 3134f3e7afe2SHans Petter Selasky return (error); 3135f3e7afe2SHans Petter Selasky } 3136f3e7afe2SHans Petter Selasky 3137f3e7afe2SHans Petter Selasky /* 313895ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 313995ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 314095ed5015SHans Petter Selasky */ 314195ed5015SHans Petter Selasky int 314295ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 314395ed5015SHans Petter Selasky { 314495ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 314595ed5015SHans Petter Selasky struct m_snd_tag *mst; 314695ed5015SHans Petter Selasky int error; 314795ed5015SHans Petter Selasky 314895ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 314995ed5015SHans Petter Selasky if (mst == NULL) 315095ed5015SHans Petter Selasky return (EINVAL); 315195ed5015SHans Petter Selasky 3152c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) 315395ed5015SHans Petter Selasky return (EOPNOTSUPP); 315495ed5015SHans Petter Selasky 3155c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 315695ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 315795ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 315895ed5015SHans Petter Selasky return (error); 315995ed5015SHans Petter Selasky } 316095ed5015SHans Petter Selasky 316195ed5015SHans Petter Selasky /* 3162f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3163f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3164f3e7afe2SHans Petter Selasky */ 3165f3e7afe2SHans Petter Selasky int 3166f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 316720abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 316820abea66SRandall Stewart 3169f3e7afe2SHans Petter Selasky { 3170f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 317195ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 317295ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3173f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3174f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 317598085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3176f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 317720abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3178f3e7afe2SHans Petter Selasky }; 3179f3e7afe2SHans Petter Selasky int error; 3180f3e7afe2SHans Petter Selasky 3181f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3182f3e7afe2SHans Petter Selasky 318385d8d30fSHans Petter Selasky /* 318485d8d30fSHans Petter Selasky * If there is already a send tag, or the INP is being torn 318585d8d30fSHans Petter Selasky * down, allocating a new send tag is not allowed. Else send 318685d8d30fSHans Petter Selasky * tags may leak. 318785d8d30fSHans Petter Selasky */ 318853af6903SGleb Smirnoff if (*st != NULL || (inp->inp_flags & INP_DROPPED) != 0) 3189f3e7afe2SHans Petter Selasky return (EINVAL); 3190f3e7afe2SHans Petter Selasky 319136e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, st); 3192903c4ee6SXin LI #ifdef INET 319320abea66SRandall Stewart if (error == 0) { 319420abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 319520abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 319636e0a362SJohn Baldwin } else if (error != EOPNOTSUPP) 319720abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3198903c4ee6SXin LI #endif 3199f3e7afe2SHans Petter Selasky return (error); 3200f3e7afe2SHans Petter Selasky } 3201f3e7afe2SHans Petter Selasky 320220abea66SRandall Stewart void 320398d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst) 320420abea66SRandall Stewart { 320520abea66SRandall Stewart 320698d7a8d9SJohn Baldwin m_snd_tag_rele(mst); 3207903c4ee6SXin LI #ifdef INET 320820abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3209903c4ee6SXin LI #endif 321020abea66SRandall Stewart } 321120abea66SRandall Stewart 3212f3e7afe2SHans Petter Selasky /* 3213f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3214f3e7afe2SHans Petter Selasky * if any: 3215f3e7afe2SHans Petter Selasky */ 3216f3e7afe2SHans Petter Selasky void 3217f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3218f3e7afe2SHans Petter Selasky { 3219f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3220f3e7afe2SHans Petter Selasky 3221f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3222f3e7afe2SHans Petter Selasky 3223f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3224f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3225f3e7afe2SHans Petter Selasky 3226f3e7afe2SHans Petter Selasky if (mst == NULL) 3227f3e7afe2SHans Petter Selasky return; 3228f3e7afe2SHans Petter Selasky 3229fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3230093e7231SHans Petter Selasky #ifdef INET 3231093e7231SHans Petter Selasky counter_u64_add(rate_limit_active, -1); 3232093e7231SHans Petter Selasky #endif 3233f3e7afe2SHans Petter Selasky } 3234f3e7afe2SHans Petter Selasky 323520abea66SRandall Stewart int 323620abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 323720abea66SRandall Stewart { 323820abea66SRandall Stewart int error; 323920abea66SRandall Stewart 324020abea66SRandall Stewart /* 324120abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 324220abea66SRandall Stewart * a route change, first drop the existing tag. Set the 324320abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 324420abea66SRandall Stewart * tag if we fail to allocate one this time. 324520abea66SRandall Stewart */ 324620abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 324720abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 324820abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 324920abea66SRandall Stewart } 325020abea66SRandall Stewart 325120abea66SRandall Stewart /* 325220abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 325320abea66SRandall Stewart * made to ensure the network interface doesn't go away until 325420abea66SRandall Stewart * all ratelimit connections are gone. The network interface 325520abea66SRandall Stewart * pointers compared below represent valid network interfaces, 325620abea66SRandall Stewart * except when comparing towards NULL. 325720abea66SRandall Stewart */ 325820abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 325920abea66SRandall Stewart error = 0; 326020abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 326120abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 326220abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 326320abea66SRandall Stewart error = 0; 326420abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 326520abea66SRandall Stewart /* 326620abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 326720abea66SRandall Stewart * to wait until there is a valid RSS hash before we 326820abea66SRandall Stewart * can proceed: 326920abea66SRandall Stewart */ 327020abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 327120abea66SRandall Stewart error = EAGAIN; 327220abea66SRandall Stewart } else { 327320abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 327420abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 327520abea66SRandall Stewart } 327620abea66SRandall Stewart } else { 327720abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 327820abea66SRandall Stewart } 327920abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 328020abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 328120abea66SRandall Stewart 328220abea66SRandall Stewart return (error); 328320abea66SRandall Stewart } 328420abea66SRandall Stewart 3285f3e7afe2SHans Petter Selasky /* 3286f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3287f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3288f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3289f3e7afe2SHans Petter Selasky */ 3290f3e7afe2SHans Petter Selasky void 3291f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3292f3e7afe2SHans Petter Selasky { 3293f3e7afe2SHans Petter Selasky struct socket *socket; 3294f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3295f3e7afe2SHans Petter Selasky bool did_upgrade; 3296f3e7afe2SHans Petter Selasky 3297f3e7afe2SHans Petter Selasky if (inp == NULL) 3298f3e7afe2SHans Petter Selasky return; 3299f3e7afe2SHans Petter Selasky 3300f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3301f3e7afe2SHans Petter Selasky if (socket == NULL) 3302f3e7afe2SHans Petter Selasky return; 3303f3e7afe2SHans Petter Selasky 3304f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3305f3e7afe2SHans Petter Selasky /* 3306f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3307f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3308f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3309f3e7afe2SHans Petter Selasky * write lock. 3310f3e7afe2SHans Petter Selasky */ 3311f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3312f3e7afe2SHans Petter Selasky return; 3313f3e7afe2SHans Petter Selasky did_upgrade = 1; 3314f3e7afe2SHans Petter Selasky } else { 3315f3e7afe2SHans Petter Selasky did_upgrade = 0; 3316f3e7afe2SHans Petter Selasky } 3317f3e7afe2SHans Petter Selasky 3318f3e7afe2SHans Petter Selasky /* 3319f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3320f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3321f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3322f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3323f3e7afe2SHans Petter Selasky */ 3324f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3325f3e7afe2SHans Petter Selasky 3326bab34d63SJohn Baldwin in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3327fb3bc596SJohn Baldwin 3328f3e7afe2SHans Petter Selasky if (did_upgrade) 3329f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3330f3e7afe2SHans Petter Selasky } 3331f3e7afe2SHans Petter Selasky 3332f3e7afe2SHans Petter Selasky /* 3333f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3334f3e7afe2SHans Petter Selasky */ 3335f3e7afe2SHans Petter Selasky void 3336f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3337f3e7afe2SHans Petter Selasky { 3338f3e7afe2SHans Petter Selasky bool did_upgrade; 3339f3e7afe2SHans Petter Selasky 3340f3e7afe2SHans Petter Selasky if (inp == NULL) 3341f3e7afe2SHans Petter Selasky return; 3342f3e7afe2SHans Petter Selasky 3343f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3344f3e7afe2SHans Petter Selasky return; 3345f3e7afe2SHans Petter Selasky 3346f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3347f3e7afe2SHans Petter Selasky /* 3348f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3349f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3350f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3351f3e7afe2SHans Petter Selasky * write lock. 3352f3e7afe2SHans Petter Selasky */ 3353f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3354f3e7afe2SHans Petter Selasky return; 3355f3e7afe2SHans Petter Selasky did_upgrade = 1; 3356f3e7afe2SHans Petter Selasky } else { 3357f3e7afe2SHans Petter Selasky did_upgrade = 0; 3358f3e7afe2SHans Petter Selasky } 3359f3e7afe2SHans Petter Selasky 3360f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3361f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3362f3e7afe2SHans Petter Selasky 3363f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3364f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3365f3e7afe2SHans Petter Selasky 3366f3e7afe2SHans Petter Selasky if (did_upgrade) 3367f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3368f3e7afe2SHans Petter Selasky } 336920abea66SRandall Stewart 3370903c4ee6SXin LI #ifdef INET 337120abea66SRandall Stewart static void 337220abea66SRandall Stewart rl_init(void *st) 337320abea66SRandall Stewart { 33741a714ff2SRandall Stewart rate_limit_new = counter_u64_alloc(M_WAITOK); 33751a714ff2SRandall Stewart rate_limit_chg = counter_u64_alloc(M_WAITOK); 337620abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 337720abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 337820abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 337920abea66SRandall Stewart } 338020abea66SRandall Stewart 338120abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3382903c4ee6SXin LI #endif 3383f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3384