in6_pcb.c revision 111119
162587Sitojun/* $FreeBSD: head/sys/netinet6/in6_pcb.c 111119 2003-02-19 05:47:46Z imp $ */ 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 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> 9953541Sshin#include <netinet6/ip6_var.h> 10053541Sshin#include <netinet6/nd6.h> 10153541Sshin#include <netinet/in_pcb.h> 10253541Sshin#include <netinet6/in6_pcb.h> 10353541Sshin 10453541Sshin#ifdef IPSEC 10553541Sshin#include <netinet6/ipsec.h> 10678064Sume#ifdef INET6 10778064Sume#include <netinet6/ipsec6.h> 10878064Sume#endif 10955009Sshin#include <netinet6/ah.h> 11078064Sume#ifdef INET6 11155009Sshin#include <netinet6/ah6.h> 11278064Sume#endif 11353541Sshin#include <netkey/key.h> 11453541Sshin#endif /* IPSEC */ 11553541Sshin 116105199Ssam#ifdef FAST_IPSEC 117105199Ssam#include <netipsec/ipsec.h> 118105199Ssam#include <netipsec/ipsec6.h> 119105199Ssam#include <netipsec/key.h> 120105199Ssam#define IPSEC 121105199Ssam#endif /* FAST_IPSEC */ 122105199Ssam 12353541Sshinstruct in6_addr zeroin6_addr; 12453541Sshin 12553541Sshinint 12683366Sjulianin6_pcbbind(inp, nam, td) 12753541Sshin register struct inpcb *inp; 12853541Sshin struct sockaddr *nam; 12983366Sjulian struct thread *td; 13053541Sshin{ 13153541Sshin struct socket *so = inp->inp_socket; 13253541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 13353541Sshin struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 13453541Sshin u_short lport = 0; 13597658Stanimura int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 13653541Sshin 13753541Sshin if (!in6_ifaddr) /* XXX broken! */ 13853541Sshin return (EADDRNOTAVAIL); 13953541Sshin if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 14053541Sshin return(EINVAL); 14153541Sshin if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 14253541Sshin wild = 1; 14353541Sshin if (nam) { 14453541Sshin sin6 = (struct sockaddr_in6 *)nam; 14553541Sshin if (nam->sa_len != sizeof(*sin6)) 14653541Sshin return(EINVAL); 14753541Sshin /* 14853541Sshin * family check. 14953541Sshin */ 15053541Sshin if (nam->sa_family != AF_INET6) 15153541Sshin return(EAFNOSUPPORT); 15253541Sshin 15362587Sitojun /* KAME hack: embed scopeid */ 15462587Sitojun if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) 15562587Sitojun return EINVAL; 15662587Sitojun /* this must be cleared for ifa_ifwithaddr() */ 15762587Sitojun sin6->sin6_scope_id = 0; 15853541Sshin 15953541Sshin lport = sin6->sin6_port; 16053541Sshin if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 16153541Sshin /* 16253541Sshin * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 16353541Sshin * allow compepte duplication of binding if 16453541Sshin * SO_REUSEPORT is set, or if SO_REUSEADDR is set 16553541Sshin * and a multicast address is bound on both 16653541Sshin * new and duplicated sockets. 16753541Sshin */ 16853541Sshin if (so->so_options & SO_REUSEADDR) 16953541Sshin reuseport = SO_REUSEADDR|SO_REUSEPORT; 17053541Sshin } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 17153541Sshin struct ifaddr *ia = NULL; 17253541Sshin 17353541Sshin sin6->sin6_port = 0; /* yech... */ 17453541Sshin if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 17553541Sshin return(EADDRNOTAVAIL); 17653541Sshin 17753541Sshin /* 17853541Sshin * XXX: bind to an anycast address might accidentally 17953541Sshin * cause sending a packet with anycast source address. 18078064Sume * We should allow to bind to a deprecated address, since 18178064Sume * the application dare to use it. 18253541Sshin */ 18353541Sshin if (ia && 18453541Sshin ((struct in6_ifaddr *)ia)->ia6_flags & 18578064Sume (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 18653541Sshin return(EADDRNOTAVAIL); 18753541Sshin } 18853541Sshin } 18953541Sshin if (lport) { 19053541Sshin struct inpcb *t; 19153541Sshin 19253541Sshin /* GROSS */ 19383366Sjulian if (ntohs(lport) < IPV6PORT_RESERVED && td && 19493593Sjhb suser_cred(td->td_ucred, PRISON_ROOT)) 19553541Sshin return(EACCES); 19653541Sshin if (so->so_cred->cr_uid != 0 && 19753541Sshin !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 19855679Sshin t = in6_pcblookup_local(pcbinfo, 19953541Sshin &sin6->sin6_addr, lport, 20053541Sshin INPLOOKUP_WILDCARD); 20197658Stanimura if (t && 20297658Stanimura (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 20397658Stanimura !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 20497658Stanimura (t->inp_socket->so_options & 20597658Stanimura SO_REUSEPORT) == 0) && 20697658Stanimura (so->so_cred->cr_uid != 20797658Stanimura t->inp_socket->so_cred->cr_uid)) 20897658Stanimura return (EADDRINUSE); 20978064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 21055679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 21155679Sshin struct sockaddr_in sin; 21255679Sshin 21355679Sshin in6_sin6_2_sin(&sin, sin6); 21455679Sshin t = in_pcblookup_local(pcbinfo, 21555679Sshin sin.sin_addr, lport, 21655679Sshin INPLOOKUP_WILDCARD); 21755679Sshin if (t && 21855679Sshin (so->so_cred->cr_uid != 21955679Sshin t->inp_socket->so_cred->cr_uid) && 22055679Sshin (ntohl(t->inp_laddr.s_addr) != 22155679Sshin INADDR_ANY || 22255679Sshin INP_SOCKAF(so) == 22355679Sshin INP_SOCKAF(t->inp_socket))) 22455679Sshin return (EADDRINUSE); 22555679Sshin } 22653541Sshin } 22753541Sshin t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 22853541Sshin lport, wild); 22997658Stanimura if (t && (reuseport & t->inp_socket->so_options) == 0) 23097658Stanimura return(EADDRINUSE); 23178064Sume if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 23255679Sshin IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 23355679Sshin struct sockaddr_in sin; 23455679Sshin 23555679Sshin in6_sin6_2_sin(&sin, sin6); 23655679Sshin t = in_pcblookup_local(pcbinfo, sin.sin_addr, 23755679Sshin lport, wild); 23897658Stanimura if (t && 23997658Stanimura (reuseport & t->inp_socket->so_options) 24097658Stanimura == 0 && 24197658Stanimura (ntohl(t->inp_laddr.s_addr) 24297658Stanimura != INADDR_ANY || 24397658Stanimura 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; 25283366Sjulian if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0) 25362587Sitojun 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 } 26353541Sshin 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 */ 27753541Sshin 27853541Sshinint 27953541Sshinin6_pcbladdr(inp, nam, plocal_addr6) 28053541Sshin register struct inpcb *inp; 28153541Sshin struct sockaddr *nam; 28253541Sshin struct in6_addr **plocal_addr6; 28353541Sshin{ 28453541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 28553541Sshin struct ifnet *ifp = NULL; 28653541Sshin int error = 0; 28753541Sshin 28853541Sshin if (nam->sa_len != sizeof (*sin6)) 28953541Sshin return (EINVAL); 29053541Sshin if (sin6->sin6_family != AF_INET6) 29153541Sshin return (EAFNOSUPPORT); 29253541Sshin if (sin6->sin6_port == 0) 29353541Sshin return (EADDRNOTAVAIL); 29453541Sshin 29562587Sitojun /* KAME hack: embed scopeid */ 29662587Sitojun if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0) 29762587Sitojun return EINVAL; 29853541Sshin 29953541Sshin if (in6_ifaddr) { 30053541Sshin /* 30153541Sshin * If the destination address is UNSPECIFIED addr, 30253541Sshin * use the loopback addr, e.g ::1. 30353541Sshin */ 30453541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 30553541Sshin sin6->sin6_addr = in6addr_loopback; 30653541Sshin } 30753541Sshin { 30853541Sshin /* 30953541Sshin * XXX: in6_selectsrc might replace the bound local address 31053541Sshin * with the address specified by setsockopt(IPV6_PKTINFO). 31153541Sshin * Is it the intended behavior? 31253541Sshin */ 31353541Sshin *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 31453541Sshin inp->in6p_moptions, 31553541Sshin &inp->in6p_route, 31653541Sshin &inp->in6p_laddr, &error); 31753541Sshin if (*plocal_addr6 == 0) { 31853541Sshin if (error == 0) 31953541Sshin error = EADDRNOTAVAIL; 32053541Sshin return(error); 32153541Sshin } 32253541Sshin /* 32353541Sshin * Don't do pcblookup call here; return interface in 32453541Sshin * plocal_addr6 32553541Sshin * and exit to caller, that will do the lookup. 32653541Sshin */ 32753541Sshin } 32853541Sshin 32953541Sshin if (inp->in6p_route.ro_rt) 33053541Sshin ifp = inp->in6p_route.ro_rt->rt_ifp; 33153541Sshin 33253541Sshin return(0); 33353541Sshin} 33453541Sshin 33553541Sshin/* 33653541Sshin * Outer subroutine: 33753541Sshin * Connect from a socket to a specified address. 33853541Sshin * Both address and port must be specified in argument sin. 33953541Sshin * If don't have a local address for this socket yet, 34053541Sshin * then pick one. 34153541Sshin */ 34253541Sshinint 34383366Sjulianin6_pcbconnect(inp, nam, td) 34453541Sshin register struct inpcb *inp; 34553541Sshin struct sockaddr *nam; 34683366Sjulian struct thread *td; 34753541Sshin{ 34853541Sshin struct in6_addr *addr6; 34953541Sshin register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 35053541Sshin int error; 35153541Sshin 35253541Sshin /* 35395023Ssuz * Call inner routine, to assign local interface address. 35495023Ssuz * in6_pcbladdr() may automatically fill in sin6_scope_id. 35553541Sshin */ 35653541Sshin if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 35753541Sshin return(error); 35853541Sshin 35953541Sshin if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 36053541Sshin sin6->sin6_port, 36153541Sshin IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 36253541Sshin ? addr6 : &inp->in6p_laddr, 36353541Sshin inp->inp_lport, 0, NULL) != NULL) { 36453541Sshin return (EADDRINUSE); 36553541Sshin } 36653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 36753541Sshin if (inp->inp_lport == 0) { 36883366Sjulian error = in6_pcbbind(inp, (struct sockaddr *)0, td); 36953541Sshin if (error) 37053541Sshin return (error); 37153541Sshin } 37253541Sshin inp->in6p_laddr = *addr6; 37353541Sshin } 37453541Sshin inp->in6p_faddr = sin6->sin6_addr; 37553541Sshin inp->inp_fport = sin6->sin6_port; 37678064Sume /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 37778064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 37878064Sume if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 37955679Sshin inp->in6p_flowinfo |= 38078064Sume (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 38153541Sshin 38253541Sshin in_pcbrehash(inp); 38353541Sshin return (0); 38453541Sshin} 38553541Sshin 38662587Sitojun#if 0 38753541Sshin/* 38853541Sshin * Return an IPv6 address, which is the most appropriate for given 38953541Sshin * destination and user specified options. 39053541Sshin * If necessary, this function lookups the routing table and return 39153541Sshin * an entry to the caller for later use. 39253541Sshin */ 39353541Sshinstruct in6_addr * 39453541Sshinin6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 39553541Sshin struct sockaddr_in6 *dstsock; 39653541Sshin struct ip6_pktopts *opts; 39753541Sshin struct ip6_moptions *mopts; 39853541Sshin struct route_in6 *ro; 39953541Sshin struct in6_addr *laddr; 40053541Sshin int *errorp; 40153541Sshin{ 40253541Sshin struct in6_addr *dst; 40353541Sshin struct in6_ifaddr *ia6 = 0; 40453541Sshin struct in6_pktinfo *pi = NULL; 40553541Sshin 40653541Sshin dst = &dstsock->sin6_addr; 40753541Sshin *errorp = 0; 40853541Sshin 40953541Sshin /* 41053541Sshin * If the source address is explicitly specified by the caller, 41153541Sshin * use it. 41253541Sshin */ 41353541Sshin if (opts && (pi = opts->ip6po_pktinfo) && 41453541Sshin !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 41553541Sshin return(&pi->ipi6_addr); 41653541Sshin 41753541Sshin /* 41853541Sshin * If the source address is not specified but the socket(if any) 41953541Sshin * is already bound, use the bound address. 42053541Sshin */ 42153541Sshin if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 42253541Sshin return(laddr); 42353541Sshin 42453541Sshin /* 42553541Sshin * If the caller doesn't specify the source address but 42653541Sshin * the outgoing interface, use an address associated with 42753541Sshin * the interface. 42853541Sshin */ 42953541Sshin if (pi && pi->ipi6_ifindex) { 43053541Sshin /* XXX boundary check is assumed to be already done. */ 43183130Sjlemon ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst); 43253541Sshin if (ia6 == 0) { 43353541Sshin *errorp = EADDRNOTAVAIL; 43453541Sshin return(0); 43553541Sshin } 43653541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 43753541Sshin } 43853541Sshin 43953541Sshin /* 44053541Sshin * If the destination address is a link-local unicast address or 44153541Sshin * a multicast address, and if the outgoing interface is specified 44253541Sshin * by the sin6_scope_id filed, use an address associated with the 44353541Sshin * interface. 44453541Sshin * XXX: We're now trying to define more specific semantics of 44553541Sshin * sin6_scope_id field, so this part will be rewritten in 44653541Sshin * the near future. 44753541Sshin */ 44853541Sshin if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 44953541Sshin dstsock->sin6_scope_id) { 45053541Sshin /* 45153541Sshin * I'm not sure if boundary check for scope_id is done 45253541Sshin * somewhere... 45353541Sshin */ 45453541Sshin if (dstsock->sin6_scope_id < 0 || 45553541Sshin if_index < dstsock->sin6_scope_id) { 45653541Sshin *errorp = ENXIO; /* XXX: better error? */ 45753541Sshin return(0); 45853541Sshin } 45983130Sjlemon ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id), 46053541Sshin dst); 46153541Sshin if (ia6 == 0) { 46253541Sshin *errorp = EADDRNOTAVAIL; 46353541Sshin return(0); 46453541Sshin } 46553541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 46653541Sshin } 46753541Sshin 46853541Sshin /* 46953541Sshin * If the destination address is a multicast address and 47053541Sshin * the outgoing interface for the address is specified 47153541Sshin * by the caller, use an address associated with the interface. 47253541Sshin * There is a sanity check here; if the destination has node-local 47353541Sshin * scope, the outgoing interfacde should be a loopback address. 47453541Sshin * Even if the outgoing interface is not specified, we also 47553541Sshin * choose a loopback interface as the outgoing interface. 47653541Sshin */ 47753541Sshin if (IN6_IS_ADDR_MULTICAST(dst)) { 47853541Sshin struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 47953541Sshin 48053541Sshin if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 48153541Sshin ifp = &loif[0]; 48253541Sshin } 48353541Sshin 48453541Sshin if (ifp) { 48553541Sshin ia6 = in6_ifawithscope(ifp, dst); 48653541Sshin if (ia6 == 0) { 48753541Sshin *errorp = EADDRNOTAVAIL; 48853541Sshin return(0); 48953541Sshin } 49053541Sshin return(&ia6->ia_addr.sin6_addr); 49153541Sshin } 49253541Sshin } 49353541Sshin 49453541Sshin /* 49553541Sshin * If the next hop address for the packet is specified 49653541Sshin * by caller, use an address associated with the route 49753541Sshin * to the next hop. 49853541Sshin */ 49953541Sshin { 50053541Sshin struct sockaddr_in6 *sin6_next; 50153541Sshin struct rtentry *rt; 50253541Sshin 50353541Sshin if (opts && opts->ip6po_nexthop) { 50453541Sshin sin6_next = satosin6(opts->ip6po_nexthop); 50553541Sshin rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 50653541Sshin if (rt) { 50753541Sshin ia6 = in6_ifawithscope(rt->rt_ifp, dst); 50853541Sshin if (ia6 == 0) 50953541Sshin ia6 = ifatoia6(rt->rt_ifa); 51053541Sshin } 51153541Sshin if (ia6 == 0) { 51253541Sshin *errorp = EADDRNOTAVAIL; 51353541Sshin return(0); 51453541Sshin } 51553541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 51653541Sshin } 51753541Sshin } 51853541Sshin 51953541Sshin /* 52053541Sshin * If route is known or can be allocated now, 52153541Sshin * our src addr is taken from the i/f, else punt. 52253541Sshin */ 52353541Sshin if (ro) { 52453541Sshin if (ro->ro_rt && 52553541Sshin !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 52653541Sshin RTFREE(ro->ro_rt); 52753541Sshin ro->ro_rt = (struct rtentry *)0; 52853541Sshin } 52953541Sshin if (ro->ro_rt == (struct rtentry *)0 || 53053541Sshin ro->ro_rt->rt_ifp == (struct ifnet *)0) { 53178064Sume struct sockaddr_in6 *dst6; 53278064Sume 53353541Sshin /* No route yet, so try to acquire one */ 53453541Sshin bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 53578064Sume dst6 = (struct sockaddr_in6 *)&ro->ro_dst; 53678064Sume dst6->sin6_family = AF_INET6; 53778064Sume dst6->sin6_len = sizeof(struct sockaddr_in6); 53878064Sume dst6->sin6_addr = *dst; 53953541Sshin if (IN6_IS_ADDR_MULTICAST(dst)) { 54053541Sshin ro->ro_rt = rtalloc1(&((struct route *)ro) 54153541Sshin ->ro_dst, 0, 0UL); 54253541Sshin } else { 54354350Sshin rtalloc((struct route *)ro); 54453541Sshin } 54553541Sshin } 54653541Sshin 54753541Sshin /* 54853541Sshin * in_pcbconnect() checks out IFF_LOOPBACK to skip using 54953541Sshin * the address. But we don't know why it does so. 55053541Sshin * It is necessary to ensure the scope even for lo0 55153541Sshin * so doesn't check out IFF_LOOPBACK. 55253541Sshin */ 55353541Sshin 55453541Sshin if (ro->ro_rt) { 55553541Sshin ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 55653541Sshin if (ia6 == 0) /* xxx scope error ?*/ 55753541Sshin ia6 = ifatoia6(ro->ro_rt->rt_ifa); 55853541Sshin } 55953541Sshin if (ia6 == 0) { 56053541Sshin *errorp = EHOSTUNREACH; /* no route */ 56153541Sshin return(0); 56253541Sshin } 56353541Sshin return(&satosin6(&ia6->ia_addr)->sin6_addr); 56453541Sshin } 56553541Sshin 56653541Sshin *errorp = EADDRNOTAVAIL; 56753541Sshin return(0); 56853541Sshin} 56953541Sshin 57053541Sshin/* 57153541Sshin * Default hop limit selection. The precedence is as follows: 57253541Sshin * 1. Hoplimit valued specified via ioctl. 57353541Sshin * 2. (If the outgoing interface is detected) the current 57453541Sshin * hop limit of the interface specified by router advertisement. 57553541Sshin * 3. The system default hoplimit. 57653541Sshin*/ 57753541Sshinint 57853541Sshinin6_selecthlim(in6p, ifp) 57953541Sshin struct in6pcb *in6p; 58053541Sshin struct ifnet *ifp; 58153541Sshin{ 58253541Sshin if (in6p && in6p->in6p_hops >= 0) 58353541Sshin return(in6p->in6p_hops); 58453541Sshin else if (ifp) 58553541Sshin return(nd_ifinfo[ifp->if_index].chlim); 58653541Sshin else 58753541Sshin return(ip6_defhlim); 58853541Sshin} 58962587Sitojun#endif 59053541Sshin 59153541Sshinvoid 59253541Sshinin6_pcbdisconnect(inp) 59353541Sshin struct inpcb *inp; 59453541Sshin{ 59553541Sshin bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 59653541Sshin inp->inp_fport = 0; 59778064Sume /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 59878064Sume inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 59953541Sshin in_pcbrehash(inp); 60097658Stanimura if (inp->inp_socket->so_state & SS_NOFDREF) 60153541Sshin in6_pcbdetach(inp); 60253541Sshin} 60353541Sshin 60453541Sshinvoid 60553541Sshinin6_pcbdetach(inp) 60653541Sshin struct inpcb *inp; 60753541Sshin{ 60853541Sshin struct socket *so = inp->inp_socket; 60953541Sshin struct inpcbinfo *ipi = inp->inp_pcbinfo; 61053541Sshin 61153541Sshin#ifdef IPSEC 61258452Sgreen if (inp->in6p_sp != NULL) 61358452Sgreen ipsec6_delete_pcbpolicy(inp); 61453541Sshin#endif /* IPSEC */ 61553541Sshin inp->inp_gencnt = ++ipi->ipi_gencnt; 61653541Sshin in_pcbremlists(inp); 61753541Sshin sotoinpcb(so) = 0; 61886487Sdillon sotryfree(so); 61978064Sume 62053541Sshin if (inp->in6p_options) 62153541Sshin m_freem(inp->in6p_options); 62278064Sume ip6_freepcbopts(inp->in6p_outputopts); 62378064Sume ip6_freemoptions(inp->in6p_moptions); 62453541Sshin if (inp->in6p_route.ro_rt) 62553541Sshin rtfree(inp->in6p_route.ro_rt); 62655679Sshin /* Check and free IPv4 related resources in case of mapped addr */ 62755679Sshin if (inp->inp_options) 62855679Sshin (void)m_free(inp->inp_options); 62955679Sshin ip_freemoptions(inp->inp_moptions); 63055679Sshin 63153541Sshin inp->inp_vflag = 0; 63298141Shsu INP_LOCK_DESTROY(inp); 63392767Sjeff uma_zfree(ipi->ipi_zone, inp); 63453541Sshin} 63553541Sshin 636102218Struckmanstruct sockaddr * 637102218Struckmanin6_sockaddr(port, addr_p) 638102218Struckman in_port_t port; 639102218Struckman struct in6_addr *addr_p; 640102218Struckman{ 641102218Struckman struct sockaddr_in6 *sin6; 642102218Struckman 643111119Simp MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 644102218Struckman bzero(sin6, sizeof *sin6); 645102218Struckman sin6->sin6_family = AF_INET6; 646102218Struckman sin6->sin6_len = sizeof(*sin6); 647102218Struckman sin6->sin6_port = port; 648102218Struckman sin6->sin6_addr = *addr_p; 649102218Struckman if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 650102218Struckman sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 651102218Struckman else 652102218Struckman sin6->sin6_scope_id = 0; /*XXX*/ 653102218Struckman if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 654102218Struckman sin6->sin6_addr.s6_addr16[1] = 0; 655102218Struckman 656102218Struckman return (struct sockaddr *)sin6; 657102218Struckman} 658102218Struckman 659102218Struckmanstruct sockaddr * 660102218Struckmanin6_v4mapsin6_sockaddr(port, addr_p) 661102218Struckman in_port_t port; 662102218Struckman struct in_addr *addr_p; 663102218Struckman{ 664102218Struckman struct sockaddr_in sin; 665102218Struckman struct sockaddr_in6 *sin6_p; 666102218Struckman 667102218Struckman bzero(&sin, sizeof sin); 668102218Struckman sin.sin_family = AF_INET; 669102218Struckman sin.sin_len = sizeof(sin); 670102218Struckman sin.sin_port = port; 671102218Struckman sin.sin_addr = *addr_p; 672102218Struckman 673102218Struckman MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 674111119Simp M_WAITOK); 675102218Struckman in6_sin_2_v4mapsin6(&sin, sin6_p); 676102218Struckman 677102218Struckman return (struct sockaddr *)sin6_p; 678102218Struckman} 679102218Struckman 68053541Sshin/* 68153541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 68253541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points 68353541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly 68453541Sshin * without the need for a wrapper function. The socket must have a valid 68553541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 68653541Sshin * except through a kernel programming error, so it is acceptable to panic 68753541Sshin * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 68853541Sshin * because there actually /is/ a programming error somewhere... XXX) 68953541Sshin */ 69053541Sshinint 69153541Sshinin6_setsockaddr(so, nam) 69253541Sshin struct socket *so; 69353541Sshin struct sockaddr **nam; 69453541Sshin{ 69553541Sshin int s; 69653541Sshin register struct inpcb *inp; 697102218Struckman struct in6_addr addr; 698102218Struckman in_port_t port; 69953541Sshin 70053541Sshin s = splnet(); 70153541Sshin inp = sotoinpcb(so); 70253541Sshin if (!inp) { 70353541Sshin splx(s); 70453541Sshin return EINVAL; 70553541Sshin } 706102218Struckman port = inp->inp_lport; 707102218Struckman addr = inp->in6p_laddr; 70853541Sshin splx(s); 70953541Sshin 710102218Struckman *nam = in6_sockaddr(port, &addr); 71153541Sshin return 0; 71253541Sshin} 71353541Sshin 71453541Sshinint 71553541Sshinin6_setpeeraddr(so, nam) 71653541Sshin struct socket *so; 71753541Sshin struct sockaddr **nam; 71853541Sshin{ 71953541Sshin int s; 72053541Sshin struct inpcb *inp; 721102218Struckman struct in6_addr addr; 722102218Struckman in_port_t port; 72353541Sshin 72453541Sshin s = splnet(); 72553541Sshin inp = sotoinpcb(so); 72653541Sshin if (!inp) { 72753541Sshin splx(s); 72853541Sshin return EINVAL; 72953541Sshin } 730102218Struckman port = inp->inp_fport; 731102218Struckman addr = inp->in6p_faddr; 73253541Sshin splx(s); 73353541Sshin 734102218Struckman *nam = in6_sockaddr(port, &addr); 73553541Sshin return 0; 73653541Sshin} 73753541Sshin 73853541Sshinint 73953541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 74053541Sshin{ 74153541Sshin struct inpcb *inp = sotoinpcb(so); 74253541Sshin int error; 74353541Sshin 74453541Sshin if (inp == NULL) 74553541Sshin return EINVAL; 74653541Sshin if (inp->inp_vflag & INP_IPV4) { 74798102Shsu error = in_setsockaddr(so, nam, &tcbinfo); 74854952Seivind if (error == 0) 74953541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 75053541Sshin } else 75195023Ssuz /* scope issues will be handled in in6_setsockaddr(). */ 75253541Sshin error = in6_setsockaddr(so, nam); 75353541Sshin 75453541Sshin return error; 75553541Sshin} 75653541Sshin 75753541Sshinint 75853541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 75953541Sshin{ 76053541Sshin struct inpcb *inp = sotoinpcb(so); 76153541Sshin int error; 76253541Sshin 76353541Sshin if (inp == NULL) 76453541Sshin return EINVAL; 76553541Sshin if (inp->inp_vflag & INP_IPV4) { 76698102Shsu error = in_setpeeraddr(so, nam, &tcbinfo); 76754952Seivind if (error == 0) 76853541Sshin in6_sin_2_v4mapsin6_in_sock(nam); 76953541Sshin } else 77095023Ssuz /* scope issues will be handled in in6_setpeeraddr(). */ 77153541Sshin error = in6_setpeeraddr(so, nam); 77253541Sshin 77353541Sshin return error; 77453541Sshin} 77553541Sshin 77653541Sshin/* 77753541Sshin * Pass some notification to all connections of a protocol 77853541Sshin * associated with address dst. The local address and/or port numbers 77953541Sshin * may be specified to limit the search. The "usual action" will be 78053541Sshin * taken, depending on the ctlinput cmd. The caller must filter any 78153541Sshin * cmds that are uninteresting (e.g., no error in the map). 78253541Sshin * Call the protocol specific routine (if any) to report 78353541Sshin * any errors for each matching socket. 78453541Sshin * 78553541Sshin * Must be called at splnet. 78653541Sshin */ 78753541Sshinvoid 78878064Sumein6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify) 78953541Sshin struct inpcbhead *head; 79091346Salfred struct sockaddr *dst; 79191346Salfred const struct sockaddr *src; 79253541Sshin u_int fport_arg, lport_arg; 79353541Sshin int cmd; 79498211Shsu struct inpcb *(*notify) __P((struct inpcb *, int)); 79553541Sshin{ 79662587Sitojun struct inpcb *inp, *ninp; 79778064Sume struct sockaddr_in6 sa6_src, *sa6_dst; 79853541Sshin u_short fport = fport_arg, lport = lport_arg; 79978064Sume u_int32_t flowinfo; 80053541Sshin int errno, s; 80153541Sshin 80253541Sshin if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 80353541Sshin return; 80478064Sume 80578064Sume sa6_dst = (struct sockaddr_in6 *)dst; 80678064Sume if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 80753541Sshin return; 80853541Sshin 80953541Sshin /* 81078064Sume * note that src can be NULL when we get notify by local fragmentation. 81178064Sume */ 81291346Salfred sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 81378064Sume flowinfo = sa6_src.sin6_flowinfo; 81478064Sume 81578064Sume /* 81653541Sshin * Redirects go to all references to the destination, 81762587Sitojun * and use in6_rtchange to invalidate the route cache. 81862587Sitojun * Dead host indications: also use in6_rtchange to invalidate 81962587Sitojun * the cache, and deliver the error to all the sockets. 82053541Sshin * Otherwise, if we have knowledge of the local port and address, 82153541Sshin * deliver only to that socket. 82253541Sshin */ 82353541Sshin if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 82453541Sshin fport = 0; 82553541Sshin lport = 0; 82678064Sume bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 82762587Sitojun 82878064Sume if (cmd != PRC_HOSTDEAD) 82978064Sume notify = in6_rtchange; 83053541Sshin } 83153541Sshin errno = inet6ctlerrmap[cmd]; 83253541Sshin s = splnet(); 83362587Sitojun for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 83462587Sitojun ninp = LIST_NEXT(inp, inp_list); 83562587Sitojun 83678064Sume if ((inp->inp_vflag & INP_IPV6) == 0) 83753541Sshin continue; 83862587Sitojun 83978064Sume /* 84078064Sume * Detect if we should notify the error. If no source and 84178064Sume * destination ports are specifed, but non-zero flowinfo and 84278064Sume * local address match, notify the error. This is the case 84378064Sume * when the error is delivered with an encrypted buffer 84478064Sume * by ESP. Otherwise, just compare addresses and ports 84578064Sume * as usual. 84678064Sume */ 84778064Sume if (lport == 0 && fport == 0 && flowinfo && 84878064Sume inp->inp_socket != NULL && 84978064Sume flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 85078064Sume IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 85178064Sume goto do_notify; 85278064Sume else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 85378064Sume &sa6_dst->sin6_addr) || 85478064Sume inp->inp_socket == 0 || 85578064Sume (lport && inp->inp_lport != lport) || 85678064Sume (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 85778064Sume !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 85878064Sume &sa6_src.sin6_addr)) || 85978064Sume (fport && inp->inp_fport != fport)) 86053541Sshin continue; 86162587Sitojun 86278064Sume do_notify: 86353541Sshin if (notify) 86478064Sume (*notify)(inp, errno); 86553541Sshin } 86653541Sshin splx(s); 86753541Sshin} 86853541Sshin 86953541Sshin/* 87053541Sshin * Lookup a PCB based on the local address and port. 87153541Sshin */ 87253541Sshinstruct inpcb * 87353541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 87453541Sshin struct inpcbinfo *pcbinfo; 87553541Sshin struct in6_addr *laddr; 87653541Sshin u_int lport_arg; 87753541Sshin int wild_okay; 87853541Sshin{ 87953541Sshin register struct inpcb *inp; 88053541Sshin int matchwild = 3, wildcard; 88153541Sshin u_short lport = lport_arg; 88253541Sshin 88353541Sshin if (!wild_okay) { 88453541Sshin struct inpcbhead *head; 88553541Sshin /* 88653541Sshin * Look for an unconnected (wildcard foreign addr) PCB that 88753541Sshin * matches the local address and port we're looking for. 88853541Sshin */ 88953541Sshin head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 89053541Sshin pcbinfo->hashmask)]; 89153541Sshin LIST_FOREACH(inp, head, inp_hash) { 89254952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 89353541Sshin continue; 89453541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 89553541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 89653541Sshin inp->inp_lport == lport) { 89753541Sshin /* 89853541Sshin * Found. 89953541Sshin */ 90053541Sshin return (inp); 90153541Sshin } 90253541Sshin } 90353541Sshin /* 90453541Sshin * Not found. 90553541Sshin */ 90653541Sshin return (NULL); 90753541Sshin } else { 90853541Sshin struct inpcbporthead *porthash; 90953541Sshin struct inpcbport *phd; 91053541Sshin struct inpcb *match = NULL; 91153541Sshin /* 91253541Sshin * Best fit PCB lookup. 91353541Sshin * 91453541Sshin * First see if this local port is in use by looking on the 91553541Sshin * port hash list. 91653541Sshin */ 91753541Sshin porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 91853541Sshin pcbinfo->porthashmask)]; 91953541Sshin LIST_FOREACH(phd, porthash, phd_hash) { 92053541Sshin if (phd->phd_port == lport) 92153541Sshin break; 92253541Sshin } 92353541Sshin if (phd != NULL) { 92453541Sshin /* 92553541Sshin * Port is in use by one or more PCBs. Look for best 92653541Sshin * fit. 92753541Sshin */ 92853541Sshin LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 92953541Sshin wildcard = 0; 93054952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 93153541Sshin continue; 93253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 93353541Sshin wildcard++; 93453541Sshin if (!IN6_IS_ADDR_UNSPECIFIED( 93553541Sshin &inp->in6p_laddr)) { 93653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 93753541Sshin wildcard++; 93853541Sshin else if (!IN6_ARE_ADDR_EQUAL( 93953541Sshin &inp->in6p_laddr, laddr)) 94053541Sshin continue; 94153541Sshin } else { 94253541Sshin if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 94353541Sshin wildcard++; 94453541Sshin } 94553541Sshin if (wildcard < matchwild) { 94653541Sshin match = inp; 94753541Sshin matchwild = wildcard; 94853541Sshin if (matchwild == 0) { 94953541Sshin break; 95053541Sshin } 95153541Sshin } 95253541Sshin } 95353541Sshin } 95453541Sshin return (match); 95553541Sshin } 95653541Sshin} 95753541Sshin 95881127Sumevoid 95981127Sumein6_pcbpurgeif0(head, ifp) 96081127Sume struct in6pcb *head; 96181127Sume struct ifnet *ifp; 96281127Sume{ 96381127Sume struct in6pcb *in6p; 96481127Sume struct ip6_moptions *im6o; 96581127Sume struct in6_multi_mship *imm, *nimm; 96681127Sume 96781127Sume for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) { 96881127Sume im6o = in6p->in6p_moptions; 96981127Sume if ((in6p->inp_vflag & INP_IPV6) && 97081127Sume im6o) { 97181127Sume /* 97281127Sume * Unselect the outgoing interface if it is being 97381127Sume * detached. 97481127Sume */ 97581127Sume if (im6o->im6o_multicast_ifp == ifp) 97681127Sume im6o->im6o_multicast_ifp = NULL; 97781127Sume 97881127Sume /* 97981127Sume * Drop multicast group membership if we joined 98081127Sume * through the interface being detached. 98181127Sume * XXX controversial - is it really legal for kernel 98281127Sume * to force this? 98381127Sume */ 98481127Sume for (imm = im6o->im6o_memberships.lh_first; 98581127Sume imm != NULL; imm = nimm) { 98681127Sume nimm = imm->i6mm_chain.le_next; 98781127Sume if (imm->i6mm_maddr->in6m_ifp == ifp) { 98881127Sume LIST_REMOVE(imm, i6mm_chain); 98981127Sume in6_delmulti(imm->i6mm_maddr); 99081127Sume free(imm, M_IPMADDR); 99181127Sume } 99281127Sume } 99381127Sume } 99481127Sume } 99581127Sume} 99681127Sume 99753541Sshin/* 99853541Sshin * Check for alternatives when higher level complains 99953541Sshin * about service problems. For now, invalidate cached 100053541Sshin * routing information. If the route was created dynamically 100153541Sshin * (by a redirect), time to try a default gateway again. 100253541Sshin */ 100353541Sshinvoid 100453541Sshinin6_losing(in6p) 100553541Sshin struct inpcb *in6p; 100653541Sshin{ 100753541Sshin struct rtentry *rt; 100853541Sshin struct rt_addrinfo info; 100953541Sshin 101053541Sshin if ((rt = in6p->in6p_route.ro_rt) != NULL) { 101153541Sshin bzero((caddr_t)&info, sizeof(info)); 101285074Sru info.rti_flags = rt->rt_flags; 101385074Sru info.rti_info[RTAX_DST] = rt_key(rt); 101453541Sshin info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 101553541Sshin info.rti_info[RTAX_NETMASK] = rt_mask(rt); 101653541Sshin rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 101753541Sshin if (rt->rt_flags & RTF_DYNAMIC) 101885074Sru (void)rtrequest1(RTM_DELETE, &info, NULL); 101985072Sru in6p->in6p_route.ro_rt = NULL; 102085072Sru rtfree(rt); 102153541Sshin /* 102253541Sshin * A new route can be allocated 102353541Sshin * the next time output is attempted. 102453541Sshin */ 102553541Sshin } 102653541Sshin} 102753541Sshin 102853541Sshin/* 102953541Sshin * After a routing change, flush old routing 103053541Sshin * and allocate a (hopefully) better one. 103153541Sshin */ 103298211Shsustruct inpcb * 103353541Sshinin6_rtchange(inp, errno) 103453541Sshin struct inpcb *inp; 103553541Sshin int errno; 103653541Sshin{ 103753541Sshin if (inp->in6p_route.ro_rt) { 103853541Sshin rtfree(inp->in6p_route.ro_rt); 103953541Sshin inp->in6p_route.ro_rt = 0; 104053541Sshin /* 104153541Sshin * A new route can be allocated the next time 104253541Sshin * output is attempted. 104353541Sshin */ 104453541Sshin } 104598211Shsu return inp; 104653541Sshin} 104753541Sshin 104853541Sshin/* 104953541Sshin * Lookup PCB in hash list. 105053541Sshin */ 105153541Sshinstruct inpcb * 105253541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 105353541Sshin struct inpcbinfo *pcbinfo; 105453541Sshin struct in6_addr *faddr, *laddr; 105553541Sshin u_int fport_arg, lport_arg; 105653541Sshin int wildcard; 105753541Sshin struct ifnet *ifp; 105853541Sshin{ 105953541Sshin struct inpcbhead *head; 106053541Sshin register struct inpcb *inp; 106153541Sshin u_short fport = fport_arg, lport = lport_arg; 106278064Sume int faith; 106353541Sshin 106483934Sbrooks if (faithprefix_p != NULL) 106583934Sbrooks faith = (*faithprefix_p)(laddr); 106683934Sbrooks else 106783934Sbrooks faith = 0; 106878064Sume 106953541Sshin /* 107053541Sshin * First look for an exact match. 107153541Sshin */ 107253541Sshin head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, 107353541Sshin lport, fport, 107453541Sshin pcbinfo->hashmask)]; 107554263Sshin LIST_FOREACH(inp, head, inp_hash) { 107654952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 107753541Sshin continue; 107853541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 107953541Sshin IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 108053541Sshin inp->inp_fport == fport && 108153541Sshin inp->inp_lport == lport) { 108253541Sshin /* 108353541Sshin * Found. 108453541Sshin */ 108553541Sshin return (inp); 108653541Sshin } 108753541Sshin } 108853541Sshin if (wildcard) { 108953541Sshin struct inpcb *local_wild = NULL; 109053541Sshin 109153541Sshin head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 109253541Sshin pcbinfo->hashmask)]; 109354263Sshin LIST_FOREACH(inp, head, inp_hash) { 109454952Seivind if ((inp->inp_vflag & INP_IPV6) == 0) 109553541Sshin continue; 109653541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 109753541Sshin inp->inp_lport == lport) { 109878064Sume if (faith && (inp->inp_flags & INP_FAITH) == 0) 109953541Sshin continue; 110053541Sshin if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 110153541Sshin laddr)) 110253541Sshin return (inp); 110353541Sshin else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 110453541Sshin local_wild = inp; 110553541Sshin } 110653541Sshin } 110753541Sshin return (local_wild); 110853541Sshin } 110953541Sshin 111053541Sshin /* 111153541Sshin * Not found. 111253541Sshin */ 111353541Sshin return (NULL); 111453541Sshin} 111553541Sshin 111653541Sshinvoid 111753541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 111853541Sshin{ 111953541Sshin struct ip6_hdr *ip; 112053541Sshin 112153541Sshin ip = mtod(m, struct ip6_hdr *); 112253541Sshin bzero(sin6, sizeof(*sin6)); 112353541Sshin sin6->sin6_len = sizeof(*sin6); 112453541Sshin sin6->sin6_family = AF_INET6; 112553541Sshin sin6->sin6_addr = ip->ip6_src; 112653541Sshin if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 112753541Sshin sin6->sin6_addr.s6_addr16[1] = 0; 112853541Sshin sin6->sin6_scope_id = 112953541Sshin (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 113053541Sshin ? m->m_pkthdr.rcvif->if_index : 0; 113153541Sshin 113253541Sshin return; 113353541Sshin} 1134