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" 4852cd27cbSRobert Watson #include "opt_pcbgroup.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> 60cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 613ee9c3c4SRandall Stewart #include <sys/smp.h> 62df8bae1dSRodney W. Grimes #include <sys/socket.h> 63df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 64f3e7afe2SHans Petter Selasky #include <sys/sockio.h> 65acd3428bSRobert Watson #include <sys/priv.h> 66df8bae1dSRodney W. Grimes #include <sys/proc.h> 6779bdc6e5SRobert Watson #include <sys/refcount.h> 6875c13541SPoul-Henning Kamp #include <sys/jail.h> 69101f9fc8SPeter Wemm #include <sys/kernel.h> 70101f9fc8SPeter Wemm #include <sys/sysctl.h> 718781d8e9SBruce Evans 72497057eeSRobert Watson #ifdef DDB 73497057eeSRobert Watson #include <ddb/ddb.h> 74497057eeSRobert Watson #endif 75497057eeSRobert Watson 7669c2d429SJeff Roberson #include <vm/uma.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> 8959854ecfSHans Petter Selasky #ifdef INET 9059854ecfSHans Petter Selasky #include <netinet/in_var.h> 91*9ac7c6cfSAlexander V. Chernikov #include <netinet/in_fib.h> 9259854ecfSHans Petter Selasky #endif 93df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 94340c35deSJonathan Lemon #include <netinet/tcp_var.h> 953ee9c3c4SRandall Stewart #ifdef TCPHPTS 963ee9c3c4SRandall Stewart #include <netinet/tcp_hpts.h> 973ee9c3c4SRandall Stewart #endif 985f311da2SMike Silbersack #include <netinet/udp.h> 995f311da2SMike Silbersack #include <netinet/udp_var.h> 100cfa1ca9dSYoshinobu Inoue #ifdef INET6 101cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 102efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 10367107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 10467107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 105cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 106*9ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h> 10759854ecfSHans Petter Selasky #endif 108cfa1ca9dSYoshinobu Inoue 109fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 110b9234fafSSam Leffler 111aed55708SRobert Watson #include <security/mac/mac_framework.h> 112aed55708SRobert Watson 1131a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 1141a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 1151a43cff9SSean Bruno 116aae49dd3SBjoern A. Zeeb static struct callout ipport_tick_callout; 117aae49dd3SBjoern A. Zeeb 118101f9fc8SPeter Wemm /* 119101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 120101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 121101f9fc8SPeter Wemm */ 122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 128101f9fc8SPeter Wemm 129b0d22693SCrist J. Clark /* 130b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 131b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 132b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 133b0d22693SCrist J. Clark */ 134eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 135eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 136b0d22693SCrist J. Clark 1375f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 138eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */ 139eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */ 140eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */ 141eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */ 142eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs); 1435f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ipport_tcplastcount); 144eddfbb76SRobert Watson 1451e77c105SRobert Watson #define V_ipport_tcplastcount VNET(ipport_tcplastcount) 1466ac48b74SMike Silbersack 147d2025bd0SBjoern A. Zeeb static void in_pcbremlists(struct inpcb *inp); 148d2025bd0SBjoern A. Zeeb #ifdef INET 149fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 150fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 151fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 152fa046d87SRobert Watson int lookupflags, struct ifnet *ifp); 15367107f45SBjoern A. Zeeb 154bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 155bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 156bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 157bbd42ad0SPeter Wemm 158bbd42ad0SPeter Wemm static int 15982d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 160bbd42ad0SPeter Wemm { 16130a4ab08SBruce Evans int error; 16230a4ab08SBruce Evans 163f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 16430a4ab08SBruce Evans if (error == 0) { 165603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 166603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 167603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 168603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 169603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 170603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 171bbd42ad0SPeter Wemm } 17230a4ab08SBruce Evans return (error); 173bbd42ad0SPeter Wemm } 174bbd42ad0SPeter Wemm 175bbd42ad0SPeter Wemm #undef RANGECHK 176bbd42ad0SPeter Wemm 1777029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, 1787029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1796472ac3dSEd Schouten "IP Ports"); 18033b3ac06SPeter Wemm 1816df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1827029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1837029da5cSPawel Biernacki &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", 1847029da5cSPawel Biernacki ""); 1856df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1867029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1877029da5cSPawel Biernacki &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", 1887029da5cSPawel Biernacki ""); 1896df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1907029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1917029da5cSPawel Biernacki &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", 1927029da5cSPawel Biernacki ""); 1936df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1947029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1957029da5cSPawel Biernacki &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", 1967029da5cSPawel Biernacki ""); 1976df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1987029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1997029da5cSPawel Biernacki &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", 2007029da5cSPawel Biernacki ""); 2016df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 2027029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 2037029da5cSPawel Biernacki &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", 2047029da5cSPawel Biernacki ""); 2056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 2066df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 2076df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 209eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 2106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 2116df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 212eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 2136df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, 2146df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 215eddfbb76SRobert Watson &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port " 2166ee79c59SMaxim Konovalov "allocations before switching to a sequental one"); 2176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, 2186df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 219eddfbb76SRobert Watson &VNET_NAME(ipport_randomtime), 0, 2208b615593SMarko Zec "Minimum time to keep sequental port " 2216ee79c59SMaxim Konovalov "allocation before switching to a random one"); 22220abea66SRandall Stewart 22320abea66SRandall Stewart #ifdef RATELIMIT 22420abea66SRandall Stewart counter_u64_t rate_limit_active; 22520abea66SRandall Stewart counter_u64_t rate_limit_alloc_fail; 22620abea66SRandall Stewart counter_u64_t rate_limit_set_ok; 22720abea66SRandall Stewart 2287029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 22920abea66SRandall Stewart "IP Rate Limiting"); 23020abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD, 23120abea66SRandall Stewart &rate_limit_active, "Active rate limited connections"); 23220abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD, 23320abea66SRandall Stewart &rate_limit_alloc_fail, "Rate limited connection failures"); 23420abea66SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD, 23520abea66SRandall Stewart &rate_limit_set_ok, "Rate limited setting succeeded"); 23620abea66SRandall Stewart #endif /* RATELIMIT */ 23720abea66SRandall Stewart 23883e521ecSBjoern A. Zeeb #endif /* INET */ 2390312fbe9SPoul-Henning Kamp 240c3229e05SDavid Greenman /* 241c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 242c3229e05SDavid Greenman * 243de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 244de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 245de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 246c3229e05SDavid Greenman */ 247c3229e05SDavid Greenman 2481a43cff9SSean Bruno static struct inpcblbgroup * 2491a43cff9SSean Bruno in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, u_char vflag, 2501a43cff9SSean Bruno uint16_t port, const union in_dependaddr *addr, int size) 2511a43cff9SSean Bruno { 2521a43cff9SSean Bruno struct inpcblbgroup *grp; 2531a43cff9SSean Bruno size_t bytes; 2541a43cff9SSean Bruno 2551a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 2561a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 2571a43cff9SSean Bruno if (!grp) 2581a43cff9SSean Bruno return (NULL); 2591a43cff9SSean Bruno grp->il_vflag = vflag; 2601a43cff9SSean Bruno grp->il_lport = port; 2611a43cff9SSean Bruno grp->il_dependladdr = *addr; 2621a43cff9SSean Bruno grp->il_inpsiz = size; 26354af3d0dSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 2641a43cff9SSean Bruno return (grp); 2651a43cff9SSean Bruno } 2661a43cff9SSean Bruno 2671a43cff9SSean Bruno static void 26854af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx) 26954af3d0dSMark Johnston { 27054af3d0dSMark Johnston struct inpcblbgroup *grp; 27154af3d0dSMark Johnston 27254af3d0dSMark Johnston grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 27354af3d0dSMark Johnston free(grp, M_PCB); 27454af3d0dSMark Johnston } 27554af3d0dSMark Johnston 27654af3d0dSMark Johnston static void 2771a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 2781a43cff9SSean Bruno { 2791a43cff9SSean Bruno 28054af3d0dSMark Johnston CK_LIST_REMOVE(grp, il_list); 2812a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx); 2821a43cff9SSean Bruno } 2831a43cff9SSean Bruno 2841a43cff9SSean Bruno static struct inpcblbgroup * 2851a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 2861a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 2871a43cff9SSean Bruno { 2881a43cff9SSean Bruno struct inpcblbgroup *grp; 2891a43cff9SSean Bruno int i; 2901a43cff9SSean Bruno 2911a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, old_grp->il_vflag, 2921a43cff9SSean Bruno old_grp->il_lport, &old_grp->il_dependladdr, size); 29379ee680bSMark Johnston if (grp == NULL) 2941a43cff9SSean Bruno return (NULL); 2951a43cff9SSean Bruno 2961a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 2971a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 2981a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 2991a43cff9SSean Bruno 3001a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 3011a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 3021a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 3031a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 3041a43cff9SSean Bruno return (grp); 3051a43cff9SSean Bruno } 3061a43cff9SSean Bruno 3071a43cff9SSean Bruno /* 3081a43cff9SSean Bruno * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 3091a43cff9SSean Bruno * and shrink group if possible. 3101a43cff9SSean Bruno */ 3111a43cff9SSean Bruno static void 3121a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 3131a43cff9SSean Bruno int i) 3141a43cff9SSean Bruno { 31579ee680bSMark Johnston struct inpcblbgroup *grp, *new_grp; 3161a43cff9SSean Bruno 31779ee680bSMark Johnston grp = *grpp; 3181a43cff9SSean Bruno for (; i + 1 < grp->il_inpcnt; ++i) 3191a43cff9SSean Bruno grp->il_inp[i] = grp->il_inp[i + 1]; 3201a43cff9SSean Bruno grp->il_inpcnt--; 3211a43cff9SSean Bruno 3221a43cff9SSean Bruno if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 32379ee680bSMark Johnston grp->il_inpcnt <= grp->il_inpsiz / 4) { 3241a43cff9SSean Bruno /* Shrink this group. */ 32579ee680bSMark Johnston new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 32679ee680bSMark Johnston if (new_grp != NULL) 3271a43cff9SSean Bruno *grpp = new_grp; 3281a43cff9SSean Bruno } 3291a43cff9SSean Bruno } 3301a43cff9SSean Bruno 3311a43cff9SSean Bruno /* 3321a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 3331a43cff9SSean Bruno */ 3341a43cff9SSean Bruno static int 3351a43cff9SSean Bruno in_pcbinslbgrouphash(struct inpcb *inp) 3361a43cff9SSean Bruno { 337a7026c7fSMark Johnston const static struct timeval interval = { 60, 0 }; 338a7026c7fSMark Johnston static struct timeval lastprint; 3391a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3401a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3411a43cff9SSean Bruno struct inpcblbgroup *grp; 34279ee680bSMark Johnston uint32_t idx; 3431a43cff9SSean Bruno 3441a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 3451a43cff9SSean Bruno 3461a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 3471a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 3481a43cff9SSean Bruno 3491a43cff9SSean Bruno /* 3501a43cff9SSean Bruno * Don't allow jailed socket to join local group. 3511a43cff9SSean Bruno */ 35279ee680bSMark Johnston if (inp->inp_socket != NULL && jailed(inp->inp_socket->so_cred)) 3531a43cff9SSean Bruno return (0); 3541a43cff9SSean Bruno 3551a43cff9SSean Bruno #ifdef INET6 3561a43cff9SSean Bruno /* 3571a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 3581a43cff9SSean Bruno */ 3591a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 3601a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 3611a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 3621a43cff9SSean Bruno return (0); 3631a43cff9SSean Bruno } 3641a43cff9SSean Bruno #endif 3651a43cff9SSean Bruno 3669d2877fcSMark Johnston idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask); 36779ee680bSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 36854af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 3691a43cff9SSean Bruno if (grp->il_vflag == inp->inp_vflag && 3701a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 3711a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 3721a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 37379ee680bSMark Johnston sizeof(grp->il_dependladdr)) == 0) 3741a43cff9SSean Bruno break; 3751a43cff9SSean Bruno } 3761a43cff9SSean Bruno if (grp == NULL) { 3771a43cff9SSean Bruno /* Create new load balance group. */ 3781a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, inp->inp_vflag, 3791a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 3801a43cff9SSean Bruno INPCBLBGROUP_SIZMIN); 38179ee680bSMark Johnston if (grp == NULL) 3821a43cff9SSean Bruno return (ENOBUFS); 3831a43cff9SSean Bruno } else if (grp->il_inpcnt == grp->il_inpsiz) { 3841a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 385a7026c7fSMark Johnston if (ratecheck(&lastprint, &interval)) 3861a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 3871a43cff9SSean Bruno ntohs(grp->il_lport)); 3881a43cff9SSean Bruno return (0); 3891a43cff9SSean Bruno } 3901a43cff9SSean Bruno 3911a43cff9SSean Bruno /* Expand this local group. */ 3921a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 39379ee680bSMark Johnston if (grp == NULL) 3941a43cff9SSean Bruno return (ENOBUFS); 3951a43cff9SSean Bruno } 3961a43cff9SSean Bruno 3971a43cff9SSean Bruno KASSERT(grp->il_inpcnt < grp->il_inpsiz, 39879ee680bSMark Johnston ("invalid local group size %d and count %d", grp->il_inpsiz, 39979ee680bSMark Johnston grp->il_inpcnt)); 4001a43cff9SSean Bruno 4011a43cff9SSean Bruno grp->il_inp[grp->il_inpcnt] = inp; 4021a43cff9SSean Bruno grp->il_inpcnt++; 4031a43cff9SSean Bruno return (0); 4041a43cff9SSean Bruno } 4051a43cff9SSean Bruno 4061a43cff9SSean Bruno /* 4071a43cff9SSean Bruno * Remove PCB from load balance group. 4081a43cff9SSean Bruno */ 4091a43cff9SSean Bruno static void 4101a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 4111a43cff9SSean Bruno { 4121a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 4131a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 4141a43cff9SSean Bruno struct inpcblbgroup *grp; 4151a43cff9SSean Bruno int i; 4161a43cff9SSean Bruno 4171a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 4181a43cff9SSean Bruno 4191a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 4201a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 4211a43cff9SSean Bruno 4221a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 4239d2877fcSMark Johnston INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 42454af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 4251a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 4261a43cff9SSean Bruno if (grp->il_inp[i] != inp) 4271a43cff9SSean Bruno continue; 4281a43cff9SSean Bruno 4291a43cff9SSean Bruno if (grp->il_inpcnt == 1) { 4301a43cff9SSean Bruno /* We are the last, free this local group. */ 4311a43cff9SSean Bruno in_pcblbgroup_free(grp); 4321a43cff9SSean Bruno } else { 4331a43cff9SSean Bruno /* Pull up inpcbs, shrink group if possible. */ 4341a43cff9SSean Bruno in_pcblbgroup_reorder(hdr, &grp, i); 4351a43cff9SSean Bruno } 4361a43cff9SSean Bruno return; 4371a43cff9SSean Bruno } 4381a43cff9SSean Bruno } 4391a43cff9SSean Bruno } 4401a43cff9SSean Bruno 441c3229e05SDavid Greenman /* 442cc487c16SGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 443cc487c16SGleb Smirnoff * different name to the lock. But they all are disposed the same. 444cc487c16SGleb Smirnoff */ 445cc487c16SGleb Smirnoff static void 446cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size) 447cc487c16SGleb Smirnoff { 448cc487c16SGleb Smirnoff struct inpcb *inp = mem; 449cc487c16SGleb Smirnoff 450cc487c16SGleb Smirnoff INP_LOCK_DESTROY(inp); 451cc487c16SGleb Smirnoff } 452cc487c16SGleb Smirnoff 453cc487c16SGleb Smirnoff /* 4549bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 4559bcd427bSRobert Watson * arguments in time. 4569bcd427bSRobert Watson */ 4579bcd427bSRobert Watson void 4589bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 4599bcd427bSRobert Watson struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 460cc487c16SGleb Smirnoff char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields) 4619bcd427bSRobert Watson { 4629bcd427bSRobert Watson 4639d2877fcSMark Johnston porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 4649d2877fcSMark Johnston 4659bcd427bSRobert Watson INP_INFO_LOCK_INIT(pcbinfo, name); 466fa046d87SRobert Watson INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 467ff9b006dSJulien Charbon INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist"); 4689bcd427bSRobert Watson #ifdef VIMAGE 4699bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 4709bcd427bSRobert Watson #endif 4719bcd427bSRobert Watson pcbinfo->ipi_listhead = listhead; 472b872626dSMatt Macy CK_LIST_INIT(pcbinfo->ipi_listhead); 473fa046d87SRobert Watson pcbinfo->ipi_count = 0; 4749bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 4759bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 4769bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 4779bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 4789d2877fcSMark Johnston pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 4791a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 48052cd27cbSRobert Watson #ifdef PCBGROUP 48152cd27cbSRobert Watson in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 48252cd27cbSRobert Watson #endif 4839bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 484cc487c16SGleb Smirnoff NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0); 4859bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 4866acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 4876acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 4889bcd427bSRobert Watson } 4899bcd427bSRobert Watson 4909bcd427bSRobert Watson /* 4919bcd427bSRobert Watson * Destroy an inpcbinfo. 4929bcd427bSRobert Watson */ 4939bcd427bSRobert Watson void 4949bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 4959bcd427bSRobert Watson { 4969bcd427bSRobert Watson 497fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 498fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 499fa046d87SRobert Watson 5009bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 5019bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 5029bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 5031a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 5041a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 50552cd27cbSRobert Watson #ifdef PCBGROUP 50652cd27cbSRobert Watson in_pcbgroup_destroy(pcbinfo); 50752cd27cbSRobert Watson #endif 5089bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 509ff9b006dSJulien Charbon INP_LIST_LOCK_DESTROY(pcbinfo); 510fa046d87SRobert Watson INP_HASH_LOCK_DESTROY(pcbinfo); 5119bcd427bSRobert Watson INP_INFO_LOCK_DESTROY(pcbinfo); 5129bcd427bSRobert Watson } 5139bcd427bSRobert Watson 5149bcd427bSRobert Watson /* 515c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 516d915b280SStephan Uphoff * On success return with the PCB locked. 517c3229e05SDavid Greenman */ 518df8bae1dSRodney W. Grimes int 519d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 520df8bae1dSRodney W. Grimes { 521136d4f1cSRobert Watson struct inpcb *inp; 52213cf67f3SHajimu UMEMOTO int error; 523a557af22SRobert Watson 524a557af22SRobert Watson error = 0; 525d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 526df8bae1dSRodney W. Grimes if (inp == NULL) 527df8bae1dSRodney W. Grimes return (ENOBUFS); 5286a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 52950575ce1SAndrew Gallatin #ifdef NUMA 53050575ce1SAndrew Gallatin inp->inp_numa_domain = M_NODOM; 53150575ce1SAndrew Gallatin #endif 53215bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 533df8bae1dSRodney W. Grimes inp->inp_socket = so; 53486d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 5358b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 536a557af22SRobert Watson #ifdef MAC 53730d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 538a557af22SRobert Watson if (error != 0) 539a557af22SRobert Watson goto out; 54030d239bcSRobert Watson mac_inpcb_create(so, inp); 541a557af22SRobert Watson #endif 542fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 543fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 5440bffde27SRobert Watson if (error != 0) { 5450bffde27SRobert Watson #ifdef MAC 5460bffde27SRobert Watson mac_inpcb_destroy(inp); 5470bffde27SRobert Watson #endif 548a557af22SRobert Watson goto out; 5490bffde27SRobert Watson } 550b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 551e3fd5ffdSRobert Watson #ifdef INET6 552340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 553340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 554603724d3SBjoern A. Zeeb if (V_ip6_v6only) 55533841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 556340c35deSJonathan Lemon } 55775daea93SPaul Saab #endif 558ff9b006dSJulien Charbon INP_WLOCK(inp); 559ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 560b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 5613d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 562df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 56333841545SHajimu UMEMOTO #ifdef INET6 564603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 56533841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 56633841545SHajimu UMEMOTO #endif 567d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 56879bdc6e5SRobert Watson refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 5698c1960d5SMike Karels 5708c1960d5SMike Karels /* 5718c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 5728c1960d5SMike Karels * to be cleaned up. 5738c1960d5SMike Karels */ 5748c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 575ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 5767a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 577a557af22SRobert Watson out: 57886d02c5cSBjoern A. Zeeb if (error != 0) { 57986d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 580a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 58186d02c5cSBjoern A. Zeeb } 582a557af22SRobert Watson #endif 583a557af22SRobert Watson return (error); 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes 58667107f45SBjoern A. Zeeb #ifdef INET 587df8bae1dSRodney W. Grimes int 588136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 589df8bae1dSRodney W. Grimes { 5904b932371SIan Dowse int anonport, error; 5914b932371SIan Dowse 5928501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 593fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 59459daba27SSam Leffler 5954b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 5964b932371SIan Dowse return (EINVAL); 5975cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 5984b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 599b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 6004b932371SIan Dowse if (error) 6014b932371SIan Dowse return (error); 6024b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 6034b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 6044b932371SIan Dowse inp->inp_lport = 0; 6054b932371SIan Dowse return (EAGAIN); 6064b932371SIan Dowse } 6074b932371SIan Dowse if (anonport) 6084b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 6094b932371SIan Dowse return (0); 6104b932371SIan Dowse } 61167107f45SBjoern A. Zeeb #endif 6124b932371SIan Dowse 61339c8c62eSHiren Panchasara /* 61439c8c62eSHiren Panchasara * Select a local port (number) to use. 61539c8c62eSHiren Panchasara */ 616efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 617efc76f72SBjoern A. Zeeb int 6184616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 6194616026fSErmal Luçi struct ucred *cred, int lookupflags) 620efc76f72SBjoern A. Zeeb { 621efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 622efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 623efc76f72SBjoern A. Zeeb unsigned short *lastport; 624efc76f72SBjoern A. Zeeb int count, dorandom, error; 625efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 626efc76f72SBjoern A. Zeeb #ifdef INET 627efc76f72SBjoern A. Zeeb struct in_addr laddr; 628efc76f72SBjoern A. Zeeb #endif 629efc76f72SBjoern A. Zeeb 630efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 631efc76f72SBjoern A. Zeeb 632efc76f72SBjoern A. Zeeb /* 633efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 634efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 635efc76f72SBjoern A. Zeeb */ 636efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 637fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 638efc76f72SBjoern A. Zeeb 639efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 640efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 641efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 642efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 643efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 644cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 645efc76f72SBjoern A. Zeeb if (error) 646efc76f72SBjoern A. Zeeb return (error); 647efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 648efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 649efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 650efc76f72SBjoern A. Zeeb } else { 651efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 652efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 653efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 654efc76f72SBjoern A. Zeeb } 655efc76f72SBjoern A. Zeeb /* 656e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 657efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 658efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 659efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 660efc76f72SBjoern A. Zeeb */ 661efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 662e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 663e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 664efc76f72SBjoern A. Zeeb dorandom = 1; 665efc76f72SBjoern A. Zeeb else 666efc76f72SBjoern A. Zeeb dorandom = 0; 667efc76f72SBjoern A. Zeeb /* 668efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 669efc76f72SBjoern A. Zeeb * we have the only port available. 670efc76f72SBjoern A. Zeeb */ 671efc76f72SBjoern A. Zeeb if (first == last) 672efc76f72SBjoern A. Zeeb dorandom = 0; 673e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 674e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 675efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 676efc76f72SBjoern A. Zeeb /* 677efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 678efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 679efc76f72SBjoern A. Zeeb * code path implementing all logic. 680efc76f72SBjoern A. Zeeb */ 681efc76f72SBjoern A. Zeeb if (first > last) { 682efc76f72SBjoern A. Zeeb aux = first; 683efc76f72SBjoern A. Zeeb first = last; 684efc76f72SBjoern A. Zeeb last = aux; 685efc76f72SBjoern A. Zeeb } 686efc76f72SBjoern A. Zeeb 687efc76f72SBjoern A. Zeeb #ifdef INET 688efc76f72SBjoern A. Zeeb /* Make the compiler happy. */ 689efc76f72SBjoern A. Zeeb laddr.s_addr = 0; 6904d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 691efc76f72SBjoern A. Zeeb KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 692efc76f72SBjoern A. Zeeb __func__, inp)); 693efc76f72SBjoern A. Zeeb laddr = *laddrp; 694efc76f72SBjoern A. Zeeb } 695efc76f72SBjoern A. Zeeb #endif 69667107f45SBjoern A. Zeeb tmpinp = NULL; /* Make compiler happy. */ 697efc76f72SBjoern A. Zeeb lport = *lportp; 698efc76f72SBjoern A. Zeeb 699efc76f72SBjoern A. Zeeb if (dorandom) 700efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 701efc76f72SBjoern A. Zeeb 702efc76f72SBjoern A. Zeeb count = last - first; 703efc76f72SBjoern A. Zeeb 704efc76f72SBjoern A. Zeeb do { 705efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 706efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 707efc76f72SBjoern A. Zeeb ++*lastport; 708efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 709efc76f72SBjoern A. Zeeb *lastport = first; 710efc76f72SBjoern A. Zeeb lport = htons(*lastport); 711efc76f72SBjoern A. Zeeb 712efc76f72SBjoern A. Zeeb #ifdef INET6 7134616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 714efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 71568e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 716efc76f72SBjoern A. Zeeb #endif 717efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 718efc76f72SBjoern A. Zeeb else 719efc76f72SBjoern A. Zeeb #endif 720efc76f72SBjoern A. Zeeb #ifdef INET 721efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 72268e0d7e0SRobert Watson lport, lookupflags, cred); 723efc76f72SBjoern A. Zeeb #endif 724efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 725efc76f72SBjoern A. Zeeb 726efc76f72SBjoern A. Zeeb #ifdef INET 7274d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 728efc76f72SBjoern A. Zeeb laddrp->s_addr = laddr.s_addr; 729efc76f72SBjoern A. Zeeb #endif 730efc76f72SBjoern A. Zeeb *lportp = lport; 731efc76f72SBjoern A. Zeeb 732efc76f72SBjoern A. Zeeb return (0); 733efc76f72SBjoern A. Zeeb } 734efdf104bSMikolaj Golub 735efdf104bSMikolaj Golub /* 736efdf104bSMikolaj Golub * Return cached socket options. 737efdf104bSMikolaj Golub */ 7381a43cff9SSean Bruno int 739efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 740efdf104bSMikolaj Golub { 7411a43cff9SSean Bruno int so_options; 742efdf104bSMikolaj Golub 743efdf104bSMikolaj Golub so_options = 0; 744efdf104bSMikolaj Golub 7451a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 7461a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 747efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 748efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 749efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 750efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 751efdf104bSMikolaj Golub return (so_options); 752efdf104bSMikolaj Golub } 753efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 754efc76f72SBjoern A. Zeeb 7554b932371SIan Dowse /* 7560a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 7570a100a6fSAdrian Chadd * 7580a100a6fSAdrian Chadd * ni points to the new inp. 7590a100a6fSAdrian Chadd * oi points to the exisitng inp. 7600a100a6fSAdrian Chadd * 7610a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 7620a100a6fSAdrian Chadd * whether the credentials match. 7630a100a6fSAdrian Chadd */ 764d5bb8bd3SAdrian Chadd int 7650a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 7660a100a6fSAdrian Chadd { 7670a100a6fSAdrian Chadd /* Check permissions match */ 7680a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 7690a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 7700a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 7710a100a6fSAdrian Chadd return (0); 7720a100a6fSAdrian Chadd 7730a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 7740a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 7750a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 7760a100a6fSAdrian Chadd return (0); 7770a100a6fSAdrian Chadd 7780a100a6fSAdrian Chadd /* 7790a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 7800a100a6fSAdrian Chadd * it is and it matches the checks. 7810a100a6fSAdrian Chadd */ 7820a100a6fSAdrian Chadd return (1); 7830a100a6fSAdrian Chadd } 7840a100a6fSAdrian Chadd 785d5bb8bd3SAdrian Chadd #ifdef INET 7860a100a6fSAdrian Chadd /* 7874b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 7884b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 7894b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 7904b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 7914b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 7924b932371SIan Dowse * 7934b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 7944b932371SIan Dowse */ 7954b932371SIan Dowse int 796136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 797136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 7984b932371SIan Dowse { 7994b932371SIan Dowse struct socket *so = inp->inp_socket; 80015bd2b43SDavid Greenman struct sockaddr_in *sin; 801c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 8024b932371SIan Dowse struct in_addr laddr; 803df8bae1dSRodney W. Grimes u_short lport = 0; 80468e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 805413628a7SBjoern A. Zeeb int error; 806df8bae1dSRodney W. Grimes 8078501a69cSRobert Watson /* 8081a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 8091a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 8101a43cff9SSean Bruno */ 8111a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 8121a43cff9SSean Bruno 8131a43cff9SSean Bruno /* 814fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 8158501a69cSRobert Watson */ 81659daba27SSam Leffler INP_LOCK_ASSERT(inp); 817fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 81859daba27SSam Leffler 819d7c5a620SMatt Macy if (CK_STAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 820df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 8214b932371SIan Dowse laddr.s_addr = *laddrp; 8224b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 823df8bae1dSRodney W. Grimes return (EINVAL); 8241a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 82568e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 8264616026fSErmal Luçi if (nam == NULL) { 8277c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 8287c2f3cb9SJamie Gritton return (error); 8297c2f3cb9SJamie Gritton } else { 83057bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 83157bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 832df8bae1dSRodney W. Grimes return (EINVAL); 833df8bae1dSRodney W. Grimes #ifdef notdef 834df8bae1dSRodney W. Grimes /* 835df8bae1dSRodney W. Grimes * We should check the family, but old programs 836df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 837df8bae1dSRodney W. Grimes */ 838df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 839df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 840df8bae1dSRodney W. Grimes #endif 841b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 842b89e82ddSJamie Gritton if (error) 843b89e82ddSJamie Gritton return (error); 8444b932371SIan Dowse if (sin->sin_port != *lportp) { 8454b932371SIan Dowse /* Don't allow the port to change. */ 8464b932371SIan Dowse if (*lportp != 0) 8474b932371SIan Dowse return (EINVAL); 848df8bae1dSRodney W. Grimes lport = sin->sin_port; 8494b932371SIan Dowse } 8504b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 851df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 852df8bae1dSRodney W. Grimes /* 853df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 854df8bae1dSRodney W. Grimes * allow complete duplication of binding if 855df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 856df8bae1dSRodney W. Grimes * and a multicast address is bound on both 857df8bae1dSRodney W. Grimes * new and duplicated sockets. 858df8bae1dSRodney W. Grimes */ 859f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 860df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 8611a43cff9SSean Bruno /* 8621a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 8631a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 8641a43cff9SSean Bruno */ 8651a43cff9SSean Bruno if ((so->so_options & 8661a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 8671a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 868df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 869df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 87083103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 8714209e01aSAdrian Chadd /* 8724209e01aSAdrian Chadd * Is the address a local IP address? 873f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 8748696873dSAdrian Chadd * to any endpoint address, local or not. 8754209e01aSAdrian Chadd */ 876f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 8778896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 878df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 879df8bae1dSRodney W. Grimes } 8804b932371SIan Dowse laddr = sin->sin_addr; 881df8bae1dSRodney W. Grimes if (lport) { 882df8bae1dSRodney W. Grimes struct inpcb *t; 883ae0e7143SRobert Watson struct tcptw *tw; 884ae0e7143SRobert Watson 885df8bae1dSRodney W. Grimes /* GROSS */ 886603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 887603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 888cc426dd3SMateusz Guzik priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 8892469dd60SGarrett Wollman return (EACCES); 890835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 891cc426dd3SMateusz Guzik priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 892078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 893413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 894340c35deSJonathan Lemon /* 895340c35deSJonathan Lemon * XXX 896340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 897340c35deSJonathan Lemon */ 8984cc20ab1SSeigo Tanimura if (t && 8990a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 900ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 9014658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 9024658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 9034cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 90452b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 9051a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 9061a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 90786d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 90886d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 9094049a042SGuido van Rooij return (EADDRINUSE); 9100a100a6fSAdrian Chadd 9110a100a6fSAdrian Chadd /* 9120a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 9130a100a6fSAdrian Chadd * the credentials need to match and the 9140a100a6fSAdrian Chadd * original socket also has to have been bound 9150a100a6fSAdrian Chadd * with BINDMULTI. 9160a100a6fSAdrian Chadd */ 9170a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9180a100a6fSAdrian Chadd return (EADDRINUSE); 9194049a042SGuido van Rooij } 920c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 92168e0d7e0SRobert Watson lport, lookupflags, cred); 922ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 923ae0e7143SRobert Watson /* 924ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 925ae0e7143SRobert Watson * state recycled, we treat the address as 926ae0e7143SRobert Watson * being in use (for now). This is better 927ae0e7143SRobert Watson * than a panic, but not desirable. 928ae0e7143SRobert Watson */ 929ec95b709SMikolaj Golub tw = intotw(t); 930ae0e7143SRobert Watson if (tw == NULL || 9311a43cff9SSean Bruno ((reuseport & tw->tw_so_options) == 0 && 9321a43cff9SSean Bruno (reuseport_lb & 9331a43cff9SSean Bruno tw->tw_so_options) == 0)) { 934340c35deSJonathan Lemon return (EADDRINUSE); 9351a43cff9SSean Bruno } 9360a100a6fSAdrian Chadd } else if (t && 9370a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 9381a43cff9SSean Bruno (reuseport & inp_so_options(t)) == 0 && 9391a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 940e3fd5ffdSRobert Watson #ifdef INET6 94133841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 942cfa1ca9dSYoshinobu Inoue INADDR_ANY || 943cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 944cfa1ca9dSYoshinobu Inoue INADDR_ANY || 945fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 946fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 947e3fd5ffdSRobert Watson #endif 948df8bae1dSRodney W. Grimes return (EADDRINUSE); 9490a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9500a100a6fSAdrian Chadd return (EADDRINUSE); 951df8bae1dSRodney W. Grimes } 952cfa1ca9dSYoshinobu Inoue } 953df8bae1dSRodney W. Grimes } 9544b932371SIan Dowse if (*lportp != 0) 9554b932371SIan Dowse lport = *lportp; 95633b3ac06SPeter Wemm if (lport == 0) { 9574616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 958efc76f72SBjoern A. Zeeb if (error != 0) 959efc76f72SBjoern A. Zeeb return (error); 96033b3ac06SPeter Wemm 96133b3ac06SPeter Wemm } 9624b932371SIan Dowse *laddrp = laddr.s_addr; 9634b932371SIan Dowse *lportp = lport; 964df8bae1dSRodney W. Grimes return (0); 965df8bae1dSRodney W. Grimes } 966df8bae1dSRodney W. Grimes 967999f1343SGarrett Wollman /* 9685200e00eSIan Dowse * Connect from a socket to a specified address. 9695200e00eSIan Dowse * Both address and port must be specified in argument sin. 9705200e00eSIan Dowse * If don't have a local address for this socket yet, 9715200e00eSIan Dowse * then pick one. 972999f1343SGarrett Wollman */ 973999f1343SGarrett Wollman int 974d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 975fe1274eeSMichael Tuexen struct ucred *cred, struct mbuf *m, bool rehash) 976999f1343SGarrett Wollman { 9775200e00eSIan Dowse u_short lport, fport; 9785200e00eSIan Dowse in_addr_t laddr, faddr; 9795200e00eSIan Dowse int anonport, error; 980df8bae1dSRodney W. Grimes 9818501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 982fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 98327f74fd0SRobert Watson 9845200e00eSIan Dowse lport = inp->inp_lport; 9855200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 9865200e00eSIan Dowse anonport = (lport == 0); 9875200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 988b0330ed9SPawel Jakub Dawidek NULL, cred); 9895200e00eSIan Dowse if (error) 9905200e00eSIan Dowse return (error); 9915200e00eSIan Dowse 9925200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 9935200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 994fe1274eeSMichael Tuexen KASSERT(rehash == true, 995fe1274eeSMichael Tuexen ("Rehashing required for unbound inps")); 9965200e00eSIan Dowse inp->inp_lport = lport; 9975200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 9985200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 9995200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 10005200e00eSIan Dowse inp->inp_lport = 0; 10015200e00eSIan Dowse return (EAGAIN); 10025200e00eSIan Dowse } 10035200e00eSIan Dowse } 10045200e00eSIan Dowse 10055200e00eSIan Dowse /* Commit the remaining changes. */ 10065200e00eSIan Dowse inp->inp_lport = lport; 10075200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10085200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 10095200e00eSIan Dowse inp->inp_fport = fport; 1010fe1274eeSMichael Tuexen if (rehash) { 1011d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 1012fe1274eeSMichael Tuexen } else { 1013fe1274eeSMichael Tuexen in_pcbinshash_mbuf(inp, m); 1014fe1274eeSMichael Tuexen } 10152cb64cb2SGeorge V. Neville-Neil 10165200e00eSIan Dowse if (anonport) 10175200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 10185200e00eSIan Dowse return (0); 10195200e00eSIan Dowse } 10205200e00eSIan Dowse 1021d3c1f003SRobert Watson int 1022d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 1023d3c1f003SRobert Watson { 1024d3c1f003SRobert Watson 1025fe1274eeSMichael Tuexen return (in_pcbconnect_mbuf(inp, nam, cred, NULL, true)); 1026d3c1f003SRobert Watson } 1027d3c1f003SRobert Watson 10285200e00eSIan Dowse /* 10290895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 10300895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 10310895aec3SBjoern A. Zeeb */ 1032ae190832SSteven Hartland int 10330895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 10340895aec3SBjoern A. Zeeb struct ucred *cred) 10350895aec3SBjoern A. Zeeb { 10360895aec3SBjoern A. Zeeb struct ifaddr *ifa; 10370895aec3SBjoern A. Zeeb struct sockaddr *sa; 1038*9ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 1039*9ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 10400895aec3SBjoern A. Zeeb int error; 10410895aec3SBjoern A. Zeeb 1042c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1043413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1044592bcae8SBjoern A. Zeeb /* 1045592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1046592bcae8SBjoern A. Zeeb * if requested. 1047592bcae8SBjoern A. Zeeb */ 1048592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 1049592bcae8SBjoern A. Zeeb return (0); 1050592bcae8SBjoern A. Zeeb 10510895aec3SBjoern A. Zeeb error = 0; 10520895aec3SBjoern A. Zeeb 1053*9ac7c6cfSAlexander V. Chernikov nh = NULL; 1054*9ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 1055*9ac7c6cfSAlexander V. Chernikov sin = &dst; 10560895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 10570895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 10580895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 10590895aec3SBjoern A. Zeeb 10600895aec3SBjoern A. Zeeb /* 10610895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 10620895aec3SBjoern A. Zeeb * else punt. 10630895aec3SBjoern A. Zeeb * 10640895aec3SBjoern A. Zeeb * Find out route to destination. 10650895aec3SBjoern A. Zeeb */ 10660895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 1067*9ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 1068*9ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 10690895aec3SBjoern A. Zeeb 10700895aec3SBjoern A. Zeeb /* 10710895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 10720895aec3SBjoern A. Zeeb * the outgoing interface. 10730895aec3SBjoern A. Zeeb * 10740895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 10750895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 10760895aec3SBjoern A. Zeeb * the source address from. 10770895aec3SBjoern A. Zeeb */ 1078*9ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 10798c0fec80SRobert Watson struct in_ifaddr *ia; 10800895aec3SBjoern A. Zeeb struct ifnet *ifp; 10810895aec3SBjoern A. Zeeb 10824f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 108358a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 10844f6c66ccSMatt Macy if (ia == NULL) { 10854f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 108658a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 10874f6c66ccSMatt Macy 10884f6c66ccSMatt Macy } 10890895aec3SBjoern A. Zeeb if (ia == NULL) { 10900895aec3SBjoern A. Zeeb error = ENETUNREACH; 10910895aec3SBjoern A. Zeeb goto done; 10920895aec3SBjoern A. Zeeb } 10930895aec3SBjoern A. Zeeb 10940304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 10950895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 10960895aec3SBjoern A. Zeeb goto done; 10970895aec3SBjoern A. Zeeb } 10980895aec3SBjoern A. Zeeb 10990895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 11000895aec3SBjoern A. Zeeb ia = NULL; 1101d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11020895aec3SBjoern A. Zeeb 11030895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11040895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11050895aec3SBjoern A. Zeeb continue; 11060895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1107b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11080895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11090895aec3SBjoern A. Zeeb break; 11100895aec3SBjoern A. Zeeb } 11110895aec3SBjoern A. Zeeb } 11120895aec3SBjoern A. Zeeb if (ia != NULL) { 11130895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11140895aec3SBjoern A. Zeeb goto done; 11150895aec3SBjoern A. Zeeb } 11160895aec3SBjoern A. Zeeb 11170895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1118b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11190895aec3SBjoern A. Zeeb goto done; 11200895aec3SBjoern A. Zeeb } 11210895aec3SBjoern A. Zeeb 11220895aec3SBjoern A. Zeeb /* 11230895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 11240895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 11250895aec3SBjoern A. Zeeb * In case of jails do those three steps: 11260895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 11270895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 11280895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 11290895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 11300895aec3SBjoern A. Zeeb */ 1131*9ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 11328c0fec80SRobert Watson struct in_ifaddr *ia; 11339317b04eSRobert Watson struct ifnet *ifp; 11340895aec3SBjoern A. Zeeb 11350895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 11360304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 1137*9ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11380895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11390895aec3SBjoern A. Zeeb goto done; 11400895aec3SBjoern A. Zeeb } 11410895aec3SBjoern A. Zeeb 11420895aec3SBjoern A. Zeeb /* Jailed. */ 11430895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 1144*9ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1145b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 1146*9ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11470895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11480895aec3SBjoern A. Zeeb goto done; 11490895aec3SBjoern A. Zeeb } 11500895aec3SBjoern A. Zeeb 11510895aec3SBjoern A. Zeeb /* 11520895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 11530895aec3SBjoern A. Zeeb * belonging to this jail. 11540895aec3SBjoern A. Zeeb */ 11558c0fec80SRobert Watson ia = NULL; 1156*9ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1157d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11580895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11590895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11600895aec3SBjoern A. Zeeb continue; 11610895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1162b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11630895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11640895aec3SBjoern A. Zeeb break; 11650895aec3SBjoern A. Zeeb } 11660895aec3SBjoern A. Zeeb } 11670895aec3SBjoern A. Zeeb if (ia != NULL) { 11680895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11690895aec3SBjoern A. Zeeb goto done; 11700895aec3SBjoern A. Zeeb } 11710895aec3SBjoern A. Zeeb 11720895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1173b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11740895aec3SBjoern A. Zeeb goto done; 11750895aec3SBjoern A. Zeeb } 11760895aec3SBjoern A. Zeeb 11770895aec3SBjoern A. Zeeb /* 11780895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 11790895aec3SBjoern A. Zeeb * to ourselves is here. 11800895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 11810895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 11820895aec3SBjoern A. Zeeb * a loopback interface. 11830895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 11840895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 11850895aec3SBjoern A. Zeeb */ 1186*9ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 11878c0fec80SRobert Watson struct in_ifaddr *ia; 11880895aec3SBjoern A. Zeeb 1189*9ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 119058a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11910895aec3SBjoern A. Zeeb if (ia == NULL) 1192*9ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 119358a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1194f0bb05fcSQing Li if (ia == NULL) 1195*9ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 11960895aec3SBjoern A. Zeeb 11970304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11980895aec3SBjoern A. Zeeb if (ia == NULL) { 11990895aec3SBjoern A. Zeeb error = ENETUNREACH; 12000895aec3SBjoern A. Zeeb goto done; 12010895aec3SBjoern A. Zeeb } 12020895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12030895aec3SBjoern A. Zeeb goto done; 12040895aec3SBjoern A. Zeeb } 12050895aec3SBjoern A. Zeeb 12060895aec3SBjoern A. Zeeb /* Jailed. */ 12070895aec3SBjoern A. Zeeb if (ia != NULL) { 12080895aec3SBjoern A. Zeeb struct ifnet *ifp; 12090895aec3SBjoern A. Zeeb 12100895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12110895aec3SBjoern A. Zeeb ia = NULL; 1212d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12130895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12140895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12150895aec3SBjoern A. Zeeb continue; 12160895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1217b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1218b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 12190895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12200895aec3SBjoern A. Zeeb break; 12210895aec3SBjoern A. Zeeb } 12220895aec3SBjoern A. Zeeb } 12230895aec3SBjoern A. Zeeb if (ia != NULL) { 12240895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12250895aec3SBjoern A. Zeeb goto done; 12260895aec3SBjoern A. Zeeb } 12270895aec3SBjoern A. Zeeb } 12280895aec3SBjoern A. Zeeb 12290895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1230b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12310895aec3SBjoern A. Zeeb goto done; 12320895aec3SBjoern A. Zeeb } 12330895aec3SBjoern A. Zeeb 12340895aec3SBjoern A. Zeeb done: 12350895aec3SBjoern A. Zeeb return (error); 12360895aec3SBjoern A. Zeeb } 12370895aec3SBjoern A. Zeeb 12380895aec3SBjoern A. Zeeb /* 12395200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 12405200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 12415200e00eSIan Dowse * address and port for the PCB; these are updated to the values 12425200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 12435200e00eSIan Dowse * the connect. 12445200e00eSIan Dowse * 12455200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 12465200e00eSIan Dowse * and port. These are not updated in the error case. 12475200e00eSIan Dowse * 12485200e00eSIan Dowse * If the operation fails because the connection already exists, 12495200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 12505200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 12515200e00eSIan Dowse * is set to NULL. 12525200e00eSIan Dowse */ 12535200e00eSIan Dowse int 1254136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1255136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1256136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 12575200e00eSIan Dowse { 1258cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 12595200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 12605200e00eSIan Dowse struct in_ifaddr *ia; 12615200e00eSIan Dowse struct inpcb *oinp; 1262b89e82ddSJamie Gritton struct in_addr laddr, faddr; 12635200e00eSIan Dowse u_short lport, fport; 12645200e00eSIan Dowse int error; 12655200e00eSIan Dowse 12668501a69cSRobert Watson /* 12678501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 12688501a69cSRobert Watson * lock is sufficient. 12698501a69cSRobert Watson */ 1270c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 127127f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1272fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 127327f74fd0SRobert Watson 12745200e00eSIan Dowse if (oinpp != NULL) 12755200e00eSIan Dowse *oinpp = NULL; 127657bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1277df8bae1dSRodney W. Grimes return (EINVAL); 1278df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1279df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1280df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1281df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 12825200e00eSIan Dowse laddr.s_addr = *laddrp; 12835200e00eSIan Dowse lport = *lportp; 12845200e00eSIan Dowse faddr = sin->sin_addr; 12855200e00eSIan Dowse fport = sin->sin_port; 12860895aec3SBjoern A. Zeeb 1287d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1288df8bae1dSRodney W. Grimes /* 1289df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1290df8bae1dSRodney W. Grimes * use the primary local address. 1291df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1292df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1293df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1294df8bae1dSRodney W. Grimes */ 1295413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1296cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1297413628a7SBjoern A. Zeeb faddr = 1298d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1299cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1300b89e82ddSJamie Gritton if (cred != NULL && 1301b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1302b89e82ddSJamie Gritton return (error); 13032d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1304cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1305d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 13062d9cfabaSRobert Watson IFF_BROADCAST) 1307d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1308603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1309cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 13102d9cfabaSRobert Watson } 1311df8bae1dSRodney W. Grimes } 13125200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1313d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1314df8bae1dSRodney W. Grimes /* 1315df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1316d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1317df8bae1dSRodney W. Grimes * address of that interface as our source address. 1318df8bae1dSRodney W. Grimes */ 13195200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1320df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1321df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1322df8bae1dSRodney W. Grimes struct ifnet *ifp; 1323df8bae1dSRodney W. Grimes 1324df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1325df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1326df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1327cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1328d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1329e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1330e691be70SDaniel Eischen (cred == NULL || 1331e691be70SDaniel Eischen prison_check_ip4(cred, 1332e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1333df8bae1dSRodney W. Grimes break; 1334e691be70SDaniel Eischen } 1335e691be70SDaniel Eischen if (ia == NULL) 1336d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1337e691be70SDaniel Eischen else { 13385200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1339d79fdd98SDaniel Eischen error = 0; 1340999f1343SGarrett Wollman } 1341cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1342d79fdd98SDaniel Eischen } 1343d79fdd98SDaniel Eischen } 134404215ed2SRandall Stewart if (error) 134504215ed2SRandall Stewart return (error); 13460895aec3SBjoern A. Zeeb } 1347fa046d87SRobert Watson oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1348fa046d87SRobert Watson laddr, lport, 0, NULL); 13495200e00eSIan Dowse if (oinp != NULL) { 13505200e00eSIan Dowse if (oinpp != NULL) 13515200e00eSIan Dowse *oinpp = oinp; 1352df8bae1dSRodney W. Grimes return (EADDRINUSE); 1353c3229e05SDavid Greenman } 13545200e00eSIan Dowse if (lport == 0) { 1355b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1356b0330ed9SPawel Jakub Dawidek cred); 13575a903f8dSPierre Beyssac if (error) 13585a903f8dSPierre Beyssac return (error); 13595a903f8dSPierre Beyssac } 13605200e00eSIan Dowse *laddrp = laddr.s_addr; 13615200e00eSIan Dowse *lportp = lport; 13625200e00eSIan Dowse *faddrp = faddr.s_addr; 13635200e00eSIan Dowse *fportp = fport; 1364df8bae1dSRodney W. Grimes return (0); 1365df8bae1dSRodney W. Grimes } 1366df8bae1dSRodney W. Grimes 136726f9a767SRodney W. Grimes void 1368136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1369df8bae1dSRodney W. Grimes { 13706b348152SRobert Watson 13718501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1372fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1373df8bae1dSRodney W. Grimes 1374df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1375df8bae1dSRodney W. Grimes inp->inp_fport = 0; 137615bd2b43SDavid Greenman in_pcbrehash(inp); 1377df8bae1dSRodney W. Grimes } 137883e521ecSBjoern A. Zeeb #endif /* INET */ 1379df8bae1dSRodney W. Grimes 13804c7c478dSRobert Watson /* 138128696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1382c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 138328696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 138428696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 13854c7c478dSRobert Watson */ 138626f9a767SRodney W. Grimes void 1387136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1388df8bae1dSRodney W. Grimes { 13894c7c478dSRobert Watson 1390a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1391c0a211c5SRobert Watson 1392f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1393f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1394f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1395f3e7afe2SHans Petter Selasky #endif 13964c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 13974c7c478dSRobert Watson inp->inp_socket = NULL; 13984c7c478dSRobert Watson } 13994c7c478dSRobert Watson 1400c0a211c5SRobert Watson /* 140179bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 140279bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 140379bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 140452cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 140552cd27cbSRobert Watson * via a pcbgroup. 140679bdc6e5SRobert Watson * 140779bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 140879bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 140979bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 141079bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 141179bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 141279bdc6e5SRobert Watson * inpcb. 141379bdc6e5SRobert Watson * 141479bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 141579bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 141679bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 141779bdc6e5SRobert Watson * using in_pcbrele(). 1418c0a211c5SRobert Watson */ 141979bdc6e5SRobert Watson void 142079bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 14214c7c478dSRobert Watson { 1422df8bae1dSRodney W. Grimes 142379bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 142479bdc6e5SRobert Watson 142579bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 142679bdc6e5SRobert Watson } 142779bdc6e5SRobert Watson 142879bdc6e5SRobert Watson /* 142979bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 143079bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 143179bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 143279bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 143379bdc6e5SRobert Watson * 143479bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 143579bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 143679bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 143779bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 143879bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 143979bdc6e5SRobert Watson */ 144079bdc6e5SRobert Watson int 144179bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 144279bdc6e5SRobert Watson { 144379bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 144479bdc6e5SRobert Watson 144579bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 144679bdc6e5SRobert Watson 144779bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 144879bdc6e5SRobert Watson 1449df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1450df4e91d3SGleb Smirnoff /* 1451df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1452df4e91d3SGleb Smirnoff * this isn't the last reference. 1453df4e91d3SGleb Smirnoff */ 1454df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1455df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1456df4e91d3SGleb Smirnoff return (1); 1457df4e91d3SGleb Smirnoff } 145879bdc6e5SRobert Watson return (0); 1459df4e91d3SGleb Smirnoff } 146079bdc6e5SRobert Watson 146179bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 14623ee9c3c4SRandall Stewart #ifdef TCPHPTS 14633ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 14643ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 14653ee9c3c4SRandall Stewart /* 14663ee9c3c4SRandall Stewart * We should not be on the hpts at 14673ee9c3c4SRandall Stewart * this point in any form. we must 14683ee9c3c4SRandall Stewart * get the lock to be sure. 14693ee9c3c4SRandall Stewart */ 14703ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 14713ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 14723ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 14733ee9c3c4SRandall Stewart hpts, inp); 14743ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 14753ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 14763ee9c3c4SRandall Stewart if (inp->inp_in_input) 14773ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 14783ee9c3c4SRandall Stewart hpts, inp); 14793ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 14803ee9c3c4SRandall Stewart } 14813ee9c3c4SRandall Stewart #endif 148279bdc6e5SRobert Watson INP_RUNLOCK(inp); 148379bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 148479bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 148579bdc6e5SRobert Watson return (1); 148679bdc6e5SRobert Watson } 148779bdc6e5SRobert Watson 148879bdc6e5SRobert Watson int 148979bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 149079bdc6e5SRobert Watson { 149179bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 149279bdc6e5SRobert Watson 149379bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 149479bdc6e5SRobert Watson 149579bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 149679bdc6e5SRobert Watson 1497edd0e0b0SFabien Thomas if (refcount_release(&inp->inp_refcount) == 0) { 1498edd0e0b0SFabien Thomas /* 1499edd0e0b0SFabien Thomas * If the inpcb has been freed, let the caller know, even if 1500edd0e0b0SFabien Thomas * this isn't the last reference. 1501edd0e0b0SFabien Thomas */ 1502edd0e0b0SFabien Thomas if (inp->inp_flags2 & INP_FREED) { 1503edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1504edd0e0b0SFabien Thomas return (1); 1505edd0e0b0SFabien Thomas } 150679bdc6e5SRobert Watson return (0); 1507edd0e0b0SFabien Thomas } 150879bdc6e5SRobert Watson 150979bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 15103ee9c3c4SRandall Stewart #ifdef TCPHPTS 15113ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 15123ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 15133ee9c3c4SRandall Stewart /* 15143ee9c3c4SRandall Stewart * We should not be on the hpts at 15153ee9c3c4SRandall Stewart * this point in any form. we must 15163ee9c3c4SRandall Stewart * get the lock to be sure. 15173ee9c3c4SRandall Stewart */ 15183ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 15193ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 15203ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 15213ee9c3c4SRandall Stewart hpts, inp); 15223ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15233ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 15243ee9c3c4SRandall Stewart if (inp->inp_in_input) 15253ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 15263ee9c3c4SRandall Stewart hpts, inp); 15273ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15283ee9c3c4SRandall Stewart } 15293ee9c3c4SRandall Stewart #endif 153079bdc6e5SRobert Watson INP_WUNLOCK(inp); 153179bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 153279bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 153379bdc6e5SRobert Watson return (1); 153479bdc6e5SRobert Watson } 153579bdc6e5SRobert Watson 153679bdc6e5SRobert Watson /* 153779bdc6e5SRobert Watson * Temporary wrapper. 153879bdc6e5SRobert Watson */ 153979bdc6e5SRobert Watson int 154079bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 154179bdc6e5SRobert Watson { 154279bdc6e5SRobert Watson 154379bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 154479bdc6e5SRobert Watson } 154579bdc6e5SRobert Watson 1546ddece765SMatt Macy void 1547ddece765SMatt Macy in_pcblist_rele_rlocked(epoch_context_t ctx) 1548ddece765SMatt Macy { 1549ddece765SMatt Macy struct in_pcblist *il; 1550ddece765SMatt Macy struct inpcb *inp; 1551ddece765SMatt Macy struct inpcbinfo *pcbinfo; 1552ddece765SMatt Macy int i, n; 1553ddece765SMatt Macy 1554ddece765SMatt Macy il = __containerof(ctx, struct in_pcblist, il_epoch_ctx); 1555ddece765SMatt Macy pcbinfo = il->il_pcbinfo; 1556ddece765SMatt Macy n = il->il_count; 1557ddece765SMatt Macy INP_INFO_WLOCK(pcbinfo); 1558ddece765SMatt Macy for (i = 0; i < n; i++) { 1559ddece765SMatt Macy inp = il->il_inp_list[i]; 1560ddece765SMatt Macy INP_RLOCK(inp); 1561ddece765SMatt Macy if (!in_pcbrele_rlocked(inp)) 1562ddece765SMatt Macy INP_RUNLOCK(inp); 1563ddece765SMatt Macy } 1564ddece765SMatt Macy INP_INFO_WUNLOCK(pcbinfo); 1565ddece765SMatt Macy free(il, M_TEMP); 1566ddece765SMatt Macy } 1567ddece765SMatt Macy 1568addf2b20SMatt Macy static void 1569f09ee4fcSMatt Macy inpcbport_free(epoch_context_t ctx) 1570f09ee4fcSMatt Macy { 1571f09ee4fcSMatt Macy struct inpcbport *phd; 1572f09ee4fcSMatt Macy 1573f09ee4fcSMatt Macy phd = __containerof(ctx, struct inpcbport, phd_epoch_ctx); 1574f09ee4fcSMatt Macy free(phd, M_PCB); 1575f09ee4fcSMatt Macy } 1576f09ee4fcSMatt Macy 1577f09ee4fcSMatt Macy static void 1578addf2b20SMatt Macy in_pcbfree_deferred(epoch_context_t ctx) 1579addf2b20SMatt Macy { 1580addf2b20SMatt Macy struct inpcb *inp; 1581addf2b20SMatt Macy int released __unused; 1582addf2b20SMatt Macy 1583addf2b20SMatt Macy inp = __containerof(ctx, struct inpcb, inp_epoch_ctx); 1584addf2b20SMatt Macy 1585addf2b20SMatt Macy INP_WLOCK(inp); 1586c7ee62fcSAndrey V. Elsukov CURVNET_SET(inp->inp_vnet); 1587addf2b20SMatt Macy #ifdef INET 1588f9be0386SMatt Macy struct ip_moptions *imo = inp->inp_moptions; 1589addf2b20SMatt Macy inp->inp_moptions = NULL; 1590addf2b20SMatt Macy #endif 1591addf2b20SMatt Macy /* XXXRW: Do as much as possible here. */ 1592addf2b20SMatt Macy #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1593addf2b20SMatt Macy if (inp->inp_sp != NULL) 1594addf2b20SMatt Macy ipsec_delete_pcbpolicy(inp); 1595addf2b20SMatt Macy #endif 1596addf2b20SMatt Macy #ifdef INET6 1597f9be0386SMatt Macy struct ip6_moptions *im6o = NULL; 1598addf2b20SMatt Macy if (inp->inp_vflag & INP_IPV6PROTO) { 1599addf2b20SMatt Macy ip6_freepcbopts(inp->in6p_outputopts); 1600f9be0386SMatt Macy im6o = inp->in6p_moptions; 1601addf2b20SMatt Macy inp->in6p_moptions = NULL; 1602addf2b20SMatt Macy } 1603addf2b20SMatt Macy #endif 1604addf2b20SMatt Macy if (inp->inp_options) 1605addf2b20SMatt Macy (void)m_free(inp->inp_options); 1606addf2b20SMatt Macy inp->inp_vflag = 0; 1607addf2b20SMatt Macy crfree(inp->inp_cred); 1608addf2b20SMatt Macy #ifdef MAC 1609addf2b20SMatt Macy mac_inpcb_destroy(inp); 1610addf2b20SMatt Macy #endif 1611addf2b20SMatt Macy released = in_pcbrele_wlocked(inp); 1612addf2b20SMatt Macy MPASS(released); 1613f9be0386SMatt Macy #ifdef INET6 1614f9be0386SMatt Macy ip6_freemoptions(im6o); 1615f9be0386SMatt Macy #endif 1616f9be0386SMatt Macy #ifdef INET 1617f9be0386SMatt Macy inp_freemoptions(imo); 1618f9be0386SMatt Macy #endif 1619c7ee62fcSAndrey V. Elsukov CURVNET_RESTORE(); 1620addf2b20SMatt Macy } 1621addf2b20SMatt Macy 162279bdc6e5SRobert Watson /* 162379bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 162479bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 162579bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 162679bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 162779bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 162879bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 162979bdc6e5SRobert Watson * the pcbinfo lock is held. 163079bdc6e5SRobert Watson */ 163179bdc6e5SRobert Watson void 163279bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 163379bdc6e5SRobert Watson { 1634f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1635f42a83f2SMatt Macy 163679bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 163745a48d07SJonathan T. Looney KASSERT((inp->inp_flags2 & INP_FREED) == 0, 163845a48d07SJonathan T. Looney ("%s: called twice for pcb %p", __func__, inp)); 163945a48d07SJonathan T. Looney if (inp->inp_flags2 & INP_FREED) { 164045a48d07SJonathan T. Looney INP_WUNLOCK(inp); 164145a48d07SJonathan T. Looney return; 164245a48d07SJonathan T. Looney } 164345a48d07SJonathan T. Looney 164479bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1645f42a83f2SMatt Macy INP_LIST_WLOCK(pcbinfo); 1646f42a83f2SMatt Macy in_pcbremlists(inp); 1647f42a83f2SMatt Macy INP_LIST_WUNLOCK(pcbinfo); 1648fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1649addf2b20SMatt Macy /* mark as destruction in progress */ 1650f42a83f2SMatt Macy inp->inp_flags2 |= INP_FREED; 1651cb6bb230SMatt Macy INP_WUNLOCK(inp); 16522a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcbfree_deferred, &inp->inp_epoch_ctx); 165328696211SRobert Watson } 165428696211SRobert Watson 165528696211SRobert Watson /* 1656c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1657c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1658c0a211c5SRobert Watson * 1659c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1660c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1661c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1662c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1663c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1664c0a211c5SRobert Watson * in_pcbdetach(). 1665c0a211c5SRobert Watson * 1666c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1667c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 166810702a28SRobert Watson */ 166910702a28SRobert Watson void 167010702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 167110702a28SRobert Watson { 167210702a28SRobert Watson 16738501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 16746573d758SMatt Macy #ifdef INVARIANTS 16756573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 16766573d758SMatt Macy MPASS(inp->inp_refcount > 1); 16776573d758SMatt Macy #endif 167810702a28SRobert Watson 1679fa046d87SRobert Watson /* 1680fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1681fa046d87SRobert Watson * the hash lock...? 1682fa046d87SRobert Watson */ 1683ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1684111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 168510702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 168610702a28SRobert Watson 1687fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 16881a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 1689b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 1690b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 1691b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 1692b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 16932a4bd982SGleb Smirnoff NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx); 169410702a28SRobert Watson } 1695fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1696111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 169752cd27cbSRobert Watson #ifdef PCBGROUP 169852cd27cbSRobert Watson in_pcbgroup_remove(inp); 169952cd27cbSRobert Watson #endif 170010702a28SRobert Watson } 170110702a28SRobert Watson } 170210702a28SRobert Watson 170367107f45SBjoern A. Zeeb #ifdef INET 170454d642bbSRobert Watson /* 170554d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 170654d642bbSRobert Watson */ 170726ef6ac4SDon Lewis struct sockaddr * 1708136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 170926ef6ac4SDon Lewis { 171026ef6ac4SDon Lewis struct sockaddr_in *sin; 171126ef6ac4SDon Lewis 17121ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1713a163d034SWarner Losh M_WAITOK | M_ZERO); 171426ef6ac4SDon Lewis sin->sin_family = AF_INET; 171526ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 171626ef6ac4SDon Lewis sin->sin_addr = *addr_p; 171726ef6ac4SDon Lewis sin->sin_port = port; 171826ef6ac4SDon Lewis 171926ef6ac4SDon Lewis return (struct sockaddr *)sin; 172026ef6ac4SDon Lewis } 172126ef6ac4SDon Lewis 1722117bcae7SGarrett Wollman int 172354d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1724df8bae1dSRodney W. Grimes { 1725136d4f1cSRobert Watson struct inpcb *inp; 172626ef6ac4SDon Lewis struct in_addr addr; 172726ef6ac4SDon Lewis in_port_t port; 172842fa505bSDavid Greenman 1729fdc984f7STor Egge inp = sotoinpcb(so); 173054d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 17316466b28aSRobert Watson 1732a69042a5SRobert Watson INP_RLOCK(inp); 173326ef6ac4SDon Lewis port = inp->inp_lport; 173426ef6ac4SDon Lewis addr = inp->inp_laddr; 1735a69042a5SRobert Watson INP_RUNLOCK(inp); 173642fa505bSDavid Greenman 173726ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1738117bcae7SGarrett Wollman return 0; 1739df8bae1dSRodney W. Grimes } 1740df8bae1dSRodney W. Grimes 1741117bcae7SGarrett Wollman int 174254d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1743df8bae1dSRodney W. Grimes { 1744136d4f1cSRobert Watson struct inpcb *inp; 174526ef6ac4SDon Lewis struct in_addr addr; 174626ef6ac4SDon Lewis in_port_t port; 174742fa505bSDavid Greenman 1748fdc984f7STor Egge inp = sotoinpcb(so); 174954d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 17506466b28aSRobert Watson 1751a69042a5SRobert Watson INP_RLOCK(inp); 175226ef6ac4SDon Lewis port = inp->inp_fport; 175326ef6ac4SDon Lewis addr = inp->inp_faddr; 1754a69042a5SRobert Watson INP_RUNLOCK(inp); 175542fa505bSDavid Greenman 175626ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1757117bcae7SGarrett Wollman return 0; 1758df8bae1dSRodney W. Grimes } 1759df8bae1dSRodney W. Grimes 176026f9a767SRodney W. Grimes void 1761136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1762136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1763d1c54148SJesper Skriver { 1764f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1765d1c54148SJesper Skriver 17663dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1767b872626dSMatt Macy CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 17688501a69cSRobert Watson INP_WLOCK(inp); 1769d1c54148SJesper Skriver #ifdef INET6 1770f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 17718501a69cSRobert Watson INP_WUNLOCK(inp); 1772d1c54148SJesper Skriver continue; 1773f76fcf6dSJeffrey Hsu } 1774d1c54148SJesper Skriver #endif 1775d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1776f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 17778501a69cSRobert Watson INP_WUNLOCK(inp); 1778d1c54148SJesper Skriver continue; 1779d1c54148SJesper Skriver } 17803dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 17818501a69cSRobert Watson INP_WUNLOCK(inp); 1782f76fcf6dSJeffrey Hsu } 17833dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1784d1c54148SJesper Skriver } 1785d1c54148SJesper Skriver 1786e43cc4aeSHajimu UMEMOTO void 1787136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1788e43cc4aeSHajimu UMEMOTO { 1789e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 179059854ecfSHans Petter Selasky struct in_multi *inm; 179159854ecfSHans Petter Selasky struct in_mfilter *imf; 1792e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1793e43cc4aeSHajimu UMEMOTO 1794ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1795b872626dSMatt Macy CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 17968501a69cSRobert Watson INP_WLOCK(inp); 1797e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1798e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1799e43cc4aeSHajimu UMEMOTO imo != NULL) { 1800e43cc4aeSHajimu UMEMOTO /* 1801e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1802e43cc4aeSHajimu UMEMOTO * detached. 1803e43cc4aeSHajimu UMEMOTO */ 1804e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1805e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1806e43cc4aeSHajimu UMEMOTO 1807e43cc4aeSHajimu UMEMOTO /* 1808e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1809e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1810cb6bb230SMatt Macy * 1811cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1812e43cc4aeSHajimu UMEMOTO */ 181359854ecfSHans Petter Selasky restart: 181459854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 181559854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 181659854ecfSHans Petter Selasky continue; 181759854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 181859854ecfSHans Petter Selasky continue; 181959854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 1820f3e1324bSStephen Hurd IN_MULTI_LOCK_ASSERT(); 182159854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 182259854ecfSHans Petter Selasky ip_mfilter_free(imf); 182359854ecfSHans Petter Selasky goto restart; 1824e43cc4aeSHajimu UMEMOTO } 1825e43cc4aeSHajimu UMEMOTO } 18268501a69cSRobert Watson INP_WUNLOCK(inp); 1827e43cc4aeSHajimu UMEMOTO } 1828ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1829e43cc4aeSHajimu UMEMOTO } 1830e43cc4aeSHajimu UMEMOTO 1831df8bae1dSRodney W. Grimes /* 1832fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1833fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1834c3229e05SDavid Greenman */ 1835d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1836df8bae1dSRodney W. Grimes struct inpcb * 1837136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 183868e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1839df8bae1dSRodney W. Grimes { 1840136d4f1cSRobert Watson struct inpcb *inp; 1841d5e8a67eSHajimu UMEMOTO #ifdef INET6 1842d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1843d5e8a67eSHajimu UMEMOTO #else 1844d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1845d5e8a67eSHajimu UMEMOTO #endif 1846d5e8a67eSHajimu UMEMOTO int wildcard; 18477bc4aca7SDavid Greenman 184868e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 184968e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 185068e0d7e0SRobert Watson 1851fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 18521b73ca0bSSam Leffler 185368e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1854c3229e05SDavid Greenman struct inpcbhead *head; 1855c3229e05SDavid Greenman /* 1856c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1857c3229e05SDavid Greenman * matches the local address and port we're looking for. 1858c3229e05SDavid Greenman */ 1859712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1860712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1861b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 1862cfa1ca9dSYoshinobu Inoue #ifdef INET6 1863413628a7SBjoern A. Zeeb /* XXX inp locking */ 1864369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1865cfa1ca9dSYoshinobu Inoue continue; 1866cfa1ca9dSYoshinobu Inoue #endif 1867c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1868c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1869c3229e05SDavid Greenman inp->inp_lport == lport) { 1870c3229e05SDavid Greenman /* 1871413628a7SBjoern A. Zeeb * Found? 1872c3229e05SDavid Greenman */ 1873413628a7SBjoern A. Zeeb if (cred == NULL || 18740304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 18750304c731SJamie Gritton inp->inp_cred->cr_prison)) 1876c3229e05SDavid Greenman return (inp); 1877df8bae1dSRodney W. Grimes } 1878c3229e05SDavid Greenman } 1879c3229e05SDavid Greenman /* 1880c3229e05SDavid Greenman * Not found. 1881c3229e05SDavid Greenman */ 1882c3229e05SDavid Greenman return (NULL); 1883c3229e05SDavid Greenman } else { 1884c3229e05SDavid Greenman struct inpcbporthead *porthash; 1885c3229e05SDavid Greenman struct inpcbport *phd; 1886c3229e05SDavid Greenman struct inpcb *match = NULL; 1887c3229e05SDavid Greenman /* 1888c3229e05SDavid Greenman * Best fit PCB lookup. 1889c3229e05SDavid Greenman * 1890c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1891c3229e05SDavid Greenman * port hash list. 1892c3229e05SDavid Greenman */ 1893712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1894712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1895b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 1896c3229e05SDavid Greenman if (phd->phd_port == lport) 1897c3229e05SDavid Greenman break; 1898c3229e05SDavid Greenman } 1899c3229e05SDavid Greenman if (phd != NULL) { 1900c3229e05SDavid Greenman /* 1901c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1902c3229e05SDavid Greenman * fit. 1903c3229e05SDavid Greenman */ 1904b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1905c3229e05SDavid Greenman wildcard = 0; 1906413628a7SBjoern A. Zeeb if (cred != NULL && 19070304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 19080304c731SJamie Gritton cred->cr_prison)) 1909413628a7SBjoern A. Zeeb continue; 1910cfa1ca9dSYoshinobu Inoue #ifdef INET6 1911413628a7SBjoern A. Zeeb /* XXX inp locking */ 1912369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1913cfa1ca9dSYoshinobu Inoue continue; 1914d5e8a67eSHajimu UMEMOTO /* 1915d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1916d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1917d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1918d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1919d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1920d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1921d5e8a67eSHajimu UMEMOTO * 1922d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1923d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1924d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1925d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1926d5e8a67eSHajimu UMEMOTO */ 1927d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1928d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1929cfa1ca9dSYoshinobu Inoue #endif 1930c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1931c3229e05SDavid Greenman wildcard++; 193215bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 193315bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 193415bd2b43SDavid Greenman wildcard++; 193515bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 193615bd2b43SDavid Greenman continue; 193715bd2b43SDavid Greenman } else { 193815bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 193915bd2b43SDavid Greenman wildcard++; 194015bd2b43SDavid Greenman } 1941df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1942df8bae1dSRodney W. Grimes match = inp; 1943df8bae1dSRodney W. Grimes matchwild = wildcard; 1944413628a7SBjoern A. Zeeb if (matchwild == 0) 1945df8bae1dSRodney W. Grimes break; 1946df8bae1dSRodney W. Grimes } 1947df8bae1dSRodney W. Grimes } 19483dbdc25cSDavid Greenman } 1949df8bae1dSRodney W. Grimes return (match); 1950df8bae1dSRodney W. Grimes } 1951c3229e05SDavid Greenman } 1952d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 195315bd2b43SDavid Greenman 19541a43cff9SSean Bruno static struct inpcb * 19551a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 19561a43cff9SSean Bruno const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr, 19571a43cff9SSean Bruno uint16_t fport, int lookupflags) 19581a43cff9SSean Bruno { 19598be02ee4SMark Johnston struct inpcb *local_wild; 19601a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 19611a43cff9SSean Bruno struct inpcblbgroup *grp; 19628be02ee4SMark Johnston uint32_t idx; 19631a43cff9SSean Bruno 19641a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 19651a43cff9SSean Bruno 19669d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 19679d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 19681a43cff9SSean Bruno 19691a43cff9SSean Bruno /* 19701a43cff9SSean Bruno * Order of socket selection: 19711a43cff9SSean Bruno * 1. non-wild. 19721a43cff9SSean Bruno * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD). 19731a43cff9SSean Bruno * 19741a43cff9SSean Bruno * NOTE: 19751a43cff9SSean Bruno * - Load balanced group does not contain jailed sockets 19761a43cff9SSean Bruno * - Load balanced group does not contain IPv4 mapped INET6 wild sockets 19771a43cff9SSean Bruno */ 19788be02ee4SMark Johnston local_wild = NULL; 197954af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 19801a43cff9SSean Bruno #ifdef INET6 19811a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 19821a43cff9SSean Bruno continue; 19831a43cff9SSean Bruno #endif 19848be02ee4SMark Johnston if (grp->il_lport != lport) 19858be02ee4SMark Johnston continue; 19861a43cff9SSean Bruno 19878be02ee4SMark Johnston idx = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, lport, fport) % 19888be02ee4SMark Johnston grp->il_inpcnt; 19898be02ee4SMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) 19901a43cff9SSean Bruno return (grp->il_inp[idx]); 19911a43cff9SSean Bruno if (grp->il_laddr.s_addr == INADDR_ANY && 19928be02ee4SMark Johnston (lookupflags & INPLOOKUP_WILDCARD) != 0) 19931a43cff9SSean Bruno local_wild = grp->il_inp[idx]; 19941a43cff9SSean Bruno } 19951a43cff9SSean Bruno return (local_wild); 19961a43cff9SSean Bruno } 19971a43cff9SSean Bruno 199852cd27cbSRobert Watson #ifdef PCBGROUP 199952cd27cbSRobert Watson /* 200052cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 200152cd27cbSRobert Watson */ 200252cd27cbSRobert Watson static struct inpcb * 200352cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 200452cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 200552cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 200652cd27cbSRobert Watson { 200752cd27cbSRobert Watson struct inpcbhead *head; 200852cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 200952cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 20107fb2986fSJonathan T. Looney bool locked; 201152cd27cbSRobert Watson 201252cd27cbSRobert Watson /* 201352cd27cbSRobert Watson * First look for an exact match. 201452cd27cbSRobert Watson */ 201552cd27cbSRobert Watson tmpinp = NULL; 201652cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 201752cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 201852cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 2019feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 202052cd27cbSRobert Watson #ifdef INET6 202152cd27cbSRobert Watson /* XXX inp locking */ 202252cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 202352cd27cbSRobert Watson continue; 202452cd27cbSRobert Watson #endif 202552cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 202652cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 202752cd27cbSRobert Watson inp->inp_fport == fport && 202852cd27cbSRobert Watson inp->inp_lport == lport) { 202952cd27cbSRobert Watson /* 203052cd27cbSRobert Watson * XXX We should be able to directly return 203152cd27cbSRobert Watson * the inp here, without any checks. 203252cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 203352cd27cbSRobert Watson */ 203452cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 203552cd27cbSRobert Watson goto found; 203652cd27cbSRobert Watson if (tmpinp == NULL) 203752cd27cbSRobert Watson tmpinp = inp; 203852cd27cbSRobert Watson } 203952cd27cbSRobert Watson } 204052cd27cbSRobert Watson if (tmpinp != NULL) { 204152cd27cbSRobert Watson inp = tmpinp; 204252cd27cbSRobert Watson goto found; 204352cd27cbSRobert Watson } 204452cd27cbSRobert Watson 20450a100a6fSAdrian Chadd #ifdef RSS 20460a100a6fSAdrian Chadd /* 20470a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 20480a100a6fSAdrian Chadd * match for an RSS-local socket. 20490a100a6fSAdrian Chadd */ 20500a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 20510a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 20520a100a6fSAdrian Chadd #ifdef INET6 20530a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 20540a100a6fSAdrian Chadd #endif 20550a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 20560a100a6fSAdrian Chadd struct inpcbhead *head; 20570a100a6fSAdrian Chadd int injail; 20580a100a6fSAdrian Chadd 20590a100a6fSAdrian Chadd /* 20600a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 20610a100a6fSAdrian Chadd * 1. jailed, non-wild. 20620a100a6fSAdrian Chadd * 2. jailed, wild. 20630a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 20640a100a6fSAdrian Chadd * 4. non-jailed, wild. 20650a100a6fSAdrian Chadd */ 20660a100a6fSAdrian Chadd 20670a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 20680a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 2069feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 20700a100a6fSAdrian Chadd #ifdef INET6 20710a100a6fSAdrian Chadd /* XXX inp locking */ 20720a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 20730a100a6fSAdrian Chadd continue; 20740a100a6fSAdrian Chadd #endif 20750a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 20760a100a6fSAdrian Chadd inp->inp_lport != lport) 20770a100a6fSAdrian Chadd continue; 20780a100a6fSAdrian Chadd 20790a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 20800a100a6fSAdrian Chadd if (injail) { 20810a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 20820a100a6fSAdrian Chadd &laddr) != 0) 20830a100a6fSAdrian Chadd continue; 20840a100a6fSAdrian Chadd } else { 20850a100a6fSAdrian Chadd if (local_exact != NULL) 20860a100a6fSAdrian Chadd continue; 20870a100a6fSAdrian Chadd } 20880a100a6fSAdrian Chadd 20890a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 20900a100a6fSAdrian Chadd if (injail) 20910a100a6fSAdrian Chadd goto found; 20920a100a6fSAdrian Chadd else 20930a100a6fSAdrian Chadd local_exact = inp; 20940a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 20950a100a6fSAdrian Chadd #ifdef INET6 20960a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 20970a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 20980a100a6fSAdrian Chadd local_wild_mapped = inp; 20990a100a6fSAdrian Chadd else 21000a100a6fSAdrian Chadd #endif 21010a100a6fSAdrian Chadd if (injail) 21020a100a6fSAdrian Chadd jail_wild = inp; 21030a100a6fSAdrian Chadd else 21040a100a6fSAdrian Chadd local_wild = inp; 21050a100a6fSAdrian Chadd } 21060a100a6fSAdrian Chadd } /* LIST_FOREACH */ 21070a100a6fSAdrian Chadd 21080a100a6fSAdrian Chadd inp = jail_wild; 21090a100a6fSAdrian Chadd if (inp == NULL) 21100a100a6fSAdrian Chadd inp = local_exact; 21110a100a6fSAdrian Chadd if (inp == NULL) 21120a100a6fSAdrian Chadd inp = local_wild; 21130a100a6fSAdrian Chadd #ifdef INET6 21140a100a6fSAdrian Chadd if (inp == NULL) 21150a100a6fSAdrian Chadd inp = local_wild_mapped; 21160a100a6fSAdrian Chadd #endif 21170a100a6fSAdrian Chadd if (inp != NULL) 21180a100a6fSAdrian Chadd goto found; 21190a100a6fSAdrian Chadd } 21200a100a6fSAdrian Chadd #endif 21210a100a6fSAdrian Chadd 212252cd27cbSRobert Watson /* 212352cd27cbSRobert Watson * Then look for a wildcard match, if requested. 212452cd27cbSRobert Watson */ 212552cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 212652cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 212752cd27cbSRobert Watson #ifdef INET6 212852cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 212952cd27cbSRobert Watson #endif 213052cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 213152cd27cbSRobert Watson struct inpcbhead *head; 213252cd27cbSRobert Watson int injail; 213352cd27cbSRobert Watson 213452cd27cbSRobert Watson /* 213552cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 213652cd27cbSRobert Watson * 1. jailed, non-wild. 213752cd27cbSRobert Watson * 2. jailed, wild. 213852cd27cbSRobert Watson * 3. non-jailed, non-wild. 213952cd27cbSRobert Watson * 4. non-jailed, wild. 214052cd27cbSRobert Watson */ 214152cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 214252cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 2143feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 214452cd27cbSRobert Watson #ifdef INET6 214552cd27cbSRobert Watson /* XXX inp locking */ 214652cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 214752cd27cbSRobert Watson continue; 214852cd27cbSRobert Watson #endif 214952cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 215052cd27cbSRobert Watson inp->inp_lport != lport) 215152cd27cbSRobert Watson continue; 215252cd27cbSRobert Watson 215352cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 215452cd27cbSRobert Watson if (injail) { 215552cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 215652cd27cbSRobert Watson &laddr) != 0) 215752cd27cbSRobert Watson continue; 215852cd27cbSRobert Watson } else { 215952cd27cbSRobert Watson if (local_exact != NULL) 216052cd27cbSRobert Watson continue; 216152cd27cbSRobert Watson } 216252cd27cbSRobert Watson 216352cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 216452cd27cbSRobert Watson if (injail) 216552cd27cbSRobert Watson goto found; 216652cd27cbSRobert Watson else 216752cd27cbSRobert Watson local_exact = inp; 216852cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 216952cd27cbSRobert Watson #ifdef INET6 217052cd27cbSRobert Watson /* XXX inp locking, NULL check */ 217152cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 217252cd27cbSRobert Watson local_wild_mapped = inp; 217352cd27cbSRobert Watson else 217483e521ecSBjoern A. Zeeb #endif 217552cd27cbSRobert Watson if (injail) 217652cd27cbSRobert Watson jail_wild = inp; 217752cd27cbSRobert Watson else 217852cd27cbSRobert Watson local_wild = inp; 217952cd27cbSRobert Watson } 218052cd27cbSRobert Watson } /* LIST_FOREACH */ 218152cd27cbSRobert Watson inp = jail_wild; 218252cd27cbSRobert Watson if (inp == NULL) 218352cd27cbSRobert Watson inp = local_exact; 218452cd27cbSRobert Watson if (inp == NULL) 218552cd27cbSRobert Watson inp = local_wild; 218652cd27cbSRobert Watson #ifdef INET6 218752cd27cbSRobert Watson if (inp == NULL) 218852cd27cbSRobert Watson inp = local_wild_mapped; 218983e521ecSBjoern A. Zeeb #endif 219052cd27cbSRobert Watson if (inp != NULL) 219152cd27cbSRobert Watson goto found; 219252cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 219352cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 219452cd27cbSRobert Watson return (NULL); 219552cd27cbSRobert Watson 219652cd27cbSRobert Watson found: 21977fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 2198b9a9e8e9SNavdeep Parhar locked = INP_TRY_WLOCK(inp); 21997fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 2200b9a9e8e9SNavdeep Parhar locked = INP_TRY_RLOCK(inp); 22017fb2986fSJonathan T. Looney else 22027fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 2203483305b9SMatt Macy if (__predict_false(locked && (inp->inp_flags2 & INP_FREED))) { 2204483305b9SMatt Macy if (lookupflags & INPLOOKUP_WLOCKPCB) 2205483305b9SMatt Macy INP_WUNLOCK(inp); 2206483305b9SMatt Macy else 2207483305b9SMatt Macy INP_RUNLOCK(inp); 2208483305b9SMatt Macy return (NULL); 2209483305b9SMatt Macy } else if (!locked) 221052cd27cbSRobert Watson in_pcbref(inp); 221152cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 22127fb2986fSJonathan T. Looney if (!locked) { 221352cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 221452cd27cbSRobert Watson INP_WLOCK(inp); 221552cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 221652cd27cbSRobert Watson return (NULL); 22177fb2986fSJonathan T. Looney } else { 221852cd27cbSRobert Watson INP_RLOCK(inp); 221952cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 222052cd27cbSRobert Watson return (NULL); 22217fb2986fSJonathan T. Looney } 22227fb2986fSJonathan T. Looney } 22237fb2986fSJonathan T. Looney #ifdef INVARIANTS 22247fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 22257fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 22267fb2986fSJonathan T. Looney else 22277fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 22287fb2986fSJonathan T. Looney #endif 222952cd27cbSRobert Watson return (inp); 223052cd27cbSRobert Watson } 223152cd27cbSRobert Watson #endif /* PCBGROUP */ 223252cd27cbSRobert Watson 223315bd2b43SDavid Greenman /* 2234fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2235fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 2236fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 223715bd2b43SDavid Greenman */ 2238fa046d87SRobert Watson static struct inpcb * 2239fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 224068e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2241136d4f1cSRobert Watson struct ifnet *ifp) 224215bd2b43SDavid Greenman { 224315bd2b43SDavid Greenman struct inpcbhead *head; 2244413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 224515bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 224615bd2b43SDavid Greenman 224768e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 224868e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2249d797164aSGleb Smirnoff INP_HASH_LOCK_ASSERT(pcbinfo); 2250d797164aSGleb Smirnoff 225115bd2b43SDavid Greenman /* 225215bd2b43SDavid Greenman * First look for an exact match. 225315bd2b43SDavid Greenman */ 2254413628a7SBjoern A. Zeeb tmpinp = NULL; 2255712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 2256712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 2257b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2258cfa1ca9dSYoshinobu Inoue #ifdef INET6 2259413628a7SBjoern A. Zeeb /* XXX inp locking */ 2260369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2261cfa1ca9dSYoshinobu Inoue continue; 2262cfa1ca9dSYoshinobu Inoue #endif 22636d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2264ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2265ca98b82cSDavid Greenman inp->inp_fport == fport && 2266413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 2267413628a7SBjoern A. Zeeb /* 2268413628a7SBjoern A. Zeeb * XXX We should be able to directly return 2269413628a7SBjoern A. Zeeb * the inp here, without any checks. 2270413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 2271413628a7SBjoern A. Zeeb */ 22720304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 2273c3229e05SDavid Greenman return (inp); 2274413628a7SBjoern A. Zeeb if (tmpinp == NULL) 2275413628a7SBjoern A. Zeeb tmpinp = inp; 2276c3229e05SDavid Greenman } 2277413628a7SBjoern A. Zeeb } 2278413628a7SBjoern A. Zeeb if (tmpinp != NULL) 2279413628a7SBjoern A. Zeeb return (tmpinp); 2280e3fd5ffdSRobert Watson 2281e3fd5ffdSRobert Watson /* 22821a43cff9SSean Bruno * Then look in lb group (for wildcard match). 22831a43cff9SSean Bruno */ 2284d9ff5789SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 22851a43cff9SSean Bruno inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr, 22861a43cff9SSean Bruno fport, lookupflags); 2287d9ff5789SMark Johnston if (inp != NULL) 22881a43cff9SSean Bruno return (inp); 22891a43cff9SSean Bruno } 22901a43cff9SSean Bruno 22911a43cff9SSean Bruno /* 2292e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 2293e3fd5ffdSRobert Watson */ 229468e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2295413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 2296e3fd5ffdSRobert Watson #ifdef INET6 2297cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 2298e3fd5ffdSRobert Watson #endif 2299413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 2300413628a7SBjoern A. Zeeb int injail; 2301413628a7SBjoern A. Zeeb 2302413628a7SBjoern A. Zeeb /* 2303413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2304413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2305413628a7SBjoern A. Zeeb * 2. jailed, wild. 2306413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2307413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2308413628a7SBjoern A. Zeeb */ 23096d6a026bSDavid Greenman 2310712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2311712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2312b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2313cfa1ca9dSYoshinobu Inoue #ifdef INET6 2314413628a7SBjoern A. Zeeb /* XXX inp locking */ 2315369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2316cfa1ca9dSYoshinobu Inoue continue; 2317cfa1ca9dSYoshinobu Inoue #endif 2318413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2319413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2320413628a7SBjoern A. Zeeb continue; 2321413628a7SBjoern A. Zeeb 23220304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2323413628a7SBjoern A. Zeeb if (injail) { 2324b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2325b89e82ddSJamie Gritton &laddr) != 0) 2326413628a7SBjoern A. Zeeb continue; 2327413628a7SBjoern A. Zeeb } else { 2328413628a7SBjoern A. Zeeb if (local_exact != NULL) 2329413628a7SBjoern A. Zeeb continue; 2330413628a7SBjoern A. Zeeb } 2331413628a7SBjoern A. Zeeb 2332413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2333413628a7SBjoern A. Zeeb if (injail) 2334c3229e05SDavid Greenman return (inp); 2335413628a7SBjoern A. Zeeb else 2336413628a7SBjoern A. Zeeb local_exact = inp; 2337413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2338e3fd5ffdSRobert Watson #ifdef INET6 2339413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23405cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2341cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2342cfa1ca9dSYoshinobu Inoue else 234383e521ecSBjoern A. Zeeb #endif 2344413628a7SBjoern A. Zeeb if (injail) 2345413628a7SBjoern A. Zeeb jail_wild = inp; 2346413628a7SBjoern A. Zeeb else 23476d6a026bSDavid Greenman local_wild = inp; 23486d6a026bSDavid Greenman } 2349413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2350413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2351413628a7SBjoern A. Zeeb return (jail_wild); 2352413628a7SBjoern A. Zeeb if (local_exact != NULL) 2353413628a7SBjoern A. Zeeb return (local_exact); 2354413628a7SBjoern A. Zeeb if (local_wild != NULL) 2355c3229e05SDavid Greenman return (local_wild); 2356413628a7SBjoern A. Zeeb #ifdef INET6 2357413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2358413628a7SBjoern A. Zeeb return (local_wild_mapped); 235983e521ecSBjoern A. Zeeb #endif 236068e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2361413628a7SBjoern A. Zeeb 23626d6a026bSDavid Greenman return (NULL); 236315bd2b43SDavid Greenman } 2364fa046d87SRobert Watson 2365fa046d87SRobert Watson /* 2366fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2367fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2368fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2369fa046d87SRobert Watson */ 2370fa046d87SRobert Watson static struct inpcb * 2371fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2372fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2373fa046d87SRobert Watson struct ifnet *ifp) 2374fa046d87SRobert Watson { 2375fa046d87SRobert Watson struct inpcb *inp; 2376fa046d87SRobert Watson 2377fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2378fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 2379fa046d87SRobert Watson if (inp != NULL) { 2380fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 2381fa046d87SRobert Watson INP_WLOCK(inp); 23823d348772SMatt Macy if (__predict_false(inp->inp_flags2 & INP_FREED)) { 23833d348772SMatt Macy INP_WUNLOCK(inp); 23843d348772SMatt Macy inp = NULL; 23853d348772SMatt Macy } 23863d348772SMatt Macy } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 2387fa046d87SRobert Watson INP_RLOCK(inp); 23883d348772SMatt Macy if (__predict_false(inp->inp_flags2 & INP_FREED)) { 23893d348772SMatt Macy INP_RUNLOCK(inp); 23903d348772SMatt Macy inp = NULL; 23917fb2986fSJonathan T. Looney } 23923d348772SMatt Macy } else 23933d348772SMatt Macy panic("%s: locking bug", __func__); 23947fb2986fSJonathan T. Looney #ifdef INVARIANTS 23953d348772SMatt Macy if (inp != NULL) { 23967fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 23977fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 23987fb2986fSJonathan T. Looney else 23997fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 24003d348772SMatt Macy } 24017fb2986fSJonathan T. Looney #endif 24023d348772SMatt Macy } 2403d797164aSGleb Smirnoff 2404fa046d87SRobert Watson return (inp); 2405fa046d87SRobert Watson } 2406fa046d87SRobert Watson 2407fa046d87SRobert Watson /* 2408d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2409d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 241052cd27cbSRobert Watson * 241152cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 2412fa046d87SRobert Watson */ 2413fa046d87SRobert Watson struct inpcb * 2414fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2415fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2416fa046d87SRobert Watson { 24177527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 241852cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 241952cd27cbSRobert Watson #endif 2420fa046d87SRobert Watson 2421fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2422fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2423fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2424fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 2425fa046d87SRobert Watson 24267527624eSRobert Watson /* 24277527624eSRobert Watson * When not using RSS, use connection groups in preference to the 24287527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 24297527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 24307527624eSRobert Watson * in software. 24317527624eSRobert Watson * 24327527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 24337527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 24347527624eSRobert Watson * in software. 24357527624eSRobert Watson */ 24367527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 243752cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 243852cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 243952cd27cbSRobert Watson fport); 244052cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 244152cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 244252cd27cbSRobert Watson } 244352cd27cbSRobert Watson #endif 2444fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2445fa046d87SRobert Watson lookupflags, ifp)); 2446fa046d87SRobert Watson } 2447d3c1f003SRobert Watson 2448d3c1f003SRobert Watson struct inpcb * 2449d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2450d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2451d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2452d3c1f003SRobert Watson { 245352cd27cbSRobert Watson #ifdef PCBGROUP 245452cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 245552cd27cbSRobert Watson #endif 2456d3c1f003SRobert Watson 2457d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2458d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2459d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2460d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2461d3c1f003SRobert Watson 246252cd27cbSRobert Watson #ifdef PCBGROUP 24637527624eSRobert Watson /* 24647527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 24657527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 24667527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 24677527624eSRobert Watson * using RSS. 24687527624eSRobert Watson * 24697527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 24707527624eSRobert Watson */ 24717527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 24727527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 247352cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 247452cd27cbSRobert Watson m->m_pkthdr.flowid); 247552cd27cbSRobert Watson if (pcbgroup != NULL) 247652cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 247752cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 24787527624eSRobert Watson #ifndef RSS 247952cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 248052cd27cbSRobert Watson fport); 248152cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 248252cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 24837527624eSRobert Watson #endif 248452cd27cbSRobert Watson } 248552cd27cbSRobert Watson #endif 2486d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2487d3c1f003SRobert Watson lookupflags, ifp)); 2488d3c1f003SRobert Watson } 248967107f45SBjoern A. Zeeb #endif /* INET */ 249015bd2b43SDavid Greenman 24917bc4aca7SDavid Greenman /* 2492c3229e05SDavid Greenman * Insert PCB onto various hash lists. 24937bc4aca7SDavid Greenman */ 249452cd27cbSRobert Watson static int 2495fe1274eeSMichael Tuexen in_pcbinshash_internal(struct inpcb *inp, struct mbuf *m) 249615bd2b43SDavid Greenman { 2497c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2498c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2499c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2500c3229e05SDavid Greenman struct inpcbport *phd; 2501cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 25021a43cff9SSean Bruno int so_options; 250315bd2b43SDavid Greenman 25048501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2505fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2506fa046d87SRobert Watson 2507111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2508111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2509602cc7f1SRobert Watson 2510cfa1ca9dSYoshinobu Inoue #ifdef INET6 2511cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 25121b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2513cfa1ca9dSYoshinobu Inoue else 251483e521ecSBjoern A. Zeeb #endif 2515cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2516cfa1ca9dSYoshinobu Inoue 2517712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2518712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 251915bd2b43SDavid Greenman 2520712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2521712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2522c3229e05SDavid Greenman 2523c3229e05SDavid Greenman /* 25241a43cff9SSean Bruno * Add entry to load balance group. 25251a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 25261a43cff9SSean Bruno */ 25271a43cff9SSean Bruno so_options = inp_so_options(inp); 25281a43cff9SSean Bruno if (so_options & SO_REUSEPORT_LB) { 25291a43cff9SSean Bruno int ret = in_pcbinslbgrouphash(inp); 25301a43cff9SSean Bruno if (ret) { 25311a43cff9SSean Bruno /* pcb lb group malloc fail (ret=ENOBUFS). */ 25321a43cff9SSean Bruno return (ret); 25331a43cff9SSean Bruno } 25341a43cff9SSean Bruno } 25351a43cff9SSean Bruno 25361a43cff9SSean Bruno /* 2537c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2538c3229e05SDavid Greenman */ 2539b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2540c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2541c3229e05SDavid Greenman break; 2542c3229e05SDavid Greenman } 2543c3229e05SDavid Greenman /* 2544c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2545c3229e05SDavid Greenman */ 2546c3229e05SDavid Greenman if (phd == NULL) { 25471ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2548c3229e05SDavid Greenman if (phd == NULL) { 2549c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2550c3229e05SDavid Greenman } 2551f09ee4fcSMatt Macy bzero(&phd->phd_epoch_ctx, sizeof(struct epoch_context)); 2552c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2553b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2554b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2555c3229e05SDavid Greenman } 2556c3229e05SDavid Greenman inp->inp_phd = phd; 2557b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2558b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2559111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 256052cd27cbSRobert Watson #ifdef PCBGROUP 2561fe1274eeSMichael Tuexen if (m != NULL) { 2562fe1274eeSMichael Tuexen in_pcbgroup_update_mbuf(inp, m); 2563fe1274eeSMichael Tuexen } else { 256452cd27cbSRobert Watson in_pcbgroup_update(inp); 2565fe1274eeSMichael Tuexen } 256652cd27cbSRobert Watson #endif 2567c3229e05SDavid Greenman return (0); 256815bd2b43SDavid Greenman } 256915bd2b43SDavid Greenman 257052cd27cbSRobert Watson int 257152cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 257252cd27cbSRobert Watson { 257352cd27cbSRobert Watson 2574fe1274eeSMichael Tuexen return (in_pcbinshash_internal(inp, NULL)); 257552cd27cbSRobert Watson } 257652cd27cbSRobert Watson 257752cd27cbSRobert Watson int 2578fe1274eeSMichael Tuexen in_pcbinshash_mbuf(struct inpcb *inp, struct mbuf *m) 257952cd27cbSRobert Watson { 258052cd27cbSRobert Watson 2581fe1274eeSMichael Tuexen return (in_pcbinshash_internal(inp, m)); 258252cd27cbSRobert Watson } 258352cd27cbSRobert Watson 258452cd27cbSRobert Watson /* 2585c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2586c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2587c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2588c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2589c3229e05SDavid Greenman */ 259015bd2b43SDavid Greenman void 2591d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 259215bd2b43SDavid Greenman { 259359daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 259415bd2b43SDavid Greenman struct inpcbhead *head; 2595cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 259615bd2b43SDavid Greenman 25978501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2598fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2599fa046d87SRobert Watson 2600111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2601111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2602602cc7f1SRobert Watson 2603cfa1ca9dSYoshinobu Inoue #ifdef INET6 2604cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 26051b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2606cfa1ca9dSYoshinobu Inoue else 260783e521ecSBjoern A. Zeeb #endif 2608cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2609cfa1ca9dSYoshinobu Inoue 2610712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2611712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 261215bd2b43SDavid Greenman 2613b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2614b872626dSMatt Macy CK_LIST_INSERT_HEAD(head, inp, inp_hash); 261552cd27cbSRobert Watson 261652cd27cbSRobert Watson #ifdef PCBGROUP 261752cd27cbSRobert Watson if (m != NULL) 261852cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 261952cd27cbSRobert Watson else 262052cd27cbSRobert Watson in_pcbgroup_update(inp); 262152cd27cbSRobert Watson #endif 2622c3229e05SDavid Greenman } 2623c3229e05SDavid Greenman 2624d3c1f003SRobert Watson void 2625d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2626d3c1f003SRobert Watson { 2627d3c1f003SRobert Watson 2628d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2629d3c1f003SRobert Watson } 2630d3c1f003SRobert Watson 2631c3229e05SDavid Greenman /* 2632c3229e05SDavid Greenman * Remove PCB from various lists. 2633c3229e05SDavid Greenman */ 26346d888973SRobert Watson static void 2635136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2636c3229e05SDavid Greenman { 263759daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 263859daba27SSam Leffler 26398501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2640ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 264159daba27SSam Leffler 264259daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2643111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2644c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2645c3229e05SDavid Greenman 2646fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 26471a43cff9SSean Bruno 26481a43cff9SSean Bruno /* XXX: Only do if SO_REUSEPORT_LB set? */ 26491a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 26501a43cff9SSean Bruno 2651b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2652b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 2653b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 2654b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 26552a4bd982SGleb Smirnoff NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx); 2656c3229e05SDavid Greenman } 2657fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2658111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2659c3229e05SDavid Greenman } 2660b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_list); 266159daba27SSam Leffler pcbinfo->ipi_count--; 266252cd27cbSRobert Watson #ifdef PCBGROUP 266352cd27cbSRobert Watson in_pcbgroup_remove(inp); 266452cd27cbSRobert Watson #endif 266515bd2b43SDavid Greenman } 266675c13541SPoul-Henning Kamp 2667a557af22SRobert Watson /* 266884cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 266984cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 267084cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 267184cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 267284cc0778SGeorge V. Neville-Neil */ 267384cc0778SGeorge V. Neville-Neil void 267484cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 267584cc0778SGeorge V. Neville-Neil { 267684cc0778SGeorge V. Neville-Neil 2677fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 267884cc0778SGeorge V. Neville-Neil return; 267984cc0778SGeorge V. Neville-Neil } 268084cc0778SGeorge V. Neville-Neil 268184cc0778SGeorge V. Neville-Neil /* 2682a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2683a557af22SRobert Watson * label change into the in_pcb for the socket. 2684a557af22SRobert Watson */ 2685a557af22SRobert Watson void 2686136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2687a557af22SRobert Watson { 2688a557af22SRobert Watson #ifdef MAC 2689a557af22SRobert Watson struct inpcb *inp; 2690a557af22SRobert Watson 26914c7c478dSRobert Watson inp = sotoinpcb(so); 26924c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2693602cc7f1SRobert Watson 26948501a69cSRobert Watson INP_WLOCK(inp); 2695310e7cebSRobert Watson SOCK_LOCK(so); 2696a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2697310e7cebSRobert Watson SOCK_UNLOCK(so); 26988501a69cSRobert Watson INP_WUNLOCK(inp); 2699a557af22SRobert Watson #endif 2700a557af22SRobert Watson } 27015f311da2SMike Silbersack 27025f311da2SMike Silbersack /* 2703ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2704ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2705ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2706ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2707ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 27085f311da2SMike Silbersack */ 2709aae49dd3SBjoern A. Zeeb static void 2710136d4f1cSRobert Watson ipport_tick(void *xtp) 27115f311da2SMike Silbersack { 27128b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2713ad3a630fSRobert Watson 27145ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 27158b615593SMarko Zec VNET_FOREACH(vnet_iter) { 27168b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 27178b615593SMarko Zec if (V_ipport_tcpallocs <= 27188b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2719603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2720603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2721ad3a630fSRobert Watson } else 2722603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2723603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 27248b615593SMarko Zec CURVNET_RESTORE(); 27258b615593SMarko Zec } 27265ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 27275f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 27285f311da2SMike Silbersack } 2729497057eeSRobert Watson 2730aae49dd3SBjoern A. Zeeb static void 2731aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2732aae49dd3SBjoern A. Zeeb { 2733aae49dd3SBjoern A. Zeeb 2734aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2735aae49dd3SBjoern A. Zeeb } 2736aae49dd3SBjoern A. Zeeb 2737aae49dd3SBjoern A. Zeeb /* 2738aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2739aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2740aae49dd3SBjoern A. Zeeb */ 2741aae49dd3SBjoern A. Zeeb static void 2742aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2743aae49dd3SBjoern A. Zeeb { 2744aae49dd3SBjoern A. Zeeb 2745aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2746fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2747aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2748aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2749aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2750aae49dd3SBjoern A. Zeeb } 2751aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2752aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2753aae49dd3SBjoern A. Zeeb 27543d585327SKip Macy void 27553d585327SKip Macy inp_wlock(struct inpcb *inp) 27563d585327SKip Macy { 27573d585327SKip Macy 27588501a69cSRobert Watson INP_WLOCK(inp); 27593d585327SKip Macy } 27603d585327SKip Macy 27613d585327SKip Macy void 27623d585327SKip Macy inp_wunlock(struct inpcb *inp) 27633d585327SKip Macy { 27643d585327SKip Macy 27658501a69cSRobert Watson INP_WUNLOCK(inp); 27663d585327SKip Macy } 27673d585327SKip Macy 27683d585327SKip Macy void 27693d585327SKip Macy inp_rlock(struct inpcb *inp) 27703d585327SKip Macy { 27713d585327SKip Macy 2772a69042a5SRobert Watson INP_RLOCK(inp); 27733d585327SKip Macy } 27743d585327SKip Macy 27753d585327SKip Macy void 27763d585327SKip Macy inp_runlock(struct inpcb *inp) 27773d585327SKip Macy { 27783d585327SKip Macy 2779a69042a5SRobert Watson INP_RUNLOCK(inp); 27803d585327SKip Macy } 27813d585327SKip Macy 2782c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 27833d585327SKip Macy void 2784e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 27853d585327SKip Macy { 27863d585327SKip Macy 27878501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 27883d585327SKip Macy } 27893d585327SKip Macy 27903d585327SKip Macy void 2791e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 27923d585327SKip Macy { 27933d585327SKip Macy 27943d585327SKip Macy INP_UNLOCK_ASSERT(inp); 27953d585327SKip Macy } 27963d585327SKip Macy #endif 27973d585327SKip Macy 27989378e437SKip Macy void 27999378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 28009378e437SKip Macy { 28019378e437SKip Macy struct inpcb *inp; 28029378e437SKip Macy 2803ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 2804b872626dSMatt Macy CK_LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 28059378e437SKip Macy INP_WLOCK(inp); 28069378e437SKip Macy func(inp, arg); 28079378e437SKip Macy INP_WUNLOCK(inp); 28089378e437SKip Macy } 2809ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 28109378e437SKip Macy } 28119378e437SKip Macy 28129378e437SKip Macy struct socket * 28139378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 28149378e437SKip Macy { 28159378e437SKip Macy 28169378e437SKip Macy INP_WLOCK_ASSERT(inp); 28179378e437SKip Macy return (inp->inp_socket); 28189378e437SKip Macy } 28199378e437SKip Macy 28209378e437SKip Macy struct tcpcb * 28219378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 28229378e437SKip Macy { 28239378e437SKip Macy 28249378e437SKip Macy INP_WLOCK_ASSERT(inp); 28259378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 28269378e437SKip Macy } 28279378e437SKip Macy 28289378e437SKip Macy int 28299378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 28309378e437SKip Macy { 28319378e437SKip Macy 28329378e437SKip Macy return (inp->inp_ip_tos); 28339378e437SKip Macy } 28349378e437SKip Macy 28359378e437SKip Macy void 28369378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 28379378e437SKip Macy { 28389378e437SKip Macy 28399378e437SKip Macy inp->inp_ip_tos = val; 28409378e437SKip Macy } 28419378e437SKip Macy 28429378e437SKip Macy void 2843df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 28449d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 28459378e437SKip Macy { 28469378e437SKip Macy 28479d29c635SKip Macy INP_LOCK_ASSERT(inp); 2848df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2849df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 28509378e437SKip Macy *lp = inp->inp_lport; 28519378e437SKip Macy *fp = inp->inp_fport; 28529378e437SKip Macy } 28539378e437SKip Macy 2854dd0e6c38SKip Macy struct inpcb * 2855dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2856dd0e6c38SKip Macy { 2857dd0e6c38SKip Macy 2858dd0e6c38SKip Macy return (sotoinpcb(so)); 2859dd0e6c38SKip Macy } 2860dd0e6c38SKip Macy 2861dd0e6c38SKip Macy struct tcpcb * 2862dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2863dd0e6c38SKip Macy { 2864dd0e6c38SKip Macy 2865dd0e6c38SKip Macy return (sototcpcb(so)); 2866dd0e6c38SKip Macy } 2867dd0e6c38SKip Macy 2868cc65eb4eSGleb Smirnoff /* 2869cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2870cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2871cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2872cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2873cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2874cc65eb4eSGleb Smirnoff * hidden from users. 2875cc65eb4eSGleb Smirnoff */ 2876cc65eb4eSGleb Smirnoff void 2877cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2878cc65eb4eSGleb Smirnoff { 2879cc65eb4eSGleb Smirnoff 288079db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2881cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2882cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2883cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2884cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2885cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 28863a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2887cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2888cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2889cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2890cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2891cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2892cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2893cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2894cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2895cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2896cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2897cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2898cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2899cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2900cc65eb4eSGleb Smirnoff } 2901cc65eb4eSGleb Smirnoff 2902497057eeSRobert Watson #ifdef DDB 2903497057eeSRobert Watson static void 2904497057eeSRobert Watson db_print_indent(int indent) 2905497057eeSRobert Watson { 2906497057eeSRobert Watson int i; 2907497057eeSRobert Watson 2908497057eeSRobert Watson for (i = 0; i < indent; i++) 2909497057eeSRobert Watson db_printf(" "); 2910497057eeSRobert Watson } 2911497057eeSRobert Watson 2912497057eeSRobert Watson static void 2913497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2914497057eeSRobert Watson { 2915497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2916497057eeSRobert Watson 2917497057eeSRobert Watson db_print_indent(indent); 2918497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2919497057eeSRobert Watson 2920497057eeSRobert Watson indent += 2; 2921497057eeSRobert Watson 292203dc38a4SRobert Watson #ifdef INET6 2923dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2924497057eeSRobert Watson /* IPv6. */ 2925497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2926497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 292783e521ecSBjoern A. Zeeb } else 292803dc38a4SRobert Watson #endif 292983e521ecSBjoern A. Zeeb { 2930497057eeSRobert Watson /* IPv4. */ 2931497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2932497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2933497057eeSRobert Watson } 2934497057eeSRobert Watson db_print_indent(indent); 2935497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2936497057eeSRobert Watson ntohs(inc->inc_lport)); 2937497057eeSRobert Watson db_print_indent(indent); 2938497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2939497057eeSRobert Watson ntohs(inc->inc_fport)); 2940497057eeSRobert Watson } 2941497057eeSRobert Watson 2942497057eeSRobert Watson static void 2943497057eeSRobert Watson db_print_inpflags(int inp_flags) 2944497057eeSRobert Watson { 2945497057eeSRobert Watson int comma; 2946497057eeSRobert Watson 2947497057eeSRobert Watson comma = 0; 2948497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2949497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2950497057eeSRobert Watson comma = 1; 2951497057eeSRobert Watson } 2952497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2953497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2954497057eeSRobert Watson comma = 1; 2955497057eeSRobert Watson } 2956497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2957497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2958497057eeSRobert Watson comma = 1; 2959497057eeSRobert Watson } 2960dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2961dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2962dce33a45SErmal Luçi comma = 1; 2963dce33a45SErmal Luçi } 2964497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2965497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2966497057eeSRobert Watson comma = 1; 2967497057eeSRobert Watson } 2968497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2969497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2970497057eeSRobert Watson comma = 1; 2971497057eeSRobert Watson } 2972497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2973497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2974497057eeSRobert Watson comma = 1; 2975497057eeSRobert Watson } 2976497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2977497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2978497057eeSRobert Watson comma = 1; 2979497057eeSRobert Watson } 2980497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2981497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2982497057eeSRobert Watson comma = 1; 2983497057eeSRobert Watson } 2984497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2985497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2986497057eeSRobert Watson comma = 1; 2987497057eeSRobert Watson } 2988497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2989497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2990497057eeSRobert Watson comma = 1; 2991497057eeSRobert Watson } 2992497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2993497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2994497057eeSRobert Watson comma = 1; 2995497057eeSRobert Watson } 29963cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 29973cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 29983cca425bSMichael Tuexen comma = 1; 29993cca425bSMichael Tuexen } 3000497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 3001497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3002497057eeSRobert Watson comma = 1; 3003497057eeSRobert Watson } 3004497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 3005497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3006497057eeSRobert Watson comma = 1; 3007497057eeSRobert Watson } 3008497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 3009497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3010497057eeSRobert Watson comma = 1; 3011497057eeSRobert Watson } 3012497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 3013497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3014497057eeSRobert Watson comma = 1; 3015497057eeSRobert Watson } 3016497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 3017497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3018497057eeSRobert Watson comma = 1; 3019497057eeSRobert Watson } 3020497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 3021497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3022497057eeSRobert Watson comma = 1; 3023497057eeSRobert Watson } 3024497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 3025497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3026497057eeSRobert Watson comma = 1; 3027497057eeSRobert Watson } 3028497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 3029497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3030497057eeSRobert Watson comma = 1; 3031497057eeSRobert Watson } 3032497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 3033497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3034497057eeSRobert Watson comma = 1; 3035497057eeSRobert Watson } 3036ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 3037ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 3038ad71fe3cSRobert Watson comma = 1; 3039ad71fe3cSRobert Watson } 3040ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 3041ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3042ad71fe3cSRobert Watson comma = 1; 3043ad71fe3cSRobert Watson } 3044ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 3045ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 3046ad71fe3cSRobert Watson comma = 1; 3047ad71fe3cSRobert Watson } 3048ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 3049ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3050ad71fe3cSRobert Watson comma = 1; 3051ad71fe3cSRobert Watson } 3052497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 3053497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3054497057eeSRobert Watson comma = 1; 3055497057eeSRobert Watson } 3056497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 3057497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 3058497057eeSRobert Watson comma = 1; 3059497057eeSRobert Watson } 3060497057eeSRobert Watson } 3061497057eeSRobert Watson 3062497057eeSRobert Watson static void 3063497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 3064497057eeSRobert Watson { 3065497057eeSRobert Watson int comma; 3066497057eeSRobert Watson 3067497057eeSRobert Watson comma = 0; 3068497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3069497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3070497057eeSRobert Watson comma = 1; 3071497057eeSRobert Watson } 3072497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3073497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3074497057eeSRobert Watson comma = 1; 3075497057eeSRobert Watson } 3076497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3077497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3078497057eeSRobert Watson comma = 1; 3079497057eeSRobert Watson } 3080497057eeSRobert Watson } 3081497057eeSRobert Watson 30826d888973SRobert Watson static void 3083497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3084497057eeSRobert Watson { 3085497057eeSRobert Watson 3086497057eeSRobert Watson db_print_indent(indent); 3087497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3088497057eeSRobert Watson 3089497057eeSRobert Watson indent += 2; 3090497057eeSRobert Watson 3091497057eeSRobert Watson db_print_indent(indent); 3092497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3093497057eeSRobert Watson 3094497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3095497057eeSRobert Watson 3096497057eeSRobert Watson db_print_indent(indent); 3097497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 3098497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 3099497057eeSRobert Watson 3100497057eeSRobert Watson db_print_indent(indent); 3101497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3102497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3103497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3104497057eeSRobert Watson db_printf(")\n"); 3105497057eeSRobert Watson 3106497057eeSRobert Watson db_print_indent(indent); 3107497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3108497057eeSRobert Watson inp->inp_vflag); 3109497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3110497057eeSRobert Watson db_printf(")\n"); 3111497057eeSRobert Watson 3112497057eeSRobert Watson db_print_indent(indent); 3113497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3114497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3115497057eeSRobert Watson 3116497057eeSRobert Watson db_print_indent(indent); 3117497057eeSRobert Watson #ifdef INET6 3118497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3119497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3120497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3121497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3122497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3123497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3124497057eeSRobert Watson inp->in6p_hops); 3125497057eeSRobert Watson } else 3126497057eeSRobert Watson #endif 3127497057eeSRobert Watson { 3128497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3129497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3130497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3131497057eeSRobert Watson } 3132497057eeSRobert Watson 3133497057eeSRobert Watson db_print_indent(indent); 3134497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3135497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3136497057eeSRobert Watson } 3137497057eeSRobert Watson 3138497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3139497057eeSRobert Watson { 3140497057eeSRobert Watson struct inpcb *inp; 3141497057eeSRobert Watson 3142497057eeSRobert Watson if (!have_addr) { 3143497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3144497057eeSRobert Watson return; 3145497057eeSRobert Watson } 3146497057eeSRobert Watson inp = (struct inpcb *)addr; 3147497057eeSRobert Watson 3148497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3149497057eeSRobert Watson } 315083e521ecSBjoern A. Zeeb #endif /* DDB */ 3151f3e7afe2SHans Petter Selasky 3152f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3153f3e7afe2SHans Petter Selasky /* 3154f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3155f3e7afe2SHans Petter Selasky * if any. 3156f3e7afe2SHans Petter Selasky */ 3157f3e7afe2SHans Petter Selasky int 3158f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3159f3e7afe2SHans Petter Selasky { 3160f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3161f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 316220abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3163f3e7afe2SHans Petter Selasky }; 3164f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3165f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3166f3e7afe2SHans Petter Selasky int error; 3167f3e7afe2SHans Petter Selasky 3168f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3169f3e7afe2SHans Petter Selasky if (mst == NULL) 3170f3e7afe2SHans Petter Selasky return (EINVAL); 3171f3e7afe2SHans Petter Selasky 3172f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3173f3e7afe2SHans Petter Selasky if (ifp == NULL) 3174f3e7afe2SHans Petter Selasky return (EINVAL); 3175f3e7afe2SHans Petter Selasky 3176f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 3177f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3178f3e7afe2SHans Petter Selasky } else { 3179f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 3180f3e7afe2SHans Petter Selasky } 3181f3e7afe2SHans Petter Selasky return (error); 3182f3e7afe2SHans Petter Selasky } 3183f3e7afe2SHans Petter Selasky 3184f3e7afe2SHans Petter Selasky /* 3185f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3186f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3187f3e7afe2SHans Petter Selasky */ 3188f3e7afe2SHans Petter Selasky int 3189f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3190f3e7afe2SHans Petter Selasky { 3191f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3192f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3193f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3194f3e7afe2SHans Petter Selasky int error; 3195f3e7afe2SHans Petter Selasky 3196f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3197f3e7afe2SHans Petter Selasky if (mst == NULL) 3198f3e7afe2SHans Petter Selasky return (EINVAL); 3199f3e7afe2SHans Petter Selasky 3200f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3201f3e7afe2SHans Petter Selasky if (ifp == NULL) 3202f3e7afe2SHans Petter Selasky return (EINVAL); 3203f3e7afe2SHans Petter Selasky 3204f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 3205f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3206f3e7afe2SHans Petter Selasky } else { 3207f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 3208f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3209f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3210f3e7afe2SHans Petter Selasky } 3211f3e7afe2SHans Petter Selasky return (error); 3212f3e7afe2SHans Petter Selasky } 3213f3e7afe2SHans Petter Selasky 3214f3e7afe2SHans Petter Selasky /* 321595ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 321695ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 321795ed5015SHans Petter Selasky */ 321895ed5015SHans Petter Selasky int 321995ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 322095ed5015SHans Petter Selasky { 322195ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 322295ed5015SHans Petter Selasky struct m_snd_tag *mst; 322395ed5015SHans Petter Selasky struct ifnet *ifp; 322495ed5015SHans Petter Selasky int error; 322595ed5015SHans Petter Selasky 322695ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 322795ed5015SHans Petter Selasky if (mst == NULL) 322895ed5015SHans Petter Selasky return (EINVAL); 322995ed5015SHans Petter Selasky 323095ed5015SHans Petter Selasky ifp = mst->ifp; 323195ed5015SHans Petter Selasky if (ifp == NULL) 323295ed5015SHans Petter Selasky return (EINVAL); 323395ed5015SHans Petter Selasky 323495ed5015SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) 323595ed5015SHans Petter Selasky return (EOPNOTSUPP); 323695ed5015SHans Petter Selasky 323795ed5015SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 323895ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 323995ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 324095ed5015SHans Petter Selasky return (error); 324195ed5015SHans Petter Selasky } 324295ed5015SHans Petter Selasky 324395ed5015SHans Petter Selasky /* 3244f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3245f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3246f3e7afe2SHans Petter Selasky */ 3247f3e7afe2SHans Petter Selasky int 3248f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 324920abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 325020abea66SRandall Stewart 3251f3e7afe2SHans Petter Selasky { 3252f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 325395ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 325495ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3255f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3256f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 325798085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3258f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 325920abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3260f3e7afe2SHans Petter Selasky }; 3261f3e7afe2SHans Petter Selasky int error; 3262f3e7afe2SHans Petter Selasky 3263f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3264f3e7afe2SHans Petter Selasky 326520abea66SRandall Stewart if (*st != NULL) 3266f3e7afe2SHans Petter Selasky return (EINVAL); 3267f3e7afe2SHans Petter Selasky 3268f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 3269f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3270f3e7afe2SHans Petter Selasky } else { 3271f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 327220abea66SRandall Stewart 3273903c4ee6SXin LI #ifdef INET 327420abea66SRandall Stewart if (error == 0) { 327520abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 327620abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 327720abea66SRandall Stewart } else 327820abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3279903c4ee6SXin LI #endif 3280f3e7afe2SHans Petter Selasky } 3281f3e7afe2SHans Petter Selasky return (error); 3282f3e7afe2SHans Petter Selasky } 3283f3e7afe2SHans Petter Selasky 328420abea66SRandall Stewart void 328520abea66SRandall Stewart in_pcbdetach_tag(struct ifnet *ifp, struct m_snd_tag *mst) 328620abea66SRandall Stewart { 328720abea66SRandall Stewart if (ifp == NULL) 328820abea66SRandall Stewart return; 328920abea66SRandall Stewart 329020abea66SRandall Stewart /* 329120abea66SRandall Stewart * If the device was detached while we still had reference(s) 329220abea66SRandall Stewart * on the ifp, we assume if_snd_tag_free() was replaced with 329320abea66SRandall Stewart * stubs. 329420abea66SRandall Stewart */ 329520abea66SRandall Stewart ifp->if_snd_tag_free(mst); 329620abea66SRandall Stewart 329720abea66SRandall Stewart /* release reference count on network interface */ 329820abea66SRandall Stewart if_rele(ifp); 3299903c4ee6SXin LI #ifdef INET 330020abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3301903c4ee6SXin LI #endif 330220abea66SRandall Stewart } 330320abea66SRandall Stewart 3304f3e7afe2SHans Petter Selasky /* 3305f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3306f3e7afe2SHans Petter Selasky * if any: 3307f3e7afe2SHans Petter Selasky */ 3308f3e7afe2SHans Petter Selasky void 3309f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3310f3e7afe2SHans Petter Selasky { 3311f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3312f3e7afe2SHans Petter Selasky 3313f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3314f3e7afe2SHans Petter Selasky 3315f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3316f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3317f3e7afe2SHans Petter Selasky 3318f3e7afe2SHans Petter Selasky if (mst == NULL) 3319f3e7afe2SHans Petter Selasky return; 3320f3e7afe2SHans Petter Selasky 3321fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3322f3e7afe2SHans Petter Selasky } 3323f3e7afe2SHans Petter Selasky 332420abea66SRandall Stewart int 332520abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 332620abea66SRandall Stewart { 332720abea66SRandall Stewart int error; 332820abea66SRandall Stewart 332920abea66SRandall Stewart /* 333020abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 333120abea66SRandall Stewart * a route change, first drop the existing tag. Set the 333220abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 333320abea66SRandall Stewart * tag if we fail to allocate one this time. 333420abea66SRandall Stewart */ 333520abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 333620abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 333720abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 333820abea66SRandall Stewart } 333920abea66SRandall Stewart 334020abea66SRandall Stewart /* 334120abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 334220abea66SRandall Stewart * made to ensure the network interface doesn't go away until 334320abea66SRandall Stewart * all ratelimit connections are gone. The network interface 334420abea66SRandall Stewart * pointers compared below represent valid network interfaces, 334520abea66SRandall Stewart * except when comparing towards NULL. 334620abea66SRandall Stewart */ 334720abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 334820abea66SRandall Stewart error = 0; 334920abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 335020abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 335120abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 335220abea66SRandall Stewart error = 0; 335320abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 335420abea66SRandall Stewart /* 335520abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 335620abea66SRandall Stewart * to wait until there is a valid RSS hash before we 335720abea66SRandall Stewart * can proceed: 335820abea66SRandall Stewart */ 335920abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 336020abea66SRandall Stewart error = EAGAIN; 336120abea66SRandall Stewart } else { 336220abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 336320abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 336420abea66SRandall Stewart } 336520abea66SRandall Stewart } else { 336620abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 336720abea66SRandall Stewart } 336820abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 336920abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 337020abea66SRandall Stewart 337120abea66SRandall Stewart return (error); 337220abea66SRandall Stewart } 337320abea66SRandall Stewart 3374f3e7afe2SHans Petter Selasky /* 3375f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3376f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3377f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3378f3e7afe2SHans Petter Selasky */ 3379f3e7afe2SHans Petter Selasky void 3380f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3381f3e7afe2SHans Petter Selasky { 3382f3e7afe2SHans Petter Selasky struct socket *socket; 3383f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3384f3e7afe2SHans Petter Selasky bool did_upgrade; 3385f3e7afe2SHans Petter Selasky int error; 3386f3e7afe2SHans Petter Selasky 3387f3e7afe2SHans Petter Selasky if (inp == NULL) 3388f3e7afe2SHans Petter Selasky return; 3389f3e7afe2SHans Petter Selasky 3390f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3391f3e7afe2SHans Petter Selasky if (socket == NULL) 3392f3e7afe2SHans Petter Selasky return; 3393f3e7afe2SHans Petter Selasky 3394f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3395f3e7afe2SHans Petter Selasky /* 3396f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3397f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3398f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3399f3e7afe2SHans Petter Selasky * write lock. 3400f3e7afe2SHans Petter Selasky */ 3401f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3402f3e7afe2SHans Petter Selasky return; 3403f3e7afe2SHans Petter Selasky did_upgrade = 1; 3404f3e7afe2SHans Petter Selasky } else { 3405f3e7afe2SHans Petter Selasky did_upgrade = 0; 3406f3e7afe2SHans Petter Selasky } 3407f3e7afe2SHans Petter Selasky 3408f3e7afe2SHans Petter Selasky /* 3409f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3410f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3411f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3412f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3413f3e7afe2SHans Petter Selasky */ 3414f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3415f3e7afe2SHans Petter Selasky 341620abea66SRandall Stewart error = in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3417fb3bc596SJohn Baldwin 3418f3e7afe2SHans Petter Selasky if (did_upgrade) 3419f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3420f3e7afe2SHans Petter Selasky } 3421f3e7afe2SHans Petter Selasky 3422f3e7afe2SHans Petter Selasky /* 3423f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3424f3e7afe2SHans Petter Selasky */ 3425f3e7afe2SHans Petter Selasky void 3426f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3427f3e7afe2SHans Petter Selasky { 3428f3e7afe2SHans Petter Selasky bool did_upgrade; 3429f3e7afe2SHans Petter Selasky 3430f3e7afe2SHans Petter Selasky if (inp == NULL) 3431f3e7afe2SHans Petter Selasky return; 3432f3e7afe2SHans Petter Selasky 3433f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3434f3e7afe2SHans Petter Selasky return; 3435f3e7afe2SHans Petter Selasky 3436f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3437f3e7afe2SHans Petter Selasky /* 3438f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3439f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3440f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3441f3e7afe2SHans Petter Selasky * write lock. 3442f3e7afe2SHans Petter Selasky */ 3443f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3444f3e7afe2SHans Petter Selasky return; 3445f3e7afe2SHans Petter Selasky did_upgrade = 1; 3446f3e7afe2SHans Petter Selasky } else { 3447f3e7afe2SHans Petter Selasky did_upgrade = 0; 3448f3e7afe2SHans Petter Selasky } 3449f3e7afe2SHans Petter Selasky 3450f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3451f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3452f3e7afe2SHans Petter Selasky 3453f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3454f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3455f3e7afe2SHans Petter Selasky 3456f3e7afe2SHans Petter Selasky if (did_upgrade) 3457f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3458f3e7afe2SHans Petter Selasky } 345920abea66SRandall Stewart 3460903c4ee6SXin LI #ifdef INET 346120abea66SRandall Stewart static void 346220abea66SRandall Stewart rl_init(void *st) 346320abea66SRandall Stewart { 346420abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 346520abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 346620abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 346720abea66SRandall Stewart } 346820abea66SRandall Stewart 346920abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3470903c4ee6SXin LI #endif 3471f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3472