in6_pcb.c revision 171260
162587Sitojun/* $FreeBSD: head/sys/netinet6/in6_pcb.c 171260 2007-07-05 16:29:40Z delphij $ */ 278064Sume/* $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ */ 3171260Sdelphij 4139826Simp/*- 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin * 3253541Sshin */ 3353541Sshin 34139826Simp/*- 3553541Sshin * Copyright (c) 1982, 1986, 1991, 1993 3653541Sshin * The Regents of the University of California. All rights reserved. 3753541Sshin * 3853541Sshin * Redistribution and use in source and binary forms, with or without 3953541Sshin * modification, are permitted provided that the following conditions 4053541Sshin * are met: 4153541Sshin * 1. Redistributions of source code must retain the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer. 4353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4453541Sshin * notice, this list of conditions and the following disclaimer in the 4553541Sshin * documentation and/or other materials provided with the distribution. 4653541Sshin * 4. Neither the name of the University nor the names of its contributors 4753541Sshin * may be used to endorse or promote products derived from this software 4853541Sshin * without specific prior written permission. 4953541Sshin * 5053541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6053541Sshin * SUCH DAMAGE. 6153541Sshin * 6253541Sshin * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 6353541Sshin */ 6453541Sshin 6578064Sume#include "opt_inet.h" 6678064Sume#include "opt_inet6.h" 6755009Sshin#include "opt_ipsec.h" 6853541Sshin 6953541Sshin#include <sys/param.h> 7053541Sshin#include <sys/systm.h> 7153541Sshin#include <sys/malloc.h> 7253541Sshin#include <sys/mbuf.h> 7355679Sshin#include <sys/domain.h> 7453541Sshin#include <sys/protosw.h> 7553541Sshin#include <sys/socket.h> 7653541Sshin#include <sys/socketvar.h> 7753541Sshin#include <sys/sockio.h> 7853541Sshin#include <sys/errno.h> 7953541Sshin#include <sys/time.h> 80164033Srwatson#include <sys/priv.h> 8153541Sshin#include <sys/proc.h> 8253541Sshin#include <sys/jail.h> 8353541Sshin 8492767Sjeff#include <vm/uma.h> 8553541Sshin 8653541Sshin#include <net/if.h> 8753541Sshin#include <net/if_types.h> 8853541Sshin#include <net/route.h> 8953541Sshin 9053541Sshin#include <netinet/in.h> 9153541Sshin#include <netinet/in_var.h> 9253541Sshin#include <netinet/in_systm.h> 9398102Shsu#include <netinet/tcp_var.h> 9462587Sitojun#include <netinet/ip6.h> 9555679Sshin#include <netinet/ip_var.h> 9653541Sshin#include <netinet6/ip6_var.h> 9753541Sshin#include <netinet6/nd6.h> 9853541Sshin#include <netinet/in_pcb.h> 9953541Sshin#include <netinet6/in6_pcb.h> 100148385Sume#include <netinet6/scope6_var.h> 10153541Sshin 102171167Sgnn#ifdef IPSEC 103105199Ssam#include <netipsec/ipsec.h> 104105199Ssam#include <netipsec/ipsec6.h> 105105199Ssam#include <netipsec/key.h> 106171167Sgnn#endif /* IPSEC */ 107105199Ssam 10853541Sshinstruct in6_addr zeroin6_addr; 10953541Sshin 11053541Sshinint 111171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam, 112171259Sdelphij struct ucred *cred) 11353541Sshin{ 11453541Sshin struct socket *so = inp->inp_socket; 11553541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 11653541Sshin struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 11753541Sshin u_short lport = 0; 11897658Stanimura int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 11953541Sshin 120132714Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 121132714Srwatson INP_LOCK_ASSERT(inp); 122132714Srwatson 12353541Sshin if (!in6_ifaddr) /* XXX broken! */ 12453541Sshin return (EADDRNOTAVAIL); 12553541Sshin if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 126120856Sume return (EINVAL); 12753541Sshin if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 128160024Sbz wild = INPLOOKUP_WILDCARD; 12953541Sshin if (nam) { 130148385Sume int error; 131148385Sume 13253541Sshin sin6 = (struct sockaddr_in6 *)nam; 13353541Sshin if (nam->sa_len != sizeof(*sin6)) 134120856Sume return (EINVAL); 13553541Sshin /* 13653541Sshin * family check. 13753541Sshin */ 13853541Sshin if (nam->sa_family != AF_INET6) 139120856Sume return (EAFNOSUPPORT); 14053541Sshin 141148385Sume if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) 142148385Sume return(error); 14353541Sshin 14453541Sshin lport = sin6->sin6_port; 14553541Sshin if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 14653541Sshin /* 14753541Sshin * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 14853541Sshin * allow compepte duplication of binding if 14953541Sshin * SO_REUSEPORT is set, or if SO_REUSEADDR is set 15053541Sshin * and a multicast address is bound on both 15153541Sshin * new and duplicated sockets. 15253541Sshin */ 15353541Sshin if (so->so_options & SO_REUSEADDR) 15453541Sshin reuseport = SO_REUSEADDR|SO_REUSEPORT; 15553541Sshin } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 15653541Sshin struct ifaddr *ia = NULL; 15753541Sshin 15853541Sshin sin6->sin6_port = 0; /* yech... */ 15953541Sshin if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 160120856Sume return (EADDRNOTAVAIL); 16153541Sshin 16253541Sshin /* 16353541Sshin * XXX: bind to an anycast address might accidentally 16453541Sshin * cause sending a packet with anycast source address. 16578064Sume * We should allow to bind to a deprecated address, since 166120913Sume * the application dares to use it. 16753541Sshin */ 16853541Sshin if (ia && 16953541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 17078064Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 171120856Sume return (EADDRNOTAVAIL); 17253541Sshin } 17353541Sshin } 17453541Sshin if (lport) { 17553541Sshin struct inpcb *t; 17653541Sshin 17753541Sshin /* GROSS */ 178156877Sdwmalone if (ntohs(lport) <= ipport_reservedhigh && 179156877Sdwmalone ntohs(lport) >= ipport_reservedlow && 180164033Srwatson priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 181170587Srwatson 0)) 182120856Sume return (EACCES); 183159976Spjd if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 184168932Srwatson priv_check_cred(so->so_cred, 185170587Srwatson PRIV_NETINET_REUSEPORT, 0) != 0) { 18655679Sshin t = in6_pcblookup_local(pcbinfo, 18753541Sshin &sin6->sin6_addr, lport, 18853541Sshin INPLOOKUP_WILDCARD); 189132699Syar if (t && 190132699Syar ((t->inp_vflag & INP_TIMEWAIT) == 0) && 191132699Syar (so->so_type != SOCK_STREAM || 192132699Syar IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 19397658Stanimura (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 194171260Sdelphij !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 195171260Sdelphij (t->inp_socket->so_options & SO_REUSEPORT) 196116453Scognet == 0) && (so->so_cred->cr_uid != 19797658Stanimura t->inp_socket->so_cred->cr_uid)) 19897658Stanimura return (EADDRINUSE); 19978064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 20055679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 20155679Sshin struct sockaddr_in sin; 20255679Sshin 20355679Sshin in6_sin6_2_sin(&sin, sin6); 20455679Sshin t = in_pcblookup_local(pcbinfo, 20555679Sshin sin.sin_addr, lport, 20655679Sshin INPLOOKUP_WILDCARD); 207132699Syar if (t && 208132699Syar ((t->inp_vflag & 209132699Syar INP_TIMEWAIT) == 0) && 210132699Syar (so->so_type != SOCK_STREAM || 211132699Syar ntohl(t->inp_faddr.s_addr) == 212132699Syar INADDR_ANY) && 21355679Sshin (so->so_cred->cr_uid != 214132794Syar t->inp_socket->so_cred->cr_uid)) 21555679Sshin return (EADDRINUSE); 21655679Sshin } 21753541Sshin } 21853541Sshin t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 21953541Sshin lport, wild); 220116453Scognet if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ? 221171260Sdelphij intotw(t)->tw_so_options : 222116453Scognet t->inp_socket->so_options)) == 0) 223120856Sume return (EADDRINUSE); 22478064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 22555679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 22655679Sshin struct sockaddr_in sin; 22755679Sshin 22855679Sshin in6_sin6_2_sin(&sin, sin6); 22955679Sshin t = in_pcblookup_local(pcbinfo, sin.sin_addr, 23055679Sshin lport, wild); 231116453Scognet if (t && t->inp_vflag & INP_TIMEWAIT) { 232171260Sdelphij if ((reuseport & 233116453Scognet intotw(t)->tw_so_options) == 0 && 234116453Scognet (ntohl(t->inp_laddr.s_addr) != 235171260Sdelphij INADDR_ANY || ((inp->inp_vflag & 236171260Sdelphij INP_IPV6PROTO) == 237116453Scognet (t->inp_vflag & INP_IPV6PROTO)))) 238116453Scognet return (EADDRINUSE); 239116453Scognet } 240171260Sdelphij else if (t && 241171260Sdelphij (reuseport & t->inp_socket->so_options) 242171260Sdelphij == 0 && (ntohl(t->inp_laddr.s_addr) != 243116453Scognet INADDR_ANY || INP_SOCKAF(so) == 24497658Stanimura INP_SOCKAF(t->inp_socket))) 24597658Stanimura return (EADDRINUSE); 24655679Sshin } 24753541Sshin } 24853541Sshin inp->in6p_laddr = sin6->sin6_addr; 24953541Sshin } 25053541Sshin if (lport == 0) { 25162587Sitojun int e; 252127505Spjd if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) 253120856Sume return (e); 25462587Sitojun } 25562587Sitojun else { 25662587Sitojun inp->inp_lport = lport; 25762587Sitojun if (in_pcbinshash(inp) != 0) { 25862587Sitojun inp->in6p_laddr = in6addr_any; 25962587Sitojun inp->inp_lport = 0; 26062587Sitojun return (EAGAIN); 26153541Sshin } 26253541Sshin } 263120856Sume return (0); 26453541Sshin} 26553541Sshin 26653541Sshin/* 26753541Sshin * Transform old in6_pcbconnect() into an inner subroutine for new 26853541Sshin * in6_pcbconnect(): Do some validity-checking on the remote 26953541Sshin * address (in mbuf 'nam') and then determine local host address 27053541Sshin * (i.e., which interface) to use to access that remote host. 27153541Sshin * 27253541Sshin * This preserves definition of in6_pcbconnect(), while supporting a 27353541Sshin * slightly different version for T/TCP. (This is more than 27453541Sshin * a bit of a kludge, but cleaning up the internal interfaces would 27553541Sshin * have forced minor changes in every protocol). 27653541Sshin */ 27753541Sshinint 278171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam, 279171259Sdelphij struct in6_addr **plocal_addr6) 28053541Sshin{ 28153541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 282148385Sume int error = 0; 28353541Sshin struct ifnet *ifp = NULL; 284148385Sume int scope_ambiguous = 0; 28553541Sshin 286158011Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 287158011Srwatson INP_LOCK_ASSERT(inp); 288158011Srwatson 28953541Sshin if (nam->sa_len != sizeof (*sin6)) 29053541Sshin return (EINVAL); 29153541Sshin if (sin6->sin6_family != AF_INET6) 29253541Sshin return (EAFNOSUPPORT); 29353541Sshin if (sin6->sin6_port == 0) 29453541Sshin return (EADDRNOTAVAIL); 29553541Sshin 296148385Sume if (sin6->sin6_scope_id == 0 && !ip6_use_defzone) 297148385Sume scope_ambiguous = 1; 298148385Sume if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) 299148385Sume return(error); 300148385Sume 30153541Sshin if (in6_ifaddr) { 30253541Sshin /* 30353541Sshin * If the destination address is UNSPECIFIED addr, 30453541Sshin * use the loopback addr, e.g ::1. 30553541Sshin */ 30653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 30753541Sshin sin6->sin6_addr = in6addr_loopback; 30853541Sshin } 309148385Sume 310148385Sume /* 311148385Sume * XXX: in6_selectsrc might replace the bound local address 312148385Sume * with the address specified by setsockopt(IPV6_PKTINFO). 313148385Sume * Is it the intended behavior? 314148385Sume */ 315148385Sume *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 316148385Sume inp->in6p_moptions, NULL, 317148385Sume &inp->in6p_laddr, &ifp, &error); 318148385Sume if (ifp && scope_ambiguous && 319148385Sume (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { 320148385Sume return(error); 32153541Sshin } 322148385Sume 323148385Sume if (*plocal_addr6 == 0) { 324148385Sume if (error == 0) 325148385Sume error = EADDRNOTAVAIL; 326148385Sume return (error); 327148385Sume } 328148385Sume /* 329148385Sume * Don't do pcblookup call here; return interface in 330148385Sume * plocal_addr6 331148385Sume * and exit to caller, that will do the lookup. 332148385Sume */ 333148385Sume 334120856Sume return (0); 33553541Sshin} 33653541Sshin 33753541Sshin/* 33853541Sshin * Outer subroutine: 33953541Sshin * Connect from a socket to a specified address. 34053541Sshin * Both address and port must be specified in argument sin. 34153541Sshin * If don't have a local address for this socket yet, 34253541Sshin * then pick one. 34353541Sshin */ 34453541Sshinint 345171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam, 346171259Sdelphij struct ucred *cred) 34753541Sshin{ 34853541Sshin struct in6_addr *addr6; 34953541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 35053541Sshin int error; 35153541Sshin 352132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 353132714Srwatson INP_LOCK_ASSERT(inp); 354132714Srwatson 35553541Sshin /* 35695023Ssuz * Call inner routine, to assign local interface address. 35795023Ssuz * in6_pcbladdr() may automatically fill in sin6_scope_id. 35853541Sshin */ 35953541Sshin if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 360120856Sume return (error); 36153541Sshin 36253541Sshin if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 36353541Sshin sin6->sin6_port, 36453541Sshin IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 36553541Sshin ? addr6 : &inp->in6p_laddr, 36653541Sshin inp->inp_lport, 0, NULL) != NULL) { 36753541Sshin return (EADDRINUSE); 36853541Sshin } 36953541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 37053541Sshin if (inp->inp_lport == 0) { 371127505Spjd error = in6_pcbbind(inp, (struct sockaddr *)0, cred); 37253541Sshin if (error) 37353541Sshin return (error); 37453541Sshin } 37553541Sshin inp->in6p_laddr = *addr6; 37653541Sshin } 37753541Sshin inp->in6p_faddr = sin6->sin6_addr; 37853541Sshin inp->inp_fport = sin6->sin6_port; 37978064Sume /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 38078064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 38178064Sume if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 38255679Sshin inp->in6p_flowinfo |= 383120649Sume (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 38453541Sshin 38553541Sshin in_pcbrehash(inp); 386171133Sgnn 38753541Sshin return (0); 38853541Sshin} 38953541Sshin 39053541Sshinvoid 391171259Sdelphijin6_pcbdisconnect(struct inpcb *inp) 39253541Sshin{ 393132714Srwatson 394132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 395132714Srwatson INP_LOCK_ASSERT(inp); 396132714Srwatson 39753541Sshin bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 39853541Sshin inp->inp_fport = 0; 39978064Sume /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 40078064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 40153541Sshin in_pcbrehash(inp); 40253541Sshin} 40353541Sshin 40453541Sshinvoid 405157373Srwatsonin6_pcbdetach(struct inpcb *inp) 40653541Sshin{ 407157373Srwatson 408157373Srwatson KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL")); 409157373Srwatson inp->inp_socket->so_pcb = NULL; 410157373Srwatson inp->inp_socket = NULL; 411157373Srwatson} 412157373Srwatson 413157373Srwatsonvoid 414157373Srwatsonin6_pcbfree(struct inpcb *inp) 415157373Srwatson{ 41653541Sshin struct inpcbinfo *ipi = inp->inp_pcbinfo; 41753541Sshin 418157373Srwatson KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL")); 419132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 420132714Srwatson INP_LOCK_ASSERT(inp); 421132714Srwatson 422171167Sgnn#ifdef IPSEC 42358452Sgreen if (inp->in6p_sp != NULL) 42458452Sgreen ipsec6_delete_pcbpolicy(inp); 425171167Sgnn#endif /* IPSEC */ 42653541Sshin inp->inp_gencnt = ++ipi->ipi_gencnt; 42753541Sshin in_pcbremlists(inp); 428171260Sdelphij ip6_freepcbopts(inp->in6p_outputopts); 429171260Sdelphij ip6_freemoptions(inp->in6p_moptions); 43055679Sshin /* Check and free IPv4 related resources in case of mapped addr */ 43155679Sshin if (inp->inp_options) 43255679Sshin (void)m_free(inp->inp_options); 433170613Sbms if (inp->inp_moptions != NULL) 434170613Sbms inp_freemoptions(inp->inp_moptions); 43553541Sshin inp->inp_vflag = 0; 436160491Sups INP_UNLOCK(inp); 43792767Sjeff uma_zfree(ipi->ipi_zone, inp); 43853541Sshin} 43953541Sshin 440102218Struckmanstruct sockaddr * 441171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p) 442102218Struckman{ 443102218Struckman struct sockaddr_in6 *sin6; 444102218Struckman 445111119Simp MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 446102218Struckman bzero(sin6, sizeof *sin6); 447102218Struckman sin6->sin6_family = AF_INET6; 448102218Struckman sin6->sin6_len = sizeof(*sin6); 449102218Struckman sin6->sin6_port = port; 450102218Struckman sin6->sin6_addr = *addr_p; 451148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 452102218Struckman 453102218Struckman return (struct sockaddr *)sin6; 454102218Struckman} 455102218Struckman 456102218Struckmanstruct sockaddr * 457171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 458102218Struckman{ 459102218Struckman struct sockaddr_in sin; 460102218Struckman struct sockaddr_in6 *sin6_p; 461102218Struckman 462102218Struckman bzero(&sin, sizeof sin); 463102218Struckman sin.sin_family = AF_INET; 464102218Struckman sin.sin_len = sizeof(sin); 465102218Struckman sin.sin_port = port; 466102218Struckman sin.sin_addr = *addr_p; 467102218Struckman 468102218Struckman MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 469111119Simp M_WAITOK); 470102218Struckman in6_sin_2_v4mapsin6(&sin, sin6_p); 471102218Struckman 472102218Struckman return (struct sockaddr *)sin6_p; 473102218Struckman} 474102218Struckman 47553541Sshinint 476171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam) 47753541Sshin{ 47853541Sshin register struct inpcb *inp; 479102218Struckman struct in6_addr addr; 480102218Struckman in_port_t port; 48153541Sshin 48253541Sshin inp = sotoinpcb(so); 483169462Srwatson KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 484157673Srwatson 485157767Srwatson INP_LOCK(inp); 486102218Struckman port = inp->inp_lport; 487102218Struckman addr = inp->in6p_laddr; 488157767Srwatson INP_UNLOCK(inp); 48953541Sshin 490102218Struckman *nam = in6_sockaddr(port, &addr); 49153541Sshin return 0; 49253541Sshin} 49353541Sshin 49453541Sshinint 495171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam) 49653541Sshin{ 49753541Sshin struct inpcb *inp; 498102218Struckman struct in6_addr addr; 499102218Struckman in_port_t port; 50053541Sshin 50153541Sshin inp = sotoinpcb(so); 502169462Srwatson KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 503157673Srwatson 504157767Srwatson INP_LOCK(inp); 505102218Struckman port = inp->inp_fport; 506102218Struckman addr = inp->in6p_faddr; 507157767Srwatson INP_UNLOCK(inp); 50853541Sshin 509102218Struckman *nam = in6_sockaddr(port, &addr); 51053541Sshin return 0; 51153541Sshin} 51253541Sshin 51353541Sshinint 51453541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 51553541Sshin{ 516157673Srwatson struct inpcb *inp; 51753541Sshin int error; 51853541Sshin 519157673Srwatson inp = sotoinpcb(so); 520157673Srwatson KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 521157673Srwatson 522124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 523169462Srwatson error = in_getsockaddr(so, nam); 52454952Seivind if (error == 0) 52553541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 526120913Sume } else { 527169462Srwatson /* scope issues will be handled in in6_getsockaddr(). */ 528169462Srwatson error = in6_getsockaddr(so, nam); 529120913Sume } 53053541Sshin 53153541Sshin return error; 53253541Sshin} 53353541Sshin 53453541Sshinint 53553541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 53653541Sshin{ 537157673Srwatson struct inpcb *inp; 53853541Sshin int error; 53953541Sshin 540157673Srwatson inp = sotoinpcb(so); 541157673Srwatson KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 542157673Srwatson 543124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 544169462Srwatson error = in_getpeeraddr(so, nam); 54554952Seivind if (error == 0) 54653541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 54753541Sshin } else 548169462Srwatson /* scope issues will be handled in in6_getpeeraddr(). */ 549169462Srwatson error = in6_getpeeraddr(so, nam); 55053541Sshin 55153541Sshin return error; 55253541Sshin} 55353541Sshin 55453541Sshin/* 55553541Sshin * Pass some notification to all connections of a protocol 55653541Sshin * associated with address dst. The local address and/or port numbers 55753541Sshin * may be specified to limit the search. The "usual action" will be 55853541Sshin * taken, depending on the ctlinput cmd. The caller must filter any 55953541Sshin * cmds that are uninteresting (e.g., no error in the map). 56053541Sshin * Call the protocol specific routine (if any) to report 56153541Sshin * any errors for each matching socket. 56253541Sshin */ 56353541Sshinvoid 564171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, 565171259Sdelphij u_int fport_arg, const struct sockaddr *src, u_int lport_arg, 566171259Sdelphij int cmd, void *cmdarg, 567171259Sdelphij struct inpcb *(*notify) __P((struct inpcb *, int))) 56853541Sshin{ 569133192Srwatson struct inpcbhead *head; 57062587Sitojun struct inpcb *inp, *ninp; 57178064Sume struct sockaddr_in6 sa6_src, *sa6_dst; 57253541Sshin u_short fport = fport_arg, lport = lport_arg; 57378064Sume u_int32_t flowinfo; 574157673Srwatson int errno; 57553541Sshin 576119995Sru if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 57753541Sshin return; 57878064Sume 57978064Sume sa6_dst = (struct sockaddr_in6 *)dst; 58078064Sume if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 58153541Sshin return; 58253541Sshin 58353541Sshin /* 58478064Sume * note that src can be NULL when we get notify by local fragmentation. 58578064Sume */ 58691346Salfred sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 58778064Sume flowinfo = sa6_src.sin6_flowinfo; 58878064Sume 58978064Sume /* 59053541Sshin * Redirects go to all references to the destination, 59162587Sitojun * and use in6_rtchange to invalidate the route cache. 59262587Sitojun * Dead host indications: also use in6_rtchange to invalidate 59362587Sitojun * the cache, and deliver the error to all the sockets. 59453541Sshin * Otherwise, if we have knowledge of the local port and address, 59553541Sshin * deliver only to that socket. 59653541Sshin */ 59753541Sshin if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 59853541Sshin fport = 0; 59953541Sshin lport = 0; 60078064Sume bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 60162587Sitojun 60278064Sume if (cmd != PRC_HOSTDEAD) 60378064Sume notify = in6_rtchange; 60453541Sshin } 60553541Sshin errno = inet6ctlerrmap[cmd]; 606169154Srwatson head = pcbinfo->ipi_listhead; 607133192Srwatson INP_INFO_WLOCK(pcbinfo); 608171260Sdelphij for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 609133192Srwatson INP_LOCK(inp); 610171260Sdelphij ninp = LIST_NEXT(inp, inp_list); 61162587Sitojun 612171260Sdelphij if ((inp->inp_vflag & INP_IPV6) == 0) { 613133192Srwatson INP_UNLOCK(inp); 61453541Sshin continue; 615133192Srwatson } 61662587Sitojun 61778064Sume /* 618125776Sume * If the error designates a new path MTU for a destination 619125776Sume * and the application (associated with this socket) wanted to 620125776Sume * know the value, notify. Note that we notify for all 621125776Sume * disconnected sockets if the corresponding application 622125776Sume * wanted. This is because some UDP applications keep sending 623125776Sume * sockets disconnected. 624125776Sume * XXX: should we avoid to notify the value to TCP sockets? 625125776Sume */ 626125776Sume if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 627125776Sume (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 628125776Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 629125776Sume ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 630125776Sume (u_int32_t *)cmdarg); 631125776Sume } 632125776Sume 633125776Sume /* 63478064Sume * Detect if we should notify the error. If no source and 63578064Sume * destination ports are specifed, but non-zero flowinfo and 63678064Sume * local address match, notify the error. This is the case 63778064Sume * when the error is delivered with an encrypted buffer 63878064Sume * by ESP. Otherwise, just compare addresses and ports 63978064Sume * as usual. 64078064Sume */ 64178064Sume if (lport == 0 && fport == 0 && flowinfo && 64278064Sume inp->inp_socket != NULL && 64378064Sume flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 64478064Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 64578064Sume goto do_notify; 64678064Sume else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 64778064Sume &sa6_dst->sin6_addr) || 64878064Sume inp->inp_socket == 0 || 64978064Sume (lport && inp->inp_lport != lport) || 65078064Sume (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 65178064Sume !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 65278064Sume &sa6_src.sin6_addr)) || 653133192Srwatson (fport && inp->inp_fport != fport)) { 654133192Srwatson INP_UNLOCK(inp); 65553541Sshin continue; 656133192Srwatson } 65762587Sitojun 65878064Sume do_notify: 659134121Srwatson if (notify) { 660134121Srwatson if ((*notify)(inp, errno)) 661134121Srwatson INP_UNLOCK(inp); 662134121Srwatson } else 663134121Srwatson INP_UNLOCK(inp); 66453541Sshin } 665133192Srwatson INP_INFO_WUNLOCK(pcbinfo); 66653541Sshin} 66753541Sshin 66853541Sshin/* 66953541Sshin * Lookup a PCB based on the local address and port. 67053541Sshin */ 67153541Sshinstruct inpcb * 672171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 673171259Sdelphij u_int lport_arg, int wild_okay) 67453541Sshin{ 67553541Sshin register struct inpcb *inp; 67653541Sshin int matchwild = 3, wildcard; 67753541Sshin u_short lport = lport_arg; 67853541Sshin 679158011Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 680158011Srwatson 68153541Sshin if (!wild_okay) { 68253541Sshin struct inpcbhead *head; 68353541Sshin /* 68453541Sshin * Look for an unconnected (wildcard foreign addr) PCB that 68553541Sshin * matches the local address and port we're looking for. 68653541Sshin */ 687169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 688169154Srwatson 0, pcbinfo->ipi_hashmask)]; 68953541Sshin LIST_FOREACH(inp, head, inp_hash) { 69054952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 69153541Sshin continue; 69253541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 69353541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 69453541Sshin inp->inp_lport == lport) { 69553541Sshin /* 69653541Sshin * Found. 69753541Sshin */ 69853541Sshin return (inp); 69953541Sshin } 70053541Sshin } 70153541Sshin /* 70253541Sshin * Not found. 70353541Sshin */ 70453541Sshin return (NULL); 70553541Sshin } else { 70653541Sshin struct inpcbporthead *porthash; 70753541Sshin struct inpcbport *phd; 70853541Sshin struct inpcb *match = NULL; 70953541Sshin /* 71053541Sshin * Best fit PCB lookup. 71153541Sshin * 71253541Sshin * First see if this local port is in use by looking on the 71353541Sshin * port hash list. 71453541Sshin */ 715169154Srwatson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 716169154Srwatson pcbinfo->ipi_porthashmask)]; 71753541Sshin LIST_FOREACH(phd, porthash, phd_hash) { 71853541Sshin if (phd->phd_port == lport) 71953541Sshin break; 72053541Sshin } 72153541Sshin if (phd != NULL) { 72253541Sshin /* 72353541Sshin * Port is in use by one or more PCBs. Look for best 72453541Sshin * fit. 72553541Sshin */ 72653541Sshin LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 72753541Sshin wildcard = 0; 72854952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 72953541Sshin continue; 73053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 73153541Sshin wildcard++; 73253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED( 73353541Sshin &inp->in6p_laddr)) { 73453541Sshin if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 73553541Sshin wildcard++; 73653541Sshin else if (!IN6_ARE_ADDR_EQUAL( 73753541Sshin &inp->in6p_laddr, laddr)) 73853541Sshin continue; 73953541Sshin } else { 74053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 74153541Sshin wildcard++; 74253541Sshin } 74353541Sshin if (wildcard < matchwild) { 74453541Sshin match = inp; 74553541Sshin matchwild = wildcard; 74653541Sshin if (matchwild == 0) { 74753541Sshin break; 74853541Sshin } 74953541Sshin } 75053541Sshin } 75153541Sshin } 75253541Sshin return (match); 75353541Sshin } 75453541Sshin} 75553541Sshin 75681127Sumevoid 757171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 75881127Sume{ 75981127Sume struct in6pcb *in6p; 76081127Sume struct ip6_moptions *im6o; 76181127Sume struct in6_multi_mship *imm, *nimm; 76281127Sume 763157978Srwatson INP_INFO_RLOCK(pcbinfo); 764169154Srwatson LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) { 765157978Srwatson INP_LOCK(in6p); 76681127Sume im6o = in6p->in6p_moptions; 76781127Sume if ((in6p->inp_vflag & INP_IPV6) && 76881127Sume im6o) { 76981127Sume /* 77081127Sume * Unselect the outgoing interface if it is being 77181127Sume * detached. 77281127Sume */ 77381127Sume if (im6o->im6o_multicast_ifp == ifp) 77481127Sume im6o->im6o_multicast_ifp = NULL; 77581127Sume 77681127Sume /* 77781127Sume * Drop multicast group membership if we joined 77881127Sume * through the interface being detached. 77981127Sume * XXX controversial - is it really legal for kernel 78081127Sume * to force this? 78181127Sume */ 78281127Sume for (imm = im6o->im6o_memberships.lh_first; 78381127Sume imm != NULL; imm = nimm) { 78481127Sume nimm = imm->i6mm_chain.le_next; 78581127Sume if (imm->i6mm_maddr->in6m_ifp == ifp) { 78681127Sume LIST_REMOVE(imm, i6mm_chain); 78781127Sume in6_delmulti(imm->i6mm_maddr); 788149849Sobrien free(imm, M_IP6MADDR); 78981127Sume } 79081127Sume } 79181127Sume } 792157978Srwatson INP_UNLOCK(in6p); 79381127Sume } 794157978Srwatson INP_INFO_RUNLOCK(pcbinfo); 79581127Sume} 79681127Sume 79753541Sshin/* 79853541Sshin * Check for alternatives when higher level complains 79953541Sshin * about service problems. For now, invalidate cached 80053541Sshin * routing information. If the route was created dynamically 80153541Sshin * (by a redirect), time to try a default gateway again. 80253541Sshin */ 80353541Sshinvoid 804171259Sdelphijin6_losing(struct inpcb *in6p) 80553541Sshin{ 806171259Sdelphij 807122922Sandre /* 808122922Sandre * We don't store route pointers in the routing table anymore 809122922Sandre */ 810122922Sandre return; 81153541Sshin} 81253541Sshin 81353541Sshin/* 81453541Sshin * After a routing change, flush old routing 81553541Sshin * and allocate a (hopefully) better one. 81653541Sshin */ 81798211Shsustruct inpcb * 818171259Sdelphijin6_rtchange(struct inpcb *inp, int errno) 81953541Sshin{ 820122922Sandre /* 821122922Sandre * We don't store route pointers in the routing table anymore 822122922Sandre */ 82398211Shsu return inp; 82453541Sshin} 82553541Sshin 82653541Sshin/* 82753541Sshin * Lookup PCB in hash list. 82853541Sshin */ 82953541Sshinstruct inpcb * 830171259Sdelphijin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 831171259Sdelphij u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, 832171259Sdelphij int wildcard, struct ifnet *ifp) 83353541Sshin{ 83453541Sshin struct inpcbhead *head; 83553541Sshin register struct inpcb *inp; 83653541Sshin u_short fport = fport_arg, lport = lport_arg; 83778064Sume int faith; 83853541Sshin 839158011Srwatson INP_INFO_RLOCK_ASSERT(pcbinfo); 840158011Srwatson 84183934Sbrooks if (faithprefix_p != NULL) 84283934Sbrooks faith = (*faithprefix_p)(laddr); 84383934Sbrooks else 84483934Sbrooks faith = 0; 84578064Sume 84653541Sshin /* 84753541Sshin * First look for an exact match. 84853541Sshin */ 849169154Srwatson head = &pcbinfo->ipi_hashbase[ 850169154Srwatson INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport, 851169154Srwatson pcbinfo->ipi_hashmask)]; 85254263Sshin LIST_FOREACH(inp, head, inp_hash) { 85354952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 85453541Sshin continue; 85553541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 85653541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 85753541Sshin inp->inp_fport == fport && 85853541Sshin inp->inp_lport == lport) { 85953541Sshin /* 86053541Sshin * Found. 86153541Sshin */ 86253541Sshin return (inp); 86353541Sshin } 86453541Sshin } 86553541Sshin if (wildcard) { 86653541Sshin struct inpcb *local_wild = NULL; 86753541Sshin 868169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 869169154Srwatson 0, pcbinfo->ipi_hashmask)]; 87054263Sshin LIST_FOREACH(inp, head, inp_hash) { 87154952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 87253541Sshin continue; 87353541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 87453541Sshin inp->inp_lport == lport) { 87578064Sume if (faith && (inp->inp_flags & INP_FAITH) == 0) 87653541Sshin continue; 87753541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 87853541Sshin laddr)) 87953541Sshin return (inp); 88053541Sshin else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 88153541Sshin local_wild = inp; 88253541Sshin } 88353541Sshin } 88453541Sshin return (local_wild); 88553541Sshin } 88653541Sshin 88753541Sshin /* 88853541Sshin * Not found. 88953541Sshin */ 89053541Sshin return (NULL); 89153541Sshin} 89253541Sshin 89353541Sshinvoid 89453541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 89553541Sshin{ 89653541Sshin struct ip6_hdr *ip; 89753541Sshin 89853541Sshin ip = mtod(m, struct ip6_hdr *); 89953541Sshin bzero(sin6, sizeof(*sin6)); 90053541Sshin sin6->sin6_len = sizeof(*sin6); 90153541Sshin sin6->sin6_family = AF_INET6; 90253541Sshin sin6->sin6_addr = ip->ip6_src; 90353541Sshin 904148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 905148385Sume 90653541Sshin return; 90753541Sshin} 908