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 111*1a43cff9SSean Bruno #define INPCBLBGROUP_SIZMIN 8 112*1a43cff9SSean Bruno #define INPCBLBGROUP_SIZMAX 256 113*1a43cff9SSean 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); 1413e288e62SDimitry Andric static VNET_DEFINE(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 223*1a43cff9SSean Bruno static struct inpcblbgroup * 224*1a43cff9SSean Bruno in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, u_char vflag, 225*1a43cff9SSean Bruno uint16_t port, const union in_dependaddr *addr, int size) 226*1a43cff9SSean Bruno { 227*1a43cff9SSean Bruno struct inpcblbgroup *grp; 228*1a43cff9SSean Bruno size_t bytes; 229*1a43cff9SSean Bruno 230*1a43cff9SSean Bruno bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 231*1a43cff9SSean Bruno grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 232*1a43cff9SSean Bruno if (!grp) 233*1a43cff9SSean Bruno return (NULL); 234*1a43cff9SSean Bruno grp->il_vflag = vflag; 235*1a43cff9SSean Bruno grp->il_lport = port; 236*1a43cff9SSean Bruno grp->il_dependladdr = *addr; 237*1a43cff9SSean Bruno grp->il_inpsiz = size; 238*1a43cff9SSean Bruno LIST_INSERT_HEAD(hdr, grp, il_list); 239*1a43cff9SSean Bruno return (grp); 240*1a43cff9SSean Bruno } 241*1a43cff9SSean Bruno 242*1a43cff9SSean Bruno static void 243*1a43cff9SSean Bruno in_pcblbgroup_free(struct inpcblbgroup *grp) 244*1a43cff9SSean Bruno { 245*1a43cff9SSean Bruno 246*1a43cff9SSean Bruno LIST_REMOVE(grp, il_list); 247*1a43cff9SSean Bruno free(grp, M_TEMP); 248*1a43cff9SSean Bruno } 249*1a43cff9SSean Bruno 250*1a43cff9SSean Bruno static struct inpcblbgroup * 251*1a43cff9SSean Bruno in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 252*1a43cff9SSean Bruno struct inpcblbgroup *old_grp, int size) 253*1a43cff9SSean Bruno { 254*1a43cff9SSean Bruno struct inpcblbgroup *grp; 255*1a43cff9SSean Bruno int i; 256*1a43cff9SSean Bruno 257*1a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, old_grp->il_vflag, 258*1a43cff9SSean Bruno old_grp->il_lport, &old_grp->il_dependladdr, size); 259*1a43cff9SSean Bruno if (!grp) 260*1a43cff9SSean Bruno return (NULL); 261*1a43cff9SSean Bruno 262*1a43cff9SSean Bruno KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 263*1a43cff9SSean Bruno ("invalid new local group size %d and old local group count %d", 264*1a43cff9SSean Bruno grp->il_inpsiz, old_grp->il_inpcnt)); 265*1a43cff9SSean Bruno 266*1a43cff9SSean Bruno for (i = 0; i < old_grp->il_inpcnt; ++i) 267*1a43cff9SSean Bruno grp->il_inp[i] = old_grp->il_inp[i]; 268*1a43cff9SSean Bruno grp->il_inpcnt = old_grp->il_inpcnt; 269*1a43cff9SSean Bruno in_pcblbgroup_free(old_grp); 270*1a43cff9SSean Bruno return (grp); 271*1a43cff9SSean Bruno } 272*1a43cff9SSean Bruno 273*1a43cff9SSean Bruno /* 274*1a43cff9SSean Bruno * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 275*1a43cff9SSean Bruno * and shrink group if possible. 276*1a43cff9SSean Bruno */ 277*1a43cff9SSean Bruno static void 278*1a43cff9SSean Bruno in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 279*1a43cff9SSean Bruno int i) 280*1a43cff9SSean Bruno { 281*1a43cff9SSean Bruno struct inpcblbgroup *grp = *grpp; 282*1a43cff9SSean Bruno 283*1a43cff9SSean Bruno for (; i + 1 < grp->il_inpcnt; ++i) 284*1a43cff9SSean Bruno grp->il_inp[i] = grp->il_inp[i + 1]; 285*1a43cff9SSean Bruno grp->il_inpcnt--; 286*1a43cff9SSean Bruno 287*1a43cff9SSean Bruno if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 288*1a43cff9SSean Bruno grp->il_inpcnt <= (grp->il_inpsiz / 4)) { 289*1a43cff9SSean Bruno /* Shrink this group. */ 290*1a43cff9SSean Bruno struct inpcblbgroup *new_grp = 291*1a43cff9SSean Bruno in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 292*1a43cff9SSean Bruno if (new_grp) 293*1a43cff9SSean Bruno *grpp = new_grp; 294*1a43cff9SSean Bruno } 295*1a43cff9SSean Bruno return; 296*1a43cff9SSean Bruno } 297*1a43cff9SSean Bruno 298*1a43cff9SSean Bruno /* 299*1a43cff9SSean Bruno * Add PCB to load balance group for SO_REUSEPORT_LB option. 300*1a43cff9SSean Bruno */ 301*1a43cff9SSean Bruno static int 302*1a43cff9SSean Bruno in_pcbinslbgrouphash(struct inpcb *inp) 303*1a43cff9SSean Bruno { 304*1a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 305*1a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 306*1a43cff9SSean Bruno struct inpcblbgroup *grp; 307*1a43cff9SSean Bruno uint16_t hashmask, lport; 308*1a43cff9SSean Bruno uint32_t group_index; 309*1a43cff9SSean Bruno struct ucred *cred; 310*1a43cff9SSean Bruno static int limit_logged = 0; 311*1a43cff9SSean Bruno 312*1a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 313*1a43cff9SSean Bruno 314*1a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 315*1a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 316*1a43cff9SSean Bruno 317*1a43cff9SSean Bruno if (pcbinfo->ipi_lbgrouphashbase == NULL) 318*1a43cff9SSean Bruno return (0); 319*1a43cff9SSean Bruno 320*1a43cff9SSean Bruno hashmask = pcbinfo->ipi_lbgrouphashmask; 321*1a43cff9SSean Bruno lport = inp->inp_lport; 322*1a43cff9SSean Bruno group_index = INP_PCBLBGROUP_PORTHASH(lport, hashmask); 323*1a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[group_index]; 324*1a43cff9SSean Bruno 325*1a43cff9SSean Bruno /* 326*1a43cff9SSean Bruno * Don't allow jailed socket to join local group. 327*1a43cff9SSean Bruno */ 328*1a43cff9SSean Bruno if (inp->inp_socket != NULL) 329*1a43cff9SSean Bruno cred = inp->inp_socket->so_cred; 330*1a43cff9SSean Bruno else 331*1a43cff9SSean Bruno cred = NULL; 332*1a43cff9SSean Bruno if (cred != NULL && jailed(cred)) 333*1a43cff9SSean Bruno return (0); 334*1a43cff9SSean Bruno 335*1a43cff9SSean Bruno #ifdef INET6 336*1a43cff9SSean Bruno /* 337*1a43cff9SSean Bruno * Don't allow IPv4 mapped INET6 wild socket. 338*1a43cff9SSean Bruno */ 339*1a43cff9SSean Bruno if ((inp->inp_vflag & INP_IPV4) && 340*1a43cff9SSean Bruno inp->inp_laddr.s_addr == INADDR_ANY && 341*1a43cff9SSean Bruno INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 342*1a43cff9SSean Bruno return (0); 343*1a43cff9SSean Bruno } 344*1a43cff9SSean Bruno #endif 345*1a43cff9SSean Bruno 346*1a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 347*1a43cff9SSean Bruno INP_PCBLBGROUP_PORTHASH(inp->inp_lport, 348*1a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask)]; 349*1a43cff9SSean Bruno LIST_FOREACH(grp, hdr, il_list) { 350*1a43cff9SSean Bruno if (grp->il_vflag == inp->inp_vflag && 351*1a43cff9SSean Bruno grp->il_lport == inp->inp_lport && 352*1a43cff9SSean Bruno memcmp(&grp->il_dependladdr, 353*1a43cff9SSean Bruno &inp->inp_inc.inc_ie.ie_dependladdr, 354*1a43cff9SSean Bruno sizeof(grp->il_dependladdr)) == 0) { 355*1a43cff9SSean Bruno break; 356*1a43cff9SSean Bruno } 357*1a43cff9SSean Bruno } 358*1a43cff9SSean Bruno if (grp == NULL) { 359*1a43cff9SSean Bruno /* Create new load balance group. */ 360*1a43cff9SSean Bruno grp = in_pcblbgroup_alloc(hdr, inp->inp_vflag, 361*1a43cff9SSean Bruno inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 362*1a43cff9SSean Bruno INPCBLBGROUP_SIZMIN); 363*1a43cff9SSean Bruno if (!grp) 364*1a43cff9SSean Bruno return (ENOBUFS); 365*1a43cff9SSean Bruno } else if (grp->il_inpcnt == grp->il_inpsiz) { 366*1a43cff9SSean Bruno if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 367*1a43cff9SSean Bruno if (!limit_logged) { 368*1a43cff9SSean Bruno limit_logged = 1; 369*1a43cff9SSean Bruno printf("lb group port %d, limit reached\n", 370*1a43cff9SSean Bruno ntohs(grp->il_lport)); 371*1a43cff9SSean Bruno } 372*1a43cff9SSean Bruno return (0); 373*1a43cff9SSean Bruno } 374*1a43cff9SSean Bruno 375*1a43cff9SSean Bruno /* Expand this local group. */ 376*1a43cff9SSean Bruno grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 377*1a43cff9SSean Bruno if (!grp) 378*1a43cff9SSean Bruno return (ENOBUFS); 379*1a43cff9SSean Bruno } 380*1a43cff9SSean Bruno 381*1a43cff9SSean Bruno KASSERT(grp->il_inpcnt < grp->il_inpsiz, 382*1a43cff9SSean Bruno ("invalid local group size %d and count %d", 383*1a43cff9SSean Bruno grp->il_inpsiz, grp->il_inpcnt)); 384*1a43cff9SSean Bruno 385*1a43cff9SSean Bruno grp->il_inp[grp->il_inpcnt] = inp; 386*1a43cff9SSean Bruno grp->il_inpcnt++; 387*1a43cff9SSean Bruno return (0); 388*1a43cff9SSean Bruno } 389*1a43cff9SSean Bruno 390*1a43cff9SSean Bruno /* 391*1a43cff9SSean Bruno * Remove PCB from load balance group. 392*1a43cff9SSean Bruno */ 393*1a43cff9SSean Bruno static void 394*1a43cff9SSean Bruno in_pcbremlbgrouphash(struct inpcb *inp) 395*1a43cff9SSean Bruno { 396*1a43cff9SSean Bruno struct inpcbinfo *pcbinfo; 397*1a43cff9SSean Bruno struct inpcblbgrouphead *hdr; 398*1a43cff9SSean Bruno struct inpcblbgroup *grp; 399*1a43cff9SSean Bruno int i; 400*1a43cff9SSean Bruno 401*1a43cff9SSean Bruno pcbinfo = inp->inp_pcbinfo; 402*1a43cff9SSean Bruno 403*1a43cff9SSean Bruno INP_WLOCK_ASSERT(inp); 404*1a43cff9SSean Bruno INP_HASH_WLOCK_ASSERT(pcbinfo); 405*1a43cff9SSean Bruno 406*1a43cff9SSean Bruno if (pcbinfo->ipi_lbgrouphashbase == NULL) 407*1a43cff9SSean Bruno return; 408*1a43cff9SSean Bruno 409*1a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 410*1a43cff9SSean Bruno INP_PCBLBGROUP_PORTHASH(inp->inp_lport, 411*1a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask)]; 412*1a43cff9SSean Bruno 413*1a43cff9SSean Bruno LIST_FOREACH(grp, hdr, il_list) { 414*1a43cff9SSean Bruno for (i = 0; i < grp->il_inpcnt; ++i) { 415*1a43cff9SSean Bruno if (grp->il_inp[i] != inp) 416*1a43cff9SSean Bruno continue; 417*1a43cff9SSean Bruno 418*1a43cff9SSean Bruno if (grp->il_inpcnt == 1) { 419*1a43cff9SSean Bruno /* We are the last, free this local group. */ 420*1a43cff9SSean Bruno in_pcblbgroup_free(grp); 421*1a43cff9SSean Bruno } else { 422*1a43cff9SSean Bruno /* Pull up inpcbs, shrink group if possible. */ 423*1a43cff9SSean Bruno in_pcblbgroup_reorder(hdr, &grp, i); 424*1a43cff9SSean Bruno } 425*1a43cff9SSean Bruno return; 426*1a43cff9SSean Bruno } 427*1a43cff9SSean Bruno } 428*1a43cff9SSean Bruno } 429*1a43cff9SSean Bruno 430c3229e05SDavid Greenman /* 431cc487c16SGleb Smirnoff * Different protocols initialize their inpcbs differently - giving 432cc487c16SGleb Smirnoff * different name to the lock. But they all are disposed the same. 433cc487c16SGleb Smirnoff */ 434cc487c16SGleb Smirnoff static void 435cc487c16SGleb Smirnoff inpcb_fini(void *mem, int size) 436cc487c16SGleb Smirnoff { 437cc487c16SGleb Smirnoff struct inpcb *inp = mem; 438cc487c16SGleb Smirnoff 439cc487c16SGleb Smirnoff INP_LOCK_DESTROY(inp); 440cc487c16SGleb Smirnoff } 441cc487c16SGleb Smirnoff 442cc487c16SGleb Smirnoff /* 4439bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 4449bcd427bSRobert Watson * arguments in time. 4459bcd427bSRobert Watson */ 4469bcd427bSRobert Watson void 4479bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 4489bcd427bSRobert Watson struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 449cc487c16SGleb Smirnoff char *inpcbzone_name, uma_init inpcbzone_init, u_int hashfields) 4509bcd427bSRobert Watson { 4519bcd427bSRobert Watson 4529bcd427bSRobert Watson INP_INFO_LOCK_INIT(pcbinfo, name); 453fa046d87SRobert Watson INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 454ff9b006dSJulien Charbon INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist"); 4559bcd427bSRobert Watson #ifdef VIMAGE 4569bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 4579bcd427bSRobert Watson #endif 4589bcd427bSRobert Watson pcbinfo->ipi_listhead = listhead; 4599bcd427bSRobert Watson LIST_INIT(pcbinfo->ipi_listhead); 460fa046d87SRobert Watson pcbinfo->ipi_count = 0; 4619bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 4629bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 4639bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 4649bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 465*1a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashbase = hashinit(hash_nelements, M_PCB, 466*1a43cff9SSean Bruno &pcbinfo->ipi_lbgrouphashmask); 46752cd27cbSRobert Watson #ifdef PCBGROUP 46852cd27cbSRobert Watson in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 46952cd27cbSRobert Watson #endif 4709bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 471cc487c16SGleb Smirnoff NULL, NULL, inpcbzone_init, inpcb_fini, UMA_ALIGN_PTR, 0); 4729bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 4736acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 4746acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 4759bcd427bSRobert Watson } 4769bcd427bSRobert Watson 4779bcd427bSRobert Watson /* 4789bcd427bSRobert Watson * Destroy an inpcbinfo. 4799bcd427bSRobert Watson */ 4809bcd427bSRobert Watson void 4819bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 4829bcd427bSRobert Watson { 4839bcd427bSRobert Watson 484fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 485fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 486fa046d87SRobert Watson 4879bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 4889bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 4899bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 490*1a43cff9SSean Bruno hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 491*1a43cff9SSean Bruno pcbinfo->ipi_lbgrouphashmask); 49252cd27cbSRobert Watson #ifdef PCBGROUP 49352cd27cbSRobert Watson in_pcbgroup_destroy(pcbinfo); 49452cd27cbSRobert Watson #endif 4959bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 496ff9b006dSJulien Charbon INP_LIST_LOCK_DESTROY(pcbinfo); 497fa046d87SRobert Watson INP_HASH_LOCK_DESTROY(pcbinfo); 4989bcd427bSRobert Watson INP_INFO_LOCK_DESTROY(pcbinfo); 4999bcd427bSRobert Watson } 5009bcd427bSRobert Watson 5019bcd427bSRobert Watson /* 502c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 503d915b280SStephan Uphoff * On success return with the PCB locked. 504c3229e05SDavid Greenman */ 505df8bae1dSRodney W. Grimes int 506d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 507df8bae1dSRodney W. Grimes { 508136d4f1cSRobert Watson struct inpcb *inp; 50913cf67f3SHajimu UMEMOTO int error; 510a557af22SRobert Watson 511ff9b006dSJulien Charbon #ifdef INVARIANTS 512ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 513ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 514ff9b006dSJulien Charbon } else { 51559daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 516ff9b006dSJulien Charbon } 517ff9b006dSJulien Charbon #endif 518ff9b006dSJulien Charbon 519a557af22SRobert Watson error = 0; 520d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 521df8bae1dSRodney W. Grimes if (inp == NULL) 522df8bae1dSRodney W. Grimes return (ENOBUFS); 5236a6cefacSGleb Smirnoff bzero(&inp->inp_start_zero, inp_zero_size); 52415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 525df8bae1dSRodney W. Grimes inp->inp_socket = so; 52686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 5278b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 528a557af22SRobert Watson #ifdef MAC 52930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 530a557af22SRobert Watson if (error != 0) 531a557af22SRobert Watson goto out; 53230d239bcSRobert Watson mac_inpcb_create(so, inp); 533a557af22SRobert Watson #endif 534fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 535fcf59617SAndrey V. Elsukov error = ipsec_init_pcbpolicy(inp); 5360bffde27SRobert Watson if (error != 0) { 5370bffde27SRobert Watson #ifdef MAC 5380bffde27SRobert Watson mac_inpcb_destroy(inp); 5390bffde27SRobert Watson #endif 540a557af22SRobert Watson goto out; 5410bffde27SRobert Watson } 542b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 543e3fd5ffdSRobert Watson #ifdef INET6 544340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 545340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 546603724d3SBjoern A. Zeeb if (V_ip6_v6only) 54733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 548340c35deSJonathan Lemon } 54975daea93SPaul Saab #endif 550ff9b006dSJulien Charbon INP_WLOCK(inp); 551ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 552712fc218SRobert Watson LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 5533d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 554df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 55533841545SHajimu UMEMOTO #ifdef INET6 556603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 55733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 55833841545SHajimu UMEMOTO #endif 559d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 56079bdc6e5SRobert Watson refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 5618c1960d5SMike Karels 5628c1960d5SMike Karels /* 5638c1960d5SMike Karels * Routes in inpcb's can cache L2 as well; they are guaranteed 5648c1960d5SMike Karels * to be cleaned up. 5658c1960d5SMike Karels */ 5668c1960d5SMike Karels inp->inp_route.ro_flags = RT_LLE_CACHE; 567ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 5687a60a910SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 569a557af22SRobert Watson out: 57086d02c5cSBjoern A. Zeeb if (error != 0) { 57186d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 572a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 57386d02c5cSBjoern A. Zeeb } 574a557af22SRobert Watson #endif 575a557af22SRobert Watson return (error); 576df8bae1dSRodney W. Grimes } 577df8bae1dSRodney W. Grimes 57867107f45SBjoern A. Zeeb #ifdef INET 579df8bae1dSRodney W. Grimes int 580136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 581df8bae1dSRodney W. Grimes { 5824b932371SIan Dowse int anonport, error; 5834b932371SIan Dowse 5848501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 585fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 58659daba27SSam Leffler 5874b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 5884b932371SIan Dowse return (EINVAL); 5895cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 5904b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 591b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 5924b932371SIan Dowse if (error) 5934b932371SIan Dowse return (error); 5944b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 5954b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 5964b932371SIan Dowse inp->inp_lport = 0; 5974b932371SIan Dowse return (EAGAIN); 5984b932371SIan Dowse } 5994b932371SIan Dowse if (anonport) 6004b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 6014b932371SIan Dowse return (0); 6024b932371SIan Dowse } 60367107f45SBjoern A. Zeeb #endif 6044b932371SIan Dowse 60539c8c62eSHiren Panchasara /* 60639c8c62eSHiren Panchasara * Select a local port (number) to use. 60739c8c62eSHiren Panchasara */ 608efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 609efc76f72SBjoern A. Zeeb int 6104616026fSErmal Luçi in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 6114616026fSErmal Luçi struct ucred *cred, int lookupflags) 612efc76f72SBjoern A. Zeeb { 613efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 614efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 615efc76f72SBjoern A. Zeeb unsigned short *lastport; 616efc76f72SBjoern A. Zeeb int count, dorandom, error; 617efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 618efc76f72SBjoern A. Zeeb #ifdef INET 619efc76f72SBjoern A. Zeeb struct in_addr laddr; 620efc76f72SBjoern A. Zeeb #endif 621efc76f72SBjoern A. Zeeb 622efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 623efc76f72SBjoern A. Zeeb 624efc76f72SBjoern A. Zeeb /* 625efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 626efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 627efc76f72SBjoern A. Zeeb */ 628efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 629fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 630efc76f72SBjoern A. Zeeb 631efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 632efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 633efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 634efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 635efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 636efc76f72SBjoern A. Zeeb error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0); 637efc76f72SBjoern A. Zeeb if (error) 638efc76f72SBjoern A. Zeeb return (error); 639efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 640efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 641efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 642efc76f72SBjoern A. Zeeb } else { 643efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 644efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 645efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 646efc76f72SBjoern A. Zeeb } 647efc76f72SBjoern A. Zeeb /* 648e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 649efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 650efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 651efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 652efc76f72SBjoern A. Zeeb */ 653efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 654e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 655e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 656efc76f72SBjoern A. Zeeb dorandom = 1; 657efc76f72SBjoern A. Zeeb else 658efc76f72SBjoern A. Zeeb dorandom = 0; 659efc76f72SBjoern A. Zeeb /* 660efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 661efc76f72SBjoern A. Zeeb * we have the only port available. 662efc76f72SBjoern A. Zeeb */ 663efc76f72SBjoern A. Zeeb if (first == last) 664efc76f72SBjoern A. Zeeb dorandom = 0; 665e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 666e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 667efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 668efc76f72SBjoern A. Zeeb /* 669efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 670efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 671efc76f72SBjoern A. Zeeb * code path implementing all logic. 672efc76f72SBjoern A. Zeeb */ 673efc76f72SBjoern A. Zeeb if (first > last) { 674efc76f72SBjoern A. Zeeb aux = first; 675efc76f72SBjoern A. Zeeb first = last; 676efc76f72SBjoern A. Zeeb last = aux; 677efc76f72SBjoern A. Zeeb } 678efc76f72SBjoern A. Zeeb 679efc76f72SBjoern A. Zeeb #ifdef INET 680efc76f72SBjoern A. Zeeb /* Make the compiler happy. */ 681efc76f72SBjoern A. Zeeb laddr.s_addr = 0; 6824d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 683efc76f72SBjoern A. Zeeb KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 684efc76f72SBjoern A. Zeeb __func__, inp)); 685efc76f72SBjoern A. Zeeb laddr = *laddrp; 686efc76f72SBjoern A. Zeeb } 687efc76f72SBjoern A. Zeeb #endif 68867107f45SBjoern A. Zeeb tmpinp = NULL; /* Make compiler happy. */ 689efc76f72SBjoern A. Zeeb lport = *lportp; 690efc76f72SBjoern A. Zeeb 691efc76f72SBjoern A. Zeeb if (dorandom) 692efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 693efc76f72SBjoern A. Zeeb 694efc76f72SBjoern A. Zeeb count = last - first; 695efc76f72SBjoern A. Zeeb 696efc76f72SBjoern A. Zeeb do { 697efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 698efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 699efc76f72SBjoern A. Zeeb ++*lastport; 700efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 701efc76f72SBjoern A. Zeeb *lastport = first; 702efc76f72SBjoern A. Zeeb lport = htons(*lastport); 703efc76f72SBjoern A. Zeeb 704efc76f72SBjoern A. Zeeb #ifdef INET6 7054616026fSErmal Luçi if ((inp->inp_vflag & INP_IPV6) != 0) 706efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 70768e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 708efc76f72SBjoern A. Zeeb #endif 709efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 710efc76f72SBjoern A. Zeeb else 711efc76f72SBjoern A. Zeeb #endif 712efc76f72SBjoern A. Zeeb #ifdef INET 713efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 71468e0d7e0SRobert Watson lport, lookupflags, cred); 715efc76f72SBjoern A. Zeeb #endif 716efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 717efc76f72SBjoern A. Zeeb 718efc76f72SBjoern A. Zeeb #ifdef INET 7194d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 720efc76f72SBjoern A. Zeeb laddrp->s_addr = laddr.s_addr; 721efc76f72SBjoern A. Zeeb #endif 722efc76f72SBjoern A. Zeeb *lportp = lport; 723efc76f72SBjoern A. Zeeb 724efc76f72SBjoern A. Zeeb return (0); 725efc76f72SBjoern A. Zeeb } 726efdf104bSMikolaj Golub 727efdf104bSMikolaj Golub /* 728efdf104bSMikolaj Golub * Return cached socket options. 729efdf104bSMikolaj Golub */ 730*1a43cff9SSean Bruno int 731efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 732efdf104bSMikolaj Golub { 733*1a43cff9SSean Bruno int so_options; 734efdf104bSMikolaj Golub 735efdf104bSMikolaj Golub so_options = 0; 736efdf104bSMikolaj Golub 737*1a43cff9SSean Bruno if ((inp->inp_flags2 & INP_REUSEPORT_LB) != 0) 738*1a43cff9SSean Bruno so_options |= SO_REUSEPORT_LB; 739efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 740efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 741efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 742efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 743efdf104bSMikolaj Golub return (so_options); 744efdf104bSMikolaj Golub } 745efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 746efc76f72SBjoern A. Zeeb 7474b932371SIan Dowse /* 7480a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 7490a100a6fSAdrian Chadd * 7500a100a6fSAdrian Chadd * ni points to the new inp. 7510a100a6fSAdrian Chadd * oi points to the exisitng inp. 7520a100a6fSAdrian Chadd * 7530a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 7540a100a6fSAdrian Chadd * whether the credentials match. 7550a100a6fSAdrian Chadd */ 756d5bb8bd3SAdrian Chadd int 7570a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 7580a100a6fSAdrian Chadd { 7590a100a6fSAdrian Chadd /* Check permissions match */ 7600a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 7610a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 7620a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 7630a100a6fSAdrian Chadd return (0); 7640a100a6fSAdrian Chadd 7650a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 7660a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 7670a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 7680a100a6fSAdrian Chadd return (0); 7690a100a6fSAdrian Chadd 7700a100a6fSAdrian Chadd /* 7710a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 7720a100a6fSAdrian Chadd * it is and it matches the checks. 7730a100a6fSAdrian Chadd */ 7740a100a6fSAdrian Chadd return (1); 7750a100a6fSAdrian Chadd } 7760a100a6fSAdrian Chadd 777d5bb8bd3SAdrian Chadd #ifdef INET 7780a100a6fSAdrian Chadd /* 7794b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 7804b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 7814b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 7824b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 7834b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 7844b932371SIan Dowse * 7854b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 7864b932371SIan Dowse */ 7874b932371SIan Dowse int 788136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 789136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 7904b932371SIan Dowse { 7914b932371SIan Dowse struct socket *so = inp->inp_socket; 79215bd2b43SDavid Greenman struct sockaddr_in *sin; 793c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 7944b932371SIan Dowse struct in_addr laddr; 795df8bae1dSRodney W. Grimes u_short lport = 0; 79668e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 797413628a7SBjoern A. Zeeb int error; 798df8bae1dSRodney W. Grimes 7998501a69cSRobert Watson /* 800*1a43cff9SSean Bruno * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 801*1a43cff9SSean Bruno * so that we don't have to add to the (already messy) code below. 802*1a43cff9SSean Bruno */ 803*1a43cff9SSean Bruno int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 804*1a43cff9SSean Bruno 805*1a43cff9SSean Bruno /* 806fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 8078501a69cSRobert Watson */ 80859daba27SSam Leffler INP_LOCK_ASSERT(inp); 809fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 81059daba27SSam Leffler 811d7c5a620SMatt Macy if (CK_STAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 812df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 8134b932371SIan Dowse laddr.s_addr = *laddrp; 8144b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 815df8bae1dSRodney W. Grimes return (EINVAL); 816*1a43cff9SSean Bruno if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 81768e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 8184616026fSErmal Luçi if (nam == NULL) { 8197c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 8207c2f3cb9SJamie Gritton return (error); 8217c2f3cb9SJamie Gritton } else { 82257bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 82357bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 824df8bae1dSRodney W. Grimes return (EINVAL); 825df8bae1dSRodney W. Grimes #ifdef notdef 826df8bae1dSRodney W. Grimes /* 827df8bae1dSRodney W. Grimes * We should check the family, but old programs 828df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 829df8bae1dSRodney W. Grimes */ 830df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 831df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 832df8bae1dSRodney W. Grimes #endif 833b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 834b89e82ddSJamie Gritton if (error) 835b89e82ddSJamie Gritton return (error); 8364b932371SIan Dowse if (sin->sin_port != *lportp) { 8374b932371SIan Dowse /* Don't allow the port to change. */ 8384b932371SIan Dowse if (*lportp != 0) 8394b932371SIan Dowse return (EINVAL); 840df8bae1dSRodney W. Grimes lport = sin->sin_port; 8414b932371SIan Dowse } 8424b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 843df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 844df8bae1dSRodney W. Grimes /* 845df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 846df8bae1dSRodney W. Grimes * allow complete duplication of binding if 847df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 848df8bae1dSRodney W. Grimes * and a multicast address is bound on both 849df8bae1dSRodney W. Grimes * new and duplicated sockets. 850df8bae1dSRodney W. Grimes */ 851f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 852df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 853*1a43cff9SSean Bruno /* 854*1a43cff9SSean Bruno * XXX: How to deal with SO_REUSEPORT_LB here? 855*1a43cff9SSean Bruno * Treat same as SO_REUSEPORT for now. 856*1a43cff9SSean Bruno */ 857*1a43cff9SSean Bruno if ((so->so_options & 858*1a43cff9SSean Bruno (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 859*1a43cff9SSean Bruno reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 860df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 861df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 86283103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 8634209e01aSAdrian Chadd /* 8644209e01aSAdrian Chadd * Is the address a local IP address? 865f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 8668696873dSAdrian Chadd * to any endpoint address, local or not. 8674209e01aSAdrian Chadd */ 868f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 8698896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 870df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 871df8bae1dSRodney W. Grimes } 8724b932371SIan Dowse laddr = sin->sin_addr; 873df8bae1dSRodney W. Grimes if (lport) { 874df8bae1dSRodney W. Grimes struct inpcb *t; 875ae0e7143SRobert Watson struct tcptw *tw; 876ae0e7143SRobert Watson 877df8bae1dSRodney W. Grimes /* GROSS */ 878603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 879603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 880acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 88132f9753cSRobert Watson 0)) 8822469dd60SGarrett Wollman return (EACCES); 883835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 88486d02c5cSBjoern A. Zeeb priv_check_cred(inp->inp_cred, 88532f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 886078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 887413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 888340c35deSJonathan Lemon /* 889340c35deSJonathan Lemon * XXX 890340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 891340c35deSJonathan Lemon */ 8924cc20ab1SSeigo Tanimura if (t && 8930a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 894ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 8954658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 8964658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 8974cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 89852b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 899*1a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT) || 900*1a43cff9SSean Bruno (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 90186d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 90286d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 9034049a042SGuido van Rooij return (EADDRINUSE); 9040a100a6fSAdrian Chadd 9050a100a6fSAdrian Chadd /* 9060a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 9070a100a6fSAdrian Chadd * the credentials need to match and the 9080a100a6fSAdrian Chadd * original socket also has to have been bound 9090a100a6fSAdrian Chadd * with BINDMULTI. 9100a100a6fSAdrian Chadd */ 9110a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9120a100a6fSAdrian Chadd return (EADDRINUSE); 9134049a042SGuido van Rooij } 914c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 91568e0d7e0SRobert Watson lport, lookupflags, cred); 916ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 917ae0e7143SRobert Watson /* 918ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 919ae0e7143SRobert Watson * state recycled, we treat the address as 920ae0e7143SRobert Watson * being in use (for now). This is better 921ae0e7143SRobert Watson * than a panic, but not desirable. 922ae0e7143SRobert Watson */ 923ec95b709SMikolaj Golub tw = intotw(t); 924ae0e7143SRobert Watson if (tw == NULL || 925*1a43cff9SSean Bruno ((reuseport & tw->tw_so_options) == 0 && 926*1a43cff9SSean Bruno (reuseport_lb & 927*1a43cff9SSean Bruno tw->tw_so_options) == 0)) { 928340c35deSJonathan Lemon return (EADDRINUSE); 929*1a43cff9SSean Bruno } 9300a100a6fSAdrian Chadd } else if (t && 9310a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 932*1a43cff9SSean Bruno (reuseport & inp_so_options(t)) == 0 && 933*1a43cff9SSean Bruno (reuseport_lb & inp_so_options(t)) == 0) { 934e3fd5ffdSRobert Watson #ifdef INET6 93533841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 936cfa1ca9dSYoshinobu Inoue INADDR_ANY || 937cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 938cfa1ca9dSYoshinobu Inoue INADDR_ANY || 939fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 940fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 941e3fd5ffdSRobert Watson #endif 942df8bae1dSRodney W. Grimes return (EADDRINUSE); 9430a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 9440a100a6fSAdrian Chadd return (EADDRINUSE); 945df8bae1dSRodney W. Grimes } 946cfa1ca9dSYoshinobu Inoue } 947df8bae1dSRodney W. Grimes } 9484b932371SIan Dowse if (*lportp != 0) 9494b932371SIan Dowse lport = *lportp; 95033b3ac06SPeter Wemm if (lport == 0) { 9514616026fSErmal Luçi error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 952efc76f72SBjoern A. Zeeb if (error != 0) 953efc76f72SBjoern A. Zeeb return (error); 95433b3ac06SPeter Wemm 95533b3ac06SPeter Wemm } 9564b932371SIan Dowse *laddrp = laddr.s_addr; 9574b932371SIan Dowse *lportp = lport; 958df8bae1dSRodney W. Grimes return (0); 959df8bae1dSRodney W. Grimes } 960df8bae1dSRodney W. Grimes 961999f1343SGarrett Wollman /* 9625200e00eSIan Dowse * Connect from a socket to a specified address. 9635200e00eSIan Dowse * Both address and port must be specified in argument sin. 9645200e00eSIan Dowse * If don't have a local address for this socket yet, 9655200e00eSIan Dowse * then pick one. 966999f1343SGarrett Wollman */ 967999f1343SGarrett Wollman int 968d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 969d3c1f003SRobert Watson struct ucred *cred, struct mbuf *m) 970999f1343SGarrett Wollman { 9715200e00eSIan Dowse u_short lport, fport; 9725200e00eSIan Dowse in_addr_t laddr, faddr; 9735200e00eSIan Dowse int anonport, error; 974df8bae1dSRodney W. Grimes 9758501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 976fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 97727f74fd0SRobert Watson 9785200e00eSIan Dowse lport = inp->inp_lport; 9795200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 9805200e00eSIan Dowse anonport = (lport == 0); 9815200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 982b0330ed9SPawel Jakub Dawidek NULL, cred); 9835200e00eSIan Dowse if (error) 9845200e00eSIan Dowse return (error); 9855200e00eSIan Dowse 9865200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 9875200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 9885200e00eSIan Dowse inp->inp_lport = lport; 9895200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 9905200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 9915200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 9925200e00eSIan Dowse inp->inp_lport = 0; 9935200e00eSIan Dowse return (EAGAIN); 9945200e00eSIan Dowse } 9955200e00eSIan Dowse } 9965200e00eSIan Dowse 9975200e00eSIan Dowse /* Commit the remaining changes. */ 9985200e00eSIan Dowse inp->inp_lport = lport; 9995200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 10005200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 10015200e00eSIan Dowse inp->inp_fport = fport; 1002d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 10032cb64cb2SGeorge V. Neville-Neil 10045200e00eSIan Dowse if (anonport) 10055200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 10065200e00eSIan Dowse return (0); 10075200e00eSIan Dowse } 10085200e00eSIan Dowse 1009d3c1f003SRobert Watson int 1010d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 1011d3c1f003SRobert Watson { 1012d3c1f003SRobert Watson 1013d3c1f003SRobert Watson return (in_pcbconnect_mbuf(inp, nam, cred, NULL)); 1014d3c1f003SRobert Watson } 1015d3c1f003SRobert Watson 10165200e00eSIan Dowse /* 10170895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 10180895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 10190895aec3SBjoern A. Zeeb */ 1020ae190832SSteven Hartland int 10210895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 10220895aec3SBjoern A. Zeeb struct ucred *cred) 10230895aec3SBjoern A. Zeeb { 10240895aec3SBjoern A. Zeeb struct ifaddr *ifa; 10250895aec3SBjoern A. Zeeb struct sockaddr *sa; 10260895aec3SBjoern A. Zeeb struct sockaddr_in *sin; 10270895aec3SBjoern A. Zeeb struct route sro; 10280895aec3SBjoern A. Zeeb int error; 10290895aec3SBjoern A. Zeeb 1030413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1031592bcae8SBjoern A. Zeeb /* 1032592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 1033592bcae8SBjoern A. Zeeb * if requested. 1034592bcae8SBjoern A. Zeeb */ 1035592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 1036592bcae8SBjoern A. Zeeb return (0); 1037592bcae8SBjoern A. Zeeb 10380895aec3SBjoern A. Zeeb error = 0; 10390895aec3SBjoern A. Zeeb bzero(&sro, sizeof(sro)); 10400895aec3SBjoern A. Zeeb 10410895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)&sro.ro_dst; 10420895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 10430895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 10440895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 10450895aec3SBjoern A. Zeeb 10460895aec3SBjoern A. Zeeb /* 10470895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 10480895aec3SBjoern A. Zeeb * else punt. 10490895aec3SBjoern A. Zeeb * 10500895aec3SBjoern A. Zeeb * Find out route to destination. 10510895aec3SBjoern A. Zeeb */ 10520895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 10536e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 10540895aec3SBjoern A. Zeeb 10550895aec3SBjoern A. Zeeb /* 10560895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 10570895aec3SBjoern A. Zeeb * the outgoing interface. 10580895aec3SBjoern A. Zeeb * 10590895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 10600895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 10610895aec3SBjoern A. Zeeb * the source address from. 10620895aec3SBjoern A. Zeeb */ 10634f6c66ccSMatt Macy NET_EPOCH_ENTER(); 10640895aec3SBjoern A. Zeeb if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 10658c0fec80SRobert Watson struct in_ifaddr *ia; 10660895aec3SBjoern A. Zeeb struct ifnet *ifp; 10670895aec3SBjoern A. Zeeb 10684f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 106958a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 10704f6c66ccSMatt Macy if (ia == NULL) { 10714f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 107258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 10734f6c66ccSMatt Macy 10744f6c66ccSMatt Macy } 10750895aec3SBjoern A. Zeeb if (ia == NULL) { 10760895aec3SBjoern A. Zeeb error = ENETUNREACH; 10770895aec3SBjoern A. Zeeb goto done; 10780895aec3SBjoern A. Zeeb } 10790895aec3SBjoern A. Zeeb 10800304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 10810895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 10820895aec3SBjoern A. Zeeb goto done; 10830895aec3SBjoern A. Zeeb } 10840895aec3SBjoern A. Zeeb 10850895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 10860895aec3SBjoern A. Zeeb ia = NULL; 1087137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 1088d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 10890895aec3SBjoern A. Zeeb 10900895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 10910895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 10920895aec3SBjoern A. Zeeb continue; 10930895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1094b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 10950895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 10960895aec3SBjoern A. Zeeb break; 10970895aec3SBjoern A. Zeeb } 10980895aec3SBjoern A. Zeeb } 10990895aec3SBjoern A. Zeeb if (ia != NULL) { 11000895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1101137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 11020895aec3SBjoern A. Zeeb goto done; 11030895aec3SBjoern A. Zeeb } 1104137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 11050895aec3SBjoern A. Zeeb 11060895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1107b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11080895aec3SBjoern A. Zeeb goto done; 11090895aec3SBjoern A. Zeeb } 11100895aec3SBjoern A. Zeeb 11110895aec3SBjoern A. Zeeb /* 11120895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 11130895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 11140895aec3SBjoern A. Zeeb * In case of jails do those three steps: 11150895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 11160895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 11170895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 11180895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 11190895aec3SBjoern A. Zeeb */ 11200895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 11218c0fec80SRobert Watson struct in_ifaddr *ia; 11229317b04eSRobert Watson struct ifnet *ifp; 11230895aec3SBjoern A. Zeeb 11240895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 11250304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11260895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 11270895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11280895aec3SBjoern A. Zeeb goto done; 11290895aec3SBjoern A. Zeeb } 11300895aec3SBjoern A. Zeeb 11310895aec3SBjoern A. Zeeb /* Jailed. */ 11320895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 11330895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 1134b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11350895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 11360895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 11370895aec3SBjoern A. Zeeb goto done; 11380895aec3SBjoern A. Zeeb } 11390895aec3SBjoern A. Zeeb 11400895aec3SBjoern A. Zeeb /* 11410895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 11420895aec3SBjoern A. Zeeb * belonging to this jail. 11430895aec3SBjoern A. Zeeb */ 11448c0fec80SRobert Watson ia = NULL; 11459317b04eSRobert Watson ifp = sro.ro_rt->rt_ifp; 1146137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 1147d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11480895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 11490895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 11500895aec3SBjoern A. Zeeb continue; 11510895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1152b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 11530895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 11540895aec3SBjoern A. Zeeb break; 11550895aec3SBjoern A. Zeeb } 11560895aec3SBjoern A. Zeeb } 11570895aec3SBjoern A. Zeeb if (ia != NULL) { 11580895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1159137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 11600895aec3SBjoern A. Zeeb goto done; 11610895aec3SBjoern A. Zeeb } 1162137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 11630895aec3SBjoern A. Zeeb 11640895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1165b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 11660895aec3SBjoern A. Zeeb goto done; 11670895aec3SBjoern A. Zeeb } 11680895aec3SBjoern A. Zeeb 11690895aec3SBjoern A. Zeeb /* 11700895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 11710895aec3SBjoern A. Zeeb * to ourselves is here. 11720895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 11730895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 11740895aec3SBjoern A. Zeeb * a loopback interface. 11750895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 11760895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 11770895aec3SBjoern A. Zeeb */ 11780895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 11790895aec3SBjoern A. Zeeb struct sockaddr_in sain; 11808c0fec80SRobert Watson struct in_ifaddr *ia; 11810895aec3SBjoern A. Zeeb 11820895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 11830895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 11840895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 11850895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 11860895aec3SBjoern A. Zeeb 118758a39d8cSAlan Somers ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain), 118858a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 11890895aec3SBjoern A. Zeeb if (ia == NULL) 11904f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0, 119158a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 1192f0bb05fcSQing Li if (ia == NULL) 1193f0bb05fcSQing Li ia = ifatoia(ifa_ifwithaddr(sintosa(&sain))); 11940895aec3SBjoern A. Zeeb 11950304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 11960895aec3SBjoern A. Zeeb if (ia == NULL) { 11970895aec3SBjoern A. Zeeb error = ENETUNREACH; 11980895aec3SBjoern A. Zeeb goto done; 11990895aec3SBjoern A. Zeeb } 12000895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 12010895aec3SBjoern A. Zeeb goto done; 12020895aec3SBjoern A. Zeeb } 12030895aec3SBjoern A. Zeeb 12040895aec3SBjoern A. Zeeb /* Jailed. */ 12050895aec3SBjoern A. Zeeb if (ia != NULL) { 12060895aec3SBjoern A. Zeeb struct ifnet *ifp; 12070895aec3SBjoern A. Zeeb 12080895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 12090895aec3SBjoern A. Zeeb ia = NULL; 1210137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 1211d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12120895aec3SBjoern A. Zeeb 12130895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 12140895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 12150895aec3SBjoern A. Zeeb continue; 12160895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 1217b89e82ddSJamie Gritton if (prison_check_ip4(cred, 1218b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 12190895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 12200895aec3SBjoern A. Zeeb break; 12210895aec3SBjoern A. Zeeb } 12220895aec3SBjoern A. Zeeb } 12230895aec3SBjoern A. Zeeb if (ia != NULL) { 12240895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1225137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 12260895aec3SBjoern A. Zeeb goto done; 12270895aec3SBjoern A. Zeeb } 1228137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 12290895aec3SBjoern A. Zeeb } 12300895aec3SBjoern A. Zeeb 12310895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 1232b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 12330895aec3SBjoern A. Zeeb goto done; 12340895aec3SBjoern A. Zeeb } 12350895aec3SBjoern A. Zeeb 12360895aec3SBjoern A. Zeeb done: 12374f6c66ccSMatt Macy NET_EPOCH_EXIT(); 12380895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 12390895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 12400895aec3SBjoern A. Zeeb return (error); 12410895aec3SBjoern A. Zeeb } 12420895aec3SBjoern A. Zeeb 12430895aec3SBjoern A. Zeeb /* 12445200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 12455200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 12465200e00eSIan Dowse * address and port for the PCB; these are updated to the values 12475200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 12485200e00eSIan Dowse * the connect. 12495200e00eSIan Dowse * 12505200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 12515200e00eSIan Dowse * and port. These are not updated in the error case. 12525200e00eSIan Dowse * 12535200e00eSIan Dowse * If the operation fails because the connection already exists, 12545200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 12555200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 12565200e00eSIan Dowse * is set to NULL. 12575200e00eSIan Dowse */ 12585200e00eSIan Dowse int 1259136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1260136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1261136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 12625200e00eSIan Dowse { 1263cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 12645200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 12655200e00eSIan Dowse struct in_ifaddr *ia; 12665200e00eSIan Dowse struct inpcb *oinp; 1267b89e82ddSJamie Gritton struct in_addr laddr, faddr; 12685200e00eSIan Dowse u_short lport, fport; 12695200e00eSIan Dowse int error; 12705200e00eSIan Dowse 12718501a69cSRobert Watson /* 12728501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 12738501a69cSRobert Watson * lock is sufficient. 12748501a69cSRobert Watson */ 127527f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1276fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 127727f74fd0SRobert Watson 12785200e00eSIan Dowse if (oinpp != NULL) 12795200e00eSIan Dowse *oinpp = NULL; 128057bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1281df8bae1dSRodney W. Grimes return (EINVAL); 1282df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1283df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1284df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1285df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 12865200e00eSIan Dowse laddr.s_addr = *laddrp; 12875200e00eSIan Dowse lport = *lportp; 12885200e00eSIan Dowse faddr = sin->sin_addr; 12895200e00eSIan Dowse fport = sin->sin_port; 12900895aec3SBjoern A. Zeeb 1291d7c5a620SMatt Macy if (!CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1292df8bae1dSRodney W. Grimes /* 1293df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1294df8bae1dSRodney W. Grimes * use the primary local address. 1295df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1296df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1297df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1298df8bae1dSRodney W. Grimes */ 1299413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1300cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1301413628a7SBjoern A. Zeeb faddr = 1302d7c5a620SMatt Macy IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1303cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1304b89e82ddSJamie Gritton if (cred != NULL && 1305b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1306b89e82ddSJamie Gritton return (error); 13072d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1308cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1309d7c5a620SMatt Macy if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 13102d9cfabaSRobert Watson IFF_BROADCAST) 1311d7c5a620SMatt Macy faddr = satosin(&CK_STAILQ_FIRST( 1312603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1313cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 13142d9cfabaSRobert Watson } 1315df8bae1dSRodney W. Grimes } 13165200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1317d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1318df8bae1dSRodney W. Grimes /* 1319df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1320d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1321df8bae1dSRodney W. Grimes * address of that interface as our source address. 1322df8bae1dSRodney W. Grimes */ 13235200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1324df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1325df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1326df8bae1dSRodney W. Grimes struct ifnet *ifp; 1327df8bae1dSRodney W. Grimes 1328df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1329df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1330df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1331cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1332d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1333e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1334e691be70SDaniel Eischen (cred == NULL || 1335e691be70SDaniel Eischen prison_check_ip4(cred, 1336e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1337df8bae1dSRodney W. Grimes break; 1338e691be70SDaniel Eischen } 1339e691be70SDaniel Eischen if (ia == NULL) 1340d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1341e691be70SDaniel Eischen else { 13425200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1343d79fdd98SDaniel Eischen error = 0; 1344999f1343SGarrett Wollman } 1345cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1346d79fdd98SDaniel Eischen } 1347d79fdd98SDaniel Eischen } 134804215ed2SRandall Stewart if (error) 134904215ed2SRandall Stewart return (error); 13500895aec3SBjoern A. Zeeb } 1351fa046d87SRobert Watson oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1352fa046d87SRobert Watson laddr, lport, 0, NULL); 13535200e00eSIan Dowse if (oinp != NULL) { 13545200e00eSIan Dowse if (oinpp != NULL) 13555200e00eSIan Dowse *oinpp = oinp; 1356df8bae1dSRodney W. Grimes return (EADDRINUSE); 1357c3229e05SDavid Greenman } 13585200e00eSIan Dowse if (lport == 0) { 1359b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1360b0330ed9SPawel Jakub Dawidek cred); 13615a903f8dSPierre Beyssac if (error) 13625a903f8dSPierre Beyssac return (error); 13635a903f8dSPierre Beyssac } 13645200e00eSIan Dowse *laddrp = laddr.s_addr; 13655200e00eSIan Dowse *lportp = lport; 13665200e00eSIan Dowse *faddrp = faddr.s_addr; 13675200e00eSIan Dowse *fportp = fport; 1368df8bae1dSRodney W. Grimes return (0); 1369df8bae1dSRodney W. Grimes } 1370df8bae1dSRodney W. Grimes 137126f9a767SRodney W. Grimes void 1372136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1373df8bae1dSRodney W. Grimes { 13746b348152SRobert Watson 13758501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1376fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1377df8bae1dSRodney W. Grimes 1378df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1379df8bae1dSRodney W. Grimes inp->inp_fport = 0; 138015bd2b43SDavid Greenman in_pcbrehash(inp); 1381df8bae1dSRodney W. Grimes } 138283e521ecSBjoern A. Zeeb #endif /* INET */ 1383df8bae1dSRodney W. Grimes 13844c7c478dSRobert Watson /* 138528696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1386c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 138728696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 138828696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 13894c7c478dSRobert Watson */ 139026f9a767SRodney W. Grimes void 1391136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1392df8bae1dSRodney W. Grimes { 13934c7c478dSRobert Watson 1394a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1395c0a211c5SRobert Watson 1396f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 1397f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 1398f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 1399f3e7afe2SHans Petter Selasky #endif 14004c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 14014c7c478dSRobert Watson inp->inp_socket = NULL; 14024c7c478dSRobert Watson } 14034c7c478dSRobert Watson 1404c0a211c5SRobert Watson /* 140579bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 140679bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 140779bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 140852cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 140952cd27cbSRobert Watson * via a pcbgroup. 141079bdc6e5SRobert Watson * 141179bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 141279bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 141379bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 141479bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 141579bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 141679bdc6e5SRobert Watson * inpcb. 141779bdc6e5SRobert Watson * 141879bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 141979bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 142079bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 142179bdc6e5SRobert Watson * using in_pcbrele(). 1422c0a211c5SRobert Watson */ 142379bdc6e5SRobert Watson void 142479bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 14254c7c478dSRobert Watson { 1426df8bae1dSRodney W. Grimes 142779bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 142879bdc6e5SRobert Watson 142979bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 143079bdc6e5SRobert Watson } 143179bdc6e5SRobert Watson 143279bdc6e5SRobert Watson /* 143379bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 143479bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 143579bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 143679bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 143779bdc6e5SRobert Watson * 143879bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 143979bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 144079bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 144179bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 144279bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 144379bdc6e5SRobert Watson */ 144479bdc6e5SRobert Watson int 144579bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 144679bdc6e5SRobert Watson { 144779bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 144879bdc6e5SRobert Watson 144979bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 145079bdc6e5SRobert Watson 145179bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 145279bdc6e5SRobert Watson 1453df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1454df4e91d3SGleb Smirnoff /* 1455df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1456df4e91d3SGleb Smirnoff * this isn't the last reference. 1457df4e91d3SGleb Smirnoff */ 1458df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1459df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1460df4e91d3SGleb Smirnoff return (1); 1461df4e91d3SGleb Smirnoff } 146279bdc6e5SRobert Watson return (0); 1463df4e91d3SGleb Smirnoff } 146479bdc6e5SRobert Watson 146579bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 14663ee9c3c4SRandall Stewart #ifdef TCPHPTS 14673ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 14683ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 14693ee9c3c4SRandall Stewart /* 14703ee9c3c4SRandall Stewart * We should not be on the hpts at 14713ee9c3c4SRandall Stewart * this point in any form. we must 14723ee9c3c4SRandall Stewart * get the lock to be sure. 14733ee9c3c4SRandall Stewart */ 14743ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 14753ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 14763ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 14773ee9c3c4SRandall Stewart hpts, inp); 14783ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 14793ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 14803ee9c3c4SRandall Stewart if (inp->inp_in_input) 14813ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 14823ee9c3c4SRandall Stewart hpts, inp); 14833ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 14843ee9c3c4SRandall Stewart } 14853ee9c3c4SRandall Stewart #endif 148679bdc6e5SRobert Watson INP_RUNLOCK(inp); 148779bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 148879bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 148979bdc6e5SRobert Watson return (1); 149079bdc6e5SRobert Watson } 149179bdc6e5SRobert Watson 149279bdc6e5SRobert Watson int 149379bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 149479bdc6e5SRobert Watson { 149579bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 149679bdc6e5SRobert Watson 149779bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 149879bdc6e5SRobert Watson 149979bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 150079bdc6e5SRobert Watson 1501edd0e0b0SFabien Thomas if (refcount_release(&inp->inp_refcount) == 0) { 1502edd0e0b0SFabien Thomas /* 1503edd0e0b0SFabien Thomas * If the inpcb has been freed, let the caller know, even if 1504edd0e0b0SFabien Thomas * this isn't the last reference. 1505edd0e0b0SFabien Thomas */ 1506edd0e0b0SFabien Thomas if (inp->inp_flags2 & INP_FREED) { 1507edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1508edd0e0b0SFabien Thomas return (1); 1509edd0e0b0SFabien Thomas } 151079bdc6e5SRobert Watson return (0); 1511edd0e0b0SFabien Thomas } 151279bdc6e5SRobert Watson 151379bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 15143ee9c3c4SRandall Stewart #ifdef TCPHPTS 15153ee9c3c4SRandall Stewart if (inp->inp_in_hpts || inp->inp_in_input) { 15163ee9c3c4SRandall Stewart struct tcp_hpts_entry *hpts; 15173ee9c3c4SRandall Stewart /* 15183ee9c3c4SRandall Stewart * We should not be on the hpts at 15193ee9c3c4SRandall Stewart * this point in any form. we must 15203ee9c3c4SRandall Stewart * get the lock to be sure. 15213ee9c3c4SRandall Stewart */ 15223ee9c3c4SRandall Stewart hpts = tcp_hpts_lock(inp); 15233ee9c3c4SRandall Stewart if (inp->inp_in_hpts) 15243ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on hpts", 15253ee9c3c4SRandall Stewart hpts, inp); 15263ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15273ee9c3c4SRandall Stewart hpts = tcp_input_lock(inp); 15283ee9c3c4SRandall Stewart if (inp->inp_in_input) 15293ee9c3c4SRandall Stewart panic("Hpts:%p inp:%p at free still on input hpts", 15303ee9c3c4SRandall Stewart hpts, inp); 15313ee9c3c4SRandall Stewart mtx_unlock(&hpts->p_mtx); 15323ee9c3c4SRandall Stewart } 15333ee9c3c4SRandall Stewart #endif 153479bdc6e5SRobert Watson INP_WUNLOCK(inp); 153579bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 153679bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 153779bdc6e5SRobert Watson return (1); 153879bdc6e5SRobert Watson } 153979bdc6e5SRobert Watson 154079bdc6e5SRobert Watson /* 154179bdc6e5SRobert Watson * Temporary wrapper. 154279bdc6e5SRobert Watson */ 154379bdc6e5SRobert Watson int 154479bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 154579bdc6e5SRobert Watson { 154679bdc6e5SRobert Watson 154779bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 154879bdc6e5SRobert Watson } 154979bdc6e5SRobert Watson 1550ddece765SMatt Macy void 1551ddece765SMatt Macy in_pcblist_rele_rlocked(epoch_context_t ctx) 1552ddece765SMatt Macy { 1553ddece765SMatt Macy struct in_pcblist *il; 1554ddece765SMatt Macy struct inpcb *inp; 1555ddece765SMatt Macy struct inpcbinfo *pcbinfo; 1556ddece765SMatt Macy int i, n; 1557ddece765SMatt Macy 1558ddece765SMatt Macy il = __containerof(ctx, struct in_pcblist, il_epoch_ctx); 1559ddece765SMatt Macy pcbinfo = il->il_pcbinfo; 1560ddece765SMatt Macy n = il->il_count; 1561ddece765SMatt Macy INP_INFO_WLOCK(pcbinfo); 1562ddece765SMatt Macy for (i = 0; i < n; i++) { 1563ddece765SMatt Macy inp = il->il_inp_list[i]; 1564ddece765SMatt Macy INP_RLOCK(inp); 1565ddece765SMatt Macy if (!in_pcbrele_rlocked(inp)) 1566ddece765SMatt Macy INP_RUNLOCK(inp); 1567ddece765SMatt Macy } 1568ddece765SMatt Macy INP_INFO_WUNLOCK(pcbinfo); 1569ddece765SMatt Macy free(il, M_TEMP); 1570ddece765SMatt Macy } 1571ddece765SMatt Macy 157279bdc6e5SRobert Watson /* 157379bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 157479bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 157579bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 157679bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 157779bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 157879bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 157979bdc6e5SRobert Watson * the pcbinfo lock is held. 158079bdc6e5SRobert Watson */ 158179bdc6e5SRobert Watson void 158279bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 158379bdc6e5SRobert Watson { 1584f42a83f2SMatt Macy struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1585f42a83f2SMatt Macy 15861a3d880cSMatt Macy #ifdef INET6 15871a3d880cSMatt Macy struct ip6_moptions *im6o = NULL; 15881a3d880cSMatt Macy #endif 15891a3d880cSMatt Macy #ifdef INET 15901a3d880cSMatt Macy struct ip_moptions *imo = NULL; 15911a3d880cSMatt Macy #endif 159279bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1593f42a83f2SMatt Macy 159445a48d07SJonathan T. Looney KASSERT((inp->inp_flags2 & INP_FREED) == 0, 159545a48d07SJonathan T. Looney ("%s: called twice for pcb %p", __func__, inp)); 159645a48d07SJonathan T. Looney if (inp->inp_flags2 & INP_FREED) { 159745a48d07SJonathan T. Looney INP_WUNLOCK(inp); 159845a48d07SJonathan T. Looney return; 159945a48d07SJonathan T. Looney } 160045a48d07SJonathan T. Looney 1601ff9b006dSJulien Charbon #ifdef INVARIANTS 1602ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 1603079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(pcbinfo); 1604ff9b006dSJulien Charbon } else { 160579bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 1606ff9b006dSJulien Charbon } 1607ff9b006dSJulien Charbon #endif 160879bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 1609f42a83f2SMatt Macy 16101a3d880cSMatt Macy #ifdef INET 16111a3d880cSMatt Macy imo = inp->inp_moptions; 16121a3d880cSMatt Macy inp->inp_moptions = NULL; 16131a3d880cSMatt Macy #endif 1614f42a83f2SMatt Macy /* XXXRW: Do as much as possible here. */ 1615f42a83f2SMatt Macy #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1616f42a83f2SMatt Macy if (inp->inp_sp != NULL) 1617f42a83f2SMatt Macy ipsec_delete_pcbpolicy(inp); 1618f42a83f2SMatt Macy #endif 1619f42a83f2SMatt Macy INP_LIST_WLOCK(pcbinfo); 1620f42a83f2SMatt Macy inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1621f42a83f2SMatt Macy in_pcbremlists(inp); 1622f42a83f2SMatt Macy INP_LIST_WUNLOCK(pcbinfo); 16231a3d880cSMatt Macy #ifdef INET6 16241a3d880cSMatt Macy if (inp->inp_vflag & INP_IPV6PROTO) { 16251a3d880cSMatt Macy ip6_freepcbopts(inp->in6p_outputopts); 16261a3d880cSMatt Macy im6o = inp->in6p_moptions; 16271a3d880cSMatt Macy inp->in6p_moptions = NULL; 16281a3d880cSMatt Macy } 16291a3d880cSMatt Macy #endif 1630f42a83f2SMatt Macy if (inp->inp_options) 1631f42a83f2SMatt Macy (void)m_free(inp->inp_options); 1632fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 1633f42a83f2SMatt Macy 1634f42a83f2SMatt Macy inp->inp_vflag = 0; 1635f42a83f2SMatt Macy inp->inp_flags2 |= INP_FREED; 1636f42a83f2SMatt Macy crfree(inp->inp_cred); 1637f42a83f2SMatt Macy #ifdef MAC 1638f42a83f2SMatt Macy mac_inpcb_destroy(inp); 1639f42a83f2SMatt Macy #endif 1640f42a83f2SMatt Macy #ifdef INET6 1641f42a83f2SMatt Macy ip6_freemoptions(im6o); 1642f42a83f2SMatt Macy #endif 1643f42a83f2SMatt Macy #ifdef INET 1644f42a83f2SMatt Macy inp_freemoptions(imo); 1645f42a83f2SMatt Macy #endif 1646f42a83f2SMatt Macy if (!in_pcbrele_wlocked(inp)) 1647cb6bb230SMatt Macy INP_WUNLOCK(inp); 164828696211SRobert Watson } 164928696211SRobert Watson 165028696211SRobert Watson /* 1651c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1652c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1653c0a211c5SRobert Watson * 1654c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1655c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1656c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1657c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1658c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1659c0a211c5SRobert Watson * in_pcbdetach(). 1660c0a211c5SRobert Watson * 1661c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1662c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 166310702a28SRobert Watson */ 166410702a28SRobert Watson void 166510702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 166610702a28SRobert Watson { 166710702a28SRobert Watson 16688501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 166910702a28SRobert Watson 1670fa046d87SRobert Watson /* 1671fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1672fa046d87SRobert Watson * the hash lock...? 1673fa046d87SRobert Watson */ 1674ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1675111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 167610702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 167710702a28SRobert Watson 1678fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 1679*1a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 168010702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 168110702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 168210702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 168310702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 168410702a28SRobert Watson free(phd, M_PCB); 168510702a28SRobert Watson } 1686fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1687111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 168852cd27cbSRobert Watson #ifdef PCBGROUP 168952cd27cbSRobert Watson in_pcbgroup_remove(inp); 169052cd27cbSRobert Watson #endif 169110702a28SRobert Watson } 169210702a28SRobert Watson } 169310702a28SRobert Watson 169467107f45SBjoern A. Zeeb #ifdef INET 169554d642bbSRobert Watson /* 169654d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 169754d642bbSRobert Watson */ 169826ef6ac4SDon Lewis struct sockaddr * 1699136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 170026ef6ac4SDon Lewis { 170126ef6ac4SDon Lewis struct sockaddr_in *sin; 170226ef6ac4SDon Lewis 17031ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1704a163d034SWarner Losh M_WAITOK | M_ZERO); 170526ef6ac4SDon Lewis sin->sin_family = AF_INET; 170626ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 170726ef6ac4SDon Lewis sin->sin_addr = *addr_p; 170826ef6ac4SDon Lewis sin->sin_port = port; 170926ef6ac4SDon Lewis 171026ef6ac4SDon Lewis return (struct sockaddr *)sin; 171126ef6ac4SDon Lewis } 171226ef6ac4SDon Lewis 1713117bcae7SGarrett Wollman int 171454d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1715df8bae1dSRodney W. Grimes { 1716136d4f1cSRobert Watson struct inpcb *inp; 171726ef6ac4SDon Lewis struct in_addr addr; 171826ef6ac4SDon Lewis in_port_t port; 171942fa505bSDavid Greenman 1720fdc984f7STor Egge inp = sotoinpcb(so); 172154d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 17226466b28aSRobert Watson 1723a69042a5SRobert Watson INP_RLOCK(inp); 172426ef6ac4SDon Lewis port = inp->inp_lport; 172526ef6ac4SDon Lewis addr = inp->inp_laddr; 1726a69042a5SRobert Watson INP_RUNLOCK(inp); 172742fa505bSDavid Greenman 172826ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1729117bcae7SGarrett Wollman return 0; 1730df8bae1dSRodney W. Grimes } 1731df8bae1dSRodney W. Grimes 1732117bcae7SGarrett Wollman int 173354d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1734df8bae1dSRodney W. Grimes { 1735136d4f1cSRobert Watson struct inpcb *inp; 173626ef6ac4SDon Lewis struct in_addr addr; 173726ef6ac4SDon Lewis in_port_t port; 173842fa505bSDavid Greenman 1739fdc984f7STor Egge inp = sotoinpcb(so); 174054d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 17416466b28aSRobert Watson 1742a69042a5SRobert Watson INP_RLOCK(inp); 174326ef6ac4SDon Lewis port = inp->inp_fport; 174426ef6ac4SDon Lewis addr = inp->inp_faddr; 1745a69042a5SRobert Watson INP_RUNLOCK(inp); 174642fa505bSDavid Greenman 174726ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1748117bcae7SGarrett Wollman return 0; 1749df8bae1dSRodney W. Grimes } 1750df8bae1dSRodney W. Grimes 175126f9a767SRodney W. Grimes void 1752136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1753136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1754d1c54148SJesper Skriver { 1755f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1756d1c54148SJesper Skriver 17573dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1758f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 17598501a69cSRobert Watson INP_WLOCK(inp); 1760d1c54148SJesper Skriver #ifdef INET6 1761f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 17628501a69cSRobert Watson INP_WUNLOCK(inp); 1763d1c54148SJesper Skriver continue; 1764f76fcf6dSJeffrey Hsu } 1765d1c54148SJesper Skriver #endif 1766d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1767f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 17688501a69cSRobert Watson INP_WUNLOCK(inp); 1769d1c54148SJesper Skriver continue; 1770d1c54148SJesper Skriver } 17713dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 17728501a69cSRobert Watson INP_WUNLOCK(inp); 1773f76fcf6dSJeffrey Hsu } 17743dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1775d1c54148SJesper Skriver } 1776d1c54148SJesper Skriver 1777e43cc4aeSHajimu UMEMOTO void 1778136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1779e43cc4aeSHajimu UMEMOTO { 1780e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1781e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1782e43cc4aeSHajimu UMEMOTO int i, gap; 1783e43cc4aeSHajimu UMEMOTO 1784ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1785712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 17868501a69cSRobert Watson INP_WLOCK(inp); 1787e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1788e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1789e43cc4aeSHajimu UMEMOTO imo != NULL) { 1790e43cc4aeSHajimu UMEMOTO /* 1791e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1792e43cc4aeSHajimu UMEMOTO * detached. 1793e43cc4aeSHajimu UMEMOTO */ 1794e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1795e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1796e43cc4aeSHajimu UMEMOTO 1797e43cc4aeSHajimu UMEMOTO /* 1798e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1799e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1800cb6bb230SMatt Macy * 1801cb6bb230SMatt Macy * XXX This can all be deferred to an epoch_call 1802e43cc4aeSHajimu UMEMOTO */ 1803e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1804e43cc4aeSHajimu UMEMOTO i++) { 1805e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1806f3e1324bSStephen Hurd IN_MULTI_LOCK_ASSERT(); 1807f3e1324bSStephen Hurd in_leavegroup_locked(imo->imo_membership[i], NULL); 1808e43cc4aeSHajimu UMEMOTO gap++; 1809e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1810e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1811e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1812e43cc4aeSHajimu UMEMOTO } 1813e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1814e43cc4aeSHajimu UMEMOTO } 18158501a69cSRobert Watson INP_WUNLOCK(inp); 1816e43cc4aeSHajimu UMEMOTO } 1817ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1818e43cc4aeSHajimu UMEMOTO } 1819e43cc4aeSHajimu UMEMOTO 1820df8bae1dSRodney W. Grimes /* 1821fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1822fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1823c3229e05SDavid Greenman */ 1824d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1825df8bae1dSRodney W. Grimes struct inpcb * 1826136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 182768e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1828df8bae1dSRodney W. Grimes { 1829136d4f1cSRobert Watson struct inpcb *inp; 1830d5e8a67eSHajimu UMEMOTO #ifdef INET6 1831d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1832d5e8a67eSHajimu UMEMOTO #else 1833d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1834d5e8a67eSHajimu UMEMOTO #endif 1835d5e8a67eSHajimu UMEMOTO int wildcard; 18367bc4aca7SDavid Greenman 183768e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 183868e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 183968e0d7e0SRobert Watson 1840fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 18411b73ca0bSSam Leffler 184268e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1843c3229e05SDavid Greenman struct inpcbhead *head; 1844c3229e05SDavid Greenman /* 1845c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1846c3229e05SDavid Greenman * matches the local address and port we're looking for. 1847c3229e05SDavid Greenman */ 1848712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1849712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1850fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1851cfa1ca9dSYoshinobu Inoue #ifdef INET6 1852413628a7SBjoern A. Zeeb /* XXX inp locking */ 1853369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1854cfa1ca9dSYoshinobu Inoue continue; 1855cfa1ca9dSYoshinobu Inoue #endif 1856c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1857c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1858c3229e05SDavid Greenman inp->inp_lport == lport) { 1859c3229e05SDavid Greenman /* 1860413628a7SBjoern A. Zeeb * Found? 1861c3229e05SDavid Greenman */ 1862413628a7SBjoern A. Zeeb if (cred == NULL || 18630304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 18640304c731SJamie Gritton inp->inp_cred->cr_prison)) 1865c3229e05SDavid Greenman return (inp); 1866df8bae1dSRodney W. Grimes } 1867c3229e05SDavid Greenman } 1868c3229e05SDavid Greenman /* 1869c3229e05SDavid Greenman * Not found. 1870c3229e05SDavid Greenman */ 1871c3229e05SDavid Greenman return (NULL); 1872c3229e05SDavid Greenman } else { 1873c3229e05SDavid Greenman struct inpcbporthead *porthash; 1874c3229e05SDavid Greenman struct inpcbport *phd; 1875c3229e05SDavid Greenman struct inpcb *match = NULL; 1876c3229e05SDavid Greenman /* 1877c3229e05SDavid Greenman * Best fit PCB lookup. 1878c3229e05SDavid Greenman * 1879c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1880c3229e05SDavid Greenman * port hash list. 1881c3229e05SDavid Greenman */ 1882712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1883712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1884fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1885c3229e05SDavid Greenman if (phd->phd_port == lport) 1886c3229e05SDavid Greenman break; 1887c3229e05SDavid Greenman } 1888c3229e05SDavid Greenman if (phd != NULL) { 1889c3229e05SDavid Greenman /* 1890c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1891c3229e05SDavid Greenman * fit. 1892c3229e05SDavid Greenman */ 189337d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1894c3229e05SDavid Greenman wildcard = 0; 1895413628a7SBjoern A. Zeeb if (cred != NULL && 18960304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 18970304c731SJamie Gritton cred->cr_prison)) 1898413628a7SBjoern A. Zeeb continue; 1899cfa1ca9dSYoshinobu Inoue #ifdef INET6 1900413628a7SBjoern A. Zeeb /* XXX inp locking */ 1901369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1902cfa1ca9dSYoshinobu Inoue continue; 1903d5e8a67eSHajimu UMEMOTO /* 1904d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1905d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1906d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1907d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1908d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1909d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1910d5e8a67eSHajimu UMEMOTO * 1911d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1912d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1913d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1914d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1915d5e8a67eSHajimu UMEMOTO */ 1916d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1917d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1918cfa1ca9dSYoshinobu Inoue #endif 1919c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1920c3229e05SDavid Greenman wildcard++; 192115bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 192215bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 192315bd2b43SDavid Greenman wildcard++; 192415bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 192515bd2b43SDavid Greenman continue; 192615bd2b43SDavid Greenman } else { 192715bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 192815bd2b43SDavid Greenman wildcard++; 192915bd2b43SDavid Greenman } 1930df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1931df8bae1dSRodney W. Grimes match = inp; 1932df8bae1dSRodney W. Grimes matchwild = wildcard; 1933413628a7SBjoern A. Zeeb if (matchwild == 0) 1934df8bae1dSRodney W. Grimes break; 1935df8bae1dSRodney W. Grimes } 1936df8bae1dSRodney W. Grimes } 19373dbdc25cSDavid Greenman } 1938df8bae1dSRodney W. Grimes return (match); 1939df8bae1dSRodney W. Grimes } 1940c3229e05SDavid Greenman } 1941d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 194215bd2b43SDavid Greenman 1943*1a43cff9SSean Bruno static struct inpcb * 1944*1a43cff9SSean Bruno in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 1945*1a43cff9SSean Bruno const struct in_addr *laddr, uint16_t lport, const struct in_addr *faddr, 1946*1a43cff9SSean Bruno uint16_t fport, int lookupflags) 1947*1a43cff9SSean Bruno { 1948*1a43cff9SSean Bruno struct inpcb *local_wild = NULL; 1949*1a43cff9SSean Bruno const struct inpcblbgrouphead *hdr; 1950*1a43cff9SSean Bruno struct inpcblbgroup *grp; 1951*1a43cff9SSean Bruno struct inpcblbgroup *grp_local_wild; 1952*1a43cff9SSean Bruno 1953*1a43cff9SSean Bruno INP_HASH_LOCK_ASSERT(pcbinfo); 1954*1a43cff9SSean Bruno 1955*1a43cff9SSean Bruno hdr = &pcbinfo->ipi_lbgrouphashbase[ 1956*1a43cff9SSean Bruno INP_PCBLBGROUP_PORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 1957*1a43cff9SSean Bruno 1958*1a43cff9SSean Bruno /* 1959*1a43cff9SSean Bruno * Order of socket selection: 1960*1a43cff9SSean Bruno * 1. non-wild. 1961*1a43cff9SSean Bruno * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD). 1962*1a43cff9SSean Bruno * 1963*1a43cff9SSean Bruno * NOTE: 1964*1a43cff9SSean Bruno * - Load balanced group does not contain jailed sockets 1965*1a43cff9SSean Bruno * - Load balanced group does not contain IPv4 mapped INET6 wild sockets 1966*1a43cff9SSean Bruno */ 1967*1a43cff9SSean Bruno LIST_FOREACH(grp, hdr, il_list) { 1968*1a43cff9SSean Bruno #ifdef INET6 1969*1a43cff9SSean Bruno if (!(grp->il_vflag & INP_IPV4)) 1970*1a43cff9SSean Bruno continue; 1971*1a43cff9SSean Bruno #endif 1972*1a43cff9SSean Bruno 1973*1a43cff9SSean Bruno if (grp->il_lport == lport) { 1974*1a43cff9SSean Bruno 1975*1a43cff9SSean Bruno uint32_t idx = 0; 1976*1a43cff9SSean Bruno int pkt_hash = INP_PCBLBGROUP_PKTHASH(faddr->s_addr, 1977*1a43cff9SSean Bruno lport, fport); 1978*1a43cff9SSean Bruno 1979*1a43cff9SSean Bruno idx = pkt_hash % grp->il_inpcnt; 1980*1a43cff9SSean Bruno 1981*1a43cff9SSean Bruno if (grp->il_laddr.s_addr == laddr->s_addr) { 1982*1a43cff9SSean Bruno return (grp->il_inp[idx]); 1983*1a43cff9SSean Bruno } else { 1984*1a43cff9SSean Bruno if (grp->il_laddr.s_addr == INADDR_ANY && 1985*1a43cff9SSean Bruno (lookupflags & INPLOOKUP_WILDCARD)) { 1986*1a43cff9SSean Bruno local_wild = grp->il_inp[idx]; 1987*1a43cff9SSean Bruno grp_local_wild = grp; 1988*1a43cff9SSean Bruno } 1989*1a43cff9SSean Bruno } 1990*1a43cff9SSean Bruno } 1991*1a43cff9SSean Bruno } 1992*1a43cff9SSean Bruno if (local_wild != NULL) { 1993*1a43cff9SSean Bruno return (local_wild); 1994*1a43cff9SSean Bruno } 1995*1a43cff9SSean Bruno return (NULL); 1996*1a43cff9SSean Bruno } 1997*1a43cff9SSean Bruno 199852cd27cbSRobert Watson #ifdef PCBGROUP 199952cd27cbSRobert Watson /* 200052cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 200152cd27cbSRobert Watson */ 200252cd27cbSRobert Watson static struct inpcb * 200352cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 200452cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 200552cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 200652cd27cbSRobert Watson { 200752cd27cbSRobert Watson struct inpcbhead *head; 200852cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 200952cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 20107fb2986fSJonathan T. Looney bool locked; 201152cd27cbSRobert Watson 201252cd27cbSRobert Watson /* 201352cd27cbSRobert Watson * First look for an exact match. 201452cd27cbSRobert Watson */ 201552cd27cbSRobert Watson tmpinp = NULL; 201652cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 201752cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 201852cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 201952cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgrouphash) { 202052cd27cbSRobert Watson #ifdef INET6 202152cd27cbSRobert Watson /* XXX inp locking */ 202252cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 202352cd27cbSRobert Watson continue; 202452cd27cbSRobert Watson #endif 202552cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 202652cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 202752cd27cbSRobert Watson inp->inp_fport == fport && 202852cd27cbSRobert Watson inp->inp_lport == lport) { 202952cd27cbSRobert Watson /* 203052cd27cbSRobert Watson * XXX We should be able to directly return 203152cd27cbSRobert Watson * the inp here, without any checks. 203252cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 203352cd27cbSRobert Watson */ 203452cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 203552cd27cbSRobert Watson goto found; 203652cd27cbSRobert Watson if (tmpinp == NULL) 203752cd27cbSRobert Watson tmpinp = inp; 203852cd27cbSRobert Watson } 203952cd27cbSRobert Watson } 204052cd27cbSRobert Watson if (tmpinp != NULL) { 204152cd27cbSRobert Watson inp = tmpinp; 204252cd27cbSRobert Watson goto found; 204352cd27cbSRobert Watson } 204452cd27cbSRobert Watson 20450a100a6fSAdrian Chadd #ifdef RSS 20460a100a6fSAdrian Chadd /* 20470a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 20480a100a6fSAdrian Chadd * match for an RSS-local socket. 20490a100a6fSAdrian Chadd */ 20500a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 20510a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 20520a100a6fSAdrian Chadd #ifdef INET6 20530a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 20540a100a6fSAdrian Chadd #endif 20550a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 20560a100a6fSAdrian Chadd struct inpcbhead *head; 20570a100a6fSAdrian Chadd int injail; 20580a100a6fSAdrian Chadd 20590a100a6fSAdrian Chadd /* 20600a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 20610a100a6fSAdrian Chadd * 1. jailed, non-wild. 20620a100a6fSAdrian Chadd * 2. jailed, wild. 20630a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 20640a100a6fSAdrian Chadd * 4. non-jailed, wild. 20650a100a6fSAdrian Chadd */ 20660a100a6fSAdrian Chadd 20670a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 20680a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 20690a100a6fSAdrian Chadd LIST_FOREACH(inp, head, inp_pcbgrouphash) { 20700a100a6fSAdrian Chadd #ifdef INET6 20710a100a6fSAdrian Chadd /* XXX inp locking */ 20720a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 20730a100a6fSAdrian Chadd continue; 20740a100a6fSAdrian Chadd #endif 20750a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 20760a100a6fSAdrian Chadd inp->inp_lport != lport) 20770a100a6fSAdrian Chadd continue; 20780a100a6fSAdrian Chadd 20790a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 20800a100a6fSAdrian Chadd if (injail) { 20810a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 20820a100a6fSAdrian Chadd &laddr) != 0) 20830a100a6fSAdrian Chadd continue; 20840a100a6fSAdrian Chadd } else { 20850a100a6fSAdrian Chadd if (local_exact != NULL) 20860a100a6fSAdrian Chadd continue; 20870a100a6fSAdrian Chadd } 20880a100a6fSAdrian Chadd 20890a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 20900a100a6fSAdrian Chadd if (injail) 20910a100a6fSAdrian Chadd goto found; 20920a100a6fSAdrian Chadd else 20930a100a6fSAdrian Chadd local_exact = inp; 20940a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 20950a100a6fSAdrian Chadd #ifdef INET6 20960a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 20970a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 20980a100a6fSAdrian Chadd local_wild_mapped = inp; 20990a100a6fSAdrian Chadd else 21000a100a6fSAdrian Chadd #endif 21010a100a6fSAdrian Chadd if (injail) 21020a100a6fSAdrian Chadd jail_wild = inp; 21030a100a6fSAdrian Chadd else 21040a100a6fSAdrian Chadd local_wild = inp; 21050a100a6fSAdrian Chadd } 21060a100a6fSAdrian Chadd } /* LIST_FOREACH */ 21070a100a6fSAdrian Chadd 21080a100a6fSAdrian Chadd inp = jail_wild; 21090a100a6fSAdrian Chadd if (inp == NULL) 21100a100a6fSAdrian Chadd inp = local_exact; 21110a100a6fSAdrian Chadd if (inp == NULL) 21120a100a6fSAdrian Chadd inp = local_wild; 21130a100a6fSAdrian Chadd #ifdef INET6 21140a100a6fSAdrian Chadd if (inp == NULL) 21150a100a6fSAdrian Chadd inp = local_wild_mapped; 21160a100a6fSAdrian Chadd #endif 21170a100a6fSAdrian Chadd if (inp != NULL) 21180a100a6fSAdrian Chadd goto found; 21190a100a6fSAdrian Chadd } 21200a100a6fSAdrian Chadd #endif 21210a100a6fSAdrian Chadd 212252cd27cbSRobert Watson /* 212352cd27cbSRobert Watson * Then look for a wildcard match, if requested. 212452cd27cbSRobert Watson */ 212552cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 212652cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 212752cd27cbSRobert Watson #ifdef INET6 212852cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 212952cd27cbSRobert Watson #endif 213052cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 213152cd27cbSRobert Watson struct inpcbhead *head; 213252cd27cbSRobert Watson int injail; 213352cd27cbSRobert Watson 213452cd27cbSRobert Watson /* 213552cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 213652cd27cbSRobert Watson * 1. jailed, non-wild. 213752cd27cbSRobert Watson * 2. jailed, wild. 213852cd27cbSRobert Watson * 3. non-jailed, non-wild. 213952cd27cbSRobert Watson * 4. non-jailed, wild. 214052cd27cbSRobert Watson */ 214152cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 214252cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 214352cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 214452cd27cbSRobert Watson #ifdef INET6 214552cd27cbSRobert Watson /* XXX inp locking */ 214652cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 214752cd27cbSRobert Watson continue; 214852cd27cbSRobert Watson #endif 214952cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 215052cd27cbSRobert Watson inp->inp_lport != lport) 215152cd27cbSRobert Watson continue; 215252cd27cbSRobert Watson 215352cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 215452cd27cbSRobert Watson if (injail) { 215552cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 215652cd27cbSRobert Watson &laddr) != 0) 215752cd27cbSRobert Watson continue; 215852cd27cbSRobert Watson } else { 215952cd27cbSRobert Watson if (local_exact != NULL) 216052cd27cbSRobert Watson continue; 216152cd27cbSRobert Watson } 216252cd27cbSRobert Watson 216352cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 216452cd27cbSRobert Watson if (injail) 216552cd27cbSRobert Watson goto found; 216652cd27cbSRobert Watson else 216752cd27cbSRobert Watson local_exact = inp; 216852cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 216952cd27cbSRobert Watson #ifdef INET6 217052cd27cbSRobert Watson /* XXX inp locking, NULL check */ 217152cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 217252cd27cbSRobert Watson local_wild_mapped = inp; 217352cd27cbSRobert Watson else 217483e521ecSBjoern A. Zeeb #endif 217552cd27cbSRobert Watson if (injail) 217652cd27cbSRobert Watson jail_wild = inp; 217752cd27cbSRobert Watson else 217852cd27cbSRobert Watson local_wild = inp; 217952cd27cbSRobert Watson } 218052cd27cbSRobert Watson } /* LIST_FOREACH */ 218152cd27cbSRobert Watson inp = jail_wild; 218252cd27cbSRobert Watson if (inp == NULL) 218352cd27cbSRobert Watson inp = local_exact; 218452cd27cbSRobert Watson if (inp == NULL) 218552cd27cbSRobert Watson inp = local_wild; 218652cd27cbSRobert Watson #ifdef INET6 218752cd27cbSRobert Watson if (inp == NULL) 218852cd27cbSRobert Watson inp = local_wild_mapped; 218983e521ecSBjoern A. Zeeb #endif 219052cd27cbSRobert Watson if (inp != NULL) 219152cd27cbSRobert Watson goto found; 219252cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 219352cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 219452cd27cbSRobert Watson return (NULL); 219552cd27cbSRobert Watson 219652cd27cbSRobert Watson found: 21977fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 2198b9a9e8e9SNavdeep Parhar locked = INP_TRY_WLOCK(inp); 21997fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 2200b9a9e8e9SNavdeep Parhar locked = INP_TRY_RLOCK(inp); 22017fb2986fSJonathan T. Looney else 22027fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 22037fb2986fSJonathan T. Looney if (!locked) 220452cd27cbSRobert Watson in_pcbref(inp); 220552cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 22067fb2986fSJonathan T. Looney if (!locked) { 220752cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 220852cd27cbSRobert Watson INP_WLOCK(inp); 220952cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 221052cd27cbSRobert Watson return (NULL); 22117fb2986fSJonathan T. Looney } else { 221252cd27cbSRobert Watson INP_RLOCK(inp); 221352cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 221452cd27cbSRobert Watson return (NULL); 22157fb2986fSJonathan T. Looney } 22167fb2986fSJonathan T. Looney } 22177fb2986fSJonathan T. Looney #ifdef INVARIANTS 22187fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 22197fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 22207fb2986fSJonathan T. Looney else 22217fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 22227fb2986fSJonathan T. Looney #endif 222352cd27cbSRobert Watson return (inp); 222452cd27cbSRobert Watson } 222552cd27cbSRobert Watson #endif /* PCBGROUP */ 222652cd27cbSRobert Watson 222715bd2b43SDavid Greenman /* 2228fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2229fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 2230fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 223115bd2b43SDavid Greenman */ 2232fa046d87SRobert Watson static struct inpcb * 2233fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 223468e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2235136d4f1cSRobert Watson struct ifnet *ifp) 223615bd2b43SDavid Greenman { 223715bd2b43SDavid Greenman struct inpcbhead *head; 2238413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 223915bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 224015bd2b43SDavid Greenman 224168e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 224268e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 224368e0d7e0SRobert Watson 2244fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 2245602cc7f1SRobert Watson 224615bd2b43SDavid Greenman /* 224715bd2b43SDavid Greenman * First look for an exact match. 224815bd2b43SDavid Greenman */ 2249413628a7SBjoern A. Zeeb tmpinp = NULL; 2250712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 2251712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 2252fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 2253cfa1ca9dSYoshinobu Inoue #ifdef INET6 2254413628a7SBjoern A. Zeeb /* XXX inp locking */ 2255369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2256cfa1ca9dSYoshinobu Inoue continue; 2257cfa1ca9dSYoshinobu Inoue #endif 22586d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 2259ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 2260ca98b82cSDavid Greenman inp->inp_fport == fport && 2261413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 2262413628a7SBjoern A. Zeeb /* 2263413628a7SBjoern A. Zeeb * XXX We should be able to directly return 2264413628a7SBjoern A. Zeeb * the inp here, without any checks. 2265413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 2266413628a7SBjoern A. Zeeb */ 22670304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 2268c3229e05SDavid Greenman return (inp); 2269413628a7SBjoern A. Zeeb if (tmpinp == NULL) 2270413628a7SBjoern A. Zeeb tmpinp = inp; 2271c3229e05SDavid Greenman } 2272413628a7SBjoern A. Zeeb } 2273413628a7SBjoern A. Zeeb if (tmpinp != NULL) 2274413628a7SBjoern A. Zeeb return (tmpinp); 2275e3fd5ffdSRobert Watson 2276e3fd5ffdSRobert Watson /* 2277*1a43cff9SSean Bruno * Then look in lb group (for wildcard match). 2278*1a43cff9SSean Bruno */ 2279*1a43cff9SSean Bruno if (pcbinfo->ipi_lbgrouphashbase != NULL && 2280*1a43cff9SSean Bruno (lookupflags & INPLOOKUP_WILDCARD)) { 2281*1a43cff9SSean Bruno inp = in_pcblookup_lbgroup(pcbinfo, &laddr, lport, &faddr, 2282*1a43cff9SSean Bruno fport, lookupflags); 2283*1a43cff9SSean Bruno if (inp != NULL) { 2284*1a43cff9SSean Bruno return (inp); 2285*1a43cff9SSean Bruno } 2286*1a43cff9SSean Bruno } 2287*1a43cff9SSean Bruno 2288*1a43cff9SSean Bruno /* 2289e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 2290e3fd5ffdSRobert Watson */ 229168e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2292413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 2293e3fd5ffdSRobert Watson #ifdef INET6 2294cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 2295e3fd5ffdSRobert Watson #endif 2296413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 2297413628a7SBjoern A. Zeeb int injail; 2298413628a7SBjoern A. Zeeb 2299413628a7SBjoern A. Zeeb /* 2300413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 2301413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 2302413628a7SBjoern A. Zeeb * 2. jailed, wild. 2303413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 2304413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 2305413628a7SBjoern A. Zeeb */ 23066d6a026bSDavid Greenman 2307712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 2308712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 2309fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 2310cfa1ca9dSYoshinobu Inoue #ifdef INET6 2311413628a7SBjoern A. Zeeb /* XXX inp locking */ 2312369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2313cfa1ca9dSYoshinobu Inoue continue; 2314cfa1ca9dSYoshinobu Inoue #endif 2315413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 2316413628a7SBjoern A. Zeeb inp->inp_lport != lport) 2317413628a7SBjoern A. Zeeb continue; 2318413628a7SBjoern A. Zeeb 23190304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 2320413628a7SBjoern A. Zeeb if (injail) { 2321b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 2322b89e82ddSJamie Gritton &laddr) != 0) 2323413628a7SBjoern A. Zeeb continue; 2324413628a7SBjoern A. Zeeb } else { 2325413628a7SBjoern A. Zeeb if (local_exact != NULL) 2326413628a7SBjoern A. Zeeb continue; 2327413628a7SBjoern A. Zeeb } 2328413628a7SBjoern A. Zeeb 2329413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 2330413628a7SBjoern A. Zeeb if (injail) 2331c3229e05SDavid Greenman return (inp); 2332413628a7SBjoern A. Zeeb else 2333413628a7SBjoern A. Zeeb local_exact = inp; 2334413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 2335e3fd5ffdSRobert Watson #ifdef INET6 2336413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 23375cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 2338cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 2339cfa1ca9dSYoshinobu Inoue else 234083e521ecSBjoern A. Zeeb #endif 2341413628a7SBjoern A. Zeeb if (injail) 2342413628a7SBjoern A. Zeeb jail_wild = inp; 2343413628a7SBjoern A. Zeeb else 23446d6a026bSDavid Greenman local_wild = inp; 23456d6a026bSDavid Greenman } 2346413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 2347413628a7SBjoern A. Zeeb if (jail_wild != NULL) 2348413628a7SBjoern A. Zeeb return (jail_wild); 2349413628a7SBjoern A. Zeeb if (local_exact != NULL) 2350413628a7SBjoern A. Zeeb return (local_exact); 2351413628a7SBjoern A. Zeeb if (local_wild != NULL) 2352c3229e05SDavid Greenman return (local_wild); 2353413628a7SBjoern A. Zeeb #ifdef INET6 2354413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 2355413628a7SBjoern A. Zeeb return (local_wild_mapped); 235683e521ecSBjoern A. Zeeb #endif 235768e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 2358413628a7SBjoern A. Zeeb 23596d6a026bSDavid Greenman return (NULL); 236015bd2b43SDavid Greenman } 2361fa046d87SRobert Watson 2362fa046d87SRobert Watson /* 2363fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 2364fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 2365fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 2366fa046d87SRobert Watson */ 2367fa046d87SRobert Watson static struct inpcb * 2368fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2369fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2370fa046d87SRobert Watson struct ifnet *ifp) 2371fa046d87SRobert Watson { 2372fa046d87SRobert Watson struct inpcb *inp; 23737fb2986fSJonathan T. Looney bool locked; 2374fa046d87SRobert Watson 2375fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 2376fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2377fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 2378fa046d87SRobert Watson if (inp != NULL) { 23797fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 23807fb2986fSJonathan T. Looney locked = INP_TRY_WLOCK(inp); 23817fb2986fSJonathan T. Looney else if (lookupflags & INPLOOKUP_RLOCKPCB) 23827fb2986fSJonathan T. Looney locked = INP_TRY_RLOCK(inp); 23837fb2986fSJonathan T. Looney else 23847fb2986fSJonathan T. Looney panic("%s: locking bug", __func__); 23857fb2986fSJonathan T. Looney if (!locked) 2386fa046d87SRobert Watson in_pcbref(inp); 2387fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 23887fb2986fSJonathan T. Looney if (!locked) { 2389fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 2390fa046d87SRobert Watson INP_WLOCK(inp); 2391fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) 2392fa046d87SRobert Watson return (NULL); 23937fb2986fSJonathan T. Looney } else { 2394fa046d87SRobert Watson INP_RLOCK(inp); 2395fa046d87SRobert Watson if (in_pcbrele_rlocked(inp)) 2396fa046d87SRobert Watson return (NULL); 23977fb2986fSJonathan T. Looney } 23987fb2986fSJonathan T. Looney } 23997fb2986fSJonathan T. Looney #ifdef INVARIANTS 24007fb2986fSJonathan T. Looney if (lookupflags & INPLOOKUP_WLOCKPCB) 24017fb2986fSJonathan T. Looney INP_WLOCK_ASSERT(inp); 24027fb2986fSJonathan T. Looney else 24037fb2986fSJonathan T. Looney INP_RLOCK_ASSERT(inp); 24047fb2986fSJonathan T. Looney #endif 2405fa046d87SRobert Watson } else 2406fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 2407fa046d87SRobert Watson return (inp); 2408fa046d87SRobert Watson } 2409fa046d87SRobert Watson 2410fa046d87SRobert Watson /* 2411d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2412d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 241352cd27cbSRobert Watson * 241452cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 2415fa046d87SRobert Watson */ 2416fa046d87SRobert Watson struct inpcb * 2417fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2418fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 2419fa046d87SRobert Watson { 24207527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 242152cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 242252cd27cbSRobert Watson #endif 2423fa046d87SRobert Watson 2424fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2425fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2426fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2427fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 2428fa046d87SRobert Watson 24297527624eSRobert Watson /* 24307527624eSRobert Watson * When not using RSS, use connection groups in preference to the 24317527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 24327527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 24337527624eSRobert Watson * in software. 24347527624eSRobert Watson * 24357527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 24367527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 24377527624eSRobert Watson * in software. 24387527624eSRobert Watson */ 24397527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 244052cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 244152cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 244252cd27cbSRobert Watson fport); 244352cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 244452cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 244552cd27cbSRobert Watson } 244652cd27cbSRobert Watson #endif 2447fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2448fa046d87SRobert Watson lookupflags, ifp)); 2449fa046d87SRobert Watson } 2450d3c1f003SRobert Watson 2451d3c1f003SRobert Watson struct inpcb * 2452d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2453d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2454d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2455d3c1f003SRobert Watson { 245652cd27cbSRobert Watson #ifdef PCBGROUP 245752cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 245852cd27cbSRobert Watson #endif 2459d3c1f003SRobert Watson 2460d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2461d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2462d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2463d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2464d3c1f003SRobert Watson 246552cd27cbSRobert Watson #ifdef PCBGROUP 24667527624eSRobert Watson /* 24677527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 24687527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 24697527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 24707527624eSRobert Watson * using RSS. 24717527624eSRobert Watson * 24727527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 24737527624eSRobert Watson */ 24747527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 24757527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 247652cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 247752cd27cbSRobert Watson m->m_pkthdr.flowid); 247852cd27cbSRobert Watson if (pcbgroup != NULL) 247952cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 248052cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 24817527624eSRobert Watson #ifndef RSS 248252cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 248352cd27cbSRobert Watson fport); 248452cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 248552cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 24867527624eSRobert Watson #endif 248752cd27cbSRobert Watson } 248852cd27cbSRobert Watson #endif 2489d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2490d3c1f003SRobert Watson lookupflags, ifp)); 2491d3c1f003SRobert Watson } 249267107f45SBjoern A. Zeeb #endif /* INET */ 249315bd2b43SDavid Greenman 24947bc4aca7SDavid Greenman /* 2495c3229e05SDavid Greenman * Insert PCB onto various hash lists. 24967bc4aca7SDavid Greenman */ 249752cd27cbSRobert Watson static int 249852cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 249915bd2b43SDavid Greenman { 2500c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2501c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2502c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2503c3229e05SDavid Greenman struct inpcbport *phd; 2504cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 2505*1a43cff9SSean Bruno int so_options; 250615bd2b43SDavid Greenman 25078501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2508fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2509fa046d87SRobert Watson 2510111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2511111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2512602cc7f1SRobert Watson 2513cfa1ca9dSYoshinobu Inoue #ifdef INET6 2514cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 25151b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2516cfa1ca9dSYoshinobu Inoue else 251783e521ecSBjoern A. Zeeb #endif 2518cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2519cfa1ca9dSYoshinobu Inoue 2520712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2521712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 252215bd2b43SDavid Greenman 2523712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2524712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2525c3229e05SDavid Greenman 2526c3229e05SDavid Greenman /* 2527*1a43cff9SSean Bruno * Add entry to load balance group. 2528*1a43cff9SSean Bruno * Only do this if SO_REUSEPORT_LB is set. 2529*1a43cff9SSean Bruno */ 2530*1a43cff9SSean Bruno so_options = inp_so_options(inp); 2531*1a43cff9SSean Bruno if (so_options & SO_REUSEPORT_LB) { 2532*1a43cff9SSean Bruno int ret = in_pcbinslbgrouphash(inp); 2533*1a43cff9SSean Bruno if (ret) { 2534*1a43cff9SSean Bruno /* pcb lb group malloc fail (ret=ENOBUFS). */ 2535*1a43cff9SSean Bruno return (ret); 2536*1a43cff9SSean Bruno } 2537*1a43cff9SSean Bruno } 2538*1a43cff9SSean Bruno 2539*1a43cff9SSean Bruno /* 2540c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2541c3229e05SDavid Greenman */ 2542fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 2543c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2544c3229e05SDavid Greenman break; 2545c3229e05SDavid Greenman } 2546c3229e05SDavid Greenman /* 2547c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2548c3229e05SDavid Greenman */ 2549c3229e05SDavid Greenman if (phd == NULL) { 25501ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2551c3229e05SDavid Greenman if (phd == NULL) { 2552c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2553c3229e05SDavid Greenman } 2554c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2555c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 2556c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2557c3229e05SDavid Greenman } 2558c3229e05SDavid Greenman inp->inp_phd = phd; 2559c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2560c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2561111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 256252cd27cbSRobert Watson #ifdef PCBGROUP 256352cd27cbSRobert Watson if (do_pcbgroup_update) 256452cd27cbSRobert Watson in_pcbgroup_update(inp); 256552cd27cbSRobert Watson #endif 2566c3229e05SDavid Greenman return (0); 256715bd2b43SDavid Greenman } 256815bd2b43SDavid Greenman 2569c3229e05SDavid Greenman /* 257052cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 257152cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 257252cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 257352cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 257452cd27cbSRobert Watson * pcbgroup until later. 257552cd27cbSRobert Watson * 257652cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 257752cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 257852cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 257952cd27cbSRobert Watson */ 258052cd27cbSRobert Watson int 258152cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 258252cd27cbSRobert Watson { 258352cd27cbSRobert Watson 258452cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 258552cd27cbSRobert Watson } 258652cd27cbSRobert Watson 258752cd27cbSRobert Watson int 258852cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 258952cd27cbSRobert Watson { 259052cd27cbSRobert Watson 259152cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 259252cd27cbSRobert Watson } 259352cd27cbSRobert Watson 259452cd27cbSRobert Watson /* 2595c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2596c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2597c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2598c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2599c3229e05SDavid Greenman */ 260015bd2b43SDavid Greenman void 2601d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 260215bd2b43SDavid Greenman { 260359daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 260415bd2b43SDavid Greenman struct inpcbhead *head; 2605cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 260615bd2b43SDavid Greenman 26078501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2608fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2609fa046d87SRobert Watson 2610111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2611111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2612602cc7f1SRobert Watson 2613cfa1ca9dSYoshinobu Inoue #ifdef INET6 2614cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 26151b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2616cfa1ca9dSYoshinobu Inoue else 261783e521ecSBjoern A. Zeeb #endif 2618cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2619cfa1ca9dSYoshinobu Inoue 2620712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2621712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 262215bd2b43SDavid Greenman 2623c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 262415bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 262552cd27cbSRobert Watson 262652cd27cbSRobert Watson #ifdef PCBGROUP 262752cd27cbSRobert Watson if (m != NULL) 262852cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 262952cd27cbSRobert Watson else 263052cd27cbSRobert Watson in_pcbgroup_update(inp); 263152cd27cbSRobert Watson #endif 2632c3229e05SDavid Greenman } 2633c3229e05SDavid Greenman 2634d3c1f003SRobert Watson void 2635d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2636d3c1f003SRobert Watson { 2637d3c1f003SRobert Watson 2638d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2639d3c1f003SRobert Watson } 2640d3c1f003SRobert Watson 2641c3229e05SDavid Greenman /* 2642c3229e05SDavid Greenman * Remove PCB from various lists. 2643c3229e05SDavid Greenman */ 26446d888973SRobert Watson static void 2645136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2646c3229e05SDavid Greenman { 264759daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 264859daba27SSam Leffler 2649ff9b006dSJulien Charbon #ifdef INVARIANTS 2650ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 2651ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 2652ff9b006dSJulien Charbon } else { 265359daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 2654ff9b006dSJulien Charbon } 2655ff9b006dSJulien Charbon #endif 2656ff9b006dSJulien Charbon 26578501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2658ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 265959daba27SSam Leffler 266059daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2661111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2662c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2663c3229e05SDavid Greenman 2664fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 2665*1a43cff9SSean Bruno 2666*1a43cff9SSean Bruno /* XXX: Only do if SO_REUSEPORT_LB set? */ 2667*1a43cff9SSean Bruno in_pcbremlbgrouphash(inp); 2668*1a43cff9SSean Bruno 2669c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 2670c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 2671fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2672c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 2673c3229e05SDavid Greenman free(phd, M_PCB); 2674c3229e05SDavid Greenman } 2675fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2676111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2677c3229e05SDavid Greenman } 2678c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 267959daba27SSam Leffler pcbinfo->ipi_count--; 268052cd27cbSRobert Watson #ifdef PCBGROUP 268152cd27cbSRobert Watson in_pcbgroup_remove(inp); 268252cd27cbSRobert Watson #endif 268315bd2b43SDavid Greenman } 268475c13541SPoul-Henning Kamp 2685a557af22SRobert Watson /* 268684cc0778SGeorge V. Neville-Neil * Check for alternatives when higher level complains 268784cc0778SGeorge V. Neville-Neil * about service problems. For now, invalidate cached 268884cc0778SGeorge V. Neville-Neil * routing information. If the route was created dynamically 268984cc0778SGeorge V. Neville-Neil * (by a redirect), time to try a default gateway again. 269084cc0778SGeorge V. Neville-Neil */ 269184cc0778SGeorge V. Neville-Neil void 269284cc0778SGeorge V. Neville-Neil in_losing(struct inpcb *inp) 269384cc0778SGeorge V. Neville-Neil { 269484cc0778SGeorge V. Neville-Neil 2695fc21c53fSRyan Stone RO_INVALIDATE_CACHE(&inp->inp_route); 269684cc0778SGeorge V. Neville-Neil return; 269784cc0778SGeorge V. Neville-Neil } 269884cc0778SGeorge V. Neville-Neil 269984cc0778SGeorge V. Neville-Neil /* 2700a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2701a557af22SRobert Watson * label change into the in_pcb for the socket. 2702a557af22SRobert Watson */ 2703a557af22SRobert Watson void 2704136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2705a557af22SRobert Watson { 2706a557af22SRobert Watson #ifdef MAC 2707a557af22SRobert Watson struct inpcb *inp; 2708a557af22SRobert Watson 27094c7c478dSRobert Watson inp = sotoinpcb(so); 27104c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2711602cc7f1SRobert Watson 27128501a69cSRobert Watson INP_WLOCK(inp); 2713310e7cebSRobert Watson SOCK_LOCK(so); 2714a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2715310e7cebSRobert Watson SOCK_UNLOCK(so); 27168501a69cSRobert Watson INP_WUNLOCK(inp); 2717a557af22SRobert Watson #endif 2718a557af22SRobert Watson } 27195f311da2SMike Silbersack 27205f311da2SMike Silbersack /* 2721ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2722ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2723ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2724ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2725ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 27265f311da2SMike Silbersack */ 2727aae49dd3SBjoern A. Zeeb static void 2728136d4f1cSRobert Watson ipport_tick(void *xtp) 27295f311da2SMike Silbersack { 27308b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2731ad3a630fSRobert Watson 27325ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 27338b615593SMarko Zec VNET_FOREACH(vnet_iter) { 27348b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 27358b615593SMarko Zec if (V_ipport_tcpallocs <= 27368b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2737603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2738603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2739ad3a630fSRobert Watson } else 2740603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2741603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 27428b615593SMarko Zec CURVNET_RESTORE(); 27438b615593SMarko Zec } 27445ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 27455f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 27465f311da2SMike Silbersack } 2747497057eeSRobert Watson 2748aae49dd3SBjoern A. Zeeb static void 2749aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2750aae49dd3SBjoern A. Zeeb { 2751aae49dd3SBjoern A. Zeeb 2752aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2753aae49dd3SBjoern A. Zeeb } 2754aae49dd3SBjoern A. Zeeb 2755aae49dd3SBjoern A. Zeeb /* 2756aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2757aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2758aae49dd3SBjoern A. Zeeb */ 2759aae49dd3SBjoern A. Zeeb static void 2760aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2761aae49dd3SBjoern A. Zeeb { 2762aae49dd3SBjoern A. Zeeb 2763aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2764fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2765aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2766aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2767aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2768aae49dd3SBjoern A. Zeeb } 2769aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2770aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2771aae49dd3SBjoern A. Zeeb 27723d585327SKip Macy void 27733d585327SKip Macy inp_wlock(struct inpcb *inp) 27743d585327SKip Macy { 27753d585327SKip Macy 27768501a69cSRobert Watson INP_WLOCK(inp); 27773d585327SKip Macy } 27783d585327SKip Macy 27793d585327SKip Macy void 27803d585327SKip Macy inp_wunlock(struct inpcb *inp) 27813d585327SKip Macy { 27823d585327SKip Macy 27838501a69cSRobert Watson INP_WUNLOCK(inp); 27843d585327SKip Macy } 27853d585327SKip Macy 27863d585327SKip Macy void 27873d585327SKip Macy inp_rlock(struct inpcb *inp) 27883d585327SKip Macy { 27893d585327SKip Macy 2790a69042a5SRobert Watson INP_RLOCK(inp); 27913d585327SKip Macy } 27923d585327SKip Macy 27933d585327SKip Macy void 27943d585327SKip Macy inp_runlock(struct inpcb *inp) 27953d585327SKip Macy { 27963d585327SKip Macy 2797a69042a5SRobert Watson INP_RUNLOCK(inp); 27983d585327SKip Macy } 27993d585327SKip Macy 2800c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT 28013d585327SKip Macy void 2802e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 28033d585327SKip Macy { 28043d585327SKip Macy 28058501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 28063d585327SKip Macy } 28073d585327SKip Macy 28083d585327SKip Macy void 2809e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 28103d585327SKip Macy { 28113d585327SKip Macy 28123d585327SKip Macy INP_UNLOCK_ASSERT(inp); 28133d585327SKip Macy } 28143d585327SKip Macy #endif 28153d585327SKip Macy 28169378e437SKip Macy void 28179378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 28189378e437SKip Macy { 28199378e437SKip Macy struct inpcb *inp; 28209378e437SKip Macy 2821ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 282297021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 28239378e437SKip Macy INP_WLOCK(inp); 28249378e437SKip Macy func(inp, arg); 28259378e437SKip Macy INP_WUNLOCK(inp); 28269378e437SKip Macy } 2827ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 28289378e437SKip Macy } 28299378e437SKip Macy 28309378e437SKip Macy struct socket * 28319378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 28329378e437SKip Macy { 28339378e437SKip Macy 28349378e437SKip Macy INP_WLOCK_ASSERT(inp); 28359378e437SKip Macy return (inp->inp_socket); 28369378e437SKip Macy } 28379378e437SKip Macy 28389378e437SKip Macy struct tcpcb * 28399378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 28409378e437SKip Macy { 28419378e437SKip Macy 28429378e437SKip Macy INP_WLOCK_ASSERT(inp); 28439378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 28449378e437SKip Macy } 28459378e437SKip Macy 28469378e437SKip Macy int 28479378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 28489378e437SKip Macy { 28499378e437SKip Macy 28509378e437SKip Macy return (inp->inp_ip_tos); 28519378e437SKip Macy } 28529378e437SKip Macy 28539378e437SKip Macy void 28549378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 28559378e437SKip Macy { 28569378e437SKip Macy 28579378e437SKip Macy inp->inp_ip_tos = val; 28589378e437SKip Macy } 28599378e437SKip Macy 28609378e437SKip Macy void 2861df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 28629d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 28639378e437SKip Macy { 28649378e437SKip Macy 28659d29c635SKip Macy INP_LOCK_ASSERT(inp); 2866df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2867df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 28689378e437SKip Macy *lp = inp->inp_lport; 28699378e437SKip Macy *fp = inp->inp_fport; 28709378e437SKip Macy } 28719378e437SKip Macy 2872dd0e6c38SKip Macy struct inpcb * 2873dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2874dd0e6c38SKip Macy { 2875dd0e6c38SKip Macy 2876dd0e6c38SKip Macy return (sotoinpcb(so)); 2877dd0e6c38SKip Macy } 2878dd0e6c38SKip Macy 2879dd0e6c38SKip Macy struct tcpcb * 2880dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2881dd0e6c38SKip Macy { 2882dd0e6c38SKip Macy 2883dd0e6c38SKip Macy return (sototcpcb(so)); 2884dd0e6c38SKip Macy } 2885dd0e6c38SKip Macy 2886cc65eb4eSGleb Smirnoff /* 2887cc65eb4eSGleb Smirnoff * Create an external-format (``xinpcb'') structure using the information in 2888cc65eb4eSGleb Smirnoff * the kernel-format in_pcb structure pointed to by inp. This is done to 2889cc65eb4eSGleb Smirnoff * reduce the spew of irrelevant information over this interface, to isolate 2890cc65eb4eSGleb Smirnoff * user code from changes in the kernel structure, and potentially to provide 2891cc65eb4eSGleb Smirnoff * information-hiding if we decide that some of this information should be 2892cc65eb4eSGleb Smirnoff * hidden from users. 2893cc65eb4eSGleb Smirnoff */ 2894cc65eb4eSGleb Smirnoff void 2895cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2896cc65eb4eSGleb Smirnoff { 2897cc65eb4eSGleb Smirnoff 2898cc65eb4eSGleb Smirnoff xi->xi_len = sizeof(struct xinpcb); 2899cc65eb4eSGleb Smirnoff if (inp->inp_socket) 2900cc65eb4eSGleb Smirnoff sotoxsocket(inp->inp_socket, &xi->xi_socket); 2901cc65eb4eSGleb Smirnoff else 2902cc65eb4eSGleb Smirnoff bzero(&xi->xi_socket, sizeof(struct xsocket)); 2903cc65eb4eSGleb Smirnoff bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2904cc65eb4eSGleb Smirnoff xi->inp_gencnt = inp->inp_gencnt; 2905cc65eb4eSGleb Smirnoff xi->inp_ppcb = inp->inp_ppcb; 2906cc65eb4eSGleb Smirnoff xi->inp_flow = inp->inp_flow; 2907cc65eb4eSGleb Smirnoff xi->inp_flowid = inp->inp_flowid; 2908cc65eb4eSGleb Smirnoff xi->inp_flowtype = inp->inp_flowtype; 2909cc65eb4eSGleb Smirnoff xi->inp_flags = inp->inp_flags; 2910cc65eb4eSGleb Smirnoff xi->inp_flags2 = inp->inp_flags2; 2911cc65eb4eSGleb Smirnoff xi->inp_rss_listen_bucket = inp->inp_rss_listen_bucket; 2912cc65eb4eSGleb Smirnoff xi->in6p_cksum = inp->in6p_cksum; 2913cc65eb4eSGleb Smirnoff xi->in6p_hops = inp->in6p_hops; 2914cc65eb4eSGleb Smirnoff xi->inp_ip_tos = inp->inp_ip_tos; 2915cc65eb4eSGleb Smirnoff xi->inp_vflag = inp->inp_vflag; 2916cc65eb4eSGleb Smirnoff xi->inp_ip_ttl = inp->inp_ip_ttl; 2917cc65eb4eSGleb Smirnoff xi->inp_ip_p = inp->inp_ip_p; 2918cc65eb4eSGleb Smirnoff xi->inp_ip_minttl = inp->inp_ip_minttl; 2919cc65eb4eSGleb Smirnoff } 2920cc65eb4eSGleb Smirnoff 2921497057eeSRobert Watson #ifdef DDB 2922497057eeSRobert Watson static void 2923497057eeSRobert Watson db_print_indent(int indent) 2924497057eeSRobert Watson { 2925497057eeSRobert Watson int i; 2926497057eeSRobert Watson 2927497057eeSRobert Watson for (i = 0; i < indent; i++) 2928497057eeSRobert Watson db_printf(" "); 2929497057eeSRobert Watson } 2930497057eeSRobert Watson 2931497057eeSRobert Watson static void 2932497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2933497057eeSRobert Watson { 2934497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2935497057eeSRobert Watson 2936497057eeSRobert Watson db_print_indent(indent); 2937497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2938497057eeSRobert Watson 2939497057eeSRobert Watson indent += 2; 2940497057eeSRobert Watson 294103dc38a4SRobert Watson #ifdef INET6 2942dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2943497057eeSRobert Watson /* IPv6. */ 2944497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2945497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 294683e521ecSBjoern A. Zeeb } else 294703dc38a4SRobert Watson #endif 294883e521ecSBjoern A. Zeeb { 2949497057eeSRobert Watson /* IPv4. */ 2950497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2951497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2952497057eeSRobert Watson } 2953497057eeSRobert Watson db_print_indent(indent); 2954497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2955497057eeSRobert Watson ntohs(inc->inc_lport)); 2956497057eeSRobert Watson db_print_indent(indent); 2957497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2958497057eeSRobert Watson ntohs(inc->inc_fport)); 2959497057eeSRobert Watson } 2960497057eeSRobert Watson 2961497057eeSRobert Watson static void 2962497057eeSRobert Watson db_print_inpflags(int inp_flags) 2963497057eeSRobert Watson { 2964497057eeSRobert Watson int comma; 2965497057eeSRobert Watson 2966497057eeSRobert Watson comma = 0; 2967497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2968497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2969497057eeSRobert Watson comma = 1; 2970497057eeSRobert Watson } 2971497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2972497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2973497057eeSRobert Watson comma = 1; 2974497057eeSRobert Watson } 2975497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2976497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2977497057eeSRobert Watson comma = 1; 2978497057eeSRobert Watson } 2979dce33a45SErmal Luçi if (inp_flags & INP_ORIGDSTADDR) { 2980dce33a45SErmal Luçi db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 2981dce33a45SErmal Luçi comma = 1; 2982dce33a45SErmal Luçi } 2983497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2984497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2985497057eeSRobert Watson comma = 1; 2986497057eeSRobert Watson } 2987497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2988497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2989497057eeSRobert Watson comma = 1; 2990497057eeSRobert Watson } 2991497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2992497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2993497057eeSRobert Watson comma = 1; 2994497057eeSRobert Watson } 2995497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2996497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2997497057eeSRobert Watson comma = 1; 2998497057eeSRobert Watson } 2999497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 3000497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 3001497057eeSRobert Watson comma = 1; 3002497057eeSRobert Watson } 3003497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 3004497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 3005497057eeSRobert Watson comma = 1; 3006497057eeSRobert Watson } 3007497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 3008497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 3009497057eeSRobert Watson comma = 1; 3010497057eeSRobert Watson } 3011497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 3012497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 3013497057eeSRobert Watson comma = 1; 3014497057eeSRobert Watson } 30153cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 30163cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 30173cca425bSMichael Tuexen comma = 1; 30183cca425bSMichael Tuexen } 3019497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 3020497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3021497057eeSRobert Watson comma = 1; 3022497057eeSRobert Watson } 3023497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 3024497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3025497057eeSRobert Watson comma = 1; 3026497057eeSRobert Watson } 3027497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 3028497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3029497057eeSRobert Watson comma = 1; 3030497057eeSRobert Watson } 3031497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 3032497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3033497057eeSRobert Watson comma = 1; 3034497057eeSRobert Watson } 3035497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 3036497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3037497057eeSRobert Watson comma = 1; 3038497057eeSRobert Watson } 3039497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 3040497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3041497057eeSRobert Watson comma = 1; 3042497057eeSRobert Watson } 3043497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 3044497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3045497057eeSRobert Watson comma = 1; 3046497057eeSRobert Watson } 3047497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 3048497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3049497057eeSRobert Watson comma = 1; 3050497057eeSRobert Watson } 3051497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 3052497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3053497057eeSRobert Watson comma = 1; 3054497057eeSRobert Watson } 3055ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 3056ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 3057ad71fe3cSRobert Watson comma = 1; 3058ad71fe3cSRobert Watson } 3059ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 3060ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3061ad71fe3cSRobert Watson comma = 1; 3062ad71fe3cSRobert Watson } 3063ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 3064ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 3065ad71fe3cSRobert Watson comma = 1; 3066ad71fe3cSRobert Watson } 3067ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 3068ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3069ad71fe3cSRobert Watson comma = 1; 3070ad71fe3cSRobert Watson } 3071497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 3072497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3073497057eeSRobert Watson comma = 1; 3074497057eeSRobert Watson } 3075497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 3076497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 3077497057eeSRobert Watson comma = 1; 3078497057eeSRobert Watson } 3079497057eeSRobert Watson } 3080497057eeSRobert Watson 3081497057eeSRobert Watson static void 3082497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 3083497057eeSRobert Watson { 3084497057eeSRobert Watson int comma; 3085497057eeSRobert Watson 3086497057eeSRobert Watson comma = 0; 3087497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 3088497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 3089497057eeSRobert Watson comma = 1; 3090497057eeSRobert Watson } 3091497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 3092497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 3093497057eeSRobert Watson comma = 1; 3094497057eeSRobert Watson } 3095497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 3096497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3097497057eeSRobert Watson comma = 1; 3098497057eeSRobert Watson } 3099497057eeSRobert Watson } 3100497057eeSRobert Watson 31016d888973SRobert Watson static void 3102497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3103497057eeSRobert Watson { 3104497057eeSRobert Watson 3105497057eeSRobert Watson db_print_indent(indent); 3106497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 3107497057eeSRobert Watson 3108497057eeSRobert Watson indent += 2; 3109497057eeSRobert Watson 3110497057eeSRobert Watson db_print_indent(indent); 3111497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3112497057eeSRobert Watson 3113497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3114497057eeSRobert Watson 3115497057eeSRobert Watson db_print_indent(indent); 3116497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 3117497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 3118497057eeSRobert Watson 3119497057eeSRobert Watson db_print_indent(indent); 3120497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 3121497057eeSRobert Watson inp->inp_label, inp->inp_flags); 3122497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 3123497057eeSRobert Watson db_printf(")\n"); 3124497057eeSRobert Watson 3125497057eeSRobert Watson db_print_indent(indent); 3126497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3127497057eeSRobert Watson inp->inp_vflag); 3128497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 3129497057eeSRobert Watson db_printf(")\n"); 3130497057eeSRobert Watson 3131497057eeSRobert Watson db_print_indent(indent); 3132497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3133497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3134497057eeSRobert Watson 3135497057eeSRobert Watson db_print_indent(indent); 3136497057eeSRobert Watson #ifdef INET6 3137497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 3138497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 3139497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 3140497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 3141497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3142497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3143497057eeSRobert Watson inp->in6p_hops); 3144497057eeSRobert Watson } else 3145497057eeSRobert Watson #endif 3146497057eeSRobert Watson { 3147497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 3148497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3149497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 3150497057eeSRobert Watson } 3151497057eeSRobert Watson 3152497057eeSRobert Watson db_print_indent(indent); 3153497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3154497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 3155497057eeSRobert Watson } 3156497057eeSRobert Watson 3157497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3158497057eeSRobert Watson { 3159497057eeSRobert Watson struct inpcb *inp; 3160497057eeSRobert Watson 3161497057eeSRobert Watson if (!have_addr) { 3162497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 3163497057eeSRobert Watson return; 3164497057eeSRobert Watson } 3165497057eeSRobert Watson inp = (struct inpcb *)addr; 3166497057eeSRobert Watson 3167497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 3168497057eeSRobert Watson } 316983e521ecSBjoern A. Zeeb #endif /* DDB */ 3170f3e7afe2SHans Petter Selasky 3171f3e7afe2SHans Petter Selasky #ifdef RATELIMIT 3172f3e7afe2SHans Petter Selasky /* 3173f3e7afe2SHans Petter Selasky * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3174f3e7afe2SHans Petter Selasky * if any. 3175f3e7afe2SHans Petter Selasky */ 3176f3e7afe2SHans Petter Selasky int 3177f3e7afe2SHans Petter Selasky in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3178f3e7afe2SHans Petter Selasky { 3179f3e7afe2SHans Petter Selasky union if_snd_tag_modify_params params = { 3180f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 3181f3e7afe2SHans Petter Selasky }; 3182f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3183f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3184f3e7afe2SHans Petter Selasky int error; 3185f3e7afe2SHans Petter Selasky 3186f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3187f3e7afe2SHans Petter Selasky if (mst == NULL) 3188f3e7afe2SHans Petter Selasky return (EINVAL); 3189f3e7afe2SHans Petter Selasky 3190f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3191f3e7afe2SHans Petter Selasky if (ifp == NULL) 3192f3e7afe2SHans Petter Selasky return (EINVAL); 3193f3e7afe2SHans Petter Selasky 3194f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_modify == NULL) { 3195f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3196f3e7afe2SHans Petter Selasky } else { 3197f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_modify(mst, ¶ms); 3198f3e7afe2SHans Petter Selasky } 3199f3e7afe2SHans Petter Selasky return (error); 3200f3e7afe2SHans Petter Selasky } 3201f3e7afe2SHans Petter Selasky 3202f3e7afe2SHans Petter Selasky /* 3203f3e7afe2SHans Petter Selasky * Query existing TX rate limit based on the existing 3204f3e7afe2SHans Petter Selasky * "inp->inp_snd_tag", if any. 3205f3e7afe2SHans Petter Selasky */ 3206f3e7afe2SHans Petter Selasky int 3207f3e7afe2SHans Petter Selasky in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3208f3e7afe2SHans Petter Selasky { 3209f3e7afe2SHans Petter Selasky union if_snd_tag_query_params params = { }; 3210f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3211f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3212f3e7afe2SHans Petter Selasky int error; 3213f3e7afe2SHans Petter Selasky 3214f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3215f3e7afe2SHans Petter Selasky if (mst == NULL) 3216f3e7afe2SHans Petter Selasky return (EINVAL); 3217f3e7afe2SHans Petter Selasky 3218f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3219f3e7afe2SHans Petter Selasky if (ifp == NULL) 3220f3e7afe2SHans Petter Selasky return (EINVAL); 3221f3e7afe2SHans Petter Selasky 3222f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) { 3223f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3224f3e7afe2SHans Petter Selasky } else { 3225f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 3226f3e7afe2SHans Petter Selasky if (error == 0 && p_max_pacing_rate != NULL) 3227f3e7afe2SHans Petter Selasky *p_max_pacing_rate = params.rate_limit.max_rate; 3228f3e7afe2SHans Petter Selasky } 3229f3e7afe2SHans Petter Selasky return (error); 3230f3e7afe2SHans Petter Selasky } 3231f3e7afe2SHans Petter Selasky 3232f3e7afe2SHans Petter Selasky /* 323395ed5015SHans Petter Selasky * Query existing TX queue level based on the existing 323495ed5015SHans Petter Selasky * "inp->inp_snd_tag", if any. 323595ed5015SHans Petter Selasky */ 323695ed5015SHans Petter Selasky int 323795ed5015SHans Petter Selasky in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 323895ed5015SHans Petter Selasky { 323995ed5015SHans Petter Selasky union if_snd_tag_query_params params = { }; 324095ed5015SHans Petter Selasky struct m_snd_tag *mst; 324195ed5015SHans Petter Selasky struct ifnet *ifp; 324295ed5015SHans Petter Selasky int error; 324395ed5015SHans Petter Selasky 324495ed5015SHans Petter Selasky mst = inp->inp_snd_tag; 324595ed5015SHans Petter Selasky if (mst == NULL) 324695ed5015SHans Petter Selasky return (EINVAL); 324795ed5015SHans Petter Selasky 324895ed5015SHans Petter Selasky ifp = mst->ifp; 324995ed5015SHans Petter Selasky if (ifp == NULL) 325095ed5015SHans Petter Selasky return (EINVAL); 325195ed5015SHans Petter Selasky 325295ed5015SHans Petter Selasky if (ifp->if_snd_tag_query == NULL) 325395ed5015SHans Petter Selasky return (EOPNOTSUPP); 325495ed5015SHans Petter Selasky 325595ed5015SHans Petter Selasky error = ifp->if_snd_tag_query(mst, ¶ms); 325695ed5015SHans Petter Selasky if (error == 0 && p_txqueue_level != NULL) 325795ed5015SHans Petter Selasky *p_txqueue_level = params.rate_limit.queue_level; 325895ed5015SHans Petter Selasky return (error); 325995ed5015SHans Petter Selasky } 326095ed5015SHans Petter Selasky 326195ed5015SHans Petter Selasky /* 3262f3e7afe2SHans Petter Selasky * Allocate a new TX rate limit send tag from the network interface 3263f3e7afe2SHans Petter Selasky * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3264f3e7afe2SHans Petter Selasky */ 3265f3e7afe2SHans Petter Selasky int 3266f3e7afe2SHans Petter Selasky in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 3267f3e7afe2SHans Petter Selasky uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate) 3268f3e7afe2SHans Petter Selasky { 3269f3e7afe2SHans Petter Selasky union if_snd_tag_alloc_params params = { 327095ed5015SHans Petter Selasky .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 327195ed5015SHans Petter Selasky IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3272f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowid = flowid, 3273f3e7afe2SHans Petter Selasky .rate_limit.hdr.flowtype = flowtype, 3274f3e7afe2SHans Petter Selasky .rate_limit.max_rate = max_pacing_rate, 3275f3e7afe2SHans Petter Selasky }; 3276f3e7afe2SHans Petter Selasky int error; 3277f3e7afe2SHans Petter Selasky 3278f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3279f3e7afe2SHans Petter Selasky 3280f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 3281f3e7afe2SHans Petter Selasky return (EINVAL); 3282f3e7afe2SHans Petter Selasky 3283f3e7afe2SHans Petter Selasky if (ifp->if_snd_tag_alloc == NULL) { 3284f3e7afe2SHans Petter Selasky error = EOPNOTSUPP; 3285f3e7afe2SHans Petter Selasky } else { 3286f3e7afe2SHans Petter Selasky error = ifp->if_snd_tag_alloc(ifp, ¶ms, &inp->inp_snd_tag); 3287f3e7afe2SHans Petter Selasky 3288f3e7afe2SHans Petter Selasky /* 3289f3e7afe2SHans Petter Selasky * At success increment the refcount on 3290f3e7afe2SHans Petter Selasky * the send tag's network interface: 3291f3e7afe2SHans Petter Selasky */ 3292f3e7afe2SHans Petter Selasky if (error == 0) 3293f3e7afe2SHans Petter Selasky if_ref(inp->inp_snd_tag->ifp); 3294f3e7afe2SHans Petter Selasky } 3295f3e7afe2SHans Petter Selasky return (error); 3296f3e7afe2SHans Petter Selasky } 3297f3e7afe2SHans Petter Selasky 3298f3e7afe2SHans Petter Selasky /* 3299f3e7afe2SHans Petter Selasky * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3300f3e7afe2SHans Petter Selasky * if any: 3301f3e7afe2SHans Petter Selasky */ 3302f3e7afe2SHans Petter Selasky void 3303f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(struct inpcb *inp) 3304f3e7afe2SHans Petter Selasky { 3305f3e7afe2SHans Petter Selasky struct m_snd_tag *mst; 3306f3e7afe2SHans Petter Selasky struct ifnet *ifp; 3307f3e7afe2SHans Petter Selasky 3308f3e7afe2SHans Petter Selasky INP_WLOCK_ASSERT(inp); 3309f3e7afe2SHans Petter Selasky 3310f3e7afe2SHans Petter Selasky mst = inp->inp_snd_tag; 3311f3e7afe2SHans Petter Selasky inp->inp_snd_tag = NULL; 3312f3e7afe2SHans Petter Selasky 3313f3e7afe2SHans Petter Selasky if (mst == NULL) 3314f3e7afe2SHans Petter Selasky return; 3315f3e7afe2SHans Petter Selasky 3316f3e7afe2SHans Petter Selasky ifp = mst->ifp; 3317f3e7afe2SHans Petter Selasky if (ifp == NULL) 3318f3e7afe2SHans Petter Selasky return; 3319f3e7afe2SHans Petter Selasky 3320f3e7afe2SHans Petter Selasky /* 3321f3e7afe2SHans Petter Selasky * If the device was detached while we still had reference(s) 3322f3e7afe2SHans Petter Selasky * on the ifp, we assume if_snd_tag_free() was replaced with 3323f3e7afe2SHans Petter Selasky * stubs. 3324f3e7afe2SHans Petter Selasky */ 3325f3e7afe2SHans Petter Selasky ifp->if_snd_tag_free(mst); 3326f3e7afe2SHans Petter Selasky 3327f3e7afe2SHans Petter Selasky /* release reference count on network interface */ 3328f3e7afe2SHans Petter Selasky if_rele(ifp); 3329f3e7afe2SHans Petter Selasky } 3330f3e7afe2SHans Petter Selasky 3331f3e7afe2SHans Petter Selasky /* 3332f3e7afe2SHans Petter Selasky * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3333f3e7afe2SHans Petter Selasky * is set in the fast path and will attach/detach/modify the TX rate 3334f3e7afe2SHans Petter Selasky * limit send tag based on the socket's so_max_pacing_rate value. 3335f3e7afe2SHans Petter Selasky */ 3336f3e7afe2SHans Petter Selasky void 3337f3e7afe2SHans Petter Selasky in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3338f3e7afe2SHans Petter Selasky { 3339f3e7afe2SHans Petter Selasky struct socket *socket; 3340f3e7afe2SHans Petter Selasky uint32_t max_pacing_rate; 3341f3e7afe2SHans Petter Selasky bool did_upgrade; 3342f3e7afe2SHans Petter Selasky int error; 3343f3e7afe2SHans Petter Selasky 3344f3e7afe2SHans Petter Selasky if (inp == NULL) 3345f3e7afe2SHans Petter Selasky return; 3346f3e7afe2SHans Petter Selasky 3347f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3348f3e7afe2SHans Petter Selasky if (socket == NULL) 3349f3e7afe2SHans Petter Selasky return; 3350f3e7afe2SHans Petter Selasky 3351f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3352f3e7afe2SHans Petter Selasky /* 3353f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3354f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3355f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3356f3e7afe2SHans Petter Selasky * write lock. 3357f3e7afe2SHans Petter Selasky */ 3358f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3359f3e7afe2SHans Petter Selasky return; 3360f3e7afe2SHans Petter Selasky did_upgrade = 1; 3361f3e7afe2SHans Petter Selasky } else { 3362f3e7afe2SHans Petter Selasky did_upgrade = 0; 3363f3e7afe2SHans Petter Selasky } 3364f3e7afe2SHans Petter Selasky 3365f3e7afe2SHans Petter Selasky /* 3366f3e7afe2SHans Petter Selasky * NOTE: The so_max_pacing_rate value is read unlocked, 3367f3e7afe2SHans Petter Selasky * because atomic updates are not required since the variable 3368f3e7afe2SHans Petter Selasky * is checked at every mbuf we send. It is assumed that the 3369f3e7afe2SHans Petter Selasky * variable read itself will be atomic. 3370f3e7afe2SHans Petter Selasky */ 3371f3e7afe2SHans Petter Selasky max_pacing_rate = socket->so_max_pacing_rate; 3372f3e7afe2SHans Petter Selasky 3373f3e7afe2SHans Petter Selasky /* 3374f3e7afe2SHans Petter Selasky * NOTE: When attaching to a network interface a reference is 3375f3e7afe2SHans Petter Selasky * made to ensure the network interface doesn't go away until 3376f3e7afe2SHans Petter Selasky * all ratelimit connections are gone. The network interface 3377f3e7afe2SHans Petter Selasky * pointers compared below represent valid network interfaces, 3378f3e7afe2SHans Petter Selasky * except when comparing towards NULL. 3379f3e7afe2SHans Petter Selasky */ 3380f3e7afe2SHans Petter Selasky if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 3381f3e7afe2SHans Petter Selasky error = 0; 3382f3e7afe2SHans Petter Selasky } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 3383f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag != NULL) 3384f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3385f3e7afe2SHans Petter Selasky error = 0; 3386f3e7afe2SHans Petter Selasky } else if (inp->inp_snd_tag == NULL) { 3387f3e7afe2SHans Petter Selasky /* 3388f3e7afe2SHans Petter Selasky * In order to utilize packet pacing with RSS, we need 3389f3e7afe2SHans Petter Selasky * to wait until there is a valid RSS hash before we 3390f3e7afe2SHans Petter Selasky * can proceed: 3391f3e7afe2SHans Petter Selasky */ 3392f3e7afe2SHans Petter Selasky if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 3393f3e7afe2SHans Petter Selasky error = EAGAIN; 3394f3e7afe2SHans Petter Selasky } else { 3395f3e7afe2SHans Petter Selasky error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 3396f3e7afe2SHans Petter Selasky mb->m_pkthdr.flowid, max_pacing_rate); 3397f3e7afe2SHans Petter Selasky } 3398f3e7afe2SHans Petter Selasky } else { 3399f3e7afe2SHans Petter Selasky error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 3400f3e7afe2SHans Petter Selasky } 3401f3e7afe2SHans Petter Selasky if (error == 0 || error == EOPNOTSUPP) 3402f3e7afe2SHans Petter Selasky inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 3403f3e7afe2SHans Petter Selasky if (did_upgrade) 3404f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3405f3e7afe2SHans Petter Selasky } 3406f3e7afe2SHans Petter Selasky 3407f3e7afe2SHans Petter Selasky /* 3408f3e7afe2SHans Petter Selasky * Track route changes for TX rate limiting. 3409f3e7afe2SHans Petter Selasky */ 3410f3e7afe2SHans Petter Selasky void 3411f3e7afe2SHans Petter Selasky in_pcboutput_eagain(struct inpcb *inp) 3412f3e7afe2SHans Petter Selasky { 3413f3e7afe2SHans Petter Selasky struct socket *socket; 3414f3e7afe2SHans Petter Selasky bool did_upgrade; 3415f3e7afe2SHans Petter Selasky 3416f3e7afe2SHans Petter Selasky if (inp == NULL) 3417f3e7afe2SHans Petter Selasky return; 3418f3e7afe2SHans Petter Selasky 3419f3e7afe2SHans Petter Selasky socket = inp->inp_socket; 3420f3e7afe2SHans Petter Selasky if (socket == NULL) 3421f3e7afe2SHans Petter Selasky return; 3422f3e7afe2SHans Petter Selasky 3423f3e7afe2SHans Petter Selasky if (inp->inp_snd_tag == NULL) 3424f3e7afe2SHans Petter Selasky return; 3425f3e7afe2SHans Petter Selasky 3426f3e7afe2SHans Petter Selasky if (!INP_WLOCKED(inp)) { 3427f3e7afe2SHans Petter Selasky /* 3428f3e7afe2SHans Petter Selasky * NOTE: If the write locking fails, we need to bail 3429f3e7afe2SHans Petter Selasky * out and use the non-ratelimited ring for the 3430f3e7afe2SHans Petter Selasky * transmit until there is a new chance to get the 3431f3e7afe2SHans Petter Selasky * write lock. 3432f3e7afe2SHans Petter Selasky */ 3433f3e7afe2SHans Petter Selasky if (!INP_TRY_UPGRADE(inp)) 3434f3e7afe2SHans Petter Selasky return; 3435f3e7afe2SHans Petter Selasky did_upgrade = 1; 3436f3e7afe2SHans Petter Selasky } else { 3437f3e7afe2SHans Petter Selasky did_upgrade = 0; 3438f3e7afe2SHans Petter Selasky } 3439f3e7afe2SHans Petter Selasky 3440f3e7afe2SHans Petter Selasky /* detach rate limiting */ 3441f3e7afe2SHans Petter Selasky in_pcbdetach_txrtlmt(inp); 3442f3e7afe2SHans Petter Selasky 3443f3e7afe2SHans Petter Selasky /* make sure new mbuf send tag allocation is made */ 3444f3e7afe2SHans Petter Selasky inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3445f3e7afe2SHans Petter Selasky 3446f3e7afe2SHans Petter Selasky if (did_upgrade) 3447f3e7afe2SHans Petter Selasky INP_DOWNGRADE(inp); 3448f3e7afe2SHans Petter Selasky } 3449f3e7afe2SHans Petter Selasky #endif /* RATELIMIT */ 3450