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> 919ac7c6cfSAlexander 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 */ 1069ac7c6cfSAlexander 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 613efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 614*25102351SMike Karels /* 615*25102351SMike Karels * Assign a local port like in_pcb_lport(), but also used with connect() 616*25102351SMike Karels * and a foreign address and port. If fsa is non-NULL, choose a local port 617*25102351SMike Karels * that is unused with those, otherwise one that is completely unused. 618*25102351SMike Karels */ 619efc76f72SBjoern A. Zeeb int 620*25102351SMike Karels in_pcb_lport_dest(struct inpcb *inp, struct sockaddr *lsa, u_short *lportp, 621*25102351SMike Karels struct sockaddr *fsa, u_short fport, struct ucred *cred, int lookupflags) 622efc76f72SBjoern A. Zeeb { 623efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 624efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 625efc76f72SBjoern A. Zeeb unsigned short *lastport; 626efc76f72SBjoern A. Zeeb int count, dorandom, error; 627efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 628efc76f72SBjoern A. Zeeb #ifdef INET 629*25102351SMike Karels struct in_addr laddr, faddr; 630*25102351SMike Karels #endif 631*25102351SMike Karels #ifdef INET6 632*25102351SMike Karels struct in6_addr *laddr6, *faddr6; 633efc76f72SBjoern A. Zeeb #endif 634efc76f72SBjoern A. Zeeb 635efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 636efc76f72SBjoern A. Zeeb 637efc76f72SBjoern A. Zeeb /* 638efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 639efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 640efc76f72SBjoern A. Zeeb */ 641efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 642fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 643efc76f72SBjoern A. Zeeb 644efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 645efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 646efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 647efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 648efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 649cc426dd3SMateusz Guzik error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 650efc76f72SBjoern A. Zeeb if (error) 651efc76f72SBjoern A. Zeeb return (error); 652efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 653efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 654efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 655efc76f72SBjoern A. Zeeb } else { 656efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 657efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 658efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 659efc76f72SBjoern A. Zeeb } 660efc76f72SBjoern A. Zeeb /* 661e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 662efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 663efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 664efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 665efc76f72SBjoern A. Zeeb */ 666efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 667e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 668e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 669efc76f72SBjoern A. Zeeb dorandom = 1; 670efc76f72SBjoern A. Zeeb else 671efc76f72SBjoern A. Zeeb dorandom = 0; 672efc76f72SBjoern A. Zeeb /* 673efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 674efc76f72SBjoern A. Zeeb * we have the only port available. 675efc76f72SBjoern A. Zeeb */ 676efc76f72SBjoern A. Zeeb if (first == last) 677efc76f72SBjoern A. Zeeb dorandom = 0; 678e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 679e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 680efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 681efc76f72SBjoern A. Zeeb /* 682efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 683efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 684efc76f72SBjoern A. Zeeb * code path implementing all logic. 685efc76f72SBjoern A. Zeeb */ 686efc76f72SBjoern A. Zeeb if (first > last) { 687efc76f72SBjoern A. Zeeb aux = first; 688efc76f72SBjoern A. Zeeb first = last; 689efc76f72SBjoern A. Zeeb last = aux; 690efc76f72SBjoern A. Zeeb } 691efc76f72SBjoern A. Zeeb 692efc76f72SBjoern A. Zeeb #ifdef INET 693*25102351SMike Karels laddr.s_addr = INADDR_ANY; 6944d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 695*25102351SMike Karels laddr = ((struct sockaddr_in *)lsa)->sin_addr; 696*25102351SMike Karels if (fsa != NULL) 697*25102351SMike Karels faddr = ((struct sockaddr_in *)fsa)->sin_addr; 698efc76f72SBjoern A. Zeeb } 699efc76f72SBjoern A. Zeeb #endif 700*25102351SMike Karels #ifdef INET6 701*25102351SMike Karels if (lsa->sa_family == AF_INET6) { 702*25102351SMike Karels laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 703*25102351SMike Karels if (fsa != NULL) 704*25102351SMike Karels faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 705*25102351SMike Karels } 706*25102351SMike Karels #endif 707*25102351SMike Karels 708*25102351SMike Karels tmpinp = NULL; 709efc76f72SBjoern A. Zeeb lport = *lportp; 710efc76f72SBjoern A. Zeeb 711efc76f72SBjoern A. Zeeb if (dorandom) 712efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 713efc76f72SBjoern A. Zeeb 714efc76f72SBjoern A. Zeeb count = last - first; 715efc76f72SBjoern A. Zeeb 716efc76f72SBjoern A. Zeeb do { 717efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 718efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 719efc76f72SBjoern A. Zeeb ++*lastport; 720efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 721efc76f72SBjoern A. Zeeb *lastport = first; 722efc76f72SBjoern A. Zeeb lport = htons(*lastport); 723efc76f72SBjoern A. Zeeb 724*25102351SMike Karels if (fsa != NULL) { 725*25102351SMike Karels 726*25102351SMike Karels #ifdef INET 727*25102351SMike Karels if (lsa->sa_family == AF_INET) { 728*25102351SMike Karels tmpinp = in_pcblookup_hash_locked(pcbinfo, 729*25102351SMike Karels faddr, fport, laddr, lport, lookupflags, 730*25102351SMike Karels NULL); 731*25102351SMike Karels } 732*25102351SMike Karels #endif 733*25102351SMike Karels #ifdef INET6 734*25102351SMike Karels if (lsa->sa_family == AF_INET6) { 735*25102351SMike Karels tmpinp = in6_pcblookup_hash_locked(pcbinfo, 736*25102351SMike Karels faddr6, fport, laddr6, lport, lookupflags, 737*25102351SMike Karels NULL); 738*25102351SMike Karels } 739*25102351SMike Karels #endif 740*25102351SMike Karels } else { 741efc76f72SBjoern A. Zeeb #ifdef INET6 7424616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 743efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 74468e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 745efc76f72SBjoern A. Zeeb #endif 746efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 747efc76f72SBjoern A. Zeeb else 748efc76f72SBjoern A. Zeeb #endif 749efc76f72SBjoern A. Zeeb #ifdef INET 750efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 75168e0d7e0SRobert Watson lport, lookupflags, cred); 752efc76f72SBjoern A. Zeeb #endif 753*25102351SMike Karels } 754efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 755efc76f72SBjoern A. Zeeb 756efc76f72SBjoern A. Zeeb *lportp = lport; 757efc76f72SBjoern A. Zeeb 758efc76f72SBjoern A. Zeeb return (0); 759efc76f72SBjoern A. Zeeb } 760efdf104bSMikolaj Golub 761efdf104bSMikolaj Golub /* 762*25102351SMike Karels * Select a local port (number) to use. 763*25102351SMike Karels */ 764*25102351SMike Karels int 765*25102351SMike Karels in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 766*25102351SMike Karels struct ucred *cred, int lookupflags) 767*25102351SMike Karels { 768*25102351SMike Karels struct sockaddr_in laddr; 769*25102351SMike Karels 770*25102351SMike Karels if (laddrp) { 771*25102351SMike Karels bzero(&laddr, sizeof(laddr)); 772*25102351SMike Karels laddr.sin_family = AF_INET; 773*25102351SMike Karels laddr.sin_addr = *laddrp; 774*25102351SMike Karels } 775*25102351SMike Karels return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 776*25102351SMike Karels NULL, lportp, NULL, 0, cred, lookupflags)); 777*25102351SMike Karels } 778*25102351SMike Karels 779*25102351SMike Karels /* 780efdf104bSMikolaj Golub * Return cached socket options. 781efdf104bSMikolaj Golub */ 7821a43cff9SSean Bruno int 783efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 784efdf104bSMikolaj Golub { 7851a43cff9SSean Bruno int so_options; 786efdf104bSMikolaj Golub 787efdf104bSMikolaj Golub so_options = 0; 788efdf104bSMikolaj Golub 7891a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 7901a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 791efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 792efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 793efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 794efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 795efdf104bSMikolaj Golub return (so_options); 796efdf104bSMikolaj Golub } 797efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 798efc76f72SBjoern A. Zeeb 7994b932371SIan Dowse /* 8000a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 8010a100a6fSAdrian Chadd * 8020a100a6fSAdrian Chadd * ni points to the new inp. 8030a100a6fSAdrian Chadd * oi points to the exisitng inp. 8040a100a6fSAdrian Chadd * 8050a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 8060a100a6fSAdrian Chadd * whether the credentials match. 8070a100a6fSAdrian Chadd */ 808d5bb8bd3SAdrian Chadd int 8090a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 8100a100a6fSAdrian Chadd { 8110a100a6fSAdrian Chadd /* Check permissions match */ 8120a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8130a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 8140a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 8150a100a6fSAdrian Chadd return (0); 8160a100a6fSAdrian Chadd 8170a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 8180a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 8190a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 8200a100a6fSAdrian Chadd return (0); 8210a100a6fSAdrian Chadd 8220a100a6fSAdrian Chadd /* 8230a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 8240a100a6fSAdrian Chadd * it is and it matches the checks. 8250a100a6fSAdrian Chadd */ 8260a100a6fSAdrian Chadd return (1); 8270a100a6fSAdrian Chadd } 8280a100a6fSAdrian Chadd 829d5bb8bd3SAdrian Chadd #ifdef INET 8300a100a6fSAdrian Chadd /* 8314b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 8324b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 8334b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 8344b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 8354b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 8364b932371SIan Dowse * 8374b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 8384b932371SIan Dowse */ 8394b932371SIan Dowse int 840136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 841136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 8424b932371SIan Dowse { 8434b932371SIan Dowse struct socket *so = inp->inp_socket; 84415bd2b43SDavid Greenman struct sockaddr_in *sin; 845c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 8464b932371SIan Dowse struct in_addr laddr; 847df8bae1dSRodney W. Grimes u_short lport = 0; 84868e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 849413628a7SBjoern A. Zeeb int error; 850df8bae1dSRodney W. Grimes 8518501a69cSRobert Watson /* 8521a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 8531a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 8541a43cff9SSean Bruno */ 8551a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 8561a43cff9SSean Bruno 8571a43cff9SSean Bruno /* 858fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 8598501a69cSRobert Watson */ 86059daba27SSam Leffler INP_LOCK_ASSERT(inp); 861fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 86259daba27SSam Leffler 863d7c5a620SMatt Macy if (CK_STAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 864df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 8654b932371SIan Dowse laddr.s_addr = *laddrp; 8664b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 867df8bae1dSRodney W. Grimes return (EINVAL); 8681a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 86968e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 8704616026fSErmal Luçi if (nam == NULL) { 8717c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 8727c2f3cb9SJamie Gritton return (error); 8737c2f3cb9SJamie Gritton } else { 87457bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 87557bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 876df8bae1dSRodney W. Grimes return (EINVAL); 877df8bae1dSRodney W. Grimes #ifdef notdef 878df8bae1dSRodney W. Grimes /* 879df8bae1dSRodney W. Grimes * We should check the family, but old programs 880df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 881df8bae1dSRodney W. Grimes */ 882df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 883df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 884df8bae1dSRodney W. Grimes #endif 885b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 886b89e82ddSJamie Gritton if (error) 887b89e82ddSJamie Gritton return (error); 8884b932371SIan Dowse if (sin->sin_port != *lportp) { 8894b932371SIan Dowse /* Don't allow the port to change. */ 8904b932371SIan Dowse if (*lportp != 0) 8914b932371SIan Dowse return (EINVAL); 892df8bae1dSRodney W. Grimes lport = sin->sin_port; 8934b932371SIan Dowse } 8944b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 895df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 896df8bae1dSRodney W. Grimes /* 897df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 898df8bae1dSRodney W. Grimes * allow complete duplication of binding if 899df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 900df8bae1dSRodney W. Grimes * and a multicast address is bound on both 901df8bae1dSRodney W. Grimes * new and duplicated sockets. 902df8bae1dSRodney W. Grimes */ 903f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 904df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 9051a43cff9SSean Bruno /* 9061a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 9071a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 9081a43cff9SSean Bruno */ 9091a43cff9SSean Bruno if ((so->so_options & 9101a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 9111a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 912df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 913df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 91483103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 9154209e01aSAdrian Chadd /* 9164209e01aSAdrian Chadd * Is the address a local IP address? 917f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 9188696873dSAdrian Chadd * to any endpoint address, local or not. 9194209e01aSAdrian Chadd */ 920f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 9218896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 922df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 923df8bae1dSRodney W. Grimes } 9244b932371SIan Dowse laddr = sin->sin_addr; 925df8bae1dSRodney W. Grimes if (lport) { 926df8bae1dSRodney W. Grimes struct inpcb *t; 927ae0e7143SRobert Watson struct tcptw *tw; 928ae0e7143SRobert Watson 929df8bae1dSRodney W. Grimes /* GROSS */ 930603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 931603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 932cc426dd3SMateusz Guzik priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 9332469dd60SGarrett Wollman return (EACCES); 934835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 935cc426dd3SMateusz Guzik priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 936078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 937413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 938340c35deSJonathan Lemon /* 939340c35deSJonathan Lemon * XXX 940340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 941340c35deSJonathan Lemon */ 9424cc20ab1SSeigo Tanimura if (t && 9430a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 944ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 9454658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 9464658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 9474cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 94852b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 9491a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 9501a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 95186d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 95286d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 9534049a042SGuido van Rooij return (EADDRINUSE); 9540a100a6fSAdrian Chadd 9550a100a6fSAdrian Chadd /* 9560a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 9570a100a6fSAdrian Chadd * the credentials need to match and the 9580a100a6fSAdrian Chadd * original socket also has to have been bound 9590a100a6fSAdrian Chadd * with BINDMULTI. 9600a100a6fSAdrian Chadd */ 9610a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9620a100a6fSAdrian Chadd return (EADDRINUSE); 9634049a042SGuido van Rooij } 964c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 96568e0d7e0SRobert Watson lport, lookupflags, cred); 966ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 967ae0e7143SRobert Watson /* 968ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 969ae0e7143SRobert Watson * state recycled, we treat the address as 970ae0e7143SRobert Watson * being in use (for now). This is better 971ae0e7143SRobert Watson * than a panic, but not desirable. 972ae0e7143SRobert Watson */ 973ec95b709SMikolaj Golub tw = intotw(t); 974ae0e7143SRobert Watson if (tw == NULL || 9751a43cff9SSean Bruno ((reuseport & tw->tw_so_options) == 0 && 9761a43cff9SSean Bruno (reuseport_lb & 9771a43cff9SSean Bruno tw->tw_so_options) == 0)) { 978340c35deSJonathan Lemon return (EADDRINUSE); 9791a43cff9SSean Bruno } 9800a100a6fSAdrian Chadd } else if (t && 9810a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 9821a43cff9SSean Bruno (reuseport & inp_so_options(t)) == 0 && 9831a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 984e3fd5ffdSRobert Watson #ifdef INET6 98533841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 986cfa1ca9dSYoshinobu Inoue INADDR_ANY || 987cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 988cfa1ca9dSYoshinobu Inoue INADDR_ANY || 989fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 990fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 991e3fd5ffdSRobert Watson #endif 992df8bae1dSRodney W. Grimes return (EADDRINUSE); 9930a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9940a100a6fSAdrian Chadd return (EADDRINUSE); 995df8bae1dSRodney W. Grimes } 996cfa1ca9dSYoshinobu Inoue } 997df8bae1dSRodney W. Grimes } 9984b932371SIan Dowse if (*lportp != 0) 9994b932371SIan Dowse lport = *lportp; 100033b3ac06SPeter Wemm if (lport == 0) { 10014616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 1002efc76f72SBjoern A. Zeeb if (error != 0) 1003efc76f72SBjoern A. Zeeb return (error); 100433b3ac06SPeter Wemm 100533b3ac06SPeter Wemm } 10064b932371SIan Dowse *laddrp = laddr.s_addr; 10074b932371SIan Dowse *lportp = lport; 1008df8bae1dSRodney W. Grimes return (0); 1009df8bae1dSRodney W. Grimes } 1010df8bae1dSRodney W. Grimes 1011999f1343SGarrett Wollman /* 10125200e00eSIan Dowse * Connect from a socket to a specified address. 10135200e00eSIan Dowse * Both address and port must be specified in argument sin. 10145200e00eSIan Dowse * If don't have a local address for this socket yet, 10155200e00eSIan Dowse * then pick one. 1016999f1343SGarrett Wollman */ 1017999f1343SGarrett Wollman int 1018d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 1019fe1274eeSMichael Tuexen struct ucred *cred, struct mbuf *m, bool rehash) 1020999f1343SGarrett Wollman { 10215200e00eSIan Dowse u_short lport, fport; 10225200e00eSIan Dowse in_addr_t laddr, faddr; 10235200e00eSIan Dowse int anonport, error; 1024df8bae1dSRodney W. Grimes 10258501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1026fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 102727f74fd0SRobert Watson 10285200e00eSIan Dowse lport = inp->inp_lport; 10295200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 10305200e00eSIan Dowse anonport = (lport == 0); 10315200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 1032b0330ed9SPawel Jakub Dawidek NULL, cred); 10335200e00eSIan Dowse if (error) 10345200e00eSIan Dowse return (error); 10355200e00eSIan Dowse 10365200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 10375200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 1038fe1274eeSMichael Tuexen KASSERT(rehash == true, 1039fe1274eeSMichael Tuexen ("Rehashing required for unbound inps")); 10405200e00eSIan Dowse inp->inp_lport = lport; 10415200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10425200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 10435200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 10445200e00eSIan Dowse inp->inp_lport = 0; 10455200e00eSIan Dowse return (EAGAIN); 10465200e00eSIan Dowse } 10475200e00eSIan Dowse } 10485200e00eSIan Dowse 10495200e00eSIan Dowse /* Commit the remaining changes. */ 10505200e00eSIan Dowse inp->inp_lport = lport; 10515200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10525200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 10535200e00eSIan Dowse inp->inp_fport = fport; 1054fe1274eeSMichael Tuexen if (rehash) { 1055d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 1056fe1274eeSMichael Tuexen } else { 1057fe1274eeSMichael Tuexen in_pcbinshash_mbuf(inp, m); 1058fe1274eeSMichael Tuexen } 10592cb64cb2SGeorge V. Neville-Neil 10605200e00eSIan Dowse if (anonport) 10615200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 10625200e00eSIan Dowse return (0); 10635200e00eSIan Dowse } 10645200e00eSIan Dowse 1065d3c1f003SRobert Watson int 1066d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 1067d3c1f003SRobert Watson { 1068d3c1f003SRobert Watson 1069fe1274eeSMichael Tuexen return (in_pcbconnect_mbuf(inp, nam, cred, NULL, true)); 1070d3c1f003SRobert Watson } 1071d3c1f003SRobert Watson 10725200e00eSIan Dowse /* 10730895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 10740895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 10750895aec3SBjoern A. Zeeb */ 1076ae190832SSteven Hartland int 10770895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 10780895aec3SBjoern A. Zeeb struct ucred *cred) 10790895aec3SBjoern A. Zeeb { 10800895aec3SBjoern A. Zeeb struct ifaddr *ifa; 10810895aec3SBjoern A. Zeeb struct sockaddr *sa; 10829ac7c6cfSAlexander V. Chernikov struct sockaddr_in *sin, dst; 10839ac7c6cfSAlexander V. Chernikov struct nhop_object *nh; 10840895aec3SBjoern A. Zeeb int error; 10850895aec3SBjoern A. Zeeb 1086c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 1087413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1088592bcae8SBjoern A. Zeeb /* 1089592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1090592bcae8SBjoern A. Zeeb * if requested. 1091592bcae8SBjoern A. Zeeb */ 1092592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 1093592bcae8SBjoern A. Zeeb return (0); 1094592bcae8SBjoern A. Zeeb 10950895aec3SBjoern A. Zeeb error = 0; 10960895aec3SBjoern A. Zeeb 10979ac7c6cfSAlexander V. Chernikov nh = NULL; 10989ac7c6cfSAlexander V. Chernikov bzero(&dst, sizeof(dst)); 10999ac7c6cfSAlexander V. Chernikov sin = &dst; 11000895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 11010895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 11020895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 11030895aec3SBjoern A. Zeeb 11040895aec3SBjoern A. Zeeb /* 11050895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 11060895aec3SBjoern A. Zeeb * else punt. 11070895aec3SBjoern A. Zeeb * 11080895aec3SBjoern A. Zeeb * Find out route to destination. 11090895aec3SBjoern A. Zeeb */ 11100895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 11119ac7c6cfSAlexander V. Chernikov nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 11129ac7c6cfSAlexander V. Chernikov 0, NHR_NONE, 0); 11130895aec3SBjoern A. Zeeb 11140895aec3SBjoern A. Zeeb /* 11150895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 11160895aec3SBjoern A. Zeeb * the outgoing interface. 11170895aec3SBjoern A. Zeeb * 11180895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 11190895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 11200895aec3SBjoern A. Zeeb * the source address from. 11210895aec3SBjoern A. Zeeb */ 11229ac7c6cfSAlexander V. Chernikov if (nh == NULL || nh->nh_ifp == NULL) { 11238c0fec80SRobert Watson struct in_ifaddr *ia; 11240895aec3SBjoern A. Zeeb struct ifnet *ifp; 11250895aec3SBjoern A. Zeeb 11264f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 112758a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11284f6c66ccSMatt Macy if (ia == NULL) { 11294f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 113058a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11314f6c66ccSMatt Macy 11324f6c66ccSMatt Macy } 11330895aec3SBjoern A. Zeeb if (ia == NULL) { 11340895aec3SBjoern A. Zeeb error = ENETUNREACH; 11350895aec3SBjoern A. Zeeb goto done; 11360895aec3SBjoern A. Zeeb } 11370895aec3SBjoern A. Zeeb 11380304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11390895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11400895aec3SBjoern A. Zeeb goto done; 11410895aec3SBjoern A. Zeeb } 11420895aec3SBjoern A. Zeeb 11430895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 11440895aec3SBjoern A. Zeeb ia = NULL; 1145d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11460895aec3SBjoern A. Zeeb 11470895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11480895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11490895aec3SBjoern A. Zeeb continue; 11500895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1151b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11520895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11530895aec3SBjoern A. Zeeb break; 11540895aec3SBjoern A. Zeeb } 11550895aec3SBjoern A. Zeeb } 11560895aec3SBjoern A. Zeeb if (ia != NULL) { 11570895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11580895aec3SBjoern A. Zeeb goto done; 11590895aec3SBjoern A. Zeeb } 11600895aec3SBjoern A. Zeeb 11610895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1162b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11630895aec3SBjoern A. Zeeb goto done; 11640895aec3SBjoern A. Zeeb } 11650895aec3SBjoern A. Zeeb 11660895aec3SBjoern A. Zeeb /* 11670895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 11680895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 11690895aec3SBjoern A. Zeeb * In case of jails do those three steps: 11700895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 11710895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 11720895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 11730895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 11740895aec3SBjoern A. Zeeb */ 11759ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 11768c0fec80SRobert Watson struct in_ifaddr *ia; 11779317b04eSRobert Watson struct ifnet *ifp; 11780895aec3SBjoern A. Zeeb 11790895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 11800304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11819ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11820895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11830895aec3SBjoern A. Zeeb goto done; 11840895aec3SBjoern A. Zeeb } 11850895aec3SBjoern A. Zeeb 11860895aec3SBjoern A. Zeeb /* Jailed. */ 11870895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 11889ac7c6cfSAlexander V. Chernikov sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1189b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11909ac7c6cfSAlexander V. Chernikov ia = (struct in_ifaddr *)nh->nh_ifa; 11910895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11920895aec3SBjoern A. Zeeb goto done; 11930895aec3SBjoern A. Zeeb } 11940895aec3SBjoern A. Zeeb 11950895aec3SBjoern A. Zeeb /* 11960895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 11970895aec3SBjoern A. Zeeb * belonging to this jail. 11980895aec3SBjoern A. Zeeb */ 11998c0fec80SRobert Watson ia = NULL; 12009ac7c6cfSAlexander V. Chernikov ifp = nh->nh_ifp; 1201d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12020895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12030895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12040895aec3SBjoern A. Zeeb continue; 12050895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1206b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 12070895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12080895aec3SBjoern A. Zeeb break; 12090895aec3SBjoern A. Zeeb } 12100895aec3SBjoern A. Zeeb } 12110895aec3SBjoern A. Zeeb if (ia != NULL) { 12120895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12130895aec3SBjoern A. Zeeb goto done; 12140895aec3SBjoern A. Zeeb } 12150895aec3SBjoern A. Zeeb 12160895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1217b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12180895aec3SBjoern A. Zeeb goto done; 12190895aec3SBjoern A. Zeeb } 12200895aec3SBjoern A. Zeeb 12210895aec3SBjoern A. Zeeb /* 12220895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 12230895aec3SBjoern A. Zeeb * to ourselves is here. 12240895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 12250895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 12260895aec3SBjoern A. Zeeb * a loopback interface. 12270895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 12280895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 12290895aec3SBjoern A. Zeeb */ 12309ac7c6cfSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 12318c0fec80SRobert Watson struct in_ifaddr *ia; 12320895aec3SBjoern A. Zeeb 12339ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 123458a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 12350895aec3SBjoern A. Zeeb if (ia == NULL) 12369ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 123758a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1238f0bb05fcSQing Li if (ia == NULL) 12399ac7c6cfSAlexander V. Chernikov ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 12400895aec3SBjoern A. Zeeb 12410304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 12420895aec3SBjoern A. Zeeb if (ia == NULL) { 12430895aec3SBjoern A. Zeeb error = ENETUNREACH; 12440895aec3SBjoern A. Zeeb goto done; 12450895aec3SBjoern A. Zeeb } 12460895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12470895aec3SBjoern A. Zeeb goto done; 12480895aec3SBjoern A. Zeeb } 12490895aec3SBjoern A. Zeeb 12500895aec3SBjoern A. Zeeb /* Jailed. */ 12510895aec3SBjoern A. Zeeb if (ia != NULL) { 12520895aec3SBjoern A. Zeeb struct ifnet *ifp; 12530895aec3SBjoern A. Zeeb 12540895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12550895aec3SBjoern A. Zeeb ia = NULL; 1256d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12570895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12580895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12590895aec3SBjoern A. Zeeb continue; 12600895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1261b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1262b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 12630895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12640895aec3SBjoern A. Zeeb break; 12650895aec3SBjoern A. Zeeb } 12660895aec3SBjoern A. Zeeb } 12670895aec3SBjoern A. Zeeb if (ia != NULL) { 12680895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12690895aec3SBjoern A. Zeeb goto done; 12700895aec3SBjoern A. Zeeb } 12710895aec3SBjoern A. Zeeb } 12720895aec3SBjoern A. Zeeb 12730895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1274b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12750895aec3SBjoern A. Zeeb goto done; 12760895aec3SBjoern A. Zeeb } 12770895aec3SBjoern A. Zeeb 12780895aec3SBjoern A. Zeeb done: 12790895aec3SBjoern A. Zeeb return (error); 12800895aec3SBjoern A. Zeeb } 12810895aec3SBjoern A. Zeeb 12820895aec3SBjoern A. Zeeb /* 12835200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 12845200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 12855200e00eSIan Dowse * address and port for the PCB; these are updated to the values 12865200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 12875200e00eSIan Dowse * the connect. 12885200e00eSIan Dowse * 12895200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 12905200e00eSIan Dowse * and port. These are not updated in the error case. 12915200e00eSIan Dowse * 12925200e00eSIan Dowse * If the operation fails because the connection already exists, 12935200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 12945200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 12955200e00eSIan Dowse * is set to NULL. 12965200e00eSIan Dowse */ 12975200e00eSIan Dowse int 1298136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1299136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1300136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 13015200e00eSIan Dowse { 1302cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 13035200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 13045200e00eSIan Dowse struct in_ifaddr *ia; 13055200e00eSIan Dowse struct inpcb *oinp; 1306b89e82ddSJamie Gritton struct in_addr laddr, faddr; 13075200e00eSIan Dowse u_short lport, fport; 13085200e00eSIan Dowse int error; 13095200e00eSIan Dowse 13108501a69cSRobert Watson /* 13118501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 13128501a69cSRobert Watson * lock is sufficient. 13138501a69cSRobert Watson */ 1314c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 131527f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1316fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 131727f74fd0SRobert Watson 13185200e00eSIan Dowse if (oinpp != NULL) 13195200e00eSIan Dowse *oinpp = NULL; 132057bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1321df8bae1dSRodney W. Grimes return (EINVAL); 1322df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1323df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1324df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1325df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 13265200e00eSIan Dowse laddr.s_addr = *laddrp; 13275200e00eSIan Dowse lport = *lportp; 13285200e00eSIan Dowse faddr = sin->sin_addr; 13295200e00eSIan Dowse fport = sin->sin_port; 13300895aec3SBjoern A. Zeeb 1331d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1332df8bae1dSRodney W. Grimes /* 1333df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1334df8bae1dSRodney W. Grimes * use the primary local address. 1335df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1336df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1337df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1338df8bae1dSRodney W. Grimes */ 1339413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1340cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1341413628a7SBjoern A. Zeeb faddr = 1342d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1343cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1344b89e82ddSJamie Gritton if (cred != NULL && 1345b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1346b89e82ddSJamie Gritton return (error); 13472d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1348cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1349d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 13502d9cfabaSRobert Watson IFF_BROADCAST) 1351d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1352603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1353cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 13542d9cfabaSRobert Watson } 1355df8bae1dSRodney W. Grimes } 13565200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1357d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1358df8bae1dSRodney W. Grimes /* 1359df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1360d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1361df8bae1dSRodney W. Grimes * address of that interface as our source address. 1362df8bae1dSRodney W. Grimes */ 13635200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1364df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1365df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1366df8bae1dSRodney W. Grimes struct ifnet *ifp; 1367df8bae1dSRodney W. Grimes 1368df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1369df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1370df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1371cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1372d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1373e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1374e691be70SDaniel Eischen (cred == NULL || 1375e691be70SDaniel Eischen prison_check_ip4(cred, 1376e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1377df8bae1dSRodney W. Grimes break; 1378e691be70SDaniel Eischen } 1379e691be70SDaniel Eischen if (ia == NULL) 1380d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1381e691be70SDaniel Eischen else { 13825200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1383d79fdd98SDaniel Eischen error = 0; 1384999f1343SGarrett Wollman } 1385cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1386d79fdd98SDaniel Eischen } 1387d79fdd98SDaniel Eischen } 138804215ed2SRandall Stewart if (error) 138904215ed2SRandall Stewart return (error); 13900895aec3SBjoern A. Zeeb } 1391*25102351SMike Karels if (lport != 0) { 1392*25102351SMike Karels oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 1393*25102351SMike Karels fport, laddr, lport, 0, NULL); 13945200e00eSIan Dowse if (oinp != NULL) { 13955200e00eSIan Dowse if (oinpp != NULL) 13965200e00eSIan Dowse *oinpp = oinp; 1397df8bae1dSRodney W. Grimes return (EADDRINUSE); 1398c3229e05SDavid Greenman } 1399*25102351SMike Karels } else { 1400*25102351SMike Karels struct sockaddr_in lsin, fsin; 1401*25102351SMike Karels 1402*25102351SMike Karels bzero(&lsin, sizeof(lsin)); 1403*25102351SMike Karels bzero(&fsin, sizeof(fsin)); 1404*25102351SMike Karels lsin.sin_family = AF_INET; 1405*25102351SMike Karels lsin.sin_addr = laddr; 1406*25102351SMike Karels fsin.sin_family = AF_INET; 1407*25102351SMike Karels fsin.sin_addr = faddr; 1408*25102351SMike Karels error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin, 1409*25102351SMike Karels &lport, (struct sockaddr *)& fsin, fport, cred, 1410*25102351SMike Karels INPLOOKUP_WILDCARD); 14115a903f8dSPierre Beyssac if (error) 14125a903f8dSPierre Beyssac return (error); 14135a903f8dSPierre Beyssac } 14145200e00eSIan Dowse *laddrp = laddr.s_addr; 14155200e00eSIan Dowse *lportp = lport; 14165200e00eSIan Dowse *faddrp = faddr.s_addr; 14175200e00eSIan Dowse *fportp = fport; 1418df8bae1dSRodney W. Grimes return (0); 1419df8bae1dSRodney W. Grimes } 1420df8bae1dSRodney W. Grimes 142126f9a767SRodney W. Grimes void 1422136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1423df8bae1dSRodney W. Grimes { 14246b348152SRobert Watson 14258501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1426fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1427df8bae1dSRodney W. Grimes 1428df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1429df8bae1dSRodney W. Grimes inp->inp_fport = 0; 143015bd2b43SDavid Greenman in_pcbrehash(inp); 1431df8bae1dSRodney W. Grimes } 143283e521ecSBjoern A. Zeeb #endif /* INET */ 1433df8bae1dSRodney W. Grimes 14344c7c478dSRobert Watson /* 143528696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1436c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 143728696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 143828696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 14394c7c478dSRobert Watson */ 144026f9a767SRodney W. Grimes void 1441136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1442df8bae1dSRodney W. Grimes { 14434c7c478dSRobert Watson 1444a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1445c0a211c5SRobert Watson 1446f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1447f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1448f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1449f3e7afe2SHans Petter Selasky #endif 14504c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 14514c7c478dSRobert Watson inp->inp_socket = NULL; 14524c7c478dSRobert Watson } 14534c7c478dSRobert Watson 1454c0a211c5SRobert Watson /* 145579bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 145679bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 145779bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 145852cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 145952cd27cbSRobert Watson * via a pcbgroup. 146079bdc6e5SRobert Watson * 146179bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 146279bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 146379bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 146479bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 146579bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 146679bdc6e5SRobert Watson * inpcb. 146779bdc6e5SRobert Watson * 146879bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 146979bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 147079bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 147179bdc6e5SRobert Watson * using in_pcbrele(). 1472c0a211c5SRobert Watson */ 147379bdc6e5SRobert Watson void 147479bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 14754c7c478dSRobert Watson { 1476df8bae1dSRodney W. Grimes 147779bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 147879bdc6e5SRobert Watson 147979bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 148079bdc6e5SRobert Watson } 148179bdc6e5SRobert Watson 148279bdc6e5SRobert Watson /* 148379bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 148479bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 148579bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 148679bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 148779bdc6e5SRobert Watson * 148879bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 148979bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 149079bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 149179bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 149279bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 149379bdc6e5SRobert Watson */ 149479bdc6e5SRobert Watson int 149579bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 149679bdc6e5SRobert Watson { 149779bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 149879bdc6e5SRobert Watson 149979bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 150079bdc6e5SRobert Watson 150179bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 150279bdc6e5SRobert Watson 1503df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1504df4e91d3SGleb Smirnoff /* 1505df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1506df4e91d3SGleb Smirnoff * this isn't the last reference. 1507df4e91d3SGleb Smirnoff */ 1508df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1509df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1510df4e91d3SGleb Smirnoff return (1); 1511df4e91d3SGleb Smirnoff } 151279bdc6e5SRobert Watson return (0); 1513df4e91d3SGleb Smirnoff } 151479bdc6e5SRobert Watson 151579bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 15163ee9c3c4SRandall Stewart #ifdef TCPHPTS 15173ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 15183ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 15193ee9c3c4SRandall Stewart /* 15203ee9c3c4SRandall Stewart * We should not be on the hpts at 15213ee9c3c4SRandall Stewart * this point in any form. we must 15223ee9c3c4SRandall Stewart * get the lock to be sure. 15233ee9c3c4SRandall Stewart */ 15243ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 15253ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 15263ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 15273ee9c3c4SRandall Stewart hpts, inp); 15283ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15293ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 15303ee9c3c4SRandall Stewart if (inp->inp_in_input) 15313ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 15323ee9c3c4SRandall Stewart hpts, inp); 15333ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15343ee9c3c4SRandall Stewart } 15353ee9c3c4SRandall Stewart #endif 153679bdc6e5SRobert Watson INP_RUNLOCK(inp); 153779bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 153879bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 153979bdc6e5SRobert Watson return (1); 154079bdc6e5SRobert Watson } 154179bdc6e5SRobert Watson 154279bdc6e5SRobert Watson int 154379bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 154479bdc6e5SRobert Watson { 154579bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 154679bdc6e5SRobert Watson 154779bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 154879bdc6e5SRobert Watson 154979bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 155079bdc6e5SRobert Watson 1551edd0e0b0SFabien Thomas if (refcount_release(&inp->inp_refcount) == 0) { 1552edd0e0b0SFabien Thomas /* 1553edd0e0b0SFabien Thomas * If the inpcb has been freed, let the caller know, even if 1554edd0e0b0SFabien Thomas * this isn't the last reference. 1555edd0e0b0SFabien Thomas */ 1556edd0e0b0SFabien Thomas if (inp->inp_flags2 & INP_FREED) { 1557edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1558edd0e0b0SFabien Thomas return (1); 1559edd0e0b0SFabien Thomas } 156079bdc6e5SRobert Watson return (0); 1561edd0e0b0SFabien Thomas } 156279bdc6e5SRobert Watson 156379bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 15643ee9c3c4SRandall Stewart #ifdef TCPHPTS 15653ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 15663ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 15673ee9c3c4SRandall Stewart /* 15683ee9c3c4SRandall Stewart * We should not be on the hpts at 15693ee9c3c4SRandall Stewart * this point in any form. we must 15703ee9c3c4SRandall Stewart * get the lock to be sure. 15713ee9c3c4SRandall Stewart */ 15723ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 15733ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 15743ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 15753ee9c3c4SRandall Stewart hpts, inp); 15763ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15773ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 15783ee9c3c4SRandall Stewart if (inp->inp_in_input) 15793ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 15803ee9c3c4SRandall Stewart hpts, inp); 15813ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15823ee9c3c4SRandall Stewart } 15833ee9c3c4SRandall Stewart #endif 158479bdc6e5SRobert Watson INP_WUNLOCK(inp); 158579bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 158679bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 158779bdc6e5SRobert Watson return (1); 158879bdc6e5SRobert Watson } 158979bdc6e5SRobert Watson 159079bdc6e5SRobert Watson /* 159179bdc6e5SRobert Watson * Temporary wrapper. 159279bdc6e5SRobert Watson */ 159379bdc6e5SRobert Watson int 159479bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 159579bdc6e5SRobert Watson { 159679bdc6e5SRobert Watson 159779bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 159879bdc6e5SRobert Watson } 159979bdc6e5SRobert Watson 1600ddece765SMatt Macy void 1601ddece765SMatt Macy in_pcblist_rele_rlocked(epoch_context_t ctx) 1602ddece765SMatt Macy { 1603ddece765SMatt Macy struct in_pcblist *il; 1604ddece765SMatt Macy struct inpcb *inp; 1605ddece765SMatt Macy struct inpcbinfo *pcbinfo; 1606ddece765SMatt Macy int i, n; 1607ddece765SMatt Macy 1608ddece765SMatt Macy il = __containerof(ctx, struct in_pcblist, il_epoch_ctx); 1609ddece765SMatt Macy pcbinfo = il->il_pcbinfo; 1610ddece765SMatt Macy n = il->il_count; 1611ddece765SMatt Macy INP_INFO_WLOCK(pcbinfo); 1612ddece765SMatt Macy for (i = 0; i < n; i++) { 1613ddece765SMatt Macy inp = il->il_inp_list[i]; 1614ddece765SMatt Macy INP_RLOCK(inp); 1615ddece765SMatt Macy if (!in_pcbrele_rlocked(inp)) 1616ddece765SMatt Macy INP_RUNLOCK(inp); 1617ddece765SMatt Macy } 1618ddece765SMatt Macy INP_INFO_WUNLOCK(pcbinfo); 1619ddece765SMatt Macy free(il, M_TEMP); 1620ddece765SMatt Macy } 1621ddece765SMatt Macy 1622addf2b20SMatt Macy static void 1623f09ee4fcSMatt Macy inpcbport_free(epoch_context_t ctx) 1624f09ee4fcSMatt Macy { 1625f09ee4fcSMatt Macy struct inpcbport *phd; 1626f09ee4fcSMatt Macy 1627f09ee4fcSMatt Macy phd = __containerof(ctx, struct inpcbport, phd_epoch_ctx); 1628f09ee4fcSMatt Macy free(phd, M_PCB); 1629f09ee4fcSMatt Macy } 1630f09ee4fcSMatt Macy 1631f09ee4fcSMatt Macy static void 1632addf2b20SMatt Macy in_pcbfree_deferred(epoch_context_t ctx) 1633addf2b20SMatt Macy { 1634addf2b20SMatt Macy struct inpcb *inp; 1635addf2b20SMatt Macy int released __unused; 1636addf2b20SMatt Macy 1637addf2b20SMatt Macy inp = __containerof(ctx, struct inpcb, inp_epoch_ctx); 1638addf2b20SMatt Macy 1639addf2b20SMatt Macy INP_WLOCK(inp); 1640c7ee62fcSAndrey V. Elsukov CURVNET_SET(inp->inp_vnet); 1641addf2b20SMatt Macy #ifdef INET 1642f9be0386SMatt Macy struct ip_moptions *imo = inp->inp_moptions; 1643addf2b20SMatt Macy inp->inp_moptions = NULL; 1644addf2b20SMatt Macy #endif 1645addf2b20SMatt Macy /* XXXRW: Do as much as possible here. */ 1646addf2b20SMatt Macy #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1647addf2b20SMatt Macy if (inp->inp_sp != NULL) 1648addf2b20SMatt Macy ipsec_delete_pcbpolicy(inp); 1649addf2b20SMatt Macy #endif 1650addf2b20SMatt Macy #ifdef INET6 1651f9be0386SMatt Macy struct ip6_moptions *im6o = NULL; 1652addf2b20SMatt Macy if (inp->inp_vflag & INP_IPV6PROTO) { 1653addf2b20SMatt Macy ip6_freepcbopts(inp->in6p_outputopts); 1654f9be0386SMatt Macy im6o = inp->in6p_moptions; 1655addf2b20SMatt Macy inp->in6p_moptions = NULL; 1656addf2b20SMatt Macy } 1657addf2b20SMatt Macy #endif 1658addf2b20SMatt Macy if (inp->inp_options) 1659addf2b20SMatt Macy (void)m_free(inp->inp_options); 1660addf2b20SMatt Macy inp->inp_vflag = 0; 1661addf2b20SMatt Macy crfree(inp->inp_cred); 1662addf2b20SMatt Macy #ifdef MAC 1663addf2b20SMatt Macy mac_inpcb_destroy(inp); 1664addf2b20SMatt Macy #endif 1665addf2b20SMatt Macy released = in_pcbrele_wlocked(inp); 1666addf2b20SMatt Macy MPASS(released); 1667f9be0386SMatt Macy #ifdef INET6 1668f9be0386SMatt Macy ip6_freemoptions(im6o); 1669f9be0386SMatt Macy #endif 1670f9be0386SMatt Macy #ifdef INET 1671f9be0386SMatt Macy inp_freemoptions(imo); 1672f9be0386SMatt Macy #endif 1673c7ee62fcSAndrey V. Elsukov CURVNET_RESTORE(); 1674addf2b20SMatt Macy } 1675addf2b20SMatt Macy 167679bdc6e5SRobert Watson /* 167779bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 167879bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 167979bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 168079bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 168179bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 168279bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 168379bdc6e5SRobert Watson * the pcbinfo lock is held. 168479bdc6e5SRobert Watson */ 168579bdc6e5SRobert Watson void 168679bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 168779bdc6e5SRobert Watson { 1688f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1689f42a83f2SMatt Macy 169079bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 169145a48d07SJonathan T. Looney KASSERT((inp->inp_flags2 & INP_FREED) == 0, 169245a48d07SJonathan T. Looney ("%s: called twice for pcb %p", __func__, inp)); 169345a48d07SJonathan T. Looney if (inp->inp_flags2 & INP_FREED) { 169445a48d07SJonathan T. Looney INP_WUNLOCK(inp); 169545a48d07SJonathan T. Looney return; 169645a48d07SJonathan T. Looney } 169745a48d07SJonathan T. Looney 169879bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1699f42a83f2SMatt Macy INP_LIST_WLOCK(pcbinfo); 1700f42a83f2SMatt Macy in_pcbremlists(inp); 1701f42a83f2SMatt Macy INP_LIST_WUNLOCK(pcbinfo); 1702fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1703addf2b20SMatt Macy /* mark as destruction in progress */ 1704f42a83f2SMatt Macy inp->inp_flags2 |= INP_FREED; 1705cb6bb230SMatt Macy INP_WUNLOCK(inp); 17062a4bd982SGleb Smirnoff NET_EPOCH_CALL(in_pcbfree_deferred, &inp->inp_epoch_ctx); 170728696211SRobert Watson } 170828696211SRobert Watson 170928696211SRobert Watson /* 1710c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1711c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1712c0a211c5SRobert Watson * 1713c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1714c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1715c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1716c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1717c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1718c0a211c5SRobert Watson * in_pcbdetach(). 1719c0a211c5SRobert Watson * 1720c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1721c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 172210702a28SRobert Watson */ 172310702a28SRobert Watson void 172410702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 172510702a28SRobert Watson { 172610702a28SRobert Watson 17278501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 17286573d758SMatt Macy #ifdef INVARIANTS 17296573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 17306573d758SMatt Macy MPASS(inp->inp_refcount > 1); 17316573d758SMatt Macy #endif 173210702a28SRobert Watson 1733fa046d87SRobert Watson /* 1734fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1735fa046d87SRobert Watson * the hash lock...? 1736fa046d87SRobert Watson */ 1737ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1738111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 173910702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 174010702a28SRobert Watson 1741fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 17421a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 1743b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 1744b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 1745b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 1746b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 17472a4bd982SGleb Smirnoff NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx); 174810702a28SRobert Watson } 1749fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1750111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 175152cd27cbSRobert Watson #ifdef PCBGROUP 175252cd27cbSRobert Watson in_pcbgroup_remove(inp); 175352cd27cbSRobert Watson #endif 175410702a28SRobert Watson } 175510702a28SRobert Watson } 175610702a28SRobert Watson 175767107f45SBjoern A. Zeeb #ifdef INET 175854d642bbSRobert Watson /* 175954d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 176054d642bbSRobert Watson */ 176126ef6ac4SDon Lewis struct sockaddr * 1762136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 176326ef6ac4SDon Lewis { 176426ef6ac4SDon Lewis struct sockaddr_in *sin; 176526ef6ac4SDon Lewis 17661ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1767a163d034SWarner Losh M_WAITOK | M_ZERO); 176826ef6ac4SDon Lewis sin->sin_family = AF_INET; 176926ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 177026ef6ac4SDon Lewis sin->sin_addr = *addr_p; 177126ef6ac4SDon Lewis sin->sin_port = port; 177226ef6ac4SDon Lewis 177326ef6ac4SDon Lewis return (struct sockaddr *)sin; 177426ef6ac4SDon Lewis } 177526ef6ac4SDon Lewis 1776117bcae7SGarrett Wollman int 177754d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1778df8bae1dSRodney W. Grimes { 1779136d4f1cSRobert Watson struct inpcb *inp; 178026ef6ac4SDon Lewis struct in_addr addr; 178126ef6ac4SDon Lewis in_port_t port; 178242fa505bSDavid Greenman 1783fdc984f7STor Egge inp = sotoinpcb(so); 178454d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 17856466b28aSRobert Watson 1786a69042a5SRobert Watson INP_RLOCK(inp); 178726ef6ac4SDon Lewis port = inp->inp_lport; 178826ef6ac4SDon Lewis addr = inp->inp_laddr; 1789a69042a5SRobert Watson INP_RUNLOCK(inp); 179042fa505bSDavid Greenman 179126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1792117bcae7SGarrett Wollman return 0; 1793df8bae1dSRodney W. Grimes } 1794df8bae1dSRodney W. Grimes 1795117bcae7SGarrett Wollman int 179654d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1797df8bae1dSRodney W. Grimes { 1798136d4f1cSRobert Watson struct inpcb *inp; 179926ef6ac4SDon Lewis struct in_addr addr; 180026ef6ac4SDon Lewis in_port_t port; 180142fa505bSDavid Greenman 1802fdc984f7STor Egge inp = sotoinpcb(so); 180354d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 18046466b28aSRobert Watson 1805a69042a5SRobert Watson INP_RLOCK(inp); 180626ef6ac4SDon Lewis port = inp->inp_fport; 180726ef6ac4SDon Lewis addr = inp->inp_faddr; 1808a69042a5SRobert Watson INP_RUNLOCK(inp); 180942fa505bSDavid Greenman 181026ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1811117bcae7SGarrett Wollman return 0; 1812df8bae1dSRodney W. Grimes } 1813df8bae1dSRodney W. Grimes 181426f9a767SRodney W. Grimes void 1815136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1816136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1817d1c54148SJesper Skriver { 1818f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1819d1c54148SJesper Skriver 18203dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1821b872626dSMatt Macy CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 18228501a69cSRobert Watson INP_WLOCK(inp); 1823d1c54148SJesper Skriver #ifdef INET6 1824f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 18258501a69cSRobert Watson INP_WUNLOCK(inp); 1826d1c54148SJesper Skriver continue; 1827f76fcf6dSJeffrey Hsu } 1828d1c54148SJesper Skriver #endif 1829d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1830f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 18318501a69cSRobert Watson INP_WUNLOCK(inp); 1832d1c54148SJesper Skriver continue; 1833d1c54148SJesper Skriver } 18343dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 18358501a69cSRobert Watson INP_WUNLOCK(inp); 1836f76fcf6dSJeffrey Hsu } 18373dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1838d1c54148SJesper Skriver } 1839d1c54148SJesper Skriver 1840e43cc4aeSHajimu UMEMOTO void 1841136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1842e43cc4aeSHajimu UMEMOTO { 1843e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 184459854ecfSHans Petter Selasky struct in_multi *inm; 184559854ecfSHans Petter Selasky struct in_mfilter *imf; 1846e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1847e43cc4aeSHajimu UMEMOTO 1848ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1849b872626dSMatt Macy CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 18508501a69cSRobert Watson INP_WLOCK(inp); 1851e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1852e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1853e43cc4aeSHajimu UMEMOTO imo != NULL) { 1854e43cc4aeSHajimu UMEMOTO /* 1855e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1856e43cc4aeSHajimu UMEMOTO * detached. 1857e43cc4aeSHajimu UMEMOTO */ 1858e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1859e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1860e43cc4aeSHajimu UMEMOTO 1861e43cc4aeSHajimu UMEMOTO /* 1862e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1863e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1864cb6bb230SMatt Macy * 1865cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1866e43cc4aeSHajimu UMEMOTO */ 186759854ecfSHans Petter Selasky restart: 186859854ecfSHans Petter Selasky IP_MFILTER_FOREACH(imf, &imo->imo_head) { 186959854ecfSHans Petter Selasky if ((inm = imf->imf_inm) == NULL) 187059854ecfSHans Petter Selasky continue; 187159854ecfSHans Petter Selasky if (inm->inm_ifp != ifp) 187259854ecfSHans Petter Selasky continue; 187359854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 1874f3e1324bSStephen Hurd IN_MULTI_LOCK_ASSERT(); 187559854ecfSHans Petter Selasky in_leavegroup_locked(inm, NULL); 187659854ecfSHans Petter Selasky ip_mfilter_free(imf); 187759854ecfSHans Petter Selasky goto restart; 1878e43cc4aeSHajimu UMEMOTO } 1879e43cc4aeSHajimu UMEMOTO } 18808501a69cSRobert Watson INP_WUNLOCK(inp); 1881e43cc4aeSHajimu UMEMOTO } 1882ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1883e43cc4aeSHajimu UMEMOTO } 1884e43cc4aeSHajimu UMEMOTO 1885df8bae1dSRodney W. Grimes /* 1886fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1887fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1888c3229e05SDavid Greenman */ 1889d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1890df8bae1dSRodney W. Grimes struct inpcb * 1891136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 189268e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1893df8bae1dSRodney W. Grimes { 1894136d4f1cSRobert Watson struct inpcb *inp; 1895d5e8a67eSHajimu UMEMOTO #ifdef INET6 1896d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1897d5e8a67eSHajimu UMEMOTO #else 1898d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1899d5e8a67eSHajimu UMEMOTO #endif 1900d5e8a67eSHajimu UMEMOTO int wildcard; 19017bc4aca7SDavid Greenman 190268e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 190368e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 190468e0d7e0SRobert Watson 1905fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 19061b73ca0bSSam Leffler 190768e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1908c3229e05SDavid Greenman struct inpcbhead *head; 1909c3229e05SDavid Greenman /* 1910c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1911c3229e05SDavid Greenman * matches the local address and port we're looking for. 1912c3229e05SDavid Greenman */ 1913712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1914712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1915b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 1916cfa1ca9dSYoshinobu Inoue #ifdef INET6 1917413628a7SBjoern A. Zeeb /* XXX inp locking */ 1918369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1919cfa1ca9dSYoshinobu Inoue continue; 1920cfa1ca9dSYoshinobu Inoue #endif 1921c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1922c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1923c3229e05SDavid Greenman inp->inp_lport == lport) { 1924c3229e05SDavid Greenman /* 1925413628a7SBjoern A. Zeeb * Found? 1926c3229e05SDavid Greenman */ 1927413628a7SBjoern A. Zeeb if (cred == NULL || 19280304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 19290304c731SJamie Gritton inp->inp_cred->cr_prison)) 1930c3229e05SDavid Greenman return (inp); 1931df8bae1dSRodney W. Grimes } 1932c3229e05SDavid Greenman } 1933c3229e05SDavid Greenman /* 1934c3229e05SDavid Greenman * Not found. 1935c3229e05SDavid Greenman */ 1936c3229e05SDavid Greenman return (NULL); 1937c3229e05SDavid Greenman } else { 1938c3229e05SDavid Greenman struct inpcbporthead *porthash; 1939c3229e05SDavid Greenman struct inpcbport *phd; 1940c3229e05SDavid Greenman struct inpcb *match = NULL; 1941c3229e05SDavid Greenman /* 1942c3229e05SDavid Greenman * Best fit PCB lookup. 1943c3229e05SDavid Greenman * 1944c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1945c3229e05SDavid Greenman * port hash list. 1946c3229e05SDavid Greenman */ 1947712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1948712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1949b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 1950c3229e05SDavid Greenman if (phd->phd_port == lport) 1951c3229e05SDavid Greenman break; 1952c3229e05SDavid Greenman } 1953c3229e05SDavid Greenman if (phd != NULL) { 1954c3229e05SDavid Greenman /* 1955c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1956c3229e05SDavid Greenman * fit. 1957c3229e05SDavid Greenman */ 1958b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1959c3229e05SDavid Greenman wildcard = 0; 1960413628a7SBjoern A. Zeeb if (cred != NULL && 19610304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 19620304c731SJamie Gritton cred->cr_prison)) 1963413628a7SBjoern A. Zeeb continue; 1964cfa1ca9dSYoshinobu Inoue #ifdef INET6 1965413628a7SBjoern A. Zeeb /* XXX inp locking */ 1966369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1967cfa1ca9dSYoshinobu Inoue continue; 1968d5e8a67eSHajimu UMEMOTO /* 1969d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1970d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1971d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1972d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1973d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1974d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1975d5e8a67eSHajimu UMEMOTO * 1976d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1977d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1978d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1979d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1980d5e8a67eSHajimu UMEMOTO */ 1981d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1982d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1983cfa1ca9dSYoshinobu Inoue #endif 1984c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1985c3229e05SDavid Greenman wildcard++; 198615bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 198715bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 198815bd2b43SDavid Greenman wildcard++; 198915bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 199015bd2b43SDavid Greenman continue; 199115bd2b43SDavid Greenman } else { 199215bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 199315bd2b43SDavid Greenman wildcard++; 199415bd2b43SDavid Greenman } 1995df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1996df8bae1dSRodney W. Grimes match = inp; 1997df8bae1dSRodney W. Grimes matchwild = wildcard; 1998413628a7SBjoern A. Zeeb if (matchwild == 0) 1999df8bae1dSRodney W. Grimes break; 2000df8bae1dSRodney W. Grimes } 2001df8bae1dSRodney W. Grimes } 20023dbdc25cSDavid Greenman } 2003df8bae1dSRodney W. Grimes return (match); 2004df8bae1dSRodney W. Grimes } 2005c3229e05SDavid Greenman } 2006d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 200715bd2b43SDavid Greenman 20081a43cff9SSean Bruno static struct inpcb * 20091a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 20101a43cff9SSean Bruno const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr, 20111a43cff9SSean Bruno uint16_t fport, int lookupflags) 20121a43cff9SSean Bruno { 20138be02ee4SMark Johnston struct inpcb *local_wild; 20141a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 20151a43cff9SSean Bruno struct inpcblbgroup *grp; 20168be02ee4SMark Johnston uint32_t idx; 20171a43cff9SSean Bruno 20181a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 20191a43cff9SSean Bruno 20209d2877fcSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[ 20219d2877fcSMark Johnston INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 20221a43cff9SSean Bruno 20231a43cff9SSean Bruno /* 20241a43cff9SSean Bruno * Order of socket selection: 20251a43cff9SSean Bruno * 1. non-wild. 20261a43cff9SSean Bruno * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD). 20271a43cff9SSean Bruno * 20281a43cff9SSean Bruno * NOTE: 20291a43cff9SSean Bruno * - Load balanced group does not contain jailed sockets 20301a43cff9SSean Bruno * - Load balanced group does not contain IPv4 mapped INET6 wild sockets 20311a43cff9SSean Bruno */ 20328be02ee4SMark Johnston local_wild = NULL; 203354af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 20341a43cff9SSean Bruno #ifdef INET6 20351a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 20361a43cff9SSean Bruno continue; 20371a43cff9SSean Bruno #endif 20388be02ee4SMark Johnston if (grp->il_lport != lport) 20398be02ee4SMark Johnston continue; 20401a43cff9SSean Bruno 20418be02ee4SMark Johnston idx = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, lport, fport) % 20428be02ee4SMark Johnston grp->il_inpcnt; 20438be02ee4SMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) 20441a43cff9SSean Bruno return (grp->il_inp[idx]); 20451a43cff9SSean Bruno if (grp->il_laddr.s_addr == INADDR_ANY && 20468be02ee4SMark Johnston (lookupflags & INPLOOKUP_WILDCARD) != 0) 20471a43cff9SSean Bruno local_wild = grp->il_inp[idx]; 20481a43cff9SSean Bruno } 20491a43cff9SSean Bruno return (local_wild); 20501a43cff9SSean Bruno } 20511a43cff9SSean Bruno 205252cd27cbSRobert Watson #ifdef PCBGROUP 205352cd27cbSRobert Watson /* 205452cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 205552cd27cbSRobert Watson */ 205652cd27cbSRobert Watson static struct inpcb * 205752cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 205852cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 205952cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 206052cd27cbSRobert Watson { 206152cd27cbSRobert Watson struct inpcbhead *head; 206252cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 206352cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 20647fb2986fSJonathan T. Looney bool locked; 206552cd27cbSRobert Watson 206652cd27cbSRobert Watson /* 206752cd27cbSRobert Watson * First look for an exact match. 206852cd27cbSRobert Watson */ 206952cd27cbSRobert Watson tmpinp = NULL; 207052cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 207152cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 207252cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 2073feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 207452cd27cbSRobert Watson #ifdef INET6 207552cd27cbSRobert Watson /* XXX inp locking */ 207652cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 207752cd27cbSRobert Watson continue; 207852cd27cbSRobert Watson #endif 207952cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 208052cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 208152cd27cbSRobert Watson inp->inp_fport == fport && 208252cd27cbSRobert Watson inp->inp_lport == lport) { 208352cd27cbSRobert Watson /* 208452cd27cbSRobert Watson * XXX We should be able to directly return 208552cd27cbSRobert Watson * the inp here, without any checks. 208652cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 208752cd27cbSRobert Watson */ 208852cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 208952cd27cbSRobert Watson goto found; 209052cd27cbSRobert Watson if (tmpinp == NULL) 209152cd27cbSRobert Watson tmpinp = inp; 209252cd27cbSRobert Watson } 209352cd27cbSRobert Watson } 209452cd27cbSRobert Watson if (tmpinp != NULL) { 209552cd27cbSRobert Watson inp = tmpinp; 209652cd27cbSRobert Watson goto found; 209752cd27cbSRobert Watson } 209852cd27cbSRobert Watson 20990a100a6fSAdrian Chadd #ifdef RSS 21000a100a6fSAdrian Chadd /* 21010a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 21020a100a6fSAdrian Chadd * match for an RSS-local socket. 21030a100a6fSAdrian Chadd */ 21040a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 21050a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 21060a100a6fSAdrian Chadd #ifdef INET6 21070a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 21080a100a6fSAdrian Chadd #endif 21090a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 21100a100a6fSAdrian Chadd struct inpcbhead *head; 21110a100a6fSAdrian Chadd int injail; 21120a100a6fSAdrian Chadd 21130a100a6fSAdrian Chadd /* 21140a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 21150a100a6fSAdrian Chadd * 1. jailed, non-wild. 21160a100a6fSAdrian Chadd * 2. jailed, wild. 21170a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 21180a100a6fSAdrian Chadd * 4. non-jailed, wild. 21190a100a6fSAdrian Chadd */ 21200a100a6fSAdrian Chadd 21210a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 21220a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 2123feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 21240a100a6fSAdrian Chadd #ifdef INET6 21250a100a6fSAdrian Chadd /* XXX inp locking */ 21260a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 21270a100a6fSAdrian Chadd continue; 21280a100a6fSAdrian Chadd #endif 21290a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 21300a100a6fSAdrian Chadd inp->inp_lport != lport) 21310a100a6fSAdrian Chadd continue; 21320a100a6fSAdrian Chadd 21330a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 21340a100a6fSAdrian Chadd if (injail) { 21350a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 21360a100a6fSAdrian Chadd &laddr) != 0) 21370a100a6fSAdrian Chadd continue; 21380a100a6fSAdrian Chadd } else { 21390a100a6fSAdrian Chadd if (local_exact != NULL) 21400a100a6fSAdrian Chadd continue; 21410a100a6fSAdrian Chadd } 21420a100a6fSAdrian Chadd 21430a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 21440a100a6fSAdrian Chadd if (injail) 21450a100a6fSAdrian Chadd goto found; 21460a100a6fSAdrian Chadd else 21470a100a6fSAdrian Chadd local_exact = inp; 21480a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 21490a100a6fSAdrian Chadd #ifdef INET6 21500a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 21510a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 21520a100a6fSAdrian Chadd local_wild_mapped = inp; 21530a100a6fSAdrian Chadd else 21540a100a6fSAdrian Chadd #endif 21550a100a6fSAdrian Chadd if (injail) 21560a100a6fSAdrian Chadd jail_wild = inp; 21570a100a6fSAdrian Chadd else 21580a100a6fSAdrian Chadd local_wild = inp; 21590a100a6fSAdrian Chadd } 21600a100a6fSAdrian Chadd } /* LIST_FOREACH */ 21610a100a6fSAdrian Chadd 21620a100a6fSAdrian Chadd inp = jail_wild; 21630a100a6fSAdrian Chadd if (inp == NULL) 21640a100a6fSAdrian Chadd inp = local_exact; 21650a100a6fSAdrian Chadd if (inp == NULL) 21660a100a6fSAdrian Chadd inp = local_wild; 21670a100a6fSAdrian Chadd #ifdef INET6 21680a100a6fSAdrian Chadd if (inp == NULL) 21690a100a6fSAdrian Chadd inp = local_wild_mapped; 21700a100a6fSAdrian Chadd #endif 21710a100a6fSAdrian Chadd if (inp != NULL) 21720a100a6fSAdrian Chadd goto found; 21730a100a6fSAdrian Chadd } 21740a100a6fSAdrian Chadd #endif 21750a100a6fSAdrian Chadd 217652cd27cbSRobert Watson /* 217752cd27cbSRobert Watson * Then look for a wildcard match, if requested. 217852cd27cbSRobert Watson */ 217952cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 218052cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 218152cd27cbSRobert Watson #ifdef INET6 218252cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 218352cd27cbSRobert Watson #endif 218452cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 218552cd27cbSRobert Watson struct inpcbhead *head; 218652cd27cbSRobert Watson int injail; 218752cd27cbSRobert Watson 218852cd27cbSRobert Watson /* 218952cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 219052cd27cbSRobert Watson * 1. jailed, non-wild. 219152cd27cbSRobert Watson * 2. jailed, wild. 219252cd27cbSRobert Watson * 3. non-jailed, non-wild. 219352cd27cbSRobert Watson * 4. non-jailed, wild. 219452cd27cbSRobert Watson */ 219552cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 219652cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 2197feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 219852cd27cbSRobert Watson #ifdef INET6 219952cd27cbSRobert Watson /* XXX inp locking */ 220052cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 220152cd27cbSRobert Watson continue; 220252cd27cbSRobert Watson #endif 220352cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 220452cd27cbSRobert Watson inp->inp_lport != lport) 220552cd27cbSRobert Watson continue; 220652cd27cbSRobert Watson 220752cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 220852cd27cbSRobert Watson if (injail) { 220952cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 221052cd27cbSRobert Watson &laddr) != 0) 221152cd27cbSRobert Watson continue; 221252cd27cbSRobert Watson } else { 221352cd27cbSRobert Watson if (local_exact != NULL) 221452cd27cbSRobert Watson continue; 221552cd27cbSRobert Watson } 221652cd27cbSRobert Watson 221752cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 221852cd27cbSRobert Watson if (injail) 221952cd27cbSRobert Watson goto found; 222052cd27cbSRobert Watson else 222152cd27cbSRobert Watson local_exact = inp; 222252cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 222352cd27cbSRobert Watson #ifdef INET6 222452cd27cbSRobert Watson /* XXX inp locking, NULL check */ 222552cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 222652cd27cbSRobert Watson local_wild_mapped = inp; 222752cd27cbSRobert Watson else 222883e521ecSBjoern A. Zeeb #endif 222952cd27cbSRobert Watson if (injail) 223052cd27cbSRobert Watson jail_wild = inp; 223152cd27cbSRobert Watson else 223252cd27cbSRobert Watson local_wild = inp; 223352cd27cbSRobert Watson } 223452cd27cbSRobert Watson } /* LIST_FOREACH */ 223552cd27cbSRobert Watson inp = jail_wild; 223652cd27cbSRobert Watson if (inp == NULL) 223752cd27cbSRobert Watson inp = local_exact; 223852cd27cbSRobert Watson if (inp == NULL) 223952cd27cbSRobert Watson inp = local_wild; 224052cd27cbSRobert Watson #ifdef INET6 224152cd27cbSRobert Watson if (inp == NULL) 224252cd27cbSRobert Watson inp = local_wild_mapped; 224383e521ecSBjoern A. Zeeb #endif 224452cd27cbSRobert Watson if (inp != NULL) 224552cd27cbSRobert Watson goto found; 224652cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 224752cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 224852cd27cbSRobert Watson return (NULL); 224952cd27cbSRobert Watson 225052cd27cbSRobert Watson found: 22517fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 2252b9a9e8e9SNavdeep Parhar locked = INP_TRY_WLOCK(inp); 22537fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 2254b9a9e8e9SNavdeep Parhar locked = INP_TRY_RLOCK(inp); 22557fb2986fSJonathan T. Looney else 22567fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 2257483305b9SMatt Macy if (__predict_false(locked && (inp->inp_flags2 & INP_FREED))) { 2258483305b9SMatt Macy if (lookupflags & INPLOOKUP_WLOCKPCB) 2259483305b9SMatt Macy INP_WUNLOCK(inp); 2260483305b9SMatt Macy else 2261483305b9SMatt Macy INP_RUNLOCK(inp); 2262483305b9SMatt Macy return (NULL); 2263483305b9SMatt Macy } else if (!locked) 226452cd27cbSRobert Watson in_pcbref(inp); 226552cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 22667fb2986fSJonathan T. Looney if (!locked) { 226752cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 226852cd27cbSRobert Watson INP_WLOCK(inp); 226952cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 227052cd27cbSRobert Watson return (NULL); 22717fb2986fSJonathan T. Looney } else { 227252cd27cbSRobert Watson INP_RLOCK(inp); 227352cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 227452cd27cbSRobert Watson return (NULL); 22757fb2986fSJonathan T. Looney } 22767fb2986fSJonathan T. Looney } 22777fb2986fSJonathan T. Looney #ifdef INVARIANTS 22787fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 22797fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 22807fb2986fSJonathan T. Looney else 22817fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 22827fb2986fSJonathan T. Looney #endif 228352cd27cbSRobert Watson return (inp); 228452cd27cbSRobert Watson } 228552cd27cbSRobert Watson #endif /* PCBGROUP */ 228652cd27cbSRobert Watson 228715bd2b43SDavid Greenman /* 2288fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2289fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 2290fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 229115bd2b43SDavid Greenman */ 2292fa046d87SRobert Watson static struct inpcb * 2293fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 229468e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2295136d4f1cSRobert Watson struct ifnet *ifp) 229615bd2b43SDavid Greenman { 229715bd2b43SDavid Greenman struct inpcbhead *head; 2298413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 229915bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 230015bd2b43SDavid Greenman 230168e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 230268e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2303d797164aSGleb Smirnoff INP_HASH_LOCK_ASSERT(pcbinfo); 2304d797164aSGleb Smirnoff 230515bd2b43SDavid Greenman /* 230615bd2b43SDavid Greenman * First look for an exact match. 230715bd2b43SDavid Greenman */ 2308413628a7SBjoern A. Zeeb tmpinp = NULL; 2309712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 2310712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 2311b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2312cfa1ca9dSYoshinobu Inoue #ifdef INET6 2313413628a7SBjoern A. Zeeb /* XXX inp locking */ 2314369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2315cfa1ca9dSYoshinobu Inoue continue; 2316cfa1ca9dSYoshinobu Inoue #endif 23176d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2318ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2319ca98b82cSDavid Greenman inp->inp_fport == fport && 2320413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 2321413628a7SBjoern A. Zeeb /* 2322413628a7SBjoern A. Zeeb * XXX We should be able to directly return 2323413628a7SBjoern A. Zeeb * the inp here, without any checks. 2324413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 2325413628a7SBjoern A. Zeeb */ 23260304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 2327c3229e05SDavid Greenman return (inp); 2328413628a7SBjoern A. Zeeb if (tmpinp == NULL) 2329413628a7SBjoern A. Zeeb tmpinp = inp; 2330c3229e05SDavid Greenman } 2331413628a7SBjoern A. Zeeb } 2332413628a7SBjoern A. Zeeb if (tmpinp != NULL) 2333413628a7SBjoern A. Zeeb return (tmpinp); 2334e3fd5ffdSRobert Watson 2335e3fd5ffdSRobert Watson /* 23361a43cff9SSean Bruno * Then look in lb group (for wildcard match). 23371a43cff9SSean Bruno */ 2338d9ff5789SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 23391a43cff9SSean Bruno inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr, 23401a43cff9SSean Bruno fport, lookupflags); 2341d9ff5789SMark Johnston if (inp != NULL) 23421a43cff9SSean Bruno return (inp); 23431a43cff9SSean Bruno } 23441a43cff9SSean Bruno 23451a43cff9SSean Bruno /* 2346e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 2347e3fd5ffdSRobert Watson */ 234868e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2349413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 2350e3fd5ffdSRobert Watson #ifdef INET6 2351cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 2352e3fd5ffdSRobert Watson #endif 2353413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 2354413628a7SBjoern A. Zeeb int injail; 2355413628a7SBjoern A. Zeeb 2356413628a7SBjoern A. Zeeb /* 2357413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2358413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2359413628a7SBjoern A. Zeeb * 2. jailed, wild. 2360413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2361413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2362413628a7SBjoern A. Zeeb */ 23636d6a026bSDavid Greenman 2364712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2365712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2366b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2367cfa1ca9dSYoshinobu Inoue #ifdef INET6 2368413628a7SBjoern A. Zeeb /* XXX inp locking */ 2369369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2370cfa1ca9dSYoshinobu Inoue continue; 2371cfa1ca9dSYoshinobu Inoue #endif 2372413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2373413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2374413628a7SBjoern A. Zeeb continue; 2375413628a7SBjoern A. Zeeb 23760304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2377413628a7SBjoern A. Zeeb if (injail) { 2378b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2379b89e82ddSJamie Gritton &laddr) != 0) 2380413628a7SBjoern A. Zeeb continue; 2381413628a7SBjoern A. Zeeb } else { 2382413628a7SBjoern A. Zeeb if (local_exact != NULL) 2383413628a7SBjoern A. Zeeb continue; 2384413628a7SBjoern A. Zeeb } 2385413628a7SBjoern A. Zeeb 2386413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2387413628a7SBjoern A. Zeeb if (injail) 2388c3229e05SDavid Greenman return (inp); 2389413628a7SBjoern A. Zeeb else 2390413628a7SBjoern A. Zeeb local_exact = inp; 2391413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2392e3fd5ffdSRobert Watson #ifdef INET6 2393413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23945cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2395cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2396cfa1ca9dSYoshinobu Inoue else 239783e521ecSBjoern A. Zeeb #endif 2398413628a7SBjoern A. Zeeb if (injail) 2399413628a7SBjoern A. Zeeb jail_wild = inp; 2400413628a7SBjoern A. Zeeb else 24016d6a026bSDavid Greenman local_wild = inp; 24026d6a026bSDavid Greenman } 2403413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2404413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2405413628a7SBjoern A. Zeeb return (jail_wild); 2406413628a7SBjoern A. Zeeb if (local_exact != NULL) 2407413628a7SBjoern A. Zeeb return (local_exact); 2408413628a7SBjoern A. Zeeb if (local_wild != NULL) 2409c3229e05SDavid Greenman return (local_wild); 2410413628a7SBjoern A. Zeeb #ifdef INET6 2411413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2412413628a7SBjoern A. Zeeb return (local_wild_mapped); 241383e521ecSBjoern A. Zeeb #endif 241468e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2415413628a7SBjoern A. Zeeb 24166d6a026bSDavid Greenman return (NULL); 241715bd2b43SDavid Greenman } 2418fa046d87SRobert Watson 2419fa046d87SRobert Watson /* 2420fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2421fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2422fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2423fa046d87SRobert Watson */ 2424fa046d87SRobert Watson static struct inpcb * 2425fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2426fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2427fa046d87SRobert Watson struct ifnet *ifp) 2428fa046d87SRobert Watson { 2429fa046d87SRobert Watson struct inpcb *inp; 2430fa046d87SRobert Watson 2431fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2432fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 2433fa046d87SRobert Watson if (inp != NULL) { 2434fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 2435fa046d87SRobert Watson INP_WLOCK(inp); 24363d348772SMatt Macy if (__predict_false(inp->inp_flags2 & INP_FREED)) { 24373d348772SMatt Macy INP_WUNLOCK(inp); 24383d348772SMatt Macy inp = NULL; 24393d348772SMatt Macy } 24403d348772SMatt Macy } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 2441fa046d87SRobert Watson INP_RLOCK(inp); 24423d348772SMatt Macy if (__predict_false(inp->inp_flags2 & INP_FREED)) { 24433d348772SMatt Macy INP_RUNLOCK(inp); 24443d348772SMatt Macy inp = NULL; 24457fb2986fSJonathan T. Looney } 24463d348772SMatt Macy } else 24473d348772SMatt Macy panic("%s: locking bug", __func__); 24487fb2986fSJonathan T. Looney #ifdef INVARIANTS 24493d348772SMatt Macy if (inp != NULL) { 24507fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 24517fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 24527fb2986fSJonathan T. Looney else 24537fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 24543d348772SMatt Macy } 24557fb2986fSJonathan T. Looney #endif 24563d348772SMatt Macy } 2457d797164aSGleb Smirnoff 2458fa046d87SRobert Watson return (inp); 2459fa046d87SRobert Watson } 2460fa046d87SRobert Watson 2461fa046d87SRobert Watson /* 2462d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2463d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 246452cd27cbSRobert Watson * 246552cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 2466fa046d87SRobert Watson */ 2467fa046d87SRobert Watson struct inpcb * 2468fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2469fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2470fa046d87SRobert Watson { 24717527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 247252cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 247352cd27cbSRobert Watson #endif 2474fa046d87SRobert Watson 2475fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2476fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2477fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2478fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 2479fa046d87SRobert Watson 24807527624eSRobert Watson /* 24817527624eSRobert Watson * When not using RSS, use connection groups in preference to the 24827527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 24837527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 24847527624eSRobert Watson * in software. 24857527624eSRobert Watson * 24867527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 24877527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 24887527624eSRobert Watson * in software. 24897527624eSRobert Watson */ 24907527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 249152cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 249252cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 249352cd27cbSRobert Watson fport); 249452cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 249552cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 249652cd27cbSRobert Watson } 249752cd27cbSRobert Watson #endif 2498fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2499fa046d87SRobert Watson lookupflags, ifp)); 2500fa046d87SRobert Watson } 2501d3c1f003SRobert Watson 2502d3c1f003SRobert Watson struct inpcb * 2503d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2504d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2505d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2506d3c1f003SRobert Watson { 250752cd27cbSRobert Watson #ifdef PCBGROUP 250852cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 250952cd27cbSRobert Watson #endif 2510d3c1f003SRobert Watson 2511d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2512d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2513d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2514d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2515d3c1f003SRobert Watson 251652cd27cbSRobert Watson #ifdef PCBGROUP 25177527624eSRobert Watson /* 25187527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 25197527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 25207527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 25217527624eSRobert Watson * using RSS. 25227527624eSRobert Watson * 25237527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 25247527624eSRobert Watson */ 25257527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 25267527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 252752cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 252852cd27cbSRobert Watson m->m_pkthdr.flowid); 252952cd27cbSRobert Watson if (pcbgroup != NULL) 253052cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 253152cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 25327527624eSRobert Watson #ifndef RSS 253352cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 253452cd27cbSRobert Watson fport); 253552cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 253652cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 25377527624eSRobert Watson #endif 253852cd27cbSRobert Watson } 253952cd27cbSRobert Watson #endif 2540d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2541d3c1f003SRobert Watson lookupflags, ifp)); 2542d3c1f003SRobert Watson } 254367107f45SBjoern A. Zeeb #endif /* INET */ 254415bd2b43SDavid Greenman 25457bc4aca7SDavid Greenman /* 2546c3229e05SDavid Greenman * Insert PCB onto various hash lists. 25477bc4aca7SDavid Greenman */ 254852cd27cbSRobert Watson static int 2549fe1274eeSMichael Tuexen in_pcbinshash_internal(struct inpcb *inp, struct mbuf *m) 255015bd2b43SDavid Greenman { 2551c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2552c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2553c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2554c3229e05SDavid Greenman struct inpcbport *phd; 2555cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 25561a43cff9SSean Bruno int so_options; 255715bd2b43SDavid Greenman 25588501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2559fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2560fa046d87SRobert Watson 2561111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2562111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2563602cc7f1SRobert Watson 2564cfa1ca9dSYoshinobu Inoue #ifdef INET6 2565cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 25661b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2567cfa1ca9dSYoshinobu Inoue else 256883e521ecSBjoern A. Zeeb #endif 2569cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2570cfa1ca9dSYoshinobu Inoue 2571712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2572712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 257315bd2b43SDavid Greenman 2574712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2575712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2576c3229e05SDavid Greenman 2577c3229e05SDavid Greenman /* 25781a43cff9SSean Bruno * Add entry to load balance group. 25791a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 25801a43cff9SSean Bruno */ 25811a43cff9SSean Bruno so_options = inp_so_options(inp); 25821a43cff9SSean Bruno if (so_options & SO_REUSEPORT_LB) { 25831a43cff9SSean Bruno int ret = in_pcbinslbgrouphash(inp); 25841a43cff9SSean Bruno if (ret) { 25851a43cff9SSean Bruno /* pcb lb group malloc fail (ret=ENOBUFS). */ 25861a43cff9SSean Bruno return (ret); 25871a43cff9SSean Bruno } 25881a43cff9SSean Bruno } 25891a43cff9SSean Bruno 25901a43cff9SSean Bruno /* 2591c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2592c3229e05SDavid Greenman */ 2593b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2594c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2595c3229e05SDavid Greenman break; 2596c3229e05SDavid Greenman } 2597c3229e05SDavid Greenman /* 2598c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2599c3229e05SDavid Greenman */ 2600c3229e05SDavid Greenman if (phd == NULL) { 26011ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2602c3229e05SDavid Greenman if (phd == NULL) { 2603c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2604c3229e05SDavid Greenman } 2605f09ee4fcSMatt Macy bzero(&phd->phd_epoch_ctx, sizeof(struct epoch_context)); 2606c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2607b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2608b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2609c3229e05SDavid Greenman } 2610c3229e05SDavid Greenman inp->inp_phd = phd; 2611b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2612b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2613111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 261452cd27cbSRobert Watson #ifdef PCBGROUP 2615fe1274eeSMichael Tuexen if (m != NULL) { 2616fe1274eeSMichael Tuexen in_pcbgroup_update_mbuf(inp, m); 2617fe1274eeSMichael Tuexen } else { 261852cd27cbSRobert Watson in_pcbgroup_update(inp); 2619fe1274eeSMichael Tuexen } 262052cd27cbSRobert Watson #endif 2621c3229e05SDavid Greenman return (0); 262215bd2b43SDavid Greenman } 262315bd2b43SDavid Greenman 262452cd27cbSRobert Watson int 262552cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 262652cd27cbSRobert Watson { 262752cd27cbSRobert Watson 2628fe1274eeSMichael Tuexen return (in_pcbinshash_internal(inp, NULL)); 262952cd27cbSRobert Watson } 263052cd27cbSRobert Watson 263152cd27cbSRobert Watson int 2632fe1274eeSMichael Tuexen in_pcbinshash_mbuf(struct inpcb *inp, struct mbuf *m) 263352cd27cbSRobert Watson { 263452cd27cbSRobert Watson 2635fe1274eeSMichael Tuexen return (in_pcbinshash_internal(inp, m)); 263652cd27cbSRobert Watson } 263752cd27cbSRobert Watson 263852cd27cbSRobert Watson /* 2639c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2640c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2641c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2642c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2643c3229e05SDavid Greenman */ 264415bd2b43SDavid Greenman void 2645d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 264615bd2b43SDavid Greenman { 264759daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 264815bd2b43SDavid Greenman struct inpcbhead *head; 2649cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 265015bd2b43SDavid Greenman 26518501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2652fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2653fa046d87SRobert Watson 2654111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2655111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2656602cc7f1SRobert Watson 2657cfa1ca9dSYoshinobu Inoue #ifdef INET6 2658cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 26591b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2660cfa1ca9dSYoshinobu Inoue else 266183e521ecSBjoern A. Zeeb #endif 2662cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2663cfa1ca9dSYoshinobu Inoue 2664712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2665712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 266615bd2b43SDavid Greenman 2667b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2668b872626dSMatt Macy CK_LIST_INSERT_HEAD(head, inp, inp_hash); 266952cd27cbSRobert Watson 267052cd27cbSRobert Watson #ifdef PCBGROUP 267152cd27cbSRobert Watson if (m != NULL) 267252cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 267352cd27cbSRobert Watson else 267452cd27cbSRobert Watson in_pcbgroup_update(inp); 267552cd27cbSRobert Watson #endif 2676c3229e05SDavid Greenman } 2677c3229e05SDavid Greenman 2678d3c1f003SRobert Watson void 2679d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2680d3c1f003SRobert Watson { 2681d3c1f003SRobert Watson 2682d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2683d3c1f003SRobert Watson } 2684d3c1f003SRobert Watson 2685c3229e05SDavid Greenman /* 2686c3229e05SDavid Greenman * Remove PCB from various lists. 2687c3229e05SDavid Greenman */ 26886d888973SRobert Watson static void 2689136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2690c3229e05SDavid Greenman { 269159daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 269259daba27SSam Leffler 26938501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2694ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 269559daba27SSam Leffler 269659daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2697111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2698c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2699c3229e05SDavid Greenman 2700fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 27011a43cff9SSean Bruno 27021a43cff9SSean Bruno /* XXX: Only do if SO_REUSEPORT_LB set? */ 27031a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 27041a43cff9SSean Bruno 2705b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2706b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 2707b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 2708b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 27092a4bd982SGleb Smirnoff NET_EPOCH_CALL(inpcbport_free, &phd->phd_epoch_ctx); 2710c3229e05SDavid Greenman } 2711fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2712111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2713c3229e05SDavid Greenman } 2714b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_list); 271559daba27SSam Leffler pcbinfo->ipi_count--; 271652cd27cbSRobert Watson #ifdef PCBGROUP 271752cd27cbSRobert Watson in_pcbgroup_remove(inp); 271852cd27cbSRobert Watson #endif 271915bd2b43SDavid Greenman } 272075c13541SPoul-Henning Kamp 2721a557af22SRobert Watson /* 272284cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 272384cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 272484cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 272584cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 272684cc0778SGeorge V. Neville-Neil */ 272784cc0778SGeorge V. Neville-Neil void 272884cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 272984cc0778SGeorge V. Neville-Neil { 273084cc0778SGeorge V. Neville-Neil 2731fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 273284cc0778SGeorge V. Neville-Neil return; 273384cc0778SGeorge V. Neville-Neil } 273484cc0778SGeorge V. Neville-Neil 273584cc0778SGeorge V. Neville-Neil /* 2736a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2737a557af22SRobert Watson * label change into the in_pcb for the socket. 2738a557af22SRobert Watson */ 2739a557af22SRobert Watson void 2740136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2741a557af22SRobert Watson { 2742a557af22SRobert Watson #ifdef MAC 2743a557af22SRobert Watson struct inpcb *inp; 2744a557af22SRobert Watson 27454c7c478dSRobert Watson inp = sotoinpcb(so); 27464c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2747602cc7f1SRobert Watson 27488501a69cSRobert Watson INP_WLOCK(inp); 2749310e7cebSRobert Watson SOCK_LOCK(so); 2750a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2751310e7cebSRobert Watson SOCK_UNLOCK(so); 27528501a69cSRobert Watson INP_WUNLOCK(inp); 2753a557af22SRobert Watson #endif 2754a557af22SRobert Watson } 27555f311da2SMike Silbersack 27565f311da2SMike Silbersack /* 2757ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2758ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2759ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2760ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2761ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 27625f311da2SMike Silbersack */ 2763aae49dd3SBjoern A. Zeeb static void 2764136d4f1cSRobert Watson ipport_tick(void *xtp) 27655f311da2SMike Silbersack { 27668b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2767ad3a630fSRobert Watson 27685ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 27698b615593SMarko Zec VNET_FOREACH(vnet_iter) { 27708b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 27718b615593SMarko Zec if (V_ipport_tcpallocs <= 27728b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2773603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2774603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2775ad3a630fSRobert Watson } else 2776603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2777603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 27788b615593SMarko Zec CURVNET_RESTORE(); 27798b615593SMarko Zec } 27805ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 27815f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 27825f311da2SMike Silbersack } 2783497057eeSRobert Watson 2784aae49dd3SBjoern A. Zeeb static void 2785aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2786aae49dd3SBjoern A. Zeeb { 2787aae49dd3SBjoern A. Zeeb 2788aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2789aae49dd3SBjoern A. Zeeb } 2790aae49dd3SBjoern A. Zeeb 2791aae49dd3SBjoern A. Zeeb /* 2792aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2793aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2794aae49dd3SBjoern A. Zeeb */ 2795aae49dd3SBjoern A. Zeeb static void 2796aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2797aae49dd3SBjoern A. Zeeb { 2798aae49dd3SBjoern A. Zeeb 2799aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2800fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2801aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2802aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2803aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2804aae49dd3SBjoern A. Zeeb } 2805aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2806aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2807aae49dd3SBjoern A. Zeeb 28083d585327SKip Macy void 28093d585327SKip Macy inp_wlock(struct inpcb *inp) 28103d585327SKip Macy { 28113d585327SKip Macy 28128501a69cSRobert Watson INP_WLOCK(inp); 28133d585327SKip Macy } 28143d585327SKip Macy 28153d585327SKip Macy void 28163d585327SKip Macy inp_wunlock(struct inpcb *inp) 28173d585327SKip Macy { 28183d585327SKip Macy 28198501a69cSRobert Watson INP_WUNLOCK(inp); 28203d585327SKip Macy } 28213d585327SKip Macy 28223d585327SKip Macy void 28233d585327SKip Macy inp_rlock(struct inpcb *inp) 28243d585327SKip Macy { 28253d585327SKip Macy 2826a69042a5SRobert Watson INP_RLOCK(inp); 28273d585327SKip Macy } 28283d585327SKip Macy 28293d585327SKip Macy void 28303d585327SKip Macy inp_runlock(struct inpcb *inp) 28313d585327SKip Macy { 28323d585327SKip Macy 2833a69042a5SRobert Watson INP_RUNLOCK(inp); 28343d585327SKip Macy } 28353d585327SKip Macy 2836c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 28373d585327SKip Macy void 2838e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 28393d585327SKip Macy { 28403d585327SKip Macy 28418501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 28423d585327SKip Macy } 28433d585327SKip Macy 28443d585327SKip Macy void 2845e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 28463d585327SKip Macy { 28473d585327SKip Macy 28483d585327SKip Macy INP_UNLOCK_ASSERT(inp); 28493d585327SKip Macy } 28503d585327SKip Macy #endif 28513d585327SKip Macy 28529378e437SKip Macy void 28539378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 28549378e437SKip Macy { 28559378e437SKip Macy struct inpcb *inp; 28569378e437SKip Macy 2857ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 2858b872626dSMatt Macy CK_LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 28599378e437SKip Macy INP_WLOCK(inp); 28609378e437SKip Macy func(inp, arg); 28619378e437SKip Macy INP_WUNLOCK(inp); 28629378e437SKip Macy } 2863ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 28649378e437SKip Macy } 28659378e437SKip Macy 28669378e437SKip Macy struct socket * 28679378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 28689378e437SKip Macy { 28699378e437SKip Macy 28709378e437SKip Macy INP_WLOCK_ASSERT(inp); 28719378e437SKip Macy return (inp->inp_socket); 28729378e437SKip Macy } 28739378e437SKip Macy 28749378e437SKip Macy struct tcpcb * 28759378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 28769378e437SKip Macy { 28779378e437SKip Macy 28789378e437SKip Macy INP_WLOCK_ASSERT(inp); 28799378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 28809378e437SKip Macy } 28819378e437SKip Macy 28829378e437SKip Macy int 28839378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 28849378e437SKip Macy { 28859378e437SKip Macy 28869378e437SKip Macy return (inp->inp_ip_tos); 28879378e437SKip Macy } 28889378e437SKip Macy 28899378e437SKip Macy void 28909378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 28919378e437SKip Macy { 28929378e437SKip Macy 28939378e437SKip Macy inp->inp_ip_tos = val; 28949378e437SKip Macy } 28959378e437SKip Macy 28969378e437SKip Macy void 2897df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 28989d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 28999378e437SKip Macy { 29009378e437SKip Macy 29019d29c635SKip Macy INP_LOCK_ASSERT(inp); 2902df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2903df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 29049378e437SKip Macy *lp = inp->inp_lport; 29059378e437SKip Macy *fp = inp->inp_fport; 29069378e437SKip Macy } 29079378e437SKip Macy 2908dd0e6c38SKip Macy struct inpcb * 2909dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2910dd0e6c38SKip Macy { 2911dd0e6c38SKip Macy 2912dd0e6c38SKip Macy return (sotoinpcb(so)); 2913dd0e6c38SKip Macy } 2914dd0e6c38SKip Macy 2915dd0e6c38SKip Macy struct tcpcb * 2916dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2917dd0e6c38SKip Macy { 2918dd0e6c38SKip Macy 2919dd0e6c38SKip Macy return (sototcpcb(so)); 2920dd0e6c38SKip Macy } 2921dd0e6c38SKip Macy 2922cc65eb4eSGleb Smirnoff /* 2923cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2924cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2925cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2926cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2927cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2928cc65eb4eSGleb Smirnoff * hidden from users. 2929cc65eb4eSGleb Smirnoff */ 2930cc65eb4eSGleb Smirnoff void 2931cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2932cc65eb4eSGleb Smirnoff { 2933cc65eb4eSGleb Smirnoff 293479db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2935cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2936cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2937cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2938cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2939cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 29403a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2941cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2942cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2943cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2944cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2945cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2946cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2947cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2948cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2949cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2950cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2951cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2952cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2953cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2954cc65eb4eSGleb Smirnoff } 2955cc65eb4eSGleb Smirnoff 2956497057eeSRobert Watson #ifdef DDB 2957497057eeSRobert Watson static void 2958497057eeSRobert Watson db_print_indent(int indent) 2959497057eeSRobert Watson { 2960497057eeSRobert Watson int i; 2961497057eeSRobert Watson 2962497057eeSRobert Watson for (i = 0; i < indent; i++) 2963497057eeSRobert Watson db_printf(" "); 2964497057eeSRobert Watson } 2965497057eeSRobert Watson 2966497057eeSRobert Watson static void 2967497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2968497057eeSRobert Watson { 2969497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2970497057eeSRobert Watson 2971497057eeSRobert Watson db_print_indent(indent); 2972497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2973497057eeSRobert Watson 2974497057eeSRobert Watson indent += 2; 2975497057eeSRobert Watson 297603dc38a4SRobert Watson #ifdef INET6 2977dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2978497057eeSRobert Watson /* IPv6. */ 2979497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2980497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 298183e521ecSBjoern A. Zeeb } else 298203dc38a4SRobert Watson #endif 298383e521ecSBjoern A. Zeeb { 2984497057eeSRobert Watson /* IPv4. */ 2985497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2986497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2987497057eeSRobert Watson } 2988497057eeSRobert Watson db_print_indent(indent); 2989497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2990497057eeSRobert Watson ntohs(inc->inc_lport)); 2991497057eeSRobert Watson db_print_indent(indent); 2992497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2993497057eeSRobert Watson ntohs(inc->inc_fport)); 2994497057eeSRobert Watson } 2995497057eeSRobert Watson 2996497057eeSRobert Watson static void 2997497057eeSRobert Watson db_print_inpflags(int inp_flags) 2998497057eeSRobert Watson { 2999497057eeSRobert Watson int comma; 3000497057eeSRobert Watson 3001497057eeSRobert Watson comma = 0; 3002497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 3003497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 3004497057eeSRobert Watson comma = 1; 3005497057eeSRobert Watson } 3006497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 3007497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 3008497057eeSRobert Watson comma = 1; 3009497057eeSRobert Watson } 3010497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 3011497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 3012497057eeSRobert Watson comma = 1; 3013497057eeSRobert Watson } 3014dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 3015dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 3016dce33a45SErmal Luçi comma = 1; 3017dce33a45SErmal Luçi } 3018497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 3019497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 3020497057eeSRobert Watson comma = 1; 3021497057eeSRobert Watson } 3022497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 3023497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 3024497057eeSRobert Watson comma = 1; 3025497057eeSRobert Watson } 3026497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 3027497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 3028497057eeSRobert Watson comma = 1; 3029497057eeSRobert Watson } 3030497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 3031497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 3032497057eeSRobert Watson comma = 1; 3033497057eeSRobert Watson } 3034497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 3035497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 3036497057eeSRobert Watson comma = 1; 3037497057eeSRobert Watson } 3038497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 3039497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 3040497057eeSRobert Watson comma = 1; 3041497057eeSRobert Watson } 3042497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 3043497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 3044497057eeSRobert Watson comma = 1; 3045497057eeSRobert Watson } 3046497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 3047497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 3048497057eeSRobert Watson comma = 1; 3049497057eeSRobert Watson } 30503cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 30513cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 30523cca425bSMichael Tuexen comma = 1; 30533cca425bSMichael Tuexen } 3054497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 3055497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3056497057eeSRobert Watson comma = 1; 3057497057eeSRobert Watson } 3058497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 3059497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3060497057eeSRobert Watson comma = 1; 3061497057eeSRobert Watson } 3062497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 3063497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3064497057eeSRobert Watson comma = 1; 3065497057eeSRobert Watson } 3066497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 3067497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3068497057eeSRobert Watson comma = 1; 3069497057eeSRobert Watson } 3070497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 3071497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3072497057eeSRobert Watson comma = 1; 3073497057eeSRobert Watson } 3074497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 3075497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3076497057eeSRobert Watson comma = 1; 3077497057eeSRobert Watson } 3078497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 3079497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3080497057eeSRobert Watson comma = 1; 3081497057eeSRobert Watson } 3082497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 3083497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3084497057eeSRobert Watson comma = 1; 3085497057eeSRobert Watson } 3086497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 3087497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3088497057eeSRobert Watson comma = 1; 3089497057eeSRobert Watson } 3090ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 3091ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 3092ad71fe3cSRobert Watson comma = 1; 3093ad71fe3cSRobert Watson } 3094ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 3095ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3096ad71fe3cSRobert Watson comma = 1; 3097ad71fe3cSRobert Watson } 3098ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 3099ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 3100ad71fe3cSRobert Watson comma = 1; 3101ad71fe3cSRobert Watson } 3102ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 3103ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3104ad71fe3cSRobert Watson comma = 1; 3105ad71fe3cSRobert Watson } 3106497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 3107497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3108497057eeSRobert Watson comma = 1; 3109497057eeSRobert Watson } 3110497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 3111497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 3112497057eeSRobert Watson comma = 1; 3113497057eeSRobert Watson } 3114497057eeSRobert Watson } 3115497057eeSRobert Watson 3116497057eeSRobert Watson static void 3117497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 3118497057eeSRobert Watson { 3119497057eeSRobert Watson int comma; 3120497057eeSRobert Watson 3121497057eeSRobert Watson comma = 0; 3122497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3123497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3124497057eeSRobert Watson comma = 1; 3125497057eeSRobert Watson } 3126497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3127497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3128497057eeSRobert Watson comma = 1; 3129497057eeSRobert Watson } 3130497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3131497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3132497057eeSRobert Watson comma = 1; 3133497057eeSRobert Watson } 3134497057eeSRobert Watson } 3135497057eeSRobert Watson 31366d888973SRobert Watson static void 3137497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3138497057eeSRobert Watson { 3139497057eeSRobert Watson 3140497057eeSRobert Watson db_print_indent(indent); 3141497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3142497057eeSRobert Watson 3143497057eeSRobert Watson indent += 2; 3144497057eeSRobert Watson 3145497057eeSRobert Watson db_print_indent(indent); 3146497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3147497057eeSRobert Watson 3148497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3149497057eeSRobert Watson 3150497057eeSRobert Watson db_print_indent(indent); 3151497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 3152497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 3153497057eeSRobert Watson 3154497057eeSRobert Watson db_print_indent(indent); 3155497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3156497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3157497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3158497057eeSRobert Watson db_printf(")\n"); 3159497057eeSRobert Watson 3160497057eeSRobert Watson db_print_indent(indent); 3161497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3162497057eeSRobert Watson inp->inp_vflag); 3163497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3164497057eeSRobert Watson db_printf(")\n"); 3165497057eeSRobert Watson 3166497057eeSRobert Watson db_print_indent(indent); 3167497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3168497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3169497057eeSRobert Watson 3170497057eeSRobert Watson db_print_indent(indent); 3171497057eeSRobert Watson #ifdef INET6 3172497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3173497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3174497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3175497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3176497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3177497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3178497057eeSRobert Watson inp->in6p_hops); 3179497057eeSRobert Watson } else 3180497057eeSRobert Watson #endif 3181497057eeSRobert Watson { 3182497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3183497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3184497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3185497057eeSRobert Watson } 3186497057eeSRobert Watson 3187497057eeSRobert Watson db_print_indent(indent); 3188497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3189497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3190497057eeSRobert Watson } 3191497057eeSRobert Watson 3192497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3193497057eeSRobert Watson { 3194497057eeSRobert Watson struct inpcb *inp; 3195497057eeSRobert Watson 3196497057eeSRobert Watson if (!have_addr) { 3197497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3198497057eeSRobert Watson return; 3199497057eeSRobert Watson } 3200497057eeSRobert Watson inp = (struct inpcb *)addr; 3201497057eeSRobert Watson 3202497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3203497057eeSRobert Watson } 320483e521ecSBjoern A. Zeeb #endif /* DDB */ 3205f3e7afe2SHans Petter Selasky 3206f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3207f3e7afe2SHans Petter Selasky /* 3208f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3209f3e7afe2SHans Petter Selasky * if any. 3210f3e7afe2SHans Petter Selasky */ 3211f3e7afe2SHans Petter Selasky int 3212f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3213f3e7afe2SHans Petter Selasky { 3214f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3215f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 321620abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3217f3e7afe2SHans Petter Selasky }; 3218f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3219f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3220f3e7afe2SHans Petter Selasky int error; 3221f3e7afe2SHans Petter Selasky 3222f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3223f3e7afe2SHans Petter Selasky if (mst == NULL) 3224f3e7afe2SHans Petter Selasky return (EINVAL); 3225f3e7afe2SHans Petter Selasky 3226f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3227f3e7afe2SHans Petter Selasky if (ifp == NULL) 3228f3e7afe2SHans Petter Selasky return (EINVAL); 3229f3e7afe2SHans Petter Selasky 3230f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 3231f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3232f3e7afe2SHans Petter Selasky } else { 3233f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 3234f3e7afe2SHans Petter Selasky } 3235f3e7afe2SHans Petter Selasky return (error); 3236f3e7afe2SHans Petter Selasky } 3237f3e7afe2SHans Petter Selasky 3238f3e7afe2SHans Petter Selasky /* 3239f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3240f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3241f3e7afe2SHans Petter Selasky */ 3242f3e7afe2SHans Petter Selasky int 3243f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3244f3e7afe2SHans Petter Selasky { 3245f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3246f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3247f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3248f3e7afe2SHans Petter Selasky int error; 3249f3e7afe2SHans Petter Selasky 3250f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3251f3e7afe2SHans Petter Selasky if (mst == NULL) 3252f3e7afe2SHans Petter Selasky return (EINVAL); 3253f3e7afe2SHans Petter Selasky 3254f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3255f3e7afe2SHans Petter Selasky if (ifp == NULL) 3256f3e7afe2SHans Petter Selasky return (EINVAL); 3257f3e7afe2SHans Petter Selasky 3258f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 3259f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3260f3e7afe2SHans Petter Selasky } else { 3261f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 3262f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3263f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3264f3e7afe2SHans Petter Selasky } 3265f3e7afe2SHans Petter Selasky return (error); 3266f3e7afe2SHans Petter Selasky } 3267f3e7afe2SHans Petter Selasky 3268f3e7afe2SHans Petter Selasky /* 326995ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 327095ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 327195ed5015SHans Petter Selasky */ 327295ed5015SHans Petter Selasky int 327395ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 327495ed5015SHans Petter Selasky { 327595ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 327695ed5015SHans Petter Selasky struct m_snd_tag *mst; 327795ed5015SHans Petter Selasky struct ifnet *ifp; 327895ed5015SHans Petter Selasky int error; 327995ed5015SHans Petter Selasky 328095ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 328195ed5015SHans Petter Selasky if (mst == NULL) 328295ed5015SHans Petter Selasky return (EINVAL); 328395ed5015SHans Petter Selasky 328495ed5015SHans Petter Selasky ifp = mst->ifp; 328595ed5015SHans Petter Selasky if (ifp == NULL) 328695ed5015SHans Petter Selasky return (EINVAL); 328795ed5015SHans Petter Selasky 328895ed5015SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) 328995ed5015SHans Petter Selasky return (EOPNOTSUPP); 329095ed5015SHans Petter Selasky 329195ed5015SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 329295ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 329395ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 329495ed5015SHans Petter Selasky return (error); 329595ed5015SHans Petter Selasky } 329695ed5015SHans Petter Selasky 329795ed5015SHans Petter Selasky /* 3298f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3299f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3300f3e7afe2SHans Petter Selasky */ 3301f3e7afe2SHans Petter Selasky int 3302f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 330320abea66SRandall Stewart uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 330420abea66SRandall Stewart 3305f3e7afe2SHans Petter Selasky { 3306f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 330795ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 330895ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3309f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3310f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 331198085baeSAndrew Gallatin .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3312f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 331320abea66SRandall Stewart .rate_limit.flags = M_NOWAIT, 3314f3e7afe2SHans Petter Selasky }; 3315f3e7afe2SHans Petter Selasky int error; 3316f3e7afe2SHans Petter Selasky 3317f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3318f3e7afe2SHans Petter Selasky 331920abea66SRandall Stewart if (*st != NULL) 3320f3e7afe2SHans Petter Selasky return (EINVAL); 3321f3e7afe2SHans Petter Selasky 3322f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 3323f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3324f3e7afe2SHans Petter Selasky } else { 3325f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 332620abea66SRandall Stewart 3327903c4ee6SXin LI #ifdef INET 332820abea66SRandall Stewart if (error == 0) { 332920abea66SRandall Stewart counter_u64_add(rate_limit_set_ok, 1); 333020abea66SRandall Stewart counter_u64_add(rate_limit_active, 1); 333120abea66SRandall Stewart } else 333220abea66SRandall Stewart counter_u64_add(rate_limit_alloc_fail, 1); 3333903c4ee6SXin LI #endif 3334f3e7afe2SHans Petter Selasky } 3335f3e7afe2SHans Petter Selasky return (error); 3336f3e7afe2SHans Petter Selasky } 3337f3e7afe2SHans Petter Selasky 333820abea66SRandall Stewart void 333920abea66SRandall Stewart in_pcbdetach_tag(struct ifnet *ifp, struct m_snd_tag *mst) 334020abea66SRandall Stewart { 334120abea66SRandall Stewart if (ifp == NULL) 334220abea66SRandall Stewart return; 334320abea66SRandall Stewart 334420abea66SRandall Stewart /* 334520abea66SRandall Stewart * If the device was detached while we still had reference(s) 334620abea66SRandall Stewart * on the ifp, we assume if_snd_tag_free() was replaced with 334720abea66SRandall Stewart * stubs. 334820abea66SRandall Stewart */ 334920abea66SRandall Stewart ifp->if_snd_tag_free(mst); 335020abea66SRandall Stewart 335120abea66SRandall Stewart /* release reference count on network interface */ 335220abea66SRandall Stewart if_rele(ifp); 3353903c4ee6SXin LI #ifdef INET 335420abea66SRandall Stewart counter_u64_add(rate_limit_active, -1); 3355903c4ee6SXin LI #endif 335620abea66SRandall Stewart } 335720abea66SRandall Stewart 3358f3e7afe2SHans Petter Selasky /* 3359f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3360f3e7afe2SHans Petter Selasky * if any: 3361f3e7afe2SHans Petter Selasky */ 3362f3e7afe2SHans Petter Selasky void 3363f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3364f3e7afe2SHans Petter Selasky { 3365f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3366f3e7afe2SHans Petter Selasky 3367f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3368f3e7afe2SHans Petter Selasky 3369f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3370f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3371f3e7afe2SHans Petter Selasky 3372f3e7afe2SHans Petter Selasky if (mst == NULL) 3373f3e7afe2SHans Petter Selasky return; 3374f3e7afe2SHans Petter Selasky 3375fb3bc596SJohn Baldwin m_snd_tag_rele(mst); 3376f3e7afe2SHans Petter Selasky } 3377f3e7afe2SHans Petter Selasky 337820abea66SRandall Stewart int 337920abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 338020abea66SRandall Stewart { 338120abea66SRandall Stewart int error; 338220abea66SRandall Stewart 338320abea66SRandall Stewart /* 338420abea66SRandall Stewart * If the existing send tag is for the wrong interface due to 338520abea66SRandall Stewart * a route change, first drop the existing tag. Set the 338620abea66SRandall Stewart * CHANGED flag so that we will keep trying to allocate a new 338720abea66SRandall Stewart * tag if we fail to allocate one this time. 338820abea66SRandall Stewart */ 338920abea66SRandall Stewart if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 339020abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 339120abea66SRandall Stewart inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 339220abea66SRandall Stewart } 339320abea66SRandall Stewart 339420abea66SRandall Stewart /* 339520abea66SRandall Stewart * NOTE: When attaching to a network interface a reference is 339620abea66SRandall Stewart * made to ensure the network interface doesn't go away until 339720abea66SRandall Stewart * all ratelimit connections are gone. The network interface 339820abea66SRandall Stewart * pointers compared below represent valid network interfaces, 339920abea66SRandall Stewart * except when comparing towards NULL. 340020abea66SRandall Stewart */ 340120abea66SRandall Stewart if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 340220abea66SRandall Stewart error = 0; 340320abea66SRandall Stewart } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 340420abea66SRandall Stewart if (inp->inp_snd_tag != NULL) 340520abea66SRandall Stewart in_pcbdetach_txrtlmt(inp); 340620abea66SRandall Stewart error = 0; 340720abea66SRandall Stewart } else if (inp->inp_snd_tag == NULL) { 340820abea66SRandall Stewart /* 340920abea66SRandall Stewart * In order to utilize packet pacing with RSS, we need 341020abea66SRandall Stewart * to wait until there is a valid RSS hash before we 341120abea66SRandall Stewart * can proceed: 341220abea66SRandall Stewart */ 341320abea66SRandall Stewart if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 341420abea66SRandall Stewart error = EAGAIN; 341520abea66SRandall Stewart } else { 341620abea66SRandall Stewart error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 341720abea66SRandall Stewart mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 341820abea66SRandall Stewart } 341920abea66SRandall Stewart } else { 342020abea66SRandall Stewart error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 342120abea66SRandall Stewart } 342220abea66SRandall Stewart if (error == 0 || error == EOPNOTSUPP) 342320abea66SRandall Stewart inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 342420abea66SRandall Stewart 342520abea66SRandall Stewart return (error); 342620abea66SRandall Stewart } 342720abea66SRandall Stewart 3428f3e7afe2SHans Petter Selasky /* 3429f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3430f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3431f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3432f3e7afe2SHans Petter Selasky */ 3433f3e7afe2SHans Petter Selasky void 3434f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3435f3e7afe2SHans Petter Selasky { 3436f3e7afe2SHans Petter Selasky struct socket *socket; 3437f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3438f3e7afe2SHans Petter Selasky bool did_upgrade; 3439f3e7afe2SHans Petter Selasky int error; 3440f3e7afe2SHans Petter Selasky 3441f3e7afe2SHans Petter Selasky if (inp == NULL) 3442f3e7afe2SHans Petter Selasky return; 3443f3e7afe2SHans Petter Selasky 3444f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3445f3e7afe2SHans Petter Selasky if (socket == NULL) 3446f3e7afe2SHans Petter Selasky return; 3447f3e7afe2SHans Petter Selasky 3448f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3449f3e7afe2SHans Petter Selasky /* 3450f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3451f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3452f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3453f3e7afe2SHans Petter Selasky * write lock. 3454f3e7afe2SHans Petter Selasky */ 3455f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3456f3e7afe2SHans Petter Selasky return; 3457f3e7afe2SHans Petter Selasky did_upgrade = 1; 3458f3e7afe2SHans Petter Selasky } else { 3459f3e7afe2SHans Petter Selasky did_upgrade = 0; 3460f3e7afe2SHans Petter Selasky } 3461f3e7afe2SHans Petter Selasky 3462f3e7afe2SHans Petter Selasky /* 3463f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3464f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3465f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3466f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3467f3e7afe2SHans Petter Selasky */ 3468f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3469f3e7afe2SHans Petter Selasky 347020abea66SRandall Stewart error = in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3471fb3bc596SJohn Baldwin 3472f3e7afe2SHans Petter Selasky if (did_upgrade) 3473f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3474f3e7afe2SHans Petter Selasky } 3475f3e7afe2SHans Petter Selasky 3476f3e7afe2SHans Petter Selasky /* 3477f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3478f3e7afe2SHans Petter Selasky */ 3479f3e7afe2SHans Petter Selasky void 3480f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3481f3e7afe2SHans Petter Selasky { 3482f3e7afe2SHans Petter Selasky bool did_upgrade; 3483f3e7afe2SHans Petter Selasky 3484f3e7afe2SHans Petter Selasky if (inp == NULL) 3485f3e7afe2SHans Petter Selasky return; 3486f3e7afe2SHans Petter Selasky 3487f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3488f3e7afe2SHans Petter Selasky return; 3489f3e7afe2SHans Petter Selasky 3490f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3491f3e7afe2SHans Petter Selasky /* 3492f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3493f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3494f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3495f3e7afe2SHans Petter Selasky * write lock. 3496f3e7afe2SHans Petter Selasky */ 3497f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3498f3e7afe2SHans Petter Selasky return; 3499f3e7afe2SHans Petter Selasky did_upgrade = 1; 3500f3e7afe2SHans Petter Selasky } else { 3501f3e7afe2SHans Petter Selasky did_upgrade = 0; 3502f3e7afe2SHans Petter Selasky } 3503f3e7afe2SHans Petter Selasky 3504f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3505f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3506f3e7afe2SHans Petter Selasky 3507f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3508f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3509f3e7afe2SHans Petter Selasky 3510f3e7afe2SHans Petter Selasky if (did_upgrade) 3511f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3512f3e7afe2SHans Petter Selasky } 351320abea66SRandall Stewart 3514903c4ee6SXin LI #ifdef INET 351520abea66SRandall Stewart static void 351620abea66SRandall Stewart rl_init(void *st) 351720abea66SRandall Stewart { 351820abea66SRandall Stewart rate_limit_active = counter_u64_alloc(M_WAITOK); 351920abea66SRandall Stewart rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 352020abea66SRandall Stewart rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 352120abea66SRandall Stewart } 352220abea66SRandall Stewart 352320abea66SRandall Stewart SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3524903c4ee6SXin LI #endif 3525f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3526