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> 50df8bae1dSRodney W. Grimes #include <sys/malloc.h> 51df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 52aae49dd3SBjoern A. Zeeb #include <sys/callout.h> 53cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 54df8bae1dSRodney W. Grimes #include <sys/protosw.h> 55df8bae1dSRodney W. Grimes #include <sys/socket.h> 56df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 57acd3428bSRobert Watson #include <sys/priv.h> 58df8bae1dSRodney W. Grimes #include <sys/proc.h> 5979bdc6e5SRobert Watson #include <sys/refcount.h> 6075c13541SPoul-Henning Kamp #include <sys/jail.h> 61101f9fc8SPeter Wemm #include <sys/kernel.h> 62101f9fc8SPeter Wemm #include <sys/sysctl.h> 638781d8e9SBruce Evans 64497057eeSRobert Watson #ifdef DDB 65497057eeSRobert Watson #include <ddb/ddb.h> 66497057eeSRobert Watson #endif 67497057eeSRobert Watson 6869c2d429SJeff Roberson #include <vm/uma.h> 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes #include <net/if.h> 7176039bc8SGleb Smirnoff #include <net/if_var.h> 72cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 73df8bae1dSRodney W. Grimes #include <net/route.h> 74530c0060SRobert Watson #include <net/vnet.h> 75df8bae1dSRodney W. Grimes 7667107f45SBjoern A. Zeeb #if defined(INET) || defined(INET6) 77df8bae1dSRodney W. Grimes #include <netinet/in.h> 78df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 797527624eSRobert Watson #include <netinet/in_rss.h> 80df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 81340c35deSJonathan Lemon #include <netinet/tcp_var.h> 825f311da2SMike Silbersack #include <netinet/udp.h> 835f311da2SMike Silbersack #include <netinet/udp_var.h> 8467107f45SBjoern A. Zeeb #endif 8567107f45SBjoern A. Zeeb #ifdef INET 8667107f45SBjoern A. Zeeb #include <netinet/in_var.h> 8767107f45SBjoern A. Zeeb #endif 88cfa1ca9dSYoshinobu Inoue #ifdef INET6 89cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 90efc76f72SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 9167107f45SBjoern A. Zeeb #include <netinet6/in6_var.h> 9267107f45SBjoern A. Zeeb #include <netinet6/ip6_var.h> 93cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 94cfa1ca9dSYoshinobu Inoue 95df8bae1dSRodney W. Grimes 96b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 97b9234fafSSam Leffler #include <netipsec/ipsec.h> 98b9234fafSSam Leffler #include <netipsec/key.h> 99b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 100b9234fafSSam Leffler 101aed55708SRobert Watson #include <security/mac/mac_framework.h> 102aed55708SRobert Watson 103aae49dd3SBjoern A. Zeeb static struct callout ipport_tick_callout; 104aae49dd3SBjoern A. Zeeb 105101f9fc8SPeter Wemm /* 106101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 107101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 108101f9fc8SPeter Wemm */ 109eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 110eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 111eddfbb76SRobert Watson VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 112eddfbb76SRobert Watson VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 113eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 114eddfbb76SRobert Watson VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 115101f9fc8SPeter Wemm 116b0d22693SCrist J. Clark /* 117b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 118b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 119b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 120b0d22693SCrist J. Clark */ 121eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 122eddfbb76SRobert Watson VNET_DEFINE(int, ipport_reservedlow); 123b0d22693SCrist J. Clark 1245f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 125eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */ 126eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */ 127eddfbb76SRobert Watson VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */ 128eddfbb76SRobert Watson VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */ 129eddfbb76SRobert Watson VNET_DEFINE(int, ipport_tcpallocs); 1303e288e62SDimitry Andric static VNET_DEFINE(int, ipport_tcplastcount); 131eddfbb76SRobert Watson 1321e77c105SRobert Watson #define V_ipport_tcplastcount VNET(ipport_tcplastcount) 1336ac48b74SMike Silbersack 134d2025bd0SBjoern A. Zeeb static void in_pcbremlists(struct inpcb *inp); 135d2025bd0SBjoern A. Zeeb #ifdef INET 136fa046d87SRobert Watson static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 137fa046d87SRobert Watson struct in_addr faddr, u_int fport_arg, 138fa046d87SRobert Watson struct in_addr laddr, u_int lport_arg, 139fa046d87SRobert Watson int lookupflags, struct ifnet *ifp); 14067107f45SBjoern A. Zeeb 141bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 142bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 143bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 144bbd42ad0SPeter Wemm 145bbd42ad0SPeter Wemm static int 14682d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 147bbd42ad0SPeter Wemm { 14830a4ab08SBruce Evans int error; 14930a4ab08SBruce Evans 150f6dfe47aSMarko Zec error = sysctl_handle_int(oidp, arg1, arg2, req); 15130a4ab08SBruce Evans if (error == 0) { 152603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 153603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 154603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 155603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 156603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 157603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 158bbd42ad0SPeter Wemm } 15930a4ab08SBruce Evans return (error); 160bbd42ad0SPeter Wemm } 161bbd42ad0SPeter Wemm 162bbd42ad0SPeter Wemm #undef RANGECHK 163bbd42ad0SPeter Wemm 1646472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, 1656472ac3dSEd Schouten "IP Ports"); 16633b3ac06SPeter Wemm 167eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 168eddfbb76SRobert Watson CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0, 1698b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 170eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 171eddfbb76SRobert Watson CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0, 1728b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 173eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first, 174eddfbb76SRobert Watson CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0, 1758b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 176eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last, 177eddfbb76SRobert Watson CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0, 1788b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 179eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 180eddfbb76SRobert Watson CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0, 1818b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 182eddfbb76SRobert Watson SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 183eddfbb76SRobert Watson CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0, 1848b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 185eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 186eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, ""); 187eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 188eddfbb76SRobert Watson CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 189eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW, 190eddfbb76SRobert Watson &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 191eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW, 192eddfbb76SRobert Watson &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port " 1936ee79c59SMaxim Konovalov "allocations before switching to a sequental one"); 194eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW, 195eddfbb76SRobert Watson &VNET_NAME(ipport_randomtime), 0, 1968b615593SMarko Zec "Minimum time to keep sequental port " 1976ee79c59SMaxim Konovalov "allocation before switching to a random one"); 19883e521ecSBjoern A. Zeeb #endif /* INET */ 1990312fbe9SPoul-Henning Kamp 200c3229e05SDavid Greenman /* 201c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 202c3229e05SDavid Greenman * 203de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 204de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 205de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 206c3229e05SDavid Greenman */ 207c3229e05SDavid Greenman 208c3229e05SDavid Greenman /* 2099bcd427bSRobert Watson * Initialize an inpcbinfo -- we should be able to reduce the number of 2109bcd427bSRobert Watson * arguments in time. 2119bcd427bSRobert Watson */ 2129bcd427bSRobert Watson void 2139bcd427bSRobert Watson in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 2149bcd427bSRobert Watson struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 2159bcd427bSRobert Watson char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini, 21652cd27cbSRobert Watson uint32_t inpcbzone_flags, u_int hashfields) 2179bcd427bSRobert Watson { 2189bcd427bSRobert Watson 2199bcd427bSRobert Watson INP_INFO_LOCK_INIT(pcbinfo, name); 220fa046d87SRobert Watson INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 2219bcd427bSRobert Watson #ifdef VIMAGE 2229bcd427bSRobert Watson pcbinfo->ipi_vnet = curvnet; 2239bcd427bSRobert Watson #endif 2249bcd427bSRobert Watson pcbinfo->ipi_listhead = listhead; 2259bcd427bSRobert Watson LIST_INIT(pcbinfo->ipi_listhead); 226fa046d87SRobert Watson pcbinfo->ipi_count = 0; 2279bcd427bSRobert Watson pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 2289bcd427bSRobert Watson &pcbinfo->ipi_hashmask); 2299bcd427bSRobert Watson pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 2309bcd427bSRobert Watson &pcbinfo->ipi_porthashmask); 23152cd27cbSRobert Watson #ifdef PCBGROUP 23252cd27cbSRobert Watson in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 23352cd27cbSRobert Watson #endif 2349bcd427bSRobert Watson pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 2359bcd427bSRobert Watson NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR, 2369bcd427bSRobert Watson inpcbzone_flags); 2379bcd427bSRobert Watson uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 2386acd596eSPawel Jakub Dawidek uma_zone_set_warning(pcbinfo->ipi_zone, 2396acd596eSPawel Jakub Dawidek "kern.ipc.maxsockets limit reached"); 2409bcd427bSRobert Watson } 2419bcd427bSRobert Watson 2429bcd427bSRobert Watson /* 2439bcd427bSRobert Watson * Destroy an inpcbinfo. 2449bcd427bSRobert Watson */ 2459bcd427bSRobert Watson void 2469bcd427bSRobert Watson in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 2479bcd427bSRobert Watson { 2489bcd427bSRobert Watson 249fa046d87SRobert Watson KASSERT(pcbinfo->ipi_count == 0, 250fa046d87SRobert Watson ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 251fa046d87SRobert Watson 2529bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 2539bcd427bSRobert Watson hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 2549bcd427bSRobert Watson pcbinfo->ipi_porthashmask); 25552cd27cbSRobert Watson #ifdef PCBGROUP 25652cd27cbSRobert Watson in_pcbgroup_destroy(pcbinfo); 25752cd27cbSRobert Watson #endif 2589bcd427bSRobert Watson uma_zdestroy(pcbinfo->ipi_zone); 259fa046d87SRobert Watson INP_HASH_LOCK_DESTROY(pcbinfo); 2609bcd427bSRobert Watson INP_INFO_LOCK_DESTROY(pcbinfo); 2619bcd427bSRobert Watson } 2629bcd427bSRobert Watson 2639bcd427bSRobert Watson /* 264c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 265d915b280SStephan Uphoff * On success return with the PCB locked. 266c3229e05SDavid Greenman */ 267df8bae1dSRodney W. Grimes int 268d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 269df8bae1dSRodney W. Grimes { 270136d4f1cSRobert Watson struct inpcb *inp; 27113cf67f3SHajimu UMEMOTO int error; 272a557af22SRobert Watson 27359daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 274a557af22SRobert Watson error = 0; 275d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 276df8bae1dSRodney W. Grimes if (inp == NULL) 277df8bae1dSRodney W. Grimes return (ENOBUFS); 278d915b280SStephan Uphoff bzero(inp, inp_zero_size); 27915bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 280df8bae1dSRodney W. Grimes inp->inp_socket = so; 28186d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 2828b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 283a557af22SRobert Watson #ifdef MAC 28430d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 285a557af22SRobert Watson if (error != 0) 286a557af22SRobert Watson goto out; 28730d239bcSRobert Watson mac_inpcb_create(so, inp); 288a557af22SRobert Watson #endif 289b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 29013cf67f3SHajimu UMEMOTO error = ipsec_init_policy(so, &inp->inp_sp); 2910bffde27SRobert Watson if (error != 0) { 2920bffde27SRobert Watson #ifdef MAC 2930bffde27SRobert Watson mac_inpcb_destroy(inp); 2940bffde27SRobert Watson #endif 295a557af22SRobert Watson goto out; 2960bffde27SRobert Watson } 297b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 298e3fd5ffdSRobert Watson #ifdef INET6 299340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 300340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 301603724d3SBjoern A. Zeeb if (V_ip6_v6only) 30233841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 303340c35deSJonathan Lemon } 30475daea93SPaul Saab #endif 305712fc218SRobert Watson LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 3063d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 307df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 30833841545SHajimu UMEMOTO #ifdef INET6 309603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 31033841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 31133841545SHajimu UMEMOTO #endif 3128501a69cSRobert Watson INP_WLOCK(inp); 313d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 31479bdc6e5SRobert Watson refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 315b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC) 316a557af22SRobert Watson out: 31786d02c5cSBjoern A. Zeeb if (error != 0) { 31886d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 319a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 32086d02c5cSBjoern A. Zeeb } 321a557af22SRobert Watson #endif 322a557af22SRobert Watson return (error); 323df8bae1dSRodney W. Grimes } 324df8bae1dSRodney W. Grimes 32567107f45SBjoern A. Zeeb #ifdef INET 326df8bae1dSRodney W. Grimes int 327136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 328df8bae1dSRodney W. Grimes { 3294b932371SIan Dowse int anonport, error; 3304b932371SIan Dowse 3318501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 332fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 33359daba27SSam Leffler 3344b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 3354b932371SIan Dowse return (EINVAL); 3365cd3dcaaSNavdeep Parhar anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 3374b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 338b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 3394b932371SIan Dowse if (error) 3404b932371SIan Dowse return (error); 3414b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 3424b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 3434b932371SIan Dowse inp->inp_lport = 0; 3444b932371SIan Dowse return (EAGAIN); 3454b932371SIan Dowse } 3464b932371SIan Dowse if (anonport) 3474b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 3484b932371SIan Dowse return (0); 3494b932371SIan Dowse } 35067107f45SBjoern A. Zeeb #endif 3514b932371SIan Dowse 35239c8c62eSHiren Panchasara /* 35339c8c62eSHiren Panchasara * Select a local port (number) to use. 35439c8c62eSHiren Panchasara */ 355efc76f72SBjoern A. Zeeb #if defined(INET) || defined(INET6) 356efc76f72SBjoern A. Zeeb int 357efc76f72SBjoern A. Zeeb in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 35868e0d7e0SRobert Watson struct ucred *cred, int lookupflags) 359efc76f72SBjoern A. Zeeb { 360efc76f72SBjoern A. Zeeb struct inpcbinfo *pcbinfo; 361efc76f72SBjoern A. Zeeb struct inpcb *tmpinp; 362efc76f72SBjoern A. Zeeb unsigned short *lastport; 363efc76f72SBjoern A. Zeeb int count, dorandom, error; 364efc76f72SBjoern A. Zeeb u_short aux, first, last, lport; 365efc76f72SBjoern A. Zeeb #ifdef INET 366efc76f72SBjoern A. Zeeb struct in_addr laddr; 367efc76f72SBjoern A. Zeeb #endif 368efc76f72SBjoern A. Zeeb 369efc76f72SBjoern A. Zeeb pcbinfo = inp->inp_pcbinfo; 370efc76f72SBjoern A. Zeeb 371efc76f72SBjoern A. Zeeb /* 372efc76f72SBjoern A. Zeeb * Because no actual state changes occur here, a global write lock on 373efc76f72SBjoern A. Zeeb * the pcbinfo isn't required. 374efc76f72SBjoern A. Zeeb */ 375efc76f72SBjoern A. Zeeb INP_LOCK_ASSERT(inp); 376fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 377efc76f72SBjoern A. Zeeb 378efc76f72SBjoern A. Zeeb if (inp->inp_flags & INP_HIGHPORT) { 379efc76f72SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 380efc76f72SBjoern A. Zeeb last = V_ipport_hilastauto; 381efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lasthi; 382efc76f72SBjoern A. Zeeb } else if (inp->inp_flags & INP_LOWPORT) { 383efc76f72SBjoern A. Zeeb error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0); 384efc76f72SBjoern A. Zeeb if (error) 385efc76f72SBjoern A. Zeeb return (error); 386efc76f72SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 387efc76f72SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 388efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastlow; 389efc76f72SBjoern A. Zeeb } else { 390efc76f72SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 391efc76f72SBjoern A. Zeeb last = V_ipport_lastauto; 392efc76f72SBjoern A. Zeeb lastport = &pcbinfo->ipi_lastport; 393efc76f72SBjoern A. Zeeb } 394efc76f72SBjoern A. Zeeb /* 395e06e816fSKevin Lo * For UDP(-Lite), use random port allocation as long as the user 396efc76f72SBjoern A. Zeeb * allows it. For TCP (and as of yet unknown) connections, 397efc76f72SBjoern A. Zeeb * use random port allocation only if the user allows it AND 398efc76f72SBjoern A. Zeeb * ipport_tick() allows it. 399efc76f72SBjoern A. Zeeb */ 400efc76f72SBjoern A. Zeeb if (V_ipport_randomized && 401e06e816fSKevin Lo (!V_ipport_stoprandom || pcbinfo == &V_udbinfo || 402e06e816fSKevin Lo pcbinfo == &V_ulitecbinfo)) 403efc76f72SBjoern A. Zeeb dorandom = 1; 404efc76f72SBjoern A. Zeeb else 405efc76f72SBjoern A. Zeeb dorandom = 0; 406efc76f72SBjoern A. Zeeb /* 407efc76f72SBjoern A. Zeeb * It makes no sense to do random port allocation if 408efc76f72SBjoern A. Zeeb * we have the only port available. 409efc76f72SBjoern A. Zeeb */ 410efc76f72SBjoern A. Zeeb if (first == last) 411efc76f72SBjoern A. Zeeb dorandom = 0; 412e06e816fSKevin Lo /* Make sure to not include UDP(-Lite) packets in the count. */ 413e06e816fSKevin Lo if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo) 414efc76f72SBjoern A. Zeeb V_ipport_tcpallocs++; 415efc76f72SBjoern A. Zeeb /* 416efc76f72SBjoern A. Zeeb * Instead of having two loops further down counting up or down 417efc76f72SBjoern A. Zeeb * make sure that first is always <= last and go with only one 418efc76f72SBjoern A. Zeeb * code path implementing all logic. 419efc76f72SBjoern A. Zeeb */ 420efc76f72SBjoern A. Zeeb if (first > last) { 421efc76f72SBjoern A. Zeeb aux = first; 422efc76f72SBjoern A. Zeeb first = last; 423efc76f72SBjoern A. Zeeb last = aux; 424efc76f72SBjoern A. Zeeb } 425efc76f72SBjoern A. Zeeb 426efc76f72SBjoern A. Zeeb #ifdef INET 427efc76f72SBjoern A. Zeeb /* Make the compiler happy. */ 428efc76f72SBjoern A. Zeeb laddr.s_addr = 0; 4294d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 430efc76f72SBjoern A. Zeeb KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 431efc76f72SBjoern A. Zeeb __func__, inp)); 432efc76f72SBjoern A. Zeeb laddr = *laddrp; 433efc76f72SBjoern A. Zeeb } 434efc76f72SBjoern A. Zeeb #endif 43567107f45SBjoern A. Zeeb tmpinp = NULL; /* Make compiler happy. */ 436efc76f72SBjoern A. Zeeb lport = *lportp; 437efc76f72SBjoern A. Zeeb 438efc76f72SBjoern A. Zeeb if (dorandom) 439efc76f72SBjoern A. Zeeb *lastport = first + (arc4random() % (last - first)); 440efc76f72SBjoern A. Zeeb 441efc76f72SBjoern A. Zeeb count = last - first; 442efc76f72SBjoern A. Zeeb 443efc76f72SBjoern A. Zeeb do { 444efc76f72SBjoern A. Zeeb if (count-- < 0) /* completely used? */ 445efc76f72SBjoern A. Zeeb return (EADDRNOTAVAIL); 446efc76f72SBjoern A. Zeeb ++*lastport; 447efc76f72SBjoern A. Zeeb if (*lastport < first || *lastport > last) 448efc76f72SBjoern A. Zeeb *lastport = first; 449efc76f72SBjoern A. Zeeb lport = htons(*lastport); 450efc76f72SBjoern A. Zeeb 451efc76f72SBjoern A. Zeeb #ifdef INET6 452efc76f72SBjoern A. Zeeb if ((inp->inp_vflag & INP_IPV6) != 0) 453efc76f72SBjoern A. Zeeb tmpinp = in6_pcblookup_local(pcbinfo, 45468e0d7e0SRobert Watson &inp->in6p_laddr, lport, lookupflags, cred); 455efc76f72SBjoern A. Zeeb #endif 456efc76f72SBjoern A. Zeeb #if defined(INET) && defined(INET6) 457efc76f72SBjoern A. Zeeb else 458efc76f72SBjoern A. Zeeb #endif 459efc76f72SBjoern A. Zeeb #ifdef INET 460efc76f72SBjoern A. Zeeb tmpinp = in_pcblookup_local(pcbinfo, laddr, 46168e0d7e0SRobert Watson lport, lookupflags, cred); 462efc76f72SBjoern A. Zeeb #endif 463efc76f72SBjoern A. Zeeb } while (tmpinp != NULL); 464efc76f72SBjoern A. Zeeb 465efc76f72SBjoern A. Zeeb #ifdef INET 4664d457387SBjoern A. Zeeb if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 467efc76f72SBjoern A. Zeeb laddrp->s_addr = laddr.s_addr; 468efc76f72SBjoern A. Zeeb #endif 469efc76f72SBjoern A. Zeeb *lportp = lport; 470efc76f72SBjoern A. Zeeb 471efc76f72SBjoern A. Zeeb return (0); 472efc76f72SBjoern A. Zeeb } 473efdf104bSMikolaj Golub 474efdf104bSMikolaj Golub /* 475efdf104bSMikolaj Golub * Return cached socket options. 476efdf104bSMikolaj Golub */ 477efdf104bSMikolaj Golub short 478efdf104bSMikolaj Golub inp_so_options(const struct inpcb *inp) 479efdf104bSMikolaj Golub { 480efdf104bSMikolaj Golub short so_options; 481efdf104bSMikolaj Golub 482efdf104bSMikolaj Golub so_options = 0; 483efdf104bSMikolaj Golub 484efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 485efdf104bSMikolaj Golub so_options |= SO_REUSEPORT; 486efdf104bSMikolaj Golub if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 487efdf104bSMikolaj Golub so_options |= SO_REUSEADDR; 488efdf104bSMikolaj Golub return (so_options); 489efdf104bSMikolaj Golub } 490efc76f72SBjoern A. Zeeb #endif /* INET || INET6 */ 491efc76f72SBjoern A. Zeeb 4924b932371SIan Dowse /* 4930a100a6fSAdrian Chadd * Check if a new BINDMULTI socket is allowed to be created. 4940a100a6fSAdrian Chadd * 4950a100a6fSAdrian Chadd * ni points to the new inp. 4960a100a6fSAdrian Chadd * oi points to the exisitng inp. 4970a100a6fSAdrian Chadd * 4980a100a6fSAdrian Chadd * This checks whether the existing inp also has BINDMULTI and 4990a100a6fSAdrian Chadd * whether the credentials match. 5000a100a6fSAdrian Chadd */ 501d5bb8bd3SAdrian Chadd int 5020a100a6fSAdrian Chadd in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi) 5030a100a6fSAdrian Chadd { 5040a100a6fSAdrian Chadd /* Check permissions match */ 5050a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5060a100a6fSAdrian Chadd (ni->inp_cred->cr_uid != 5070a100a6fSAdrian Chadd oi->inp_cred->cr_uid)) 5080a100a6fSAdrian Chadd return (0); 5090a100a6fSAdrian Chadd 5100a100a6fSAdrian Chadd /* Check the existing inp has BINDMULTI set */ 5110a100a6fSAdrian Chadd if ((ni->inp_flags2 & INP_BINDMULTI) && 5120a100a6fSAdrian Chadd ((oi->inp_flags2 & INP_BINDMULTI) == 0)) 5130a100a6fSAdrian Chadd return (0); 5140a100a6fSAdrian Chadd 5150a100a6fSAdrian Chadd /* 5160a100a6fSAdrian Chadd * We're okay - either INP_BINDMULTI isn't set on ni, or 5170a100a6fSAdrian Chadd * it is and it matches the checks. 5180a100a6fSAdrian Chadd */ 5190a100a6fSAdrian Chadd return (1); 5200a100a6fSAdrian Chadd } 5210a100a6fSAdrian Chadd 522d5bb8bd3SAdrian Chadd #ifdef INET 5230a100a6fSAdrian Chadd /* 5244b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 5254b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 5264b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 5274b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 5284b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 5294b932371SIan Dowse * 5304b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 5314b932371SIan Dowse */ 5324b932371SIan Dowse int 533136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 534136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 5354b932371SIan Dowse { 5364b932371SIan Dowse struct socket *so = inp->inp_socket; 53715bd2b43SDavid Greenman struct sockaddr_in *sin; 538c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 5394b932371SIan Dowse struct in_addr laddr; 540df8bae1dSRodney W. Grimes u_short lport = 0; 54168e0d7e0SRobert Watson int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 542413628a7SBjoern A. Zeeb int error; 543df8bae1dSRodney W. Grimes 5448501a69cSRobert Watson /* 545fa046d87SRobert Watson * No state changes, so read locks are sufficient here. 5468501a69cSRobert Watson */ 54759daba27SSam Leffler INP_LOCK_ASSERT(inp); 548fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 54959daba27SSam Leffler 550603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 551df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 5524b932371SIan Dowse laddr.s_addr = *laddrp; 5534b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 554df8bae1dSRodney W. Grimes return (EINVAL); 555c3229e05SDavid Greenman if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 55668e0d7e0SRobert Watson lookupflags = INPLOOKUP_WILDCARD; 5577c2f3cb9SJamie Gritton if (nam == NULL) { 5587c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 5597c2f3cb9SJamie Gritton return (error); 5607c2f3cb9SJamie Gritton } else { 56157bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 56257bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 563df8bae1dSRodney W. Grimes return (EINVAL); 564df8bae1dSRodney W. Grimes #ifdef notdef 565df8bae1dSRodney W. Grimes /* 566df8bae1dSRodney W. Grimes * We should check the family, but old programs 567df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 568df8bae1dSRodney W. Grimes */ 569df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 570df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 571df8bae1dSRodney W. Grimes #endif 572b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 573b89e82ddSJamie Gritton if (error) 574b89e82ddSJamie Gritton return (error); 5754b932371SIan Dowse if (sin->sin_port != *lportp) { 5764b932371SIan Dowse /* Don't allow the port to change. */ 5774b932371SIan Dowse if (*lportp != 0) 5784b932371SIan Dowse return (EINVAL); 579df8bae1dSRodney W. Grimes lport = sin->sin_port; 5804b932371SIan Dowse } 5814b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 582df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 583df8bae1dSRodney W. Grimes /* 584df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 585df8bae1dSRodney W. Grimes * allow complete duplication of binding if 586df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 587df8bae1dSRodney W. Grimes * and a multicast address is bound on both 588df8bae1dSRodney W. Grimes * new and duplicated sockets. 589df8bae1dSRodney W. Grimes */ 590f122b319SMikolaj Golub if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 591df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 592df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 593df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 59483103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 5954209e01aSAdrian Chadd /* 5964209e01aSAdrian Chadd * Is the address a local IP address? 597f44270e7SPawel Jakub Dawidek * If INP_BINDANY is set, then the socket may be bound 5988696873dSAdrian Chadd * to any endpoint address, local or not. 5994209e01aSAdrian Chadd */ 600f44270e7SPawel Jakub Dawidek if ((inp->inp_flags & INP_BINDANY) == 0 && 6018896f83aSRobert Watson ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 602df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 603df8bae1dSRodney W. Grimes } 6044b932371SIan Dowse laddr = sin->sin_addr; 605df8bae1dSRodney W. Grimes if (lport) { 606df8bae1dSRodney W. Grimes struct inpcb *t; 607ae0e7143SRobert Watson struct tcptw *tw; 608ae0e7143SRobert Watson 609df8bae1dSRodney W. Grimes /* GROSS */ 610603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 611603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 612acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 61332f9753cSRobert Watson 0)) 6142469dd60SGarrett Wollman return (EACCES); 615835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 61686d02c5cSBjoern A. Zeeb priv_check_cred(inp->inp_cred, 61732f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 618078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 619413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 620340c35deSJonathan Lemon /* 621340c35deSJonathan Lemon * XXX 622340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 623340c35deSJonathan Lemon */ 6244cc20ab1SSeigo Tanimura if (t && 6250a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 626ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 6274658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 6284658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 6294cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 63052b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 631fc06cd42SMikolaj Golub (t->inp_flags2 & INP_REUSEPORT) == 0) && 63286d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 63386d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 6344049a042SGuido van Rooij return (EADDRINUSE); 6350a100a6fSAdrian Chadd 6360a100a6fSAdrian Chadd /* 6370a100a6fSAdrian Chadd * If the socket is a BINDMULTI socket, then 6380a100a6fSAdrian Chadd * the credentials need to match and the 6390a100a6fSAdrian Chadd * original socket also has to have been bound 6400a100a6fSAdrian Chadd * with BINDMULTI. 6410a100a6fSAdrian Chadd */ 6420a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 6430a100a6fSAdrian Chadd return (EADDRINUSE); 6444049a042SGuido van Rooij } 645c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 64668e0d7e0SRobert Watson lport, lookupflags, cred); 647ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 648ae0e7143SRobert Watson /* 649ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 650ae0e7143SRobert Watson * state recycled, we treat the address as 651ae0e7143SRobert Watson * being in use (for now). This is better 652ae0e7143SRobert Watson * than a panic, but not desirable. 653ae0e7143SRobert Watson */ 654ec95b709SMikolaj Golub tw = intotw(t); 655ae0e7143SRobert Watson if (tw == NULL || 656ae0e7143SRobert Watson (reuseport & tw->tw_so_options) == 0) 657340c35deSJonathan Lemon return (EADDRINUSE); 6580a100a6fSAdrian Chadd } else if (t && 6590a100a6fSAdrian Chadd ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 6600a100a6fSAdrian Chadd (reuseport & inp_so_options(t)) == 0) { 661e3fd5ffdSRobert Watson #ifdef INET6 66233841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 663cfa1ca9dSYoshinobu Inoue INADDR_ANY || 664cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 665cfa1ca9dSYoshinobu Inoue INADDR_ANY || 666fc06cd42SMikolaj Golub (inp->inp_vflag & INP_IPV6PROTO) == 0 || 667fc06cd42SMikolaj Golub (t->inp_vflag & INP_IPV6PROTO) == 0) 668e3fd5ffdSRobert Watson #endif 669df8bae1dSRodney W. Grimes return (EADDRINUSE); 6700a100a6fSAdrian Chadd if (t && (! in_pcbbind_check_bindmulti(inp, t))) 6710a100a6fSAdrian Chadd return (EADDRINUSE); 672df8bae1dSRodney W. Grimes } 673cfa1ca9dSYoshinobu Inoue } 674df8bae1dSRodney W. Grimes } 6754b932371SIan Dowse if (*lportp != 0) 6764b932371SIan Dowse lport = *lportp; 67733b3ac06SPeter Wemm if (lport == 0) { 67868e0d7e0SRobert Watson error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 679efc76f72SBjoern A. Zeeb if (error != 0) 680efc76f72SBjoern A. Zeeb return (error); 68133b3ac06SPeter Wemm 68233b3ac06SPeter Wemm } 6834b932371SIan Dowse *laddrp = laddr.s_addr; 6844b932371SIan Dowse *lportp = lport; 685df8bae1dSRodney W. Grimes return (0); 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes 688999f1343SGarrett Wollman /* 6895200e00eSIan Dowse * Connect from a socket to a specified address. 6905200e00eSIan Dowse * Both address and port must be specified in argument sin. 6915200e00eSIan Dowse * If don't have a local address for this socket yet, 6925200e00eSIan Dowse * then pick one. 693999f1343SGarrett Wollman */ 694999f1343SGarrett Wollman int 695d3c1f003SRobert Watson in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 696d3c1f003SRobert Watson struct ucred *cred, struct mbuf *m) 697999f1343SGarrett Wollman { 6985200e00eSIan Dowse u_short lport, fport; 6995200e00eSIan Dowse in_addr_t laddr, faddr; 7005200e00eSIan Dowse int anonport, error; 701df8bae1dSRodney W. Grimes 7028501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 703fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 70427f74fd0SRobert Watson 7055200e00eSIan Dowse lport = inp->inp_lport; 7065200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 7075200e00eSIan Dowse anonport = (lport == 0); 7085200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 709b0330ed9SPawel Jakub Dawidek NULL, cred); 7105200e00eSIan Dowse if (error) 7115200e00eSIan Dowse return (error); 7125200e00eSIan Dowse 7135200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 7145200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 7155200e00eSIan Dowse inp->inp_lport = lport; 7165200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7175200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 7185200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 7195200e00eSIan Dowse inp->inp_lport = 0; 7205200e00eSIan Dowse return (EAGAIN); 7215200e00eSIan Dowse } 7225200e00eSIan Dowse } 7235200e00eSIan Dowse 7245200e00eSIan Dowse /* Commit the remaining changes. */ 7255200e00eSIan Dowse inp->inp_lport = lport; 7265200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 7275200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 7285200e00eSIan Dowse inp->inp_fport = fport; 729d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, m); 7302cb64cb2SGeorge V. Neville-Neil 7315200e00eSIan Dowse if (anonport) 7325200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 7335200e00eSIan Dowse return (0); 7345200e00eSIan Dowse } 7355200e00eSIan Dowse 736d3c1f003SRobert Watson int 737d3c1f003SRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 738d3c1f003SRobert Watson { 739d3c1f003SRobert Watson 740d3c1f003SRobert Watson return (in_pcbconnect_mbuf(inp, nam, cred, NULL)); 741d3c1f003SRobert Watson } 742d3c1f003SRobert Watson 7435200e00eSIan Dowse /* 7440895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 7450895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 7460895aec3SBjoern A. Zeeb */ 747ae190832SSteven Hartland int 7480895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 7490895aec3SBjoern A. Zeeb struct ucred *cred) 7500895aec3SBjoern A. Zeeb { 7510895aec3SBjoern A. Zeeb struct ifaddr *ifa; 7520895aec3SBjoern A. Zeeb struct sockaddr *sa; 7530895aec3SBjoern A. Zeeb struct sockaddr_in *sin; 7540895aec3SBjoern A. Zeeb struct route sro; 7550895aec3SBjoern A. Zeeb int error; 7560895aec3SBjoern A. Zeeb 757413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 7580895aec3SBjoern A. Zeeb 759592bcae8SBjoern A. Zeeb /* 760592bcae8SBjoern A. Zeeb * Bypass source address selection and use the primary jail IP 761592bcae8SBjoern A. Zeeb * if requested. 762592bcae8SBjoern A. Zeeb */ 763592bcae8SBjoern A. Zeeb if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 764592bcae8SBjoern A. Zeeb return (0); 765592bcae8SBjoern A. Zeeb 7660895aec3SBjoern A. Zeeb error = 0; 7670895aec3SBjoern A. Zeeb bzero(&sro, sizeof(sro)); 7680895aec3SBjoern A. Zeeb 7690895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)&sro.ro_dst; 7700895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 7710895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 7720895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 7730895aec3SBjoern A. Zeeb 7740895aec3SBjoern A. Zeeb /* 7750895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 7760895aec3SBjoern A. Zeeb * else punt. 7770895aec3SBjoern A. Zeeb * 7780895aec3SBjoern A. Zeeb * Find out route to destination. 7790895aec3SBjoern A. Zeeb */ 7800895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 7816e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 7820895aec3SBjoern A. Zeeb 7830895aec3SBjoern A. Zeeb /* 7840895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 7850895aec3SBjoern A. Zeeb * the outgoing interface. 7860895aec3SBjoern A. Zeeb * 7870895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 7880895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 7890895aec3SBjoern A. Zeeb * the source address from. 7900895aec3SBjoern A. Zeeb */ 7910895aec3SBjoern A. Zeeb if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 7928c0fec80SRobert Watson struct in_ifaddr *ia; 7930895aec3SBjoern A. Zeeb struct ifnet *ifp; 7940895aec3SBjoern A. Zeeb 795*4f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 796*4f8585e0SAlan Somers RT_ALL_FIBS)); 7970895aec3SBjoern A. Zeeb if (ia == NULL) 798*4f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 799*4f8585e0SAlan Somers RT_ALL_FIBS)); 8000895aec3SBjoern A. Zeeb if (ia == NULL) { 8010895aec3SBjoern A. Zeeb error = ENETUNREACH; 8020895aec3SBjoern A. Zeeb goto done; 8030895aec3SBjoern A. Zeeb } 8040895aec3SBjoern A. Zeeb 8050304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8060895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8078c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8080895aec3SBjoern A. Zeeb goto done; 8090895aec3SBjoern A. Zeeb } 8100895aec3SBjoern A. Zeeb 8110895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 8128c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 8130895aec3SBjoern A. Zeeb ia = NULL; 814137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 8150895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8160895aec3SBjoern A. Zeeb 8170895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 8180895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 8190895aec3SBjoern A. Zeeb continue; 8200895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 821b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8220895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 8230895aec3SBjoern A. Zeeb break; 8240895aec3SBjoern A. Zeeb } 8250895aec3SBjoern A. Zeeb } 8260895aec3SBjoern A. Zeeb if (ia != NULL) { 8270895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 828137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8290895aec3SBjoern A. Zeeb goto done; 8300895aec3SBjoern A. Zeeb } 831137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8320895aec3SBjoern A. Zeeb 8330895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 834b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 8350895aec3SBjoern A. Zeeb goto done; 8360895aec3SBjoern A. Zeeb } 8370895aec3SBjoern A. Zeeb 8380895aec3SBjoern A. Zeeb /* 8390895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 8400895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 8410895aec3SBjoern A. Zeeb * In case of jails do those three steps: 8420895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 8430895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 8440895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 8450895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 8460895aec3SBjoern A. Zeeb */ 8470895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 8488c0fec80SRobert Watson struct in_ifaddr *ia; 8499317b04eSRobert Watson struct ifnet *ifp; 8500895aec3SBjoern A. Zeeb 8510895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 8520304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 8530895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8540895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8550895aec3SBjoern A. Zeeb goto done; 8560895aec3SBjoern A. Zeeb } 8570895aec3SBjoern A. Zeeb 8580895aec3SBjoern A. Zeeb /* Jailed. */ 8590895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 8600895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 861b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8620895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 8630895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 8640895aec3SBjoern A. Zeeb goto done; 8650895aec3SBjoern A. Zeeb } 8660895aec3SBjoern A. Zeeb 8670895aec3SBjoern A. Zeeb /* 8680895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 8690895aec3SBjoern A. Zeeb * belonging to this jail. 8700895aec3SBjoern A. Zeeb */ 8718c0fec80SRobert Watson ia = NULL; 8729317b04eSRobert Watson ifp = sro.ro_rt->rt_ifp; 873137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 8749317b04eSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8750895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 8760895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 8770895aec3SBjoern A. Zeeb continue; 8780895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 879b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 8800895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 8810895aec3SBjoern A. Zeeb break; 8820895aec3SBjoern A. Zeeb } 8830895aec3SBjoern A. Zeeb } 8840895aec3SBjoern A. Zeeb if (ia != NULL) { 8850895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 886137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8870895aec3SBjoern A. Zeeb goto done; 8880895aec3SBjoern A. Zeeb } 889137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 8900895aec3SBjoern A. Zeeb 8910895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 892b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 8930895aec3SBjoern A. Zeeb goto done; 8940895aec3SBjoern A. Zeeb } 8950895aec3SBjoern A. Zeeb 8960895aec3SBjoern A. Zeeb /* 8970895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 8980895aec3SBjoern A. Zeeb * to ourselves is here. 8990895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 9000895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 9010895aec3SBjoern A. Zeeb * a loopback interface. 9020895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 9030895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 9040895aec3SBjoern A. Zeeb */ 9050895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 9060895aec3SBjoern A. Zeeb struct sockaddr_in sain; 9078c0fec80SRobert Watson struct in_ifaddr *ia; 9080895aec3SBjoern A. Zeeb 9090895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 9100895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 9110895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 9120895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 9130895aec3SBjoern A. Zeeb 914*4f8585e0SAlan Somers ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain), RT_ALL_FIBS)); 9150895aec3SBjoern A. Zeeb if (ia == NULL) 916*4f8585e0SAlan Somers ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0, 917*4f8585e0SAlan Somers RT_ALL_FIBS)); 918f0bb05fcSQing Li if (ia == NULL) 919f0bb05fcSQing Li ia = ifatoia(ifa_ifwithaddr(sintosa(&sain))); 9200895aec3SBjoern A. Zeeb 9210304c731SJamie Gritton if (cred == NULL || !prison_flag(cred, PR_IP4)) { 9220895aec3SBjoern A. Zeeb if (ia == NULL) { 9230895aec3SBjoern A. Zeeb error = ENETUNREACH; 9240895aec3SBjoern A. Zeeb goto done; 9250895aec3SBjoern A. Zeeb } 9260895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 9278c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9280895aec3SBjoern A. Zeeb goto done; 9290895aec3SBjoern A. Zeeb } 9300895aec3SBjoern A. Zeeb 9310895aec3SBjoern A. Zeeb /* Jailed. */ 9320895aec3SBjoern A. Zeeb if (ia != NULL) { 9330895aec3SBjoern A. Zeeb struct ifnet *ifp; 9340895aec3SBjoern A. Zeeb 9350895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 9368c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 9370895aec3SBjoern A. Zeeb ia = NULL; 938137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 9390895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 9400895aec3SBjoern A. Zeeb 9410895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 9420895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 9430895aec3SBjoern A. Zeeb continue; 9440895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 945b89e82ddSJamie Gritton if (prison_check_ip4(cred, 946b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 9470895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 9480895aec3SBjoern A. Zeeb break; 9490895aec3SBjoern A. Zeeb } 9500895aec3SBjoern A. Zeeb } 9510895aec3SBjoern A. Zeeb if (ia != NULL) { 9520895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 953137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9540895aec3SBjoern A. Zeeb goto done; 9550895aec3SBjoern A. Zeeb } 956137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 9570895aec3SBjoern A. Zeeb } 9580895aec3SBjoern A. Zeeb 9590895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 960b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 9610895aec3SBjoern A. Zeeb goto done; 9620895aec3SBjoern A. Zeeb } 9630895aec3SBjoern A. Zeeb 9640895aec3SBjoern A. Zeeb done: 9650895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 9660895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 9670895aec3SBjoern A. Zeeb return (error); 9680895aec3SBjoern A. Zeeb } 9690895aec3SBjoern A. Zeeb 9700895aec3SBjoern A. Zeeb /* 9715200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 9725200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 9735200e00eSIan Dowse * address and port for the PCB; these are updated to the values 9745200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 9755200e00eSIan Dowse * the connect. 9765200e00eSIan Dowse * 9775200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 9785200e00eSIan Dowse * and port. These are not updated in the error case. 9795200e00eSIan Dowse * 9805200e00eSIan Dowse * If the operation fails because the connection already exists, 9815200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 9825200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 9835200e00eSIan Dowse * is set to NULL. 9845200e00eSIan Dowse */ 9855200e00eSIan Dowse int 986136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 987136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 988136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 9895200e00eSIan Dowse { 9905200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 9915200e00eSIan Dowse struct in_ifaddr *ia; 9925200e00eSIan Dowse struct inpcb *oinp; 993b89e82ddSJamie Gritton struct in_addr laddr, faddr; 9945200e00eSIan Dowse u_short lport, fport; 9955200e00eSIan Dowse int error; 9965200e00eSIan Dowse 9978501a69cSRobert Watson /* 9988501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 9998501a69cSRobert Watson * lock is sufficient. 10008501a69cSRobert Watson */ 100127f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 1002fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 100327f74fd0SRobert Watson 10045200e00eSIan Dowse if (oinpp != NULL) 10055200e00eSIan Dowse *oinpp = NULL; 100657bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 1007df8bae1dSRodney W. Grimes return (EINVAL); 1008df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 1009df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 1010df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 1011df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 10125200e00eSIan Dowse laddr.s_addr = *laddrp; 10135200e00eSIan Dowse lport = *lportp; 10145200e00eSIan Dowse faddr = sin->sin_addr; 10155200e00eSIan Dowse fport = sin->sin_port; 10160895aec3SBjoern A. Zeeb 1017603724d3SBjoern A. Zeeb if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 1018df8bae1dSRodney W. Grimes /* 1019df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 1020df8bae1dSRodney W. Grimes * use the primary local address. 1021df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 1022df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 1023df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 1024df8bae1dSRodney W. Grimes */ 1025413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 10262d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 1027413628a7SBjoern A. Zeeb faddr = 1028b89e82ddSJamie Gritton IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 10292d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 1030b89e82ddSJamie Gritton if (cred != NULL && 1031b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 1032b89e82ddSJamie Gritton return (error); 10332d9cfabaSRobert Watson } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 10342d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 10352d9cfabaSRobert Watson if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 10362d9cfabaSRobert Watson IFF_BROADCAST) 10375200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 1038603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 10392d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 10402d9cfabaSRobert Watson } 1041df8bae1dSRodney W. Grimes } 10425200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 1043d79fdd98SDaniel Eischen error = in_pcbladdr(inp, &faddr, &laddr, cred); 1044df8bae1dSRodney W. Grimes /* 1045df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 1046d79fdd98SDaniel Eischen * interface has been set as a multicast option, prefer the 1047df8bae1dSRodney W. Grimes * address of that interface as our source address. 1048df8bae1dSRodney W. Grimes */ 10495200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1050df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 1051df8bae1dSRodney W. Grimes struct ip_moptions *imo; 1052df8bae1dSRodney W. Grimes struct ifnet *ifp; 1053df8bae1dSRodney W. Grimes 1054df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 1055df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 1056df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 10572d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 1058e691be70SDaniel Eischen TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1059e691be70SDaniel Eischen if ((ia->ia_ifp == ifp) && 1060e691be70SDaniel Eischen (cred == NULL || 1061e691be70SDaniel Eischen prison_check_ip4(cred, 1062e691be70SDaniel Eischen &ia->ia_addr.sin_addr) == 0)) 1063df8bae1dSRodney W. Grimes break; 1064e691be70SDaniel Eischen } 1065e691be70SDaniel Eischen if (ia == NULL) 1066d79fdd98SDaniel Eischen error = EADDRNOTAVAIL; 1067e691be70SDaniel Eischen else { 10685200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 1069d79fdd98SDaniel Eischen error = 0; 1070999f1343SGarrett Wollman } 1071e691be70SDaniel Eischen IN_IFADDR_RUNLOCK(); 1072d79fdd98SDaniel Eischen } 1073d79fdd98SDaniel Eischen } 107404215ed2SRandall Stewart if (error) 107504215ed2SRandall Stewart return (error); 10760895aec3SBjoern A. Zeeb } 1077fa046d87SRobert Watson oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1078fa046d87SRobert Watson laddr, lport, 0, NULL); 10795200e00eSIan Dowse if (oinp != NULL) { 10805200e00eSIan Dowse if (oinpp != NULL) 10815200e00eSIan Dowse *oinpp = oinp; 1082df8bae1dSRodney W. Grimes return (EADDRINUSE); 1083c3229e05SDavid Greenman } 10845200e00eSIan Dowse if (lport == 0) { 1085b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1086b0330ed9SPawel Jakub Dawidek cred); 10875a903f8dSPierre Beyssac if (error) 10885a903f8dSPierre Beyssac return (error); 10895a903f8dSPierre Beyssac } 10905200e00eSIan Dowse *laddrp = laddr.s_addr; 10915200e00eSIan Dowse *lportp = lport; 10925200e00eSIan Dowse *faddrp = faddr.s_addr; 10935200e00eSIan Dowse *fportp = fport; 1094df8bae1dSRodney W. Grimes return (0); 1095df8bae1dSRodney W. Grimes } 1096df8bae1dSRodney W. Grimes 109726f9a767SRodney W. Grimes void 1098136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 1099df8bae1dSRodney W. Grimes { 11006b348152SRobert Watson 11018501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1102fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1103df8bae1dSRodney W. Grimes 1104df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 1105df8bae1dSRodney W. Grimes inp->inp_fport = 0; 110615bd2b43SDavid Greenman in_pcbrehash(inp); 1107df8bae1dSRodney W. Grimes } 110883e521ecSBjoern A. Zeeb #endif /* INET */ 1109df8bae1dSRodney W. Grimes 11104c7c478dSRobert Watson /* 111128696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1112c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 111328696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 111428696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 11154c7c478dSRobert Watson */ 111626f9a767SRodney W. Grimes void 1117136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 1118df8bae1dSRodney W. Grimes { 11194c7c478dSRobert Watson 1120a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1121c0a211c5SRobert Watson 11224c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 11234c7c478dSRobert Watson inp->inp_socket = NULL; 11244c7c478dSRobert Watson } 11254c7c478dSRobert Watson 1126c0a211c5SRobert Watson /* 112779bdc6e5SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 112879bdc6e5SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 112979bdc6e5SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 113052cd27cbSRobert Watson * but where the inpcb lock may already held, or when acquiring a reference 113152cd27cbSRobert Watson * via a pcbgroup. 113279bdc6e5SRobert Watson * 113379bdc6e5SRobert Watson * in_pcbref() should be used only to provide brief memory stability, and 113479bdc6e5SRobert Watson * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 113579bdc6e5SRobert Watson * garbage collect the inpcb if it has been in_pcbfree()'d from another 113679bdc6e5SRobert Watson * context. Until in_pcbrele() has returned that the inpcb is still valid, 113779bdc6e5SRobert Watson * lock and rele are the *only* safe operations that may be performed on the 113879bdc6e5SRobert Watson * inpcb. 113979bdc6e5SRobert Watson * 114079bdc6e5SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 114179bdc6e5SRobert Watson * connection's state may change, so the caller should be careful to 114279bdc6e5SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 114379bdc6e5SRobert Watson * using in_pcbrele(). 1144c0a211c5SRobert Watson */ 114579bdc6e5SRobert Watson void 114679bdc6e5SRobert Watson in_pcbref(struct inpcb *inp) 11474c7c478dSRobert Watson { 1148df8bae1dSRodney W. Grimes 114979bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 115079bdc6e5SRobert Watson 115179bdc6e5SRobert Watson refcount_acquire(&inp->inp_refcount); 115279bdc6e5SRobert Watson } 115379bdc6e5SRobert Watson 115479bdc6e5SRobert Watson /* 115579bdc6e5SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 115679bdc6e5SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 115779bdc6e5SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 115879bdc6e5SRobert Watson * valid, we return with the inpcb lock held. 115979bdc6e5SRobert Watson * 116079bdc6e5SRobert Watson * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 116179bdc6e5SRobert Watson * reference on an inpcb. Historically more work was done here (actually, in 116279bdc6e5SRobert Watson * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 116379bdc6e5SRobert Watson * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 116479bdc6e5SRobert Watson * about memory stability (and continued use of the write lock). 116579bdc6e5SRobert Watson */ 116679bdc6e5SRobert Watson int 116779bdc6e5SRobert Watson in_pcbrele_rlocked(struct inpcb *inp) 116879bdc6e5SRobert Watson { 116979bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 117079bdc6e5SRobert Watson 117179bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 117279bdc6e5SRobert Watson 117379bdc6e5SRobert Watson INP_RLOCK_ASSERT(inp); 117479bdc6e5SRobert Watson 1175df4e91d3SGleb Smirnoff if (refcount_release(&inp->inp_refcount) == 0) { 1176df4e91d3SGleb Smirnoff /* 1177df4e91d3SGleb Smirnoff * If the inpcb has been freed, let the caller know, even if 1178df4e91d3SGleb Smirnoff * this isn't the last reference. 1179df4e91d3SGleb Smirnoff */ 1180df4e91d3SGleb Smirnoff if (inp->inp_flags2 & INP_FREED) { 1181df4e91d3SGleb Smirnoff INP_RUNLOCK(inp); 1182df4e91d3SGleb Smirnoff return (1); 1183df4e91d3SGleb Smirnoff } 118479bdc6e5SRobert Watson return (0); 1185df4e91d3SGleb Smirnoff } 118679bdc6e5SRobert Watson 118779bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 118879bdc6e5SRobert Watson 118979bdc6e5SRobert Watson INP_RUNLOCK(inp); 119079bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 119179bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 119279bdc6e5SRobert Watson return (1); 119379bdc6e5SRobert Watson } 119479bdc6e5SRobert Watson 119579bdc6e5SRobert Watson int 119679bdc6e5SRobert Watson in_pcbrele_wlocked(struct inpcb *inp) 119779bdc6e5SRobert Watson { 119879bdc6e5SRobert Watson struct inpcbinfo *pcbinfo; 119979bdc6e5SRobert Watson 120079bdc6e5SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 120179bdc6e5SRobert Watson 120279bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 120379bdc6e5SRobert Watson 120479bdc6e5SRobert Watson if (refcount_release(&inp->inp_refcount) == 0) 120579bdc6e5SRobert Watson return (0); 120679bdc6e5SRobert Watson 120779bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 120879bdc6e5SRobert Watson 120979bdc6e5SRobert Watson INP_WUNLOCK(inp); 121079bdc6e5SRobert Watson pcbinfo = inp->inp_pcbinfo; 121179bdc6e5SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 121279bdc6e5SRobert Watson return (1); 121379bdc6e5SRobert Watson } 121479bdc6e5SRobert Watson 121579bdc6e5SRobert Watson /* 121679bdc6e5SRobert Watson * Temporary wrapper. 121779bdc6e5SRobert Watson */ 121879bdc6e5SRobert Watson int 121979bdc6e5SRobert Watson in_pcbrele(struct inpcb *inp) 122079bdc6e5SRobert Watson { 122179bdc6e5SRobert Watson 122279bdc6e5SRobert Watson return (in_pcbrele_wlocked(inp)); 122379bdc6e5SRobert Watson } 122479bdc6e5SRobert Watson 122579bdc6e5SRobert Watson /* 122679bdc6e5SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 122779bdc6e5SRobert Watson * reference count, which should occur only after the inpcb has been detached 122879bdc6e5SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 122979bdc6e5SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 123079bdc6e5SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 123179bdc6e5SRobert Watson * work, including removal from global lists, is done in this context, where 123279bdc6e5SRobert Watson * the pcbinfo lock is held. 123379bdc6e5SRobert Watson */ 123479bdc6e5SRobert Watson void 123579bdc6e5SRobert Watson in_pcbfree(struct inpcb *inp) 123679bdc6e5SRobert Watson { 123779bdc6e5SRobert Watson struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 123879bdc6e5SRobert Watson 123979bdc6e5SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 124079bdc6e5SRobert Watson 124179bdc6e5SRobert Watson INP_INFO_WLOCK_ASSERT(pcbinfo); 124279bdc6e5SRobert Watson INP_WLOCK_ASSERT(inp); 124379bdc6e5SRobert Watson 124479bdc6e5SRobert Watson /* XXXRW: Do as much as possible here. */ 1245b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 12466aee2fc5SBjoern A. Zeeb if (inp->inp_sp != NULL) 12476974bd9eSBjoern A. Zeeb ipsec_delete_pcbpolicy(inp); 124883e521ecSBjoern A. Zeeb #endif 124979bdc6e5SRobert Watson inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1250c3229e05SDavid Greenman in_pcbremlists(inp); 12516aee2fc5SBjoern A. Zeeb #ifdef INET6 12526aee2fc5SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 12536aee2fc5SBjoern A. Zeeb ip6_freepcbopts(inp->in6p_outputopts); 12541096332aSBruce M Simpson if (inp->in6p_moptions != NULL) 12556aee2fc5SBjoern A. Zeeb ip6_freemoptions(inp->in6p_moptions); 12566aee2fc5SBjoern A. Zeeb } 12576aee2fc5SBjoern A. Zeeb #endif 1258df8bae1dSRodney W. Grimes if (inp->inp_options) 1259df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 126067107f45SBjoern A. Zeeb #ifdef INET 126171498f30SBruce M Simpson if (inp->inp_moptions != NULL) 126271498f30SBruce M Simpson inp_freemoptions(inp->inp_moptions); 126367107f45SBjoern A. Zeeb #endif 1264cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 1265df4e91d3SGleb Smirnoff inp->inp_flags2 |= INP_FREED; 126686d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 1267a557af22SRobert Watson #ifdef MAC 126830d239bcSRobert Watson mac_inpcb_destroy(inp); 1269a557af22SRobert Watson #endif 127079bdc6e5SRobert Watson if (!in_pcbrele_wlocked(inp)) 127128696211SRobert Watson INP_WUNLOCK(inp); 127228696211SRobert Watson } 127328696211SRobert Watson 127428696211SRobert Watson /* 1275c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1276c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1277c0a211c5SRobert Watson * 1278c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1279c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1280c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1281c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1282c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1283c0a211c5SRobert Watson * in_pcbdetach(). 1284c0a211c5SRobert Watson * 1285c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1286c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 128710702a28SRobert Watson */ 128810702a28SRobert Watson void 128910702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 129010702a28SRobert Watson { 129110702a28SRobert Watson 12928501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 129310702a28SRobert Watson 1294fa046d87SRobert Watson /* 1295fa046d87SRobert Watson * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1296fa046d87SRobert Watson * the hash lock...? 1297fa046d87SRobert Watson */ 1298ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1299111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 130010702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 130110702a28SRobert Watson 1302fa046d87SRobert Watson INP_HASH_WLOCK(inp->inp_pcbinfo); 130310702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 130410702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 130510702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 130610702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 130710702a28SRobert Watson free(phd, M_PCB); 130810702a28SRobert Watson } 1309fa046d87SRobert Watson INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1310111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 131152cd27cbSRobert Watson #ifdef PCBGROUP 131252cd27cbSRobert Watson in_pcbgroup_remove(inp); 131352cd27cbSRobert Watson #endif 131410702a28SRobert Watson } 131510702a28SRobert Watson } 131610702a28SRobert Watson 131767107f45SBjoern A. Zeeb #ifdef INET 131854d642bbSRobert Watson /* 131954d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 132054d642bbSRobert Watson */ 132126ef6ac4SDon Lewis struct sockaddr * 1322136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 132326ef6ac4SDon Lewis { 132426ef6ac4SDon Lewis struct sockaddr_in *sin; 132526ef6ac4SDon Lewis 13261ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1327a163d034SWarner Losh M_WAITOK | M_ZERO); 132826ef6ac4SDon Lewis sin->sin_family = AF_INET; 132926ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 133026ef6ac4SDon Lewis sin->sin_addr = *addr_p; 133126ef6ac4SDon Lewis sin->sin_port = port; 133226ef6ac4SDon Lewis 133326ef6ac4SDon Lewis return (struct sockaddr *)sin; 133426ef6ac4SDon Lewis } 133526ef6ac4SDon Lewis 1336117bcae7SGarrett Wollman int 133754d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1338df8bae1dSRodney W. Grimes { 1339136d4f1cSRobert Watson struct inpcb *inp; 134026ef6ac4SDon Lewis struct in_addr addr; 134126ef6ac4SDon Lewis in_port_t port; 134242fa505bSDavid Greenman 1343fdc984f7STor Egge inp = sotoinpcb(so); 134454d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 13456466b28aSRobert Watson 1346a69042a5SRobert Watson INP_RLOCK(inp); 134726ef6ac4SDon Lewis port = inp->inp_lport; 134826ef6ac4SDon Lewis addr = inp->inp_laddr; 1349a69042a5SRobert Watson INP_RUNLOCK(inp); 135042fa505bSDavid Greenman 135126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1352117bcae7SGarrett Wollman return 0; 1353df8bae1dSRodney W. Grimes } 1354df8bae1dSRodney W. Grimes 1355117bcae7SGarrett Wollman int 135654d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1357df8bae1dSRodney W. Grimes { 1358136d4f1cSRobert Watson struct inpcb *inp; 135926ef6ac4SDon Lewis struct in_addr addr; 136026ef6ac4SDon Lewis in_port_t port; 136142fa505bSDavid Greenman 1362fdc984f7STor Egge inp = sotoinpcb(so); 136354d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 13646466b28aSRobert Watson 1365a69042a5SRobert Watson INP_RLOCK(inp); 136626ef6ac4SDon Lewis port = inp->inp_fport; 136726ef6ac4SDon Lewis addr = inp->inp_faddr; 1368a69042a5SRobert Watson INP_RUNLOCK(inp); 136942fa505bSDavid Greenman 137026ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1371117bcae7SGarrett Wollman return 0; 1372df8bae1dSRodney W. Grimes } 1373df8bae1dSRodney W. Grimes 137426f9a767SRodney W. Grimes void 1375136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1376136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1377d1c54148SJesper Skriver { 1378f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1379d1c54148SJesper Skriver 13803dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1381f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 13828501a69cSRobert Watson INP_WLOCK(inp); 1383d1c54148SJesper Skriver #ifdef INET6 1384f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 13858501a69cSRobert Watson INP_WUNLOCK(inp); 1386d1c54148SJesper Skriver continue; 1387f76fcf6dSJeffrey Hsu } 1388d1c54148SJesper Skriver #endif 1389d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1390f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 13918501a69cSRobert Watson INP_WUNLOCK(inp); 1392d1c54148SJesper Skriver continue; 1393d1c54148SJesper Skriver } 13943dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 13958501a69cSRobert Watson INP_WUNLOCK(inp); 1396f76fcf6dSJeffrey Hsu } 13973dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1398d1c54148SJesper Skriver } 1399d1c54148SJesper Skriver 1400e43cc4aeSHajimu UMEMOTO void 1401136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1402e43cc4aeSHajimu UMEMOTO { 1403e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1404e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1405e43cc4aeSHajimu UMEMOTO int i, gap; 1406e43cc4aeSHajimu UMEMOTO 1407f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(pcbinfo); 1408712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 14098501a69cSRobert Watson INP_WLOCK(inp); 1410e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1411e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1412e43cc4aeSHajimu UMEMOTO imo != NULL) { 1413e43cc4aeSHajimu UMEMOTO /* 1414e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1415e43cc4aeSHajimu UMEMOTO * detached. 1416e43cc4aeSHajimu UMEMOTO */ 1417e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1418e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1419e43cc4aeSHajimu UMEMOTO 1420e43cc4aeSHajimu UMEMOTO /* 1421e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1422e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1423e43cc4aeSHajimu UMEMOTO */ 1424e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1425e43cc4aeSHajimu UMEMOTO i++) { 1426e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1427e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 1428e43cc4aeSHajimu UMEMOTO gap++; 1429e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1430e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1431e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1432e43cc4aeSHajimu UMEMOTO } 1433e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1434e43cc4aeSHajimu UMEMOTO } 14358501a69cSRobert Watson INP_WUNLOCK(inp); 1436e43cc4aeSHajimu UMEMOTO } 14373cfcc388SJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 1438e43cc4aeSHajimu UMEMOTO } 1439e43cc4aeSHajimu UMEMOTO 1440df8bae1dSRodney W. Grimes /* 1441fa046d87SRobert Watson * Lookup a PCB based on the local address and port. Caller must hold the 1442fa046d87SRobert Watson * hash lock. No inpcb locks or references are acquired. 1443c3229e05SDavid Greenman */ 1444d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1445df8bae1dSRodney W. Grimes struct inpcb * 1446136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 144768e0d7e0SRobert Watson u_short lport, int lookupflags, struct ucred *cred) 1448df8bae1dSRodney W. Grimes { 1449136d4f1cSRobert Watson struct inpcb *inp; 1450d5e8a67eSHajimu UMEMOTO #ifdef INET6 1451d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1452d5e8a67eSHajimu UMEMOTO #else 1453d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1454d5e8a67eSHajimu UMEMOTO #endif 1455d5e8a67eSHajimu UMEMOTO int wildcard; 14567bc4aca7SDavid Greenman 145768e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 145868e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 145968e0d7e0SRobert Watson 1460fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 14611b73ca0bSSam Leffler 146268e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1463c3229e05SDavid Greenman struct inpcbhead *head; 1464c3229e05SDavid Greenman /* 1465c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1466c3229e05SDavid Greenman * matches the local address and port we're looking for. 1467c3229e05SDavid Greenman */ 1468712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1469712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1470fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1471cfa1ca9dSYoshinobu Inoue #ifdef INET6 1472413628a7SBjoern A. Zeeb /* XXX inp locking */ 1473369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1474cfa1ca9dSYoshinobu Inoue continue; 1475cfa1ca9dSYoshinobu Inoue #endif 1476c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1477c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1478c3229e05SDavid Greenman inp->inp_lport == lport) { 1479c3229e05SDavid Greenman /* 1480413628a7SBjoern A. Zeeb * Found? 1481c3229e05SDavid Greenman */ 1482413628a7SBjoern A. Zeeb if (cred == NULL || 14830304c731SJamie Gritton prison_equal_ip4(cred->cr_prison, 14840304c731SJamie Gritton inp->inp_cred->cr_prison)) 1485c3229e05SDavid Greenman return (inp); 1486df8bae1dSRodney W. Grimes } 1487c3229e05SDavid Greenman } 1488c3229e05SDavid Greenman /* 1489c3229e05SDavid Greenman * Not found. 1490c3229e05SDavid Greenman */ 1491c3229e05SDavid Greenman return (NULL); 1492c3229e05SDavid Greenman } else { 1493c3229e05SDavid Greenman struct inpcbporthead *porthash; 1494c3229e05SDavid Greenman struct inpcbport *phd; 1495c3229e05SDavid Greenman struct inpcb *match = NULL; 1496c3229e05SDavid Greenman /* 1497c3229e05SDavid Greenman * Best fit PCB lookup. 1498c3229e05SDavid Greenman * 1499c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1500c3229e05SDavid Greenman * port hash list. 1501c3229e05SDavid Greenman */ 1502712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1503712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1504fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1505c3229e05SDavid Greenman if (phd->phd_port == lport) 1506c3229e05SDavid Greenman break; 1507c3229e05SDavid Greenman } 1508c3229e05SDavid Greenman if (phd != NULL) { 1509c3229e05SDavid Greenman /* 1510c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1511c3229e05SDavid Greenman * fit. 1512c3229e05SDavid Greenman */ 151337d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1514c3229e05SDavid Greenman wildcard = 0; 1515413628a7SBjoern A. Zeeb if (cred != NULL && 15160304c731SJamie Gritton !prison_equal_ip4(inp->inp_cred->cr_prison, 15170304c731SJamie Gritton cred->cr_prison)) 1518413628a7SBjoern A. Zeeb continue; 1519cfa1ca9dSYoshinobu Inoue #ifdef INET6 1520413628a7SBjoern A. Zeeb /* XXX inp locking */ 1521369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1522cfa1ca9dSYoshinobu Inoue continue; 1523d5e8a67eSHajimu UMEMOTO /* 1524d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1525d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1526d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1527d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1528d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1529d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1530d5e8a67eSHajimu UMEMOTO * 1531d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1532d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1533d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1534d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1535d5e8a67eSHajimu UMEMOTO */ 1536d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1537d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1538cfa1ca9dSYoshinobu Inoue #endif 1539c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1540c3229e05SDavid Greenman wildcard++; 154115bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 154215bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 154315bd2b43SDavid Greenman wildcard++; 154415bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 154515bd2b43SDavid Greenman continue; 154615bd2b43SDavid Greenman } else { 154715bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 154815bd2b43SDavid Greenman wildcard++; 154915bd2b43SDavid Greenman } 1550df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1551df8bae1dSRodney W. Grimes match = inp; 1552df8bae1dSRodney W. Grimes matchwild = wildcard; 1553413628a7SBjoern A. Zeeb if (matchwild == 0) 1554df8bae1dSRodney W. Grimes break; 1555df8bae1dSRodney W. Grimes } 1556df8bae1dSRodney W. Grimes } 15573dbdc25cSDavid Greenman } 1558df8bae1dSRodney W. Grimes return (match); 1559df8bae1dSRodney W. Grimes } 1560c3229e05SDavid Greenman } 1561d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 156215bd2b43SDavid Greenman 156352cd27cbSRobert Watson #ifdef PCBGROUP 156452cd27cbSRobert Watson /* 156552cd27cbSRobert Watson * Lookup PCB in hash list, using pcbgroup tables. 156652cd27cbSRobert Watson */ 156752cd27cbSRobert Watson static struct inpcb * 156852cd27cbSRobert Watson in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 156952cd27cbSRobert Watson struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 157052cd27cbSRobert Watson u_int lport_arg, int lookupflags, struct ifnet *ifp) 157152cd27cbSRobert Watson { 157252cd27cbSRobert Watson struct inpcbhead *head; 157352cd27cbSRobert Watson struct inpcb *inp, *tmpinp; 157452cd27cbSRobert Watson u_short fport = fport_arg, lport = lport_arg; 157552cd27cbSRobert Watson 157652cd27cbSRobert Watson /* 157752cd27cbSRobert Watson * First look for an exact match. 157852cd27cbSRobert Watson */ 157952cd27cbSRobert Watson tmpinp = NULL; 158052cd27cbSRobert Watson INP_GROUP_LOCK(pcbgroup); 158152cd27cbSRobert Watson head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 158252cd27cbSRobert Watson pcbgroup->ipg_hashmask)]; 158352cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgrouphash) { 158452cd27cbSRobert Watson #ifdef INET6 158552cd27cbSRobert Watson /* XXX inp locking */ 158652cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 158752cd27cbSRobert Watson continue; 158852cd27cbSRobert Watson #endif 158952cd27cbSRobert Watson if (inp->inp_faddr.s_addr == faddr.s_addr && 159052cd27cbSRobert Watson inp->inp_laddr.s_addr == laddr.s_addr && 159152cd27cbSRobert Watson inp->inp_fport == fport && 159252cd27cbSRobert Watson inp->inp_lport == lport) { 159352cd27cbSRobert Watson /* 159452cd27cbSRobert Watson * XXX We should be able to directly return 159552cd27cbSRobert Watson * the inp here, without any checks. 159652cd27cbSRobert Watson * Well unless both bound with SO_REUSEPORT? 159752cd27cbSRobert Watson */ 159852cd27cbSRobert Watson if (prison_flag(inp->inp_cred, PR_IP4)) 159952cd27cbSRobert Watson goto found; 160052cd27cbSRobert Watson if (tmpinp == NULL) 160152cd27cbSRobert Watson tmpinp = inp; 160252cd27cbSRobert Watson } 160352cd27cbSRobert Watson } 160452cd27cbSRobert Watson if (tmpinp != NULL) { 160552cd27cbSRobert Watson inp = tmpinp; 160652cd27cbSRobert Watson goto found; 160752cd27cbSRobert Watson } 160852cd27cbSRobert Watson 16090a100a6fSAdrian Chadd #ifdef RSS 16100a100a6fSAdrian Chadd /* 16110a100a6fSAdrian Chadd * For incoming connections, we may wish to do a wildcard 16120a100a6fSAdrian Chadd * match for an RSS-local socket. 16130a100a6fSAdrian Chadd */ 16140a100a6fSAdrian Chadd if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 16150a100a6fSAdrian Chadd struct inpcb *local_wild = NULL, *local_exact = NULL; 16160a100a6fSAdrian Chadd #ifdef INET6 16170a100a6fSAdrian Chadd struct inpcb *local_wild_mapped = NULL; 16180a100a6fSAdrian Chadd #endif 16190a100a6fSAdrian Chadd struct inpcb *jail_wild = NULL; 16200a100a6fSAdrian Chadd struct inpcbhead *head; 16210a100a6fSAdrian Chadd int injail; 16220a100a6fSAdrian Chadd 16230a100a6fSAdrian Chadd /* 16240a100a6fSAdrian Chadd * Order of socket selection - we always prefer jails. 16250a100a6fSAdrian Chadd * 1. jailed, non-wild. 16260a100a6fSAdrian Chadd * 2. jailed, wild. 16270a100a6fSAdrian Chadd * 3. non-jailed, non-wild. 16280a100a6fSAdrian Chadd * 4. non-jailed, wild. 16290a100a6fSAdrian Chadd */ 16300a100a6fSAdrian Chadd 16310a100a6fSAdrian Chadd head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY, 16320a100a6fSAdrian Chadd lport, 0, pcbgroup->ipg_hashmask)]; 16330a100a6fSAdrian Chadd LIST_FOREACH(inp, head, inp_pcbgrouphash) { 16340a100a6fSAdrian Chadd #ifdef INET6 16350a100a6fSAdrian Chadd /* XXX inp locking */ 16360a100a6fSAdrian Chadd if ((inp->inp_vflag & INP_IPV4) == 0) 16370a100a6fSAdrian Chadd continue; 16380a100a6fSAdrian Chadd #endif 16390a100a6fSAdrian Chadd if (inp->inp_faddr.s_addr != INADDR_ANY || 16400a100a6fSAdrian Chadd inp->inp_lport != lport) 16410a100a6fSAdrian Chadd continue; 16420a100a6fSAdrian Chadd 16430a100a6fSAdrian Chadd /* XXX inp locking */ 16440a100a6fSAdrian Chadd if (ifp && ifp->if_type == IFT_FAITH && 16450a100a6fSAdrian Chadd (inp->inp_flags & INP_FAITH) == 0) 16460a100a6fSAdrian Chadd continue; 16470a100a6fSAdrian Chadd 16480a100a6fSAdrian Chadd injail = prison_flag(inp->inp_cred, PR_IP4); 16490a100a6fSAdrian Chadd if (injail) { 16500a100a6fSAdrian Chadd if (prison_check_ip4(inp->inp_cred, 16510a100a6fSAdrian Chadd &laddr) != 0) 16520a100a6fSAdrian Chadd continue; 16530a100a6fSAdrian Chadd } else { 16540a100a6fSAdrian Chadd if (local_exact != NULL) 16550a100a6fSAdrian Chadd continue; 16560a100a6fSAdrian Chadd } 16570a100a6fSAdrian Chadd 16580a100a6fSAdrian Chadd if (inp->inp_laddr.s_addr == laddr.s_addr) { 16590a100a6fSAdrian Chadd if (injail) 16600a100a6fSAdrian Chadd goto found; 16610a100a6fSAdrian Chadd else 16620a100a6fSAdrian Chadd local_exact = inp; 16630a100a6fSAdrian Chadd } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 16640a100a6fSAdrian Chadd #ifdef INET6 16650a100a6fSAdrian Chadd /* XXX inp locking, NULL check */ 16660a100a6fSAdrian Chadd if (inp->inp_vflag & INP_IPV6PROTO) 16670a100a6fSAdrian Chadd local_wild_mapped = inp; 16680a100a6fSAdrian Chadd else 16690a100a6fSAdrian Chadd #endif 16700a100a6fSAdrian Chadd if (injail) 16710a100a6fSAdrian Chadd jail_wild = inp; 16720a100a6fSAdrian Chadd else 16730a100a6fSAdrian Chadd local_wild = inp; 16740a100a6fSAdrian Chadd } 16750a100a6fSAdrian Chadd } /* LIST_FOREACH */ 16760a100a6fSAdrian Chadd 16770a100a6fSAdrian Chadd inp = jail_wild; 16780a100a6fSAdrian Chadd if (inp == NULL) 16790a100a6fSAdrian Chadd inp = local_exact; 16800a100a6fSAdrian Chadd if (inp == NULL) 16810a100a6fSAdrian Chadd inp = local_wild; 16820a100a6fSAdrian Chadd #ifdef INET6 16830a100a6fSAdrian Chadd if (inp == NULL) 16840a100a6fSAdrian Chadd inp = local_wild_mapped; 16850a100a6fSAdrian Chadd #endif 16860a100a6fSAdrian Chadd if (inp != NULL) 16870a100a6fSAdrian Chadd goto found; 16880a100a6fSAdrian Chadd } 16890a100a6fSAdrian Chadd #endif 16900a100a6fSAdrian Chadd 169152cd27cbSRobert Watson /* 169252cd27cbSRobert Watson * Then look for a wildcard match, if requested. 169352cd27cbSRobert Watson */ 169452cd27cbSRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 169552cd27cbSRobert Watson struct inpcb *local_wild = NULL, *local_exact = NULL; 169652cd27cbSRobert Watson #ifdef INET6 169752cd27cbSRobert Watson struct inpcb *local_wild_mapped = NULL; 169852cd27cbSRobert Watson #endif 169952cd27cbSRobert Watson struct inpcb *jail_wild = NULL; 170052cd27cbSRobert Watson struct inpcbhead *head; 170152cd27cbSRobert Watson int injail; 170252cd27cbSRobert Watson 170352cd27cbSRobert Watson /* 170452cd27cbSRobert Watson * Order of socket selection - we always prefer jails. 170552cd27cbSRobert Watson * 1. jailed, non-wild. 170652cd27cbSRobert Watson * 2. jailed, wild. 170752cd27cbSRobert Watson * 3. non-jailed, non-wild. 170852cd27cbSRobert Watson * 4. non-jailed, wild. 170952cd27cbSRobert Watson */ 171052cd27cbSRobert Watson head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 171152cd27cbSRobert Watson 0, pcbinfo->ipi_wildmask)]; 171252cd27cbSRobert Watson LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 171352cd27cbSRobert Watson #ifdef INET6 171452cd27cbSRobert Watson /* XXX inp locking */ 171552cd27cbSRobert Watson if ((inp->inp_vflag & INP_IPV4) == 0) 171652cd27cbSRobert Watson continue; 171752cd27cbSRobert Watson #endif 171852cd27cbSRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY || 171952cd27cbSRobert Watson inp->inp_lport != lport) 172052cd27cbSRobert Watson continue; 172152cd27cbSRobert Watson 172252cd27cbSRobert Watson /* XXX inp locking */ 172352cd27cbSRobert Watson if (ifp && ifp->if_type == IFT_FAITH && 172452cd27cbSRobert Watson (inp->inp_flags & INP_FAITH) == 0) 172552cd27cbSRobert Watson continue; 172652cd27cbSRobert Watson 172752cd27cbSRobert Watson injail = prison_flag(inp->inp_cred, PR_IP4); 172852cd27cbSRobert Watson if (injail) { 172952cd27cbSRobert Watson if (prison_check_ip4(inp->inp_cred, 173052cd27cbSRobert Watson &laddr) != 0) 173152cd27cbSRobert Watson continue; 173252cd27cbSRobert Watson } else { 173352cd27cbSRobert Watson if (local_exact != NULL) 173452cd27cbSRobert Watson continue; 173552cd27cbSRobert Watson } 173652cd27cbSRobert Watson 173752cd27cbSRobert Watson if (inp->inp_laddr.s_addr == laddr.s_addr) { 173852cd27cbSRobert Watson if (injail) 173952cd27cbSRobert Watson goto found; 174052cd27cbSRobert Watson else 174152cd27cbSRobert Watson local_exact = inp; 174252cd27cbSRobert Watson } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 174352cd27cbSRobert Watson #ifdef INET6 174452cd27cbSRobert Watson /* XXX inp locking, NULL check */ 174552cd27cbSRobert Watson if (inp->inp_vflag & INP_IPV6PROTO) 174652cd27cbSRobert Watson local_wild_mapped = inp; 174752cd27cbSRobert Watson else 174883e521ecSBjoern A. Zeeb #endif 174952cd27cbSRobert Watson if (injail) 175052cd27cbSRobert Watson jail_wild = inp; 175152cd27cbSRobert Watson else 175252cd27cbSRobert Watson local_wild = inp; 175352cd27cbSRobert Watson } 175452cd27cbSRobert Watson } /* LIST_FOREACH */ 175552cd27cbSRobert Watson inp = jail_wild; 175652cd27cbSRobert Watson if (inp == NULL) 175752cd27cbSRobert Watson inp = local_exact; 175852cd27cbSRobert Watson if (inp == NULL) 175952cd27cbSRobert Watson inp = local_wild; 176052cd27cbSRobert Watson #ifdef INET6 176152cd27cbSRobert Watson if (inp == NULL) 176252cd27cbSRobert Watson inp = local_wild_mapped; 176383e521ecSBjoern A. Zeeb #endif 176452cd27cbSRobert Watson if (inp != NULL) 176552cd27cbSRobert Watson goto found; 176652cd27cbSRobert Watson } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 176752cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 176852cd27cbSRobert Watson return (NULL); 176952cd27cbSRobert Watson 177052cd27cbSRobert Watson found: 177152cd27cbSRobert Watson in_pcbref(inp); 177252cd27cbSRobert Watson INP_GROUP_UNLOCK(pcbgroup); 177352cd27cbSRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 177452cd27cbSRobert Watson INP_WLOCK(inp); 177552cd27cbSRobert Watson if (in_pcbrele_wlocked(inp)) 177652cd27cbSRobert Watson return (NULL); 177752cd27cbSRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 177852cd27cbSRobert Watson INP_RLOCK(inp); 177952cd27cbSRobert Watson if (in_pcbrele_rlocked(inp)) 178052cd27cbSRobert Watson return (NULL); 178152cd27cbSRobert Watson } else 178252cd27cbSRobert Watson panic("%s: locking bug", __func__); 178352cd27cbSRobert Watson return (inp); 178452cd27cbSRobert Watson } 178552cd27cbSRobert Watson #endif /* PCBGROUP */ 178652cd27cbSRobert Watson 178715bd2b43SDavid Greenman /* 1788fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 1789fa046d87SRobert Watson * that the caller has locked the hash list, and will not perform any further 1790fa046d87SRobert Watson * locking or reference operations on either the hash list or the connection. 179115bd2b43SDavid Greenman */ 1792fa046d87SRobert Watson static struct inpcb * 1793fa046d87SRobert Watson in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 179468e0d7e0SRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 1795136d4f1cSRobert Watson struct ifnet *ifp) 179615bd2b43SDavid Greenman { 179715bd2b43SDavid Greenman struct inpcbhead *head; 1798413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 179915bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 180015bd2b43SDavid Greenman 180168e0d7e0SRobert Watson KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 180268e0d7e0SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 180368e0d7e0SRobert Watson 1804fa046d87SRobert Watson INP_HASH_LOCK_ASSERT(pcbinfo); 1805602cc7f1SRobert Watson 180615bd2b43SDavid Greenman /* 180715bd2b43SDavid Greenman * First look for an exact match. 180815bd2b43SDavid Greenman */ 1809413628a7SBjoern A. Zeeb tmpinp = NULL; 1810712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1811712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1812fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1813cfa1ca9dSYoshinobu Inoue #ifdef INET6 1814413628a7SBjoern A. Zeeb /* XXX inp locking */ 1815369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1816cfa1ca9dSYoshinobu Inoue continue; 1817cfa1ca9dSYoshinobu Inoue #endif 18186d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1819ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1820ca98b82cSDavid Greenman inp->inp_fport == fport && 1821413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1822413628a7SBjoern A. Zeeb /* 1823413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1824413628a7SBjoern A. Zeeb * the inp here, without any checks. 1825413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1826413628a7SBjoern A. Zeeb */ 18270304c731SJamie Gritton if (prison_flag(inp->inp_cred, PR_IP4)) 1828c3229e05SDavid Greenman return (inp); 1829413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1830413628a7SBjoern A. Zeeb tmpinp = inp; 1831c3229e05SDavid Greenman } 1832413628a7SBjoern A. Zeeb } 1833413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1834413628a7SBjoern A. Zeeb return (tmpinp); 1835e3fd5ffdSRobert Watson 1836e3fd5ffdSRobert Watson /* 1837e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1838e3fd5ffdSRobert Watson */ 183968e0d7e0SRobert Watson if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1840413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1841e3fd5ffdSRobert Watson #ifdef INET6 1842cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1843e3fd5ffdSRobert Watson #endif 1844413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1845413628a7SBjoern A. Zeeb int injail; 1846413628a7SBjoern A. Zeeb 1847413628a7SBjoern A. Zeeb /* 1848413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 1849413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 1850413628a7SBjoern A. Zeeb * 2. jailed, wild. 1851413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 1852413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 1853413628a7SBjoern A. Zeeb */ 18546d6a026bSDavid Greenman 1855712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1856712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1857fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1858cfa1ca9dSYoshinobu Inoue #ifdef INET6 1859413628a7SBjoern A. Zeeb /* XXX inp locking */ 1860369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1861cfa1ca9dSYoshinobu Inoue continue; 1862cfa1ca9dSYoshinobu Inoue #endif 1863413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 1864413628a7SBjoern A. Zeeb inp->inp_lport != lport) 1865413628a7SBjoern A. Zeeb continue; 1866413628a7SBjoern A. Zeeb 1867413628a7SBjoern A. Zeeb /* XXX inp locking */ 1868cfa1ca9dSYoshinobu Inoue if (ifp && ifp->if_type == IFT_FAITH && 1869cfa1ca9dSYoshinobu Inoue (inp->inp_flags & INP_FAITH) == 0) 1870cfa1ca9dSYoshinobu Inoue continue; 1871413628a7SBjoern A. Zeeb 18720304c731SJamie Gritton injail = prison_flag(inp->inp_cred, PR_IP4); 1873413628a7SBjoern A. Zeeb if (injail) { 1874b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 1875b89e82ddSJamie Gritton &laddr) != 0) 1876413628a7SBjoern A. Zeeb continue; 1877413628a7SBjoern A. Zeeb } else { 1878413628a7SBjoern A. Zeeb if (local_exact != NULL) 1879413628a7SBjoern A. Zeeb continue; 1880413628a7SBjoern A. Zeeb } 1881413628a7SBjoern A. Zeeb 1882413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 1883413628a7SBjoern A. Zeeb if (injail) 1884c3229e05SDavid Greenman return (inp); 1885413628a7SBjoern A. Zeeb else 1886413628a7SBjoern A. Zeeb local_exact = inp; 1887413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1888e3fd5ffdSRobert Watson #ifdef INET6 1889413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 18905cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 1891cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1892cfa1ca9dSYoshinobu Inoue else 189383e521ecSBjoern A. Zeeb #endif 1894413628a7SBjoern A. Zeeb if (injail) 1895413628a7SBjoern A. Zeeb jail_wild = inp; 1896413628a7SBjoern A. Zeeb else 18976d6a026bSDavid Greenman local_wild = inp; 18986d6a026bSDavid Greenman } 1899413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 1900413628a7SBjoern A. Zeeb if (jail_wild != NULL) 1901413628a7SBjoern A. Zeeb return (jail_wild); 1902413628a7SBjoern A. Zeeb if (local_exact != NULL) 1903413628a7SBjoern A. Zeeb return (local_exact); 1904413628a7SBjoern A. Zeeb if (local_wild != NULL) 1905c3229e05SDavid Greenman return (local_wild); 1906413628a7SBjoern A. Zeeb #ifdef INET6 1907413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 1908413628a7SBjoern A. Zeeb return (local_wild_mapped); 190983e521ecSBjoern A. Zeeb #endif 191068e0d7e0SRobert Watson } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 1911413628a7SBjoern A. Zeeb 19126d6a026bSDavid Greenman return (NULL); 191315bd2b43SDavid Greenman } 1914fa046d87SRobert Watson 1915fa046d87SRobert Watson /* 1916fa046d87SRobert Watson * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 1917fa046d87SRobert Watson * hash list lock, and will return the inpcb locked (i.e., requires 1918fa046d87SRobert Watson * INPLOOKUP_LOCKPCB). 1919fa046d87SRobert Watson */ 1920fa046d87SRobert Watson static struct inpcb * 1921fa046d87SRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1922fa046d87SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 1923fa046d87SRobert Watson struct ifnet *ifp) 1924fa046d87SRobert Watson { 1925fa046d87SRobert Watson struct inpcb *inp; 1926fa046d87SRobert Watson 1927fa046d87SRobert Watson INP_HASH_RLOCK(pcbinfo); 1928fa046d87SRobert Watson inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1929fa046d87SRobert Watson (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 1930fa046d87SRobert Watson if (inp != NULL) { 1931fa046d87SRobert Watson in_pcbref(inp); 1932fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1933fa046d87SRobert Watson if (lookupflags & INPLOOKUP_WLOCKPCB) { 1934fa046d87SRobert Watson INP_WLOCK(inp); 1935fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) 1936fa046d87SRobert Watson return (NULL); 1937fa046d87SRobert Watson } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 1938fa046d87SRobert Watson INP_RLOCK(inp); 1939fa046d87SRobert Watson if (in_pcbrele_rlocked(inp)) 1940fa046d87SRobert Watson return (NULL); 1941fa046d87SRobert Watson } else 1942fa046d87SRobert Watson panic("%s: locking bug", __func__); 1943fa046d87SRobert Watson } else 1944fa046d87SRobert Watson INP_HASH_RUNLOCK(pcbinfo); 1945fa046d87SRobert Watson return (inp); 1946fa046d87SRobert Watson } 1947fa046d87SRobert Watson 1948fa046d87SRobert Watson /* 1949d3c1f003SRobert Watson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1950d3c1f003SRobert Watson * from which a pre-calculated hash value may be extracted. 195152cd27cbSRobert Watson * 195252cd27cbSRobert Watson * Possibly more of this logic should be in in_pcbgroup.c. 1953fa046d87SRobert Watson */ 1954fa046d87SRobert Watson struct inpcb * 1955fa046d87SRobert Watson in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 1956fa046d87SRobert Watson struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 1957fa046d87SRobert Watson { 19587527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 195952cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 196052cd27cbSRobert Watson #endif 1961fa046d87SRobert Watson 1962fa046d87SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1963fa046d87SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 1964fa046d87SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1965fa046d87SRobert Watson ("%s: LOCKPCB not set", __func__)); 1966fa046d87SRobert Watson 19677527624eSRobert Watson /* 19687527624eSRobert Watson * When not using RSS, use connection groups in preference to the 19697527624eSRobert Watson * reservation table when looking up 4-tuples. When using RSS, just 19707527624eSRobert Watson * use the reservation table, due to the cost of the Toeplitz hash 19717527624eSRobert Watson * in software. 19727527624eSRobert Watson * 19737527624eSRobert Watson * XXXRW: This policy belongs in the pcbgroup code, as in principle 19747527624eSRobert Watson * we could be doing RSS with a non-Toeplitz hash that is affordable 19757527624eSRobert Watson * in software. 19767527624eSRobert Watson */ 19777527624eSRobert Watson #if defined(PCBGROUP) && !defined(RSS) 197852cd27cbSRobert Watson if (in_pcbgroup_enabled(pcbinfo)) { 197952cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 198052cd27cbSRobert Watson fport); 198152cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 198252cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 198352cd27cbSRobert Watson } 198452cd27cbSRobert Watson #endif 1985fa046d87SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1986fa046d87SRobert Watson lookupflags, ifp)); 1987fa046d87SRobert Watson } 1988d3c1f003SRobert Watson 1989d3c1f003SRobert Watson struct inpcb * 1990d3c1f003SRobert Watson in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1991d3c1f003SRobert Watson u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 1992d3c1f003SRobert Watson struct ifnet *ifp, struct mbuf *m) 1993d3c1f003SRobert Watson { 199452cd27cbSRobert Watson #ifdef PCBGROUP 199552cd27cbSRobert Watson struct inpcbgroup *pcbgroup; 199652cd27cbSRobert Watson #endif 1997d3c1f003SRobert Watson 1998d3c1f003SRobert Watson KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1999d3c1f003SRobert Watson ("%s: invalid lookup flags %d", __func__, lookupflags)); 2000d3c1f003SRobert Watson KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2001d3c1f003SRobert Watson ("%s: LOCKPCB not set", __func__)); 2002d3c1f003SRobert Watson 200352cd27cbSRobert Watson #ifdef PCBGROUP 20047527624eSRobert Watson /* 20057527624eSRobert Watson * If we can use a hardware-generated hash to look up the connection 20067527624eSRobert Watson * group, use that connection group to find the inpcb. Otherwise 20077527624eSRobert Watson * fall back on a software hash -- or the reservation table if we're 20087527624eSRobert Watson * using RSS. 20097527624eSRobert Watson * 20107527624eSRobert Watson * XXXRW: As above, that policy belongs in the pcbgroup code. 20117527624eSRobert Watson */ 20127527624eSRobert Watson if (in_pcbgroup_enabled(pcbinfo) && 20137527624eSRobert Watson !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) { 201452cd27cbSRobert Watson pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 201552cd27cbSRobert Watson m->m_pkthdr.flowid); 201652cd27cbSRobert Watson if (pcbgroup != NULL) 201752cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 201852cd27cbSRobert Watson fport, laddr, lport, lookupflags, ifp)); 20197527624eSRobert Watson #ifndef RSS 202052cd27cbSRobert Watson pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 202152cd27cbSRobert Watson fport); 202252cd27cbSRobert Watson return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 202352cd27cbSRobert Watson laddr, lport, lookupflags, ifp)); 20247527624eSRobert Watson #endif 202552cd27cbSRobert Watson } 202652cd27cbSRobert Watson #endif 2027d3c1f003SRobert Watson return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2028d3c1f003SRobert Watson lookupflags, ifp)); 2029d3c1f003SRobert Watson } 203067107f45SBjoern A. Zeeb #endif /* INET */ 203115bd2b43SDavid Greenman 20327bc4aca7SDavid Greenman /* 2033c3229e05SDavid Greenman * Insert PCB onto various hash lists. 20347bc4aca7SDavid Greenman */ 203552cd27cbSRobert Watson static int 203652cd27cbSRobert Watson in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 203715bd2b43SDavid Greenman { 2038c3229e05SDavid Greenman struct inpcbhead *pcbhash; 2039c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 2040c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2041c3229e05SDavid Greenman struct inpcbport *phd; 2042cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 204315bd2b43SDavid Greenman 20448501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2045fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2046fa046d87SRobert Watson 2047111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2048111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 2049602cc7f1SRobert Watson 2050cfa1ca9dSYoshinobu Inoue #ifdef INET6 2051cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 20521b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2053cfa1ca9dSYoshinobu Inoue else 205483e521ecSBjoern A. Zeeb #endif 2055cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2056cfa1ca9dSYoshinobu Inoue 2057712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2058712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 205915bd2b43SDavid Greenman 2060712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 2061712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2062c3229e05SDavid Greenman 2063c3229e05SDavid Greenman /* 2064c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 2065c3229e05SDavid Greenman */ 2066fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 2067c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 2068c3229e05SDavid Greenman break; 2069c3229e05SDavid Greenman } 2070c3229e05SDavid Greenman /* 2071c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 2072c3229e05SDavid Greenman */ 2073c3229e05SDavid Greenman if (phd == NULL) { 20741ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 2075c3229e05SDavid Greenman if (phd == NULL) { 2076c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 2077c3229e05SDavid Greenman } 2078c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 2079c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 2080c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2081c3229e05SDavid Greenman } 2082c3229e05SDavid Greenman inp->inp_phd = phd; 2083c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2084c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 2085111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 208652cd27cbSRobert Watson #ifdef PCBGROUP 208752cd27cbSRobert Watson if (do_pcbgroup_update) 208852cd27cbSRobert Watson in_pcbgroup_update(inp); 208952cd27cbSRobert Watson #endif 2090c3229e05SDavid Greenman return (0); 209115bd2b43SDavid Greenman } 209215bd2b43SDavid Greenman 2093c3229e05SDavid Greenman /* 209452cd27cbSRobert Watson * For now, there are two public interfaces to insert an inpcb into the hash 209552cd27cbSRobert Watson * lists -- one that does update pcbgroups, and one that doesn't. The latter 209652cd27cbSRobert Watson * is used only in the TCP syncache, where in_pcbinshash is called before the 209752cd27cbSRobert Watson * full 4-tuple is set for the inpcb, and we don't want to install in the 209852cd27cbSRobert Watson * pcbgroup until later. 209952cd27cbSRobert Watson * 210052cd27cbSRobert Watson * XXXRW: This seems like a misfeature. in_pcbinshash should always update 210152cd27cbSRobert Watson * connection groups, and partially initialised inpcbs should not be exposed 210252cd27cbSRobert Watson * to either reservation hash tables or pcbgroups. 210352cd27cbSRobert Watson */ 210452cd27cbSRobert Watson int 210552cd27cbSRobert Watson in_pcbinshash(struct inpcb *inp) 210652cd27cbSRobert Watson { 210752cd27cbSRobert Watson 210852cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 1)); 210952cd27cbSRobert Watson } 211052cd27cbSRobert Watson 211152cd27cbSRobert Watson int 211252cd27cbSRobert Watson in_pcbinshash_nopcbgroup(struct inpcb *inp) 211352cd27cbSRobert Watson { 211452cd27cbSRobert Watson 211552cd27cbSRobert Watson return (in_pcbinshash_internal(inp, 0)); 211652cd27cbSRobert Watson } 211752cd27cbSRobert Watson 211852cd27cbSRobert Watson /* 2119c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 2120c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 2121c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 2122c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 2123c3229e05SDavid Greenman */ 212415bd2b43SDavid Greenman void 2125d3c1f003SRobert Watson in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 212615bd2b43SDavid Greenman { 212759daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 212815bd2b43SDavid Greenman struct inpcbhead *head; 2129cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 213015bd2b43SDavid Greenman 21318501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2132fa046d87SRobert Watson INP_HASH_WLOCK_ASSERT(pcbinfo); 2133fa046d87SRobert Watson 2134111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 2135111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 2136602cc7f1SRobert Watson 2137cfa1ca9dSYoshinobu Inoue #ifdef INET6 2138cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 21391b44e5ffSAndrey V. Elsukov hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr); 2140cfa1ca9dSYoshinobu Inoue else 214183e521ecSBjoern A. Zeeb #endif 2142cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 2143cfa1ca9dSYoshinobu Inoue 2144712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 2145712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 214615bd2b43SDavid Greenman 2147c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 214815bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 214952cd27cbSRobert Watson 215052cd27cbSRobert Watson #ifdef PCBGROUP 215152cd27cbSRobert Watson if (m != NULL) 215252cd27cbSRobert Watson in_pcbgroup_update_mbuf(inp, m); 215352cd27cbSRobert Watson else 215452cd27cbSRobert Watson in_pcbgroup_update(inp); 215552cd27cbSRobert Watson #endif 2156c3229e05SDavid Greenman } 2157c3229e05SDavid Greenman 2158d3c1f003SRobert Watson void 2159d3c1f003SRobert Watson in_pcbrehash(struct inpcb *inp) 2160d3c1f003SRobert Watson { 2161d3c1f003SRobert Watson 2162d3c1f003SRobert Watson in_pcbrehash_mbuf(inp, NULL); 2163d3c1f003SRobert Watson } 2164d3c1f003SRobert Watson 2165c3229e05SDavid Greenman /* 2166c3229e05SDavid Greenman * Remove PCB from various lists. 2167c3229e05SDavid Greenman */ 21686d888973SRobert Watson static void 2169136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 2170c3229e05SDavid Greenman { 217159daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 217259daba27SSam Leffler 217359daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 21748501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 217559daba27SSam Leffler 217659daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2177111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 2178c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 2179c3229e05SDavid Greenman 2180fa046d87SRobert Watson INP_HASH_WLOCK(pcbinfo); 2181c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 2182c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 2183fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2184c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 2185c3229e05SDavid Greenman free(phd, M_PCB); 2186c3229e05SDavid Greenman } 2187fa046d87SRobert Watson INP_HASH_WUNLOCK(pcbinfo); 2188111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 2189c3229e05SDavid Greenman } 2190c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 219159daba27SSam Leffler pcbinfo->ipi_count--; 219252cd27cbSRobert Watson #ifdef PCBGROUP 219352cd27cbSRobert Watson in_pcbgroup_remove(inp); 219452cd27cbSRobert Watson #endif 219515bd2b43SDavid Greenman } 219675c13541SPoul-Henning Kamp 2197a557af22SRobert Watson /* 2198a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 2199a557af22SRobert Watson * label change into the in_pcb for the socket. 2200a557af22SRobert Watson */ 2201a557af22SRobert Watson void 2202136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 2203a557af22SRobert Watson { 2204a557af22SRobert Watson #ifdef MAC 2205a557af22SRobert Watson struct inpcb *inp; 2206a557af22SRobert Watson 22074c7c478dSRobert Watson inp = sotoinpcb(so); 22084c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2209602cc7f1SRobert Watson 22108501a69cSRobert Watson INP_WLOCK(inp); 2211310e7cebSRobert Watson SOCK_LOCK(so); 2212a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 2213310e7cebSRobert Watson SOCK_UNLOCK(so); 22148501a69cSRobert Watson INP_WUNLOCK(inp); 2215a557af22SRobert Watson #endif 2216a557af22SRobert Watson } 22175f311da2SMike Silbersack 22185f311da2SMike Silbersack /* 2219ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 2220ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 2221ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 2222ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 2223ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 22245f311da2SMike Silbersack */ 2225aae49dd3SBjoern A. Zeeb static void 2226136d4f1cSRobert Watson ipport_tick(void *xtp) 22275f311da2SMike Silbersack { 22288b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 2229ad3a630fSRobert Watson 22305ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 22318b615593SMarko Zec VNET_FOREACH(vnet_iter) { 22328b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 22338b615593SMarko Zec if (V_ipport_tcpallocs <= 22348b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 2235603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 2236603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 2237ad3a630fSRobert Watson } else 2238603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 2239603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 22408b615593SMarko Zec CURVNET_RESTORE(); 22418b615593SMarko Zec } 22425ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 22435f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 22445f311da2SMike Silbersack } 2245497057eeSRobert Watson 2246aae49dd3SBjoern A. Zeeb static void 2247aae49dd3SBjoern A. Zeeb ip_fini(void *xtp) 2248aae49dd3SBjoern A. Zeeb { 2249aae49dd3SBjoern A. Zeeb 2250aae49dd3SBjoern A. Zeeb callout_stop(&ipport_tick_callout); 2251aae49dd3SBjoern A. Zeeb } 2252aae49dd3SBjoern A. Zeeb 2253aae49dd3SBjoern A. Zeeb /* 2254aae49dd3SBjoern A. Zeeb * The ipport_callout should start running at about the time we attach the 2255aae49dd3SBjoern A. Zeeb * inet or inet6 domains. 2256aae49dd3SBjoern A. Zeeb */ 2257aae49dd3SBjoern A. Zeeb static void 2258aae49dd3SBjoern A. Zeeb ipport_tick_init(const void *unused __unused) 2259aae49dd3SBjoern A. Zeeb { 2260aae49dd3SBjoern A. Zeeb 2261aae49dd3SBjoern A. Zeeb /* Start ipport_tick. */ 2262aae49dd3SBjoern A. Zeeb callout_init(&ipport_tick_callout, CALLOUT_MPSAFE); 2263aae49dd3SBjoern A. Zeeb callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2264aae49dd3SBjoern A. Zeeb EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2265aae49dd3SBjoern A. Zeeb SHUTDOWN_PRI_DEFAULT); 2266aae49dd3SBjoern A. Zeeb } 2267aae49dd3SBjoern A. Zeeb SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2268aae49dd3SBjoern A. Zeeb ipport_tick_init, NULL); 2269aae49dd3SBjoern A. Zeeb 22703d585327SKip Macy void 22713d585327SKip Macy inp_wlock(struct inpcb *inp) 22723d585327SKip Macy { 22733d585327SKip Macy 22748501a69cSRobert Watson INP_WLOCK(inp); 22753d585327SKip Macy } 22763d585327SKip Macy 22773d585327SKip Macy void 22783d585327SKip Macy inp_wunlock(struct inpcb *inp) 22793d585327SKip Macy { 22803d585327SKip Macy 22818501a69cSRobert Watson INP_WUNLOCK(inp); 22823d585327SKip Macy } 22833d585327SKip Macy 22843d585327SKip Macy void 22853d585327SKip Macy inp_rlock(struct inpcb *inp) 22863d585327SKip Macy { 22873d585327SKip Macy 2288a69042a5SRobert Watson INP_RLOCK(inp); 22893d585327SKip Macy } 22903d585327SKip Macy 22913d585327SKip Macy void 22923d585327SKip Macy inp_runlock(struct inpcb *inp) 22933d585327SKip Macy { 22943d585327SKip Macy 2295a69042a5SRobert Watson INP_RUNLOCK(inp); 22963d585327SKip Macy } 22973d585327SKip Macy 22983d585327SKip Macy #ifdef INVARIANTS 22993d585327SKip Macy void 2300e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 23013d585327SKip Macy { 23023d585327SKip Macy 23038501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 23043d585327SKip Macy } 23053d585327SKip Macy 23063d585327SKip Macy void 2307e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 23083d585327SKip Macy { 23093d585327SKip Macy 23103d585327SKip Macy INP_UNLOCK_ASSERT(inp); 23113d585327SKip Macy } 23123d585327SKip Macy #endif 23133d585327SKip Macy 23149378e437SKip Macy void 23159378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 23169378e437SKip Macy { 23179378e437SKip Macy struct inpcb *inp; 23189378e437SKip Macy 2319603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_tcbinfo); 232097021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 23219378e437SKip Macy INP_WLOCK(inp); 23229378e437SKip Macy func(inp, arg); 23239378e437SKip Macy INP_WUNLOCK(inp); 23249378e437SKip Macy } 2325603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_tcbinfo); 23269378e437SKip Macy } 23279378e437SKip Macy 23289378e437SKip Macy struct socket * 23299378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 23309378e437SKip Macy { 23319378e437SKip Macy 23329378e437SKip Macy INP_WLOCK_ASSERT(inp); 23339378e437SKip Macy return (inp->inp_socket); 23349378e437SKip Macy } 23359378e437SKip Macy 23369378e437SKip Macy struct tcpcb * 23379378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 23389378e437SKip Macy { 23399378e437SKip Macy 23409378e437SKip Macy INP_WLOCK_ASSERT(inp); 23419378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 23429378e437SKip Macy } 23439378e437SKip Macy 23449378e437SKip Macy int 23459378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 23469378e437SKip Macy { 23479378e437SKip Macy 23489378e437SKip Macy return (inp->inp_ip_tos); 23499378e437SKip Macy } 23509378e437SKip Macy 23519378e437SKip Macy void 23529378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 23539378e437SKip Macy { 23549378e437SKip Macy 23559378e437SKip Macy inp->inp_ip_tos = val; 23569378e437SKip Macy } 23579378e437SKip Macy 23589378e437SKip Macy void 2359df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 23609d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 23619378e437SKip Macy { 23629378e437SKip Macy 23639d29c635SKip Macy INP_LOCK_ASSERT(inp); 2364df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 2365df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 23669378e437SKip Macy *lp = inp->inp_lport; 23679378e437SKip Macy *fp = inp->inp_fport; 23689378e437SKip Macy } 23699378e437SKip Macy 2370dd0e6c38SKip Macy struct inpcb * 2371dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 2372dd0e6c38SKip Macy { 2373dd0e6c38SKip Macy 2374dd0e6c38SKip Macy return (sotoinpcb(so)); 2375dd0e6c38SKip Macy } 2376dd0e6c38SKip Macy 2377dd0e6c38SKip Macy struct tcpcb * 2378dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 2379dd0e6c38SKip Macy { 2380dd0e6c38SKip Macy 2381dd0e6c38SKip Macy return (sototcpcb(so)); 2382dd0e6c38SKip Macy } 2383dd0e6c38SKip Macy 2384497057eeSRobert Watson #ifdef DDB 2385497057eeSRobert Watson static void 2386497057eeSRobert Watson db_print_indent(int indent) 2387497057eeSRobert Watson { 2388497057eeSRobert Watson int i; 2389497057eeSRobert Watson 2390497057eeSRobert Watson for (i = 0; i < indent; i++) 2391497057eeSRobert Watson db_printf(" "); 2392497057eeSRobert Watson } 2393497057eeSRobert Watson 2394497057eeSRobert Watson static void 2395497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2396497057eeSRobert Watson { 2397497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 2398497057eeSRobert Watson 2399497057eeSRobert Watson db_print_indent(indent); 2400497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 2401497057eeSRobert Watson 2402497057eeSRobert Watson indent += 2; 2403497057eeSRobert Watson 240403dc38a4SRobert Watson #ifdef INET6 2405dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 2406497057eeSRobert Watson /* IPv6. */ 2407497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 2408497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 240983e521ecSBjoern A. Zeeb } else 241003dc38a4SRobert Watson #endif 241183e521ecSBjoern A. Zeeb { 2412497057eeSRobert Watson /* IPv4. */ 2413497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 2414497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 2415497057eeSRobert Watson } 2416497057eeSRobert Watson db_print_indent(indent); 2417497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2418497057eeSRobert Watson ntohs(inc->inc_lport)); 2419497057eeSRobert Watson db_print_indent(indent); 2420497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2421497057eeSRobert Watson ntohs(inc->inc_fport)); 2422497057eeSRobert Watson } 2423497057eeSRobert Watson 2424497057eeSRobert Watson static void 2425497057eeSRobert Watson db_print_inpflags(int inp_flags) 2426497057eeSRobert Watson { 2427497057eeSRobert Watson int comma; 2428497057eeSRobert Watson 2429497057eeSRobert Watson comma = 0; 2430497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 2431497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2432497057eeSRobert Watson comma = 1; 2433497057eeSRobert Watson } 2434497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 2435497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2436497057eeSRobert Watson comma = 1; 2437497057eeSRobert Watson } 2438497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 2439497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2440497057eeSRobert Watson comma = 1; 2441497057eeSRobert Watson } 2442497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 2443497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2444497057eeSRobert Watson comma = 1; 2445497057eeSRobert Watson } 2446497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 2447497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2448497057eeSRobert Watson comma = 1; 2449497057eeSRobert Watson } 2450497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 2451497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2452497057eeSRobert Watson comma = 1; 2453497057eeSRobert Watson } 2454497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 2455497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2456497057eeSRobert Watson comma = 1; 2457497057eeSRobert Watson } 2458497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 2459497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 2460497057eeSRobert Watson comma = 1; 2461497057eeSRobert Watson } 2462497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 2463497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2464497057eeSRobert Watson comma = 1; 2465497057eeSRobert Watson } 2466497057eeSRobert Watson if (inp_flags & INP_FAITH) { 2467497057eeSRobert Watson db_printf("%sINP_FAITH", comma ? ", " : ""); 2468497057eeSRobert Watson comma = 1; 2469497057eeSRobert Watson } 2470497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 2471497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2472497057eeSRobert Watson comma = 1; 2473497057eeSRobert Watson } 2474497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 2475497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2476497057eeSRobert Watson comma = 1; 2477497057eeSRobert Watson } 24783cca425bSMichael Tuexen if (inp_flags & INP_RECVTOS) { 24793cca425bSMichael Tuexen db_printf("%sINP_RECVTOS", comma ? ", " : ""); 24803cca425bSMichael Tuexen comma = 1; 24813cca425bSMichael Tuexen } 2482497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 2483497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2484497057eeSRobert Watson comma = 1; 2485497057eeSRobert Watson } 2486497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 2487497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2488497057eeSRobert Watson comma = 1; 2489497057eeSRobert Watson } 2490497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 2491497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2492497057eeSRobert Watson comma = 1; 2493497057eeSRobert Watson } 2494497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 2495497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2496497057eeSRobert Watson comma = 1; 2497497057eeSRobert Watson } 2498497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 2499497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2500497057eeSRobert Watson comma = 1; 2501497057eeSRobert Watson } 2502497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 2503497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2504497057eeSRobert Watson comma = 1; 2505497057eeSRobert Watson } 2506497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 2507497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2508497057eeSRobert Watson comma = 1; 2509497057eeSRobert Watson } 2510497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 2511497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2512497057eeSRobert Watson comma = 1; 2513497057eeSRobert Watson } 2514497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 2515497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2516497057eeSRobert Watson comma = 1; 2517497057eeSRobert Watson } 2518ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 2519ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2520ad71fe3cSRobert Watson comma = 1; 2521ad71fe3cSRobert Watson } 2522ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 2523ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2524ad71fe3cSRobert Watson comma = 1; 2525ad71fe3cSRobert Watson } 2526ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 2527ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 2528ad71fe3cSRobert Watson comma = 1; 2529ad71fe3cSRobert Watson } 2530ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 2531ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2532ad71fe3cSRobert Watson comma = 1; 2533ad71fe3cSRobert Watson } 2534497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 2535497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2536497057eeSRobert Watson comma = 1; 2537497057eeSRobert Watson } 2538497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 2539497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 2540497057eeSRobert Watson comma = 1; 2541497057eeSRobert Watson } 2542497057eeSRobert Watson } 2543497057eeSRobert Watson 2544497057eeSRobert Watson static void 2545497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 2546497057eeSRobert Watson { 2547497057eeSRobert Watson int comma; 2548497057eeSRobert Watson 2549497057eeSRobert Watson comma = 0; 2550497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 2551497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 2552497057eeSRobert Watson comma = 1; 2553497057eeSRobert Watson } 2554497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 2555497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 2556497057eeSRobert Watson comma = 1; 2557497057eeSRobert Watson } 2558497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 2559497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2560497057eeSRobert Watson comma = 1; 2561497057eeSRobert Watson } 2562497057eeSRobert Watson } 2563497057eeSRobert Watson 25646d888973SRobert Watson static void 2565497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2566497057eeSRobert Watson { 2567497057eeSRobert Watson 2568497057eeSRobert Watson db_print_indent(indent); 2569497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 2570497057eeSRobert Watson 2571497057eeSRobert Watson indent += 2; 2572497057eeSRobert Watson 2573497057eeSRobert Watson db_print_indent(indent); 2574497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2575497057eeSRobert Watson 2576497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2577497057eeSRobert Watson 2578497057eeSRobert Watson db_print_indent(indent); 2579497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2580497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2581497057eeSRobert Watson 2582497057eeSRobert Watson db_print_indent(indent); 2583497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 2584497057eeSRobert Watson inp->inp_label, inp->inp_flags); 2585497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 2586497057eeSRobert Watson db_printf(")\n"); 2587497057eeSRobert Watson 2588497057eeSRobert Watson db_print_indent(indent); 2589497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2590497057eeSRobert Watson inp->inp_vflag); 2591497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 2592497057eeSRobert Watson db_printf(")\n"); 2593497057eeSRobert Watson 2594497057eeSRobert Watson db_print_indent(indent); 2595497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2596497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2597497057eeSRobert Watson 2598497057eeSRobert Watson db_print_indent(indent); 2599497057eeSRobert Watson #ifdef INET6 2600497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 2601497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 2602497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 2603497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 2604497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2605497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2606497057eeSRobert Watson inp->in6p_hops); 2607497057eeSRobert Watson } else 2608497057eeSRobert Watson #endif 2609497057eeSRobert Watson { 2610497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 2611497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2612497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 2613497057eeSRobert Watson } 2614497057eeSRobert Watson 2615497057eeSRobert Watson db_print_indent(indent); 2616497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2617497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 2618497057eeSRobert Watson } 2619497057eeSRobert Watson 2620497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2621497057eeSRobert Watson { 2622497057eeSRobert Watson struct inpcb *inp; 2623497057eeSRobert Watson 2624497057eeSRobert Watson if (!have_addr) { 2625497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 2626497057eeSRobert Watson return; 2627497057eeSRobert Watson } 2628497057eeSRobert Watson inp = (struct inpcb *)addr; 2629497057eeSRobert Watson 2630497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 2631497057eeSRobert Watson } 263283e521ecSBjoern A. Zeeb #endif /* DDB */ 2633