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 116*db0ac6deSCy 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 505c3229e05SDavid Greenman /* 506cc487c16SGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 507cc487c16SGleb Smirnoff * different name to the lock. But they all are disposed the same. 508cc487c16SGleb Smirnoff */ 509cc487c16SGleb Smirnoff static void 510cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size) 511cc487c16SGleb Smirnoff { 512cc487c16SGleb Smirnoff struct inpcb *inp = mem; 513cc487c16SGleb Smirnoff 514cc487c16SGleb Smirnoff INP_LOCK_DESTROY(inp); 515cc487c16SGleb Smirnoff } 516cc487c16SGleb Smirnoff 517*db0ac6deSCy Schubert /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 518*db0ac6deSCy Schubert CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb))); 519*db0ac6deSCy Schubert 520cc487c16SGleb Smirnoff /* 5219bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 5229bcd427bSRobert Watson * arguments in time. 5239bcd427bSRobert Watson */ 5249bcd427bSRobert Watson void 5259bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 526*db0ac6deSCy Schubert u_int hash_nelements, int porthash_nelements, char *inpcbzone_name, 527*db0ac6deSCy Schubert uma_init inpcbzone_init) 5289bcd427bSRobert Watson { 5299bcd427bSRobert Watson 530*db0ac6deSCy Schubert mtx_init(&pcbinfo->ipi_lock, name, NULL, MTX_DEF); 531*db0ac6deSCy Schubert mtx_init(&pcbinfo->ipi_hash_lock, "pcbinfohash", NULL, MTX_DEF); 5329bcd427bSRobert Watson #ifdef VIMAGE 5339bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 5349bcd427bSRobert Watson #endif 535*db0ac6deSCy Schubert CK_LIST_INIT(&pcbinfo->ipi_listhead); 536fa046d87SRobert Watson pcbinfo->ipi_count = 0; 5379bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 5389bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 539*db0ac6deSCy Schubert porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 5409bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 5419bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 5429d2877fcSMark Johnston pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 5431a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 5449bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 545*db0ac6deSCy Schubert NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 546*db0ac6deSCy Schubert UMA_ZONE_SMR); 5479bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 5486acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 5496acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 550*db0ac6deSCy Schubert pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone); 551*db0ac6deSCy Schubert pcbinfo->ipi_portzone = uma_zcreate(inpcbzone_name, 552*db0ac6deSCy Schubert sizeof(struct inpcbport), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 553*db0ac6deSCy Schubert uma_zone_set_smr(pcbinfo->ipi_portzone, pcbinfo->ipi_smr); 5549bcd427bSRobert Watson } 5559bcd427bSRobert Watson 5569bcd427bSRobert Watson /* 5579bcd427bSRobert Watson * Destroy an inpcbinfo. 5589bcd427bSRobert Watson */ 5599bcd427bSRobert Watson void 5609bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 5619bcd427bSRobert Watson { 5629bcd427bSRobert Watson 563fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 564fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 565fa046d87SRobert Watson 5669bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 5679bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 5689bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 5691a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 5701a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 5719bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 572*db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_hash_lock); 573*db0ac6deSCy Schubert mtx_destroy(&pcbinfo->ipi_lock); 5749bcd427bSRobert Watson } 5759bcd427bSRobert Watson 5769bcd427bSRobert Watson /* 577c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 578d915b280SStephan Uphoff * On success return with the PCB locked. 579c3229e05SDavid Greenman */ 580df8bae1dSRodney W. Grimes int 581d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 582df8bae1dSRodney W. Grimes { 583136d4f1cSRobert Watson struct inpcb *inp; 58413cf67f3SHajimu UMEMOTO int error; 585a557af22SRobert Watson 586a557af22SRobert Watson error = 0; 587*db0ac6deSCy Schubert inp = uma_zalloc_smr(pcbinfo->ipi_zone, M_NOWAIT); 588df8bae1dSRodney W. Grimes if (inp == NULL) 589df8bae1dSRodney W. Grimes return (ENOBUFS); 5906a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 59150575ce1SAndrew Gallatin #ifdef NUMA 59250575ce1SAndrew Gallatin inp->inp_numa_domain = M_NODOM; 59350575ce1SAndrew Gallatin #endif 59415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 595df8bae1dSRodney W. Grimes inp->inp_socket = so; 59686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 5978b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 598a557af22SRobert Watson #ifdef MAC 59930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 600a557af22SRobert Watson if (error != 0) 601a557af22SRobert Watson goto out; 60230d239bcSRobert Watson mac_inpcb_create(so, inp); 603a557af22SRobert Watson #endif 604fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 605fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 6060bffde27SRobert Watson if (error != 0) { 6070bffde27SRobert Watson #ifdef MAC 6080bffde27SRobert Watson mac_inpcb_destroy(inp); 6090bffde27SRobert Watson #endif 610a557af22SRobert Watson goto out; 6110bffde27SRobert Watson } 612b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 613e3fd5ffdSRobert Watson #ifdef INET6 614340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 615340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 616603724d3SBjoern A. Zeeb if (V_ip6_v6only) 61733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 618340c35deSJonathan Lemon } 619603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 62033841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 62133841545SHajimu UMEMOTO #endif 6228c1960d5SMike Karels /* 6238c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 6248c1960d5SMike Karels * to be cleaned up. 6258c1960d5SMike Karels */ 6268c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 627*db0ac6deSCy Schubert #ifdef TCPHPTS 628*db0ac6deSCy Schubert /* 629*db0ac6deSCy Schubert * If using hpts lets drop a random number in so 630*db0ac6deSCy Schubert * not all new connections fall on the same CPU. 631*db0ac6deSCy Schubert */ 632*db0ac6deSCy Schubert inp->inp_hpts_cpu = inp->inp_dropq_cpu = hpts_random_cpu(inp); 633*db0ac6deSCy Schubert #endif 634*db0ac6deSCy Schubert refcount_init(&inp->inp_refcount, 1); /* Reference from socket. */ 635*db0ac6deSCy Schubert INP_WLOCK(inp); 636*db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 637*db0ac6deSCy Schubert pcbinfo->ipi_count++; 638*db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 639*db0ac6deSCy Schubert CK_LIST_INSERT_HEAD(&pcbinfo->ipi_listhead, inp, inp_list); 640*db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 641*db0ac6deSCy Schubert so->so_pcb = inp; 642*db0ac6deSCy Schubert 643*db0ac6deSCy Schubert return (0); 644*db0ac6deSCy Schubert 6457a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 646a557af22SRobert Watson out: 64786d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 648*db0ac6deSCy Schubert uma_zfree_smr(pcbinfo->ipi_zone, inp); 649266f97b5SCy Schubert return (error); 650*db0ac6deSCy Schubert #endif 651df8bae1dSRodney W. Grimes } 652df8bae1dSRodney W. Grimes 65367107f45SBjoern A. Zeeb #ifdef INET 654df8bae1dSRodney W. Grimes int 655136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 656df8bae1dSRodney W. Grimes { 6574b932371SIan Dowse int anonport, error; 6584b932371SIan Dowse 659f161d294SMark Johnston KASSERT(nam == NULL || nam->sa_family == AF_INET, 660f161d294SMark Johnston ("%s: invalid address family for %p", __func__, nam)); 661f161d294SMark Johnston KASSERT(nam == NULL || nam->sa_len == sizeof(struct sockaddr_in), 662f161d294SMark Johnston ("%s: invalid address length for %p", __func__, nam)); 6638501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 664fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 66559daba27SSam Leffler 6664b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 6674b932371SIan Dowse return (EINVAL); 6685cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 6694b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 670b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 6714b932371SIan Dowse if (error) 6724b932371SIan Dowse return (error); 6734b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 6744b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 6754b932371SIan Dowse inp->inp_lport = 0; 6764b932371SIan Dowse return (EAGAIN); 6774b932371SIan Dowse } 6784b932371SIan Dowse if (anonport) 6794b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 6804b932371SIan Dowse return (0); 6814b932371SIan Dowse } 68267107f45SBjoern A. Zeeb #endif 6834b932371SIan Dowse 684efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 68525102351SMike Karels /* 68625102351SMike Karels * Assign a local port like in_pcb_lport(), but also used with connect() 68725102351SMike Karels * and a foreign address and port. If fsa is non-NULL, choose a local port 68825102351SMike Karels * that is unused with those, otherwise one that is completely unused. 6892fdbcbeaSMike Karels * lsa can be NULL for IPv6. 69025102351SMike Karels */ 691efc76f72SBjoern A. Zeeb int 69225102351SMike Karels in_pcb_lport_dest(struct inpcb *inp, struct sockaddr *lsa, u_short *lportp, 69325102351SMike Karels struct sockaddr *fsa, u_short fport, struct ucred *cred, int lookupflags) 694efc76f72SBjoern A. Zeeb { 695efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 696efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 697efc76f72SBjoern A. Zeeb unsigned short *lastport; 698efc76f72SBjoern A. Zeeb int count, dorandom, error; 699efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 700efc76f72SBjoern A. Zeeb #ifdef INET 70125102351SMike Karels struct in_addr laddr, faddr; 70225102351SMike Karels #endif 70325102351SMike Karels #ifdef INET6 70425102351SMike Karels struct in6_addr *laddr6, *faddr6; 705efc76f72SBjoern A. Zeeb #endif 706efc76f72SBjoern A. Zeeb 707efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 708efc76f72SBjoern A. Zeeb 709efc76f72SBjoern A. Zeeb /* 710efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 711efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 712efc76f72SBjoern A. Zeeb */ 713efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 714fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 715efc76f72SBjoern A. Zeeb 716efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 717efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 718efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 719efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 720efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 721cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 722efc76f72SBjoern A. Zeeb if (error) 723efc76f72SBjoern A. Zeeb return (error); 724efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 725efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 726efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 727efc76f72SBjoern A. Zeeb } else { 728efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 729efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 730efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 731efc76f72SBjoern A. Zeeb } 732efc76f72SBjoern A. Zeeb /* 733e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 734efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 735efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 736efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 737efc76f72SBjoern A. Zeeb */ 738efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 739e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 740e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 741efc76f72SBjoern A. Zeeb dorandom = 1; 742efc76f72SBjoern A. Zeeb else 743efc76f72SBjoern A. Zeeb dorandom = 0; 744efc76f72SBjoern A. Zeeb /* 745efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 746efc76f72SBjoern A. Zeeb * we have the only port available. 747efc76f72SBjoern A. Zeeb */ 748efc76f72SBjoern A. Zeeb if (first == last) 749efc76f72SBjoern A. Zeeb dorandom = 0; 750e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 751e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 752efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 753efc76f72SBjoern A. Zeeb /* 754efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 755efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 756efc76f72SBjoern A. Zeeb * code path implementing all logic. 757efc76f72SBjoern A. Zeeb */ 758efc76f72SBjoern A. Zeeb if (first > last) { 759efc76f72SBjoern A. Zeeb aux = first; 760efc76f72SBjoern A. Zeeb first = last; 761efc76f72SBjoern A. Zeeb last = aux; 762efc76f72SBjoern A. Zeeb } 763efc76f72SBjoern A. Zeeb 764efc76f72SBjoern A. Zeeb #ifdef INET 76525102351SMike Karels laddr.s_addr = INADDR_ANY; 7664d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 7672fdbcbeaSMike Karels if (lsa != NULL) 76825102351SMike Karels laddr = ((struct sockaddr_in *)lsa)->sin_addr; 76925102351SMike Karels if (fsa != NULL) 77025102351SMike Karels faddr = ((struct sockaddr_in *)fsa)->sin_addr; 771efc76f72SBjoern A. Zeeb } 772efc76f72SBjoern A. Zeeb #endif 77325102351SMike Karels #ifdef INET6 7742fdbcbeaSMike Karels laddr6 = NULL; 7752fdbcbeaSMike Karels if ((inp->inp_vflag & INP_IPV6) != 0) { 7762fdbcbeaSMike Karels if (lsa != NULL) 77725102351SMike Karels laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 77825102351SMike Karels if (fsa != NULL) 77925102351SMike Karels faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 78025102351SMike Karels } 78125102351SMike Karels #endif 78225102351SMike Karels 78325102351SMike Karels tmpinp = NULL; 784efc76f72SBjoern A. Zeeb lport = *lportp; 785efc76f72SBjoern A. Zeeb 786efc76f72SBjoern A. Zeeb if (dorandom) 787efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 788efc76f72SBjoern A. Zeeb 789efc76f72SBjoern A. Zeeb count = last - first; 790efc76f72SBjoern A. Zeeb 791efc76f72SBjoern A. Zeeb do { 792efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 793efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 794efc76f72SBjoern A. Zeeb ++*lastport; 795efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 796efc76f72SBjoern A. Zeeb *lastport = first; 797efc76f72SBjoern A. Zeeb lport = htons(*lastport); 798efc76f72SBjoern A. Zeeb 79925102351SMike Karels if (fsa != NULL) { 80025102351SMike Karels #ifdef INET 80125102351SMike Karels if (lsa->sa_family == AF_INET) { 80225102351SMike Karels tmpinp = in_pcblookup_hash_locked(pcbinfo, 80325102351SMike Karels faddr, fport, laddr, lport, lookupflags, 804a034518aSAndrew Gallatin NULL, M_NODOM); 80525102351SMike Karels } 80625102351SMike Karels #endif 80725102351SMike Karels #ifdef INET6 80825102351SMike Karels if (lsa->sa_family == AF_INET6) { 80925102351SMike Karels tmpinp = in6_pcblookup_hash_locked(pcbinfo, 81025102351SMike Karels faddr6, fport, laddr6, lport, lookupflags, 811a034518aSAndrew Gallatin NULL, M_NODOM); 81225102351SMike Karels } 81325102351SMike Karels #endif 81425102351SMike Karels } else { 815efc76f72SBjoern A. Zeeb #ifdef INET6 8164616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 817efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 81868e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 819efc76f72SBjoern A. Zeeb #endif 820efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 821efc76f72SBjoern A. Zeeb else 822efc76f72SBjoern A. Zeeb #endif 823efc76f72SBjoern A. Zeeb #ifdef INET 824efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 82568e0d7e0SRobert Watson lport, lookupflags, cred); 826efc76f72SBjoern A. Zeeb #endif 82725102351SMike Karels } 828efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 829efc76f72SBjoern A. Zeeb 830efc76f72SBjoern A. Zeeb *lportp = lport; 831efc76f72SBjoern A. Zeeb 832efc76f72SBjoern A. Zeeb return (0); 833efc76f72SBjoern A. Zeeb } 834efdf104bSMikolaj Golub 835efdf104bSMikolaj Golub /* 83625102351SMike Karels * Select a local port (number) to use. 83725102351SMike Karels */ 83825102351SMike Karels int 83925102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 84025102351SMike Karels struct ucred *cred, int lookupflags) 84125102351SMike Karels { 84225102351SMike Karels struct sockaddr_in laddr; 84325102351SMike Karels 84425102351SMike Karels if (laddrp) { 84525102351SMike Karels bzero(&laddr, sizeof(laddr)); 84625102351SMike Karels laddr.sin_family = AF_INET; 84725102351SMike Karels laddr.sin_addr = *laddrp; 84825102351SMike Karels } 84925102351SMike Karels return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 85025102351SMike Karels NULL, lportp, NULL, 0, cred, lookupflags)); 85125102351SMike Karels } 85225102351SMike Karels 85325102351SMike Karels /* 854efdf104bSMikolaj Golub * Return cached socket options. 855efdf104bSMikolaj Golub */ 8561a43cff9SSean Bruno int 857efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 858efdf104bSMikolaj Golub { 8591a43cff9SSean Bruno int so_options; 860efdf104bSMikolaj Golub 861efdf104bSMikolaj Golub so_options = 0; 862efdf104bSMikolaj Golub 8631a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 8641a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 865efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 866efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 867efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 868efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 869efdf104bSMikolaj Golub return (so_options); 870efdf104bSMikolaj Golub } 871efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 872efc76f72SBjoern A. Zeeb 8734b932371SIan Dowse /* 8740a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 8750a100a6fSAdrian Chadd * 8760a100a6fSAdrian Chadd * ni points to the new inp. 8770a100a6fSAdrian Chadd * oi points to the exisitng inp. 8780a100a6fSAdrian Chadd * 8790a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 8800a100a6fSAdrian Chadd * whether the credentials match. 8810a100a6fSAdrian Chadd */ 882d5bb8bd3SAdrian Chadd int 8830a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 8840a100a6fSAdrian Chadd { 8850a100a6fSAdrian Chadd /* Check permissions match */ 8860a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8870a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 8880a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 8890a100a6fSAdrian Chadd return (0); 8900a100a6fSAdrian Chadd 8910a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 8920a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8930a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 8940a100a6fSAdrian Chadd return (0); 8950a100a6fSAdrian Chadd 8960a100a6fSAdrian Chadd /* 8970a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 8980a100a6fSAdrian Chadd * it is and it matches the checks. 8990a100a6fSAdrian Chadd */ 9000a100a6fSAdrian Chadd return (1); 9010a100a6fSAdrian Chadd } 9020a100a6fSAdrian Chadd 903d5bb8bd3SAdrian Chadd #ifdef INET 9040a100a6fSAdrian Chadd /* 9054b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 9064b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 9074b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 9084b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 9094b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 9104b932371SIan Dowse * 9114b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 9124b932371SIan Dowse */ 9134b932371SIan Dowse int 914136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 915136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 9164b932371SIan Dowse { 9174b932371SIan Dowse struct socket *so = inp->inp_socket; 91815bd2b43SDavid Greenman struct sockaddr_in *sin; 919c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 9204b932371SIan Dowse struct in_addr laddr; 921df8bae1dSRodney W. Grimes u_short lport = 0; 92268e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 923413628a7SBjoern A. Zeeb int error; 924df8bae1dSRodney W. Grimes 9258501a69cSRobert Watson /* 9261a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 9271a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 9281a43cff9SSean Bruno */ 9291a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 9301a43cff9SSean Bruno 9311a43cff9SSean Bruno /* 932fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 9338501a69cSRobert Watson */ 93459daba27SSam Leffler INP_LOCK_ASSERT(inp); 935fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 93659daba27SSam Leffler 9374b932371SIan Dowse laddr.s_addr = *laddrp; 9384b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 939df8bae1dSRodney W. Grimes return (EINVAL); 9401a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 94168e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 9424616026fSErmal Luçi if (nam == NULL) { 9437c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 9447c2f3cb9SJamie Gritton return (error); 9457c2f3cb9SJamie Gritton } else { 94657bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 947f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 948f161d294SMark Johnston ("%s: invalid family for address %p", __func__, sin)); 949f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 950f161d294SMark Johnston ("%s: invalid length for address %p", __func__, sin)); 951f161d294SMark Johnston 952b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 953b89e82ddSJamie Gritton if (error) 954b89e82ddSJamie Gritton return (error); 9554b932371SIan Dowse if (sin->sin_port != *lportp) { 9564b932371SIan Dowse /* Don't allow the port to change. */ 9574b932371SIan Dowse if (*lportp != 0) 9584b932371SIan Dowse return (EINVAL); 959df8bae1dSRodney W. Grimes lport = sin->sin_port; 9604b932371SIan Dowse } 9614b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 962df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 963df8bae1dSRodney W. Grimes /* 964df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 965df8bae1dSRodney W. Grimes * allow complete duplication of binding if 966df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 967df8bae1dSRodney W. Grimes * and a multicast address is bound on both 968df8bae1dSRodney W. Grimes * new and duplicated sockets. 969df8bae1dSRodney W. Grimes */ 970f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 971df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 9721a43cff9SSean Bruno /* 9731a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 9741a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 9751a43cff9SSean Bruno */ 9761a43cff9SSean Bruno if ((so->so_options & 9771a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 9781a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 979df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 980df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 98183103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 9824209e01aSAdrian Chadd /* 9834209e01aSAdrian Chadd * Is the address a local IP address? 984f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 9858696873dSAdrian Chadd * to any endpoint address, local or not. 9864209e01aSAdrian Chadd */ 987f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 9888896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 989df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 990df8bae1dSRodney W. Grimes } 9914b932371SIan Dowse laddr = sin->sin_addr; 992df8bae1dSRodney W. Grimes if (lport) { 993df8bae1dSRodney W. Grimes struct inpcb *t; 994ae0e7143SRobert Watson struct tcptw *tw; 995ae0e7143SRobert Watson 996df8bae1dSRodney W. Grimes /* GROSS */ 997603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 998603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 999cc426dd3SMateusz Guzik priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 10002469dd60SGarrett Wollman return (EACCES); 1001835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 1002cc426dd3SMateusz Guzik priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 1003078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 1004413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 1005340c35deSJonathan Lemon /* 1006340c35deSJonathan Lemon * XXX 1007340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 1008340c35deSJonathan Lemon */ 10094cc20ab1SSeigo Tanimura if (t && 10100a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 1011ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 10124658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 10134658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 10144cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 101552b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 10161a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 10171a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 101886d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 101986d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 10204049a042SGuido van Rooij return (EADDRINUSE); 10210a100a6fSAdrian Chadd 10220a100a6fSAdrian Chadd /* 10230a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 10240a100a6fSAdrian Chadd * the credentials need to match and the 10250a100a6fSAdrian Chadd * original socket also has to have been bound 10260a100a6fSAdrian Chadd * with BINDMULTI. 10270a100a6fSAdrian Chadd */ 10280a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 10290a100a6fSAdrian Chadd return (EADDRINUSE); 10304049a042SGuido van Rooij } 1031c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 103268e0d7e0SRobert Watson lport, lookupflags, cred); 1033ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 1034ae0e7143SRobert Watson /* 1035ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 1036ae0e7143SRobert Watson * state recycled, we treat the address as 1037ae0e7143SRobert Watson * being in use (for now). This is better 1038ae0e7143SRobert Watson * than a panic, but not desirable. 1039ae0e7143SRobert Watson */ 1040ec95b709SMikolaj Golub tw = intotw(t); 1041ae0e7143SRobert Watson if (tw == NULL || 10421a43cff9SSean Bruno ((reuseport & tw->tw_so_options) == 0 && 10431a43cff9SSean Bruno (reuseport_lb & 10441a43cff9SSean Bruno tw->tw_so_options) == 0)) { 1045340c35deSJonathan Lemon return (EADDRINUSE); 10461a43cff9SSean Bruno } 10470a100a6fSAdrian Chadd } else if (t && 10480a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 10491a43cff9SSean Bruno (reuseport & inp_so_options(t)) == 0 && 10501a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 1051e3fd5ffdSRobert Watson #ifdef INET6 105233841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 1053cfa1ca9dSYoshinobu Inoue INADDR_ANY || 1054cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 1055cfa1ca9dSYoshinobu Inoue INADDR_ANY || 1056fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 1057fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 1058e3fd5ffdSRobert Watson #endif 1059df8bae1dSRodney W. Grimes return (EADDRINUSE); 10600a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 10610a100a6fSAdrian Chadd return (EADDRINUSE); 1062df8bae1dSRodney W. Grimes } 1063cfa1ca9dSYoshinobu Inoue } 1064df8bae1dSRodney W. Grimes } 10654b932371SIan Dowse if (*lportp != 0) 10664b932371SIan Dowse lport = *lportp; 106733b3ac06SPeter Wemm if (lport == 0) { 10684616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 1069efc76f72SBjoern A. Zeeb if (error != 0) 1070efc76f72SBjoern A. Zeeb return (error); 107133b3ac06SPeter Wemm } 10724b932371SIan Dowse *laddrp = laddr.s_addr; 10734b932371SIan Dowse *lportp = lport; 1074df8bae1dSRodney W. Grimes return (0); 1075df8bae1dSRodney W. Grimes } 1076df8bae1dSRodney W. Grimes 1077999f1343SGarrett Wollman /* 10785200e00eSIan Dowse * Connect from a socket to a specified address. 10795200e00eSIan Dowse * Both address and port must be specified in argument sin. 10805200e00eSIan Dowse * If don't have a local address for this socket yet, 10815200e00eSIan Dowse * then pick one. 1082999f1343SGarrett Wollman */ 1083999f1343SGarrett Wollman int 1084*db0ac6deSCy Schubert in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred, 1085*db0ac6deSCy Schubert bool rehash) 1086999f1343SGarrett Wollman { 10875200e00eSIan Dowse u_short lport, fport; 10885200e00eSIan Dowse in_addr_t laddr, faddr; 10895200e00eSIan Dowse int anonport, error; 1090df8bae1dSRodney W. Grimes 10918501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1092fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 109327f74fd0SRobert Watson 10945200e00eSIan Dowse lport = inp->inp_lport; 10955200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 10965200e00eSIan Dowse anonport = (lport == 0); 10975200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 1098b0330ed9SPawel Jakub Dawidek NULL, cred); 10995200e00eSIan Dowse if (error) 11005200e00eSIan Dowse return (error); 11015200e00eSIan Dowse 11025200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 11035200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 1104fe1274eeSMichael Tuexen KASSERT(rehash == true, 1105fe1274eeSMichael Tuexen ("Rehashing required for unbound inps")); 11065200e00eSIan Dowse inp->inp_lport = lport; 11075200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 11085200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 11095200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 11105200e00eSIan Dowse inp->inp_lport = 0; 11115200e00eSIan Dowse return (EAGAIN); 11125200e00eSIan Dowse } 11135200e00eSIan Dowse } 11145200e00eSIan Dowse 11155200e00eSIan Dowse /* Commit the remaining changes. */ 11165200e00eSIan Dowse inp->inp_lport = lport; 11175200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 11185200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 11195200e00eSIan Dowse inp->inp_fport = fport; 1120fe1274eeSMichael Tuexen if (rehash) { 1121*db0ac6deSCy Schubert in_pcbrehash(inp); 1122fe1274eeSMichael Tuexen } else { 1123*db0ac6deSCy Schubert in_pcbinshash(inp); 1124fe1274eeSMichael Tuexen } 11252cb64cb2SGeorge V. Neville-Neil 11265200e00eSIan Dowse if (anonport) 11275200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 11285200e00eSIan Dowse return (0); 11295200e00eSIan Dowse } 11305200e00eSIan Dowse 11315200e00eSIan Dowse /* 11320895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 11330895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 11340895aec3SBjoern A. Zeeb */ 1135ae190832SSteven Hartland int 11360895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 11370895aec3SBjoern A. Zeeb struct ucred *cred) 11380895aec3SBjoern A. Zeeb { 11390895aec3SBjoern A. Zeeb struct ifaddr *ifa; 11400895aec3SBjoern A. Zeeb struct sockaddr *sa; 11419ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 11429ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 11430895aec3SBjoern A. Zeeb int error; 11440895aec3SBjoern A. Zeeb 1145c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1146413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1147592bcae8SBjoern A. Zeeb /* 1148592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1149592bcae8SBjoern A. Zeeb * if requested. 1150592bcae8SBjoern A. Zeeb */ 1151592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 1152592bcae8SBjoern A. Zeeb return (0); 1153592bcae8SBjoern A. Zeeb 11540895aec3SBjoern A. Zeeb error = 0; 11550895aec3SBjoern A. Zeeb 11569ac7c6cfSAlexander V. Chernikov nh = NULL; 11579ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 11589ac7c6cfSAlexander V. Chernikov sin = &dst; 11590895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 11600895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 11610895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 11620895aec3SBjoern A. Zeeb 11630895aec3SBjoern A. Zeeb /* 11640895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 11650895aec3SBjoern A. Zeeb * else punt. 11660895aec3SBjoern A. Zeeb * 11670895aec3SBjoern A. Zeeb * Find out route to destination. 11680895aec3SBjoern A. Zeeb */ 11690895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 11709ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 11719ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 11720895aec3SBjoern A. Zeeb 11730895aec3SBjoern A. Zeeb /* 11740895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 11750895aec3SBjoern A. Zeeb * the outgoing interface. 11760895aec3SBjoern A. Zeeb * 11770895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 11780895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 11790895aec3SBjoern A. Zeeb * the source address from. 11800895aec3SBjoern A. Zeeb */ 11819ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 11828c0fec80SRobert Watson struct in_ifaddr *ia; 11830895aec3SBjoern A. Zeeb struct ifnet *ifp; 11840895aec3SBjoern A. Zeeb 11854f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 118658a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11874f6c66ccSMatt Macy if (ia == NULL) { 11884f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 118958a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11904f6c66ccSMatt Macy } 11910895aec3SBjoern A. Zeeb if (ia == NULL) { 11920895aec3SBjoern A. Zeeb error = ENETUNREACH; 11930895aec3SBjoern A. Zeeb goto done; 11940895aec3SBjoern A. Zeeb } 11950895aec3SBjoern A. Zeeb 11960304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11970895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11980895aec3SBjoern A. Zeeb goto done; 11990895aec3SBjoern A. Zeeb } 12000895aec3SBjoern A. Zeeb 12010895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12020895aec3SBjoern A. Zeeb ia = NULL; 1203d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12040895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12050895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12060895aec3SBjoern A. Zeeb continue; 12070895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1208b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12090895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12100895aec3SBjoern A. Zeeb break; 12110895aec3SBjoern A. Zeeb } 12120895aec3SBjoern A. Zeeb } 12130895aec3SBjoern A. Zeeb if (ia != NULL) { 12140895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12150895aec3SBjoern A. Zeeb goto done; 12160895aec3SBjoern A. Zeeb } 12170895aec3SBjoern A. Zeeb 12180895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1219b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12200895aec3SBjoern A. Zeeb goto done; 12210895aec3SBjoern A. Zeeb } 12220895aec3SBjoern A. Zeeb 12230895aec3SBjoern A. Zeeb /* 12240895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 12250895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 12260895aec3SBjoern A. Zeeb * In case of jails do those three steps: 12270895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 12280895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 12290895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 12300895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 12310895aec3SBjoern A. Zeeb */ 12329ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 12338c0fec80SRobert Watson struct in_ifaddr *ia; 12349317b04eSRobert Watson struct ifnet *ifp; 12350895aec3SBjoern A. Zeeb 12360895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 12370304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 12389ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 12390895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12400895aec3SBjoern A. Zeeb goto done; 12410895aec3SBjoern A. Zeeb } 12420895aec3SBjoern A. Zeeb 12430895aec3SBjoern A. Zeeb /* Jailed. */ 12440895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 12459ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1246b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12479ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 12480895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12490895aec3SBjoern A. Zeeb goto done; 12500895aec3SBjoern A. Zeeb } 12510895aec3SBjoern A. Zeeb 12520895aec3SBjoern A. Zeeb /* 12530895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 12540895aec3SBjoern A. Zeeb * belonging to this jail. 12550895aec3SBjoern A. Zeeb */ 12568c0fec80SRobert Watson ia = NULL; 12579ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1258d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12590895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12600895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12610895aec3SBjoern A. Zeeb continue; 12620895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1263b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12640895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12650895aec3SBjoern A. Zeeb break; 12660895aec3SBjoern A. Zeeb } 12670895aec3SBjoern A. Zeeb } 12680895aec3SBjoern A. Zeeb if (ia != NULL) { 12690895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12700895aec3SBjoern A. Zeeb goto done; 12710895aec3SBjoern A. Zeeb } 12720895aec3SBjoern A. Zeeb 12730895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1274b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12750895aec3SBjoern A. Zeeb goto done; 12760895aec3SBjoern A. Zeeb } 12770895aec3SBjoern A. Zeeb 12780895aec3SBjoern A. Zeeb /* 12790895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 12800895aec3SBjoern A. Zeeb * to ourselves is here. 12810895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 12820895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 12830895aec3SBjoern A. Zeeb * a loopback interface. 12840895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 12850895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 12860895aec3SBjoern A. Zeeb */ 12879ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 12888c0fec80SRobert Watson struct in_ifaddr *ia; 12890895aec3SBjoern A. Zeeb 12909ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 129158a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12920895aec3SBjoern A. Zeeb if (ia == NULL) 12939ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 129458a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1295f0bb05fcSQing Li if (ia == NULL) 12969ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 12970895aec3SBjoern A. Zeeb 12980304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 12990895aec3SBjoern A. Zeeb if (ia == NULL) { 13000895aec3SBjoern A. Zeeb error = ENETUNREACH; 13010895aec3SBjoern A. Zeeb goto done; 13020895aec3SBjoern A. Zeeb } 13030895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13040895aec3SBjoern A. Zeeb goto done; 13050895aec3SBjoern A. Zeeb } 13060895aec3SBjoern A. Zeeb 13070895aec3SBjoern A. Zeeb /* Jailed. */ 13080895aec3SBjoern A. Zeeb if (ia != NULL) { 13090895aec3SBjoern A. Zeeb struct ifnet *ifp; 13100895aec3SBjoern A. Zeeb 13110895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 13120895aec3SBjoern A. Zeeb ia = NULL; 1313d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 13140895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 13150895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 13160895aec3SBjoern A. Zeeb continue; 13170895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1318b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1319b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 13200895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 13210895aec3SBjoern A. Zeeb break; 13220895aec3SBjoern A. Zeeb } 13230895aec3SBjoern A. Zeeb } 13240895aec3SBjoern A. Zeeb if (ia != NULL) { 13250895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 13260895aec3SBjoern A. Zeeb goto done; 13270895aec3SBjoern A. Zeeb } 13280895aec3SBjoern A. Zeeb } 13290895aec3SBjoern A. Zeeb 13300895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1331b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 13320895aec3SBjoern A. Zeeb goto done; 13330895aec3SBjoern A. Zeeb } 13340895aec3SBjoern A. Zeeb 13350895aec3SBjoern A. Zeeb done: 13360895aec3SBjoern A. Zeeb return (error); 13370895aec3SBjoern A. Zeeb } 13380895aec3SBjoern A. Zeeb 13390895aec3SBjoern A. Zeeb /* 13405200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 13415200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 13425200e00eSIan Dowse * address and port for the PCB; these are updated to the values 13435200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 13445200e00eSIan Dowse * the connect. 13455200e00eSIan Dowse * 13465200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 13475200e00eSIan Dowse * and port. These are not updated in the error case. 13485200e00eSIan Dowse * 13495200e00eSIan Dowse * If the operation fails because the connection already exists, 13505200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 13515200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 13525200e00eSIan Dowse * is set to NULL. 13535200e00eSIan Dowse */ 13545200e00eSIan Dowse int 1355136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1356136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1357136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 13585200e00eSIan Dowse { 13595200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 13605200e00eSIan Dowse struct in_ifaddr *ia; 13615200e00eSIan Dowse struct inpcb *oinp; 1362b89e82ddSJamie Gritton struct in_addr laddr, faddr; 13635200e00eSIan Dowse u_short lport, fport; 13645200e00eSIan Dowse int error; 13655200e00eSIan Dowse 1366f161d294SMark Johnston KASSERT(sin->sin_family == AF_INET, 1367f161d294SMark Johnston ("%s: invalid address family for %p", __func__, sin)); 1368f161d294SMark Johnston KASSERT(sin->sin_len == sizeof(*sin), 1369f161d294SMark Johnston ("%s: invalid address length for %p", __func__, sin)); 1370f161d294SMark Johnston 13718501a69cSRobert Watson /* 13728501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 13738501a69cSRobert Watson * lock is sufficient. 13748501a69cSRobert Watson */ 1375c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 137627f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1377fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 137827f74fd0SRobert Watson 13795200e00eSIan Dowse if (oinpp != NULL) 13805200e00eSIan Dowse *oinpp = NULL; 1381df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1382df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 13835200e00eSIan Dowse laddr.s_addr = *laddrp; 13845200e00eSIan Dowse lport = *lportp; 13855200e00eSIan Dowse faddr = sin->sin_addr; 13865200e00eSIan Dowse fport = sin->sin_port; 13870c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH 13880c325f53SAlexander V. Chernikov if (CALC_FLOWID_OUTBOUND) { 13890c325f53SAlexander V. Chernikov uint32_t hash_val, hash_type; 13900895aec3SBjoern A. Zeeb 13910c325f53SAlexander V. Chernikov hash_val = fib4_calc_software_hash(laddr, faddr, 0, fport, 13920c325f53SAlexander V. Chernikov inp->inp_socket->so_proto->pr_protocol, &hash_type); 13930c325f53SAlexander V. Chernikov 13940c325f53SAlexander V. Chernikov inp->inp_flowid = hash_val; 13950c325f53SAlexander V. Chernikov inp->inp_flowtype = hash_type; 13960c325f53SAlexander V. Chernikov } 13970c325f53SAlexander V. Chernikov #endif 1398d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1399df8bae1dSRodney W. Grimes /* 1400df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1401df8bae1dSRodney W. Grimes * use the primary local address. 1402df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1403df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1404df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1405df8bae1dSRodney W. Grimes */ 1406413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1407413628a7SBjoern A. Zeeb faddr = 1408d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1409b89e82ddSJamie Gritton if (cred != NULL && 1410b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1411b89e82ddSJamie Gritton return (error); 14122d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1413d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 14142d9cfabaSRobert Watson IFF_BROADCAST) 1415d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1416603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 14172d9cfabaSRobert Watson } 1418df8bae1dSRodney W. Grimes } 14195200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1420d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1421df8bae1dSRodney W. Grimes /* 1422df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1423d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1424df8bae1dSRodney W. Grimes * address of that interface as our source address. 1425df8bae1dSRodney W. Grimes */ 14265200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1427df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1428df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1429df8bae1dSRodney W. Grimes struct ifnet *ifp; 1430df8bae1dSRodney W. Grimes 1431df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1432df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1433df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1434d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1435e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1436e691be70SDaniel Eischen (cred == NULL || 1437e691be70SDaniel Eischen prison_check_ip4(cred, 1438e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1439df8bae1dSRodney W. Grimes break; 1440e691be70SDaniel Eischen } 1441e691be70SDaniel Eischen if (ia == NULL) 1442d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1443e691be70SDaniel Eischen else { 14445200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1445d79fdd98SDaniel Eischen error = 0; 1446999f1343SGarrett Wollman } 1447d79fdd98SDaniel Eischen } 1448d79fdd98SDaniel Eischen } 144904215ed2SRandall Stewart if (error) 145004215ed2SRandall Stewart return (error); 14510895aec3SBjoern A. Zeeb } 1452a034518aSAndrew Gallatin 145325102351SMike Karels if (lport != 0) { 145425102351SMike Karels oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 1455a034518aSAndrew Gallatin fport, laddr, lport, 0, NULL, M_NODOM); 14565200e00eSIan Dowse if (oinp != NULL) { 14575200e00eSIan Dowse if (oinpp != NULL) 14585200e00eSIan Dowse *oinpp = oinp; 1459df8bae1dSRodney W. Grimes return (EADDRINUSE); 1460c3229e05SDavid Greenman } 146125102351SMike Karels } else { 146225102351SMike Karels struct sockaddr_in lsin, fsin; 146325102351SMike Karels 146425102351SMike Karels bzero(&lsin, sizeof(lsin)); 146525102351SMike Karels bzero(&fsin, sizeof(fsin)); 146625102351SMike Karels lsin.sin_family = AF_INET; 146725102351SMike Karels lsin.sin_addr = laddr; 146825102351SMike Karels fsin.sin_family = AF_INET; 146925102351SMike Karels fsin.sin_addr = faddr; 147025102351SMike Karels error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin, 147125102351SMike Karels &lport, (struct sockaddr *)& fsin, fport, cred, 147225102351SMike Karels INPLOOKUP_WILDCARD); 14735a903f8dSPierre Beyssac if (error) 14745a903f8dSPierre Beyssac return (error); 14755a903f8dSPierre Beyssac } 14765200e00eSIan Dowse *laddrp = laddr.s_addr; 14775200e00eSIan Dowse *lportp = lport; 14785200e00eSIan Dowse *faddrp = faddr.s_addr; 14795200e00eSIan Dowse *fportp = fport; 1480df8bae1dSRodney W. Grimes return (0); 1481df8bae1dSRodney W. Grimes } 1482df8bae1dSRodney W. Grimes 148326f9a767SRodney W. Grimes void 1484136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1485df8bae1dSRodney W. Grimes { 14866b348152SRobert Watson 14878501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1488fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1489df8bae1dSRodney W. Grimes 1490df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1491df8bae1dSRodney W. Grimes inp->inp_fport = 0; 149215bd2b43SDavid Greenman in_pcbrehash(inp); 1493df8bae1dSRodney W. Grimes } 149483e521ecSBjoern A. Zeeb #endif /* INET */ 1495df8bae1dSRodney W. Grimes 14964c7c478dSRobert Watson /* 149728696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1498c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 149928696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 150028696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 15014c7c478dSRobert Watson */ 150226f9a767SRodney W. Grimes void 1503136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1504df8bae1dSRodney W. Grimes { 15054c7c478dSRobert Watson 1506a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1507c0a211c5SRobert Watson 1508f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1509f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1510f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1511f3e7afe2SHans Petter Selasky #endif 15124c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 15134c7c478dSRobert Watson inp->inp_socket = NULL; 15144c7c478dSRobert Watson } 15154c7c478dSRobert Watson 1516c0a211c5SRobert Watson /* 1517*db0ac6deSCy Schubert * inpcb hash lookups are protected by SMR section. 1518*db0ac6deSCy Schubert * 1519*db0ac6deSCy Schubert * Once desired pcb has been found, switching from SMR section to a pcb 1520*db0ac6deSCy Schubert * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1521*db0ac6deSCy Schubert * here because SMR is a critical section. 1522*db0ac6deSCy Schubert * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1523*db0ac6deSCy Schubert */ 1524*db0ac6deSCy Schubert static inline void 1525*db0ac6deSCy Schubert inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1526*db0ac6deSCy Schubert { 1527*db0ac6deSCy Schubert 1528*db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1529*db0ac6deSCy Schubert rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1530*db0ac6deSCy Schubert } 1531*db0ac6deSCy Schubert 1532*db0ac6deSCy Schubert static inline void 1533*db0ac6deSCy Schubert inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1534*db0ac6deSCy Schubert { 1535*db0ac6deSCy Schubert 1536*db0ac6deSCy Schubert lock == INPLOOKUP_RLOCKPCB ? 1537*db0ac6deSCy Schubert rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1538*db0ac6deSCy Schubert } 1539*db0ac6deSCy Schubert 1540*db0ac6deSCy Schubert static inline int 1541*db0ac6deSCy Schubert inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1542*db0ac6deSCy Schubert { 1543*db0ac6deSCy Schubert 1544*db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1545*db0ac6deSCy Schubert rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1546*db0ac6deSCy Schubert } 1547*db0ac6deSCy Schubert 1548*db0ac6deSCy Schubert static inline bool 1549*db0ac6deSCy Schubert in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 1550*db0ac6deSCy Schubert { 1551*db0ac6deSCy Schubert 1552*db0ac6deSCy Schubert return (lock == INPLOOKUP_RLOCKPCB ? 1553*db0ac6deSCy Schubert in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 1554*db0ac6deSCy Schubert } 1555*db0ac6deSCy Schubert 1556*db0ac6deSCy Schubert bool 1557*db0ac6deSCy Schubert inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1558*db0ac6deSCy Schubert { 1559*db0ac6deSCy Schubert 1560*db0ac6deSCy Schubert MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1561*db0ac6deSCy Schubert SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1562*db0ac6deSCy Schubert 1563*db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1564*db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1565*db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1566*db0ac6deSCy Schubert inp_unlock(inp, lock); 1567*db0ac6deSCy Schubert return (false); 1568*db0ac6deSCy Schubert } 1569*db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1570*db0ac6deSCy Schubert return (true); 1571*db0ac6deSCy Schubert } 1572*db0ac6deSCy Schubert 1573*db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1574*db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1575*db0ac6deSCy Schubert inp_lock(inp, lock); 1576*db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) 1577*db0ac6deSCy Schubert return (false); 1578*db0ac6deSCy Schubert /* 1579*db0ac6deSCy Schubert * inp acquired through refcount & lock for sure didn't went 1580*db0ac6deSCy Schubert * through uma_zfree(). However, it may have already went 1581*db0ac6deSCy Schubert * through in_pcbfree() and has another reference, that 1582*db0ac6deSCy Schubert * prevented its release by our in_pcbrele(). 1583*db0ac6deSCy Schubert */ 1584*db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1585*db0ac6deSCy Schubert inp_unlock(inp, lock); 1586*db0ac6deSCy Schubert return (false); 1587*db0ac6deSCy Schubert } 1588*db0ac6deSCy Schubert return (true); 1589*db0ac6deSCy Schubert } else { 1590*db0ac6deSCy Schubert smr_exit(inp->inp_pcbinfo->ipi_smr); 1591*db0ac6deSCy Schubert return (false); 1592*db0ac6deSCy Schubert } 1593*db0ac6deSCy Schubert } 1594*db0ac6deSCy Schubert 1595*db0ac6deSCy Schubert /* 1596*db0ac6deSCy Schubert * inp_next() - inpcb hash/list traversal iterator 1597*db0ac6deSCy Schubert * 1598*db0ac6deSCy Schubert * Requires initialized struct inpcb_iterator for context. 1599*db0ac6deSCy Schubert * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1600*db0ac6deSCy Schubert * 1601*db0ac6deSCy Schubert * - Iterator can have either write-lock or read-lock semantics, that can not 1602*db0ac6deSCy Schubert * be changed later. 1603*db0ac6deSCy Schubert * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1604*db0ac6deSCy Schubert * a single hash slot. Note: only rip_input() does the latter. 1605*db0ac6deSCy Schubert * - Iterator may have optional bool matching function. The matching function 1606*db0ac6deSCy Schubert * will be executed for each inpcb in the SMR context, so it can not acquire 1607*db0ac6deSCy Schubert * locks and can safely access only immutable fields of inpcb. 1608*db0ac6deSCy Schubert * 1609*db0ac6deSCy Schubert * A fresh initialized iterator has NULL inpcb in its context and that 1610*db0ac6deSCy Schubert * means that inp_next() call would return the very first inpcb on the list 1611*db0ac6deSCy Schubert * locked with desired semantic. In all following calls the context pointer 1612*db0ac6deSCy Schubert * shall hold the current inpcb pointer. The KPI user is not supposed to 1613*db0ac6deSCy Schubert * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1614*db0ac6deSCy Schubert * and write NULL to its context. After end of traversal an iterator can be 1615*db0ac6deSCy Schubert * reused. 1616*db0ac6deSCy Schubert * 1617*db0ac6deSCy Schubert * List traversals have the following features/constraints: 1618*db0ac6deSCy Schubert * - New entries won't be seen, as they are always added to the head of a list. 1619*db0ac6deSCy Schubert * - Removed entries won't stop traversal as long as they are not added to 1620*db0ac6deSCy Schubert * a different list. This is violated by in_pcbrehash(). 1621*db0ac6deSCy Schubert */ 1622*db0ac6deSCy Schubert #define II_LIST_FIRST(ipi, hash) \ 1623*db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1624*db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1625*db0ac6deSCy Schubert CK_LIST_FIRST(&(ipi)->ipi_hashbase[(hash)])) 1626*db0ac6deSCy Schubert #define II_LIST_NEXT(inp, hash) \ 1627*db0ac6deSCy Schubert (((hash) == INP_ALL_LIST) ? \ 1628*db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_list) : \ 1629*db0ac6deSCy Schubert CK_LIST_NEXT((inp), inp_hash)) 1630*db0ac6deSCy Schubert #define II_LOCK_ASSERT(inp, lock) \ 1631*db0ac6deSCy Schubert rw_assert(&(inp)->inp_lock, \ 1632*db0ac6deSCy Schubert (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1633*db0ac6deSCy Schubert struct inpcb * 1634*db0ac6deSCy Schubert inp_next(struct inpcb_iterator *ii) 1635*db0ac6deSCy Schubert { 1636*db0ac6deSCy Schubert const struct inpcbinfo *ipi = ii->ipi; 1637*db0ac6deSCy Schubert inp_match_t *match = ii->match; 1638*db0ac6deSCy Schubert void *ctx = ii->ctx; 1639*db0ac6deSCy Schubert inp_lookup_t lock = ii->lock; 1640*db0ac6deSCy Schubert int hash = ii->hash; 1641*db0ac6deSCy Schubert struct inpcb *inp; 1642*db0ac6deSCy Schubert 1643*db0ac6deSCy Schubert if (ii->inp == NULL) { /* First call. */ 1644*db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1645*db0ac6deSCy Schubert /* This is unrolled CK_LIST_FOREACH(). */ 1646*db0ac6deSCy Schubert for (inp = II_LIST_FIRST(ipi, hash); 1647*db0ac6deSCy Schubert inp != NULL; 1648*db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash)) { 1649*db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1650*db0ac6deSCy Schubert continue; 1651*db0ac6deSCy Schubert if (__predict_true(inp_smr_lock(inp, lock))) 1652*db0ac6deSCy Schubert break; 1653*db0ac6deSCy Schubert else { 1654*db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1655*db0ac6deSCy Schubert MPASS(inp != II_LIST_FIRST(ipi, hash)); 1656*db0ac6deSCy Schubert inp = II_LIST_FIRST(ipi, hash); 1657*db0ac6deSCy Schubert } 1658*db0ac6deSCy Schubert } 1659*db0ac6deSCy Schubert 1660*db0ac6deSCy Schubert if (inp == NULL) 1661*db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1662*db0ac6deSCy Schubert else 1663*db0ac6deSCy Schubert ii->inp = inp; 1664*db0ac6deSCy Schubert 1665*db0ac6deSCy Schubert return (inp); 1666*db0ac6deSCy Schubert } 1667*db0ac6deSCy Schubert 1668*db0ac6deSCy Schubert /* Not a first call. */ 1669*db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1670*db0ac6deSCy Schubert restart: 1671*db0ac6deSCy Schubert inp = ii->inp; 1672*db0ac6deSCy Schubert II_LOCK_ASSERT(inp, lock); 1673*db0ac6deSCy Schubert next: 1674*db0ac6deSCy Schubert inp = II_LIST_NEXT(inp, hash); 1675*db0ac6deSCy Schubert if (inp == NULL) { 1676*db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1677*db0ac6deSCy Schubert goto found; 1678*db0ac6deSCy Schubert } 1679*db0ac6deSCy Schubert 1680*db0ac6deSCy Schubert if (match != NULL && (match)(inp, ctx) == false) 1681*db0ac6deSCy Schubert goto next; 1682*db0ac6deSCy Schubert 1683*db0ac6deSCy Schubert if (__predict_true(inp_trylock(inp, lock))) { 1684*db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1685*db0ac6deSCy Schubert /* 1686*db0ac6deSCy Schubert * Entries are never inserted in middle of a list, thus 1687*db0ac6deSCy Schubert * as long as we are in SMR, we can continue traversal. 1688*db0ac6deSCy Schubert * Jump to 'restart' should yield in the same result, 1689*db0ac6deSCy Schubert * but could produce unnecessary looping. Could this 1690*db0ac6deSCy Schubert * looping be unbound? 1691*db0ac6deSCy Schubert */ 1692*db0ac6deSCy Schubert inp_unlock(inp, lock); 1693*db0ac6deSCy Schubert goto next; 1694*db0ac6deSCy Schubert } else { 1695*db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1696*db0ac6deSCy Schubert goto found; 1697*db0ac6deSCy Schubert } 1698*db0ac6deSCy Schubert } 1699*db0ac6deSCy Schubert 1700*db0ac6deSCy Schubert /* 1701*db0ac6deSCy Schubert * Can't obtain lock immediately, thus going hard. Once we exit the 1702*db0ac6deSCy Schubert * SMR section we can no longer jump to 'next', and our only stable 1703*db0ac6deSCy Schubert * anchoring point is ii->inp, which we keep locked for this case, so 1704*db0ac6deSCy Schubert * we jump to 'restart'. 1705*db0ac6deSCy Schubert */ 1706*db0ac6deSCy Schubert if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1707*db0ac6deSCy Schubert smr_exit(ipi->ipi_smr); 1708*db0ac6deSCy Schubert inp_lock(inp, lock); 1709*db0ac6deSCy Schubert if (__predict_false(in_pcbrele(inp, lock))) { 1710*db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1711*db0ac6deSCy Schubert goto restart; 1712*db0ac6deSCy Schubert } 1713*db0ac6deSCy Schubert /* 1714*db0ac6deSCy Schubert * See comment in inp_smr_lock(). 1715*db0ac6deSCy Schubert */ 1716*db0ac6deSCy Schubert if (__predict_false(inp->inp_flags & INP_FREED)) { 1717*db0ac6deSCy Schubert inp_unlock(inp, lock); 1718*db0ac6deSCy Schubert smr_enter(ipi->ipi_smr); 1719*db0ac6deSCy Schubert goto restart; 1720*db0ac6deSCy Schubert } 1721*db0ac6deSCy Schubert } else 1722*db0ac6deSCy Schubert goto next; 1723*db0ac6deSCy Schubert 1724*db0ac6deSCy Schubert found: 1725*db0ac6deSCy Schubert inp_unlock(ii->inp, lock); 1726*db0ac6deSCy Schubert ii->inp = inp; 1727*db0ac6deSCy Schubert 1728*db0ac6deSCy Schubert return (ii->inp); 1729*db0ac6deSCy Schubert } 1730*db0ac6deSCy Schubert 1731*db0ac6deSCy Schubert /* 173279bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 1733*db0ac6deSCy Schubert * stability of an inpcb pointer despite the inpcb lock being released or 1734*db0ac6deSCy Schubert * SMR section exited. 173579bdc6e5SRobert Watson * 1736*db0ac6deSCy Schubert * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1737c0a211c5SRobert Watson */ 173879bdc6e5SRobert Watson void 173979bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 17404c7c478dSRobert Watson { 1741*db0ac6deSCy Schubert u_int old __diagused; 1742df8bae1dSRodney W. Grimes 1743*db0ac6deSCy Schubert old = refcount_acquire(&inp->inp_refcount); 1744*db0ac6deSCy Schubert KASSERT(old > 0, ("%s: refcount 0", __func__)); 174579bdc6e5SRobert Watson } 174679bdc6e5SRobert Watson 174779bdc6e5SRobert Watson /* 1748*db0ac6deSCy Schubert * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1749*db0ac6deSCy Schubert * freeing the pcb, if the reference was very last. 175079bdc6e5SRobert Watson */ 1751*db0ac6deSCy Schubert bool 175279bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 175379bdc6e5SRobert Watson { 175479bdc6e5SRobert Watson 175579bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 175679bdc6e5SRobert Watson 1757*db0ac6deSCy Schubert if (refcount_release(&inp->inp_refcount) == 0) 1758*db0ac6deSCy Schubert return (false); 1759*db0ac6deSCy Schubert 1760*db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1761*db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1762*db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1763*db0ac6deSCy Schubert MPASS(inp->inp_in_dropq == 0); 1764df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1765*db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1766*db0ac6deSCy Schubert return (true); 1767df4e91d3SGleb Smirnoff } 176879bdc6e5SRobert Watson 1769*db0ac6deSCy Schubert bool 177079bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 177179bdc6e5SRobert Watson { 177279bdc6e5SRobert Watson 177379bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 177479bdc6e5SRobert Watson 1775*db0ac6deSCy Schubert if (refcount_release(&inp->inp_refcount) == 0) 1776*db0ac6deSCy Schubert return (false); 1777*db0ac6deSCy Schubert 1778*db0ac6deSCy Schubert MPASS(inp->inp_flags & INP_FREED); 1779*db0ac6deSCy Schubert MPASS(inp->inp_socket == NULL); 1780*db0ac6deSCy Schubert MPASS(inp->inp_in_hpts == 0); 1781*db0ac6deSCy Schubert MPASS(inp->inp_in_dropq == 0); 1782edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1783*db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1784*db0ac6deSCy Schubert return (true); 1785addf2b20SMatt Macy } 1786addf2b20SMatt Macy 178779bdc6e5SRobert Watson /* 178879bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 178979bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 179079bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 179179bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 1792*db0ac6deSCy Schubert * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1793*db0ac6deSCy Schubert * Almost all work, including removal from global lists, is done in this 1794*db0ac6deSCy Schubert * context, where the pcbinfo lock is held. 179579bdc6e5SRobert Watson */ 179679bdc6e5SRobert Watson void 179779bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 179879bdc6e5SRobert Watson { 1799f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1800*db0ac6deSCy Schubert #ifdef INET 1801*db0ac6deSCy Schubert struct ip_moptions *imo; 1802*db0ac6deSCy Schubert #endif 1803*db0ac6deSCy Schubert #ifdef INET6 1804*db0ac6deSCy Schubert struct ip6_moptions *im6o; 1805*db0ac6deSCy Schubert #endif 180645a48d07SJonathan T. Looney 180779bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1808*db0ac6deSCy Schubert KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1809*db0ac6deSCy Schubert KASSERT((inp->inp_flags & INP_FREED) == 0, 1810*db0ac6deSCy Schubert ("%s: called twice for pcb %p", __func__, inp)); 1811*db0ac6deSCy Schubert 1812*db0ac6deSCy Schubert inp->inp_flags |= INP_FREED; 1813*db0ac6deSCy Schubert INP_INFO_WLOCK(pcbinfo); 1814*db0ac6deSCy Schubert inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1815*db0ac6deSCy Schubert pcbinfo->ipi_count--; 1816*db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_list); 1817*db0ac6deSCy Schubert INP_INFO_WUNLOCK(pcbinfo); 1818*db0ac6deSCy Schubert 1819*db0ac6deSCy Schubert if (inp->inp_flags & INP_INHASHLIST) { 1820*db0ac6deSCy Schubert struct inpcbport *phd = inp->inp_phd; 1821*db0ac6deSCy Schubert 1822*db0ac6deSCy Schubert INP_HASH_WLOCK(pcbinfo); 1823*db0ac6deSCy Schubert /* XXX: Only do if SO_REUSEPORT_LB set? */ 1824*db0ac6deSCy Schubert in_pcbremlbgrouphash(inp); 1825*db0ac6deSCy Schubert 1826*db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_hash); 1827*db0ac6deSCy Schubert CK_LIST_REMOVE(inp, inp_portlist); 1828*db0ac6deSCy Schubert if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 1829*db0ac6deSCy Schubert CK_LIST_REMOVE(phd, phd_hash); 1830*db0ac6deSCy Schubert uma_zfree_smr(pcbinfo->ipi_portzone, phd); 1831*db0ac6deSCy Schubert } 1832*db0ac6deSCy Schubert INP_HASH_WUNLOCK(pcbinfo); 1833*db0ac6deSCy Schubert inp->inp_flags &= ~INP_INHASHLIST; 1834*db0ac6deSCy Schubert } 1835*db0ac6deSCy Schubert 1836*db0ac6deSCy Schubert crfree(inp->inp_cred); 1837fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1838*db0ac6deSCy Schubert #ifdef MAC 1839*db0ac6deSCy Schubert mac_inpcb_destroy(inp); 1840*db0ac6deSCy Schubert #endif 1841*db0ac6deSCy Schubert #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1842*db0ac6deSCy Schubert if (inp->inp_sp != NULL) 1843*db0ac6deSCy Schubert ipsec_delete_pcbpolicy(inp); 1844*db0ac6deSCy Schubert #endif 1845*db0ac6deSCy Schubert #ifdef INET 1846*db0ac6deSCy Schubert if (inp->inp_options) 1847*db0ac6deSCy Schubert (void)m_free(inp->inp_options); 1848*db0ac6deSCy Schubert imo = inp->inp_moptions; 1849*db0ac6deSCy Schubert #endif 1850*db0ac6deSCy Schubert #ifdef INET6 1851*db0ac6deSCy Schubert if (inp->inp_vflag & INP_IPV6PROTO) { 1852*db0ac6deSCy Schubert ip6_freepcbopts(inp->in6p_outputopts); 1853*db0ac6deSCy Schubert im6o = inp->in6p_moptions; 1854*db0ac6deSCy Schubert } else 1855*db0ac6deSCy Schubert im6o = NULL; 1856*db0ac6deSCy Schubert #endif 1857*db0ac6deSCy Schubert 1858*db0ac6deSCy Schubert if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1859cb6bb230SMatt Macy INP_WUNLOCK(inp); 1860*db0ac6deSCy Schubert } 1861*db0ac6deSCy Schubert #ifdef INET6 1862*db0ac6deSCy Schubert ip6_freemoptions(im6o); 1863*db0ac6deSCy Schubert #endif 1864*db0ac6deSCy Schubert #ifdef INET 1865*db0ac6deSCy Schubert inp_freemoptions(imo); 1866*db0ac6deSCy Schubert #endif 186728696211SRobert Watson } 186828696211SRobert Watson 186928696211SRobert Watson /* 1870c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1871c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1872c0a211c5SRobert Watson * 1873c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1874c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1875c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1876c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1877c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1878c0a211c5SRobert Watson * in_pcbdetach(). 1879c0a211c5SRobert Watson * 1880c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1881c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 188210702a28SRobert Watson */ 188310702a28SRobert Watson void 188410702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 188510702a28SRobert Watson { 188610702a28SRobert Watson 18878501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 18886573d758SMatt Macy #ifdef INVARIANTS 18896573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 18906573d758SMatt Macy MPASS(inp->inp_refcount > 1); 18916573d758SMatt Macy #endif 189210702a28SRobert Watson 1893fa046d87SRobert Watson /* 1894fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1895fa046d87SRobert Watson * the hash lock...? 1896fa046d87SRobert Watson */ 1897ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1898111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 189910702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 190010702a28SRobert Watson 1901fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 19021a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 1903b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 1904b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 1905b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 1906b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 1907*db0ac6deSCy Schubert uma_zfree_smr(inp->inp_pcbinfo->ipi_portzone, phd); 190810702a28SRobert Watson } 1909fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1910111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 191110702a28SRobert Watson } 191210702a28SRobert Watson } 191310702a28SRobert Watson 191467107f45SBjoern A. Zeeb #ifdef INET 191554d642bbSRobert Watson /* 191654d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 191754d642bbSRobert Watson */ 191826ef6ac4SDon Lewis struct sockaddr * 1919136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 192026ef6ac4SDon Lewis { 192126ef6ac4SDon Lewis struct sockaddr_in *sin; 192226ef6ac4SDon Lewis 19231ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1924a163d034SWarner Losh M_WAITOK | M_ZERO); 192526ef6ac4SDon Lewis sin->sin_family = AF_INET; 192626ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 192726ef6ac4SDon Lewis sin->sin_addr = *addr_p; 192826ef6ac4SDon Lewis sin->sin_port = port; 192926ef6ac4SDon Lewis 193026ef6ac4SDon Lewis return (struct sockaddr *)sin; 193126ef6ac4SDon Lewis } 193226ef6ac4SDon Lewis 1933117bcae7SGarrett Wollman int 193454d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1935df8bae1dSRodney W. Grimes { 1936136d4f1cSRobert Watson struct inpcb *inp; 193726ef6ac4SDon Lewis struct in_addr addr; 193826ef6ac4SDon Lewis in_port_t port; 193942fa505bSDavid Greenman 1940fdc984f7STor Egge inp = sotoinpcb(so); 194154d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 19426466b28aSRobert Watson 1943a69042a5SRobert Watson INP_RLOCK(inp); 194426ef6ac4SDon Lewis port = inp->inp_lport; 194526ef6ac4SDon Lewis addr = inp->inp_laddr; 1946a69042a5SRobert Watson INP_RUNLOCK(inp); 194742fa505bSDavid Greenman 194826ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1949117bcae7SGarrett Wollman return 0; 1950df8bae1dSRodney W. Grimes } 1951df8bae1dSRodney W. Grimes 1952117bcae7SGarrett Wollman int 195354d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1954df8bae1dSRodney W. Grimes { 1955136d4f1cSRobert Watson struct inpcb *inp; 195626ef6ac4SDon Lewis struct in_addr addr; 195726ef6ac4SDon Lewis in_port_t port; 195842fa505bSDavid Greenman 1959fdc984f7STor Egge inp = sotoinpcb(so); 196054d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 19616466b28aSRobert Watson 1962a69042a5SRobert Watson INP_RLOCK(inp); 196326ef6ac4SDon Lewis port = inp->inp_fport; 196426ef6ac4SDon Lewis addr = inp->inp_faddr; 1965a69042a5SRobert Watson INP_RUNLOCK(inp); 196642fa505bSDavid Greenman 196726ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1968117bcae7SGarrett Wollman return 0; 1969df8bae1dSRodney W. Grimes } 1970df8bae1dSRodney W. Grimes 197126f9a767SRodney W. Grimes void 1972136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1973136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1974d1c54148SJesper Skriver { 1975f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1976d1c54148SJesper Skriver 19773dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1978*db0ac6deSCy Schubert CK_LIST_FOREACH_SAFE(inp, &pcbinfo->ipi_listhead, inp_list, inp_temp) { 19798501a69cSRobert Watson INP_WLOCK(inp); 1980d1c54148SJesper Skriver #ifdef INET6 1981f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 19828501a69cSRobert Watson INP_WUNLOCK(inp); 1983d1c54148SJesper Skriver continue; 1984f76fcf6dSJeffrey Hsu } 1985d1c54148SJesper Skriver #endif 1986d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1987f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 19888501a69cSRobert Watson INP_WUNLOCK(inp); 1989d1c54148SJesper Skriver continue; 1990d1c54148SJesper Skriver } 19913dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 19928501a69cSRobert Watson INP_WUNLOCK(inp); 1993f76fcf6dSJeffrey Hsu } 19943dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1995d1c54148SJesper Skriver } 1996d1c54148SJesper Skriver 1997*db0ac6deSCy Schubert static bool 1998*db0ac6deSCy Schubert inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 1999*db0ac6deSCy Schubert { 2000*db0ac6deSCy Schubert 2001*db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 2002*db0ac6deSCy Schubert return (true); 2003*db0ac6deSCy Schubert else 2004*db0ac6deSCy Schubert return (false); 2005*db0ac6deSCy Schubert } 2006*db0ac6deSCy Schubert 2007e43cc4aeSHajimu UMEMOTO void 2008136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 2009e43cc4aeSHajimu UMEMOTO { 2010*db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 2011*db0ac6deSCy Schubert inp_v4_multi_match, NULL); 2012e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 201359854ecfSHans Petter Selasky struct in_multi *inm; 201459854ecfSHans Petter Selasky struct in_mfilter *imf; 2015e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 2016e43cc4aeSHajimu UMEMOTO 2017*db0ac6deSCy Schubert IN_MULTI_LOCK_ASSERT(); 2018*db0ac6deSCy Schubert 2019*db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 2020*db0ac6deSCy Schubert INP_WLOCK_ASSERT(inp); 2021*db0ac6deSCy Schubert 2022e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 2023e43cc4aeSHajimu UMEMOTO /* 2024e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 2025e43cc4aeSHajimu UMEMOTO * detached. 2026e43cc4aeSHajimu UMEMOTO */ 2027e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 2028e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 2029e43cc4aeSHajimu UMEMOTO 2030e43cc4aeSHajimu UMEMOTO /* 2031e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 2032e43cc4aeSHajimu UMEMOTO * through the interface being detached. 2033cb6bb230SMatt Macy * 2034cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 2035e43cc4aeSHajimu UMEMOTO */ 203659854ecfSHans Petter Selasky restart: 203759854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 203859854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 203959854ecfSHans Petter Selasky continue; 204059854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 204159854ecfSHans Petter Selasky continue; 204259854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 204359854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 204459854ecfSHans Petter Selasky ip_mfilter_free(imf); 204559854ecfSHans Petter Selasky goto restart; 2046e43cc4aeSHajimu UMEMOTO } 2047e43cc4aeSHajimu UMEMOTO } 2048e43cc4aeSHajimu UMEMOTO } 2049e43cc4aeSHajimu UMEMOTO 2050df8bae1dSRodney W. Grimes /* 2051fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 2052fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 2053c3229e05SDavid Greenman */ 2054d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 2055df8bae1dSRodney W. Grimes struct inpcb * 2056136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 205768e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 2058df8bae1dSRodney W. Grimes { 2059136d4f1cSRobert Watson struct inpcb *inp; 2060d5e8a67eSHajimu UMEMOTO #ifdef INET6 2061d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 2062d5e8a67eSHajimu UMEMOTO #else 2063d5e8a67eSHajimu UMEMOTO int matchwild = 3; 2064d5e8a67eSHajimu UMEMOTO #endif 2065d5e8a67eSHajimu UMEMOTO int wildcard; 20667bc4aca7SDavid Greenman 206768e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 206868e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2069fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 20701b73ca0bSSam Leffler 207168e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 2072c3229e05SDavid Greenman struct inpcbhead *head; 2073c3229e05SDavid Greenman /* 2074c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 2075c3229e05SDavid Greenman * matches the local address and port we're looking for. 2076c3229e05SDavid Greenman */ 2077712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2078712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2079b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2080cfa1ca9dSYoshinobu Inoue #ifdef INET6 2081413628a7SBjoern A. Zeeb /* XXX inp locking */ 2082369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2083cfa1ca9dSYoshinobu Inoue continue; 2084cfa1ca9dSYoshinobu Inoue #endif 2085c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 2086c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2087c3229e05SDavid Greenman inp->inp_lport == lport) { 2088c3229e05SDavid Greenman /* 2089413628a7SBjoern A. Zeeb * Found? 2090c3229e05SDavid Greenman */ 2091413628a7SBjoern A. Zeeb if (cred == NULL || 20920304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 20930304c731SJamie Gritton inp->inp_cred->cr_prison)) 2094c3229e05SDavid Greenman return (inp); 2095df8bae1dSRodney W. Grimes } 2096c3229e05SDavid Greenman } 2097c3229e05SDavid Greenman /* 2098c3229e05SDavid Greenman * Not found. 2099c3229e05SDavid Greenman */ 2100c3229e05SDavid Greenman return (NULL); 2101c3229e05SDavid Greenman } else { 2102c3229e05SDavid Greenman struct inpcbporthead *porthash; 2103c3229e05SDavid Greenman struct inpcbport *phd; 2104c3229e05SDavid Greenman struct inpcb *match = NULL; 2105c3229e05SDavid Greenman /* 2106c3229e05SDavid Greenman * Best fit PCB lookup. 2107c3229e05SDavid Greenman * 2108c3229e05SDavid Greenman * First see if this local port is in use by looking on the 2109c3229e05SDavid Greenman * port hash list. 2110c3229e05SDavid Greenman */ 2111712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 2112712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 2113b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 2114c3229e05SDavid Greenman if (phd->phd_port == lport) 2115c3229e05SDavid Greenman break; 2116c3229e05SDavid Greenman } 2117c3229e05SDavid Greenman if (phd != NULL) { 2118c3229e05SDavid Greenman /* 2119c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 2120c3229e05SDavid Greenman * fit. 2121c3229e05SDavid Greenman */ 2122b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 2123c3229e05SDavid Greenman wildcard = 0; 2124413628a7SBjoern A. Zeeb if (cred != NULL && 21250304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 21260304c731SJamie Gritton cred->cr_prison)) 2127413628a7SBjoern A. Zeeb continue; 2128cfa1ca9dSYoshinobu Inoue #ifdef INET6 2129413628a7SBjoern A. Zeeb /* XXX inp locking */ 2130369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2131cfa1ca9dSYoshinobu Inoue continue; 2132d5e8a67eSHajimu UMEMOTO /* 2133d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 2134d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 2135d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 2136d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 2137d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 2138d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 2139d5e8a67eSHajimu UMEMOTO * 2140d5e8a67eSHajimu UMEMOTO * Note that the case only happens 2141d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 2142d5e8a67eSHajimu UMEMOTO * the condition that the use of the 2143d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 2144d5e8a67eSHajimu UMEMOTO */ 2145d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 2146d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2147cfa1ca9dSYoshinobu Inoue #endif 2148c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 2149c3229e05SDavid Greenman wildcard++; 215015bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 215115bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 215215bd2b43SDavid Greenman wildcard++; 215315bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 215415bd2b43SDavid Greenman continue; 215515bd2b43SDavid Greenman } else { 215615bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 215715bd2b43SDavid Greenman wildcard++; 215815bd2b43SDavid Greenman } 2159df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 2160df8bae1dSRodney W. Grimes match = inp; 2161df8bae1dSRodney W. Grimes matchwild = wildcard; 2162413628a7SBjoern A. Zeeb if (matchwild == 0) 2163df8bae1dSRodney W. Grimes break; 2164df8bae1dSRodney W. Grimes } 2165df8bae1dSRodney W. Grimes } 21663dbdc25cSDavid Greenman } 2167df8bae1dSRodney W. Grimes return (match); 2168df8bae1dSRodney W. Grimes } 2169c3229e05SDavid Greenman } 2170d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 217115bd2b43SDavid Greenman 21721a43cff9SSean Bruno static struct inpcb * 21731a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 21741a43cff9SSean Bruno const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr, 2175a034518aSAndrew Gallatin uint16_t fport, int lookupflags, int numa_domain) 21761a43cff9SSean Bruno { 2177a034518aSAndrew Gallatin struct inpcb *local_wild, *numa_wild; 21781a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 21791a43cff9SSean Bruno struct inpcblbgroup *grp; 21808be02ee4SMark Johnston uint32_t idx; 21811a43cff9SSean Bruno 21821a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 21831a43cff9SSean Bruno 21849d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 21859d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 21861a43cff9SSean Bruno 21871a43cff9SSean Bruno /* 21881a43cff9SSean Bruno * Order of socket selection: 21891a43cff9SSean Bruno * 1. non-wild. 21901a43cff9SSean Bruno * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD). 21911a43cff9SSean Bruno * 21921a43cff9SSean Bruno * NOTE: 21931a43cff9SSean Bruno * - Load balanced group does not contain jailed sockets 21941a43cff9SSean Bruno * - Load balanced group does not contain IPv4 mapped INET6 wild sockets 21951a43cff9SSean Bruno */ 21968be02ee4SMark Johnston local_wild = NULL; 2197a034518aSAndrew Gallatin numa_wild = NULL; 219854af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 21991a43cff9SSean Bruno #ifdef INET6 22001a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 22011a43cff9SSean Bruno continue; 22021a43cff9SSean Bruno #endif 22038be02ee4SMark Johnston if (grp->il_lport != lport) 22048be02ee4SMark Johnston continue; 22051a43cff9SSean Bruno 22068be02ee4SMark Johnston idx = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, lport, fport) % 22078be02ee4SMark Johnston grp->il_inpcnt; 2208a034518aSAndrew Gallatin if (grp->il_laddr.s_addr == laddr->s_addr) { 2209a034518aSAndrew Gallatin if (numa_domain == M_NODOM || 2210a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain) { 22111a43cff9SSean Bruno return (grp->il_inp[idx]); 2212a034518aSAndrew Gallatin } else { 2213a034518aSAndrew Gallatin numa_wild = grp->il_inp[idx]; 2214a034518aSAndrew Gallatin } 2215a034518aSAndrew Gallatin } 22161a43cff9SSean Bruno if (grp->il_laddr.s_addr == INADDR_ANY && 2217a034518aSAndrew Gallatin (lookupflags & INPLOOKUP_WILDCARD) != 0 && 2218a034518aSAndrew Gallatin (local_wild == NULL || numa_domain == M_NODOM || 2219a034518aSAndrew Gallatin grp->il_numa_domain == numa_domain)) { 22201a43cff9SSean Bruno local_wild = grp->il_inp[idx]; 22211a43cff9SSean Bruno } 2222a034518aSAndrew Gallatin } 2223a034518aSAndrew Gallatin if (numa_wild != NULL) 2224a034518aSAndrew Gallatin return (numa_wild); 2225a034518aSAndrew Gallatin 22261a43cff9SSean Bruno return (local_wild); 22271a43cff9SSean Bruno } 22281a43cff9SSean Bruno 222915bd2b43SDavid Greenman /* 2230fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2231*db0ac6deSCy Schubert * that the caller has either locked the hash list, which usually happens 2232*db0ac6deSCy Schubert * for bind(2) operations, or is in SMR section, which happens when sorting 2233*db0ac6deSCy Schubert * out incoming packets. 223415bd2b43SDavid Greenman */ 2235fa046d87SRobert Watson static struct inpcb * 2236fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 223768e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2238a034518aSAndrew Gallatin struct ifnet *ifp, uint8_t numa_domain) 223915bd2b43SDavid Greenman { 224015bd2b43SDavid Greenman struct inpcbhead *head; 2241413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 224215bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 224315bd2b43SDavid Greenman 224468e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 224568e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2246d797164aSGleb Smirnoff INP_HASH_LOCK_ASSERT(pcbinfo); 2247d797164aSGleb Smirnoff 224815bd2b43SDavid Greenman /* 224915bd2b43SDavid Greenman * First look for an exact match. 225015bd2b43SDavid Greenman */ 2251413628a7SBjoern A. Zeeb tmpinp = NULL; 2252712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 2253712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 2254b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2255cfa1ca9dSYoshinobu Inoue #ifdef INET6 2256413628a7SBjoern A. Zeeb /* XXX inp locking */ 2257369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2258cfa1ca9dSYoshinobu Inoue continue; 2259cfa1ca9dSYoshinobu Inoue #endif 22606d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2261ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2262ca98b82cSDavid Greenman inp->inp_fport == fport && 2263413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 2264413628a7SBjoern A. Zeeb /* 2265413628a7SBjoern A. Zeeb * XXX We should be able to directly return 2266413628a7SBjoern A. Zeeb * the inp here, without any checks. 2267413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 2268413628a7SBjoern A. Zeeb */ 22690304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 2270c3229e05SDavid Greenman return (inp); 2271413628a7SBjoern A. Zeeb if (tmpinp == NULL) 2272413628a7SBjoern A. Zeeb tmpinp = inp; 2273c3229e05SDavid Greenman } 2274413628a7SBjoern A. Zeeb } 2275413628a7SBjoern A. Zeeb if (tmpinp != NULL) 2276413628a7SBjoern A. Zeeb return (tmpinp); 2277e3fd5ffdSRobert Watson 2278e3fd5ffdSRobert Watson /* 22791a43cff9SSean Bruno * Then look in lb group (for wildcard match). 22801a43cff9SSean Bruno */ 2281d9ff5789SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 22821a43cff9SSean Bruno inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr, 2283a034518aSAndrew Gallatin fport, lookupflags, numa_domain); 2284d9ff5789SMark Johnston if (inp != NULL) 22851a43cff9SSean Bruno return (inp); 22861a43cff9SSean Bruno } 22871a43cff9SSean Bruno 22881a43cff9SSean Bruno /* 2289e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 2290e3fd5ffdSRobert Watson */ 229168e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2292413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 2293e3fd5ffdSRobert Watson #ifdef INET6 2294cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 2295e3fd5ffdSRobert Watson #endif 2296413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 2297413628a7SBjoern A. Zeeb int injail; 2298413628a7SBjoern A. Zeeb 2299413628a7SBjoern A. Zeeb /* 2300413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2301413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2302413628a7SBjoern A. Zeeb * 2. jailed, wild. 2303413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2304413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2305413628a7SBjoern A. Zeeb */ 23066d6a026bSDavid Greenman 2307712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2308712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2309b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2310cfa1ca9dSYoshinobu Inoue #ifdef INET6 2311413628a7SBjoern A. Zeeb /* XXX inp locking */ 2312369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2313cfa1ca9dSYoshinobu Inoue continue; 2314cfa1ca9dSYoshinobu Inoue #endif 2315413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2316413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2317413628a7SBjoern A. Zeeb continue; 2318413628a7SBjoern A. Zeeb 23190304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2320413628a7SBjoern A. Zeeb if (injail) { 2321b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2322b89e82ddSJamie Gritton &laddr) != 0) 2323413628a7SBjoern A. Zeeb continue; 2324413628a7SBjoern A. Zeeb } else { 2325413628a7SBjoern A. Zeeb if (local_exact != NULL) 2326413628a7SBjoern A. Zeeb continue; 2327413628a7SBjoern A. Zeeb } 2328413628a7SBjoern A. Zeeb 2329413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2330413628a7SBjoern A. Zeeb if (injail) 2331c3229e05SDavid Greenman return (inp); 2332413628a7SBjoern A. Zeeb else 2333413628a7SBjoern A. Zeeb local_exact = inp; 2334413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2335e3fd5ffdSRobert Watson #ifdef INET6 2336413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23375cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2338cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2339cfa1ca9dSYoshinobu Inoue else 234083e521ecSBjoern A. Zeeb #endif 2341413628a7SBjoern A. Zeeb if (injail) 2342413628a7SBjoern A. Zeeb jail_wild = inp; 2343413628a7SBjoern A. Zeeb else 23446d6a026bSDavid Greenman local_wild = inp; 23456d6a026bSDavid Greenman } 2346413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2347413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2348413628a7SBjoern A. Zeeb return (jail_wild); 2349413628a7SBjoern A. Zeeb if (local_exact != NULL) 2350413628a7SBjoern A. Zeeb return (local_exact); 2351413628a7SBjoern A. Zeeb if (local_wild != NULL) 2352c3229e05SDavid Greenman return (local_wild); 2353413628a7SBjoern A. Zeeb #ifdef INET6 2354413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2355413628a7SBjoern A. Zeeb return (local_wild_mapped); 235683e521ecSBjoern A. Zeeb #endif 235768e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2358413628a7SBjoern A. Zeeb 23596d6a026bSDavid Greenman return (NULL); 236015bd2b43SDavid Greenman } 2361fa046d87SRobert Watson 2362fa046d87SRobert Watson /* 2363fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2364fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2365fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2366fa046d87SRobert Watson */ 2367fa046d87SRobert Watson static struct inpcb * 2368fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2369fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2370a034518aSAndrew Gallatin struct ifnet *ifp, uint8_t numa_domain) 2371fa046d87SRobert Watson { 2372fa046d87SRobert Watson struct inpcb *inp; 2373fa046d87SRobert Watson 2374*db0ac6deSCy Schubert smr_enter(pcbinfo->ipi_smr); 2375fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 237608d9c920SGleb Smirnoff lookupflags & INPLOOKUP_WILDCARD, ifp, numa_domain); 2377fa046d87SRobert Watson if (inp != NULL) { 2378*db0ac6deSCy Schubert if (__predict_false(inp_smr_lock(inp, 2379*db0ac6deSCy Schubert (lookupflags & INPLOOKUP_LOCKMASK)) == false)) 2380266f97b5SCy Schubert inp = NULL; 2381*db0ac6deSCy Schubert } else 2382*db0ac6deSCy Schubert smr_exit(pcbinfo->ipi_smr); 2383d797164aSGleb Smirnoff 2384fa046d87SRobert Watson return (inp); 2385fa046d87SRobert Watson } 2386fa046d87SRobert Watson 2387fa046d87SRobert Watson /* 2388d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2389d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 2390fa046d87SRobert Watson */ 2391fa046d87SRobert Watson struct inpcb * 2392fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2393fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2394fa046d87SRobert Watson { 2395fa046d87SRobert Watson 2396fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2397fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 23981db08fbeSGleb Smirnoff KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 23991db08fbeSGleb Smirnoff ("%s: LOCKPCB not set", __func__)); 2400fa046d87SRobert Watson 2401fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2402a034518aSAndrew Gallatin lookupflags, ifp, M_NODOM)); 2403fa046d87SRobert Watson } 2404d3c1f003SRobert Watson 2405d3c1f003SRobert Watson struct inpcb * 2406d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2407d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2408d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2409d3c1f003SRobert Watson { 2410d3c1f003SRobert Watson 2411d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2412d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 24131db08fbeSGleb Smirnoff KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 24141db08fbeSGleb Smirnoff ("%s: LOCKPCB not set", __func__)); 2415d3c1f003SRobert Watson 2416d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2417a034518aSAndrew Gallatin lookupflags, ifp, m->m_pkthdr.numa_domain)); 2418d3c1f003SRobert Watson } 241967107f45SBjoern A. Zeeb #endif /* INET */ 242015bd2b43SDavid Greenman 24217bc4aca7SDavid Greenman /* 2422c3229e05SDavid Greenman * Insert PCB onto various hash lists. 24237bc4aca7SDavid Greenman */ 2424*db0ac6deSCy Schubert int 2425*db0ac6deSCy Schubert in_pcbinshash(struct inpcb *inp) 242615bd2b43SDavid Greenman { 2427c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2428c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2429c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2430c3229e05SDavid Greenman struct inpcbport *phd; 2431cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 24321a43cff9SSean Bruno int so_options; 243315bd2b43SDavid Greenman 24348501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2435fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2436fa046d87SRobert Watson 2437111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2438111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2439602cc7f1SRobert Watson 2440cfa1ca9dSYoshinobu Inoue #ifdef INET6 2441cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 24421b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2443cfa1ca9dSYoshinobu Inoue else 244483e521ecSBjoern A. Zeeb #endif 2445cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2446cfa1ca9dSYoshinobu Inoue 2447712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2448712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 244915bd2b43SDavid Greenman 2450712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2451712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2452c3229e05SDavid Greenman 2453c3229e05SDavid Greenman /* 24541a43cff9SSean Bruno * Add entry to load balance group. 24551a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 24561a43cff9SSean Bruno */ 24571a43cff9SSean Bruno so_options = inp_so_options(inp); 24581a43cff9SSean Bruno if (so_options & SO_REUSEPORT_LB) { 2459a034518aSAndrew Gallatin int ret = in_pcbinslbgrouphash(inp, M_NODOM); 24601a43cff9SSean Bruno if (ret) { 24611a43cff9SSean Bruno /* pcb lb group malloc fail (ret=ENOBUFS). */ 24621a43cff9SSean Bruno return (ret); 24631a43cff9SSean Bruno } 24641a43cff9SSean Bruno } 24651a43cff9SSean Bruno 24661a43cff9SSean Bruno /* 2467c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2468c3229e05SDavid Greenman */ 2469b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2470c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2471c3229e05SDavid Greenman break; 2472c3229e05SDavid Greenman } 2473c3229e05SDavid Greenman /* 2474c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2475c3229e05SDavid Greenman */ 2476c3229e05SDavid Greenman if (phd == NULL) { 2477*db0ac6deSCy Schubert phd = uma_zalloc_smr(pcbinfo->ipi_portzone, M_NOWAIT); 2478c3229e05SDavid Greenman if (phd == NULL) { 2479c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2480c3229e05SDavid Greenman } 2481c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2482b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2483b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2484c3229e05SDavid Greenman } 2485c3229e05SDavid Greenman inp->inp_phd = phd; 2486b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2487b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2488111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 2489*db0ac6deSCy Schubert 2490c3229e05SDavid Greenman return (0); 249115bd2b43SDavid Greenman } 249215bd2b43SDavid Greenman 249352cd27cbSRobert Watson /* 2494c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2495c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2496c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2497c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2498*db0ac6deSCy Schubert * 2499*db0ac6deSCy Schubert * XXXGL: a race between this function and SMR-protected hash iterator 2500*db0ac6deSCy Schubert * will lead to iterator traversing a possibly wrong hash list. However, 2501*db0ac6deSCy Schubert * this race should have been here since change from rwlock to epoch. 2502c3229e05SDavid Greenman */ 250315bd2b43SDavid Greenman void 2504*db0ac6deSCy Schubert in_pcbrehash(struct inpcb *inp) 250515bd2b43SDavid Greenman { 250659daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 250715bd2b43SDavid Greenman struct inpcbhead *head; 2508cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 250915bd2b43SDavid Greenman 25108501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2511fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2512fa046d87SRobert Watson 2513111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2514111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2515602cc7f1SRobert Watson 2516cfa1ca9dSYoshinobu Inoue #ifdef INET6 2517cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 25181b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2519cfa1ca9dSYoshinobu Inoue else 252083e521ecSBjoern A. Zeeb #endif 2521cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2522cfa1ca9dSYoshinobu Inoue 2523712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2524712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 252515bd2b43SDavid Greenman 2526b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2527b872626dSMatt Macy CK_LIST_INSERT_HEAD(head, inp, inp_hash); 2528c3229e05SDavid Greenman } 2529c3229e05SDavid Greenman 2530c3229e05SDavid Greenman /* 253184cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 253284cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 253384cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 253484cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 253584cc0778SGeorge V. Neville-Neil */ 253684cc0778SGeorge V. Neville-Neil void 253784cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 253884cc0778SGeorge V. Neville-Neil { 253984cc0778SGeorge V. Neville-Neil 2540fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 254184cc0778SGeorge V. Neville-Neil return; 254284cc0778SGeorge V. Neville-Neil } 254384cc0778SGeorge V. Neville-Neil 254484cc0778SGeorge V. Neville-Neil /* 2545a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2546a557af22SRobert Watson * label change into the in_pcb for the socket. 2547a557af22SRobert Watson */ 2548a557af22SRobert Watson void 2549136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2550a557af22SRobert Watson { 2551a557af22SRobert Watson #ifdef MAC 2552a557af22SRobert Watson struct inpcb *inp; 2553a557af22SRobert Watson 25544c7c478dSRobert Watson inp = sotoinpcb(so); 25554c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2556602cc7f1SRobert Watson 25578501a69cSRobert Watson INP_WLOCK(inp); 2558310e7cebSRobert Watson SOCK_LOCK(so); 2559a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2560310e7cebSRobert Watson SOCK_UNLOCK(so); 25618501a69cSRobert Watson INP_WUNLOCK(inp); 2562a557af22SRobert Watson #endif 2563a557af22SRobert Watson } 25645f311da2SMike Silbersack 25655f311da2SMike Silbersack /* 2566ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2567ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2568ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2569ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2570ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 25715f311da2SMike Silbersack */ 2572aae49dd3SBjoern A. Zeeb static void 2573136d4f1cSRobert Watson ipport_tick(void *xtp) 25745f311da2SMike Silbersack { 25758b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2576ad3a630fSRobert Watson 25775ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 25788b615593SMarko Zec VNET_FOREACH(vnet_iter) { 25798b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 25808b615593SMarko Zec if (V_ipport_tcpallocs <= 25818b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2582603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2583603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2584ad3a630fSRobert Watson } else 2585603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2586603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 25878b615593SMarko Zec CURVNET_RESTORE(); 25888b615593SMarko Zec } 25895ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 25905f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 25915f311da2SMike Silbersack } 2592497057eeSRobert Watson 2593aae49dd3SBjoern A. Zeeb static void 2594aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2595aae49dd3SBjoern A. Zeeb { 2596aae49dd3SBjoern A. Zeeb 2597aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2598aae49dd3SBjoern A. Zeeb } 2599aae49dd3SBjoern A. Zeeb 2600aae49dd3SBjoern A. Zeeb /* 2601aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2602aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2603aae49dd3SBjoern A. Zeeb */ 2604aae49dd3SBjoern A. Zeeb static void 2605aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2606aae49dd3SBjoern A. Zeeb { 2607aae49dd3SBjoern A. Zeeb 2608aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2609fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2610aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2611aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2612aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2613aae49dd3SBjoern A. Zeeb } 2614aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2615aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2616aae49dd3SBjoern A. Zeeb 26173d585327SKip Macy void 26183d585327SKip Macy inp_wlock(struct inpcb *inp) 26193d585327SKip Macy { 26203d585327SKip Macy 26218501a69cSRobert Watson INP_WLOCK(inp); 26223d585327SKip Macy } 26233d585327SKip Macy 26243d585327SKip Macy void 26253d585327SKip Macy inp_wunlock(struct inpcb *inp) 26263d585327SKip Macy { 26273d585327SKip Macy 26288501a69cSRobert Watson INP_WUNLOCK(inp); 26293d585327SKip Macy } 26303d585327SKip Macy 26313d585327SKip Macy void 26323d585327SKip Macy inp_rlock(struct inpcb *inp) 26333d585327SKip Macy { 26343d585327SKip Macy 2635a69042a5SRobert Watson INP_RLOCK(inp); 26363d585327SKip Macy } 26373d585327SKip Macy 26383d585327SKip Macy void 26393d585327SKip Macy inp_runlock(struct inpcb *inp) 26403d585327SKip Macy { 26413d585327SKip Macy 2642a69042a5SRobert Watson INP_RUNLOCK(inp); 26433d585327SKip Macy } 26443d585327SKip Macy 2645c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 26463d585327SKip Macy void 2647e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 26483d585327SKip Macy { 26493d585327SKip Macy 26508501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 26513d585327SKip Macy } 26523d585327SKip Macy 26533d585327SKip Macy void 2654e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 26553d585327SKip Macy { 26563d585327SKip Macy 26573d585327SKip Macy INP_UNLOCK_ASSERT(inp); 26583d585327SKip Macy } 26593d585327SKip Macy #endif 26603d585327SKip Macy 26619378e437SKip Macy void 26629378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 26639378e437SKip Macy { 2664*db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo, 2665*db0ac6deSCy Schubert INPLOOKUP_WLOCKPCB); 26669378e437SKip Macy struct inpcb *inp; 26679378e437SKip Macy 2668*db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) 26699378e437SKip Macy func(inp, arg); 26709378e437SKip Macy } 26719378e437SKip Macy 26729378e437SKip Macy struct socket * 26739378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 26749378e437SKip Macy { 26759378e437SKip Macy 26769378e437SKip Macy INP_WLOCK_ASSERT(inp); 26779378e437SKip Macy return (inp->inp_socket); 26789378e437SKip Macy } 26799378e437SKip Macy 26809378e437SKip Macy struct tcpcb * 26819378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 26829378e437SKip Macy { 26839378e437SKip Macy 26849378e437SKip Macy INP_WLOCK_ASSERT(inp); 26859378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 26869378e437SKip Macy } 26879378e437SKip Macy 26889378e437SKip Macy int 26899378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 26909378e437SKip Macy { 26919378e437SKip Macy 26929378e437SKip Macy return (inp->inp_ip_tos); 26939378e437SKip Macy } 26949378e437SKip Macy 26959378e437SKip Macy void 26969378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 26979378e437SKip Macy { 26989378e437SKip Macy 26999378e437SKip Macy inp->inp_ip_tos = val; 27009378e437SKip Macy } 27019378e437SKip Macy 27029378e437SKip Macy void 2703df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 27049d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 27059378e437SKip Macy { 27069378e437SKip Macy 27079d29c635SKip Macy INP_LOCK_ASSERT(inp); 2708df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2709df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 27109378e437SKip Macy *lp = inp->inp_lport; 27119378e437SKip Macy *fp = inp->inp_fport; 27129378e437SKip Macy } 27139378e437SKip Macy 2714dd0e6c38SKip Macy struct inpcb * 2715dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2716dd0e6c38SKip Macy { 2717dd0e6c38SKip Macy 2718dd0e6c38SKip Macy return (sotoinpcb(so)); 2719dd0e6c38SKip Macy } 2720dd0e6c38SKip Macy 2721dd0e6c38SKip Macy struct tcpcb * 2722dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2723dd0e6c38SKip Macy { 2724dd0e6c38SKip Macy 2725dd0e6c38SKip Macy return (sototcpcb(so)); 2726dd0e6c38SKip Macy } 2727dd0e6c38SKip Macy 2728cc65eb4eSGleb Smirnoff /* 2729cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2730cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2731cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2732cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2733cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2734cc65eb4eSGleb Smirnoff * hidden from users. 2735cc65eb4eSGleb Smirnoff */ 2736cc65eb4eSGleb Smirnoff void 2737cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2738cc65eb4eSGleb Smirnoff { 2739cc65eb4eSGleb Smirnoff 274079db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2741cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2742cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2743cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2744cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2745cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 27463a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2747cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2748cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2749cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2750cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2751cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2752cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2753cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2754cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2755cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2756cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2757cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2758cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2759cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2760cc65eb4eSGleb Smirnoff } 2761cc65eb4eSGleb Smirnoff 2762497057eeSRobert Watson #ifdef DDB 2763497057eeSRobert Watson static void 2764497057eeSRobert Watson db_print_indent(int indent) 2765497057eeSRobert Watson { 2766497057eeSRobert Watson int i; 2767497057eeSRobert Watson 2768497057eeSRobert Watson for (i = 0; i < indent; i++) 2769497057eeSRobert Watson db_printf(" "); 2770497057eeSRobert Watson } 2771497057eeSRobert Watson 2772497057eeSRobert Watson static void 2773497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2774497057eeSRobert Watson { 2775497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2776497057eeSRobert Watson 2777497057eeSRobert Watson db_print_indent(indent); 2778497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2779497057eeSRobert Watson 2780497057eeSRobert Watson indent += 2; 2781497057eeSRobert Watson 278203dc38a4SRobert Watson #ifdef INET6 2783dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2784497057eeSRobert Watson /* IPv6. */ 2785497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2786497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 278783e521ecSBjoern A. Zeeb } else 278803dc38a4SRobert Watson #endif 278983e521ecSBjoern A. Zeeb { 2790497057eeSRobert Watson /* IPv4. */ 2791497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2792497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2793497057eeSRobert Watson } 2794497057eeSRobert Watson db_print_indent(indent); 2795497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2796497057eeSRobert Watson ntohs(inc->inc_lport)); 2797497057eeSRobert Watson db_print_indent(indent); 2798497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2799497057eeSRobert Watson ntohs(inc->inc_fport)); 2800497057eeSRobert Watson } 2801497057eeSRobert Watson 2802497057eeSRobert Watson static void 2803497057eeSRobert Watson db_print_inpflags(int inp_flags) 2804497057eeSRobert Watson { 2805497057eeSRobert Watson int comma; 2806497057eeSRobert Watson 2807497057eeSRobert Watson comma = 0; 2808497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2809497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2810497057eeSRobert Watson comma = 1; 2811497057eeSRobert Watson } 2812497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2813497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2814497057eeSRobert Watson comma = 1; 2815497057eeSRobert Watson } 2816497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2817497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2818497057eeSRobert Watson comma = 1; 2819497057eeSRobert Watson } 2820dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2821dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2822dce33a45SErmal Luçi comma = 1; 2823dce33a45SErmal Luçi } 2824497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2825497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2826497057eeSRobert Watson comma = 1; 2827497057eeSRobert Watson } 2828497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2829497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2830497057eeSRobert Watson comma = 1; 2831497057eeSRobert Watson } 2832497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2833497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2834497057eeSRobert Watson comma = 1; 2835497057eeSRobert Watson } 2836497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2837497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2838497057eeSRobert Watson comma = 1; 2839497057eeSRobert Watson } 2840497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2841497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2842497057eeSRobert Watson comma = 1; 2843497057eeSRobert Watson } 2844497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2845497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2846497057eeSRobert Watson comma = 1; 2847497057eeSRobert Watson } 2848497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2849497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2850497057eeSRobert Watson comma = 1; 2851497057eeSRobert Watson } 2852497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2853497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2854497057eeSRobert Watson comma = 1; 2855497057eeSRobert Watson } 28563cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 28573cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 28583cca425bSMichael Tuexen comma = 1; 28593cca425bSMichael Tuexen } 2860497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2861497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2862497057eeSRobert Watson comma = 1; 2863497057eeSRobert Watson } 2864497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2865497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2866497057eeSRobert Watson comma = 1; 2867497057eeSRobert Watson } 2868497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2869497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2870497057eeSRobert Watson comma = 1; 2871497057eeSRobert Watson } 2872497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2873497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2874497057eeSRobert Watson comma = 1; 2875497057eeSRobert Watson } 2876497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2877497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2878497057eeSRobert Watson comma = 1; 2879497057eeSRobert Watson } 2880497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2881497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2882497057eeSRobert Watson comma = 1; 2883497057eeSRobert Watson } 2884497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2885497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2886497057eeSRobert Watson comma = 1; 2887497057eeSRobert Watson } 2888497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2889497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2890497057eeSRobert Watson comma = 1; 2891497057eeSRobert Watson } 2892497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2893497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2894497057eeSRobert Watson comma = 1; 2895497057eeSRobert Watson } 2896ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2897ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2898ad71fe3cSRobert Watson comma = 1; 2899ad71fe3cSRobert Watson } 2900ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2901ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2902ad71fe3cSRobert Watson comma = 1; 2903ad71fe3cSRobert Watson } 2904ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2905ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2906ad71fe3cSRobert Watson comma = 1; 2907ad71fe3cSRobert Watson } 2908ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2909ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2910ad71fe3cSRobert Watson comma = 1; 2911ad71fe3cSRobert Watson } 2912497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2913497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2914497057eeSRobert Watson comma = 1; 2915497057eeSRobert Watson } 2916497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2917497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2918497057eeSRobert Watson comma = 1; 2919497057eeSRobert Watson } 2920497057eeSRobert Watson } 2921497057eeSRobert Watson 2922497057eeSRobert Watson static void 2923497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2924497057eeSRobert Watson { 2925497057eeSRobert Watson int comma; 2926497057eeSRobert Watson 2927497057eeSRobert Watson comma = 0; 2928497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2929497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2930497057eeSRobert Watson comma = 1; 2931497057eeSRobert Watson } 2932497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2933497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2934497057eeSRobert Watson comma = 1; 2935497057eeSRobert Watson } 2936497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2937497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2938497057eeSRobert Watson comma = 1; 2939497057eeSRobert Watson } 2940497057eeSRobert Watson } 2941497057eeSRobert Watson 29426d888973SRobert Watson static void 2943497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2944497057eeSRobert Watson { 2945497057eeSRobert Watson 2946497057eeSRobert Watson db_print_indent(indent); 2947497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2948497057eeSRobert Watson 2949497057eeSRobert Watson indent += 2; 2950497057eeSRobert Watson 2951497057eeSRobert Watson db_print_indent(indent); 2952497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2953497057eeSRobert Watson 2954497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2955497057eeSRobert Watson 2956497057eeSRobert Watson db_print_indent(indent); 2957497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2958497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2959497057eeSRobert Watson 2960497057eeSRobert Watson db_print_indent(indent); 2961497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2962497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2963497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2964497057eeSRobert Watson db_printf(")\n"); 2965497057eeSRobert Watson 2966497057eeSRobert Watson db_print_indent(indent); 2967497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2968497057eeSRobert Watson inp->inp_vflag); 2969497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2970497057eeSRobert Watson db_printf(")\n"); 2971497057eeSRobert Watson 2972497057eeSRobert Watson db_print_indent(indent); 2973497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2974497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2975497057eeSRobert Watson 2976497057eeSRobert Watson db_print_indent(indent); 2977497057eeSRobert Watson #ifdef INET6 2978497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2979497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2980497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2981497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2982497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2983497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2984497057eeSRobert Watson inp->in6p_hops); 2985497057eeSRobert Watson } else 2986497057eeSRobert Watson #endif 2987497057eeSRobert Watson { 2988497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2989497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2990497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2991497057eeSRobert Watson } 2992497057eeSRobert Watson 2993497057eeSRobert Watson db_print_indent(indent); 2994497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2995497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 2996497057eeSRobert Watson } 2997497057eeSRobert Watson 2998497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2999497057eeSRobert Watson { 3000497057eeSRobert Watson struct inpcb *inp; 3001497057eeSRobert Watson 3002497057eeSRobert Watson if (!have_addr) { 3003497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3004497057eeSRobert Watson return; 3005497057eeSRobert Watson } 3006497057eeSRobert Watson inp = (struct inpcb *)addr; 3007497057eeSRobert Watson 3008497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3009497057eeSRobert Watson } 301083e521ecSBjoern A. Zeeb #endif /* DDB */ 3011f3e7afe2SHans Petter Selasky 3012f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3013f3e7afe2SHans Petter Selasky /* 3014f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3015f3e7afe2SHans Petter Selasky * if any. 3016f3e7afe2SHans Petter Selasky */ 3017f3e7afe2SHans Petter Selasky int 3018f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3019f3e7afe2SHans Petter Selasky { 3020f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3021f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 302220abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3023f3e7afe2SHans Petter Selasky }; 3024f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3025f3e7afe2SHans Petter Selasky int error; 3026f3e7afe2SHans Petter Selasky 3027f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3028f3e7afe2SHans Petter Selasky if (mst == NULL) 3029f3e7afe2SHans Petter Selasky return (EINVAL); 3030f3e7afe2SHans Petter Selasky 3031c782ea8bSJohn Baldwin if (mst->sw->snd_tag_modify == NULL) { 3032f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3033f3e7afe2SHans Petter Selasky } else { 3034c782ea8bSJohn Baldwin error = mst->sw->snd_tag_modify(mst, ¶ms); 3035f3e7afe2SHans Petter Selasky } 3036f3e7afe2SHans Petter Selasky return (error); 3037f3e7afe2SHans Petter Selasky } 3038f3e7afe2SHans Petter Selasky 3039f3e7afe2SHans Petter Selasky /* 3040f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3041f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3042f3e7afe2SHans Petter Selasky */ 3043f3e7afe2SHans Petter Selasky int 3044f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3045f3e7afe2SHans Petter Selasky { 3046f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3047f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3048f3e7afe2SHans Petter Selasky int error; 3049f3e7afe2SHans Petter Selasky 3050f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3051f3e7afe2SHans Petter Selasky if (mst == NULL) 3052f3e7afe2SHans Petter Selasky return (EINVAL); 3053f3e7afe2SHans Petter Selasky 3054c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) { 3055f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3056f3e7afe2SHans Petter Selasky } else { 3057c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 3058f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3059f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3060f3e7afe2SHans Petter Selasky } 3061f3e7afe2SHans Petter Selasky return (error); 3062f3e7afe2SHans Petter Selasky } 3063f3e7afe2SHans Petter Selasky 3064f3e7afe2SHans Petter Selasky /* 306595ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 306695ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 306795ed5015SHans Petter Selasky */ 306895ed5015SHans Petter Selasky int 306995ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 307095ed5015SHans Petter Selasky { 307195ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 307295ed5015SHans Petter Selasky struct m_snd_tag *mst; 307395ed5015SHans Petter Selasky int error; 307495ed5015SHans Petter Selasky 307595ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 307695ed5015SHans Petter Selasky if (mst == NULL) 307795ed5015SHans Petter Selasky return (EINVAL); 307895ed5015SHans Petter Selasky 3079c782ea8bSJohn Baldwin if (mst->sw->snd_tag_query == NULL) 308095ed5015SHans Petter Selasky return (EOPNOTSUPP); 308195ed5015SHans Petter Selasky 3082c782ea8bSJohn Baldwin error = mst->sw->snd_tag_query(mst, ¶ms); 308395ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 308495ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 308595ed5015SHans Petter Selasky return (error); 308695ed5015SHans Petter Selasky } 308795ed5015SHans Petter Selasky 308895ed5015SHans Petter Selasky /* 3089f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3090f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3091f3e7afe2SHans Petter Selasky */ 3092f3e7afe2SHans Petter Selasky int 3093f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 309420abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 309520abea66SRandall Stewart 3096f3e7afe2SHans Petter Selasky { 3097f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 309895ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 309995ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3100f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3101f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 310298085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3103f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 310420abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3105f3e7afe2SHans Petter Selasky }; 3106f3e7afe2SHans Petter Selasky int error; 3107f3e7afe2SHans Petter Selasky 3108f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3109f3e7afe2SHans Petter Selasky 311085d8d30fSHans Petter Selasky /* 311185d8d30fSHans Petter Selasky * If there is already a send tag, or the INP is being torn 311285d8d30fSHans Petter Selasky * down, allocating a new send tag is not allowed. Else send 311385d8d30fSHans Petter Selasky * tags may leak. 311485d8d30fSHans Petter Selasky */ 311585d8d30fSHans Petter Selasky if (*st != NULL || (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) != 0) 3116f3e7afe2SHans Petter Selasky return (EINVAL); 3117f3e7afe2SHans Petter Selasky 311836e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, st); 3119903c4ee6SXin LI #ifdef INET 312020abea66SRandall Stewart if (error == 0) { 312120abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 312220abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 312336e0a362SJohn Baldwin } else if (error != EOPNOTSUPP) 312420abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3125903c4ee6SXin LI #endif 3126f3e7afe2SHans Petter Selasky return (error); 3127f3e7afe2SHans Petter Selasky } 3128f3e7afe2SHans Petter Selasky 312920abea66SRandall Stewart void 313098d7a8d9SJohn Baldwin in_pcbdetach_tag(struct m_snd_tag *mst) 313120abea66SRandall Stewart { 313220abea66SRandall Stewart 313398d7a8d9SJohn Baldwin m_snd_tag_rele(mst); 3134903c4ee6SXin LI #ifdef INET 313520abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3136903c4ee6SXin LI #endif 313720abea66SRandall Stewart } 313820abea66SRandall Stewart 3139f3e7afe2SHans Petter Selasky /* 3140f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3141f3e7afe2SHans Petter Selasky * if any: 3142f3e7afe2SHans Petter Selasky */ 3143f3e7afe2SHans Petter Selasky void 3144f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3145f3e7afe2SHans Petter Selasky { 3146f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3147f3e7afe2SHans Petter Selasky 3148f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3149f3e7afe2SHans Petter Selasky 3150f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3151f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3152f3e7afe2SHans Petter Selasky 3153f3e7afe2SHans Petter Selasky if (mst == NULL) 3154f3e7afe2SHans Petter Selasky return; 3155f3e7afe2SHans Petter Selasky 3156fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3157093e7231SHans Petter Selasky #ifdef INET 3158093e7231SHans Petter Selasky counter_u64_add(rate_limit_active, -1); 3159093e7231SHans Petter Selasky #endif 3160f3e7afe2SHans Petter Selasky } 3161f3e7afe2SHans Petter Selasky 316220abea66SRandall Stewart int 316320abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 316420abea66SRandall Stewart { 316520abea66SRandall Stewart int error; 316620abea66SRandall Stewart 316720abea66SRandall Stewart /* 316820abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 316920abea66SRandall Stewart * a route change, first drop the existing tag. Set the 317020abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 317120abea66SRandall Stewart * tag if we fail to allocate one this time. 317220abea66SRandall Stewart */ 317320abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 317420abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 317520abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 317620abea66SRandall Stewart } 317720abea66SRandall Stewart 317820abea66SRandall Stewart /* 317920abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 318020abea66SRandall Stewart * made to ensure the network interface doesn't go away until 318120abea66SRandall Stewart * all ratelimit connections are gone. The network interface 318220abea66SRandall Stewart * pointers compared below represent valid network interfaces, 318320abea66SRandall Stewart * except when comparing towards NULL. 318420abea66SRandall Stewart */ 318520abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 318620abea66SRandall Stewart error = 0; 318720abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 318820abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 318920abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 319020abea66SRandall Stewart error = 0; 319120abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 319220abea66SRandall Stewart /* 319320abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 319420abea66SRandall Stewart * to wait until there is a valid RSS hash before we 319520abea66SRandall Stewart * can proceed: 319620abea66SRandall Stewart */ 319720abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 319820abea66SRandall Stewart error = EAGAIN; 319920abea66SRandall Stewart } else { 320020abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 320120abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 320220abea66SRandall Stewart } 320320abea66SRandall Stewart } else { 320420abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 320520abea66SRandall Stewart } 320620abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 320720abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 320820abea66SRandall Stewart 320920abea66SRandall Stewart return (error); 321020abea66SRandall Stewart } 321120abea66SRandall Stewart 3212f3e7afe2SHans Petter Selasky /* 3213f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3214f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3215f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3216f3e7afe2SHans Petter Selasky */ 3217f3e7afe2SHans Petter Selasky void 3218f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3219f3e7afe2SHans Petter Selasky { 3220f3e7afe2SHans Petter Selasky struct socket *socket; 3221f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3222f3e7afe2SHans Petter Selasky bool did_upgrade; 3223f3e7afe2SHans Petter Selasky int error; 3224f3e7afe2SHans Petter Selasky 3225f3e7afe2SHans Petter Selasky if (inp == NULL) 3226f3e7afe2SHans Petter Selasky return; 3227f3e7afe2SHans Petter Selasky 3228f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3229f3e7afe2SHans Petter Selasky if (socket == NULL) 3230f3e7afe2SHans Petter Selasky return; 3231f3e7afe2SHans Petter Selasky 3232f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3233f3e7afe2SHans Petter Selasky /* 3234f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3235f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3236f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3237f3e7afe2SHans Petter Selasky * write lock. 3238f3e7afe2SHans Petter Selasky */ 3239f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3240f3e7afe2SHans Petter Selasky return; 3241f3e7afe2SHans Petter Selasky did_upgrade = 1; 3242f3e7afe2SHans Petter Selasky } else { 3243f3e7afe2SHans Petter Selasky did_upgrade = 0; 3244f3e7afe2SHans Petter Selasky } 3245f3e7afe2SHans Petter Selasky 3246f3e7afe2SHans Petter Selasky /* 3247f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3248f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3249f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3250f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3251f3e7afe2SHans Petter Selasky */ 3252f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3253f3e7afe2SHans Petter Selasky 325420abea66SRandall Stewart error = in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3255fb3bc596SJohn Baldwin 3256f3e7afe2SHans Petter Selasky if (did_upgrade) 3257f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3258f3e7afe2SHans Petter Selasky } 3259f3e7afe2SHans Petter Selasky 3260f3e7afe2SHans Petter Selasky /* 3261f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3262f3e7afe2SHans Petter Selasky */ 3263f3e7afe2SHans Petter Selasky void 3264f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3265f3e7afe2SHans Petter Selasky { 3266f3e7afe2SHans Petter Selasky bool did_upgrade; 3267f3e7afe2SHans Petter Selasky 3268f3e7afe2SHans Petter Selasky if (inp == NULL) 3269f3e7afe2SHans Petter Selasky return; 3270f3e7afe2SHans Petter Selasky 3271f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3272f3e7afe2SHans Petter Selasky return; 3273f3e7afe2SHans Petter Selasky 3274f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3275f3e7afe2SHans Petter Selasky /* 3276f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3277f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3278f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3279f3e7afe2SHans Petter Selasky * write lock. 3280f3e7afe2SHans Petter Selasky */ 3281f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3282f3e7afe2SHans Petter Selasky return; 3283f3e7afe2SHans Petter Selasky did_upgrade = 1; 3284f3e7afe2SHans Petter Selasky } else { 3285f3e7afe2SHans Petter Selasky did_upgrade = 0; 3286f3e7afe2SHans Petter Selasky } 3287f3e7afe2SHans Petter Selasky 3288f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3289f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3290f3e7afe2SHans Petter Selasky 3291f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3292f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3293f3e7afe2SHans Petter Selasky 3294f3e7afe2SHans Petter Selasky if (did_upgrade) 3295f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3296f3e7afe2SHans Petter Selasky } 329720abea66SRandall Stewart 3298903c4ee6SXin LI #ifdef INET 329920abea66SRandall Stewart static void 330020abea66SRandall Stewart rl_init(void *st) 330120abea66SRandall Stewart { 33021a714ff2SRandall Stewart rate_limit_new = counter_u64_alloc(M_WAITOK); 33031a714ff2SRandall Stewart rate_limit_chg = counter_u64_alloc(M_WAITOK); 330420abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 330520abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 330620abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 330720abea66SRandall Stewart } 330820abea66SRandall Stewart 330920abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3310903c4ee6SXin LI #endif 3311f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3312