in6_pcb.c revision 83366
162587Sitojun/* $FreeBSD: head/sys/netinet6/in6_pcb.c 83366 2001-09-12 08:38:13Z julian $ */ 278064Sume/* $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ */ 362587Sitojun 453541Sshin/* 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 3453541Sshin/* 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 * 3. All advertising materials mentioning features or use of this software 4753541Sshin * must display the following acknowledgement: 4853541Sshin * This product includes software developed by the University of 4953541Sshin * California, Berkeley and its contributors. 5053541Sshin * 4. Neither the name of the University nor the names of its contributors 5153541Sshin * may be used to endorse or promote products derived from this software 5253541Sshin * without specific prior written permission. 5353541Sshin * 5453541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5553541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5653541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5753541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5853541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5953541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 6053541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6153541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6253541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6353541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6453541Sshin * SUCH DAMAGE. 6553541Sshin * 6653541Sshin * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 6753541Sshin */ 6853541Sshin 6978064Sume#include "opt_inet.h" 7078064Sume#include "opt_inet6.h" 7155009Sshin#include "opt_ipsec.h" 7253541Sshin 7353541Sshin#include <sys/param.h> 7453541Sshin#include <sys/systm.h> 7553541Sshin#include <sys/malloc.h> 7653541Sshin#include <sys/mbuf.h> 7755679Sshin#include <sys/domain.h> 7853541Sshin#include <sys/protosw.h> 7953541Sshin#include <sys/socket.h> 8053541Sshin#include <sys/socketvar.h> 8153541Sshin#include <sys/sockio.h> 8253541Sshin#include <sys/errno.h> 8353541Sshin#include <sys/time.h> 8453541Sshin#include <sys/proc.h> 8553541Sshin#include <sys/jail.h> 8653541Sshin 8753541Sshin#include <vm/vm_zone.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> 9662587Sitojun#include <netinet/ip6.h> 9755679Sshin#include <netinet/ip_var.h> 9853541Sshin#include <netinet6/ip6_var.h> 9953541Sshin#include <netinet6/nd6.h> 10053541Sshin#include <netinet/in_pcb.h> 10153541Sshin#include <netinet6/in6_pcb.h> 10253541Sshin 10354263Sshin#include "faith.h" 10478064Sume#if defined(NFAITH) && NFAITH > 0 10578064Sume#include <net/if_faith.h> 10678064Sume#endif 10753541Sshin 10853541Sshin#ifdef IPSEC 10953541Sshin#include <netinet6/ipsec.h> 11078064Sume#ifdef INET6 11178064Sume#include <netinet6/ipsec6.h> 11278064Sume#endif 11355009Sshin#include <netinet6/ah.h> 11478064Sume#ifdef INET6 11555009Sshin#include <netinet6/ah6.h> 11678064Sume#endif 11753541Sshin#include <netkey/key.h> 11853541Sshin#endif /* IPSEC */ 11953541Sshin 12053541Sshinstruct in6_addr zeroin6_addr; 12153541Sshin 12253541Sshinint 12383366Sjulianin6_pcbbind(inp, nam, td) 12453541Sshin register struct inpcb *inp; 12553541Sshin struct sockaddr *nam; 12683366Sjulian struct thread *td; 12753541Sshin{ 12853541Sshin struct socket *so = inp->inp_socket; 12953541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 13053541Sshin struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 13153541Sshin u_short lport = 0; 13253541Sshin int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 13353541Sshin 13453541Sshin if (!in6_ifaddr) /* XXX broken! */ 13553541Sshin return (EADDRNOTAVAIL); 13653541Sshin if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 13753541Sshin return(EINVAL); 13853541Sshin if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 13953541Sshin wild = 1; 14053541Sshin if (nam) { 14153541Sshin sin6 = (struct sockaddr_in6 *)nam; 14253541Sshin if (nam->sa_len != sizeof(*sin6)) 14353541Sshin return(EINVAL); 14453541Sshin /* 14553541Sshin * family check. 14653541Sshin */ 14753541Sshin if (nam->sa_family != AF_INET6) 14853541Sshin return(EAFNOSUPPORT); 14953541Sshin 15062587Sitojun /* KAME hack: embed scopeid */ 15162587Sitojun if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) 15262587Sitojun return EINVAL; 15362587Sitojun /* this must be cleared for ifa_ifwithaddr() */ 15462587Sitojun sin6->sin6_scope_id = 0; 15553541Sshin 15653541Sshin lport = sin6->sin6_port; 15753541Sshin if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 15853541Sshin /* 15953541Sshin * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 16053541Sshin * allow compepte duplication of binding if 16153541Sshin * SO_REUSEPORT is set, or if SO_REUSEADDR is set 16253541Sshin * and a multicast address is bound on both 16353541Sshin * new and duplicated sockets. 16453541Sshin */ 16553541Sshin if (so->so_options & SO_REUSEADDR) 16653541Sshin reuseport = SO_REUSEADDR|SO_REUSEPORT; 16753541Sshin } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 16853541Sshin struct ifaddr *ia = NULL; 16953541Sshin 17053541Sshin sin6->sin6_port = 0; /* yech... */ 17153541Sshin if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 17253541Sshin return(EADDRNOTAVAIL); 17353541Sshin 17453541Sshin /* 17553541Sshin * XXX: bind to an anycast address might accidentally 17653541Sshin * cause sending a packet with anycast source address. 17778064Sume * We should allow to bind to a deprecated address, since 17878064Sume * the application dare to use it. 17953541Sshin */ 18053541Sshin if (ia && 18153541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 18278064Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 18353541Sshin return(EADDRNOTAVAIL); 18453541Sshin } 18553541Sshin } 18653541Sshin if (lport) { 18753541Sshin struct inpcb *t; 18853541Sshin 18953541Sshin /* GROSS */ 19083366Sjulian if (ntohs(lport) < IPV6PORT_RESERVED && td && 19183366Sjulian suser_xxx(0, td->td_proc, PRISON_ROOT)) 19253541Sshin return(EACCES); 19353541Sshin if (so->so_cred->cr_uid != 0 && 19453541Sshin !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 19555679Sshin t = in6_pcblookup_local(pcbinfo, 19653541Sshin &sin6->sin6_addr, lport, 19753541Sshin INPLOOKUP_WILDCARD); 19853541Sshin if (t && 19953541Sshin (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 20053541Sshin !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 20153541Sshin (t->inp_socket->so_options & 20253541Sshin SO_REUSEPORT) == 0) && 20353541Sshin (so->so_cred->cr_uid != 20453541Sshin t->inp_socket->so_cred->cr_uid)) 20553541Sshin return (EADDRINUSE); 20678064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 20755679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 20855679Sshin struct sockaddr_in sin; 20955679Sshin 21055679Sshin in6_sin6_2_sin(&sin, sin6); 21155679Sshin t = in_pcblookup_local(pcbinfo, 21255679Sshin sin.sin_addr, lport, 21355679Sshin INPLOOKUP_WILDCARD); 21455679Sshin if (t && 21555679Sshin (so->so_cred->cr_uid != 21655679Sshin t->inp_socket->so_cred->cr_uid) && 21755679Sshin (ntohl(t->inp_laddr.s_addr) != 21855679Sshin INADDR_ANY || 21955679Sshin INP_SOCKAF(so) == 22055679Sshin INP_SOCKAF(t->inp_socket))) 22155679Sshin return (EADDRINUSE); 22255679Sshin } 22353541Sshin } 22453541Sshin t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 22553541Sshin lport, wild); 22653541Sshin if (t && (reuseport & t->inp_socket->so_options) == 0) 22753541Sshin return(EADDRINUSE); 22878064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 22955679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 23055679Sshin struct sockaddr_in sin; 23155679Sshin 23255679Sshin in6_sin6_2_sin(&sin, sin6); 23355679Sshin t = in_pcblookup_local(pcbinfo, sin.sin_addr, 23455679Sshin lport, wild); 23555679Sshin if (t && 23655679Sshin (reuseport & t->inp_socket->so_options) 23755679Sshin == 0 && 23855679Sshin (ntohl(t->inp_laddr.s_addr) 23955679Sshin != INADDR_ANY || 24055679Sshin INP_SOCKAF(so) == 24155679Sshin INP_SOCKAF(t->inp_socket))) 24255679Sshin return (EADDRINUSE); 24355679Sshin } 24453541Sshin } 24553541Sshin inp->in6p_laddr = sin6->sin6_addr; 24653541Sshin } 24753541Sshin if (lport == 0) { 24862587Sitojun int e; 24983366Sjulian if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0) 25062587Sitojun return(e); 25162587Sitojun } 25262587Sitojun else { 25362587Sitojun inp->inp_lport = lport; 25462587Sitojun if (in_pcbinshash(inp) != 0) { 25562587Sitojun inp->in6p_laddr = in6addr_any; 25662587Sitojun inp->inp_lport = 0; 25762587Sitojun return (EAGAIN); 25853541Sshin } 25953541Sshin } 26053541Sshin return(0); 26153541Sshin} 26253541Sshin 26353541Sshin/* 26453541Sshin * Transform old in6_pcbconnect() into an inner subroutine for new 26553541Sshin * in6_pcbconnect(): Do some validity-checking on the remote 26653541Sshin * address (in mbuf 'nam') and then determine local host address 26753541Sshin * (i.e., which interface) to use to access that remote host. 26853541Sshin * 26953541Sshin * This preserves definition of in6_pcbconnect(), while supporting a 27053541Sshin * slightly different version for T/TCP. (This is more than 27153541Sshin * a bit of a kludge, but cleaning up the internal interfaces would 27253541Sshin * have forced minor changes in every protocol). 27353541Sshin */ 27453541Sshin 27553541Sshinint 27653541Sshinin6_pcbladdr(inp, nam, plocal_addr6) 27753541Sshin register struct inpcb *inp; 27853541Sshin struct sockaddr *nam; 27953541Sshin struct in6_addr **plocal_addr6; 28053541Sshin{ 28153541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 28253541Sshin struct ifnet *ifp = NULL; 28353541Sshin int error = 0; 28453541Sshin 28553541Sshin if (nam->sa_len != sizeof (*sin6)) 28653541Sshin return (EINVAL); 28753541Sshin if (sin6->sin6_family != AF_INET6) 28853541Sshin return (EAFNOSUPPORT); 28953541Sshin if (sin6->sin6_port == 0) 29053541Sshin return (EADDRNOTAVAIL); 29153541Sshin 29262587Sitojun /* KAME hack: embed scopeid */ 29362587Sitojun if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0) 29462587Sitojun return EINVAL; 29553541Sshin 29653541Sshin if (in6_ifaddr) { 29753541Sshin /* 29853541Sshin * If the destination address is UNSPECIFIED addr, 29953541Sshin * use the loopback addr, e.g ::1. 30053541Sshin */ 30153541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 30253541Sshin sin6->sin6_addr = in6addr_loopback; 30353541Sshin } 30453541Sshin { 30553541Sshin /* 30653541Sshin * XXX: in6_selectsrc might replace the bound local address 30753541Sshin * with the address specified by setsockopt(IPV6_PKTINFO). 30853541Sshin * Is it the intended behavior? 30953541Sshin */ 31053541Sshin *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 31153541Sshin inp->in6p_moptions, 31253541Sshin &inp->in6p_route, 31353541Sshin &inp->in6p_laddr, &error); 31453541Sshin if (*plocal_addr6 == 0) { 31553541Sshin if (error == 0) 31653541Sshin error = EADDRNOTAVAIL; 31753541Sshin return(error); 31853541Sshin } 31953541Sshin /* 32053541Sshin * Don't do pcblookup call here; return interface in 32153541Sshin * plocal_addr6 32253541Sshin * and exit to caller, that will do the lookup. 32353541Sshin */ 32453541Sshin } 32553541Sshin 32653541Sshin if (inp->in6p_route.ro_rt) 32753541Sshin ifp = inp->in6p_route.ro_rt->rt_ifp; 32853541Sshin 32953541Sshin return(0); 33053541Sshin} 33153541Sshin 33253541Sshin/* 33353541Sshin * Outer subroutine: 33453541Sshin * Connect from a socket to a specified address. 33553541Sshin * Both address and port must be specified in argument sin. 33653541Sshin * If don't have a local address for this socket yet, 33753541Sshin * then pick one. 33853541Sshin */ 33953541Sshinint 34083366Sjulianin6_pcbconnect(inp, nam, td) 34153541Sshin register struct inpcb *inp; 34253541Sshin struct sockaddr *nam; 34383366Sjulian struct thread *td; 34453541Sshin{ 34553541Sshin struct in6_addr *addr6; 34653541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 34753541Sshin int error; 34853541Sshin 34953541Sshin /* 35053541Sshin * Call inner routine, to assign local interface address. 35153541Sshin */ 35253541Sshin if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 35353541Sshin return(error); 35453541Sshin 35553541Sshin if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 35653541Sshin sin6->sin6_port, 35753541Sshin IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 35853541Sshin ? addr6 : &inp->in6p_laddr, 35953541Sshin inp->inp_lport, 0, NULL) != NULL) { 36053541Sshin return (EADDRINUSE); 36153541Sshin } 36253541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 36353541Sshin if (inp->inp_lport == 0) { 36483366Sjulian error = in6_pcbbind(inp, (struct sockaddr *)0, td); 36553541Sshin if (error) 36653541Sshin return (error); 36753541Sshin } 36853541Sshin inp->in6p_laddr = *addr6; 36953541Sshin } 37053541Sshin inp->in6p_faddr = sin6->sin6_addr; 37153541Sshin inp->inp_fport = sin6->sin6_port; 37278064Sume /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 37378064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 37478064Sume if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 37555679Sshin inp->in6p_flowinfo |= 37678064Sume (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 37753541Sshin 37853541Sshin in_pcbrehash(inp); 37953541Sshin return (0); 38053541Sshin} 38153541Sshin 38262587Sitojun#if 0 38353541Sshin/* 38453541Sshin * Return an IPv6 address, which is the most appropriate for given 38553541Sshin * destination and user specified options. 38653541Sshin * If necessary, this function lookups the routing table and return 38753541Sshin * an entry to the caller for later use. 38853541Sshin */ 38953541Sshinstruct in6_addr * 39053541Sshinin6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 39153541Sshin struct sockaddr_in6 *dstsock; 39253541Sshin struct ip6_pktopts *opts; 39353541Sshin struct ip6_moptions *mopts; 39453541Sshin struct route_in6 *ro; 39553541Sshin struct in6_addr *laddr; 39653541Sshin int *errorp; 39753541Sshin{ 39853541Sshin struct in6_addr *dst; 39953541Sshin struct in6_ifaddr *ia6 = 0; 40053541Sshin struct in6_pktinfo *pi = NULL; 40153541Sshin 40253541Sshin dst = &dstsock->sin6_addr; 40353541Sshin *errorp = 0; 40453541Sshin 40553541Sshin /* 40653541Sshin * If the source address is explicitly specified by the caller, 40753541Sshin * use it. 40853541Sshin */ 40953541Sshin if (opts && (pi = opts->ip6po_pktinfo) && 41053541Sshin !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 41153541Sshin return(&pi->ipi6_addr); 41253541Sshin 41353541Sshin /* 41453541Sshin * If the source address is not specified but the socket(if any) 41553541Sshin * is already bound, use the bound address. 41653541Sshin */ 41753541Sshin if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 41853541Sshin return(laddr); 41953541Sshin 42053541Sshin /* 42153541Sshin * If the caller doesn't specify the source address but 42253541Sshin * the outgoing interface, use an address associated with 42353541Sshin * the interface. 42453541Sshin */ 42553541Sshin if (pi && pi->ipi6_ifindex) { 42653541Sshin /* XXX boundary check is assumed to be already done. */ 42783130Sjlemon ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst); 42853541Sshin if (ia6 == 0) { 42953541Sshin *errorp = EADDRNOTAVAIL; 43053541Sshin return(0); 43153541Sshin } 43253541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 43353541Sshin } 43453541Sshin 43553541Sshin /* 43653541Sshin * If the destination address is a link-local unicast address or 43753541Sshin * a multicast address, and if the outgoing interface is specified 43853541Sshin * by the sin6_scope_id filed, use an address associated with the 43953541Sshin * interface. 44053541Sshin * XXX: We're now trying to define more specific semantics of 44153541Sshin * sin6_scope_id field, so this part will be rewritten in 44253541Sshin * the near future. 44353541Sshin */ 44453541Sshin if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 44553541Sshin dstsock->sin6_scope_id) { 44653541Sshin /* 44753541Sshin * I'm not sure if boundary check for scope_id is done 44853541Sshin * somewhere... 44953541Sshin */ 45053541Sshin if (dstsock->sin6_scope_id < 0 || 45153541Sshin if_index < dstsock->sin6_scope_id) { 45253541Sshin *errorp = ENXIO; /* XXX: better error? */ 45353541Sshin return(0); 45453541Sshin } 45583130Sjlemon ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id), 45653541Sshin dst); 45753541Sshin if (ia6 == 0) { 45853541Sshin *errorp = EADDRNOTAVAIL; 45953541Sshin return(0); 46053541Sshin } 46153541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 46253541Sshin } 46353541Sshin 46453541Sshin /* 46553541Sshin * If the destination address is a multicast address and 46653541Sshin * the outgoing interface for the address is specified 46753541Sshin * by the caller, use an address associated with the interface. 46853541Sshin * There is a sanity check here; if the destination has node-local 46953541Sshin * scope, the outgoing interfacde should be a loopback address. 47053541Sshin * Even if the outgoing interface is not specified, we also 47153541Sshin * choose a loopback interface as the outgoing interface. 47253541Sshin */ 47353541Sshin if (IN6_IS_ADDR_MULTICAST(dst)) { 47453541Sshin struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 47553541Sshin 47653541Sshin if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 47753541Sshin ifp = &loif[0]; 47853541Sshin } 47953541Sshin 48053541Sshin if (ifp) { 48153541Sshin ia6 = in6_ifawithscope(ifp, dst); 48253541Sshin if (ia6 == 0) { 48353541Sshin *errorp = EADDRNOTAVAIL; 48453541Sshin return(0); 48553541Sshin } 48653541Sshin return(&ia6->ia_addr.sin6_addr); 48753541Sshin } 48853541Sshin } 48953541Sshin 49053541Sshin /* 49153541Sshin * If the next hop address for the packet is specified 49253541Sshin * by caller, use an address associated with the route 49353541Sshin * to the next hop. 49453541Sshin */ 49553541Sshin { 49653541Sshin struct sockaddr_in6 *sin6_next; 49753541Sshin struct rtentry *rt; 49853541Sshin 49953541Sshin if (opts && opts->ip6po_nexthop) { 50053541Sshin sin6_next = satosin6(opts->ip6po_nexthop); 50153541Sshin rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 50253541Sshin if (rt) { 50353541Sshin ia6 = in6_ifawithscope(rt->rt_ifp, dst); 50453541Sshin if (ia6 == 0) 50553541Sshin ia6 = ifatoia6(rt->rt_ifa); 50653541Sshin } 50753541Sshin if (ia6 == 0) { 50853541Sshin *errorp = EADDRNOTAVAIL; 50953541Sshin return(0); 51053541Sshin } 51153541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 51253541Sshin } 51353541Sshin } 51453541Sshin 51553541Sshin /* 51653541Sshin * If route is known or can be allocated now, 51753541Sshin * our src addr is taken from the i/f, else punt. 51853541Sshin */ 51953541Sshin if (ro) { 52053541Sshin if (ro->ro_rt && 52153541Sshin !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 52253541Sshin RTFREE(ro->ro_rt); 52353541Sshin ro->ro_rt = (struct rtentry *)0; 52453541Sshin } 52553541Sshin if (ro->ro_rt == (struct rtentry *)0 || 52653541Sshin ro->ro_rt->rt_ifp == (struct ifnet *)0) { 52778064Sume struct sockaddr_in6 *dst6; 52878064Sume 52953541Sshin /* No route yet, so try to acquire one */ 53053541Sshin bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 53178064Sume dst6 = (struct sockaddr_in6 *)&ro->ro_dst; 53278064Sume dst6->sin6_family = AF_INET6; 53378064Sume dst6->sin6_len = sizeof(struct sockaddr_in6); 53478064Sume dst6->sin6_addr = *dst; 53553541Sshin if (IN6_IS_ADDR_MULTICAST(dst)) { 53653541Sshin ro->ro_rt = rtalloc1(&((struct route *)ro) 53753541Sshin ->ro_dst, 0, 0UL); 53853541Sshin } else { 53954350Sshin rtalloc((struct route *)ro); 54053541Sshin } 54153541Sshin } 54253541Sshin 54353541Sshin /* 54453541Sshin * in_pcbconnect() checks out IFF_LOOPBACK to skip using 54553541Sshin * the address. But we don't know why it does so. 54653541Sshin * It is necessary to ensure the scope even for lo0 54753541Sshin * so doesn't check out IFF_LOOPBACK. 54853541Sshin */ 54953541Sshin 55053541Sshin if (ro->ro_rt) { 55153541Sshin ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 55253541Sshin if (ia6 == 0) /* xxx scope error ?*/ 55353541Sshin ia6 = ifatoia6(ro->ro_rt->rt_ifa); 55453541Sshin } 55553541Sshin if (ia6 == 0) { 55653541Sshin *errorp = EHOSTUNREACH; /* no route */ 55753541Sshin return(0); 55853541Sshin } 55953541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 56053541Sshin } 56153541Sshin 56253541Sshin *errorp = EADDRNOTAVAIL; 56353541Sshin return(0); 56453541Sshin} 56553541Sshin 56653541Sshin/* 56753541Sshin * Default hop limit selection. The precedence is as follows: 56853541Sshin * 1. Hoplimit valued specified via ioctl. 56953541Sshin * 2. (If the outgoing interface is detected) the current 57053541Sshin * hop limit of the interface specified by router advertisement. 57153541Sshin * 3. The system default hoplimit. 57253541Sshin*/ 57353541Sshinint 57453541Sshinin6_selecthlim(in6p, ifp) 57553541Sshin struct in6pcb *in6p; 57653541Sshin struct ifnet *ifp; 57753541Sshin{ 57853541Sshin if (in6p && in6p->in6p_hops >= 0) 57953541Sshin return(in6p->in6p_hops); 58053541Sshin else if (ifp) 58153541Sshin return(nd_ifinfo[ifp->if_index].chlim); 58253541Sshin else 58353541Sshin return(ip6_defhlim); 58453541Sshin} 58562587Sitojun#endif 58653541Sshin 58753541Sshinvoid 58853541Sshinin6_pcbdisconnect(inp) 58953541Sshin struct inpcb *inp; 59053541Sshin{ 59153541Sshin bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 59253541Sshin inp->inp_fport = 0; 59378064Sume /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 59478064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 59553541Sshin in_pcbrehash(inp); 59653541Sshin if (inp->inp_socket->so_state & SS_NOFDREF) 59753541Sshin in6_pcbdetach(inp); 59853541Sshin} 59953541Sshin 60053541Sshinvoid 60153541Sshinin6_pcbdetach(inp) 60253541Sshin struct inpcb *inp; 60353541Sshin{ 60453541Sshin struct socket *so = inp->inp_socket; 60553541Sshin struct inpcbinfo *ipi = inp->inp_pcbinfo; 60653541Sshin 60753541Sshin#ifdef IPSEC 60858452Sgreen if (inp->in6p_sp != NULL) 60958452Sgreen ipsec6_delete_pcbpolicy(inp); 61053541Sshin#endif /* IPSEC */ 61153541Sshin inp->inp_gencnt = ++ipi->ipi_gencnt; 61253541Sshin in_pcbremlists(inp); 61353541Sshin sotoinpcb(so) = 0; 61453541Sshin sofree(so); 61578064Sume 61653541Sshin if (inp->in6p_options) 61753541Sshin m_freem(inp->in6p_options); 61878064Sume ip6_freepcbopts(inp->in6p_outputopts); 61978064Sume ip6_freemoptions(inp->in6p_moptions); 62053541Sshin if (inp->in6p_route.ro_rt) 62153541Sshin rtfree(inp->in6p_route.ro_rt); 62255679Sshin /* Check and free IPv4 related resources in case of mapped addr */ 62355679Sshin if (inp->inp_options) 62455679Sshin (void)m_free(inp->inp_options); 62555679Sshin ip_freemoptions(inp->inp_moptions); 62655679Sshin 62753541Sshin inp->inp_vflag = 0; 62871350Sdes zfree(ipi->ipi_zone, inp); 62953541Sshin} 63053541Sshin 63153541Sshin/* 63253541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 63353541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points 63453541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly 63553541Sshin * without the need for a wrapper function. The socket must have a valid 63653541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 63753541Sshin * except through a kernel programming error, so it is acceptable to panic 63853541Sshin * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 63953541Sshin * because there actually /is/ a programming error somewhere... XXX) 64053541Sshin */ 64153541Sshinint 64253541Sshinin6_setsockaddr(so, nam) 64353541Sshin struct socket *so; 64453541Sshin struct sockaddr **nam; 64553541Sshin{ 64653541Sshin int s; 64753541Sshin register struct inpcb *inp; 64853541Sshin register struct sockaddr_in6 *sin6; 64953541Sshin 65053541Sshin /* 65153541Sshin * Do the malloc first in case it blocks. 65253541Sshin */ 65353541Sshin MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 65453541Sshin bzero(sin6, sizeof *sin6); 65553541Sshin sin6->sin6_family = AF_INET6; 65653541Sshin sin6->sin6_len = sizeof(*sin6); 65753541Sshin 65853541Sshin s = splnet(); 65953541Sshin inp = sotoinpcb(so); 66053541Sshin if (!inp) { 66153541Sshin splx(s); 66253541Sshin free(sin6, M_SONAME); 66353541Sshin return EINVAL; 66453541Sshin } 66553541Sshin sin6->sin6_port = inp->inp_lport; 66653541Sshin sin6->sin6_addr = inp->in6p_laddr; 66753541Sshin splx(s); 66853541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 66953541Sshin sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 67053541Sshin else 67153541Sshin sin6->sin6_scope_id = 0; /*XXX*/ 67253541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 67353541Sshin sin6->sin6_addr.s6_addr16[1] = 0; 67453541Sshin 67553541Sshin *nam = (struct sockaddr *)sin6; 67653541Sshin return 0; 67753541Sshin} 67853541Sshin 67953541Sshinint 68053541Sshinin6_setpeeraddr(so, nam) 68153541Sshin struct socket *so; 68253541Sshin struct sockaddr **nam; 68353541Sshin{ 68453541Sshin int s; 68553541Sshin struct inpcb *inp; 68653541Sshin register struct sockaddr_in6 *sin6; 68753541Sshin 68853541Sshin /* 68953541Sshin * Do the malloc first in case it blocks. 69053541Sshin */ 69153541Sshin MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK); 69253541Sshin bzero((caddr_t)sin6, sizeof (*sin6)); 69353541Sshin sin6->sin6_family = AF_INET6; 69453541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 69553541Sshin 69653541Sshin s = splnet(); 69753541Sshin inp = sotoinpcb(so); 69853541Sshin if (!inp) { 69953541Sshin splx(s); 70053541Sshin free(sin6, M_SONAME); 70153541Sshin return EINVAL; 70253541Sshin } 70353541Sshin sin6->sin6_port = inp->inp_fport; 70453541Sshin sin6->sin6_addr = inp->in6p_faddr; 70553541Sshin splx(s); 70653541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 70753541Sshin sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 70853541Sshin else 70953541Sshin sin6->sin6_scope_id = 0; /*XXX*/ 71053541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 71153541Sshin sin6->sin6_addr.s6_addr16[1] = 0; 71253541Sshin 71353541Sshin *nam = (struct sockaddr *)sin6; 71453541Sshin return 0; 71553541Sshin} 71653541Sshin 71753541Sshinint 71853541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 71953541Sshin{ 72053541Sshin struct inpcb *inp = sotoinpcb(so); 72153541Sshin int error; 72253541Sshin 72353541Sshin if (inp == NULL) 72453541Sshin return EINVAL; 72553541Sshin if (inp->inp_vflag & INP_IPV4) { 72653541Sshin error = in_setsockaddr(so, nam); 72754952Seivind if (error == 0) 72853541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 72953541Sshin } else 73053541Sshin error = in6_setsockaddr(so, nam); 73153541Sshin 73253541Sshin return error; 73353541Sshin} 73453541Sshin 73553541Sshinint 73653541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 73753541Sshin{ 73853541Sshin struct inpcb *inp = sotoinpcb(so); 73953541Sshin int error; 74053541Sshin 74153541Sshin if (inp == NULL) 74253541Sshin return EINVAL; 74353541Sshin if (inp->inp_vflag & INP_IPV4) { 74453541Sshin error = in_setpeeraddr(so, nam); 74554952Seivind if (error == 0) 74653541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 74753541Sshin } else 74853541Sshin error = in6_setpeeraddr(so, nam); 74953541Sshin 75053541Sshin return error; 75153541Sshin} 75253541Sshin 75353541Sshin/* 75453541Sshin * Pass some notification to all connections of a protocol 75553541Sshin * associated with address dst. The local address and/or port numbers 75653541Sshin * may be specified to limit the search. The "usual action" will be 75753541Sshin * taken, depending on the ctlinput cmd. The caller must filter any 75853541Sshin * cmds that are uninteresting (e.g., no error in the map). 75953541Sshin * Call the protocol specific routine (if any) to report 76053541Sshin * any errors for each matching socket. 76153541Sshin * 76253541Sshin * Must be called at splnet. 76353541Sshin */ 76453541Sshinvoid 76578064Sumein6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify) 76653541Sshin struct inpcbhead *head; 76778064Sume struct sockaddr *dst, *src; 76853541Sshin u_int fport_arg, lport_arg; 76953541Sshin int cmd; 77053541Sshin void (*notify) __P((struct inpcb *, int)); 77153541Sshin{ 77262587Sitojun struct inpcb *inp, *ninp; 77378064Sume struct sockaddr_in6 sa6_src, *sa6_dst; 77453541Sshin u_short fport = fport_arg, lport = lport_arg; 77578064Sume u_int32_t flowinfo; 77653541Sshin int errno, s; 77753541Sshin 77853541Sshin if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 77953541Sshin return; 78078064Sume 78178064Sume sa6_dst = (struct sockaddr_in6 *)dst; 78278064Sume if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 78353541Sshin return; 78453541Sshin 78553541Sshin /* 78678064Sume * note that src can be NULL when we get notify by local fragmentation. 78778064Sume */ 78878064Sume sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src; 78978064Sume flowinfo = sa6_src.sin6_flowinfo; 79078064Sume 79178064Sume /* 79253541Sshin * Redirects go to all references to the destination, 79362587Sitojun * and use in6_rtchange to invalidate the route cache. 79462587Sitojun * Dead host indications: also use in6_rtchange to invalidate 79562587Sitojun * the cache, and deliver the error to all the sockets. 79653541Sshin * Otherwise, if we have knowledge of the local port and address, 79753541Sshin * deliver only to that socket. 79853541Sshin */ 79953541Sshin if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 80053541Sshin fport = 0; 80153541Sshin lport = 0; 80278064Sume bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 80362587Sitojun 80478064Sume if (cmd != PRC_HOSTDEAD) 80578064Sume notify = in6_rtchange; 80653541Sshin } 80753541Sshin errno = inet6ctlerrmap[cmd]; 80853541Sshin s = splnet(); 80962587Sitojun for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 81062587Sitojun ninp = LIST_NEXT(inp, inp_list); 81162587Sitojun 81278064Sume if ((inp->inp_vflag & INP_IPV6) == 0) 81353541Sshin continue; 81462587Sitojun 81578064Sume /* 81678064Sume * Detect if we should notify the error. If no source and 81778064Sume * destination ports are specifed, but non-zero flowinfo and 81878064Sume * local address match, notify the error. This is the case 81978064Sume * when the error is delivered with an encrypted buffer 82078064Sume * by ESP. Otherwise, just compare addresses and ports 82178064Sume * as usual. 82278064Sume */ 82378064Sume if (lport == 0 && fport == 0 && flowinfo && 82478064Sume inp->inp_socket != NULL && 82578064Sume flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 82678064Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 82778064Sume goto do_notify; 82878064Sume else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 82978064Sume &sa6_dst->sin6_addr) || 83078064Sume inp->inp_socket == 0 || 83178064Sume (lport && inp->inp_lport != lport) || 83278064Sume (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 83378064Sume !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 83478064Sume &sa6_src.sin6_addr)) || 83578064Sume (fport && inp->inp_fport != fport)) 83653541Sshin continue; 83762587Sitojun 83878064Sume do_notify: 83953541Sshin if (notify) 84078064Sume (*notify)(inp, errno); 84153541Sshin } 84253541Sshin splx(s); 84353541Sshin} 84453541Sshin 84553541Sshin/* 84653541Sshin * Lookup a PCB based on the local address and port. 84753541Sshin */ 84853541Sshinstruct inpcb * 84953541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 85053541Sshin struct inpcbinfo *pcbinfo; 85153541Sshin struct in6_addr *laddr; 85253541Sshin u_int lport_arg; 85353541Sshin int wild_okay; 85453541Sshin{ 85553541Sshin register struct inpcb *inp; 85653541Sshin int matchwild = 3, wildcard; 85753541Sshin u_short lport = lport_arg; 85853541Sshin 85953541Sshin if (!wild_okay) { 86053541Sshin struct inpcbhead *head; 86153541Sshin /* 86253541Sshin * Look for an unconnected (wildcard foreign addr) PCB that 86353541Sshin * matches the local address and port we're looking for. 86453541Sshin */ 86553541Sshin head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 86653541Sshin pcbinfo->hashmask)]; 86753541Sshin LIST_FOREACH(inp, head, inp_hash) { 86854952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 86953541Sshin continue; 87053541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 87153541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 87253541Sshin inp->inp_lport == lport) { 87353541Sshin /* 87453541Sshin * Found. 87553541Sshin */ 87653541Sshin return (inp); 87753541Sshin } 87853541Sshin } 87953541Sshin /* 88053541Sshin * Not found. 88153541Sshin */ 88253541Sshin return (NULL); 88353541Sshin } else { 88453541Sshin struct inpcbporthead *porthash; 88553541Sshin struct inpcbport *phd; 88653541Sshin struct inpcb *match = NULL; 88753541Sshin /* 88853541Sshin * Best fit PCB lookup. 88953541Sshin * 89053541Sshin * First see if this local port is in use by looking on the 89153541Sshin * port hash list. 89253541Sshin */ 89353541Sshin porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 89453541Sshin pcbinfo->porthashmask)]; 89553541Sshin LIST_FOREACH(phd, porthash, phd_hash) { 89653541Sshin if (phd->phd_port == lport) 89753541Sshin break; 89853541Sshin } 89953541Sshin if (phd != NULL) { 90053541Sshin /* 90153541Sshin * Port is in use by one or more PCBs. Look for best 90253541Sshin * fit. 90353541Sshin */ 90453541Sshin LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 90553541Sshin wildcard = 0; 90654952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 90753541Sshin continue; 90853541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 90953541Sshin wildcard++; 91053541Sshin if (!IN6_IS_ADDR_UNSPECIFIED( 91153541Sshin &inp->in6p_laddr)) { 91253541Sshin if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 91353541Sshin wildcard++; 91453541Sshin else if (!IN6_ARE_ADDR_EQUAL( 91553541Sshin &inp->in6p_laddr, laddr)) 91653541Sshin continue; 91753541Sshin } else { 91853541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 91953541Sshin wildcard++; 92053541Sshin } 92153541Sshin if (wildcard < matchwild) { 92253541Sshin match = inp; 92353541Sshin matchwild = wildcard; 92453541Sshin if (matchwild == 0) { 92553541Sshin break; 92653541Sshin } 92753541Sshin } 92853541Sshin } 92953541Sshin } 93053541Sshin return (match); 93153541Sshin } 93253541Sshin} 93353541Sshin 93481127Sumevoid 93581127Sumein6_pcbpurgeif0(head, ifp) 93681127Sume struct in6pcb *head; 93781127Sume struct ifnet *ifp; 93881127Sume{ 93981127Sume struct in6pcb *in6p; 94081127Sume struct ip6_moptions *im6o; 94181127Sume struct in6_multi_mship *imm, *nimm; 94281127Sume 94381127Sume for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) { 94481127Sume im6o = in6p->in6p_moptions; 94581127Sume if ((in6p->inp_vflag & INP_IPV6) && 94681127Sume im6o) { 94781127Sume /* 94881127Sume * Unselect the outgoing interface if it is being 94981127Sume * detached. 95081127Sume */ 95181127Sume if (im6o->im6o_multicast_ifp == ifp) 95281127Sume im6o->im6o_multicast_ifp = NULL; 95381127Sume 95481127Sume /* 95581127Sume * Drop multicast group membership if we joined 95681127Sume * through the interface being detached. 95781127Sume * XXX controversial - is it really legal for kernel 95881127Sume * to force this? 95981127Sume */ 96081127Sume for (imm = im6o->im6o_memberships.lh_first; 96181127Sume imm != NULL; imm = nimm) { 96281127Sume nimm = imm->i6mm_chain.le_next; 96381127Sume if (imm->i6mm_maddr->in6m_ifp == ifp) { 96481127Sume LIST_REMOVE(imm, i6mm_chain); 96581127Sume in6_delmulti(imm->i6mm_maddr); 96681127Sume free(imm, M_IPMADDR); 96781127Sume } 96881127Sume } 96981127Sume } 97081127Sume } 97181127Sume} 97281127Sume 97353541Sshin/* 97453541Sshin * Check for alternatives when higher level complains 97553541Sshin * about service problems. For now, invalidate cached 97653541Sshin * routing information. If the route was created dynamically 97753541Sshin * (by a redirect), time to try a default gateway again. 97853541Sshin */ 97953541Sshinvoid 98053541Sshinin6_losing(in6p) 98153541Sshin struct inpcb *in6p; 98253541Sshin{ 98353541Sshin struct rtentry *rt; 98453541Sshin struct rt_addrinfo info; 98553541Sshin 98653541Sshin if ((rt = in6p->in6p_route.ro_rt) != NULL) { 98753541Sshin in6p->in6p_route.ro_rt = 0; 98853541Sshin bzero((caddr_t)&info, sizeof(info)); 98953541Sshin info.rti_info[RTAX_DST] = 99053541Sshin (struct sockaddr *)&in6p->in6p_route.ro_dst; 99153541Sshin info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 99253541Sshin info.rti_info[RTAX_NETMASK] = rt_mask(rt); 99353541Sshin rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 99453541Sshin if (rt->rt_flags & RTF_DYNAMIC) 99553541Sshin (void)rtrequest(RTM_DELETE, rt_key(rt), 99653541Sshin rt->rt_gateway, rt_mask(rt), rt->rt_flags, 99753541Sshin (struct rtentry **)0); 99853541Sshin else 99953541Sshin /* 100053541Sshin * A new route can be allocated 100153541Sshin * the next time output is attempted. 100253541Sshin */ 100353541Sshin rtfree(rt); 100453541Sshin } 100553541Sshin} 100653541Sshin 100753541Sshin/* 100853541Sshin * After a routing change, flush old routing 100953541Sshin * and allocate a (hopefully) better one. 101053541Sshin */ 101153541Sshinvoid 101253541Sshinin6_rtchange(inp, errno) 101353541Sshin struct inpcb *inp; 101453541Sshin int errno; 101553541Sshin{ 101653541Sshin if (inp->in6p_route.ro_rt) { 101753541Sshin rtfree(inp->in6p_route.ro_rt); 101853541Sshin inp->in6p_route.ro_rt = 0; 101953541Sshin /* 102053541Sshin * A new route can be allocated the next time 102153541Sshin * output is attempted. 102253541Sshin */ 102353541Sshin } 102453541Sshin} 102553541Sshin 102653541Sshin/* 102753541Sshin * Lookup PCB in hash list. 102853541Sshin */ 102953541Sshinstruct inpcb * 103053541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 103153541Sshin struct inpcbinfo *pcbinfo; 103253541Sshin struct in6_addr *faddr, *laddr; 103353541Sshin u_int fport_arg, lport_arg; 103453541Sshin int wildcard; 103553541Sshin struct ifnet *ifp; 103653541Sshin{ 103753541Sshin struct inpcbhead *head; 103853541Sshin register struct inpcb *inp; 103953541Sshin u_short fport = fport_arg, lport = lport_arg; 104078064Sume int faith; 104153541Sshin 104278064Sume#if defined(NFAITH) && NFAITH > 0 104378064Sume faith = faithprefix(laddr); 104478064Sume#else 104578064Sume faith = 0; 104678064Sume#endif 104778064Sume 104853541Sshin /* 104953541Sshin * First look for an exact match. 105053541Sshin */ 105153541Sshin head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, 105253541Sshin lport, fport, 105353541Sshin pcbinfo->hashmask)]; 105454263Sshin LIST_FOREACH(inp, head, inp_hash) { 105554952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 105653541Sshin continue; 105753541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 105853541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 105953541Sshin inp->inp_fport == fport && 106053541Sshin inp->inp_lport == lport) { 106153541Sshin /* 106253541Sshin * Found. 106353541Sshin */ 106453541Sshin return (inp); 106553541Sshin } 106653541Sshin } 106753541Sshin if (wildcard) { 106853541Sshin struct inpcb *local_wild = NULL; 106953541Sshin 107053541Sshin head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 107153541Sshin pcbinfo->hashmask)]; 107254263Sshin LIST_FOREACH(inp, head, inp_hash) { 107354952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 107453541Sshin continue; 107553541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 107653541Sshin inp->inp_lport == lport) { 107778064Sume if (faith && (inp->inp_flags & INP_FAITH) == 0) 107853541Sshin continue; 107953541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 108053541Sshin laddr)) 108153541Sshin return (inp); 108253541Sshin else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 108353541Sshin local_wild = inp; 108453541Sshin } 108553541Sshin } 108653541Sshin return (local_wild); 108753541Sshin } 108853541Sshin 108953541Sshin /* 109053541Sshin * Not found. 109153541Sshin */ 109253541Sshin return (NULL); 109353541Sshin} 109453541Sshin 109553541Sshinvoid 109653541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 109753541Sshin{ 109853541Sshin struct ip6_hdr *ip; 109953541Sshin 110053541Sshin ip = mtod(m, struct ip6_hdr *); 110153541Sshin bzero(sin6, sizeof(*sin6)); 110253541Sshin sin6->sin6_len = sizeof(*sin6); 110353541Sshin sin6->sin6_family = AF_INET6; 110453541Sshin sin6->sin6_addr = ip->ip6_src; 110553541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 110653541Sshin sin6->sin6_addr.s6_addr16[1] = 0; 110753541Sshin sin6->sin6_scope_id = 110853541Sshin (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 110953541Sshin ? m->m_pkthdr.rcvif->if_index : 0; 111053541Sshin 111153541Sshin return; 111253541Sshin} 1113