1c398230bSWarner Losh /*- 22469dd60SGarrett Wollman * Copyright (c) 1982, 1986, 1991, 1993, 1995 3497057eeSRobert Watson * The Regents of the University of California. 4111d57a6SRobert Watson * Copyright (c) 2007-2009 Robert N. M. Watson 579bdc6e5SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 6497057eeSRobert Watson * All rights reserved. 7df8bae1dSRodney W. Grimes * 879bdc6e5SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 979bdc6e5SRobert Watson * contract to Juniper Networks, Inc. 1079bdc6e5SRobert Watson * 11df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 12df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 13df8bae1dSRodney W. Grimes * are met: 14df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 16df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 18df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 19df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 20df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 21df8bae1dSRodney W. Grimes * without specific prior written permission. 22df8bae1dSRodney W. Grimes * 23df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33df8bae1dSRodney W. Grimes * SUCH DAMAGE. 34df8bae1dSRodney W. Grimes * 352469dd60SGarrett Wollman * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 36df8bae1dSRodney W. Grimes */ 37df8bae1dSRodney W. Grimes 384b421e2dSMike Silbersack #include <sys/cdefs.h> 394b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 404b421e2dSMike Silbersack 41497057eeSRobert Watson #include "opt_ddb.h" 426a800098SYoshinobu Inoue #include "opt_ipsec.h" 43efc76f72SBjoern A. Zeeb #include "opt_inet.h" 44cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 4552cd27cbSRobert Watson #include "opt_pcbgroup.h" 467527624eSRobert Watson #include "opt_rss.h" 47cfa1ca9dSYoshinobu Inoue 48df8bae1dSRodney W. Grimes #include <sys/param.h> 49df8bae1dSRodney W. Grimes #include <sys/systm.h> 50cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 51df8bae1dSRodney W. Grimes #include <sys/malloc.h> 52df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 53aae49dd3SBjoern A. Zeeb #include <sys/callout.h> 54cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 55df8bae1dSRodney W. Grimes #include <sys/protosw.h> 56cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 57df8bae1dSRodney W. Grimes #include <sys/socket.h> 58df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 59acd3428bSRobert Watson #include <sys/priv.h> 60df8bae1dSRodney W. Grimes #include <sys/proc.h> 6179bdc6e5SRobert Watson #include <sys/refcount.h> 6275c13541SPoul-Henning Kamp #include <sys/jail.h> 63101f9fc8SPeter Wemm #include <sys/kernel.h> 64101f9fc8SPeter Wemm #include <sys/sysctl.h> 658781d8e9SBruce Evans 66497057eeSRobert Watson #ifdef DDB 67497057eeSRobert Watson #include <ddb/ddb.h> 68497057eeSRobert Watson #endif 69497057eeSRobert Watson 7069c2d429SJeff Roberson #include <vm/uma.h> 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes #include <net/if.h> 7376039bc8SGleb Smirnoff #include <net/if_var.h> 74cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 75df8bae1dSRodney W. Grimes #include <net/route.h> 76b2bdc62aSAdrian Chadd #include <net/rss_config.h> 77530c0060SRobert Watson #include <net/vnet.h> 78df8bae1dSRodney W. Grimes 7967107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 80df8bae1dSRodney W. Grimes #include <netinet/in.h> 81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 82df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 83340c35deSJonathan Lemon #include <netinet/tcp_var.h> 845f311da2SMike Silbersack #include <netinet/udp.h> 855f311da2SMike Silbersack #include <netinet/udp_var.h> 8667107f45SBjoern A. Zeeb #endif 8767107f45SBjoern A. Zeeb #ifdef INET 8867107f45SBjoern A. Zeeb #include <netinet/in_var.h> 8967107f45SBjoern A. Zeeb #endif 90cfa1ca9dSYoshinobu Inoue #ifdef INET6 91cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 92efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 9367107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 9467107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 95cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 96cfa1ca9dSYoshinobu Inoue 97df8bae1dSRodney W. Grimes 98b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 99b9234fafSSam Leffler #include <netipsec/ipsec.h> 100b9234fafSSam Leffler #include <netipsec/key.h> 101b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 102b9234fafSSam Leffler 103aed55708SRobert Watson #include <security/mac/mac_framework.h> 104aed55708SRobert Watson 105aae49dd3SBjoern A. Zeeb static struct callout ipport_tick_callout; 106aae49dd3SBjoern A. Zeeb 107101f9fc8SPeter Wemm /* 108101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 109101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 110101f9fc8SPeter Wemm */ 111eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 112eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 115eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 116eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 117101f9fc8SPeter Wemm 118b0d22693SCrist J. Clark /* 119b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 120b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 121b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 122b0d22693SCrist J. Clark */ 123eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 124eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 125b0d22693SCrist J. Clark 1265f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */ 128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */ 129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */ 130eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */ 131eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs); 1323e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount); 133eddfbb76SRobert Watson 1341e77c105SRobert Watson #define V_ipport_tcplastcount VNET(ipport_tcplastcount) 1356ac48b74SMike Silbersack 136d2025bd0SBjoern A. Zeeb static void in_pcbremlists(struct inpcb *inp); 137d2025bd0SBjoern A. Zeeb #ifdef INET 138fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 139fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 140fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 141fa046d87SRobert Watson int lookupflags, struct ifnet *ifp); 14267107f45SBjoern A. Zeeb 143bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 144bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 145bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 146bbd42ad0SPeter Wemm 147bbd42ad0SPeter Wemm static int 14882d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 149bbd42ad0SPeter Wemm { 15030a4ab08SBruce Evans int error; 15130a4ab08SBruce Evans 152f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 15330a4ab08SBruce Evans if (error == 0) { 154603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 155603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 156603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 157603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 158603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 159603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 160bbd42ad0SPeter Wemm } 16130a4ab08SBruce Evans return (error); 162bbd42ad0SPeter Wemm } 163bbd42ad0SPeter Wemm 164bbd42ad0SPeter Wemm #undef RANGECHK 165bbd42ad0SPeter Wemm 1666472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, 1676472ac3dSEd Schouten "IP Ports"); 16833b3ac06SPeter Wemm 1696df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 1706df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1716df8a710SGleb Smirnoff &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", ""); 1726df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 1736df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1746df8a710SGleb Smirnoff &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", ""); 1756df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 1766df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1776df8a710SGleb Smirnoff &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", ""); 1786df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 1796df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1806df8a710SGleb Smirnoff &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", ""); 1816df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 1826df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1836df8a710SGleb Smirnoff &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", ""); 1846df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 1856df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 1866df8a710SGleb Smirnoff &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", ""); 1876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 1886df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 1896df8a710SGleb Smirnoff &VNET_NAME(ipport_reservedhigh), 0, ""); 1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 191eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 1936df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 194eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 1956df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, 1966df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 197eddfbb76SRobert Watson &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port " 1986ee79c59SMaxim Konovalov "allocations before switching to a sequental one"); 1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, 2006df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 201eddfbb76SRobert Watson &VNET_NAME(ipport_randomtime), 0, 2028b615593SMarko Zec "Minimum time to keep sequental port " 2036ee79c59SMaxim Konovalov "allocation before switching to a random one"); 20483e521ecSBjoern A. Zeeb #endif /* INET */ 2050312fbe9SPoul-Henning Kamp 206c3229e05SDavid Greenman /* 207c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 208c3229e05SDavid Greenman * 209de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 210de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 211de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 212c3229e05SDavid Greenman */ 213c3229e05SDavid Greenman 214c3229e05SDavid Greenman /* 2159bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 2169bcd427bSRobert Watson * arguments in time. 2179bcd427bSRobert Watson */ 2189bcd427bSRobert Watson void 2199bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 2209bcd427bSRobert Watson struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 2219bcd427bSRobert Watson char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini, 22252cd27cbSRobert Watson uint32_t inpcbzone_flags, u_int hashfields) 2239bcd427bSRobert Watson { 2249bcd427bSRobert Watson 2259bcd427bSRobert Watson INP_INFO_LOCK_INIT(pcbinfo, name); 226fa046d87SRobert Watson INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 227ff9b006dSJulien Charbon INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist"); 2289bcd427bSRobert Watson #ifdef VIMAGE 2299bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 2309bcd427bSRobert Watson #endif 2319bcd427bSRobert Watson pcbinfo->ipi_listhead = listhead; 2329bcd427bSRobert Watson LIST_INIT(pcbinfo->ipi_listhead); 233fa046d87SRobert Watson pcbinfo->ipi_count = 0; 2349bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 2359bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 2369bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 2379bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 23852cd27cbSRobert Watson #ifdef PCBGROUP 23952cd27cbSRobert Watson in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 24052cd27cbSRobert Watson #endif 2419bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 2429bcd427bSRobert Watson NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR, 2439bcd427bSRobert Watson inpcbzone_flags); 2449bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 2456acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 2466acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 2479bcd427bSRobert Watson } 2489bcd427bSRobert Watson 2499bcd427bSRobert Watson /* 2509bcd427bSRobert Watson * Destroy an inpcbinfo. 2519bcd427bSRobert Watson */ 2529bcd427bSRobert Watson void 2539bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 2549bcd427bSRobert Watson { 2559bcd427bSRobert Watson 256fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 257fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 258fa046d87SRobert Watson 2599bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 2609bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 2619bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 26252cd27cbSRobert Watson #ifdef PCBGROUP 26352cd27cbSRobert Watson in_pcbgroup_destroy(pcbinfo); 26452cd27cbSRobert Watson #endif 2659bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 266ff9b006dSJulien Charbon INP_LIST_LOCK_DESTROY(pcbinfo); 267fa046d87SRobert Watson INP_HASH_LOCK_DESTROY(pcbinfo); 2689bcd427bSRobert Watson INP_INFO_LOCK_DESTROY(pcbinfo); 2699bcd427bSRobert Watson } 2709bcd427bSRobert Watson 2719bcd427bSRobert Watson /* 272c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 273d915b280SStephan Uphoff * On success return with the PCB locked. 274c3229e05SDavid Greenman */ 275df8bae1dSRodney W. Grimes int 276d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 277df8bae1dSRodney W. Grimes { 278136d4f1cSRobert Watson struct inpcb *inp; 27913cf67f3SHajimu UMEMOTO int error; 280a557af22SRobert Watson 281ff9b006dSJulien Charbon #ifdef INVARIANTS 282ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 283ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 284ff9b006dSJulien Charbon } else { 28559daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 286ff9b006dSJulien Charbon } 287ff9b006dSJulien Charbon #endif 288ff9b006dSJulien Charbon 289a557af22SRobert Watson error = 0; 290d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 291df8bae1dSRodney W. Grimes if (inp == NULL) 292df8bae1dSRodney W. Grimes return (ENOBUFS); 293d915b280SStephan Uphoff bzero(inp, inp_zero_size); 29415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 295df8bae1dSRodney W. Grimes inp->inp_socket = so; 29686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 2978b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 298a557af22SRobert Watson #ifdef MAC 29930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 300a557af22SRobert Watson if (error != 0) 301a557af22SRobert Watson goto out; 30230d239bcSRobert Watson mac_inpcb_create(so, inp); 303a557af22SRobert Watson #endif 304b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 30513cf67f3SHajimu UMEMOTO error = ipsec_init_policy(so, &inp->inp_sp); 3060bffde27SRobert Watson if (error != 0) { 3070bffde27SRobert Watson #ifdef MAC 3080bffde27SRobert Watson mac_inpcb_destroy(inp); 3090bffde27SRobert Watson #endif 310a557af22SRobert Watson goto out; 3110bffde27SRobert Watson } 312b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 313e3fd5ffdSRobert Watson #ifdef INET6 314340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 315340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 316603724d3SBjoern A. Zeeb if (V_ip6_v6only) 31733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 318340c35deSJonathan Lemon } 31975daea93SPaul Saab #endif 320ff9b006dSJulien Charbon INP_WLOCK(inp); 321ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 322712fc218SRobert Watson LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 3233d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 324df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 32533841545SHajimu UMEMOTO #ifdef INET6 326603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 32733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 32833841545SHajimu UMEMOTO #endif 329d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 33079bdc6e5SRobert Watson refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 331ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 332b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC) 333a557af22SRobert Watson out: 33486d02c5cSBjoern A. Zeeb if (error != 0) { 33586d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 336a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 33786d02c5cSBjoern A. Zeeb } 338a557af22SRobert Watson #endif 339a557af22SRobert Watson return (error); 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes 34267107f45SBjoern A. Zeeb #ifdef INET 343df8bae1dSRodney W. Grimes int 344136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 345df8bae1dSRodney W. Grimes { 3464b932371SIan Dowse int anonport, error; 3474b932371SIan Dowse 3488501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 349fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 35059daba27SSam Leffler 3514b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 3524b932371SIan Dowse return (EINVAL); 3535cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 3544b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 355b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 3564b932371SIan Dowse if (error) 3574b932371SIan Dowse return (error); 3584b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 3594b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 3604b932371SIan Dowse inp->inp_lport = 0; 3614b932371SIan Dowse return (EAGAIN); 3624b932371SIan Dowse } 3634b932371SIan Dowse if (anonport) 3644b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 3654b932371SIan Dowse return (0); 3664b932371SIan Dowse } 36767107f45SBjoern A. Zeeb #endif 3684b932371SIan Dowse 36939c8c62eSHiren Panchasara /* 37039c8c62eSHiren Panchasara * Select a local port (number) to use. 37139c8c62eSHiren Panchasara */ 372efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 373efc76f72SBjoern A. Zeeb int 374efc76f72SBjoern A. Zeeb in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 37568e0d7e0SRobert Watson struct ucred *cred, int lookupflags) 376efc76f72SBjoern A. Zeeb { 377efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 378efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 379efc76f72SBjoern A. Zeeb unsigned short *lastport; 380efc76f72SBjoern A. Zeeb int count, dorandom, error; 381efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 382efc76f72SBjoern A. Zeeb #ifdef INET 383efc76f72SBjoern A. Zeeb struct in_addr laddr; 384efc76f72SBjoern A. Zeeb #endif 385efc76f72SBjoern A. Zeeb 386efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 387efc76f72SBjoern A. Zeeb 388efc76f72SBjoern A. Zeeb /* 389efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 390efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 391efc76f72SBjoern A. Zeeb */ 392efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 393fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 394efc76f72SBjoern A. Zeeb 395efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 396efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 397efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 398efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 399efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 400efc76f72SBjoern A. Zeeb error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0); 401efc76f72SBjoern A. Zeeb if (error) 402efc76f72SBjoern A. Zeeb return (error); 403efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 404efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 405efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 406efc76f72SBjoern A. Zeeb } else { 407efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 408efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 409efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 410efc76f72SBjoern A. Zeeb } 411efc76f72SBjoern A. Zeeb /* 412e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 413efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 414efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 415efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 416efc76f72SBjoern A. Zeeb */ 417efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 418e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 419e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 420efc76f72SBjoern A. Zeeb dorandom = 1; 421efc76f72SBjoern A. Zeeb else 422efc76f72SBjoern A. Zeeb dorandom = 0; 423efc76f72SBjoern A. Zeeb /* 424efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 425efc76f72SBjoern A. Zeeb * we have the only port available. 426efc76f72SBjoern A. Zeeb */ 427efc76f72SBjoern A. Zeeb if (first == last) 428efc76f72SBjoern A. Zeeb dorandom = 0; 429e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 430e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 431efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 432efc76f72SBjoern A. Zeeb /* 433efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 434efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 435efc76f72SBjoern A. Zeeb * code path implementing all logic. 436efc76f72SBjoern A. Zeeb */ 437efc76f72SBjoern A. Zeeb if (first > last) { 438efc76f72SBjoern A. Zeeb aux = first; 439efc76f72SBjoern A. Zeeb first = last; 440efc76f72SBjoern A. Zeeb last = aux; 441efc76f72SBjoern A. Zeeb } 442efc76f72SBjoern A. Zeeb 443efc76f72SBjoern A. Zeeb #ifdef INET 444efc76f72SBjoern A. Zeeb /* Make the compiler happy. */ 445efc76f72SBjoern A. Zeeb laddr.s_addr = 0; 4464d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 447efc76f72SBjoern A. Zeeb KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 448efc76f72SBjoern A. Zeeb __func__, inp)); 449efc76f72SBjoern A. Zeeb laddr = *laddrp; 450efc76f72SBjoern A. Zeeb } 451efc76f72SBjoern A. Zeeb #endif 45267107f45SBjoern A. Zeeb tmpinp = NULL; /* Make compiler happy. */ 453efc76f72SBjoern A. Zeeb lport = *lportp; 454efc76f72SBjoern A. Zeeb 455efc76f72SBjoern A. Zeeb if (dorandom) 456efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 457efc76f72SBjoern A. Zeeb 458efc76f72SBjoern A. Zeeb count = last - first; 459efc76f72SBjoern A. Zeeb 460efc76f72SBjoern A. Zeeb do { 461efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 462efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 463efc76f72SBjoern A. Zeeb ++*lastport; 464efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 465efc76f72SBjoern A. Zeeb *lastport = first; 466efc76f72SBjoern A. Zeeb lport = htons(*lastport); 467efc76f72SBjoern A. Zeeb 468efc76f72SBjoern A. Zeeb #ifdef INET6 469efc76f72SBjoern A. Zeeb if ((inp->inp_vflag & INP_IPV6) != 0) 470efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 47168e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 472efc76f72SBjoern A. Zeeb #endif 473efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 474efc76f72SBjoern A. Zeeb else 475efc76f72SBjoern A. Zeeb #endif 476efc76f72SBjoern A. Zeeb #ifdef INET 477efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 47868e0d7e0SRobert Watson lport, lookupflags, cred); 479efc76f72SBjoern A. Zeeb #endif 480efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 481efc76f72SBjoern A. Zeeb 482efc76f72SBjoern A. Zeeb #ifdef INET 4834d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 484efc76f72SBjoern A. Zeeb laddrp->s_addr = laddr.s_addr; 485efc76f72SBjoern A. Zeeb #endif 486efc76f72SBjoern A. Zeeb *lportp = lport; 487efc76f72SBjoern A. Zeeb 488efc76f72SBjoern A. Zeeb return (0); 489efc76f72SBjoern A. Zeeb } 490efdf104bSMikolaj Golub 491efdf104bSMikolaj Golub /* 492efdf104bSMikolaj Golub * Return cached socket options. 493efdf104bSMikolaj Golub */ 494efdf104bSMikolaj Golub short 495efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 496efdf104bSMikolaj Golub { 497efdf104bSMikolaj Golub short so_options; 498efdf104bSMikolaj Golub 499efdf104bSMikolaj Golub so_options = 0; 500efdf104bSMikolaj Golub 501efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 502efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 503efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 504efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 505efdf104bSMikolaj Golub return (so_options); 506efdf104bSMikolaj Golub } 507efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 508efc76f72SBjoern A. Zeeb 5094b932371SIan Dowse /* 5100a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 5110a100a6fSAdrian Chadd * 5120a100a6fSAdrian Chadd * ni points to the new inp. 5130a100a6fSAdrian Chadd * oi points to the exisitng inp. 5140a100a6fSAdrian Chadd * 5150a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 5160a100a6fSAdrian Chadd * whether the credentials match. 5170a100a6fSAdrian Chadd */ 518d5bb8bd3SAdrian Chadd int 5190a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 5200a100a6fSAdrian Chadd { 5210a100a6fSAdrian Chadd /* Check permissions match */ 5220a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5230a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 5240a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 5250a100a6fSAdrian Chadd return (0); 5260a100a6fSAdrian Chadd 5270a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 5280a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5290a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 5300a100a6fSAdrian Chadd return (0); 5310a100a6fSAdrian Chadd 5320a100a6fSAdrian Chadd /* 5330a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 5340a100a6fSAdrian Chadd * it is and it matches the checks. 5350a100a6fSAdrian Chadd */ 5360a100a6fSAdrian Chadd return (1); 5370a100a6fSAdrian Chadd } 5380a100a6fSAdrian Chadd 539d5bb8bd3SAdrian Chadd #ifdef INET 5400a100a6fSAdrian Chadd /* 5414b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 5424b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 5434b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 5444b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 5454b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 5464b932371SIan Dowse * 5474b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 5484b932371SIan Dowse */ 5494b932371SIan Dowse int 550136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 551136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 5524b932371SIan Dowse { 5534b932371SIan Dowse struct socket *so = inp->inp_socket; 55415bd2b43SDavid Greenman struct sockaddr_in *sin; 555c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 5564b932371SIan Dowse struct in_addr laddr; 557df8bae1dSRodney W. Grimes u_short lport = 0; 55868e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 559413628a7SBjoern A. Zeeb int error; 560df8bae1dSRodney W. Grimes 5618501a69cSRobert Watson /* 562fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 5638501a69cSRobert Watson */ 56459daba27SSam Leffler INP_LOCK_ASSERT(inp); 565fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 56659daba27SSam Leffler 567603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 568df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 5694b932371SIan Dowse laddr.s_addr = *laddrp; 5704b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 571df8bae1dSRodney W. Grimes return (EINVAL); 572c3229e05SDavid Greenman if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 57368e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 5747c2f3cb9SJamie Gritton if (nam == NULL) { 5757c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 5767c2f3cb9SJamie Gritton return (error); 5777c2f3cb9SJamie Gritton } else { 57857bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 57957bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 580df8bae1dSRodney W. Grimes return (EINVAL); 581df8bae1dSRodney W. Grimes #ifdef notdef 582df8bae1dSRodney W. Grimes /* 583df8bae1dSRodney W. Grimes * We should check the family, but old programs 584df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 585df8bae1dSRodney W. Grimes */ 586df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 587df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 588df8bae1dSRodney W. Grimes #endif 589b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 590b89e82ddSJamie Gritton if (error) 591b89e82ddSJamie Gritton return (error); 5924b932371SIan Dowse if (sin->sin_port != *lportp) { 5934b932371SIan Dowse /* Don't allow the port to change. */ 5944b932371SIan Dowse if (*lportp != 0) 5954b932371SIan Dowse return (EINVAL); 596df8bae1dSRodney W. Grimes lport = sin->sin_port; 5974b932371SIan Dowse } 5984b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 599df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 600df8bae1dSRodney W. Grimes /* 601df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 602df8bae1dSRodney W. Grimes * allow complete duplication of binding if 603df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 604df8bae1dSRodney W. Grimes * and a multicast address is bound on both 605df8bae1dSRodney W. Grimes * new and duplicated sockets. 606df8bae1dSRodney W. Grimes */ 607f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 608df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 609df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 610df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 61183103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 6124209e01aSAdrian Chadd /* 6134209e01aSAdrian Chadd * Is the address a local IP address? 614f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 6158696873dSAdrian Chadd * to any endpoint address, local or not. 6164209e01aSAdrian Chadd */ 617f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 6188896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 619df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 620df8bae1dSRodney W. Grimes } 6214b932371SIan Dowse laddr = sin->sin_addr; 622df8bae1dSRodney W. Grimes if (lport) { 623df8bae1dSRodney W. Grimes struct inpcb *t; 624ae0e7143SRobert Watson struct tcptw *tw; 625ae0e7143SRobert Watson 626df8bae1dSRodney W. Grimes /* GROSS */ 627603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 628603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 629acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 63032f9753cSRobert Watson 0)) 6312469dd60SGarrett Wollman return (EACCES); 632835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 63386d02c5cSBjoern A. Zeeb priv_check_cred(inp->inp_cred, 63432f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 635078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 636413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 637340c35deSJonathan Lemon /* 638340c35deSJonathan Lemon * XXX 639340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 640340c35deSJonathan Lemon */ 6414cc20ab1SSeigo Tanimura if (t && 6420a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 643ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 6444658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 6454658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 6464cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 64752b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 648fc06cd42SMikolaj Golub (t->inp_flags2 & INP_REUSEPORT) == 0) && 64986d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 65086d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 6514049a042SGuido van Rooij return (EADDRINUSE); 6520a100a6fSAdrian Chadd 6530a100a6fSAdrian Chadd /* 6540a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 6550a100a6fSAdrian Chadd * the credentials need to match and the 6560a100a6fSAdrian Chadd * original socket also has to have been bound 6570a100a6fSAdrian Chadd * with BINDMULTI. 6580a100a6fSAdrian Chadd */ 6590a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 6600a100a6fSAdrian Chadd return (EADDRINUSE); 6614049a042SGuido van Rooij } 662c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 66368e0d7e0SRobert Watson lport, lookupflags, cred); 664ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 665ae0e7143SRobert Watson /* 666ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 667ae0e7143SRobert Watson * state recycled, we treat the address as 668ae0e7143SRobert Watson * being in use (for now). This is better 669ae0e7143SRobert Watson * than a panic, but not desirable. 670ae0e7143SRobert Watson */ 671ec95b709SMikolaj Golub tw = intotw(t); 672ae0e7143SRobert Watson if (tw == NULL || 673ae0e7143SRobert Watson (reuseport & tw->tw_so_options) == 0) 674340c35deSJonathan Lemon return (EADDRINUSE); 6750a100a6fSAdrian Chadd } else if (t && 6760a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 6770a100a6fSAdrian Chadd (reuseport & inp_so_options(t)) == 0) { 678e3fd5ffdSRobert Watson #ifdef INET6 67933841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 680cfa1ca9dSYoshinobu Inoue INADDR_ANY || 681cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 682cfa1ca9dSYoshinobu Inoue INADDR_ANY || 683fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 684fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 685e3fd5ffdSRobert Watson #endif 686df8bae1dSRodney W. Grimes return (EADDRINUSE); 6870a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 6880a100a6fSAdrian Chadd return (EADDRINUSE); 689df8bae1dSRodney W. Grimes } 690cfa1ca9dSYoshinobu Inoue } 691df8bae1dSRodney W. Grimes } 6924b932371SIan Dowse if (*lportp != 0) 6934b932371SIan Dowse lport = *lportp; 69433b3ac06SPeter Wemm if (lport == 0) { 69568e0d7e0SRobert Watson error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 696efc76f72SBjoern A. Zeeb if (error != 0) 697efc76f72SBjoern A. Zeeb return (error); 69833b3ac06SPeter Wemm 69933b3ac06SPeter Wemm } 7004b932371SIan Dowse *laddrp = laddr.s_addr; 7014b932371SIan Dowse *lportp = lport; 702df8bae1dSRodney W. Grimes return (0); 703df8bae1dSRodney W. Grimes } 704df8bae1dSRodney W. Grimes 705999f1343SGarrett Wollman /* 7065200e00eSIan Dowse * Connect from a socket to a specified address. 7075200e00eSIan Dowse * Both address and port must be specified in argument sin. 7085200e00eSIan Dowse * If don't have a local address for this socket yet, 7095200e00eSIan Dowse * then pick one. 710999f1343SGarrett Wollman */ 711999f1343SGarrett Wollman int 712d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 713d3c1f003SRobert Watson struct ucred *cred, struct mbuf *m) 714999f1343SGarrett Wollman { 7155200e00eSIan Dowse u_short lport, fport; 7165200e00eSIan Dowse in_addr_t laddr, faddr; 7175200e00eSIan Dowse int anonport, error; 718df8bae1dSRodney W. Grimes 7198501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 720fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 72127f74fd0SRobert Watson 7225200e00eSIan Dowse lport = inp->inp_lport; 7235200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 7245200e00eSIan Dowse anonport = (lport == 0); 7255200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 726b0330ed9SPawel Jakub Dawidek NULL, cred); 7275200e00eSIan Dowse if (error) 7285200e00eSIan Dowse return (error); 7295200e00eSIan Dowse 7305200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 7315200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 7325200e00eSIan Dowse inp->inp_lport = lport; 7335200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7345200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 7355200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 7365200e00eSIan Dowse inp->inp_lport = 0; 7375200e00eSIan Dowse return (EAGAIN); 7385200e00eSIan Dowse } 7395200e00eSIan Dowse } 7405200e00eSIan Dowse 7415200e00eSIan Dowse /* Commit the remaining changes. */ 7425200e00eSIan Dowse inp->inp_lport = lport; 7435200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7445200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 7455200e00eSIan Dowse inp->inp_fport = fport; 746d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 7472cb64cb2SGeorge V. Neville-Neil 7485200e00eSIan Dowse if (anonport) 7495200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 7505200e00eSIan Dowse return (0); 7515200e00eSIan Dowse } 7525200e00eSIan Dowse 753d3c1f003SRobert Watson int 754d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 755d3c1f003SRobert Watson { 756d3c1f003SRobert Watson 757d3c1f003SRobert Watson return (in_pcbconnect_mbuf(inp, nam, cred, NULL)); 758d3c1f003SRobert Watson } 759d3c1f003SRobert Watson 7605200e00eSIan Dowse /* 7610895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 7620895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 7630895aec3SBjoern A. Zeeb */ 764ae190832SSteven Hartland int 7650895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 7660895aec3SBjoern A. Zeeb struct ucred *cred) 7670895aec3SBjoern A. Zeeb { 7680895aec3SBjoern A. Zeeb struct ifaddr *ifa; 7690895aec3SBjoern A. Zeeb struct sockaddr *sa; 7700895aec3SBjoern A. Zeeb struct sockaddr_in *sin; 7710895aec3SBjoern A. Zeeb struct route sro; 7720895aec3SBjoern A. Zeeb int error; 7730895aec3SBjoern A. Zeeb 774413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 7750895aec3SBjoern A. Zeeb 776592bcae8SBjoern A. Zeeb /* 777592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 778592bcae8SBjoern A. Zeeb * if requested. 779592bcae8SBjoern A. Zeeb */ 780592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 781592bcae8SBjoern A. Zeeb return (0); 782592bcae8SBjoern A. Zeeb 7830895aec3SBjoern A. Zeeb error = 0; 7840895aec3SBjoern A. Zeeb bzero(&sro, sizeof(sro)); 7850895aec3SBjoern A. Zeeb 7860895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)&sro.ro_dst; 7870895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 7880895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 7890895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 7900895aec3SBjoern A. Zeeb 7910895aec3SBjoern A. Zeeb /* 7920895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 7930895aec3SBjoern A. Zeeb * else punt. 7940895aec3SBjoern A. Zeeb * 7950895aec3SBjoern A. Zeeb * Find out route to destination. 7960895aec3SBjoern A. Zeeb */ 7970895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 7986e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 7990895aec3SBjoern A. Zeeb 8000895aec3SBjoern A. Zeeb /* 8010895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 8020895aec3SBjoern A. Zeeb * the outgoing interface. 8030895aec3SBjoern A. Zeeb * 8040895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 8050895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 8060895aec3SBjoern A. Zeeb * the source address from. 8070895aec3SBjoern A. Zeeb */ 8080895aec3SBjoern A. Zeeb if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 8098c0fec80SRobert Watson struct in_ifaddr *ia; 8100895aec3SBjoern A. Zeeb struct ifnet *ifp; 8110895aec3SBjoern A. Zeeb 8124f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 81358a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 8140895aec3SBjoern A. Zeeb if (ia == NULL) 8154f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 81658a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 8170895aec3SBjoern A. Zeeb if (ia == NULL) { 8180895aec3SBjoern A. Zeeb error = ENETUNREACH; 8190895aec3SBjoern A. Zeeb goto done; 8200895aec3SBjoern A. Zeeb } 8210895aec3SBjoern A. Zeeb 8220304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8230895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8248c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8250895aec3SBjoern A. Zeeb goto done; 8260895aec3SBjoern A. Zeeb } 8270895aec3SBjoern A. Zeeb 8280895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 8298c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8300895aec3SBjoern A. Zeeb ia = NULL; 831137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 8320895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8330895aec3SBjoern A. Zeeb 8340895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 8350895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 8360895aec3SBjoern A. Zeeb continue; 8370895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 838b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8390895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 8400895aec3SBjoern A. Zeeb break; 8410895aec3SBjoern A. Zeeb } 8420895aec3SBjoern A. Zeeb } 8430895aec3SBjoern A. Zeeb if (ia != NULL) { 8440895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 845137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8460895aec3SBjoern A. Zeeb goto done; 8470895aec3SBjoern A. Zeeb } 848137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8490895aec3SBjoern A. Zeeb 8500895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 851b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 8520895aec3SBjoern A. Zeeb goto done; 8530895aec3SBjoern A. Zeeb } 8540895aec3SBjoern A. Zeeb 8550895aec3SBjoern A. Zeeb /* 8560895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 8570895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 8580895aec3SBjoern A. Zeeb * In case of jails do those three steps: 8590895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 8600895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 8610895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 8620895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 8630895aec3SBjoern A. Zeeb */ 8640895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 8658c0fec80SRobert Watson struct in_ifaddr *ia; 8669317b04eSRobert Watson struct ifnet *ifp; 8670895aec3SBjoern A. Zeeb 8680895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 8690304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8700895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8710895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8720895aec3SBjoern A. Zeeb goto done; 8730895aec3SBjoern A. Zeeb } 8740895aec3SBjoern A. Zeeb 8750895aec3SBjoern A. Zeeb /* Jailed. */ 8760895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 8770895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 878b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8790895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8800895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8810895aec3SBjoern A. Zeeb goto done; 8820895aec3SBjoern A. Zeeb } 8830895aec3SBjoern A. Zeeb 8840895aec3SBjoern A. Zeeb /* 8850895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 8860895aec3SBjoern A. Zeeb * belonging to this jail. 8870895aec3SBjoern A. Zeeb */ 8888c0fec80SRobert Watson ia = NULL; 8899317b04eSRobert Watson ifp = sro.ro_rt->rt_ifp; 890137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 8919317b04eSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8920895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 8930895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 8940895aec3SBjoern A. Zeeb continue; 8950895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 896b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8970895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 8980895aec3SBjoern A. Zeeb break; 8990895aec3SBjoern A. Zeeb } 9000895aec3SBjoern A. Zeeb } 9010895aec3SBjoern A. Zeeb if (ia != NULL) { 9020895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 903137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9040895aec3SBjoern A. Zeeb goto done; 9050895aec3SBjoern A. Zeeb } 906137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9070895aec3SBjoern A. Zeeb 9080895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 909b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 9100895aec3SBjoern A. Zeeb goto done; 9110895aec3SBjoern A. Zeeb } 9120895aec3SBjoern A. Zeeb 9130895aec3SBjoern A. Zeeb /* 9140895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 9150895aec3SBjoern A. Zeeb * to ourselves is here. 9160895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 9170895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 9180895aec3SBjoern A. Zeeb * a loopback interface. 9190895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 9200895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 9210895aec3SBjoern A. Zeeb */ 9220895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 9230895aec3SBjoern A. Zeeb struct sockaddr_in sain; 9248c0fec80SRobert Watson struct in_ifaddr *ia; 9250895aec3SBjoern A. Zeeb 9260895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 9270895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 9280895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 9290895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 9300895aec3SBjoern A. Zeeb 93158a39d8cSAlan Somers ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain), 93258a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 9330895aec3SBjoern A. Zeeb if (ia == NULL) 9344f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0, 93558a39d8cSAlan Somers inp->inp_socket->so_fibnum)); 936f0bb05fcSQing Li if (ia == NULL) 937f0bb05fcSQing Li ia = ifatoia(ifa_ifwithaddr(sintosa(&sain))); 9380895aec3SBjoern A. Zeeb 9390304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 9400895aec3SBjoern A. Zeeb if (ia == NULL) { 9410895aec3SBjoern A. Zeeb error = ENETUNREACH; 9420895aec3SBjoern A. Zeeb goto done; 9430895aec3SBjoern A. Zeeb } 9440895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 9458c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9460895aec3SBjoern A. Zeeb goto done; 9470895aec3SBjoern A. Zeeb } 9480895aec3SBjoern A. Zeeb 9490895aec3SBjoern A. Zeeb /* Jailed. */ 9500895aec3SBjoern A. Zeeb if (ia != NULL) { 9510895aec3SBjoern A. Zeeb struct ifnet *ifp; 9520895aec3SBjoern A. Zeeb 9530895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 9548c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9550895aec3SBjoern A. Zeeb ia = NULL; 956137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 9570895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 9580895aec3SBjoern A. Zeeb 9590895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 9600895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 9610895aec3SBjoern A. Zeeb continue; 9620895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 963b89e82ddSJamie Gritton if (prison_check_ip4(cred, 964b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 9650895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 9660895aec3SBjoern A. Zeeb break; 9670895aec3SBjoern A. Zeeb } 9680895aec3SBjoern A. Zeeb } 9690895aec3SBjoern A. Zeeb if (ia != NULL) { 9700895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 971137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9720895aec3SBjoern A. Zeeb goto done; 9730895aec3SBjoern A. Zeeb } 974137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9750895aec3SBjoern A. Zeeb } 9760895aec3SBjoern A. Zeeb 9770895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 978b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 9790895aec3SBjoern A. Zeeb goto done; 9800895aec3SBjoern A. Zeeb } 9810895aec3SBjoern A. Zeeb 9820895aec3SBjoern A. Zeeb done: 9830895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 9840895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 9850895aec3SBjoern A. Zeeb return (error); 9860895aec3SBjoern A. Zeeb } 9870895aec3SBjoern A. Zeeb 9880895aec3SBjoern A. Zeeb /* 9895200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 9905200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 9915200e00eSIan Dowse * address and port for the PCB; these are updated to the values 9925200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 9935200e00eSIan Dowse * the connect. 9945200e00eSIan Dowse * 9955200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 9965200e00eSIan Dowse * and port. These are not updated in the error case. 9975200e00eSIan Dowse * 9985200e00eSIan Dowse * If the operation fails because the connection already exists, 9995200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 10005200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 10015200e00eSIan Dowse * is set to NULL. 10025200e00eSIan Dowse */ 10035200e00eSIan Dowse int 1004136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 1005136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1006136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 10075200e00eSIan Dowse { 1008cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 10095200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 10105200e00eSIan Dowse struct in_ifaddr *ia; 10115200e00eSIan Dowse struct inpcb *oinp; 1012b89e82ddSJamie Gritton struct in_addr laddr, faddr; 10135200e00eSIan Dowse u_short lport, fport; 10145200e00eSIan Dowse int error; 10155200e00eSIan Dowse 10168501a69cSRobert Watson /* 10178501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 10188501a69cSRobert Watson * lock is sufficient. 10198501a69cSRobert Watson */ 102027f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1021fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 102227f74fd0SRobert Watson 10235200e00eSIan Dowse if (oinpp != NULL) 10245200e00eSIan Dowse *oinpp = NULL; 102557bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1026df8bae1dSRodney W. Grimes return (EINVAL); 1027df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1028df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1029df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1030df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 10315200e00eSIan Dowse laddr.s_addr = *laddrp; 10325200e00eSIan Dowse lport = *lportp; 10335200e00eSIan Dowse faddr = sin->sin_addr; 10345200e00eSIan Dowse fport = sin->sin_port; 10350895aec3SBjoern A. Zeeb 1036603724d3SBjoern A. Zeeb if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 1037df8bae1dSRodney W. Grimes /* 1038df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1039df8bae1dSRodney W. Grimes * use the primary local address. 1040df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1041df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1042df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1043df8bae1dSRodney W. Grimes */ 1044413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 1045cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1046413628a7SBjoern A. Zeeb faddr = 1047b89e82ddSJamie Gritton IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1048cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1049b89e82ddSJamie Gritton if (cred != NULL && 1050b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1051b89e82ddSJamie Gritton return (error); 10522d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1053cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 10542d9cfabaSRobert Watson if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 10552d9cfabaSRobert Watson IFF_BROADCAST) 10565200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 1057603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1058cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 10592d9cfabaSRobert Watson } 1060df8bae1dSRodney W. Grimes } 10615200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1062d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1063df8bae1dSRodney W. Grimes /* 1064df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1065d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1066df8bae1dSRodney W. Grimes * address of that interface as our source address. 1067df8bae1dSRodney W. Grimes */ 10685200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1069df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1070df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1071df8bae1dSRodney W. Grimes struct ifnet *ifp; 1072df8bae1dSRodney W. Grimes 1073df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1074df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1075df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 1076cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 1077e691be70SDaniel Eischen TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1078e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1079e691be70SDaniel Eischen (cred == NULL || 1080e691be70SDaniel Eischen prison_check_ip4(cred, 1081e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1082df8bae1dSRodney W. Grimes break; 1083e691be70SDaniel Eischen } 1084e691be70SDaniel Eischen if (ia == NULL) 1085d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1086e691be70SDaniel Eischen else { 10875200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1088d79fdd98SDaniel Eischen error = 0; 1089999f1343SGarrett Wollman } 1090cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 1091d79fdd98SDaniel Eischen } 1092d79fdd98SDaniel Eischen } 109304215ed2SRandall Stewart if (error) 109404215ed2SRandall Stewart return (error); 10950895aec3SBjoern A. Zeeb } 1096fa046d87SRobert Watson oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1097fa046d87SRobert Watson laddr, lport, 0, NULL); 10985200e00eSIan Dowse if (oinp != NULL) { 10995200e00eSIan Dowse if (oinpp != NULL) 11005200e00eSIan Dowse *oinpp = oinp; 1101df8bae1dSRodney W. Grimes return (EADDRINUSE); 1102c3229e05SDavid Greenman } 11035200e00eSIan Dowse if (lport == 0) { 1104b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1105b0330ed9SPawel Jakub Dawidek cred); 11065a903f8dSPierre Beyssac if (error) 11075a903f8dSPierre Beyssac return (error); 11085a903f8dSPierre Beyssac } 11095200e00eSIan Dowse *laddrp = laddr.s_addr; 11105200e00eSIan Dowse *lportp = lport; 11115200e00eSIan Dowse *faddrp = faddr.s_addr; 11125200e00eSIan Dowse *fportp = fport; 1113df8bae1dSRodney W. Grimes return (0); 1114df8bae1dSRodney W. Grimes } 1115df8bae1dSRodney W. Grimes 111626f9a767SRodney W. Grimes void 1117136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1118df8bae1dSRodney W. Grimes { 11196b348152SRobert Watson 11208501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1121fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1122df8bae1dSRodney W. Grimes 1123df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1124df8bae1dSRodney W. Grimes inp->inp_fport = 0; 112515bd2b43SDavid Greenman in_pcbrehash(inp); 1126df8bae1dSRodney W. Grimes } 112783e521ecSBjoern A. Zeeb #endif /* INET */ 1128df8bae1dSRodney W. Grimes 11294c7c478dSRobert Watson /* 113028696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1131c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 113228696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 113328696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 11344c7c478dSRobert Watson */ 113526f9a767SRodney W. Grimes void 1136136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1137df8bae1dSRodney W. Grimes { 11384c7c478dSRobert Watson 1139a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1140c0a211c5SRobert Watson 11414c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 11424c7c478dSRobert Watson inp->inp_socket = NULL; 11434c7c478dSRobert Watson } 11444c7c478dSRobert Watson 1145c0a211c5SRobert Watson /* 114679bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 114779bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 114879bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 114952cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 115052cd27cbSRobert Watson * via a pcbgroup. 115179bdc6e5SRobert Watson * 115279bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 115379bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 115479bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 115579bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 115679bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 115779bdc6e5SRobert Watson * inpcb. 115879bdc6e5SRobert Watson * 115979bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 116079bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 116179bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 116279bdc6e5SRobert Watson * using in_pcbrele(). 1163c0a211c5SRobert Watson */ 116479bdc6e5SRobert Watson void 116579bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 11664c7c478dSRobert Watson { 1167df8bae1dSRodney W. Grimes 116879bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 116979bdc6e5SRobert Watson 117079bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 117179bdc6e5SRobert Watson } 117279bdc6e5SRobert Watson 117379bdc6e5SRobert Watson /* 117479bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 117579bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 117679bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 117779bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 117879bdc6e5SRobert Watson * 117979bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 118079bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 118179bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 118279bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 118379bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 118479bdc6e5SRobert Watson */ 118579bdc6e5SRobert Watson int 118679bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 118779bdc6e5SRobert Watson { 118879bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 118979bdc6e5SRobert Watson 119079bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 119179bdc6e5SRobert Watson 119279bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 119379bdc6e5SRobert Watson 1194df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1195df4e91d3SGleb Smirnoff /* 1196df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1197df4e91d3SGleb Smirnoff * this isn't the last reference. 1198df4e91d3SGleb Smirnoff */ 1199df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1200df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1201df4e91d3SGleb Smirnoff return (1); 1202df4e91d3SGleb Smirnoff } 120379bdc6e5SRobert Watson return (0); 1204df4e91d3SGleb Smirnoff } 120579bdc6e5SRobert Watson 120679bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 120779bdc6e5SRobert Watson 120879bdc6e5SRobert Watson INP_RUNLOCK(inp); 120979bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 121079bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 121179bdc6e5SRobert Watson return (1); 121279bdc6e5SRobert Watson } 121379bdc6e5SRobert Watson 121479bdc6e5SRobert Watson int 121579bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 121679bdc6e5SRobert Watson { 121779bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 121879bdc6e5SRobert Watson 121979bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 122079bdc6e5SRobert Watson 122179bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 122279bdc6e5SRobert Watson 1223*edd0e0b0SFabien Thomas if (refcount_release(&inp->inp_refcount) == 0) { 1224*edd0e0b0SFabien Thomas /* 1225*edd0e0b0SFabien Thomas * If the inpcb has been freed, let the caller know, even if 1226*edd0e0b0SFabien Thomas * this isn't the last reference. 1227*edd0e0b0SFabien Thomas */ 1228*edd0e0b0SFabien Thomas if (inp->inp_flags2 & INP_FREED) { 1229*edd0e0b0SFabien Thomas INP_WUNLOCK(inp); 1230*edd0e0b0SFabien Thomas return (1); 1231*edd0e0b0SFabien Thomas } 123279bdc6e5SRobert Watson return (0); 1233*edd0e0b0SFabien Thomas } 123479bdc6e5SRobert Watson 123579bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 123679bdc6e5SRobert Watson 123779bdc6e5SRobert Watson INP_WUNLOCK(inp); 123879bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 123979bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 124079bdc6e5SRobert Watson return (1); 124179bdc6e5SRobert Watson } 124279bdc6e5SRobert Watson 124379bdc6e5SRobert Watson /* 124479bdc6e5SRobert Watson * Temporary wrapper. 124579bdc6e5SRobert Watson */ 124679bdc6e5SRobert Watson int 124779bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 124879bdc6e5SRobert Watson { 124979bdc6e5SRobert Watson 125079bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 125179bdc6e5SRobert Watson } 125279bdc6e5SRobert Watson 125379bdc6e5SRobert Watson /* 125479bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 125579bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 125679bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 125779bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 125879bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 125979bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 126079bdc6e5SRobert Watson * the pcbinfo lock is held. 126179bdc6e5SRobert Watson */ 126279bdc6e5SRobert Watson void 126379bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 126479bdc6e5SRobert Watson { 126579bdc6e5SRobert Watson struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 126679bdc6e5SRobert Watson 126779bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 126879bdc6e5SRobert Watson 1269ff9b006dSJulien Charbon #ifdef INVARIANTS 1270ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 1271079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(pcbinfo); 1272ff9b006dSJulien Charbon } else { 127379bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 1274ff9b006dSJulien Charbon } 1275ff9b006dSJulien Charbon #endif 127679bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 127779bdc6e5SRobert Watson 127879bdc6e5SRobert Watson /* XXXRW: Do as much as possible here. */ 1279b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 12806aee2fc5SBjoern A. Zeeb if (inp->inp_sp != NULL) 12816974bd9eSBjoern A. Zeeb ipsec_delete_pcbpolicy(inp); 128283e521ecSBjoern A. Zeeb #endif 1283ff9b006dSJulien Charbon INP_LIST_WLOCK(pcbinfo); 128479bdc6e5SRobert Watson inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1285c3229e05SDavid Greenman in_pcbremlists(inp); 1286ff9b006dSJulien Charbon INP_LIST_WUNLOCK(pcbinfo); 12876aee2fc5SBjoern A. Zeeb #ifdef INET6 12886aee2fc5SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 12896aee2fc5SBjoern A. Zeeb ip6_freepcbopts(inp->in6p_outputopts); 12901096332aSBruce M Simpson if (inp->in6p_moptions != NULL) 12916aee2fc5SBjoern A. Zeeb ip6_freemoptions(inp->in6p_moptions); 12926aee2fc5SBjoern A. Zeeb } 12936aee2fc5SBjoern A. Zeeb #endif 1294df8bae1dSRodney W. Grimes if (inp->inp_options) 1295df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 129667107f45SBjoern A. Zeeb #ifdef INET 129771498f30SBruce M Simpson if (inp->inp_moptions != NULL) 129871498f30SBruce M Simpson inp_freemoptions(inp->inp_moptions); 129967107f45SBjoern A. Zeeb #endif 1300cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 1301df4e91d3SGleb Smirnoff inp->inp_flags2 |= INP_FREED; 130286d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 1303a557af22SRobert Watson #ifdef MAC 130430d239bcSRobert Watson mac_inpcb_destroy(inp); 1305a557af22SRobert Watson #endif 130679bdc6e5SRobert Watson if (!in_pcbrele_wlocked(inp)) 130728696211SRobert Watson INP_WUNLOCK(inp); 130828696211SRobert Watson } 130928696211SRobert Watson 131028696211SRobert Watson /* 1311c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1312c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1313c0a211c5SRobert Watson * 1314c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1315c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1316c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1317c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1318c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1319c0a211c5SRobert Watson * in_pcbdetach(). 1320c0a211c5SRobert Watson * 1321c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1322c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 132310702a28SRobert Watson */ 132410702a28SRobert Watson void 132510702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 132610702a28SRobert Watson { 132710702a28SRobert Watson 13288501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 132910702a28SRobert Watson 1330fa046d87SRobert Watson /* 1331fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1332fa046d87SRobert Watson * the hash lock...? 1333fa046d87SRobert Watson */ 1334ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1335111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 133610702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 133710702a28SRobert Watson 1338fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 133910702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 134010702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 134110702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 134210702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 134310702a28SRobert Watson free(phd, M_PCB); 134410702a28SRobert Watson } 1345fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1346111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 134752cd27cbSRobert Watson #ifdef PCBGROUP 134852cd27cbSRobert Watson in_pcbgroup_remove(inp); 134952cd27cbSRobert Watson #endif 135010702a28SRobert Watson } 135110702a28SRobert Watson } 135210702a28SRobert Watson 135367107f45SBjoern A. Zeeb #ifdef INET 135454d642bbSRobert Watson /* 135554d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 135654d642bbSRobert Watson */ 135726ef6ac4SDon Lewis struct sockaddr * 1358136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 135926ef6ac4SDon Lewis { 136026ef6ac4SDon Lewis struct sockaddr_in *sin; 136126ef6ac4SDon Lewis 13621ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1363a163d034SWarner Losh M_WAITOK | M_ZERO); 136426ef6ac4SDon Lewis sin->sin_family = AF_INET; 136526ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 136626ef6ac4SDon Lewis sin->sin_addr = *addr_p; 136726ef6ac4SDon Lewis sin->sin_port = port; 136826ef6ac4SDon Lewis 136926ef6ac4SDon Lewis return (struct sockaddr *)sin; 137026ef6ac4SDon Lewis } 137126ef6ac4SDon Lewis 1372117bcae7SGarrett Wollman int 137354d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1374df8bae1dSRodney W. Grimes { 1375136d4f1cSRobert Watson struct inpcb *inp; 137626ef6ac4SDon Lewis struct in_addr addr; 137726ef6ac4SDon Lewis in_port_t port; 137842fa505bSDavid Greenman 1379fdc984f7STor Egge inp = sotoinpcb(so); 138054d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 13816466b28aSRobert Watson 1382a69042a5SRobert Watson INP_RLOCK(inp); 138326ef6ac4SDon Lewis port = inp->inp_lport; 138426ef6ac4SDon Lewis addr = inp->inp_laddr; 1385a69042a5SRobert Watson INP_RUNLOCK(inp); 138642fa505bSDavid Greenman 138726ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1388117bcae7SGarrett Wollman return 0; 1389df8bae1dSRodney W. Grimes } 1390df8bae1dSRodney W. Grimes 1391117bcae7SGarrett Wollman int 139254d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1393df8bae1dSRodney W. Grimes { 1394136d4f1cSRobert Watson struct inpcb *inp; 139526ef6ac4SDon Lewis struct in_addr addr; 139626ef6ac4SDon Lewis in_port_t port; 139742fa505bSDavid Greenman 1398fdc984f7STor Egge inp = sotoinpcb(so); 139954d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 14006466b28aSRobert Watson 1401a69042a5SRobert Watson INP_RLOCK(inp); 140226ef6ac4SDon Lewis port = inp->inp_fport; 140326ef6ac4SDon Lewis addr = inp->inp_faddr; 1404a69042a5SRobert Watson INP_RUNLOCK(inp); 140542fa505bSDavid Greenman 140626ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1407117bcae7SGarrett Wollman return 0; 1408df8bae1dSRodney W. Grimes } 1409df8bae1dSRodney W. Grimes 141026f9a767SRodney W. Grimes void 1411136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1412136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1413d1c54148SJesper Skriver { 1414f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1415d1c54148SJesper Skriver 14163dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1417f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 14188501a69cSRobert Watson INP_WLOCK(inp); 1419d1c54148SJesper Skriver #ifdef INET6 1420f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 14218501a69cSRobert Watson INP_WUNLOCK(inp); 1422d1c54148SJesper Skriver continue; 1423f76fcf6dSJeffrey Hsu } 1424d1c54148SJesper Skriver #endif 1425d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1426f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 14278501a69cSRobert Watson INP_WUNLOCK(inp); 1428d1c54148SJesper Skriver continue; 1429d1c54148SJesper Skriver } 14303dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 14318501a69cSRobert Watson INP_WUNLOCK(inp); 1432f76fcf6dSJeffrey Hsu } 14333dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1434d1c54148SJesper Skriver } 1435d1c54148SJesper Skriver 1436e43cc4aeSHajimu UMEMOTO void 1437136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1438e43cc4aeSHajimu UMEMOTO { 1439e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1440e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1441e43cc4aeSHajimu UMEMOTO int i, gap; 1442e43cc4aeSHajimu UMEMOTO 1443ff9b006dSJulien Charbon INP_INFO_WLOCK(pcbinfo); 1444712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 14458501a69cSRobert Watson INP_WLOCK(inp); 1446e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1447e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1448e43cc4aeSHajimu UMEMOTO imo != NULL) { 1449e43cc4aeSHajimu UMEMOTO /* 1450e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1451e43cc4aeSHajimu UMEMOTO * detached. 1452e43cc4aeSHajimu UMEMOTO */ 1453e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1454e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1455e43cc4aeSHajimu UMEMOTO 1456e43cc4aeSHajimu UMEMOTO /* 1457e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1458e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1459e43cc4aeSHajimu UMEMOTO */ 1460e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1461e43cc4aeSHajimu UMEMOTO i++) { 1462e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1463e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 1464e43cc4aeSHajimu UMEMOTO gap++; 1465e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1466e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1467e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1468e43cc4aeSHajimu UMEMOTO } 1469e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1470e43cc4aeSHajimu UMEMOTO } 14718501a69cSRobert Watson INP_WUNLOCK(inp); 1472e43cc4aeSHajimu UMEMOTO } 1473ff9b006dSJulien Charbon INP_INFO_WUNLOCK(pcbinfo); 1474e43cc4aeSHajimu UMEMOTO } 1475e43cc4aeSHajimu UMEMOTO 1476df8bae1dSRodney W. Grimes /* 1477fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1478fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1479c3229e05SDavid Greenman */ 1480d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1481df8bae1dSRodney W. Grimes struct inpcb * 1482136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 148368e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1484df8bae1dSRodney W. Grimes { 1485136d4f1cSRobert Watson struct inpcb *inp; 1486d5e8a67eSHajimu UMEMOTO #ifdef INET6 1487d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1488d5e8a67eSHajimu UMEMOTO #else 1489d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1490d5e8a67eSHajimu UMEMOTO #endif 1491d5e8a67eSHajimu UMEMOTO int wildcard; 14927bc4aca7SDavid Greenman 149368e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 149468e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 149568e0d7e0SRobert Watson 1496fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 14971b73ca0bSSam Leffler 149868e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1499c3229e05SDavid Greenman struct inpcbhead *head; 1500c3229e05SDavid Greenman /* 1501c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1502c3229e05SDavid Greenman * matches the local address and port we're looking for. 1503c3229e05SDavid Greenman */ 1504712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1505712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1506fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1507cfa1ca9dSYoshinobu Inoue #ifdef INET6 1508413628a7SBjoern A. Zeeb /* XXX inp locking */ 1509369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1510cfa1ca9dSYoshinobu Inoue continue; 1511cfa1ca9dSYoshinobu Inoue #endif 1512c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1513c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1514c3229e05SDavid Greenman inp->inp_lport == lport) { 1515c3229e05SDavid Greenman /* 1516413628a7SBjoern A. Zeeb * Found? 1517c3229e05SDavid Greenman */ 1518413628a7SBjoern A. Zeeb if (cred == NULL || 15190304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 15200304c731SJamie Gritton inp->inp_cred->cr_prison)) 1521c3229e05SDavid Greenman return (inp); 1522df8bae1dSRodney W. Grimes } 1523c3229e05SDavid Greenman } 1524c3229e05SDavid Greenman /* 1525c3229e05SDavid Greenman * Not found. 1526c3229e05SDavid Greenman */ 1527c3229e05SDavid Greenman return (NULL); 1528c3229e05SDavid Greenman } else { 1529c3229e05SDavid Greenman struct inpcbporthead *porthash; 1530c3229e05SDavid Greenman struct inpcbport *phd; 1531c3229e05SDavid Greenman struct inpcb *match = NULL; 1532c3229e05SDavid Greenman /* 1533c3229e05SDavid Greenman * Best fit PCB lookup. 1534c3229e05SDavid Greenman * 1535c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1536c3229e05SDavid Greenman * port hash list. 1537c3229e05SDavid Greenman */ 1538712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1539712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1540fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1541c3229e05SDavid Greenman if (phd->phd_port == lport) 1542c3229e05SDavid Greenman break; 1543c3229e05SDavid Greenman } 1544c3229e05SDavid Greenman if (phd != NULL) { 1545c3229e05SDavid Greenman /* 1546c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1547c3229e05SDavid Greenman * fit. 1548c3229e05SDavid Greenman */ 154937d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1550c3229e05SDavid Greenman wildcard = 0; 1551413628a7SBjoern A. Zeeb if (cred != NULL && 15520304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 15530304c731SJamie Gritton cred->cr_prison)) 1554413628a7SBjoern A. Zeeb continue; 1555cfa1ca9dSYoshinobu Inoue #ifdef INET6 1556413628a7SBjoern A. Zeeb /* XXX inp locking */ 1557369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1558cfa1ca9dSYoshinobu Inoue continue; 1559d5e8a67eSHajimu UMEMOTO /* 1560d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1561d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1562d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1563d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1564d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1565d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1566d5e8a67eSHajimu UMEMOTO * 1567d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1568d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1569d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1570d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1571d5e8a67eSHajimu UMEMOTO */ 1572d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1573d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1574cfa1ca9dSYoshinobu Inoue #endif 1575c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1576c3229e05SDavid Greenman wildcard++; 157715bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 157815bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 157915bd2b43SDavid Greenman wildcard++; 158015bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 158115bd2b43SDavid Greenman continue; 158215bd2b43SDavid Greenman } else { 158315bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 158415bd2b43SDavid Greenman wildcard++; 158515bd2b43SDavid Greenman } 1586df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1587df8bae1dSRodney W. Grimes match = inp; 1588df8bae1dSRodney W. Grimes matchwild = wildcard; 1589413628a7SBjoern A. Zeeb if (matchwild == 0) 1590df8bae1dSRodney W. Grimes break; 1591df8bae1dSRodney W. Grimes } 1592df8bae1dSRodney W. Grimes } 15933dbdc25cSDavid Greenman } 1594df8bae1dSRodney W. Grimes return (match); 1595df8bae1dSRodney W. Grimes } 1596c3229e05SDavid Greenman } 1597d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 159815bd2b43SDavid Greenman 159952cd27cbSRobert Watson #ifdef PCBGROUP 160052cd27cbSRobert Watson /* 160152cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 160252cd27cbSRobert Watson */ 160352cd27cbSRobert Watson static struct inpcb * 160452cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 160552cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 160652cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 160752cd27cbSRobert Watson { 160852cd27cbSRobert Watson struct inpcbhead *head; 160952cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 161052cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 161152cd27cbSRobert Watson 161252cd27cbSRobert Watson /* 161352cd27cbSRobert Watson * First look for an exact match. 161452cd27cbSRobert Watson */ 161552cd27cbSRobert Watson tmpinp = NULL; 161652cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 161752cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 161852cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 161952cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgrouphash) { 162052cd27cbSRobert Watson #ifdef INET6 162152cd27cbSRobert Watson /* XXX inp locking */ 162252cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 162352cd27cbSRobert Watson continue; 162452cd27cbSRobert Watson #endif 162552cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 162652cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 162752cd27cbSRobert Watson inp->inp_fport == fport && 162852cd27cbSRobert Watson inp->inp_lport == lport) { 162952cd27cbSRobert Watson /* 163052cd27cbSRobert Watson * XXX We should be able to directly return 163152cd27cbSRobert Watson * the inp here, without any checks. 163252cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 163352cd27cbSRobert Watson */ 163452cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 163552cd27cbSRobert Watson goto found; 163652cd27cbSRobert Watson if (tmpinp == NULL) 163752cd27cbSRobert Watson tmpinp = inp; 163852cd27cbSRobert Watson } 163952cd27cbSRobert Watson } 164052cd27cbSRobert Watson if (tmpinp != NULL) { 164152cd27cbSRobert Watson inp = tmpinp; 164252cd27cbSRobert Watson goto found; 164352cd27cbSRobert Watson } 164452cd27cbSRobert Watson 16450a100a6fSAdrian Chadd #ifdef RSS 16460a100a6fSAdrian Chadd /* 16470a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 16480a100a6fSAdrian Chadd * match for an RSS-local socket. 16490a100a6fSAdrian Chadd */ 16500a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 16510a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 16520a100a6fSAdrian Chadd #ifdef INET6 16530a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 16540a100a6fSAdrian Chadd #endif 16550a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 16560a100a6fSAdrian Chadd struct inpcbhead *head; 16570a100a6fSAdrian Chadd int injail; 16580a100a6fSAdrian Chadd 16590a100a6fSAdrian Chadd /* 16600a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 16610a100a6fSAdrian Chadd * 1. jailed, non-wild. 16620a100a6fSAdrian Chadd * 2. jailed, wild. 16630a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 16640a100a6fSAdrian Chadd * 4. non-jailed, wild. 16650a100a6fSAdrian Chadd */ 16660a100a6fSAdrian Chadd 16670a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 16680a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 16690a100a6fSAdrian Chadd LIST_FOREACH(inp, head, inp_pcbgrouphash) { 16700a100a6fSAdrian Chadd #ifdef INET6 16710a100a6fSAdrian Chadd /* XXX inp locking */ 16720a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 16730a100a6fSAdrian Chadd continue; 16740a100a6fSAdrian Chadd #endif 16750a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 16760a100a6fSAdrian Chadd inp->inp_lport != lport) 16770a100a6fSAdrian Chadd continue; 16780a100a6fSAdrian Chadd 16790a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 16800a100a6fSAdrian Chadd if (injail) { 16810a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 16820a100a6fSAdrian Chadd &laddr) != 0) 16830a100a6fSAdrian Chadd continue; 16840a100a6fSAdrian Chadd } else { 16850a100a6fSAdrian Chadd if (local_exact != NULL) 16860a100a6fSAdrian Chadd continue; 16870a100a6fSAdrian Chadd } 16880a100a6fSAdrian Chadd 16890a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 16900a100a6fSAdrian Chadd if (injail) 16910a100a6fSAdrian Chadd goto found; 16920a100a6fSAdrian Chadd else 16930a100a6fSAdrian Chadd local_exact = inp; 16940a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 16950a100a6fSAdrian Chadd #ifdef INET6 16960a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 16970a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 16980a100a6fSAdrian Chadd local_wild_mapped = inp; 16990a100a6fSAdrian Chadd else 17000a100a6fSAdrian Chadd #endif 17010a100a6fSAdrian Chadd if (injail) 17020a100a6fSAdrian Chadd jail_wild = inp; 17030a100a6fSAdrian Chadd else 17040a100a6fSAdrian Chadd local_wild = inp; 17050a100a6fSAdrian Chadd } 17060a100a6fSAdrian Chadd } /* LIST_FOREACH */ 17070a100a6fSAdrian Chadd 17080a100a6fSAdrian Chadd inp = jail_wild; 17090a100a6fSAdrian Chadd if (inp == NULL) 17100a100a6fSAdrian Chadd inp = local_exact; 17110a100a6fSAdrian Chadd if (inp == NULL) 17120a100a6fSAdrian Chadd inp = local_wild; 17130a100a6fSAdrian Chadd #ifdef INET6 17140a100a6fSAdrian Chadd if (inp == NULL) 17150a100a6fSAdrian Chadd inp = local_wild_mapped; 17160a100a6fSAdrian Chadd #endif 17170a100a6fSAdrian Chadd if (inp != NULL) 17180a100a6fSAdrian Chadd goto found; 17190a100a6fSAdrian Chadd } 17200a100a6fSAdrian Chadd #endif 17210a100a6fSAdrian Chadd 172252cd27cbSRobert Watson /* 172352cd27cbSRobert Watson * Then look for a wildcard match, if requested. 172452cd27cbSRobert Watson */ 172552cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 172652cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 172752cd27cbSRobert Watson #ifdef INET6 172852cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 172952cd27cbSRobert Watson #endif 173052cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 173152cd27cbSRobert Watson struct inpcbhead *head; 173252cd27cbSRobert Watson int injail; 173352cd27cbSRobert Watson 173452cd27cbSRobert Watson /* 173552cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 173652cd27cbSRobert Watson * 1. jailed, non-wild. 173752cd27cbSRobert Watson * 2. jailed, wild. 173852cd27cbSRobert Watson * 3. non-jailed, non-wild. 173952cd27cbSRobert Watson * 4. non-jailed, wild. 174052cd27cbSRobert Watson */ 174152cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 174252cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 174352cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 174452cd27cbSRobert Watson #ifdef INET6 174552cd27cbSRobert Watson /* XXX inp locking */ 174652cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 174752cd27cbSRobert Watson continue; 174852cd27cbSRobert Watson #endif 174952cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 175052cd27cbSRobert Watson inp->inp_lport != lport) 175152cd27cbSRobert Watson continue; 175252cd27cbSRobert Watson 175352cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 175452cd27cbSRobert Watson if (injail) { 175552cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 175652cd27cbSRobert Watson &laddr) != 0) 175752cd27cbSRobert Watson continue; 175852cd27cbSRobert Watson } else { 175952cd27cbSRobert Watson if (local_exact != NULL) 176052cd27cbSRobert Watson continue; 176152cd27cbSRobert Watson } 176252cd27cbSRobert Watson 176352cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 176452cd27cbSRobert Watson if (injail) 176552cd27cbSRobert Watson goto found; 176652cd27cbSRobert Watson else 176752cd27cbSRobert Watson local_exact = inp; 176852cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 176952cd27cbSRobert Watson #ifdef INET6 177052cd27cbSRobert Watson /* XXX inp locking, NULL check */ 177152cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 177252cd27cbSRobert Watson local_wild_mapped = inp; 177352cd27cbSRobert Watson else 177483e521ecSBjoern A. Zeeb #endif 177552cd27cbSRobert Watson if (injail) 177652cd27cbSRobert Watson jail_wild = inp; 177752cd27cbSRobert Watson else 177852cd27cbSRobert Watson local_wild = inp; 177952cd27cbSRobert Watson } 178052cd27cbSRobert Watson } /* LIST_FOREACH */ 178152cd27cbSRobert Watson inp = jail_wild; 178252cd27cbSRobert Watson if (inp == NULL) 178352cd27cbSRobert Watson inp = local_exact; 178452cd27cbSRobert Watson if (inp == NULL) 178552cd27cbSRobert Watson inp = local_wild; 178652cd27cbSRobert Watson #ifdef INET6 178752cd27cbSRobert Watson if (inp == NULL) 178852cd27cbSRobert Watson inp = local_wild_mapped; 178983e521ecSBjoern A. Zeeb #endif 179052cd27cbSRobert Watson if (inp != NULL) 179152cd27cbSRobert Watson goto found; 179252cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 179352cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 179452cd27cbSRobert Watson return (NULL); 179552cd27cbSRobert Watson 179652cd27cbSRobert Watson found: 179752cd27cbSRobert Watson in_pcbref(inp); 179852cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 179952cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 180052cd27cbSRobert Watson INP_WLOCK(inp); 180152cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 180252cd27cbSRobert Watson return (NULL); 180352cd27cbSRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 180452cd27cbSRobert Watson INP_RLOCK(inp); 180552cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 180652cd27cbSRobert Watson return (NULL); 180752cd27cbSRobert Watson } else 180852cd27cbSRobert Watson panic("%s: locking bug", __func__); 180952cd27cbSRobert Watson return (inp); 181052cd27cbSRobert Watson } 181152cd27cbSRobert Watson #endif /* PCBGROUP */ 181252cd27cbSRobert Watson 181315bd2b43SDavid Greenman /* 1814fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 1815fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 1816fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 181715bd2b43SDavid Greenman */ 1818fa046d87SRobert Watson static struct inpcb * 1819fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 182068e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 1821136d4f1cSRobert Watson struct ifnet *ifp) 182215bd2b43SDavid Greenman { 182315bd2b43SDavid Greenman struct inpcbhead *head; 1824413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 182515bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 182615bd2b43SDavid Greenman 182768e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 182868e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 182968e0d7e0SRobert Watson 1830fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 1831602cc7f1SRobert Watson 183215bd2b43SDavid Greenman /* 183315bd2b43SDavid Greenman * First look for an exact match. 183415bd2b43SDavid Greenman */ 1835413628a7SBjoern A. Zeeb tmpinp = NULL; 1836712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1837712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1838fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1839cfa1ca9dSYoshinobu Inoue #ifdef INET6 1840413628a7SBjoern A. Zeeb /* XXX inp locking */ 1841369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1842cfa1ca9dSYoshinobu Inoue continue; 1843cfa1ca9dSYoshinobu Inoue #endif 18446d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1845ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1846ca98b82cSDavid Greenman inp->inp_fport == fport && 1847413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1848413628a7SBjoern A. Zeeb /* 1849413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1850413628a7SBjoern A. Zeeb * the inp here, without any checks. 1851413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1852413628a7SBjoern A. Zeeb */ 18530304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 1854c3229e05SDavid Greenman return (inp); 1855413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1856413628a7SBjoern A. Zeeb tmpinp = inp; 1857c3229e05SDavid Greenman } 1858413628a7SBjoern A. Zeeb } 1859413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1860413628a7SBjoern A. Zeeb return (tmpinp); 1861e3fd5ffdSRobert Watson 1862e3fd5ffdSRobert Watson /* 1863e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1864e3fd5ffdSRobert Watson */ 186568e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1866413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1867e3fd5ffdSRobert Watson #ifdef INET6 1868cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1869e3fd5ffdSRobert Watson #endif 1870413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1871413628a7SBjoern A. Zeeb int injail; 1872413628a7SBjoern A. Zeeb 1873413628a7SBjoern A. Zeeb /* 1874413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 1875413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 1876413628a7SBjoern A. Zeeb * 2. jailed, wild. 1877413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 1878413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 1879413628a7SBjoern A. Zeeb */ 18806d6a026bSDavid Greenman 1881712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1882712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1883fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1884cfa1ca9dSYoshinobu Inoue #ifdef INET6 1885413628a7SBjoern A. Zeeb /* XXX inp locking */ 1886369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1887cfa1ca9dSYoshinobu Inoue continue; 1888cfa1ca9dSYoshinobu Inoue #endif 1889413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 1890413628a7SBjoern A. Zeeb inp->inp_lport != lport) 1891413628a7SBjoern A. Zeeb continue; 1892413628a7SBjoern A. Zeeb 18930304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 1894413628a7SBjoern A. Zeeb if (injail) { 1895b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 1896b89e82ddSJamie Gritton &laddr) != 0) 1897413628a7SBjoern A. Zeeb continue; 1898413628a7SBjoern A. Zeeb } else { 1899413628a7SBjoern A. Zeeb if (local_exact != NULL) 1900413628a7SBjoern A. Zeeb continue; 1901413628a7SBjoern A. Zeeb } 1902413628a7SBjoern A. Zeeb 1903413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 1904413628a7SBjoern A. Zeeb if (injail) 1905c3229e05SDavid Greenman return (inp); 1906413628a7SBjoern A. Zeeb else 1907413628a7SBjoern A. Zeeb local_exact = inp; 1908413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1909e3fd5ffdSRobert Watson #ifdef INET6 1910413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 19115cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 1912cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1913cfa1ca9dSYoshinobu Inoue else 191483e521ecSBjoern A. Zeeb #endif 1915413628a7SBjoern A. Zeeb if (injail) 1916413628a7SBjoern A. Zeeb jail_wild = inp; 1917413628a7SBjoern A. Zeeb else 19186d6a026bSDavid Greenman local_wild = inp; 19196d6a026bSDavid Greenman } 1920413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 1921413628a7SBjoern A. Zeeb if (jail_wild != NULL) 1922413628a7SBjoern A. Zeeb return (jail_wild); 1923413628a7SBjoern A. Zeeb if (local_exact != NULL) 1924413628a7SBjoern A. Zeeb return (local_exact); 1925413628a7SBjoern A. Zeeb if (local_wild != NULL) 1926c3229e05SDavid Greenman return (local_wild); 1927413628a7SBjoern A. Zeeb #ifdef INET6 1928413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 1929413628a7SBjoern A. Zeeb return (local_wild_mapped); 193083e521ecSBjoern A. Zeeb #endif 193168e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 1932413628a7SBjoern A. Zeeb 19336d6a026bSDavid Greenman return (NULL); 193415bd2b43SDavid Greenman } 1935fa046d87SRobert Watson 1936fa046d87SRobert Watson /* 1937fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 1938fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 1939fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 1940fa046d87SRobert Watson */ 1941fa046d87SRobert Watson static struct inpcb * 1942fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1943fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 1944fa046d87SRobert Watson struct ifnet *ifp) 1945fa046d87SRobert Watson { 1946fa046d87SRobert Watson struct inpcb *inp; 1947fa046d87SRobert Watson 1948fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 1949fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1950fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 1951fa046d87SRobert Watson if (inp != NULL) { 1952fa046d87SRobert Watson in_pcbref(inp); 1953fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1954fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 1955fa046d87SRobert Watson INP_WLOCK(inp); 1956fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) 1957fa046d87SRobert Watson return (NULL); 1958fa046d87SRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 1959fa046d87SRobert Watson INP_RLOCK(inp); 1960fa046d87SRobert Watson if (in_pcbrele_rlocked(inp)) 1961fa046d87SRobert Watson return (NULL); 1962fa046d87SRobert Watson } else 1963fa046d87SRobert Watson panic("%s: locking bug", __func__); 1964fa046d87SRobert Watson } else 1965fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1966fa046d87SRobert Watson return (inp); 1967fa046d87SRobert Watson } 1968fa046d87SRobert Watson 1969fa046d87SRobert Watson /* 1970d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1971d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 197252cd27cbSRobert Watson * 197352cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 1974fa046d87SRobert Watson */ 1975fa046d87SRobert Watson struct inpcb * 1976fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 1977fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 1978fa046d87SRobert Watson { 19797527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 198052cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 198152cd27cbSRobert Watson #endif 1982fa046d87SRobert Watson 1983fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1984fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 1985fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1986fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 1987fa046d87SRobert Watson 19887527624eSRobert Watson /* 19897527624eSRobert Watson * When not using RSS, use connection groups in preference to the 19907527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 19917527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 19927527624eSRobert Watson * in software. 19937527624eSRobert Watson * 19947527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 19957527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 19967527624eSRobert Watson * in software. 19977527624eSRobert Watson */ 19987527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 199952cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 200052cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 200152cd27cbSRobert Watson fport); 200252cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 200352cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 200452cd27cbSRobert Watson } 200552cd27cbSRobert Watson #endif 2006fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2007fa046d87SRobert Watson lookupflags, ifp)); 2008fa046d87SRobert Watson } 2009d3c1f003SRobert Watson 2010d3c1f003SRobert Watson struct inpcb * 2011d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2012d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2013d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 2014d3c1f003SRobert Watson { 201552cd27cbSRobert Watson #ifdef PCBGROUP 201652cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 201752cd27cbSRobert Watson #endif 2018d3c1f003SRobert Watson 2019d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2020d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2021d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2022d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2023d3c1f003SRobert Watson 202452cd27cbSRobert Watson #ifdef PCBGROUP 20257527624eSRobert Watson /* 20267527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 20277527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 20287527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 20297527624eSRobert Watson * using RSS. 20307527624eSRobert Watson * 20317527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 20327527624eSRobert Watson */ 20337527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 20347527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 203552cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 203652cd27cbSRobert Watson m->m_pkthdr.flowid); 203752cd27cbSRobert Watson if (pcbgroup != NULL) 203852cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 203952cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 20407527624eSRobert Watson #ifndef RSS 204152cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 204252cd27cbSRobert Watson fport); 204352cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 204452cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 20457527624eSRobert Watson #endif 204652cd27cbSRobert Watson } 204752cd27cbSRobert Watson #endif 2048d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2049d3c1f003SRobert Watson lookupflags, ifp)); 2050d3c1f003SRobert Watson } 205167107f45SBjoern A. Zeeb #endif /* INET */ 205215bd2b43SDavid Greenman 20537bc4aca7SDavid Greenman /* 2054c3229e05SDavid Greenman * Insert PCB onto various hash lists. 20557bc4aca7SDavid Greenman */ 205652cd27cbSRobert Watson static int 205752cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 205815bd2b43SDavid Greenman { 2059c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2060c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2061c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2062c3229e05SDavid Greenman struct inpcbport *phd; 2063cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 206415bd2b43SDavid Greenman 20658501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2066fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2067fa046d87SRobert Watson 2068111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2069111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2070602cc7f1SRobert Watson 2071cfa1ca9dSYoshinobu Inoue #ifdef INET6 2072cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 20731b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2074cfa1ca9dSYoshinobu Inoue else 207583e521ecSBjoern A. Zeeb #endif 2076cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2077cfa1ca9dSYoshinobu Inoue 2078712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2079712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 208015bd2b43SDavid Greenman 2081712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2082712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2083c3229e05SDavid Greenman 2084c3229e05SDavid Greenman /* 2085c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2086c3229e05SDavid Greenman */ 2087fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 2088c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2089c3229e05SDavid Greenman break; 2090c3229e05SDavid Greenman } 2091c3229e05SDavid Greenman /* 2092c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2093c3229e05SDavid Greenman */ 2094c3229e05SDavid Greenman if (phd == NULL) { 20951ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2096c3229e05SDavid Greenman if (phd == NULL) { 2097c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2098c3229e05SDavid Greenman } 2099c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2100c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 2101c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2102c3229e05SDavid Greenman } 2103c3229e05SDavid Greenman inp->inp_phd = phd; 2104c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2105c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2106111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 210752cd27cbSRobert Watson #ifdef PCBGROUP 210852cd27cbSRobert Watson if (do_pcbgroup_update) 210952cd27cbSRobert Watson in_pcbgroup_update(inp); 211052cd27cbSRobert Watson #endif 2111c3229e05SDavid Greenman return (0); 211215bd2b43SDavid Greenman } 211315bd2b43SDavid Greenman 2114c3229e05SDavid Greenman /* 211552cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 211652cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 211752cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 211852cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 211952cd27cbSRobert Watson * pcbgroup until later. 212052cd27cbSRobert Watson * 212152cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 212252cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 212352cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 212452cd27cbSRobert Watson */ 212552cd27cbSRobert Watson int 212652cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 212752cd27cbSRobert Watson { 212852cd27cbSRobert Watson 212952cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 213052cd27cbSRobert Watson } 213152cd27cbSRobert Watson 213252cd27cbSRobert Watson int 213352cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 213452cd27cbSRobert Watson { 213552cd27cbSRobert Watson 213652cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 213752cd27cbSRobert Watson } 213852cd27cbSRobert Watson 213952cd27cbSRobert Watson /* 2140c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2141c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2142c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2143c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2144c3229e05SDavid Greenman */ 214515bd2b43SDavid Greenman void 2146d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 214715bd2b43SDavid Greenman { 214859daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 214915bd2b43SDavid Greenman struct inpcbhead *head; 2150cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 215115bd2b43SDavid Greenman 21528501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2153fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2154fa046d87SRobert Watson 2155111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2156111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2157602cc7f1SRobert Watson 2158cfa1ca9dSYoshinobu Inoue #ifdef INET6 2159cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 21601b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2161cfa1ca9dSYoshinobu Inoue else 216283e521ecSBjoern A. Zeeb #endif 2163cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2164cfa1ca9dSYoshinobu Inoue 2165712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2166712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 216715bd2b43SDavid Greenman 2168c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 216915bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 217052cd27cbSRobert Watson 217152cd27cbSRobert Watson #ifdef PCBGROUP 217252cd27cbSRobert Watson if (m != NULL) 217352cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 217452cd27cbSRobert Watson else 217552cd27cbSRobert Watson in_pcbgroup_update(inp); 217652cd27cbSRobert Watson #endif 2177c3229e05SDavid Greenman } 2178c3229e05SDavid Greenman 2179d3c1f003SRobert Watson void 2180d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2181d3c1f003SRobert Watson { 2182d3c1f003SRobert Watson 2183d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2184d3c1f003SRobert Watson } 2185d3c1f003SRobert Watson 2186c3229e05SDavid Greenman /* 2187c3229e05SDavid Greenman * Remove PCB from various lists. 2188c3229e05SDavid Greenman */ 21896d888973SRobert Watson static void 2190136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2191c3229e05SDavid Greenman { 219259daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 219359daba27SSam Leffler 2194ff9b006dSJulien Charbon #ifdef INVARIANTS 2195ff9b006dSJulien Charbon if (pcbinfo == &V_tcbinfo) { 2196ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(pcbinfo); 2197ff9b006dSJulien Charbon } else { 219859daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 2199ff9b006dSJulien Charbon } 2200ff9b006dSJulien Charbon #endif 2201ff9b006dSJulien Charbon 22028501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2203ff9b006dSJulien Charbon INP_LIST_WLOCK_ASSERT(pcbinfo); 220459daba27SSam Leffler 220559daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2206111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2207c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2208c3229e05SDavid Greenman 2209fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 2210c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 2211c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 2212fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2213c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 2214c3229e05SDavid Greenman free(phd, M_PCB); 2215c3229e05SDavid Greenman } 2216fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2217111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2218c3229e05SDavid Greenman } 2219c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 222059daba27SSam Leffler pcbinfo->ipi_count--; 222152cd27cbSRobert Watson #ifdef PCBGROUP 222252cd27cbSRobert Watson in_pcbgroup_remove(inp); 222352cd27cbSRobert Watson #endif 222415bd2b43SDavid Greenman } 222575c13541SPoul-Henning Kamp 2226a557af22SRobert Watson /* 2227a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2228a557af22SRobert Watson * label change into the in_pcb for the socket. 2229a557af22SRobert Watson */ 2230a557af22SRobert Watson void 2231136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2232a557af22SRobert Watson { 2233a557af22SRobert Watson #ifdef MAC 2234a557af22SRobert Watson struct inpcb *inp; 2235a557af22SRobert Watson 22364c7c478dSRobert Watson inp = sotoinpcb(so); 22374c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2238602cc7f1SRobert Watson 22398501a69cSRobert Watson INP_WLOCK(inp); 2240310e7cebSRobert Watson SOCK_LOCK(so); 2241a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2242310e7cebSRobert Watson SOCK_UNLOCK(so); 22438501a69cSRobert Watson INP_WUNLOCK(inp); 2244a557af22SRobert Watson #endif 2245a557af22SRobert Watson } 22465f311da2SMike Silbersack 22475f311da2SMike Silbersack /* 2248ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2249ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2250ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2251ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2252ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 22535f311da2SMike Silbersack */ 2254aae49dd3SBjoern A. Zeeb static void 2255136d4f1cSRobert Watson ipport_tick(void *xtp) 22565f311da2SMike Silbersack { 22578b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2258ad3a630fSRobert Watson 22595ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 22608b615593SMarko Zec VNET_FOREACH(vnet_iter) { 22618b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 22628b615593SMarko Zec if (V_ipport_tcpallocs <= 22638b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2264603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2265603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2266ad3a630fSRobert Watson } else 2267603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2268603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 22698b615593SMarko Zec CURVNET_RESTORE(); 22708b615593SMarko Zec } 22715ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 22725f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 22735f311da2SMike Silbersack } 2274497057eeSRobert Watson 2275aae49dd3SBjoern A. Zeeb static void 2276aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2277aae49dd3SBjoern A. Zeeb { 2278aae49dd3SBjoern A. Zeeb 2279aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2280aae49dd3SBjoern A. Zeeb } 2281aae49dd3SBjoern A. Zeeb 2282aae49dd3SBjoern A. Zeeb /* 2283aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2284aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2285aae49dd3SBjoern A. Zeeb */ 2286aae49dd3SBjoern A. Zeeb static void 2287aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2288aae49dd3SBjoern A. Zeeb { 2289aae49dd3SBjoern A. Zeeb 2290aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2291fd90e2edSJung-uk Kim callout_init(&ipport_tick_callout, 1); 2292aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2293aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2294aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2295aae49dd3SBjoern A. Zeeb } 2296aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2297aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2298aae49dd3SBjoern A. Zeeb 22993d585327SKip Macy void 23003d585327SKip Macy inp_wlock(struct inpcb *inp) 23013d585327SKip Macy { 23023d585327SKip Macy 23038501a69cSRobert Watson INP_WLOCK(inp); 23043d585327SKip Macy } 23053d585327SKip Macy 23063d585327SKip Macy void 23073d585327SKip Macy inp_wunlock(struct inpcb *inp) 23083d585327SKip Macy { 23093d585327SKip Macy 23108501a69cSRobert Watson INP_WUNLOCK(inp); 23113d585327SKip Macy } 23123d585327SKip Macy 23133d585327SKip Macy void 23143d585327SKip Macy inp_rlock(struct inpcb *inp) 23153d585327SKip Macy { 23163d585327SKip Macy 2317a69042a5SRobert Watson INP_RLOCK(inp); 23183d585327SKip Macy } 23193d585327SKip Macy 23203d585327SKip Macy void 23213d585327SKip Macy inp_runlock(struct inpcb *inp) 23223d585327SKip Macy { 23233d585327SKip Macy 2324a69042a5SRobert Watson INP_RUNLOCK(inp); 23253d585327SKip Macy } 23263d585327SKip Macy 23273d585327SKip Macy #ifdef INVARIANTS 23283d585327SKip Macy void 2329e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 23303d585327SKip Macy { 23313d585327SKip Macy 23328501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 23333d585327SKip Macy } 23343d585327SKip Macy 23353d585327SKip Macy void 2336e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 23373d585327SKip Macy { 23383d585327SKip Macy 23393d585327SKip Macy INP_UNLOCK_ASSERT(inp); 23403d585327SKip Macy } 23413d585327SKip Macy #endif 23423d585327SKip Macy 23439378e437SKip Macy void 23449378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 23459378e437SKip Macy { 23469378e437SKip Macy struct inpcb *inp; 23479378e437SKip Macy 2348ff9b006dSJulien Charbon INP_INFO_WLOCK(&V_tcbinfo); 234997021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 23509378e437SKip Macy INP_WLOCK(inp); 23519378e437SKip Macy func(inp, arg); 23529378e437SKip Macy INP_WUNLOCK(inp); 23539378e437SKip Macy } 2354ff9b006dSJulien Charbon INP_INFO_WUNLOCK(&V_tcbinfo); 23559378e437SKip Macy } 23569378e437SKip Macy 23579378e437SKip Macy struct socket * 23589378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 23599378e437SKip Macy { 23609378e437SKip Macy 23619378e437SKip Macy INP_WLOCK_ASSERT(inp); 23629378e437SKip Macy return (inp->inp_socket); 23639378e437SKip Macy } 23649378e437SKip Macy 23659378e437SKip Macy struct tcpcb * 23669378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 23679378e437SKip Macy { 23689378e437SKip Macy 23699378e437SKip Macy INP_WLOCK_ASSERT(inp); 23709378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 23719378e437SKip Macy } 23729378e437SKip Macy 23739378e437SKip Macy int 23749378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 23759378e437SKip Macy { 23769378e437SKip Macy 23779378e437SKip Macy return (inp->inp_ip_tos); 23789378e437SKip Macy } 23799378e437SKip Macy 23809378e437SKip Macy void 23819378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 23829378e437SKip Macy { 23839378e437SKip Macy 23849378e437SKip Macy inp->inp_ip_tos = val; 23859378e437SKip Macy } 23869378e437SKip Macy 23879378e437SKip Macy void 2388df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 23899d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 23909378e437SKip Macy { 23919378e437SKip Macy 23929d29c635SKip Macy INP_LOCK_ASSERT(inp); 2393df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2394df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 23959378e437SKip Macy *lp = inp->inp_lport; 23969378e437SKip Macy *fp = inp->inp_fport; 23979378e437SKip Macy } 23989378e437SKip Macy 2399dd0e6c38SKip Macy struct inpcb * 2400dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2401dd0e6c38SKip Macy { 2402dd0e6c38SKip Macy 2403dd0e6c38SKip Macy return (sotoinpcb(so)); 2404dd0e6c38SKip Macy } 2405dd0e6c38SKip Macy 2406dd0e6c38SKip Macy struct tcpcb * 2407dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2408dd0e6c38SKip Macy { 2409dd0e6c38SKip Macy 2410dd0e6c38SKip Macy return (sototcpcb(so)); 2411dd0e6c38SKip Macy } 2412dd0e6c38SKip Macy 2413497057eeSRobert Watson #ifdef DDB 2414497057eeSRobert Watson static void 2415497057eeSRobert Watson db_print_indent(int indent) 2416497057eeSRobert Watson { 2417497057eeSRobert Watson int i; 2418497057eeSRobert Watson 2419497057eeSRobert Watson for (i = 0; i < indent; i++) 2420497057eeSRobert Watson db_printf(" "); 2421497057eeSRobert Watson } 2422497057eeSRobert Watson 2423497057eeSRobert Watson static void 2424497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2425497057eeSRobert Watson { 2426497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2427497057eeSRobert Watson 2428497057eeSRobert Watson db_print_indent(indent); 2429497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2430497057eeSRobert Watson 2431497057eeSRobert Watson indent += 2; 2432497057eeSRobert Watson 243303dc38a4SRobert Watson #ifdef INET6 2434dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2435497057eeSRobert Watson /* IPv6. */ 2436497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2437497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 243883e521ecSBjoern A. Zeeb } else 243903dc38a4SRobert Watson #endif 244083e521ecSBjoern A. Zeeb { 2441497057eeSRobert Watson /* IPv4. */ 2442497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2443497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2444497057eeSRobert Watson } 2445497057eeSRobert Watson db_print_indent(indent); 2446497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2447497057eeSRobert Watson ntohs(inc->inc_lport)); 2448497057eeSRobert Watson db_print_indent(indent); 2449497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2450497057eeSRobert Watson ntohs(inc->inc_fport)); 2451497057eeSRobert Watson } 2452497057eeSRobert Watson 2453497057eeSRobert Watson static void 2454497057eeSRobert Watson db_print_inpflags(int inp_flags) 2455497057eeSRobert Watson { 2456497057eeSRobert Watson int comma; 2457497057eeSRobert Watson 2458497057eeSRobert Watson comma = 0; 2459497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2460497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2461497057eeSRobert Watson comma = 1; 2462497057eeSRobert Watson } 2463497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2464497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2465497057eeSRobert Watson comma = 1; 2466497057eeSRobert Watson } 2467497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2468497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2469497057eeSRobert Watson comma = 1; 2470497057eeSRobert Watson } 2471497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2472497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2473497057eeSRobert Watson comma = 1; 2474497057eeSRobert Watson } 2475497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2476497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2477497057eeSRobert Watson comma = 1; 2478497057eeSRobert Watson } 2479497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2480497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2481497057eeSRobert Watson comma = 1; 2482497057eeSRobert Watson } 2483497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2484497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2485497057eeSRobert Watson comma = 1; 2486497057eeSRobert Watson } 2487497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2488497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2489497057eeSRobert Watson comma = 1; 2490497057eeSRobert Watson } 2491497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2492497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2493497057eeSRobert Watson comma = 1; 2494497057eeSRobert Watson } 2495497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2496497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2497497057eeSRobert Watson comma = 1; 2498497057eeSRobert Watson } 2499497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2500497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2501497057eeSRobert Watson comma = 1; 2502497057eeSRobert Watson } 25033cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 25043cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 25053cca425bSMichael Tuexen comma = 1; 25063cca425bSMichael Tuexen } 2507497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2508497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2509497057eeSRobert Watson comma = 1; 2510497057eeSRobert Watson } 2511497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2512497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2513497057eeSRobert Watson comma = 1; 2514497057eeSRobert Watson } 2515497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2516497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2517497057eeSRobert Watson comma = 1; 2518497057eeSRobert Watson } 2519497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2520497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2521497057eeSRobert Watson comma = 1; 2522497057eeSRobert Watson } 2523497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2524497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2525497057eeSRobert Watson comma = 1; 2526497057eeSRobert Watson } 2527497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2528497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2529497057eeSRobert Watson comma = 1; 2530497057eeSRobert Watson } 2531497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2532497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2533497057eeSRobert Watson comma = 1; 2534497057eeSRobert Watson } 2535497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2536497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2537497057eeSRobert Watson comma = 1; 2538497057eeSRobert Watson } 2539497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2540497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2541497057eeSRobert Watson comma = 1; 2542497057eeSRobert Watson } 2543ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2544ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2545ad71fe3cSRobert Watson comma = 1; 2546ad71fe3cSRobert Watson } 2547ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2548ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2549ad71fe3cSRobert Watson comma = 1; 2550ad71fe3cSRobert Watson } 2551ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2552ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2553ad71fe3cSRobert Watson comma = 1; 2554ad71fe3cSRobert Watson } 2555ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2556ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2557ad71fe3cSRobert Watson comma = 1; 2558ad71fe3cSRobert Watson } 2559497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2560497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2561497057eeSRobert Watson comma = 1; 2562497057eeSRobert Watson } 2563497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2564497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2565497057eeSRobert Watson comma = 1; 2566497057eeSRobert Watson } 2567497057eeSRobert Watson } 2568497057eeSRobert Watson 2569497057eeSRobert Watson static void 2570497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2571497057eeSRobert Watson { 2572497057eeSRobert Watson int comma; 2573497057eeSRobert Watson 2574497057eeSRobert Watson comma = 0; 2575497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2576497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2577497057eeSRobert Watson comma = 1; 2578497057eeSRobert Watson } 2579497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2580497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2581497057eeSRobert Watson comma = 1; 2582497057eeSRobert Watson } 2583497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2584497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2585497057eeSRobert Watson comma = 1; 2586497057eeSRobert Watson } 2587497057eeSRobert Watson } 2588497057eeSRobert Watson 25896d888973SRobert Watson static void 2590497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2591497057eeSRobert Watson { 2592497057eeSRobert Watson 2593497057eeSRobert Watson db_print_indent(indent); 2594497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2595497057eeSRobert Watson 2596497057eeSRobert Watson indent += 2; 2597497057eeSRobert Watson 2598497057eeSRobert Watson db_print_indent(indent); 2599497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2600497057eeSRobert Watson 2601497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2602497057eeSRobert Watson 2603497057eeSRobert Watson db_print_indent(indent); 2604497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2605497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2606497057eeSRobert Watson 2607497057eeSRobert Watson db_print_indent(indent); 2608497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2609497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2610497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2611497057eeSRobert Watson db_printf(")\n"); 2612497057eeSRobert Watson 2613497057eeSRobert Watson db_print_indent(indent); 2614497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2615497057eeSRobert Watson inp->inp_vflag); 2616497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2617497057eeSRobert Watson db_printf(")\n"); 2618497057eeSRobert Watson 2619497057eeSRobert Watson db_print_indent(indent); 2620497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2621497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2622497057eeSRobert Watson 2623497057eeSRobert Watson db_print_indent(indent); 2624497057eeSRobert Watson #ifdef INET6 2625497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2626497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2627497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2628497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2629497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2630497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2631497057eeSRobert Watson inp->in6p_hops); 2632497057eeSRobert Watson } else 2633497057eeSRobert Watson #endif 2634497057eeSRobert Watson { 2635497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2636497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2637497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2638497057eeSRobert Watson } 2639497057eeSRobert Watson 2640497057eeSRobert Watson db_print_indent(indent); 2641497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2642497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 2643497057eeSRobert Watson } 2644497057eeSRobert Watson 2645497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2646497057eeSRobert Watson { 2647497057eeSRobert Watson struct inpcb *inp; 2648497057eeSRobert Watson 2649497057eeSRobert Watson if (!have_addr) { 2650497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 2651497057eeSRobert Watson return; 2652497057eeSRobert Watson } 2653497057eeSRobert Watson inp = (struct inpcb *)addr; 2654497057eeSRobert Watson 2655497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 2656497057eeSRobert Watson } 265783e521ecSBjoern A. Zeeb #endif /* DDB */ 2658