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 5497057eeSRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 312469dd60SGarrett Wollman * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 344b421e2dSMike Silbersack #include <sys/cdefs.h> 354b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 364b421e2dSMike Silbersack 37497057eeSRobert Watson #include "opt_ddb.h" 38be9347e3SAdrian Chadd #include "opt_inet.h" 396a800098SYoshinobu Inoue #include "opt_ipsec.h" 40cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 41a557af22SRobert Watson #include "opt_mac.h" 42cfa1ca9dSYoshinobu Inoue 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51acd3428bSRobert Watson #include <sys/priv.h> 52df8bae1dSRodney W. Grimes #include <sys/proc.h> 5375c13541SPoul-Henning Kamp #include <sys/jail.h> 54101f9fc8SPeter Wemm #include <sys/kernel.h> 55101f9fc8SPeter Wemm #include <sys/sysctl.h> 56603724d3SBjoern A. Zeeb #include <sys/vimage.h> 578781d8e9SBruce Evans 58497057eeSRobert Watson #ifdef DDB 59497057eeSRobert Watson #include <ddb/ddb.h> 60497057eeSRobert Watson #endif 61497057eeSRobert Watson 6269c2d429SJeff Roberson #include <vm/uma.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <net/if.h> 65cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 66df8bae1dSRodney W. Grimes #include <net/route.h> 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes #include <netinet/in.h> 69df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 70df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 72340c35deSJonathan Lemon #include <netinet/tcp_var.h> 735f311da2SMike Silbersack #include <netinet/udp.h> 745f311da2SMike Silbersack #include <netinet/udp_var.h> 754b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 76cfa1ca9dSYoshinobu Inoue #ifdef INET6 77cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 78cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 794b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h> 80cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 81cfa1ca9dSYoshinobu Inoue 82df8bae1dSRodney W. Grimes 83b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 84b9234fafSSam Leffler #include <netipsec/ipsec.h> 85b9234fafSSam Leffler #include <netipsec/key.h> 86b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 87b9234fafSSam Leffler 88aed55708SRobert Watson #include <security/mac/mac_framework.h> 89aed55708SRobert Watson 9044e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 91101f9fc8SPeter Wemm /* 92101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 93101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 94101f9fc8SPeter Wemm */ 9544e33a07SMarko Zec int ipport_lowfirstauto; 9644e33a07SMarko Zec int ipport_lowlastauto; 9744e33a07SMarko Zec int ipport_firstauto; 9844e33a07SMarko Zec int ipport_lastauto; 9944e33a07SMarko Zec int ipport_hifirstauto; 10044e33a07SMarko Zec int ipport_hilastauto; 101101f9fc8SPeter Wemm 102b0d22693SCrist J. Clark /* 103b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 104b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 105b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 106b0d22693SCrist J. Clark */ 10744e33a07SMarko Zec int ipport_reservedhigh; 10844e33a07SMarko Zec int ipport_reservedlow; 109b0d22693SCrist J. Clark 1105f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 11144e33a07SMarko Zec int ipport_randomized; 11244e33a07SMarko Zec int ipport_randomcps; 11344e33a07SMarko Zec int ipport_randomtime; 11444e33a07SMarko Zec int ipport_stoprandom; 1155f311da2SMike Silbersack int ipport_tcpallocs; 1165f311da2SMike Silbersack int ipport_tcplastcount; 11744e33a07SMarko Zec #endif 1186ac48b74SMike Silbersack 119bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 120bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 121bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 122bbd42ad0SPeter Wemm 123bbd42ad0SPeter Wemm static int 12482d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 125bbd42ad0SPeter Wemm { 12697021c24SMarko Zec INIT_VNET_INET(curvnet); 12730a4ab08SBruce Evans int error; 12830a4ab08SBruce Evans 12930a4ab08SBruce Evans error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 13030a4ab08SBruce Evans if (error == 0) { 131603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 132603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 133603724d3SBjoern A. Zeeb RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 134603724d3SBjoern A. Zeeb RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 135603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 136603724d3SBjoern A. Zeeb RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 137bbd42ad0SPeter Wemm } 13830a4ab08SBruce Evans return (error); 139bbd42ad0SPeter Wemm } 140bbd42ad0SPeter Wemm 141bbd42ad0SPeter Wemm #undef RANGECHK 142bbd42ad0SPeter Wemm 14333b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 14433b3ac06SPeter Wemm 1458b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 1468b615593SMarko Zec lowfirst, CTLTYPE_INT|CTLFLAG_RW, ipport_lowfirstauto, 0, 1478b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 1488b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 1498b615593SMarko Zec lowlast, CTLTYPE_INT|CTLFLAG_RW, ipport_lowlastauto, 0, 1508b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 1518b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 1528b615593SMarko Zec first, CTLTYPE_INT|CTLFLAG_RW, ipport_firstauto, 0, 1538b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 1548b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 1558b615593SMarko Zec last, CTLTYPE_INT|CTLFLAG_RW, ipport_lastauto, 0, 1568b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 1578b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 1588b615593SMarko Zec hifirst, CTLTYPE_INT|CTLFLAG_RW, ipport_hifirstauto, 0, 1598b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 1608b615593SMarko Zec SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 1618b615593SMarko Zec hilast, CTLTYPE_INT|CTLFLAG_RW, ipport_hilastauto, 0, 1628b615593SMarko Zec &sysctl_net_ipport_check, "I", ""); 1638b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 1648b615593SMarko Zec reservedhigh, CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedhigh, 0, ""); 1658b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, reservedlow, 1668b615593SMarko Zec CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedlow, 0, ""); 1678b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomized, 1688b615593SMarko Zec CTLFLAG_RW, ipport_randomized, 0, "Enable random port allocation"); 1698b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomcps, 1708b615593SMarko Zec CTLFLAG_RW, ipport_randomcps, 0, "Maximum number of random port " 1716ee79c59SMaxim Konovalov "allocations before switching to a sequental one"); 1728b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomtime, 1738b615593SMarko Zec CTLFLAG_RW, ipport_randomtime, 0, 1748b615593SMarko Zec "Minimum time to keep sequental port " 1756ee79c59SMaxim Konovalov "allocation before switching to a random one"); 1760312fbe9SPoul-Henning Kamp 177c3229e05SDavid Greenman /* 178c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 179c3229e05SDavid Greenman * 180de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 181de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 182de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 183c3229e05SDavid Greenman */ 184c3229e05SDavid Greenman 185c3229e05SDavid Greenman /* 186c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 187d915b280SStephan Uphoff * On success return with the PCB locked. 188c3229e05SDavid Greenman */ 189df8bae1dSRodney W. Grimes int 190d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 191df8bae1dSRodney W. Grimes { 1928b615593SMarko Zec #ifdef INET6 1938b615593SMarko Zec INIT_VNET_INET6(curvnet); 1948b615593SMarko Zec #endif 195136d4f1cSRobert Watson struct inpcb *inp; 19613cf67f3SHajimu UMEMOTO int error; 197a557af22SRobert Watson 19859daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 199a557af22SRobert Watson error = 0; 200d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 201df8bae1dSRodney W. Grimes if (inp == NULL) 202df8bae1dSRodney W. Grimes return (ENOBUFS); 203d915b280SStephan Uphoff bzero(inp, inp_zero_size); 20415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 205df8bae1dSRodney W. Grimes inp->inp_socket = so; 20686d02c5cSBjoern A. Zeeb inp->inp_cred = crhold(so->so_cred); 2078b07e49aSJulian Elischer inp->inp_inc.inc_fibnum = so->so_fibnum; 208a557af22SRobert Watson #ifdef MAC 20930d239bcSRobert Watson error = mac_inpcb_init(inp, M_NOWAIT); 210a557af22SRobert Watson if (error != 0) 211a557af22SRobert Watson goto out; 212310e7cebSRobert Watson SOCK_LOCK(so); 21330d239bcSRobert Watson mac_inpcb_create(so, inp); 214310e7cebSRobert Watson SOCK_UNLOCK(so); 215a557af22SRobert Watson #endif 216b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 21713cf67f3SHajimu UMEMOTO error = ipsec_init_policy(so, &inp->inp_sp); 2180bffde27SRobert Watson if (error != 0) { 2190bffde27SRobert Watson #ifdef MAC 2200bffde27SRobert Watson mac_inpcb_destroy(inp); 2210bffde27SRobert Watson #endif 222a557af22SRobert Watson goto out; 2230bffde27SRobert Watson } 224b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 225e3fd5ffdSRobert Watson #ifdef INET6 226340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 227340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 228603724d3SBjoern A. Zeeb if (V_ip6_v6only) 22933841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 230340c35deSJonathan Lemon } 23175daea93SPaul Saab #endif 232712fc218SRobert Watson LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 2333d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 234df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 23533841545SHajimu UMEMOTO #ifdef INET6 236603724d3SBjoern A. Zeeb if (V_ip6_auto_flowlabel) 23733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 23833841545SHajimu UMEMOTO #endif 2398501a69cSRobert Watson INP_WLOCK(inp); 240d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 24128696211SRobert Watson inp->inp_refcount = 1; /* Reference from the inpcbinfo */ 242b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC) 243a557af22SRobert Watson out: 24486d02c5cSBjoern A. Zeeb if (error != 0) { 24586d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 246a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 24786d02c5cSBjoern A. Zeeb } 248a557af22SRobert Watson #endif 249a557af22SRobert Watson return (error); 250df8bae1dSRodney W. Grimes } 251df8bae1dSRodney W. Grimes 252df8bae1dSRodney W. Grimes int 253136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 254df8bae1dSRodney W. Grimes { 2554b932371SIan Dowse int anonport, error; 2564b932371SIan Dowse 2571b73ca0bSSam Leffler INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 2588501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 25959daba27SSam Leffler 2604b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 2614b932371SIan Dowse return (EINVAL); 2624b932371SIan Dowse anonport = inp->inp_lport == 0 && (nam == NULL || 2634b932371SIan Dowse ((struct sockaddr_in *)nam)->sin_port == 0); 2644b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 265b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 2664b932371SIan Dowse if (error) 2674b932371SIan Dowse return (error); 2684b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 2694b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 2704b932371SIan Dowse inp->inp_lport = 0; 2714b932371SIan Dowse return (EAGAIN); 2724b932371SIan Dowse } 2734b932371SIan Dowse if (anonport) 2744b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 2754b932371SIan Dowse return (0); 2764b932371SIan Dowse } 2774b932371SIan Dowse 2784b932371SIan Dowse /* 2794b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 2804b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 2814b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 2824b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 2834b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 2844b932371SIan Dowse * 2854b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 2864b932371SIan Dowse */ 2874b932371SIan Dowse int 288136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 289136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 2904b932371SIan Dowse { 2918b615593SMarko Zec INIT_VNET_INET(inp->inp_vnet); 2924b932371SIan Dowse struct socket *so = inp->inp_socket; 29337bd2b30SPeter Wemm unsigned short *lastport; 29415bd2b43SDavid Greenman struct sockaddr_in *sin; 295c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2964b932371SIan Dowse struct in_addr laddr; 297df8bae1dSRodney W. Grimes u_short lport = 0; 2984cc20ab1SSeigo Tanimura int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 299413628a7SBjoern A. Zeeb int error; 3005f311da2SMike Silbersack int dorandom; 301df8bae1dSRodney W. Grimes 3028501a69cSRobert Watson /* 30372bed082SRobert Watson * Because no actual state changes occur here, a global write lock on 30472bed082SRobert Watson * the pcbinfo isn't required. 3058501a69cSRobert Watson */ 3068501a69cSRobert Watson INP_INFO_LOCK_ASSERT(pcbinfo); 30759daba27SSam Leffler INP_LOCK_ASSERT(inp); 30859daba27SSam Leffler 309603724d3SBjoern A. Zeeb if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 310df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 3114b932371SIan Dowse laddr.s_addr = *laddrp; 3124b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 313df8bae1dSRodney W. Grimes return (EINVAL); 314c3229e05SDavid Greenman if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 315421d8aa6SBjoern A. Zeeb wild = INPLOOKUP_WILDCARD; 3167c2f3cb9SJamie Gritton if (nam == NULL) { 3177c2f3cb9SJamie Gritton if ((error = prison_local_ip4(cred, &laddr)) != 0) 3187c2f3cb9SJamie Gritton return (error); 3197c2f3cb9SJamie Gritton } else { 32057bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 32157bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 322df8bae1dSRodney W. Grimes return (EINVAL); 323df8bae1dSRodney W. Grimes #ifdef notdef 324df8bae1dSRodney W. Grimes /* 325df8bae1dSRodney W. Grimes * We should check the family, but old programs 326df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 327df8bae1dSRodney W. Grimes */ 328df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 329df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 330df8bae1dSRodney W. Grimes #endif 331b89e82ddSJamie Gritton error = prison_local_ip4(cred, &sin->sin_addr); 332b89e82ddSJamie Gritton if (error) 333b89e82ddSJamie Gritton return (error); 3344b932371SIan Dowse if (sin->sin_port != *lportp) { 3354b932371SIan Dowse /* Don't allow the port to change. */ 3364b932371SIan Dowse if (*lportp != 0) 3374b932371SIan Dowse return (EINVAL); 338df8bae1dSRodney W. Grimes lport = sin->sin_port; 3394b932371SIan Dowse } 3404b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 341df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 342df8bae1dSRodney W. Grimes /* 343df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 344df8bae1dSRodney W. Grimes * allow complete duplication of binding if 345df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 346df8bae1dSRodney W. Grimes * and a multicast address is bound on both 347df8bae1dSRodney W. Grimes * new and duplicated sockets. 348df8bae1dSRodney W. Grimes */ 349df8bae1dSRodney W. Grimes if (so->so_options & SO_REUSEADDR) 350df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 351df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 352df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 35383103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 3544209e01aSAdrian Chadd /* 3554209e01aSAdrian Chadd * Is the address a local IP address? 3564209e01aSAdrian Chadd * If INP_NONLOCALOK is set, then the socket may be bound 3578696873dSAdrian Chadd * to any endpoint address, local or not. 3584209e01aSAdrian Chadd */ 359be9347e3SAdrian Chadd if ( 360be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND) 361be9347e3SAdrian Chadd ((inp->inp_flags & INP_NONLOCALOK) == 0) && 362be9347e3SAdrian Chadd #endif 363be9347e3SAdrian Chadd (ifa_ifwithaddr((struct sockaddr *)sin) == 0)) 364df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 365df8bae1dSRodney W. Grimes } 3664b932371SIan Dowse laddr = sin->sin_addr; 367df8bae1dSRodney W. Grimes if (lport) { 368df8bae1dSRodney W. Grimes struct inpcb *t; 369ae0e7143SRobert Watson struct tcptw *tw; 370ae0e7143SRobert Watson 371df8bae1dSRodney W. Grimes /* GROSS */ 372603724d3SBjoern A. Zeeb if (ntohs(lport) <= V_ipport_reservedhigh && 373603724d3SBjoern A. Zeeb ntohs(lport) >= V_ipport_reservedlow && 374acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 37532f9753cSRobert Watson 0)) 3762469dd60SGarrett Wollman return (EACCES); 377835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 37886d02c5cSBjoern A. Zeeb priv_check_cred(inp->inp_cred, 37932f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 380078b7042SBjoern A. Zeeb t = in_pcblookup_local(pcbinfo, sin->sin_addr, 381413628a7SBjoern A. Zeeb lport, INPLOOKUP_WILDCARD, cred); 382340c35deSJonathan Lemon /* 383340c35deSJonathan Lemon * XXX 384340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 385340c35deSJonathan Lemon */ 3864cc20ab1SSeigo Tanimura if (t && 387ad71fe3cSRobert Watson ((t->inp_flags & INP_TIMEWAIT) == 0) && 3884658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 3894658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 3904cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 39152b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 39252b65dbeSBill Fenner (t->inp_socket->so_options & 39352b65dbeSBill Fenner SO_REUSEPORT) == 0) && 39486d02c5cSBjoern A. Zeeb (inp->inp_cred->cr_uid != 39586d02c5cSBjoern A. Zeeb t->inp_cred->cr_uid)) 3964049a042SGuido van Rooij return (EADDRINUSE); 3974049a042SGuido van Rooij } 398c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 399413628a7SBjoern A. Zeeb lport, wild, cred); 400ad71fe3cSRobert Watson if (t && (t->inp_flags & INP_TIMEWAIT)) { 401ae0e7143SRobert Watson /* 402ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 403ae0e7143SRobert Watson * state recycled, we treat the address as 404ae0e7143SRobert Watson * being in use (for now). This is better 405ae0e7143SRobert Watson * than a panic, but not desirable. 406ae0e7143SRobert Watson */ 407ae0e7143SRobert Watson tw = intotw(inp); 408ae0e7143SRobert Watson if (tw == NULL || 409ae0e7143SRobert Watson (reuseport & tw->tw_so_options) == 0) 410340c35deSJonathan Lemon return (EADDRINUSE); 411ae0e7143SRobert Watson } else if (t && 4124cc20ab1SSeigo Tanimura (reuseport & t->inp_socket->so_options) == 0) { 413e3fd5ffdSRobert Watson #ifdef INET6 41433841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 415cfa1ca9dSYoshinobu Inoue INADDR_ANY || 416cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 417cfa1ca9dSYoshinobu Inoue INADDR_ANY || 418cfa1ca9dSYoshinobu Inoue INP_SOCKAF(so) == 419cfa1ca9dSYoshinobu Inoue INP_SOCKAF(t->inp_socket)) 420e3fd5ffdSRobert Watson #endif 421df8bae1dSRodney W. Grimes return (EADDRINUSE); 422df8bae1dSRodney W. Grimes } 423cfa1ca9dSYoshinobu Inoue } 424df8bae1dSRodney W. Grimes } 4254b932371SIan Dowse if (*lportp != 0) 4264b932371SIan Dowse lport = *lportp; 42733b3ac06SPeter Wemm if (lport == 0) { 4281cf6e4f5SRui Paulo u_short first, last, aux; 429174624e0SMike Silbersack int count; 43033b3ac06SPeter Wemm 43133b3ac06SPeter Wemm if (inp->inp_flags & INP_HIGHPORT) { 432603724d3SBjoern A. Zeeb first = V_ipport_hifirstauto; /* sysctl */ 433603724d3SBjoern A. Zeeb last = V_ipport_hilastauto; 434712fc218SRobert Watson lastport = &pcbinfo->ipi_lasthi; 43533b3ac06SPeter Wemm } else if (inp->inp_flags & INP_LOWPORT) { 436acd3428bSRobert Watson error = priv_check_cred(cred, 43732f9753cSRobert Watson PRIV_NETINET_RESERVEDPORT, 0); 438acd3428bSRobert Watson if (error) 439a29f300eSGarrett Wollman return error; 440603724d3SBjoern A. Zeeb first = V_ipport_lowfirstauto; /* 1023 */ 441603724d3SBjoern A. Zeeb last = V_ipport_lowlastauto; /* 600 */ 442712fc218SRobert Watson lastport = &pcbinfo->ipi_lastlow; 44333b3ac06SPeter Wemm } else { 444603724d3SBjoern A. Zeeb first = V_ipport_firstauto; /* sysctl */ 445603724d3SBjoern A. Zeeb last = V_ipport_lastauto; 446712fc218SRobert Watson lastport = &pcbinfo->ipi_lastport; 44733b3ac06SPeter Wemm } 44833b3ac06SPeter Wemm /* 4495f311da2SMike Silbersack * For UDP, use random port allocation as long as the user 4505f311da2SMike Silbersack * allows it. For TCP (and as of yet unknown) connections, 4515f311da2SMike Silbersack * use random port allocation only if the user allows it AND 45229f2a6ecSMaxim Konovalov * ipport_tick() allows it. 4535f311da2SMike Silbersack */ 454603724d3SBjoern A. Zeeb if (V_ipport_randomized && 455603724d3SBjoern A. Zeeb (!V_ipport_stoprandom || pcbinfo == &V_udbinfo)) 4565f311da2SMike Silbersack dorandom = 1; 4575f311da2SMike Silbersack else 4585f311da2SMike Silbersack dorandom = 0; 459e99971bfSMaxim Konovalov /* 460e99971bfSMaxim Konovalov * It makes no sense to do random port allocation if 461e99971bfSMaxim Konovalov * we have the only port available. 462e99971bfSMaxim Konovalov */ 463e99971bfSMaxim Konovalov if (first == last) 464e99971bfSMaxim Konovalov dorandom = 0; 4655f311da2SMike Silbersack /* Make sure to not include UDP packets in the count. */ 466603724d3SBjoern A. Zeeb if (pcbinfo != &V_udbinfo) 467603724d3SBjoern A. Zeeb V_ipport_tcpallocs++; 4685f311da2SMike Silbersack /* 4697e1bc272SBjoern A. Zeeb * Instead of having two loops further down counting up or down 4707e1bc272SBjoern A. Zeeb * make sure that first is always <= last and go with only one 4717e1bc272SBjoern A. Zeeb * code path implementing all logic. 47233b3ac06SPeter Wemm */ 47333b3ac06SPeter Wemm if (first > last) { 4741cf6e4f5SRui Paulo aux = first; 4751cf6e4f5SRui Paulo first = last; 4761cf6e4f5SRui Paulo last = aux; 4771cf6e4f5SRui Paulo } 478174624e0SMike Silbersack 4795f311da2SMike Silbersack if (dorandom) 4806b2fc10bSMike Silbersack *lastport = first + 4816b2fc10bSMike Silbersack (arc4random() % (last - first)); 4821cf6e4f5SRui Paulo 48333b3ac06SPeter Wemm count = last - first; 484174624e0SMike Silbersack 48533b3ac06SPeter Wemm do { 4866ac48b74SMike Silbersack if (count-- < 0) /* completely used? */ 487550b1518SWes Peters return (EADDRNOTAVAIL); 48833b3ac06SPeter Wemm ++*lastport; 48933b3ac06SPeter Wemm if (*lastport < first || *lastport > last) 49033b3ac06SPeter Wemm *lastport = first; 49133b3ac06SPeter Wemm lport = htons(*lastport); 492078b7042SBjoern A. Zeeb } while (in_pcblookup_local(pcbinfo, laddr, 493078b7042SBjoern A. Zeeb lport, wild, cred)); 49433b3ac06SPeter Wemm } 4954b932371SIan Dowse *laddrp = laddr.s_addr; 4964b932371SIan Dowse *lportp = lport; 497df8bae1dSRodney W. Grimes return (0); 498df8bae1dSRodney W. Grimes } 499df8bae1dSRodney W. Grimes 500999f1343SGarrett Wollman /* 5015200e00eSIan Dowse * Connect from a socket to a specified address. 5025200e00eSIan Dowse * Both address and port must be specified in argument sin. 5035200e00eSIan Dowse * If don't have a local address for this socket yet, 5045200e00eSIan Dowse * then pick one. 505999f1343SGarrett Wollman */ 506999f1343SGarrett Wollman int 507136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 508999f1343SGarrett Wollman { 5095200e00eSIan Dowse u_short lport, fport; 5105200e00eSIan Dowse in_addr_t laddr, faddr; 5115200e00eSIan Dowse int anonport, error; 512df8bae1dSRodney W. Grimes 51327f74fd0SRobert Watson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 5148501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 51527f74fd0SRobert Watson 5165200e00eSIan Dowse lport = inp->inp_lport; 5175200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 5185200e00eSIan Dowse anonport = (lport == 0); 5195200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 520b0330ed9SPawel Jakub Dawidek NULL, cred); 5215200e00eSIan Dowse if (error) 5225200e00eSIan Dowse return (error); 5235200e00eSIan Dowse 5245200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 5255200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 5265200e00eSIan Dowse inp->inp_lport = lport; 5275200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 5285200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 5295200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 5305200e00eSIan Dowse inp->inp_lport = 0; 5315200e00eSIan Dowse return (EAGAIN); 5325200e00eSIan Dowse } 5335200e00eSIan Dowse } 5345200e00eSIan Dowse 5355200e00eSIan Dowse /* Commit the remaining changes. */ 5365200e00eSIan Dowse inp->inp_lport = lport; 5375200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 5385200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 5395200e00eSIan Dowse inp->inp_fport = fport; 5405200e00eSIan Dowse in_pcbrehash(inp); 5412cb64cb2SGeorge V. Neville-Neil 5425200e00eSIan Dowse if (anonport) 5435200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 5445200e00eSIan Dowse return (0); 5455200e00eSIan Dowse } 5465200e00eSIan Dowse 5475200e00eSIan Dowse /* 5480895aec3SBjoern A. Zeeb * Do proper source address selection on an unbound socket in case 5490895aec3SBjoern A. Zeeb * of connect. Take jails into account as well. 5500895aec3SBjoern A. Zeeb */ 5510895aec3SBjoern A. Zeeb static int 5520895aec3SBjoern A. Zeeb in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 5530895aec3SBjoern A. Zeeb struct ucred *cred) 5540895aec3SBjoern A. Zeeb { 5550895aec3SBjoern A. Zeeb struct in_ifaddr *ia; 5560895aec3SBjoern A. Zeeb struct ifaddr *ifa; 5570895aec3SBjoern A. Zeeb struct sockaddr *sa; 5580895aec3SBjoern A. Zeeb struct sockaddr_in *sin; 5590895aec3SBjoern A. Zeeb struct route sro; 5600895aec3SBjoern A. Zeeb int error; 5610895aec3SBjoern A. Zeeb 562413628a7SBjoern A. Zeeb KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 5630895aec3SBjoern A. Zeeb 5640895aec3SBjoern A. Zeeb error = 0; 5650895aec3SBjoern A. Zeeb ia = NULL; 5660895aec3SBjoern A. Zeeb bzero(&sro, sizeof(sro)); 5670895aec3SBjoern A. Zeeb 5680895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)&sro.ro_dst; 5690895aec3SBjoern A. Zeeb sin->sin_family = AF_INET; 5700895aec3SBjoern A. Zeeb sin->sin_len = sizeof(struct sockaddr_in); 5710895aec3SBjoern A. Zeeb sin->sin_addr.s_addr = faddr->s_addr; 5720895aec3SBjoern A. Zeeb 5730895aec3SBjoern A. Zeeb /* 5740895aec3SBjoern A. Zeeb * If route is known our src addr is taken from the i/f, 5750895aec3SBjoern A. Zeeb * else punt. 5760895aec3SBjoern A. Zeeb * 5770895aec3SBjoern A. Zeeb * Find out route to destination. 5780895aec3SBjoern A. Zeeb */ 5790895aec3SBjoern A. Zeeb if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 5806e6b3f7cSQing Li in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 5810895aec3SBjoern A. Zeeb 5820895aec3SBjoern A. Zeeb /* 5830895aec3SBjoern A. Zeeb * If we found a route, use the address corresponding to 5840895aec3SBjoern A. Zeeb * the outgoing interface. 5850895aec3SBjoern A. Zeeb * 5860895aec3SBjoern A. Zeeb * Otherwise assume faddr is reachable on a directly connected 5870895aec3SBjoern A. Zeeb * network and try to find a corresponding interface to take 5880895aec3SBjoern A. Zeeb * the source address from. 5890895aec3SBjoern A. Zeeb */ 5900895aec3SBjoern A. Zeeb if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 5910895aec3SBjoern A. Zeeb struct ifnet *ifp; 5920895aec3SBjoern A. Zeeb 5930895aec3SBjoern A. Zeeb ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin)); 5940895aec3SBjoern A. Zeeb if (ia == NULL) 5950895aec3SBjoern A. Zeeb ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin)); 5960895aec3SBjoern A. Zeeb if (ia == NULL) { 5970895aec3SBjoern A. Zeeb error = ENETUNREACH; 5980895aec3SBjoern A. Zeeb goto done; 5990895aec3SBjoern A. Zeeb } 6000895aec3SBjoern A. Zeeb 6010895aec3SBjoern A. Zeeb if (cred == NULL || !jailed(cred)) { 6020895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6030895aec3SBjoern A. Zeeb goto done; 6040895aec3SBjoern A. Zeeb } 6050895aec3SBjoern A. Zeeb 6060895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 6070895aec3SBjoern A. Zeeb ia = NULL; 6089317b04eSRobert Watson IF_ADDR_LOCK(ifp); 6090895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 6100895aec3SBjoern A. Zeeb 6110895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 6120895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 6130895aec3SBjoern A. Zeeb continue; 6140895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 615b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 6160895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 6170895aec3SBjoern A. Zeeb break; 6180895aec3SBjoern A. Zeeb } 6190895aec3SBjoern A. Zeeb } 6200895aec3SBjoern A. Zeeb if (ia != NULL) { 6210895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6229317b04eSRobert Watson IF_ADDR_UNLOCK(ifp); 6230895aec3SBjoern A. Zeeb goto done; 6240895aec3SBjoern A. Zeeb } 6259317b04eSRobert Watson IF_ADDR_UNLOCK(ifp); 6260895aec3SBjoern A. Zeeb 6270895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 628b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 6290895aec3SBjoern A. Zeeb goto done; 6300895aec3SBjoern A. Zeeb } 6310895aec3SBjoern A. Zeeb 6320895aec3SBjoern A. Zeeb /* 6330895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 6340895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 6350895aec3SBjoern A. Zeeb * In case of jails do those three steps: 6360895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 6370895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 6380895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 6390895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 6400895aec3SBjoern A. Zeeb */ 6410895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 6429317b04eSRobert Watson struct ifnet *ifp; 6430895aec3SBjoern A. Zeeb 6440895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 6450895aec3SBjoern A. Zeeb if (cred == NULL || !jailed(cred)) { 6460895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 6470895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6480895aec3SBjoern A. Zeeb goto done; 6490895aec3SBjoern A. Zeeb } 6500895aec3SBjoern A. Zeeb 6510895aec3SBjoern A. Zeeb /* Jailed. */ 6520895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 6530895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 654b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 6550895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 6560895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6570895aec3SBjoern A. Zeeb goto done; 6580895aec3SBjoern A. Zeeb } 6590895aec3SBjoern A. Zeeb 6600895aec3SBjoern A. Zeeb /* 6610895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 6620895aec3SBjoern A. Zeeb * belonging to this jail. 6630895aec3SBjoern A. Zeeb */ 6649317b04eSRobert Watson ifp = sro.ro_rt->rt_ifp; 6659317b04eSRobert Watson IF_ADDR_LOCK(ifp); 6669317b04eSRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 6670895aec3SBjoern A. Zeeb 6680895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 6690895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 6700895aec3SBjoern A. Zeeb continue; 6710895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 672b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 6730895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 6740895aec3SBjoern A. Zeeb break; 6750895aec3SBjoern A. Zeeb } 6760895aec3SBjoern A. Zeeb } 6770895aec3SBjoern A. Zeeb if (ia != NULL) { 6780895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6799317b04eSRobert Watson IF_ADDR_UNLOCK(ifp); 6800895aec3SBjoern A. Zeeb goto done; 6810895aec3SBjoern A. Zeeb } 6829317b04eSRobert Watson IF_ADDR_UNLOCK(ifp); 6830895aec3SBjoern A. Zeeb 6840895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 685b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 6860895aec3SBjoern A. Zeeb goto done; 6870895aec3SBjoern A. Zeeb } 6880895aec3SBjoern A. Zeeb 6890895aec3SBjoern A. Zeeb /* 6900895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 6910895aec3SBjoern A. Zeeb * to ourselves is here. 6920895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 6930895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 6940895aec3SBjoern A. Zeeb * a loopback interface. 6950895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 6960895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 6970895aec3SBjoern A. Zeeb */ 6980895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 6990895aec3SBjoern A. Zeeb struct sockaddr_in sain; 7000895aec3SBjoern A. Zeeb 7010895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 7020895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 7030895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 7040895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 7050895aec3SBjoern A. Zeeb 7060895aec3SBjoern A. Zeeb ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain))); 7070895aec3SBjoern A. Zeeb if (ia == NULL) 7080895aec3SBjoern A. Zeeb ia = ifatoia(ifa_ifwithnet(sintosa(&sain))); 7090895aec3SBjoern A. Zeeb 7100895aec3SBjoern A. Zeeb if (cred == NULL || !jailed(cred)) { 71103d8b6fdSBjoern A. Zeeb #if __FreeBSD_version < 800000 71203d8b6fdSBjoern A. Zeeb if (ia == NULL) 71303d8b6fdSBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 71403d8b6fdSBjoern A. Zeeb #endif 7150895aec3SBjoern A. Zeeb if (ia == NULL) { 7160895aec3SBjoern A. Zeeb error = ENETUNREACH; 7170895aec3SBjoern A. Zeeb goto done; 7180895aec3SBjoern A. Zeeb } 7190895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 7200895aec3SBjoern A. Zeeb goto done; 7210895aec3SBjoern A. Zeeb } 7220895aec3SBjoern A. Zeeb 7230895aec3SBjoern A. Zeeb /* Jailed. */ 7240895aec3SBjoern A. Zeeb if (ia != NULL) { 7250895aec3SBjoern A. Zeeb struct ifnet *ifp; 7260895aec3SBjoern A. Zeeb 7270895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 7280895aec3SBjoern A. Zeeb ia = NULL; 7299317b04eSRobert Watson IF_ADDR_LOCK(ifp); 7300895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 7310895aec3SBjoern A. Zeeb 7320895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 7330895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 7340895aec3SBjoern A. Zeeb continue; 7350895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 736b89e82ddSJamie Gritton if (prison_check_ip4(cred, 737b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 7380895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 7390895aec3SBjoern A. Zeeb break; 7400895aec3SBjoern A. Zeeb } 7410895aec3SBjoern A. Zeeb } 7420895aec3SBjoern A. Zeeb if (ia != NULL) { 7430895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 7449317b04eSRobert Watson IF_ADDR_UNLOCK(ifp); 7450895aec3SBjoern A. Zeeb goto done; 7460895aec3SBjoern A. Zeeb } 7479317b04eSRobert Watson IF_ADDR_UNLOCK(ifp); 7480895aec3SBjoern A. Zeeb } 7490895aec3SBjoern A. Zeeb 7500895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 751b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 7520895aec3SBjoern A. Zeeb goto done; 7530895aec3SBjoern A. Zeeb } 7540895aec3SBjoern A. Zeeb 7550895aec3SBjoern A. Zeeb done: 7560895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 7570895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 7580895aec3SBjoern A. Zeeb return (error); 7590895aec3SBjoern A. Zeeb } 7600895aec3SBjoern A. Zeeb 7610895aec3SBjoern A. Zeeb /* 7625200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 7635200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 7645200e00eSIan Dowse * address and port for the PCB; these are updated to the values 7655200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 7665200e00eSIan Dowse * the connect. 7675200e00eSIan Dowse * 7685200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 7695200e00eSIan Dowse * and port. These are not updated in the error case. 7705200e00eSIan Dowse * 7715200e00eSIan Dowse * If the operation fails because the connection already exists, 7725200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 7735200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 7745200e00eSIan Dowse * is set to NULL. 7755200e00eSIan Dowse */ 7765200e00eSIan Dowse int 777136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 778136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 779136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 7805200e00eSIan Dowse { 7818b615593SMarko Zec INIT_VNET_INET(inp->inp_vnet); 7825200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 7835200e00eSIan Dowse struct in_ifaddr *ia; 7845200e00eSIan Dowse struct inpcb *oinp; 785b89e82ddSJamie Gritton struct in_addr laddr, faddr; 7865200e00eSIan Dowse u_short lport, fport; 7875200e00eSIan Dowse int error; 7885200e00eSIan Dowse 7898501a69cSRobert Watson /* 7908501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 7918501a69cSRobert Watson * lock is sufficient. 7928501a69cSRobert Watson */ 7938501a69cSRobert Watson INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo); 79427f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 79527f74fd0SRobert Watson 7965200e00eSIan Dowse if (oinpp != NULL) 7975200e00eSIan Dowse *oinpp = NULL; 79857bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 799df8bae1dSRodney W. Grimes return (EINVAL); 800df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 801df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 802df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 803df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 8045200e00eSIan Dowse laddr.s_addr = *laddrp; 8055200e00eSIan Dowse lport = *lportp; 8065200e00eSIan Dowse faddr = sin->sin_addr; 8075200e00eSIan Dowse fport = sin->sin_port; 8080895aec3SBjoern A. Zeeb 809603724d3SBjoern A. Zeeb if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 810df8bae1dSRodney W. Grimes /* 811df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 812df8bae1dSRodney W. Grimes * use the primary local address. 813df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 814df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 815df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 816df8bae1dSRodney W. Grimes */ 817413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 818413628a7SBjoern A. Zeeb faddr = 819b89e82ddSJamie Gritton IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 820b89e82ddSJamie Gritton if (cred != NULL && 821b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 822b89e82ddSJamie Gritton return (error); 823413628a7SBjoern A. Zeeb } else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 824603724d3SBjoern A. Zeeb (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 8255200e00eSIan Dowse IFF_BROADCAST)) 8265200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 827603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 828df8bae1dSRodney W. Grimes } 8295200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 8300895aec3SBjoern A. Zeeb error = in_pcbladdr(inp, &faddr, &laddr, cred); 8310895aec3SBjoern A. Zeeb if (error) 8320895aec3SBjoern A. Zeeb return (error); 833df8bae1dSRodney W. Grimes 834df8bae1dSRodney W. Grimes /* 835df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 836df8bae1dSRodney W. Grimes * interface has been set as a multicast option, use the 837df8bae1dSRodney W. Grimes * address of that interface as our source address. 838df8bae1dSRodney W. Grimes */ 8395200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 840df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 841df8bae1dSRodney W. Grimes struct ip_moptions *imo; 842df8bae1dSRodney W. Grimes struct ifnet *ifp; 843df8bae1dSRodney W. Grimes 844df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 845df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 846df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 847603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 848df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp) 849df8bae1dSRodney W. Grimes break; 8508699ea08SBjoern A. Zeeb if (ia == NULL) 851df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 8525200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 853999f1343SGarrett Wollman } 8540895aec3SBjoern A. Zeeb } 8550895aec3SBjoern A. Zeeb } 856999f1343SGarrett Wollman 8575200e00eSIan Dowse oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 8585200e00eSIan Dowse 0, NULL); 8595200e00eSIan Dowse if (oinp != NULL) { 8605200e00eSIan Dowse if (oinpp != NULL) 8615200e00eSIan Dowse *oinpp = oinp; 862df8bae1dSRodney W. Grimes return (EADDRINUSE); 863c3229e05SDavid Greenman } 8645200e00eSIan Dowse if (lport == 0) { 865b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 866b0330ed9SPawel Jakub Dawidek cred); 8675a903f8dSPierre Beyssac if (error) 8685a903f8dSPierre Beyssac return (error); 8695a903f8dSPierre Beyssac } 8705200e00eSIan Dowse *laddrp = laddr.s_addr; 8715200e00eSIan Dowse *lportp = lport; 8725200e00eSIan Dowse *faddrp = faddr.s_addr; 8735200e00eSIan Dowse *fportp = fport; 874df8bae1dSRodney W. Grimes return (0); 875df8bae1dSRodney W. Grimes } 876df8bae1dSRodney W. Grimes 87726f9a767SRodney W. Grimes void 878136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 879df8bae1dSRodney W. Grimes { 8806b348152SRobert Watson 881fe6bfc37SRobert Watson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 8828501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 883df8bae1dSRodney W. Grimes 884df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 885df8bae1dSRodney W. Grimes inp->inp_fport = 0; 88615bd2b43SDavid Greenman in_pcbrehash(inp); 887df8bae1dSRodney W. Grimes } 888df8bae1dSRodney W. Grimes 8894c7c478dSRobert Watson /* 89028696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 891c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 89228696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 89328696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 8944c7c478dSRobert Watson */ 89526f9a767SRodney W. Grimes void 896136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 897df8bae1dSRodney W. Grimes { 8984c7c478dSRobert Watson 899a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 900c0a211c5SRobert Watson 9014c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 9024c7c478dSRobert Watson inp->inp_socket = NULL; 9034c7c478dSRobert Watson } 9044c7c478dSRobert Watson 905c0a211c5SRobert Watson /* 90628696211SRobert Watson * in_pcbfree_internal() frees an inpcb that has been detached from its 90728696211SRobert Watson * socket, and whose reference count has reached 0. It will also remove the 90828696211SRobert Watson * inpcb from any global lists it might remain on. 909c0a211c5SRobert Watson */ 91028696211SRobert Watson static void 91128696211SRobert Watson in_pcbfree_internal(struct inpcb *inp) 9124c7c478dSRobert Watson { 9133d4d47f3SGarrett Wollman struct inpcbinfo *ipi = inp->inp_pcbinfo; 914df8bae1dSRodney W. Grimes 915a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 91628696211SRobert Watson KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__)); 9178501a69cSRobert Watson 918fe6bfc37SRobert Watson INP_INFO_WLOCK_ASSERT(ipi); 9198501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 92059daba27SSam Leffler 921b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 9226aee2fc5SBjoern A. Zeeb if (inp->inp_sp != NULL) 9236974bd9eSBjoern A. Zeeb ipsec_delete_pcbpolicy(inp); 924b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 9253d4d47f3SGarrett Wollman inp->inp_gencnt = ++ipi->ipi_gencnt; 926c3229e05SDavid Greenman in_pcbremlists(inp); 9276aee2fc5SBjoern A. Zeeb #ifdef INET6 9286aee2fc5SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 9296aee2fc5SBjoern A. Zeeb ip6_freepcbopts(inp->in6p_outputopts); 9306aee2fc5SBjoern A. Zeeb ip6_freemoptions(inp->in6p_moptions); 9316aee2fc5SBjoern A. Zeeb } 9326aee2fc5SBjoern A. Zeeb #endif 933df8bae1dSRodney W. Grimes if (inp->inp_options) 934df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 93571498f30SBruce M Simpson if (inp->inp_moptions != NULL) 93671498f30SBruce M Simpson inp_freemoptions(inp->inp_moptions); 937cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 93886d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 939d915b280SStephan Uphoff 940a557af22SRobert Watson #ifdef MAC 94130d239bcSRobert Watson mac_inpcb_destroy(inp); 942a557af22SRobert Watson #endif 9438501a69cSRobert Watson INP_WUNLOCK(inp); 94469c2d429SJeff Roberson uma_zfree(ipi->ipi_zone, inp); 945df8bae1dSRodney W. Grimes } 946df8bae1dSRodney W. Grimes 94710702a28SRobert Watson /* 94828696211SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 94928696211SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 95028696211SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 95128696211SRobert Watson * but where the inpcb lock is already held. 95228696211SRobert Watson * 95328696211SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 95428696211SRobert Watson * connection's state may change, so the caller should be careful to 95528696211SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 95628696211SRobert Watson * using in_pcbrele(). 95728696211SRobert Watson */ 95828696211SRobert Watson void 95928696211SRobert Watson in_pcbref(struct inpcb *inp) 96028696211SRobert Watson { 96128696211SRobert Watson 96228696211SRobert Watson INP_WLOCK_ASSERT(inp); 96328696211SRobert Watson 96428696211SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 96528696211SRobert Watson 96628696211SRobert Watson inp->inp_refcount++; 96728696211SRobert Watson } 96828696211SRobert Watson 96928696211SRobert Watson /* 97028696211SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 97128696211SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 97228696211SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 97328696211SRobert Watson * valid, we return with the inpcb lock held. 97428696211SRobert Watson */ 97528696211SRobert Watson int 97628696211SRobert Watson in_pcbrele(struct inpcb *inp) 97728696211SRobert Watson { 97828696211SRobert Watson #ifdef INVARIANTS 97928696211SRobert Watson struct inpcbinfo *ipi = inp->inp_pcbinfo; 98028696211SRobert Watson #endif 98128696211SRobert Watson 98228696211SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 98328696211SRobert Watson 98428696211SRobert Watson INP_INFO_WLOCK_ASSERT(ipi); 98528696211SRobert Watson INP_WLOCK_ASSERT(inp); 98628696211SRobert Watson 98728696211SRobert Watson inp->inp_refcount--; 98828696211SRobert Watson if (inp->inp_refcount > 0) 98928696211SRobert Watson return (0); 99028696211SRobert Watson in_pcbfree_internal(inp); 99128696211SRobert Watson return (1); 99228696211SRobert Watson } 99328696211SRobert Watson 99428696211SRobert Watson /* 99528696211SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 99628696211SRobert Watson * reference count, which should occur only after the inpcb has been detached 99728696211SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 99828696211SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 99928696211SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. 100028696211SRobert Watson */ 100128696211SRobert Watson void 100228696211SRobert Watson in_pcbfree(struct inpcb *inp) 100328696211SRobert Watson { 100428696211SRobert Watson #ifdef INVARIANTS 100528696211SRobert Watson struct inpcbinfo *ipi = inp->inp_pcbinfo; 100628696211SRobert Watson #endif 100728696211SRobert Watson 100828696211SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", 100928696211SRobert Watson __func__)); 101028696211SRobert Watson 101128696211SRobert Watson INP_INFO_WLOCK_ASSERT(ipi); 101228696211SRobert Watson INP_WLOCK_ASSERT(inp); 101328696211SRobert Watson 101428696211SRobert Watson if (!in_pcbrele(inp)) 101528696211SRobert Watson INP_WUNLOCK(inp); 101628696211SRobert Watson } 101728696211SRobert Watson 101828696211SRobert Watson /* 1019c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1020c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1021c0a211c5SRobert Watson * 1022c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1023c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1024c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1025c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1026c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1027c0a211c5SRobert Watson * in_pcbdetach(). 1028c0a211c5SRobert Watson * 1029c0a211c5SRobert Watson * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash 1030c0a211c5SRobert Watson * lists, but can lead to confusing netstat output, as open sockets with 1031c0a211c5SRobert Watson * closed TCP connections will no longer appear to have their bound port 1032c0a211c5SRobert Watson * number. An explicit flag would be better, as it would allow us to leave 1033c0a211c5SRobert Watson * the port number intact after the connection is dropped. 1034c0a211c5SRobert Watson * 1035c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1036c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 103710702a28SRobert Watson */ 103810702a28SRobert Watson void 103910702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 104010702a28SRobert Watson { 104110702a28SRobert Watson 10427c5a8ab2SMarcel Moolenaar INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 10438501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 104410702a28SRobert Watson 1045ad71fe3cSRobert Watson inp->inp_flags |= INP_DROPPED; 1046111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 104710702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 104810702a28SRobert Watson 104910702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 105010702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 105110702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 105210702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 105310702a28SRobert Watson free(phd, M_PCB); 105410702a28SRobert Watson } 1055111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 105610702a28SRobert Watson } 105710702a28SRobert Watson } 105810702a28SRobert Watson 105954d642bbSRobert Watson /* 106054d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 106154d642bbSRobert Watson */ 106226ef6ac4SDon Lewis struct sockaddr * 1063136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 106426ef6ac4SDon Lewis { 106526ef6ac4SDon Lewis struct sockaddr_in *sin; 106626ef6ac4SDon Lewis 10671ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1068a163d034SWarner Losh M_WAITOK | M_ZERO); 106926ef6ac4SDon Lewis sin->sin_family = AF_INET; 107026ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 107126ef6ac4SDon Lewis sin->sin_addr = *addr_p; 107226ef6ac4SDon Lewis sin->sin_port = port; 107326ef6ac4SDon Lewis 107426ef6ac4SDon Lewis return (struct sockaddr *)sin; 107526ef6ac4SDon Lewis } 107626ef6ac4SDon Lewis 1077117bcae7SGarrett Wollman int 107854d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1079df8bae1dSRodney W. Grimes { 1080136d4f1cSRobert Watson struct inpcb *inp; 108126ef6ac4SDon Lewis struct in_addr addr; 108226ef6ac4SDon Lewis in_port_t port; 108342fa505bSDavid Greenman 1084fdc984f7STor Egge inp = sotoinpcb(so); 108554d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 10866466b28aSRobert Watson 1087a69042a5SRobert Watson INP_RLOCK(inp); 108826ef6ac4SDon Lewis port = inp->inp_lport; 108926ef6ac4SDon Lewis addr = inp->inp_laddr; 1090a69042a5SRobert Watson INP_RUNLOCK(inp); 109142fa505bSDavid Greenman 109226ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1093117bcae7SGarrett Wollman return 0; 1094df8bae1dSRodney W. Grimes } 1095df8bae1dSRodney W. Grimes 1096117bcae7SGarrett Wollman int 109754d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1098df8bae1dSRodney W. Grimes { 1099136d4f1cSRobert Watson struct inpcb *inp; 110026ef6ac4SDon Lewis struct in_addr addr; 110126ef6ac4SDon Lewis in_port_t port; 110242fa505bSDavid Greenman 1103fdc984f7STor Egge inp = sotoinpcb(so); 110454d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 11056466b28aSRobert Watson 1106a69042a5SRobert Watson INP_RLOCK(inp); 110726ef6ac4SDon Lewis port = inp->inp_fport; 110826ef6ac4SDon Lewis addr = inp->inp_faddr; 1109a69042a5SRobert Watson INP_RUNLOCK(inp); 111042fa505bSDavid Greenman 111126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1112117bcae7SGarrett Wollman return 0; 1113df8bae1dSRodney W. Grimes } 1114df8bae1dSRodney W. Grimes 111526f9a767SRodney W. Grimes void 1116136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1117136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1118d1c54148SJesper Skriver { 1119f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1120d1c54148SJesper Skriver 11213dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1122f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 11238501a69cSRobert Watson INP_WLOCK(inp); 1124d1c54148SJesper Skriver #ifdef INET6 1125f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 11268501a69cSRobert Watson INP_WUNLOCK(inp); 1127d1c54148SJesper Skriver continue; 1128f76fcf6dSJeffrey Hsu } 1129d1c54148SJesper Skriver #endif 1130d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1131f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 11328501a69cSRobert Watson INP_WUNLOCK(inp); 1133d1c54148SJesper Skriver continue; 1134d1c54148SJesper Skriver } 11353dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 11368501a69cSRobert Watson INP_WUNLOCK(inp); 1137f76fcf6dSJeffrey Hsu } 11383dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1139d1c54148SJesper Skriver } 1140d1c54148SJesper Skriver 1141e43cc4aeSHajimu UMEMOTO void 1142136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1143e43cc4aeSHajimu UMEMOTO { 1144e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1145e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1146e43cc4aeSHajimu UMEMOTO int i, gap; 1147e43cc4aeSHajimu UMEMOTO 1148f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(pcbinfo); 1149712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 11508501a69cSRobert Watson INP_WLOCK(inp); 1151e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1152e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1153e43cc4aeSHajimu UMEMOTO imo != NULL) { 1154e43cc4aeSHajimu UMEMOTO /* 1155e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1156e43cc4aeSHajimu UMEMOTO * detached. 1157e43cc4aeSHajimu UMEMOTO */ 1158e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1159e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1160e43cc4aeSHajimu UMEMOTO 1161e43cc4aeSHajimu UMEMOTO /* 1162e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1163e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1164e43cc4aeSHajimu UMEMOTO */ 1165e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1166e43cc4aeSHajimu UMEMOTO i++) { 1167e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1168e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 1169e43cc4aeSHajimu UMEMOTO gap++; 1170e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1171e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1172e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1173e43cc4aeSHajimu UMEMOTO } 1174e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1175e43cc4aeSHajimu UMEMOTO } 11768501a69cSRobert Watson INP_WUNLOCK(inp); 1177e43cc4aeSHajimu UMEMOTO } 11783cfcc388SJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 1179e43cc4aeSHajimu UMEMOTO } 1180e43cc4aeSHajimu UMEMOTO 1181df8bae1dSRodney W. Grimes /* 1182c3229e05SDavid Greenman * Lookup a PCB based on the local address and port. 1183c3229e05SDavid Greenman */ 1184d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1185df8bae1dSRodney W. Grimes struct inpcb * 1186136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 1187078b7042SBjoern A. Zeeb u_short lport, int wild_okay, struct ucred *cred) 1188df8bae1dSRodney W. Grimes { 1189136d4f1cSRobert Watson struct inpcb *inp; 1190d5e8a67eSHajimu UMEMOTO #ifdef INET6 1191d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1192d5e8a67eSHajimu UMEMOTO #else 1193d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1194d5e8a67eSHajimu UMEMOTO #endif 1195d5e8a67eSHajimu UMEMOTO int wildcard; 11967bc4aca7SDavid Greenman 11978501a69cSRobert Watson INP_INFO_LOCK_ASSERT(pcbinfo); 11981b73ca0bSSam Leffler 1199c3229e05SDavid Greenman if (!wild_okay) { 1200c3229e05SDavid Greenman struct inpcbhead *head; 1201c3229e05SDavid Greenman /* 1202c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1203c3229e05SDavid Greenman * matches the local address and port we're looking for. 1204c3229e05SDavid Greenman */ 1205712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1206712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1207fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1208cfa1ca9dSYoshinobu Inoue #ifdef INET6 1209413628a7SBjoern A. Zeeb /* XXX inp locking */ 1210369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1211cfa1ca9dSYoshinobu Inoue continue; 1212cfa1ca9dSYoshinobu Inoue #endif 1213c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1214c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1215c3229e05SDavid Greenman inp->inp_lport == lport) { 1216c3229e05SDavid Greenman /* 1217413628a7SBjoern A. Zeeb * Found? 1218c3229e05SDavid Greenman */ 1219413628a7SBjoern A. Zeeb if (cred == NULL || 1220413628a7SBjoern A. Zeeb inp->inp_cred->cr_prison == cred->cr_prison) 1221c3229e05SDavid Greenman return (inp); 1222df8bae1dSRodney W. Grimes } 1223c3229e05SDavid Greenman } 1224c3229e05SDavid Greenman /* 1225c3229e05SDavid Greenman * Not found. 1226c3229e05SDavid Greenman */ 1227c3229e05SDavid Greenman return (NULL); 1228c3229e05SDavid Greenman } else { 1229c3229e05SDavid Greenman struct inpcbporthead *porthash; 1230c3229e05SDavid Greenman struct inpcbport *phd; 1231c3229e05SDavid Greenman struct inpcb *match = NULL; 1232c3229e05SDavid Greenman /* 1233c3229e05SDavid Greenman * Best fit PCB lookup. 1234c3229e05SDavid Greenman * 1235c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1236c3229e05SDavid Greenman * port hash list. 1237c3229e05SDavid Greenman */ 1238712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1239712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1240fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1241c3229e05SDavid Greenman if (phd->phd_port == lport) 1242c3229e05SDavid Greenman break; 1243c3229e05SDavid Greenman } 1244c3229e05SDavid Greenman if (phd != NULL) { 1245c3229e05SDavid Greenman /* 1246c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1247c3229e05SDavid Greenman * fit. 1248c3229e05SDavid Greenman */ 124937d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1250c3229e05SDavid Greenman wildcard = 0; 1251413628a7SBjoern A. Zeeb if (cred != NULL && 1252413628a7SBjoern A. Zeeb inp->inp_cred->cr_prison != cred->cr_prison) 1253413628a7SBjoern A. Zeeb continue; 1254cfa1ca9dSYoshinobu Inoue #ifdef INET6 1255413628a7SBjoern A. Zeeb /* XXX inp locking */ 1256369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1257cfa1ca9dSYoshinobu Inoue continue; 1258d5e8a67eSHajimu UMEMOTO /* 1259d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1260d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1261d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1262d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1263d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1264d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1265d5e8a67eSHajimu UMEMOTO * 1266d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1267d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1268d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1269d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1270d5e8a67eSHajimu UMEMOTO */ 1271d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1272d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1273cfa1ca9dSYoshinobu Inoue #endif 1274c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1275c3229e05SDavid Greenman wildcard++; 127615bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 127715bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 127815bd2b43SDavid Greenman wildcard++; 127915bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 128015bd2b43SDavid Greenman continue; 128115bd2b43SDavid Greenman } else { 128215bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 128315bd2b43SDavid Greenman wildcard++; 128415bd2b43SDavid Greenman } 1285df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1286df8bae1dSRodney W. Grimes match = inp; 1287df8bae1dSRodney W. Grimes matchwild = wildcard; 1288413628a7SBjoern A. Zeeb if (matchwild == 0) 1289df8bae1dSRodney W. Grimes break; 1290df8bae1dSRodney W. Grimes } 1291df8bae1dSRodney W. Grimes } 12923dbdc25cSDavid Greenman } 1293df8bae1dSRodney W. Grimes return (match); 1294df8bae1dSRodney W. Grimes } 1295c3229e05SDavid Greenman } 1296d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 129715bd2b43SDavid Greenman 129815bd2b43SDavid Greenman /* 129915bd2b43SDavid Greenman * Lookup PCB in hash list. 130015bd2b43SDavid Greenman */ 130115bd2b43SDavid Greenman struct inpcb * 1302136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1303136d4f1cSRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard, 1304136d4f1cSRobert Watson struct ifnet *ifp) 130515bd2b43SDavid Greenman { 130615bd2b43SDavid Greenman struct inpcbhead *head; 1307413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 130815bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 130915bd2b43SDavid Greenman 13108501a69cSRobert Watson INP_INFO_LOCK_ASSERT(pcbinfo); 1311602cc7f1SRobert Watson 131215bd2b43SDavid Greenman /* 131315bd2b43SDavid Greenman * First look for an exact match. 131415bd2b43SDavid Greenman */ 1315413628a7SBjoern A. Zeeb tmpinp = NULL; 1316712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1317712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1318fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1319cfa1ca9dSYoshinobu Inoue #ifdef INET6 1320413628a7SBjoern A. Zeeb /* XXX inp locking */ 1321369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1322cfa1ca9dSYoshinobu Inoue continue; 1323cfa1ca9dSYoshinobu Inoue #endif 13246d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1325ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1326ca98b82cSDavid Greenman inp->inp_fport == fport && 1327413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1328413628a7SBjoern A. Zeeb /* 1329413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1330413628a7SBjoern A. Zeeb * the inp here, without any checks. 1331413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1332413628a7SBjoern A. Zeeb */ 1333413628a7SBjoern A. Zeeb if (jailed(inp->inp_cred)) 1334c3229e05SDavid Greenman return (inp); 1335413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1336413628a7SBjoern A. Zeeb tmpinp = inp; 1337c3229e05SDavid Greenman } 1338413628a7SBjoern A. Zeeb } 1339413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1340413628a7SBjoern A. Zeeb return (tmpinp); 1341e3fd5ffdSRobert Watson 1342e3fd5ffdSRobert Watson /* 1343e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1344e3fd5ffdSRobert Watson */ 1345413628a7SBjoern A. Zeeb if (wildcard == INPLOOKUP_WILDCARD) { 1346413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1347e3fd5ffdSRobert Watson #ifdef INET6 1348cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1349e3fd5ffdSRobert Watson #endif 1350413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1351413628a7SBjoern A. Zeeb int injail; 1352413628a7SBjoern A. Zeeb 1353413628a7SBjoern A. Zeeb /* 1354413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 1355413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 1356413628a7SBjoern A. Zeeb * 2. jailed, wild. 1357413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 1358413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 1359413628a7SBjoern A. Zeeb */ 13606d6a026bSDavid Greenman 1361712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1362712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1363fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1364cfa1ca9dSYoshinobu Inoue #ifdef INET6 1365413628a7SBjoern A. Zeeb /* XXX inp locking */ 1366369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1367cfa1ca9dSYoshinobu Inoue continue; 1368cfa1ca9dSYoshinobu Inoue #endif 1369413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 1370413628a7SBjoern A. Zeeb inp->inp_lport != lport) 1371413628a7SBjoern A. Zeeb continue; 1372413628a7SBjoern A. Zeeb 1373413628a7SBjoern A. Zeeb /* XXX inp locking */ 1374cfa1ca9dSYoshinobu Inoue if (ifp && ifp->if_type == IFT_FAITH && 1375cfa1ca9dSYoshinobu Inoue (inp->inp_flags & INP_FAITH) == 0) 1376cfa1ca9dSYoshinobu Inoue continue; 1377413628a7SBjoern A. Zeeb 1378413628a7SBjoern A. Zeeb injail = jailed(inp->inp_cred); 1379413628a7SBjoern A. Zeeb if (injail) { 1380b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 1381b89e82ddSJamie Gritton &laddr) != 0) 1382413628a7SBjoern A. Zeeb continue; 1383413628a7SBjoern A. Zeeb } else { 1384413628a7SBjoern A. Zeeb if (local_exact != NULL) 1385413628a7SBjoern A. Zeeb continue; 1386413628a7SBjoern A. Zeeb } 1387413628a7SBjoern A. Zeeb 1388413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 1389413628a7SBjoern A. Zeeb if (injail) 1390c3229e05SDavid Greenman return (inp); 1391413628a7SBjoern A. Zeeb else 1392413628a7SBjoern A. Zeeb local_exact = inp; 1393413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1394e3fd5ffdSRobert Watson #ifdef INET6 1395413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 13965cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 1397cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1398cfa1ca9dSYoshinobu Inoue else 1399413628a7SBjoern A. Zeeb #endif /* INET6 */ 1400413628a7SBjoern A. Zeeb if (injail) 1401413628a7SBjoern A. Zeeb jail_wild = inp; 1402413628a7SBjoern A. Zeeb else 14036d6a026bSDavid Greenman local_wild = inp; 14046d6a026bSDavid Greenman } 1405413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 1406413628a7SBjoern A. Zeeb if (jail_wild != NULL) 1407413628a7SBjoern A. Zeeb return (jail_wild); 1408413628a7SBjoern A. Zeeb if (local_exact != NULL) 1409413628a7SBjoern A. Zeeb return (local_exact); 1410413628a7SBjoern A. Zeeb if (local_wild != NULL) 1411c3229e05SDavid Greenman return (local_wild); 1412413628a7SBjoern A. Zeeb #ifdef INET6 1413413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 1414413628a7SBjoern A. Zeeb return (local_wild_mapped); 1415413628a7SBjoern A. Zeeb #endif /* defined(INET6) */ 1416413628a7SBjoern A. Zeeb } /* if (wildcard == INPLOOKUP_WILDCARD) */ 1417413628a7SBjoern A. Zeeb 14186d6a026bSDavid Greenman return (NULL); 141915bd2b43SDavid Greenman } 142015bd2b43SDavid Greenman 14217bc4aca7SDavid Greenman /* 1422c3229e05SDavid Greenman * Insert PCB onto various hash lists. 14237bc4aca7SDavid Greenman */ 1424c3229e05SDavid Greenman int 1425136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp) 142615bd2b43SDavid Greenman { 1427c3229e05SDavid Greenman struct inpcbhead *pcbhash; 1428c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 1429c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1430c3229e05SDavid Greenman struct inpcbport *phd; 1431cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 143215bd2b43SDavid Greenman 143359daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 14348501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1435111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 1436111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 1437602cc7f1SRobert Watson 1438cfa1ca9dSYoshinobu Inoue #ifdef INET6 1439cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1440cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1441cfa1ca9dSYoshinobu Inoue else 1442cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1443cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1444cfa1ca9dSYoshinobu Inoue 1445712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1446712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 144715bd2b43SDavid Greenman 1448712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 1449712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 1450c3229e05SDavid Greenman 1451c3229e05SDavid Greenman /* 1452c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 1453c3229e05SDavid Greenman */ 1454fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 1455c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 1456c3229e05SDavid Greenman break; 1457c3229e05SDavid Greenman } 1458c3229e05SDavid Greenman /* 1459c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 1460c3229e05SDavid Greenman */ 1461c3229e05SDavid Greenman if (phd == NULL) { 14621ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1463c3229e05SDavid Greenman if (phd == NULL) { 1464c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 1465c3229e05SDavid Greenman } 1466c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 1467c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 1468c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1469c3229e05SDavid Greenman } 1470c3229e05SDavid Greenman inp->inp_phd = phd; 1471c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1472c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1473111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 1474c3229e05SDavid Greenman return (0); 147515bd2b43SDavid Greenman } 147615bd2b43SDavid Greenman 1477c3229e05SDavid Greenman /* 1478c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 1479c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 1480c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 1481c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 1482c3229e05SDavid Greenman */ 148315bd2b43SDavid Greenman void 1484136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp) 148515bd2b43SDavid Greenman { 148659daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 148715bd2b43SDavid Greenman struct inpcbhead *head; 1488cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 148915bd2b43SDavid Greenman 149059daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 14918501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1492111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 1493111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 1494602cc7f1SRobert Watson 1495cfa1ca9dSYoshinobu Inoue #ifdef INET6 1496cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1497cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1498cfa1ca9dSYoshinobu Inoue else 1499cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1500cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1501cfa1ca9dSYoshinobu Inoue 1502712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1503712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 150415bd2b43SDavid Greenman 1505c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 150615bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 1507c3229e05SDavid Greenman } 1508c3229e05SDavid Greenman 1509c3229e05SDavid Greenman /* 1510c3229e05SDavid Greenman * Remove PCB from various lists. 1511c3229e05SDavid Greenman */ 151276429de4SYoshinobu Inoue void 1513136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 1514c3229e05SDavid Greenman { 151559daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 151659daba27SSam Leffler 151759daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 15188501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 151959daba27SSam Leffler 152059daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1521111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 1522c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 1523c3229e05SDavid Greenman 1524c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 1525c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 1526fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1527c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 1528c3229e05SDavid Greenman free(phd, M_PCB); 1529c3229e05SDavid Greenman } 1530111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 1531c3229e05SDavid Greenman } 1532c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 153359daba27SSam Leffler pcbinfo->ipi_count--; 153415bd2b43SDavid Greenman } 153575c13541SPoul-Henning Kamp 1536a557af22SRobert Watson /* 1537a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 1538a557af22SRobert Watson * label change into the in_pcb for the socket. 1539a557af22SRobert Watson */ 1540a557af22SRobert Watson void 1541136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 1542a557af22SRobert Watson { 1543a557af22SRobert Watson #ifdef MAC 1544a557af22SRobert Watson struct inpcb *inp; 1545a557af22SRobert Watson 15464c7c478dSRobert Watson inp = sotoinpcb(so); 15474c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 1548602cc7f1SRobert Watson 15498501a69cSRobert Watson INP_WLOCK(inp); 1550310e7cebSRobert Watson SOCK_LOCK(so); 1551a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 1552310e7cebSRobert Watson SOCK_UNLOCK(so); 15538501a69cSRobert Watson INP_WUNLOCK(inp); 1554a557af22SRobert Watson #endif 1555a557af22SRobert Watson } 15565f311da2SMike Silbersack 15575f311da2SMike Silbersack /* 1558ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 1559ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 1560ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 1561ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 1562ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 15635f311da2SMike Silbersack */ 15645f311da2SMike Silbersack void 1565136d4f1cSRobert Watson ipport_tick(void *xtp) 15665f311da2SMike Silbersack { 15678b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 1568ad3a630fSRobert Watson 15698b615593SMarko Zec VNET_LIST_RLOCK(); 15708b615593SMarko Zec VNET_FOREACH(vnet_iter) { 15718b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 15728b615593SMarko Zec INIT_VNET_INET(vnet_iter); 15738b615593SMarko Zec if (V_ipport_tcpallocs <= 15748b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 1575603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 1576603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 1577ad3a630fSRobert Watson } else 1578603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 1579603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 15808b615593SMarko Zec CURVNET_RESTORE(); 15818b615593SMarko Zec } 15828b615593SMarko Zec VNET_LIST_RUNLOCK(); 15835f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 15845f311da2SMike Silbersack } 1585497057eeSRobert Watson 15863d585327SKip Macy void 15873d585327SKip Macy inp_wlock(struct inpcb *inp) 15883d585327SKip Macy { 15893d585327SKip Macy 15908501a69cSRobert Watson INP_WLOCK(inp); 15913d585327SKip Macy } 15923d585327SKip Macy 15933d585327SKip Macy void 15943d585327SKip Macy inp_wunlock(struct inpcb *inp) 15953d585327SKip Macy { 15963d585327SKip Macy 15978501a69cSRobert Watson INP_WUNLOCK(inp); 15983d585327SKip Macy } 15993d585327SKip Macy 16003d585327SKip Macy void 16013d585327SKip Macy inp_rlock(struct inpcb *inp) 16023d585327SKip Macy { 16033d585327SKip Macy 1604a69042a5SRobert Watson INP_RLOCK(inp); 16053d585327SKip Macy } 16063d585327SKip Macy 16073d585327SKip Macy void 16083d585327SKip Macy inp_runlock(struct inpcb *inp) 16093d585327SKip Macy { 16103d585327SKip Macy 1611a69042a5SRobert Watson INP_RUNLOCK(inp); 16123d585327SKip Macy } 16133d585327SKip Macy 16143d585327SKip Macy #ifdef INVARIANTS 16153d585327SKip Macy void 1616e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 16173d585327SKip Macy { 16183d585327SKip Macy 16198501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 16203d585327SKip Macy } 16213d585327SKip Macy 16223d585327SKip Macy void 1623e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 16243d585327SKip Macy { 16253d585327SKip Macy 16263d585327SKip Macy INP_UNLOCK_ASSERT(inp); 16273d585327SKip Macy } 16283d585327SKip Macy #endif 16293d585327SKip Macy 16309378e437SKip Macy void 16319378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 16329378e437SKip Macy { 16338b615593SMarko Zec INIT_VNET_INET(curvnet); 16349378e437SKip Macy struct inpcb *inp; 16359378e437SKip Macy 1636603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_tcbinfo); 163797021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 16389378e437SKip Macy INP_WLOCK(inp); 16399378e437SKip Macy func(inp, arg); 16409378e437SKip Macy INP_WUNLOCK(inp); 16419378e437SKip Macy } 1642603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_tcbinfo); 16439378e437SKip Macy } 16449378e437SKip Macy 16459378e437SKip Macy struct socket * 16469378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 16479378e437SKip Macy { 16489378e437SKip Macy 16499378e437SKip Macy INP_WLOCK_ASSERT(inp); 16509378e437SKip Macy return (inp->inp_socket); 16519378e437SKip Macy } 16529378e437SKip Macy 16539378e437SKip Macy struct tcpcb * 16549378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 16559378e437SKip Macy { 16569378e437SKip Macy 16579378e437SKip Macy INP_WLOCK_ASSERT(inp); 16589378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 16599378e437SKip Macy } 16609378e437SKip Macy 16619378e437SKip Macy int 16629378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 16639378e437SKip Macy { 16649378e437SKip Macy 16659378e437SKip Macy return (inp->inp_ip_tos); 16669378e437SKip Macy } 16679378e437SKip Macy 16689378e437SKip Macy void 16699378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 16709378e437SKip Macy { 16719378e437SKip Macy 16729378e437SKip Macy inp->inp_ip_tos = val; 16739378e437SKip Macy } 16749378e437SKip Macy 16759378e437SKip Macy void 1676df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 16779d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 16789378e437SKip Macy { 16799378e437SKip Macy 16809d29c635SKip Macy INP_LOCK_ASSERT(inp); 1681df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 1682df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 16839378e437SKip Macy *lp = inp->inp_lport; 16849378e437SKip Macy *fp = inp->inp_fport; 16859378e437SKip Macy } 16869378e437SKip Macy 1687dd0e6c38SKip Macy struct inpcb * 1688dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 1689dd0e6c38SKip Macy { 1690dd0e6c38SKip Macy 1691dd0e6c38SKip Macy return (sotoinpcb(so)); 1692dd0e6c38SKip Macy } 1693dd0e6c38SKip Macy 1694dd0e6c38SKip Macy struct tcpcb * 1695dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 1696dd0e6c38SKip Macy { 1697dd0e6c38SKip Macy 1698dd0e6c38SKip Macy return (sototcpcb(so)); 1699dd0e6c38SKip Macy } 1700dd0e6c38SKip Macy 1701497057eeSRobert Watson #ifdef DDB 1702497057eeSRobert Watson static void 1703497057eeSRobert Watson db_print_indent(int indent) 1704497057eeSRobert Watson { 1705497057eeSRobert Watson int i; 1706497057eeSRobert Watson 1707497057eeSRobert Watson for (i = 0; i < indent; i++) 1708497057eeSRobert Watson db_printf(" "); 1709497057eeSRobert Watson } 1710497057eeSRobert Watson 1711497057eeSRobert Watson static void 1712497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 1713497057eeSRobert Watson { 1714497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 1715497057eeSRobert Watson 1716497057eeSRobert Watson db_print_indent(indent); 1717497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 1718497057eeSRobert Watson 1719497057eeSRobert Watson indent += 2; 1720497057eeSRobert Watson 172103dc38a4SRobert Watson #ifdef INET6 1722dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 1723497057eeSRobert Watson /* IPv6. */ 1724497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 1725497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 1726497057eeSRobert Watson } else { 172703dc38a4SRobert Watson #endif 1728497057eeSRobert Watson /* IPv4. */ 1729497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 1730497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 173103dc38a4SRobert Watson #ifdef INET6 1732497057eeSRobert Watson } 173303dc38a4SRobert Watson #endif 1734497057eeSRobert Watson db_print_indent(indent); 1735497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 1736497057eeSRobert Watson ntohs(inc->inc_lport)); 1737497057eeSRobert Watson db_print_indent(indent); 1738497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 1739497057eeSRobert Watson ntohs(inc->inc_fport)); 1740497057eeSRobert Watson } 1741497057eeSRobert Watson 1742497057eeSRobert Watson static void 1743497057eeSRobert Watson db_print_inpflags(int inp_flags) 1744497057eeSRobert Watson { 1745497057eeSRobert Watson int comma; 1746497057eeSRobert Watson 1747497057eeSRobert Watson comma = 0; 1748497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 1749497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 1750497057eeSRobert Watson comma = 1; 1751497057eeSRobert Watson } 1752497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 1753497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 1754497057eeSRobert Watson comma = 1; 1755497057eeSRobert Watson } 1756497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 1757497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 1758497057eeSRobert Watson comma = 1; 1759497057eeSRobert Watson } 1760497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 1761497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 1762497057eeSRobert Watson comma = 1; 1763497057eeSRobert Watson } 1764497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 1765497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 1766497057eeSRobert Watson comma = 1; 1767497057eeSRobert Watson } 1768497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 1769497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 1770497057eeSRobert Watson comma = 1; 1771497057eeSRobert Watson } 1772497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 1773497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 1774497057eeSRobert Watson comma = 1; 1775497057eeSRobert Watson } 1776497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 1777497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 1778497057eeSRobert Watson comma = 1; 1779497057eeSRobert Watson } 1780497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 1781497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 1782497057eeSRobert Watson comma = 1; 1783497057eeSRobert Watson } 1784497057eeSRobert Watson if (inp_flags & INP_FAITH) { 1785497057eeSRobert Watson db_printf("%sINP_FAITH", comma ? ", " : ""); 1786497057eeSRobert Watson comma = 1; 1787497057eeSRobert Watson } 1788497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 1789497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 1790497057eeSRobert Watson comma = 1; 1791497057eeSRobert Watson } 1792497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 1793497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 1794497057eeSRobert Watson comma = 1; 1795497057eeSRobert Watson } 1796497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 1797497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 1798497057eeSRobert Watson comma = 1; 1799497057eeSRobert Watson } 1800497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 1801497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 1802497057eeSRobert Watson comma = 1; 1803497057eeSRobert Watson } 1804497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 1805497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 1806497057eeSRobert Watson comma = 1; 1807497057eeSRobert Watson } 1808497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 1809497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 1810497057eeSRobert Watson comma = 1; 1811497057eeSRobert Watson } 1812497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 1813497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 1814497057eeSRobert Watson comma = 1; 1815497057eeSRobert Watson } 1816497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 1817497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 1818497057eeSRobert Watson comma = 1; 1819497057eeSRobert Watson } 1820497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 1821497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 1822497057eeSRobert Watson comma = 1; 1823497057eeSRobert Watson } 1824497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 1825497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 1826497057eeSRobert Watson comma = 1; 1827497057eeSRobert Watson } 1828497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 1829497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 1830497057eeSRobert Watson comma = 1; 1831497057eeSRobert Watson } 1832ad71fe3cSRobert Watson if (inp_flags & INP_TIMEWAIT) { 1833ad71fe3cSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 1834ad71fe3cSRobert Watson comma = 1; 1835ad71fe3cSRobert Watson } 1836ad71fe3cSRobert Watson if (inp_flags & INP_ONESBCAST) { 1837ad71fe3cSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 1838ad71fe3cSRobert Watson comma = 1; 1839ad71fe3cSRobert Watson } 1840ad71fe3cSRobert Watson if (inp_flags & INP_DROPPED) { 1841ad71fe3cSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 1842ad71fe3cSRobert Watson comma = 1; 1843ad71fe3cSRobert Watson } 1844ad71fe3cSRobert Watson if (inp_flags & INP_SOCKREF) { 1845ad71fe3cSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 1846ad71fe3cSRobert Watson comma = 1; 1847ad71fe3cSRobert Watson } 1848497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 1849497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 1850497057eeSRobert Watson comma = 1; 1851497057eeSRobert Watson } 1852497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 1853497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 1854497057eeSRobert Watson comma = 1; 1855497057eeSRobert Watson } 1856497057eeSRobert Watson } 1857497057eeSRobert Watson 1858497057eeSRobert Watson static void 1859497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 1860497057eeSRobert Watson { 1861497057eeSRobert Watson int comma; 1862497057eeSRobert Watson 1863497057eeSRobert Watson comma = 0; 1864497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 1865497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 1866497057eeSRobert Watson comma = 1; 1867497057eeSRobert Watson } 1868497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 1869497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 1870497057eeSRobert Watson comma = 1; 1871497057eeSRobert Watson } 1872497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 1873497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 1874497057eeSRobert Watson comma = 1; 1875497057eeSRobert Watson } 1876497057eeSRobert Watson } 1877497057eeSRobert Watson 1878497057eeSRobert Watson void 1879497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 1880497057eeSRobert Watson { 1881497057eeSRobert Watson 1882497057eeSRobert Watson db_print_indent(indent); 1883497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 1884497057eeSRobert Watson 1885497057eeSRobert Watson indent += 2; 1886497057eeSRobert Watson 1887497057eeSRobert Watson db_print_indent(indent); 1888497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 1889497057eeSRobert Watson 1890497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 1891497057eeSRobert Watson 1892497057eeSRobert Watson db_print_indent(indent); 1893497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 1894497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 1895497057eeSRobert Watson 1896497057eeSRobert Watson db_print_indent(indent); 1897497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 1898497057eeSRobert Watson inp->inp_label, inp->inp_flags); 1899497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 1900497057eeSRobert Watson db_printf(")\n"); 1901497057eeSRobert Watson 1902497057eeSRobert Watson db_print_indent(indent); 1903497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 1904497057eeSRobert Watson inp->inp_vflag); 1905497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 1906497057eeSRobert Watson db_printf(")\n"); 1907497057eeSRobert Watson 1908497057eeSRobert Watson db_print_indent(indent); 1909497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 1910497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 1911497057eeSRobert Watson 1912497057eeSRobert Watson db_print_indent(indent); 1913497057eeSRobert Watson #ifdef INET6 1914497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 1915497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 1916497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 1917497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 1918497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 1919497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 1920497057eeSRobert Watson inp->in6p_hops); 1921497057eeSRobert Watson } else 1922497057eeSRobert Watson #endif 1923497057eeSRobert Watson { 1924497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 1925497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 1926497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 1927497057eeSRobert Watson } 1928497057eeSRobert Watson 1929497057eeSRobert Watson db_print_indent(indent); 1930497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 1931497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 1932497057eeSRobert Watson } 1933497057eeSRobert Watson 1934497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 1935497057eeSRobert Watson { 1936497057eeSRobert Watson struct inpcb *inp; 1937497057eeSRobert Watson 1938497057eeSRobert Watson if (!have_addr) { 1939497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 1940497057eeSRobert Watson return; 1941497057eeSRobert Watson } 1942497057eeSRobert Watson inp = (struct inpcb *)addr; 1943497057eeSRobert Watson 1944497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 1945497057eeSRobert Watson } 1946497057eeSRobert Watson #endif 1947