1df8bae1dSRodney W. Grimes /* 22469dd60SGarrett Wollman * Copyright (c) 1982, 1986, 1991, 1993, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 332469dd60SGarrett Wollman * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 376a800098SYoshinobu Inoue #include "opt_ipsec.h" 38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.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> 48df8bae1dSRodney W. Grimes #include <sys/proc.h> 4975c13541SPoul-Henning Kamp #include <sys/jail.h> 50101f9fc8SPeter Wemm #include <sys/kernel.h> 51101f9fc8SPeter Wemm #include <sys/sysctl.h> 528781d8e9SBruce Evans 5308637435SBruce Evans #include <machine/limits.h> 5408637435SBruce Evans 5569c2d429SJeff Roberson #include <vm/uma.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <net/if.h> 58cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 59df8bae1dSRodney W. Grimes #include <net/route.h> 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes #include <netinet/in.h> 62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 63df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 64df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 65340c35deSJonathan Lemon #include <netinet/tcp_var.h> 66cfa1ca9dSYoshinobu Inoue #ifdef INET6 67cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 68cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 69cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 70cfa1ca9dSYoshinobu Inoue 71cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 72cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h> 73cfa1ca9dSYoshinobu Inoue #include <netkey/key.h> 74cfa1ca9dSYoshinobu Inoue #endif /* IPSEC */ 75df8bae1dSRodney W. Grimes 76b9234fafSSam Leffler #ifdef FAST_IPSEC 77b9234fafSSam Leffler #if defined(IPSEC) || defined(IPSEC_ESP) 78b9234fafSSam Leffler #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!" 79b9234fafSSam Leffler #endif 80b9234fafSSam Leffler 81b9234fafSSam Leffler #include <netipsec/ipsec.h> 82b9234fafSSam Leffler #include <netipsec/key.h> 83b9234fafSSam Leffler #define IPSEC 84b9234fafSSam Leffler #endif /* FAST_IPSEC */ 85b9234fafSSam Leffler 86df8bae1dSRodney W. Grimes struct in_addr zeroin_addr; 87df8bae1dSRodney W. Grimes 88101f9fc8SPeter Wemm /* 89101f9fc8SPeter Wemm * These configure the range of local port addresses assigned to 90101f9fc8SPeter Wemm * "unspecified" outgoing connections/packets/whatever. 91101f9fc8SPeter Wemm */ 9282cd038dSYoshinobu Inoue int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 9382cd038dSYoshinobu Inoue int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 949e5a5ed4SMike Silbersack int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 959e5a5ed4SMike Silbersack int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */ 9682cd038dSYoshinobu Inoue int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 9782cd038dSYoshinobu Inoue int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 98101f9fc8SPeter Wemm 99b0d22693SCrist J. Clark /* 100b0d22693SCrist J. Clark * Reserved ports accessible only to root. There are significant 101b0d22693SCrist J. Clark * security considerations that must be accounted for when changing these, 102b0d22693SCrist J. Clark * but the security benefits can be great. Please be careful. 103b0d22693SCrist J. Clark */ 104b0d22693SCrist J. Clark int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */ 105b0d22693SCrist J. Clark int ipport_reservedlow = 0; 106b0d22693SCrist J. Clark 107bbd42ad0SPeter Wemm #define RANGECHK(var, min, max) \ 108bbd42ad0SPeter Wemm if ((var) < (min)) { (var) = (min); } \ 109bbd42ad0SPeter Wemm else if ((var) > (max)) { (var) = (max); } 110bbd42ad0SPeter Wemm 111bbd42ad0SPeter Wemm static int 11282d9ae4eSPoul-Henning Kamp sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 113bbd42ad0SPeter Wemm { 114bbd42ad0SPeter Wemm int error = sysctl_handle_int(oidp, 115bbd42ad0SPeter Wemm oidp->oid_arg1, oidp->oid_arg2, req); 116bbd42ad0SPeter Wemm if (!error) { 117bbd42ad0SPeter Wemm RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 118bbd42ad0SPeter Wemm RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 119bbd42ad0SPeter Wemm RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX); 120bbd42ad0SPeter Wemm RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX); 121bbd42ad0SPeter Wemm RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX); 122bbd42ad0SPeter Wemm RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX); 123bbd42ad0SPeter Wemm } 124bbd42ad0SPeter Wemm return error; 125bbd42ad0SPeter Wemm } 126bbd42ad0SPeter Wemm 127bbd42ad0SPeter Wemm #undef RANGECHK 128bbd42ad0SPeter Wemm 12933b3ac06SPeter Wemm SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 13033b3ac06SPeter Wemm 131bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW, 132bbd42ad0SPeter Wemm &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", ""); 133bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW, 134bbd42ad0SPeter Wemm &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", ""); 135bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW, 136bbd42ad0SPeter Wemm &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", ""); 137bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW, 138bbd42ad0SPeter Wemm &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", ""); 139bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW, 140bbd42ad0SPeter Wemm &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", ""); 141bbd42ad0SPeter Wemm SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW, 142bbd42ad0SPeter Wemm &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", ""); 143b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 144b0d22693SCrist J. Clark CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, ""); 145b0d22693SCrist J. Clark SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 146b0d22693SCrist J. Clark CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, ""); 1470312fbe9SPoul-Henning Kamp 148c3229e05SDavid Greenman /* 149c3229e05SDavid Greenman * in_pcb.c: manage the Protocol Control Blocks. 150c3229e05SDavid Greenman * 151c3229e05SDavid Greenman * NOTE: It is assumed that most of these functions will be called at 152c3229e05SDavid Greenman * splnet(). XXX - There are, unfortunately, a few exceptions to this 153c3229e05SDavid Greenman * rule that should be fixed. 154c3229e05SDavid Greenman */ 155c3229e05SDavid Greenman 156c3229e05SDavid Greenman /* 157c3229e05SDavid Greenman * Allocate a PCB and associate it with the socket. 158c3229e05SDavid Greenman */ 159df8bae1dSRodney W. Grimes int 160b40ce416SJulian Elischer in_pcballoc(so, pcbinfo, td) 161df8bae1dSRodney W. Grimes struct socket *so; 16215bd2b43SDavid Greenman struct inpcbinfo *pcbinfo; 163b40ce416SJulian Elischer struct thread *td; 164df8bae1dSRodney W. Grimes { 165df8bae1dSRodney W. Grimes register struct inpcb *inp; 16613cf67f3SHajimu UMEMOTO #ifdef IPSEC 16713cf67f3SHajimu UMEMOTO int error; 16813cf67f3SHajimu UMEMOTO #endif 169df8bae1dSRodney W. Grimes 170f76fcf6dSJeffrey Hsu inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 171df8bae1dSRodney W. Grimes if (inp == NULL) 172df8bae1dSRodney W. Grimes return (ENOBUFS); 173df8bae1dSRodney W. Grimes bzero((caddr_t)inp, sizeof(*inp)); 1743d4d47f3SGarrett Wollman inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 17515bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 176df8bae1dSRodney W. Grimes inp->inp_socket = so; 17713cf67f3SHajimu UMEMOTO #ifdef IPSEC 17813cf67f3SHajimu UMEMOTO error = ipsec_init_policy(so, &inp->inp_sp); 17913cf67f3SHajimu UMEMOTO if (error != 0) { 18069c2d429SJeff Roberson uma_zfree(pcbinfo->ipi_zone, inp); 18113cf67f3SHajimu UMEMOTO return error; 18213cf67f3SHajimu UMEMOTO } 18313cf67f3SHajimu UMEMOTO #endif /*IPSEC*/ 18475daea93SPaul Saab #if defined(INET6) 185340c35deSJonathan Lemon if (INP_SOCKAF(so) == AF_INET6) { 186340c35deSJonathan Lemon inp->inp_vflag |= INP_IPV6PROTO; 187340c35deSJonathan Lemon if (ip6_v6only) 18833841545SHajimu UMEMOTO inp->inp_flags |= IN6P_IPV6_V6ONLY; 189340c35deSJonathan Lemon } 19075daea93SPaul Saab #endif 19115bd2b43SDavid Greenman LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list); 1923d4d47f3SGarrett Wollman pcbinfo->ipi_count++; 193df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 194f76fcf6dSJeffrey Hsu INP_LOCK_INIT(inp, "inp"); 19533841545SHajimu UMEMOTO #ifdef INET6 19633841545SHajimu UMEMOTO if (ip6_auto_flowlabel) 19733841545SHajimu UMEMOTO inp->inp_flags |= IN6P_AUTOFLOWLABEL; 19833841545SHajimu UMEMOTO #endif 199df8bae1dSRodney W. Grimes return (0); 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes 202df8bae1dSRodney W. Grimes int 203b40ce416SJulian Elischer in_pcbbind(inp, nam, td) 204df8bae1dSRodney W. Grimes register struct inpcb *inp; 20557bf258eSGarrett Wollman struct sockaddr *nam; 206b40ce416SJulian Elischer struct thread *td; 207df8bae1dSRodney W. Grimes { 2084b932371SIan Dowse int anonport, error; 2094b932371SIan Dowse 2104b932371SIan Dowse if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 2114b932371SIan Dowse return (EINVAL); 2124b932371SIan Dowse anonport = inp->inp_lport == 0 && (nam == NULL || 2134b932371SIan Dowse ((struct sockaddr_in *)nam)->sin_port == 0); 2144b932371SIan Dowse error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 2154b932371SIan Dowse &inp->inp_lport, td); 2164b932371SIan Dowse if (error) 2174b932371SIan Dowse return (error); 2184b932371SIan Dowse if (in_pcbinshash(inp) != 0) { 2194b932371SIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 2204b932371SIan Dowse inp->inp_lport = 0; 2214b932371SIan Dowse return (EAGAIN); 2224b932371SIan Dowse } 2234b932371SIan Dowse if (anonport) 2244b932371SIan Dowse inp->inp_flags |= INP_ANONPORT; 2254b932371SIan Dowse return (0); 2264b932371SIan Dowse } 2274b932371SIan Dowse 2284b932371SIan Dowse /* 2294b932371SIan Dowse * Set up a bind operation on a PCB, performing port allocation 2304b932371SIan Dowse * as required, but do not actually modify the PCB. Callers can 2314b932371SIan Dowse * either complete the bind by setting inp_laddr/inp_lport and 2324b932371SIan Dowse * calling in_pcbinshash(), or they can just use the resulting 2334b932371SIan Dowse * port and address to authorise the sending of a once-off packet. 2344b932371SIan Dowse * 2354b932371SIan Dowse * On error, the values of *laddrp and *lportp are not changed. 2364b932371SIan Dowse */ 2374b932371SIan Dowse int 2384b932371SIan Dowse in_pcbbind_setup(inp, nam, laddrp, lportp, td) 2394b932371SIan Dowse struct inpcb *inp; 2404b932371SIan Dowse struct sockaddr *nam; 2414b932371SIan Dowse in_addr_t *laddrp; 2424b932371SIan Dowse u_short *lportp; 2434b932371SIan Dowse struct thread *td; 2444b932371SIan Dowse { 2454b932371SIan Dowse struct socket *so = inp->inp_socket; 24637bd2b30SPeter Wemm unsigned short *lastport; 24715bd2b43SDavid Greenman struct sockaddr_in *sin; 248c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2494b932371SIan Dowse struct in_addr laddr; 250df8bae1dSRodney W. Grimes u_short lport = 0; 2514cc20ab1SSeigo Tanimura int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 25275c13541SPoul-Henning Kamp int error, prison = 0; 253df8bae1dSRodney W. Grimes 25459562606SGarrett Wollman if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */ 255df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 2564b932371SIan Dowse laddr.s_addr = *laddrp; 2574b932371SIan Dowse if (nam != NULL && laddr.s_addr != INADDR_ANY) 258df8bae1dSRodney W. Grimes return (EINVAL); 259c3229e05SDavid Greenman if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 2606d6a026bSDavid Greenman wild = 1; 261df8bae1dSRodney W. Grimes if (nam) { 26257bf258eSGarrett Wollman sin = (struct sockaddr_in *)nam; 26357bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 264df8bae1dSRodney W. Grimes return (EINVAL); 265df8bae1dSRodney W. Grimes #ifdef notdef 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * We should check the family, but old programs 268df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 269df8bae1dSRodney W. Grimes */ 270df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 271df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 272df8bae1dSRodney W. Grimes #endif 273e4bdf25dSPoul-Henning Kamp if (sin->sin_addr.s_addr != INADDR_ANY) 274a854ed98SJohn Baldwin if (prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr)) 27575c13541SPoul-Henning Kamp return(EINVAL); 2764b932371SIan Dowse if (sin->sin_port != *lportp) { 2774b932371SIan Dowse /* Don't allow the port to change. */ 2784b932371SIan Dowse if (*lportp != 0) 2794b932371SIan Dowse return (EINVAL); 280df8bae1dSRodney W. Grimes lport = sin->sin_port; 2814b932371SIan Dowse } 2824b932371SIan Dowse /* NB: lport is left as 0 if the port isn't being changed. */ 283df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 284df8bae1dSRodney W. Grimes /* 285df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 286df8bae1dSRodney W. Grimes * allow complete duplication of binding if 287df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 288df8bae1dSRodney W. Grimes * and a multicast address is bound on both 289df8bae1dSRodney W. Grimes * new and duplicated sockets. 290df8bae1dSRodney W. Grimes */ 291df8bae1dSRodney W. Grimes if (so->so_options & SO_REUSEADDR) 292df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 293df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 294df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 29583103a73SAndrew R. Reiter bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 296df8bae1dSRodney W. Grimes if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 297df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 298df8bae1dSRodney W. Grimes } 2994b932371SIan Dowse laddr = sin->sin_addr; 300df8bae1dSRodney W. Grimes if (lport) { 301df8bae1dSRodney W. Grimes struct inpcb *t; 302df8bae1dSRodney W. Grimes /* GROSS */ 303b0d22693SCrist J. Clark if (ntohs(lport) <= ipport_reservedhigh && 304b0d22693SCrist J. Clark ntohs(lport) >= ipport_reservedlow && 305b0d22693SCrist J. Clark td && suser_cred(td->td_ucred, PRISON_ROOT)) 3062469dd60SGarrett Wollman return (EACCES); 307a854ed98SJohn Baldwin if (td && jailed(td->td_ucred)) 30875c13541SPoul-Henning Kamp prison = 1; 3092f9a2132SBrian Feldman if (so->so_cred->cr_uid != 0 && 31052b65dbeSBill Fenner !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 3114049a042SGuido van Rooij t = in_pcblookup_local(inp->inp_pcbinfo, 31275c13541SPoul-Henning Kamp sin->sin_addr, lport, 31375c13541SPoul-Henning Kamp prison ? 0 : INPLOOKUP_WILDCARD); 314340c35deSJonathan Lemon /* 315340c35deSJonathan Lemon * XXX 316340c35deSJonathan Lemon * This entire block sorely needs a rewrite. 317340c35deSJonathan Lemon */ 318340c35deSJonathan Lemon if (t && (t->inp_vflag & INP_TIMEWAIT)) { 319340c35deSJonathan Lemon if ((ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 320340c35deSJonathan Lemon ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 321340c35deSJonathan Lemon (intotw(t)->tw_so_options & SO_REUSEPORT) == 0) && 322340c35deSJonathan Lemon (so->so_cred->cr_uid != intotw(t)->tw_cred->cr_uid)) 323340c35deSJonathan Lemon return (EADDRINUSE); 324340c35deSJonathan Lemon } else 3254cc20ab1SSeigo Tanimura if (t && 3264cc20ab1SSeigo Tanimura (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 32752b65dbeSBill Fenner ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 32852b65dbeSBill Fenner (t->inp_socket->so_options & 32952b65dbeSBill Fenner SO_REUSEPORT) == 0) && 3302f9a2132SBrian Feldman (so->so_cred->cr_uid != 331cfa1ca9dSYoshinobu Inoue t->inp_socket->so_cred->cr_uid)) { 332cfa1ca9dSYoshinobu Inoue #if defined(INET6) 33333841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 334cfa1ca9dSYoshinobu Inoue INADDR_ANY || 335cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 336cfa1ca9dSYoshinobu Inoue INADDR_ANY || 337cfa1ca9dSYoshinobu Inoue INP_SOCKAF(so) == 338cfa1ca9dSYoshinobu Inoue INP_SOCKAF(t->inp_socket)) 339cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */ 3404049a042SGuido van Rooij return (EADDRINUSE); 3414049a042SGuido van Rooij } 342cfa1ca9dSYoshinobu Inoue } 343970680faSPoul-Henning Kamp if (prison && 344a854ed98SJohn Baldwin prison_ip(td->td_ucred, 0, &sin->sin_addr.s_addr)) 345970680faSPoul-Henning Kamp return (EADDRNOTAVAIL); 346c3229e05SDavid Greenman t = in_pcblookup_local(pcbinfo, sin->sin_addr, 34775c13541SPoul-Henning Kamp lport, prison ? 0 : wild); 348340c35deSJonathan Lemon if (t && (t->inp_vflag & INP_TIMEWAIT)) { 349340c35deSJonathan Lemon if ((reuseport & intotw(t)->tw_so_options) == 0) 350340c35deSJonathan Lemon return (EADDRINUSE); 351340c35deSJonathan Lemon } else 3524cc20ab1SSeigo Tanimura if (t && 3534cc20ab1SSeigo Tanimura (reuseport & t->inp_socket->so_options) == 0) { 354cfa1ca9dSYoshinobu Inoue #if defined(INET6) 35533841545SHajimu UMEMOTO if (ntohl(sin->sin_addr.s_addr) != 356cfa1ca9dSYoshinobu Inoue INADDR_ANY || 357cfa1ca9dSYoshinobu Inoue ntohl(t->inp_laddr.s_addr) != 358cfa1ca9dSYoshinobu Inoue INADDR_ANY || 359cfa1ca9dSYoshinobu Inoue INP_SOCKAF(so) == 360cfa1ca9dSYoshinobu Inoue INP_SOCKAF(t->inp_socket)) 361cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */ 362df8bae1dSRodney W. Grimes return (EADDRINUSE); 363df8bae1dSRodney W. Grimes } 364cfa1ca9dSYoshinobu Inoue } 365df8bae1dSRodney W. Grimes } 3664b932371SIan Dowse if (*lportp != 0) 3674b932371SIan Dowse lport = *lportp; 36833b3ac06SPeter Wemm if (lport == 0) { 36933b3ac06SPeter Wemm ushort first, last; 37033b3ac06SPeter Wemm int count; 37133b3ac06SPeter Wemm 3724b932371SIan Dowse if (laddr.s_addr != INADDR_ANY) 3734b932371SIan Dowse if (prison_ip(td->td_ucred, 0, &laddr.s_addr)) 37475c13541SPoul-Henning Kamp return (EINVAL); 375321a2846SPoul-Henning Kamp 37633b3ac06SPeter Wemm if (inp->inp_flags & INP_HIGHPORT) { 37733b3ac06SPeter Wemm first = ipport_hifirstauto; /* sysctl */ 37833b3ac06SPeter Wemm last = ipport_hilastauto; 379c3229e05SDavid Greenman lastport = &pcbinfo->lasthi; 38033b3ac06SPeter Wemm } else if (inp->inp_flags & INP_LOWPORT) { 3814b932371SIan Dowse if (td && (error = suser_cred(td->td_ucred, 3824b932371SIan Dowse PRISON_ROOT)) != 0) 383a29f300eSGarrett Wollman return error; 384bbd42ad0SPeter Wemm first = ipport_lowfirstauto; /* 1023 */ 385bbd42ad0SPeter Wemm last = ipport_lowlastauto; /* 600 */ 386c3229e05SDavid Greenman lastport = &pcbinfo->lastlow; 38733b3ac06SPeter Wemm } else { 38833b3ac06SPeter Wemm first = ipport_firstauto; /* sysctl */ 38933b3ac06SPeter Wemm last = ipport_lastauto; 390c3229e05SDavid Greenman lastport = &pcbinfo->lastport; 39133b3ac06SPeter Wemm } 39233b3ac06SPeter Wemm /* 39333b3ac06SPeter Wemm * Simple check to ensure all ports are not used up causing 39433b3ac06SPeter Wemm * a deadlock here. 39533b3ac06SPeter Wemm * 39633b3ac06SPeter Wemm * We split the two cases (up and down) so that the direction 39733b3ac06SPeter Wemm * is not being tested on each round of the loop. 39833b3ac06SPeter Wemm */ 39933b3ac06SPeter Wemm if (first > last) { 40033b3ac06SPeter Wemm /* 40133b3ac06SPeter Wemm * counting down 40233b3ac06SPeter Wemm */ 40333b3ac06SPeter Wemm count = first - last; 40433b3ac06SPeter Wemm 405df8bae1dSRodney W. Grimes do { 4064b932371SIan Dowse if (count-- < 0) /* completely used? */ 407550b1518SWes Peters return (EADDRNOTAVAIL); 40833b3ac06SPeter Wemm --*lastport; 40933b3ac06SPeter Wemm if (*lastport > first || *lastport < last) 41033b3ac06SPeter Wemm *lastport = first; 41115bd2b43SDavid Greenman lport = htons(*lastport); 4124b932371SIan Dowse } while (in_pcblookup_local(pcbinfo, laddr, lport, 4134b932371SIan Dowse wild)); 41433b3ac06SPeter Wemm } else { 41533b3ac06SPeter Wemm /* 41633b3ac06SPeter Wemm * counting up 41733b3ac06SPeter Wemm */ 41833b3ac06SPeter Wemm count = last - first; 41933b3ac06SPeter Wemm 42033b3ac06SPeter Wemm do { 4214b932371SIan Dowse if (count-- < 0) /* completely used? */ 422550b1518SWes Peters return (EADDRNOTAVAIL); 42333b3ac06SPeter Wemm ++*lastport; 42433b3ac06SPeter Wemm if (*lastport < first || *lastport > last) 42533b3ac06SPeter Wemm *lastport = first; 42633b3ac06SPeter Wemm lport = htons(*lastport); 4274b932371SIan Dowse } while (in_pcblookup_local(pcbinfo, laddr, lport, 4284b932371SIan Dowse wild)); 42933b3ac06SPeter Wemm } 43033b3ac06SPeter Wemm } 4314b932371SIan Dowse if (prison_ip(td->td_ucred, 0, &laddr.s_addr)) 432e4bdf25dSPoul-Henning Kamp return (EINVAL); 4334b932371SIan Dowse *laddrp = laddr.s_addr; 4344b932371SIan Dowse *lportp = lport; 435df8bae1dSRodney W. Grimes return (0); 436df8bae1dSRodney W. Grimes } 437df8bae1dSRodney W. Grimes 438999f1343SGarrett Wollman /* 4395200e00eSIan Dowse * Connect from a socket to a specified address. 4405200e00eSIan Dowse * Both address and port must be specified in argument sin. 4415200e00eSIan Dowse * If don't have a local address for this socket yet, 4425200e00eSIan Dowse * then pick one. 443999f1343SGarrett Wollman */ 444999f1343SGarrett Wollman int 4455200e00eSIan Dowse in_pcbconnect(inp, nam, td) 446999f1343SGarrett Wollman register struct inpcb *inp; 44757bf258eSGarrett Wollman struct sockaddr *nam; 4485200e00eSIan Dowse struct thread *td; 449999f1343SGarrett Wollman { 4505200e00eSIan Dowse u_short lport, fport; 4515200e00eSIan Dowse in_addr_t laddr, faddr; 4525200e00eSIan Dowse int anonport, error; 453df8bae1dSRodney W. Grimes 4545200e00eSIan Dowse lport = inp->inp_lport; 4555200e00eSIan Dowse laddr = inp->inp_laddr.s_addr; 4565200e00eSIan Dowse anonport = (lport == 0); 4575200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 4585200e00eSIan Dowse NULL, td); 4595200e00eSIan Dowse if (error) 4605200e00eSIan Dowse return (error); 4615200e00eSIan Dowse 4625200e00eSIan Dowse /* Do the initial binding of the local address if required. */ 4635200e00eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 4645200e00eSIan Dowse inp->inp_lport = lport; 4655200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 4665200e00eSIan Dowse if (in_pcbinshash(inp) != 0) { 4675200e00eSIan Dowse inp->inp_laddr.s_addr = INADDR_ANY; 4685200e00eSIan Dowse inp->inp_lport = 0; 4695200e00eSIan Dowse return (EAGAIN); 4705200e00eSIan Dowse } 4715200e00eSIan Dowse } 4725200e00eSIan Dowse 4735200e00eSIan Dowse /* Commit the remaining changes. */ 4745200e00eSIan Dowse inp->inp_lport = lport; 4755200e00eSIan Dowse inp->inp_laddr.s_addr = laddr; 4765200e00eSIan Dowse inp->inp_faddr.s_addr = faddr; 4775200e00eSIan Dowse inp->inp_fport = fport; 4785200e00eSIan Dowse in_pcbrehash(inp); 4795200e00eSIan Dowse if (anonport) 4805200e00eSIan Dowse inp->inp_flags |= INP_ANONPORT; 4815200e00eSIan Dowse return (0); 4825200e00eSIan Dowse } 4835200e00eSIan Dowse 4845200e00eSIan Dowse /* 4855200e00eSIan Dowse * Set up for a connect from a socket to the specified address. 4865200e00eSIan Dowse * On entry, *laddrp and *lportp should contain the current local 4875200e00eSIan Dowse * address and port for the PCB; these are updated to the values 4885200e00eSIan Dowse * that should be placed in inp_laddr and inp_lport to complete 4895200e00eSIan Dowse * the connect. 4905200e00eSIan Dowse * 4915200e00eSIan Dowse * On success, *faddrp and *fportp will be set to the remote address 4925200e00eSIan Dowse * and port. These are not updated in the error case. 4935200e00eSIan Dowse * 4945200e00eSIan Dowse * If the operation fails because the connection already exists, 4955200e00eSIan Dowse * *oinpp will be set to the PCB of that connection so that the 4965200e00eSIan Dowse * caller can decide to override it. In all other cases, *oinpp 4975200e00eSIan Dowse * is set to NULL. 4985200e00eSIan Dowse */ 4995200e00eSIan Dowse int 5005200e00eSIan Dowse in_pcbconnect_setup(inp, nam, laddrp, lportp, faddrp, fportp, oinpp, td) 5015200e00eSIan Dowse register struct inpcb *inp; 5025200e00eSIan Dowse struct sockaddr *nam; 5035200e00eSIan Dowse in_addr_t *laddrp; 5045200e00eSIan Dowse u_short *lportp; 5055200e00eSIan Dowse in_addr_t *faddrp; 5065200e00eSIan Dowse u_short *fportp; 5075200e00eSIan Dowse struct inpcb **oinpp; 5085200e00eSIan Dowse struct thread *td; 5095200e00eSIan Dowse { 5105200e00eSIan Dowse struct sockaddr_in *sin = (struct sockaddr_in *)nam; 5115200e00eSIan Dowse struct in_ifaddr *ia; 5125200e00eSIan Dowse struct sockaddr_in sa; 5135200e00eSIan Dowse struct ucred *cred; 5145200e00eSIan Dowse struct inpcb *oinp; 5155200e00eSIan Dowse struct in_addr laddr, faddr; 5165200e00eSIan Dowse u_short lport, fport; 5175200e00eSIan Dowse int error; 5185200e00eSIan Dowse 5195200e00eSIan Dowse if (oinpp != NULL) 5205200e00eSIan Dowse *oinpp = NULL; 52157bf258eSGarrett Wollman if (nam->sa_len != sizeof (*sin)) 522df8bae1dSRodney W. Grimes return (EINVAL); 523df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 524df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 525df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 526df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 5275200e00eSIan Dowse laddr.s_addr = *laddrp; 5285200e00eSIan Dowse lport = *lportp; 5295200e00eSIan Dowse faddr = sin->sin_addr; 5305200e00eSIan Dowse fport = sin->sin_port; 5315200e00eSIan Dowse cred = inp->inp_socket->so_cred; 5325200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY && jailed(cred)) { 5335200e00eSIan Dowse bzero(&sa, sizeof(sa)); 5345200e00eSIan Dowse sa.sin_addr.s_addr = htonl(prison_getip(cred)); 5355200e00eSIan Dowse sa.sin_len = sizeof(sa); 5365200e00eSIan Dowse sa.sin_family = AF_INET; 5375200e00eSIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&sa, 5385200e00eSIan Dowse &laddr.s_addr, &lport, td); 5395200e00eSIan Dowse if (error) 5405200e00eSIan Dowse return (error); 5415200e00eSIan Dowse } 5425200e00eSIan Dowse 54359562606SGarrett Wollman if (!TAILQ_EMPTY(&in_ifaddrhead)) { 544df8bae1dSRodney W. Grimes /* 545df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 546df8bae1dSRodney W. Grimes * use the primary local address. 547df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 548df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 549df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 550df8bae1dSRodney W. Grimes */ 5515200e00eSIan Dowse if (faddr.s_addr == INADDR_ANY) 5525200e00eSIan Dowse faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr; 5535200e00eSIan Dowse else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 5545200e00eSIan Dowse (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & 5555200e00eSIan Dowse IFF_BROADCAST)) 5565200e00eSIan Dowse faddr = satosin(&TAILQ_FIRST( 5575200e00eSIan Dowse &in_ifaddrhead)->ia_broadaddr)->sin_addr; 558df8bae1dSRodney W. Grimes } 5595200e00eSIan Dowse if (laddr.s_addr == INADDR_ANY) { 560df8bae1dSRodney W. Grimes register struct route *ro; 561df8bae1dSRodney W. Grimes 562df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)0; 563df8bae1dSRodney W. Grimes /* 564df8bae1dSRodney W. Grimes * If route is known or can be allocated now, 565df8bae1dSRodney W. Grimes * our src addr is taken from the i/f, else punt. 566a4a6e773SHajimu UMEMOTO * Note that we should check the address family of the cached 567a4a6e773SHajimu UMEMOTO * destination, in case of sharing the cache with IPv6. 568df8bae1dSRodney W. Grimes */ 569df8bae1dSRodney W. Grimes ro = &inp->inp_route; 570df8bae1dSRodney W. Grimes if (ro->ro_rt && 571a4a6e773SHajimu UMEMOTO (ro->ro_dst.sa_family != AF_INET || 5725200e00eSIan Dowse satosin(&ro->ro_dst)->sin_addr.s_addr != faddr.s_addr || 573df8bae1dSRodney W. Grimes inp->inp_socket->so_options & SO_DONTROUTE)) { 574df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 575df8bae1dSRodney W. Grimes ro->ro_rt = (struct rtentry *)0; 576df8bae1dSRodney W. Grimes } 577df8bae1dSRodney W. Grimes if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/ 578df8bae1dSRodney W. Grimes (ro->ro_rt == (struct rtentry *)0 || 579df8bae1dSRodney W. Grimes ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 580df8bae1dSRodney W. Grimes /* No route yet, so try to acquire one */ 581a4a6e773SHajimu UMEMOTO bzero(&ro->ro_dst, sizeof(struct sockaddr_in)); 582df8bae1dSRodney W. Grimes ro->ro_dst.sa_family = AF_INET; 583df8bae1dSRodney W. Grimes ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 5845200e00eSIan Dowse ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = faddr; 585df8bae1dSRodney W. Grimes rtalloc(ro); 5864cc20ab1SSeigo Tanimura } 587df8bae1dSRodney W. Grimes /* 588df8bae1dSRodney W. Grimes * If we found a route, use the address 589df8bae1dSRodney W. Grimes * corresponding to the outgoing interface 590df8bae1dSRodney W. Grimes * unless it is the loopback (in case a route 591df8bae1dSRodney W. Grimes * to our address on another net goes to loopback). 592df8bae1dSRodney W. Grimes */ 593df8bae1dSRodney W. Grimes if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 594df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 595df8bae1dSRodney W. Grimes if (ia == 0) { 5965200e00eSIan Dowse bzero(&sa, sizeof(sa)); 5975200e00eSIan Dowse sa.sin_addr = faddr; 5985200e00eSIan Dowse sa.sin_len = sizeof(sa); 5995200e00eSIan Dowse sa.sin_family = AF_INET; 600df8bae1dSRodney W. Grimes 6015200e00eSIan Dowse ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa))); 602df8bae1dSRodney W. Grimes if (ia == 0) 6035200e00eSIan Dowse ia = ifatoia(ifa_ifwithnet(sintosa(&sa))); 604df8bae1dSRodney W. Grimes if (ia == 0) 605fc2ffbe6SPoul-Henning Kamp ia = TAILQ_FIRST(&in_ifaddrhead); 606df8bae1dSRodney W. Grimes if (ia == 0) 607df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes /* 610df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 611df8bae1dSRodney W. Grimes * interface has been set as a multicast option, use the 612df8bae1dSRodney W. Grimes * address of that interface as our source address. 613df8bae1dSRodney W. Grimes */ 6145200e00eSIan Dowse if (IN_MULTICAST(ntohl(faddr.s_addr)) && 615df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 616df8bae1dSRodney W. Grimes struct ip_moptions *imo; 617df8bae1dSRodney W. Grimes struct ifnet *ifp; 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 620df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 621df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 62237d40066SPoul-Henning Kamp TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 623df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp) 624df8bae1dSRodney W. Grimes break; 625df8bae1dSRodney W. Grimes if (ia == 0) 626df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 627df8bae1dSRodney W. Grimes } 628df8bae1dSRodney W. Grimes } 6295200e00eSIan Dowse laddr = ia->ia_addr.sin_addr; 630999f1343SGarrett Wollman } 631999f1343SGarrett Wollman 6325200e00eSIan Dowse oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 6335200e00eSIan Dowse 0, NULL); 6345200e00eSIan Dowse if (oinp != NULL) { 6355200e00eSIan Dowse if (oinpp != NULL) 6365200e00eSIan Dowse *oinpp = oinp; 637df8bae1dSRodney W. Grimes return (EADDRINUSE); 638c3229e05SDavid Greenman } 6395200e00eSIan Dowse if (lport == 0) { 6405200e00eSIan Dowse error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, td); 6415a903f8dSPierre Beyssac if (error) 6425a903f8dSPierre Beyssac return (error); 6435a903f8dSPierre Beyssac } 6445200e00eSIan Dowse *laddrp = laddr.s_addr; 6455200e00eSIan Dowse *lportp = lport; 6465200e00eSIan Dowse *faddrp = faddr.s_addr; 6475200e00eSIan Dowse *fportp = fport; 648df8bae1dSRodney W. Grimes return (0); 649df8bae1dSRodney W. Grimes } 650df8bae1dSRodney W. Grimes 65126f9a767SRodney W. Grimes void 652df8bae1dSRodney W. Grimes in_pcbdisconnect(inp) 653df8bae1dSRodney W. Grimes struct inpcb *inp; 654df8bae1dSRodney W. Grimes { 655df8bae1dSRodney W. Grimes 656df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 657df8bae1dSRodney W. Grimes inp->inp_fport = 0; 65815bd2b43SDavid Greenman in_pcbrehash(inp); 6594cc20ab1SSeigo Tanimura if (inp->inp_socket->so_state & SS_NOFDREF) 660df8bae1dSRodney W. Grimes in_pcbdetach(inp); 661df8bae1dSRodney W. Grimes } 662df8bae1dSRodney W. Grimes 66326f9a767SRodney W. Grimes void 664df8bae1dSRodney W. Grimes in_pcbdetach(inp) 665df8bae1dSRodney W. Grimes struct inpcb *inp; 666df8bae1dSRodney W. Grimes { 667df8bae1dSRodney W. Grimes struct socket *so = inp->inp_socket; 6683d4d47f3SGarrett Wollman struct inpcbinfo *ipi = inp->inp_pcbinfo; 669df8bae1dSRodney W. Grimes 670cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 671cfa1ca9dSYoshinobu Inoue ipsec4_delete_pcbpolicy(inp); 672cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 6733d4d47f3SGarrett Wollman inp->inp_gencnt = ++ipi->ipi_gencnt; 674c3229e05SDavid Greenman in_pcbremlists(inp); 675340c35deSJonathan Lemon if (so) { 676df8bae1dSRodney W. Grimes so->so_pcb = 0; 677b1e4abd2SMatthew Dillon sotryfree(so); 678340c35deSJonathan Lemon } 679df8bae1dSRodney W. Grimes if (inp->inp_options) 680df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 681be2ac88cSJonathan Lemon if (inp->inp_route.ro_rt) 682be2ac88cSJonathan Lemon rtfree(inp->inp_route.ro_rt); 683df8bae1dSRodney W. Grimes ip_freemoptions(inp->inp_moptions); 684cfa1ca9dSYoshinobu Inoue inp->inp_vflag = 0; 685f76fcf6dSJeffrey Hsu INP_LOCK_DESTROY(inp); 68669c2d429SJeff Roberson uma_zfree(ipi->ipi_zone, inp); 687df8bae1dSRodney W. Grimes } 688df8bae1dSRodney W. Grimes 68926ef6ac4SDon Lewis struct sockaddr * 69026ef6ac4SDon Lewis in_sockaddr(port, addr_p) 69126ef6ac4SDon Lewis in_port_t port; 69226ef6ac4SDon Lewis struct in_addr *addr_p; 69326ef6ac4SDon Lewis { 69426ef6ac4SDon Lewis struct sockaddr_in *sin; 69526ef6ac4SDon Lewis 69626ef6ac4SDon Lewis MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 697a163d034SWarner Losh M_WAITOK | M_ZERO); 69826ef6ac4SDon Lewis sin->sin_family = AF_INET; 69926ef6ac4SDon Lewis sin->sin_len = sizeof(*sin); 70026ef6ac4SDon Lewis sin->sin_addr = *addr_p; 70126ef6ac4SDon Lewis sin->sin_port = port; 70226ef6ac4SDon Lewis 70326ef6ac4SDon Lewis return (struct sockaddr *)sin; 70426ef6ac4SDon Lewis } 70526ef6ac4SDon Lewis 706117bcae7SGarrett Wollman /* 707f76fcf6dSJeffrey Hsu * The wrapper function will pass down the pcbinfo for this function to lock. 708f76fcf6dSJeffrey Hsu * The socket must have a valid 709117bcae7SGarrett Wollman * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 710117bcae7SGarrett Wollman * except through a kernel programming error, so it is acceptable to panic 71157bf258eSGarrett Wollman * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 71257bf258eSGarrett Wollman * because there actually /is/ a programming error somewhere... XXX) 713117bcae7SGarrett Wollman */ 714117bcae7SGarrett Wollman int 715f76fcf6dSJeffrey Hsu in_setsockaddr(so, nam, pcbinfo) 716117bcae7SGarrett Wollman struct socket *so; 71757bf258eSGarrett Wollman struct sockaddr **nam; 718f76fcf6dSJeffrey Hsu struct inpcbinfo *pcbinfo; 719df8bae1dSRodney W. Grimes { 720fdc984f7STor Egge int s; 721fdc984f7STor Egge register struct inpcb *inp; 72226ef6ac4SDon Lewis struct in_addr addr; 72326ef6ac4SDon Lewis in_port_t port; 72442fa505bSDavid Greenman 725fdc984f7STor Egge s = splnet(); 726f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(pcbinfo); 727fdc984f7STor Egge inp = sotoinpcb(so); 728db112f04STor Egge if (!inp) { 729f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 730db112f04STor Egge splx(s); 731ff079ca4SPeter Wemm return ECONNRESET; 732db112f04STor Egge } 733f76fcf6dSJeffrey Hsu INP_LOCK(inp); 73426ef6ac4SDon Lewis port = inp->inp_lport; 73526ef6ac4SDon Lewis addr = inp->inp_laddr; 736f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 737f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 738db112f04STor Egge splx(s); 73942fa505bSDavid Greenman 74026ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 741117bcae7SGarrett Wollman return 0; 742df8bae1dSRodney W. Grimes } 743df8bae1dSRodney W. Grimes 744f76fcf6dSJeffrey Hsu /* 745f76fcf6dSJeffrey Hsu * The wrapper function will pass down the pcbinfo for this function to lock. 746f76fcf6dSJeffrey Hsu */ 747117bcae7SGarrett Wollman int 748f76fcf6dSJeffrey Hsu in_setpeeraddr(so, nam, pcbinfo) 749117bcae7SGarrett Wollman struct socket *so; 75057bf258eSGarrett Wollman struct sockaddr **nam; 751f76fcf6dSJeffrey Hsu struct inpcbinfo *pcbinfo; 752df8bae1dSRodney W. Grimes { 753fdc984f7STor Egge int s; 754f76fcf6dSJeffrey Hsu register struct inpcb *inp; 75526ef6ac4SDon Lewis struct in_addr addr; 75626ef6ac4SDon Lewis in_port_t port; 75742fa505bSDavid Greenman 758fdc984f7STor Egge s = splnet(); 759f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(pcbinfo); 760fdc984f7STor Egge inp = sotoinpcb(so); 761db112f04STor Egge if (!inp) { 762f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 763db112f04STor Egge splx(s); 764ff079ca4SPeter Wemm return ECONNRESET; 765db112f04STor Egge } 766f76fcf6dSJeffrey Hsu INP_LOCK(inp); 76726ef6ac4SDon Lewis port = inp->inp_fport; 76826ef6ac4SDon Lewis addr = inp->inp_faddr; 769f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 770f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 771db112f04STor Egge splx(s); 77242fa505bSDavid Greenman 77326ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 774117bcae7SGarrett Wollman return 0; 775df8bae1dSRodney W. Grimes } 776df8bae1dSRodney W. Grimes 77726f9a767SRodney W. Grimes void 778f76fcf6dSJeffrey Hsu in_pcbnotifyall(pcbinfo, faddr, errno, notify) 779f76fcf6dSJeffrey Hsu struct inpcbinfo *pcbinfo; 780df8bae1dSRodney W. Grimes struct in_addr faddr; 781c693a045SJonathan Lemon int errno; 7823ce144eaSJeffrey Hsu struct inpcb *(*notify)(struct inpcb *, int); 783d1c54148SJesper Skriver { 784c693a045SJonathan Lemon struct inpcb *inp, *ninp; 785f76fcf6dSJeffrey Hsu struct inpcbhead *head; 786c693a045SJonathan Lemon int s; 787d1c54148SJesper Skriver 788d1c54148SJesper Skriver s = splnet(); 7893dc7ebf9SJeffrey Hsu INP_INFO_WLOCK(pcbinfo); 790f76fcf6dSJeffrey Hsu head = pcbinfo->listhead; 791c693a045SJonathan Lemon for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 792f76fcf6dSJeffrey Hsu INP_LOCK(inp); 793c693a045SJonathan Lemon ninp = LIST_NEXT(inp, inp_list); 794d1c54148SJesper Skriver #ifdef INET6 795f76fcf6dSJeffrey Hsu if ((inp->inp_vflag & INP_IPV4) == 0) { 796f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 797d1c54148SJesper Skriver continue; 798f76fcf6dSJeffrey Hsu } 799d1c54148SJesper Skriver #endif 800d1c54148SJesper Skriver if (inp->inp_faddr.s_addr != faddr.s_addr || 801f76fcf6dSJeffrey Hsu inp->inp_socket == NULL) { 802f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 803d1c54148SJesper Skriver continue; 804d1c54148SJesper Skriver } 8053dc7ebf9SJeffrey Hsu if ((*notify)(inp, errno)) 806f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 807f76fcf6dSJeffrey Hsu } 8083dc7ebf9SJeffrey Hsu INP_INFO_WUNLOCK(pcbinfo); 809d1c54148SJesper Skriver splx(s); 810d1c54148SJesper Skriver } 811d1c54148SJesper Skriver 812e43cc4aeSHajimu UMEMOTO void 813f76fcf6dSJeffrey Hsu in_pcbpurgeif0(pcbinfo, ifp) 814f76fcf6dSJeffrey Hsu struct inpcbinfo *pcbinfo; 815e43cc4aeSHajimu UMEMOTO struct ifnet *ifp; 816e43cc4aeSHajimu UMEMOTO { 817e43cc4aeSHajimu UMEMOTO struct inpcb *inp; 818e43cc4aeSHajimu UMEMOTO struct ip_moptions *imo; 819e43cc4aeSHajimu UMEMOTO int i, gap; 820e43cc4aeSHajimu UMEMOTO 821f76fcf6dSJeffrey Hsu /* why no splnet here? XXX */ 822f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(pcbinfo); 8233cfcc388SJeffrey Hsu LIST_FOREACH(inp, pcbinfo->listhead, inp_list) { 824f76fcf6dSJeffrey Hsu INP_LOCK(inp); 825e43cc4aeSHajimu UMEMOTO imo = inp->inp_moptions; 826e43cc4aeSHajimu UMEMOTO if ((inp->inp_vflag & INP_IPV4) && 827e43cc4aeSHajimu UMEMOTO imo != NULL) { 828e43cc4aeSHajimu UMEMOTO /* 829e43cc4aeSHajimu UMEMOTO * Unselect the outgoing interface if it is being 830e43cc4aeSHajimu UMEMOTO * detached. 831e43cc4aeSHajimu UMEMOTO */ 832e43cc4aeSHajimu UMEMOTO if (imo->imo_multicast_ifp == ifp) 833e43cc4aeSHajimu UMEMOTO imo->imo_multicast_ifp = NULL; 834e43cc4aeSHajimu UMEMOTO 835e43cc4aeSHajimu UMEMOTO /* 836e43cc4aeSHajimu UMEMOTO * Drop multicast group membership if we joined 837e43cc4aeSHajimu UMEMOTO * through the interface being detached. 838e43cc4aeSHajimu UMEMOTO */ 839e43cc4aeSHajimu UMEMOTO for (i = 0, gap = 0; i < imo->imo_num_memberships; 840e43cc4aeSHajimu UMEMOTO i++) { 841e43cc4aeSHajimu UMEMOTO if (imo->imo_membership[i]->inm_ifp == ifp) { 842e43cc4aeSHajimu UMEMOTO in_delmulti(imo->imo_membership[i]); 843e43cc4aeSHajimu UMEMOTO gap++; 844e43cc4aeSHajimu UMEMOTO } else if (gap != 0) 845e43cc4aeSHajimu UMEMOTO imo->imo_membership[i - gap] = 846e43cc4aeSHajimu UMEMOTO imo->imo_membership[i]; 847e43cc4aeSHajimu UMEMOTO } 848e43cc4aeSHajimu UMEMOTO imo->imo_num_memberships -= gap; 849e43cc4aeSHajimu UMEMOTO } 850f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 851e43cc4aeSHajimu UMEMOTO } 8523cfcc388SJeffrey Hsu INP_INFO_RUNLOCK(pcbinfo); 853e43cc4aeSHajimu UMEMOTO } 854e43cc4aeSHajimu UMEMOTO 855df8bae1dSRodney W. Grimes /* 856df8bae1dSRodney W. Grimes * Check for alternatives when higher level complains 857df8bae1dSRodney W. Grimes * about service problems. For now, invalidate cached 858df8bae1dSRodney W. Grimes * routing information. If the route was created dynamically 859df8bae1dSRodney W. Grimes * (by a redirect), time to try a default gateway again. 860df8bae1dSRodney W. Grimes */ 86126f9a767SRodney W. Grimes void 862df8bae1dSRodney W. Grimes in_losing(inp) 863df8bae1dSRodney W. Grimes struct inpcb *inp; 864df8bae1dSRodney W. Grimes { 865df8bae1dSRodney W. Grimes register struct rtentry *rt; 866df8bae1dSRodney W. Grimes struct rt_addrinfo info; 867df8bae1dSRodney W. Grimes 868df8bae1dSRodney W. Grimes if ((rt = inp->inp_route.ro_rt)) { 869df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 8708071913dSRuslan Ermilov info.rti_flags = rt->rt_flags; 8718071913dSRuslan Ermilov info.rti_info[RTAX_DST] = rt_key(rt); 872df8bae1dSRodney W. Grimes info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 873df8bae1dSRodney W. Grimes info.rti_info[RTAX_NETMASK] = rt_mask(rt); 874df8bae1dSRodney W. Grimes rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 875df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_DYNAMIC) 8768071913dSRuslan Ermilov (void) rtrequest1(RTM_DELETE, &info, NULL); 8778071913dSRuslan Ermilov inp->inp_route.ro_rt = NULL; 8788bf82a92SRuslan Ermilov rtfree(rt); 879df8bae1dSRodney W. Grimes /* 880df8bae1dSRodney W. Grimes * A new route can be allocated 881df8bae1dSRodney W. Grimes * the next time output is attempted. 882df8bae1dSRodney W. Grimes */ 883df8bae1dSRodney W. Grimes } 884df8bae1dSRodney W. Grimes } 885df8bae1dSRodney W. Grimes 886df8bae1dSRodney W. Grimes /* 887df8bae1dSRodney W. Grimes * After a routing change, flush old routing 888df8bae1dSRodney W. Grimes * and allocate a (hopefully) better one. 889df8bae1dSRodney W. Grimes */ 8903ce144eaSJeffrey Hsu struct inpcb * 891df8bae1dSRodney W. Grimes in_rtchange(inp, errno) 892df8bae1dSRodney W. Grimes register struct inpcb *inp; 893df8bae1dSRodney W. Grimes int errno; 894df8bae1dSRodney W. Grimes { 895df8bae1dSRodney W. Grimes if (inp->inp_route.ro_rt) { 896df8bae1dSRodney W. Grimes rtfree(inp->inp_route.ro_rt); 897df8bae1dSRodney W. Grimes inp->inp_route.ro_rt = 0; 898df8bae1dSRodney W. Grimes /* 899df8bae1dSRodney W. Grimes * A new route can be allocated the next time 900df8bae1dSRodney W. Grimes * output is attempted. 901df8bae1dSRodney W. Grimes */ 902df8bae1dSRodney W. Grimes } 9033ce144eaSJeffrey Hsu return inp; 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes 906c3229e05SDavid Greenman /* 907c3229e05SDavid Greenman * Lookup a PCB based on the local address and port. 908c3229e05SDavid Greenman */ 909df8bae1dSRodney W. Grimes struct inpcb * 910c3229e05SDavid Greenman in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 9116d6a026bSDavid Greenman struct inpcbinfo *pcbinfo; 912c3229e05SDavid Greenman struct in_addr laddr; 913c3229e05SDavid Greenman u_int lport_arg; 9146d6a026bSDavid Greenman int wild_okay; 915df8bae1dSRodney W. Grimes { 916f1d19042SArchie Cobbs register struct inpcb *inp; 917df8bae1dSRodney W. Grimes int matchwild = 3, wildcard; 918c3229e05SDavid Greenman u_short lport = lport_arg; 9197bc4aca7SDavid Greenman 920c3229e05SDavid Greenman if (!wild_okay) { 921c3229e05SDavid Greenman struct inpcbhead *head; 922c3229e05SDavid Greenman /* 923c3229e05SDavid Greenman * Look for an unconnected (wildcard foreign addr) PCB that 924c3229e05SDavid Greenman * matches the local address and port we're looking for. 925c3229e05SDavid Greenman */ 926c3229e05SDavid Greenman head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 927fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 928cfa1ca9dSYoshinobu Inoue #ifdef INET6 929369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 930cfa1ca9dSYoshinobu Inoue continue; 931cfa1ca9dSYoshinobu Inoue #endif 932c3229e05SDavid Greenman if (inp->inp_faddr.s_addr == INADDR_ANY && 933c3229e05SDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 934c3229e05SDavid Greenman inp->inp_lport == lport) { 935c3229e05SDavid Greenman /* 936c3229e05SDavid Greenman * Found. 937c3229e05SDavid Greenman */ 938c3229e05SDavid Greenman return (inp); 939df8bae1dSRodney W. Grimes } 940c3229e05SDavid Greenman } 941c3229e05SDavid Greenman /* 942c3229e05SDavid Greenman * Not found. 943c3229e05SDavid Greenman */ 944c3229e05SDavid Greenman return (NULL); 945c3229e05SDavid Greenman } else { 946c3229e05SDavid Greenman struct inpcbporthead *porthash; 947c3229e05SDavid Greenman struct inpcbport *phd; 948c3229e05SDavid Greenman struct inpcb *match = NULL; 949c3229e05SDavid Greenman /* 950c3229e05SDavid Greenman * Best fit PCB lookup. 951c3229e05SDavid Greenman * 952c3229e05SDavid Greenman * First see if this local port is in use by looking on the 953c3229e05SDavid Greenman * port hash list. 954c3229e05SDavid Greenman */ 955c3229e05SDavid Greenman porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 956c3229e05SDavid Greenman pcbinfo->porthashmask)]; 957fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, porthash, phd_hash) { 958c3229e05SDavid Greenman if (phd->phd_port == lport) 959c3229e05SDavid Greenman break; 960c3229e05SDavid Greenman } 961c3229e05SDavid Greenman if (phd != NULL) { 962c3229e05SDavid Greenman /* 963c3229e05SDavid Greenman * Port is in use by one or more PCBs. Look for best 964c3229e05SDavid Greenman * fit. 965c3229e05SDavid Greenman */ 96637d40066SPoul-Henning Kamp LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 967c3229e05SDavid Greenman wildcard = 0; 968cfa1ca9dSYoshinobu Inoue #ifdef INET6 969369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 970cfa1ca9dSYoshinobu Inoue continue; 971cfa1ca9dSYoshinobu Inoue #endif 972c3229e05SDavid Greenman if (inp->inp_faddr.s_addr != INADDR_ANY) 973c3229e05SDavid Greenman wildcard++; 97415bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 97515bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 97615bd2b43SDavid Greenman wildcard++; 97715bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 97815bd2b43SDavid Greenman continue; 97915bd2b43SDavid Greenman } else { 98015bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 98115bd2b43SDavid Greenman wildcard++; 98215bd2b43SDavid Greenman } 983df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 984df8bae1dSRodney W. Grimes match = inp; 985df8bae1dSRodney W. Grimes matchwild = wildcard; 9863dbdc25cSDavid Greenman if (matchwild == 0) { 987df8bae1dSRodney W. Grimes break; 988df8bae1dSRodney W. Grimes } 989df8bae1dSRodney W. Grimes } 9903dbdc25cSDavid Greenman } 991c3229e05SDavid Greenman } 992df8bae1dSRodney W. Grimes return (match); 993df8bae1dSRodney W. Grimes } 994c3229e05SDavid Greenman } 99515bd2b43SDavid Greenman 99615bd2b43SDavid Greenman /* 99715bd2b43SDavid Greenman * Lookup PCB in hash list. 99815bd2b43SDavid Greenman */ 99915bd2b43SDavid Greenman struct inpcb * 1000cfa1ca9dSYoshinobu Inoue in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, 1001cfa1ca9dSYoshinobu Inoue ifp) 100215bd2b43SDavid Greenman struct inpcbinfo *pcbinfo; 100315bd2b43SDavid Greenman struct in_addr faddr, laddr; 100415bd2b43SDavid Greenman u_int fport_arg, lport_arg; 10056d6a026bSDavid Greenman int wildcard; 1006cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 100715bd2b43SDavid Greenman { 100815bd2b43SDavid Greenman struct inpcbhead *head; 100915bd2b43SDavid Greenman register struct inpcb *inp; 101015bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 101115bd2b43SDavid Greenman 101215bd2b43SDavid Greenman /* 101315bd2b43SDavid Greenman * First look for an exact match. 101415bd2b43SDavid Greenman */ 1015ddd79a97SDavid Greenman head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)]; 1016fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(inp, head, inp_hash) { 1017cfa1ca9dSYoshinobu Inoue #ifdef INET6 1018369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 1019cfa1ca9dSYoshinobu Inoue continue; 1020cfa1ca9dSYoshinobu Inoue #endif 10216d6a026bSDavid Greenman if (inp->inp_faddr.s_addr == faddr.s_addr && 1022ca98b82cSDavid Greenman inp->inp_laddr.s_addr == laddr.s_addr && 1023ca98b82cSDavid Greenman inp->inp_fport == fport && 1024c3229e05SDavid Greenman inp->inp_lport == lport) { 1025c3229e05SDavid Greenman /* 1026c3229e05SDavid Greenman * Found. 1027c3229e05SDavid Greenman */ 1028c3229e05SDavid Greenman return (inp); 1029c3229e05SDavid Greenman } 10306d6a026bSDavid Greenman } 10316d6a026bSDavid Greenman if (wildcard) { 10326d6a026bSDavid Greenman struct inpcb *local_wild = NULL; 1033cfa1ca9dSYoshinobu Inoue #if defined(INET6) 1034cfa1ca9dSYoshinobu Inoue struct inpcb *local_wild_mapped = NULL; 1035cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */ 10366d6a026bSDavid Greenman 1037ddd79a97SDavid Greenman head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->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) { 1051cfa1ca9dSYoshinobu Inoue #if defined(INET6) 1052cfa1ca9dSYoshinobu Inoue if (INP_CHECK_SOCKAF(inp->inp_socket, 1053cfa1ca9dSYoshinobu Inoue AF_INET6)) 1054cfa1ca9dSYoshinobu Inoue local_wild_mapped = inp; 1055cfa1ca9dSYoshinobu Inoue else 1056cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */ 10576d6a026bSDavid Greenman local_wild = inp; 10586d6a026bSDavid Greenman } 10596d6a026bSDavid Greenman } 1060cfa1ca9dSYoshinobu Inoue } 1061cfa1ca9dSYoshinobu Inoue #if defined(INET6) 1062cfa1ca9dSYoshinobu Inoue if (local_wild == NULL) 1063cfa1ca9dSYoshinobu Inoue return (local_wild_mapped); 1064cfa1ca9dSYoshinobu Inoue #endif /* defined(INET6) */ 1065c3229e05SDavid Greenman return (local_wild); 10666d6a026bSDavid Greenman } 1067c3229e05SDavid Greenman 1068c3229e05SDavid Greenman /* 1069c3229e05SDavid Greenman * Not found. 1070c3229e05SDavid Greenman */ 10716d6a026bSDavid Greenman return (NULL); 107215bd2b43SDavid Greenman } 107315bd2b43SDavid Greenman 10747bc4aca7SDavid Greenman /* 1075c3229e05SDavid Greenman * Insert PCB onto various hash lists. 10767bc4aca7SDavid Greenman */ 1077c3229e05SDavid Greenman int 107815bd2b43SDavid Greenman in_pcbinshash(inp) 107915bd2b43SDavid Greenman struct inpcb *inp; 108015bd2b43SDavid Greenman { 1081c3229e05SDavid Greenman struct inpcbhead *pcbhash; 1082c3229e05SDavid Greenman struct inpcbporthead *pcbporthash; 1083c3229e05SDavid Greenman struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1084c3229e05SDavid Greenman struct inpcbport *phd; 1085cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 108615bd2b43SDavid Greenman 1087cfa1ca9dSYoshinobu Inoue #ifdef INET6 1088cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1089cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1090cfa1ca9dSYoshinobu Inoue else 1091cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1092cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1093cfa1ca9dSYoshinobu Inoue 1094cfa1ca9dSYoshinobu Inoue pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 1095c3229e05SDavid Greenman inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)]; 109615bd2b43SDavid Greenman 1097c3229e05SDavid Greenman pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport, 1098c3229e05SDavid Greenman pcbinfo->porthashmask)]; 1099c3229e05SDavid Greenman 1100c3229e05SDavid Greenman /* 1101c3229e05SDavid Greenman * Go through port list and look for a head for this lport. 1102c3229e05SDavid Greenman */ 1103fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(phd, pcbporthash, phd_hash) { 1104c3229e05SDavid Greenman if (phd->phd_port == inp->inp_lport) 1105c3229e05SDavid Greenman break; 1106c3229e05SDavid Greenman } 1107c3229e05SDavid Greenman /* 1108c3229e05SDavid Greenman * If none exists, malloc one and tack it on. 1109c3229e05SDavid Greenman */ 1110c3229e05SDavid Greenman if (phd == NULL) { 1111c3229e05SDavid Greenman MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1112c3229e05SDavid Greenman if (phd == NULL) { 1113c3229e05SDavid Greenman return (ENOBUFS); /* XXX */ 1114c3229e05SDavid Greenman } 1115c3229e05SDavid Greenman phd->phd_port = inp->inp_lport; 1116c3229e05SDavid Greenman LIST_INIT(&phd->phd_pcblist); 1117c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1118c3229e05SDavid Greenman } 1119c3229e05SDavid Greenman inp->inp_phd = phd; 1120c3229e05SDavid Greenman LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1121c3229e05SDavid Greenman LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1122c3229e05SDavid Greenman return (0); 112315bd2b43SDavid Greenman } 112415bd2b43SDavid Greenman 1125c3229e05SDavid Greenman /* 1126c3229e05SDavid Greenman * Move PCB to the proper hash bucket when { faddr, fport } have been 1127c3229e05SDavid Greenman * changed. NOTE: This does not handle the case of the lport changing (the 1128c3229e05SDavid Greenman * hashed port list would have to be updated as well), so the lport must 1129c3229e05SDavid Greenman * not change after in_pcbinshash() has been called. 1130c3229e05SDavid Greenman */ 113115bd2b43SDavid Greenman void 113215bd2b43SDavid Greenman in_pcbrehash(inp) 113315bd2b43SDavid Greenman struct inpcb *inp; 113415bd2b43SDavid Greenman { 113515bd2b43SDavid Greenman struct inpcbhead *head; 1136cfa1ca9dSYoshinobu Inoue u_int32_t hashkey_faddr; 113715bd2b43SDavid Greenman 1138cfa1ca9dSYoshinobu Inoue #ifdef INET6 1139cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) 1140cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1141cfa1ca9dSYoshinobu Inoue else 1142cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 1143cfa1ca9dSYoshinobu Inoue hashkey_faddr = inp->inp_faddr.s_addr; 1144cfa1ca9dSYoshinobu Inoue 1145cfa1ca9dSYoshinobu Inoue head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 1146ddd79a97SDavid Greenman inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)]; 114715bd2b43SDavid Greenman 1148c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 114915bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 1150c3229e05SDavid Greenman } 1151c3229e05SDavid Greenman 1152c3229e05SDavid Greenman /* 1153c3229e05SDavid Greenman * Remove PCB from various lists. 1154c3229e05SDavid Greenman */ 115576429de4SYoshinobu Inoue void 1156c3229e05SDavid Greenman in_pcbremlists(inp) 1157c3229e05SDavid Greenman struct inpcb *inp; 1158c3229e05SDavid Greenman { 115998271db4SGarrett Wollman inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt; 1160c3229e05SDavid Greenman if (inp->inp_lport) { 1161c3229e05SDavid Greenman struct inpcbport *phd = inp->inp_phd; 1162c3229e05SDavid Greenman 1163c3229e05SDavid Greenman LIST_REMOVE(inp, inp_hash); 1164c3229e05SDavid Greenman LIST_REMOVE(inp, inp_portlist); 1165fc2ffbe6SPoul-Henning Kamp if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1166c3229e05SDavid Greenman LIST_REMOVE(phd, phd_hash); 1167c3229e05SDavid Greenman free(phd, M_PCB); 1168c3229e05SDavid Greenman } 1169c3229e05SDavid Greenman } 1170c3229e05SDavid Greenman LIST_REMOVE(inp, inp_list); 11713d4d47f3SGarrett Wollman inp->inp_pcbinfo->ipi_count--; 117215bd2b43SDavid Greenman } 117375c13541SPoul-Henning Kamp 117475c13541SPoul-Henning Kamp int 1175ad278afdSJohn Baldwin prison_xinpcb(struct thread *td, struct inpcb *inp) 117675c13541SPoul-Henning Kamp { 1177ad278afdSJohn Baldwin if (!jailed(td->td_ucred)) 117875c13541SPoul-Henning Kamp return (0); 1179ad278afdSJohn Baldwin if (ntohl(inp->inp_laddr.s_addr) == prison_getip(td->td_ucred)) 118075c13541SPoul-Henning Kamp return (0); 118175c13541SPoul-Henning Kamp return (1); 118275c13541SPoul-Henning Kamp } 1183