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. 8497057eeSRobert Watson * All rights reserved. 9df8bae1dSRodney W. Grimes * 1079bdc6e5SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 1179bdc6e5SRobert Watson * contract to Juniper Networks, Inc. 1279bdc6e5SRobert Watson * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 22df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 23df8bae1dSRodney W. Grimes * without specific prior written permission. 24df8bae1dSRodney W. Grimes * 25df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35df8bae1dSRodney W. Grimes * SUCH DAMAGE. 36df8bae1dSRodney W. Grimes * 372469dd60SGarrett Wollman * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 38df8bae1dSRodney W. Grimes */ 39df8bae1dSRodney W. Grimes 404b421e2dSMike Silbersack #include <sys/cdefs.h> 414b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 424b421e2dSMike Silbersack 43497057eeSRobert Watson #include "opt_ddb.h" 446a800098SYoshinobu Inoue #include "opt_ipsec.h" 45efc76f72SBjoern A. Zeeb #include "opt_inet.h" 46cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 47f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h" 480c325f53SAlexander V. Chernikov #include "opt_route.h" 497527624eSRobert Watson #include "opt_rss.h" 50cfa1ca9dSYoshinobu Inoue 51df8bae1dSRodney W. Grimes #include <sys/param.h> 52df8bae1dSRodney W. Grimes #include <sys/systm.h> 53cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 54df8bae1dSRodney W. Grimes #include <sys/malloc.h> 55df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 56aae49dd3SBjoern A. Zeeb #include <sys/callout.h> 57ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h> 58cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 59df8bae1dSRodney W. Grimes #include <sys/protosw.h> 603ee9c3c4SRandall Stewart #include <sys/smp.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> 80cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 816d768226SGeorge V. Neville-Neil #include <net/if_llatbl.h> 82df8bae1dSRodney W. Grimes #include <net/route.h> 83b2bdc62aSAdrian Chadd #include <net/rss_config.h> 84530c0060SRobert Watson #include <net/vnet.h> 85df8bae1dSRodney W. Grimes 8667107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 87df8bae1dSRodney W. Grimes #include <netinet/in.h> 88df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 890f617ae4SGleb Smirnoff #include <netinet/in_pcb_var.h> 9059854ecfSHans Petter Selasky #ifdef INET 9159854ecfSHans Petter Selasky #include <netinet/in_var.h> 929ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h> 9359854ecfSHans Petter Selasky #endif 94df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 95340c35deSJonathan Lemon #include <netinet/tcp_var.h> 963ee9c3c4SRandall Stewart #ifdef TCPHPTS 973ee9c3c4SRandall Stewart #include <netinet/tcp_hpts.h> 983ee9c3c4SRandall Stewart #endif 995f311da2SMike Silbersack #include <netinet/udp.h> 1005f311da2SMike Silbersack #include <netinet/udp_var.h> 101cfa1ca9dSYoshinobu Inoue #ifdef INET6 102cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 103efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 10467107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 10567107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 106cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1079ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h> 10859854ecfSHans Petter Selasky #endif 109cfa1ca9dSYoshinobu Inoue 110fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 111b9234fafSSam Leffler 112aed55708SRobert Watson #include <security/mac/mac_framework.h> 113aed55708SRobert Watson 1141a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 1151a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 116db0ac6deSCy Schubert #define INP_FREED 0x00000200 /* See in_pcb.h. */ 1171a43cff9SSean Bruno 118aae49dd3SBjoern A. Zeeb static struct callout ipport_tick_callout; 119aae49dd3SBjoern A. Zeeb 120101f9fc8SPeter Wemm /* 121101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 122101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 123101f9fc8SPeter Wemm */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 130101f9fc8SPeter Wemm 131b0d22693SCrist J. Clark /* 132b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 133b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 134b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 135b0d22693SCrist J. Clark */ 136eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 137eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 138b0d22693SCrist J. Clark 1395f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 140eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */ 141eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */ 142eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */ 143eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */ 144eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs); 1455f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ipport_tcplastcount); 146eddfbb76SRobert Watson 1471e77c105SRobert Watson #define V_ipport_tcplastcount VNET(ipport_tcplastcount) 1486ac48b74SMike Silbersack 149d2025bd0SBjoern A. Zeeb #ifdef INET 150fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 151fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 152fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 153a034518aSAndrew Gallatin int lookupflags, struct ifnet *ifp, 154a034518aSAndrew Gallatin uint8_t numa_domain); 15567107f45SBjoern A. Zeeb 156bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 157bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 158bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 159bbd42ad0SPeter Wemm 160bbd42ad0SPeter Wemm static int 16182d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 162bbd42ad0SPeter Wemm { 16330a4ab08SBruce Evans int error; 16430a4ab08SBruce Evans 165f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 16630a4ab08SBruce Evans if (error == 0) { 167603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 168603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 169603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 170603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 171603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 172603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 173bbd42ad0SPeter Wemm } 17430a4ab08SBruce Evans return (error); 175bbd42ad0SPeter Wemm } 176bbd42ad0SPeter Wemm 177bbd42ad0SPeter Wemm #undef RANGECHK 178bbd42ad0SPeter Wemm 1797029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, 1807029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1816472ac3dSEd Schouten "IP Ports"); 18233b3ac06SPeter Wemm 1836df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1847029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1857029da5cSPawel Biernacki &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", 1867029da5cSPawel Biernacki ""); 1876df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1887029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1897029da5cSPawel Biernacki &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", 1907029da5cSPawel Biernacki ""); 1916df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1927029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1937029da5cSPawel Biernacki &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", 1947029da5cSPawel Biernacki ""); 1956df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1967029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1977029da5cSPawel Biernacki &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", 1987029da5cSPawel Biernacki ""); 1996df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 2007029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 2017029da5cSPawel Biernacki &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", 2027029da5cSPawel Biernacki ""); 2036df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 2047029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 2057029da5cSPawel Biernacki &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", 2067029da5cSPawel Biernacki ""); 2076df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 2086df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 2096df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 2106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 211eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 2126df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 2136df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 214eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 2156df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, 2166df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 217eddfbb76SRobert Watson &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port " 21827c4abc7SGordon Bergling "allocations before switching to a sequential one"); 2196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, 2206df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 221eddfbb76SRobert Watson &VNET_NAME(ipport_randomtime), 0, 22227c4abc7SGordon Bergling "Minimum time to keep sequential port " 2236ee79c59SMaxim Konovalov "allocation before switching to a random one"); 22420abea66SRandall Stewart 22520abea66SRandall Stewart #ifdef RATELIMIT 2261a714ff2SRandall Stewart counter_u64_t rate_limit_new; 2271a714ff2SRandall Stewart counter_u64_t rate_limit_chg; 22820abea66SRandall Stewart counter_u64_t rate_limit_active; 22920abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail; 23020abea66SRandall Stewart counter_u64_t rate_limit_set_ok; 23120abea66SRandall Stewart 2327029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 23320abea66SRandall Stewart "IP Rate Limiting"); 23420abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD, 23520abea66SRandall Stewart &rate_limit_active, "Active rate limited connections"); 23620abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD, 23720abea66SRandall Stewart &rate_limit_alloc_fail, "Rate limited connection failures"); 23820abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD, 23920abea66SRandall Stewart &rate_limit_set_ok, "Rate limited setting succeeded"); 2401a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD, 2411a714ff2SRandall Stewart &rate_limit_new, "Total Rate limit new attempts"); 2421a714ff2SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD, 2431a714ff2SRandall Stewart &rate_limit_chg, "Total Rate limited change attempts"); 2441a714ff2SRandall Stewart 24520abea66SRandall Stewart #endif /* RATELIMIT */ 24620abea66SRandall Stewart 24783e521ecSBjoern A. Zeeb #endif /* INET */ 2480312fbe9SPoul-Henning Kamp 249c3229e05SDavid Greenman /* 250c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 251c3229e05SDavid Greenman * 252de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 253de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 254de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 255c3229e05SDavid Greenman */ 256c3229e05SDavid Greenman 2571a43cff9SSean Bruno static struct inpcblbgroup * 2581a43cff9SSean Bruno in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, u_char vflag, 259a034518aSAndrew Gallatin uint16_t port, const union in_dependaddr *addr, int size, 260a034518aSAndrew Gallatin uint8_t numa_domain) 2611a43cff9SSean Bruno { 2621a43cff9SSean Bruno struct inpcblbgroup *grp; 2631a43cff9SSean Bruno size_t bytes; 2641a43cff9SSean Bruno 2651a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 2661a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 2671a43cff9SSean Bruno if (!grp) 2681a43cff9SSean Bruno return (NULL); 2691a43cff9SSean Bruno grp->il_vflag = vflag; 2701a43cff9SSean Bruno grp->il_lport = port; 271a034518aSAndrew Gallatin grp->il_numa_domain = numa_domain; 2721a43cff9SSean Bruno grp->il_dependladdr = *addr; 2731a43cff9SSean Bruno grp->il_inpsiz = size; 27454af3d0dSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 2751a43cff9SSean Bruno return (grp); 2761a43cff9SSean Bruno } 2771a43cff9SSean Bruno 2781a43cff9SSean Bruno static void 27954af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx) 28054af3d0dSMark Johnston { 28154af3d0dSMark Johnston struct inpcblbgroup *grp; 28254af3d0dSMark Johnston 28354af3d0dSMark Johnston grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 28454af3d0dSMark Johnston free(grp, M_PCB); 28554af3d0dSMark Johnston } 28654af3d0dSMark Johnston 28754af3d0dSMark Johnston static void 2881a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 2891a43cff9SSean Bruno { 2901a43cff9SSean Bruno 29154af3d0dSMark Johnston CK_LIST_REMOVE(grp, il_list); 2922a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx); 2931a43cff9SSean Bruno } 2941a43cff9SSean Bruno 2951a43cff9SSean Bruno static struct inpcblbgroup * 2961a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 2971a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 2981a43cff9SSean Bruno { 2991a43cff9SSean Bruno struct inpcblbgroup *grp; 3001a43cff9SSean Bruno int i; 3011a43cff9SSean Bruno 3021a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, old_grp->il_vflag, 303a034518aSAndrew Gallatin old_grp->il_lport, &old_grp->il_dependladdr, size, 304a034518aSAndrew Gallatin old_grp->il_numa_domain); 30579ee680bSMark Johnston if (grp == NULL) 3061a43cff9SSean Bruno return (NULL); 3071a43cff9SSean Bruno 3081a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 3091a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 3101a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 3111a43cff9SSean Bruno 3121a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 3131a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 3141a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 3151a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 3161a43cff9SSean Bruno return (grp); 3171a43cff9SSean Bruno } 3181a43cff9SSean Bruno 3191a43cff9SSean Bruno /* 3201a43cff9SSean Bruno * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 3211a43cff9SSean Bruno * and shrink group if possible. 3221a43cff9SSean Bruno */ 3231a43cff9SSean Bruno static void 3241a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 3251a43cff9SSean Bruno int i) 3261a43cff9SSean Bruno { 32779ee680bSMark Johnston struct inpcblbgroup *grp, *new_grp; 3281a43cff9SSean Bruno 32979ee680bSMark Johnston grp = *grpp; 3301a43cff9SSean Bruno for (; i + 1 < grp->il_inpcnt; ++i) 3311a43cff9SSean Bruno grp->il_inp[i] = grp->il_inp[i + 1]; 3321a43cff9SSean Bruno grp->il_inpcnt--; 3331a43cff9SSean Bruno 3341a43cff9SSean Bruno if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 33579ee680bSMark Johnston grp->il_inpcnt <= grp->il_inpsiz / 4) { 3361a43cff9SSean Bruno /* Shrink this group. */ 33779ee680bSMark Johnston new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 33879ee680bSMark Johnston if (new_grp != NULL) 3391a43cff9SSean Bruno *grpp = new_grp; 3401a43cff9SSean Bruno } 3411a43cff9SSean Bruno } 3421a43cff9SSean Bruno 3431a43cff9SSean Bruno /* 3441a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 3451a43cff9SSean Bruno */ 3461a43cff9SSean Bruno static int 347a034518aSAndrew Gallatin in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain) 3481a43cff9SSean Bruno { 349a7026c7fSMark Johnston const static struct timeval interval = { 60, 0 }; 350a7026c7fSMark Johnston static struct timeval lastprint; 3511a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3521a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3531a43cff9SSean Bruno struct inpcblbgroup *grp; 35479ee680bSMark Johnston uint32_t idx; 3551a43cff9SSean Bruno 3561a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 3571a43cff9SSean Bruno 3581a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 3591a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 3601a43cff9SSean Bruno 3611a43cff9SSean Bruno /* 3621a43cff9SSean Bruno * Don't allow jailed socket to join local group. 3631a43cff9SSean Bruno */ 36479ee680bSMark Johnston if (inp->inp_socket != NULL && jailed(inp->inp_socket->so_cred)) 3651a43cff9SSean Bruno return (0); 3661a43cff9SSean Bruno 3671a43cff9SSean Bruno #ifdef INET6 3681a43cff9SSean Bruno /* 3691a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 3701a43cff9SSean Bruno */ 3711a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 3721a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 3731a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 3741a43cff9SSean Bruno return (0); 3751a43cff9SSean Bruno } 3761a43cff9SSean Bruno #endif 3771a43cff9SSean Bruno 3789d2877fcSMark Johnston idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask); 37979ee680bSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 38054af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 3811a43cff9SSean Bruno if (grp->il_vflag == inp->inp_vflag && 3821a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 383a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain && 3841a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 3851a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 38679ee680bSMark Johnston sizeof(grp->il_dependladdr)) == 0) 3871a43cff9SSean Bruno break; 3881a43cff9SSean Bruno } 3891a43cff9SSean Bruno if (grp == NULL) { 3901a43cff9SSean Bruno /* Create new load balance group. */ 3911a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, inp->inp_vflag, 3921a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 393a034518aSAndrew Gallatin INPCBLBGROUP_SIZMIN, numa_domain); 39479ee680bSMark Johnston if (grp == NULL) 3951a43cff9SSean Bruno return (ENOBUFS); 3961a43cff9SSean Bruno } else if (grp->il_inpcnt == grp->il_inpsiz) { 3971a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 398a7026c7fSMark Johnston if (ratecheck(&lastprint, &interval)) 3991a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 4001a43cff9SSean Bruno ntohs(grp->il_lport)); 4011a43cff9SSean Bruno return (0); 4021a43cff9SSean Bruno } 4031a43cff9SSean Bruno 4041a43cff9SSean Bruno /* Expand this local group. */ 4051a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 40679ee680bSMark Johnston if (grp == NULL) 4071a43cff9SSean Bruno return (ENOBUFS); 4081a43cff9SSean Bruno } 4091a43cff9SSean Bruno 4101a43cff9SSean Bruno KASSERT(grp->il_inpcnt < grp->il_inpsiz, 41179ee680bSMark Johnston ("invalid local group size %d and count %d", grp->il_inpsiz, 41279ee680bSMark Johnston grp->il_inpcnt)); 4131a43cff9SSean Bruno 4141a43cff9SSean Bruno grp->il_inp[grp->il_inpcnt] = inp; 4151a43cff9SSean Bruno grp->il_inpcnt++; 4161a43cff9SSean Bruno return (0); 4171a43cff9SSean Bruno } 4181a43cff9SSean Bruno 4191a43cff9SSean Bruno /* 4201a43cff9SSean Bruno * Remove PCB from load balance group. 4211a43cff9SSean Bruno */ 4221a43cff9SSean Bruno static void 4231a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 4241a43cff9SSean Bruno { 4251a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 4261a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 4271a43cff9SSean Bruno struct inpcblbgroup *grp; 4281a43cff9SSean Bruno int i; 4291a43cff9SSean Bruno 4301a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 4311a43cff9SSean Bruno 4321a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 4331a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 4341a43cff9SSean Bruno 4351a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 4369d2877fcSMark Johnston INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 43754af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 4381a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 4391a43cff9SSean Bruno if (grp->il_inp[i] != inp) 4401a43cff9SSean Bruno continue; 4411a43cff9SSean Bruno 4421a43cff9SSean Bruno if (grp->il_inpcnt == 1) { 4431a43cff9SSean Bruno /* We are the last, free this local group. */ 4441a43cff9SSean Bruno in_pcblbgroup_free(grp); 4451a43cff9SSean Bruno } else { 4461a43cff9SSean Bruno /* Pull up inpcbs, shrink group if possible. */ 4471a43cff9SSean Bruno in_pcblbgroup_reorder(hdr, &grp, i); 4481a43cff9SSean Bruno } 4491a43cff9SSean Bruno return; 4501a43cff9SSean Bruno } 4511a43cff9SSean Bruno } 4521a43cff9SSean Bruno } 4531a43cff9SSean Bruno 454a034518aSAndrew Gallatin int 455a034518aSAndrew Gallatin in_pcblbgroup_numa(struct inpcb *inp, int arg) 456a034518aSAndrew Gallatin { 457a034518aSAndrew Gallatin struct inpcbinfo *pcbinfo; 458a034518aSAndrew Gallatin struct inpcblbgrouphead *hdr; 459a034518aSAndrew Gallatin struct inpcblbgroup *grp; 460a034518aSAndrew Gallatin int err, i; 461a034518aSAndrew Gallatin uint8_t numa_domain; 462a034518aSAndrew Gallatin 463a034518aSAndrew Gallatin switch (arg) { 464a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_NODOM: 465a034518aSAndrew Gallatin numa_domain = M_NODOM; 466a034518aSAndrew Gallatin break; 467a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA_CURDOM: 468a034518aSAndrew Gallatin numa_domain = PCPU_GET(domain); 469a034518aSAndrew Gallatin break; 470a034518aSAndrew Gallatin default: 471a034518aSAndrew Gallatin if (arg < 0 || arg >= vm_ndomains) 472a034518aSAndrew Gallatin return (EINVAL); 473a034518aSAndrew Gallatin numa_domain = arg; 474a034518aSAndrew Gallatin } 475a034518aSAndrew Gallatin 476a034518aSAndrew Gallatin err = 0; 477a034518aSAndrew Gallatin pcbinfo = inp->inp_pcbinfo; 478a034518aSAndrew Gallatin INP_WLOCK_ASSERT(inp); 479a034518aSAndrew Gallatin INP_HASH_WLOCK(pcbinfo); 480a034518aSAndrew Gallatin hdr = &pcbinfo->ipi_lbgrouphashbase[ 481a034518aSAndrew Gallatin INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 482a034518aSAndrew Gallatin CK_LIST_FOREACH(grp, hdr, il_list) { 483a034518aSAndrew Gallatin for (i = 0; i < grp->il_inpcnt; ++i) { 484a034518aSAndrew Gallatin if (grp->il_inp[i] != inp) 485a034518aSAndrew Gallatin continue; 486a034518aSAndrew Gallatin 487a034518aSAndrew Gallatin if (grp->il_numa_domain == numa_domain) { 488a034518aSAndrew Gallatin goto abort_with_hash_wlock; 489a034518aSAndrew Gallatin } 490a034518aSAndrew Gallatin 491a034518aSAndrew Gallatin /* Remove it from the old group. */ 492a034518aSAndrew Gallatin in_pcbremlbgrouphash(inp); 493a034518aSAndrew Gallatin 494a034518aSAndrew Gallatin /* Add it to the new group based on numa domain. */ 495a034518aSAndrew Gallatin in_pcbinslbgrouphash(inp, numa_domain); 496a034518aSAndrew Gallatin goto abort_with_hash_wlock; 497a034518aSAndrew Gallatin } 498a034518aSAndrew Gallatin } 499a034518aSAndrew Gallatin err = ENOENT; 500a034518aSAndrew Gallatin abort_with_hash_wlock: 501a034518aSAndrew Gallatin INP_HASH_WUNLOCK(pcbinfo); 502a034518aSAndrew Gallatin return (err); 503a034518aSAndrew Gallatin } 504a034518aSAndrew Gallatin 505db0ac6deSCy Schubert /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 506db0ac6deSCy Schubert CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb))); 507db0ac6deSCy Schubert 508cc487c16SGleb Smirnoff /* 5099bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 5109bcd427bSRobert Watson * arguments in time. 5119bcd427bSRobert Watson */ 512*eb93b99dSGleb Smirnoff static void inpcb_dtor(void *, int, void *); 513*eb93b99dSGleb Smirnoff static void inpcb_fini(void *, int); 5149bcd427bSRobert Watson void 5159bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 516db0ac6deSCy Schubert u_int hash_nelements, int porthash_nelements, char *inpcbzone_name, 517db0ac6deSCy Schubert uma_init inpcbzone_init) 5189bcd427bSRobert Watson { 5199bcd427bSRobert Watson 520db0ac6deSCy Schubert mtx_init(&pcbinfo->ipi_lock, name, NULL, MTX_DEF); 521db0ac6deSCy Schubert mtx_init(&pcbinfo->ipi_hash_lock, "pcbinfohash", NULL, MTX_DEF); 5229bcd427bSRobert Watson #ifdef VIMAGE 5239bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 5249bcd427bSRobert Watson #endif 525db0ac6deSCy Schubert CK_LIST_INIT(&pcbinfo->ipi_listhead); 526fa046d87SRobert Watson pcbinfo->ipi_count = 0; 5279bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 5289bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 529db0ac6deSCy Schubert porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 5309bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 5319bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 5329d2877fcSMark Johnston pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 5331a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 5349bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 535*eb93b99dSGleb Smirnoff NULL, inpcb_dtor, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 536db0ac6deSCy Schubert UMA_ZONE_SMR); 5379bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 5386acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 5396acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 540db0ac6deSCy Schubert pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone); 541db0ac6deSCy Schubert pcbinfo->ipi_portzone = uma_zcreate(inpcbzone_name, 542db0ac6deSCy Schubert sizeof(struct inpcbport), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 543db0ac6deSCy Schubert uma_zone_set_smr(pcbinfo->ipi_portzone, pcbinfo->ipi_smr); 5449bcd427bSRobert Watson } 5459bcd427bSRobert Watson 5469bcd427bSRobert Watson /* 5479bcd427bSRobert Watson * Destroy an inpcbinfo. 5489bcd427bSRobert Watson */ 5499bcd427bSRobert Watson void 5509bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 5519bcd427bSRobert Watson { 5529bcd427bSRobert Watson 553fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 554fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 555fa046d87SRobert Watson 5569bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 5579bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 5589bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 5591a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 5601a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 5619bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 562db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_hash_lock); 563db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_lock); 5649bcd427bSRobert Watson } 5659bcd427bSRobert Watson 5669bcd427bSRobert Watson /* 567c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 568d915b280SStephan Uphoff * On success return with the PCB locked. 569c3229e05SDavid Greenman */ 570df8bae1dSRodney W. Grimes int 571d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 572df8bae1dSRodney W. Grimes { 573136d4f1cSRobert Watson struct inpcb *inp; 57413cf67f3SHajimu UMEMOTO int error; 575a557af22SRobert Watson 576a557af22SRobert Watson error = 0; 577db0ac6deSCy Schubert inp = uma_zalloc_smr(pcbinfo->ipi_zone, M_NOWAIT); 578df8bae1dSRodney W. Grimes if (inp == NULL) 579df8bae1dSRodney W. Grimes return (ENOBUFS); 5806a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 58150575ce1SAndrew Gallatin #ifdef NUMA 58250575ce1SAndrew Gallatin inp->inp_numa_domain = M_NODOM; 58350575ce1SAndrew Gallatin #endif 58415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 585df8bae1dSRodney W. Grimes inp->inp_socket = so; 58686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 5878b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 588a557af22SRobert Watson #ifdef MAC 58930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 590a557af22SRobert Watson if (error != 0) 591a557af22SRobert Watson goto out; 59230d239bcSRobert Watson mac_inpcb_create(so, inp); 593a557af22SRobert Watson #endif 594fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 595fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 5960bffde27SRobert Watson if (error != 0) { 5970bffde27SRobert Watson #ifdef MAC 5980bffde27SRobert Watson mac_inpcb_destroy(inp); 5990bffde27SRobert Watson #endif 600a557af22SRobert Watson goto out; 6010bffde27SRobert Watson } 602b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 603e3fd5ffdSRobert Watson #ifdef INET6 604340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 605340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 606603724d3SBjoern A. Zeeb if (V_ip6_v6only) 60733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 608340c35deSJonathan Lemon } 609603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 61033841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 61133841545SHajimu UMEMOTO #endif 6128c1960d5SMike Karels /* 6138c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 6148c1960d5SMike Karels * to be cleaned up. 6158c1960d5SMike Karels */ 6168c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 617db0ac6deSCy Schubert #ifdef TCPHPTS 618db0ac6deSCy Schubert /* 619db0ac6deSCy Schubert * If using hpts lets drop a random number in so 620db0ac6deSCy Schubert * not all new connections fall on the same CPU. 621db0ac6deSCy Schubert */ 622db0ac6deSCy Schubert inp->inp_hpts_cpu = inp->inp_dropq_cpu = hpts_random_cpu(inp); 623db0ac6deSCy Schubert #endif 624db0ac6deSCy Schubert refcount_init(&inp->inp_refcount, 1); /* Reference from socket. */ 625db0ac6deSCy Schubert INP_WLOCK(inp); 626db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 627db0ac6deSCy Schubert pcbinfo->ipi_count++; 628db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 629db0ac6deSCy Schubert CK_LIST_INSERT_HEAD(&pcbinfo->ipi_listhead, inp, inp_list); 630db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 631db0ac6deSCy Schubert so->so_pcb = inp; 632db0ac6deSCy Schubert 633db0ac6deSCy Schubert return (0); 634db0ac6deSCy Schubert 6357a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 636a557af22SRobert Watson out: 637db0ac6deSCy Schubert uma_zfree_smr(pcbinfo->ipi_zone, inp); 638266f97b5SCy Schubert return (error); 639db0ac6deSCy Schubert #endif 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes 64267107f45SBjoern A. Zeeb #ifdef INET 643df8bae1dSRodney W. Grimes int 644136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 645df8bae1dSRodney W. Grimes { 6464b932371SIan Dowse int anonport, error; 6474b932371SIan Dowse 648f161d294SMark Johnston KASSERT(nam == NULL || nam->sa_family == AF_INET, 649f161d294SMark Johnston ("%s: invalid address family for %p", __func__, nam)); 650f161d294SMark Johnston KASSERT(nam == NULL || nam->sa_len == sizeof(struct sockaddr_in), 651f161d294SMark Johnston ("%s: invalid address length for %p", __func__, nam)); 6528501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 653fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 65459daba27SSam Leffler 6554b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 6564b932371SIan Dowse return (EINVAL); 6575cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 6584b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 659b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 6604b932371SIan Dowse if (error) 6614b932371SIan Dowse return (error); 6624b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 6634b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 6644b932371SIan Dowse inp->inp_lport = 0; 6654b932371SIan Dowse return (EAGAIN); 6664b932371SIan Dowse } 6674b932371SIan Dowse if (anonport) 6684b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 6694b932371SIan Dowse return (0); 6704b932371SIan Dowse } 67167107f45SBjoern A. Zeeb #endif 6724b932371SIan Dowse 673efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 67425102351SMike Karels /* 67525102351SMike Karels * Assign a local port like in_pcb_lport(), but also used with connect() 67625102351SMike Karels * and a foreign address and port. If fsa is non-NULL, choose a local port 67725102351SMike Karels * that is unused with those, otherwise one that is completely unused. 6782fdbcbeaSMike Karels * lsa can be NULL for IPv6. 67925102351SMike Karels */ 680efc76f72SBjoern A. Zeeb int 68125102351SMike Karels in_pcb_lport_dest(struct inpcb *inp, struct sockaddr *lsa, u_short *lportp, 68225102351SMike Karels struct sockaddr *fsa, u_short fport, struct ucred *cred, int lookupflags) 683efc76f72SBjoern A. Zeeb { 684efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 685efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 686efc76f72SBjoern A. Zeeb unsigned short *lastport; 687efc76f72SBjoern A. Zeeb int count, dorandom, error; 688efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 689efc76f72SBjoern A. Zeeb #ifdef INET 69025102351SMike Karels struct in_addr laddr, faddr; 69125102351SMike Karels #endif 69225102351SMike Karels #ifdef INET6 69325102351SMike Karels struct in6_addr *laddr6, *faddr6; 694efc76f72SBjoern A. Zeeb #endif 695efc76f72SBjoern A. Zeeb 696efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 697efc76f72SBjoern A. Zeeb 698efc76f72SBjoern A. Zeeb /* 699efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 700efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 701efc76f72SBjoern A. Zeeb */ 702efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 703fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 704efc76f72SBjoern A. Zeeb 705efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 706efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 707efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 708efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 709efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 710cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 711efc76f72SBjoern A. Zeeb if (error) 712efc76f72SBjoern A. Zeeb return (error); 713efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 714efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 715efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 716efc76f72SBjoern A. Zeeb } else { 717efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 718efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 719efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 720efc76f72SBjoern A. Zeeb } 721efc76f72SBjoern A. Zeeb /* 722e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 723efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 724efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 725efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 726efc76f72SBjoern A. Zeeb */ 727efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 728e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 729e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 730efc76f72SBjoern A. Zeeb dorandom = 1; 731efc76f72SBjoern A. Zeeb else 732efc76f72SBjoern A. Zeeb dorandom = 0; 733efc76f72SBjoern A. Zeeb /* 734efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 735efc76f72SBjoern A. Zeeb * we have the only port available. 736efc76f72SBjoern A. Zeeb */ 737efc76f72SBjoern A. Zeeb if (first == last) 738efc76f72SBjoern A. Zeeb dorandom = 0; 739e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 74013e3f334SPeter Lei if (pcbinfo != &V_udbinfo && pcbinfo != &V_ulitecbinfo) 741efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 742efc76f72SBjoern A. Zeeb /* 743efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 744efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 745efc76f72SBjoern A. Zeeb * code path implementing all logic. 746efc76f72SBjoern A. Zeeb */ 747efc76f72SBjoern A. Zeeb if (first > last) { 748efc76f72SBjoern A. Zeeb aux = first; 749efc76f72SBjoern A. Zeeb first = last; 750efc76f72SBjoern A. Zeeb last = aux; 751efc76f72SBjoern A. Zeeb } 752efc76f72SBjoern A. Zeeb 753efc76f72SBjoern A. Zeeb #ifdef INET 75425102351SMike Karels laddr.s_addr = INADDR_ANY; 7554d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 7562fdbcbeaSMike Karels if (lsa != NULL) 75725102351SMike Karels laddr = ((struct sockaddr_in *)lsa)->sin_addr; 75825102351SMike Karels if (fsa != NULL) 75925102351SMike Karels faddr = ((struct sockaddr_in *)fsa)->sin_addr; 760efc76f72SBjoern A. Zeeb } 761efc76f72SBjoern A. Zeeb #endif 76225102351SMike Karels #ifdef INET6 7632fdbcbeaSMike Karels laddr6 = NULL; 7642fdbcbeaSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 7652fdbcbeaSMike Karels if (lsa != NULL) 76625102351SMike Karels laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 76725102351SMike Karels if (fsa != NULL) 76825102351SMike Karels faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 76925102351SMike Karels } 77025102351SMike Karels #endif 77125102351SMike Karels 77225102351SMike Karels tmpinp = NULL; 773efc76f72SBjoern A. Zeeb lport = *lportp; 774efc76f72SBjoern A. Zeeb 775efc76f72SBjoern A. Zeeb if (dorandom) 776efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 777efc76f72SBjoern A. Zeeb 778efc76f72SBjoern A. Zeeb count = last - first; 779efc76f72SBjoern A. Zeeb 780efc76f72SBjoern A. Zeeb do { 781efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 782efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 783efc76f72SBjoern A. Zeeb ++*lastport; 784efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 785efc76f72SBjoern A. Zeeb *lastport = first; 786efc76f72SBjoern A. Zeeb lport = htons(*lastport); 787efc76f72SBjoern A. Zeeb 78825102351SMike Karels if (fsa != NULL) { 78925102351SMike Karels #ifdef INET 79025102351SMike Karels if (lsa->sa_family == AF_INET) { 79125102351SMike Karels tmpinp = in_pcblookup_hash_locked(pcbinfo, 79225102351SMike Karels faddr, fport, laddr, lport, lookupflags, 793a034518aSAndrew Gallatin NULL, M_NODOM); 79425102351SMike Karels } 79525102351SMike Karels #endif 79625102351SMike Karels #ifdef INET6 79725102351SMike Karels if (lsa->sa_family == AF_INET6) { 79825102351SMike Karels tmpinp = in6_pcblookup_hash_locked(pcbinfo, 79925102351SMike Karels faddr6, fport, laddr6, lport, lookupflags, 800a034518aSAndrew Gallatin NULL, M_NODOM); 80125102351SMike Karels } 80225102351SMike Karels #endif 80325102351SMike Karels } else { 804efc76f72SBjoern A. Zeeb #ifdef INET6 8054616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 806efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 80768e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 808efc76f72SBjoern A. Zeeb #endif 809efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 810efc76f72SBjoern A. Zeeb else 811efc76f72SBjoern A. Zeeb #endif 812efc76f72SBjoern A. Zeeb #ifdef INET 813efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 81468e0d7e0SRobert Watson lport, lookupflags, cred); 815efc76f72SBjoern A. Zeeb #endif 81625102351SMike Karels } 817efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 818efc76f72SBjoern A. Zeeb 819efc76f72SBjoern A. Zeeb *lportp = lport; 820efc76f72SBjoern A. Zeeb 821efc76f72SBjoern A. Zeeb return (0); 822efc76f72SBjoern A. Zeeb } 823efdf104bSMikolaj Golub 824efdf104bSMikolaj Golub /* 82525102351SMike Karels * Select a local port (number) to use. 82625102351SMike Karels */ 82725102351SMike Karels int 82825102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 82925102351SMike Karels struct ucred *cred, int lookupflags) 83025102351SMike Karels { 83125102351SMike Karels struct sockaddr_in laddr; 83225102351SMike Karels 83325102351SMike Karels if (laddrp) { 83425102351SMike Karels bzero(&laddr, sizeof(laddr)); 83525102351SMike Karels laddr.sin_family = AF_INET; 83625102351SMike Karels laddr.sin_addr = *laddrp; 83725102351SMike Karels } 83825102351SMike Karels return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 83925102351SMike Karels NULL, lportp, NULL, 0, cred, lookupflags)); 84025102351SMike Karels } 84125102351SMike Karels 84225102351SMike Karels /* 843efdf104bSMikolaj Golub * Return cached socket options. 844efdf104bSMikolaj Golub */ 8451a43cff9SSean Bruno int 846efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 847efdf104bSMikolaj Golub { 8481a43cff9SSean Bruno int so_options; 849efdf104bSMikolaj Golub 850efdf104bSMikolaj Golub so_options = 0; 851efdf104bSMikolaj Golub 8521a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 8531a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 854efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 855efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 856efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 857efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 858efdf104bSMikolaj Golub return (so_options); 859efdf104bSMikolaj Golub } 860efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 861efc76f72SBjoern A. Zeeb 8624b932371SIan Dowse /* 8630a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 8640a100a6fSAdrian Chadd * 8650a100a6fSAdrian Chadd * ni points to the new inp. 8660a100a6fSAdrian Chadd * oi points to the exisitng inp. 8670a100a6fSAdrian Chadd * 8680a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 8690a100a6fSAdrian Chadd * whether the credentials match. 8700a100a6fSAdrian Chadd */ 871d5bb8bd3SAdrian Chadd int 8720a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 8730a100a6fSAdrian Chadd { 8740a100a6fSAdrian Chadd /* Check permissions match */ 8750a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8760a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 8770a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 8780a100a6fSAdrian Chadd return (0); 8790a100a6fSAdrian Chadd 8800a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 8810a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8820a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 8830a100a6fSAdrian Chadd return (0); 8840a100a6fSAdrian Chadd 8850a100a6fSAdrian Chadd /* 8860a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 8870a100a6fSAdrian Chadd * it is and it matches the checks. 8880a100a6fSAdrian Chadd */ 8890a100a6fSAdrian Chadd return (1); 8900a100a6fSAdrian Chadd } 8910a100a6fSAdrian Chadd 892d5bb8bd3SAdrian Chadd #ifdef INET 8930a100a6fSAdrian Chadd /* 8944b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 8954b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 8964b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 8974b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 8984b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 8994b932371SIan Dowse * 9004b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 9014b932371SIan Dowse */ 9024b932371SIan Dowse int 903136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 904136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 9054b932371SIan Dowse { 9064b932371SIan Dowse struct socket *so = inp->inp_socket; 90715bd2b43SDavid Greenman struct sockaddr_in *sin; 908c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 9094b932371SIan Dowse struct in_addr laddr; 910df8bae1dSRodney W. Grimes u_short lport = 0; 91168e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 912413628a7SBjoern A. Zeeb int error; 913df8bae1dSRodney W. Grimes 9148501a69cSRobert Watson /* 9151a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 9161a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 9171a43cff9SSean Bruno */ 9181a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 9191a43cff9SSean Bruno 9201a43cff9SSean Bruno /* 921fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 9228501a69cSRobert Watson */ 92359daba27SSam Leffler INP_LOCK_ASSERT(inp); 924fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 92559daba27SSam Leffler 9264b932371SIan Dowse laddr.s_addr = *laddrp; 9274b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 928df8bae1dSRodney W. Grimes return (EINVAL); 9291a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 93068e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 9314616026fSErmal Luçi if (nam == NULL) { 9327c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 9337c2f3cb9SJamie Gritton return (error); 9347c2f3cb9SJamie Gritton } else { 93557bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 936f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 937f161d294SMark Johnston ("%s: invalid family for address %p", __func__, sin)); 938f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 939f161d294SMark Johnston ("%s: invalid length for address %p", __func__, sin)); 940f161d294SMark Johnston 941b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 942b89e82ddSJamie Gritton if (error) 943b89e82ddSJamie Gritton return (error); 9444b932371SIan Dowse if (sin->sin_port != *lportp) { 9454b932371SIan Dowse /* Don't allow the port to change. */ 9464b932371SIan Dowse if (*lportp != 0) 9474b932371SIan Dowse return (EINVAL); 948df8bae1dSRodney W. Grimes lport = sin->sin_port; 9494b932371SIan Dowse } 9504b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 951df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 952df8bae1dSRodney W. Grimes /* 953df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 954df8bae1dSRodney W. Grimes * allow complete duplication of binding if 955df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 956df8bae1dSRodney W. Grimes * and a multicast address is bound on both 957df8bae1dSRodney W. Grimes * new and duplicated sockets. 958df8bae1dSRodney W. Grimes */ 959f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 960df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 9611a43cff9SSean Bruno /* 9621a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 9631a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 9641a43cff9SSean Bruno */ 9651a43cff9SSean Bruno if ((so->so_options & 9661a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 9671a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 968df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 969df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 97083103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 9714209e01aSAdrian Chadd /* 9724209e01aSAdrian Chadd * Is the address a local IP address? 973f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 9748696873dSAdrian Chadd * to any endpoint address, local or not. 9754209e01aSAdrian Chadd */ 976f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 9778896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 978df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 979df8bae1dSRodney W. Grimes } 9804b932371SIan Dowse laddr = sin->sin_addr; 981df8bae1dSRodney W. Grimes if (lport) { 982df8bae1dSRodney W. Grimes struct inpcb *t; 983ae0e7143SRobert Watson struct tcptw *tw; 984ae0e7143SRobert Watson 985df8bae1dSRodney W. Grimes /* GROSS */ 986603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 987603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 988cc426dd3SMateusz Guzik priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 9892469dd60SGarrett Wollman return (EACCES); 990835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 991cc426dd3SMateusz Guzik priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 992078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 993413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 994340c35deSJonathan Lemon /* 995340c35deSJonathan Lemon * XXX 996340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 997340c35deSJonathan Lemon */ 9984cc20ab1SSeigo Tanimura if (t && 9990a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 1000ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 10014658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 10024658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 10034cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 100452b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 10051a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 10061a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 100786d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 100886d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 10094049a042SGuido van Rooij return (EADDRINUSE); 10100a100a6fSAdrian Chadd 10110a100a6fSAdrian Chadd /* 10120a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 10130a100a6fSAdrian Chadd * the credentials need to match and the 10140a100a6fSAdrian Chadd * original socket also has to have been bound 10150a100a6fSAdrian Chadd * with BINDMULTI. 10160a100a6fSAdrian Chadd */ 10170a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 10180a100a6fSAdrian Chadd return (EADDRINUSE); 10194049a042SGuido van Rooij } 1020c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 102168e0d7e0SRobert Watson lport, lookupflags, cred); 1022ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 1023ae0e7143SRobert Watson /* 1024ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 1025ae0e7143SRobert Watson * state recycled, we treat the address as 1026ae0e7143SRobert Watson * being in use (for now). This is better 1027ae0e7143SRobert Watson * than a panic, but not desirable. 1028ae0e7143SRobert Watson */ 1029ec95b709SMikolaj Golub tw = intotw(t); 1030ae0e7143SRobert Watson if (tw == NULL || 10311a43cff9SSean Bruno ((reuseport & tw->tw_so_options) == 0 && 10321a43cff9SSean Bruno (reuseport_lb & 10331a43cff9SSean Bruno tw->tw_so_options) == 0)) { 1034340c35deSJonathan Lemon return (EADDRINUSE); 10351a43cff9SSean Bruno } 10360a100a6fSAdrian Chadd } else if (t && 10370a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 10381a43cff9SSean Bruno (reuseport & inp_so_options(t)) == 0 && 10391a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 1040e3fd5ffdSRobert Watson #ifdef INET6 104133841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 1042cfa1ca9dSYoshinobu Inoue INADDR_ANY || 1043cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 1044cfa1ca9dSYoshinobu Inoue INADDR_ANY || 1045fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 1046fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 1047e3fd5ffdSRobert Watson #endif 1048df8bae1dSRodney W. Grimes return (EADDRINUSE); 10490a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 10500a100a6fSAdrian Chadd return (EADDRINUSE); 1051df8bae1dSRodney W. Grimes } 1052cfa1ca9dSYoshinobu Inoue } 1053df8bae1dSRodney W. Grimes } 10544b932371SIan Dowse if (*lportp != 0) 10554b932371SIan Dowse lport = *lportp; 105633b3ac06SPeter Wemm if (lport == 0) { 10574616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 1058efc76f72SBjoern A. Zeeb if (error != 0) 1059efc76f72SBjoern A. Zeeb return (error); 106033b3ac06SPeter Wemm } 10614b932371SIan Dowse *laddrp = laddr.s_addr; 10624b932371SIan Dowse *lportp = lport; 1063df8bae1dSRodney W. Grimes return (0); 1064df8bae1dSRodney W. Grimes } 1065df8bae1dSRodney W. Grimes 1066999f1343SGarrett Wollman /* 10675200e00eSIan Dowse * Connect from a socket to a specified address. 10685200e00eSIan Dowse * Both address and port must be specified in argument sin. 10695200e00eSIan Dowse * If don't have a local address for this socket yet, 10705200e00eSIan Dowse * then pick one. 1071999f1343SGarrett Wollman */ 1072999f1343SGarrett Wollman int 1073db0ac6deSCy Schubert in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred, 1074db0ac6deSCy Schubert bool rehash) 1075999f1343SGarrett Wollman { 10765200e00eSIan Dowse u_short lport, fport; 10775200e00eSIan Dowse in_addr_t laddr, faddr; 10785200e00eSIan Dowse int anonport, error; 1079df8bae1dSRodney W. Grimes 10808501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1081fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 108227f74fd0SRobert Watson 10835200e00eSIan Dowse lport = inp->inp_lport; 10845200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 10855200e00eSIan Dowse anonport = (lport == 0); 10865200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 1087b0330ed9SPawel Jakub Dawidek NULL, cred); 10885200e00eSIan Dowse if (error) 10895200e00eSIan Dowse return (error); 10905200e00eSIan Dowse 10915200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 10925200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 1093fe1274eeSMichael Tuexen KASSERT(rehash == true, 1094fe1274eeSMichael Tuexen ("Rehashing required for unbound inps")); 10955200e00eSIan Dowse inp->inp_lport = lport; 10965200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10975200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 10985200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 10995200e00eSIan Dowse inp->inp_lport = 0; 11005200e00eSIan Dowse return (EAGAIN); 11015200e00eSIan Dowse } 11025200e00eSIan Dowse } 11035200e00eSIan Dowse 11045200e00eSIan Dowse /* Commit the remaining changes. */ 11055200e00eSIan Dowse inp->inp_lport = lport; 11065200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 11075200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 11085200e00eSIan Dowse inp->inp_fport = fport; 1109fe1274eeSMichael Tuexen if (rehash) { 1110db0ac6deSCy Schubert in_pcbrehash(inp); 1111fe1274eeSMichael Tuexen } else { 1112db0ac6deSCy Schubert in_pcbinshash(inp); 1113fe1274eeSMichael Tuexen } 11142cb64cb2SGeorge V. Neville-Neil 11155200e00eSIan Dowse if (anonport) 11165200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 11175200e00eSIan Dowse return (0); 11185200e00eSIan Dowse } 11195200e00eSIan Dowse 11205200e00eSIan Dowse /* 11210895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 11220895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 11230895aec3SBjoern A. Zeeb */ 1124ae190832SSteven Hartland int 11250895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 11260895aec3SBjoern A. Zeeb struct ucred *cred) 11270895aec3SBjoern A. Zeeb { 11280895aec3SBjoern A. Zeeb struct ifaddr *ifa; 11290895aec3SBjoern A. Zeeb struct sockaddr *sa; 11309ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 11319ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 11320895aec3SBjoern A. Zeeb int error; 11330895aec3SBjoern A. Zeeb 1134c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1135413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1136592bcae8SBjoern A. Zeeb /* 1137592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1138592bcae8SBjoern A. Zeeb * if requested. 1139592bcae8SBjoern A. Zeeb */ 1140592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 1141592bcae8SBjoern A. Zeeb return (0); 1142592bcae8SBjoern A. Zeeb 11430895aec3SBjoern A. Zeeb error = 0; 11440895aec3SBjoern A. Zeeb 11459ac7c6cfSAlexander V. Chernikov nh = NULL; 11469ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 11479ac7c6cfSAlexander V. Chernikov sin = &dst; 11480895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 11490895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 11500895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 11510895aec3SBjoern A. Zeeb 11520895aec3SBjoern A. Zeeb /* 11530895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 11540895aec3SBjoern A. Zeeb * else punt. 11550895aec3SBjoern A. Zeeb * 11560895aec3SBjoern A. Zeeb * Find out route to destination. 11570895aec3SBjoern A. Zeeb */ 11580895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 11599ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 11609ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 11610895aec3SBjoern A. Zeeb 11620895aec3SBjoern A. Zeeb /* 11630895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 11640895aec3SBjoern A. Zeeb * the outgoing interface. 11650895aec3SBjoern A. Zeeb * 11660895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 11670895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 11680895aec3SBjoern A. Zeeb * the source address from. 11690895aec3SBjoern A. Zeeb */ 11709ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 11718c0fec80SRobert Watson struct in_ifaddr *ia; 11720895aec3SBjoern A. Zeeb struct ifnet *ifp; 11730895aec3SBjoern A. Zeeb 11744f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 117558a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11764f6c66ccSMatt Macy if (ia == NULL) { 11774f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 117858a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11794f6c66ccSMatt Macy } 11800895aec3SBjoern A. Zeeb if (ia == NULL) { 11810895aec3SBjoern A. Zeeb error = ENETUNREACH; 11820895aec3SBjoern A. Zeeb goto done; 11830895aec3SBjoern A. Zeeb } 11840895aec3SBjoern A. Zeeb 11850304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 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 ifp = ia->ia_ifp; 11910895aec3SBjoern A. Zeeb ia = NULL; 1192d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11930895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11940895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11950895aec3SBjoern A. Zeeb continue; 11960895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1197b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11980895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11990895aec3SBjoern A. Zeeb break; 12000895aec3SBjoern A. Zeeb } 12010895aec3SBjoern A. Zeeb } 12020895aec3SBjoern A. Zeeb if (ia != NULL) { 12030895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12040895aec3SBjoern A. Zeeb goto done; 12050895aec3SBjoern A. Zeeb } 12060895aec3SBjoern A. Zeeb 12070895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1208b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12090895aec3SBjoern A. Zeeb goto done; 12100895aec3SBjoern A. Zeeb } 12110895aec3SBjoern A. Zeeb 12120895aec3SBjoern A. Zeeb /* 12130895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 12140895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 12150895aec3SBjoern A. Zeeb * In case of jails do those three steps: 12160895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 12170895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 12180895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 12190895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 12200895aec3SBjoern A. Zeeb */ 12219ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 12228c0fec80SRobert Watson struct in_ifaddr *ia; 12239317b04eSRobert Watson struct ifnet *ifp; 12240895aec3SBjoern A. Zeeb 12250895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 12260304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 12279ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 12280895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12290895aec3SBjoern A. Zeeb goto done; 12300895aec3SBjoern A. Zeeb } 12310895aec3SBjoern A. Zeeb 12320895aec3SBjoern A. Zeeb /* Jailed. */ 12330895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 12349ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1235b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12369ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 12370895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12380895aec3SBjoern A. Zeeb goto done; 12390895aec3SBjoern A. Zeeb } 12400895aec3SBjoern A. Zeeb 12410895aec3SBjoern A. Zeeb /* 12420895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 12430895aec3SBjoern A. Zeeb * belonging to this jail. 12440895aec3SBjoern A. Zeeb */ 12458c0fec80SRobert Watson ia = NULL; 12469ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1247d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12480895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12490895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12500895aec3SBjoern A. Zeeb continue; 12510895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1252b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12530895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12540895aec3SBjoern A. Zeeb break; 12550895aec3SBjoern A. Zeeb } 12560895aec3SBjoern A. Zeeb } 12570895aec3SBjoern A. Zeeb if (ia != NULL) { 12580895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12590895aec3SBjoern A. Zeeb goto done; 12600895aec3SBjoern A. Zeeb } 12610895aec3SBjoern A. Zeeb 12620895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1263b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12640895aec3SBjoern A. Zeeb goto done; 12650895aec3SBjoern A. Zeeb } 12660895aec3SBjoern A. Zeeb 12670895aec3SBjoern A. Zeeb /* 12680895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 12690895aec3SBjoern A. Zeeb * to ourselves is here. 12700895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 12710895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 12720895aec3SBjoern A. Zeeb * a loopback interface. 12730895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 12740895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 12750895aec3SBjoern A. Zeeb */ 12769ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 12778c0fec80SRobert Watson struct in_ifaddr *ia; 12780895aec3SBjoern A. Zeeb 12799ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 128058a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12810895aec3SBjoern A. Zeeb if (ia == NULL) 12829ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 128358a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1284f0bb05fcSQing Li if (ia == NULL) 12859ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 12860895aec3SBjoern A. Zeeb 12870304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 12880895aec3SBjoern A. Zeeb if (ia == NULL) { 12890895aec3SBjoern A. Zeeb error = ENETUNREACH; 12900895aec3SBjoern A. Zeeb goto done; 12910895aec3SBjoern A. Zeeb } 12920895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12930895aec3SBjoern A. Zeeb goto done; 12940895aec3SBjoern A. Zeeb } 12950895aec3SBjoern A. Zeeb 12960895aec3SBjoern A. Zeeb /* Jailed. */ 12970895aec3SBjoern A. Zeeb if (ia != NULL) { 12980895aec3SBjoern A. Zeeb struct ifnet *ifp; 12990895aec3SBjoern A. Zeeb 13000895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 13010895aec3SBjoern A. Zeeb ia = NULL; 1302d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 13030895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 13040895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 13050895aec3SBjoern A. Zeeb continue; 13060895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1307b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1308b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 13090895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 13100895aec3SBjoern A. Zeeb break; 13110895aec3SBjoern A. Zeeb } 13120895aec3SBjoern A. Zeeb } 13130895aec3SBjoern A. Zeeb if (ia != NULL) { 13140895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13150895aec3SBjoern A. Zeeb goto done; 13160895aec3SBjoern A. Zeeb } 13170895aec3SBjoern A. Zeeb } 13180895aec3SBjoern A. Zeeb 13190895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1320b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 13210895aec3SBjoern A. Zeeb goto done; 13220895aec3SBjoern A. Zeeb } 13230895aec3SBjoern A. Zeeb 13240895aec3SBjoern A. Zeeb done: 13250895aec3SBjoern A. Zeeb return (error); 13260895aec3SBjoern A. Zeeb } 13270895aec3SBjoern A. Zeeb 13280895aec3SBjoern A. Zeeb /* 13295200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 13305200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 13315200e00eSIan Dowse * address and port for the PCB; these are updated to the values 13325200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 13335200e00eSIan Dowse * the connect. 13345200e00eSIan Dowse * 13355200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 13365200e00eSIan Dowse * and port. These are not updated in the error case. 13375200e00eSIan Dowse * 13385200e00eSIan Dowse * If the operation fails because the connection already exists, 13395200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 13405200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 13415200e00eSIan Dowse * is set to NULL. 13425200e00eSIan Dowse */ 13435200e00eSIan Dowse int 1344136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1345136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1346136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 13475200e00eSIan Dowse { 13485200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 13495200e00eSIan Dowse struct in_ifaddr *ia; 13505200e00eSIan Dowse struct inpcb *oinp; 1351b89e82ddSJamie Gritton struct in_addr laddr, faddr; 13525200e00eSIan Dowse u_short lport, fport; 13535200e00eSIan Dowse int error; 13545200e00eSIan Dowse 1355f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 1356f161d294SMark Johnston ("%s: invalid address family for %p", __func__, sin)); 1357f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 1358f161d294SMark Johnston ("%s: invalid address length for %p", __func__, sin)); 1359f161d294SMark Johnston 13608501a69cSRobert Watson /* 13618501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 13628501a69cSRobert Watson * lock is sufficient. 13638501a69cSRobert Watson */ 1364c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 136527f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1366fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 136727f74fd0SRobert Watson 13685200e00eSIan Dowse if (oinpp != NULL) 13695200e00eSIan Dowse *oinpp = NULL; 1370df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1371df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 13725200e00eSIan Dowse laddr.s_addr = *laddrp; 13735200e00eSIan Dowse lport = *lportp; 13745200e00eSIan Dowse faddr = sin->sin_addr; 13755200e00eSIan Dowse fport = sin->sin_port; 13760c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH 13770c325f53SAlexander V. Chernikov if (CALC_FLOWID_OUTBOUND) { 13780c325f53SAlexander V. Chernikov uint32_t hash_val, hash_type; 13790895aec3SBjoern A. Zeeb 13800c325f53SAlexander V. Chernikov hash_val = fib4_calc_software_hash(laddr, faddr, 0, fport, 13810c325f53SAlexander V. Chernikov inp->inp_socket->so_proto->pr_protocol, &hash_type); 13820c325f53SAlexander V. Chernikov 13830c325f53SAlexander V. Chernikov inp->inp_flowid = hash_val; 13840c325f53SAlexander V. Chernikov inp->inp_flowtype = hash_type; 13850c325f53SAlexander V. Chernikov } 13860c325f53SAlexander V. Chernikov #endif 1387d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1388df8bae1dSRodney W. Grimes /* 1389df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1390df8bae1dSRodney W. Grimes * use the primary local address. 1391df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1392df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1393df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1394df8bae1dSRodney W. Grimes */ 1395413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1396413628a7SBjoern A. Zeeb faddr = 1397d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1398b89e82ddSJamie Gritton if (cred != NULL && 1399b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1400b89e82ddSJamie Gritton return (error); 14012d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1402d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 14032d9cfabaSRobert Watson IFF_BROADCAST) 1404d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1405603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 14062d9cfabaSRobert Watson } 1407df8bae1dSRodney W. Grimes } 14085200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1409d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1410df8bae1dSRodney W. Grimes /* 1411df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1412d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1413df8bae1dSRodney W. Grimes * address of that interface as our source address. 1414df8bae1dSRodney W. Grimes */ 14155200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1416df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1417df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1418df8bae1dSRodney W. Grimes struct ifnet *ifp; 1419df8bae1dSRodney W. Grimes 1420df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1421df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1422df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1423d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1424e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1425e691be70SDaniel Eischen (cred == NULL || 1426e691be70SDaniel Eischen prison_check_ip4(cred, 1427e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1428df8bae1dSRodney W. Grimes break; 1429e691be70SDaniel Eischen } 1430e691be70SDaniel Eischen if (ia == NULL) 1431d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1432e691be70SDaniel Eischen else { 14335200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1434d79fdd98SDaniel Eischen error = 0; 1435999f1343SGarrett Wollman } 1436d79fdd98SDaniel Eischen } 1437d79fdd98SDaniel Eischen } 143804215ed2SRandall Stewart if (error) 143904215ed2SRandall Stewart return (error); 14400895aec3SBjoern A. Zeeb } 1441a034518aSAndrew Gallatin 144225102351SMike Karels if (lport != 0) { 144325102351SMike Karels oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 1444a034518aSAndrew Gallatin fport, laddr, lport, 0, NULL, M_NODOM); 14455200e00eSIan Dowse if (oinp != NULL) { 14465200e00eSIan Dowse if (oinpp != NULL) 14475200e00eSIan Dowse *oinpp = oinp; 1448df8bae1dSRodney W. Grimes return (EADDRINUSE); 1449c3229e05SDavid Greenman } 145025102351SMike Karels } else { 145125102351SMike Karels struct sockaddr_in lsin, fsin; 145225102351SMike Karels 145325102351SMike Karels bzero(&lsin, sizeof(lsin)); 145425102351SMike Karels bzero(&fsin, sizeof(fsin)); 145525102351SMike Karels lsin.sin_family = AF_INET; 145625102351SMike Karels lsin.sin_addr = laddr; 145725102351SMike Karels fsin.sin_family = AF_INET; 145825102351SMike Karels fsin.sin_addr = faddr; 145925102351SMike Karels error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin, 146025102351SMike Karels &lport, (struct sockaddr *)& fsin, fport, cred, 146125102351SMike Karels INPLOOKUP_WILDCARD); 14625a903f8dSPierre Beyssac if (error) 14635a903f8dSPierre Beyssac return (error); 14645a903f8dSPierre Beyssac } 14655200e00eSIan Dowse *laddrp = laddr.s_addr; 14665200e00eSIan Dowse *lportp = lport; 14675200e00eSIan Dowse *faddrp = faddr.s_addr; 14685200e00eSIan Dowse *fportp = fport; 1469df8bae1dSRodney W. Grimes return (0); 1470df8bae1dSRodney W. Grimes } 1471df8bae1dSRodney W. Grimes 147226f9a767SRodney W. Grimes void 1473136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1474df8bae1dSRodney W. Grimes { 14756b348152SRobert Watson 14768501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1477fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1478df8bae1dSRodney W. Grimes 1479df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1480df8bae1dSRodney W. Grimes inp->inp_fport = 0; 148115bd2b43SDavid Greenman in_pcbrehash(inp); 1482df8bae1dSRodney W. Grimes } 148383e521ecSBjoern A. Zeeb #endif /* INET */ 1484df8bae1dSRodney W. Grimes 14854c7c478dSRobert Watson /* 148628696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1487c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 148828696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 148928696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 14904c7c478dSRobert Watson */ 149126f9a767SRodney W. Grimes void 1492136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1493df8bae1dSRodney W. Grimes { 14944c7c478dSRobert Watson 1495a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1496c0a211c5SRobert Watson 1497f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1498f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1499f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1500f3e7afe2SHans Petter Selasky #endif 15014c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 15024c7c478dSRobert Watson inp->inp_socket = NULL; 15034c7c478dSRobert Watson } 15044c7c478dSRobert Watson 1505c0a211c5SRobert Watson /* 1506db0ac6deSCy Schubert * inpcb hash lookups are protected by SMR section. 1507db0ac6deSCy Schubert * 1508db0ac6deSCy Schubert * Once desired pcb has been found, switching from SMR section to a pcb 1509db0ac6deSCy Schubert * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1510db0ac6deSCy Schubert * here because SMR is a critical section. 1511db0ac6deSCy Schubert * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1512db0ac6deSCy Schubert */ 1513db0ac6deSCy Schubert static inline void 1514db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1515db0ac6deSCy Schubert { 1516db0ac6deSCy Schubert 1517db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1518db0ac6deSCy Schubert rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1519db0ac6deSCy Schubert } 1520db0ac6deSCy Schubert 1521db0ac6deSCy Schubert static inline void 1522db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1523db0ac6deSCy Schubert { 1524db0ac6deSCy Schubert 1525db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1526db0ac6deSCy Schubert rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1527db0ac6deSCy Schubert } 1528db0ac6deSCy Schubert 1529db0ac6deSCy Schubert static inline int 1530db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1531db0ac6deSCy Schubert { 1532db0ac6deSCy Schubert 1533db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1534db0ac6deSCy Schubert rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1535db0ac6deSCy Schubert } 1536db0ac6deSCy Schubert 1537db0ac6deSCy Schubert static inline bool 1538db0ac6deSCy Schubert in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 1539db0ac6deSCy Schubert { 1540db0ac6deSCy Schubert 1541db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1542db0ac6deSCy Schubert in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 1543db0ac6deSCy Schubert } 1544db0ac6deSCy Schubert 1545db0ac6deSCy Schubert bool 1546db0ac6deSCy Schubert inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1547db0ac6deSCy Schubert { 1548db0ac6deSCy Schubert 1549db0ac6deSCy Schubert MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1550db0ac6deSCy Schubert SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1551db0ac6deSCy Schubert 1552db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1553db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1554db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1555db0ac6deSCy Schubert inp_unlock(inp, lock); 1556db0ac6deSCy Schubert return (false); 1557db0ac6deSCy Schubert } 1558db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1559db0ac6deSCy Schubert return (true); 1560db0ac6deSCy Schubert } 1561db0ac6deSCy Schubert 1562db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1563db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1564db0ac6deSCy Schubert inp_lock(inp, lock); 1565db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) 1566db0ac6deSCy Schubert return (false); 1567db0ac6deSCy Schubert /* 1568db0ac6deSCy Schubert * inp acquired through refcount & lock for sure didn't went 1569db0ac6deSCy Schubert * through uma_zfree(). However, it may have already went 1570db0ac6deSCy Schubert * through in_pcbfree() and has another reference, that 1571db0ac6deSCy Schubert * prevented its release by our in_pcbrele(). 1572db0ac6deSCy Schubert */ 1573db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1574db0ac6deSCy Schubert inp_unlock(inp, lock); 1575db0ac6deSCy Schubert return (false); 1576db0ac6deSCy Schubert } 1577db0ac6deSCy Schubert return (true); 1578db0ac6deSCy Schubert } else { 1579db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1580db0ac6deSCy Schubert return (false); 1581db0ac6deSCy Schubert } 1582db0ac6deSCy Schubert } 1583db0ac6deSCy Schubert 1584db0ac6deSCy Schubert /* 1585db0ac6deSCy Schubert * inp_next() - inpcb hash/list traversal iterator 1586db0ac6deSCy Schubert * 1587db0ac6deSCy Schubert * Requires initialized struct inpcb_iterator for context. 1588db0ac6deSCy Schubert * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1589db0ac6deSCy Schubert * 1590db0ac6deSCy Schubert * - Iterator can have either write-lock or read-lock semantics, that can not 1591db0ac6deSCy Schubert * be changed later. 1592db0ac6deSCy Schubert * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1593db0ac6deSCy Schubert * a single hash slot. Note: only rip_input() does the latter. 1594db0ac6deSCy Schubert * - Iterator may have optional bool matching function. The matching function 1595db0ac6deSCy Schubert * will be executed for each inpcb in the SMR context, so it can not acquire 1596db0ac6deSCy Schubert * locks and can safely access only immutable fields of inpcb. 1597db0ac6deSCy Schubert * 1598db0ac6deSCy Schubert * A fresh initialized iterator has NULL inpcb in its context and that 1599db0ac6deSCy Schubert * means that inp_next() call would return the very first inpcb on the list 1600db0ac6deSCy Schubert * locked with desired semantic. In all following calls the context pointer 1601db0ac6deSCy Schubert * shall hold the current inpcb pointer. The KPI user is not supposed to 1602db0ac6deSCy Schubert * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1603db0ac6deSCy Schubert * and write NULL to its context. After end of traversal an iterator can be 1604db0ac6deSCy Schubert * reused. 1605db0ac6deSCy Schubert * 1606db0ac6deSCy Schubert * List traversals have the following features/constraints: 1607db0ac6deSCy Schubert * - New entries won't be seen, as they are always added to the head of a list. 1608db0ac6deSCy Schubert * - Removed entries won't stop traversal as long as they are not added to 1609db0ac6deSCy Schubert * a different list. This is violated by in_pcbrehash(). 1610db0ac6deSCy Schubert */ 1611db0ac6deSCy Schubert #define II_LIST_FIRST(ipi, hash) \ 1612db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1613db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1614db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_hashbase[(hash)])) 1615db0ac6deSCy Schubert #define II_LIST_NEXT(inp, hash) \ 1616db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1617db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_list) : \ 1618db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_hash)) 1619db0ac6deSCy Schubert #define II_LOCK_ASSERT(inp, lock) \ 1620db0ac6deSCy Schubert rw_assert(&(inp)->inp_lock, \ 1621db0ac6deSCy Schubert (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1622db0ac6deSCy Schubert struct inpcb * 1623db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii) 1624db0ac6deSCy Schubert { 1625db0ac6deSCy Schubert const struct inpcbinfo *ipi = ii->ipi; 1626db0ac6deSCy Schubert inp_match_t *match = ii->match; 1627db0ac6deSCy Schubert void *ctx = ii->ctx; 1628db0ac6deSCy Schubert inp_lookup_t lock = ii->lock; 1629db0ac6deSCy Schubert int hash = ii->hash; 1630db0ac6deSCy Schubert struct inpcb *inp; 1631db0ac6deSCy Schubert 1632db0ac6deSCy Schubert if (ii->inp == NULL) { /* First call. */ 1633db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1634db0ac6deSCy Schubert /* This is unrolled CK_LIST_FOREACH(). */ 1635db0ac6deSCy Schubert for (inp = II_LIST_FIRST(ipi, hash); 1636db0ac6deSCy Schubert inp != NULL; 1637db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash)) { 1638db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1639db0ac6deSCy Schubert continue; 1640db0ac6deSCy Schubert if (__predict_true(inp_smr_lock(inp, lock))) 1641db0ac6deSCy Schubert break; 1642db0ac6deSCy Schubert else { 1643db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1644db0ac6deSCy Schubert MPASS(inp != II_LIST_FIRST(ipi, hash)); 1645db0ac6deSCy Schubert inp = II_LIST_FIRST(ipi, hash); 1646db0ac6deSCy Schubert } 1647db0ac6deSCy Schubert } 1648db0ac6deSCy Schubert 1649db0ac6deSCy Schubert if (inp == NULL) 1650db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1651db0ac6deSCy Schubert else 1652db0ac6deSCy Schubert ii->inp = inp; 1653db0ac6deSCy Schubert 1654db0ac6deSCy Schubert return (inp); 1655db0ac6deSCy Schubert } 1656db0ac6deSCy Schubert 1657db0ac6deSCy Schubert /* Not a first call. */ 1658db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1659db0ac6deSCy Schubert restart: 1660db0ac6deSCy Schubert inp = ii->inp; 1661db0ac6deSCy Schubert II_LOCK_ASSERT(inp, lock); 1662db0ac6deSCy Schubert next: 1663db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash); 1664db0ac6deSCy Schubert if (inp == NULL) { 1665db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1666db0ac6deSCy Schubert goto found; 1667db0ac6deSCy Schubert } 1668db0ac6deSCy Schubert 1669db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1670db0ac6deSCy Schubert goto next; 1671db0ac6deSCy Schubert 1672db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1673db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1674db0ac6deSCy Schubert /* 1675db0ac6deSCy Schubert * Entries are never inserted in middle of a list, thus 1676db0ac6deSCy Schubert * as long as we are in SMR, we can continue traversal. 1677db0ac6deSCy Schubert * Jump to 'restart' should yield in the same result, 1678db0ac6deSCy Schubert * but could produce unnecessary looping. Could this 1679db0ac6deSCy Schubert * looping be unbound? 1680db0ac6deSCy Schubert */ 1681db0ac6deSCy Schubert inp_unlock(inp, lock); 1682db0ac6deSCy Schubert goto next; 1683db0ac6deSCy Schubert } else { 1684db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1685db0ac6deSCy Schubert goto found; 1686db0ac6deSCy Schubert } 1687db0ac6deSCy Schubert } 1688db0ac6deSCy Schubert 1689db0ac6deSCy Schubert /* 1690db0ac6deSCy Schubert * Can't obtain lock immediately, thus going hard. Once we exit the 1691db0ac6deSCy Schubert * SMR section we can no longer jump to 'next', and our only stable 1692db0ac6deSCy Schubert * anchoring point is ii->inp, which we keep locked for this case, so 1693db0ac6deSCy Schubert * we jump to 'restart'. 1694db0ac6deSCy Schubert */ 1695db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1696db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1697db0ac6deSCy Schubert inp_lock(inp, lock); 1698db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) { 1699db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1700db0ac6deSCy Schubert goto restart; 1701db0ac6deSCy Schubert } 1702db0ac6deSCy Schubert /* 1703db0ac6deSCy Schubert * See comment in inp_smr_lock(). 1704db0ac6deSCy Schubert */ 1705db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1706db0ac6deSCy Schubert inp_unlock(inp, lock); 1707db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1708db0ac6deSCy Schubert goto restart; 1709db0ac6deSCy Schubert } 1710db0ac6deSCy Schubert } else 1711db0ac6deSCy Schubert goto next; 1712db0ac6deSCy Schubert 1713db0ac6deSCy Schubert found: 1714db0ac6deSCy Schubert inp_unlock(ii->inp, lock); 1715db0ac6deSCy Schubert ii->inp = inp; 1716db0ac6deSCy Schubert 1717db0ac6deSCy Schubert return (ii->inp); 1718db0ac6deSCy Schubert } 1719db0ac6deSCy Schubert 1720db0ac6deSCy Schubert /* 172179bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 1722db0ac6deSCy Schubert * stability of an inpcb pointer despite the inpcb lock being released or 1723db0ac6deSCy Schubert * SMR section exited. 172479bdc6e5SRobert Watson * 1725db0ac6deSCy Schubert * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1726c0a211c5SRobert Watson */ 172779bdc6e5SRobert Watson void 172879bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 17294c7c478dSRobert Watson { 1730db0ac6deSCy Schubert u_int old __diagused; 1731df8bae1dSRodney W. Grimes 1732db0ac6deSCy Schubert old = refcount_acquire(&inp->inp_refcount); 1733db0ac6deSCy Schubert KASSERT(old > 0, ("%s: refcount 0", __func__)); 173479bdc6e5SRobert Watson } 173579bdc6e5SRobert Watson 173679bdc6e5SRobert Watson /* 1737db0ac6deSCy Schubert * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1738db0ac6deSCy Schubert * freeing the pcb, if the reference was very last. 173979bdc6e5SRobert Watson */ 1740db0ac6deSCy Schubert bool 174179bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 174279bdc6e5SRobert Watson { 174379bdc6e5SRobert Watson 174479bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 174579bdc6e5SRobert Watson 1746db0ac6deSCy Schubert if (refcount_release(&inp->inp_refcount) == 0) 1747db0ac6deSCy Schubert return (false); 1748db0ac6deSCy Schubert 1749db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1750db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1751db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1752db0ac6deSCy Schubert MPASS(inp->inp_in_dropq == 0); 1753df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1754db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1755db0ac6deSCy Schubert return (true); 1756df4e91d3SGleb Smirnoff } 175779bdc6e5SRobert Watson 1758db0ac6deSCy Schubert bool 175979bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 176079bdc6e5SRobert Watson { 176179bdc6e5SRobert Watson 176279bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 176379bdc6e5SRobert Watson 1764db0ac6deSCy Schubert if (refcount_release(&inp->inp_refcount) == 0) 1765db0ac6deSCy Schubert return (false); 1766db0ac6deSCy Schubert 1767db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1768db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1769db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1770db0ac6deSCy Schubert MPASS(inp->inp_in_dropq == 0); 1771edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1772db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1773db0ac6deSCy Schubert return (true); 1774addf2b20SMatt Macy } 1775addf2b20SMatt Macy 177679bdc6e5SRobert Watson /* 177779bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 177879bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 177979bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 178079bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 1781db0ac6deSCy Schubert * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1782db0ac6deSCy Schubert * Almost all work, including removal from global lists, is done in this 1783db0ac6deSCy Schubert * context, where the pcbinfo lock is held. 178479bdc6e5SRobert Watson */ 178579bdc6e5SRobert Watson void 178679bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 178779bdc6e5SRobert Watson { 1788f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1789db0ac6deSCy Schubert #ifdef INET 1790db0ac6deSCy Schubert struct ip_moptions *imo; 1791db0ac6deSCy Schubert #endif 1792db0ac6deSCy Schubert #ifdef INET6 1793db0ac6deSCy Schubert struct ip6_moptions *im6o; 1794db0ac6deSCy Schubert #endif 179545a48d07SJonathan T. Looney 179679bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1797db0ac6deSCy Schubert KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1798db0ac6deSCy Schubert KASSERT((inp->inp_flags & INP_FREED) == 0, 1799db0ac6deSCy Schubert ("%s: called twice for pcb %p", __func__, inp)); 1800db0ac6deSCy Schubert 1801db0ac6deSCy Schubert inp->inp_flags |= INP_FREED; 1802db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 1803db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1804db0ac6deSCy Schubert pcbinfo->ipi_count--; 1805db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_list); 1806db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 1807db0ac6deSCy Schubert 1808db0ac6deSCy Schubert if (inp->inp_flags & INP_INHASHLIST) { 1809db0ac6deSCy Schubert struct inpcbport *phd = inp->inp_phd; 1810db0ac6deSCy Schubert 1811db0ac6deSCy Schubert INP_HASH_WLOCK(pcbinfo); 1812db0ac6deSCy Schubert /* XXX: Only do if SO_REUSEPORT_LB set? */ 1813db0ac6deSCy Schubert in_pcbremlbgrouphash(inp); 1814db0ac6deSCy Schubert 1815db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_hash); 1816db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_portlist); 1817db0ac6deSCy Schubert if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 1818db0ac6deSCy Schubert CK_LIST_REMOVE(phd, phd_hash); 1819db0ac6deSCy Schubert uma_zfree_smr(pcbinfo->ipi_portzone, phd); 1820db0ac6deSCy Schubert } 1821db0ac6deSCy Schubert INP_HASH_WUNLOCK(pcbinfo); 1822db0ac6deSCy Schubert inp->inp_flags &= ~INP_INHASHLIST; 1823db0ac6deSCy Schubert } 1824db0ac6deSCy Schubert 1825fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1826db0ac6deSCy Schubert #ifdef MAC 1827db0ac6deSCy Schubert mac_inpcb_destroy(inp); 1828db0ac6deSCy Schubert #endif 1829db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1830db0ac6deSCy Schubert if (inp->inp_sp != NULL) 1831db0ac6deSCy Schubert ipsec_delete_pcbpolicy(inp); 1832db0ac6deSCy Schubert #endif 1833db0ac6deSCy Schubert #ifdef INET 1834db0ac6deSCy Schubert if (inp->inp_options) 1835db0ac6deSCy Schubert (void)m_free(inp->inp_options); 1836db0ac6deSCy Schubert imo = inp->inp_moptions; 1837db0ac6deSCy Schubert #endif 1838db0ac6deSCy Schubert #ifdef INET6 1839db0ac6deSCy Schubert if (inp->inp_vflag & INP_IPV6PROTO) { 1840db0ac6deSCy Schubert ip6_freepcbopts(inp->in6p_outputopts); 1841db0ac6deSCy Schubert im6o = inp->in6p_moptions; 1842db0ac6deSCy Schubert } else 1843db0ac6deSCy Schubert im6o = NULL; 1844db0ac6deSCy Schubert #endif 1845db0ac6deSCy Schubert 1846db0ac6deSCy Schubert if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1847cb6bb230SMatt Macy INP_WUNLOCK(inp); 1848db0ac6deSCy Schubert } 1849db0ac6deSCy Schubert #ifdef INET6 1850db0ac6deSCy Schubert ip6_freemoptions(im6o); 1851db0ac6deSCy Schubert #endif 1852db0ac6deSCy Schubert #ifdef INET 1853db0ac6deSCy Schubert inp_freemoptions(imo); 1854db0ac6deSCy Schubert #endif 1855*eb93b99dSGleb Smirnoff /* Destruction is finalized in inpcb_dtor(). */ 1856*eb93b99dSGleb Smirnoff } 1857*eb93b99dSGleb Smirnoff 1858*eb93b99dSGleb Smirnoff static void 1859*eb93b99dSGleb Smirnoff inpcb_dtor(void *mem, int size, void *arg) 1860*eb93b99dSGleb Smirnoff { 1861*eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1862*eb93b99dSGleb Smirnoff 1863*eb93b99dSGleb Smirnoff crfree(inp->inp_cred); 1864*eb93b99dSGleb Smirnoff #ifdef INVARIANTS 1865*eb93b99dSGleb Smirnoff inp->inp_cred = NULL; 1866*eb93b99dSGleb Smirnoff #endif 1867*eb93b99dSGleb Smirnoff } 1868*eb93b99dSGleb Smirnoff 1869*eb93b99dSGleb Smirnoff /* 1870*eb93b99dSGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 1871*eb93b99dSGleb Smirnoff * different name to the lock. But they all are disposed the same. 1872*eb93b99dSGleb Smirnoff */ 1873*eb93b99dSGleb Smirnoff static void 1874*eb93b99dSGleb Smirnoff inpcb_fini(void *mem, int size) 1875*eb93b99dSGleb Smirnoff { 1876*eb93b99dSGleb Smirnoff struct inpcb *inp = mem; 1877*eb93b99dSGleb Smirnoff 1878*eb93b99dSGleb Smirnoff INP_LOCK_DESTROY(inp); 187928696211SRobert Watson } 188028696211SRobert Watson 188128696211SRobert Watson /* 1882c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1883c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1884c0a211c5SRobert Watson * 1885c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1886c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1887c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1888c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1889c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1890c0a211c5SRobert Watson * in_pcbdetach(). 1891c0a211c5SRobert Watson * 1892c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1893c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 189410702a28SRobert Watson */ 189510702a28SRobert Watson void 189610702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 189710702a28SRobert Watson { 189810702a28SRobert Watson 18998501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 19006573d758SMatt Macy #ifdef INVARIANTS 19016573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 19026573d758SMatt Macy MPASS(inp->inp_refcount > 1); 19036573d758SMatt Macy #endif 190410702a28SRobert Watson 1905fa046d87SRobert Watson /* 1906fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1907fa046d87SRobert Watson * the hash lock...? 1908fa046d87SRobert Watson */ 1909ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1910111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 191110702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 191210702a28SRobert Watson 1913fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 19141a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 1915b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 1916b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 1917b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 1918b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 1919db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_portzone, phd); 192010702a28SRobert Watson } 1921fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1922111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 192310702a28SRobert Watson } 192410702a28SRobert Watson } 192510702a28SRobert Watson 192667107f45SBjoern A. Zeeb #ifdef INET 192754d642bbSRobert Watson /* 192854d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 192954d642bbSRobert Watson */ 193026ef6ac4SDon Lewis struct sockaddr * 1931136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 193226ef6ac4SDon Lewis { 193326ef6ac4SDon Lewis struct sockaddr_in *sin; 193426ef6ac4SDon Lewis 19351ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1936a163d034SWarner Losh M_WAITOK | M_ZERO); 193726ef6ac4SDon Lewis sin->sin_family = AF_INET; 193826ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 193926ef6ac4SDon Lewis sin->sin_addr = *addr_p; 194026ef6ac4SDon Lewis sin->sin_port = port; 194126ef6ac4SDon Lewis 194226ef6ac4SDon Lewis return (struct sockaddr *)sin; 194326ef6ac4SDon Lewis } 194426ef6ac4SDon Lewis 1945117bcae7SGarrett Wollman int 194654d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1947df8bae1dSRodney W. Grimes { 1948136d4f1cSRobert Watson struct inpcb *inp; 194926ef6ac4SDon Lewis struct in_addr addr; 195026ef6ac4SDon Lewis in_port_t port; 195142fa505bSDavid Greenman 1952fdc984f7STor Egge inp = sotoinpcb(so); 195354d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 19546466b28aSRobert Watson 1955a69042a5SRobert Watson INP_RLOCK(inp); 195626ef6ac4SDon Lewis port = inp->inp_lport; 195726ef6ac4SDon Lewis addr = inp->inp_laddr; 1958a69042a5SRobert Watson INP_RUNLOCK(inp); 195942fa505bSDavid Greenman 196026ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1961117bcae7SGarrett Wollman return 0; 1962df8bae1dSRodney W. Grimes } 1963df8bae1dSRodney W. Grimes 1964117bcae7SGarrett Wollman int 196554d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1966df8bae1dSRodney W. Grimes { 1967136d4f1cSRobert Watson struct inpcb *inp; 196826ef6ac4SDon Lewis struct in_addr addr; 196926ef6ac4SDon Lewis in_port_t port; 197042fa505bSDavid Greenman 1971fdc984f7STor Egge inp = sotoinpcb(so); 197254d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 19736466b28aSRobert Watson 1974a69042a5SRobert Watson INP_RLOCK(inp); 197526ef6ac4SDon Lewis port = inp->inp_fport; 197626ef6ac4SDon Lewis addr = inp->inp_faddr; 1977a69042a5SRobert Watson INP_RUNLOCK(inp); 197842fa505bSDavid Greenman 197926ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1980117bcae7SGarrett Wollman return 0; 1981df8bae1dSRodney W. Grimes } 1982df8bae1dSRodney W. Grimes 198326f9a767SRodney W. Grimes void 1984136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1985136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1986d1c54148SJesper Skriver { 1987f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1988d1c54148SJesper Skriver 19893dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1990db0ac6deSCy Schubert CK_LIST_FOREACH_SAFE(inp, &pcbinfo->ipi_listhead, inp_list, inp_temp) { 19918501a69cSRobert Watson INP_WLOCK(inp); 1992d1c54148SJesper Skriver #ifdef INET6 1993f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 19948501a69cSRobert Watson INP_WUNLOCK(inp); 1995d1c54148SJesper Skriver continue; 1996f76fcf6dSJeffrey Hsu } 1997d1c54148SJesper Skriver #endif 1998d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1999f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 20008501a69cSRobert Watson INP_WUNLOCK(inp); 2001d1c54148SJesper Skriver continue; 2002d1c54148SJesper Skriver } 20033dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 20048501a69cSRobert Watson INP_WUNLOCK(inp); 2005f76fcf6dSJeffrey Hsu } 20063dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 2007d1c54148SJesper Skriver } 2008d1c54148SJesper Skriver 2009db0ac6deSCy Schubert static bool 2010db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 2011db0ac6deSCy Schubert { 2012db0ac6deSCy Schubert 2013db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 2014db0ac6deSCy Schubert return (true); 2015db0ac6deSCy Schubert else 2016db0ac6deSCy Schubert return (false); 2017db0ac6deSCy Schubert } 2018db0ac6deSCy Schubert 2019e43cc4aeSHajimu UMEMOTO void 2020136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 2021e43cc4aeSHajimu UMEMOTO { 2022db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 2023db0ac6deSCy Schubert inp_v4_multi_match, NULL); 2024e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 202559854ecfSHans Petter Selasky struct in_multi *inm; 202659854ecfSHans Petter Selasky struct in_mfilter *imf; 2027e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 2028e43cc4aeSHajimu UMEMOTO 2029db0ac6deSCy Schubert IN_MULTI_LOCK_ASSERT(); 2030db0ac6deSCy Schubert 2031db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 2032db0ac6deSCy Schubert INP_WLOCK_ASSERT(inp); 2033db0ac6deSCy Schubert 2034e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 2035e43cc4aeSHajimu UMEMOTO /* 2036e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 2037e43cc4aeSHajimu UMEMOTO * detached. 2038e43cc4aeSHajimu UMEMOTO */ 2039e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 2040e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 2041e43cc4aeSHajimu UMEMOTO 2042e43cc4aeSHajimu UMEMOTO /* 2043e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 2044e43cc4aeSHajimu UMEMOTO * through the interface being detached. 2045cb6bb230SMatt Macy * 2046cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 2047e43cc4aeSHajimu UMEMOTO */ 204859854ecfSHans Petter Selasky restart: 204959854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 205059854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 205159854ecfSHans Petter Selasky continue; 205259854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 205359854ecfSHans Petter Selasky continue; 205459854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 205559854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 205659854ecfSHans Petter Selasky ip_mfilter_free(imf); 205759854ecfSHans Petter Selasky goto restart; 2058e43cc4aeSHajimu UMEMOTO } 2059e43cc4aeSHajimu UMEMOTO } 2060e43cc4aeSHajimu UMEMOTO } 2061e43cc4aeSHajimu UMEMOTO 2062df8bae1dSRodney W. Grimes /* 2063fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 2064fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 2065c3229e05SDavid Greenman */ 2066d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 2067df8bae1dSRodney W. Grimes struct inpcb * 2068136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 206968e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 2070df8bae1dSRodney W. Grimes { 2071136d4f1cSRobert Watson struct inpcb *inp; 2072d5e8a67eSHajimu UMEMOTO #ifdef INET6 2073d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 2074d5e8a67eSHajimu UMEMOTO #else 2075d5e8a67eSHajimu UMEMOTO int matchwild = 3; 2076d5e8a67eSHajimu UMEMOTO #endif 2077d5e8a67eSHajimu UMEMOTO int wildcard; 20787bc4aca7SDavid Greenman 207968e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 208068e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2081fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 20821b73ca0bSSam Leffler 208368e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 2084c3229e05SDavid Greenman struct inpcbhead *head; 2085c3229e05SDavid Greenman /* 2086c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 2087c3229e05SDavid Greenman * matches the local address and port we're looking for. 2088c3229e05SDavid Greenman */ 2089712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2090712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2091b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2092cfa1ca9dSYoshinobu Inoue #ifdef INET6 2093413628a7SBjoern A. Zeeb /* XXX inp locking */ 2094369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2095cfa1ca9dSYoshinobu Inoue continue; 2096cfa1ca9dSYoshinobu Inoue #endif 2097c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 2098c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2099c3229e05SDavid Greenman inp->inp_lport == lport) { 2100c3229e05SDavid Greenman /* 2101413628a7SBjoern A. Zeeb * Found? 2102c3229e05SDavid Greenman */ 2103413628a7SBjoern A. Zeeb if (cred == NULL || 21040304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 21050304c731SJamie Gritton inp->inp_cred->cr_prison)) 2106c3229e05SDavid Greenman return (inp); 2107df8bae1dSRodney W. Grimes } 2108c3229e05SDavid Greenman } 2109c3229e05SDavid Greenman /* 2110c3229e05SDavid Greenman * Not found. 2111c3229e05SDavid Greenman */ 2112c3229e05SDavid Greenman return (NULL); 2113c3229e05SDavid Greenman } else { 2114c3229e05SDavid Greenman struct inpcbporthead *porthash; 2115c3229e05SDavid Greenman struct inpcbport *phd; 2116c3229e05SDavid Greenman struct inpcb *match = NULL; 2117c3229e05SDavid Greenman /* 2118c3229e05SDavid Greenman * Best fit PCB lookup. 2119c3229e05SDavid Greenman * 2120c3229e05SDavid Greenman * First see if this local port is in use by looking on the 2121c3229e05SDavid Greenman * port hash list. 2122c3229e05SDavid Greenman */ 2123712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 2124712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 2125b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 2126c3229e05SDavid Greenman if (phd->phd_port == lport) 2127c3229e05SDavid Greenman break; 2128c3229e05SDavid Greenman } 2129c3229e05SDavid Greenman if (phd != NULL) { 2130c3229e05SDavid Greenman /* 2131c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 2132c3229e05SDavid Greenman * fit. 2133c3229e05SDavid Greenman */ 2134b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 2135c3229e05SDavid Greenman wildcard = 0; 2136413628a7SBjoern A. Zeeb if (cred != NULL && 21370304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 21380304c731SJamie Gritton cred->cr_prison)) 2139413628a7SBjoern A. Zeeb continue; 2140cfa1ca9dSYoshinobu Inoue #ifdef INET6 2141413628a7SBjoern A. Zeeb /* XXX inp locking */ 2142369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2143cfa1ca9dSYoshinobu Inoue continue; 2144d5e8a67eSHajimu UMEMOTO /* 2145d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 2146d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 2147d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 2148d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 2149d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 2150d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 2151d5e8a67eSHajimu UMEMOTO * 2152d5e8a67eSHajimu UMEMOTO * Note that the case only happens 2153d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 2154d5e8a67eSHajimu UMEMOTO * the condition that the use of the 2155d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 2156d5e8a67eSHajimu UMEMOTO */ 2157d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 2158d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2159cfa1ca9dSYoshinobu Inoue #endif 2160c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 2161c3229e05SDavid Greenman wildcard++; 216215bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 216315bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 216415bd2b43SDavid Greenman wildcard++; 216515bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 216615bd2b43SDavid Greenman continue; 216715bd2b43SDavid Greenman } else { 216815bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 216915bd2b43SDavid Greenman wildcard++; 217015bd2b43SDavid Greenman } 2171df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 2172df8bae1dSRodney W. Grimes match = inp; 2173df8bae1dSRodney W. Grimes matchwild = wildcard; 2174413628a7SBjoern A. Zeeb if (matchwild == 0) 2175df8bae1dSRodney W. Grimes break; 2176df8bae1dSRodney W. Grimes } 2177df8bae1dSRodney W. Grimes } 21783dbdc25cSDavid Greenman } 2179df8bae1dSRodney W. Grimes return (match); 2180df8bae1dSRodney W. Grimes } 2181c3229e05SDavid Greenman } 2182d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 218315bd2b43SDavid Greenman 21841a43cff9SSean Bruno static struct inpcb * 21851a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 21861a43cff9SSean Bruno const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr, 2187a034518aSAndrew Gallatin uint16_t fport, int lookupflags, int numa_domain) 21881a43cff9SSean Bruno { 2189a034518aSAndrew Gallatin struct inpcb *local_wild, *numa_wild; 21901a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 21911a43cff9SSean Bruno struct inpcblbgroup *grp; 21928be02ee4SMark Johnston uint32_t idx; 21931a43cff9SSean Bruno 21941a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 21951a43cff9SSean Bruno 21969d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 21979d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 21981a43cff9SSean Bruno 21991a43cff9SSean Bruno /* 22001a43cff9SSean Bruno * Order of socket selection: 22011a43cff9SSean Bruno * 1. non-wild. 22021a43cff9SSean Bruno * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD). 22031a43cff9SSean Bruno * 22041a43cff9SSean Bruno * NOTE: 22051a43cff9SSean Bruno * - Load balanced group does not contain jailed sockets 22061a43cff9SSean Bruno * - Load balanced group does not contain IPv4 mapped INET6 wild sockets 22071a43cff9SSean Bruno */ 22088be02ee4SMark Johnston local_wild = NULL; 2209a034518aSAndrew Gallatin numa_wild = NULL; 221054af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 22111a43cff9SSean Bruno #ifdef INET6 22121a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 22131a43cff9SSean Bruno continue; 22141a43cff9SSean Bruno #endif 22158be02ee4SMark Johnston if (grp->il_lport != lport) 22168be02ee4SMark Johnston continue; 22171a43cff9SSean Bruno 22188be02ee4SMark Johnston idx = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, lport, fport) % 22198be02ee4SMark Johnston grp->il_inpcnt; 2220a034518aSAndrew Gallatin if (grp->il_laddr.s_addr == laddr->s_addr) { 2221a034518aSAndrew Gallatin if (numa_domain == M_NODOM || 2222a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain) { 22231a43cff9SSean Bruno return (grp->il_inp[idx]); 2224a034518aSAndrew Gallatin } else { 2225a034518aSAndrew Gallatin numa_wild = grp->il_inp[idx]; 2226a034518aSAndrew Gallatin } 2227a034518aSAndrew Gallatin } 22281a43cff9SSean Bruno if (grp->il_laddr.s_addr == INADDR_ANY && 2229a034518aSAndrew Gallatin (lookupflags & INPLOOKUP_WILDCARD) != 0 && 2230a034518aSAndrew Gallatin (local_wild == NULL || numa_domain == M_NODOM || 2231a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain)) { 22321a43cff9SSean Bruno local_wild = grp->il_inp[idx]; 22331a43cff9SSean Bruno } 2234a034518aSAndrew Gallatin } 2235a034518aSAndrew Gallatin if (numa_wild != NULL) 2236a034518aSAndrew Gallatin return (numa_wild); 2237a034518aSAndrew Gallatin 22381a43cff9SSean Bruno return (local_wild); 22391a43cff9SSean Bruno } 22401a43cff9SSean Bruno 224115bd2b43SDavid Greenman /* 2242fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2243db0ac6deSCy Schubert * that the caller has either locked the hash list, which usually happens 2244db0ac6deSCy Schubert * for bind(2) operations, or is in SMR section, which happens when sorting 2245db0ac6deSCy Schubert * out incoming packets. 224615bd2b43SDavid Greenman */ 2247fa046d87SRobert Watson static struct inpcb * 2248fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 224968e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2250a034518aSAndrew Gallatin struct ifnet *ifp, uint8_t numa_domain) 225115bd2b43SDavid Greenman { 225215bd2b43SDavid Greenman struct inpcbhead *head; 2253413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 225415bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 225515bd2b43SDavid Greenman 225668e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 225768e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2258d797164aSGleb Smirnoff INP_HASH_LOCK_ASSERT(pcbinfo); 2259d797164aSGleb Smirnoff 226015bd2b43SDavid Greenman /* 226115bd2b43SDavid Greenman * First look for an exact match. 226215bd2b43SDavid Greenman */ 2263413628a7SBjoern A. Zeeb tmpinp = NULL; 2264712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 2265712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 2266b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2267cfa1ca9dSYoshinobu Inoue #ifdef INET6 2268413628a7SBjoern A. Zeeb /* XXX inp locking */ 2269369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2270cfa1ca9dSYoshinobu Inoue continue; 2271cfa1ca9dSYoshinobu Inoue #endif 22726d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2273ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2274ca98b82cSDavid Greenman inp->inp_fport == fport && 2275413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 2276413628a7SBjoern A. Zeeb /* 2277413628a7SBjoern A. Zeeb * XXX We should be able to directly return 2278413628a7SBjoern A. Zeeb * the inp here, without any checks. 2279413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 2280413628a7SBjoern A. Zeeb */ 22810304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 2282c3229e05SDavid Greenman return (inp); 2283413628a7SBjoern A. Zeeb if (tmpinp == NULL) 2284413628a7SBjoern A. Zeeb tmpinp = inp; 2285c3229e05SDavid Greenman } 2286413628a7SBjoern A. Zeeb } 2287413628a7SBjoern A. Zeeb if (tmpinp != NULL) 2288413628a7SBjoern A. Zeeb return (tmpinp); 2289e3fd5ffdSRobert Watson 2290e3fd5ffdSRobert Watson /* 22911a43cff9SSean Bruno * Then look in lb group (for wildcard match). 22921a43cff9SSean Bruno */ 2293d9ff5789SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 22941a43cff9SSean Bruno inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr, 2295a034518aSAndrew Gallatin fport, lookupflags, numa_domain); 2296d9ff5789SMark Johnston if (inp != NULL) 22971a43cff9SSean Bruno return (inp); 22981a43cff9SSean Bruno } 22991a43cff9SSean Bruno 23001a43cff9SSean Bruno /* 2301e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 2302e3fd5ffdSRobert Watson */ 230368e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2304413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 2305e3fd5ffdSRobert Watson #ifdef INET6 2306cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 2307e3fd5ffdSRobert Watson #endif 2308413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 2309413628a7SBjoern A. Zeeb int injail; 2310413628a7SBjoern A. Zeeb 2311413628a7SBjoern A. Zeeb /* 2312413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2313413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2314413628a7SBjoern A. Zeeb * 2. jailed, wild. 2315413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2316413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2317413628a7SBjoern A. Zeeb */ 23186d6a026bSDavid Greenman 2319712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2320712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2321b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2322cfa1ca9dSYoshinobu Inoue #ifdef INET6 2323413628a7SBjoern A. Zeeb /* XXX inp locking */ 2324369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2325cfa1ca9dSYoshinobu Inoue continue; 2326cfa1ca9dSYoshinobu Inoue #endif 2327413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2328413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2329413628a7SBjoern A. Zeeb continue; 2330413628a7SBjoern A. Zeeb 23310304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2332413628a7SBjoern A. Zeeb if (injail) { 2333b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2334b89e82ddSJamie Gritton &laddr) != 0) 2335413628a7SBjoern A. Zeeb continue; 2336413628a7SBjoern A. Zeeb } else { 2337413628a7SBjoern A. Zeeb if (local_exact != NULL) 2338413628a7SBjoern A. Zeeb continue; 2339413628a7SBjoern A. Zeeb } 2340413628a7SBjoern A. Zeeb 2341413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2342413628a7SBjoern A. Zeeb if (injail) 2343c3229e05SDavid Greenman return (inp); 2344413628a7SBjoern A. Zeeb else 2345413628a7SBjoern A. Zeeb local_exact = inp; 2346413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2347e3fd5ffdSRobert Watson #ifdef INET6 2348413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23495cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2350cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2351cfa1ca9dSYoshinobu Inoue else 235283e521ecSBjoern A. Zeeb #endif 2353413628a7SBjoern A. Zeeb if (injail) 2354413628a7SBjoern A. Zeeb jail_wild = inp; 2355413628a7SBjoern A. Zeeb else 23566d6a026bSDavid Greenman local_wild = inp; 23576d6a026bSDavid Greenman } 2358413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2359413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2360413628a7SBjoern A. Zeeb return (jail_wild); 2361413628a7SBjoern A. Zeeb if (local_exact != NULL) 2362413628a7SBjoern A. Zeeb return (local_exact); 2363413628a7SBjoern A. Zeeb if (local_wild != NULL) 2364c3229e05SDavid Greenman return (local_wild); 2365413628a7SBjoern A. Zeeb #ifdef INET6 2366413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2367413628a7SBjoern A. Zeeb return (local_wild_mapped); 236883e521ecSBjoern A. Zeeb #endif 236968e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2370413628a7SBjoern A. Zeeb 23716d6a026bSDavid Greenman return (NULL); 237215bd2b43SDavid Greenman } 2373fa046d87SRobert Watson 2374fa046d87SRobert Watson /* 2375fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2376fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2377fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2378fa046d87SRobert Watson */ 2379fa046d87SRobert Watson static struct inpcb * 2380fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2381fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2382a034518aSAndrew Gallatin struct ifnet *ifp, uint8_t numa_domain) 2383fa046d87SRobert Watson { 2384fa046d87SRobert Watson struct inpcb *inp; 2385fa046d87SRobert Watson 2386db0ac6deSCy Schubert smr_enter(pcbinfo->ipi_smr); 2387fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 238808d9c920SGleb Smirnoff lookupflags & INPLOOKUP_WILDCARD, ifp, numa_domain); 2389fa046d87SRobert Watson if (inp != NULL) { 2390db0ac6deSCy Schubert if (__predict_false(inp_smr_lock(inp, 2391db0ac6deSCy Schubert (lookupflags & INPLOOKUP_LOCKMASK)) == false)) 2392266f97b5SCy Schubert inp = NULL; 2393db0ac6deSCy Schubert } else 2394db0ac6deSCy Schubert smr_exit(pcbinfo->ipi_smr); 2395d797164aSGleb Smirnoff 2396fa046d87SRobert Watson return (inp); 2397fa046d87SRobert Watson } 2398fa046d87SRobert Watson 2399fa046d87SRobert Watson /* 2400d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2401d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 2402fa046d87SRobert Watson */ 2403fa046d87SRobert Watson struct inpcb * 2404fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2405fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2406fa046d87SRobert Watson { 2407fa046d87SRobert Watson 2408fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2409fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 24101db08fbeSGleb Smirnoff KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 24111db08fbeSGleb Smirnoff ("%s: LOCKPCB not set", __func__)); 2412fa046d87SRobert Watson 2413fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2414a034518aSAndrew Gallatin lookupflags, ifp, M_NODOM)); 2415fa046d87SRobert Watson } 2416d3c1f003SRobert Watson 2417d3c1f003SRobert Watson struct inpcb * 2418d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2419d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2420d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2421d3c1f003SRobert Watson { 2422d3c1f003SRobert Watson 2423d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2424d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 24251db08fbeSGleb Smirnoff KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 24261db08fbeSGleb Smirnoff ("%s: LOCKPCB not set", __func__)); 2427d3c1f003SRobert Watson 2428d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2429a034518aSAndrew Gallatin lookupflags, ifp, m->m_pkthdr.numa_domain)); 2430d3c1f003SRobert Watson } 243167107f45SBjoern A. Zeeb #endif /* INET */ 243215bd2b43SDavid Greenman 24337bc4aca7SDavid Greenman /* 2434c3229e05SDavid Greenman * Insert PCB onto various hash lists. 24357bc4aca7SDavid Greenman */ 2436db0ac6deSCy Schubert int 2437db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp) 243815bd2b43SDavid Greenman { 2439c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2440c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2441c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2442c3229e05SDavid Greenman struct inpcbport *phd; 2443cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 24441a43cff9SSean Bruno int so_options; 244515bd2b43SDavid Greenman 24468501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2447fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2448fa046d87SRobert Watson 2449111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2450111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2451602cc7f1SRobert Watson 2452cfa1ca9dSYoshinobu Inoue #ifdef INET6 2453cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 24541b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2455cfa1ca9dSYoshinobu Inoue else 245683e521ecSBjoern A. Zeeb #endif 2457cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2458cfa1ca9dSYoshinobu Inoue 2459712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2460712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 246115bd2b43SDavid Greenman 2462712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2463712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2464c3229e05SDavid Greenman 2465c3229e05SDavid Greenman /* 24661a43cff9SSean Bruno * Add entry to load balance group. 24671a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 24681a43cff9SSean Bruno */ 24691a43cff9SSean Bruno so_options = inp_so_options(inp); 24701a43cff9SSean Bruno if (so_options & SO_REUSEPORT_LB) { 2471a034518aSAndrew Gallatin int ret = in_pcbinslbgrouphash(inp, M_NODOM); 24721a43cff9SSean Bruno if (ret) { 24731a43cff9SSean Bruno /* pcb lb group malloc fail (ret=ENOBUFS). */ 24741a43cff9SSean Bruno return (ret); 24751a43cff9SSean Bruno } 24761a43cff9SSean Bruno } 24771a43cff9SSean Bruno 24781a43cff9SSean Bruno /* 2479c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2480c3229e05SDavid Greenman */ 2481b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2482c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2483c3229e05SDavid Greenman break; 2484c3229e05SDavid Greenman } 2485c3229e05SDavid Greenman /* 2486c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2487c3229e05SDavid Greenman */ 2488c3229e05SDavid Greenman if (phd == NULL) { 2489db0ac6deSCy Schubert phd = uma_zalloc_smr(pcbinfo->ipi_portzone, M_NOWAIT); 2490c3229e05SDavid Greenman if (phd == NULL) { 2491c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2492c3229e05SDavid Greenman } 2493c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2494b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2495b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2496c3229e05SDavid Greenman } 2497c3229e05SDavid Greenman inp->inp_phd = phd; 2498b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2499b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2500111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 2501db0ac6deSCy Schubert 2502c3229e05SDavid Greenman return (0); 250315bd2b43SDavid Greenman } 250415bd2b43SDavid Greenman 250552cd27cbSRobert Watson /* 2506c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2507c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2508c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2509c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2510db0ac6deSCy Schubert * 2511db0ac6deSCy Schubert * XXXGL: a race between this function and SMR-protected hash iterator 2512db0ac6deSCy Schubert * will lead to iterator traversing a possibly wrong hash list. However, 2513db0ac6deSCy Schubert * this race should have been here since change from rwlock to epoch. 2514c3229e05SDavid Greenman */ 251515bd2b43SDavid Greenman void 2516db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp) 251715bd2b43SDavid Greenman { 251859daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 251915bd2b43SDavid Greenman struct inpcbhead *head; 2520cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 252115bd2b43SDavid Greenman 25228501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2523fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2524fa046d87SRobert Watson 2525111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2526111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2527602cc7f1SRobert Watson 2528cfa1ca9dSYoshinobu Inoue #ifdef INET6 2529cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 25301b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2531cfa1ca9dSYoshinobu Inoue else 253283e521ecSBjoern A. Zeeb #endif 2533cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2534cfa1ca9dSYoshinobu Inoue 2535712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2536712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 253715bd2b43SDavid Greenman 2538b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2539b872626dSMatt Macy CK_LIST_INSERT_HEAD(head, inp, inp_hash); 2540c3229e05SDavid Greenman } 2541c3229e05SDavid Greenman 2542c3229e05SDavid Greenman /* 254384cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 254484cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 254584cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 254684cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 254784cc0778SGeorge V. Neville-Neil */ 254884cc0778SGeorge V. Neville-Neil void 254984cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 255084cc0778SGeorge V. Neville-Neil { 255184cc0778SGeorge V. Neville-Neil 2552fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 255384cc0778SGeorge V. Neville-Neil return; 255484cc0778SGeorge V. Neville-Neil } 255584cc0778SGeorge V. Neville-Neil 255684cc0778SGeorge V. Neville-Neil /* 2557a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2558a557af22SRobert Watson * label change into the in_pcb for the socket. 2559a557af22SRobert Watson */ 2560a557af22SRobert Watson void 2561136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2562a557af22SRobert Watson { 2563a557af22SRobert Watson #ifdef MAC 2564a557af22SRobert Watson struct inpcb *inp; 2565a557af22SRobert Watson 25664c7c478dSRobert Watson inp = sotoinpcb(so); 25674c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2568602cc7f1SRobert Watson 25698501a69cSRobert Watson INP_WLOCK(inp); 2570310e7cebSRobert Watson SOCK_LOCK(so); 2571a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2572310e7cebSRobert Watson SOCK_UNLOCK(so); 25738501a69cSRobert Watson INP_WUNLOCK(inp); 2574a557af22SRobert Watson #endif 2575a557af22SRobert Watson } 25765f311da2SMike Silbersack 25775f311da2SMike Silbersack /* 2578ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2579ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2580ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2581ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2582ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 25835f311da2SMike Silbersack */ 2584aae49dd3SBjoern A. Zeeb static void 2585136d4f1cSRobert Watson ipport_tick(void *xtp) 25865f311da2SMike Silbersack { 25878b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2588ad3a630fSRobert Watson 25895ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 25908b615593SMarko Zec VNET_FOREACH(vnet_iter) { 25918b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 25924c018b5aSPeter Lei if (V_ipport_tcpallocs - V_ipport_tcplastcount <= 25934c018b5aSPeter Lei V_ipport_randomcps) { 2594603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2595603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2596ad3a630fSRobert Watson } else 2597603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2598603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 25998b615593SMarko Zec CURVNET_RESTORE(); 26008b615593SMarko Zec } 26015ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 26025f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 26035f311da2SMike Silbersack } 2604497057eeSRobert Watson 2605aae49dd3SBjoern A. Zeeb static void 2606aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2607aae49dd3SBjoern A. Zeeb { 2608aae49dd3SBjoern A. Zeeb 2609aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2610aae49dd3SBjoern A. Zeeb } 2611aae49dd3SBjoern A. Zeeb 2612aae49dd3SBjoern A. Zeeb /* 2613aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2614aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2615aae49dd3SBjoern A. Zeeb */ 2616aae49dd3SBjoern A. Zeeb static void 2617aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2618aae49dd3SBjoern A. Zeeb { 2619aae49dd3SBjoern A. Zeeb 2620aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2621fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2622aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2623aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2624aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2625aae49dd3SBjoern A. Zeeb } 2626aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2627aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2628aae49dd3SBjoern A. Zeeb 26293d585327SKip Macy void 26303d585327SKip Macy inp_wlock(struct inpcb *inp) 26313d585327SKip Macy { 26323d585327SKip Macy 26338501a69cSRobert Watson INP_WLOCK(inp); 26343d585327SKip Macy } 26353d585327SKip Macy 26363d585327SKip Macy void 26373d585327SKip Macy inp_wunlock(struct inpcb *inp) 26383d585327SKip Macy { 26393d585327SKip Macy 26408501a69cSRobert Watson INP_WUNLOCK(inp); 26413d585327SKip Macy } 26423d585327SKip Macy 26433d585327SKip Macy void 26443d585327SKip Macy inp_rlock(struct inpcb *inp) 26453d585327SKip Macy { 26463d585327SKip Macy 2647a69042a5SRobert Watson INP_RLOCK(inp); 26483d585327SKip Macy } 26493d585327SKip Macy 26503d585327SKip Macy void 26513d585327SKip Macy inp_runlock(struct inpcb *inp) 26523d585327SKip Macy { 26533d585327SKip Macy 2654a69042a5SRobert Watson INP_RUNLOCK(inp); 26553d585327SKip Macy } 26563d585327SKip Macy 2657c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 26583d585327SKip Macy void 2659e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 26603d585327SKip Macy { 26613d585327SKip Macy 26628501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 26633d585327SKip Macy } 26643d585327SKip Macy 26653d585327SKip Macy void 2666e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 26673d585327SKip Macy { 26683d585327SKip Macy 26693d585327SKip Macy INP_UNLOCK_ASSERT(inp); 26703d585327SKip Macy } 26713d585327SKip Macy #endif 26723d585327SKip Macy 26739378e437SKip Macy void 26749378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 26759378e437SKip Macy { 2676db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo, 2677db0ac6deSCy Schubert INPLOOKUP_WLOCKPCB); 26789378e437SKip Macy struct inpcb *inp; 26799378e437SKip Macy 2680db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) 26819378e437SKip Macy func(inp, arg); 26829378e437SKip Macy } 26839378e437SKip Macy 26849378e437SKip Macy struct socket * 26859378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 26869378e437SKip Macy { 26879378e437SKip Macy 26889378e437SKip Macy INP_WLOCK_ASSERT(inp); 26899378e437SKip Macy return (inp->inp_socket); 26909378e437SKip Macy } 26919378e437SKip Macy 26929378e437SKip Macy struct tcpcb * 26939378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 26949378e437SKip Macy { 26959378e437SKip Macy 26969378e437SKip Macy INP_WLOCK_ASSERT(inp); 26979378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 26989378e437SKip Macy } 26999378e437SKip Macy 27009378e437SKip Macy int 27019378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 27029378e437SKip Macy { 27039378e437SKip Macy 27049378e437SKip Macy return (inp->inp_ip_tos); 27059378e437SKip Macy } 27069378e437SKip Macy 27079378e437SKip Macy void 27089378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 27099378e437SKip Macy { 27109378e437SKip Macy 27119378e437SKip Macy inp->inp_ip_tos = val; 27129378e437SKip Macy } 27139378e437SKip Macy 27149378e437SKip Macy void 2715df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 27169d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 27179378e437SKip Macy { 27189378e437SKip Macy 27199d29c635SKip Macy INP_LOCK_ASSERT(inp); 2720df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2721df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 27229378e437SKip Macy *lp = inp->inp_lport; 27239378e437SKip Macy *fp = inp->inp_fport; 27249378e437SKip Macy } 27259378e437SKip Macy 2726dd0e6c38SKip Macy struct inpcb * 2727dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2728dd0e6c38SKip Macy { 2729dd0e6c38SKip Macy 2730dd0e6c38SKip Macy return (sotoinpcb(so)); 2731dd0e6c38SKip Macy } 2732dd0e6c38SKip Macy 2733dd0e6c38SKip Macy struct tcpcb * 2734dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2735dd0e6c38SKip Macy { 2736dd0e6c38SKip Macy 2737dd0e6c38SKip Macy return (sototcpcb(so)); 2738dd0e6c38SKip Macy } 2739dd0e6c38SKip Macy 2740cc65eb4eSGleb Smirnoff /* 2741cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2742cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2743cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2744cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2745cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2746cc65eb4eSGleb Smirnoff * hidden from users. 2747cc65eb4eSGleb Smirnoff */ 2748cc65eb4eSGleb Smirnoff void 2749cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2750cc65eb4eSGleb Smirnoff { 2751cc65eb4eSGleb Smirnoff 275279db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2753cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2754cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2755cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2756cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2757cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 27583a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2759cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2760cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2761cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2762cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2763cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2764cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2765cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2766cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2767cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2768cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2769cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2770cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2771cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2772cc65eb4eSGleb Smirnoff } 2773cc65eb4eSGleb Smirnoff 2774497057eeSRobert Watson #ifdef DDB 2775497057eeSRobert Watson static void 2776497057eeSRobert Watson db_print_indent(int indent) 2777497057eeSRobert Watson { 2778497057eeSRobert Watson int i; 2779497057eeSRobert Watson 2780497057eeSRobert Watson for (i = 0; i < indent; i++) 2781497057eeSRobert Watson db_printf(" "); 2782497057eeSRobert Watson } 2783497057eeSRobert Watson 2784497057eeSRobert Watson static void 2785497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2786497057eeSRobert Watson { 2787497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2788497057eeSRobert Watson 2789497057eeSRobert Watson db_print_indent(indent); 2790497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2791497057eeSRobert Watson 2792497057eeSRobert Watson indent += 2; 2793497057eeSRobert Watson 279403dc38a4SRobert Watson #ifdef INET6 2795dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2796497057eeSRobert Watson /* IPv6. */ 2797497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2798497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 279983e521ecSBjoern A. Zeeb } else 280003dc38a4SRobert Watson #endif 280183e521ecSBjoern A. Zeeb { 2802497057eeSRobert Watson /* IPv4. */ 2803497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2804497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2805497057eeSRobert Watson } 2806497057eeSRobert Watson db_print_indent(indent); 2807497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2808497057eeSRobert Watson ntohs(inc->inc_lport)); 2809497057eeSRobert Watson db_print_indent(indent); 2810497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2811497057eeSRobert Watson ntohs(inc->inc_fport)); 2812497057eeSRobert Watson } 2813497057eeSRobert Watson 2814497057eeSRobert Watson static void 2815497057eeSRobert Watson db_print_inpflags(int inp_flags) 2816497057eeSRobert Watson { 2817497057eeSRobert Watson int comma; 2818497057eeSRobert Watson 2819497057eeSRobert Watson comma = 0; 2820497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2821497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2822497057eeSRobert Watson comma = 1; 2823497057eeSRobert Watson } 2824497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2825497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2826497057eeSRobert Watson comma = 1; 2827497057eeSRobert Watson } 2828497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2829497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2830497057eeSRobert Watson comma = 1; 2831497057eeSRobert Watson } 2832dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2833dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2834dce33a45SErmal Luçi comma = 1; 2835dce33a45SErmal Luçi } 2836497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2837497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2838497057eeSRobert Watson comma = 1; 2839497057eeSRobert Watson } 2840497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2841497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2842497057eeSRobert Watson comma = 1; 2843497057eeSRobert Watson } 2844497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2845497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2846497057eeSRobert Watson comma = 1; 2847497057eeSRobert Watson } 2848497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2849497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2850497057eeSRobert Watson comma = 1; 2851497057eeSRobert Watson } 2852497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2853497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2854497057eeSRobert Watson comma = 1; 2855497057eeSRobert Watson } 2856497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2857497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2858497057eeSRobert Watson comma = 1; 2859497057eeSRobert Watson } 2860497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2861497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2862497057eeSRobert Watson comma = 1; 2863497057eeSRobert Watson } 2864497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2865497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2866497057eeSRobert Watson comma = 1; 2867497057eeSRobert Watson } 28683cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 28693cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 28703cca425bSMichael Tuexen comma = 1; 28713cca425bSMichael Tuexen } 2872497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2873497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2874497057eeSRobert Watson comma = 1; 2875497057eeSRobert Watson } 2876497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2877497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2878497057eeSRobert Watson comma = 1; 2879497057eeSRobert Watson } 2880497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2881497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2882497057eeSRobert Watson comma = 1; 2883497057eeSRobert Watson } 2884497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2885497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2886497057eeSRobert Watson comma = 1; 2887497057eeSRobert Watson } 2888497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2889497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2890497057eeSRobert Watson comma = 1; 2891497057eeSRobert Watson } 2892497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2893497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2894497057eeSRobert Watson comma = 1; 2895497057eeSRobert Watson } 2896497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2897497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2898497057eeSRobert Watson comma = 1; 2899497057eeSRobert Watson } 2900497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2901497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2902497057eeSRobert Watson comma = 1; 2903497057eeSRobert Watson } 2904497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2905497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2906497057eeSRobert Watson comma = 1; 2907497057eeSRobert Watson } 2908ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2909ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2910ad71fe3cSRobert Watson comma = 1; 2911ad71fe3cSRobert Watson } 2912ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2913ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2914ad71fe3cSRobert Watson comma = 1; 2915ad71fe3cSRobert Watson } 2916ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2917ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2918ad71fe3cSRobert Watson comma = 1; 2919ad71fe3cSRobert Watson } 2920ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2921ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2922ad71fe3cSRobert Watson comma = 1; 2923ad71fe3cSRobert Watson } 2924497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2925497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2926497057eeSRobert Watson comma = 1; 2927497057eeSRobert Watson } 2928497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2929497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2930497057eeSRobert Watson comma = 1; 2931497057eeSRobert Watson } 2932497057eeSRobert Watson } 2933497057eeSRobert Watson 2934497057eeSRobert Watson static void 2935497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2936497057eeSRobert Watson { 2937497057eeSRobert Watson int comma; 2938497057eeSRobert Watson 2939497057eeSRobert Watson comma = 0; 2940497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2941497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2942497057eeSRobert Watson comma = 1; 2943497057eeSRobert Watson } 2944497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2945497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2946497057eeSRobert Watson comma = 1; 2947497057eeSRobert Watson } 2948497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2949497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2950497057eeSRobert Watson comma = 1; 2951497057eeSRobert Watson } 2952497057eeSRobert Watson } 2953497057eeSRobert Watson 29546d888973SRobert Watson static void 2955497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2956497057eeSRobert Watson { 2957497057eeSRobert Watson 2958497057eeSRobert Watson db_print_indent(indent); 2959497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2960497057eeSRobert Watson 2961497057eeSRobert Watson indent += 2; 2962497057eeSRobert Watson 2963497057eeSRobert Watson db_print_indent(indent); 2964497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2965497057eeSRobert Watson 2966497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2967497057eeSRobert Watson 2968497057eeSRobert Watson db_print_indent(indent); 2969497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2970497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2971497057eeSRobert Watson 2972497057eeSRobert Watson db_print_indent(indent); 2973497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2974497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2975497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2976497057eeSRobert Watson db_printf(")\n"); 2977497057eeSRobert Watson 2978497057eeSRobert Watson db_print_indent(indent); 2979497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2980497057eeSRobert Watson inp->inp_vflag); 2981497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2982497057eeSRobert Watson db_printf(")\n"); 2983497057eeSRobert Watson 2984497057eeSRobert Watson db_print_indent(indent); 2985497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2986497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2987497057eeSRobert Watson 2988497057eeSRobert Watson db_print_indent(indent); 2989497057eeSRobert Watson #ifdef INET6 2990497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2991497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2992497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2993497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2994497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2995497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2996497057eeSRobert Watson inp->in6p_hops); 2997497057eeSRobert Watson } else 2998497057eeSRobert Watson #endif 2999497057eeSRobert Watson { 3000497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3001497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3002497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3003497057eeSRobert Watson } 3004497057eeSRobert Watson 3005497057eeSRobert Watson db_print_indent(indent); 3006497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3007497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3008497057eeSRobert Watson } 3009497057eeSRobert Watson 3010497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3011497057eeSRobert Watson { 3012497057eeSRobert Watson struct inpcb *inp; 3013497057eeSRobert Watson 3014497057eeSRobert Watson if (!have_addr) { 3015497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3016497057eeSRobert Watson return; 3017497057eeSRobert Watson } 3018497057eeSRobert Watson inp = (struct inpcb *)addr; 3019497057eeSRobert Watson 3020497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3021497057eeSRobert Watson } 302283e521ecSBjoern A. Zeeb #endif /* DDB */ 3023f3e7afe2SHans Petter Selasky 3024f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3025f3e7afe2SHans Petter Selasky /* 3026f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3027f3e7afe2SHans Petter Selasky * if any. 3028f3e7afe2SHans Petter Selasky */ 3029f3e7afe2SHans Petter Selasky int 3030f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3031f3e7afe2SHans Petter Selasky { 3032f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3033f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 303420abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3035f3e7afe2SHans Petter Selasky }; 3036f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3037f3e7afe2SHans Petter Selasky int error; 3038f3e7afe2SHans Petter Selasky 3039f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3040f3e7afe2SHans Petter Selasky if (mst == NULL) 3041f3e7afe2SHans Petter Selasky return (EINVAL); 3042f3e7afe2SHans Petter Selasky 3043c782ea8bSJohn Baldwin if (mst->sw->snd_tag_modify == NULL) { 3044f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3045f3e7afe2SHans Petter Selasky } else { 3046c782ea8bSJohn Baldwin error = mst->sw->snd_tag_modify(mst, ¶ms); 3047f3e7afe2SHans Petter Selasky } 3048f3e7afe2SHans Petter Selasky return (error); 3049f3e7afe2SHans Petter Selasky } 3050f3e7afe2SHans Petter Selasky 3051f3e7afe2SHans Petter Selasky /* 3052f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3053f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3054f3e7afe2SHans Petter Selasky */ 3055f3e7afe2SHans Petter Selasky int 3056f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3057f3e7afe2SHans Petter Selasky { 3058f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3059f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3060f3e7afe2SHans Petter Selasky int error; 3061f3e7afe2SHans Petter Selasky 3062f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3063f3e7afe2SHans Petter Selasky if (mst == NULL) 3064f3e7afe2SHans Petter Selasky return (EINVAL); 3065f3e7afe2SHans Petter Selasky 3066c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) { 3067f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3068f3e7afe2SHans Petter Selasky } else { 3069c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 3070f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3071f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3072f3e7afe2SHans Petter Selasky } 3073f3e7afe2SHans Petter Selasky return (error); 3074f3e7afe2SHans Petter Selasky } 3075f3e7afe2SHans Petter Selasky 3076f3e7afe2SHans Petter Selasky /* 307795ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 307895ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 307995ed5015SHans Petter Selasky */ 308095ed5015SHans Petter Selasky int 308195ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 308295ed5015SHans Petter Selasky { 308395ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 308495ed5015SHans Petter Selasky struct m_snd_tag *mst; 308595ed5015SHans Petter Selasky int error; 308695ed5015SHans Petter Selasky 308795ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 308895ed5015SHans Petter Selasky if (mst == NULL) 308995ed5015SHans Petter Selasky return (EINVAL); 309095ed5015SHans Petter Selasky 3091c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) 309295ed5015SHans Petter Selasky return (EOPNOTSUPP); 309395ed5015SHans Petter Selasky 3094c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 309595ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 309695ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 309795ed5015SHans Petter Selasky return (error); 309895ed5015SHans Petter Selasky } 309995ed5015SHans Petter Selasky 310095ed5015SHans Petter Selasky /* 3101f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3102f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3103f3e7afe2SHans Petter Selasky */ 3104f3e7afe2SHans Petter Selasky int 3105f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 310620abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 310720abea66SRandall Stewart 3108f3e7afe2SHans Petter Selasky { 3109f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 311095ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 311195ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3112f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3113f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 311498085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3115f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 311620abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3117f3e7afe2SHans Petter Selasky }; 3118f3e7afe2SHans Petter Selasky int error; 3119f3e7afe2SHans Petter Selasky 3120f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3121f3e7afe2SHans Petter Selasky 312285d8d30fSHans Petter Selasky /* 312385d8d30fSHans Petter Selasky * If there is already a send tag, or the INP is being torn 312485d8d30fSHans Petter Selasky * down, allocating a new send tag is not allowed. Else send 312585d8d30fSHans Petter Selasky * tags may leak. 312685d8d30fSHans Petter Selasky */ 312785d8d30fSHans Petter Selasky if (*st != NULL || (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) != 0) 3128f3e7afe2SHans Petter Selasky return (EINVAL); 3129f3e7afe2SHans Petter Selasky 313036e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, st); 3131903c4ee6SXin LI #ifdef INET 313220abea66SRandall Stewart if (error == 0) { 313320abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 313420abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 313536e0a362SJohn Baldwin } else if (error != EOPNOTSUPP) 313620abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3137903c4ee6SXin LI #endif 3138f3e7afe2SHans Petter Selasky return (error); 3139f3e7afe2SHans Petter Selasky } 3140f3e7afe2SHans Petter Selasky 314120abea66SRandall Stewart void 314298d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst) 314320abea66SRandall Stewart { 314420abea66SRandall Stewart 314598d7a8d9SJohn Baldwin m_snd_tag_rele(mst); 3146903c4ee6SXin LI #ifdef INET 314720abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3148903c4ee6SXin LI #endif 314920abea66SRandall Stewart } 315020abea66SRandall Stewart 3151f3e7afe2SHans Petter Selasky /* 3152f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3153f3e7afe2SHans Petter Selasky * if any: 3154f3e7afe2SHans Petter Selasky */ 3155f3e7afe2SHans Petter Selasky void 3156f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3157f3e7afe2SHans Petter Selasky { 3158f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3159f3e7afe2SHans Petter Selasky 3160f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3161f3e7afe2SHans Petter Selasky 3162f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3163f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3164f3e7afe2SHans Petter Selasky 3165f3e7afe2SHans Petter Selasky if (mst == NULL) 3166f3e7afe2SHans Petter Selasky return; 3167f3e7afe2SHans Petter Selasky 3168fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3169093e7231SHans Petter Selasky #ifdef INET 3170093e7231SHans Petter Selasky counter_u64_add(rate_limit_active, -1); 3171093e7231SHans Petter Selasky #endif 3172f3e7afe2SHans Petter Selasky } 3173f3e7afe2SHans Petter Selasky 317420abea66SRandall Stewart int 317520abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 317620abea66SRandall Stewart { 317720abea66SRandall Stewart int error; 317820abea66SRandall Stewart 317920abea66SRandall Stewart /* 318020abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 318120abea66SRandall Stewart * a route change, first drop the existing tag. Set the 318220abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 318320abea66SRandall Stewart * tag if we fail to allocate one this time. 318420abea66SRandall Stewart */ 318520abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 318620abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 318720abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 318820abea66SRandall Stewart } 318920abea66SRandall Stewart 319020abea66SRandall Stewart /* 319120abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 319220abea66SRandall Stewart * made to ensure the network interface doesn't go away until 319320abea66SRandall Stewart * all ratelimit connections are gone. The network interface 319420abea66SRandall Stewart * pointers compared below represent valid network interfaces, 319520abea66SRandall Stewart * except when comparing towards NULL. 319620abea66SRandall Stewart */ 319720abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 319820abea66SRandall Stewart error = 0; 319920abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 320020abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 320120abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 320220abea66SRandall Stewart error = 0; 320320abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 320420abea66SRandall Stewart /* 320520abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 320620abea66SRandall Stewart * to wait until there is a valid RSS hash before we 320720abea66SRandall Stewart * can proceed: 320820abea66SRandall Stewart */ 320920abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 321020abea66SRandall Stewart error = EAGAIN; 321120abea66SRandall Stewart } else { 321220abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 321320abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 321420abea66SRandall Stewart } 321520abea66SRandall Stewart } else { 321620abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 321720abea66SRandall Stewart } 321820abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 321920abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 322020abea66SRandall Stewart 322120abea66SRandall Stewart return (error); 322220abea66SRandall Stewart } 322320abea66SRandall Stewart 3224f3e7afe2SHans Petter Selasky /* 3225f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3226f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3227f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3228f3e7afe2SHans Petter Selasky */ 3229f3e7afe2SHans Petter Selasky void 3230f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3231f3e7afe2SHans Petter Selasky { 3232f3e7afe2SHans Petter Selasky struct socket *socket; 3233f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3234f3e7afe2SHans Petter Selasky bool did_upgrade; 3235f3e7afe2SHans Petter Selasky int error; 3236f3e7afe2SHans Petter Selasky 3237f3e7afe2SHans Petter Selasky if (inp == NULL) 3238f3e7afe2SHans Petter Selasky return; 3239f3e7afe2SHans Petter Selasky 3240f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3241f3e7afe2SHans Petter Selasky if (socket == NULL) 3242f3e7afe2SHans Petter Selasky return; 3243f3e7afe2SHans Petter Selasky 3244f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3245f3e7afe2SHans Petter Selasky /* 3246f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3247f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3248f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3249f3e7afe2SHans Petter Selasky * write lock. 3250f3e7afe2SHans Petter Selasky */ 3251f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3252f3e7afe2SHans Petter Selasky return; 3253f3e7afe2SHans Petter Selasky did_upgrade = 1; 3254f3e7afe2SHans Petter Selasky } else { 3255f3e7afe2SHans Petter Selasky did_upgrade = 0; 3256f3e7afe2SHans Petter Selasky } 3257f3e7afe2SHans Petter Selasky 3258f3e7afe2SHans Petter Selasky /* 3259f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3260f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3261f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3262f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3263f3e7afe2SHans Petter Selasky */ 3264f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3265f3e7afe2SHans Petter Selasky 326620abea66SRandall Stewart error = in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3267fb3bc596SJohn Baldwin 3268f3e7afe2SHans Petter Selasky if (did_upgrade) 3269f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3270f3e7afe2SHans Petter Selasky } 3271f3e7afe2SHans Petter Selasky 3272f3e7afe2SHans Petter Selasky /* 3273f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3274f3e7afe2SHans Petter Selasky */ 3275f3e7afe2SHans Petter Selasky void 3276f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3277f3e7afe2SHans Petter Selasky { 3278f3e7afe2SHans Petter Selasky bool did_upgrade; 3279f3e7afe2SHans Petter Selasky 3280f3e7afe2SHans Petter Selasky if (inp == NULL) 3281f3e7afe2SHans Petter Selasky return; 3282f3e7afe2SHans Petter Selasky 3283f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3284f3e7afe2SHans Petter Selasky return; 3285f3e7afe2SHans Petter Selasky 3286f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3287f3e7afe2SHans Petter Selasky /* 3288f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3289f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3290f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3291f3e7afe2SHans Petter Selasky * write lock. 3292f3e7afe2SHans Petter Selasky */ 3293f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3294f3e7afe2SHans Petter Selasky return; 3295f3e7afe2SHans Petter Selasky did_upgrade = 1; 3296f3e7afe2SHans Petter Selasky } else { 3297f3e7afe2SHans Petter Selasky did_upgrade = 0; 3298f3e7afe2SHans Petter Selasky } 3299f3e7afe2SHans Petter Selasky 3300f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3301f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3302f3e7afe2SHans Petter Selasky 3303f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3304f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3305f3e7afe2SHans Petter Selasky 3306f3e7afe2SHans Petter Selasky if (did_upgrade) 3307f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3308f3e7afe2SHans Petter Selasky } 330920abea66SRandall Stewart 3310903c4ee6SXin LI #ifdef INET 331120abea66SRandall Stewart static void 331220abea66SRandall Stewart rl_init(void *st) 331320abea66SRandall Stewart { 33141a714ff2SRandall Stewart rate_limit_new = counter_u64_alloc(M_WAITOK); 33151a714ff2SRandall Stewart rate_limit_chg = counter_u64_alloc(M_WAITOK); 331620abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 331720abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 331820abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 331920abea66SRandall Stewart } 332020abea66SRandall Stewart 332120abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3322903c4ee6SXin LI #endif 3323f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3324