1c398230bSWarner Losh /*- 22469dd60SGarrett Wollman * Copyright (c) 1982, 1986, 1991, 1993, 1995 3497057eeSRobert Watson * The Regents of the University of California. 4497057eeSRobert Watson * Copyright (c) 2007 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 32c3aac50fSPeter Wemm * $FreeBSD$ 33df8bae1dSRodney W. Grimes */ 34df8bae1dSRodney W. Grimes 35497057eeSRobert Watson #include "opt_ddb.h" 366a800098SYoshinobu Inoue #include "opt_ipsec.h" 37cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 38a557af22SRobert Watson #include "opt_mac.h" 39cfa1ca9dSYoshinobu Inoue 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 45df8bae1dSRodney W. Grimes #include <sys/protosw.h> 46df8bae1dSRodney W. Grimes #include <sys/socket.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48acd3428bSRobert Watson #include <sys/priv.h> 49df8bae1dSRodney W. Grimes #include <sys/proc.h> 5075c13541SPoul-Henning Kamp #include <sys/jail.h> 51101f9fc8SPeter Wemm #include <sys/kernel.h> 52101f9fc8SPeter Wemm #include <sys/sysctl.h> 538781d8e9SBruce Evans 54497057eeSRobert Watson #ifdef DDB 55497057eeSRobert Watson #include <ddb/ddb.h> 56497057eeSRobert Watson #endif 57497057eeSRobert Watson 5869c2d429SJeff Roberson #include <vm/uma.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <net/if.h> 61cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 62df8bae1dSRodney W. Grimes #include <net/route.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <netinet/in.h> 65df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 66df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 68340c35deSJonathan Lemon #include <netinet/tcp_var.h> 695f311da2SMike Silbersack #include <netinet/udp.h> 705f311da2SMike Silbersack #include <netinet/udp_var.h> 71cfa1ca9dSYoshinobu Inoue #ifdef INET6 72cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 73cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 74cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 75cfa1ca9dSYoshinobu Inoue 76df8bae1dSRodney W. Grimes 77b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 78b9234fafSSam Leffler #include <netipsec/ipsec.h> 79b9234fafSSam Leffler #include <netipsec/key.h> 80b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 81b9234fafSSam Leffler 82aed55708SRobert Watson #include <security/mac/mac_framework.h> 83aed55708SRobert Watson 84101f9fc8SPeter Wemm /* 85101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 86101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 87101f9fc8SPeter Wemm */ 8882cd038dSYoshinobu Inoue int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 8982cd038dSYoshinobu Inoue int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 909e5a5ed4SMike Silbersack int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 919e5a5ed4SMike Silbersack int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */ 9282cd038dSYoshinobu Inoue int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 9382cd038dSYoshinobu Inoue int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 94101f9fc8SPeter Wemm 95b0d22693SCrist J. Clark /* 96b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 97b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 98b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 99b0d22693SCrist J. Clark */ 100b0d22693SCrist J. Clark int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */ 101b0d22693SCrist J. Clark int ipport_reservedlow = 0; 102b0d22693SCrist J. Clark 1035f311da2SMike Silbersack /* Variables dealing with random ephemeral port allocation. */ 1045f311da2SMike Silbersack int ipport_randomized = 1; /* user controlled via sysctl */ 1055f311da2SMike Silbersack int ipport_randomcps = 10; /* user controlled via sysctl */ 1065f311da2SMike Silbersack int ipport_randomtime = 45; /* user controlled via sysctl */ 1075f311da2SMike Silbersack int ipport_stoprandom = 0; /* toggled by ipport_tick */ 1085f311da2SMike Silbersack int ipport_tcpallocs; 1095f311da2SMike Silbersack int ipport_tcplastcount; 1106ac48b74SMike Silbersack 111bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 112bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 113bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 114bbd42ad0SPeter Wemm 115bbd42ad0SPeter Wemm static int 11682d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 117bbd42ad0SPeter Wemm { 11830a4ab08SBruce Evans int error; 11930a4ab08SBruce Evans 12030a4ab08SBruce Evans error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 12130a4ab08SBruce Evans if (error == 0) { 122bbd42ad0SPeter Wemm RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 123bbd42ad0SPeter Wemm RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 12430a4ab08SBruce Evans RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 12530a4ab08SBruce Evans RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 12630a4ab08SBruce Evans RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 12730a4ab08SBruce Evans RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 128bbd42ad0SPeter Wemm } 12930a4ab08SBruce Evans return (error); 130bbd42ad0SPeter Wemm } 131bbd42ad0SPeter Wemm 132bbd42ad0SPeter Wemm #undef RANGECHK 133bbd42ad0SPeter Wemm 13433b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 13533b3ac06SPeter Wemm 136bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW, 137bbd42ad0SPeter Wemm &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", ""); 138bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW, 139bbd42ad0SPeter Wemm &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", ""); 140bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW, 141bbd42ad0SPeter Wemm &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", ""); 142bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW, 143bbd42ad0SPeter Wemm &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", ""); 144bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW, 145bbd42ad0SPeter Wemm &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", ""); 146bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW, 147bbd42ad0SPeter Wemm &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", ""); 148b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 149b0d22693SCrist J. Clark CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, ""); 150b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 151b0d22693SCrist J. Clark CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, ""); 1526ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW, 1536ee79c59SMaxim Konovalov &ipport_randomized, 0, "Enable random port allocation"); 1546ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW, 1556ee79c59SMaxim Konovalov &ipport_randomcps, 0, "Maximum number of random port " 1566ee79c59SMaxim Konovalov "allocations before switching to a sequental one"); 1576ee79c59SMaxim Konovalov SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW, 1586ee79c59SMaxim Konovalov &ipport_randomtime, 0, "Minimum time to keep sequental port " 1596ee79c59SMaxim Konovalov "allocation before switching to a random one"); 1600312fbe9SPoul-Henning Kamp 161c3229e05SDavid Greenman /* 162c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 163c3229e05SDavid Greenman * 164de35559fSRobert Watson * NOTE: It is assumed that most of these functions will be called with 165de35559fSRobert Watson * the pcbinfo lock held, and often, the inpcb lock held, as these utility 166de35559fSRobert Watson * functions often modify hash chains or addresses in pcbs. 167c3229e05SDavid Greenman */ 168c3229e05SDavid Greenman 169c3229e05SDavid Greenman /* 170c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 171d915b280SStephan Uphoff * On success return with the PCB locked. 172c3229e05SDavid Greenman */ 173df8bae1dSRodney W. Grimes int 174d915b280SStephan Uphoff in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 175df8bae1dSRodney W. Grimes { 176136d4f1cSRobert Watson struct inpcb *inp; 17713cf67f3SHajimu UMEMOTO int error; 178a557af22SRobert Watson 17959daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 180a557af22SRobert Watson error = 0; 181d915b280SStephan Uphoff inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 182df8bae1dSRodney W. Grimes if (inp == NULL) 183df8bae1dSRodney W. Grimes return (ENOBUFS); 184d915b280SStephan Uphoff bzero(inp, inp_zero_size); 18515bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 186df8bae1dSRodney W. Grimes inp->inp_socket = so; 187a557af22SRobert Watson #ifdef MAC 188a557af22SRobert Watson error = mac_init_inpcb(inp, M_NOWAIT); 189a557af22SRobert Watson if (error != 0) 190a557af22SRobert Watson goto out; 191310e7cebSRobert Watson SOCK_LOCK(so); 192a557af22SRobert Watson mac_create_inpcb_from_socket(so, inp); 193310e7cebSRobert Watson SOCK_UNLOCK(so); 194a557af22SRobert Watson #endif 1952cb64cb2SGeorge V. Neville-Neil 196b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 19713cf67f3SHajimu UMEMOTO error = ipsec_init_policy(so, &inp->inp_sp); 198a557af22SRobert Watson if (error != 0) 199a557af22SRobert Watson goto out; 200b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 201e3fd5ffdSRobert Watson #ifdef INET6 202340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 203340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 204340c35deSJonathan Lemon if (ip6_v6only) 20533841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 206340c35deSJonathan Lemon } 20775daea93SPaul Saab #endif 208712fc218SRobert Watson LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 2093d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 210df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 21133841545SHajimu UMEMOTO #ifdef INET6 21233841545SHajimu UMEMOTO if (ip6_auto_flowlabel) 21333841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 21433841545SHajimu UMEMOTO #endif 215d915b280SStephan Uphoff INP_LOCK(inp); 216d915b280SStephan Uphoff inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 217d915b280SStephan Uphoff 218b2630c29SGeorge V. Neville-Neil #if defined(IPSEC) || defined(MAC) 219a557af22SRobert Watson out: 220a557af22SRobert Watson if (error != 0) 221a557af22SRobert Watson uma_zfree(pcbinfo->ipi_zone, inp); 222a557af22SRobert Watson #endif 223a557af22SRobert Watson return (error); 224df8bae1dSRodney W. Grimes } 225df8bae1dSRodney W. Grimes 226df8bae1dSRodney W. Grimes int 227136d4f1cSRobert Watson in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 228df8bae1dSRodney W. Grimes { 2294b932371SIan Dowse int anonport, error; 2304b932371SIan Dowse 2311b73ca0bSSam Leffler INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 23259daba27SSam Leffler INP_LOCK_ASSERT(inp); 23359daba27SSam Leffler 2344b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 2354b932371SIan Dowse return (EINVAL); 2364b932371SIan Dowse anonport = inp->inp_lport == 0 && (nam == NULL || 2374b932371SIan Dowse ((struct sockaddr_in *)nam)->sin_port == 0); 2384b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 239b0330ed9SPawel Jakub Dawidek &inp->inp_lport, cred); 2404b932371SIan Dowse if (error) 2414b932371SIan Dowse return (error); 2424b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 2434b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 2444b932371SIan Dowse inp->inp_lport = 0; 2454b932371SIan Dowse return (EAGAIN); 2464b932371SIan Dowse } 2474b932371SIan Dowse if (anonport) 2484b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 2494b932371SIan Dowse return (0); 2504b932371SIan Dowse } 2514b932371SIan Dowse 2524b932371SIan Dowse /* 2534b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 2544b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 2554b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 2564b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 2574b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 2584b932371SIan Dowse * 2594b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 2604b932371SIan Dowse */ 2614b932371SIan Dowse int 262136d4f1cSRobert Watson in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 263136d4f1cSRobert Watson u_short *lportp, struct ucred *cred) 2644b932371SIan Dowse { 2654b932371SIan Dowse struct socket *so = inp->inp_socket; 26637bd2b30SPeter Wemm unsigned short *lastport; 26715bd2b43SDavid Greenman struct sockaddr_in *sin; 268c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2694b932371SIan Dowse struct in_addr laddr; 270df8bae1dSRodney W. Grimes u_short lport = 0; 2714cc20ab1SSeigo Tanimura int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 27275c13541SPoul-Henning Kamp int error, prison = 0; 2735f311da2SMike Silbersack int dorandom; 274df8bae1dSRodney W. Grimes 2751b73ca0bSSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 27659daba27SSam Leffler INP_LOCK_ASSERT(inp); 27759daba27SSam Leffler 27859562606SGarrett Wollman if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */ 279df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 2804b932371SIan Dowse laddr.s_addr = *laddrp; 2814b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 282df8bae1dSRodney W. Grimes return (EINVAL); 283c3229e05SDavid Greenman if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 284421d8aa6SBjoern A. Zeeb wild = INPLOOKUP_WILDCARD; 285df8bae1dSRodney W. Grimes if (nam) { 28657bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 28757bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 288df8bae1dSRodney W. Grimes return (EINVAL); 289df8bae1dSRodney W. Grimes #ifdef notdef 290df8bae1dSRodney W. Grimes /* 291df8bae1dSRodney W. Grimes * We should check the family, but old programs 292df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 293df8bae1dSRodney W. Grimes */ 294df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 295df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 296df8bae1dSRodney W. Grimes #endif 297e4bdf25dSPoul-Henning Kamp if (sin->sin_addr.s_addr != INADDR_ANY) 298b0330ed9SPawel Jakub Dawidek if (prison_ip(cred, 0, &sin->sin_addr.s_addr)) 29975c13541SPoul-Henning Kamp return(EINVAL); 3004b932371SIan Dowse if (sin->sin_port != *lportp) { 3014b932371SIan Dowse /* Don't allow the port to change. */ 3024b932371SIan Dowse if (*lportp != 0) 3034b932371SIan Dowse return (EINVAL); 304df8bae1dSRodney W. Grimes lport = sin->sin_port; 3054b932371SIan Dowse } 3064b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 307df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 308df8bae1dSRodney W. Grimes /* 309df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 310df8bae1dSRodney W. Grimes * allow complete duplication of binding if 311df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 312df8bae1dSRodney W. Grimes * and a multicast address is bound on both 313df8bae1dSRodney W. Grimes * new and duplicated sockets. 314df8bae1dSRodney W. Grimes */ 315df8bae1dSRodney W. Grimes if (so->so_options & SO_REUSEADDR) 316df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 317df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 318df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 31983103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 320df8bae1dSRodney W. Grimes if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 321df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 322df8bae1dSRodney W. Grimes } 3234b932371SIan Dowse laddr = sin->sin_addr; 324df8bae1dSRodney W. Grimes if (lport) { 325df8bae1dSRodney W. Grimes struct inpcb *t; 326ae0e7143SRobert Watson struct tcptw *tw; 327ae0e7143SRobert Watson 328df8bae1dSRodney W. Grimes /* GROSS */ 329b0d22693SCrist J. Clark if (ntohs(lport) <= ipport_reservedhigh && 330b0d22693SCrist J. Clark ntohs(lport) >= ipport_reservedlow && 331acd3428bSRobert Watson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 33232f9753cSRobert Watson 0)) 3332469dd60SGarrett Wollman return (EACCES); 334b0330ed9SPawel Jakub Dawidek if (jailed(cred)) 33575c13541SPoul-Henning Kamp prison = 1; 336835d4b89SPawel Jakub Dawidek if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 3376493245dSRobert Watson priv_check_cred(so->so_cred, 33832f9753cSRobert Watson PRIV_NETINET_REUSEPORT, 0) != 0) { 3394049a042SGuido van Rooij t = in_pcblookup_local(inp->inp_pcbinfo, 34075c13541SPoul-Henning Kamp sin->sin_addr, lport, 34175c13541SPoul-Henning Kamp prison ? 0 : INPLOOKUP_WILDCARD); 342340c35deSJonathan Lemon /* 343340c35deSJonathan Lemon * XXX 344340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 345340c35deSJonathan Lemon */ 3464cc20ab1SSeigo Tanimura if (t && 3474658dc83SYaroslav Tykhiy ((t->inp_vflag & INP_TIMEWAIT) == 0) && 3484658dc83SYaroslav Tykhiy (so->so_type != SOCK_STREAM || 3494658dc83SYaroslav Tykhiy ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 3504cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 35152b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 35252b65dbeSBill Fenner (t->inp_socket->so_options & 35352b65dbeSBill Fenner SO_REUSEPORT) == 0) && 3542f9a2132SBrian Feldman (so->so_cred->cr_uid != 355a4eb4405SYaroslav Tykhiy t->inp_socket->so_cred->cr_uid)) 3564049a042SGuido van Rooij return (EADDRINUSE); 3574049a042SGuido van Rooij } 358b0330ed9SPawel Jakub Dawidek if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr)) 359970680faSPoul-Henning Kamp return (EADDRNOTAVAIL); 360c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 36175c13541SPoul-Henning Kamp lport, prison ? 0 : wild); 362340c35deSJonathan Lemon if (t && (t->inp_vflag & INP_TIMEWAIT)) { 363ae0e7143SRobert Watson /* 364ae0e7143SRobert Watson * XXXRW: If an incpb has had its timewait 365ae0e7143SRobert Watson * state recycled, we treat the address as 366ae0e7143SRobert Watson * being in use (for now). This is better 367ae0e7143SRobert Watson * than a panic, but not desirable. 368ae0e7143SRobert Watson */ 369ae0e7143SRobert Watson tw = intotw(inp); 370ae0e7143SRobert Watson if (tw == NULL || 371ae0e7143SRobert Watson (reuseport & tw->tw_so_options) == 0) 372340c35deSJonathan Lemon return (EADDRINUSE); 373ae0e7143SRobert Watson } else if (t && 3744cc20ab1SSeigo Tanimura (reuseport & t->inp_socket->so_options) == 0) { 375e3fd5ffdSRobert Watson #ifdef INET6 37633841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 377cfa1ca9dSYoshinobu Inoue INADDR_ANY || 378cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 379cfa1ca9dSYoshinobu Inoue INADDR_ANY || 380cfa1ca9dSYoshinobu Inoue INP_SOCKAF(so) == 381cfa1ca9dSYoshinobu Inoue INP_SOCKAF(t->inp_socket)) 382e3fd5ffdSRobert Watson #endif 383df8bae1dSRodney W. Grimes return (EADDRINUSE); 384df8bae1dSRodney W. Grimes } 385cfa1ca9dSYoshinobu Inoue } 386df8bae1dSRodney W. Grimes } 3874b932371SIan Dowse if (*lportp != 0) 3884b932371SIan Dowse lport = *lportp; 38933b3ac06SPeter Wemm if (lport == 0) { 3906ac48b74SMike Silbersack u_short first, last; 391174624e0SMike Silbersack int count; 39233b3ac06SPeter Wemm 3934b932371SIan Dowse if (laddr.s_addr != INADDR_ANY) 394b0330ed9SPawel Jakub Dawidek if (prison_ip(cred, 0, &laddr.s_addr)) 39575c13541SPoul-Henning Kamp return (EINVAL); 396321a2846SPoul-Henning Kamp 39733b3ac06SPeter Wemm if (inp->inp_flags & INP_HIGHPORT) { 39833b3ac06SPeter Wemm first = ipport_hifirstauto; /* sysctl */ 39933b3ac06SPeter Wemm last = ipport_hilastauto; 400712fc218SRobert Watson lastport = &pcbinfo->ipi_lasthi; 40133b3ac06SPeter Wemm } else if (inp->inp_flags & INP_LOWPORT) { 402acd3428bSRobert Watson error = priv_check_cred(cred, 40332f9753cSRobert Watson PRIV_NETINET_RESERVEDPORT, 0); 404acd3428bSRobert Watson if (error) 405a29f300eSGarrett Wollman return error; 406bbd42ad0SPeter Wemm first = ipport_lowfirstauto; /* 1023 */ 407bbd42ad0SPeter Wemm last = ipport_lowlastauto; /* 600 */ 408712fc218SRobert Watson lastport = &pcbinfo->ipi_lastlow; 40933b3ac06SPeter Wemm } else { 41033b3ac06SPeter Wemm first = ipport_firstauto; /* sysctl */ 41133b3ac06SPeter Wemm last = ipport_lastauto; 412712fc218SRobert Watson lastport = &pcbinfo->ipi_lastport; 41333b3ac06SPeter Wemm } 41433b3ac06SPeter Wemm /* 4155f311da2SMike Silbersack * For UDP, use random port allocation as long as the user 4165f311da2SMike Silbersack * allows it. For TCP (and as of yet unknown) connections, 4175f311da2SMike Silbersack * use random port allocation only if the user allows it AND 41829f2a6ecSMaxim Konovalov * ipport_tick() allows it. 4195f311da2SMike Silbersack */ 4205f311da2SMike Silbersack if (ipport_randomized && 4215f311da2SMike Silbersack (!ipport_stoprandom || pcbinfo == &udbinfo)) 4225f311da2SMike Silbersack dorandom = 1; 4235f311da2SMike Silbersack else 4245f311da2SMike Silbersack dorandom = 0; 425e99971bfSMaxim Konovalov /* 426e99971bfSMaxim Konovalov * It makes no sense to do random port allocation if 427e99971bfSMaxim Konovalov * we have the only port available. 428e99971bfSMaxim Konovalov */ 429e99971bfSMaxim Konovalov if (first == last) 430e99971bfSMaxim Konovalov dorandom = 0; 4315f311da2SMike Silbersack /* Make sure to not include UDP packets in the count. */ 4325f311da2SMike Silbersack if (pcbinfo != &udbinfo) 4335f311da2SMike Silbersack ipport_tcpallocs++; 4345f311da2SMike Silbersack /* 43533b3ac06SPeter Wemm * Simple check to ensure all ports are not used up causing 43633b3ac06SPeter Wemm * a deadlock here. 43733b3ac06SPeter Wemm * 43833b3ac06SPeter Wemm * We split the two cases (up and down) so that the direction 43933b3ac06SPeter Wemm * is not being tested on each round of the loop. 44033b3ac06SPeter Wemm */ 44133b3ac06SPeter Wemm if (first > last) { 44233b3ac06SPeter Wemm /* 44333b3ac06SPeter Wemm * counting down 44433b3ac06SPeter Wemm */ 4455f311da2SMike Silbersack if (dorandom) 4466b2fc10bSMike Silbersack *lastport = first - 4476b2fc10bSMike Silbersack (arc4random() % (first - last)); 44833b3ac06SPeter Wemm count = first - last; 449174624e0SMike Silbersack 450df8bae1dSRodney W. Grimes do { 4516ac48b74SMike Silbersack if (count-- < 0) /* completely used? */ 452550b1518SWes Peters return (EADDRNOTAVAIL); 45333b3ac06SPeter Wemm --*lastport; 45433b3ac06SPeter Wemm if (*lastport > first || *lastport < last) 45533b3ac06SPeter Wemm *lastport = first; 45615bd2b43SDavid Greenman lport = htons(*lastport); 4574b932371SIan Dowse } while (in_pcblookup_local(pcbinfo, laddr, lport, 4584b932371SIan Dowse wild)); 45933b3ac06SPeter Wemm } else { 46033b3ac06SPeter Wemm /* 46133b3ac06SPeter Wemm * counting up 46233b3ac06SPeter Wemm */ 4635f311da2SMike Silbersack if (dorandom) 4646b2fc10bSMike Silbersack *lastport = first + 4656b2fc10bSMike Silbersack (arc4random() % (last - first)); 46633b3ac06SPeter Wemm count = last - first; 467174624e0SMike Silbersack 46833b3ac06SPeter Wemm do { 4696ac48b74SMike Silbersack if (count-- < 0) /* completely used? */ 470550b1518SWes Peters return (EADDRNOTAVAIL); 47133b3ac06SPeter Wemm ++*lastport; 47233b3ac06SPeter Wemm if (*lastport < first || *lastport > last) 47333b3ac06SPeter Wemm *lastport = first; 47433b3ac06SPeter Wemm lport = htons(*lastport); 4754b932371SIan Dowse } while (in_pcblookup_local(pcbinfo, laddr, lport, 4764b932371SIan Dowse wild)); 47733b3ac06SPeter Wemm } 47833b3ac06SPeter Wemm } 479b0330ed9SPawel Jakub Dawidek if (prison_ip(cred, 0, &laddr.s_addr)) 480e4bdf25dSPoul-Henning Kamp return (EINVAL); 4814b932371SIan Dowse *laddrp = laddr.s_addr; 4824b932371SIan Dowse *lportp = lport; 483df8bae1dSRodney W. Grimes return (0); 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes 486999f1343SGarrett Wollman /* 4875200e00eSIan Dowse * Connect from a socket to a specified address. 4885200e00eSIan Dowse * Both address and port must be specified in argument sin. 4895200e00eSIan Dowse * If don't have a local address for this socket yet, 4905200e00eSIan Dowse * then pick one. 491999f1343SGarrett Wollman */ 492999f1343SGarrett Wollman int 493136d4f1cSRobert Watson in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 494999f1343SGarrett Wollman { 4955200e00eSIan Dowse u_short lport, fport; 4965200e00eSIan Dowse in_addr_t laddr, faddr; 4975200e00eSIan Dowse int anonport, error; 498df8bae1dSRodney W. Grimes 49927f74fd0SRobert Watson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 50027f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 50127f74fd0SRobert Watson 5025200e00eSIan Dowse lport = inp->inp_lport; 5035200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 5045200e00eSIan Dowse anonport = (lport == 0); 5055200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 506b0330ed9SPawel Jakub Dawidek NULL, cred); 5075200e00eSIan Dowse if (error) 5085200e00eSIan Dowse return (error); 5095200e00eSIan Dowse 5105200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 5115200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 5125200e00eSIan Dowse inp->inp_lport = lport; 5135200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 5145200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 5155200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 5165200e00eSIan Dowse inp->inp_lport = 0; 5175200e00eSIan Dowse return (EAGAIN); 5185200e00eSIan Dowse } 5195200e00eSIan Dowse } 5205200e00eSIan Dowse 5215200e00eSIan Dowse /* Commit the remaining changes. */ 5225200e00eSIan Dowse inp->inp_lport = lport; 5235200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 5245200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 5255200e00eSIan Dowse inp->inp_fport = fport; 5265200e00eSIan Dowse in_pcbrehash(inp); 5272cb64cb2SGeorge V. Neville-Neil 5285200e00eSIan Dowse if (anonport) 5295200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 5305200e00eSIan Dowse return (0); 5315200e00eSIan Dowse } 5325200e00eSIan Dowse 5335200e00eSIan Dowse /* 5345200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 5355200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 5365200e00eSIan Dowse * address and port for the PCB; these are updated to the values 5375200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 5385200e00eSIan Dowse * the connect. 5395200e00eSIan Dowse * 5405200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 5415200e00eSIan Dowse * and port. These are not updated in the error case. 5425200e00eSIan Dowse * 5435200e00eSIan Dowse * If the operation fails because the connection already exists, 5445200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 5455200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 5465200e00eSIan Dowse * is set to NULL. 5475200e00eSIan Dowse */ 5485200e00eSIan Dowse int 549136d4f1cSRobert Watson in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 550136d4f1cSRobert Watson in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 551136d4f1cSRobert Watson struct inpcb **oinpp, struct ucred *cred) 5525200e00eSIan Dowse { 5535200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 5545200e00eSIan Dowse struct in_ifaddr *ia; 5555200e00eSIan Dowse struct sockaddr_in sa; 556b0330ed9SPawel Jakub Dawidek struct ucred *socred; 5575200e00eSIan Dowse struct inpcb *oinp; 5585200e00eSIan Dowse struct in_addr laddr, faddr; 5595200e00eSIan Dowse u_short lport, fport; 5605200e00eSIan Dowse int error; 5615200e00eSIan Dowse 56227f74fd0SRobert Watson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 56327f74fd0SRobert Watson INP_LOCK_ASSERT(inp); 56427f74fd0SRobert Watson 5655200e00eSIan Dowse if (oinpp != NULL) 5665200e00eSIan Dowse *oinpp = NULL; 56757bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 568df8bae1dSRodney W. Grimes return (EINVAL); 569df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 570df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 571df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 572df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 5735200e00eSIan Dowse laddr.s_addr = *laddrp; 5745200e00eSIan Dowse lport = *lportp; 5755200e00eSIan Dowse faddr = sin->sin_addr; 5765200e00eSIan Dowse fport = sin->sin_port; 577b0330ed9SPawel Jakub Dawidek socred = inp->inp_socket->so_cred; 578b0330ed9SPawel Jakub Dawidek if (laddr.s_addr == INADDR_ANY && jailed(socred)) { 5795200e00eSIan Dowse bzero(&sa, sizeof(sa)); 580b0330ed9SPawel Jakub Dawidek sa.sin_addr.s_addr = htonl(prison_getip(socred)); 5815200e00eSIan Dowse sa.sin_len = sizeof(sa); 5825200e00eSIan Dowse sa.sin_family = AF_INET; 5835200e00eSIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&sa, 584b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, cred); 5855200e00eSIan Dowse if (error) 5865200e00eSIan Dowse return (error); 5875200e00eSIan Dowse } 58859562606SGarrett Wollman if (!TAILQ_EMPTY(&in_ifaddrhead)) { 589df8bae1dSRodney W. Grimes /* 590df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 591df8bae1dSRodney W. Grimes * use the primary local address. 592df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 593df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 594df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 595df8bae1dSRodney W. Grimes */ 5965200e00eSIan Dowse if (faddr.s_addr == INADDR_ANY) 5975200e00eSIan Dowse faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr; 5985200e00eSIan Dowse else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 5995200e00eSIan Dowse (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & 6005200e00eSIan Dowse IFF_BROADCAST)) 6015200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 6025200e00eSIan Dowse &in_ifaddrhead)->ia_broadaddr)->sin_addr; 603df8bae1dSRodney W. Grimes } 6045200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 605df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)0; 606df8bae1dSRodney W. Grimes /* 60797d8d152SAndre Oppermann * If route is known our src addr is taken from the i/f, 60897d8d152SAndre Oppermann * else punt. 609cf744713SAndre Oppermann * 610cf744713SAndre Oppermann * Find out route to destination 611df8bae1dSRodney W. Grimes */ 612cf744713SAndre Oppermann if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 613cf744713SAndre Oppermann ia = ip_rtaddr(faddr); 614df8bae1dSRodney W. Grimes /* 615cf744713SAndre Oppermann * If we found a route, use the address corresponding to 616cf744713SAndre Oppermann * the outgoing interface. 617cf744713SAndre Oppermann * 618cf744713SAndre Oppermann * Otherwise assume faddr is reachable on a directly connected 619cf744713SAndre Oppermann * network and try to find a corresponding interface to take 620cf744713SAndre Oppermann * the source address from. 621df8bae1dSRodney W. Grimes */ 622df8bae1dSRodney W. Grimes if (ia == 0) { 6235200e00eSIan Dowse bzero(&sa, sizeof(sa)); 6245200e00eSIan Dowse sa.sin_addr = faddr; 6255200e00eSIan Dowse sa.sin_len = sizeof(sa); 6265200e00eSIan Dowse sa.sin_family = AF_INET; 627df8bae1dSRodney W. Grimes 6285200e00eSIan Dowse ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa))); 629df8bae1dSRodney W. Grimes if (ia == 0) 6305200e00eSIan Dowse ia = ifatoia(ifa_ifwithnet(sintosa(&sa))); 631df8bae1dSRodney W. Grimes if (ia == 0) 632ef14c369SMaxim Konovalov return (ENETUNREACH); 633df8bae1dSRodney W. Grimes } 634df8bae1dSRodney W. Grimes /* 635df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 636df8bae1dSRodney W. Grimes * interface has been set as a multicast option, use the 637df8bae1dSRodney W. Grimes * address of that interface as our source address. 638df8bae1dSRodney W. Grimes */ 6395200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 640df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 641df8bae1dSRodney W. Grimes struct ip_moptions *imo; 642df8bae1dSRodney W. Grimes struct ifnet *ifp; 643df8bae1dSRodney W. Grimes 644df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 645df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 646df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 64737d40066SPoul-Henning Kamp TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 648df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp) 649df8bae1dSRodney W. Grimes break; 650df8bae1dSRodney W. Grimes if (ia == 0) 651df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 652df8bae1dSRodney W. Grimes } 653df8bae1dSRodney W. Grimes } 6545200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 655999f1343SGarrett Wollman } 656999f1343SGarrett Wollman 6575200e00eSIan Dowse oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 6585200e00eSIan Dowse 0, NULL); 6595200e00eSIan Dowse if (oinp != NULL) { 6605200e00eSIan Dowse if (oinpp != NULL) 6615200e00eSIan Dowse *oinpp = oinp; 662df8bae1dSRodney W. Grimes return (EADDRINUSE); 663c3229e05SDavid Greenman } 6645200e00eSIan Dowse if (lport == 0) { 665b0330ed9SPawel Jakub Dawidek error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 666b0330ed9SPawel Jakub Dawidek cred); 6675a903f8dSPierre Beyssac if (error) 6685a903f8dSPierre Beyssac return (error); 6695a903f8dSPierre Beyssac } 6705200e00eSIan Dowse *laddrp = laddr.s_addr; 6715200e00eSIan Dowse *lportp = lport; 6725200e00eSIan Dowse *faddrp = faddr.s_addr; 6735200e00eSIan Dowse *fportp = fport; 674df8bae1dSRodney W. Grimes return (0); 675df8bae1dSRodney W. Grimes } 676df8bae1dSRodney W. Grimes 67726f9a767SRodney W. Grimes void 678136d4f1cSRobert Watson in_pcbdisconnect(struct inpcb *inp) 679df8bae1dSRodney W. Grimes { 6806b348152SRobert Watson 681fe6bfc37SRobert Watson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 68259daba27SSam Leffler INP_LOCK_ASSERT(inp); 683df8bae1dSRodney W. Grimes 684df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 685df8bae1dSRodney W. Grimes inp->inp_fport = 0; 68615bd2b43SDavid Greenman in_pcbrehash(inp); 687df8bae1dSRodney W. Grimes } 688df8bae1dSRodney W. Grimes 6894c7c478dSRobert Watson /* 6904c7c478dSRobert Watson * In the old world order, in_pcbdetach() served two functions: to detach the 6914c7c478dSRobert Watson * pcb from the socket/potentially free the socket, and to free the pcb 6924c7c478dSRobert Watson * itself. In the new world order, the protocol code is responsible for 6934c7c478dSRobert Watson * managing the relationship with the socket, and this code simply frees the 6944c7c478dSRobert Watson * pcb. 6954c7c478dSRobert Watson */ 69626f9a767SRodney W. Grimes void 697136d4f1cSRobert Watson in_pcbdetach(struct inpcb *inp) 698df8bae1dSRodney W. Grimes { 6994c7c478dSRobert Watson 7004c7c478dSRobert Watson KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL")); 7014c7c478dSRobert Watson inp->inp_socket->so_pcb = NULL; 7024c7c478dSRobert Watson inp->inp_socket = NULL; 7034c7c478dSRobert Watson } 7044c7c478dSRobert Watson 7054c7c478dSRobert Watson void 7064c7c478dSRobert Watson in_pcbfree(struct inpcb *inp) 7074c7c478dSRobert Watson { 7083d4d47f3SGarrett Wollman struct inpcbinfo *ipi = inp->inp_pcbinfo; 709df8bae1dSRodney W. Grimes 7104c7c478dSRobert Watson KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL")); 711fe6bfc37SRobert Watson INP_INFO_WLOCK_ASSERT(ipi); 71259daba27SSam Leffler INP_LOCK_ASSERT(inp); 71359daba27SSam Leffler 714b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 715cfa1ca9dSYoshinobu Inoue ipsec4_delete_pcbpolicy(inp); 716b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 7173d4d47f3SGarrett Wollman inp->inp_gencnt = ++ipi->ipi_gencnt; 718c3229e05SDavid Greenman in_pcbremlists(inp); 719df8bae1dSRodney W. Grimes if (inp->inp_options) 720df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 72171498f30SBruce M Simpson if (inp->inp_moptions != NULL) 72271498f30SBruce M Simpson inp_freemoptions(inp->inp_moptions); 723cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 724d915b280SStephan Uphoff 725a557af22SRobert Watson #ifdef MAC 726a557af22SRobert Watson mac_destroy_inpcb(inp); 727a557af22SRobert Watson #endif 728d915b280SStephan Uphoff INP_UNLOCK(inp); 72969c2d429SJeff Roberson uma_zfree(ipi->ipi_zone, inp); 730df8bae1dSRodney W. Grimes } 731df8bae1dSRodney W. Grimes 73210702a28SRobert Watson /* 73310702a28SRobert Watson * TCP needs to maintain its inpcb structure after the TCP connection has 73410702a28SRobert Watson * been torn down. However, it must be disconnected from the inpcb hashes as 73510702a28SRobert Watson * it must not prevent binding of future connections to the same port/ip 73610702a28SRobert Watson * combination by other inpcbs. 73710702a28SRobert Watson */ 73810702a28SRobert Watson void 73910702a28SRobert Watson in_pcbdrop(struct inpcb *inp) 74010702a28SRobert Watson { 74110702a28SRobert Watson 7427c5a8ab2SMarcel Moolenaar INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 74310702a28SRobert Watson INP_LOCK_ASSERT(inp); 74410702a28SRobert Watson 74510702a28SRobert Watson inp->inp_vflag |= INP_DROPPED; 74610702a28SRobert Watson if (inp->inp_lport) { 74710702a28SRobert Watson struct inpcbport *phd = inp->inp_phd; 74810702a28SRobert Watson 74910702a28SRobert Watson LIST_REMOVE(inp, inp_hash); 75010702a28SRobert Watson LIST_REMOVE(inp, inp_portlist); 75110702a28SRobert Watson if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 75210702a28SRobert Watson LIST_REMOVE(phd, phd_hash); 75310702a28SRobert Watson free(phd, M_PCB); 75410702a28SRobert Watson } 75510702a28SRobert Watson inp->inp_lport = 0; 75610702a28SRobert Watson } 75710702a28SRobert Watson } 75810702a28SRobert Watson 75954d642bbSRobert Watson /* 76054d642bbSRobert Watson * Common routines to return the socket addresses associated with inpcbs. 76154d642bbSRobert Watson */ 76226ef6ac4SDon Lewis struct sockaddr * 763136d4f1cSRobert Watson in_sockaddr(in_port_t port, struct in_addr *addr_p) 76426ef6ac4SDon Lewis { 76526ef6ac4SDon Lewis struct sockaddr_in *sin; 76626ef6ac4SDon Lewis 76726ef6ac4SDon Lewis MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 768a163d034SWarner Losh M_WAITOK | M_ZERO); 76926ef6ac4SDon Lewis sin->sin_family = AF_INET; 77026ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 77126ef6ac4SDon Lewis sin->sin_addr = *addr_p; 77226ef6ac4SDon Lewis sin->sin_port = port; 77326ef6ac4SDon Lewis 77426ef6ac4SDon Lewis return (struct sockaddr *)sin; 77526ef6ac4SDon Lewis } 77626ef6ac4SDon Lewis 777117bcae7SGarrett Wollman int 77854d642bbSRobert Watson in_getsockaddr(struct socket *so, struct sockaddr **nam) 779df8bae1dSRodney W. Grimes { 780136d4f1cSRobert Watson struct inpcb *inp; 78126ef6ac4SDon Lewis struct in_addr addr; 78226ef6ac4SDon Lewis in_port_t port; 78342fa505bSDavid Greenman 784fdc984f7STor Egge inp = sotoinpcb(so); 78554d642bbSRobert Watson KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 7866466b28aSRobert Watson 787f76fcf6dSJeffrey Hsu INP_LOCK(inp); 78826ef6ac4SDon Lewis port = inp->inp_lport; 78926ef6ac4SDon Lewis addr = inp->inp_laddr; 790f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 79142fa505bSDavid Greenman 79226ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 793117bcae7SGarrett Wollman return 0; 794df8bae1dSRodney W. Grimes } 795df8bae1dSRodney W. Grimes 796117bcae7SGarrett Wollman int 79754d642bbSRobert Watson in_getpeeraddr(struct socket *so, struct sockaddr **nam) 798df8bae1dSRodney W. Grimes { 799136d4f1cSRobert Watson struct inpcb *inp; 80026ef6ac4SDon Lewis struct in_addr addr; 80126ef6ac4SDon Lewis in_port_t port; 80242fa505bSDavid Greenman 803fdc984f7STor Egge inp = sotoinpcb(so); 80454d642bbSRobert Watson KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 8056466b28aSRobert Watson 806f76fcf6dSJeffrey Hsu INP_LOCK(inp); 80726ef6ac4SDon Lewis port = inp->inp_fport; 80826ef6ac4SDon Lewis addr = inp->inp_faddr; 809f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 81042fa505bSDavid Greenman 81126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 812117bcae7SGarrett Wollman return 0; 813df8bae1dSRodney W. Grimes } 814df8bae1dSRodney W. Grimes 81526f9a767SRodney W. Grimes void 816136d4f1cSRobert Watson in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 817136d4f1cSRobert Watson struct inpcb *(*notify)(struct inpcb *, int)) 818d1c54148SJesper Skriver { 819c693a045SJonathan Lemon struct inpcb *inp, *ninp; 820f76fcf6dSJeffrey Hsu struct inpcbhead *head; 821d1c54148SJesper Skriver 8223dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 823712fc218SRobert Watson head = pcbinfo->ipi_listhead; 824c693a045SJonathan Lemon for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 825f76fcf6dSJeffrey Hsu INP_LOCK(inp); 826c693a045SJonathan Lemon ninp = LIST_NEXT(inp, inp_list); 827d1c54148SJesper Skriver #ifdef INET6 828f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 829f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 830d1c54148SJesper Skriver continue; 831f76fcf6dSJeffrey Hsu } 832d1c54148SJesper Skriver #endif 833d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 834f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 835f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 836d1c54148SJesper Skriver continue; 837d1c54148SJesper Skriver } 8383dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 839f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 840f76fcf6dSJeffrey Hsu } 8413dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 842d1c54148SJesper Skriver } 843d1c54148SJesper Skriver 844e43cc4aeSHajimu UMEMOTO void 845136d4f1cSRobert Watson in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 846e43cc4aeSHajimu UMEMOTO { 847e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 848e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 849e43cc4aeSHajimu UMEMOTO int i, gap; 850e43cc4aeSHajimu UMEMOTO 851f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(pcbinfo); 852712fc218SRobert Watson LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 853f76fcf6dSJeffrey Hsu INP_LOCK(inp); 854e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 855e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 856e43cc4aeSHajimu UMEMOTO imo != NULL) { 857e43cc4aeSHajimu UMEMOTO /* 858e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 859e43cc4aeSHajimu UMEMOTO * detached. 860e43cc4aeSHajimu UMEMOTO */ 861e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 862e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 863e43cc4aeSHajimu UMEMOTO 864e43cc4aeSHajimu UMEMOTO /* 865e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 866e43cc4aeSHajimu UMEMOTO * through the interface being detached. 867e43cc4aeSHajimu UMEMOTO */ 868e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 869e43cc4aeSHajimu UMEMOTO i++) { 870e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 871e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 872e43cc4aeSHajimu UMEMOTO gap++; 873e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 874e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 875e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 876e43cc4aeSHajimu UMEMOTO } 877e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 878e43cc4aeSHajimu UMEMOTO } 879f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 880e43cc4aeSHajimu UMEMOTO } 8813cfcc388SJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 882e43cc4aeSHajimu UMEMOTO } 883e43cc4aeSHajimu UMEMOTO 884df8bae1dSRodney W. Grimes /* 885c3229e05SDavid Greenman * Lookup a PCB based on the local address and port. 886c3229e05SDavid Greenman */ 887d5e8a67eSHajimu UMEMOTO #define INP_LOOKUP_MAPPED_PCB_COST 3 888df8bae1dSRodney W. Grimes struct inpcb * 889136d4f1cSRobert Watson in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 890136d4f1cSRobert Watson u_int lport_arg, int wild_okay) 891df8bae1dSRodney W. Grimes { 892136d4f1cSRobert Watson struct inpcb *inp; 893d5e8a67eSHajimu UMEMOTO #ifdef INET6 894d5e8a67eSHajimu UMEMOTO int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 895d5e8a67eSHajimu UMEMOTO #else 896d5e8a67eSHajimu UMEMOTO int matchwild = 3; 897d5e8a67eSHajimu UMEMOTO #endif 898d5e8a67eSHajimu UMEMOTO int wildcard; 899c3229e05SDavid Greenman u_short lport = lport_arg; 9007bc4aca7SDavid Greenman 9011b73ca0bSSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 9021b73ca0bSSam Leffler 903c3229e05SDavid Greenman if (!wild_okay) { 904c3229e05SDavid Greenman struct inpcbhead *head; 905c3229e05SDavid Greenman /* 906c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 907c3229e05SDavid Greenman * matches the local address and port we're looking for. 908c3229e05SDavid Greenman */ 909712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 910712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 911fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 912cfa1ca9dSYoshinobu Inoue #ifdef INET6 913369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 914cfa1ca9dSYoshinobu Inoue continue; 915cfa1ca9dSYoshinobu Inoue #endif 916c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 917c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 918c3229e05SDavid Greenman inp->inp_lport == lport) { 919c3229e05SDavid Greenman /* 920c3229e05SDavid Greenman * Found. 921c3229e05SDavid Greenman */ 922c3229e05SDavid Greenman return (inp); 923df8bae1dSRodney W. Grimes } 924c3229e05SDavid Greenman } 925c3229e05SDavid Greenman /* 926c3229e05SDavid Greenman * Not found. 927c3229e05SDavid Greenman */ 928c3229e05SDavid Greenman return (NULL); 929c3229e05SDavid Greenman } else { 930c3229e05SDavid Greenman struct inpcbporthead *porthash; 931c3229e05SDavid Greenman struct inpcbport *phd; 932c3229e05SDavid Greenman struct inpcb *match = NULL; 933c3229e05SDavid Greenman /* 934c3229e05SDavid Greenman * Best fit PCB lookup. 935c3229e05SDavid Greenman * 936c3229e05SDavid Greenman * First see if this local port is in use by looking on the 937c3229e05SDavid Greenman * port hash list. 938c3229e05SDavid Greenman */ 939712fc218SRobert Watson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 940712fc218SRobert Watson pcbinfo->ipi_porthashmask)]; 941fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 942c3229e05SDavid Greenman if (phd->phd_port == lport) 943c3229e05SDavid Greenman break; 944c3229e05SDavid Greenman } 945c3229e05SDavid Greenman if (phd != NULL) { 946c3229e05SDavid Greenman /* 947c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 948c3229e05SDavid Greenman * fit. 949c3229e05SDavid Greenman */ 95037d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 951c3229e05SDavid Greenman wildcard = 0; 952cfa1ca9dSYoshinobu Inoue #ifdef INET6 953369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 954cfa1ca9dSYoshinobu Inoue continue; 955d5e8a67eSHajimu UMEMOTO /* 956d5e8a67eSHajimu UMEMOTO * We never select the PCB that has 957d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag and is bound to :: if 958d5e8a67eSHajimu UMEMOTO * we have another PCB which is bound 959d5e8a67eSHajimu UMEMOTO * to 0.0.0.0. If a PCB has the 960d5e8a67eSHajimu UMEMOTO * INP_IPV6 flag, then we set its cost 961d5e8a67eSHajimu UMEMOTO * higher than IPv4 only PCBs. 962d5e8a67eSHajimu UMEMOTO * 963d5e8a67eSHajimu UMEMOTO * Note that the case only happens 964d5e8a67eSHajimu UMEMOTO * when a socket is bound to ::, under 965d5e8a67eSHajimu UMEMOTO * the condition that the use of the 966d5e8a67eSHajimu UMEMOTO * mapped address is allowed. 967d5e8a67eSHajimu UMEMOTO */ 968d5e8a67eSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV6) != 0) 969d5e8a67eSHajimu UMEMOTO wildcard += INP_LOOKUP_MAPPED_PCB_COST; 970cfa1ca9dSYoshinobu Inoue #endif 971c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 972c3229e05SDavid Greenman wildcard++; 97315bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 97415bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 97515bd2b43SDavid Greenman wildcard++; 97615bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 97715bd2b43SDavid Greenman continue; 97815bd2b43SDavid Greenman } else { 97915bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 98015bd2b43SDavid Greenman wildcard++; 98115bd2b43SDavid Greenman } 982df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 983df8bae1dSRodney W. Grimes match = inp; 984df8bae1dSRodney W. Grimes matchwild = wildcard; 9853dbdc25cSDavid Greenman if (matchwild == 0) { 986df8bae1dSRodney W. Grimes break; 987df8bae1dSRodney W. Grimes } 988df8bae1dSRodney W. Grimes } 9893dbdc25cSDavid Greenman } 990c3229e05SDavid Greenman } 991df8bae1dSRodney W. Grimes return (match); 992df8bae1dSRodney W. Grimes } 993c3229e05SDavid Greenman } 994d5e8a67eSHajimu UMEMOTO #undef INP_LOOKUP_MAPPED_PCB_COST 99515bd2b43SDavid Greenman 99615bd2b43SDavid Greenman /* 99715bd2b43SDavid Greenman * Lookup PCB in hash list. 99815bd2b43SDavid Greenman */ 99915bd2b43SDavid Greenman struct inpcb * 1000136d4f1cSRobert Watson in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1001136d4f1cSRobert Watson u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard, 1002136d4f1cSRobert Watson struct ifnet *ifp) 100315bd2b43SDavid Greenman { 100415bd2b43SDavid Greenman struct inpcbhead *head; 1005136d4f1cSRobert Watson struct inpcb *inp; 100615bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 100715bd2b43SDavid Greenman 100859daba27SSam Leffler INP_INFO_RLOCK_ASSERT(pcbinfo); 1009602cc7f1SRobert Watson 101015bd2b43SDavid Greenman /* 101115bd2b43SDavid Greenman * First look for an exact match. 101215bd2b43SDavid Greenman */ 1013712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1014712fc218SRobert Watson pcbinfo->ipi_hashmask)]; 1015fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1016cfa1ca9dSYoshinobu Inoue #ifdef INET6 1017369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1018cfa1ca9dSYoshinobu Inoue continue; 1019cfa1ca9dSYoshinobu Inoue #endif 10206d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1021ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1022ca98b82cSDavid Greenman inp->inp_fport == fport && 1023e3fd5ffdSRobert Watson inp->inp_lport == lport) 1024c3229e05SDavid Greenman return (inp); 1025c3229e05SDavid Greenman } 1026e3fd5ffdSRobert Watson 1027e3fd5ffdSRobert Watson /* 1028e3fd5ffdSRobert Watson * Then look for a wildcard match, if requested. 1029e3fd5ffdSRobert Watson */ 10306d6a026bSDavid Greenman if (wildcard) { 10316d6a026bSDavid Greenman struct inpcb *local_wild = NULL; 1032e3fd5ffdSRobert Watson #ifdef INET6 1033cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1034e3fd5ffdSRobert Watson #endif 10356d6a026bSDavid Greenman 1036712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1037712fc218SRobert Watson 0, pcbinfo->ipi_hashmask)]; 1038fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1039cfa1ca9dSYoshinobu Inoue #ifdef INET6 1040369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1041cfa1ca9dSYoshinobu Inoue continue; 1042cfa1ca9dSYoshinobu Inoue #endif 10436d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 1044c3229e05SDavid Greenman inp->inp_lport == lport) { 1045cfa1ca9dSYoshinobu Inoue if (ifp && ifp->if_type == IFT_FAITH && 1046cfa1ca9dSYoshinobu Inoue (inp->inp_flags & INP_FAITH) == 0) 1047cfa1ca9dSYoshinobu Inoue continue; 10486d6a026bSDavid Greenman if (inp->inp_laddr.s_addr == laddr.s_addr) 1049c3229e05SDavid Greenman return (inp); 1050cfa1ca9dSYoshinobu Inoue else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1051e3fd5ffdSRobert Watson #ifdef INET6 1052cfa1ca9dSYoshinobu Inoue if (INP_CHECK_SOCKAF(inp->inp_socket, 1053cfa1ca9dSYoshinobu Inoue AF_INET6)) 1054cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1055cfa1ca9dSYoshinobu Inoue else 1056e3fd5ffdSRobert Watson #endif 10576d6a026bSDavid Greenman local_wild = inp; 10586d6a026bSDavid Greenman } 10596d6a026bSDavid Greenman } 1060cfa1ca9dSYoshinobu Inoue } 1061e3fd5ffdSRobert Watson #ifdef INET6 1062cfa1ca9dSYoshinobu Inoue if (local_wild == NULL) 1063cfa1ca9dSYoshinobu Inoue return (local_wild_mapped); 1064e3fd5ffdSRobert Watson #endif 1065c3229e05SDavid Greenman return (local_wild); 10666d6a026bSDavid Greenman } 10676d6a026bSDavid Greenman return (NULL); 106815bd2b43SDavid Greenman } 106915bd2b43SDavid Greenman 10707bc4aca7SDavid Greenman /* 1071c3229e05SDavid Greenman * Insert PCB onto various hash lists. 10727bc4aca7SDavid Greenman */ 1073c3229e05SDavid Greenman int 1074136d4f1cSRobert Watson in_pcbinshash(struct inpcb *inp) 107515bd2b43SDavid Greenman { 1076c3229e05SDavid Greenman struct inpcbhead *pcbhash; 1077c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 1078c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1079c3229e05SDavid Greenman struct inpcbport *phd; 1080cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 108115bd2b43SDavid Greenman 108259daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 1083602cc7f1SRobert Watson INP_LOCK_ASSERT(inp); 1084602cc7f1SRobert Watson 1085cfa1ca9dSYoshinobu Inoue #ifdef INET6 1086cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1087cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1088cfa1ca9dSYoshinobu Inoue else 1089cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1090cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1091cfa1ca9dSYoshinobu Inoue 1092712fc218SRobert Watson pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1093712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 109415bd2b43SDavid Greenman 1095712fc218SRobert Watson pcbporthash = &pcbinfo->ipi_porthashbase[ 1096712fc218SRobert Watson INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 1097c3229e05SDavid Greenman 1098c3229e05SDavid Greenman /* 1099c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 1100c3229e05SDavid Greenman */ 1101fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 1102c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 1103c3229e05SDavid Greenman break; 1104c3229e05SDavid Greenman } 1105c3229e05SDavid Greenman /* 1106c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 1107c3229e05SDavid Greenman */ 1108c3229e05SDavid Greenman if (phd == NULL) { 1109c3229e05SDavid Greenman MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1110c3229e05SDavid Greenman if (phd == NULL) { 1111c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 1112c3229e05SDavid Greenman } 1113c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 1114c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 1115c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1116c3229e05SDavid Greenman } 1117c3229e05SDavid Greenman inp->inp_phd = phd; 1118c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1119c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1120c3229e05SDavid Greenman return (0); 112115bd2b43SDavid Greenman } 112215bd2b43SDavid Greenman 1123c3229e05SDavid Greenman /* 1124c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 1125c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 1126c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 1127c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 1128c3229e05SDavid Greenman */ 112915bd2b43SDavid Greenman void 1130136d4f1cSRobert Watson in_pcbrehash(struct inpcb *inp) 113115bd2b43SDavid Greenman { 113259daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 113315bd2b43SDavid Greenman struct inpcbhead *head; 1134cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 113515bd2b43SDavid Greenman 113659daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 11374c2bb15aSRobert Watson INP_LOCK_ASSERT(inp); 1138602cc7f1SRobert Watson 1139cfa1ca9dSYoshinobu Inoue #ifdef INET6 1140cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1141cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1142cfa1ca9dSYoshinobu Inoue else 1143cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1144cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1145cfa1ca9dSYoshinobu Inoue 1146712fc218SRobert Watson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1147712fc218SRobert Watson inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 114815bd2b43SDavid Greenman 1149c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 115015bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 1151c3229e05SDavid Greenman } 1152c3229e05SDavid Greenman 1153c3229e05SDavid Greenman /* 1154c3229e05SDavid Greenman * Remove PCB from various lists. 1155c3229e05SDavid Greenman */ 115676429de4SYoshinobu Inoue void 1157136d4f1cSRobert Watson in_pcbremlists(struct inpcb *inp) 1158c3229e05SDavid Greenman { 115959daba27SSam Leffler struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 116059daba27SSam Leffler 116159daba27SSam Leffler INP_INFO_WLOCK_ASSERT(pcbinfo); 116259daba27SSam Leffler INP_LOCK_ASSERT(inp); 116359daba27SSam Leffler 116459daba27SSam Leffler inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1165c3229e05SDavid Greenman if (inp->inp_lport) { 1166c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 1167c3229e05SDavid Greenman 1168c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 1169c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 1170fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1171c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 1172c3229e05SDavid Greenman free(phd, M_PCB); 1173c3229e05SDavid Greenman } 1174c3229e05SDavid Greenman } 1175c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 117659daba27SSam Leffler pcbinfo->ipi_count--; 117715bd2b43SDavid Greenman } 117875c13541SPoul-Henning Kamp 1179a557af22SRobert Watson /* 1180a557af22SRobert Watson * A set label operation has occurred at the socket layer, propagate the 1181a557af22SRobert Watson * label change into the in_pcb for the socket. 1182a557af22SRobert Watson */ 1183a557af22SRobert Watson void 1184136d4f1cSRobert Watson in_pcbsosetlabel(struct socket *so) 1185a557af22SRobert Watson { 1186a557af22SRobert Watson #ifdef MAC 1187a557af22SRobert Watson struct inpcb *inp; 1188a557af22SRobert Watson 11894c7c478dSRobert Watson inp = sotoinpcb(so); 11904c7c478dSRobert Watson KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 1191602cc7f1SRobert Watson 1192a557af22SRobert Watson INP_LOCK(inp); 1193310e7cebSRobert Watson SOCK_LOCK(so); 1194a557af22SRobert Watson mac_inpcb_sosetlabel(so, inp); 1195310e7cebSRobert Watson SOCK_UNLOCK(so); 1196a557af22SRobert Watson INP_UNLOCK(inp); 1197a557af22SRobert Watson #endif 1198a557af22SRobert Watson } 11995f311da2SMike Silbersack 12005f311da2SMike Silbersack /* 1201ad3a630fSRobert Watson * ipport_tick runs once per second, determining if random port allocation 1202ad3a630fSRobert Watson * should be continued. If more than ipport_randomcps ports have been 1203ad3a630fSRobert Watson * allocated in the last second, then we return to sequential port 1204ad3a630fSRobert Watson * allocation. We return to random allocation only once we drop below 1205ad3a630fSRobert Watson * ipport_randomcps for at least ipport_randomtime seconds. 12065f311da2SMike Silbersack */ 12075f311da2SMike Silbersack void 1208136d4f1cSRobert Watson ipport_tick(void *xtp) 12095f311da2SMike Silbersack { 1210ad3a630fSRobert Watson 1211ad3a630fSRobert Watson if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) { 12125f311da2SMike Silbersack if (ipport_stoprandom > 0) 12135f311da2SMike Silbersack ipport_stoprandom--; 1214ad3a630fSRobert Watson } else 1215ad3a630fSRobert Watson ipport_stoprandom = ipport_randomtime; 12165f311da2SMike Silbersack ipport_tcplastcount = ipport_tcpallocs; 12175f311da2SMike Silbersack callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 12185f311da2SMike Silbersack } 1219497057eeSRobert Watson 1220497057eeSRobert Watson #ifdef DDB 1221497057eeSRobert Watson static void 1222497057eeSRobert Watson db_print_indent(int indent) 1223497057eeSRobert Watson { 1224497057eeSRobert Watson int i; 1225497057eeSRobert Watson 1226497057eeSRobert Watson for (i = 0; i < indent; i++) 1227497057eeSRobert Watson db_printf(" "); 1228497057eeSRobert Watson } 1229497057eeSRobert Watson 1230497057eeSRobert Watson static void 1231497057eeSRobert Watson db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 1232497057eeSRobert Watson { 1233497057eeSRobert Watson char faddr_str[48], laddr_str[48]; 1234497057eeSRobert Watson 1235497057eeSRobert Watson db_print_indent(indent); 1236497057eeSRobert Watson db_printf("%s at %p\n", name, inc); 1237497057eeSRobert Watson 1238497057eeSRobert Watson indent += 2; 1239497057eeSRobert Watson 124003dc38a4SRobert Watson #ifdef INET6 1241497057eeSRobert Watson if (inc->inc_flags == 1) { 1242497057eeSRobert Watson /* IPv6. */ 1243497057eeSRobert Watson ip6_sprintf(laddr_str, &inc->inc6_laddr); 1244497057eeSRobert Watson ip6_sprintf(faddr_str, &inc->inc6_faddr); 1245497057eeSRobert Watson } else { 124603dc38a4SRobert Watson #endif 1247497057eeSRobert Watson /* IPv4. */ 1248497057eeSRobert Watson inet_ntoa_r(inc->inc_laddr, laddr_str); 1249497057eeSRobert Watson inet_ntoa_r(inc->inc_faddr, faddr_str); 125003dc38a4SRobert Watson #ifdef INET6 1251497057eeSRobert Watson } 125203dc38a4SRobert Watson #endif 1253497057eeSRobert Watson db_print_indent(indent); 1254497057eeSRobert Watson db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 1255497057eeSRobert Watson ntohs(inc->inc_lport)); 1256497057eeSRobert Watson db_print_indent(indent); 1257497057eeSRobert Watson db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 1258497057eeSRobert Watson ntohs(inc->inc_fport)); 1259497057eeSRobert Watson } 1260497057eeSRobert Watson 1261497057eeSRobert Watson static void 1262497057eeSRobert Watson db_print_inpflags(int inp_flags) 1263497057eeSRobert Watson { 1264497057eeSRobert Watson int comma; 1265497057eeSRobert Watson 1266497057eeSRobert Watson comma = 0; 1267497057eeSRobert Watson if (inp_flags & INP_RECVOPTS) { 1268497057eeSRobert Watson db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 1269497057eeSRobert Watson comma = 1; 1270497057eeSRobert Watson } 1271497057eeSRobert Watson if (inp_flags & INP_RECVRETOPTS) { 1272497057eeSRobert Watson db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 1273497057eeSRobert Watson comma = 1; 1274497057eeSRobert Watson } 1275497057eeSRobert Watson if (inp_flags & INP_RECVDSTADDR) { 1276497057eeSRobert Watson db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 1277497057eeSRobert Watson comma = 1; 1278497057eeSRobert Watson } 1279497057eeSRobert Watson if (inp_flags & INP_HDRINCL) { 1280497057eeSRobert Watson db_printf("%sINP_HDRINCL", comma ? ", " : ""); 1281497057eeSRobert Watson comma = 1; 1282497057eeSRobert Watson } 1283497057eeSRobert Watson if (inp_flags & INP_HIGHPORT) { 1284497057eeSRobert Watson db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 1285497057eeSRobert Watson comma = 1; 1286497057eeSRobert Watson } 1287497057eeSRobert Watson if (inp_flags & INP_LOWPORT) { 1288497057eeSRobert Watson db_printf("%sINP_LOWPORT", comma ? ", " : ""); 1289497057eeSRobert Watson comma = 1; 1290497057eeSRobert Watson } 1291497057eeSRobert Watson if (inp_flags & INP_ANONPORT) { 1292497057eeSRobert Watson db_printf("%sINP_ANONPORT", comma ? ", " : ""); 1293497057eeSRobert Watson comma = 1; 1294497057eeSRobert Watson } 1295497057eeSRobert Watson if (inp_flags & INP_RECVIF) { 1296497057eeSRobert Watson db_printf("%sINP_RECVIF", comma ? ", " : ""); 1297497057eeSRobert Watson comma = 1; 1298497057eeSRobert Watson } 1299497057eeSRobert Watson if (inp_flags & INP_MTUDISC) { 1300497057eeSRobert Watson db_printf("%sINP_MTUDISC", comma ? ", " : ""); 1301497057eeSRobert Watson comma = 1; 1302497057eeSRobert Watson } 1303497057eeSRobert Watson if (inp_flags & INP_FAITH) { 1304497057eeSRobert Watson db_printf("%sINP_FAITH", comma ? ", " : ""); 1305497057eeSRobert Watson comma = 1; 1306497057eeSRobert Watson } 1307497057eeSRobert Watson if (inp_flags & INP_RECVTTL) { 1308497057eeSRobert Watson db_printf("%sINP_RECVTTL", comma ? ", " : ""); 1309497057eeSRobert Watson comma = 1; 1310497057eeSRobert Watson } 1311497057eeSRobert Watson if (inp_flags & INP_DONTFRAG) { 1312497057eeSRobert Watson db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 1313497057eeSRobert Watson comma = 1; 1314497057eeSRobert Watson } 1315497057eeSRobert Watson if (inp_flags & IN6P_IPV6_V6ONLY) { 1316497057eeSRobert Watson db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 1317497057eeSRobert Watson comma = 1; 1318497057eeSRobert Watson } 1319497057eeSRobert Watson if (inp_flags & IN6P_PKTINFO) { 1320497057eeSRobert Watson db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 1321497057eeSRobert Watson comma = 1; 1322497057eeSRobert Watson } 1323497057eeSRobert Watson if (inp_flags & IN6P_HOPLIMIT) { 1324497057eeSRobert Watson db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 1325497057eeSRobert Watson comma = 1; 1326497057eeSRobert Watson } 1327497057eeSRobert Watson if (inp_flags & IN6P_HOPOPTS) { 1328497057eeSRobert Watson db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 1329497057eeSRobert Watson comma = 1; 1330497057eeSRobert Watson } 1331497057eeSRobert Watson if (inp_flags & IN6P_DSTOPTS) { 1332497057eeSRobert Watson db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 1333497057eeSRobert Watson comma = 1; 1334497057eeSRobert Watson } 1335497057eeSRobert Watson if (inp_flags & IN6P_RTHDR) { 1336497057eeSRobert Watson db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 1337497057eeSRobert Watson comma = 1; 1338497057eeSRobert Watson } 1339497057eeSRobert Watson if (inp_flags & IN6P_RTHDRDSTOPTS) { 1340497057eeSRobert Watson db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 1341497057eeSRobert Watson comma = 1; 1342497057eeSRobert Watson } 1343497057eeSRobert Watson if (inp_flags & IN6P_TCLASS) { 1344497057eeSRobert Watson db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 1345497057eeSRobert Watson comma = 1; 1346497057eeSRobert Watson } 1347497057eeSRobert Watson if (inp_flags & IN6P_AUTOFLOWLABEL) { 1348497057eeSRobert Watson db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 1349497057eeSRobert Watson comma = 1; 1350497057eeSRobert Watson } 1351497057eeSRobert Watson if (inp_flags & IN6P_RFC2292) { 1352497057eeSRobert Watson db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 1353497057eeSRobert Watson comma = 1; 1354497057eeSRobert Watson } 1355497057eeSRobert Watson if (inp_flags & IN6P_MTU) { 1356497057eeSRobert Watson db_printf("IN6P_MTU%s", comma ? ", " : ""); 1357497057eeSRobert Watson comma = 1; 1358497057eeSRobert Watson } 1359497057eeSRobert Watson } 1360497057eeSRobert Watson 1361497057eeSRobert Watson static void 1362497057eeSRobert Watson db_print_inpvflag(u_char inp_vflag) 1363497057eeSRobert Watson { 1364497057eeSRobert Watson int comma; 1365497057eeSRobert Watson 1366497057eeSRobert Watson comma = 0; 1367497057eeSRobert Watson if (inp_vflag & INP_IPV4) { 1368497057eeSRobert Watson db_printf("%sINP_IPV4", comma ? ", " : ""); 1369497057eeSRobert Watson comma = 1; 1370497057eeSRobert Watson } 1371497057eeSRobert Watson if (inp_vflag & INP_IPV6) { 1372497057eeSRobert Watson db_printf("%sINP_IPV6", comma ? ", " : ""); 1373497057eeSRobert Watson comma = 1; 1374497057eeSRobert Watson } 1375497057eeSRobert Watson if (inp_vflag & INP_IPV6PROTO) { 1376497057eeSRobert Watson db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 1377497057eeSRobert Watson comma = 1; 1378497057eeSRobert Watson } 1379497057eeSRobert Watson if (inp_vflag & INP_TIMEWAIT) { 1380497057eeSRobert Watson db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 1381497057eeSRobert Watson comma = 1; 1382497057eeSRobert Watson } 1383497057eeSRobert Watson if (inp_vflag & INP_ONESBCAST) { 1384497057eeSRobert Watson db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 1385497057eeSRobert Watson comma = 1; 1386497057eeSRobert Watson } 1387497057eeSRobert Watson if (inp_vflag & INP_DROPPED) { 1388497057eeSRobert Watson db_printf("%sINP_DROPPED", comma ? ", " : ""); 1389497057eeSRobert Watson comma = 1; 1390497057eeSRobert Watson } 1391497057eeSRobert Watson if (inp_vflag & INP_SOCKREF) { 1392497057eeSRobert Watson db_printf("%sINP_SOCKREF", comma ? ", " : ""); 1393497057eeSRobert Watson comma = 1; 1394497057eeSRobert Watson } 1395497057eeSRobert Watson } 1396497057eeSRobert Watson 1397497057eeSRobert Watson void 1398497057eeSRobert Watson db_print_inpcb(struct inpcb *inp, const char *name, int indent) 1399497057eeSRobert Watson { 1400497057eeSRobert Watson 1401497057eeSRobert Watson db_print_indent(indent); 1402497057eeSRobert Watson db_printf("%s at %p\n", name, inp); 1403497057eeSRobert Watson 1404497057eeSRobert Watson indent += 2; 1405497057eeSRobert Watson 1406497057eeSRobert Watson db_print_indent(indent); 1407497057eeSRobert Watson db_printf("inp_flow: 0x%x\n", inp->inp_flow); 1408497057eeSRobert Watson 1409497057eeSRobert Watson db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 1410497057eeSRobert Watson 1411497057eeSRobert Watson db_print_indent(indent); 1412497057eeSRobert Watson db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 1413497057eeSRobert Watson inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 1414497057eeSRobert Watson 1415497057eeSRobert Watson db_print_indent(indent); 1416497057eeSRobert Watson db_printf("inp_label: %p inp_flags: 0x%x (", 1417497057eeSRobert Watson inp->inp_label, inp->inp_flags); 1418497057eeSRobert Watson db_print_inpflags(inp->inp_flags); 1419497057eeSRobert Watson db_printf(")\n"); 1420497057eeSRobert Watson 1421497057eeSRobert Watson db_print_indent(indent); 1422497057eeSRobert Watson db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 1423497057eeSRobert Watson inp->inp_vflag); 1424497057eeSRobert Watson db_print_inpvflag(inp->inp_vflag); 1425497057eeSRobert Watson db_printf(")\n"); 1426497057eeSRobert Watson 1427497057eeSRobert Watson db_print_indent(indent); 1428497057eeSRobert Watson db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 1429497057eeSRobert Watson inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 1430497057eeSRobert Watson 1431497057eeSRobert Watson db_print_indent(indent); 1432497057eeSRobert Watson #ifdef INET6 1433497057eeSRobert Watson if (inp->inp_vflag & INP_IPV6) { 1434497057eeSRobert Watson db_printf("in6p_options: %p in6p_outputopts: %p " 1435497057eeSRobert Watson "in6p_moptions: %p\n", inp->in6p_options, 1436497057eeSRobert Watson inp->in6p_outputopts, inp->in6p_moptions); 1437497057eeSRobert Watson db_printf("in6p_icmp6filt: %p in6p_cksum %d " 1438497057eeSRobert Watson "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 1439497057eeSRobert Watson inp->in6p_hops); 1440497057eeSRobert Watson } else 1441497057eeSRobert Watson #endif 1442497057eeSRobert Watson { 1443497057eeSRobert Watson db_printf("inp_ip_tos: %d inp_ip_options: %p " 1444497057eeSRobert Watson "inp_ip_moptions: %p\n", inp->inp_ip_tos, 1445497057eeSRobert Watson inp->inp_options, inp->inp_moptions); 1446497057eeSRobert Watson } 1447497057eeSRobert Watson 1448497057eeSRobert Watson db_print_indent(indent); 1449497057eeSRobert Watson db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 1450497057eeSRobert Watson (uintmax_t)inp->inp_gencnt); 1451497057eeSRobert Watson } 1452497057eeSRobert Watson 1453497057eeSRobert Watson DB_SHOW_COMMAND(inpcb, db_show_inpcb) 1454497057eeSRobert Watson { 1455497057eeSRobert Watson struct inpcb *inp; 1456497057eeSRobert Watson 1457497057eeSRobert Watson if (!have_addr) { 1458497057eeSRobert Watson db_printf("usage: show inpcb <addr>\n"); 1459497057eeSRobert Watson return; 1460497057eeSRobert Watson } 1461497057eeSRobert Watson inp = (struct inpcb *)addr; 1462497057eeSRobert Watson 1463497057eeSRobert Watson db_print_inpcb(inp, "inpcb", 0); 1464497057eeSRobert Watson } 1465497057eeSRobert Watson #endif 1466