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 && 3874658dc83SYaroslav Tykhiy ((t->inp_vflag & 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); 400340c35deSJonathan Lemon if (t && (t->inp_vflag & 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; 6080895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 6090895aec3SBjoern A. Zeeb 6100895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 6110895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 6120895aec3SBjoern A. Zeeb continue; 6130895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 614b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 6150895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 6160895aec3SBjoern A. Zeeb break; 6170895aec3SBjoern A. Zeeb } 6180895aec3SBjoern A. Zeeb } 6190895aec3SBjoern A. Zeeb if (ia != NULL) { 6200895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6210895aec3SBjoern A. Zeeb goto done; 6220895aec3SBjoern A. Zeeb } 6230895aec3SBjoern A. Zeeb 6240895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 625b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 6260895aec3SBjoern A. Zeeb goto done; 6270895aec3SBjoern A. Zeeb } 6280895aec3SBjoern A. Zeeb 6290895aec3SBjoern A. Zeeb /* 6300895aec3SBjoern A. Zeeb * If the outgoing interface on the route found is not 6310895aec3SBjoern A. Zeeb * a loopback interface, use the address from that interface. 6320895aec3SBjoern A. Zeeb * In case of jails do those three steps: 6330895aec3SBjoern A. Zeeb * 1. check if the interface address belongs to the jail. If so use it. 6340895aec3SBjoern A. Zeeb * 2. check if we have any address on the outgoing interface 6350895aec3SBjoern A. Zeeb * belonging to this jail. If so use it. 6360895aec3SBjoern A. Zeeb * 3. as a last resort return the 'default' jail address. 6370895aec3SBjoern A. Zeeb */ 6380895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 6390895aec3SBjoern A. Zeeb 6400895aec3SBjoern A. Zeeb /* If not jailed, use the default returned. */ 6410895aec3SBjoern A. Zeeb if (cred == NULL || !jailed(cred)) { 6420895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 6430895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6440895aec3SBjoern A. Zeeb goto done; 6450895aec3SBjoern A. Zeeb } 6460895aec3SBjoern A. Zeeb 6470895aec3SBjoern A. Zeeb /* Jailed. */ 6480895aec3SBjoern A. Zeeb /* 1. Check if the iface address belongs to the jail. */ 6490895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 650b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 6510895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 6520895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6530895aec3SBjoern A. Zeeb goto done; 6540895aec3SBjoern A. Zeeb } 6550895aec3SBjoern A. Zeeb 6560895aec3SBjoern A. Zeeb /* 6570895aec3SBjoern A. Zeeb * 2. Check if we have any address on the outgoing interface 6580895aec3SBjoern A. Zeeb * belonging to this jail. 6590895aec3SBjoern A. Zeeb */ 6600895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &sro.ro_rt->rt_ifp->if_addrhead, ifa_link) { 6610895aec3SBjoern A. Zeeb 6620895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 6630895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 6640895aec3SBjoern A. Zeeb continue; 6650895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 666b89e82ddSJamie Gritton if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 6670895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 6680895aec3SBjoern A. Zeeb break; 6690895aec3SBjoern A. Zeeb } 6700895aec3SBjoern A. Zeeb } 6710895aec3SBjoern A. Zeeb if (ia != NULL) { 6720895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 6730895aec3SBjoern A. Zeeb goto done; 6740895aec3SBjoern A. Zeeb } 6750895aec3SBjoern A. Zeeb 6760895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 677b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 6780895aec3SBjoern A. Zeeb goto done; 6790895aec3SBjoern A. Zeeb } 6800895aec3SBjoern A. Zeeb 6810895aec3SBjoern A. Zeeb /* 6820895aec3SBjoern A. Zeeb * The outgoing interface is marked with 'loopback net', so a route 6830895aec3SBjoern A. Zeeb * to ourselves is here. 6840895aec3SBjoern A. Zeeb * Try to find the interface of the destination address and then 6850895aec3SBjoern A. Zeeb * take the address from there. That interface is not necessarily 6860895aec3SBjoern A. Zeeb * a loopback interface. 6870895aec3SBjoern A. Zeeb * In case of jails, check that it is an address of the jail 6880895aec3SBjoern A. Zeeb * and if we cannot find, fall back to the 'default' jail address. 6890895aec3SBjoern A. Zeeb */ 6900895aec3SBjoern A. Zeeb if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 6910895aec3SBjoern A. Zeeb struct sockaddr_in sain; 6920895aec3SBjoern A. Zeeb 6930895aec3SBjoern A. Zeeb bzero(&sain, sizeof(struct sockaddr_in)); 6940895aec3SBjoern A. Zeeb sain.sin_family = AF_INET; 6950895aec3SBjoern A. Zeeb sain.sin_len = sizeof(struct sockaddr_in); 6960895aec3SBjoern A. Zeeb sain.sin_addr.s_addr = faddr->s_addr; 6970895aec3SBjoern A. Zeeb 6980895aec3SBjoern A. Zeeb ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain))); 6990895aec3SBjoern A. Zeeb if (ia == NULL) 7000895aec3SBjoern A. Zeeb ia = ifatoia(ifa_ifwithnet(sintosa(&sain))); 7010895aec3SBjoern A. Zeeb 7020895aec3SBjoern A. Zeeb if (cred == NULL || !jailed(cred)) { 70303d8b6fdSBjoern A. Zeeb #if __FreeBSD_version < 800000 70403d8b6fdSBjoern A. Zeeb if (ia == NULL) 70503d8b6fdSBjoern A. Zeeb ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 70603d8b6fdSBjoern A. Zeeb #endif 7070895aec3SBjoern A. Zeeb if (ia == NULL) { 7080895aec3SBjoern A. Zeeb error = ENETUNREACH; 7090895aec3SBjoern A. Zeeb goto done; 7100895aec3SBjoern A. Zeeb } 7110895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 7120895aec3SBjoern A. Zeeb goto done; 7130895aec3SBjoern A. Zeeb } 7140895aec3SBjoern A. Zeeb 7150895aec3SBjoern A. Zeeb /* Jailed. */ 7160895aec3SBjoern A. Zeeb if (ia != NULL) { 7170895aec3SBjoern A. Zeeb struct ifnet *ifp; 7180895aec3SBjoern A. Zeeb 7190895aec3SBjoern A. Zeeb ifp = ia->ia_ifp; 7200895aec3SBjoern A. Zeeb ia = NULL; 7210895aec3SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 7220895aec3SBjoern A. Zeeb 7230895aec3SBjoern A. Zeeb sa = ifa->ifa_addr; 7240895aec3SBjoern A. Zeeb if (sa->sa_family != AF_INET) 7250895aec3SBjoern A. Zeeb continue; 7260895aec3SBjoern A. Zeeb sin = (struct sockaddr_in *)sa; 727b89e82ddSJamie Gritton if (prison_check_ip4(cred, 728b89e82ddSJamie Gritton &sin->sin_addr) == 0) { 7290895aec3SBjoern A. Zeeb ia = (struct in_ifaddr *)ifa; 7300895aec3SBjoern A. Zeeb break; 7310895aec3SBjoern A. Zeeb } 7320895aec3SBjoern A. Zeeb } 7330895aec3SBjoern A. Zeeb if (ia != NULL) { 7340895aec3SBjoern A. Zeeb laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 7350895aec3SBjoern A. Zeeb goto done; 7360895aec3SBjoern A. Zeeb } 7370895aec3SBjoern A. Zeeb } 7380895aec3SBjoern A. Zeeb 7390895aec3SBjoern A. Zeeb /* 3. As a last resort return the 'default' jail address. */ 740b89e82ddSJamie Gritton error = prison_get_ip4(cred, laddr); 7410895aec3SBjoern A. Zeeb goto done; 7420895aec3SBjoern A. Zeeb } 7430895aec3SBjoern A. Zeeb 7440895aec3SBjoern A. Zeeb done: 7450895aec3SBjoern A. Zeeb if (sro.ro_rt != NULL) 7460895aec3SBjoern A. Zeeb RTFREE(sro.ro_rt); 7470895aec3SBjoern A. Zeeb return (error); 7480895aec3SBjoern A. Zeeb } 7490895aec3SBjoern A. Zeeb 7500895aec3SBjoern A. Zeeb /* 7515200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 7525200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 7535200e00eSIan Dowse * address and port for the PCB; these are updated to the values 7545200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 7555200e00eSIan Dowse * the connect. 7565200e00eSIan Dowse * 7575200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 7585200e00eSIan Dowse * and port. These are not updated in the error case. 7595200e00eSIan Dowse * 7605200e00eSIan Dowse * If the operation fails because the connection already exists, 7615200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 7625200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 7635200e00eSIan Dowse * is set to NULL. 7645200e00eSIan Dowse */ 7655200e00eSIan Dowse int 766136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 767136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 768136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 7695200e00eSIan Dowse { 7708b615593SMarko Zec INIT_VNET_INET(inp->inp_vnet); 7715200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 7725200e00eSIan Dowse struct in_ifaddr *ia; 7735200e00eSIan Dowse struct inpcb *oinp; 774b89e82ddSJamie Gritton struct in_addr laddr, faddr; 7755200e00eSIan Dowse u_short lport, fport; 7765200e00eSIan Dowse int error; 7775200e00eSIan Dowse 7788501a69cSRobert Watson /* 7798501a69cSRobert Watson * Because a global state change doesn't actually occur here, a read 7808501a69cSRobert Watson * lock is sufficient. 7818501a69cSRobert Watson */ 7828501a69cSRobert Watson INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo); 78327f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 78427f74fd0SRobert Watson 7855200e00eSIan Dowse if (oinpp != NULL) 7865200e00eSIan Dowse *oinpp = NULL; 78757bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 788df8bae1dSRodney W. Grimes return (EINVAL); 789df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 790df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 791df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 792df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 7935200e00eSIan Dowse laddr.s_addr = *laddrp; 7945200e00eSIan Dowse lport = *lportp; 7955200e00eSIan Dowse faddr = sin->sin_addr; 7965200e00eSIan Dowse fport = sin->sin_port; 7970895aec3SBjoern A. Zeeb 798603724d3SBjoern A. Zeeb if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 799df8bae1dSRodney W. Grimes /* 800df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 801df8bae1dSRodney W. Grimes * use the primary local address. 802df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 803df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 804df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 805df8bae1dSRodney W. Grimes */ 806413628a7SBjoern A. Zeeb if (faddr.s_addr == INADDR_ANY) { 807413628a7SBjoern A. Zeeb faddr = 808b89e82ddSJamie Gritton IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 809b89e82ddSJamie Gritton if (cred != NULL && 810b89e82ddSJamie Gritton (error = prison_get_ip4(cred, &faddr)) != 0) 811b89e82ddSJamie Gritton return (error); 812413628a7SBjoern A. Zeeb } else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 813603724d3SBjoern A. Zeeb (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 8145200e00eSIan Dowse IFF_BROADCAST)) 8155200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 816603724d3SBjoern A. Zeeb &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 817df8bae1dSRodney W. Grimes } 8185200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 8190895aec3SBjoern A. Zeeb error = in_pcbladdr(inp, &faddr, &laddr, cred); 8200895aec3SBjoern A. Zeeb if (error) 8210895aec3SBjoern A. Zeeb return (error); 822df8bae1dSRodney W. Grimes 823df8bae1dSRodney W. Grimes /* 824df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 825df8bae1dSRodney W. Grimes * interface has been set as a multicast option, use the 826df8bae1dSRodney W. Grimes * address of that interface as our source address. 827df8bae1dSRodney W. Grimes */ 8285200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 829df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 830df8bae1dSRodney W. Grimes struct ip_moptions *imo; 831df8bae1dSRodney W. Grimes struct ifnet *ifp; 832df8bae1dSRodney W. Grimes 833df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 834df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 835df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 836603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 837df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp) 838df8bae1dSRodney W. Grimes break; 8398699ea08SBjoern A. Zeeb if (ia == NULL) 840df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 8415200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 842999f1343SGarrett Wollman } 8430895aec3SBjoern A. Zeeb } 8440895aec3SBjoern A. Zeeb } 845999f1343SGarrett Wollman 8465200e00eSIan Dowse oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 8475200e00eSIan Dowse 0, NULL); 8485200e00eSIan Dowse if (oinp != NULL) { 8495200e00eSIan Dowse if (oinpp != NULL) 8505200e00eSIan Dowse *oinpp = oinp; 851df8bae1dSRodney W. Grimes return (EADDRINUSE); 852c3229e05SDavid Greenman } 8535200e00eSIan Dowse if (lport == 0) { 854b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 855b0330ed9SPawel Jakub Dawidek cred); 8565a903f8dSPierre Beyssac if (error) 8575a903f8dSPierre Beyssac return (error); 8585a903f8dSPierre Beyssac } 8595200e00eSIan Dowse *laddrp = laddr.s_addr; 8605200e00eSIan Dowse *lportp = lport; 8615200e00eSIan Dowse *faddrp = faddr.s_addr; 8625200e00eSIan Dowse *fportp = fport; 863df8bae1dSRodney W. Grimes return (0); 864df8bae1dSRodney W. Grimes } 865df8bae1dSRodney W. Grimes 86626f9a767SRodney W. Grimes void 867136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 868df8bae1dSRodney W. Grimes { 8696b348152SRobert Watson 870fe6bfc37SRobert Watson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 8718501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 872df8bae1dSRodney W. Grimes 873df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 874df8bae1dSRodney W. Grimes inp->inp_fport = 0; 87515bd2b43SDavid Greenman in_pcbrehash(inp); 876df8bae1dSRodney W. Grimes } 877df8bae1dSRodney W. Grimes 8784c7c478dSRobert Watson /* 87928696211SRobert Watson * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 880c0a211c5SRobert Watson * For most protocols, this will be invoked immediately prior to calling 88128696211SRobert Watson * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 88228696211SRobert Watson * socket, in which case in_pcbfree() is deferred. 8834c7c478dSRobert Watson */ 88426f9a767SRodney W. Grimes void 885136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 886df8bae1dSRodney W. Grimes { 8874c7c478dSRobert Watson 888a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 889c0a211c5SRobert Watson 8904c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 8914c7c478dSRobert Watson inp->inp_socket = NULL; 8924c7c478dSRobert Watson } 8934c7c478dSRobert Watson 894c0a211c5SRobert Watson /* 89528696211SRobert Watson * in_pcbfree_internal() frees an inpcb that has been detached from its 89628696211SRobert Watson * socket, and whose reference count has reached 0. It will also remove the 89728696211SRobert Watson * inpcb from any global lists it might remain on. 898c0a211c5SRobert Watson */ 89928696211SRobert Watson static void 90028696211SRobert Watson in_pcbfree_internal(struct inpcb *inp) 9014c7c478dSRobert Watson { 9023d4d47f3SGarrett Wollman struct inpcbinfo *ipi = inp->inp_pcbinfo; 903df8bae1dSRodney W. Grimes 904a7df09e8SBjoern A. Zeeb KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 90528696211SRobert Watson KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__)); 9068501a69cSRobert Watson 907fe6bfc37SRobert Watson INP_INFO_WLOCK_ASSERT(ipi); 9088501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 90959daba27SSam Leffler 910b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 9116aee2fc5SBjoern A. Zeeb if (inp->inp_sp != NULL) 9126974bd9eSBjoern A. Zeeb ipsec_delete_pcbpolicy(inp); 913b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 9143d4d47f3SGarrett Wollman inp->inp_gencnt = ++ipi->ipi_gencnt; 915c3229e05SDavid Greenman in_pcbremlists(inp); 9166aee2fc5SBjoern A. Zeeb #ifdef INET6 9176aee2fc5SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 9186aee2fc5SBjoern A. Zeeb ip6_freepcbopts(inp->in6p_outputopts); 9196aee2fc5SBjoern A. Zeeb ip6_freemoptions(inp->in6p_moptions); 9206aee2fc5SBjoern A. Zeeb } 9216aee2fc5SBjoern A. Zeeb #endif 922df8bae1dSRodney W. Grimes if (inp->inp_options) 923df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 92471498f30SBruce M Simpson if (inp->inp_moptions != NULL) 92571498f30SBruce M Simpson inp_freemoptions(inp->inp_moptions); 926cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 92786d02c5cSBjoern A. Zeeb crfree(inp->inp_cred); 928d915b280SStephan Uphoff 929a557af22SRobert Watson #ifdef MAC 93030d239bcSRobert Watson mac_inpcb_destroy(inp); 931a557af22SRobert Watson #endif 9328501a69cSRobert Watson INP_WUNLOCK(inp); 93369c2d429SJeff Roberson uma_zfree(ipi->ipi_zone, inp); 934df8bae1dSRodney W. Grimes } 935df8bae1dSRodney W. Grimes 93610702a28SRobert Watson /* 93728696211SRobert Watson * in_pcbref() bumps the reference count on an inpcb in order to maintain 93828696211SRobert Watson * stability of an inpcb pointer despite the inpcb lock being released. This 93928696211SRobert Watson * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 94028696211SRobert Watson * but where the inpcb lock is already held. 94128696211SRobert Watson * 94228696211SRobert Watson * While the inpcb will not be freed, releasing the inpcb lock means that the 94328696211SRobert Watson * connection's state may change, so the caller should be careful to 94428696211SRobert Watson * revalidate any cached state on reacquiring the lock. Drop the reference 94528696211SRobert Watson * using in_pcbrele(). 94628696211SRobert Watson */ 94728696211SRobert Watson void 94828696211SRobert Watson in_pcbref(struct inpcb *inp) 94928696211SRobert Watson { 95028696211SRobert Watson 95128696211SRobert Watson INP_WLOCK_ASSERT(inp); 95228696211SRobert Watson 95328696211SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 95428696211SRobert Watson 95528696211SRobert Watson inp->inp_refcount++; 95628696211SRobert Watson } 95728696211SRobert Watson 95828696211SRobert Watson /* 95928696211SRobert Watson * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 96028696211SRobert Watson * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 96128696211SRobert Watson * return a flag indicating whether or not the inpcb remains valid. If it is 96228696211SRobert Watson * valid, we return with the inpcb lock held. 96328696211SRobert Watson */ 96428696211SRobert Watson int 96528696211SRobert Watson in_pcbrele(struct inpcb *inp) 96628696211SRobert Watson { 96728696211SRobert Watson #ifdef INVARIANTS 96828696211SRobert Watson struct inpcbinfo *ipi = inp->inp_pcbinfo; 96928696211SRobert Watson #endif 97028696211SRobert Watson 97128696211SRobert Watson KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 97228696211SRobert Watson 97328696211SRobert Watson INP_INFO_WLOCK_ASSERT(ipi); 97428696211SRobert Watson INP_WLOCK_ASSERT(inp); 97528696211SRobert Watson 97628696211SRobert Watson inp->inp_refcount--; 97728696211SRobert Watson if (inp->inp_refcount > 0) 97828696211SRobert Watson return (0); 97928696211SRobert Watson in_pcbfree_internal(inp); 98028696211SRobert Watson return (1); 98128696211SRobert Watson } 98228696211SRobert Watson 98328696211SRobert Watson /* 98428696211SRobert Watson * Unconditionally schedule an inpcb to be freed by decrementing its 98528696211SRobert Watson * reference count, which should occur only after the inpcb has been detached 98628696211SRobert Watson * from its socket. If another thread holds a temporary reference (acquired 98728696211SRobert Watson * using in_pcbref()) then the free is deferred until that reference is 98828696211SRobert Watson * released using in_pcbrele(), but the inpcb is still unlocked. 98928696211SRobert Watson */ 99028696211SRobert Watson void 99128696211SRobert Watson in_pcbfree(struct inpcb *inp) 99228696211SRobert Watson { 99328696211SRobert Watson #ifdef INVARIANTS 99428696211SRobert Watson struct inpcbinfo *ipi = inp->inp_pcbinfo; 99528696211SRobert Watson #endif 99628696211SRobert Watson 99728696211SRobert Watson KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", 99828696211SRobert Watson __func__)); 99928696211SRobert Watson 100028696211SRobert Watson INP_INFO_WLOCK_ASSERT(ipi); 100128696211SRobert Watson INP_WLOCK_ASSERT(inp); 100228696211SRobert Watson 100328696211SRobert Watson if (!in_pcbrele(inp)) 100428696211SRobert Watson INP_WUNLOCK(inp); 100528696211SRobert Watson } 100628696211SRobert Watson 100728696211SRobert Watson /* 1008c0a211c5SRobert Watson * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1009c0a211c5SRobert Watson * port reservation, and preventing it from being returned by inpcb lookups. 1010c0a211c5SRobert Watson * 1011c0a211c5SRobert Watson * It is used by TCP to mark an inpcb as unused and avoid future packet 1012c0a211c5SRobert Watson * delivery or event notification when a socket remains open but TCP has 1013c0a211c5SRobert Watson * closed. This might occur as a result of a shutdown()-initiated TCP close 1014c0a211c5SRobert Watson * or a RST on the wire, and allows the port binding to be reused while still 1015c0a211c5SRobert Watson * maintaining the invariant that so_pcb always points to a valid inpcb until 1016c0a211c5SRobert Watson * in_pcbdetach(). 1017c0a211c5SRobert Watson * 1018c0a211c5SRobert Watson * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash 1019c0a211c5SRobert Watson * lists, but can lead to confusing netstat output, as open sockets with 1020c0a211c5SRobert Watson * closed TCP connections will no longer appear to have their bound port 1021c0a211c5SRobert Watson * number. An explicit flag would be better, as it would allow us to leave 1022c0a211c5SRobert Watson * the port number intact after the connection is dropped. 1023c0a211c5SRobert Watson * 1024c0a211c5SRobert Watson * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1025c0a211c5SRobert Watson * in_pcbnotifyall() and in_pcbpurgeif0()? 102610702a28SRobert Watson */ 102710702a28SRobert Watson void 102810702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 102910702a28SRobert Watson { 103010702a28SRobert Watson 10317c5a8ab2SMarcel Moolenaar INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 10328501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 103310702a28SRobert Watson 103410702a28SRobert Watson inp->inp_vflag |= INP_DROPPED; 1035111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 103610702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 103710702a28SRobert Watson 103810702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 103910702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 104010702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 104110702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 104210702a28SRobert Watson free(phd, M_PCB); 104310702a28SRobert Watson } 1044111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 104510702a28SRobert Watson } 104610702a28SRobert Watson } 104710702a28SRobert Watson 104854d642bbSRobert Watson /* 104954d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 105054d642bbSRobert Watson */ 105126ef6ac4SDon Lewis struct sockaddr * 1052136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 105326ef6ac4SDon Lewis { 105426ef6ac4SDon Lewis struct sockaddr_in *sin; 105526ef6ac4SDon Lewis 10561ede983cSDag-Erling Smørgrav sin = malloc(sizeof *sin, M_SONAME, 1057a163d034SWarner Losh M_WAITOK | M_ZERO); 105826ef6ac4SDon Lewis sin->sin_family = AF_INET; 105926ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 106026ef6ac4SDon Lewis sin->sin_addr = *addr_p; 106126ef6ac4SDon Lewis sin->sin_port = port; 106226ef6ac4SDon Lewis 106326ef6ac4SDon Lewis return (struct sockaddr *)sin; 106426ef6ac4SDon Lewis } 106526ef6ac4SDon Lewis 1066117bcae7SGarrett Wollman int 106754d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 1068df8bae1dSRodney W. Grimes { 1069136d4f1cSRobert Watson struct inpcb *inp; 107026ef6ac4SDon Lewis struct in_addr addr; 107126ef6ac4SDon Lewis in_port_t port; 107242fa505bSDavid Greenman 1073fdc984f7STor Egge inp = sotoinpcb(so); 107454d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 10756466b28aSRobert Watson 1076a69042a5SRobert Watson INP_RLOCK(inp); 107726ef6ac4SDon Lewis port = inp->inp_lport; 107826ef6ac4SDon Lewis addr = inp->inp_laddr; 1079a69042a5SRobert Watson INP_RUNLOCK(inp); 108042fa505bSDavid Greenman 108126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1082117bcae7SGarrett Wollman return 0; 1083df8bae1dSRodney W. Grimes } 1084df8bae1dSRodney W. Grimes 1085117bcae7SGarrett Wollman int 108654d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1087df8bae1dSRodney W. Grimes { 1088136d4f1cSRobert Watson struct inpcb *inp; 108926ef6ac4SDon Lewis struct in_addr addr; 109026ef6ac4SDon Lewis in_port_t port; 109142fa505bSDavid Greenman 1092fdc984f7STor Egge inp = sotoinpcb(so); 109354d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 10946466b28aSRobert Watson 1095a69042a5SRobert Watson INP_RLOCK(inp); 109626ef6ac4SDon Lewis port = inp->inp_fport; 109726ef6ac4SDon Lewis addr = inp->inp_faddr; 1098a69042a5SRobert Watson INP_RUNLOCK(inp); 109942fa505bSDavid Greenman 110026ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 1101117bcae7SGarrett Wollman return 0; 1102df8bae1dSRodney W. Grimes } 1103df8bae1dSRodney W. Grimes 110426f9a767SRodney W. Grimes void 1105136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1106136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 1107d1c54148SJesper Skriver { 1108f457d580SRobert Watson struct inpcb *inp, *inp_temp; 1109d1c54148SJesper Skriver 11103dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 1111f457d580SRobert Watson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 11128501a69cSRobert Watson INP_WLOCK(inp); 1113d1c54148SJesper Skriver #ifdef INET6 1114f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 11158501a69cSRobert Watson INP_WUNLOCK(inp); 1116d1c54148SJesper Skriver continue; 1117f76fcf6dSJeffrey Hsu } 1118d1c54148SJesper Skriver #endif 1119d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 1120f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 11218501a69cSRobert Watson INP_WUNLOCK(inp); 1122d1c54148SJesper Skriver continue; 1123d1c54148SJesper Skriver } 11243dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 11258501a69cSRobert Watson INP_WUNLOCK(inp); 1126f76fcf6dSJeffrey Hsu } 11273dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 1128d1c54148SJesper Skriver } 1129d1c54148SJesper Skriver 1130e43cc4aeSHajimu UMEMOTO void 1131136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1132e43cc4aeSHajimu UMEMOTO { 1133e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 1134e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 1135e43cc4aeSHajimu UMEMOTO int i, gap; 1136e43cc4aeSHajimu UMEMOTO 1137f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(pcbinfo); 1138712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 11398501a69cSRobert Watson INP_WLOCK(inp); 1140e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 1141e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 1142e43cc4aeSHajimu UMEMOTO imo != NULL) { 1143e43cc4aeSHajimu UMEMOTO /* 1144e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 1145e43cc4aeSHajimu UMEMOTO * detached. 1146e43cc4aeSHajimu UMEMOTO */ 1147e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 1148e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 1149e43cc4aeSHajimu UMEMOTO 1150e43cc4aeSHajimu UMEMOTO /* 1151e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 1152e43cc4aeSHajimu UMEMOTO * through the interface being detached. 1153e43cc4aeSHajimu UMEMOTO */ 1154e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 1155e43cc4aeSHajimu UMEMOTO i++) { 1156e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 1157e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 1158e43cc4aeSHajimu UMEMOTO gap++; 1159e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 1160e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 1161e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 1162e43cc4aeSHajimu UMEMOTO } 1163e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 1164e43cc4aeSHajimu UMEMOTO } 11658501a69cSRobert Watson INP_WUNLOCK(inp); 1166e43cc4aeSHajimu UMEMOTO } 11673cfcc388SJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 1168e43cc4aeSHajimu UMEMOTO } 1169e43cc4aeSHajimu UMEMOTO 1170df8bae1dSRodney W. Grimes /* 1171c3229e05SDavid Greenman * Lookup a PCB based on the local address and port. 1172c3229e05SDavid Greenman */ 1173d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 1174df8bae1dSRodney W. Grimes struct inpcb * 1175136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 1176078b7042SBjoern A. Zeeb u_short lport, int wild_okay, struct ucred *cred) 1177df8bae1dSRodney W. Grimes { 1178136d4f1cSRobert Watson struct inpcb *inp; 1179d5e8a67eSHajimu UMEMOTO #ifdef INET6 1180d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1181d5e8a67eSHajimu UMEMOTO #else 1182d5e8a67eSHajimu UMEMOTO int matchwild = 3; 1183d5e8a67eSHajimu UMEMOTO #endif 1184d5e8a67eSHajimu UMEMOTO int wildcard; 11857bc4aca7SDavid Greenman 11868501a69cSRobert Watson INP_INFO_LOCK_ASSERT(pcbinfo); 11871b73ca0bSSam Leffler 1188c3229e05SDavid Greenman if (!wild_okay) { 1189c3229e05SDavid Greenman struct inpcbhead *head; 1190c3229e05SDavid Greenman /* 1191c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 1192c3229e05SDavid Greenman * matches the local address and port we're looking for. 1193c3229e05SDavid Greenman */ 1194712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1195712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1196fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1197cfa1ca9dSYoshinobu Inoue #ifdef INET6 1198413628a7SBjoern A. Zeeb /* XXX inp locking */ 1199369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1200cfa1ca9dSYoshinobu Inoue continue; 1201cfa1ca9dSYoshinobu Inoue #endif 1202c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1203c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1204c3229e05SDavid Greenman inp->inp_lport == lport) { 1205c3229e05SDavid Greenman /* 1206413628a7SBjoern A. Zeeb * Found? 1207c3229e05SDavid Greenman */ 1208413628a7SBjoern A. Zeeb if (cred == NULL || 1209413628a7SBjoern A. Zeeb inp->inp_cred->cr_prison == cred->cr_prison) 1210c3229e05SDavid Greenman return (inp); 1211df8bae1dSRodney W. Grimes } 1212c3229e05SDavid Greenman } 1213c3229e05SDavid Greenman /* 1214c3229e05SDavid Greenman * Not found. 1215c3229e05SDavid Greenman */ 1216c3229e05SDavid Greenman return (NULL); 1217c3229e05SDavid Greenman } else { 1218c3229e05SDavid Greenman struct inpcbporthead *porthash; 1219c3229e05SDavid Greenman struct inpcbport *phd; 1220c3229e05SDavid Greenman struct inpcb *match = NULL; 1221c3229e05SDavid Greenman /* 1222c3229e05SDavid Greenman * Best fit PCB lookup. 1223c3229e05SDavid Greenman * 1224c3229e05SDavid Greenman * First see if this local port is in use by looking on the 1225c3229e05SDavid Greenman * port hash list. 1226c3229e05SDavid Greenman */ 1227712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1228712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 1229fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 1230c3229e05SDavid Greenman if (phd->phd_port == lport) 1231c3229e05SDavid Greenman break; 1232c3229e05SDavid Greenman } 1233c3229e05SDavid Greenman if (phd != NULL) { 1234c3229e05SDavid Greenman /* 1235c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 1236c3229e05SDavid Greenman * fit. 1237c3229e05SDavid Greenman */ 123837d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1239c3229e05SDavid Greenman wildcard = 0; 1240413628a7SBjoern A. Zeeb if (cred != NULL && 1241413628a7SBjoern A. Zeeb inp->inp_cred->cr_prison != cred->cr_prison) 1242413628a7SBjoern A. Zeeb continue; 1243cfa1ca9dSYoshinobu Inoue #ifdef INET6 1244413628a7SBjoern A. Zeeb /* XXX inp locking */ 1245369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1246cfa1ca9dSYoshinobu Inoue continue; 1247d5e8a67eSHajimu UMEMOTO /* 1248d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 1249d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 1250d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 1251d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 1252d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 1253d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 1254d5e8a67eSHajimu UMEMOTO * 1255d5e8a67eSHajimu UMEMOTO * Note that the case only happens 1256d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 1257d5e8a67eSHajimu UMEMOTO * the condition that the use of the 1258d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 1259d5e8a67eSHajimu UMEMOTO */ 1260d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 1261d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1262cfa1ca9dSYoshinobu Inoue #endif 1263c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 1264c3229e05SDavid Greenman wildcard++; 126515bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 126615bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 126715bd2b43SDavid Greenman wildcard++; 126815bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 126915bd2b43SDavid Greenman continue; 127015bd2b43SDavid Greenman } else { 127115bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 127215bd2b43SDavid Greenman wildcard++; 127315bd2b43SDavid Greenman } 1274df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 1275df8bae1dSRodney W. Grimes match = inp; 1276df8bae1dSRodney W. Grimes matchwild = wildcard; 1277413628a7SBjoern A. Zeeb if (matchwild == 0) 1278df8bae1dSRodney W. Grimes break; 1279df8bae1dSRodney W. Grimes } 1280df8bae1dSRodney W. Grimes } 12813dbdc25cSDavid Greenman } 1282df8bae1dSRodney W. Grimes return (match); 1283df8bae1dSRodney W. Grimes } 1284c3229e05SDavid Greenman } 1285d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 128615bd2b43SDavid Greenman 128715bd2b43SDavid Greenman /* 128815bd2b43SDavid Greenman * Lookup PCB in hash list. 128915bd2b43SDavid Greenman */ 129015bd2b43SDavid Greenman struct inpcb * 1291136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1292136d4f1cSRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard, 1293136d4f1cSRobert Watson struct ifnet *ifp) 129415bd2b43SDavid Greenman { 129515bd2b43SDavid Greenman struct inpcbhead *head; 1296413628a7SBjoern A. Zeeb struct inpcb *inp, *tmpinp; 129715bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 129815bd2b43SDavid Greenman 12998501a69cSRobert Watson INP_INFO_LOCK_ASSERT(pcbinfo); 1300602cc7f1SRobert Watson 130115bd2b43SDavid Greenman /* 130215bd2b43SDavid Greenman * First look for an exact match. 130315bd2b43SDavid Greenman */ 1304413628a7SBjoern A. Zeeb tmpinp = NULL; 1305712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1306712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1307fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1308cfa1ca9dSYoshinobu Inoue #ifdef INET6 1309413628a7SBjoern A. Zeeb /* XXX inp locking */ 1310369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1311cfa1ca9dSYoshinobu Inoue continue; 1312cfa1ca9dSYoshinobu Inoue #endif 13136d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1314ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1315ca98b82cSDavid Greenman inp->inp_fport == fport && 1316413628a7SBjoern A. Zeeb inp->inp_lport == lport) { 1317413628a7SBjoern A. Zeeb /* 1318413628a7SBjoern A. Zeeb * XXX We should be able to directly return 1319413628a7SBjoern A. Zeeb * the inp here, without any checks. 1320413628a7SBjoern A. Zeeb * Well unless both bound with SO_REUSEPORT? 1321413628a7SBjoern A. Zeeb */ 1322413628a7SBjoern A. Zeeb if (jailed(inp->inp_cred)) 1323c3229e05SDavid Greenman return (inp); 1324413628a7SBjoern A. Zeeb if (tmpinp == NULL) 1325413628a7SBjoern A. Zeeb tmpinp = inp; 1326c3229e05SDavid Greenman } 1327413628a7SBjoern A. Zeeb } 1328413628a7SBjoern A. Zeeb if (tmpinp != NULL) 1329413628a7SBjoern A. Zeeb return (tmpinp); 1330e3fd5ffdSRobert Watson 1331e3fd5ffdSRobert Watson /* 1332e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1333e3fd5ffdSRobert Watson */ 1334413628a7SBjoern A. Zeeb if (wildcard == INPLOOKUP_WILDCARD) { 1335413628a7SBjoern A. Zeeb struct inpcb *local_wild = NULL, *local_exact = NULL; 1336e3fd5ffdSRobert Watson #ifdef INET6 1337cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1338e3fd5ffdSRobert Watson #endif 1339413628a7SBjoern A. Zeeb struct inpcb *jail_wild = NULL; 1340413628a7SBjoern A. Zeeb int injail; 1341413628a7SBjoern A. Zeeb 1342413628a7SBjoern A. Zeeb /* 1343413628a7SBjoern A. Zeeb * Order of socket selection - we always prefer jails. 1344413628a7SBjoern A. Zeeb * 1. jailed, non-wild. 1345413628a7SBjoern A. Zeeb * 2. jailed, wild. 1346413628a7SBjoern A. Zeeb * 3. non-jailed, non-wild. 1347413628a7SBjoern A. Zeeb * 4. non-jailed, wild. 1348413628a7SBjoern A. Zeeb */ 13496d6a026bSDavid Greenman 1350712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1351712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1352fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1353cfa1ca9dSYoshinobu Inoue #ifdef INET6 1354413628a7SBjoern A. Zeeb /* XXX inp locking */ 1355369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1356cfa1ca9dSYoshinobu Inoue continue; 1357cfa1ca9dSYoshinobu Inoue #endif 1358413628a7SBjoern A. Zeeb if (inp->inp_faddr.s_addr != INADDR_ANY || 1359413628a7SBjoern A. Zeeb inp->inp_lport != lport) 1360413628a7SBjoern A. Zeeb continue; 1361413628a7SBjoern A. Zeeb 1362413628a7SBjoern A. Zeeb /* XXX inp locking */ 1363cfa1ca9dSYoshinobu Inoue if (ifp && ifp->if_type == IFT_FAITH && 1364cfa1ca9dSYoshinobu Inoue (inp->inp_flags & INP_FAITH) == 0) 1365cfa1ca9dSYoshinobu Inoue continue; 1366413628a7SBjoern A. Zeeb 1367413628a7SBjoern A. Zeeb injail = jailed(inp->inp_cred); 1368413628a7SBjoern A. Zeeb if (injail) { 1369b89e82ddSJamie Gritton if (prison_check_ip4(inp->inp_cred, 1370b89e82ddSJamie Gritton &laddr) != 0) 1371413628a7SBjoern A. Zeeb continue; 1372413628a7SBjoern A. Zeeb } else { 1373413628a7SBjoern A. Zeeb if (local_exact != NULL) 1374413628a7SBjoern A. Zeeb continue; 1375413628a7SBjoern A. Zeeb } 1376413628a7SBjoern A. Zeeb 1377413628a7SBjoern A. Zeeb if (inp->inp_laddr.s_addr == laddr.s_addr) { 1378413628a7SBjoern A. Zeeb if (injail) 1379c3229e05SDavid Greenman return (inp); 1380413628a7SBjoern A. Zeeb else 1381413628a7SBjoern A. Zeeb local_exact = inp; 1382413628a7SBjoern A. Zeeb } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1383e3fd5ffdSRobert Watson #ifdef INET6 1384413628a7SBjoern A. Zeeb /* XXX inp locking, NULL check */ 13855cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) 1386cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1387cfa1ca9dSYoshinobu Inoue else 1388413628a7SBjoern A. Zeeb #endif /* INET6 */ 1389413628a7SBjoern A. Zeeb if (injail) 1390413628a7SBjoern A. Zeeb jail_wild = inp; 1391413628a7SBjoern A. Zeeb else 13926d6a026bSDavid Greenman local_wild = inp; 13936d6a026bSDavid Greenman } 1394413628a7SBjoern A. Zeeb } /* LIST_FOREACH */ 1395413628a7SBjoern A. Zeeb if (jail_wild != NULL) 1396413628a7SBjoern A. Zeeb return (jail_wild); 1397413628a7SBjoern A. Zeeb if (local_exact != NULL) 1398413628a7SBjoern A. Zeeb return (local_exact); 1399413628a7SBjoern A. Zeeb if (local_wild != NULL) 1400c3229e05SDavid Greenman return (local_wild); 1401413628a7SBjoern A. Zeeb #ifdef INET6 1402413628a7SBjoern A. Zeeb if (local_wild_mapped != NULL) 1403413628a7SBjoern A. Zeeb return (local_wild_mapped); 1404413628a7SBjoern A. Zeeb #endif /* defined(INET6) */ 1405413628a7SBjoern A. Zeeb } /* if (wildcard == INPLOOKUP_WILDCARD) */ 1406413628a7SBjoern A. Zeeb 14076d6a026bSDavid Greenman return (NULL); 140815bd2b43SDavid Greenman } 140915bd2b43SDavid Greenman 14107bc4aca7SDavid Greenman /* 1411c3229e05SDavid Greenman * Insert PCB onto various hash lists. 14127bc4aca7SDavid Greenman */ 1413c3229e05SDavid Greenman int 1414136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp) 141515bd2b43SDavid Greenman { 1416c3229e05SDavid Greenman struct inpcbhead *pcbhash; 1417c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 1418c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1419c3229e05SDavid Greenman struct inpcbport *phd; 1420cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 142115bd2b43SDavid Greenman 142259daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 14238501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1424111d57a6SRobert Watson KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 1425111d57a6SRobert Watson ("in_pcbinshash: INP_INHASHLIST")); 1426602cc7f1SRobert Watson 1427cfa1ca9dSYoshinobu Inoue #ifdef INET6 1428cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1429cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1430cfa1ca9dSYoshinobu Inoue else 1431cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1432cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1433cfa1ca9dSYoshinobu Inoue 1434712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1435712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 143615bd2b43SDavid Greenman 1437712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 1438712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 1439c3229e05SDavid Greenman 1440c3229e05SDavid Greenman /* 1441c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 1442c3229e05SDavid Greenman */ 1443fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 1444c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 1445c3229e05SDavid Greenman break; 1446c3229e05SDavid Greenman } 1447c3229e05SDavid Greenman /* 1448c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 1449c3229e05SDavid Greenman */ 1450c3229e05SDavid Greenman if (phd == NULL) { 14511ede983cSDag-Erling Smørgrav phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1452c3229e05SDavid Greenman if (phd == NULL) { 1453c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 1454c3229e05SDavid Greenman } 1455c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 1456c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 1457c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1458c3229e05SDavid Greenman } 1459c3229e05SDavid Greenman inp->inp_phd = phd; 1460c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1461c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1462111d57a6SRobert Watson inp->inp_flags |= INP_INHASHLIST; 1463c3229e05SDavid Greenman return (0); 146415bd2b43SDavid Greenman } 146515bd2b43SDavid Greenman 1466c3229e05SDavid Greenman /* 1467c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 1468c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 1469c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 1470c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 1471c3229e05SDavid Greenman */ 147215bd2b43SDavid Greenman void 1473136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp) 147415bd2b43SDavid Greenman { 147559daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 147615bd2b43SDavid Greenman struct inpcbhead *head; 1477cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 147815bd2b43SDavid Greenman 147959daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 14808501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1481111d57a6SRobert Watson KASSERT(inp->inp_flags & INP_INHASHLIST, 1482111d57a6SRobert Watson ("in_pcbrehash: !INP_INHASHLIST")); 1483602cc7f1SRobert Watson 1484cfa1ca9dSYoshinobu Inoue #ifdef INET6 1485cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1486cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1487cfa1ca9dSYoshinobu Inoue else 1488cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1489cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1490cfa1ca9dSYoshinobu Inoue 1491712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1492712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 149315bd2b43SDavid Greenman 1494c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 149515bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 1496c3229e05SDavid Greenman } 1497c3229e05SDavid Greenman 1498c3229e05SDavid Greenman /* 1499c3229e05SDavid Greenman * Remove PCB from various lists. 1500c3229e05SDavid Greenman */ 150176429de4SYoshinobu Inoue void 1502136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 1503c3229e05SDavid Greenman { 150459daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 150559daba27SSam Leffler 150659daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 15078501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 150859daba27SSam Leffler 150959daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1510111d57a6SRobert Watson if (inp->inp_flags & INP_INHASHLIST) { 1511c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 1512c3229e05SDavid Greenman 1513c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 1514c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 1515fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1516c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 1517c3229e05SDavid Greenman free(phd, M_PCB); 1518c3229e05SDavid Greenman } 1519111d57a6SRobert Watson inp->inp_flags &= ~INP_INHASHLIST; 1520c3229e05SDavid Greenman } 1521c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 152259daba27SSam Leffler pcbinfo->ipi_count--; 152315bd2b43SDavid Greenman } 152475c13541SPoul-Henning Kamp 1525a557af22SRobert Watson /* 1526a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 1527a557af22SRobert Watson * label change into the in_pcb for the socket. 1528a557af22SRobert Watson */ 1529a557af22SRobert Watson void 1530136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 1531a557af22SRobert Watson { 1532a557af22SRobert Watson #ifdef MAC 1533a557af22SRobert Watson struct inpcb *inp; 1534a557af22SRobert Watson 15354c7c478dSRobert Watson inp = sotoinpcb(so); 15364c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 1537602cc7f1SRobert Watson 15388501a69cSRobert Watson INP_WLOCK(inp); 1539310e7cebSRobert Watson SOCK_LOCK(so); 1540a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 1541310e7cebSRobert Watson SOCK_UNLOCK(so); 15428501a69cSRobert Watson INP_WUNLOCK(inp); 1543a557af22SRobert Watson #endif 1544a557af22SRobert Watson } 15455f311da2SMike Silbersack 15465f311da2SMike Silbersack /* 1547ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 1548ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 1549ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 1550ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 1551ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 15525f311da2SMike Silbersack */ 15535f311da2SMike Silbersack void 1554136d4f1cSRobert Watson ipport_tick(void *xtp) 15555f311da2SMike Silbersack { 15568b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 1557ad3a630fSRobert Watson 15588b615593SMarko Zec VNET_LIST_RLOCK(); 15598b615593SMarko Zec VNET_FOREACH(vnet_iter) { 15608b615593SMarko Zec CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 15618b615593SMarko Zec INIT_VNET_INET(vnet_iter); 15628b615593SMarko Zec if (V_ipport_tcpallocs <= 15638b615593SMarko Zec V_ipport_tcplastcount + V_ipport_randomcps) { 1564603724d3SBjoern A. Zeeb if (V_ipport_stoprandom > 0) 1565603724d3SBjoern A. Zeeb V_ipport_stoprandom--; 1566ad3a630fSRobert Watson } else 1567603724d3SBjoern A. Zeeb V_ipport_stoprandom = V_ipport_randomtime; 1568603724d3SBjoern A. Zeeb V_ipport_tcplastcount = V_ipport_tcpallocs; 15698b615593SMarko Zec CURVNET_RESTORE(); 15708b615593SMarko Zec } 15718b615593SMarko Zec VNET_LIST_RUNLOCK(); 15725f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 15735f311da2SMike Silbersack } 1574497057eeSRobert Watson 15753d585327SKip Macy void 15763d585327SKip Macy inp_wlock(struct inpcb *inp) 15773d585327SKip Macy { 15783d585327SKip Macy 15798501a69cSRobert Watson INP_WLOCK(inp); 15803d585327SKip Macy } 15813d585327SKip Macy 15823d585327SKip Macy void 15833d585327SKip Macy inp_wunlock(struct inpcb *inp) 15843d585327SKip Macy { 15853d585327SKip Macy 15868501a69cSRobert Watson INP_WUNLOCK(inp); 15873d585327SKip Macy } 15883d585327SKip Macy 15893d585327SKip Macy void 15903d585327SKip Macy inp_rlock(struct inpcb *inp) 15913d585327SKip Macy { 15923d585327SKip Macy 1593a69042a5SRobert Watson INP_RLOCK(inp); 15943d585327SKip Macy } 15953d585327SKip Macy 15963d585327SKip Macy void 15973d585327SKip Macy inp_runlock(struct inpcb *inp) 15983d585327SKip Macy { 15993d585327SKip Macy 1600a69042a5SRobert Watson INP_RUNLOCK(inp); 16013d585327SKip Macy } 16023d585327SKip Macy 16033d585327SKip Macy #ifdef INVARIANTS 16043d585327SKip Macy void 1605e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp) 16063d585327SKip Macy { 16073d585327SKip Macy 16088501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 16093d585327SKip Macy } 16103d585327SKip Macy 16113d585327SKip Macy void 1612e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp) 16133d585327SKip Macy { 16143d585327SKip Macy 16153d585327SKip Macy INP_UNLOCK_ASSERT(inp); 16163d585327SKip Macy } 16173d585327SKip Macy #endif 16183d585327SKip Macy 16199378e437SKip Macy void 16209378e437SKip Macy inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 16219378e437SKip Macy { 16228b615593SMarko Zec INIT_VNET_INET(curvnet); 16239378e437SKip Macy struct inpcb *inp; 16249378e437SKip Macy 1625603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_tcbinfo); 162697021c24SMarko Zec LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 16279378e437SKip Macy INP_WLOCK(inp); 16289378e437SKip Macy func(inp, arg); 16299378e437SKip Macy INP_WUNLOCK(inp); 16309378e437SKip Macy } 1631603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_tcbinfo); 16329378e437SKip Macy } 16339378e437SKip Macy 16349378e437SKip Macy struct socket * 16359378e437SKip Macy inp_inpcbtosocket(struct inpcb *inp) 16369378e437SKip Macy { 16379378e437SKip Macy 16389378e437SKip Macy INP_WLOCK_ASSERT(inp); 16399378e437SKip Macy return (inp->inp_socket); 16409378e437SKip Macy } 16419378e437SKip Macy 16429378e437SKip Macy struct tcpcb * 16439378e437SKip Macy inp_inpcbtotcpcb(struct inpcb *inp) 16449378e437SKip Macy { 16459378e437SKip Macy 16469378e437SKip Macy INP_WLOCK_ASSERT(inp); 16479378e437SKip Macy return ((struct tcpcb *)inp->inp_ppcb); 16489378e437SKip Macy } 16499378e437SKip Macy 16509378e437SKip Macy int 16519378e437SKip Macy inp_ip_tos_get(const struct inpcb *inp) 16529378e437SKip Macy { 16539378e437SKip Macy 16549378e437SKip Macy return (inp->inp_ip_tos); 16559378e437SKip Macy } 16569378e437SKip Macy 16579378e437SKip Macy void 16589378e437SKip Macy inp_ip_tos_set(struct inpcb *inp, int val) 16599378e437SKip Macy { 16609378e437SKip Macy 16619378e437SKip Macy inp->inp_ip_tos = val; 16629378e437SKip Macy } 16639378e437SKip Macy 16649378e437SKip Macy void 1665df9cf830STai-hwa Liang inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 16669d29c635SKip Macy uint32_t *faddr, uint16_t *fp) 16679378e437SKip Macy { 16689378e437SKip Macy 16699d29c635SKip Macy INP_LOCK_ASSERT(inp); 1670df9cf830STai-hwa Liang *laddr = inp->inp_laddr.s_addr; 1671df9cf830STai-hwa Liang *faddr = inp->inp_faddr.s_addr; 16729378e437SKip Macy *lp = inp->inp_lport; 16739378e437SKip Macy *fp = inp->inp_fport; 16749378e437SKip Macy } 16759378e437SKip Macy 1676dd0e6c38SKip Macy struct inpcb * 1677dd0e6c38SKip Macy so_sotoinpcb(struct socket *so) 1678dd0e6c38SKip Macy { 1679dd0e6c38SKip Macy 1680dd0e6c38SKip Macy return (sotoinpcb(so)); 1681dd0e6c38SKip Macy } 1682dd0e6c38SKip Macy 1683dd0e6c38SKip Macy struct tcpcb * 1684dd0e6c38SKip Macy so_sototcpcb(struct socket *so) 1685dd0e6c38SKip Macy { 1686dd0e6c38SKip Macy 1687dd0e6c38SKip Macy return (sototcpcb(so)); 1688dd0e6c38SKip Macy } 1689dd0e6c38SKip Macy 1690497057eeSRobert Watson #ifdef DDB 1691497057eeSRobert Watson static void 1692497057eeSRobert Watson db_print_indent(int indent) 1693497057eeSRobert Watson { 1694497057eeSRobert Watson int i; 1695497057eeSRobert Watson 1696497057eeSRobert Watson for (i = 0; i < indent; i++) 1697497057eeSRobert Watson db_printf(" "); 1698497057eeSRobert Watson } 1699497057eeSRobert Watson 1700497057eeSRobert Watson static void 1701497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 1702497057eeSRobert Watson { 1703497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 1704497057eeSRobert Watson 1705497057eeSRobert Watson db_print_indent(indent); 1706497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 1707497057eeSRobert Watson 1708497057eeSRobert Watson indent += 2; 1709497057eeSRobert Watson 171003dc38a4SRobert Watson #ifdef INET6 1711dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 1712497057eeSRobert Watson /* IPv6. */ 1713497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 1714497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 1715497057eeSRobert Watson } else { 171603dc38a4SRobert Watson #endif 1717497057eeSRobert Watson /* IPv4. */ 1718497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 1719497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 172003dc38a4SRobert Watson #ifdef INET6 1721497057eeSRobert Watson } 172203dc38a4SRobert Watson #endif 1723497057eeSRobert Watson db_print_indent(indent); 1724497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 1725497057eeSRobert Watson ntohs(inc->inc_lport)); 1726497057eeSRobert Watson db_print_indent(indent); 1727497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 1728497057eeSRobert Watson ntohs(inc->inc_fport)); 1729497057eeSRobert Watson } 1730497057eeSRobert Watson 1731497057eeSRobert Watson static void 1732497057eeSRobert Watson db_print_inpflags(int inp_flags) 1733497057eeSRobert Watson { 1734497057eeSRobert Watson int comma; 1735497057eeSRobert Watson 1736497057eeSRobert Watson comma = 0; 1737497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 1738497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 1739497057eeSRobert Watson comma = 1; 1740497057eeSRobert Watson } 1741497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 1742497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 1743497057eeSRobert Watson comma = 1; 1744497057eeSRobert Watson } 1745497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 1746497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 1747497057eeSRobert Watson comma = 1; 1748497057eeSRobert Watson } 1749497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 1750497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 1751497057eeSRobert Watson comma = 1; 1752497057eeSRobert Watson } 1753497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 1754497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 1755497057eeSRobert Watson comma = 1; 1756497057eeSRobert Watson } 1757497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 1758497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 1759497057eeSRobert Watson comma = 1; 1760497057eeSRobert Watson } 1761497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 1762497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 1763497057eeSRobert Watson comma = 1; 1764497057eeSRobert Watson } 1765497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 1766497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 1767497057eeSRobert Watson comma = 1; 1768497057eeSRobert Watson } 1769497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 1770497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 1771497057eeSRobert Watson comma = 1; 1772497057eeSRobert Watson } 1773497057eeSRobert Watson if (inp_flags & INP_FAITH) { 1774497057eeSRobert Watson db_printf("%sINP_FAITH", comma ? ", " : ""); 1775497057eeSRobert Watson comma = 1; 1776497057eeSRobert Watson } 1777497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 1778497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 1779497057eeSRobert Watson comma = 1; 1780497057eeSRobert Watson } 1781497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 1782497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 1783497057eeSRobert Watson comma = 1; 1784497057eeSRobert Watson } 1785497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 1786497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 1787497057eeSRobert Watson comma = 1; 1788497057eeSRobert Watson } 1789497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 1790497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 1791497057eeSRobert Watson comma = 1; 1792497057eeSRobert Watson } 1793497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 1794497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 1795497057eeSRobert Watson comma = 1; 1796497057eeSRobert Watson } 1797497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 1798497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 1799497057eeSRobert Watson comma = 1; 1800497057eeSRobert Watson } 1801497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 1802497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 1803497057eeSRobert Watson comma = 1; 1804497057eeSRobert Watson } 1805497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 1806497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 1807497057eeSRobert Watson comma = 1; 1808497057eeSRobert Watson } 1809497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 1810497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 1811497057eeSRobert Watson comma = 1; 1812497057eeSRobert Watson } 1813497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 1814497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 1815497057eeSRobert Watson comma = 1; 1816497057eeSRobert Watson } 1817497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 1818497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 1819497057eeSRobert Watson comma = 1; 1820497057eeSRobert Watson } 1821497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 1822497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 1823497057eeSRobert Watson comma = 1; 1824497057eeSRobert Watson } 1825497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 1826497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 1827497057eeSRobert Watson comma = 1; 1828497057eeSRobert Watson } 1829497057eeSRobert Watson } 1830497057eeSRobert Watson 1831497057eeSRobert Watson static void 1832497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 1833497057eeSRobert Watson { 1834497057eeSRobert Watson int comma; 1835497057eeSRobert Watson 1836497057eeSRobert Watson comma = 0; 1837497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 1838497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 1839497057eeSRobert Watson comma = 1; 1840497057eeSRobert Watson } 1841497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 1842497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 1843497057eeSRobert Watson comma = 1; 1844497057eeSRobert Watson } 1845497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 1846497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 1847497057eeSRobert Watson comma = 1; 1848497057eeSRobert Watson } 1849497057eeSRobert Watson if (inp_vflag & INP_TIMEWAIT) { 1850497057eeSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 1851497057eeSRobert Watson comma = 1; 1852497057eeSRobert Watson } 1853497057eeSRobert Watson if (inp_vflag & INP_ONESBCAST) { 1854497057eeSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 1855497057eeSRobert Watson comma = 1; 1856497057eeSRobert Watson } 1857497057eeSRobert Watson if (inp_vflag & INP_DROPPED) { 1858497057eeSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 1859497057eeSRobert Watson comma = 1; 1860497057eeSRobert Watson } 1861497057eeSRobert Watson if (inp_vflag & INP_SOCKREF) { 1862497057eeSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 1863497057eeSRobert Watson comma = 1; 1864497057eeSRobert Watson } 1865497057eeSRobert Watson } 1866497057eeSRobert Watson 1867497057eeSRobert Watson void 1868497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 1869497057eeSRobert Watson { 1870497057eeSRobert Watson 1871497057eeSRobert Watson db_print_indent(indent); 1872497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 1873497057eeSRobert Watson 1874497057eeSRobert Watson indent += 2; 1875497057eeSRobert Watson 1876497057eeSRobert Watson db_print_indent(indent); 1877497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 1878497057eeSRobert Watson 1879497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 1880497057eeSRobert Watson 1881497057eeSRobert Watson db_print_indent(indent); 1882497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 1883497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 1884497057eeSRobert Watson 1885497057eeSRobert Watson db_print_indent(indent); 1886497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 1887497057eeSRobert Watson inp->inp_label, inp->inp_flags); 1888497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 1889497057eeSRobert Watson db_printf(")\n"); 1890497057eeSRobert Watson 1891497057eeSRobert Watson db_print_indent(indent); 1892497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 1893497057eeSRobert Watson inp->inp_vflag); 1894497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 1895497057eeSRobert Watson db_printf(")\n"); 1896497057eeSRobert Watson 1897497057eeSRobert Watson db_print_indent(indent); 1898497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 1899497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 1900497057eeSRobert Watson 1901497057eeSRobert Watson db_print_indent(indent); 1902497057eeSRobert Watson #ifdef INET6 1903497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 1904497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 1905497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 1906497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 1907497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 1908497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 1909497057eeSRobert Watson inp->in6p_hops); 1910497057eeSRobert Watson } else 1911497057eeSRobert Watson #endif 1912497057eeSRobert Watson { 1913497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 1914497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 1915497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 1916497057eeSRobert Watson } 1917497057eeSRobert Watson 1918497057eeSRobert Watson db_print_indent(indent); 1919497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 1920497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 1921497057eeSRobert Watson } 1922497057eeSRobert Watson 1923497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 1924497057eeSRobert Watson { 1925497057eeSRobert Watson struct inpcb *inp; 1926497057eeSRobert Watson 1927497057eeSRobert Watson if (!have_addr) { 1928497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 1929497057eeSRobert Watson return; 1930497057eeSRobert Watson } 1931497057eeSRobert Watson inp = (struct inpcb *)addr; 1932497057eeSRobert Watson 1933497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 1934497057eeSRobert Watson } 1935497057eeSRobert Watson #endif 1936