in6_pcb.c revision 219570
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 219570 2011-03-12 16:45:15Z bz $"); 65174510Sobrien 6678064Sume#include "opt_inet.h" 6778064Sume#include "opt_inet6.h" 6855009Sshin#include "opt_ipsec.h" 6953541Sshin 7053541Sshin#include <sys/param.h> 7153541Sshin#include <sys/systm.h> 7253541Sshin#include <sys/malloc.h> 7353541Sshin#include <sys/mbuf.h> 7455679Sshin#include <sys/domain.h> 7553541Sshin#include <sys/protosw.h> 7653541Sshin#include <sys/socket.h> 7753541Sshin#include <sys/socketvar.h> 7853541Sshin#include <sys/sockio.h> 7953541Sshin#include <sys/errno.h> 8053541Sshin#include <sys/time.h> 81164033Srwatson#include <sys/priv.h> 8253541Sshin#include <sys/proc.h> 8353541Sshin#include <sys/jail.h> 8453541Sshin 8592767Sjeff#include <vm/uma.h> 8653541Sshin 8753541Sshin#include <net/if.h> 8853541Sshin#include <net/if_types.h> 8953541Sshin#include <net/route.h> 9053541Sshin 9153541Sshin#include <netinet/in.h> 9253541Sshin#include <netinet/in_var.h> 9353541Sshin#include <netinet/in_systm.h> 9498102Shsu#include <netinet/tcp_var.h> 9562587Sitojun#include <netinet/ip6.h> 9655679Sshin#include <netinet/ip_var.h> 97181887Sjulian 9853541Sshin#include <netinet6/ip6_var.h> 9953541Sshin#include <netinet6/nd6.h> 10053541Sshin#include <netinet/in_pcb.h> 10153541Sshin#include <netinet6/in6_pcb.h> 102148385Sume#include <netinet6/scope6_var.h> 10353541Sshin 10453541Sshinstruct in6_addr zeroin6_addr; 10553541Sshin 10653541Sshinint 107171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam, 108171259Sdelphij struct ucred *cred) 10953541Sshin{ 11053541Sshin struct socket *so = inp->inp_socket; 11153541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 11253541Sshin struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 11353541Sshin u_short lport = 0; 114188144Sjamie int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 11553541Sshin 116132714Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 117178285Srwatson INP_WLOCK_ASSERT(inp); 118132714Srwatson 119194907Srwatson if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */ 12053541Sshin return (EADDRNOTAVAIL); 12153541Sshin if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 122120856Sume return (EINVAL); 12353541Sshin if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 124160024Sbz wild = INPLOOKUP_WILDCARD; 125188148Sjamie if (nam == NULL) { 126188148Sjamie if ((error = prison_local_ip6(cred, &inp->in6p_laddr, 127188148Sjamie ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 128188148Sjamie return (error); 129188148Sjamie } else { 13053541Sshin sin6 = (struct sockaddr_in6 *)nam; 13153541Sshin if (nam->sa_len != sizeof(*sin6)) 132120856Sume return (EINVAL); 13353541Sshin /* 13453541Sshin * family check. 13553541Sshin */ 13653541Sshin if (nam->sa_family != AF_INET6) 137120856Sume return (EAFNOSUPPORT); 13853541Sshin 139181803Sbz if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 140148385Sume return(error); 14153541Sshin 142188144Sjamie if ((error = prison_local_ip6(cred, &sin6->sin6_addr, 143188144Sjamie ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 144188144Sjamie return (error); 145185435Sbz 14653541Sshin lport = sin6->sin6_port; 14753541Sshin if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 14853541Sshin /* 14953541Sshin * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 15053541Sshin * allow compepte duplication of binding if 15153541Sshin * SO_REUSEPORT is set, or if SO_REUSEADDR is set 15253541Sshin * and a multicast address is bound on both 15353541Sshin * new and duplicated sockets. 15453541Sshin */ 15553541Sshin if (so->so_options & SO_REUSEADDR) 15653541Sshin reuseport = SO_REUSEADDR|SO_REUSEPORT; 15753541Sshin } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 158194760Srwatson struct ifaddr *ifa; 15953541Sshin 16053541Sshin sin6->sin6_port = 0; /* yech... */ 161194760Srwatson if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) == 162194760Srwatson NULL && 163193217Spjd (inp->inp_flags & INP_BINDANY) == 0) { 164120856Sume return (EADDRNOTAVAIL); 165193217Spjd } 16653541Sshin 16753541Sshin /* 16853541Sshin * XXX: bind to an anycast address might accidentally 16953541Sshin * cause sending a packet with anycast source address. 17078064Sume * We should allow to bind to a deprecated address, since 171120913Sume * the application dares to use it. 17253541Sshin */ 173194760Srwatson if (ifa != NULL && 174194760Srwatson ((struct in6_ifaddr *)ifa)->ia6_flags & 17578064Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 176194760Srwatson ifa_free(ifa); 177120856Sume return (EADDRNOTAVAIL); 17853541Sshin } 179194760Srwatson if (ifa != NULL) 180194760Srwatson ifa_free(ifa); 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 && 198189848Srwatson ((t->inp_flags & 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 && 216189848Srwatson ((t->inp_flags & 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 } 22653541Sshin t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 227180427Sbz lport, wild, cred); 228189848Srwatson if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ? 229171260Sdelphij intotw(t)->tw_so_options : 230116453Scognet t->inp_socket->so_options)) == 0) 231120856Sume return (EADDRINUSE); 23278064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 23355679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 23455679Sshin struct sockaddr_in sin; 23555679Sshin 23655679Sshin in6_sin6_2_sin(&sin, sin6); 23755679Sshin t = in_pcblookup_local(pcbinfo, sin.sin_addr, 238180427Sbz lport, wild, cred); 239189848Srwatson if (t && t->inp_flags & INP_TIMEWAIT) { 240171260Sdelphij if ((reuseport & 241116453Scognet intotw(t)->tw_so_options) == 0 && 242116453Scognet (ntohl(t->inp_laddr.s_addr) != 243171260Sdelphij INADDR_ANY || ((inp->inp_vflag & 244171260Sdelphij INP_IPV6PROTO) == 245116453Scognet (t->inp_vflag & INP_IPV6PROTO)))) 246116453Scognet return (EADDRINUSE); 247116453Scognet } 248171260Sdelphij else if (t && 249171260Sdelphij (reuseport & t->inp_socket->so_options) 250171260Sdelphij == 0 && (ntohl(t->inp_laddr.s_addr) != 251116453Scognet INADDR_ANY || INP_SOCKAF(so) == 25297658Stanimura INP_SOCKAF(t->inp_socket))) 25397658Stanimura return (EADDRINUSE); 25455679Sshin } 25553541Sshin } 25653541Sshin inp->in6p_laddr = sin6->sin6_addr; 25753541Sshin } 25853541Sshin if (lport == 0) { 259219570Sbz if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) { 260219570Sbz /* Undo an address bind that may have occurred. */ 261219570Sbz inp->in6p_laddr = in6addr_any; 262188144Sjamie return (error); 263219570Sbz } 264183611Sbz } else { 26562587Sitojun inp->inp_lport = lport; 26662587Sitojun if (in_pcbinshash(inp) != 0) { 26762587Sitojun inp->in6p_laddr = in6addr_any; 26862587Sitojun inp->inp_lport = 0; 26962587Sitojun return (EAGAIN); 27053541Sshin } 27153541Sshin } 272120856Sume return (0); 27353541Sshin} 27453541Sshin 27553541Sshin/* 27653541Sshin * Transform old in6_pcbconnect() into an inner subroutine for new 27753541Sshin * in6_pcbconnect(): Do some validity-checking on the remote 27853541Sshin * address (in mbuf 'nam') and then determine local host address 27953541Sshin * (i.e., which interface) to use to access that remote host. 28053541Sshin * 28153541Sshin * This preserves definition of in6_pcbconnect(), while supporting a 28253541Sshin * slightly different version for T/TCP. (This is more than 28353541Sshin * a bit of a kludge, but cleaning up the internal interfaces would 28453541Sshin * have forced minor changes in every protocol). 28553541Sshin */ 28653541Sshinint 287171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam, 288194777Sbz struct in6_addr *plocal_addr6) 28953541Sshin{ 29053541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 291148385Sume int error = 0; 29253541Sshin struct ifnet *ifp = NULL; 293148385Sume int scope_ambiguous = 0; 294194777Sbz struct in6_addr in6a; 29553541Sshin 296158011Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 297178285Srwatson INP_WLOCK_ASSERT(inp); 298158011Srwatson 29953541Sshin if (nam->sa_len != sizeof (*sin6)) 30053541Sshin return (EINVAL); 30153541Sshin if (sin6->sin6_family != AF_INET6) 30253541Sshin return (EAFNOSUPPORT); 30353541Sshin if (sin6->sin6_port == 0) 30453541Sshin return (EADDRNOTAVAIL); 30553541Sshin 306181803Sbz if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 307148385Sume scope_ambiguous = 1; 308181803Sbz if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 309148385Sume return(error); 310148385Sume 311194907Srwatson if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) { 31253541Sshin /* 31353541Sshin * If the destination address is UNSPECIFIED addr, 31453541Sshin * use the loopback addr, e.g ::1. 31553541Sshin */ 31653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 31753541Sshin sin6->sin6_addr = in6addr_loopback; 31853541Sshin } 319188144Sjamie if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0) 320188144Sjamie return (error); 321148385Sume 322194777Sbz error = in6_selectsrc(sin6, inp->in6p_outputopts, 323194777Sbz inp, NULL, inp->inp_cred, &ifp, &in6a); 324194777Sbz if (error) 325194777Sbz return (error); 326194777Sbz 327148385Sume if (ifp && scope_ambiguous && 328148385Sume (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { 329148385Sume return(error); 33053541Sshin } 331148385Sume 332148385Sume /* 333194777Sbz * Do not update this earlier, in case we return with an error. 334194777Sbz * 335194777Sbz * XXX: this in6_selectsrc result might replace the bound local 336202915Sbz * address with the address specified by setsockopt(IPV6_PKTINFO). 337194777Sbz * Is it the intended behavior? 338194777Sbz */ 339194777Sbz *plocal_addr6 = in6a; 340194777Sbz 341194777Sbz /* 342148385Sume * Don't do pcblookup call here; return interface in 343148385Sume * plocal_addr6 344148385Sume * and exit to caller, that will do the lookup. 345148385Sume */ 346148385Sume 347120856Sume return (0); 34853541Sshin} 34953541Sshin 35053541Sshin/* 35153541Sshin * Outer subroutine: 35253541Sshin * Connect from a socket to a specified address. 35353541Sshin * Both address and port must be specified in argument sin. 35453541Sshin * If don't have a local address for this socket yet, 35553541Sshin * then pick one. 35653541Sshin */ 35753541Sshinint 358171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam, 359171259Sdelphij struct ucred *cred) 36053541Sshin{ 36153541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 362194777Sbz struct in6_addr addr6; 36353541Sshin int error; 36453541Sshin 365132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 366178285Srwatson INP_WLOCK_ASSERT(inp); 367132714Srwatson 36853541Sshin /* 36995023Ssuz * Call inner routine, to assign local interface address. 37095023Ssuz * in6_pcbladdr() may automatically fill in sin6_scope_id. 37153541Sshin */ 37253541Sshin if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 373120856Sume return (error); 37453541Sshin 37553541Sshin if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 37653541Sshin sin6->sin6_port, 37753541Sshin IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 378194777Sbz ? &addr6 : &inp->in6p_laddr, 37953541Sshin inp->inp_lport, 0, NULL) != NULL) { 38053541Sshin return (EADDRINUSE); 38153541Sshin } 38253541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 38353541Sshin if (inp->inp_lport == 0) { 384127505Spjd error = in6_pcbbind(inp, (struct sockaddr *)0, cred); 38553541Sshin if (error) 38653541Sshin return (error); 38753541Sshin } 388194777Sbz inp->in6p_laddr = addr6; 38953541Sshin } 39053541Sshin inp->in6p_faddr = sin6->sin6_addr; 39153541Sshin inp->inp_fport = sin6->sin6_port; 39278064Sume /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 393186141Sbz inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 394186141Sbz if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 395186141Sbz inp->inp_flow |= 396120649Sume (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 39753541Sshin 39853541Sshin in_pcbrehash(inp); 399171133Sgnn 40053541Sshin return (0); 40153541Sshin} 40253541Sshin 40353541Sshinvoid 404171259Sdelphijin6_pcbdisconnect(struct inpcb *inp) 40553541Sshin{ 406132714Srwatson 407132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 408178285Srwatson INP_WLOCK_ASSERT(inp); 409132714Srwatson 41053541Sshin bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 41153541Sshin inp->inp_fport = 0; 41278064Sume /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 413186141Sbz inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 41453541Sshin in_pcbrehash(inp); 41553541Sshin} 41653541Sshin 417102218Struckmanstruct sockaddr * 418171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p) 419102218Struckman{ 420102218Struckman struct sockaddr_in6 *sin6; 421102218Struckman 422184205Sdes sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK); 423102218Struckman bzero(sin6, sizeof *sin6); 424102218Struckman sin6->sin6_family = AF_INET6; 425102218Struckman sin6->sin6_len = sizeof(*sin6); 426102218Struckman sin6->sin6_port = port; 427102218Struckman sin6->sin6_addr = *addr_p; 428148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 429102218Struckman 430102218Struckman return (struct sockaddr *)sin6; 431102218Struckman} 432102218Struckman 433102218Struckmanstruct sockaddr * 434171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 435102218Struckman{ 436102218Struckman struct sockaddr_in sin; 437102218Struckman struct sockaddr_in6 *sin6_p; 438102218Struckman 439102218Struckman bzero(&sin, sizeof sin); 440102218Struckman sin.sin_family = AF_INET; 441102218Struckman sin.sin_len = sizeof(sin); 442102218Struckman sin.sin_port = port; 443102218Struckman sin.sin_addr = *addr_p; 444102218Struckman 445184205Sdes sin6_p = malloc(sizeof *sin6_p, M_SONAME, 446111119Simp M_WAITOK); 447102218Struckman in6_sin_2_v4mapsin6(&sin, sin6_p); 448102218Struckman 449102218Struckman return (struct sockaddr *)sin6_p; 450102218Struckman} 451102218Struckman 45253541Sshinint 453171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam) 45453541Sshin{ 45553541Sshin register struct inpcb *inp; 456102218Struckman struct in6_addr addr; 457102218Struckman in_port_t port; 45853541Sshin 45953541Sshin inp = sotoinpcb(so); 460169462Srwatson KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 461157673Srwatson 462178320Srwatson INP_RLOCK(inp); 463102218Struckman port = inp->inp_lport; 464102218Struckman addr = inp->in6p_laddr; 465178320Srwatson INP_RUNLOCK(inp); 46653541Sshin 467102218Struckman *nam = in6_sockaddr(port, &addr); 46853541Sshin return 0; 46953541Sshin} 47053541Sshin 47153541Sshinint 472171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam) 47353541Sshin{ 47453541Sshin struct inpcb *inp; 475102218Struckman struct in6_addr addr; 476102218Struckman in_port_t port; 47753541Sshin 47853541Sshin inp = sotoinpcb(so); 479169462Srwatson KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 480157673Srwatson 481178320Srwatson INP_RLOCK(inp); 482102218Struckman port = inp->inp_fport; 483102218Struckman addr = inp->in6p_faddr; 484178320Srwatson INP_RUNLOCK(inp); 48553541Sshin 486102218Struckman *nam = in6_sockaddr(port, &addr); 48753541Sshin return 0; 48853541Sshin} 48953541Sshin 49053541Sshinint 49153541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 49253541Sshin{ 493157673Srwatson struct inpcb *inp; 49453541Sshin int error; 49553541Sshin 496157673Srwatson inp = sotoinpcb(so); 497157673Srwatson KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 498157673Srwatson 499124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 500169462Srwatson error = in_getsockaddr(so, nam); 50154952Seivind if (error == 0) 50253541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 503120913Sume } else { 504169462Srwatson /* scope issues will be handled in in6_getsockaddr(). */ 505169462Srwatson error = in6_getsockaddr(so, nam); 506120913Sume } 50753541Sshin 50853541Sshin return error; 50953541Sshin} 51053541Sshin 51153541Sshinint 51253541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 51353541Sshin{ 514157673Srwatson struct inpcb *inp; 51553541Sshin int error; 51653541Sshin 517157673Srwatson inp = sotoinpcb(so); 518157673Srwatson KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 519157673Srwatson 520124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 521169462Srwatson error = in_getpeeraddr(so, nam); 52254952Seivind if (error == 0) 52353541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 52453541Sshin } else 525169462Srwatson /* scope issues will be handled in in6_getpeeraddr(). */ 526169462Srwatson error = in6_getpeeraddr(so, nam); 52753541Sshin 52853541Sshin return error; 52953541Sshin} 53053541Sshin 53153541Sshin/* 53253541Sshin * Pass some notification to all connections of a protocol 53353541Sshin * associated with address dst. The local address and/or port numbers 53453541Sshin * may be specified to limit the search. The "usual action" will be 53553541Sshin * taken, depending on the ctlinput cmd. The caller must filter any 53653541Sshin * cmds that are uninteresting (e.g., no error in the map). 53753541Sshin * Call the protocol specific routine (if any) to report 53853541Sshin * any errors for each matching socket. 53953541Sshin */ 54053541Sshinvoid 541171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, 542171259Sdelphij u_int fport_arg, const struct sockaddr *src, u_int lport_arg, 543171259Sdelphij int cmd, void *cmdarg, 544175162Sobrien struct inpcb *(*notify)(struct inpcb *, int)) 54553541Sshin{ 546177961Srwatson struct inpcb *inp, *inp_temp; 54778064Sume struct sockaddr_in6 sa6_src, *sa6_dst; 54853541Sshin u_short fport = fport_arg, lport = lport_arg; 54978064Sume u_int32_t flowinfo; 550157673Srwatson int errno; 55153541Sshin 552119995Sru if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 55353541Sshin return; 55478064Sume 55578064Sume sa6_dst = (struct sockaddr_in6 *)dst; 55678064Sume if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 55753541Sshin return; 55853541Sshin 55953541Sshin /* 56078064Sume * note that src can be NULL when we get notify by local fragmentation. 56178064Sume */ 56291346Salfred sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 56378064Sume flowinfo = sa6_src.sin6_flowinfo; 56478064Sume 56578064Sume /* 56653541Sshin * Redirects go to all references to the destination, 56762587Sitojun * and use in6_rtchange to invalidate the route cache. 56862587Sitojun * Dead host indications: also use in6_rtchange to invalidate 56962587Sitojun * the cache, and deliver the error to all the sockets. 57053541Sshin * Otherwise, if we have knowledge of the local port and address, 57153541Sshin * deliver only to that socket. 57253541Sshin */ 57353541Sshin if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 57453541Sshin fport = 0; 57553541Sshin lport = 0; 57678064Sume bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 57762587Sitojun 57878064Sume if (cmd != PRC_HOSTDEAD) 57978064Sume notify = in6_rtchange; 58053541Sshin } 58153541Sshin errno = inet6ctlerrmap[cmd]; 582133192Srwatson INP_INFO_WLOCK(pcbinfo); 583177961Srwatson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 584178285Srwatson INP_WLOCK(inp); 585171260Sdelphij if ((inp->inp_vflag & INP_IPV6) == 0) { 586178285Srwatson INP_WUNLOCK(inp); 58753541Sshin continue; 588133192Srwatson } 58962587Sitojun 59078064Sume /* 591125776Sume * If the error designates a new path MTU for a destination 592125776Sume * and the application (associated with this socket) wanted to 593125776Sume * know the value, notify. Note that we notify for all 594125776Sume * disconnected sockets if the corresponding application 595125776Sume * wanted. This is because some UDP applications keep sending 596125776Sume * sockets disconnected. 597125776Sume * XXX: should we avoid to notify the value to TCP sockets? 598125776Sume */ 599125776Sume if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 600125776Sume (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 601125776Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 602125776Sume ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 603125776Sume (u_int32_t *)cmdarg); 604125776Sume } 605125776Sume 606125776Sume /* 60778064Sume * Detect if we should notify the error. If no source and 60878064Sume * destination ports are specifed, but non-zero flowinfo and 60978064Sume * local address match, notify the error. This is the case 61078064Sume * when the error is delivered with an encrypted buffer 61178064Sume * by ESP. Otherwise, just compare addresses and ports 61278064Sume * as usual. 61378064Sume */ 61478064Sume if (lport == 0 && fport == 0 && flowinfo && 61578064Sume inp->inp_socket != NULL && 616186141Sbz flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) && 61778064Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 61878064Sume goto do_notify; 61978064Sume else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 62078064Sume &sa6_dst->sin6_addr) || 62178064Sume inp->inp_socket == 0 || 62278064Sume (lport && inp->inp_lport != lport) || 62378064Sume (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 62478064Sume !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 62578064Sume &sa6_src.sin6_addr)) || 626133192Srwatson (fport && inp->inp_fport != fport)) { 627178285Srwatson INP_WUNLOCK(inp); 62853541Sshin continue; 629133192Srwatson } 63062587Sitojun 63178064Sume do_notify: 632134121Srwatson if (notify) { 633134121Srwatson if ((*notify)(inp, errno)) 634178285Srwatson INP_WUNLOCK(inp); 635134121Srwatson } else 636178285Srwatson INP_WUNLOCK(inp); 63753541Sshin } 638133192Srwatson INP_INFO_WUNLOCK(pcbinfo); 63953541Sshin} 64053541Sshin 64153541Sshin/* 64253541Sshin * Lookup a PCB based on the local address and port. 64353541Sshin */ 64453541Sshinstruct inpcb * 645171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 646180427Sbz u_short lport, int wild_okay, struct ucred *cred) 64753541Sshin{ 64853541Sshin register struct inpcb *inp; 64953541Sshin int matchwild = 3, wildcard; 65053541Sshin 651158011Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 652158011Srwatson 65353541Sshin if (!wild_okay) { 65453541Sshin struct inpcbhead *head; 65553541Sshin /* 65653541Sshin * Look for an unconnected (wildcard foreign addr) PCB that 65753541Sshin * matches the local address and port we're looking for. 65853541Sshin */ 659169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 660169154Srwatson 0, pcbinfo->ipi_hashmask)]; 66153541Sshin LIST_FOREACH(inp, head, inp_hash) { 662185435Sbz /* XXX inp locking */ 66354952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 66453541Sshin continue; 66553541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 66653541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 66753541Sshin inp->inp_lport == lport) { 668185435Sbz /* Found. */ 669185435Sbz if (cred == NULL || 670192895Sjamie prison_equal_ip6(cred->cr_prison, 671192895Sjamie inp->inp_cred->cr_prison)) 672185435Sbz return (inp); 67353541Sshin } 67453541Sshin } 67553541Sshin /* 67653541Sshin * Not found. 67753541Sshin */ 67853541Sshin return (NULL); 67953541Sshin } else { 68053541Sshin struct inpcbporthead *porthash; 68153541Sshin struct inpcbport *phd; 68253541Sshin struct inpcb *match = NULL; 68353541Sshin /* 68453541Sshin * Best fit PCB lookup. 68553541Sshin * 68653541Sshin * First see if this local port is in use by looking on the 68753541Sshin * port hash list. 68853541Sshin */ 689169154Srwatson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 690169154Srwatson pcbinfo->ipi_porthashmask)]; 69153541Sshin LIST_FOREACH(phd, porthash, phd_hash) { 69253541Sshin if (phd->phd_port == lport) 69353541Sshin break; 69453541Sshin } 69553541Sshin if (phd != NULL) { 69653541Sshin /* 69753541Sshin * Port is in use by one or more PCBs. Look for best 69853541Sshin * fit. 69953541Sshin */ 70053541Sshin LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 70153541Sshin wildcard = 0; 702185435Sbz if (cred != NULL && 703192895Sjamie !prison_equal_ip6(cred->cr_prison, 704192895Sjamie inp->inp_cred->cr_prison)) 705185435Sbz continue; 706185435Sbz /* XXX inp locking */ 70754952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 70853541Sshin continue; 70953541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 71053541Sshin wildcard++; 71153541Sshin if (!IN6_IS_ADDR_UNSPECIFIED( 71253541Sshin &inp->in6p_laddr)) { 71353541Sshin if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 71453541Sshin wildcard++; 71553541Sshin else if (!IN6_ARE_ADDR_EQUAL( 716185435Sbz &inp->in6p_laddr, laddr)) 71753541Sshin continue; 71853541Sshin } else { 71953541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 72053541Sshin wildcard++; 72153541Sshin } 72253541Sshin if (wildcard < matchwild) { 72353541Sshin match = inp; 72453541Sshin matchwild = wildcard; 725185435Sbz if (matchwild == 0) 72653541Sshin break; 72753541Sshin } 72853541Sshin } 72953541Sshin } 73053541Sshin return (match); 73153541Sshin } 73253541Sshin} 73353541Sshin 73481127Sumevoid 735171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 73681127Sume{ 737186141Sbz struct inpcb *in6p; 73881127Sume struct ip6_moptions *im6o; 739191672Sbms int i, gap; 74081127Sume 741157978Srwatson INP_INFO_RLOCK(pcbinfo); 742169154Srwatson LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) { 743178285Srwatson INP_WLOCK(in6p); 74481127Sume im6o = in6p->in6p_moptions; 745191672Sbms if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) { 74681127Sume /* 747191672Sbms * Unselect the outgoing ifp for multicast if it 748191672Sbms * is being detached. 74981127Sume */ 75081127Sume if (im6o->im6o_multicast_ifp == ifp) 75181127Sume im6o->im6o_multicast_ifp = NULL; 75281127Sume /* 75381127Sume * Drop multicast group membership if we joined 75481127Sume * through the interface being detached. 75581127Sume */ 756191672Sbms gap = 0; 757191672Sbms for (i = 0; i < im6o->im6o_num_memberships; i++) { 758191672Sbms if (im6o->im6o_membership[i]->in6m_ifp == 759191672Sbms ifp) { 760191672Sbms in6_mc_leave(im6o->im6o_membership[i], 761191672Sbms NULL); 762191672Sbms gap++; 763191672Sbms } else if (gap != 0) { 764191672Sbms im6o->im6o_membership[i - gap] = 765191672Sbms im6o->im6o_membership[i]; 76681127Sume } 76781127Sume } 768191672Sbms im6o->im6o_num_memberships -= gap; 76981127Sume } 770178285Srwatson INP_WUNLOCK(in6p); 77181127Sume } 772157978Srwatson INP_INFO_RUNLOCK(pcbinfo); 77381127Sume} 77481127Sume 77553541Sshin/* 77653541Sshin * Check for alternatives when higher level complains 77753541Sshin * about service problems. For now, invalidate cached 77853541Sshin * routing information. If the route was created dynamically 77953541Sshin * (by a redirect), time to try a default gateway again. 78053541Sshin */ 78153541Sshinvoid 782171259Sdelphijin6_losing(struct inpcb *in6p) 78353541Sshin{ 784171259Sdelphij 785122922Sandre /* 786122922Sandre * We don't store route pointers in the routing table anymore 787122922Sandre */ 788122922Sandre return; 78953541Sshin} 79053541Sshin 79153541Sshin/* 79253541Sshin * After a routing change, flush old routing 79353541Sshin * and allocate a (hopefully) better one. 79453541Sshin */ 79598211Shsustruct inpcb * 796171259Sdelphijin6_rtchange(struct inpcb *inp, int errno) 79753541Sshin{ 798122922Sandre /* 799122922Sandre * We don't store route pointers in the routing table anymore 800122922Sandre */ 80198211Shsu return inp; 80253541Sshin} 80353541Sshin 80453541Sshin/* 80553541Sshin * Lookup PCB in hash list. 80653541Sshin */ 80753541Sshinstruct inpcb * 808171259Sdelphijin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 809185435Sbz u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard, 810185435Sbz struct ifnet *ifp) 81153541Sshin{ 81253541Sshin struct inpcbhead *head; 813185435Sbz struct inpcb *inp, *tmpinp; 81453541Sshin u_short fport = fport_arg, lport = lport_arg; 81578064Sume int faith; 81653541Sshin 817178285Srwatson INP_INFO_LOCK_ASSERT(pcbinfo); 818158011Srwatson 81983934Sbrooks if (faithprefix_p != NULL) 82083934Sbrooks faith = (*faithprefix_p)(laddr); 82183934Sbrooks else 82283934Sbrooks faith = 0; 82378064Sume 82453541Sshin /* 82553541Sshin * First look for an exact match. 82653541Sshin */ 827185435Sbz tmpinp = NULL; 828169154Srwatson head = &pcbinfo->ipi_hashbase[ 829169154Srwatson INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport, 830169154Srwatson pcbinfo->ipi_hashmask)]; 83154263Sshin LIST_FOREACH(inp, head, inp_hash) { 832185435Sbz /* XXX inp locking */ 83354952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 83453541Sshin continue; 83553541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 83653541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 83753541Sshin inp->inp_fport == fport && 83853541Sshin inp->inp_lport == lport) { 83953541Sshin /* 840185435Sbz * XXX We should be able to directly return 841185435Sbz * the inp here, without any checks. 842185435Sbz * Well unless both bound with SO_REUSEPORT? 84353541Sshin */ 844192895Sjamie if (prison_flag(inp->inp_cred, PR_IP6)) 845185435Sbz return (inp); 846185435Sbz if (tmpinp == NULL) 847185435Sbz tmpinp = inp; 84853541Sshin } 84953541Sshin } 850185435Sbz if (tmpinp != NULL) 851185435Sbz return (tmpinp); 85253541Sshin 853185435Sbz /* 854185435Sbz * Then look for a wildcard match, if requested. 855185435Sbz */ 856185435Sbz if (wildcard == INPLOOKUP_WILDCARD) { 857185435Sbz struct inpcb *local_wild = NULL, *local_exact = NULL; 858185435Sbz struct inpcb *jail_wild = NULL; 859185435Sbz int injail; 860185435Sbz 861185435Sbz /* 862185435Sbz * Order of socket selection - we always prefer jails. 863185435Sbz * 1. jailed, non-wild. 864185435Sbz * 2. jailed, wild. 865185435Sbz * 3. non-jailed, non-wild. 866185435Sbz * 4. non-jailed, wild. 867185435Sbz */ 868169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 869169154Srwatson 0, pcbinfo->ipi_hashmask)]; 87054263Sshin LIST_FOREACH(inp, head, inp_hash) { 871185435Sbz /* XXX inp locking */ 87254952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 87353541Sshin continue; 874185435Sbz 875185435Sbz if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 876185435Sbz inp->inp_lport != lport) { 877185435Sbz continue; 878185435Sbz } 879185435Sbz 880185435Sbz /* XXX inp locking */ 881185435Sbz if (faith && (inp->inp_flags & INP_FAITH) == 0) 882185435Sbz continue; 883185435Sbz 884192895Sjamie injail = prison_flag(inp->inp_cred, PR_IP6); 885185435Sbz if (injail) { 886188144Sjamie if (prison_check_ip6(inp->inp_cred, 887188144Sjamie laddr) != 0) 88853541Sshin continue; 889185435Sbz } else { 890185435Sbz if (local_exact != NULL) 891185435Sbz continue; 892185435Sbz } 893185435Sbz 894185435Sbz if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) { 895185435Sbz if (injail) 89653541Sshin return (inp); 897185435Sbz else 898185435Sbz local_exact = inp; 899185435Sbz } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 900185435Sbz if (injail) 901185435Sbz jail_wild = inp; 902185435Sbz else 90353541Sshin local_wild = inp; 90453541Sshin } 905185435Sbz } /* LIST_FOREACH */ 90653541Sshin 907185435Sbz if (jail_wild != NULL) 908185435Sbz return (jail_wild); 909185435Sbz if (local_exact != NULL) 910185435Sbz return (local_exact); 911185435Sbz if (local_wild != NULL) 912185435Sbz return (local_wild); 913185435Sbz } /* if (wildcard == INPLOOKUP_WILDCARD) */ 914185435Sbz 91553541Sshin /* 91653541Sshin * Not found. 91753541Sshin */ 91853541Sshin return (NULL); 91953541Sshin} 92053541Sshin 92153541Sshinvoid 92253541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 92353541Sshin{ 92453541Sshin struct ip6_hdr *ip; 92553541Sshin 92653541Sshin ip = mtod(m, struct ip6_hdr *); 92753541Sshin bzero(sin6, sizeof(*sin6)); 92853541Sshin sin6->sin6_len = sizeof(*sin6); 92953541Sshin sin6->sin6_family = AF_INET6; 93053541Sshin sin6->sin6_addr = ip->ip6_src; 93153541Sshin 932148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 933148385Sume 93453541Sshin return; 93553541Sshin} 936