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> 89df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 90340c35deSJonathan Lemon #include <netinet/tcp_var.h> 913ee9c3c4SRandall Stewart #ifdef TCPHPTS 923ee9c3c4SRandall Stewart #include <netinet/tcp_hpts.h> 933ee9c3c4SRandall Stewart #endif 945f311da2SMike Silbersack #include <netinet/udp.h> 955f311da2SMike Silbersack #include <netinet/udp_var.h> 9667107f45SBjoern A. Zeeb #endif 9767107f45SBjoern A. Zeeb #ifdef INET 9867107f45SBjoern A. Zeeb #include <netinet/in_var.h> 9967107f45SBjoern A. Zeeb #endif 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 */ 106cfa1ca9dSYoshinobu Inoue 107fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 108b9234fafSSam Leffler 109aed55708SRobert Watson #include <security/mac/mac_framework.h> 110aed55708SRobert Watson 1111a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 1121a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 1131a43cff9SSean Bruno 114aae49dd3SBjoern A. Zeeb static struct callout ipport_tick_callout; 115aae49dd3SBjoern A. Zeeb 116101f9fc8SPeter Wemm /* 117101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 118101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 119101f9fc8SPeter Wemm */ 120eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 126101f9fc8SPeter Wemm 127b0d22693SCrist J. Clark /* 128b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 129b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 130b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 131b0d22693SCrist J. Clark */ 132eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 133eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 134b0d22693SCrist J. Clark 1355f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 136eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */ 137eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */ 138eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */ 139eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */ 140eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs); 1415f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ipport_tcplastcount); 142eddfbb76SRobert Watson 1431e77c105SRobert Watson #define V_ipport_tcplastcount VNET(ipport_tcplastcount) 1446ac48b74SMike Silbersack 145d2025bd0SBjoern A. Zeeb static void in_pcbremlists(struct inpcb *inp); 146d2025bd0SBjoern A. Zeeb #ifdef INET 147fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 148fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 149fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 150fa046d87SRobert Watson int lookupflags, struct ifnet *ifp); 15167107f45SBjoern A. Zeeb 152bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 153bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 154bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 155bbd42ad0SPeter Wemm 156bbd42ad0SPeter Wemm static int 15782d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 158bbd42ad0SPeter Wemm { 15930a4ab08SBruce Evans int error; 16030a4ab08SBruce Evans 161f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 16230a4ab08SBruce Evans if (error == 0) { 163603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 164603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 165603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 166603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 167603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 168603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 169bbd42ad0SPeter Wemm } 17030a4ab08SBruce Evans return (error); 171bbd42ad0SPeter Wemm } 172bbd42ad0SPeter Wemm 173bbd42ad0SPeter Wemm #undef RANGECHK 174bbd42ad0SPeter Wemm 1756472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, 1766472ac3dSEd Schouten "IP Ports"); 17733b3ac06SPeter Wemm 1786df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1796df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1806df8a710SGleb Smirnoff &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", ""); 1816df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1826df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1836df8a710SGleb Smirnoff &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", ""); 1846df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1856df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1866df8a710SGleb Smirnoff &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", ""); 1876df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1886df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1896df8a710SGleb Smirnoff &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", ""); 1906df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1916df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1926df8a710SGleb Smirnoff &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", ""); 1936df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 1946df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1956df8a710SGleb Smirnoff &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", ""); 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 1976df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 1986df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 200eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 2026df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 203eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 2046df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, 2056df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 206eddfbb76SRobert Watson &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port " 2076ee79c59SMaxim Konovalov "allocations before switching to a sequental one"); 2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, 2096df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 210eddfbb76SRobert Watson &VNET_NAME(ipport_randomtime), 0, 2118b615593SMarko Zec "Minimum time to keep sequental port " 2126ee79c59SMaxim Konovalov "allocation before switching to a random one"); 21383e521ecSBjoern A. Zeeb #endif /* INET */ 2140312fbe9SPoul-Henning Kamp 215c3229e05SDavid Greenman /* 216c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 217c3229e05SDavid Greenman * 218de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 219de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 220de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 221c3229e05SDavid Greenman */ 222c3229e05SDavid Greenman 2231a43cff9SSean Bruno static struct inpcblbgroup * 2241a43cff9SSean Bruno in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, u_char vflag, 2251a43cff9SSean Bruno uint16_t port, const union in_dependaddr *addr, int size) 2261a43cff9SSean Bruno { 2271a43cff9SSean Bruno struct inpcblbgroup *grp; 2281a43cff9SSean Bruno size_t bytes; 2291a43cff9SSean Bruno 2301a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 2311a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 2321a43cff9SSean Bruno if (!grp) 2331a43cff9SSean Bruno return (NULL); 2341a43cff9SSean Bruno grp->il_vflag = vflag; 2351a43cff9SSean Bruno grp->il_lport = port; 2361a43cff9SSean Bruno grp->il_dependladdr = *addr; 2371a43cff9SSean Bruno grp->il_inpsiz = size; 23854af3d0dSMark Johnston CK_LIST_INSERT_HEAD(hdr, grp, il_list); 2391a43cff9SSean Bruno return (grp); 2401a43cff9SSean Bruno } 2411a43cff9SSean Bruno 2421a43cff9SSean Bruno static void 24354af3d0dSMark Johnston in_pcblbgroup_free_deferred(epoch_context_t ctx) 24454af3d0dSMark Johnston { 24554af3d0dSMark Johnston struct inpcblbgroup *grp; 24654af3d0dSMark Johnston 24754af3d0dSMark Johnston grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 24854af3d0dSMark Johnston free(grp, M_PCB); 24954af3d0dSMark Johnston } 25054af3d0dSMark Johnston 25154af3d0dSMark Johnston static void 2521a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 2531a43cff9SSean Bruno { 2541a43cff9SSean Bruno 25554af3d0dSMark Johnston CK_LIST_REMOVE(grp, il_list); 25654af3d0dSMark Johnston epoch_call(net_epoch_preempt, &grp->il_epoch_ctx, 25754af3d0dSMark Johnston in_pcblbgroup_free_deferred); 2581a43cff9SSean Bruno } 2591a43cff9SSean Bruno 2601a43cff9SSean Bruno static struct inpcblbgroup * 2611a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 2621a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 2631a43cff9SSean Bruno { 2641a43cff9SSean Bruno struct inpcblbgroup *grp; 2651a43cff9SSean Bruno int i; 2661a43cff9SSean Bruno 2671a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, old_grp->il_vflag, 2681a43cff9SSean Bruno old_grp->il_lport, &old_grp->il_dependladdr, size); 26979ee680bSMark Johnston if (grp == NULL) 2701a43cff9SSean Bruno return (NULL); 2711a43cff9SSean Bruno 2721a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 2731a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 2741a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 2751a43cff9SSean Bruno 2761a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 2771a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 2781a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 2791a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 2801a43cff9SSean Bruno return (grp); 2811a43cff9SSean Bruno } 2821a43cff9SSean Bruno 2831a43cff9SSean Bruno /* 2841a43cff9SSean Bruno * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 2851a43cff9SSean Bruno * and shrink group if possible. 2861a43cff9SSean Bruno */ 2871a43cff9SSean Bruno static void 2881a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 2891a43cff9SSean Bruno int i) 2901a43cff9SSean Bruno { 29179ee680bSMark Johnston struct inpcblbgroup *grp, *new_grp; 2921a43cff9SSean Bruno 29379ee680bSMark Johnston grp = *grpp; 2941a43cff9SSean Bruno for (; i + 1 < grp->il_inpcnt; ++i) 2951a43cff9SSean Bruno grp->il_inp[i] = grp->il_inp[i + 1]; 2961a43cff9SSean Bruno grp->il_inpcnt--; 2971a43cff9SSean Bruno 2981a43cff9SSean Bruno if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 29979ee680bSMark Johnston grp->il_inpcnt <= grp->il_inpsiz / 4) { 3001a43cff9SSean Bruno /* Shrink this group. */ 30179ee680bSMark Johnston new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 30279ee680bSMark Johnston if (new_grp != NULL) 3031a43cff9SSean Bruno *grpp = new_grp; 3041a43cff9SSean Bruno } 3051a43cff9SSean Bruno } 3061a43cff9SSean Bruno 3071a43cff9SSean Bruno /* 3081a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 3091a43cff9SSean Bruno */ 3101a43cff9SSean Bruno static int 3111a43cff9SSean Bruno in_pcbinslbgrouphash(struct inpcb *inp) 3121a43cff9SSean Bruno { 313a7026c7fSMark Johnston const static struct timeval interval = { 60, 0 }; 314a7026c7fSMark Johnston static struct timeval lastprint; 3151a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3161a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3171a43cff9SSean Bruno struct inpcblbgroup *grp; 31879ee680bSMark Johnston uint32_t idx; 3191a43cff9SSean Bruno 3201a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 3211a43cff9SSean Bruno 3221a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 3231a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 3241a43cff9SSean Bruno 3251a43cff9SSean Bruno /* 3261a43cff9SSean Bruno * Don't allow jailed socket to join local group. 3271a43cff9SSean Bruno */ 32879ee680bSMark Johnston if (inp->inp_socket != NULL && jailed(inp->inp_socket->so_cred)) 3291a43cff9SSean Bruno return (0); 3301a43cff9SSean Bruno 3311a43cff9SSean Bruno #ifdef INET6 3321a43cff9SSean Bruno /* 3331a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 3341a43cff9SSean Bruno */ 3351a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 3361a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 3371a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 3381a43cff9SSean Bruno return (0); 3391a43cff9SSean Bruno } 3401a43cff9SSean Bruno #endif 3411a43cff9SSean Bruno 34279ee680bSMark Johnston idx = INP_PCBLBGROUP_PORTHASH(inp->inp_lport, 34379ee680bSMark Johnston pcbinfo->ipi_lbgrouphashmask); 34479ee680bSMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 34554af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 3461a43cff9SSean Bruno if (grp->il_vflag == inp->inp_vflag && 3471a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 3481a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 3491a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 35079ee680bSMark Johnston sizeof(grp->il_dependladdr)) == 0) 3511a43cff9SSean Bruno break; 3521a43cff9SSean Bruno } 3531a43cff9SSean Bruno if (grp == NULL) { 3541a43cff9SSean Bruno /* Create new load balance group. */ 3551a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, inp->inp_vflag, 3561a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 3571a43cff9SSean Bruno INPCBLBGROUP_SIZMIN); 35879ee680bSMark Johnston if (grp == NULL) 3591a43cff9SSean Bruno return (ENOBUFS); 3601a43cff9SSean Bruno } else if (grp->il_inpcnt == grp->il_inpsiz) { 3611a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 362a7026c7fSMark Johnston if (ratecheck(&lastprint, &interval)) 3631a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 3641a43cff9SSean Bruno ntohs(grp->il_lport)); 3651a43cff9SSean Bruno return (0); 3661a43cff9SSean Bruno } 3671a43cff9SSean Bruno 3681a43cff9SSean Bruno /* Expand this local group. */ 3691a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 37079ee680bSMark Johnston if (grp == NULL) 3711a43cff9SSean Bruno return (ENOBUFS); 3721a43cff9SSean Bruno } 3731a43cff9SSean Bruno 3741a43cff9SSean Bruno KASSERT(grp->il_inpcnt < grp->il_inpsiz, 37579ee680bSMark Johnston ("invalid local group size %d and count %d", grp->il_inpsiz, 37679ee680bSMark Johnston grp->il_inpcnt)); 3771a43cff9SSean Bruno 3781a43cff9SSean Bruno grp->il_inp[grp->il_inpcnt] = inp; 3791a43cff9SSean Bruno grp->il_inpcnt++; 3801a43cff9SSean Bruno return (0); 3811a43cff9SSean Bruno } 3821a43cff9SSean Bruno 3831a43cff9SSean Bruno /* 3841a43cff9SSean Bruno * Remove PCB from load balance group. 3851a43cff9SSean Bruno */ 3861a43cff9SSean Bruno static void 3871a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 3881a43cff9SSean Bruno { 3891a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 3901a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 3911a43cff9SSean Bruno struct inpcblbgroup *grp; 3921a43cff9SSean Bruno int i; 3931a43cff9SSean Bruno 3941a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 3951a43cff9SSean Bruno 3961a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 3971a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 3981a43cff9SSean Bruno 3991a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 4001a43cff9SSean Bruno INP_PCBLBGROUP_PORTHASH(inp->inp_lport, 4011a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask)]; 4021a43cff9SSean Bruno 40354af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 4041a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 4051a43cff9SSean Bruno if (grp->il_inp[i] != inp) 4061a43cff9SSean Bruno continue; 4071a43cff9SSean Bruno 4081a43cff9SSean Bruno if (grp->il_inpcnt == 1) { 4091a43cff9SSean Bruno /* We are the last, free this local group. */ 4101a43cff9SSean Bruno in_pcblbgroup_free(grp); 4111a43cff9SSean Bruno } else { 4121a43cff9SSean Bruno /* Pull up inpcbs, shrink group if possible. */ 4131a43cff9SSean Bruno in_pcblbgroup_reorder(hdr, &grp, i); 4141a43cff9SSean Bruno } 4151a43cff9SSean Bruno return; 4161a43cff9SSean Bruno } 4171a43cff9SSean Bruno } 4181a43cff9SSean Bruno } 4191a43cff9SSean Bruno 420c3229e05SDavid Greenman /* 421cc487c16SGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 422cc487c16SGleb Smirnoff * different name to the lock. But they all are disposed the same. 423cc487c16SGleb Smirnoff */ 424cc487c16SGleb Smirnoff static void 425cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size) 426cc487c16SGleb Smirnoff { 427cc487c16SGleb Smirnoff struct inpcb *inp = mem; 428cc487c16SGleb Smirnoff 429cc487c16SGleb Smirnoff INP_LOCK_DESTROY(inp); 430cc487c16SGleb Smirnoff } 431cc487c16SGleb Smirnoff 432cc487c16SGleb Smirnoff /* 4339bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 4349bcd427bSRobert Watson * arguments in time. 4359bcd427bSRobert Watson */ 4369bcd427bSRobert Watson void 4379bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 4389bcd427bSRobert Watson struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 439cc487c16SGleb Smirnoff char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields) 4409bcd427bSRobert Watson { 4419bcd427bSRobert Watson 4429bcd427bSRobert Watson INP_INFO_LOCK_INIT(pcbinfo, name); 443fa046d87SRobert Watson INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 444ff9b006dSJulien Charbon INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist"); 4459bcd427bSRobert Watson #ifdef VIMAGE 4469bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 4479bcd427bSRobert Watson #endif 4489bcd427bSRobert Watson pcbinfo->ipi_listhead = listhead; 449b872626dSMatt Macy CK_LIST_INIT(pcbinfo->ipi_listhead); 450fa046d87SRobert Watson pcbinfo->ipi_count = 0; 4519bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 4529bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 4539bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 4549bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 4551a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashbase = hashinit(hash_nelements, M_PCB, 4561a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 45752cd27cbSRobert Watson #ifdef PCBGROUP 45852cd27cbSRobert Watson in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 45952cd27cbSRobert Watson #endif 4609bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 461cc487c16SGleb Smirnoff NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0); 4629bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 4636acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 4646acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 4659bcd427bSRobert Watson } 4669bcd427bSRobert Watson 4679bcd427bSRobert Watson /* 4689bcd427bSRobert Watson * Destroy an inpcbinfo. 4699bcd427bSRobert Watson */ 4709bcd427bSRobert Watson void 4719bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 4729bcd427bSRobert Watson { 4739bcd427bSRobert Watson 474fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 475fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 476fa046d87SRobert Watson 4779bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 4789bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 4799bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 4801a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 4811a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 48252cd27cbSRobert Watson #ifdef PCBGROUP 48352cd27cbSRobert Watson in_pcbgroup_destroy(pcbinfo); 48452cd27cbSRobert Watson #endif 4859bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 486ff9b006dSJulien Charbon INP_LIST_LOCK_DESTROY(pcbinfo); 487fa046d87SRobert Watson INP_HASH_LOCK_DESTROY(pcbinfo); 4889bcd427bSRobert Watson INP_INFO_LOCK_DESTROY(pcbinfo); 4899bcd427bSRobert Watson } 4909bcd427bSRobert Watson 4919bcd427bSRobert Watson /* 492c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 493d915b280SStephan Uphoff * On success return with the PCB locked. 494c3229e05SDavid Greenman */ 495df8bae1dSRodney W. Grimes int 496d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 497df8bae1dSRodney W. Grimes { 498136d4f1cSRobert Watson struct inpcb *inp; 49913cf67f3SHajimu UMEMOTO int error; 500a557af22SRobert Watson 501ff9b006dSJulien Charbon #ifdef INVARIANTS 502ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 503ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 504ff9b006dSJulien Charbon } else { 50559daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 506ff9b006dSJulien Charbon } 507ff9b006dSJulien Charbon #endif 508ff9b006dSJulien Charbon 509a557af22SRobert Watson error = 0; 510d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 511df8bae1dSRodney W. Grimes if (inp == NULL) 512df8bae1dSRodney W. Grimes return (ENOBUFS); 5136a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 51415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 515df8bae1dSRodney W. Grimes inp->inp_socket = so; 51686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 5178b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 518a557af22SRobert Watson #ifdef MAC 51930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 520a557af22SRobert Watson if (error != 0) 521a557af22SRobert Watson goto out; 52230d239bcSRobert Watson mac_inpcb_create(so, inp); 523a557af22SRobert Watson #endif 524fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 525fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 5260bffde27SRobert Watson if (error != 0) { 5270bffde27SRobert Watson #ifdef MAC 5280bffde27SRobert Watson mac_inpcb_destroy(inp); 5290bffde27SRobert Watson #endif 530a557af22SRobert Watson goto out; 5310bffde27SRobert Watson } 532b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 533e3fd5ffdSRobert Watson #ifdef INET6 534340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 535340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 536603724d3SBjoern A. Zeeb if (V_ip6_v6only) 53733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 538340c35deSJonathan Lemon } 53975daea93SPaul Saab #endif 540ff9b006dSJulien Charbon INP_WLOCK(inp); 541ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 542b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 5433d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 544df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 54533841545SHajimu UMEMOTO #ifdef INET6 546603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 54733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 54833841545SHajimu UMEMOTO #endif 549d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 55079bdc6e5SRobert Watson refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 5518c1960d5SMike Karels 5528c1960d5SMike Karels /* 5538c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 5548c1960d5SMike Karels * to be cleaned up. 5558c1960d5SMike Karels */ 5568c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 557ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 5587a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 559a557af22SRobert Watson out: 56086d02c5cSBjoern A. Zeeb if (error != 0) { 56186d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 562a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 56386d02c5cSBjoern A. Zeeb } 564a557af22SRobert Watson #endif 565a557af22SRobert Watson return (error); 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes 56867107f45SBjoern A. Zeeb #ifdef INET 569df8bae1dSRodney W. Grimes int 570136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 571df8bae1dSRodney W. Grimes { 5724b932371SIan Dowse int anonport, error; 5734b932371SIan Dowse 5748501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 575fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 57659daba27SSam Leffler 5774b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 5784b932371SIan Dowse return (EINVAL); 5795cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 5804b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 581b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 5824b932371SIan Dowse if (error) 5834b932371SIan Dowse return (error); 5844b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 5854b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 5864b932371SIan Dowse inp->inp_lport = 0; 5874b932371SIan Dowse return (EAGAIN); 5884b932371SIan Dowse } 5894b932371SIan Dowse if (anonport) 5904b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 5914b932371SIan Dowse return (0); 5924b932371SIan Dowse } 59367107f45SBjoern A. Zeeb #endif 5944b932371SIan Dowse 59539c8c62eSHiren Panchasara /* 59639c8c62eSHiren Panchasara * Select a local port (number) to use. 59739c8c62eSHiren Panchasara */ 598efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 599efc76f72SBjoern A. Zeeb int 6004616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 6014616026fSErmal Luçi struct ucred *cred, int lookupflags) 602efc76f72SBjoern A. Zeeb { 603efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 604efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 605efc76f72SBjoern A. Zeeb unsigned short *lastport; 606efc76f72SBjoern A. Zeeb int count, dorandom, error; 607efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 608efc76f72SBjoern A. Zeeb #ifdef INET 609efc76f72SBjoern A. Zeeb struct in_addr laddr; 610efc76f72SBjoern A. Zeeb #endif 611efc76f72SBjoern A. Zeeb 612efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 613efc76f72SBjoern A. Zeeb 614efc76f72SBjoern A. Zeeb /* 615efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 616efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 617efc76f72SBjoern A. Zeeb */ 618efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 619fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 620efc76f72SBjoern A. Zeeb 621efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 622efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 623efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 624efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 625efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 626efc76f72SBjoern A. Zeeb error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0); 627efc76f72SBjoern A. Zeeb if (error) 628efc76f72SBjoern A. Zeeb return (error); 629efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 630efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 631efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 632efc76f72SBjoern A. Zeeb } else { 633efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 634efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 635efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 636efc76f72SBjoern A. Zeeb } 637efc76f72SBjoern A. Zeeb /* 638e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 639efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 640efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 641efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 642efc76f72SBjoern A. Zeeb */ 643efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 644e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 645e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 646efc76f72SBjoern A. Zeeb dorandom = 1; 647efc76f72SBjoern A. Zeeb else 648efc76f72SBjoern A. Zeeb dorandom = 0; 649efc76f72SBjoern A. Zeeb /* 650efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 651efc76f72SBjoern A. Zeeb * we have the only port available. 652efc76f72SBjoern A. Zeeb */ 653efc76f72SBjoern A. Zeeb if (first == last) 654efc76f72SBjoern A. Zeeb dorandom = 0; 655e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 656e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 657efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 658efc76f72SBjoern A. Zeeb /* 659efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 660efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 661efc76f72SBjoern A. Zeeb * code path implementing all logic. 662efc76f72SBjoern A. Zeeb */ 663efc76f72SBjoern A. Zeeb if (first > last) { 664efc76f72SBjoern A. Zeeb aux = first; 665efc76f72SBjoern A. Zeeb first = last; 666efc76f72SBjoern A. Zeeb last = aux; 667efc76f72SBjoern A. Zeeb } 668efc76f72SBjoern A. Zeeb 669efc76f72SBjoern A. Zeeb #ifdef INET 670efc76f72SBjoern A. Zeeb /* Make the compiler happy. */ 671efc76f72SBjoern A. Zeeb laddr.s_addr = 0; 6724d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 673efc76f72SBjoern A. Zeeb KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 674efc76f72SBjoern A. Zeeb __func__, inp)); 675efc76f72SBjoern A. Zeeb laddr = *laddrp; 676efc76f72SBjoern A. Zeeb } 677efc76f72SBjoern A. Zeeb #endif 67867107f45SBjoern A. Zeeb tmpinp = NULL; /* Make compiler happy. */ 679efc76f72SBjoern A. Zeeb lport = *lportp; 680efc76f72SBjoern A. Zeeb 681efc76f72SBjoern A. Zeeb if (dorandom) 682efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 683efc76f72SBjoern A. Zeeb 684efc76f72SBjoern A. Zeeb count = last - first; 685efc76f72SBjoern A. Zeeb 686efc76f72SBjoern A. Zeeb do { 687efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 688efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 689efc76f72SBjoern A. Zeeb ++*lastport; 690efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 691efc76f72SBjoern A. Zeeb *lastport = first; 692efc76f72SBjoern A. Zeeb lport = htons(*lastport); 693efc76f72SBjoern A. Zeeb 694efc76f72SBjoern A. Zeeb #ifdef INET6 6954616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 696efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 69768e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 698efc76f72SBjoern A. Zeeb #endif 699efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 700efc76f72SBjoern A. Zeeb else 701efc76f72SBjoern A. Zeeb #endif 702efc76f72SBjoern A. Zeeb #ifdef INET 703efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 70468e0d7e0SRobert Watson lport, lookupflags, cred); 705efc76f72SBjoern A. Zeeb #endif 706efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 707efc76f72SBjoern A. Zeeb 708efc76f72SBjoern A. Zeeb #ifdef INET 7094d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 710efc76f72SBjoern A. Zeeb laddrp->s_addr = laddr.s_addr; 711efc76f72SBjoern A. Zeeb #endif 712efc76f72SBjoern A. Zeeb *lportp = lport; 713efc76f72SBjoern A. Zeeb 714efc76f72SBjoern A. Zeeb return (0); 715efc76f72SBjoern A. Zeeb } 716efdf104bSMikolaj Golub 717efdf104bSMikolaj Golub /* 718efdf104bSMikolaj Golub * Return cached socket options. 719efdf104bSMikolaj Golub */ 7201a43cff9SSean Bruno int 721efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 722efdf104bSMikolaj Golub { 7231a43cff9SSean Bruno int so_options; 724efdf104bSMikolaj Golub 725efdf104bSMikolaj Golub so_options = 0; 726efdf104bSMikolaj Golub 7271a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 7281a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 729efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 730efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 731efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 732efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 733efdf104bSMikolaj Golub return (so_options); 734efdf104bSMikolaj Golub } 735efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 736efc76f72SBjoern A. Zeeb 7374b932371SIan Dowse /* 7380a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 7390a100a6fSAdrian Chadd * 7400a100a6fSAdrian Chadd * ni points to the new inp. 7410a100a6fSAdrian Chadd * oi points to the exisitng inp. 7420a100a6fSAdrian Chadd * 7430a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 7440a100a6fSAdrian Chadd * whether the credentials match. 7450a100a6fSAdrian Chadd */ 746d5bb8bd3SAdrian Chadd int 7470a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 7480a100a6fSAdrian Chadd { 7490a100a6fSAdrian Chadd /* Check permissions match */ 7500a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 7510a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 7520a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 7530a100a6fSAdrian Chadd return (0); 7540a100a6fSAdrian Chadd 7550a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 7560a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 7570a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 7580a100a6fSAdrian Chadd return (0); 7590a100a6fSAdrian Chadd 7600a100a6fSAdrian Chadd /* 7610a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 7620a100a6fSAdrian Chadd * it is and it matches the checks. 7630a100a6fSAdrian Chadd */ 7640a100a6fSAdrian Chadd return (1); 7650a100a6fSAdrian Chadd } 7660a100a6fSAdrian Chadd 767d5bb8bd3SAdrian Chadd #ifdef INET 7680a100a6fSAdrian Chadd /* 7694b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 7704b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 7714b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 7724b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 7734b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 7744b932371SIan Dowse * 7754b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 7764b932371SIan Dowse */ 7774b932371SIan Dowse int 778136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 779136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 7804b932371SIan Dowse { 7814b932371SIan Dowse struct socket *so = inp->inp_socket; 78215bd2b43SDavid Greenman struct sockaddr_in *sin; 783c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 7844b932371SIan Dowse struct in_addr laddr; 785df8bae1dSRodney W. Grimes u_short lport = 0; 78668e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 787413628a7SBjoern A. Zeeb int error; 788df8bae1dSRodney W. Grimes 7898501a69cSRobert Watson /* 7901a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 7911a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 7921a43cff9SSean Bruno */ 7931a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 7941a43cff9SSean Bruno 7951a43cff9SSean Bruno /* 796fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 7978501a69cSRobert Watson */ 79859daba27SSam Leffler INP_LOCK_ASSERT(inp); 799fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 80059daba27SSam Leffler 801d7c5a620SMatt Macy if (CK_STAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 802df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 8034b932371SIan Dowse laddr.s_addr = *laddrp; 8044b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 805df8bae1dSRodney W. Grimes return (EINVAL); 8061a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 80768e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 8084616026fSErmal Luçi if (nam == NULL) { 8097c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 8107c2f3cb9SJamie Gritton return (error); 8117c2f3cb9SJamie Gritton } else { 81257bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 81357bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 814df8bae1dSRodney W. Grimes return (EINVAL); 815df8bae1dSRodney W. Grimes #ifdef notdef 816df8bae1dSRodney W. Grimes /* 817df8bae1dSRodney W. Grimes * We should check the family, but old programs 818df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 819df8bae1dSRodney W. Grimes */ 820df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 821df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 822df8bae1dSRodney W. Grimes #endif 823b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 824b89e82ddSJamie Gritton if (error) 825b89e82ddSJamie Gritton return (error); 8264b932371SIan Dowse if (sin->sin_port != *lportp) { 8274b932371SIan Dowse /* Don't allow the port to change. */ 8284b932371SIan Dowse if (*lportp != 0) 8294b932371SIan Dowse return (EINVAL); 830df8bae1dSRodney W. Grimes lport = sin->sin_port; 8314b932371SIan Dowse } 8324b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 833df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 834df8bae1dSRodney W. Grimes /* 835df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 836df8bae1dSRodney W. Grimes * allow complete duplication of binding if 837df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 838df8bae1dSRodney W. Grimes * and a multicast address is bound on both 839df8bae1dSRodney W. Grimes * new and duplicated sockets. 840df8bae1dSRodney W. Grimes */ 841f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 842df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 8431a43cff9SSean Bruno /* 8441a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 8451a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 8461a43cff9SSean Bruno */ 8471a43cff9SSean Bruno if ((so->so_options & 8481a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 8491a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 850df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 851df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 85283103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 8534209e01aSAdrian Chadd /* 8544209e01aSAdrian Chadd * Is the address a local IP address? 855f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 8568696873dSAdrian Chadd * to any endpoint address, local or not. 8574209e01aSAdrian Chadd */ 858f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 8598896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 860df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 861df8bae1dSRodney W. Grimes } 8624b932371SIan Dowse laddr = sin->sin_addr; 863df8bae1dSRodney W. Grimes if (lport) { 864df8bae1dSRodney W. Grimes struct inpcb *t; 865ae0e7143SRobert Watson struct tcptw *tw; 866ae0e7143SRobert Watson 867df8bae1dSRodney W. Grimes /* GROSS */ 868603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 869603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 870acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 87132f9753cSRobert Watson 0)) 8722469dd60SGarrett Wollman return (EACCES); 873835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 87486d02c5cSBjoern A. Zeeb priv_check_cred(inp->inp_cred, 87532f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 876078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 877413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 878340c35deSJonathan Lemon /* 879340c35deSJonathan Lemon * XXX 880340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 881340c35deSJonathan Lemon */ 8824cc20ab1SSeigo Tanimura if (t && 8830a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 884ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 8854658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 8864658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 8874cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 88852b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 8891a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 8901a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 89186d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 89286d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 8934049a042SGuido van Rooij return (EADDRINUSE); 8940a100a6fSAdrian Chadd 8950a100a6fSAdrian Chadd /* 8960a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 8970a100a6fSAdrian Chadd * the credentials need to match and the 8980a100a6fSAdrian Chadd * original socket also has to have been bound 8990a100a6fSAdrian Chadd * with BINDMULTI. 9000a100a6fSAdrian Chadd */ 9010a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9020a100a6fSAdrian Chadd return (EADDRINUSE); 9034049a042SGuido van Rooij } 904c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 90568e0d7e0SRobert Watson lport, lookupflags, cred); 906ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 907ae0e7143SRobert Watson /* 908ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 909ae0e7143SRobert Watson * state recycled, we treat the address as 910ae0e7143SRobert Watson * being in use (for now). This is better 911ae0e7143SRobert Watson * than a panic, but not desirable. 912ae0e7143SRobert Watson */ 913ec95b709SMikolaj Golub tw = intotw(t); 914ae0e7143SRobert Watson if (tw == NULL || 9151a43cff9SSean Bruno ((reuseport & tw->tw_so_options) == 0 && 9161a43cff9SSean Bruno (reuseport_lb & 9171a43cff9SSean Bruno tw->tw_so_options) == 0)) { 918340c35deSJonathan Lemon return (EADDRINUSE); 9191a43cff9SSean Bruno } 9200a100a6fSAdrian Chadd } else if (t && 9210a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 9221a43cff9SSean Bruno (reuseport & inp_so_options(t)) == 0 && 9231a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 924e3fd5ffdSRobert Watson #ifdef INET6 92533841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 926cfa1ca9dSYoshinobu Inoue INADDR_ANY || 927cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 928cfa1ca9dSYoshinobu Inoue INADDR_ANY || 929fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 930fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 931e3fd5ffdSRobert Watson #endif 932df8bae1dSRodney W. Grimes return (EADDRINUSE); 9330a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9340a100a6fSAdrian Chadd return (EADDRINUSE); 935df8bae1dSRodney W. Grimes } 936cfa1ca9dSYoshinobu Inoue } 937df8bae1dSRodney W. Grimes } 9384b932371SIan Dowse if (*lportp != 0) 9394b932371SIan Dowse lport = *lportp; 94033b3ac06SPeter Wemm if (lport == 0) { 9414616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 942efc76f72SBjoern A. Zeeb if (error != 0) 943efc76f72SBjoern A. Zeeb return (error); 94433b3ac06SPeter Wemm 94533b3ac06SPeter Wemm } 9464b932371SIan Dowse *laddrp = laddr.s_addr; 9474b932371SIan Dowse *lportp = lport; 948df8bae1dSRodney W. Grimes return (0); 949df8bae1dSRodney W. Grimes } 950df8bae1dSRodney W. Grimes 951999f1343SGarrett Wollman /* 9525200e00eSIan Dowse * Connect from a socket to a specified address. 9535200e00eSIan Dowse * Both address and port must be specified in argument sin. 9545200e00eSIan Dowse * If don't have a local address for this socket yet, 9555200e00eSIan Dowse * then pick one. 956999f1343SGarrett Wollman */ 957999f1343SGarrett Wollman int 958d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 959d3c1f003SRobert Watson struct ucred *cred, struct mbuf *m) 960999f1343SGarrett Wollman { 9615200e00eSIan Dowse u_short lport, fport; 9625200e00eSIan Dowse in_addr_t laddr, faddr; 9635200e00eSIan Dowse int anonport, error; 964df8bae1dSRodney W. Grimes 9658501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 966fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 96727f74fd0SRobert Watson 9685200e00eSIan Dowse lport = inp->inp_lport; 9695200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 9705200e00eSIan Dowse anonport = (lport == 0); 9715200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 972b0330ed9SPawel Jakub Dawidek NULL, cred); 9735200e00eSIan Dowse if (error) 9745200e00eSIan Dowse return (error); 9755200e00eSIan Dowse 9765200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 9775200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 9785200e00eSIan Dowse inp->inp_lport = lport; 9795200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 9805200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 9815200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 9825200e00eSIan Dowse inp->inp_lport = 0; 9835200e00eSIan Dowse return (EAGAIN); 9845200e00eSIan Dowse } 9855200e00eSIan Dowse } 9865200e00eSIan Dowse 9875200e00eSIan Dowse /* Commit the remaining changes. */ 9885200e00eSIan Dowse inp->inp_lport = lport; 9895200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 9905200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 9915200e00eSIan Dowse inp->inp_fport = fport; 992d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 9932cb64cb2SGeorge V. Neville-Neil 9945200e00eSIan Dowse if (anonport) 9955200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 9965200e00eSIan Dowse return (0); 9975200e00eSIan Dowse } 9985200e00eSIan Dowse 999d3c1f003SRobert Watson int 1000d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 1001d3c1f003SRobert Watson { 1002d3c1f003SRobert Watson 1003d3c1f003SRobert Watson return (in_pcbconnect_mbuf(inp, nam, cred, NULL)); 1004d3c1f003SRobert Watson } 1005d3c1f003SRobert Watson 10065200e00eSIan Dowse /* 10070895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 10080895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 10090895aec3SBjoern A. Zeeb */ 1010ae190832SSteven Hartland int 10110895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 10120895aec3SBjoern A. Zeeb struct ucred *cred) 10130895aec3SBjoern A. Zeeb { 10140895aec3SBjoern A. Zeeb struct ifaddr *ifa; 10150895aec3SBjoern A. Zeeb struct sockaddr *sa; 10160895aec3SBjoern A. Zeeb struct sockaddr_in *sin; 10170895aec3SBjoern A. Zeeb struct route sro; 10180895aec3SBjoern A. Zeeb int error; 10190895aec3SBjoern A. Zeeb 1020413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1021592bcae8SBjoern A. Zeeb /* 1022592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1023592bcae8SBjoern A. Zeeb * if requested. 1024592bcae8SBjoern A. Zeeb */ 1025592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 1026592bcae8SBjoern A. Zeeb return (0); 1027592bcae8SBjoern A. Zeeb 10280895aec3SBjoern A. Zeeb error = 0; 10290895aec3SBjoern A. Zeeb bzero(&sro, sizeof(sro)); 10300895aec3SBjoern A. Zeeb 10310895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)&sro.ro_dst; 10320895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 10330895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 10340895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 10350895aec3SBjoern A. Zeeb 10360895aec3SBjoern A. Zeeb /* 10370895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 10380895aec3SBjoern A. Zeeb * else punt. 10390895aec3SBjoern A. Zeeb * 10400895aec3SBjoern A. Zeeb * Find out route to destination. 10410895aec3SBjoern A. Zeeb */ 10420895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 10436e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 10440895aec3SBjoern A. Zeeb 10450895aec3SBjoern A. Zeeb /* 10460895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 10470895aec3SBjoern A. Zeeb * the outgoing interface. 10480895aec3SBjoern A. Zeeb * 10490895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 10500895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 10510895aec3SBjoern A. Zeeb * the source address from. 10520895aec3SBjoern A. Zeeb */ 10534f6c66ccSMatt Macy NET_EPOCH_ENTER(); 10540895aec3SBjoern A. Zeeb if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 10558c0fec80SRobert Watson struct in_ifaddr *ia; 10560895aec3SBjoern A. Zeeb struct ifnet *ifp; 10570895aec3SBjoern A. Zeeb 10584f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 105958a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 10604f6c66ccSMatt Macy if (ia == NULL) { 10614f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 106258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 10634f6c66ccSMatt Macy 10644f6c66ccSMatt Macy } 10650895aec3SBjoern A. Zeeb if (ia == NULL) { 10660895aec3SBjoern A. Zeeb error = ENETUNREACH; 10670895aec3SBjoern A. Zeeb goto done; 10680895aec3SBjoern A. Zeeb } 10690895aec3SBjoern A. Zeeb 10700304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 10710895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 10720895aec3SBjoern A. Zeeb goto done; 10730895aec3SBjoern A. Zeeb } 10740895aec3SBjoern A. Zeeb 10750895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 10760895aec3SBjoern A. Zeeb ia = NULL; 1077d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 10780895aec3SBjoern A. Zeeb 10790895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 10800895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 10810895aec3SBjoern A. Zeeb continue; 10820895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1083b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 10840895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 10850895aec3SBjoern A. Zeeb break; 10860895aec3SBjoern A. Zeeb } 10870895aec3SBjoern A. Zeeb } 10880895aec3SBjoern A. Zeeb if (ia != NULL) { 10890895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 10900895aec3SBjoern A. Zeeb goto done; 10910895aec3SBjoern A. Zeeb } 10920895aec3SBjoern A. Zeeb 10930895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1094b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 10950895aec3SBjoern A. Zeeb goto done; 10960895aec3SBjoern A. Zeeb } 10970895aec3SBjoern A. Zeeb 10980895aec3SBjoern A. Zeeb /* 10990895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 11000895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 11010895aec3SBjoern A. Zeeb * In case of jails do those three steps: 11020895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 11030895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 11040895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 11050895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 11060895aec3SBjoern A. Zeeb */ 11070895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 11088c0fec80SRobert Watson struct in_ifaddr *ia; 11099317b04eSRobert Watson struct ifnet *ifp; 11100895aec3SBjoern A. Zeeb 11110895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 11120304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11130895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 11140895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11150895aec3SBjoern A. Zeeb goto done; 11160895aec3SBjoern A. Zeeb } 11170895aec3SBjoern A. Zeeb 11180895aec3SBjoern A. Zeeb /* Jailed. */ 11190895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 11200895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 1121b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11220895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 11230895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11240895aec3SBjoern A. Zeeb goto done; 11250895aec3SBjoern A. Zeeb } 11260895aec3SBjoern A. Zeeb 11270895aec3SBjoern A. Zeeb /* 11280895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 11290895aec3SBjoern A. Zeeb * belonging to this jail. 11300895aec3SBjoern A. Zeeb */ 11318c0fec80SRobert Watson ia = NULL; 11329317b04eSRobert Watson ifp = sro.ro_rt->rt_ifp; 1133d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11340895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11350895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11360895aec3SBjoern A. Zeeb continue; 11370895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1138b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11390895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11400895aec3SBjoern A. Zeeb break; 11410895aec3SBjoern A. Zeeb } 11420895aec3SBjoern A. Zeeb } 11430895aec3SBjoern A. Zeeb if (ia != NULL) { 11440895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11450895aec3SBjoern A. Zeeb goto done; 11460895aec3SBjoern A. Zeeb } 11470895aec3SBjoern A. Zeeb 11480895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1149b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11500895aec3SBjoern A. Zeeb goto done; 11510895aec3SBjoern A. Zeeb } 11520895aec3SBjoern A. Zeeb 11530895aec3SBjoern A. Zeeb /* 11540895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 11550895aec3SBjoern A. Zeeb * to ourselves is here. 11560895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 11570895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 11580895aec3SBjoern A. Zeeb * a loopback interface. 11590895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 11600895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 11610895aec3SBjoern A. Zeeb */ 11620895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 11630895aec3SBjoern A. Zeeb struct sockaddr_in sain; 11648c0fec80SRobert Watson struct in_ifaddr *ia; 11650895aec3SBjoern A. Zeeb 11660895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 11670895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 11680895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 11690895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 11700895aec3SBjoern A. Zeeb 117158a39d8cSAlan Somers ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain), 117258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11730895aec3SBjoern A. Zeeb if (ia == NULL) 11744f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0, 117558a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1176f0bb05fcSQing Li if (ia == NULL) 1177f0bb05fcSQing Li ia = ifatoia(ifa_ifwithaddr(sintosa(&sain))); 11780895aec3SBjoern A. Zeeb 11790304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11800895aec3SBjoern A. Zeeb if (ia == NULL) { 11810895aec3SBjoern A. Zeeb error = ENETUNREACH; 11820895aec3SBjoern A. Zeeb goto done; 11830895aec3SBjoern A. Zeeb } 11840895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11850895aec3SBjoern A. Zeeb goto done; 11860895aec3SBjoern A. Zeeb } 11870895aec3SBjoern A. Zeeb 11880895aec3SBjoern A. Zeeb /* Jailed. */ 11890895aec3SBjoern A. Zeeb if (ia != NULL) { 11900895aec3SBjoern A. Zeeb struct ifnet *ifp; 11910895aec3SBjoern A. Zeeb 11920895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 11930895aec3SBjoern A. Zeeb ia = NULL; 1194d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11950895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11960895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11970895aec3SBjoern A. Zeeb continue; 11980895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1199b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1200b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 12010895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12020895aec3SBjoern A. Zeeb break; 12030895aec3SBjoern A. Zeeb } 12040895aec3SBjoern A. Zeeb } 12050895aec3SBjoern A. Zeeb if (ia != NULL) { 12060895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12070895aec3SBjoern A. Zeeb goto done; 12080895aec3SBjoern A. Zeeb } 12090895aec3SBjoern A. Zeeb } 12100895aec3SBjoern A. Zeeb 12110895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1212b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12130895aec3SBjoern A. Zeeb goto done; 12140895aec3SBjoern A. Zeeb } 12150895aec3SBjoern A. Zeeb 12160895aec3SBjoern A. Zeeb done: 12174f6c66ccSMatt Macy NET_EPOCH_EXIT(); 12180895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 12190895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 12200895aec3SBjoern A. Zeeb return (error); 12210895aec3SBjoern A. Zeeb } 12220895aec3SBjoern A. Zeeb 12230895aec3SBjoern A. Zeeb /* 12245200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 12255200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 12265200e00eSIan Dowse * address and port for the PCB; these are updated to the values 12275200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 12285200e00eSIan Dowse * the connect. 12295200e00eSIan Dowse * 12305200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 12315200e00eSIan Dowse * and port. These are not updated in the error case. 12325200e00eSIan Dowse * 12335200e00eSIan Dowse * If the operation fails because the connection already exists, 12345200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 12355200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 12365200e00eSIan Dowse * is set to NULL. 12375200e00eSIan Dowse */ 12385200e00eSIan Dowse int 1239136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1240136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1241136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 12425200e00eSIan Dowse { 1243cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 12445200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 12455200e00eSIan Dowse struct in_ifaddr *ia; 12465200e00eSIan Dowse struct inpcb *oinp; 1247b89e82ddSJamie Gritton struct in_addr laddr, faddr; 12485200e00eSIan Dowse u_short lport, fport; 12495200e00eSIan Dowse int error; 12505200e00eSIan Dowse 12518501a69cSRobert Watson /* 12528501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 12538501a69cSRobert Watson * lock is sufficient. 12548501a69cSRobert Watson */ 125527f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1256fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 125727f74fd0SRobert Watson 12585200e00eSIan Dowse if (oinpp != NULL) 12595200e00eSIan Dowse *oinpp = NULL; 126057bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1261df8bae1dSRodney W. Grimes return (EINVAL); 1262df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1263df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1264df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1265df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 12665200e00eSIan Dowse laddr.s_addr = *laddrp; 12675200e00eSIan Dowse lport = *lportp; 12685200e00eSIan Dowse faddr = sin->sin_addr; 12695200e00eSIan Dowse fport = sin->sin_port; 12700895aec3SBjoern A. Zeeb 1271d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1272df8bae1dSRodney W. Grimes /* 1273df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1274df8bae1dSRodney W. Grimes * use the primary local address. 1275df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1276df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1277df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1278df8bae1dSRodney W. Grimes */ 1279413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1280cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1281413628a7SBjoern A. Zeeb faddr = 1282d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1283cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1284b89e82ddSJamie Gritton if (cred != NULL && 1285b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1286b89e82ddSJamie Gritton return (error); 12872d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1288cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1289d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 12902d9cfabaSRobert Watson IFF_BROADCAST) 1291d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1292603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1293cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 12942d9cfabaSRobert Watson } 1295df8bae1dSRodney W. Grimes } 12965200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1297d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1298df8bae1dSRodney W. Grimes /* 1299df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1300d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1301df8bae1dSRodney W. Grimes * address of that interface as our source address. 1302df8bae1dSRodney W. Grimes */ 13035200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1304df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1305df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1306df8bae1dSRodney W. Grimes struct ifnet *ifp; 1307df8bae1dSRodney W. Grimes 1308df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1309df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1310df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1311cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1312d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1313e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1314e691be70SDaniel Eischen (cred == NULL || 1315e691be70SDaniel Eischen prison_check_ip4(cred, 1316e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1317df8bae1dSRodney W. Grimes break; 1318e691be70SDaniel Eischen } 1319e691be70SDaniel Eischen if (ia == NULL) 1320d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1321e691be70SDaniel Eischen else { 13225200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1323d79fdd98SDaniel Eischen error = 0; 1324999f1343SGarrett Wollman } 1325cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1326d79fdd98SDaniel Eischen } 1327d79fdd98SDaniel Eischen } 132804215ed2SRandall Stewart if (error) 132904215ed2SRandall Stewart return (error); 13300895aec3SBjoern A. Zeeb } 1331fa046d87SRobert Watson oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1332fa046d87SRobert Watson laddr, lport, 0, NULL); 13335200e00eSIan Dowse if (oinp != NULL) { 13345200e00eSIan Dowse if (oinpp != NULL) 13355200e00eSIan Dowse *oinpp = oinp; 1336df8bae1dSRodney W. Grimes return (EADDRINUSE); 1337c3229e05SDavid Greenman } 13385200e00eSIan Dowse if (lport == 0) { 1339b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1340b0330ed9SPawel Jakub Dawidek cred); 13415a903f8dSPierre Beyssac if (error) 13425a903f8dSPierre Beyssac return (error); 13435a903f8dSPierre Beyssac } 13445200e00eSIan Dowse *laddrp = laddr.s_addr; 13455200e00eSIan Dowse *lportp = lport; 13465200e00eSIan Dowse *faddrp = faddr.s_addr; 13475200e00eSIan Dowse *fportp = fport; 1348df8bae1dSRodney W. Grimes return (0); 1349df8bae1dSRodney W. Grimes } 1350df8bae1dSRodney W. Grimes 135126f9a767SRodney W. Grimes void 1352136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1353df8bae1dSRodney W. Grimes { 13546b348152SRobert Watson 13558501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1356fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1357df8bae1dSRodney W. Grimes 1358df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1359df8bae1dSRodney W. Grimes inp->inp_fport = 0; 136015bd2b43SDavid Greenman in_pcbrehash(inp); 1361df8bae1dSRodney W. Grimes } 136283e521ecSBjoern A. Zeeb #endif /* INET */ 1363df8bae1dSRodney W. Grimes 13644c7c478dSRobert Watson /* 136528696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1366c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 136728696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 136828696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 13694c7c478dSRobert Watson */ 137026f9a767SRodney W. Grimes void 1371136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1372df8bae1dSRodney W. Grimes { 13734c7c478dSRobert Watson 1374a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1375c0a211c5SRobert Watson 1376f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1377f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1378f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1379f3e7afe2SHans Petter Selasky #endif 13804c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 13814c7c478dSRobert Watson inp->inp_socket = NULL; 13824c7c478dSRobert Watson } 13834c7c478dSRobert Watson 1384c0a211c5SRobert Watson /* 138579bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 138679bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 138779bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 138852cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 138952cd27cbSRobert Watson * via a pcbgroup. 139079bdc6e5SRobert Watson * 139179bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 139279bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 139379bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 139479bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 139579bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 139679bdc6e5SRobert Watson * inpcb. 139779bdc6e5SRobert Watson * 139879bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 139979bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 140079bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 140179bdc6e5SRobert Watson * using in_pcbrele(). 1402c0a211c5SRobert Watson */ 140379bdc6e5SRobert Watson void 140479bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 14054c7c478dSRobert Watson { 1406df8bae1dSRodney W. Grimes 140779bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 140879bdc6e5SRobert Watson 140979bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 141079bdc6e5SRobert Watson } 141179bdc6e5SRobert Watson 141279bdc6e5SRobert Watson /* 141379bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 141479bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 141579bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 141679bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 141779bdc6e5SRobert Watson * 141879bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 141979bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 142079bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 142179bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 142279bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 142379bdc6e5SRobert Watson */ 142479bdc6e5SRobert Watson int 142579bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 142679bdc6e5SRobert Watson { 142779bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 142879bdc6e5SRobert Watson 142979bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 143079bdc6e5SRobert Watson 143179bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 143279bdc6e5SRobert Watson 1433df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1434df4e91d3SGleb Smirnoff /* 1435df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1436df4e91d3SGleb Smirnoff * this isn't the last reference. 1437df4e91d3SGleb Smirnoff */ 1438df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1439df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1440df4e91d3SGleb Smirnoff return (1); 1441df4e91d3SGleb Smirnoff } 144279bdc6e5SRobert Watson return (0); 1443df4e91d3SGleb Smirnoff } 144479bdc6e5SRobert Watson 144579bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 14463ee9c3c4SRandall Stewart #ifdef TCPHPTS 14473ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 14483ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 14493ee9c3c4SRandall Stewart /* 14503ee9c3c4SRandall Stewart * We should not be on the hpts at 14513ee9c3c4SRandall Stewart * this point in any form. we must 14523ee9c3c4SRandall Stewart * get the lock to be sure. 14533ee9c3c4SRandall Stewart */ 14543ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 14553ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 14563ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 14573ee9c3c4SRandall Stewart hpts, inp); 14583ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 14593ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 14603ee9c3c4SRandall Stewart if (inp->inp_in_input) 14613ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 14623ee9c3c4SRandall Stewart hpts, inp); 14633ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 14643ee9c3c4SRandall Stewart } 14653ee9c3c4SRandall Stewart #endif 146679bdc6e5SRobert Watson INP_RUNLOCK(inp); 146779bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 146879bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 146979bdc6e5SRobert Watson return (1); 147079bdc6e5SRobert Watson } 147179bdc6e5SRobert Watson 147279bdc6e5SRobert Watson int 147379bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 147479bdc6e5SRobert Watson { 147579bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 147679bdc6e5SRobert Watson 147779bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 147879bdc6e5SRobert Watson 147979bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 148079bdc6e5SRobert Watson 1481edd0e0b0SFabien Thomas if (refcount_release(&inp->inp_refcount) == 0) { 1482edd0e0b0SFabien Thomas /* 1483edd0e0b0SFabien Thomas * If the inpcb has been freed, let the caller know, even if 1484edd0e0b0SFabien Thomas * this isn't the last reference. 1485edd0e0b0SFabien Thomas */ 1486edd0e0b0SFabien Thomas if (inp->inp_flags2 & INP_FREED) { 1487edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1488edd0e0b0SFabien Thomas return (1); 1489edd0e0b0SFabien Thomas } 149079bdc6e5SRobert Watson return (0); 1491edd0e0b0SFabien Thomas } 149279bdc6e5SRobert Watson 149379bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 14943ee9c3c4SRandall Stewart #ifdef TCPHPTS 14953ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 14963ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 14973ee9c3c4SRandall Stewart /* 14983ee9c3c4SRandall Stewart * We should not be on the hpts at 14993ee9c3c4SRandall Stewart * this point in any form. we must 15003ee9c3c4SRandall Stewart * get the lock to be sure. 15013ee9c3c4SRandall Stewart */ 15023ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 15033ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 15043ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 15053ee9c3c4SRandall Stewart hpts, inp); 15063ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15073ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 15083ee9c3c4SRandall Stewart if (inp->inp_in_input) 15093ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 15103ee9c3c4SRandall Stewart hpts, inp); 15113ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15123ee9c3c4SRandall Stewart } 15133ee9c3c4SRandall Stewart #endif 151479bdc6e5SRobert Watson INP_WUNLOCK(inp); 151579bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 151679bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 151779bdc6e5SRobert Watson return (1); 151879bdc6e5SRobert Watson } 151979bdc6e5SRobert Watson 152079bdc6e5SRobert Watson /* 152179bdc6e5SRobert Watson * Temporary wrapper. 152279bdc6e5SRobert Watson */ 152379bdc6e5SRobert Watson int 152479bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 152579bdc6e5SRobert Watson { 152679bdc6e5SRobert Watson 152779bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 152879bdc6e5SRobert Watson } 152979bdc6e5SRobert Watson 1530ddece765SMatt Macy void 1531ddece765SMatt Macy in_pcblist_rele_rlocked(epoch_context_t ctx) 1532ddece765SMatt Macy { 1533ddece765SMatt Macy struct in_pcblist *il; 1534ddece765SMatt Macy struct inpcb *inp; 1535ddece765SMatt Macy struct inpcbinfo *pcbinfo; 1536ddece765SMatt Macy int i, n; 1537ddece765SMatt Macy 1538ddece765SMatt Macy il = __containerof(ctx, struct in_pcblist, il_epoch_ctx); 1539ddece765SMatt Macy pcbinfo = il->il_pcbinfo; 1540ddece765SMatt Macy n = il->il_count; 1541ddece765SMatt Macy INP_INFO_WLOCK(pcbinfo); 1542ddece765SMatt Macy for (i = 0; i < n; i++) { 1543ddece765SMatt Macy inp = il->il_inp_list[i]; 1544ddece765SMatt Macy INP_RLOCK(inp); 1545ddece765SMatt Macy if (!in_pcbrele_rlocked(inp)) 1546ddece765SMatt Macy INP_RUNLOCK(inp); 1547ddece765SMatt Macy } 1548ddece765SMatt Macy INP_INFO_WUNLOCK(pcbinfo); 1549ddece765SMatt Macy free(il, M_TEMP); 1550ddece765SMatt Macy } 1551ddece765SMatt Macy 1552addf2b20SMatt Macy static void 1553f09ee4fcSMatt Macy inpcbport_free(epoch_context_t ctx) 1554f09ee4fcSMatt Macy { 1555f09ee4fcSMatt Macy struct inpcbport *phd; 1556f09ee4fcSMatt Macy 1557f09ee4fcSMatt Macy phd = __containerof(ctx, struct inpcbport, phd_epoch_ctx); 1558f09ee4fcSMatt Macy free(phd, M_PCB); 1559f09ee4fcSMatt Macy } 1560f09ee4fcSMatt Macy 1561f09ee4fcSMatt Macy static void 1562addf2b20SMatt Macy in_pcbfree_deferred(epoch_context_t ctx) 1563addf2b20SMatt Macy { 1564addf2b20SMatt Macy struct inpcb *inp; 1565addf2b20SMatt Macy int released __unused; 1566addf2b20SMatt Macy 1567addf2b20SMatt Macy inp = __containerof(ctx, struct inpcb, inp_epoch_ctx); 1568addf2b20SMatt Macy 1569addf2b20SMatt Macy INP_WLOCK(inp); 1570addf2b20SMatt Macy #ifdef INET 1571f9be0386SMatt Macy struct ip_moptions *imo = inp->inp_moptions; 1572addf2b20SMatt Macy inp->inp_moptions = NULL; 1573addf2b20SMatt Macy #endif 1574addf2b20SMatt Macy /* XXXRW: Do as much as possible here. */ 1575addf2b20SMatt Macy #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1576addf2b20SMatt Macy if (inp->inp_sp != NULL) 1577addf2b20SMatt Macy ipsec_delete_pcbpolicy(inp); 1578addf2b20SMatt Macy #endif 1579addf2b20SMatt Macy #ifdef INET6 1580f9be0386SMatt Macy struct ip6_moptions *im6o = NULL; 1581addf2b20SMatt Macy if (inp->inp_vflag & INP_IPV6PROTO) { 1582addf2b20SMatt Macy ip6_freepcbopts(inp->in6p_outputopts); 1583f9be0386SMatt Macy im6o = inp->in6p_moptions; 1584addf2b20SMatt Macy inp->in6p_moptions = NULL; 1585addf2b20SMatt Macy } 1586addf2b20SMatt Macy #endif 1587addf2b20SMatt Macy if (inp->inp_options) 1588addf2b20SMatt Macy (void)m_free(inp->inp_options); 1589addf2b20SMatt Macy inp->inp_vflag = 0; 1590addf2b20SMatt Macy crfree(inp->inp_cred); 1591addf2b20SMatt Macy #ifdef MAC 1592addf2b20SMatt Macy mac_inpcb_destroy(inp); 1593addf2b20SMatt Macy #endif 1594addf2b20SMatt Macy released = in_pcbrele_wlocked(inp); 1595addf2b20SMatt Macy MPASS(released); 1596f9be0386SMatt Macy #ifdef INET6 1597f9be0386SMatt Macy ip6_freemoptions(im6o); 1598f9be0386SMatt Macy #endif 1599f9be0386SMatt Macy #ifdef INET 1600f9be0386SMatt Macy inp_freemoptions(imo); 1601f9be0386SMatt Macy #endif 1602addf2b20SMatt Macy } 1603addf2b20SMatt Macy 160479bdc6e5SRobert Watson /* 160579bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 160679bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 160779bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 160879bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 160979bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 161079bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 161179bdc6e5SRobert Watson * the pcbinfo lock is held. 161279bdc6e5SRobert Watson */ 161379bdc6e5SRobert Watson void 161479bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 161579bdc6e5SRobert Watson { 1616f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1617f42a83f2SMatt Macy 161879bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 161945a48d07SJonathan T. Looney KASSERT((inp->inp_flags2 & INP_FREED) == 0, 162045a48d07SJonathan T. Looney ("%s: called twice for pcb %p", __func__, inp)); 162145a48d07SJonathan T. Looney if (inp->inp_flags2 & INP_FREED) { 162245a48d07SJonathan T. Looney INP_WUNLOCK(inp); 162345a48d07SJonathan T. Looney return; 162445a48d07SJonathan T. Looney } 162545a48d07SJonathan T. Looney 1626ff9b006dSJulien Charbon #ifdef INVARIANTS 1627ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 1628079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(pcbinfo); 1629ff9b006dSJulien Charbon } else { 163079bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 1631ff9b006dSJulien Charbon } 1632ff9b006dSJulien Charbon #endif 163379bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1634f42a83f2SMatt Macy INP_LIST_WLOCK(pcbinfo); 1635f42a83f2SMatt Macy in_pcbremlists(inp); 1636f42a83f2SMatt Macy INP_LIST_WUNLOCK(pcbinfo); 1637fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1638addf2b20SMatt Macy /* mark as destruction in progress */ 1639f42a83f2SMatt Macy inp->inp_flags2 |= INP_FREED; 1640cb6bb230SMatt Macy INP_WUNLOCK(inp); 1641addf2b20SMatt Macy epoch_call(net_epoch_preempt, &inp->inp_epoch_ctx, in_pcbfree_deferred); 164228696211SRobert Watson } 164328696211SRobert Watson 164428696211SRobert Watson /* 1645c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1646c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1647c0a211c5SRobert Watson * 1648c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1649c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1650c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1651c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1652c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1653c0a211c5SRobert Watson * in_pcbdetach(). 1654c0a211c5SRobert Watson * 1655c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1656c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 165710702a28SRobert Watson */ 165810702a28SRobert Watson void 165910702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 166010702a28SRobert Watson { 166110702a28SRobert Watson 16628501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 16636573d758SMatt Macy #ifdef INVARIANTS 16646573d758SMatt Macy if (inp->inp_socket != NULL && inp->inp_ppcb != NULL) 16656573d758SMatt Macy MPASS(inp->inp_refcount > 1); 16666573d758SMatt Macy #endif 166710702a28SRobert Watson 1668fa046d87SRobert Watson /* 1669fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1670fa046d87SRobert Watson * the hash lock...? 1671fa046d87SRobert Watson */ 1672ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1673111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 167410702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 167510702a28SRobert Watson 1676fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 16771a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 1678b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 1679b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 1680b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 1681b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 1682f09ee4fcSMatt Macy epoch_call(net_epoch_preempt, &phd->phd_epoch_ctx, inpcbport_free); 168310702a28SRobert Watson } 1684fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1685111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 168652cd27cbSRobert Watson #ifdef PCBGROUP 168752cd27cbSRobert Watson in_pcbgroup_remove(inp); 168852cd27cbSRobert Watson #endif 168910702a28SRobert Watson } 169010702a28SRobert Watson } 169110702a28SRobert Watson 169267107f45SBjoern A. Zeeb #ifdef INET 169354d642bbSRobert Watson /* 169454d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 169554d642bbSRobert Watson */ 169626ef6ac4SDon Lewis struct sockaddr * 1697136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 169826ef6ac4SDon Lewis { 169926ef6ac4SDon Lewis struct sockaddr_in *sin; 170026ef6ac4SDon Lewis 17011ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1702a163d034SWarner Losh M_WAITOK | M_ZERO); 170326ef6ac4SDon Lewis sin->sin_family = AF_INET; 170426ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 170526ef6ac4SDon Lewis sin->sin_addr = *addr_p; 170626ef6ac4SDon Lewis sin->sin_port = port; 170726ef6ac4SDon Lewis 170826ef6ac4SDon Lewis return (struct sockaddr *)sin; 170926ef6ac4SDon Lewis } 171026ef6ac4SDon Lewis 1711117bcae7SGarrett Wollman int 171254d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1713df8bae1dSRodney W. Grimes { 1714136d4f1cSRobert Watson struct inpcb *inp; 171526ef6ac4SDon Lewis struct in_addr addr; 171626ef6ac4SDon Lewis in_port_t port; 171742fa505bSDavid Greenman 1718fdc984f7STor Egge inp = sotoinpcb(so); 171954d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 17206466b28aSRobert Watson 1721a69042a5SRobert Watson INP_RLOCK(inp); 172226ef6ac4SDon Lewis port = inp->inp_lport; 172326ef6ac4SDon Lewis addr = inp->inp_laddr; 1724a69042a5SRobert Watson INP_RUNLOCK(inp); 172542fa505bSDavid Greenman 172626ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1727117bcae7SGarrett Wollman return 0; 1728df8bae1dSRodney W. Grimes } 1729df8bae1dSRodney W. Grimes 1730117bcae7SGarrett Wollman int 173154d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1732df8bae1dSRodney W. Grimes { 1733136d4f1cSRobert Watson struct inpcb *inp; 173426ef6ac4SDon Lewis struct in_addr addr; 173526ef6ac4SDon Lewis in_port_t port; 173642fa505bSDavid Greenman 1737fdc984f7STor Egge inp = sotoinpcb(so); 173854d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 17396466b28aSRobert Watson 1740a69042a5SRobert Watson INP_RLOCK(inp); 174126ef6ac4SDon Lewis port = inp->inp_fport; 174226ef6ac4SDon Lewis addr = inp->inp_faddr; 1743a69042a5SRobert Watson INP_RUNLOCK(inp); 174442fa505bSDavid Greenman 174526ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1746117bcae7SGarrett Wollman return 0; 1747df8bae1dSRodney W. Grimes } 1748df8bae1dSRodney W. Grimes 174926f9a767SRodney W. Grimes void 1750136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1751136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1752d1c54148SJesper Skriver { 1753f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1754d1c54148SJesper Skriver 17553dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1756b872626dSMatt Macy CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 17578501a69cSRobert Watson INP_WLOCK(inp); 1758d1c54148SJesper Skriver #ifdef INET6 1759f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 17608501a69cSRobert Watson INP_WUNLOCK(inp); 1761d1c54148SJesper Skriver continue; 1762f76fcf6dSJeffrey Hsu } 1763d1c54148SJesper Skriver #endif 1764d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1765f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 17668501a69cSRobert Watson INP_WUNLOCK(inp); 1767d1c54148SJesper Skriver continue; 1768d1c54148SJesper Skriver } 17693dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 17708501a69cSRobert Watson INP_WUNLOCK(inp); 1771f76fcf6dSJeffrey Hsu } 17723dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1773d1c54148SJesper Skriver } 1774d1c54148SJesper Skriver 1775e43cc4aeSHajimu UMEMOTO void 1776136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1777e43cc4aeSHajimu UMEMOTO { 1778e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1779e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1780e43cc4aeSHajimu UMEMOTO int i, gap; 1781e43cc4aeSHajimu UMEMOTO 1782ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1783b872626dSMatt Macy CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 17848501a69cSRobert Watson INP_WLOCK(inp); 1785e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1786e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1787e43cc4aeSHajimu UMEMOTO imo != NULL) { 1788e43cc4aeSHajimu UMEMOTO /* 1789e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1790e43cc4aeSHajimu UMEMOTO * detached. 1791e43cc4aeSHajimu UMEMOTO */ 1792e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1793e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1794e43cc4aeSHajimu UMEMOTO 1795e43cc4aeSHajimu UMEMOTO /* 1796e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1797e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1798cb6bb230SMatt Macy * 1799cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1800e43cc4aeSHajimu UMEMOTO */ 1801e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1802e43cc4aeSHajimu UMEMOTO i++) { 1803e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1804f3e1324bSStephen Hurd IN_MULTI_LOCK_ASSERT(); 1805f3e1324bSStephen Hurd in_leavegroup_locked(imo->imo_membership[i], NULL); 1806e43cc4aeSHajimu UMEMOTO gap++; 1807e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1808e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1809e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1810e43cc4aeSHajimu UMEMOTO } 1811e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1812e43cc4aeSHajimu UMEMOTO } 18138501a69cSRobert Watson INP_WUNLOCK(inp); 1814e43cc4aeSHajimu UMEMOTO } 1815ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1816e43cc4aeSHajimu UMEMOTO } 1817e43cc4aeSHajimu UMEMOTO 1818df8bae1dSRodney W. Grimes /* 1819fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1820fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1821c3229e05SDavid Greenman */ 1822d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1823df8bae1dSRodney W. Grimes struct inpcb * 1824136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 182568e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1826df8bae1dSRodney W. Grimes { 1827136d4f1cSRobert Watson struct inpcb *inp; 1828d5e8a67eSHajimu UMEMOTO #ifdef INET6 1829d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1830d5e8a67eSHajimu UMEMOTO #else 1831d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1832d5e8a67eSHajimu UMEMOTO #endif 1833d5e8a67eSHajimu UMEMOTO int wildcard; 18347bc4aca7SDavid Greenman 183568e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 183668e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 183768e0d7e0SRobert Watson 1838fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 18391b73ca0bSSam Leffler 184068e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1841c3229e05SDavid Greenman struct inpcbhead *head; 1842c3229e05SDavid Greenman /* 1843c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1844c3229e05SDavid Greenman * matches the local address and port we're looking for. 1845c3229e05SDavid Greenman */ 1846712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1847712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1848b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 1849cfa1ca9dSYoshinobu Inoue #ifdef INET6 1850413628a7SBjoern A. Zeeb /* XXX inp locking */ 1851369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1852cfa1ca9dSYoshinobu Inoue continue; 1853cfa1ca9dSYoshinobu Inoue #endif 1854c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1855c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1856c3229e05SDavid Greenman inp->inp_lport == lport) { 1857c3229e05SDavid Greenman /* 1858413628a7SBjoern A. Zeeb * Found? 1859c3229e05SDavid Greenman */ 1860413628a7SBjoern A. Zeeb if (cred == NULL || 18610304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 18620304c731SJamie Gritton inp->inp_cred->cr_prison)) 1863c3229e05SDavid Greenman return (inp); 1864df8bae1dSRodney W. Grimes } 1865c3229e05SDavid Greenman } 1866c3229e05SDavid Greenman /* 1867c3229e05SDavid Greenman * Not found. 1868c3229e05SDavid Greenman */ 1869c3229e05SDavid Greenman return (NULL); 1870c3229e05SDavid Greenman } else { 1871c3229e05SDavid Greenman struct inpcbporthead *porthash; 1872c3229e05SDavid Greenman struct inpcbport *phd; 1873c3229e05SDavid Greenman struct inpcb *match = NULL; 1874c3229e05SDavid Greenman /* 1875c3229e05SDavid Greenman * Best fit PCB lookup. 1876c3229e05SDavid Greenman * 1877c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1878c3229e05SDavid Greenman * port hash list. 1879c3229e05SDavid Greenman */ 1880712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1881712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1882b872626dSMatt Macy CK_LIST_FOREACH(phd, porthash, phd_hash) { 1883c3229e05SDavid Greenman if (phd->phd_port == lport) 1884c3229e05SDavid Greenman break; 1885c3229e05SDavid Greenman } 1886c3229e05SDavid Greenman if (phd != NULL) { 1887c3229e05SDavid Greenman /* 1888c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1889c3229e05SDavid Greenman * fit. 1890c3229e05SDavid Greenman */ 1891b872626dSMatt Macy CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1892c3229e05SDavid Greenman wildcard = 0; 1893413628a7SBjoern A. Zeeb if (cred != NULL && 18940304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 18950304c731SJamie Gritton cred->cr_prison)) 1896413628a7SBjoern A. Zeeb continue; 1897cfa1ca9dSYoshinobu Inoue #ifdef INET6 1898413628a7SBjoern A. Zeeb /* XXX inp locking */ 1899369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1900cfa1ca9dSYoshinobu Inoue continue; 1901d5e8a67eSHajimu UMEMOTO /* 1902d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1903d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1904d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1905d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1906d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1907d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1908d5e8a67eSHajimu UMEMOTO * 1909d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1910d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1911d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1912d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1913d5e8a67eSHajimu UMEMOTO */ 1914d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1915d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1916cfa1ca9dSYoshinobu Inoue #endif 1917c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1918c3229e05SDavid Greenman wildcard++; 191915bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 192015bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 192115bd2b43SDavid Greenman wildcard++; 192215bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 192315bd2b43SDavid Greenman continue; 192415bd2b43SDavid Greenman } else { 192515bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 192615bd2b43SDavid Greenman wildcard++; 192715bd2b43SDavid Greenman } 1928df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1929df8bae1dSRodney W. Grimes match = inp; 1930df8bae1dSRodney W. Grimes matchwild = wildcard; 1931413628a7SBjoern A. Zeeb if (matchwild == 0) 1932df8bae1dSRodney W. Grimes break; 1933df8bae1dSRodney W. Grimes } 1934df8bae1dSRodney W. Grimes } 19353dbdc25cSDavid Greenman } 1936df8bae1dSRodney W. Grimes return (match); 1937df8bae1dSRodney W. Grimes } 1938c3229e05SDavid Greenman } 1939d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 194015bd2b43SDavid Greenman 19411a43cff9SSean Bruno static struct inpcb * 19421a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 19431a43cff9SSean Bruno const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr, 19441a43cff9SSean Bruno uint16_t fport, int lookupflags) 19451a43cff9SSean Bruno { 19468be02ee4SMark Johnston struct inpcb *local_wild; 19471a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 19481a43cff9SSean Bruno struct inpcblbgroup *grp; 19498be02ee4SMark Johnston uint32_t idx; 19501a43cff9SSean Bruno 19511a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 19521a43cff9SSean Bruno 19538be02ee4SMark Johnston hdr = &pcbinfo->ipi_lbgrouphashbase[INP_PCBLBGROUP_PORTHASH(lport, 19548be02ee4SMark Johnston pcbinfo->ipi_lbgrouphashmask)]; 19551a43cff9SSean Bruno 19561a43cff9SSean Bruno /* 19571a43cff9SSean Bruno * Order of socket selection: 19581a43cff9SSean Bruno * 1. non-wild. 19591a43cff9SSean Bruno * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD). 19601a43cff9SSean Bruno * 19611a43cff9SSean Bruno * NOTE: 19621a43cff9SSean Bruno * - Load balanced group does not contain jailed sockets 19631a43cff9SSean Bruno * - Load balanced group does not contain IPv4 mapped INET6 wild sockets 19641a43cff9SSean Bruno */ 19658be02ee4SMark Johnston local_wild = NULL; 196654af3d0dSMark Johnston CK_LIST_FOREACH(grp, hdr, il_list) { 19671a43cff9SSean Bruno #ifdef INET6 19681a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 19691a43cff9SSean Bruno continue; 19701a43cff9SSean Bruno #endif 19718be02ee4SMark Johnston if (grp->il_lport != lport) 19728be02ee4SMark Johnston continue; 19731a43cff9SSean Bruno 19748be02ee4SMark Johnston idx = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, lport, fport) % 19758be02ee4SMark Johnston grp->il_inpcnt; 19768be02ee4SMark Johnston if (grp->il_laddr.s_addr == laddr->s_addr) 19771a43cff9SSean Bruno return (grp->il_inp[idx]); 19781a43cff9SSean Bruno if (grp->il_laddr.s_addr == INADDR_ANY && 19798be02ee4SMark Johnston (lookupflags & INPLOOKUP_WILDCARD) != 0) 19801a43cff9SSean Bruno local_wild = grp->il_inp[idx]; 19811a43cff9SSean Bruno } 19821a43cff9SSean Bruno return (local_wild); 19831a43cff9SSean Bruno } 19841a43cff9SSean Bruno 198552cd27cbSRobert Watson #ifdef PCBGROUP 198652cd27cbSRobert Watson /* 198752cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 198852cd27cbSRobert Watson */ 198952cd27cbSRobert Watson static struct inpcb * 199052cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 199152cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 199252cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 199352cd27cbSRobert Watson { 199452cd27cbSRobert Watson struct inpcbhead *head; 199552cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 199652cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 19977fb2986fSJonathan T. Looney bool locked; 199852cd27cbSRobert Watson 199952cd27cbSRobert Watson /* 200052cd27cbSRobert Watson * First look for an exact match. 200152cd27cbSRobert Watson */ 200252cd27cbSRobert Watson tmpinp = NULL; 200352cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 200452cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 200552cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 2006feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 200752cd27cbSRobert Watson #ifdef INET6 200852cd27cbSRobert Watson /* XXX inp locking */ 200952cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 201052cd27cbSRobert Watson continue; 201152cd27cbSRobert Watson #endif 201252cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 201352cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 201452cd27cbSRobert Watson inp->inp_fport == fport && 201552cd27cbSRobert Watson inp->inp_lport == lport) { 201652cd27cbSRobert Watson /* 201752cd27cbSRobert Watson * XXX We should be able to directly return 201852cd27cbSRobert Watson * the inp here, without any checks. 201952cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 202052cd27cbSRobert Watson */ 202152cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 202252cd27cbSRobert Watson goto found; 202352cd27cbSRobert Watson if (tmpinp == NULL) 202452cd27cbSRobert Watson tmpinp = inp; 202552cd27cbSRobert Watson } 202652cd27cbSRobert Watson } 202752cd27cbSRobert Watson if (tmpinp != NULL) { 202852cd27cbSRobert Watson inp = tmpinp; 202952cd27cbSRobert Watson goto found; 203052cd27cbSRobert Watson } 203152cd27cbSRobert Watson 20320a100a6fSAdrian Chadd #ifdef RSS 20330a100a6fSAdrian Chadd /* 20340a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 20350a100a6fSAdrian Chadd * match for an RSS-local socket. 20360a100a6fSAdrian Chadd */ 20370a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 20380a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 20390a100a6fSAdrian Chadd #ifdef INET6 20400a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 20410a100a6fSAdrian Chadd #endif 20420a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 20430a100a6fSAdrian Chadd struct inpcbhead *head; 20440a100a6fSAdrian Chadd int injail; 20450a100a6fSAdrian Chadd 20460a100a6fSAdrian Chadd /* 20470a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 20480a100a6fSAdrian Chadd * 1. jailed, non-wild. 20490a100a6fSAdrian Chadd * 2. jailed, wild. 20500a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 20510a100a6fSAdrian Chadd * 4. non-jailed, wild. 20520a100a6fSAdrian Chadd */ 20530a100a6fSAdrian Chadd 20540a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 20550a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 2056feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 20570a100a6fSAdrian Chadd #ifdef INET6 20580a100a6fSAdrian Chadd /* XXX inp locking */ 20590a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 20600a100a6fSAdrian Chadd continue; 20610a100a6fSAdrian Chadd #endif 20620a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 20630a100a6fSAdrian Chadd inp->inp_lport != lport) 20640a100a6fSAdrian Chadd continue; 20650a100a6fSAdrian Chadd 20660a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 20670a100a6fSAdrian Chadd if (injail) { 20680a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 20690a100a6fSAdrian Chadd &laddr) != 0) 20700a100a6fSAdrian Chadd continue; 20710a100a6fSAdrian Chadd } else { 20720a100a6fSAdrian Chadd if (local_exact != NULL) 20730a100a6fSAdrian Chadd continue; 20740a100a6fSAdrian Chadd } 20750a100a6fSAdrian Chadd 20760a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 20770a100a6fSAdrian Chadd if (injail) 20780a100a6fSAdrian Chadd goto found; 20790a100a6fSAdrian Chadd else 20800a100a6fSAdrian Chadd local_exact = inp; 20810a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 20820a100a6fSAdrian Chadd #ifdef INET6 20830a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 20840a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 20850a100a6fSAdrian Chadd local_wild_mapped = inp; 20860a100a6fSAdrian Chadd else 20870a100a6fSAdrian Chadd #endif 20880a100a6fSAdrian Chadd if (injail) 20890a100a6fSAdrian Chadd jail_wild = inp; 20900a100a6fSAdrian Chadd else 20910a100a6fSAdrian Chadd local_wild = inp; 20920a100a6fSAdrian Chadd } 20930a100a6fSAdrian Chadd } /* LIST_FOREACH */ 20940a100a6fSAdrian Chadd 20950a100a6fSAdrian Chadd inp = jail_wild; 20960a100a6fSAdrian Chadd if (inp == NULL) 20970a100a6fSAdrian Chadd inp = local_exact; 20980a100a6fSAdrian Chadd if (inp == NULL) 20990a100a6fSAdrian Chadd inp = local_wild; 21000a100a6fSAdrian Chadd #ifdef INET6 21010a100a6fSAdrian Chadd if (inp == NULL) 21020a100a6fSAdrian Chadd inp = local_wild_mapped; 21030a100a6fSAdrian Chadd #endif 21040a100a6fSAdrian Chadd if (inp != NULL) 21050a100a6fSAdrian Chadd goto found; 21060a100a6fSAdrian Chadd } 21070a100a6fSAdrian Chadd #endif 21080a100a6fSAdrian Chadd 210952cd27cbSRobert Watson /* 211052cd27cbSRobert Watson * Then look for a wildcard match, if requested. 211152cd27cbSRobert Watson */ 211252cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 211352cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 211452cd27cbSRobert Watson #ifdef INET6 211552cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 211652cd27cbSRobert Watson #endif 211752cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 211852cd27cbSRobert Watson struct inpcbhead *head; 211952cd27cbSRobert Watson int injail; 212052cd27cbSRobert Watson 212152cd27cbSRobert Watson /* 212252cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 212352cd27cbSRobert Watson * 1. jailed, non-wild. 212452cd27cbSRobert Watson * 2. jailed, wild. 212552cd27cbSRobert Watson * 3. non-jailed, non-wild. 212652cd27cbSRobert Watson * 4. non-jailed, wild. 212752cd27cbSRobert Watson */ 212852cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 212952cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 2130feeef850SMatt Macy CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 213152cd27cbSRobert Watson #ifdef INET6 213252cd27cbSRobert Watson /* XXX inp locking */ 213352cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 213452cd27cbSRobert Watson continue; 213552cd27cbSRobert Watson #endif 213652cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 213752cd27cbSRobert Watson inp->inp_lport != lport) 213852cd27cbSRobert Watson continue; 213952cd27cbSRobert Watson 214052cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 214152cd27cbSRobert Watson if (injail) { 214252cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 214352cd27cbSRobert Watson &laddr) != 0) 214452cd27cbSRobert Watson continue; 214552cd27cbSRobert Watson } else { 214652cd27cbSRobert Watson if (local_exact != NULL) 214752cd27cbSRobert Watson continue; 214852cd27cbSRobert Watson } 214952cd27cbSRobert Watson 215052cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 215152cd27cbSRobert Watson if (injail) 215252cd27cbSRobert Watson goto found; 215352cd27cbSRobert Watson else 215452cd27cbSRobert Watson local_exact = inp; 215552cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 215652cd27cbSRobert Watson #ifdef INET6 215752cd27cbSRobert Watson /* XXX inp locking, NULL check */ 215852cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 215952cd27cbSRobert Watson local_wild_mapped = inp; 216052cd27cbSRobert Watson else 216183e521ecSBjoern A. Zeeb #endif 216252cd27cbSRobert Watson if (injail) 216352cd27cbSRobert Watson jail_wild = inp; 216452cd27cbSRobert Watson else 216552cd27cbSRobert Watson local_wild = inp; 216652cd27cbSRobert Watson } 216752cd27cbSRobert Watson } /* LIST_FOREACH */ 216852cd27cbSRobert Watson inp = jail_wild; 216952cd27cbSRobert Watson if (inp == NULL) 217052cd27cbSRobert Watson inp = local_exact; 217152cd27cbSRobert Watson if (inp == NULL) 217252cd27cbSRobert Watson inp = local_wild; 217352cd27cbSRobert Watson #ifdef INET6 217452cd27cbSRobert Watson if (inp == NULL) 217552cd27cbSRobert Watson inp = local_wild_mapped; 217683e521ecSBjoern A. Zeeb #endif 217752cd27cbSRobert Watson if (inp != NULL) 217852cd27cbSRobert Watson goto found; 217952cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 218052cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 218152cd27cbSRobert Watson return (NULL); 218252cd27cbSRobert Watson 218352cd27cbSRobert Watson found: 21847fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 2185b9a9e8e9SNavdeep Parhar locked = INP_TRY_WLOCK(inp); 21867fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 2187b9a9e8e9SNavdeep Parhar locked = INP_TRY_RLOCK(inp); 21887fb2986fSJonathan T. Looney else 21897fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 2190483305b9SMatt Macy if (__predict_false(locked && (inp->inp_flags2 & INP_FREED))) { 2191483305b9SMatt Macy if (lookupflags & INPLOOKUP_WLOCKPCB) 2192483305b9SMatt Macy INP_WUNLOCK(inp); 2193483305b9SMatt Macy else 2194483305b9SMatt Macy INP_RUNLOCK(inp); 2195483305b9SMatt Macy return (NULL); 2196483305b9SMatt Macy } else if (!locked) 219752cd27cbSRobert Watson in_pcbref(inp); 219852cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 21997fb2986fSJonathan T. Looney if (!locked) { 220052cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 220152cd27cbSRobert Watson INP_WLOCK(inp); 220252cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 220352cd27cbSRobert Watson return (NULL); 22047fb2986fSJonathan T. Looney } else { 220552cd27cbSRobert Watson INP_RLOCK(inp); 220652cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 220752cd27cbSRobert Watson return (NULL); 22087fb2986fSJonathan T. Looney } 22097fb2986fSJonathan T. Looney } 22107fb2986fSJonathan T. Looney #ifdef INVARIANTS 22117fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 22127fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 22137fb2986fSJonathan T. Looney else 22147fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 22157fb2986fSJonathan T. Looney #endif 221652cd27cbSRobert Watson return (inp); 221752cd27cbSRobert Watson } 221852cd27cbSRobert Watson #endif /* PCBGROUP */ 221952cd27cbSRobert Watson 222015bd2b43SDavid Greenman /* 2221fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2222fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 2223fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 222415bd2b43SDavid Greenman */ 2225fa046d87SRobert Watson static struct inpcb * 2226fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 222768e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2228136d4f1cSRobert Watson struct ifnet *ifp) 222915bd2b43SDavid Greenman { 223015bd2b43SDavid Greenman struct inpcbhead *head; 2231413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 223215bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 223315bd2b43SDavid Greenman 22346573d758SMatt Macy #ifdef INVARIANTS 223568e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 223668e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 22376573d758SMatt Macy if (!mtx_owned(&pcbinfo->ipi_hash_lock)) 22386573d758SMatt Macy MPASS(in_epoch_verbose(net_epoch_preempt, 1)); 22396573d758SMatt Macy #endif 224015bd2b43SDavid Greenman /* 224115bd2b43SDavid Greenman * First look for an exact match. 224215bd2b43SDavid Greenman */ 2243413628a7SBjoern A. Zeeb tmpinp = NULL; 2244712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 2245712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 2246b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2247cfa1ca9dSYoshinobu Inoue #ifdef INET6 2248413628a7SBjoern A. Zeeb /* XXX inp locking */ 2249369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2250cfa1ca9dSYoshinobu Inoue continue; 2251cfa1ca9dSYoshinobu Inoue #endif 22526d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2253ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2254ca98b82cSDavid Greenman inp->inp_fport == fport && 2255413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 2256413628a7SBjoern A. Zeeb /* 2257413628a7SBjoern A. Zeeb * XXX We should be able to directly return 2258413628a7SBjoern A. Zeeb * the inp here, without any checks. 2259413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 2260413628a7SBjoern A. Zeeb */ 22610304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 2262c3229e05SDavid Greenman return (inp); 2263413628a7SBjoern A. Zeeb if (tmpinp == NULL) 2264413628a7SBjoern A. Zeeb tmpinp = inp; 2265c3229e05SDavid Greenman } 2266413628a7SBjoern A. Zeeb } 2267413628a7SBjoern A. Zeeb if (tmpinp != NULL) 2268413628a7SBjoern A. Zeeb return (tmpinp); 2269e3fd5ffdSRobert Watson 2270e3fd5ffdSRobert Watson /* 22711a43cff9SSean Bruno * Then look in lb group (for wildcard match). 22721a43cff9SSean Bruno */ 2273d9ff5789SMark Johnston if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 22741a43cff9SSean Bruno inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr, 22751a43cff9SSean Bruno fport, lookupflags); 2276d9ff5789SMark Johnston if (inp != NULL) 22771a43cff9SSean Bruno return (inp); 22781a43cff9SSean Bruno } 22791a43cff9SSean Bruno 22801a43cff9SSean Bruno /* 2281e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 2282e3fd5ffdSRobert Watson */ 228368e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2284413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 2285e3fd5ffdSRobert Watson #ifdef INET6 2286cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 2287e3fd5ffdSRobert Watson #endif 2288413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 2289413628a7SBjoern A. Zeeb int injail; 2290413628a7SBjoern A. Zeeb 2291413628a7SBjoern A. Zeeb /* 2292413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2293413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2294413628a7SBjoern A. Zeeb * 2. jailed, wild. 2295413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2296413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2297413628a7SBjoern A. Zeeb */ 22986d6a026bSDavid Greenman 2299712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2300712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2301b872626dSMatt Macy CK_LIST_FOREACH(inp, head, inp_hash) { 2302cfa1ca9dSYoshinobu Inoue #ifdef INET6 2303413628a7SBjoern A. Zeeb /* XXX inp locking */ 2304369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2305cfa1ca9dSYoshinobu Inoue continue; 2306cfa1ca9dSYoshinobu Inoue #endif 2307413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2308413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2309413628a7SBjoern A. Zeeb continue; 2310413628a7SBjoern A. Zeeb 23110304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2312413628a7SBjoern A. Zeeb if (injail) { 2313b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2314b89e82ddSJamie Gritton &laddr) != 0) 2315413628a7SBjoern A. Zeeb continue; 2316413628a7SBjoern A. Zeeb } else { 2317413628a7SBjoern A. Zeeb if (local_exact != NULL) 2318413628a7SBjoern A. Zeeb continue; 2319413628a7SBjoern A. Zeeb } 2320413628a7SBjoern A. Zeeb 2321413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2322413628a7SBjoern A. Zeeb if (injail) 2323c3229e05SDavid Greenman return (inp); 2324413628a7SBjoern A. Zeeb else 2325413628a7SBjoern A. Zeeb local_exact = inp; 2326413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2327e3fd5ffdSRobert Watson #ifdef INET6 2328413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23295cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2330cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2331cfa1ca9dSYoshinobu Inoue else 233283e521ecSBjoern A. Zeeb #endif 2333413628a7SBjoern A. Zeeb if (injail) 2334413628a7SBjoern A. Zeeb jail_wild = inp; 2335413628a7SBjoern A. Zeeb else 23366d6a026bSDavid Greenman local_wild = inp; 23376d6a026bSDavid Greenman } 2338413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2339413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2340413628a7SBjoern A. Zeeb return (jail_wild); 2341413628a7SBjoern A. Zeeb if (local_exact != NULL) 2342413628a7SBjoern A. Zeeb return (local_exact); 2343413628a7SBjoern A. Zeeb if (local_wild != NULL) 2344c3229e05SDavid Greenman return (local_wild); 2345413628a7SBjoern A. Zeeb #ifdef INET6 2346413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2347413628a7SBjoern A. Zeeb return (local_wild_mapped); 234883e521ecSBjoern A. Zeeb #endif 234968e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2350413628a7SBjoern A. Zeeb 23516d6a026bSDavid Greenman return (NULL); 235215bd2b43SDavid Greenman } 2353fa046d87SRobert Watson 2354fa046d87SRobert Watson /* 2355fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2356fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2357fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2358fa046d87SRobert Watson */ 2359fa046d87SRobert Watson static struct inpcb * 2360fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2361fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2362fa046d87SRobert Watson struct ifnet *ifp) 2363fa046d87SRobert Watson { 2364fa046d87SRobert Watson struct inpcb *inp; 2365fa046d87SRobert Watson 2366fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 2367fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2368fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 2369fa046d87SRobert Watson if (inp != NULL) { 2370fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 2371fa046d87SRobert Watson INP_WLOCK(inp); 23723d348772SMatt Macy if (__predict_false(inp->inp_flags2 & INP_FREED)) { 23733d348772SMatt Macy INP_WUNLOCK(inp); 23743d348772SMatt Macy inp = NULL; 23753d348772SMatt Macy } 23763d348772SMatt Macy } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 2377fa046d87SRobert Watson INP_RLOCK(inp); 23783d348772SMatt Macy if (__predict_false(inp->inp_flags2 & INP_FREED)) { 23793d348772SMatt Macy INP_RUNLOCK(inp); 23803d348772SMatt Macy inp = NULL; 23817fb2986fSJonathan T. Looney } 23823d348772SMatt Macy } else 23833d348772SMatt Macy panic("%s: locking bug", __func__); 23847fb2986fSJonathan T. Looney #ifdef INVARIANTS 23853d348772SMatt Macy if (inp != NULL) { 23867fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 23877fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 23887fb2986fSJonathan T. Looney else 23897fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 23903d348772SMatt Macy } 23917fb2986fSJonathan T. Looney #endif 23923d348772SMatt Macy } 2393fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 2394fa046d87SRobert Watson return (inp); 2395fa046d87SRobert Watson } 2396fa046d87SRobert Watson 2397fa046d87SRobert Watson /* 2398d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2399d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 240052cd27cbSRobert Watson * 240152cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 2402fa046d87SRobert Watson */ 2403fa046d87SRobert Watson struct inpcb * 2404fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2405fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2406fa046d87SRobert Watson { 24077527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 240852cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 240952cd27cbSRobert Watson #endif 2410fa046d87SRobert Watson 2411fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2412fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2413fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2414fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 2415fa046d87SRobert Watson 24167527624eSRobert Watson /* 24177527624eSRobert Watson * When not using RSS, use connection groups in preference to the 24187527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 24197527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 24207527624eSRobert Watson * in software. 24217527624eSRobert Watson * 24227527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 24237527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 24247527624eSRobert Watson * in software. 24257527624eSRobert Watson */ 24267527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 242752cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 242852cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 242952cd27cbSRobert Watson fport); 243052cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 243152cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 243252cd27cbSRobert Watson } 243352cd27cbSRobert Watson #endif 2434fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2435fa046d87SRobert Watson lookupflags, ifp)); 2436fa046d87SRobert Watson } 2437d3c1f003SRobert Watson 2438d3c1f003SRobert Watson struct inpcb * 2439d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2440d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2441d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2442d3c1f003SRobert Watson { 244352cd27cbSRobert Watson #ifdef PCBGROUP 244452cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 244552cd27cbSRobert Watson #endif 2446d3c1f003SRobert Watson 2447d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2448d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2449d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2450d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2451d3c1f003SRobert Watson 245252cd27cbSRobert Watson #ifdef PCBGROUP 24537527624eSRobert Watson /* 24547527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 24557527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 24567527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 24577527624eSRobert Watson * using RSS. 24587527624eSRobert Watson * 24597527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 24607527624eSRobert Watson */ 24617527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 24627527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 246352cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 246452cd27cbSRobert Watson m->m_pkthdr.flowid); 246552cd27cbSRobert Watson if (pcbgroup != NULL) 246652cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 246752cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 24687527624eSRobert Watson #ifndef RSS 246952cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 247052cd27cbSRobert Watson fport); 247152cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 247252cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 24737527624eSRobert Watson #endif 247452cd27cbSRobert Watson } 247552cd27cbSRobert Watson #endif 2476d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2477d3c1f003SRobert Watson lookupflags, ifp)); 2478d3c1f003SRobert Watson } 247967107f45SBjoern A. Zeeb #endif /* INET */ 248015bd2b43SDavid Greenman 24817bc4aca7SDavid Greenman /* 2482c3229e05SDavid Greenman * Insert PCB onto various hash lists. 24837bc4aca7SDavid Greenman */ 248452cd27cbSRobert Watson static int 248552cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 248615bd2b43SDavid Greenman { 2487c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2488c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2489c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2490c3229e05SDavid Greenman struct inpcbport *phd; 2491cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 24921a43cff9SSean Bruno int so_options; 249315bd2b43SDavid Greenman 24948501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2495fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2496fa046d87SRobert Watson 2497111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2498111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2499602cc7f1SRobert Watson 2500cfa1ca9dSYoshinobu Inoue #ifdef INET6 2501cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 25021b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2503cfa1ca9dSYoshinobu Inoue else 250483e521ecSBjoern A. Zeeb #endif 2505cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2506cfa1ca9dSYoshinobu Inoue 2507712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2508712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 250915bd2b43SDavid Greenman 2510712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2511712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2512c3229e05SDavid Greenman 2513c3229e05SDavid Greenman /* 25141a43cff9SSean Bruno * Add entry to load balance group. 25151a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 25161a43cff9SSean Bruno */ 25171a43cff9SSean Bruno so_options = inp_so_options(inp); 25181a43cff9SSean Bruno if (so_options & SO_REUSEPORT_LB) { 25191a43cff9SSean Bruno int ret = in_pcbinslbgrouphash(inp); 25201a43cff9SSean Bruno if (ret) { 25211a43cff9SSean Bruno /* pcb lb group malloc fail (ret=ENOBUFS). */ 25221a43cff9SSean Bruno return (ret); 25231a43cff9SSean Bruno } 25241a43cff9SSean Bruno } 25251a43cff9SSean Bruno 25261a43cff9SSean Bruno /* 2527c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2528c3229e05SDavid Greenman */ 2529b872626dSMatt Macy CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2530c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2531c3229e05SDavid Greenman break; 2532c3229e05SDavid Greenman } 2533c3229e05SDavid Greenman /* 2534c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2535c3229e05SDavid Greenman */ 2536c3229e05SDavid Greenman if (phd == NULL) { 25371ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2538c3229e05SDavid Greenman if (phd == NULL) { 2539c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2540c3229e05SDavid Greenman } 2541f09ee4fcSMatt Macy bzero(&phd->phd_epoch_ctx, sizeof(struct epoch_context)); 2542c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2543b872626dSMatt Macy CK_LIST_INIT(&phd->phd_pcblist); 2544b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2545c3229e05SDavid Greenman } 2546c3229e05SDavid Greenman inp->inp_phd = phd; 2547b872626dSMatt Macy CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2548b872626dSMatt Macy CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2549111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 255052cd27cbSRobert Watson #ifdef PCBGROUP 255152cd27cbSRobert Watson if (do_pcbgroup_update) 255252cd27cbSRobert Watson in_pcbgroup_update(inp); 255352cd27cbSRobert Watson #endif 2554c3229e05SDavid Greenman return (0); 255515bd2b43SDavid Greenman } 255615bd2b43SDavid Greenman 2557c3229e05SDavid Greenman /* 255852cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 255952cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 256052cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 256152cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 256252cd27cbSRobert Watson * pcbgroup until later. 256352cd27cbSRobert Watson * 256452cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 256552cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 256652cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 256752cd27cbSRobert Watson */ 256852cd27cbSRobert Watson int 256952cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 257052cd27cbSRobert Watson { 257152cd27cbSRobert Watson 257252cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 257352cd27cbSRobert Watson } 257452cd27cbSRobert Watson 257552cd27cbSRobert Watson int 257652cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 257752cd27cbSRobert Watson { 257852cd27cbSRobert Watson 257952cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 258052cd27cbSRobert Watson } 258152cd27cbSRobert Watson 258252cd27cbSRobert Watson /* 2583c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2584c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2585c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2586c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2587c3229e05SDavid Greenman */ 258815bd2b43SDavid Greenman void 2589d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 259015bd2b43SDavid Greenman { 259159daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 259215bd2b43SDavid Greenman struct inpcbhead *head; 2593cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 259415bd2b43SDavid Greenman 25958501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2596fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2597fa046d87SRobert Watson 2598111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2599111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2600602cc7f1SRobert Watson 2601cfa1ca9dSYoshinobu Inoue #ifdef INET6 2602cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 26031b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2604cfa1ca9dSYoshinobu Inoue else 260583e521ecSBjoern A. Zeeb #endif 2606cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2607cfa1ca9dSYoshinobu Inoue 2608712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2609712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 261015bd2b43SDavid Greenman 2611b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2612b872626dSMatt Macy CK_LIST_INSERT_HEAD(head, inp, inp_hash); 261352cd27cbSRobert Watson 261452cd27cbSRobert Watson #ifdef PCBGROUP 261552cd27cbSRobert Watson if (m != NULL) 261652cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 261752cd27cbSRobert Watson else 261852cd27cbSRobert Watson in_pcbgroup_update(inp); 261952cd27cbSRobert Watson #endif 2620c3229e05SDavid Greenman } 2621c3229e05SDavid Greenman 2622d3c1f003SRobert Watson void 2623d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2624d3c1f003SRobert Watson { 2625d3c1f003SRobert Watson 2626d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2627d3c1f003SRobert Watson } 2628d3c1f003SRobert Watson 2629c3229e05SDavid Greenman /* 2630c3229e05SDavid Greenman * Remove PCB from various lists. 2631c3229e05SDavid Greenman */ 26326d888973SRobert Watson static void 2633136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2634c3229e05SDavid Greenman { 263559daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 263659daba27SSam Leffler 2637ff9b006dSJulien Charbon #ifdef INVARIANTS 2638ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 2639ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 2640ff9b006dSJulien Charbon } else { 264159daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 2642ff9b006dSJulien Charbon } 2643ff9b006dSJulien Charbon #endif 2644ff9b006dSJulien Charbon 26458501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2646ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 264759daba27SSam Leffler 264859daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2649111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2650c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2651c3229e05SDavid Greenman 2652fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 26531a43cff9SSean Bruno 26541a43cff9SSean Bruno /* XXX: Only do if SO_REUSEPORT_LB set? */ 26551a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 26561a43cff9SSean Bruno 2657b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_hash); 2658b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_portlist); 2659b872626dSMatt Macy if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 2660b872626dSMatt Macy CK_LIST_REMOVE(phd, phd_hash); 2661700e893cSMatt Macy epoch_call(net_epoch_preempt, &phd->phd_epoch_ctx, inpcbport_free); 2662c3229e05SDavid Greenman } 2663fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2664111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2665c3229e05SDavid Greenman } 2666b872626dSMatt Macy CK_LIST_REMOVE(inp, inp_list); 266759daba27SSam Leffler pcbinfo->ipi_count--; 266852cd27cbSRobert Watson #ifdef PCBGROUP 266952cd27cbSRobert Watson in_pcbgroup_remove(inp); 267052cd27cbSRobert Watson #endif 267115bd2b43SDavid Greenman } 267275c13541SPoul-Henning Kamp 2673a557af22SRobert Watson /* 267484cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 267584cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 267684cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 267784cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 267884cc0778SGeorge V. Neville-Neil */ 267984cc0778SGeorge V. Neville-Neil void 268084cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 268184cc0778SGeorge V. Neville-Neil { 268284cc0778SGeorge V. Neville-Neil 2683fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 268484cc0778SGeorge V. Neville-Neil return; 268584cc0778SGeorge V. Neville-Neil } 268684cc0778SGeorge V. Neville-Neil 268784cc0778SGeorge V. Neville-Neil /* 2688a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2689a557af22SRobert Watson * label change into the in_pcb for the socket. 2690a557af22SRobert Watson */ 2691a557af22SRobert Watson void 2692136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2693a557af22SRobert Watson { 2694a557af22SRobert Watson #ifdef MAC 2695a557af22SRobert Watson struct inpcb *inp; 2696a557af22SRobert Watson 26974c7c478dSRobert Watson inp = sotoinpcb(so); 26984c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2699602cc7f1SRobert Watson 27008501a69cSRobert Watson INP_WLOCK(inp); 2701310e7cebSRobert Watson SOCK_LOCK(so); 2702a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2703310e7cebSRobert Watson SOCK_UNLOCK(so); 27048501a69cSRobert Watson INP_WUNLOCK(inp); 2705a557af22SRobert Watson #endif 2706a557af22SRobert Watson } 27075f311da2SMike Silbersack 27085f311da2SMike Silbersack /* 2709ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2710ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2711ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2712ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2713ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 27145f311da2SMike Silbersack */ 2715aae49dd3SBjoern A. Zeeb static void 2716136d4f1cSRobert Watson ipport_tick(void *xtp) 27175f311da2SMike Silbersack { 27188b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2719ad3a630fSRobert Watson 27205ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 27218b615593SMarko Zec VNET_FOREACH(vnet_iter) { 27228b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 27238b615593SMarko Zec if (V_ipport_tcpallocs <= 27248b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2725603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2726603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2727ad3a630fSRobert Watson } else 2728603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2729603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 27308b615593SMarko Zec CURVNET_RESTORE(); 27318b615593SMarko Zec } 27325ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 27335f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 27345f311da2SMike Silbersack } 2735497057eeSRobert Watson 2736aae49dd3SBjoern A. Zeeb static void 2737aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2738aae49dd3SBjoern A. Zeeb { 2739aae49dd3SBjoern A. Zeeb 2740aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2741aae49dd3SBjoern A. Zeeb } 2742aae49dd3SBjoern A. Zeeb 2743aae49dd3SBjoern A. Zeeb /* 2744aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2745aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2746aae49dd3SBjoern A. Zeeb */ 2747aae49dd3SBjoern A. Zeeb static void 2748aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2749aae49dd3SBjoern A. Zeeb { 2750aae49dd3SBjoern A. Zeeb 2751aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2752fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2753aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2754aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2755aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2756aae49dd3SBjoern A. Zeeb } 2757aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2758aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2759aae49dd3SBjoern A. Zeeb 27603d585327SKip Macy void 27613d585327SKip Macy inp_wlock(struct inpcb *inp) 27623d585327SKip Macy { 27633d585327SKip Macy 27648501a69cSRobert Watson INP_WLOCK(inp); 27653d585327SKip Macy } 27663d585327SKip Macy 27673d585327SKip Macy void 27683d585327SKip Macy inp_wunlock(struct inpcb *inp) 27693d585327SKip Macy { 27703d585327SKip Macy 27718501a69cSRobert Watson INP_WUNLOCK(inp); 27723d585327SKip Macy } 27733d585327SKip Macy 27743d585327SKip Macy void 27753d585327SKip Macy inp_rlock(struct inpcb *inp) 27763d585327SKip Macy { 27773d585327SKip Macy 2778a69042a5SRobert Watson INP_RLOCK(inp); 27793d585327SKip Macy } 27803d585327SKip Macy 27813d585327SKip Macy void 27823d585327SKip Macy inp_runlock(struct inpcb *inp) 27833d585327SKip Macy { 27843d585327SKip Macy 2785a69042a5SRobert Watson INP_RUNLOCK(inp); 27863d585327SKip Macy } 27873d585327SKip Macy 2788c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 27893d585327SKip Macy void 2790e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 27913d585327SKip Macy { 27923d585327SKip Macy 27938501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 27943d585327SKip Macy } 27953d585327SKip Macy 27963d585327SKip Macy void 2797e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 27983d585327SKip Macy { 27993d585327SKip Macy 28003d585327SKip Macy INP_UNLOCK_ASSERT(inp); 28013d585327SKip Macy } 28023d585327SKip Macy #endif 28033d585327SKip Macy 28049378e437SKip Macy void 28059378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 28069378e437SKip Macy { 28079378e437SKip Macy struct inpcb *inp; 28089378e437SKip Macy 2809ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 2810b872626dSMatt Macy CK_LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 28119378e437SKip Macy INP_WLOCK(inp); 28129378e437SKip Macy func(inp, arg); 28139378e437SKip Macy INP_WUNLOCK(inp); 28149378e437SKip Macy } 2815ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 28169378e437SKip Macy } 28179378e437SKip Macy 28189378e437SKip Macy struct socket * 28199378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 28209378e437SKip Macy { 28219378e437SKip Macy 28229378e437SKip Macy INP_WLOCK_ASSERT(inp); 28239378e437SKip Macy return (inp->inp_socket); 28249378e437SKip Macy } 28259378e437SKip Macy 28269378e437SKip Macy struct tcpcb * 28279378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 28289378e437SKip Macy { 28299378e437SKip Macy 28309378e437SKip Macy INP_WLOCK_ASSERT(inp); 28319378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 28329378e437SKip Macy } 28339378e437SKip Macy 28349378e437SKip Macy int 28359378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 28369378e437SKip Macy { 28379378e437SKip Macy 28389378e437SKip Macy return (inp->inp_ip_tos); 28399378e437SKip Macy } 28409378e437SKip Macy 28419378e437SKip Macy void 28429378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 28439378e437SKip Macy { 28449378e437SKip Macy 28459378e437SKip Macy inp->inp_ip_tos = val; 28469378e437SKip Macy } 28479378e437SKip Macy 28489378e437SKip Macy void 2849df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 28509d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 28519378e437SKip Macy { 28529378e437SKip Macy 28539d29c635SKip Macy INP_LOCK_ASSERT(inp); 2854df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2855df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 28569378e437SKip Macy *lp = inp->inp_lport; 28579378e437SKip Macy *fp = inp->inp_fport; 28589378e437SKip Macy } 28599378e437SKip Macy 2860dd0e6c38SKip Macy struct inpcb * 2861dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2862dd0e6c38SKip Macy { 2863dd0e6c38SKip Macy 2864dd0e6c38SKip Macy return (sotoinpcb(so)); 2865dd0e6c38SKip Macy } 2866dd0e6c38SKip Macy 2867dd0e6c38SKip Macy struct tcpcb * 2868dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2869dd0e6c38SKip Macy { 2870dd0e6c38SKip Macy 2871dd0e6c38SKip Macy return (sototcpcb(so)); 2872dd0e6c38SKip Macy } 2873dd0e6c38SKip Macy 2874cc65eb4eSGleb Smirnoff /* 2875cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2876cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2877cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2878cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2879cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2880cc65eb4eSGleb Smirnoff * hidden from users. 2881cc65eb4eSGleb Smirnoff */ 2882cc65eb4eSGleb Smirnoff void 2883cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2884cc65eb4eSGleb Smirnoff { 2885cc65eb4eSGleb Smirnoff 2886*79db6fe7SMark Johnston bzero(xi, sizeof(*xi)); 2887cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2888cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2889cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2890cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2891cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 28923a20f06aSBrooks Davis xi->inp_ppcb = (uintptr_t)inp->inp_ppcb; 2893cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2894cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2895cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2896cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2897cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2898cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2899cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2900cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2901cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2902cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2903cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2904cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2905cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2906cc65eb4eSGleb Smirnoff } 2907cc65eb4eSGleb Smirnoff 2908497057eeSRobert Watson #ifdef DDB 2909497057eeSRobert Watson static void 2910497057eeSRobert Watson db_print_indent(int indent) 2911497057eeSRobert Watson { 2912497057eeSRobert Watson int i; 2913497057eeSRobert Watson 2914497057eeSRobert Watson for (i = 0; i < indent; i++) 2915497057eeSRobert Watson db_printf(" "); 2916497057eeSRobert Watson } 2917497057eeSRobert Watson 2918497057eeSRobert Watson static void 2919497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2920497057eeSRobert Watson { 2921497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2922497057eeSRobert Watson 2923497057eeSRobert Watson db_print_indent(indent); 2924497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2925497057eeSRobert Watson 2926497057eeSRobert Watson indent += 2; 2927497057eeSRobert Watson 292803dc38a4SRobert Watson #ifdef INET6 2929dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2930497057eeSRobert Watson /* IPv6. */ 2931497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2932497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 293383e521ecSBjoern A. Zeeb } else 293403dc38a4SRobert Watson #endif 293583e521ecSBjoern A. Zeeb { 2936497057eeSRobert Watson /* IPv4. */ 2937497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2938497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2939497057eeSRobert Watson } 2940497057eeSRobert Watson db_print_indent(indent); 2941497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2942497057eeSRobert Watson ntohs(inc->inc_lport)); 2943497057eeSRobert Watson db_print_indent(indent); 2944497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2945497057eeSRobert Watson ntohs(inc->inc_fport)); 2946497057eeSRobert Watson } 2947497057eeSRobert Watson 2948497057eeSRobert Watson static void 2949497057eeSRobert Watson db_print_inpflags(int inp_flags) 2950497057eeSRobert Watson { 2951497057eeSRobert Watson int comma; 2952497057eeSRobert Watson 2953497057eeSRobert Watson comma = 0; 2954497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2955497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2956497057eeSRobert Watson comma = 1; 2957497057eeSRobert Watson } 2958497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2959497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2960497057eeSRobert Watson comma = 1; 2961497057eeSRobert Watson } 2962497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2963497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2964497057eeSRobert Watson comma = 1; 2965497057eeSRobert Watson } 2966dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2967dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2968dce33a45SErmal Luçi comma = 1; 2969dce33a45SErmal Luçi } 2970497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2971497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2972497057eeSRobert Watson comma = 1; 2973497057eeSRobert Watson } 2974497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2975497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2976497057eeSRobert Watson comma = 1; 2977497057eeSRobert Watson } 2978497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2979497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2980497057eeSRobert Watson comma = 1; 2981497057eeSRobert Watson } 2982497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2983497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2984497057eeSRobert Watson comma = 1; 2985497057eeSRobert Watson } 2986497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2987497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2988497057eeSRobert Watson comma = 1; 2989497057eeSRobert Watson } 2990497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2991497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2992497057eeSRobert Watson comma = 1; 2993497057eeSRobert Watson } 2994497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2995497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2996497057eeSRobert Watson comma = 1; 2997497057eeSRobert Watson } 2998497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2999497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 3000497057eeSRobert Watson comma = 1; 3001497057eeSRobert Watson } 30023cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 30033cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 30043cca425bSMichael Tuexen comma = 1; 30053cca425bSMichael Tuexen } 3006497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 3007497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3008497057eeSRobert Watson comma = 1; 3009497057eeSRobert Watson } 3010497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 3011497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3012497057eeSRobert Watson comma = 1; 3013497057eeSRobert Watson } 3014497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 3015497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3016497057eeSRobert Watson comma = 1; 3017497057eeSRobert Watson } 3018497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 3019497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3020497057eeSRobert Watson comma = 1; 3021497057eeSRobert Watson } 3022497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 3023497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3024497057eeSRobert Watson comma = 1; 3025497057eeSRobert Watson } 3026497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 3027497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3028497057eeSRobert Watson comma = 1; 3029497057eeSRobert Watson } 3030497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 3031497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3032497057eeSRobert Watson comma = 1; 3033497057eeSRobert Watson } 3034497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 3035497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3036497057eeSRobert Watson comma = 1; 3037497057eeSRobert Watson } 3038497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 3039497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3040497057eeSRobert Watson comma = 1; 3041497057eeSRobert Watson } 3042ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 3043ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 3044ad71fe3cSRobert Watson comma = 1; 3045ad71fe3cSRobert Watson } 3046ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 3047ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3048ad71fe3cSRobert Watson comma = 1; 3049ad71fe3cSRobert Watson } 3050ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 3051ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 3052ad71fe3cSRobert Watson comma = 1; 3053ad71fe3cSRobert Watson } 3054ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 3055ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3056ad71fe3cSRobert Watson comma = 1; 3057ad71fe3cSRobert Watson } 3058497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 3059497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3060497057eeSRobert Watson comma = 1; 3061497057eeSRobert Watson } 3062497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 3063497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 3064497057eeSRobert Watson comma = 1; 3065497057eeSRobert Watson } 3066497057eeSRobert Watson } 3067497057eeSRobert Watson 3068497057eeSRobert Watson static void 3069497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 3070497057eeSRobert Watson { 3071497057eeSRobert Watson int comma; 3072497057eeSRobert Watson 3073497057eeSRobert Watson comma = 0; 3074497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3075497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3076497057eeSRobert Watson comma = 1; 3077497057eeSRobert Watson } 3078497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3079497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3080497057eeSRobert Watson comma = 1; 3081497057eeSRobert Watson } 3082497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3083497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3084497057eeSRobert Watson comma = 1; 3085497057eeSRobert Watson } 3086497057eeSRobert Watson } 3087497057eeSRobert Watson 30886d888973SRobert Watson static void 3089497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3090497057eeSRobert Watson { 3091497057eeSRobert Watson 3092497057eeSRobert Watson db_print_indent(indent); 3093497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3094497057eeSRobert Watson 3095497057eeSRobert Watson indent += 2; 3096497057eeSRobert Watson 3097497057eeSRobert Watson db_print_indent(indent); 3098497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3099497057eeSRobert Watson 3100497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3101497057eeSRobert Watson 3102497057eeSRobert Watson db_print_indent(indent); 3103497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 3104497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 3105497057eeSRobert Watson 3106497057eeSRobert Watson db_print_indent(indent); 3107497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3108497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3109497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3110497057eeSRobert Watson db_printf(")\n"); 3111497057eeSRobert Watson 3112497057eeSRobert Watson db_print_indent(indent); 3113497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3114497057eeSRobert Watson inp->inp_vflag); 3115497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3116497057eeSRobert Watson db_printf(")\n"); 3117497057eeSRobert Watson 3118497057eeSRobert Watson db_print_indent(indent); 3119497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3120497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3121497057eeSRobert Watson 3122497057eeSRobert Watson db_print_indent(indent); 3123497057eeSRobert Watson #ifdef INET6 3124497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3125497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3126497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3127497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3128497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3129497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3130497057eeSRobert Watson inp->in6p_hops); 3131497057eeSRobert Watson } else 3132497057eeSRobert Watson #endif 3133497057eeSRobert Watson { 3134497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3135497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3136497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3137497057eeSRobert Watson } 3138497057eeSRobert Watson 3139497057eeSRobert Watson db_print_indent(indent); 3140497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3141497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3142497057eeSRobert Watson } 3143497057eeSRobert Watson 3144497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3145497057eeSRobert Watson { 3146497057eeSRobert Watson struct inpcb *inp; 3147497057eeSRobert Watson 3148497057eeSRobert Watson if (!have_addr) { 3149497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3150497057eeSRobert Watson return; 3151497057eeSRobert Watson } 3152497057eeSRobert Watson inp = (struct inpcb *)addr; 3153497057eeSRobert Watson 3154497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3155497057eeSRobert Watson } 315683e521ecSBjoern A. Zeeb #endif /* DDB */ 3157f3e7afe2SHans Petter Selasky 3158f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3159f3e7afe2SHans Petter Selasky /* 3160f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3161f3e7afe2SHans Petter Selasky * if any. 3162f3e7afe2SHans Petter Selasky */ 3163f3e7afe2SHans Petter Selasky int 3164f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3165f3e7afe2SHans Petter Selasky { 3166f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3167f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 3168f3e7afe2SHans Petter Selasky }; 3169f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3170f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3171f3e7afe2SHans Petter Selasky int error; 3172f3e7afe2SHans Petter Selasky 3173f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3174f3e7afe2SHans Petter Selasky if (mst == NULL) 3175f3e7afe2SHans Petter Selasky return (EINVAL); 3176f3e7afe2SHans Petter Selasky 3177f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3178f3e7afe2SHans Petter Selasky if (ifp == NULL) 3179f3e7afe2SHans Petter Selasky return (EINVAL); 3180f3e7afe2SHans Petter Selasky 3181f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 3182f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3183f3e7afe2SHans Petter Selasky } else { 3184f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 3185f3e7afe2SHans Petter Selasky } 3186f3e7afe2SHans Petter Selasky return (error); 3187f3e7afe2SHans Petter Selasky } 3188f3e7afe2SHans Petter Selasky 3189f3e7afe2SHans Petter Selasky /* 3190f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3191f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3192f3e7afe2SHans Petter Selasky */ 3193f3e7afe2SHans Petter Selasky int 3194f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3195f3e7afe2SHans Petter Selasky { 3196f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3197f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3198f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3199f3e7afe2SHans Petter Selasky int error; 3200f3e7afe2SHans Petter Selasky 3201f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3202f3e7afe2SHans Petter Selasky if (mst == NULL) 3203f3e7afe2SHans Petter Selasky return (EINVAL); 3204f3e7afe2SHans Petter Selasky 3205f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3206f3e7afe2SHans Petter Selasky if (ifp == NULL) 3207f3e7afe2SHans Petter Selasky return (EINVAL); 3208f3e7afe2SHans Petter Selasky 3209f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 3210f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3211f3e7afe2SHans Petter Selasky } else { 3212f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 3213f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3214f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3215f3e7afe2SHans Petter Selasky } 3216f3e7afe2SHans Petter Selasky return (error); 3217f3e7afe2SHans Petter Selasky } 3218f3e7afe2SHans Petter Selasky 3219f3e7afe2SHans Petter Selasky /* 322095ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 322195ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 322295ed5015SHans Petter Selasky */ 322395ed5015SHans Petter Selasky int 322495ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 322595ed5015SHans Petter Selasky { 322695ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 322795ed5015SHans Petter Selasky struct m_snd_tag *mst; 322895ed5015SHans Petter Selasky struct ifnet *ifp; 322995ed5015SHans Petter Selasky int error; 323095ed5015SHans Petter Selasky 323195ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 323295ed5015SHans Petter Selasky if (mst == NULL) 323395ed5015SHans Petter Selasky return (EINVAL); 323495ed5015SHans Petter Selasky 323595ed5015SHans Petter Selasky ifp = mst->ifp; 323695ed5015SHans Petter Selasky if (ifp == NULL) 323795ed5015SHans Petter Selasky return (EINVAL); 323895ed5015SHans Petter Selasky 323995ed5015SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) 324095ed5015SHans Petter Selasky return (EOPNOTSUPP); 324195ed5015SHans Petter Selasky 324295ed5015SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 324395ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 324495ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 324595ed5015SHans Petter Selasky return (error); 324695ed5015SHans Petter Selasky } 324795ed5015SHans Petter Selasky 324895ed5015SHans Petter Selasky /* 3249f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3250f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3251f3e7afe2SHans Petter Selasky */ 3252f3e7afe2SHans Petter Selasky int 3253f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 3254f3e7afe2SHans Petter Selasky uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate) 3255f3e7afe2SHans Petter Selasky { 3256f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 325795ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 325895ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3259f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3260f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 3261f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 3262f3e7afe2SHans Petter Selasky }; 3263f3e7afe2SHans Petter Selasky int error; 3264f3e7afe2SHans Petter Selasky 3265f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3266f3e7afe2SHans Petter Selasky 3267f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 3268f3e7afe2SHans Petter Selasky return (EINVAL); 3269f3e7afe2SHans Petter Selasky 3270f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 3271f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3272f3e7afe2SHans Petter Selasky } else { 3273f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 3274f3e7afe2SHans Petter Selasky 3275f3e7afe2SHans Petter Selasky /* 3276f3e7afe2SHans Petter Selasky * At success increment the refcount on 3277f3e7afe2SHans Petter Selasky * the send tag's network interface: 3278f3e7afe2SHans Petter Selasky */ 3279f3e7afe2SHans Petter Selasky if (error == 0) 3280f3e7afe2SHans Petter Selasky if_ref(inp->inp_snd_tag->ifp); 3281f3e7afe2SHans Petter Selasky } 3282f3e7afe2SHans Petter Selasky return (error); 3283f3e7afe2SHans Petter Selasky } 3284f3e7afe2SHans Petter Selasky 3285f3e7afe2SHans Petter Selasky /* 3286f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3287f3e7afe2SHans Petter Selasky * if any: 3288f3e7afe2SHans Petter Selasky */ 3289f3e7afe2SHans Petter Selasky void 3290f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3291f3e7afe2SHans Petter Selasky { 3292f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3293f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3294f3e7afe2SHans Petter Selasky 3295f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3296f3e7afe2SHans Petter Selasky 3297f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3298f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3299f3e7afe2SHans Petter Selasky 3300f3e7afe2SHans Petter Selasky if (mst == NULL) 3301f3e7afe2SHans Petter Selasky return; 3302f3e7afe2SHans Petter Selasky 3303f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3304f3e7afe2SHans Petter Selasky if (ifp == NULL) 3305f3e7afe2SHans Petter Selasky return; 3306f3e7afe2SHans Petter Selasky 3307f3e7afe2SHans Petter Selasky /* 3308f3e7afe2SHans Petter Selasky * If the device was detached while we still had reference(s) 3309f3e7afe2SHans Petter Selasky * on the ifp, we assume if_snd_tag_free() was replaced with 3310f3e7afe2SHans Petter Selasky * stubs. 3311f3e7afe2SHans Petter Selasky */ 3312f3e7afe2SHans Petter Selasky ifp->if_snd_tag_free(mst); 3313f3e7afe2SHans Petter Selasky 3314f3e7afe2SHans Petter Selasky /* release reference count on network interface */ 3315f3e7afe2SHans Petter Selasky if_rele(ifp); 3316f3e7afe2SHans Petter Selasky } 3317f3e7afe2SHans Petter Selasky 3318f3e7afe2SHans Petter Selasky /* 3319f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3320f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3321f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3322f3e7afe2SHans Petter Selasky */ 3323f3e7afe2SHans Petter Selasky void 3324f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3325f3e7afe2SHans Petter Selasky { 3326f3e7afe2SHans Petter Selasky struct socket *socket; 3327f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3328f3e7afe2SHans Petter Selasky bool did_upgrade; 3329f3e7afe2SHans Petter Selasky int error; 3330f3e7afe2SHans Petter Selasky 3331f3e7afe2SHans Petter Selasky if (inp == NULL) 3332f3e7afe2SHans Petter Selasky return; 3333f3e7afe2SHans Petter Selasky 3334f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3335f3e7afe2SHans Petter Selasky if (socket == NULL) 3336f3e7afe2SHans Petter Selasky return; 3337f3e7afe2SHans Petter Selasky 3338f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3339f3e7afe2SHans Petter Selasky /* 3340f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3341f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3342f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3343f3e7afe2SHans Petter Selasky * write lock. 3344f3e7afe2SHans Petter Selasky */ 3345f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3346f3e7afe2SHans Petter Selasky return; 3347f3e7afe2SHans Petter Selasky did_upgrade = 1; 3348f3e7afe2SHans Petter Selasky } else { 3349f3e7afe2SHans Petter Selasky did_upgrade = 0; 3350f3e7afe2SHans Petter Selasky } 3351f3e7afe2SHans Petter Selasky 3352f3e7afe2SHans Petter Selasky /* 3353f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3354f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3355f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3356f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3357f3e7afe2SHans Petter Selasky */ 3358f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3359f3e7afe2SHans Petter Selasky 3360f3e7afe2SHans Petter Selasky /* 3361f3e7afe2SHans Petter Selasky * NOTE: When attaching to a network interface a reference is 3362f3e7afe2SHans Petter Selasky * made to ensure the network interface doesn't go away until 3363f3e7afe2SHans Petter Selasky * all ratelimit connections are gone. The network interface 3364f3e7afe2SHans Petter Selasky * pointers compared below represent valid network interfaces, 3365f3e7afe2SHans Petter Selasky * except when comparing towards NULL. 3366f3e7afe2SHans Petter Selasky */ 3367f3e7afe2SHans Petter Selasky if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 3368f3e7afe2SHans Petter Selasky error = 0; 3369f3e7afe2SHans Petter Selasky } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 3370f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 3371f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3372f3e7afe2SHans Petter Selasky error = 0; 3373f3e7afe2SHans Petter Selasky } else if (inp->inp_snd_tag == NULL) { 3374f3e7afe2SHans Petter Selasky /* 3375f3e7afe2SHans Petter Selasky * In order to utilize packet pacing with RSS, we need 3376f3e7afe2SHans Petter Selasky * to wait until there is a valid RSS hash before we 3377f3e7afe2SHans Petter Selasky * can proceed: 3378f3e7afe2SHans Petter Selasky */ 3379f3e7afe2SHans Petter Selasky if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 3380f3e7afe2SHans Petter Selasky error = EAGAIN; 3381f3e7afe2SHans Petter Selasky } else { 3382f3e7afe2SHans Petter Selasky error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 3383f3e7afe2SHans Petter Selasky mb->m_pkthdr.flowid, max_pacing_rate); 3384f3e7afe2SHans Petter Selasky } 3385f3e7afe2SHans Petter Selasky } else { 3386f3e7afe2SHans Petter Selasky error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 3387f3e7afe2SHans Petter Selasky } 3388f3e7afe2SHans Petter Selasky if (error == 0 || error == EOPNOTSUPP) 3389f3e7afe2SHans Petter Selasky inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 3390f3e7afe2SHans Petter Selasky if (did_upgrade) 3391f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3392f3e7afe2SHans Petter Selasky } 3393f3e7afe2SHans Petter Selasky 3394f3e7afe2SHans Petter Selasky /* 3395f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3396f3e7afe2SHans Petter Selasky */ 3397f3e7afe2SHans Petter Selasky void 3398f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3399f3e7afe2SHans Petter Selasky { 3400f3e7afe2SHans Petter Selasky struct socket *socket; 3401f3e7afe2SHans Petter Selasky bool did_upgrade; 3402f3e7afe2SHans Petter Selasky 3403f3e7afe2SHans Petter Selasky if (inp == NULL) 3404f3e7afe2SHans Petter Selasky return; 3405f3e7afe2SHans Petter Selasky 3406f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3407f3e7afe2SHans Petter Selasky if (socket == NULL) 3408f3e7afe2SHans Petter Selasky return; 3409f3e7afe2SHans Petter Selasky 3410f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3411f3e7afe2SHans Petter Selasky return; 3412f3e7afe2SHans Petter Selasky 3413f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3414f3e7afe2SHans Petter Selasky /* 3415f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3416f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3417f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3418f3e7afe2SHans Petter Selasky * write lock. 3419f3e7afe2SHans Petter Selasky */ 3420f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3421f3e7afe2SHans Petter Selasky return; 3422f3e7afe2SHans Petter Selasky did_upgrade = 1; 3423f3e7afe2SHans Petter Selasky } else { 3424f3e7afe2SHans Petter Selasky did_upgrade = 0; 3425f3e7afe2SHans Petter Selasky } 3426f3e7afe2SHans Petter Selasky 3427f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3428f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3429f3e7afe2SHans Petter Selasky 3430f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3431f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3432f3e7afe2SHans Petter Selasky 3433f3e7afe2SHans Petter Selasky if (did_upgrade) 3434f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3435f3e7afe2SHans Petter Selasky } 3436f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3437