in6_pcb.c revision 183611
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 183611 2008-10-04 17:07:58Z 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> 99181887Sjulian 10053541Sshin#include <netinet6/ip6_var.h> 10153541Sshin#include <netinet6/nd6.h> 10253541Sshin#include <netinet/in_pcb.h> 10353541Sshin#include <netinet6/in6_pcb.h> 104148385Sume#include <netinet6/scope6_var.h> 10553541Sshin 106171167Sgnn#ifdef IPSEC 107105199Ssam#include <netipsec/ipsec.h> 108105199Ssam#include <netipsec/ipsec6.h> 109105199Ssam#include <netipsec/key.h> 110171167Sgnn#endif /* IPSEC */ 111105199Ssam 112174717Srwatson#include <security/mac/mac_framework.h> 113174717Srwatson 11453541Sshinstruct in6_addr zeroin6_addr; 11553541Sshin 11653541Sshinint 117171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam, 118171259Sdelphij struct ucred *cred) 11953541Sshin{ 120183550Szec INIT_VNET_INET6(inp->inp_vnet); 121183550Szec INIT_VNET_INET(inp->inp_vnet); 12253541Sshin struct socket *so = inp->inp_socket; 12353541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 12453541Sshin struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 12553541Sshin u_short lport = 0; 12697658Stanimura int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 12753541Sshin 128132714Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 129178285Srwatson INP_WLOCK_ASSERT(inp); 130132714Srwatson 131181803Sbz if (!V_in6_ifaddr) /* XXX broken! */ 13253541Sshin return (EADDRNOTAVAIL); 13353541Sshin if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 134120856Sume return (EINVAL); 13553541Sshin if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 136160024Sbz wild = INPLOOKUP_WILDCARD; 13753541Sshin if (nam) { 138148385Sume int error; 139148385Sume 14053541Sshin sin6 = (struct sockaddr_in6 *)nam; 14153541Sshin if (nam->sa_len != sizeof(*sin6)) 142120856Sume return (EINVAL); 14353541Sshin /* 14453541Sshin * family check. 14553541Sshin */ 14653541Sshin if (nam->sa_family != AF_INET6) 147120856Sume return (EAFNOSUPPORT); 14853541Sshin 149181803Sbz if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 150148385Sume return(error); 15153541Sshin 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 } 22653541Sshin t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 227180427Sbz lport, wild, cred); 228116453Scognet if (t && (reuseport & ((t->inp_vflag & 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); 239116453Scognet if (t && t->inp_vflag & 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) { 25962587Sitojun int e; 260127505Spjd if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) 261120856Sume return (e); 262183611Sbz } else { 26362587Sitojun inp->inp_lport = lport; 26462587Sitojun if (in_pcbinshash(inp) != 0) { 26562587Sitojun inp->in6p_laddr = in6addr_any; 26662587Sitojun inp->inp_lport = 0; 26762587Sitojun return (EAGAIN); 26853541Sshin } 26953541Sshin } 270120856Sume return (0); 27153541Sshin} 27253541Sshin 27353541Sshin/* 27453541Sshin * Transform old in6_pcbconnect() into an inner subroutine for new 27553541Sshin * in6_pcbconnect(): Do some validity-checking on the remote 27653541Sshin * address (in mbuf 'nam') and then determine local host address 27753541Sshin * (i.e., which interface) to use to access that remote host. 27853541Sshin * 27953541Sshin * This preserves definition of in6_pcbconnect(), while supporting a 28053541Sshin * slightly different version for T/TCP. (This is more than 28153541Sshin * a bit of a kludge, but cleaning up the internal interfaces would 28253541Sshin * have forced minor changes in every protocol). 28353541Sshin */ 28453541Sshinint 285171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam, 286171259Sdelphij struct in6_addr **plocal_addr6) 28753541Sshin{ 288183550Szec INIT_VNET_INET6(inp->inp_vnet); 28953541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 290148385Sume int error = 0; 29153541Sshin struct ifnet *ifp = NULL; 292148385Sume int scope_ambiguous = 0; 29353541Sshin 294158011Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 295178285Srwatson INP_WLOCK_ASSERT(inp); 296158011Srwatson 29753541Sshin if (nam->sa_len != sizeof (*sin6)) 29853541Sshin return (EINVAL); 29953541Sshin if (sin6->sin6_family != AF_INET6) 30053541Sshin return (EAFNOSUPPORT); 30153541Sshin if (sin6->sin6_port == 0) 30253541Sshin return (EADDRNOTAVAIL); 30353541Sshin 304181803Sbz if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 305148385Sume scope_ambiguous = 1; 306181803Sbz if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 307148385Sume return(error); 308148385Sume 309181803Sbz if (V_in6_ifaddr) { 31053541Sshin /* 31153541Sshin * If the destination address is UNSPECIFIED addr, 31253541Sshin * use the loopback addr, e.g ::1. 31353541Sshin */ 31453541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 31553541Sshin sin6->sin6_addr = in6addr_loopback; 31653541Sshin } 317148385Sume 318148385Sume /* 319148385Sume * XXX: in6_selectsrc might replace the bound local address 320148385Sume * with the address specified by setsockopt(IPV6_PKTINFO). 321148385Sume * Is it the intended behavior? 322148385Sume */ 323148385Sume *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 324180371Sbz inp, NULL, 325183606Sbz inp->inp_cred, 326180371Sbz &ifp, &error); 327148385Sume if (ifp && scope_ambiguous && 328148385Sume (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { 329148385Sume return(error); 33053541Sshin } 331148385Sume 332183611Sbz if (*plocal_addr6 == NULL) { 333148385Sume if (error == 0) 334148385Sume error = EADDRNOTAVAIL; 335148385Sume return (error); 336148385Sume } 337148385Sume /* 338148385Sume * Don't do pcblookup call here; return interface in 339148385Sume * plocal_addr6 340148385Sume * and exit to caller, that will do the lookup. 341148385Sume */ 342148385Sume 343120856Sume return (0); 34453541Sshin} 34553541Sshin 34653541Sshin/* 34753541Sshin * Outer subroutine: 34853541Sshin * Connect from a socket to a specified address. 34953541Sshin * Both address and port must be specified in argument sin. 35053541Sshin * If don't have a local address for this socket yet, 35153541Sshin * then pick one. 35253541Sshin */ 35353541Sshinint 354171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam, 355171259Sdelphij struct ucred *cred) 35653541Sshin{ 35753541Sshin struct in6_addr *addr6; 35853541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 35953541Sshin int error; 36053541Sshin 361132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 362178285Srwatson INP_WLOCK_ASSERT(inp); 363132714Srwatson 36453541Sshin /* 36595023Ssuz * Call inner routine, to assign local interface address. 36695023Ssuz * in6_pcbladdr() may automatically fill in sin6_scope_id. 36753541Sshin */ 36853541Sshin if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 369120856Sume return (error); 37053541Sshin 37153541Sshin if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 37253541Sshin sin6->sin6_port, 37353541Sshin IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 37453541Sshin ? addr6 : &inp->in6p_laddr, 37553541Sshin inp->inp_lport, 0, NULL) != NULL) { 37653541Sshin return (EADDRINUSE); 37753541Sshin } 37853541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 37953541Sshin if (inp->inp_lport == 0) { 380127505Spjd error = in6_pcbbind(inp, (struct sockaddr *)0, cred); 38153541Sshin if (error) 38253541Sshin return (error); 38353541Sshin } 38453541Sshin inp->in6p_laddr = *addr6; 38553541Sshin } 38653541Sshin inp->in6p_faddr = sin6->sin6_addr; 38753541Sshin inp->inp_fport = sin6->sin6_port; 38878064Sume /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 38978064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 39078064Sume if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 39155679Sshin inp->in6p_flowinfo |= 392120649Sume (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 39353541Sshin 39453541Sshin in_pcbrehash(inp); 395171133Sgnn 39653541Sshin return (0); 39753541Sshin} 39853541Sshin 39953541Sshinvoid 400171259Sdelphijin6_pcbdisconnect(struct inpcb *inp) 40153541Sshin{ 402132714Srwatson 403132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 404178285Srwatson INP_WLOCK_ASSERT(inp); 405132714Srwatson 40653541Sshin bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 40753541Sshin inp->inp_fport = 0; 40878064Sume /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 40978064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 41053541Sshin in_pcbrehash(inp); 41153541Sshin} 41253541Sshin 41353541Sshinvoid 414157373Srwatsonin6_pcbdetach(struct inpcb *inp) 41553541Sshin{ 416157373Srwatson 417157373Srwatson KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL")); 418157373Srwatson inp->inp_socket->so_pcb = NULL; 419157373Srwatson inp->inp_socket = NULL; 420157373Srwatson} 421157373Srwatson 422157373Srwatsonvoid 423157373Srwatsonin6_pcbfree(struct inpcb *inp) 424157373Srwatson{ 42553541Sshin struct inpcbinfo *ipi = inp->inp_pcbinfo; 42653541Sshin 427157373Srwatson KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL")); 428132714Srwatson INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 429178285Srwatson INP_WLOCK_ASSERT(inp); 430132714Srwatson 431171167Sgnn#ifdef IPSEC 43258452Sgreen if (inp->in6p_sp != NULL) 43358452Sgreen ipsec6_delete_pcbpolicy(inp); 434171167Sgnn#endif /* IPSEC */ 43553541Sshin inp->inp_gencnt = ++ipi->ipi_gencnt; 43653541Sshin in_pcbremlists(inp); 437171260Sdelphij ip6_freepcbopts(inp->in6p_outputopts); 438171260Sdelphij ip6_freemoptions(inp->in6p_moptions); 43955679Sshin /* Check and free IPv4 related resources in case of mapped addr */ 44055679Sshin if (inp->inp_options) 44155679Sshin (void)m_free(inp->inp_options); 442170613Sbms if (inp->inp_moptions != NULL) 443170613Sbms inp_freemoptions(inp->inp_moptions); 44453541Sshin inp->inp_vflag = 0; 445174717Srwatson#ifdef MAC 446174717Srwatson mac_inpcb_destroy(inp); 447174717Srwatson#endif 448178285Srwatson INP_WUNLOCK(inp); 44992767Sjeff uma_zfree(ipi->ipi_zone, inp); 45053541Sshin} 45153541Sshin 452102218Struckmanstruct sockaddr * 453171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p) 454102218Struckman{ 455102218Struckman struct sockaddr_in6 *sin6; 456102218Struckman 457111119Simp MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 458102218Struckman bzero(sin6, sizeof *sin6); 459102218Struckman sin6->sin6_family = AF_INET6; 460102218Struckman sin6->sin6_len = sizeof(*sin6); 461102218Struckman sin6->sin6_port = port; 462102218Struckman sin6->sin6_addr = *addr_p; 463148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 464102218Struckman 465102218Struckman return (struct sockaddr *)sin6; 466102218Struckman} 467102218Struckman 468102218Struckmanstruct sockaddr * 469171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 470102218Struckman{ 471102218Struckman struct sockaddr_in sin; 472102218Struckman struct sockaddr_in6 *sin6_p; 473102218Struckman 474102218Struckman bzero(&sin, sizeof sin); 475102218Struckman sin.sin_family = AF_INET; 476102218Struckman sin.sin_len = sizeof(sin); 477102218Struckman sin.sin_port = port; 478102218Struckman sin.sin_addr = *addr_p; 479102218Struckman 480102218Struckman MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 481111119Simp M_WAITOK); 482102218Struckman in6_sin_2_v4mapsin6(&sin, sin6_p); 483102218Struckman 484102218Struckman return (struct sockaddr *)sin6_p; 485102218Struckman} 486102218Struckman 48753541Sshinint 488171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam) 48953541Sshin{ 49053541Sshin register struct inpcb *inp; 491102218Struckman struct in6_addr addr; 492102218Struckman in_port_t port; 49353541Sshin 49453541Sshin inp = sotoinpcb(so); 495169462Srwatson KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 496157673Srwatson 497178320Srwatson INP_RLOCK(inp); 498102218Struckman port = inp->inp_lport; 499102218Struckman addr = inp->in6p_laddr; 500178320Srwatson INP_RUNLOCK(inp); 50153541Sshin 502102218Struckman *nam = in6_sockaddr(port, &addr); 50353541Sshin return 0; 50453541Sshin} 50553541Sshin 50653541Sshinint 507171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam) 50853541Sshin{ 50953541Sshin struct inpcb *inp; 510102218Struckman struct in6_addr addr; 511102218Struckman in_port_t port; 51253541Sshin 51353541Sshin inp = sotoinpcb(so); 514169462Srwatson KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 515157673Srwatson 516178320Srwatson INP_RLOCK(inp); 517102218Struckman port = inp->inp_fport; 518102218Struckman addr = inp->in6p_faddr; 519178320Srwatson INP_RUNLOCK(inp); 52053541Sshin 521102218Struckman *nam = in6_sockaddr(port, &addr); 52253541Sshin return 0; 52353541Sshin} 52453541Sshin 52553541Sshinint 52653541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 52753541Sshin{ 528157673Srwatson struct inpcb *inp; 52953541Sshin int error; 53053541Sshin 531157673Srwatson inp = sotoinpcb(so); 532157673Srwatson KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 533157673Srwatson 534124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 535169462Srwatson error = in_getsockaddr(so, nam); 53654952Seivind if (error == 0) 53753541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 538120913Sume } else { 539169462Srwatson /* scope issues will be handled in in6_getsockaddr(). */ 540169462Srwatson error = in6_getsockaddr(so, nam); 541120913Sume } 54253541Sshin 54353541Sshin return error; 54453541Sshin} 54553541Sshin 54653541Sshinint 54753541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 54853541Sshin{ 549157673Srwatson struct inpcb *inp; 55053541Sshin int error; 55153541Sshin 552157673Srwatson inp = sotoinpcb(so); 553157673Srwatson KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 554157673Srwatson 555124332Sume if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 556169462Srwatson error = in_getpeeraddr(so, nam); 55754952Seivind if (error == 0) 55853541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 55953541Sshin } else 560169462Srwatson /* scope issues will be handled in in6_getpeeraddr(). */ 561169462Srwatson error = in6_getpeeraddr(so, nam); 56253541Sshin 56353541Sshin return error; 56453541Sshin} 56553541Sshin 56653541Sshin/* 56753541Sshin * Pass some notification to all connections of a protocol 56853541Sshin * associated with address dst. The local address and/or port numbers 56953541Sshin * may be specified to limit the search. The "usual action" will be 57053541Sshin * taken, depending on the ctlinput cmd. The caller must filter any 57153541Sshin * cmds that are uninteresting (e.g., no error in the map). 57253541Sshin * Call the protocol specific routine (if any) to report 57353541Sshin * any errors for each matching socket. 57453541Sshin */ 57553541Sshinvoid 576171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, 577171259Sdelphij u_int fport_arg, const struct sockaddr *src, u_int lport_arg, 578171259Sdelphij int cmd, void *cmdarg, 579175162Sobrien struct inpcb *(*notify)(struct inpcb *, int)) 58053541Sshin{ 581177961Srwatson struct inpcb *inp, *inp_temp; 58278064Sume struct sockaddr_in6 sa6_src, *sa6_dst; 58353541Sshin u_short fport = fport_arg, lport = lport_arg; 58478064Sume u_int32_t flowinfo; 585157673Srwatson int errno; 58653541Sshin 587119995Sru if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 58853541Sshin return; 58978064Sume 59078064Sume sa6_dst = (struct sockaddr_in6 *)dst; 59178064Sume if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 59253541Sshin return; 59353541Sshin 59453541Sshin /* 59578064Sume * note that src can be NULL when we get notify by local fragmentation. 59678064Sume */ 59791346Salfred sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 59878064Sume flowinfo = sa6_src.sin6_flowinfo; 59978064Sume 60078064Sume /* 60153541Sshin * Redirects go to all references to the destination, 60262587Sitojun * and use in6_rtchange to invalidate the route cache. 60362587Sitojun * Dead host indications: also use in6_rtchange to invalidate 60462587Sitojun * the cache, and deliver the error to all the sockets. 60553541Sshin * Otherwise, if we have knowledge of the local port and address, 60653541Sshin * deliver only to that socket. 60753541Sshin */ 60853541Sshin if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 60953541Sshin fport = 0; 61053541Sshin lport = 0; 61178064Sume bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 61262587Sitojun 61378064Sume if (cmd != PRC_HOSTDEAD) 61478064Sume notify = in6_rtchange; 61553541Sshin } 61653541Sshin errno = inet6ctlerrmap[cmd]; 617133192Srwatson INP_INFO_WLOCK(pcbinfo); 618177961Srwatson LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 619178285Srwatson INP_WLOCK(inp); 620171260Sdelphij if ((inp->inp_vflag & INP_IPV6) == 0) { 621178285Srwatson INP_WUNLOCK(inp); 62253541Sshin continue; 623133192Srwatson } 62462587Sitojun 62578064Sume /* 626125776Sume * If the error designates a new path MTU for a destination 627125776Sume * and the application (associated with this socket) wanted to 628125776Sume * know the value, notify. Note that we notify for all 629125776Sume * disconnected sockets if the corresponding application 630125776Sume * wanted. This is because some UDP applications keep sending 631125776Sume * sockets disconnected. 632125776Sume * XXX: should we avoid to notify the value to TCP sockets? 633125776Sume */ 634125776Sume if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 635125776Sume (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 636125776Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 637125776Sume ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 638125776Sume (u_int32_t *)cmdarg); 639125776Sume } 640125776Sume 641125776Sume /* 64278064Sume * Detect if we should notify the error. If no source and 64378064Sume * destination ports are specifed, but non-zero flowinfo and 64478064Sume * local address match, notify the error. This is the case 64578064Sume * when the error is delivered with an encrypted buffer 64678064Sume * by ESP. Otherwise, just compare addresses and ports 64778064Sume * as usual. 64878064Sume */ 64978064Sume if (lport == 0 && fport == 0 && flowinfo && 65078064Sume inp->inp_socket != NULL && 65178064Sume flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 65278064Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 65378064Sume goto do_notify; 65478064Sume else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 65578064Sume &sa6_dst->sin6_addr) || 65678064Sume inp->inp_socket == 0 || 65778064Sume (lport && inp->inp_lport != lport) || 65878064Sume (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 65978064Sume !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 66078064Sume &sa6_src.sin6_addr)) || 661133192Srwatson (fport && inp->inp_fport != fport)) { 662178285Srwatson INP_WUNLOCK(inp); 66353541Sshin continue; 664133192Srwatson } 66562587Sitojun 66678064Sume do_notify: 667134121Srwatson if (notify) { 668134121Srwatson if ((*notify)(inp, errno)) 669178285Srwatson INP_WUNLOCK(inp); 670134121Srwatson } else 671178285Srwatson INP_WUNLOCK(inp); 67253541Sshin } 673133192Srwatson INP_INFO_WUNLOCK(pcbinfo); 67453541Sshin} 67553541Sshin 67653541Sshin/* 67753541Sshin * Lookup a PCB based on the local address and port. 67853541Sshin */ 67953541Sshinstruct inpcb * 680171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 681180427Sbz u_short lport, int wild_okay, struct ucred *cred) 68253541Sshin{ 68353541Sshin register struct inpcb *inp; 68453541Sshin int matchwild = 3, wildcard; 68553541Sshin 686158011Srwatson INP_INFO_WLOCK_ASSERT(pcbinfo); 687158011Srwatson 68853541Sshin if (!wild_okay) { 68953541Sshin struct inpcbhead *head; 69053541Sshin /* 69153541Sshin * Look for an unconnected (wildcard foreign addr) PCB that 69253541Sshin * matches the local address and port we're looking for. 69353541Sshin */ 694169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 695169154Srwatson 0, pcbinfo->ipi_hashmask)]; 69653541Sshin LIST_FOREACH(inp, head, inp_hash) { 69754952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 69853541Sshin continue; 69953541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 70053541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 70153541Sshin inp->inp_lport == lport) { 70253541Sshin /* 70353541Sshin * Found. 70453541Sshin */ 70553541Sshin return (inp); 70653541Sshin } 70753541Sshin } 70853541Sshin /* 70953541Sshin * Not found. 71053541Sshin */ 71153541Sshin return (NULL); 71253541Sshin } else { 71353541Sshin struct inpcbporthead *porthash; 71453541Sshin struct inpcbport *phd; 71553541Sshin struct inpcb *match = NULL; 71653541Sshin /* 71753541Sshin * Best fit PCB lookup. 71853541Sshin * 71953541Sshin * First see if this local port is in use by looking on the 72053541Sshin * port hash list. 72153541Sshin */ 722169154Srwatson porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 723169154Srwatson pcbinfo->ipi_porthashmask)]; 72453541Sshin LIST_FOREACH(phd, porthash, phd_hash) { 72553541Sshin if (phd->phd_port == lport) 72653541Sshin break; 72753541Sshin } 72853541Sshin if (phd != NULL) { 72953541Sshin /* 73053541Sshin * Port is in use by one or more PCBs. Look for best 73153541Sshin * fit. 73253541Sshin */ 73353541Sshin LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 73453541Sshin wildcard = 0; 73554952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 73653541Sshin continue; 73753541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 73853541Sshin wildcard++; 73953541Sshin if (!IN6_IS_ADDR_UNSPECIFIED( 74053541Sshin &inp->in6p_laddr)) { 74153541Sshin if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 74253541Sshin wildcard++; 74353541Sshin else if (!IN6_ARE_ADDR_EQUAL( 74453541Sshin &inp->in6p_laddr, laddr)) 74553541Sshin continue; 74653541Sshin } else { 74753541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 74853541Sshin wildcard++; 74953541Sshin } 75053541Sshin if (wildcard < matchwild) { 75153541Sshin match = inp; 75253541Sshin matchwild = wildcard; 75353541Sshin if (matchwild == 0) { 75453541Sshin break; 75553541Sshin } 75653541Sshin } 75753541Sshin } 75853541Sshin } 75953541Sshin return (match); 76053541Sshin } 76153541Sshin} 76253541Sshin 76381127Sumevoid 764171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 76581127Sume{ 76681127Sume struct in6pcb *in6p; 76781127Sume struct ip6_moptions *im6o; 76881127Sume struct in6_multi_mship *imm, *nimm; 76981127Sume 770157978Srwatson INP_INFO_RLOCK(pcbinfo); 771169154Srwatson LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) { 772178285Srwatson INP_WLOCK(in6p); 77381127Sume im6o = in6p->in6p_moptions; 77481127Sume if ((in6p->inp_vflag & INP_IPV6) && 77581127Sume im6o) { 77681127Sume /* 77781127Sume * Unselect the outgoing interface if it is being 77881127Sume * detached. 77981127Sume */ 78081127Sume if (im6o->im6o_multicast_ifp == ifp) 78181127Sume im6o->im6o_multicast_ifp = NULL; 78281127Sume 78381127Sume /* 78481127Sume * Drop multicast group membership if we joined 78581127Sume * through the interface being detached. 78681127Sume * XXX controversial - is it really legal for kernel 78781127Sume * to force this? 78881127Sume */ 78981127Sume for (imm = im6o->im6o_memberships.lh_first; 79081127Sume imm != NULL; imm = nimm) { 79181127Sume nimm = imm->i6mm_chain.le_next; 79281127Sume if (imm->i6mm_maddr->in6m_ifp == ifp) { 79381127Sume LIST_REMOVE(imm, i6mm_chain); 79481127Sume in6_delmulti(imm->i6mm_maddr); 795149849Sobrien free(imm, M_IP6MADDR); 79681127Sume } 79781127Sume } 79881127Sume } 799178285Srwatson INP_WUNLOCK(in6p); 80081127Sume } 801157978Srwatson INP_INFO_RUNLOCK(pcbinfo); 80281127Sume} 80381127Sume 80453541Sshin/* 80553541Sshin * Check for alternatives when higher level complains 80653541Sshin * about service problems. For now, invalidate cached 80753541Sshin * routing information. If the route was created dynamically 80853541Sshin * (by a redirect), time to try a default gateway again. 80953541Sshin */ 81053541Sshinvoid 811171259Sdelphijin6_losing(struct inpcb *in6p) 81253541Sshin{ 813171259Sdelphij 814122922Sandre /* 815122922Sandre * We don't store route pointers in the routing table anymore 816122922Sandre */ 817122922Sandre return; 81853541Sshin} 81953541Sshin 82053541Sshin/* 82153541Sshin * After a routing change, flush old routing 82253541Sshin * and allocate a (hopefully) better one. 82353541Sshin */ 82498211Shsustruct inpcb * 825171259Sdelphijin6_rtchange(struct inpcb *inp, int errno) 82653541Sshin{ 827122922Sandre /* 828122922Sandre * We don't store route pointers in the routing table anymore 829122922Sandre */ 83098211Shsu return inp; 83153541Sshin} 83253541Sshin 83353541Sshin/* 83453541Sshin * Lookup PCB in hash list. 83553541Sshin */ 83653541Sshinstruct inpcb * 837171259Sdelphijin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 838171259Sdelphij u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, 839171259Sdelphij int wildcard, struct ifnet *ifp) 84053541Sshin{ 84153541Sshin struct inpcbhead *head; 84253541Sshin register struct inpcb *inp; 84353541Sshin u_short fport = fport_arg, lport = lport_arg; 84478064Sume int faith; 84553541Sshin 846178285Srwatson INP_INFO_LOCK_ASSERT(pcbinfo); 847158011Srwatson 84883934Sbrooks if (faithprefix_p != NULL) 84983934Sbrooks faith = (*faithprefix_p)(laddr); 85083934Sbrooks else 85183934Sbrooks faith = 0; 85278064Sume 85353541Sshin /* 85453541Sshin * First look for an exact match. 85553541Sshin */ 856169154Srwatson head = &pcbinfo->ipi_hashbase[ 857169154Srwatson INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport, 858169154Srwatson pcbinfo->ipi_hashmask)]; 85954263Sshin LIST_FOREACH(inp, head, inp_hash) { 86054952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 86153541Sshin continue; 86253541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 86353541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 86453541Sshin inp->inp_fport == fport && 86553541Sshin inp->inp_lport == lport) { 86653541Sshin /* 86753541Sshin * Found. 86853541Sshin */ 86953541Sshin return (inp); 87053541Sshin } 87153541Sshin } 87253541Sshin if (wildcard) { 87353541Sshin struct inpcb *local_wild = NULL; 87453541Sshin 875169154Srwatson head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 876169154Srwatson 0, pcbinfo->ipi_hashmask)]; 87754263Sshin LIST_FOREACH(inp, head, inp_hash) { 87854952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 87953541Sshin continue; 88053541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 88153541Sshin inp->inp_lport == lport) { 88278064Sume if (faith && (inp->inp_flags & INP_FAITH) == 0) 88353541Sshin continue; 88453541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 88553541Sshin laddr)) 88653541Sshin return (inp); 88753541Sshin else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 88853541Sshin local_wild = inp; 88953541Sshin } 89053541Sshin } 89153541Sshin return (local_wild); 89253541Sshin } 89353541Sshin 89453541Sshin /* 89553541Sshin * Not found. 89653541Sshin */ 89753541Sshin return (NULL); 89853541Sshin} 89953541Sshin 90053541Sshinvoid 90153541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 90253541Sshin{ 90353541Sshin struct ip6_hdr *ip; 90453541Sshin 90553541Sshin ip = mtod(m, struct ip6_hdr *); 90653541Sshin bzero(sin6, sizeof(*sin6)); 90753541Sshin sin6->sin6_len = sizeof(*sin6); 90853541Sshin sin6->sin6_family = AF_INET6; 90953541Sshin sin6->sin6_addr = ip->ip6_src; 91053541Sshin 911148385Sume (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 912148385Sume 91353541Sshin return; 91453541Sshin} 915