1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 42469dd60SGarrett Wollman * Copyright (c) 1982, 1986, 1991, 1993, 1995 5497057eeSRobert Watson * The Regents of the University of California. 6111d57a6SRobert Watson * Copyright (c) 2007-2009 Robert N. M. Watson 779bdc6e5SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 85ebea466SGleb Smirnoff * Copyright (c) 2021-2022 Gleb Smirnoff <glebius@FreeBSD.org> 9497057eeSRobert Watson * All rights reserved. 10df8bae1dSRodney W. Grimes * 1179bdc6e5SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 1279bdc6e5SRobert Watson * contract to Juniper Networks, Inc. 1379bdc6e5SRobert Watson * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 394b421e2dSMike Silbersack #include <sys/cdefs.h> 40497057eeSRobert Watson #include "opt_ddb.h" 416a800098SYoshinobu Inoue #include "opt_ipsec.h" 42efc76f72SBjoern A. Zeeb #include "opt_inet.h" 43cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 44f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 450c325f53SAlexander V. Chernikov #include "opt_route.h" 467527624eSRobert Watson #include "opt_rss.h" 47cfa1ca9dSYoshinobu Inoue 48df8bae1dSRodney W. Grimes #include <sys/param.h> 49a0577692SGleb Smirnoff #include <sys/hash.h> 50df8bae1dSRodney W. Grimes #include <sys/systm.h> 51a0577692SGleb Smirnoff #include <sys/libkern.h> 52cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 53df8bae1dSRodney W. Grimes #include <sys/malloc.h> 54df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 55ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h> 56cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 57fdb987beSMark Johnston #include <sys/proc.h> 58df8bae1dSRodney W. Grimes #include <sys/protosw.h> 593ee9c3c4SRandall Stewart #include <sys/smp.h> 60fdb987beSMark Johnston #include <sys/smr.h> 61df8bae1dSRodney W. Grimes #include <sys/socket.h> 62df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 63f3e7afe2SHans Petter Selasky #include <sys/sockio.h> 64acd3428bSRobert Watson #include <sys/priv.h> 65df8bae1dSRodney W. Grimes #include <sys/proc.h> 6679bdc6e5SRobert Watson #include <sys/refcount.h> 6775c13541SPoul-Henning Kamp #include <sys/jail.h> 68101f9fc8SPeter Wemm #include <sys/kernel.h> 69101f9fc8SPeter Wemm #include <sys/sysctl.h> 708781d8e9SBruce Evans 71497057eeSRobert Watson #ifdef DDB 72497057eeSRobert Watson #include <ddb/ddb.h> 73497057eeSRobert Watson #endif 74497057eeSRobert Watson 7569c2d429SJeff Roberson #include <vm/uma.h> 76a034518aSAndrew Gallatin #include <vm/vm.h> 77df8bae1dSRodney W. Grimes 78df8bae1dSRodney W. Grimes #include <net/if.h> 7976039bc8SGleb Smirnoff #include <net/if_var.h> 803d0d5b21SJustin Hibbits #include <net/if_private.h> 81cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 826d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h> 83df8bae1dSRodney W. Grimes #include <net/route.h> 84b2bdc62aSAdrian Chadd #include <net/rss_config.h> 85530c0060SRobert Watson #include <net/vnet.h> 86df8bae1dSRodney W. Grimes 8767107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 88df8bae1dSRodney W. Grimes #include <netinet/in.h> 89df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 900f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h> 9173bebcc5SGleb Smirnoff #include <netinet/tcp.h> 9259854ecfSHans Petter Selasky #ifdef INET 9359854ecfSHans Petter Selasky #include <netinet/in_var.h> 949ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h> 9559854ecfSHans Petter Selasky #endif 96df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 97cfa1ca9dSYoshinobu Inoue #ifdef INET6 98cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 99efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 10067107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 10167107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 102cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1039ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h> 10459854ecfSHans Petter Selasky #endif 105cfa1ca9dSYoshinobu Inoue 106fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 107b9234fafSSam Leffler 108aed55708SRobert Watson #include <security/mac/mac_framework.h> 109aed55708SRobert Watson 1101a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 1111a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 112a43e7a96SGleb Smirnoff 113a43e7a96SGleb Smirnoff #define INP_FREED 0x00000200 /* Went through in_pcbfree(). */ 114a43e7a96SGleb Smirnoff #define INP_INLBGROUP 0x01000000 /* Inserted into inpcblbgroup. */ 1151a43cff9SSean Bruno 116101f9fc8SPeter Wemm /* 117101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 118101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 119101f9fc8SPeter Wemm */ 120eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 126101f9fc8SPeter Wemm 127b0d22693SCrist J. Clark /* 128b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 129b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 130b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 131b0d22693SCrist J. Clark */ 132eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 133eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 134b0d22693SCrist J. Clark 13519acc506SGleb Smirnoff /* Enable random ephemeral port allocation by default. */ 13619acc506SGleb Smirnoff VNET_DEFINE(int, ipport_randomized) = 1; 1376ac48b74SMike Silbersack 138d2025bd0SBjoern A. Zeeb #ifdef INET 139fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 140fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 141fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 1424130ea61SMark Johnston int lookupflags, uint8_t numa_domain); 14367107f45SBjoern A. Zeeb 144bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 145bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 146bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 147bbd42ad0SPeter Wemm 148bbd42ad0SPeter Wemm static int 14982d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 150bbd42ad0SPeter Wemm { 15130a4ab08SBruce Evans int error; 15230a4ab08SBruce Evans 153f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 15430a4ab08SBruce Evans if (error == 0) { 155603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 156603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 157603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 158603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 159603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 160603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 161bbd42ad0SPeter Wemm } 16230a4ab08SBruce Evans return (error); 163bbd42ad0SPeter Wemm } 164bbd42ad0SPeter Wemm 165bbd42ad0SPeter Wemm #undef RANGECHK 166bbd42ad0SPeter Wemm 1677029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, 1687029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1696472ac3dSEd Schouten "IP Ports"); 17033b3ac06SPeter Wemm 1716df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1727029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1737029da5cSPawel Biernacki &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", 1747029da5cSPawel Biernacki ""); 1756df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1767029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1777029da5cSPawel Biernacki &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", 1787029da5cSPawel Biernacki ""); 1796df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1807029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1817029da5cSPawel Biernacki &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", 1827029da5cSPawel Biernacki ""); 1836df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1847029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1857029da5cSPawel Biernacki &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", 1867029da5cSPawel Biernacki ""); 1876df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1887029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1897029da5cSPawel Biernacki &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", 1907029da5cSPawel Biernacki ""); 1916df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 1927029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1937029da5cSPawel Biernacki &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", 1947029da5cSPawel Biernacki ""); 1956df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 1966df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 1976df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 199eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 2016df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 202eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 20320abea66SRandall Stewart 20420abea66SRandall Stewart #ifdef RATELIMIT 2051a714ff2SRandall Stewart counter_u64_t rate_limit_new; 2061a714ff2SRandall Stewart counter_u64_t rate_limit_chg; 20720abea66SRandall Stewart counter_u64_t rate_limit_active; 20820abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail; 20920abea66SRandall Stewart counter_u64_t rate_limit_set_ok; 21020abea66SRandall Stewart 2117029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 21220abea66SRandall Stewart "IP Rate Limiting"); 21320abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD, 21420abea66SRandall Stewart &rate_limit_active, "Active rate limited connections"); 21520abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD, 21620abea66SRandall Stewart &rate_limit_alloc_fail, "Rate limited connection failures"); 21720abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD, 21820abea66SRandall Stewart &rate_limit_set_ok, "Rate limited setting succeeded"); 2191a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD, 2201a714ff2SRandall Stewart &rate_limit_new, "Total Rate limit new attempts"); 2211a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD, 2221a714ff2SRandall Stewart &rate_limit_chg, "Total Rate limited change attempts"); 22320abea66SRandall Stewart #endif /* RATELIMIT */ 22420abea66SRandall Stewart 22583e521ecSBjoern A. Zeeb #endif /* INET */ 2260312fbe9SPoul-Henning Kamp 227a0577692SGleb Smirnoff VNET_DEFINE(uint32_t, in_pcbhashseed); 228a0577692SGleb Smirnoff static void 229a0577692SGleb Smirnoff in_pcbhashseed_init(void) 230a0577692SGleb Smirnoff { 231a0577692SGleb Smirnoff 232a0577692SGleb Smirnoff V_in_pcbhashseed = arc4random(); 233a0577692SGleb Smirnoff } 234a0577692SGleb Smirnoff VNET_SYSINIT(in_pcbhashseed_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 235a0577692SGleb Smirnoff in_pcbhashseed_init, 0); 236a0577692SGleb Smirnoff 2373ba34b07SGleb Smirnoff static void in_pcbremhash(struct inpcb *); 2383ba34b07SGleb Smirnoff 239c3229e05SDavid Greenman /* 240c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 241c3229e05SDavid Greenman * 242de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 243de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 244de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 245c3229e05SDavid Greenman */ 246c3229e05SDavid Greenman 2471a43cff9SSean Bruno static struct inpcblbgroup * 248d93ec8cbSMark Johnston in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, struct ucred *cred, 249d93ec8cbSMark Johnston u_char vflag, uint16_t port, const union in_dependaddr *addr, int size, 250a034518aSAndrew Gallatin uint8_t numa_domain) 2511a43cff9SSean Bruno { 2521a43cff9SSean Bruno struct inpcblbgroup *grp; 2531a43cff9SSean Bruno size_t bytes; 2541a43cff9SSean Bruno 2551a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 2561a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 257d93ec8cbSMark Johnston if (grp == NULL) 2581a43cff9SSean Bruno return (NULL); 259d93ec8cbSMark Johnston grp->il_cred = crhold(cred); 2601a43cff9SSean Bruno grp->il_vflag = vflag; 2611a43cff9SSean Bruno grp->il_lport = port; 262a034518aSAndrew Gallatin grp->il_numa_domain = numa_domain; 2631a43cff9SSean Bruno grp->il_dependladdr = *addr; 2641a43cff9SSean Bruno grp->il_inpsiz = size; 26554af3d0dSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 2661a43cff9SSean Bruno return (grp); 2671a43cff9SSean Bruno } 2681a43cff9SSean Bruno 2691a43cff9SSean Bruno static void 27054af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx) 27154af3d0dSMark Johnston { 27254af3d0dSMark Johnston struct inpcblbgroup *grp; 27354af3d0dSMark Johnston 27454af3d0dSMark Johnston grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 275d93ec8cbSMark Johnston crfree(grp->il_cred); 27654af3d0dSMark Johnston free(grp, M_PCB); 27754af3d0dSMark Johnston } 27854af3d0dSMark Johnston 27954af3d0dSMark Johnston static void 2801a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 2811a43cff9SSean Bruno { 2821a43cff9SSean Bruno 28354af3d0dSMark Johnston CK_LIST_REMOVE(grp, il_list); 2842a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx); 2851a43cff9SSean Bruno } 2861a43cff9SSean Bruno 2871a43cff9SSean Bruno static struct inpcblbgroup * 2881a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 2891a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 2901a43cff9SSean Bruno { 2911a43cff9SSean Bruno struct inpcblbgroup *grp; 2921a43cff9SSean Bruno int i; 2931a43cff9SSean Bruno 294d93ec8cbSMark Johnston grp = in_pcblbgroup_alloc(hdr, old_grp->il_cred, old_grp->il_vflag, 295a034518aSAndrew Gallatin old_grp->il_lport, &old_grp->il_dependladdr, size, 296a034518aSAndrew Gallatin old_grp->il_numa_domain); 29779ee680bSMark Johnston if (grp == NULL) 2981a43cff9SSean Bruno return (NULL); 2991a43cff9SSean Bruno 3001a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 3011a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 3021a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 3031a43cff9SSean Bruno 3041a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 3051a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 3061a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 3071a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 3081a43cff9SSean Bruno return (grp); 3091a43cff9SSean Bruno } 3101a43cff9SSean Bruno 3111a43cff9SSean Bruno /* 3121a43cff9SSean Bruno * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 3131a43cff9SSean Bruno * and shrink group if possible. 3141a43cff9SSean Bruno */ 3151a43cff9SSean Bruno static void 3161a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 3171a43cff9SSean Bruno int i) 3181a43cff9SSean Bruno { 31979ee680bSMark Johnston struct inpcblbgroup *grp, *new_grp; 3201a43cff9SSean Bruno 32179ee680bSMark Johnston grp = *grpp; 3221a43cff9SSean Bruno for (; i + 1 < grp->il_inpcnt; ++i) 3231a43cff9SSean Bruno grp->il_inp[i] = grp->il_inp[i + 1]; 3241a43cff9SSean Bruno grp->il_inpcnt--; 3251a43cff9SSean Bruno 3261a43cff9SSean Bruno if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 32779ee680bSMark Johnston grp->il_inpcnt <= grp->il_inpsiz / 4) { 3281a43cff9SSean Bruno /* Shrink this group. */ 32979ee680bSMark Johnston new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 33079ee680bSMark Johnston if (new_grp != NULL) 3311a43cff9SSean Bruno *grpp = new_grp; 3321a43cff9SSean Bruno } 3331a43cff9SSean Bruno } 3341a43cff9SSean Bruno 3351a43cff9SSean Bruno /* 3361a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 3371a43cff9SSean Bruno */ 3381a43cff9SSean Bruno static int 339a034518aSAndrew Gallatin in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain) 3401a43cff9SSean Bruno { 341a7026c7fSMark Johnston const static struct timeval interval = { 60, 0 }; 342a7026c7fSMark Johnston static struct timeval lastprint; 3431a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3441a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3451a43cff9SSean Bruno struct inpcblbgroup *grp; 34679ee680bSMark Johnston uint32_t idx; 3471a43cff9SSean Bruno 3481a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 3491a43cff9SSean Bruno 3501a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 3511a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 3521a43cff9SSean Bruno 3531a43cff9SSean Bruno #ifdef INET6 3541a43cff9SSean Bruno /* 3551a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 3561a43cff9SSean Bruno */ 3571a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 3581a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 3591a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 3601a43cff9SSean Bruno return (0); 3611a43cff9SSean Bruno } 3621a43cff9SSean Bruno #endif 3631a43cff9SSean Bruno 3649d2877fcSMark Johnston idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask); 36579ee680bSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 36654af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 367d93ec8cbSMark Johnston if (grp->il_cred->cr_prison == inp->inp_cred->cr_prison && 368d93ec8cbSMark Johnston grp->il_vflag == inp->inp_vflag && 3691a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 370a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain && 3711a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 3721a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 373d93ec8cbSMark Johnston sizeof(grp->il_dependladdr)) == 0) { 3741a43cff9SSean Bruno break; 3751a43cff9SSean Bruno } 376d93ec8cbSMark Johnston } 3771a43cff9SSean Bruno if (grp == NULL) { 3781a43cff9SSean Bruno /* Create new load balance group. */ 379d93ec8cbSMark Johnston grp = in_pcblbgroup_alloc(hdr, inp->inp_cred, inp->inp_vflag, 3801a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 381a034518aSAndrew Gallatin INPCBLBGROUP_SIZMIN, numa_domain); 38279ee680bSMark Johnston if (grp == NULL) 3831a43cff9SSean Bruno return (ENOBUFS); 3841a43cff9SSean Bruno } else if (grp->il_inpcnt == grp->il_inpsiz) { 3851a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 386a7026c7fSMark Johnston if (ratecheck(&lastprint, &interval)) 3871a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 3881a43cff9SSean Bruno ntohs(grp->il_lport)); 3891a43cff9SSean Bruno return (0); 3901a43cff9SSean Bruno } 3911a43cff9SSean Bruno 3921a43cff9SSean Bruno /* Expand this local group. */ 3931a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 39479ee680bSMark Johnston if (grp == NULL) 3951a43cff9SSean Bruno return (ENOBUFS); 3961a43cff9SSean Bruno } 3971a43cff9SSean Bruno 3981a43cff9SSean Bruno KASSERT(grp->il_inpcnt < grp->il_inpsiz, 39979ee680bSMark Johnston ("invalid local group size %d and count %d", grp->il_inpsiz, 40079ee680bSMark Johnston grp->il_inpcnt)); 4011a43cff9SSean Bruno 4021a43cff9SSean Bruno grp->il_inp[grp->il_inpcnt] = inp; 4031a43cff9SSean Bruno grp->il_inpcnt++; 404a43e7a96SGleb Smirnoff inp->inp_flags |= INP_INLBGROUP; 4051a43cff9SSean Bruno return (0); 4061a43cff9SSean Bruno } 4071a43cff9SSean Bruno 4081a43cff9SSean Bruno /* 4091a43cff9SSean Bruno * Remove PCB from load balance group. 4101a43cff9SSean Bruno */ 4111a43cff9SSean Bruno static void 4121a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 4131a43cff9SSean Bruno { 4141a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 4151a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 4161a43cff9SSean Bruno struct inpcblbgroup *grp; 4171a43cff9SSean Bruno int i; 4181a43cff9SSean Bruno 4191a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 4201a43cff9SSean Bruno 4211a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 422a43e7a96SGleb Smirnoff MPASS(inp->inp_flags & INP_INLBGROUP); 4231a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 4241a43cff9SSean Bruno 4251a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 4269d2877fcSMark Johnston INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 42754af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 4281a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 4291a43cff9SSean Bruno if (grp->il_inp[i] != inp) 4301a43cff9SSean Bruno continue; 4311a43cff9SSean Bruno 4321a43cff9SSean Bruno if (grp->il_inpcnt == 1) { 4331a43cff9SSean Bruno /* We are the last, free this local group. */ 4341a43cff9SSean Bruno in_pcblbgroup_free(grp); 4351a43cff9SSean Bruno } else { 4361a43cff9SSean Bruno /* Pull up inpcbs, shrink group if possible. */ 4371a43cff9SSean Bruno in_pcblbgroup_reorder(hdr, &grp, i); 4381a43cff9SSean Bruno } 439a43e7a96SGleb Smirnoff inp->inp_flags &= ~INP_INLBGROUP; 4401a43cff9SSean Bruno return; 4411a43cff9SSean Bruno } 4421a43cff9SSean Bruno } 443a43e7a96SGleb Smirnoff KASSERT(0, ("%s: did not find %p", __func__, inp)); 4441a43cff9SSean Bruno } 4451a43cff9SSean Bruno 446a034518aSAndrew Gallatin int 447a034518aSAndrew Gallatin in_pcblbgroup_numa(struct inpcb *inp, int arg) 448a034518aSAndrew Gallatin { 449a034518aSAndrew Gallatin struct inpcbinfo *pcbinfo; 450a034518aSAndrew Gallatin struct inpcblbgrouphead *hdr; 451a034518aSAndrew Gallatin struct inpcblbgroup *grp; 452a034518aSAndrew Gallatin int err, i; 453a034518aSAndrew Gallatin uint8_t numa_domain; 454a034518aSAndrew Gallatin 455a034518aSAndrew Gallatin switch (arg) { 456a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_NODOM: 457a034518aSAndrew Gallatin numa_domain = M_NODOM; 458a034518aSAndrew Gallatin break; 459a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_CURDOM: 460a034518aSAndrew Gallatin numa_domain = PCPU_GET(domain); 461a034518aSAndrew Gallatin break; 462a034518aSAndrew Gallatin default: 463a034518aSAndrew Gallatin if (arg < 0 || arg >= vm_ndomains) 464a034518aSAndrew Gallatin return (EINVAL); 465a034518aSAndrew Gallatin numa_domain = arg; 466a034518aSAndrew Gallatin } 467a034518aSAndrew Gallatin 468a034518aSAndrew Gallatin err = 0; 469a034518aSAndrew Gallatin pcbinfo = inp->inp_pcbinfo; 470a034518aSAndrew Gallatin INP_WLOCK_ASSERT(inp); 471a034518aSAndrew Gallatin INP_HASH_WLOCK(pcbinfo); 472a034518aSAndrew Gallatin hdr = &pcbinfo->ipi_lbgrouphashbase[ 473a034518aSAndrew Gallatin INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 474a034518aSAndrew Gallatin CK_LIST_FOREACH(grp, hdr, il_list) { 475a034518aSAndrew Gallatin for (i = 0; i < grp->il_inpcnt; ++i) { 476a034518aSAndrew Gallatin if (grp->il_inp[i] != inp) 477a034518aSAndrew Gallatin continue; 478a034518aSAndrew Gallatin 479a034518aSAndrew Gallatin if (grp->il_numa_domain == numa_domain) { 480a034518aSAndrew Gallatin goto abort_with_hash_wlock; 481a034518aSAndrew Gallatin } 482a034518aSAndrew Gallatin 483a034518aSAndrew Gallatin /* Remove it from the old group. */ 484a034518aSAndrew Gallatin in_pcbremlbgrouphash(inp); 485a034518aSAndrew Gallatin 486a034518aSAndrew Gallatin /* Add it to the new group based on numa domain. */ 487a034518aSAndrew Gallatin in_pcbinslbgrouphash(inp, numa_domain); 488a034518aSAndrew Gallatin goto abort_with_hash_wlock; 489a034518aSAndrew Gallatin } 490a034518aSAndrew Gallatin } 491a034518aSAndrew Gallatin err = ENOENT; 492a034518aSAndrew Gallatin abort_with_hash_wlock: 493a034518aSAndrew Gallatin INP_HASH_WUNLOCK(pcbinfo); 494a034518aSAndrew Gallatin return (err); 495a034518aSAndrew Gallatin } 496a034518aSAndrew Gallatin 497db0ac6deSCy Schubert /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 498db0ac6deSCy Schubert CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb))); 499db0ac6deSCy Schubert 500cc487c16SGleb Smirnoff /* 501fec8a8c7SGleb Smirnoff * Initialize an inpcbinfo - a per-VNET instance of connections db. 5029bcd427bSRobert Watson */ 5039bcd427bSRobert Watson void 504fec8a8c7SGleb Smirnoff in_pcbinfo_init(struct inpcbinfo *pcbinfo, struct inpcbstorage *pcbstor, 505fec8a8c7SGleb Smirnoff u_int hash_nelements, u_int porthash_nelements) 5069bcd427bSRobert Watson { 5079bcd427bSRobert Watson 508fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_lock, pcbstor->ips_infolock_name, NULL, MTX_DEF); 509fec8a8c7SGleb Smirnoff mtx_init(&pcbinfo->ipi_hash_lock, pcbstor->ips_hashlock_name, 510fec8a8c7SGleb Smirnoff NULL, MTX_DEF); 5119bcd427bSRobert Watson #ifdef VIMAGE 5129bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 5139bcd427bSRobert Watson #endif 514db0ac6deSCy Schubert CK_LIST_INIT(&pcbinfo->ipi_listhead); 515fa046d87SRobert Watson pcbinfo->ipi_count = 0; 516fdb987beSMark Johnston pcbinfo->ipi_hash_exact = hashinit(hash_nelements, M_PCB, 517fdb987beSMark Johnston &pcbinfo->ipi_hashmask); 518fdb987beSMark Johnston pcbinfo->ipi_hash_wild = hashinit(hash_nelements, M_PCB, 5199bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 520db0ac6deSCy Schubert porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 5219bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 5229bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 5239d2877fcSMark Johnston pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 5241a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 525fec8a8c7SGleb Smirnoff pcbinfo->ipi_zone = pcbstor->ips_zone; 526fec8a8c7SGleb Smirnoff pcbinfo->ipi_portzone = pcbstor->ips_portzone; 527db0ac6deSCy Schubert pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone); 5289bcd427bSRobert Watson } 5299bcd427bSRobert Watson 5309bcd427bSRobert Watson /* 5319bcd427bSRobert Watson * Destroy an inpcbinfo. 5329bcd427bSRobert Watson */ 5339bcd427bSRobert Watson void 5349bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 5359bcd427bSRobert Watson { 5369bcd427bSRobert Watson 537fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 538fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 539fa046d87SRobert Watson 540fdb987beSMark Johnston hashdestroy(pcbinfo->ipi_hash_exact, M_PCB, pcbinfo->ipi_hashmask); 541fdb987beSMark Johnston hashdestroy(pcbinfo->ipi_hash_wild, M_PCB, pcbinfo->ipi_hashmask); 5429bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 5439bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 5441a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 5451a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 546db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_hash_lock); 547db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_lock); 5489bcd427bSRobert Watson } 5499bcd427bSRobert Watson 5509bcd427bSRobert Watson /* 551fec8a8c7SGleb Smirnoff * Initialize a pcbstorage - per protocol zones to allocate inpcbs. 552fec8a8c7SGleb Smirnoff */ 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, 5605fd1a67eSMark Johnston pcbstor->ips_size, NULL, NULL, 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 } 849efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 850efc76f72SBjoern A. Zeeb 851d5bb8bd3SAdrian Chadd #ifdef INET 8520a100a6fSAdrian Chadd /* 8534b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 8544b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 8554b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 8564b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 8574b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 8584b932371SIan Dowse * 8594b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 8604b932371SIan Dowse */ 8614b932371SIan Dowse int 86296871af0SGleb Smirnoff in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, 863136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 8644b932371SIan Dowse { 8654b932371SIan Dowse struct socket *so = inp->inp_socket; 866c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 8674b932371SIan Dowse struct in_addr laddr; 868df8bae1dSRodney W. Grimes u_short lport = 0; 86968e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 870413628a7SBjoern A. Zeeb int error; 871df8bae1dSRodney W. Grimes 8728501a69cSRobert Watson /* 8731a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 8741a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 8751a43cff9SSean Bruno */ 8761a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 8771a43cff9SSean Bruno 8781a43cff9SSean Bruno /* 879fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 8808501a69cSRobert Watson */ 88159daba27SSam Leffler INP_LOCK_ASSERT(inp); 882fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 88359daba27SSam Leffler 8844b932371SIan Dowse laddr.s_addr = *laddrp; 88596871af0SGleb Smirnoff if (sin != NULL && laddr.s_addr != INADDR_ANY) 886df8bae1dSRodney W. Grimes return (EINVAL); 8871a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 88868e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 88996871af0SGleb Smirnoff if (sin == NULL) { 8907c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 8917c2f3cb9SJamie Gritton return (error); 8927c2f3cb9SJamie Gritton } else { 893f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 894f161d294SMark Johnston ("%s: invalid family for address %p", __func__, sin)); 895f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 896f161d294SMark Johnston ("%s: invalid length for address %p", __func__, sin)); 897f161d294SMark Johnston 898b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 899b89e82ddSJamie Gritton if (error) 900b89e82ddSJamie Gritton return (error); 9014b932371SIan Dowse if (sin->sin_port != *lportp) { 9024b932371SIan Dowse /* Don't allow the port to change. */ 9034b932371SIan Dowse if (*lportp != 0) 9044b932371SIan Dowse return (EINVAL); 905df8bae1dSRodney W. Grimes lport = sin->sin_port; 9064b932371SIan Dowse } 9074b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 908df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 909df8bae1dSRodney W. Grimes /* 910df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 911df8bae1dSRodney W. Grimes * allow complete duplication of binding if 912df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 913df8bae1dSRodney W. Grimes * and a multicast address is bound on both 914df8bae1dSRodney W. Grimes * new and duplicated sockets. 915df8bae1dSRodney W. Grimes */ 916f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 917df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 9181a43cff9SSean Bruno /* 9191a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 9201a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 9211a43cff9SSean Bruno */ 9221a43cff9SSean Bruno if ((so->so_options & 9231a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 9241a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 925df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 926df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 92783103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 9284209e01aSAdrian Chadd /* 9294209e01aSAdrian Chadd * Is the address a local IP address? 930f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 9318696873dSAdrian Chadd * to any endpoint address, local or not. 9324209e01aSAdrian Chadd */ 933f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 9348896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 935df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 936df8bae1dSRodney W. Grimes } 9374b932371SIan Dowse laddr = sin->sin_addr; 938df8bae1dSRodney W. Grimes if (lport) { 939df8bae1dSRodney W. Grimes struct inpcb *t; 940ae0e7143SRobert Watson 941df8bae1dSRodney W. Grimes /* GROSS */ 942603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 943603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 944cc426dd3SMateusz Guzik priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 9452469dd60SGarrett Wollman return (EACCES); 946835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 947cc426dd3SMateusz Guzik priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 948078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 949413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 950340c35deSJonathan Lemon /* 951340c35deSJonathan Lemon * XXX 952340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 953340c35deSJonathan Lemon */ 9543aff4ccdSMark Johnston if (t != NULL && 9554658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 9564658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 9574cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 95852b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 959e3ba0d6aSGleb Smirnoff (t->inp_socket->so_options & SO_REUSEPORT) || 960e3ba0d6aSGleb Smirnoff (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) && 96186d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 96286d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 9634049a042SGuido van Rooij return (EADDRINUSE); 9644049a042SGuido van Rooij } 965c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 96668e0d7e0SRobert Watson lport, lookupflags, cred); 967e3ba0d6aSGleb Smirnoff if (t != NULL && (reuseport & t->inp_socket->so_options) == 0 && 968e3ba0d6aSGleb Smirnoff (reuseport_lb & t->inp_socket->so_options) == 0) { 969e3fd5ffdSRobert Watson #ifdef INET6 97033841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 971cfa1ca9dSYoshinobu Inoue INADDR_ANY || 972cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 973cfa1ca9dSYoshinobu Inoue INADDR_ANY || 974fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 975fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 976e3fd5ffdSRobert Watson #endif 977df8bae1dSRodney W. Grimes return (EADDRINUSE); 978df8bae1dSRodney W. Grimes } 979cfa1ca9dSYoshinobu Inoue } 980df8bae1dSRodney W. Grimes } 9814b932371SIan Dowse if (*lportp != 0) 9824b932371SIan Dowse lport = *lportp; 98333b3ac06SPeter Wemm if (lport == 0) { 9844616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 985efc76f72SBjoern A. Zeeb if (error != 0) 986efc76f72SBjoern A. Zeeb return (error); 98733b3ac06SPeter Wemm } 9884b932371SIan Dowse *laddrp = laddr.s_addr; 9894b932371SIan Dowse *lportp = lport; 990df8bae1dSRodney W. Grimes return (0); 991df8bae1dSRodney W. Grimes } 992df8bae1dSRodney W. Grimes 993999f1343SGarrett Wollman /* 9945200e00eSIan Dowse * Connect from a socket to a specified address. 9955200e00eSIan Dowse * Both address and port must be specified in argument sin. 9965200e00eSIan Dowse * If don't have a local address for this socket yet, 9975200e00eSIan Dowse * then pick one. 998999f1343SGarrett Wollman */ 999999f1343SGarrett Wollman int 1000a9d22cceSGleb Smirnoff in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred, 1001fdb987beSMark Johnston bool rehash __unused) 1002999f1343SGarrett Wollman { 10035200e00eSIan Dowse u_short lport, fport; 10045200e00eSIan Dowse in_addr_t laddr, faddr; 10055200e00eSIan Dowse int anonport, error; 1006df8bae1dSRodney W. Grimes 10078501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1008fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1009fdb987beSMark Johnston KASSERT(in_nullhost(inp->inp_faddr), 1010fdb987beSMark Johnston ("%s: inp is already connected", __func__)); 101127f74fd0SRobert Watson 10125200e00eSIan Dowse lport = inp->inp_lport; 10135200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 10145200e00eSIan Dowse anonport = (lport == 0); 1015a9d22cceSGleb Smirnoff error = in_pcbconnect_setup(inp, sin, &laddr, &lport, &faddr, &fport, 10169e46ff4dSGleb Smirnoff cred); 10175200e00eSIan Dowse if (error) 10185200e00eSIan Dowse return (error); 10195200e00eSIan Dowse 1020fdb987beSMark Johnston inp->inp_faddr.s_addr = faddr; 1021fdb987beSMark Johnston inp->inp_fport = fport; 1022fdb987beSMark Johnston 10235200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 10245200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 10255200e00eSIan Dowse inp->inp_lport = lport; 10265200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10275200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 1028fdb987beSMark Johnston inp->inp_laddr.s_addr = inp->inp_faddr.s_addr = 1029fdb987beSMark Johnston INADDR_ANY; 1030fdb987beSMark Johnston inp->inp_lport = inp->inp_fport = 0; 10315200e00eSIan Dowse return (EAGAIN); 10325200e00eSIan Dowse } 1033fdb987beSMark Johnston } else { 10345200e00eSIan Dowse inp->inp_lport = lport; 10355200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 1036fdb987beSMark Johnston if ((inp->inp_flags & INP_INHASHLIST) != 0) 1037db0ac6deSCy Schubert in_pcbrehash(inp); 1038fdb987beSMark Johnston else 1039db0ac6deSCy Schubert in_pcbinshash(inp); 1040fe1274eeSMichael Tuexen } 10412cb64cb2SGeorge V. Neville-Neil 10425200e00eSIan Dowse if (anonport) 10435200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 10445200e00eSIan Dowse return (0); 10455200e00eSIan Dowse } 10465200e00eSIan Dowse 10475200e00eSIan Dowse /* 10480895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 10490895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 10500895aec3SBjoern A. Zeeb */ 1051ae190832SSteven Hartland int 10520895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 10530895aec3SBjoern A. Zeeb struct ucred *cred) 10540895aec3SBjoern A. Zeeb { 10550895aec3SBjoern A. Zeeb struct ifaddr *ifa; 10560895aec3SBjoern A. Zeeb struct sockaddr *sa; 10579ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 10589ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 10590895aec3SBjoern A. Zeeb int error; 10600895aec3SBjoern A. Zeeb 1061c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1062413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1063ac1750ddSMark Johnston 1064592bcae8SBjoern A. Zeeb /* 1065592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1066592bcae8SBjoern A. Zeeb * if requested. 1067592bcae8SBjoern A. Zeeb */ 1068ac1750ddSMark Johnston if (!prison_saddrsel_ip4(cred, laddr)) 1069592bcae8SBjoern A. Zeeb return (0); 1070592bcae8SBjoern A. Zeeb 10710895aec3SBjoern A. Zeeb error = 0; 10720895aec3SBjoern A. Zeeb 10739ac7c6cfSAlexander V. Chernikov nh = NULL; 10749ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 10759ac7c6cfSAlexander V. Chernikov sin = &dst; 10760895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 10770895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 10780895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 10790895aec3SBjoern A. Zeeb 10800895aec3SBjoern A. Zeeb /* 10810895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 10820895aec3SBjoern A. Zeeb * else punt. 10830895aec3SBjoern A. Zeeb * 10840895aec3SBjoern A. Zeeb * Find out route to destination. 10850895aec3SBjoern A. Zeeb */ 10860895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 10879ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 10889ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 10890895aec3SBjoern A. Zeeb 10900895aec3SBjoern A. Zeeb /* 10910895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 10920895aec3SBjoern A. Zeeb * the outgoing interface. 10930895aec3SBjoern A. Zeeb * 10940895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 10950895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 10960895aec3SBjoern A. Zeeb * the source address from. 10970895aec3SBjoern A. Zeeb */ 10989ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 10998c0fec80SRobert Watson struct in_ifaddr *ia; 11000895aec3SBjoern A. Zeeb struct ifnet *ifp; 11010895aec3SBjoern A. Zeeb 11024f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 110358a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11044f6c66ccSMatt Macy if (ia == NULL) { 11054f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 110658a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11074f6c66ccSMatt Macy } 11080895aec3SBjoern A. Zeeb if (ia == NULL) { 11090895aec3SBjoern A. Zeeb error = ENETUNREACH; 11100895aec3SBjoern A. Zeeb goto done; 11110895aec3SBjoern A. Zeeb } 11120895aec3SBjoern A. Zeeb 1113ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 11140895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11150895aec3SBjoern A. Zeeb goto done; 11160895aec3SBjoern A. Zeeb } 11170895aec3SBjoern A. Zeeb 11180895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 11190895aec3SBjoern A. Zeeb ia = NULL; 1120d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11210895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11220895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11230895aec3SBjoern A. Zeeb continue; 11240895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1125b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11260895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11270895aec3SBjoern A. Zeeb break; 11280895aec3SBjoern A. Zeeb } 11290895aec3SBjoern A. Zeeb } 11300895aec3SBjoern A. Zeeb if (ia != NULL) { 11310895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11320895aec3SBjoern A. Zeeb goto done; 11330895aec3SBjoern A. Zeeb } 11340895aec3SBjoern A. Zeeb 11350895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1136b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11370895aec3SBjoern A. Zeeb goto done; 11380895aec3SBjoern A. Zeeb } 11390895aec3SBjoern A. Zeeb 11400895aec3SBjoern A. Zeeb /* 11410895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 11420895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 11430895aec3SBjoern A. Zeeb * In case of jails do those three steps: 11440895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 11450895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 11460895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 11470895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 11480895aec3SBjoern A. Zeeb */ 11499ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 11508c0fec80SRobert Watson struct in_ifaddr *ia; 11519317b04eSRobert Watson struct ifnet *ifp; 11520895aec3SBjoern A. Zeeb 11530895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 1154ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 11559ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11560895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11570895aec3SBjoern A. Zeeb goto done; 11580895aec3SBjoern A. Zeeb } 11590895aec3SBjoern A. Zeeb 11600895aec3SBjoern A. Zeeb /* Jailed. */ 11610895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 11629ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1163b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11649ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11650895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11660895aec3SBjoern A. Zeeb goto done; 11670895aec3SBjoern A. Zeeb } 11680895aec3SBjoern A. Zeeb 11690895aec3SBjoern A. Zeeb /* 11700895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 11710895aec3SBjoern A. Zeeb * belonging to this jail. 11720895aec3SBjoern A. Zeeb */ 11738c0fec80SRobert Watson ia = NULL; 11749ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1175d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11760895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11770895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11780895aec3SBjoern A. Zeeb continue; 11790895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1180b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11810895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11820895aec3SBjoern A. Zeeb break; 11830895aec3SBjoern A. Zeeb } 11840895aec3SBjoern A. Zeeb } 11850895aec3SBjoern A. Zeeb if (ia != NULL) { 11860895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11870895aec3SBjoern A. Zeeb goto done; 11880895aec3SBjoern A. Zeeb } 11890895aec3SBjoern A. Zeeb 11900895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1191b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11920895aec3SBjoern A. Zeeb goto done; 11930895aec3SBjoern A. Zeeb } 11940895aec3SBjoern A. Zeeb 11950895aec3SBjoern A. Zeeb /* 11960895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 11970895aec3SBjoern A. Zeeb * to ourselves is here. 11980895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 11990895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 12000895aec3SBjoern A. Zeeb * a loopback interface. 12010895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 12020895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 12030895aec3SBjoern A. Zeeb */ 12049ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 12058c0fec80SRobert Watson struct in_ifaddr *ia; 12060895aec3SBjoern A. Zeeb 12079ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 120858a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12090895aec3SBjoern A. Zeeb if (ia == NULL) 12109ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 121158a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1212f0bb05fcSQing Li if (ia == NULL) 12139ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 12140895aec3SBjoern A. Zeeb 1215ac1750ddSMark Johnston if (!prison_flag(cred, PR_IP4)) { 12160895aec3SBjoern A. Zeeb if (ia == NULL) { 12170895aec3SBjoern A. Zeeb error = ENETUNREACH; 12180895aec3SBjoern A. Zeeb goto done; 12190895aec3SBjoern A. Zeeb } 12200895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12210895aec3SBjoern A. Zeeb goto done; 12220895aec3SBjoern A. Zeeb } 12230895aec3SBjoern A. Zeeb 12240895aec3SBjoern A. Zeeb /* Jailed. */ 12250895aec3SBjoern A. Zeeb if (ia != NULL) { 12260895aec3SBjoern A. Zeeb struct ifnet *ifp; 12270895aec3SBjoern A. Zeeb 12280895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12290895aec3SBjoern A. Zeeb ia = NULL; 1230d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12310895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12320895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12330895aec3SBjoern A. Zeeb continue; 12340895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1235b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1236b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 12370895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12380895aec3SBjoern A. Zeeb break; 12390895aec3SBjoern A. Zeeb } 12400895aec3SBjoern A. Zeeb } 12410895aec3SBjoern A. Zeeb if (ia != NULL) { 12420895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12430895aec3SBjoern A. Zeeb goto done; 12440895aec3SBjoern A. Zeeb } 12450895aec3SBjoern A. Zeeb } 12460895aec3SBjoern A. Zeeb 12470895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1248b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12490895aec3SBjoern A. Zeeb goto done; 12500895aec3SBjoern A. Zeeb } 12510895aec3SBjoern A. Zeeb 12520895aec3SBjoern A. Zeeb done: 1253713264f6SMark Johnston if (error == 0 && laddr->s_addr == INADDR_ANY) 1254713264f6SMark Johnston return (EHOSTUNREACH); 12550895aec3SBjoern A. Zeeb return (error); 12560895aec3SBjoern A. Zeeb } 12570895aec3SBjoern A. Zeeb 12580895aec3SBjoern A. Zeeb /* 12595200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 12605200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 12615200e00eSIan Dowse * address and port for the PCB; these are updated to the values 12625200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 12635200e00eSIan Dowse * the connect. 12645200e00eSIan Dowse * 12655200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 12665200e00eSIan Dowse * and port. These are not updated in the error case. 12675200e00eSIan Dowse */ 12685200e00eSIan Dowse int 1269a9d22cceSGleb Smirnoff in_pcbconnect_setup(struct inpcb *inp, struct sockaddr_in *sin, 1270136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 12719e46ff4dSGleb Smirnoff struct ucred *cred) 12725200e00eSIan Dowse { 12735200e00eSIan Dowse struct in_ifaddr *ia; 1274b89e82ddSJamie Gritton struct in_addr laddr, faddr; 12755200e00eSIan Dowse u_short lport, fport; 12765200e00eSIan Dowse int error; 12775200e00eSIan Dowse 1278f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 1279f161d294SMark Johnston ("%s: invalid address family for %p", __func__, sin)); 1280f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 1281f161d294SMark Johnston ("%s: invalid address length for %p", __func__, sin)); 1282f161d294SMark Johnston 12838501a69cSRobert Watson /* 12848501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 12858501a69cSRobert Watson * lock is sufficient. 12868501a69cSRobert Watson */ 1287c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 128827f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1289fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 129027f74fd0SRobert Watson 1291df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1292df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 12935200e00eSIan Dowse laddr.s_addr = *laddrp; 12945200e00eSIan Dowse lport = *lportp; 12955200e00eSIan Dowse faddr = sin->sin_addr; 12965200e00eSIan Dowse fport = sin->sin_port; 12970c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH 12980c325f53SAlexander V. Chernikov if (CALC_FLOWID_OUTBOUND) { 12990c325f53SAlexander V. Chernikov uint32_t hash_val, hash_type; 13000895aec3SBjoern A. Zeeb 13010c325f53SAlexander V. Chernikov hash_val = fib4_calc_software_hash(laddr, faddr, 0, fport, 13020c325f53SAlexander V. Chernikov inp->inp_socket->so_proto->pr_protocol, &hash_type); 13030c325f53SAlexander V. Chernikov 13040c325f53SAlexander V. Chernikov inp->inp_flowid = hash_val; 13050c325f53SAlexander V. Chernikov inp->inp_flowtype = hash_type; 13060c325f53SAlexander V. Chernikov } 13070c325f53SAlexander V. Chernikov #endif 1308d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1309df8bae1dSRodney W. Grimes /* 1310df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1311df8bae1dSRodney W. Grimes * use the primary local address. 1312df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1313df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1314df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1315df8bae1dSRodney W. Grimes */ 1316413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1317413628a7SBjoern A. Zeeb faddr = 1318d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1319ac1750ddSMark Johnston if ((error = prison_get_ip4(cred, &faddr)) != 0) 1320b89e82ddSJamie Gritton return (error); 13212d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1322d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 13232d9cfabaSRobert Watson IFF_BROADCAST) 1324d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1325603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 13262d9cfabaSRobert Watson } 1327df8bae1dSRodney W. Grimes } 13285200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1329d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1330df8bae1dSRodney W. Grimes /* 1331df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1332d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1333df8bae1dSRodney W. Grimes * address of that interface as our source address. 1334df8bae1dSRodney W. Grimes */ 13355200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1336df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1337df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1338df8bae1dSRodney W. Grimes struct ifnet *ifp; 1339df8bae1dSRodney W. Grimes 1340df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1341df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1342df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1343d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1344ac1750ddSMark Johnston if (ia->ia_ifp == ifp && 1345e691be70SDaniel Eischen prison_check_ip4(cred, 1346ac1750ddSMark Johnston &ia->ia_addr.sin_addr) == 0) 1347df8bae1dSRodney W. Grimes break; 1348e691be70SDaniel Eischen } 1349e691be70SDaniel Eischen if (ia == NULL) 1350d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1351e691be70SDaniel Eischen else { 13525200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1353d79fdd98SDaniel Eischen error = 0; 1354999f1343SGarrett Wollman } 1355d79fdd98SDaniel Eischen } 1356d79fdd98SDaniel Eischen } 135704215ed2SRandall Stewart if (error) 135804215ed2SRandall Stewart return (error); 13590895aec3SBjoern A. Zeeb } 1360a034518aSAndrew Gallatin 136125102351SMike Karels if (lport != 0) { 13629e46ff4dSGleb Smirnoff if (in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 13634130ea61SMark Johnston fport, laddr, lport, 0, M_NODOM) != NULL) 1364df8bae1dSRodney W. Grimes return (EADDRINUSE); 136525102351SMike Karels } else { 136625102351SMike Karels struct sockaddr_in lsin, fsin; 136725102351SMike Karels 136825102351SMike Karels bzero(&lsin, sizeof(lsin)); 136925102351SMike Karels bzero(&fsin, sizeof(fsin)); 137025102351SMike Karels lsin.sin_family = AF_INET; 137125102351SMike Karels lsin.sin_addr = laddr; 137225102351SMike Karels fsin.sin_family = AF_INET; 137325102351SMike Karels fsin.sin_addr = faddr; 137425102351SMike Karels error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin, 137525102351SMike Karels &lport, (struct sockaddr *)& fsin, fport, cred, 137625102351SMike Karels INPLOOKUP_WILDCARD); 13775a903f8dSPierre Beyssac if (error) 13785a903f8dSPierre Beyssac return (error); 13795a903f8dSPierre Beyssac } 13805200e00eSIan Dowse *laddrp = laddr.s_addr; 13815200e00eSIan Dowse *lportp = lport; 13825200e00eSIan Dowse *faddrp = faddr.s_addr; 13835200e00eSIan Dowse *fportp = fport; 1384df8bae1dSRodney W. Grimes return (0); 1385df8bae1dSRodney W. Grimes } 1386df8bae1dSRodney W. Grimes 138726f9a767SRodney W. Grimes void 1388136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1389df8bae1dSRodney W. Grimes { 13906b348152SRobert Watson 13918501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1392fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1393fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 1394fdb987beSMark Johnston ("%s: inp %p was already disconnected", __func__, inp)); 1395df8bae1dSRodney W. Grimes 1396fdb987beSMark Johnston in_pcbremhash_locked(inp); 1397fdb987beSMark Johnston 1398fdb987beSMark Johnston /* See the comment in in_pcbinshash(). */ 1399fdb987beSMark Johnston inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr); 14002589ec0fSMark Johnston inp->inp_laddr.s_addr = INADDR_ANY; 1401df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1402df8bae1dSRodney W. Grimes inp->inp_fport = 0; 1403df8bae1dSRodney W. Grimes } 140483e521ecSBjoern A. Zeeb #endif /* INET */ 1405df8bae1dSRodney W. Grimes 14064c7c478dSRobert Watson /* 1407db0ac6deSCy Schubert * inpcb hash lookups are protected by SMR section. 1408db0ac6deSCy Schubert * 1409db0ac6deSCy Schubert * Once desired pcb has been found, switching from SMR section to a pcb 1410db0ac6deSCy Schubert * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1411db0ac6deSCy Schubert * here because SMR is a critical section. 1412db0ac6deSCy Schubert * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1413db0ac6deSCy Schubert */ 14147b92493aSMark Johnston void 1415db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1416db0ac6deSCy Schubert { 1417db0ac6deSCy Schubert 1418db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1419db0ac6deSCy Schubert rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1420db0ac6deSCy Schubert } 1421db0ac6deSCy Schubert 14227b92493aSMark Johnston void 1423db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1424db0ac6deSCy Schubert { 1425db0ac6deSCy Schubert 1426db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1427db0ac6deSCy Schubert rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1428db0ac6deSCy Schubert } 1429db0ac6deSCy Schubert 14307b92493aSMark Johnston int 1431db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1432db0ac6deSCy Schubert { 1433db0ac6deSCy Schubert 1434db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1435db0ac6deSCy Schubert rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1436db0ac6deSCy Schubert } 1437db0ac6deSCy Schubert 1438db0ac6deSCy Schubert static inline bool 1439f567d55fSGleb Smirnoff _inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock, const int ignflags) 1440db0ac6deSCy Schubert { 1441db0ac6deSCy Schubert 1442db0ac6deSCy Schubert MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1443db0ac6deSCy Schubert SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1444db0ac6deSCy Schubert 1445db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1446f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1447db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1448db0ac6deSCy Schubert inp_unlock(inp, lock); 1449db0ac6deSCy Schubert return (false); 1450db0ac6deSCy Schubert } 1451db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1452db0ac6deSCy Schubert return (true); 1453db0ac6deSCy Schubert } 1454db0ac6deSCy Schubert 1455db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1456db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1457db0ac6deSCy Schubert inp_lock(inp, lock); 1458db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) 1459db0ac6deSCy Schubert return (false); 1460db0ac6deSCy Schubert /* 1461db0ac6deSCy Schubert * inp acquired through refcount & lock for sure didn't went 1462db0ac6deSCy Schubert * through uma_zfree(). However, it may have already went 1463db0ac6deSCy Schubert * through in_pcbfree() and has another reference, that 1464db0ac6deSCy Schubert * prevented its release by our in_pcbrele(). 1465db0ac6deSCy Schubert */ 1466f567d55fSGleb Smirnoff if (__predict_false(inp->inp_flags & ignflags)) { 1467db0ac6deSCy Schubert inp_unlock(inp, lock); 1468db0ac6deSCy Schubert return (false); 1469db0ac6deSCy Schubert } 1470db0ac6deSCy Schubert return (true); 1471db0ac6deSCy Schubert } else { 1472db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1473db0ac6deSCy Schubert return (false); 1474db0ac6deSCy Schubert } 1475db0ac6deSCy Schubert } 1476db0ac6deSCy Schubert 1477f567d55fSGleb Smirnoff bool 1478f567d55fSGleb Smirnoff inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1479f567d55fSGleb Smirnoff { 1480f567d55fSGleb Smirnoff 1481f567d55fSGleb Smirnoff /* 1482f567d55fSGleb Smirnoff * in_pcblookup() family of functions ignore not only freed entries, 1483f567d55fSGleb Smirnoff * that may be found due to lockless access to the hash, but dropped 1484f567d55fSGleb Smirnoff * entries, too. 1485f567d55fSGleb Smirnoff */ 1486f567d55fSGleb Smirnoff return (_inp_smr_lock(inp, lock, INP_FREED | INP_DROPPED)); 1487f567d55fSGleb Smirnoff } 1488f567d55fSGleb Smirnoff 1489db0ac6deSCy Schubert /* 1490db0ac6deSCy Schubert * inp_next() - inpcb hash/list traversal iterator 1491db0ac6deSCy Schubert * 1492db0ac6deSCy Schubert * Requires initialized struct inpcb_iterator for context. 1493db0ac6deSCy Schubert * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1494db0ac6deSCy Schubert * 1495db0ac6deSCy Schubert * - Iterator can have either write-lock or read-lock semantics, that can not 1496db0ac6deSCy Schubert * be changed later. 1497db0ac6deSCy Schubert * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1498db0ac6deSCy Schubert * a single hash slot. Note: only rip_input() does the latter. 1499db0ac6deSCy Schubert * - Iterator may have optional bool matching function. The matching function 1500db0ac6deSCy Schubert * will be executed for each inpcb in the SMR context, so it can not acquire 1501db0ac6deSCy Schubert * locks and can safely access only immutable fields of inpcb. 1502db0ac6deSCy Schubert * 1503db0ac6deSCy Schubert * A fresh initialized iterator has NULL inpcb in its context and that 1504db0ac6deSCy Schubert * means that inp_next() call would return the very first inpcb on the list 1505db0ac6deSCy Schubert * locked with desired semantic. In all following calls the context pointer 1506db0ac6deSCy Schubert * shall hold the current inpcb pointer. The KPI user is not supposed to 1507db0ac6deSCy Schubert * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1508db0ac6deSCy Schubert * and write NULL to its context. After end of traversal an iterator can be 1509db0ac6deSCy Schubert * reused. 1510db0ac6deSCy Schubert * 1511db0ac6deSCy Schubert * List traversals have the following features/constraints: 1512db0ac6deSCy Schubert * - New entries won't be seen, as they are always added to the head of a list. 1513db0ac6deSCy Schubert * - Removed entries won't stop traversal as long as they are not added to 1514db0ac6deSCy Schubert * a different list. This is violated by in_pcbrehash(). 1515db0ac6deSCy Schubert */ 1516db0ac6deSCy Schubert #define II_LIST_FIRST(ipi, hash) \ 1517db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1518db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1519fdb987beSMark Johnston CK_LIST_FIRST(&(ipi)->ipi_hash_exact[(hash)])) 1520db0ac6deSCy Schubert #define II_LIST_NEXT(inp, hash) \ 1521db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1522db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_list) : \ 1523fdb987beSMark Johnston CK_LIST_NEXT((inp), inp_hash_exact)) 1524db0ac6deSCy Schubert #define II_LOCK_ASSERT(inp, lock) \ 1525db0ac6deSCy Schubert rw_assert(&(inp)->inp_lock, \ 1526db0ac6deSCy Schubert (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1527db0ac6deSCy Schubert struct inpcb * 1528db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii) 1529db0ac6deSCy Schubert { 1530db0ac6deSCy Schubert const struct inpcbinfo *ipi = ii->ipi; 1531db0ac6deSCy Schubert inp_match_t *match = ii->match; 1532db0ac6deSCy Schubert void *ctx = ii->ctx; 1533db0ac6deSCy Schubert inp_lookup_t lock = ii->lock; 1534db0ac6deSCy Schubert int hash = ii->hash; 1535db0ac6deSCy Schubert struct inpcb *inp; 1536db0ac6deSCy Schubert 1537db0ac6deSCy Schubert if (ii->inp == NULL) { /* First call. */ 1538db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1539db0ac6deSCy Schubert /* This is unrolled CK_LIST_FOREACH(). */ 1540db0ac6deSCy Schubert for (inp = II_LIST_FIRST(ipi, hash); 1541db0ac6deSCy Schubert inp != NULL; 1542db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash)) { 1543db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1544db0ac6deSCy Schubert continue; 1545f567d55fSGleb Smirnoff if (__predict_true(_inp_smr_lock(inp, lock, INP_FREED))) 1546db0ac6deSCy Schubert break; 1547db0ac6deSCy Schubert else { 1548db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1549db0ac6deSCy Schubert MPASS(inp != II_LIST_FIRST(ipi, hash)); 1550db0ac6deSCy Schubert inp = II_LIST_FIRST(ipi, hash); 1551430df2abSMichael Tuexen if (inp == NULL) 1552430df2abSMichael Tuexen break; 1553db0ac6deSCy Schubert } 1554db0ac6deSCy Schubert } 1555db0ac6deSCy Schubert 1556db0ac6deSCy Schubert if (inp == NULL) 1557db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1558db0ac6deSCy Schubert else 1559db0ac6deSCy Schubert ii->inp = inp; 1560db0ac6deSCy Schubert 1561db0ac6deSCy Schubert return (inp); 1562db0ac6deSCy Schubert } 1563db0ac6deSCy Schubert 1564db0ac6deSCy Schubert /* Not a first call. */ 1565db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1566db0ac6deSCy Schubert restart: 1567db0ac6deSCy Schubert inp = ii->inp; 1568db0ac6deSCy Schubert II_LOCK_ASSERT(inp, lock); 1569db0ac6deSCy Schubert next: 1570db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash); 1571db0ac6deSCy Schubert if (inp == NULL) { 1572db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1573db0ac6deSCy Schubert goto found; 1574db0ac6deSCy Schubert } 1575db0ac6deSCy Schubert 1576db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1577db0ac6deSCy Schubert goto next; 1578db0ac6deSCy Schubert 1579db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1580db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1581db0ac6deSCy Schubert /* 1582db0ac6deSCy Schubert * Entries are never inserted in middle of a list, thus 1583db0ac6deSCy Schubert * as long as we are in SMR, we can continue traversal. 1584db0ac6deSCy Schubert * Jump to 'restart' should yield in the same result, 1585db0ac6deSCy Schubert * but could produce unnecessary looping. Could this 1586db0ac6deSCy Schubert * looping be unbound? 1587db0ac6deSCy Schubert */ 1588db0ac6deSCy Schubert inp_unlock(inp, lock); 1589db0ac6deSCy Schubert goto next; 1590db0ac6deSCy Schubert } else { 1591db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1592db0ac6deSCy Schubert goto found; 1593db0ac6deSCy Schubert } 1594db0ac6deSCy Schubert } 1595db0ac6deSCy Schubert 1596db0ac6deSCy Schubert /* 1597db0ac6deSCy Schubert * Can't obtain lock immediately, thus going hard. Once we exit the 1598db0ac6deSCy Schubert * SMR section we can no longer jump to 'next', and our only stable 1599db0ac6deSCy Schubert * anchoring point is ii->inp, which we keep locked for this case, so 1600db0ac6deSCy Schubert * we jump to 'restart'. 1601db0ac6deSCy Schubert */ 1602db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1603db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1604db0ac6deSCy Schubert inp_lock(inp, lock); 1605db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) { 1606db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1607db0ac6deSCy Schubert goto restart; 1608db0ac6deSCy Schubert } 1609db0ac6deSCy Schubert /* 1610db0ac6deSCy Schubert * See comment in inp_smr_lock(). 1611db0ac6deSCy Schubert */ 1612db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1613db0ac6deSCy Schubert inp_unlock(inp, lock); 1614db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1615db0ac6deSCy Schubert goto restart; 1616db0ac6deSCy Schubert } 1617db0ac6deSCy Schubert } else 1618db0ac6deSCy Schubert goto next; 1619db0ac6deSCy Schubert 1620db0ac6deSCy Schubert found: 1621db0ac6deSCy Schubert inp_unlock(ii->inp, lock); 1622db0ac6deSCy Schubert ii->inp = inp; 1623db0ac6deSCy Schubert 1624db0ac6deSCy Schubert return (ii->inp); 1625db0ac6deSCy Schubert } 1626db0ac6deSCy Schubert 1627db0ac6deSCy Schubert /* 162879bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 1629db0ac6deSCy Schubert * stability of an inpcb pointer despite the inpcb lock being released or 1630db0ac6deSCy Schubert * SMR section exited. 163179bdc6e5SRobert Watson * 1632db0ac6deSCy Schubert * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1633c0a211c5SRobert Watson */ 163479bdc6e5SRobert Watson void 163579bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 16364c7c478dSRobert Watson { 1637db0ac6deSCy Schubert u_int old __diagused; 1638df8bae1dSRodney W. Grimes 1639db0ac6deSCy Schubert old = refcount_acquire(&inp->inp_refcount); 1640db0ac6deSCy Schubert KASSERT(old > 0, ("%s: refcount 0", __func__)); 164179bdc6e5SRobert Watson } 164279bdc6e5SRobert Watson 164379bdc6e5SRobert Watson /* 1644db0ac6deSCy Schubert * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1645db0ac6deSCy Schubert * freeing the pcb, if the reference was very last. 164679bdc6e5SRobert Watson */ 1647db0ac6deSCy Schubert bool 164879bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 164979bdc6e5SRobert Watson { 165079bdc6e5SRobert Watson 165179bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 165279bdc6e5SRobert Watson 1653c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1654db0ac6deSCy Schubert return (false); 1655db0ac6deSCy Schubert 1656db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1657db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 16585fd1a67eSMark Johnston crfree(inp->inp_cred); 16595fd1a67eSMark Johnston #ifdef INVARIANTS 16605fd1a67eSMark Johnston inp->inp_cred = NULL; 16615fd1a67eSMark Johnston #endif 1662df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1663db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1664db0ac6deSCy Schubert return (true); 1665df4e91d3SGleb Smirnoff } 166679bdc6e5SRobert Watson 1667db0ac6deSCy Schubert bool 166879bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 166979bdc6e5SRobert Watson { 167079bdc6e5SRobert Watson 167179bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 167279bdc6e5SRobert Watson 1673c7ea65ecSMark Johnston if (!refcount_release(&inp->inp_refcount)) 1674db0ac6deSCy Schubert return (false); 1675db0ac6deSCy Schubert 1676db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1677db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 16785fd1a67eSMark Johnston crfree(inp->inp_cred); 16795fd1a67eSMark Johnston #ifdef INVARIANTS 16805fd1a67eSMark Johnston inp->inp_cred = NULL; 16815fd1a67eSMark Johnston #endif 1682edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1683db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1684db0ac6deSCy Schubert return (true); 1685addf2b20SMatt Macy } 1686addf2b20SMatt Macy 16877b92493aSMark Johnston bool 16887b92493aSMark Johnston in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 16897b92493aSMark Johnston { 16907b92493aSMark Johnston 16917b92493aSMark Johnston return (lock == INPLOOKUP_RLOCKPCB ? 16927b92493aSMark Johnston in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 16937b92493aSMark Johnston } 16947b92493aSMark Johnston 169579bdc6e5SRobert Watson /* 169679bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 169779bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 169879bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 169979bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 1700db0ac6deSCy Schubert * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1701db0ac6deSCy Schubert * Almost all work, including removal from global lists, is done in this 1702db0ac6deSCy Schubert * context, where the pcbinfo lock is held. 170379bdc6e5SRobert Watson */ 170479bdc6e5SRobert Watson void 170579bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 170679bdc6e5SRobert Watson { 1707f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1708db0ac6deSCy Schubert #ifdef INET 1709db0ac6deSCy Schubert struct ip_moptions *imo; 1710db0ac6deSCy Schubert #endif 1711db0ac6deSCy Schubert #ifdef INET6 1712db0ac6deSCy Schubert struct ip6_moptions *im6o; 1713db0ac6deSCy Schubert #endif 171445a48d07SJonathan T. Looney 171579bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1716a13039e2SGleb Smirnoff KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1717db0ac6deSCy Schubert KASSERT((inp->inp_flags & INP_FREED) == 0, 1718db0ac6deSCy Schubert ("%s: called twice for pcb %p", __func__, inp)); 1719db0ac6deSCy Schubert 1720a13039e2SGleb Smirnoff /* 1721a13039e2SGleb Smirnoff * in_pcblookup_local() and in6_pcblookup_local() may return an inpcb 1722a13039e2SGleb Smirnoff * from the hash without acquiring inpcb lock, they rely on the hash 1723a13039e2SGleb Smirnoff * lock, thus in_pcbremhash() should be the first action. 1724a13039e2SGleb Smirnoff */ 1725a13039e2SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 1726a13039e2SGleb Smirnoff in_pcbremhash(inp); 1727db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 1728db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1729db0ac6deSCy Schubert pcbinfo->ipi_count--; 1730db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_list); 1731db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 1732db0ac6deSCy Schubert 1733a13039e2SGleb Smirnoff #ifdef RATELIMIT 1734a13039e2SGleb Smirnoff if (inp->inp_snd_tag != NULL) 1735a13039e2SGleb Smirnoff in_pcbdetach_txrtlmt(inp); 1736a13039e2SGleb Smirnoff #endif 1737a13039e2SGleb Smirnoff inp->inp_flags |= INP_FREED; 1738a13039e2SGleb Smirnoff inp->inp_socket->so_pcb = NULL; 1739a13039e2SGleb Smirnoff inp->inp_socket = NULL; 1740db0ac6deSCy Schubert 1741fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1742db0ac6deSCy Schubert #ifdef MAC 1743db0ac6deSCy Schubert mac_inpcb_destroy(inp); 1744db0ac6deSCy Schubert #endif 1745db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1746db0ac6deSCy Schubert if (inp->inp_sp != NULL) 1747db0ac6deSCy Schubert ipsec_delete_pcbpolicy(inp); 1748db0ac6deSCy Schubert #endif 1749db0ac6deSCy Schubert #ifdef INET 1750db0ac6deSCy Schubert if (inp->inp_options) 1751db0ac6deSCy Schubert (void)m_free(inp->inp_options); 1752*4a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->inp_options); 1753db0ac6deSCy Schubert imo = inp->inp_moptions; 1754*4a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->inp_moptions); 1755db0ac6deSCy Schubert #endif 1756db0ac6deSCy Schubert #ifdef INET6 1757db0ac6deSCy Schubert if (inp->inp_vflag & INP_IPV6PROTO) { 1758db0ac6deSCy Schubert ip6_freepcbopts(inp->in6p_outputopts); 1759*4a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->in6p_outputopts); 1760db0ac6deSCy Schubert im6o = inp->in6p_moptions; 1761*4a0c6403SGleb Smirnoff DEBUG_POISON_POINTER(inp->in6p_moptions); 1762db0ac6deSCy Schubert } else 1763db0ac6deSCy Schubert im6o = NULL; 1764db0ac6deSCy Schubert #endif 1765db0ac6deSCy Schubert 1766db0ac6deSCy Schubert if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1767cb6bb230SMatt Macy INP_WUNLOCK(inp); 1768db0ac6deSCy Schubert } 1769db0ac6deSCy Schubert #ifdef INET6 1770db0ac6deSCy Schubert ip6_freemoptions(im6o); 1771db0ac6deSCy Schubert #endif 1772db0ac6deSCy Schubert #ifdef INET 1773db0ac6deSCy Schubert inp_freemoptions(imo); 1774db0ac6deSCy Schubert #endif 1775eb93b99dSGleb Smirnoff } 1776eb93b99dSGleb Smirnoff 1777eb93b99dSGleb Smirnoff /* 1778eb93b99dSGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 1779eb93b99dSGleb Smirnoff * different name to the lock. But they all are disposed the same. 1780eb93b99dSGleb Smirnoff */ 1781eb93b99dSGleb Smirnoff static void 1782eb93b99dSGleb Smirnoff inpcb_fini(void *mem, int size) 1783eb93b99dSGleb Smirnoff { 1784eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1785eb93b99dSGleb Smirnoff 1786eb93b99dSGleb Smirnoff INP_LOCK_DESTROY(inp); 178728696211SRobert Watson } 178828696211SRobert Watson 178928696211SRobert Watson /* 1790c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1791c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1792c0a211c5SRobert Watson * 1793c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1794c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1795c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1796c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1797c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1798c0a211c5SRobert Watson * in_pcbdetach(). 1799c0a211c5SRobert Watson * 1800c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1801bbbd7aabSGleb Smirnoff * in_pcbpurgeif0()? 180210702a28SRobert Watson */ 180310702a28SRobert Watson void 180410702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 180510702a28SRobert Watson { 180610702a28SRobert Watson 18078501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 18086573d758SMatt Macy #ifdef INVARIANTS 18096573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 18106573d758SMatt Macy MPASS(inp->inp_refcount > 1); 18116573d758SMatt Macy #endif 181210702a28SRobert Watson 1813ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 18143ba34b07SGleb Smirnoff if (inp->inp_flags & INP_INHASHLIST) 18153ba34b07SGleb Smirnoff in_pcbremhash(inp); 181610702a28SRobert Watson } 181710702a28SRobert Watson 181867107f45SBjoern A. Zeeb #ifdef INET 181954d642bbSRobert Watson /* 182054d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 182154d642bbSRobert Watson */ 1822117bcae7SGarrett Wollman int 18230fac350cSGleb Smirnoff in_getsockaddr(struct socket *so, struct sockaddr *sa) 1824df8bae1dSRodney W. Grimes { 1825136d4f1cSRobert Watson struct inpcb *inp; 182642fa505bSDavid Greenman 1827fdc984f7STor Egge inp = sotoinpcb(so); 182854d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 18296466b28aSRobert Watson 18300fac350cSGleb Smirnoff *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 18310fac350cSGleb Smirnoff .sin_len = sizeof(struct sockaddr_in), 18320fac350cSGleb Smirnoff .sin_family = AF_INET, 18330fac350cSGleb Smirnoff .sin_port = inp->inp_lport, 18340fac350cSGleb Smirnoff .sin_addr = inp->inp_laddr, 18350fac350cSGleb Smirnoff }; 183642fa505bSDavid Greenman 18370fac350cSGleb Smirnoff return (0); 1838df8bae1dSRodney W. Grimes } 1839df8bae1dSRodney W. Grimes 1840117bcae7SGarrett Wollman int 18410fac350cSGleb Smirnoff in_getpeeraddr(struct socket *so, struct sockaddr *sa) 1842df8bae1dSRodney W. Grimes { 1843136d4f1cSRobert Watson struct inpcb *inp; 184442fa505bSDavid Greenman 1845fdc984f7STor Egge inp = sotoinpcb(so); 184654d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 18476466b28aSRobert Watson 18480fac350cSGleb Smirnoff *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 18490fac350cSGleb Smirnoff .sin_len = sizeof(struct sockaddr_in), 18500fac350cSGleb Smirnoff .sin_family = AF_INET, 18510fac350cSGleb Smirnoff .sin_port = inp->inp_fport, 18520fac350cSGleb Smirnoff .sin_addr = inp->inp_faddr, 18530fac350cSGleb Smirnoff }; 185442fa505bSDavid Greenman 18550fac350cSGleb Smirnoff return (0); 1856df8bae1dSRodney W. Grimes } 1857df8bae1dSRodney W. Grimes 1858db0ac6deSCy Schubert static bool 1859db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 1860db0ac6deSCy Schubert { 1861db0ac6deSCy Schubert 1862db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 1863db0ac6deSCy Schubert return (true); 1864db0ac6deSCy Schubert else 1865db0ac6deSCy Schubert return (false); 1866db0ac6deSCy Schubert } 1867db0ac6deSCy Schubert 1868e43cc4aeSHajimu UMEMOTO void 1869136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1870e43cc4aeSHajimu UMEMOTO { 1871db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 1872db0ac6deSCy Schubert inp_v4_multi_match, NULL); 1873e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 187459854ecfSHans Petter Selasky struct in_multi *inm; 187559854ecfSHans Petter Selasky struct in_mfilter *imf; 1876e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1877e43cc4aeSHajimu UMEMOTO 1878db0ac6deSCy Schubert IN_MULTI_LOCK_ASSERT(); 1879db0ac6deSCy Schubert 1880db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 1881db0ac6deSCy Schubert INP_WLOCK_ASSERT(inp); 1882db0ac6deSCy Schubert 1883e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1884e43cc4aeSHajimu UMEMOTO /* 1885e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1886e43cc4aeSHajimu UMEMOTO * detached. 1887e43cc4aeSHajimu UMEMOTO */ 1888e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1889e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1890e43cc4aeSHajimu UMEMOTO 1891e43cc4aeSHajimu UMEMOTO /* 1892e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1893e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1894cb6bb230SMatt Macy * 1895cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1896e43cc4aeSHajimu UMEMOTO */ 189759854ecfSHans Petter Selasky restart: 189859854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 189959854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 190059854ecfSHans Petter Selasky continue; 190159854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 190259854ecfSHans Petter Selasky continue; 190359854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 190459854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 190559854ecfSHans Petter Selasky ip_mfilter_free(imf); 190659854ecfSHans Petter Selasky goto restart; 1907e43cc4aeSHajimu UMEMOTO } 1908e43cc4aeSHajimu UMEMOTO } 1909e43cc4aeSHajimu UMEMOTO } 1910e43cc4aeSHajimu UMEMOTO 1911df8bae1dSRodney W. Grimes /* 1912fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1913fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1914c3229e05SDavid Greenman */ 1915d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1916df8bae1dSRodney W. Grimes struct inpcb * 1917136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 191868e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1919df8bae1dSRodney W. Grimes { 1920136d4f1cSRobert Watson struct inpcb *inp; 1921d5e8a67eSHajimu UMEMOTO #ifdef INET6 1922d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1923d5e8a67eSHajimu UMEMOTO #else 1924d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1925d5e8a67eSHajimu UMEMOTO #endif 1926d5e8a67eSHajimu UMEMOTO int wildcard; 19277bc4aca7SDavid Greenman 192868e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 192968e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 1930fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 19311b73ca0bSSam Leffler 193268e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1933c3229e05SDavid Greenman struct inpcbhead *head; 1934c3229e05SDavid Greenman /* 1935c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1936c3229e05SDavid Greenman * matches the local address and port we're looking for. 1937c3229e05SDavid Greenman */ 1938fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1939a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 1940fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1941cfa1ca9dSYoshinobu Inoue #ifdef INET6 1942413628a7SBjoern A. Zeeb /* XXX inp locking */ 1943369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1944cfa1ca9dSYoshinobu Inoue continue; 1945cfa1ca9dSYoshinobu Inoue #endif 1946c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1947c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1948c3229e05SDavid Greenman inp->inp_lport == lport) { 1949c3229e05SDavid Greenman /* 1950413628a7SBjoern A. Zeeb * Found? 1951c3229e05SDavid Greenman */ 1952ac1750ddSMark Johnston if (prison_equal_ip4(cred->cr_prison, 19530304c731SJamie Gritton inp->inp_cred->cr_prison)) 1954c3229e05SDavid Greenman return (inp); 1955df8bae1dSRodney W. Grimes } 1956c3229e05SDavid Greenman } 1957c3229e05SDavid Greenman /* 1958c3229e05SDavid Greenman * Not found. 1959c3229e05SDavid Greenman */ 1960c3229e05SDavid Greenman return (NULL); 1961c3229e05SDavid Greenman } else { 1962c3229e05SDavid Greenman struct inpcbporthead *porthash; 1963c3229e05SDavid Greenman struct inpcbport *phd; 1964c3229e05SDavid Greenman struct inpcb *match = NULL; 1965c3229e05SDavid Greenman /* 1966c3229e05SDavid Greenman * Best fit PCB lookup. 1967c3229e05SDavid Greenman * 1968c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1969c3229e05SDavid Greenman * port hash list. 1970c3229e05SDavid Greenman */ 1971712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1972712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1973b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 1974c3229e05SDavid Greenman if (phd->phd_port == lport) 1975c3229e05SDavid Greenman break; 1976c3229e05SDavid Greenman } 1977c3229e05SDavid Greenman if (phd != NULL) { 1978c3229e05SDavid Greenman /* 1979c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1980c3229e05SDavid Greenman * fit. 1981c3229e05SDavid Greenman */ 1982b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1983c3229e05SDavid Greenman wildcard = 0; 1984ac1750ddSMark Johnston if (!prison_equal_ip4(inp->inp_cred->cr_prison, 19850304c731SJamie Gritton cred->cr_prison)) 1986413628a7SBjoern A. Zeeb continue; 1987cfa1ca9dSYoshinobu Inoue #ifdef INET6 1988413628a7SBjoern A. Zeeb /* XXX inp locking */ 1989369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1990cfa1ca9dSYoshinobu Inoue continue; 1991d5e8a67eSHajimu UMEMOTO /* 1992d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1993d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1994d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1995d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1996d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1997d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1998d5e8a67eSHajimu UMEMOTO * 1999d5e8a67eSHajimu UMEMOTO * Note that the case only happens 2000d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 2001d5e8a67eSHajimu UMEMOTO * the condition that the use of the 2002d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 2003d5e8a67eSHajimu UMEMOTO */ 2004d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 2005d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2006cfa1ca9dSYoshinobu Inoue #endif 2007c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 2008c3229e05SDavid Greenman wildcard++; 200915bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 201015bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 201115bd2b43SDavid Greenman wildcard++; 201215bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 201315bd2b43SDavid Greenman continue; 201415bd2b43SDavid Greenman } else { 201515bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 201615bd2b43SDavid Greenman wildcard++; 201715bd2b43SDavid Greenman } 2018df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 2019df8bae1dSRodney W. Grimes match = inp; 2020df8bae1dSRodney W. Grimes matchwild = wildcard; 2021413628a7SBjoern A. Zeeb if (matchwild == 0) 2022df8bae1dSRodney W. Grimes break; 2023df8bae1dSRodney W. Grimes } 2024df8bae1dSRodney W. Grimes } 20253dbdc25cSDavid Greenman } 2026df8bae1dSRodney W. Grimes return (match); 2027df8bae1dSRodney W. Grimes } 2028c3229e05SDavid Greenman } 2029d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 203015bd2b43SDavid Greenman 2031d93ec8cbSMark Johnston static bool 2032d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain) 2033d93ec8cbSMark Johnston { 2034d93ec8cbSMark Johnston return (domain == M_NODOM || domain == grp->il_numa_domain); 2035d93ec8cbSMark Johnston } 2036d93ec8cbSMark Johnston 20371a43cff9SSean Bruno static struct inpcb * 20381a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 20394130ea61SMark Johnston const struct in_addr *faddr, uint16_t fport, const struct in_addr *laddr, 20404130ea61SMark Johnston uint16_t lport, int domain) 20411a43cff9SSean Bruno { 20421a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 20431a43cff9SSean Bruno struct inpcblbgroup *grp; 2044d93ec8cbSMark Johnston struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 20451a43cff9SSean Bruno 20461a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 20471a43cff9SSean Bruno 20489d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 20499d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 20501a43cff9SSean Bruno 20511a43cff9SSean Bruno /* 2052d93ec8cbSMark Johnston * Search for an LB group match based on the following criteria: 2053d93ec8cbSMark Johnston * - prefer jailed groups to non-jailed groups 2054d93ec8cbSMark Johnston * - prefer exact source address matches to wildcard matches 2055d93ec8cbSMark Johnston * - prefer groups bound to the specified NUMA domain 20561a43cff9SSean Bruno */ 2057d93ec8cbSMark Johnston jail_exact = jail_wild = local_exact = local_wild = NULL; 205854af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 2059d93ec8cbSMark Johnston bool injail; 2060d93ec8cbSMark Johnston 20611a43cff9SSean Bruno #ifdef INET6 20621a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 20631a43cff9SSean Bruno continue; 20641a43cff9SSean Bruno #endif 20658be02ee4SMark Johnston if (grp->il_lport != lport) 20668be02ee4SMark Johnston continue; 20671a43cff9SSean Bruno 2068d93ec8cbSMark Johnston injail = prison_flag(grp->il_cred, PR_IP4) != 0; 2069d93ec8cbSMark Johnston if (injail && prison_check_ip4_locked(grp->il_cred->cr_prison, 2070d93ec8cbSMark Johnston laddr) != 0) 2071d93ec8cbSMark Johnston continue; 2072a034518aSAndrew Gallatin 2073d93ec8cbSMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) { 2074d93ec8cbSMark Johnston if (injail) { 2075d93ec8cbSMark Johnston jail_exact = grp; 2076d93ec8cbSMark Johnston if (in_pcblookup_lb_numa_match(grp, domain)) 2077d93ec8cbSMark Johnston /* This is a perfect match. */ 2078d93ec8cbSMark Johnston goto out; 2079d93ec8cbSMark Johnston } else if (local_exact == NULL || 2080d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) { 2081d93ec8cbSMark Johnston local_exact = grp; 2082d93ec8cbSMark Johnston } 20834130ea61SMark Johnston } else if (grp->il_laddr.s_addr == INADDR_ANY) { 2084d93ec8cbSMark Johnston if (injail) { 2085d93ec8cbSMark Johnston if (jail_wild == NULL || 2086d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) 2087d93ec8cbSMark Johnston jail_wild = grp; 2088d93ec8cbSMark Johnston } else if (local_wild == NULL || 2089d93ec8cbSMark Johnston in_pcblookup_lb_numa_match(grp, domain)) { 2090d93ec8cbSMark Johnston local_wild = grp; 2091d93ec8cbSMark Johnston } 2092d93ec8cbSMark Johnston } 2093d93ec8cbSMark Johnston } 2094d93ec8cbSMark Johnston 2095d93ec8cbSMark Johnston if (jail_exact != NULL) 2096d93ec8cbSMark Johnston grp = jail_exact; 2097d93ec8cbSMark Johnston else if (jail_wild != NULL) 2098d93ec8cbSMark Johnston grp = jail_wild; 2099d93ec8cbSMark Johnston else if (local_exact != NULL) 2100d93ec8cbSMark Johnston grp = local_exact; 2101d93ec8cbSMark Johnston else 2102d93ec8cbSMark Johnston grp = local_wild; 2103d93ec8cbSMark Johnston if (grp == NULL) 2104d93ec8cbSMark Johnston return (NULL); 2105d93ec8cbSMark Johnston out: 2106d93ec8cbSMark Johnston return (grp->il_inp[INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) % 2107d93ec8cbSMark Johnston grp->il_inpcnt]); 21081a43cff9SSean Bruno } 21091a43cff9SSean Bruno 21103e98dcb3SMark Johnston static bool 21113e98dcb3SMark Johnston in_pcblookup_exact_match(const struct inpcb *inp, struct in_addr faddr, 21124130ea61SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 211315bd2b43SDavid Greenman { 2114cfa1ca9dSYoshinobu Inoue #ifdef INET6 2115413628a7SBjoern A. Zeeb /* XXX inp locking */ 2116369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 21173e98dcb3SMark Johnston return (false); 2118cfa1ca9dSYoshinobu Inoue #endif 21196d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2120ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2121ca98b82cSDavid Greenman inp->inp_fport == fport && 2122220d8921SGleb Smirnoff inp->inp_lport == lport) 21233e98dcb3SMark Johnston return (true); 21243e98dcb3SMark Johnston return (false); 21253e98dcb3SMark Johnston } 21263e98dcb3SMark Johnston 21273e98dcb3SMark Johnston static struct inpcb * 21283e98dcb3SMark Johnston in_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in_addr faddr, 21293e98dcb3SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 21303e98dcb3SMark Johnston { 21313e98dcb3SMark Johnston struct inpcbhead *head; 21323e98dcb3SMark Johnston struct inpcb *inp; 21333e98dcb3SMark Johnston 21343e98dcb3SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 21353e98dcb3SMark Johnston 21363e98dcb3SMark Johnston head = &pcbinfo->ipi_hash_exact[INP_PCBHASH(&faddr, lport, fport, 21373e98dcb3SMark Johnston pcbinfo->ipi_hashmask)]; 21383e98dcb3SMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_exact) { 21393e98dcb3SMark Johnston if (in_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) 2140c3229e05SDavid Greenman return (inp); 2141c3229e05SDavid Greenman } 21423e98dcb3SMark Johnston return (NULL); 21433e98dcb3SMark Johnston } 21443e98dcb3SMark Johnston 21453e98dcb3SMark Johnston typedef enum { 21463e98dcb3SMark Johnston INPLOOKUP_MATCH_NONE = 0, 21473e98dcb3SMark Johnston INPLOOKUP_MATCH_WILD = 1, 21483e98dcb3SMark Johnston INPLOOKUP_MATCH_LADDR = 2, 21493e98dcb3SMark Johnston } inp_lookup_match_t; 21503e98dcb3SMark Johnston 21513e98dcb3SMark Johnston static inp_lookup_match_t 21523e98dcb3SMark Johnston in_pcblookup_wild_match(const struct inpcb *inp, struct in_addr laddr, 21533e98dcb3SMark Johnston u_short lport) 21543e98dcb3SMark Johnston { 21553e98dcb3SMark Johnston #ifdef INET6 21563e98dcb3SMark Johnston /* XXX inp locking */ 21573e98dcb3SMark Johnston if ((inp->inp_vflag & INP_IPV4) == 0) 21583e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 21593e98dcb3SMark Johnston #endif 21603e98dcb3SMark Johnston if (inp->inp_faddr.s_addr != INADDR_ANY || inp->inp_lport != lport) 21613e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 21623e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == INADDR_ANY) 21633e98dcb3SMark Johnston return (INPLOOKUP_MATCH_WILD); 21643e98dcb3SMark Johnston if (inp->inp_laddr.s_addr == laddr.s_addr) 21653e98dcb3SMark Johnston return (INPLOOKUP_MATCH_LADDR); 21663e98dcb3SMark Johnston return (INPLOOKUP_MATCH_NONE); 21674130ea61SMark Johnston } 2168e3fd5ffdSRobert Watson 21697b92493aSMark Johnston #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1) 21707b92493aSMark Johnston 21717b92493aSMark Johnston static struct inpcb * 21727b92493aSMark Johnston in_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr, 21737b92493aSMark Johnston u_short fport, struct in_addr laddr, u_short lport, 21747b92493aSMark Johnston const inp_lookup_t lockflags) 21757b92493aSMark Johnston { 21767b92493aSMark Johnston struct inpcbhead *head; 21777b92493aSMark Johnston struct inpcb *inp; 21787b92493aSMark Johnston 21797b92493aSMark Johnston KASSERT(SMR_ENTERED(pcbinfo->ipi_smr), 21807b92493aSMark Johnston ("%s: not in SMR read section", __func__)); 21817b92493aSMark Johnston 21827b92493aSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 21837b92493aSMark Johnston pcbinfo->ipi_hashmask)]; 21847b92493aSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 21857b92493aSMark Johnston inp_lookup_match_t match; 21867b92493aSMark Johnston 21877b92493aSMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport); 21887b92493aSMark Johnston if (match == INPLOOKUP_MATCH_NONE) 21897b92493aSMark Johnston continue; 21907b92493aSMark Johnston 21917b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 2192a306ed50SMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport); 2193a306ed50SMark Johnston if (match != INPLOOKUP_MATCH_NONE && 2194a306ed50SMark Johnston prison_check_ip4_locked(inp->inp_cred->cr_prison, 2195a306ed50SMark Johnston &laddr) == 0) 21967b92493aSMark Johnston return (inp); 21977b92493aSMark Johnston inp_unlock(inp, lockflags); 21987b92493aSMark Johnston } 21997b92493aSMark Johnston 22007b92493aSMark Johnston /* 22017b92493aSMark Johnston * The matching socket disappeared out from under us. Fall back 22027b92493aSMark Johnston * to a serialized lookup. 22037b92493aSMark Johnston */ 22047b92493aSMark Johnston return (INP_LOOKUP_AGAIN); 22057b92493aSMark Johnston } 22067b92493aSMark Johnston return (NULL); 22077b92493aSMark Johnston } 22087b92493aSMark Johnston 22094130ea61SMark Johnston static struct inpcb * 22104130ea61SMark Johnston in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 22114130ea61SMark Johnston u_short fport, struct in_addr laddr, u_short lport) 22124130ea61SMark Johnston { 22134130ea61SMark Johnston struct inpcbhead *head; 22144130ea61SMark Johnston struct inpcb *inp, *local_wild, *local_exact, *jail_wild; 2215e3fd5ffdSRobert Watson #ifdef INET6 22164130ea61SMark Johnston struct inpcb *local_wild_mapped; 2217e3fd5ffdSRobert Watson #endif 2218413628a7SBjoern A. Zeeb 22194130ea61SMark Johnston INP_HASH_LOCK_ASSERT(pcbinfo); 2220d93ec8cbSMark Johnston 2221d93ec8cbSMark Johnston /* 2222413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2223413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2224413628a7SBjoern A. Zeeb * 2. jailed, wild. 2225413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2226413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2227413628a7SBjoern A. Zeeb */ 2228fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 2229a0577692SGleb Smirnoff pcbinfo->ipi_hashmask)]; 22304130ea61SMark Johnston local_wild = local_exact = jail_wild = NULL; 22314130ea61SMark Johnston #ifdef INET6 22324130ea61SMark Johnston local_wild_mapped = NULL; 22334130ea61SMark Johnston #endif 2234fdb987beSMark Johnston CK_LIST_FOREACH(inp, head, inp_hash_wild) { 22353e98dcb3SMark Johnston inp_lookup_match_t match; 22364130ea61SMark Johnston bool injail; 22374130ea61SMark Johnston 22383e98dcb3SMark Johnston match = in_pcblookup_wild_match(inp, laddr, lport); 22393e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_NONE) 2240413628a7SBjoern A. Zeeb continue; 2241413628a7SBjoern A. Zeeb 22424130ea61SMark Johnston injail = prison_flag(inp->inp_cred, PR_IP4) != 0; 2243413628a7SBjoern A. Zeeb if (injail) { 22444130ea61SMark Johnston if (prison_check_ip4_locked(inp->inp_cred->cr_prison, 22454130ea61SMark Johnston &laddr) != 0) 2246413628a7SBjoern A. Zeeb continue; 2247413628a7SBjoern A. Zeeb } else { 2248413628a7SBjoern A. Zeeb if (local_exact != NULL) 2249413628a7SBjoern A. Zeeb continue; 2250413628a7SBjoern A. Zeeb } 2251413628a7SBjoern A. Zeeb 22523e98dcb3SMark Johnston if (match == INPLOOKUP_MATCH_LADDR) { 2253413628a7SBjoern A. Zeeb if (injail) 2254c3229e05SDavid Greenman return (inp); 2255413628a7SBjoern A. Zeeb local_exact = inp; 22563e98dcb3SMark Johnston } else { 2257e3fd5ffdSRobert Watson #ifdef INET6 2258413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 22595cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2260cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2261cfa1ca9dSYoshinobu Inoue else 226283e521ecSBjoern A. Zeeb #endif 2263413628a7SBjoern A. Zeeb if (injail) 2264413628a7SBjoern A. Zeeb jail_wild = inp; 2265413628a7SBjoern A. Zeeb else 22666d6a026bSDavid Greenman local_wild = inp; 22676d6a026bSDavid Greenman } 22684130ea61SMark Johnston } 2269413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2270413628a7SBjoern A. Zeeb return (jail_wild); 2271413628a7SBjoern A. Zeeb if (local_exact != NULL) 2272413628a7SBjoern A. Zeeb return (local_exact); 2273413628a7SBjoern A. Zeeb if (local_wild != NULL) 2274c3229e05SDavid Greenman return (local_wild); 2275413628a7SBjoern A. Zeeb #ifdef INET6 2276413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2277413628a7SBjoern A. Zeeb return (local_wild_mapped); 227883e521ecSBjoern A. Zeeb #endif 22796d6a026bSDavid Greenman return (NULL); 228015bd2b43SDavid Greenman } 2281fa046d87SRobert Watson 2282fa046d87SRobert Watson /* 22834130ea61SMark Johnston * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 22844130ea61SMark Johnston * that the caller has either locked the hash list, which usually happens 22854130ea61SMark Johnston * for bind(2) operations, or is in SMR section, which happens when sorting 22864130ea61SMark Johnston * out incoming packets. 2287fa046d87SRobert Watson */ 2288fa046d87SRobert Watson static struct inpcb * 22894130ea61SMark Johnston in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 22904130ea61SMark Johnston u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 22914130ea61SMark Johnston uint8_t numa_domain) 22924130ea61SMark Johnston { 22934130ea61SMark Johnston struct inpcb *inp; 22944130ea61SMark Johnston const u_short fport = fport_arg, lport = lport_arg; 22954130ea61SMark Johnston 22964130ea61SMark Johnston KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0, 22974130ea61SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 22984130ea61SMark Johnston KASSERT(faddr.s_addr != INADDR_ANY, 22994130ea61SMark Johnston ("%s: invalid foreign address", __func__)); 23004130ea61SMark Johnston KASSERT(laddr.s_addr != INADDR_ANY, 23014130ea61SMark Johnston ("%s: invalid local address", __func__)); 23027b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(pcbinfo); 23034130ea61SMark Johnston 23044130ea61SMark Johnston inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 23054130ea61SMark Johnston if (inp != NULL) 23064130ea61SMark Johnston return (inp); 23074130ea61SMark Johnston 23084130ea61SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 23097b92493aSMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 23107b92493aSMark Johnston &laddr, lport, numa_domain); 23114130ea61SMark Johnston if (inp == NULL) { 23124130ea61SMark Johnston inp = in_pcblookup_hash_wild_locked(pcbinfo, faddr, 23134130ea61SMark Johnston fport, laddr, lport); 23144130ea61SMark Johnston } 23154130ea61SMark Johnston } 23164130ea61SMark Johnston 23174130ea61SMark Johnston return (inp); 23184130ea61SMark Johnston } 23194130ea61SMark Johnston 23204130ea61SMark Johnston static struct inpcb * 23217b92493aSMark Johnston in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2322fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 23234130ea61SMark Johnston uint8_t numa_domain) 2324fa046d87SRobert Watson { 2325fa046d87SRobert Watson struct inpcb *inp; 23267b92493aSMark Johnston const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 23277b92493aSMark Johnston 23287b92493aSMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 23297b92493aSMark Johnston ("%s: LOCKPCB not set", __func__)); 23307b92493aSMark Johnston 23317b92493aSMark Johnston INP_HASH_WLOCK(pcbinfo); 23327b92493aSMark Johnston inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 23337b92493aSMark Johnston lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain); 23347b92493aSMark Johnston if (inp != NULL && !inp_trylock(inp, lockflags)) { 23357b92493aSMark Johnston in_pcbref(inp); 23367b92493aSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 23377b92493aSMark Johnston inp_lock(inp, lockflags); 23387b92493aSMark Johnston if (in_pcbrele(inp, lockflags)) 23397b92493aSMark Johnston /* XXX-MJ or retry until we get a negative match? */ 23407b92493aSMark Johnston inp = NULL; 23417b92493aSMark Johnston } else { 23427b92493aSMark Johnston INP_HASH_WUNLOCK(pcbinfo); 23437b92493aSMark Johnston } 23447b92493aSMark Johnston return (inp); 23457b92493aSMark Johnston } 23467b92493aSMark Johnston 23477b92493aSMark Johnston static struct inpcb * 23487b92493aSMark Johnston in_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr, 23497b92493aSMark Johnston u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 23507b92493aSMark Johnston uint8_t numa_domain) 23517b92493aSMark Johnston { 23527b92493aSMark Johnston struct inpcb *inp; 23537b92493aSMark Johnston const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 23547b92493aSMark Johnston const u_short fport = fport_arg, lport = lport_arg; 2355fa046d87SRobert Watson 2356675e2618SMark Johnston KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2357675e2618SMark Johnston ("%s: invalid lookup flags %d", __func__, lookupflags)); 2358675e2618SMark Johnston KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2359675e2618SMark Johnston ("%s: LOCKPCB not set", __func__)); 2360675e2618SMark Johnston 2361db0ac6deSCy Schubert smr_enter(pcbinfo->ipi_smr); 23627b92493aSMark Johnston inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 2363fa046d87SRobert Watson if (inp != NULL) { 23647b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 23657b92493aSMark Johnston /* 23667b92493aSMark Johnston * Revalidate the 4-tuple, the socket could have been 23677b92493aSMark Johnston * disconnected. 23687b92493aSMark Johnston */ 23697b92493aSMark Johnston if (__predict_true(in_pcblookup_exact_match(inp, 23707b92493aSMark Johnston faddr, fport, laddr, lport))) 23717b92493aSMark Johnston return (inp); 23727b92493aSMark Johnston inp_unlock(inp, lockflags); 23737b92493aSMark Johnston } 23747b92493aSMark Johnston 23757b92493aSMark Johnston /* 23767b92493aSMark Johnston * We failed to lock the inpcb, or its connection state changed 23777b92493aSMark Johnston * out from under us. Fall back to a precise search. 23787b92493aSMark Johnston */ 23797b92493aSMark Johnston return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 23807b92493aSMark Johnston lookupflags, numa_domain)); 23817b92493aSMark Johnston } 23827b92493aSMark Johnston 23837b92493aSMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 23847b92493aSMark Johnston inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 23857b92493aSMark Johnston &laddr, lport, numa_domain); 23867b92493aSMark Johnston if (inp != NULL) { 23877b92493aSMark Johnston if (__predict_true(inp_smr_lock(inp, lockflags))) { 23887b92493aSMark Johnston if (__predict_true(in_pcblookup_wild_match(inp, 23897b92493aSMark Johnston laddr, lport) != INPLOOKUP_MATCH_NONE)) 23907b92493aSMark Johnston return (inp); 23917b92493aSMark Johnston inp_unlock(inp, lockflags); 23927b92493aSMark Johnston } 23937b92493aSMark Johnston inp = INP_LOOKUP_AGAIN; 23947b92493aSMark Johnston } else { 23957b92493aSMark Johnston inp = in_pcblookup_hash_wild_smr(pcbinfo, faddr, fport, 23967b92493aSMark Johnston laddr, lport, lockflags); 23977b92493aSMark Johnston } 23987b92493aSMark Johnston if (inp == INP_LOOKUP_AGAIN) { 23997b92493aSMark Johnston return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, 24007b92493aSMark Johnston lport, lookupflags, numa_domain)); 24017b92493aSMark Johnston } 24027b92493aSMark Johnston } 24037b92493aSMark Johnston 24047b92493aSMark Johnston if (inp == NULL) 2405db0ac6deSCy Schubert smr_exit(pcbinfo->ipi_smr); 2406d797164aSGleb Smirnoff 2407fa046d87SRobert Watson return (inp); 2408fa046d87SRobert Watson } 2409fa046d87SRobert Watson 2410fa046d87SRobert Watson /* 2411d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2412d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 2413fa046d87SRobert Watson */ 2414fa046d87SRobert Watson struct inpcb * 2415fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 24164130ea61SMark Johnston struct in_addr laddr, u_int lport, int lookupflags, 24174130ea61SMark Johnston struct ifnet *ifp __unused) 2418fa046d87SRobert Watson { 24194130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 24204130ea61SMark Johnston lookupflags, M_NODOM)); 2421fa046d87SRobert Watson } 2422d3c1f003SRobert Watson 2423d3c1f003SRobert Watson struct inpcb * 2424d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2425d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 24264130ea61SMark Johnston struct ifnet *ifp __unused, struct mbuf *m) 2427d3c1f003SRobert Watson { 24284130ea61SMark Johnston return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 24294130ea61SMark Johnston lookupflags, m->m_pkthdr.numa_domain)); 2430d3c1f003SRobert Watson } 243167107f45SBjoern A. Zeeb #endif /* INET */ 243215bd2b43SDavid Greenman 24337b92493aSMark Johnston static bool 24347b92493aSMark Johnston in_pcbjailed(const struct inpcb *inp, unsigned int flag) 24357b92493aSMark Johnston { 24367b92493aSMark Johnston return (prison_flag(inp->inp_cred, flag) != 0); 24377b92493aSMark Johnston } 24387b92493aSMark Johnston 24397b92493aSMark Johnston /* 24407b92493aSMark Johnston * Insert the PCB into a hash chain using ordering rules which ensure that 24417b92493aSMark Johnston * in_pcblookup_hash_wild_*() always encounter the highest-ranking PCB first. 24427b92493aSMark Johnston * 24437b92493aSMark Johnston * Specifically, keep jailed PCBs in front of non-jailed PCBs, and keep PCBs 244493998d16SMark Johnston * with exact local addresses ahead of wildcard PCBs. Unbound v4-mapped v6 PCBs 244593998d16SMark Johnston * always appear last no matter whether they are jailed. 24467b92493aSMark Johnston */ 24477b92493aSMark Johnston static void 24487b92493aSMark Johnston _in_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 24497b92493aSMark Johnston { 24507b92493aSMark Johnston struct inpcb *last; 24517b92493aSMark Johnston bool bound, injail; 24527b92493aSMark Johnston 245393998d16SMark Johnston INP_LOCK_ASSERT(inp); 24547b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 24557b92493aSMark Johnston 24567b92493aSMark Johnston last = NULL; 24577b92493aSMark Johnston bound = inp->inp_laddr.s_addr != INADDR_ANY; 245893998d16SMark Johnston if (!bound && (inp->inp_vflag & INP_IPV6PROTO) != 0) { 245993998d16SMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 246093998d16SMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 246193998d16SMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 246293998d16SMark Johnston return; 246393998d16SMark Johnston } 246493998d16SMark Johnston } 246593998d16SMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 246693998d16SMark Johnston return; 246793998d16SMark Johnston } 246893998d16SMark Johnston 24697b92493aSMark Johnston injail = in_pcbjailed(inp, PR_IP4); 24707b92493aSMark Johnston if (!injail) { 24717b92493aSMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 247293998d16SMark Johnston if (!in_pcbjailed(last, PR_IP4)) 24737b92493aSMark Johnston break; 24747b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 24757b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 24767b92493aSMark Johnston return; 24777b92493aSMark Johnston } 24787b92493aSMark Johnston } 24797b92493aSMark Johnston } else if (!CK_LIST_EMPTY(pcbhash) && 24807b92493aSMark Johnston !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP4)) { 24817b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 24827b92493aSMark Johnston return; 24837b92493aSMark Johnston } 24847b92493aSMark Johnston if (!bound) { 24857b92493aSMark Johnston CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 24867b92493aSMark Johnston if (last->inp_laddr.s_addr == INADDR_ANY) 24877b92493aSMark Johnston break; 24887b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 24897b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 24907b92493aSMark Johnston return; 24917b92493aSMark Johnston } 24927b92493aSMark Johnston } 24937b92493aSMark Johnston } 24947b92493aSMark Johnston if (last == NULL) 24957b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 24967b92493aSMark Johnston else 24977b92493aSMark Johnston CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 24987b92493aSMark Johnston } 24997b92493aSMark Johnston 25007b92493aSMark Johnston #ifdef INET6 25017b92493aSMark Johnston /* 25027b92493aSMark Johnston * See the comment above _in_pcbinshash_wild(). 25037b92493aSMark Johnston */ 25047b92493aSMark Johnston static void 25057b92493aSMark Johnston _in6_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 25067b92493aSMark Johnston { 25077b92493aSMark Johnston struct inpcb *last; 25087b92493aSMark Johnston bool bound, injail; 25097b92493aSMark Johnston 251093998d16SMark Johnston INP_LOCK_ASSERT(inp); 25117b92493aSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 25127b92493aSMark Johnston 25137b92493aSMark Johnston last = NULL; 25147b92493aSMark Johnston bound = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr); 25157b92493aSMark Johnston injail = in_pcbjailed(inp, PR_IP6); 25167b92493aSMark Johnston if (!injail) { 25177b92493aSMark Johnston CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 251893998d16SMark Johnston if (!in_pcbjailed(last, PR_IP6)) 25197b92493aSMark Johnston break; 25207b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25217b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25227b92493aSMark Johnston return; 25237b92493aSMark Johnston } 25247b92493aSMark Johnston } 25257b92493aSMark Johnston } else if (!CK_LIST_EMPTY(pcbhash) && 25267b92493aSMark Johnston !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP6)) { 25277b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25287b92493aSMark Johnston return; 25297b92493aSMark Johnston } 25307b92493aSMark Johnston if (!bound) { 25317b92493aSMark Johnston CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 25327b92493aSMark Johnston if (IN6_IS_ADDR_UNSPECIFIED(&last->in6p_laddr)) 25337b92493aSMark Johnston break; 25347b92493aSMark Johnston if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 25357b92493aSMark Johnston CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 25367b92493aSMark Johnston return; 25377b92493aSMark Johnston } 25387b92493aSMark Johnston } 25397b92493aSMark Johnston } 25407b92493aSMark Johnston if (last == NULL) 25417b92493aSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 25427b92493aSMark Johnston else 25437b92493aSMark Johnston CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 25447b92493aSMark Johnston } 25457b92493aSMark Johnston #endif 25467b92493aSMark Johnston 25477bc4aca7SDavid Greenman /* 2548c3229e05SDavid Greenman * Insert PCB onto various hash lists. 25497bc4aca7SDavid Greenman */ 2550db0ac6deSCy Schubert int 2551db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp) 255215bd2b43SDavid Greenman { 2553c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2554c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2555c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2556c3229e05SDavid Greenman struct inpcbport *phd; 2557fdb987beSMark Johnston uint32_t hash; 2558fdb987beSMark Johnston bool connected; 255915bd2b43SDavid Greenman 25608501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2561fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2562111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2563111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2564602cc7f1SRobert Watson 2565cfa1ca9dSYoshinobu Inoue #ifdef INET6 2566fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2567fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2568fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2569fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2570fdb987beSMark Johnston } else 257183e521ecSBjoern A. Zeeb #endif 2572fdb987beSMark Johnston { 2573fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2574fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2575fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2576fdb987beSMark Johnston } 2577fdb987beSMark Johnston 2578fdb987beSMark Johnston if (connected) 2579fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_exact[hash]; 2580fdb987beSMark Johnston else 2581fdb987beSMark Johnston pcbhash = &pcbinfo->ipi_hash_wild[hash]; 258215bd2b43SDavid Greenman 2583712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2584712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2585c3229e05SDavid Greenman 2586c3229e05SDavid Greenman /* 25871a43cff9SSean Bruno * Add entry to load balance group. 25881a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 25891a43cff9SSean Bruno */ 2590e3ba0d6aSGleb Smirnoff if ((inp->inp_socket->so_options & SO_REUSEPORT_LB) != 0) { 2591a152dd86SMark Johnston int error = in_pcbinslbgrouphash(inp, M_NODOM); 2592a152dd86SMark Johnston if (error != 0) 2593a152dd86SMark Johnston return (error); 25941a43cff9SSean Bruno } 25951a43cff9SSean Bruno 25961a43cff9SSean Bruno /* 2597c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2598c3229e05SDavid Greenman */ 2599b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2600c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2601c3229e05SDavid Greenman break; 2602c3229e05SDavid Greenman } 2603a152dd86SMark Johnston 2604c3229e05SDavid Greenman /* 2605c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2606c3229e05SDavid Greenman */ 2607c3229e05SDavid Greenman if (phd == NULL) { 2608db0ac6deSCy Schubert phd = uma_zalloc_smr(pcbinfo->ipi_portzone, M_NOWAIT); 2609c3229e05SDavid Greenman if (phd == NULL) { 2610a43e7a96SGleb Smirnoff if ((inp->inp_flags & INP_INLBGROUP) != 0) 2611a152dd86SMark Johnston in_pcbremlbgrouphash(inp); 2612a152dd86SMark Johnston return (ENOMEM); 2613c3229e05SDavid Greenman } 2614c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2615b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2616b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2617c3229e05SDavid Greenman } 2618c3229e05SDavid Greenman inp->inp_phd = phd; 2619b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2620fdb987beSMark Johnston 2621fdb987beSMark Johnston /* 2622fdb987beSMark Johnston * The PCB may have been disconnected in the past. Before we can safely 2623fdb987beSMark Johnston * make it visible in the hash table, we must wait for all readers which 2624fdb987beSMark Johnston * may be traversing this PCB to finish. 2625fdb987beSMark Johnston */ 2626fdb987beSMark Johnston if (inp->inp_smr != SMR_SEQ_INVALID) { 2627fdb987beSMark Johnston smr_wait(pcbinfo->ipi_smr, inp->inp_smr); 2628fdb987beSMark Johnston inp->inp_smr = SMR_SEQ_INVALID; 2629fdb987beSMark Johnston } 2630fdb987beSMark Johnston 2631fdb987beSMark Johnston if (connected) 2632fdb987beSMark Johnston CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_exact); 26337b92493aSMark Johnston else { 26347b92493aSMark Johnston #ifdef INET6 26357b92493aSMark Johnston if ((inp->inp_vflag & INP_IPV6) != 0) 26367b92493aSMark Johnston _in6_pcbinshash_wild(pcbhash, inp); 2637fdb987beSMark Johnston else 26387b92493aSMark Johnston #endif 26397b92493aSMark Johnston _in_pcbinshash_wild(pcbhash, inp); 26407b92493aSMark Johnston } 2641111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 2642db0ac6deSCy Schubert 2643c3229e05SDavid Greenman return (0); 264415bd2b43SDavid Greenman } 264515bd2b43SDavid Greenman 2646fdb987beSMark Johnston void 2647fdb987beSMark Johnston in_pcbremhash_locked(struct inpcb *inp) 26483ba34b07SGleb Smirnoff { 26493ba34b07SGleb Smirnoff struct inpcbport *phd = inp->inp_phd; 26503ba34b07SGleb Smirnoff 26513ba34b07SGleb Smirnoff INP_WLOCK_ASSERT(inp); 2652fdb987beSMark Johnston INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 26533ba34b07SGleb Smirnoff MPASS(inp->inp_flags & INP_INHASHLIST); 26543ba34b07SGleb Smirnoff 2655a43e7a96SGleb Smirnoff if ((inp->inp_flags & INP_INLBGROUP) != 0) 26563ba34b07SGleb Smirnoff in_pcbremlbgrouphash(inp); 2657fdb987beSMark Johnston #ifdef INET6 2658fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2659fdb987beSMark Johnston if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 2660fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2661fdb987beSMark Johnston else 2662fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2663fdb987beSMark Johnston } else 2664fdb987beSMark Johnston #endif 2665fdb987beSMark Johnston { 2666fdb987beSMark Johnston if (in_nullhost(inp->inp_faddr)) 2667fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2668fdb987beSMark Johnston else 2669fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2670fdb987beSMark Johnston } 26713ba34b07SGleb Smirnoff CK_LIST_REMOVE(inp, inp_portlist); 26723ba34b07SGleb Smirnoff if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 26733ba34b07SGleb Smirnoff CK_LIST_REMOVE(phd, phd_hash); 26743ba34b07SGleb Smirnoff uma_zfree_smr(inp->inp_pcbinfo->ipi_portzone, phd); 26753ba34b07SGleb Smirnoff } 26763ba34b07SGleb Smirnoff inp->inp_flags &= ~INP_INHASHLIST; 26773ba34b07SGleb Smirnoff } 26783ba34b07SGleb Smirnoff 2679fdb987beSMark Johnston static void 2680fdb987beSMark Johnston in_pcbremhash(struct inpcb *inp) 2681fdb987beSMark Johnston { 2682fdb987beSMark Johnston INP_HASH_WLOCK(inp->inp_pcbinfo); 2683fdb987beSMark Johnston in_pcbremhash_locked(inp); 2684fdb987beSMark Johnston INP_HASH_WUNLOCK(inp->inp_pcbinfo); 2685fdb987beSMark Johnston } 2686fdb987beSMark Johnston 268752cd27cbSRobert Watson /* 2688c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2689c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2690c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2691c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2692c3229e05SDavid Greenman */ 269315bd2b43SDavid Greenman void 2694db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp) 269515bd2b43SDavid Greenman { 269659daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 269715bd2b43SDavid Greenman struct inpcbhead *head; 2698fdb987beSMark Johnston uint32_t hash; 2699fdb987beSMark Johnston bool connected; 270015bd2b43SDavid Greenman 27018501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2702fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2703111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2704fdb987beSMark Johnston ("%s: !INP_INHASHLIST", __func__)); 2705fdb987beSMark Johnston KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 2706fdb987beSMark Johnston ("%s: inp was disconnected", __func__)); 2707602cc7f1SRobert Watson 2708cfa1ca9dSYoshinobu Inoue #ifdef INET6 2709fdb987beSMark Johnston if (inp->inp_vflag & INP_IPV6) { 2710fdb987beSMark Johnston hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2711fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2712fdb987beSMark Johnston connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2713fdb987beSMark Johnston } else 271483e521ecSBjoern A. Zeeb #endif 2715fdb987beSMark Johnston { 2716fdb987beSMark Johnston hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2717fdb987beSMark Johnston inp->inp_fport, pcbinfo->ipi_hashmask); 2718fdb987beSMark Johnston connected = !in_nullhost(inp->inp_faddr); 2719fdb987beSMark Johnston } 272015bd2b43SDavid Greenman 2721fdb987beSMark Johnston /* 2722fdb987beSMark Johnston * When rehashing, the caller must ensure that either the new or the old 2723fdb987beSMark Johnston * foreign address was unspecified. 2724fdb987beSMark Johnston */ 2725fdb987beSMark Johnston if (connected) 2726fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_wild); 2727fdb987beSMark Johnston else 2728fdb987beSMark Johnston CK_LIST_REMOVE(inp, inp_hash_exact); 2729fdb987beSMark Johnston 2730fdb987beSMark Johnston if (connected) { 2731fdb987beSMark Johnston head = &pcbinfo->ipi_hash_exact[hash]; 2732fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_exact); 2733fdb987beSMark Johnston } else { 2734fdb987beSMark Johnston head = &pcbinfo->ipi_hash_wild[hash]; 2735fdb987beSMark Johnston CK_LIST_INSERT_HEAD(head, inp, inp_hash_wild); 2736fdb987beSMark Johnston } 2737c3229e05SDavid Greenman } 2738c3229e05SDavid Greenman 2739c3229e05SDavid Greenman /* 274084cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 274184cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 274284cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 274384cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 274484cc0778SGeorge V. Neville-Neil */ 274584cc0778SGeorge V. Neville-Neil void 274684cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 274784cc0778SGeorge V. Neville-Neil { 274884cc0778SGeorge V. Neville-Neil 2749fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 275084cc0778SGeorge V. Neville-Neil return; 275184cc0778SGeorge V. Neville-Neil } 275284cc0778SGeorge V. Neville-Neil 275384cc0778SGeorge V. Neville-Neil /* 2754a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2755a557af22SRobert Watson * label change into the in_pcb for the socket. 2756a557af22SRobert Watson */ 2757a557af22SRobert Watson void 2758136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2759a557af22SRobert Watson { 2760a557af22SRobert Watson #ifdef MAC 2761a557af22SRobert Watson struct inpcb *inp; 2762a557af22SRobert Watson 27634c7c478dSRobert Watson inp = sotoinpcb(so); 27644c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2765602cc7f1SRobert Watson 27668501a69cSRobert Watson INP_WLOCK(inp); 2767310e7cebSRobert Watson SOCK_LOCK(so); 2768a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2769310e7cebSRobert Watson SOCK_UNLOCK(so); 27708501a69cSRobert Watson INP_WUNLOCK(inp); 2771a557af22SRobert Watson #endif 2772a557af22SRobert Watson } 27735f311da2SMike Silbersack 27743d585327SKip Macy void 27753d585327SKip Macy inp_wlock(struct inpcb *inp) 27763d585327SKip Macy { 27773d585327SKip Macy 27788501a69cSRobert Watson INP_WLOCK(inp); 27793d585327SKip Macy } 27803d585327SKip Macy 27813d585327SKip Macy void 27823d585327SKip Macy inp_wunlock(struct inpcb *inp) 27833d585327SKip Macy { 27843d585327SKip Macy 27858501a69cSRobert Watson INP_WUNLOCK(inp); 27863d585327SKip Macy } 27873d585327SKip Macy 27883d585327SKip Macy void 27893d585327SKip Macy inp_rlock(struct inpcb *inp) 27903d585327SKip Macy { 27913d585327SKip Macy 2792a69042a5SRobert Watson INP_RLOCK(inp); 27933d585327SKip Macy } 27943d585327SKip Macy 27953d585327SKip Macy void 27963d585327SKip Macy inp_runlock(struct inpcb *inp) 27973d585327SKip Macy { 27983d585327SKip Macy 2799a69042a5SRobert Watson INP_RUNLOCK(inp); 28003d585327SKip Macy } 28013d585327SKip Macy 2802c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 28033d585327SKip Macy void 2804e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 28053d585327SKip Macy { 28063d585327SKip Macy 28078501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 28083d585327SKip Macy } 28093d585327SKip Macy 28103d585327SKip Macy void 2811e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 28123d585327SKip Macy { 28133d585327SKip Macy 28143d585327SKip Macy INP_UNLOCK_ASSERT(inp); 28153d585327SKip Macy } 28163d585327SKip Macy #endif 28173d585327SKip Macy 28189378e437SKip Macy void 281924cf7a8dSGleb Smirnoff inp_apply_all(struct inpcbinfo *pcbinfo, 282024cf7a8dSGleb Smirnoff void (*func)(struct inpcb *, void *), void *arg) 28219378e437SKip Macy { 282224cf7a8dSGleb Smirnoff struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2823db0ac6deSCy Schubert INPLOOKUP_WLOCKPCB); 28249378e437SKip Macy struct inpcb *inp; 28259378e437SKip Macy 2826db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) 28279378e437SKip Macy func(inp, arg); 28289378e437SKip Macy } 28299378e437SKip Macy 28309378e437SKip Macy struct socket * 28319378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 28329378e437SKip Macy { 28339378e437SKip Macy 28349378e437SKip Macy INP_WLOCK_ASSERT(inp); 28359378e437SKip Macy return (inp->inp_socket); 28369378e437SKip Macy } 28379378e437SKip Macy 28389378e437SKip Macy struct tcpcb * 28399378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 28409378e437SKip Macy { 28419378e437SKip Macy 28429378e437SKip Macy INP_WLOCK_ASSERT(inp); 28439378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 28449378e437SKip Macy } 28459378e437SKip Macy 28469378e437SKip Macy int 28479378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 28489378e437SKip Macy { 28499378e437SKip Macy 28509378e437SKip Macy return (inp->inp_ip_tos); 28519378e437SKip Macy } 28529378e437SKip Macy 28539378e437SKip Macy void 28549378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 28559378e437SKip Macy { 28569378e437SKip Macy 28579378e437SKip Macy inp->inp_ip_tos = val; 28589378e437SKip Macy } 28599378e437SKip Macy 28609378e437SKip Macy void 2861df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 28629d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 28639378e437SKip Macy { 28649378e437SKip Macy 28659d29c635SKip Macy INP_LOCK_ASSERT(inp); 2866df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2867df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 28689378e437SKip Macy *lp = inp->inp_lport; 28699378e437SKip Macy *fp = inp->inp_fport; 28709378e437SKip Macy } 28719378e437SKip Macy 2872dd0e6c38SKip Macy struct inpcb * 2873dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2874dd0e6c38SKip Macy { 2875dd0e6c38SKip Macy 2876dd0e6c38SKip Macy return (sotoinpcb(so)); 2877dd0e6c38SKip Macy } 2878dd0e6c38SKip Macy 2879cc65eb4eSGleb Smirnoff /* 2880cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2881cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2882cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2883cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2884cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2885cc65eb4eSGleb Smirnoff * hidden from users. 2886cc65eb4eSGleb Smirnoff */ 2887cc65eb4eSGleb Smirnoff void 2888cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2889cc65eb4eSGleb Smirnoff { 2890cc65eb4eSGleb Smirnoff 289179db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2892cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2893cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2894cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2895cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2896cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 28973a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2898cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2899cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2900cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2901cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2902cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2903cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2904cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2905cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2906cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2907cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2908cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2909cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2910cc65eb4eSGleb Smirnoff } 2911cc65eb4eSGleb Smirnoff 2912a35bdd44SMichael Tuexen int 2913a35bdd44SMichael Tuexen sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, 2914a35bdd44SMichael Tuexen int (*ctloutput_set)(struct inpcb *, struct sockopt *)) 2915a35bdd44SMichael Tuexen { 2916a35bdd44SMichael Tuexen struct sockopt sopt; 2917a35bdd44SMichael Tuexen struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2918a35bdd44SMichael Tuexen INPLOOKUP_WLOCKPCB); 2919a35bdd44SMichael Tuexen struct inpcb *inp; 2920a35bdd44SMichael Tuexen struct sockopt_parameters *params; 2921a35bdd44SMichael Tuexen struct socket *so; 2922a35bdd44SMichael Tuexen int error; 2923a35bdd44SMichael Tuexen char buf[1024]; 2924a35bdd44SMichael Tuexen 2925a35bdd44SMichael Tuexen if (req->oldptr != NULL || req->oldlen != 0) 2926a35bdd44SMichael Tuexen return (EINVAL); 2927a35bdd44SMichael Tuexen if (req->newptr == NULL) 2928a35bdd44SMichael Tuexen return (EPERM); 2929a35bdd44SMichael Tuexen if (req->newlen > sizeof(buf)) 2930a35bdd44SMichael Tuexen return (ENOMEM); 2931a35bdd44SMichael Tuexen error = SYSCTL_IN(req, buf, req->newlen); 2932a35bdd44SMichael Tuexen if (error != 0) 2933a35bdd44SMichael Tuexen return (error); 2934a35bdd44SMichael Tuexen if (req->newlen < sizeof(struct sockopt_parameters)) 2935a35bdd44SMichael Tuexen return (EINVAL); 2936a35bdd44SMichael Tuexen params = (struct sockopt_parameters *)buf; 2937a35bdd44SMichael Tuexen sopt.sopt_level = params->sop_level; 2938a35bdd44SMichael Tuexen sopt.sopt_name = params->sop_optname; 2939a35bdd44SMichael Tuexen sopt.sopt_dir = SOPT_SET; 2940a35bdd44SMichael Tuexen sopt.sopt_val = params->sop_optval; 2941a35bdd44SMichael Tuexen sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters); 2942a35bdd44SMichael Tuexen sopt.sopt_td = NULL; 2943a0aeb1ceSMichael Tuexen #ifdef INET6 2944a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) { 2945a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_laddr)) 2946a35bdd44SMichael Tuexen params->sop_inc.inc6_laddr.s6_addr16[1] = 2947a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2948a35bdd44SMichael Tuexen if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_faddr)) 2949a35bdd44SMichael Tuexen params->sop_inc.inc6_faddr.s6_addr16[1] = 2950a35bdd44SMichael Tuexen htons(params->sop_inc.inc6_zoneid & 0xffff); 2951a35bdd44SMichael Tuexen } 2952a0aeb1ceSMichael Tuexen #endif 2953a35bdd44SMichael Tuexen if (params->sop_inc.inc_lport != htons(0)) { 2954a35bdd44SMichael Tuexen if (params->sop_inc.inc_fport == htons(0)) 2955a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH_WILD(params->sop_inc.inc_lport, 2956a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2957a35bdd44SMichael Tuexen else 2958a0aeb1ceSMichael Tuexen #ifdef INET6 2959a35bdd44SMichael Tuexen if (params->sop_inc.inc_flags & INC_ISIPV6) 2960a35bdd44SMichael Tuexen inpi.hash = INP6_PCBHASH( 2961a35bdd44SMichael Tuexen ¶ms->sop_inc.inc6_faddr, 2962a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2963a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2964a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2965a35bdd44SMichael Tuexen else 2966a0aeb1ceSMichael Tuexen #endif 2967a35bdd44SMichael Tuexen inpi.hash = INP_PCBHASH( 2968a35bdd44SMichael Tuexen ¶ms->sop_inc.inc_faddr, 2969a35bdd44SMichael Tuexen params->sop_inc.inc_lport, 2970a35bdd44SMichael Tuexen params->sop_inc.inc_fport, 2971a35bdd44SMichael Tuexen pcbinfo->ipi_hashmask); 2972a35bdd44SMichael Tuexen } 2973a35bdd44SMichael Tuexen while ((inp = inp_next(&inpi)) != NULL) 2974a35bdd44SMichael Tuexen if (inp->inp_gencnt == params->sop_id) { 297553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 2976a35bdd44SMichael Tuexen INP_WUNLOCK(inp); 2977a35bdd44SMichael Tuexen return (ECONNRESET); 2978a35bdd44SMichael Tuexen } 2979a35bdd44SMichael Tuexen so = inp->inp_socket; 2980a35bdd44SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 2981a35bdd44SMichael Tuexen soref(so); 2982bed7633bSMichael Tuexen if (params->sop_level == SOL_SOCKET) { 2983bed7633bSMichael Tuexen INP_WUNLOCK(inp); 2984bed7633bSMichael Tuexen error = sosetopt(so, &sopt); 2985bed7633bSMichael Tuexen } else 2986a35bdd44SMichael Tuexen error = (*ctloutput_set)(inp, &sopt); 2987a35bdd44SMichael Tuexen sorele(so); 2988a35bdd44SMichael Tuexen break; 2989a35bdd44SMichael Tuexen } 2990a35bdd44SMichael Tuexen if (inp == NULL) 2991a35bdd44SMichael Tuexen error = ESRCH; 2992a35bdd44SMichael Tuexen return (error); 2993a35bdd44SMichael Tuexen } 2994a35bdd44SMichael Tuexen 2995497057eeSRobert Watson #ifdef DDB 2996497057eeSRobert Watson static void 2997497057eeSRobert Watson db_print_indent(int indent) 2998497057eeSRobert Watson { 2999497057eeSRobert Watson int i; 3000497057eeSRobert Watson 3001497057eeSRobert Watson for (i = 0; i < indent; i++) 3002497057eeSRobert Watson db_printf(" "); 3003497057eeSRobert Watson } 3004497057eeSRobert Watson 3005497057eeSRobert Watson static void 3006497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 3007497057eeSRobert Watson { 3008497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 3009497057eeSRobert Watson 3010497057eeSRobert Watson db_print_indent(indent); 3011497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 3012497057eeSRobert Watson 3013497057eeSRobert Watson indent += 2; 3014497057eeSRobert Watson 301503dc38a4SRobert Watson #ifdef INET6 3016dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3017497057eeSRobert Watson /* IPv6. */ 3018497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 3019497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 302083e521ecSBjoern A. Zeeb } else 302103dc38a4SRobert Watson #endif 302283e521ecSBjoern A. Zeeb { 3023497057eeSRobert Watson /* IPv4. */ 3024497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 3025497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 3026497057eeSRobert Watson } 3027497057eeSRobert Watson db_print_indent(indent); 3028497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 3029497057eeSRobert Watson ntohs(inc->inc_lport)); 3030497057eeSRobert Watson db_print_indent(indent); 3031497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 3032497057eeSRobert Watson ntohs(inc->inc_fport)); 3033497057eeSRobert Watson } 3034497057eeSRobert Watson 3035497057eeSRobert Watson static void 3036497057eeSRobert Watson db_print_inpflags(int inp_flags) 3037497057eeSRobert Watson { 3038497057eeSRobert Watson int comma; 3039497057eeSRobert Watson 3040497057eeSRobert Watson comma = 0; 3041497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 3042497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 3043497057eeSRobert Watson comma = 1; 3044497057eeSRobert Watson } 3045497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 3046497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 3047497057eeSRobert Watson comma = 1; 3048497057eeSRobert Watson } 3049497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 3050497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 3051497057eeSRobert Watson comma = 1; 3052497057eeSRobert Watson } 3053dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 3054dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 3055dce33a45SErmal Luçi comma = 1; 3056dce33a45SErmal Luçi } 3057497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 3058497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 3059497057eeSRobert Watson comma = 1; 3060497057eeSRobert Watson } 3061497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 3062497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 3063497057eeSRobert Watson comma = 1; 3064497057eeSRobert Watson } 3065497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 3066497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 3067497057eeSRobert Watson comma = 1; 3068497057eeSRobert Watson } 3069497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 3070497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 3071497057eeSRobert Watson comma = 1; 3072497057eeSRobert Watson } 3073497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 3074497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 3075497057eeSRobert Watson comma = 1; 3076497057eeSRobert Watson } 3077497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 3078497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 3079497057eeSRobert Watson comma = 1; 3080497057eeSRobert Watson } 3081497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 3082497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 3083497057eeSRobert Watson comma = 1; 3084497057eeSRobert Watson } 3085497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 3086497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 3087497057eeSRobert Watson comma = 1; 3088497057eeSRobert Watson } 30893cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 30903cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 30913cca425bSMichael Tuexen comma = 1; 30923cca425bSMichael Tuexen } 3093497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 3094497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3095497057eeSRobert Watson comma = 1; 3096497057eeSRobert Watson } 3097497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 3098497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3099497057eeSRobert Watson comma = 1; 3100497057eeSRobert Watson } 3101497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 3102497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3103497057eeSRobert Watson comma = 1; 3104497057eeSRobert Watson } 3105497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 3106497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3107497057eeSRobert Watson comma = 1; 3108497057eeSRobert Watson } 3109497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 3110497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3111497057eeSRobert Watson comma = 1; 3112497057eeSRobert Watson } 3113497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 3114497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3115497057eeSRobert Watson comma = 1; 3116497057eeSRobert Watson } 3117497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 3118497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3119497057eeSRobert Watson comma = 1; 3120497057eeSRobert Watson } 3121497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 3122497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3123497057eeSRobert Watson comma = 1; 3124497057eeSRobert Watson } 3125497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 3126497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3127497057eeSRobert Watson comma = 1; 3128497057eeSRobert Watson } 3129ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 3130ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3131ad71fe3cSRobert Watson comma = 1; 3132ad71fe3cSRobert Watson } 3133ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 3134ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 3135ad71fe3cSRobert Watson comma = 1; 3136ad71fe3cSRobert Watson } 3137ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 3138ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3139ad71fe3cSRobert Watson comma = 1; 3140ad71fe3cSRobert Watson } 3141497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 3142497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3143497057eeSRobert Watson comma = 1; 3144497057eeSRobert Watson } 3145497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 3146497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 3147497057eeSRobert Watson comma = 1; 3148497057eeSRobert Watson } 3149497057eeSRobert Watson } 3150497057eeSRobert Watson 3151497057eeSRobert Watson static void 3152497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 3153497057eeSRobert Watson { 3154497057eeSRobert Watson int comma; 3155497057eeSRobert Watson 3156497057eeSRobert Watson comma = 0; 3157497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3158497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3159497057eeSRobert Watson comma = 1; 3160497057eeSRobert Watson } 3161497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3162497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3163497057eeSRobert Watson comma = 1; 3164497057eeSRobert Watson } 3165497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3166497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3167497057eeSRobert Watson comma = 1; 3168497057eeSRobert Watson } 3169497057eeSRobert Watson } 3170497057eeSRobert Watson 31716d888973SRobert Watson static void 3172497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3173497057eeSRobert Watson { 3174497057eeSRobert Watson 3175497057eeSRobert Watson db_print_indent(indent); 3176497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3177497057eeSRobert Watson 3178497057eeSRobert Watson indent += 2; 3179497057eeSRobert Watson 3180497057eeSRobert Watson db_print_indent(indent); 3181497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3182497057eeSRobert Watson 3183497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3184497057eeSRobert Watson 3185497057eeSRobert Watson db_print_indent(indent); 3186497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 3187497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 3188497057eeSRobert Watson 3189497057eeSRobert Watson db_print_indent(indent); 3190497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3191497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3192497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3193497057eeSRobert Watson db_printf(")\n"); 3194497057eeSRobert Watson 3195497057eeSRobert Watson db_print_indent(indent); 3196497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3197497057eeSRobert Watson inp->inp_vflag); 3198497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3199497057eeSRobert Watson db_printf(")\n"); 3200497057eeSRobert Watson 3201497057eeSRobert Watson db_print_indent(indent); 3202497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3203497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3204497057eeSRobert Watson 3205497057eeSRobert Watson db_print_indent(indent); 3206497057eeSRobert Watson #ifdef INET6 3207497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3208497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3209497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3210497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3211497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3212497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3213497057eeSRobert Watson inp->in6p_hops); 3214497057eeSRobert Watson } else 3215497057eeSRobert Watson #endif 3216497057eeSRobert Watson { 3217497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3218497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3219497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3220497057eeSRobert Watson } 3221497057eeSRobert Watson 3222497057eeSRobert Watson db_print_indent(indent); 3223497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3224497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3225497057eeSRobert Watson } 3226497057eeSRobert Watson 3227497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3228497057eeSRobert Watson { 3229497057eeSRobert Watson struct inpcb *inp; 3230497057eeSRobert Watson 3231497057eeSRobert Watson if (!have_addr) { 3232497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3233497057eeSRobert Watson return; 3234497057eeSRobert Watson } 3235497057eeSRobert Watson inp = (struct inpcb *)addr; 3236497057eeSRobert Watson 3237497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3238497057eeSRobert Watson } 323983e521ecSBjoern A. Zeeb #endif /* DDB */ 3240f3e7afe2SHans Petter Selasky 3241f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3242f3e7afe2SHans Petter Selasky /* 3243f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3244f3e7afe2SHans Petter Selasky * if any. 3245f3e7afe2SHans Petter Selasky */ 3246f3e7afe2SHans Petter Selasky int 3247f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3248f3e7afe2SHans Petter Selasky { 3249f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3250f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 325120abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3252f3e7afe2SHans Petter Selasky }; 3253f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3254f3e7afe2SHans Petter Selasky int error; 3255f3e7afe2SHans Petter Selasky 3256f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3257f3e7afe2SHans Petter Selasky if (mst == NULL) 3258f3e7afe2SHans Petter Selasky return (EINVAL); 3259f3e7afe2SHans Petter Selasky 3260c782ea8bSJohn Baldwin if (mst->sw->snd_tag_modify == NULL) { 3261f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3262f3e7afe2SHans Petter Selasky } else { 3263c782ea8bSJohn Baldwin error = mst->sw->snd_tag_modify(mst, ¶ms); 3264f3e7afe2SHans Petter Selasky } 3265f3e7afe2SHans Petter Selasky return (error); 3266f3e7afe2SHans Petter Selasky } 3267f3e7afe2SHans Petter Selasky 3268f3e7afe2SHans Petter Selasky /* 3269f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3270f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3271f3e7afe2SHans Petter Selasky */ 3272f3e7afe2SHans Petter Selasky int 3273f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3274f3e7afe2SHans Petter Selasky { 3275f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3276f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3277f3e7afe2SHans Petter Selasky int error; 3278f3e7afe2SHans Petter Selasky 3279f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3280f3e7afe2SHans Petter Selasky if (mst == NULL) 3281f3e7afe2SHans Petter Selasky return (EINVAL); 3282f3e7afe2SHans Petter Selasky 3283c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) { 3284f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3285f3e7afe2SHans Petter Selasky } else { 3286c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 3287f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3288f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3289f3e7afe2SHans Petter Selasky } 3290f3e7afe2SHans Petter Selasky return (error); 3291f3e7afe2SHans Petter Selasky } 3292f3e7afe2SHans Petter Selasky 3293f3e7afe2SHans Petter Selasky /* 329495ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 329595ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 329695ed5015SHans Petter Selasky */ 329795ed5015SHans Petter Selasky int 329895ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 329995ed5015SHans Petter Selasky { 330095ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 330195ed5015SHans Petter Selasky struct m_snd_tag *mst; 330295ed5015SHans Petter Selasky int error; 330395ed5015SHans Petter Selasky 330495ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 330595ed5015SHans Petter Selasky if (mst == NULL) 330695ed5015SHans Petter Selasky return (EINVAL); 330795ed5015SHans Petter Selasky 3308c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) 330995ed5015SHans Petter Selasky return (EOPNOTSUPP); 331095ed5015SHans Petter Selasky 3311c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 331295ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 331395ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 331495ed5015SHans Petter Selasky return (error); 331595ed5015SHans Petter Selasky } 331695ed5015SHans Petter Selasky 331795ed5015SHans Petter Selasky /* 3318f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3319f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3320f3e7afe2SHans Petter Selasky */ 3321f3e7afe2SHans Petter Selasky int 3322f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 332320abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 332420abea66SRandall Stewart 3325f3e7afe2SHans Petter Selasky { 3326f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 332795ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 332895ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3329f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3330f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 333198085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3332f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 333320abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3334f3e7afe2SHans Petter Selasky }; 3335f3e7afe2SHans Petter Selasky int error; 3336f3e7afe2SHans Petter Selasky 3337f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3338f3e7afe2SHans Petter Selasky 333985d8d30fSHans Petter Selasky /* 334085d8d30fSHans Petter Selasky * If there is already a send tag, or the INP is being torn 334185d8d30fSHans Petter Selasky * down, allocating a new send tag is not allowed. Else send 334285d8d30fSHans Petter Selasky * tags may leak. 334385d8d30fSHans Petter Selasky */ 334453af6903SGleb Smirnoff if (*st != NULL || (inp->inp_flags & INP_DROPPED) != 0) 3345f3e7afe2SHans Petter Selasky return (EINVAL); 3346f3e7afe2SHans Petter Selasky 334736e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, st); 3348903c4ee6SXin LI #ifdef INET 334920abea66SRandall Stewart if (error == 0) { 335020abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 335120abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 335236e0a362SJohn Baldwin } else if (error != EOPNOTSUPP) 335320abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3354903c4ee6SXin LI #endif 3355f3e7afe2SHans Petter Selasky return (error); 3356f3e7afe2SHans Petter Selasky } 3357f3e7afe2SHans Petter Selasky 335820abea66SRandall Stewart void 335998d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst) 336020abea66SRandall Stewart { 336120abea66SRandall Stewart 336298d7a8d9SJohn Baldwin m_snd_tag_rele(mst); 3363903c4ee6SXin LI #ifdef INET 336420abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3365903c4ee6SXin LI #endif 336620abea66SRandall Stewart } 336720abea66SRandall Stewart 3368f3e7afe2SHans Petter Selasky /* 3369f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3370f3e7afe2SHans Petter Selasky * if any: 3371f3e7afe2SHans Petter Selasky */ 3372f3e7afe2SHans Petter Selasky void 3373f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3374f3e7afe2SHans Petter Selasky { 3375f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3376f3e7afe2SHans Petter Selasky 3377f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3378f3e7afe2SHans Petter Selasky 3379f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3380f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3381f3e7afe2SHans Petter Selasky 3382f3e7afe2SHans Petter Selasky if (mst == NULL) 3383f3e7afe2SHans Petter Selasky return; 3384f3e7afe2SHans Petter Selasky 3385fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3386093e7231SHans Petter Selasky #ifdef INET 3387093e7231SHans Petter Selasky counter_u64_add(rate_limit_active, -1); 3388093e7231SHans Petter Selasky #endif 3389f3e7afe2SHans Petter Selasky } 3390f3e7afe2SHans Petter Selasky 339120abea66SRandall Stewart int 339220abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 339320abea66SRandall Stewart { 339420abea66SRandall Stewart int error; 339520abea66SRandall Stewart 339620abea66SRandall Stewart /* 339720abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 339820abea66SRandall Stewart * a route change, first drop the existing tag. Set the 339920abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 340020abea66SRandall Stewart * tag if we fail to allocate one this time. 340120abea66SRandall Stewart */ 340220abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 340320abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 340420abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 340520abea66SRandall Stewart } 340620abea66SRandall Stewart 340720abea66SRandall Stewart /* 340820abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 340920abea66SRandall Stewart * made to ensure the network interface doesn't go away until 341020abea66SRandall Stewart * all ratelimit connections are gone. The network interface 341120abea66SRandall Stewart * pointers compared below represent valid network interfaces, 341220abea66SRandall Stewart * except when comparing towards NULL. 341320abea66SRandall Stewart */ 341420abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 341520abea66SRandall Stewart error = 0; 341620abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 341720abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 341820abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 341920abea66SRandall Stewart error = 0; 342020abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 342120abea66SRandall Stewart /* 342220abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 342320abea66SRandall Stewart * to wait until there is a valid RSS hash before we 342420abea66SRandall Stewart * can proceed: 342520abea66SRandall Stewart */ 342620abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 342720abea66SRandall Stewart error = EAGAIN; 342820abea66SRandall Stewart } else { 342920abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 343020abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 343120abea66SRandall Stewart } 343220abea66SRandall Stewart } else { 343320abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 343420abea66SRandall Stewart } 343520abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 343620abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 343720abea66SRandall Stewart 343820abea66SRandall Stewart return (error); 343920abea66SRandall Stewart } 344020abea66SRandall Stewart 3441f3e7afe2SHans Petter Selasky /* 3442f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3443f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3444f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3445f3e7afe2SHans Petter Selasky */ 3446f3e7afe2SHans Petter Selasky void 3447f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3448f3e7afe2SHans Petter Selasky { 3449f3e7afe2SHans Petter Selasky struct socket *socket; 3450f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3451f3e7afe2SHans Petter Selasky bool did_upgrade; 3452f3e7afe2SHans Petter Selasky 3453f3e7afe2SHans Petter Selasky if (inp == NULL) 3454f3e7afe2SHans Petter Selasky return; 3455f3e7afe2SHans Petter Selasky 3456f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3457f3e7afe2SHans Petter Selasky if (socket == NULL) 3458f3e7afe2SHans Petter Selasky return; 3459f3e7afe2SHans Petter Selasky 3460f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3461f3e7afe2SHans Petter Selasky /* 3462f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3463f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3464f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3465f3e7afe2SHans Petter Selasky * write lock. 3466f3e7afe2SHans Petter Selasky */ 3467f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3468f3e7afe2SHans Petter Selasky return; 3469f3e7afe2SHans Petter Selasky did_upgrade = 1; 3470f3e7afe2SHans Petter Selasky } else { 3471f3e7afe2SHans Petter Selasky did_upgrade = 0; 3472f3e7afe2SHans Petter Selasky } 3473f3e7afe2SHans Petter Selasky 3474f3e7afe2SHans Petter Selasky /* 3475f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3476f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3477f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3478f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3479f3e7afe2SHans Petter Selasky */ 3480f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3481f3e7afe2SHans Petter Selasky 3482bab34d63SJohn Baldwin in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3483fb3bc596SJohn Baldwin 3484f3e7afe2SHans Petter Selasky if (did_upgrade) 3485f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3486f3e7afe2SHans Petter Selasky } 3487f3e7afe2SHans Petter Selasky 3488f3e7afe2SHans Petter Selasky /* 3489f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3490f3e7afe2SHans Petter Selasky */ 3491f3e7afe2SHans Petter Selasky void 3492f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3493f3e7afe2SHans Petter Selasky { 3494f3e7afe2SHans Petter Selasky bool did_upgrade; 3495f3e7afe2SHans Petter Selasky 3496f3e7afe2SHans Petter Selasky if (inp == NULL) 3497f3e7afe2SHans Petter Selasky return; 3498f3e7afe2SHans Petter Selasky 3499f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3500f3e7afe2SHans Petter Selasky return; 3501f3e7afe2SHans Petter Selasky 3502f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3503f3e7afe2SHans Petter Selasky /* 3504f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3505f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3506f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3507f3e7afe2SHans Petter Selasky * write lock. 3508f3e7afe2SHans Petter Selasky */ 3509f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3510f3e7afe2SHans Petter Selasky return; 3511f3e7afe2SHans Petter Selasky did_upgrade = 1; 3512f3e7afe2SHans Petter Selasky } else { 3513f3e7afe2SHans Petter Selasky did_upgrade = 0; 3514f3e7afe2SHans Petter Selasky } 3515f3e7afe2SHans Petter Selasky 3516f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3517f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3518f3e7afe2SHans Petter Selasky 3519f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3520f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3521f3e7afe2SHans Petter Selasky 3522f3e7afe2SHans Petter Selasky if (did_upgrade) 3523f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3524f3e7afe2SHans Petter Selasky } 352520abea66SRandall Stewart 3526903c4ee6SXin LI #ifdef INET 352720abea66SRandall Stewart static void 352820abea66SRandall Stewart rl_init(void *st) 352920abea66SRandall Stewart { 35301a714ff2SRandall Stewart rate_limit_new = counter_u64_alloc(M_WAITOK); 35311a714ff2SRandall Stewart rate_limit_chg = counter_u64_alloc(M_WAITOK); 353220abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 353320abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 353420abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 353520abea66SRandall Stewart } 353620abea66SRandall Stewart 353720abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3538903c4ee6SXin LI #endif 3539f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3540