in6_pcb.c revision 186141
1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 2853541Sshin * 29174510Sobrien * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 3353541Sshin * Copyright (c) 1982, 1986, 1991, 1993 3453541Sshin * The Regents of the University of California. All rights reserved. 3553541Sshin * 3653541Sshin * Redistribution and use in source and binary forms, with or without 3753541Sshin * modification, are permitted provided that the following conditions 3853541Sshin * are met: 3953541Sshin * 1. Redistributions of source code must retain the above copyright 4053541Sshin * notice, this list of conditions and the following disclaimer. 4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer in the 4353541Sshin * documentation and/or other materials provided with the distribution. 4453541Sshin * 4. Neither the name of the University nor the names of its contributors 4553541Sshin * may be used to endorse or promote products derived from this software 4653541Sshin * without specific prior written permission. 4753541Sshin * 4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5153541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 6153541Sshin */ 6253541Sshin 63174510Sobrien#include <sys/cdefs.h> 64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 186141 2008-12-15 21:50:54Z bz $"); 65174510Sobrien 6678064Sume#include "opt_inet.h" 6778064Sume#include "opt_inet6.h" 6855009Sshin#include "opt_ipsec.h" 69174717Srwatson#include "opt_mac.h" 7053541Sshin 7153541Sshin#include <sys/param.h> 7253541Sshin#include <sys/systm.h> 7353541Sshin#include <sys/malloc.h> 7453541Sshin#include <sys/mbuf.h> 7555679Sshin#include <sys/domain.h> 7653541Sshin#include <sys/protosw.h> 7753541Sshin#include <sys/socket.h> 7853541Sshin#include <sys/socketvar.h> 7953541Sshin#include <sys/sockio.h> 8053541Sshin#include <sys/errno.h> 8153541Sshin#include <sys/time.h> 82164033Srwatson#include <sys/priv.h> 8353541Sshin#include <sys/proc.h> 8453541Sshin#include <sys/jail.h> 85181803Sbz#include <sys/vimage.h> 8653541Sshin 8792767Sjeff#include <vm/uma.h> 8853541Sshin 8953541Sshin#include <net/if.h> 9053541Sshin#include <net/if_types.h> 9153541Sshin#include <net/route.h> 9253541Sshin 9353541Sshin#include <netinet/in.h> 9453541Sshin#include <netinet/in_var.h> 9553541Sshin#include <netinet/in_systm.h> 9698102Shsu#include <netinet/tcp_var.h> 9762587Sitojun#include <netinet/ip6.h> 9855679Sshin#include <netinet/ip_var.h> 99185571Sbz#include <netinet/vinet.h> 100181887Sjulian 10153541Sshin#include <netinet6/ip6_var.h> 10253541Sshin#include <netinet6/nd6.h> 10353541Sshin#include <netinet/in_pcb.h> 10453541Sshin#include <netinet6/in6_pcb.h> 105148385Sume#include <netinet6/scope6_var.h> 106185571Sbz#include <netinet6/vinet6.h> 10753541Sshin 108174717Srwatson#include <security/mac/mac_framework.h> 109174717Srwatson 11053541Sshinstruct in6_addr zeroin6_addr; 11153541Sshin 11253541Sshinint 113171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam, 114171259Sdelphij struct ucred *cred) 11553541Sshin{ 116183550Szec INIT_VNET_INET6(inp->inp_vnet); 117183550Szec INIT_VNET_INET(inp->inp_vnet); 11853541Sshin struct socket *so = inp->inp_socket; 11953541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 12053541Sshin struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 12153541Sshin u_short lport = 0; 12297658Stanimura int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 12353541Sshin 124132714Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 125178285Srwatson INP_WLOCK_ASSERT(inp); 126132714Srwatson 127181803Sbz if (!V_in6_ifaddr) /* XXX broken! */ 12853541Sshin return (EADDRNOTAVAIL); 12953541Sshin if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 130120856Sume return (EINVAL); 13153541Sshin if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 132160024Sbz wild = INPLOOKUP_WILDCARD; 13353541Sshin if (nam) { 134148385Sume int error; 135148385Sume 13653541Sshin sin6 = (struct sockaddr_in6 *)nam; 13753541Sshin if (nam->sa_len != sizeof(*sin6)) 138120856Sume return (EINVAL); 13953541Sshin /* 14053541Sshin * family check. 14153541Sshin */ 14253541Sshin if (nam->sa_family != AF_INET6) 143120856Sume return (EAFNOSUPPORT); 14453541Sshin 145181803Sbz if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 146148385Sume return(error); 14753541Sshin 148185435Sbz if (prison_local_ip6(cred, &sin6->sin6_addr, 149185435Sbz ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0) 150185435Sbz return (EINVAL); 151185435Sbz 15253541Sshin lport = sin6->sin6_port; 15353541Sshin if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 15453541Sshin /* 15553541Sshin * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 15653541Sshin * allow compepte duplication of binding if 15753541Sshin * SO_REUSEPORT is set, or if SO_REUSEADDR is set 15853541Sshin * and a multicast address is bound on both 15953541Sshin * new and duplicated sockets. 16053541Sshin */ 16153541Sshin if (so->so_options & SO_REUSEADDR) 16253541Sshin reuseport = SO_REUSEADDR|SO_REUSEPORT; 16353541Sshin } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 16453541Sshin struct ifaddr *ia = NULL; 16553541Sshin 16653541Sshin sin6->sin6_port = 0; /* yech... */ 16753541Sshin if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 168120856Sume return (EADDRNOTAVAIL); 16953541Sshin 17053541Sshin /* 17153541Sshin * XXX: bind to an anycast address might accidentally 17253541Sshin * cause sending a packet with anycast source address. 17378064Sume * We should allow to bind to a deprecated address, since 174120913Sume * the application dares to use it. 17553541Sshin */ 17653541Sshin if (ia && 17753541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 17878064Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 179120856Sume return (EADDRNOTAVAIL); 18053541Sshin } 18153541Sshin } 18253541Sshin if (lport) { 18353541Sshin struct inpcb *t; 18453541Sshin 18553541Sshin /* GROSS */ 186181803Sbz if (ntohs(lport) <= V_ipport_reservedhigh && 187181803Sbz ntohs(lport) >= V_ipport_reservedlow && 188164033Srwatson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 189170587Srwatson 0)) 190120856Sume return (EACCES); 191159976Spjd if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 192183606Sbz priv_check_cred(inp->inp_cred, 193170587Srwatson PRIV_NETINET_REUSEPORT, 0) != 0) { 19455679Sshin t = in6_pcblookup_local(pcbinfo, 19553541Sshin &sin6->sin6_addr, lport, 196180427Sbz INPLOOKUP_WILDCARD, cred); 197132699Syar if (t && 198132699Syar ((t->inp_vflag & INP_TIMEWAIT) == 0) && 199132699Syar (so->so_type != SOCK_STREAM || 200132699Syar IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 20197658Stanimura (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 202171260Sdelphij !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 203171260Sdelphij (t->inp_socket->so_options & SO_REUSEPORT) 204183606Sbz == 0) && (inp->inp_cred->cr_uid != 205183606Sbz t->inp_cred->cr_uid)) 20697658Stanimura return (EADDRINUSE); 20778064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 20855679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 20955679Sshin struct sockaddr_in sin; 21055679Sshin 21155679Sshin in6_sin6_2_sin(&sin, sin6); 21255679Sshin t = in_pcblookup_local(pcbinfo, 213180427Sbz sin.sin_addr, lport, 214180427Sbz INPLOOKUP_WILDCARD, cred); 215132699Syar if (t && 216132699Syar ((t->inp_vflag & 217132699Syar INP_TIMEWAIT) == 0) && 218132699Syar (so->so_type != SOCK_STREAM || 219132699Syar ntohl(t->inp_faddr.s_addr) == 220132699Syar INADDR_ANY) && 221183606Sbz (inp->inp_cred->cr_uid != 222183606Sbz t->inp_cred->cr_uid)) 22355679Sshin return (EADDRINUSE); 22455679Sshin } 22553541Sshin } 226185435Sbz if (prison_local_ip6(cred, &sin6->sin6_addr, 227185435Sbz ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0) 228185435Sbz return (EADDRNOTAVAIL); 22953541Sshin t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 230180427Sbz lport, wild, cred); 231116453Scognet if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ? 232171260Sdelphij intotw(t)->tw_so_options : 233116453Scognet t->inp_socket->so_options)) == 0) 234120856Sume return (EADDRINUSE); 23578064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 23655679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 23755679Sshin struct sockaddr_in sin; 23855679Sshin 23955679Sshin in6_sin6_2_sin(&sin, sin6); 24055679Sshin t = in_pcblookup_local(pcbinfo, sin.sin_addr, 241180427Sbz lport, wild, cred); 242116453Scognet if (t && t->inp_vflag & INP_TIMEWAIT) { 243171260Sdelphij if ((reuseport & 244116453Scognet intotw(t)->tw_so_options) == 0 && 245116453Scognet (ntohl(t->inp_laddr.s_addr) != 246171260Sdelphij INADDR_ANY || ((inp->inp_vflag & 247171260Sdelphij INP_IPV6PROTO) == 248116453Scognet (t->inp_vflag & INP_IPV6PROTO)))) 249116453Scognet return (EADDRINUSE); 250116453Scognet } 251171260Sdelphij else if (t && 252171260Sdelphij (reuseport & t->inp_socket->so_options) 253171260Sdelphij == 0 && (ntohl(t->inp_laddr.s_addr) != 254116453Scognet INADDR_ANY || INP_SOCKAF(so) == 25597658Stanimura INP_SOCKAF(t->inp_socket))) 25697658Stanimura return (EADDRINUSE); 25755679Sshin } 25853541Sshin } 25953541Sshin inp->in6p_laddr = sin6->sin6_addr; 26053541Sshin } 261185435Sbz if (prison_local_ip6(cred, &inp->in6p_laddr, 262185435Sbz ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0) 263185435Sbz return (EINVAL); 26453541Sshin if (lport == 0) { 26562587Sitojun int e; 266127505Spjd if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) 267120856Sume return (e); 268183611Sbz } else { 26962587Sitojun inp->inp_lport = lport; 27062587Sitojun if (in_pcbinshash(inp) != 0) { 27162587Sitojun inp->in6p_laddr = in6addr_any; 27262587Sitojun inp->inp_lport = 0; 27362587Sitojun return (EAGAIN); 27453541Sshin } 27553541Sshin } 276120856Sume return (0); 27753541Sshin} 27853541Sshin 27953541Sshin/* 28053541Sshin * Transform old in6_pcbconnect() into an inner subroutine for new 28153541Sshin * in6_pcbconnect(): Do some validity-checking on the remote 28253541Sshin * address (in mbuf 'nam') and then determine local host address 28353541Sshin * (i.e., which interface) to use to access that remote host. 28453541Sshin * 28553541Sshin * This preserves definition of in6_pcbconnect(), while supporting a 28653541Sshin * slightly different version for T/TCP. (This is more than 28753541Sshin * a bit of a kludge, but cleaning up the internal interfaces would 28853541Sshin * have forced minor changes in every protocol). 28953541Sshin */ 29053541Sshinint 291171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam, 292171259Sdelphij struct in6_addr **plocal_addr6) 29353541Sshin{ 294183550Szec INIT_VNET_INET6(inp->inp_vnet); 29553541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 296148385Sume int error = 0; 29753541Sshin struct ifnet *ifp = NULL; 298148385Sume int scope_ambiguous = 0; 29953541Sshin 300158011Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 301178285Srwatson INP_WLOCK_ASSERT(inp); 302158011Srwatson 30353541Sshin if (nam->sa_len != sizeof (*sin6)) 30453541Sshin return (EINVAL); 30553541Sshin if (sin6->sin6_family != AF_INET6) 30653541Sshin return (EAFNOSUPPORT); 30753541Sshin if (sin6->sin6_port == 0) 30853541Sshin return (EADDRNOTAVAIL); 30953541Sshin 310181803Sbz if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 311148385Sume scope_ambiguous = 1; 312181803Sbz if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 313148385Sume return(error); 314148385Sume 315181803Sbz if (V_in6_ifaddr) { 31653541Sshin /* 31753541Sshin * If the destination address is UNSPECIFIED addr, 31853541Sshin * use the loopback addr, e.g ::1. 31953541Sshin */ 32053541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 32153541Sshin sin6->sin6_addr = in6addr_loopback; 32253541Sshin } 323185435Sbz if (prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr) != 0) 324185435Sbz return (EADDRNOTAVAIL); 325148385Sume 326148385Sume /* 327148385Sume * XXX: in6_selectsrc might replace the bound local address 328148385Sume * with the address specified by setsockopt(IPV6_PKTINFO). 329148385Sume * Is it the intended behavior? 330148385Sume */ 331148385Sume *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 332180371Sbz inp, NULL, 333183606Sbz inp->inp_cred, 334180371Sbz &ifp, &error); 335148385Sume if (ifp && scope_ambiguous && 336148385Sume (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { 337148385Sume return(error); 33853541Sshin } 339148385Sume 340183611Sbz if (*plocal_addr6 == NULL) { 341148385Sume if (error == 0) 342148385Sume error = EADDRNOTAVAIL; 343148385Sume return (error); 344148385Sume } 345148385Sume /* 346148385Sume * Don't do pcblookup call here; return interface in 347148385Sume * plocal_addr6 348148385Sume * and exit to caller, that will do the lookup. 349148385Sume */ 350148385Sume 351120856Sume return (0); 35253541Sshin} 35353541Sshin 35453541Sshin/* 35553541Sshin * Outer subroutine: 35653541Sshin * Connect from a socket to a specified address. 35753541Sshin * Both address and port must be specified in argument sin. 35853541Sshin * If don't have a local address for this socket yet, 35953541Sshin * then pick one. 36053541Sshin */ 36153541Sshinint 362171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam, 363171259Sdelphij struct ucred *cred) 36453541Sshin{ 36553541Sshin struct in6_addr *addr6; 36653541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 36753541Sshin int error; 36853541Sshin 369132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 370178285Srwatson INP_WLOCK_ASSERT(inp); 371132714Srwatson 37253541Sshin /* 37395023Ssuz * Call inner routine, to assign local interface address. 37495023Ssuz * in6_pcbladdr() may automatically fill in sin6_scope_id. 37553541Sshin */ 37653541Sshin if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 377120856Sume return (error); 37853541Sshin 37953541Sshin if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 38053541Sshin sin6->sin6_port, 38153541Sshin IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 38253541Sshin ? addr6 : &inp->in6p_laddr, 38353541Sshin inp->inp_lport, 0, NULL) != NULL) { 38453541Sshin return (EADDRINUSE); 38553541Sshin } 38653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 38753541Sshin if (inp->inp_lport == 0) { 388127505Spjd error = in6_pcbbind(inp, (struct sockaddr *)0, cred); 38953541Sshin if (error) 39053541Sshin return (error); 39153541Sshin } 39253541Sshin inp->in6p_laddr = *addr6; 39353541Sshin } 39453541Sshin inp->in6p_faddr = sin6->sin6_addr; 39553541Sshin inp->inp_fport = sin6->sin6_port; 39678064Sume /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 397186141Sbz inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 398186141Sbz if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 399186141Sbz inp->inp_flow |= 400120649Sume (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 40153541Sshin 40253541Sshin in_pcbrehash(inp); 403171133Sgnn 40453541Sshin return (0); 40553541Sshin} 40653541Sshin 40753541Sshinvoid 408171259Sdelphijin6_pcbdisconnect(struct inpcb *inp) 40953541Sshin{ 410132714Srwatson 411132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 412178285Srwatson INP_WLOCK_ASSERT(inp); 413132714Srwatson 41453541Sshin bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 41553541Sshin inp->inp_fport = 0; 41678064Sume /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 417186141Sbz inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 41853541Sshin in_pcbrehash(inp); 41953541Sshin} 42053541Sshin 421102218Struckmanstruct sockaddr * 422171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p) 423102218Struckman{ 424102218Struckman struct sockaddr_in6 *sin6; 425102218Struckman 426184205Sdes sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK); 427102218Struckman bzero(sin6, sizeof *sin6); 428102218Struckman sin6->sin6_family = AF_INET6; 429102218Struckman sin6->sin6_len = sizeof(*sin6); 430102218Struckman sin6->sin6_port = port; 431102218Struckman sin6->sin6_addr = *addr_p; 432148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 433102218Struckman 434102218Struckman return (struct sockaddr *)sin6; 435102218Struckman} 436102218Struckman 437102218Struckmanstruct sockaddr * 438171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 439102218Struckman{ 440102218Struckman struct sockaddr_in sin; 441102218Struckman struct sockaddr_in6 *sin6_p; 442102218Struckman 443102218Struckman bzero(&sin, sizeof sin); 444102218Struckman sin.sin_family = AF_INET; 445102218Struckman sin.sin_len = sizeof(sin); 446102218Struckman sin.sin_port = port; 447102218Struckman sin.sin_addr = *addr_p; 448102218Struckman 449184205Sdes sin6_p = malloc(sizeof *sin6_p, M_SONAME, 450111119Simp M_WAITOK); 451102218Struckman in6_sin_2_v4mapsin6(&sin, sin6_p); 452102218Struckman 453102218Struckman return (struct sockaddr *)sin6_p; 454102218Struckman} 455102218Struckman 45653541Sshinint 457171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam) 45853541Sshin{ 45953541Sshin register struct inpcb *inp; 460102218Struckman struct in6_addr addr; 461102218Struckman in_port_t port; 46253541Sshin 46353541Sshin inp = sotoinpcb(so); 464169462Srwatson KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 465157673Srwatson 466178320Srwatson INP_RLOCK(inp); 467102218Struckman port = inp->inp_lport; 468102218Struckman addr = inp->in6p_laddr; 469178320Srwatson INP_RUNLOCK(inp); 47053541Sshin 471102218Struckman *nam = in6_sockaddr(port, &addr); 47253541Sshin return 0; 47353541Sshin} 47453541Sshin 47553541Sshinint 476171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam) 47753541Sshin{ 47853541Sshin struct inpcb *inp; 479102218Struckman struct in6_addr addr; 480102218Struckman in_port_t port; 48153541Sshin 48253541Sshin inp = sotoinpcb(so); 483169462Srwatson KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 484157673Srwatson 485178320Srwatson INP_RLOCK(inp); 486102218Struckman port = inp->inp_fport; 487102218Struckman addr = inp->in6p_faddr; 488178320Srwatson INP_RUNLOCK(inp); 48953541Sshin 490102218Struckman *nam = in6_sockaddr(port, &addr); 49153541Sshin return 0; 49253541Sshin} 49353541Sshin 49453541Sshinint 49553541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 49653541Sshin{ 497157673Srwatson struct inpcb *inp; 49853541Sshin int error; 49953541Sshin 500157673Srwatson inp = sotoinpcb(so); 501157673Srwatson KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 502157673Srwatson 503124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 504169462Srwatson error = in_getsockaddr(so, nam); 50554952Seivind if (error == 0) 50653541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 507120913Sume } else { 508169462Srwatson /* scope issues will be handled in in6_getsockaddr(). */ 509169462Srwatson error = in6_getsockaddr(so, nam); 510120913Sume } 51153541Sshin 51253541Sshin return error; 51353541Sshin} 51453541Sshin 51553541Sshinint 51653541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 51753541Sshin{ 518157673Srwatson struct inpcb *inp; 51953541Sshin int error; 52053541Sshin 521157673Srwatson inp = sotoinpcb(so); 522157673Srwatson KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 523157673Srwatson 524124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 525169462Srwatson error = in_getpeeraddr(so, nam); 52654952Seivind if (error == 0) 52753541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 52853541Sshin } else 529169462Srwatson /* scope issues will be handled in in6_getpeeraddr(). */ 530169462Srwatson error = in6_getpeeraddr(so, nam); 53153541Sshin 53253541Sshin return error; 53353541Sshin} 53453541Sshin 53553541Sshin/* 53653541Sshin * Pass some notification to all connections of a protocol 53753541Sshin * associated with address dst. The local address and/or port numbers 53853541Sshin * may be specified to limit the search. The "usual action" will be 53953541Sshin * taken, depending on the ctlinput cmd. The caller must filter any 54053541Sshin * cmds that are uninteresting (e.g., no error in the map). 54153541Sshin * Call the protocol specific routine (if any) to report 54253541Sshin * any errors for each matching socket. 54353541Sshin */ 54453541Sshinvoid 545171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, 546171259Sdelphij u_int fport_arg, const struct sockaddr *src, u_int lport_arg, 547171259Sdelphij int cmd, void *cmdarg, 548175162Sobrien struct inpcb *(*notify)(struct inpcb *, int)) 54953541Sshin{ 550177961Srwatson struct inpcb *inp, *inp_temp; 55178064Sume struct sockaddr_in6 sa6_src, *sa6_dst; 55253541Sshin u_short fport = fport_arg, lport = lport_arg; 55378064Sume u_int32_t flowinfo; 554157673Srwatson int errno; 55553541Sshin 556119995Sru if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 55753541Sshin return; 55878064Sume 55978064Sume sa6_dst = (struct sockaddr_in6 *)dst; 56078064Sume if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 56153541Sshin return; 56253541Sshin 56353541Sshin /* 56478064Sume * note that src can be NULL when we get notify by local fragmentation. 56578064Sume */ 56691346Salfred sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 56778064Sume flowinfo = sa6_src.sin6_flowinfo; 56878064Sume 56978064Sume /* 57053541Sshin * Redirects go to all references to the destination, 57162587Sitojun * and use in6_rtchange to invalidate the route cache. 57262587Sitojun * Dead host indications: also use in6_rtchange to invalidate 57362587Sitojun * the cache, and deliver the error to all the sockets. 57453541Sshin * Otherwise, if we have knowledge of the local port and address, 57553541Sshin * deliver only to that socket. 57653541Sshin */ 57753541Sshin if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 57853541Sshin fport = 0; 57953541Sshin lport = 0; 58078064Sume bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 58162587Sitojun 58278064Sume if (cmd != PRC_HOSTDEAD) 58378064Sume notify = in6_rtchange; 58453541Sshin } 58553541Sshin errno = inet6ctlerrmap[cmd]; 586133192Srwatson INP_INFO_WLOCK(pcbinfo); 587177961Srwatson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 588178285Srwatson INP_WLOCK(inp); 589171260Sdelphij if ((inp->inp_vflag & INP_IPV6) == 0) { 590178285Srwatson INP_WUNLOCK(inp); 59153541Sshin continue; 592133192Srwatson } 59362587Sitojun 59478064Sume /* 595125776Sume * If the error designates a new path MTU for a destination 596125776Sume * and the application (associated with this socket) wanted to 597125776Sume * know the value, notify. Note that we notify for all 598125776Sume * disconnected sockets if the corresponding application 599125776Sume * wanted. This is because some UDP applications keep sending 600125776Sume * sockets disconnected. 601125776Sume * XXX: should we avoid to notify the value to TCP sockets? 602125776Sume */ 603125776Sume if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 604125776Sume (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 605125776Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 606125776Sume ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 607125776Sume (u_int32_t *)cmdarg); 608125776Sume } 609125776Sume 610125776Sume /* 61178064Sume * Detect if we should notify the error. If no source and 61278064Sume * destination ports are specifed, but non-zero flowinfo and 61378064Sume * local address match, notify the error. This is the case 61478064Sume * when the error is delivered with an encrypted buffer 61578064Sume * by ESP. Otherwise, just compare addresses and ports 61678064Sume * as usual. 61778064Sume */ 61878064Sume if (lport == 0 && fport == 0 && flowinfo && 61978064Sume inp->inp_socket != NULL && 620186141Sbz flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) && 62178064Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 62278064Sume goto do_notify; 62378064Sume else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 62478064Sume &sa6_dst->sin6_addr) || 62578064Sume inp->inp_socket == 0 || 62678064Sume (lport && inp->inp_lport != lport) || 62778064Sume (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 62878064Sume !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 62978064Sume &sa6_src.sin6_addr)) || 630133192Srwatson (fport && inp->inp_fport != fport)) { 631178285Srwatson INP_WUNLOCK(inp); 63253541Sshin continue; 633133192Srwatson } 63462587Sitojun 63578064Sume do_notify: 636134121Srwatson if (notify) { 637134121Srwatson if ((*notify)(inp, errno)) 638178285Srwatson INP_WUNLOCK(inp); 639134121Srwatson } else 640178285Srwatson INP_WUNLOCK(inp); 64153541Sshin } 642133192Srwatson INP_INFO_WUNLOCK(pcbinfo); 64353541Sshin} 64453541Sshin 64553541Sshin/* 64653541Sshin * Lookup a PCB based on the local address and port. 64753541Sshin */ 64853541Sshinstruct inpcb * 649171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 650180427Sbz u_short lport, int wild_okay, struct ucred *cred) 65153541Sshin{ 65253541Sshin register struct inpcb *inp; 65353541Sshin int matchwild = 3, wildcard; 65453541Sshin 655158011Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 656158011Srwatson 65753541Sshin if (!wild_okay) { 65853541Sshin struct inpcbhead *head; 65953541Sshin /* 66053541Sshin * Look for an unconnected (wildcard foreign addr) PCB that 66153541Sshin * matches the local address and port we're looking for. 66253541Sshin */ 663169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 664169154Srwatson 0, pcbinfo->ipi_hashmask)]; 66553541Sshin LIST_FOREACH(inp, head, inp_hash) { 666185435Sbz /* XXX inp locking */ 66754952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 66853541Sshin continue; 66953541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 67053541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 67153541Sshin inp->inp_lport == lport) { 672185435Sbz /* Found. */ 673185435Sbz if (cred == NULL || 674185435Sbz inp->inp_cred->cr_prison == cred->cr_prison) 675185435Sbz return (inp); 67653541Sshin } 67753541Sshin } 67853541Sshin /* 67953541Sshin * Not found. 68053541Sshin */ 68153541Sshin return (NULL); 68253541Sshin } else { 68353541Sshin struct inpcbporthead *porthash; 68453541Sshin struct inpcbport *phd; 68553541Sshin struct inpcb *match = NULL; 68653541Sshin /* 68753541Sshin * Best fit PCB lookup. 68853541Sshin * 68953541Sshin * First see if this local port is in use by looking on the 69053541Sshin * port hash list. 69153541Sshin */ 692169154Srwatson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 693169154Srwatson pcbinfo->ipi_porthashmask)]; 69453541Sshin LIST_FOREACH(phd, porthash, phd_hash) { 69553541Sshin if (phd->phd_port == lport) 69653541Sshin break; 69753541Sshin } 69853541Sshin if (phd != NULL) { 69953541Sshin /* 70053541Sshin * Port is in use by one or more PCBs. Look for best 70153541Sshin * fit. 70253541Sshin */ 70353541Sshin LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 70453541Sshin wildcard = 0; 705185435Sbz if (cred != NULL && 706185435Sbz inp->inp_cred->cr_prison != cred->cr_prison) 707185435Sbz continue; 708185435Sbz /* XXX inp locking */ 70954952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 71053541Sshin continue; 71153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 71253541Sshin wildcard++; 71353541Sshin if (!IN6_IS_ADDR_UNSPECIFIED( 71453541Sshin &inp->in6p_laddr)) { 71553541Sshin if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 71653541Sshin wildcard++; 71753541Sshin else if (!IN6_ARE_ADDR_EQUAL( 718185435Sbz &inp->in6p_laddr, laddr)) 71953541Sshin continue; 72053541Sshin } else { 72153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 72253541Sshin wildcard++; 72353541Sshin } 72453541Sshin if (wildcard < matchwild) { 72553541Sshin match = inp; 72653541Sshin matchwild = wildcard; 727185435Sbz if (matchwild == 0) 72853541Sshin break; 72953541Sshin } 73053541Sshin } 73153541Sshin } 73253541Sshin return (match); 73353541Sshin } 73453541Sshin} 73553541Sshin 73681127Sumevoid 737171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 73881127Sume{ 739186141Sbz struct inpcb *in6p; 74081127Sume struct ip6_moptions *im6o; 74181127Sume struct in6_multi_mship *imm, *nimm; 74281127Sume 743157978Srwatson INP_INFO_RLOCK(pcbinfo); 744169154Srwatson LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) { 745178285Srwatson INP_WLOCK(in6p); 74681127Sume im6o = in6p->in6p_moptions; 74781127Sume if ((in6p->inp_vflag & INP_IPV6) && 74881127Sume im6o) { 74981127Sume /* 75081127Sume * Unselect the outgoing interface if it is being 75181127Sume * detached. 75281127Sume */ 75381127Sume if (im6o->im6o_multicast_ifp == ifp) 75481127Sume im6o->im6o_multicast_ifp = NULL; 75581127Sume 75681127Sume /* 75781127Sume * Drop multicast group membership if we joined 75881127Sume * through the interface being detached. 75981127Sume * XXX controversial - is it really legal for kernel 76081127Sume * to force this? 76181127Sume */ 76281127Sume for (imm = im6o->im6o_memberships.lh_first; 76381127Sume imm != NULL; imm = nimm) { 76481127Sume nimm = imm->i6mm_chain.le_next; 76581127Sume if (imm->i6mm_maddr->in6m_ifp == ifp) { 76681127Sume LIST_REMOVE(imm, i6mm_chain); 76781127Sume in6_delmulti(imm->i6mm_maddr); 768149849Sobrien free(imm, M_IP6MADDR); 76981127Sume } 77081127Sume } 77181127Sume } 772178285Srwatson INP_WUNLOCK(in6p); 77381127Sume } 774157978Srwatson INP_INFO_RUNLOCK(pcbinfo); 77581127Sume} 77681127Sume 77753541Sshin/* 77853541Sshin * Check for alternatives when higher level complains 77953541Sshin * about service problems. For now, invalidate cached 78053541Sshin * routing information. If the route was created dynamically 78153541Sshin * (by a redirect), time to try a default gateway again. 78253541Sshin */ 78353541Sshinvoid 784171259Sdelphijin6_losing(struct inpcb *in6p) 78553541Sshin{ 786171259Sdelphij 787122922Sandre /* 788122922Sandre * We don't store route pointers in the routing table anymore 789122922Sandre */ 790122922Sandre return; 79153541Sshin} 79253541Sshin 79353541Sshin/* 79453541Sshin * After a routing change, flush old routing 79553541Sshin * and allocate a (hopefully) better one. 79653541Sshin */ 79798211Shsustruct inpcb * 798171259Sdelphijin6_rtchange(struct inpcb *inp, int errno) 79953541Sshin{ 800122922Sandre /* 801122922Sandre * We don't store route pointers in the routing table anymore 802122922Sandre */ 80398211Shsu return inp; 80453541Sshin} 80553541Sshin 80653541Sshin/* 80753541Sshin * Lookup PCB in hash list. 80853541Sshin */ 80953541Sshinstruct inpcb * 810171259Sdelphijin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 811185435Sbz u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard, 812185435Sbz struct ifnet *ifp) 81353541Sshin{ 81453541Sshin struct inpcbhead *head; 815185435Sbz struct inpcb *inp, *tmpinp; 81653541Sshin u_short fport = fport_arg, lport = lport_arg; 81778064Sume int faith; 81853541Sshin 819178285Srwatson INP_INFO_LOCK_ASSERT(pcbinfo); 820158011Srwatson 82183934Sbrooks if (faithprefix_p != NULL) 82283934Sbrooks faith = (*faithprefix_p)(laddr); 82383934Sbrooks else 82483934Sbrooks faith = 0; 82578064Sume 82653541Sshin /* 82753541Sshin * First look for an exact match. 82853541Sshin */ 829185435Sbz tmpinp = NULL; 830169154Srwatson head = &pcbinfo->ipi_hashbase[ 831169154Srwatson INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport, 832169154Srwatson pcbinfo->ipi_hashmask)]; 83354263Sshin LIST_FOREACH(inp, head, inp_hash) { 834185435Sbz /* XXX inp locking */ 83554952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 83653541Sshin continue; 83753541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 83853541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 83953541Sshin inp->inp_fport == fport && 84053541Sshin inp->inp_lport == lport) { 84153541Sshin /* 842185435Sbz * XXX We should be able to directly return 843185435Sbz * the inp here, without any checks. 844185435Sbz * Well unless both bound with SO_REUSEPORT? 84553541Sshin */ 846185435Sbz if (jailed(inp->inp_cred)) 847185435Sbz return (inp); 848185435Sbz if (tmpinp == NULL) 849185435Sbz tmpinp = inp; 85053541Sshin } 85153541Sshin } 852185435Sbz if (tmpinp != NULL) 853185435Sbz return (tmpinp); 85453541Sshin 855185435Sbz /* 856185435Sbz * Then look for a wildcard match, if requested. 857185435Sbz */ 858185435Sbz if (wildcard == INPLOOKUP_WILDCARD) { 859185435Sbz struct inpcb *local_wild = NULL, *local_exact = NULL; 860185435Sbz struct inpcb *jail_wild = NULL; 861185435Sbz int injail; 862185435Sbz 863185435Sbz /* 864185435Sbz * Order of socket selection - we always prefer jails. 865185435Sbz * 1. jailed, non-wild. 866185435Sbz * 2. jailed, wild. 867185435Sbz * 3. non-jailed, non-wild. 868185435Sbz * 4. non-jailed, wild. 869185435Sbz */ 870169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 871169154Srwatson 0, pcbinfo->ipi_hashmask)]; 87254263Sshin LIST_FOREACH(inp, head, inp_hash) { 873185435Sbz /* XXX inp locking */ 87454952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 87553541Sshin continue; 876185435Sbz 877185435Sbz if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 878185435Sbz inp->inp_lport != lport) { 879185435Sbz continue; 880185435Sbz } 881185435Sbz 882185435Sbz /* XXX inp locking */ 883185435Sbz if (faith && (inp->inp_flags & INP_FAITH) == 0) 884185435Sbz continue; 885185435Sbz 886185435Sbz injail = jailed(inp->inp_cred); 887185435Sbz if (injail) { 888185435Sbz if (!prison_check_ip6(inp->inp_cred, laddr)) 88953541Sshin continue; 890185435Sbz } else { 891185435Sbz if (local_exact != NULL) 892185435Sbz continue; 893185435Sbz } 894185435Sbz 895185435Sbz if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) { 896185435Sbz if (injail) 89753541Sshin return (inp); 898185435Sbz else 899185435Sbz local_exact = inp; 900185435Sbz } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 901185435Sbz if (injail) 902185435Sbz jail_wild = inp; 903185435Sbz else 90453541Sshin local_wild = inp; 90553541Sshin } 906185435Sbz } /* LIST_FOREACH */ 90753541Sshin 908185435Sbz if (jail_wild != NULL) 909185435Sbz return (jail_wild); 910185435Sbz if (local_exact != NULL) 911185435Sbz return (local_exact); 912185435Sbz if (local_wild != NULL) 913185435Sbz return (local_wild); 914185435Sbz } /* if (wildcard == INPLOOKUP_WILDCARD) */ 915185435Sbz 91653541Sshin /* 91753541Sshin * Not found. 91853541Sshin */ 91953541Sshin return (NULL); 92053541Sshin} 92153541Sshin 92253541Sshinvoid 92353541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 92453541Sshin{ 92553541Sshin struct ip6_hdr *ip; 92653541Sshin 92753541Sshin ip = mtod(m, struct ip6_hdr *); 92853541Sshin bzero(sin6, sizeof(*sin6)); 92953541Sshin sin6->sin6_len = sizeof(*sin6); 93053541Sshin sin6->sin6_family = AF_INET6; 93153541Sshin sin6->sin6_addr = ip->ip6_src; 93253541Sshin 933148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 934148385Sume 93553541Sshin return; 93653541Sshin} 937