in6_src.c revision 1.46
1167465Smp/* $OpenBSD: in6_src.c,v 1.46 2014/09/27 12:26:16 mpi Exp $ */ 259243Sobrien/* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $ */ 359243Sobrien 459243Sobrien/* 559243Sobrien * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 659243Sobrien * All rights reserved. 759243Sobrien * 859243Sobrien * Redistribution and use in source and binary forms, with or without 959243Sobrien * modification, are permitted provided that the following conditions 1059243Sobrien * are met: 11100616Smp * 1. Redistributions of source code must retain the above copyright 1259243Sobrien * notice, this list of conditions and the following disclaimer. 1359243Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1459243Sobrien * notice, this list of conditions and the following disclaimer in the 1559243Sobrien * documentation and/or other materials provided with the distribution. 1659243Sobrien * 3. Neither the name of the project nor the names of its contributors 1759243Sobrien * may be used to endorse or promote products derived from this software 1859243Sobrien * without specific prior written permission. 1959243Sobrien * 2059243Sobrien * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2159243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2259243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2359243Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2459243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2559243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2659243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2759243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2859243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2959243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3059243Sobrien * SUCH DAMAGE. 3159243Sobrien */ 3259243Sobrien 3359243Sobrien/* 3459243Sobrien * Copyright (c) 1982, 1986, 1991, 1993 3559243Sobrien * The Regents of the University of California. All rights reserved. 3659243Sobrien * 3759243Sobrien * Redistribution and use in source and binary forms, with or without 3859243Sobrien * modification, are permitted provided that the following conditions 3959243Sobrien * are met: 4059243Sobrien * 1. Redistributions of source code must retain the above copyright 4159243Sobrien * notice, this list of conditions and the following disclaimer. 4259243Sobrien * 2. Redistributions in binary form must reproduce the above copyright 4359243Sobrien * notice, this list of conditions and the following disclaimer in the 4459243Sobrien * documentation and/or other materials provided with the distribution. 4559243Sobrien * 3. Neither the name of the University nor the names of its contributors 4659243Sobrien * may be used to endorse or promote products derived from this software 4759243Sobrien * without specific prior written permission. 4859243Sobrien * 4959243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5059243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5159243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5259243Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5359243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5459243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5559243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5659243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5759243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5859243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5959243Sobrien * SUCH DAMAGE. 6059243Sobrien * 6159243Sobrien * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 6259243Sobrien */ 6359243Sobrien 6459243Sobrien#include <sys/param.h> 6559243Sobrien#include <sys/systm.h> 6659243Sobrien#include <sys/mbuf.h> 6759243Sobrien#include <sys/protosw.h> 6859243Sobrien#include <sys/socket.h> 6959243Sobrien#include <sys/socketvar.h> 7059243Sobrien#include <sys/ioctl.h> 7159243Sobrien#include <sys/errno.h> 7259243Sobrien#include <sys/time.h> 7359243Sobrien 7459243Sobrien#include <net/if.h> 7559243Sobrien#include <net/route.h> 7659243Sobrien 7759243Sobrien#include <netinet/in.h> 7859243Sobrien#include <netinet/ip.h> 7959243Sobrien#include <netinet/in_pcb.h> 8059243Sobrien#include <netinet6/in6_var.h> 8159243Sobrien#include <netinet/ip6.h> 8259243Sobrien#include <netinet6/ip6_var.h> 8359243Sobrien#include <netinet6/nd6.h> 8459243Sobrien 8559243Sobrienint in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *, 8659243Sobrien struct ip6_moptions *, struct route_in6 *, struct ifnet **, u_int); 8759243Sobrienint selectroute(struct sockaddr_in6 *, struct ip6_pktopts *, 8859243Sobrien struct ip6_moptions *, struct route_in6 *, struct ifnet **, 8959243Sobrien struct rtentry **, int, u_int); 9059243Sobrien 9159243Sobrien/* 9259243Sobrien * Return an IPv6 address, which is the most appropriate for a given 9359243Sobrien * destination and user specified options. 9459243Sobrien * If necessary, this function lookups the routing table and returns 9559243Sobrien * an entry to the caller for later use. 9659243Sobrien */ 9759243Sobrienint 9859243Sobrienin6_selectsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock, 9959243Sobrien struct ip6_pktopts *opts, struct ip6_moptions *mopts, 10059243Sobrien struct route_in6 *ro, struct in6_addr *laddr, u_int rtableid) 10159243Sobrien{ 10259243Sobrien struct ifnet *ifp = NULL; 10359243Sobrien struct in6_addr *dst; 10459243Sobrien struct in6_ifaddr *ia6 = NULL; 10559243Sobrien struct in6_pktinfo *pi = NULL; 10659243Sobrien int error; 10759243Sobrien 10859243Sobrien dst = &dstsock->sin6_addr; 10959243Sobrien 11059243Sobrien /* 11159243Sobrien * If the source address is explicitly specified by the caller, 11259243Sobrien * check if the requested source address is indeed a unicast address 11359243Sobrien * assigned to the node, and can be used as the packet's source 11459243Sobrien * address. If everything is okay, use the address as source. 11559243Sobrien */ 11659243Sobrien if (opts && (pi = opts->ip6po_pktinfo) && 11759243Sobrien !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) { 11859243Sobrien struct sockaddr_in6 sa6; 11959243Sobrien 12059243Sobrien /* get the outgoing interface */ 12159243Sobrien error = in6_selectif(dstsock, opts, mopts, ro, &ifp, rtableid); 12259243Sobrien if (error) 12359243Sobrien return (error); 12459243Sobrien 12559243Sobrien bzero(&sa6, sizeof(sa6)); 12659243Sobrien sa6.sin6_family = AF_INET6; 12759243Sobrien sa6.sin6_len = sizeof(sa6); 12859243Sobrien sa6.sin6_addr = pi->ipi6_addr; 12959243Sobrien 13059243Sobrien if (ifp && IN6_IS_SCOPE_EMBED(&sa6.sin6_addr)) 13159243Sobrien sa6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 13259243Sobrien 13359243Sobrien ia6 = ifatoia6(ifa_ifwithaddr(sin6tosa(&sa6), rtableid)); 13459243Sobrien if (ia6 == NULL || 13559243Sobrien (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) 13659243Sobrien return (EADDRNOTAVAIL); 13759243Sobrien 13859243Sobrien pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */ 13959243Sobrien 14059243Sobrien *in6src = &pi->ipi6_addr; 14159243Sobrien return (0); 14259243Sobrien } 14359243Sobrien 14459243Sobrien /* 14559243Sobrien * If the source address is not specified but the socket(if any) 14659243Sobrien * is already bound, use the bound address. 14759243Sobrien */ 14859243Sobrien if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) { 14959243Sobrien *in6src = laddr; 15059243Sobrien return (0); 15159243Sobrien } 15259243Sobrien 15359243Sobrien /* 15459243Sobrien * If the caller doesn't specify the source address but 15559243Sobrien * the outgoing interface, use an address associated with 15659243Sobrien * the interface. 15759243Sobrien */ 15859243Sobrien if (pi && pi->ipi6_ifindex) { 15959243Sobrien ifp = if_get(pi->ipi6_ifindex); 16059243Sobrien if (ifp == NULL) 16159243Sobrien return (ENXIO); /* XXX: better error? */ 16259243Sobrien 16359243Sobrien ia6 = in6_ifawithscope(ifp, dst, rtableid); 16459243Sobrien if (ia6 == NULL) 16559243Sobrien return (EADDRNOTAVAIL); 16659243Sobrien 16759243Sobrien *in6src = &ia6->ia_addr.sin6_addr; 16859243Sobrien return (0); 16959243Sobrien } 17059243Sobrien 17159243Sobrien /* 17259243Sobrien * If the destination address is a link-local unicast address or 17359243Sobrien * a link/interface-local multicast address, and if the outgoing 17459243Sobrien * interface is specified by the sin6_scope_id filed, use an address 17559243Sobrien * associated with the interface. 17659243Sobrien * XXX: We're now trying to define more specific semantics of 17759243Sobrien * sin6_scope_id field, so this part will be rewritten in 17859243Sobrien * the near future. 17959243Sobrien */ 18059243Sobrien if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) || 18159243Sobrien IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) { 18259243Sobrien ifp = if_get(dstsock->sin6_scope_id); 18359243Sobrien if (ifp == NULL) 18459243Sobrien return (ENXIO); /* XXX: better error? */ 18559243Sobrien 18659243Sobrien ia6 = in6_ifawithscope(ifp, dst, rtableid); 18759243Sobrien if (ia6 == NULL) 18859243Sobrien return (EADDRNOTAVAIL); 18959243Sobrien 19059243Sobrien *in6src = &ia6->ia_addr.sin6_addr; 19159243Sobrien return (0); 19259243Sobrien } 19359243Sobrien 19459243Sobrien /* 19559243Sobrien * If the destination address is a multicast address and 19659243Sobrien * the outgoing interface for the address is specified 19759243Sobrien * by the caller, use an address associated with the interface. 19859243Sobrien * Even if the outgoing interface is not specified, we also 19959243Sobrien * choose a loopback interface as the outgoing interface. 20059243Sobrien */ 20159243Sobrien if (IN6_IS_ADDR_MULTICAST(dst)) { 20259243Sobrien ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 20359243Sobrien 20459243Sobrien if (!ifp && dstsock->sin6_scope_id) 20559243Sobrien ifp = if_get(htons(dstsock->sin6_scope_id)); 20659243Sobrien 20759243Sobrien if (ifp) { 20859243Sobrien ia6 = in6_ifawithscope(ifp, dst, rtableid); 20959243Sobrien if (ia6 == NULL) 21059243Sobrien return (EADDRNOTAVAIL); 21159243Sobrien 21259243Sobrien *in6src = &ia6->ia_addr.sin6_addr; 21359243Sobrien return (0); 21459243Sobrien } 21559243Sobrien } 21659243Sobrien 21759243Sobrien /* 21859243Sobrien * If the next hop address for the packet is specified 21959243Sobrien * by caller, use an address associated with the route 22059243Sobrien * to the next hop. 22159243Sobrien */ 22259243Sobrien { 22359243Sobrien struct sockaddr_in6 *sin6_next; 22459243Sobrien struct rtentry *rt; 22559243Sobrien 22659243Sobrien if (opts && opts->ip6po_nexthop) { 22759243Sobrien sin6_next = satosin6(opts->ip6po_nexthop); 22859243Sobrien rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL, 22959243Sobrien rtableid); 23059243Sobrien if (rt) { 23159243Sobrien ia6 = in6_ifawithscope(rt->rt_ifp, dst, 23259243Sobrien rtableid); 23359243Sobrien if (ia6 == 0) 23459243Sobrien ia6 = ifatoia6(rt->rt_ifa); 23559243Sobrien } 23659243Sobrien if (ia6 == NULL) 23759243Sobrien return (EADDRNOTAVAIL); 23859243Sobrien 23959243Sobrien *in6src = &ia6->ia_addr.sin6_addr; 24059243Sobrien return (0); 24159243Sobrien } 24259243Sobrien } 24359243Sobrien 24459243Sobrien /* 24559243Sobrien * If route is known or can be allocated now, 24659243Sobrien * our src addr is taken from the i/f, else punt. 24759243Sobrien */ 24859243Sobrien if (ro) { 24959243Sobrien if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 25059243Sobrien !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) { 25159243Sobrien RTFREE(ro->ro_rt); 25259243Sobrien ro->ro_rt = (struct rtentry *)0; 25359243Sobrien } 25459243Sobrien if (ro->ro_rt == (struct rtentry *)0 || 25559243Sobrien ro->ro_rt->rt_ifp == (struct ifnet *)0) { 25659243Sobrien struct sockaddr_in6 *sa6; 25759243Sobrien 25859243Sobrien /* No route yet, so try to acquire one */ 25959243Sobrien bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 26059243Sobrien ro->ro_tableid = rtableid; 26159243Sobrien sa6 = &ro->ro_dst; 26259243Sobrien sa6->sin6_family = AF_INET6; 26359243Sobrien sa6->sin6_len = sizeof(struct sockaddr_in6); 26459243Sobrien sa6->sin6_addr = *dst; 26559243Sobrien sa6->sin6_scope_id = dstsock->sin6_scope_id; 26659243Sobrien if (IN6_IS_ADDR_MULTICAST(dst)) { 26759243Sobrien ro->ro_rt = rtalloc1(sin6tosa(&ro->ro_dst), 26859243Sobrien RT_REPORT, ro->ro_tableid); 26959243Sobrien } else { 27059243Sobrien ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst), 27159243Sobrien NULL, ro->ro_tableid); 27259243Sobrien } 27359243Sobrien } 27459243Sobrien 27559243Sobrien /* 27659243Sobrien * in_pcbconnect() checks out IFF_LOOPBACK to skip using 27759243Sobrien * the address. But we don't know why it does so. 27859243Sobrien * It is necessary to ensure the scope even for lo0 27959243Sobrien * so doesn't check out IFF_LOOPBACK. 28059243Sobrien */ 28159243Sobrien 28259243Sobrien if (ro->ro_rt) { 28359243Sobrien ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst, 28459243Sobrien rtableid); 28559243Sobrien if (ia6 == 0) /* xxx scope error ?*/ 28659243Sobrien ia6 = ifatoia6(ro->ro_rt->rt_ifa); 28759243Sobrien } 28859243Sobrien if (ia6 == NULL) 289145479Smp return (EHOSTUNREACH); /* no route */ 29059243Sobrien 29159243Sobrien *in6src = &ia6->ia_addr.sin6_addr; 29259243Sobrien return (0); 293145479Smp } 29459243Sobrien 29559243Sobrien return (EADDRNOTAVAIL); 29659243Sobrien} 29759243Sobrien 29859243Sobrienint 29959243Sobrienselectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 30059243Sobrien struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp, 30159243Sobrien struct rtentry **retrt, int norouteok, u_int rtableid) 30259243Sobrien{ 30359243Sobrien int error = 0; 30459243Sobrien struct ifnet *ifp = NULL; 30559243Sobrien struct rtentry *rt = NULL; 30659243Sobrien struct sockaddr_in6 *sin6_next; 30759243Sobrien struct in6_pktinfo *pi = NULL; 30859243Sobrien struct in6_addr *dst; 30959243Sobrien 31059243Sobrien dst = &dstsock->sin6_addr; 31159243Sobrien 31259243Sobrien#if 0 31359243Sobrien char ip[INET6_ADDRSTRLEN]; 31459243Sobrien 31559243Sobrien if (dstsock->sin6_addr.s6_addr32[0] == 0 && 31659243Sobrien dstsock->sin6_addr.s6_addr32[1] == 0 && 31759243Sobrien !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) { 31859243Sobrien printf("in6_selectroute: strange destination %s\n", 31959243Sobrien inet_ntop(AF_INET6, &dstsock->sin6_addr, ip, sizeof(ip))); 32059243Sobrien } else { 32159243Sobrien printf("in6_selectroute: destination = %s%%%d\n", 32259243Sobrien inet_ntop(AF_INET6, &dstsock->sin6_addr, ip, sizeof(ip)), 32359243Sobrien dstsock->sin6_scope_id); /* for debug */ 32459243Sobrien } 32559243Sobrien#endif 32659243Sobrien 32759243Sobrien /* If the caller specify the outgoing interface explicitly, use it. */ 32859243Sobrien if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) { 32959243Sobrien ifp = if_get(pi->ipi6_ifindex); 33059243Sobrien if (ifp != NULL && 33159243Sobrien (norouteok || retrt == NULL || 33259243Sobrien IN6_IS_ADDR_MULTICAST(dst))) { 33359243Sobrien /* 33459243Sobrien * we do not have to check or get the route for 33559243Sobrien * multicast. 33659243Sobrien */ 33759243Sobrien goto done; 33859243Sobrien } else 33959243Sobrien goto getroute; 34059243Sobrien } 34159243Sobrien 34259243Sobrien /* 34359243Sobrien * If the destination address is a multicast address and the outgoing 34459243Sobrien * interface for the address is specified by the caller, use it. 34559243Sobrien */ 34659243Sobrien if (IN6_IS_ADDR_MULTICAST(dst) && 34759243Sobrien mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) { 34859243Sobrien goto done; /* we do not need a route for multicast. */ 34959243Sobrien } 35059243Sobrien 35159243Sobrien getroute: 35259243Sobrien /* 35359243Sobrien * If the next hop address for the packet is specified by the caller, 35459243Sobrien * use it as the gateway. 35559243Sobrien */ 35659243Sobrien if (opts && opts->ip6po_nexthop) { 35759243Sobrien struct route_in6 *ron; 35859243Sobrien 35959243Sobrien sin6_next = satosin6(opts->ip6po_nexthop); 36059243Sobrien 36159243Sobrien /* at this moment, we only support AF_INET6 next hops */ 36259243Sobrien if (sin6_next->sin6_family != AF_INET6) { 36359243Sobrien error = EAFNOSUPPORT; /* or should we proceed? */ 36459243Sobrien goto done; 36559243Sobrien } 36659243Sobrien 36759243Sobrien /* 36859243Sobrien * If the next hop is an IPv6 address, then the node identified 36959243Sobrien * by that address must be a neighbor of the sending host. 37059243Sobrien */ 37159243Sobrien ron = &opts->ip6po_nextroute; 37259243Sobrien if ((ron->ro_rt && 37359243Sobrien (ron->ro_rt->rt_flags & (RTF_UP | RTF_GATEWAY)) != 37459243Sobrien RTF_UP) || 37559243Sobrien !IN6_ARE_ADDR_EQUAL(&ron->ro_dst.sin6_addr, 37659243Sobrien &sin6_next->sin6_addr)) { 37759243Sobrien if (ron->ro_rt) { 37859243Sobrien RTFREE(ron->ro_rt); 37959243Sobrien ron->ro_rt = NULL; 38059243Sobrien } 38159243Sobrien ron->ro_dst = *sin6_next; 38259243Sobrien ron->ro_tableid = rtableid; 38359243Sobrien } 38459243Sobrien if (ron->ro_rt == NULL) { 38559243Sobrien ron->ro_rt = rtalloc1(sin6tosa(&ron->ro_dst), 38659243Sobrien RT_REPORT, ron->ro_tableid); /* multi path case? */ 38759243Sobrien if (ron->ro_rt == NULL || 38859243Sobrien (ron->ro_rt->rt_flags & RTF_GATEWAY)) { 38959243Sobrien if (ron->ro_rt) { 39059243Sobrien RTFREE(ron->ro_rt); 39159243Sobrien ron->ro_rt = NULL; 39259243Sobrien } 39359243Sobrien error = EHOSTUNREACH; 39459243Sobrien goto done; 39559243Sobrien } 39659243Sobrien } 39759243Sobrien if (!nd6_is_addr_neighbor(sin6_next, ron->ro_rt->rt_ifp)) { 39859243Sobrien RTFREE(ron->ro_rt); 39959243Sobrien ron->ro_rt = NULL; 40059243Sobrien error = EHOSTUNREACH; 40159243Sobrien goto done; 40259243Sobrien } 40359243Sobrien rt = ron->ro_rt; 404100616Smp ifp = rt->rt_ifp; 40559243Sobrien 40659243Sobrien /* 40759243Sobrien * When cloning is required, try to allocate a route to the 40859243Sobrien * destination so that the caller can store path MTU 40959243Sobrien * information. 41059243Sobrien */ 41159243Sobrien goto done; 41259243Sobrien } 41359243Sobrien 41459243Sobrien /* 41559243Sobrien * Use a cached route if it exists and is valid, else try to allocate 41659243Sobrien * a new one. Note that we should check the address family of the 41759243Sobrien * cached destination, in case of sharing the cache with IPv4. 41859243Sobrien */ 41959243Sobrien if (ro) { 42059243Sobrien if (ro->ro_rt && 42159243Sobrien (!(ro->ro_rt->rt_flags & RTF_UP) || 42259243Sobrien sin6tosa(&ro->ro_dst)->sa_family != AF_INET6 || 42359243Sobrien !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) { 42459243Sobrien RTFREE(ro->ro_rt); 42559243Sobrien ro->ro_rt = NULL; 42659243Sobrien } 42759243Sobrien if (ro->ro_rt == NULL) { 42859243Sobrien struct sockaddr_in6 *sa6; 42959243Sobrien 43059243Sobrien /* No route yet, so try to acquire one */ 43159243Sobrien bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 43259243Sobrien ro->ro_tableid = rtableid; 43359243Sobrien sa6 = &ro->ro_dst; 43459243Sobrien *sa6 = *dstsock; 43559243Sobrien sa6->sin6_scope_id = 0; 43659243Sobrien ro->ro_tableid = rtableid; 43759243Sobrien ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst), 43859243Sobrien NULL, ro->ro_tableid); 43959243Sobrien } 44059243Sobrien 44159243Sobrien /* 44259243Sobrien * do not care about the result if we have the nexthop 44359243Sobrien * explicitly specified. 44459243Sobrien */ 44559243Sobrien if (opts && opts->ip6po_nexthop) 44659243Sobrien goto done; 44759243Sobrien 44859243Sobrien if (ro->ro_rt) { 44959243Sobrien ifp = ro->ro_rt->rt_ifp; 45059243Sobrien 45159243Sobrien if (ifp == NULL) { /* can this really happen? */ 45259243Sobrien RTFREE(ro->ro_rt); 45359243Sobrien ro->ro_rt = NULL; 45459243Sobrien } 45559243Sobrien } 45659243Sobrien if (ro->ro_rt == NULL) 45759243Sobrien error = EHOSTUNREACH; 45859243Sobrien rt = ro->ro_rt; 45959243Sobrien 46059243Sobrien /* 46159243Sobrien * Check if the outgoing interface conflicts with 46259243Sobrien * the interface specified by ipi6_ifindex (if specified). 46359243Sobrien * Note that loopback interface is always okay. 46459243Sobrien * (this may happen when we are sending a packet to one of 46559243Sobrien * our own addresses.) 46659243Sobrien */ 46759243Sobrien if (opts && opts->ip6po_pktinfo && 46859243Sobrien opts->ip6po_pktinfo->ipi6_ifindex) { 46959243Sobrien if (!(ifp->if_flags & IFF_LOOPBACK) && 47059243Sobrien ifp->if_index != 47159243Sobrien opts->ip6po_pktinfo->ipi6_ifindex) { 47259243Sobrien error = EHOSTUNREACH; 47359243Sobrien goto done; 47459243Sobrien } 47559243Sobrien } 47659243Sobrien } 47759243Sobrien 47859243Sobrien done: 47959243Sobrien if (ifp == NULL && rt == NULL) { 48059243Sobrien /* 48159243Sobrien * This can happen if the caller did not pass a cached route 48259243Sobrien * nor any other hints. We treat this case an error. 48359243Sobrien */ 48459243Sobrien error = EHOSTUNREACH; 48559243Sobrien } 48659243Sobrien if (error == EHOSTUNREACH) 48759243Sobrien ip6stat.ip6s_noroute++; 48859243Sobrien 48959243Sobrien if (retifp != NULL) 49059243Sobrien *retifp = ifp; 49159243Sobrien if (retrt != NULL) 49259243Sobrien *retrt = rt; /* rt may be NULL */ 49359243Sobrien 49459243Sobrien return (error); 49559243Sobrien} 49659243Sobrien 49759243Sobrienint 49859243Sobrienin6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 49959243Sobrien struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp, 50059243Sobrien u_int rtableid) 50159243Sobrien{ 50259243Sobrien struct rtentry *rt = NULL; 50359243Sobrien int error; 50459243Sobrien 50559243Sobrien if ((error = selectroute(dstsock, opts, mopts, ro, retifp, 50659243Sobrien &rt, 1, rtableid)) != 0) 50759243Sobrien return (error); 50859243Sobrien 50959243Sobrien /* 51059243Sobrien * do not use a rejected or black hole route. 51159243Sobrien * XXX: this check should be done in the L2 output routine. 51259243Sobrien * However, if we skipped this check here, we'd see the following 51359243Sobrien * scenario: 51459243Sobrien * - install a rejected route for a scoped address prefix 51559243Sobrien * (like fe80::/10) 51659243Sobrien * - send a packet to a destination that matches the scoped prefix, 51759243Sobrien * with ambiguity about the scope zone. 51859243Sobrien * - pick the outgoing interface from the route, and disambiguate the 51959243Sobrien * scope zone with the interface. 52059243Sobrien * - ip6_output() would try to get another route with the "new" 52159243Sobrien * destination, which may be valid. 52259243Sobrien * - we'd see no error on output. 52359243Sobrien * Although this may not be very harmful, it should still be confusing. 52459243Sobrien * We thus reject the case here. 52559243Sobrien */ 52659243Sobrien if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) 52759243Sobrien return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 52859243Sobrien 52959243Sobrien /* 53059243Sobrien * Adjust the "outgoing" interface. If we're going to loop the packet 53159243Sobrien * back to ourselves, the ifp would be the loopback interface. 53259243Sobrien * However, we'd rather know the interface associated to the 53359243Sobrien * destination address (which should probably be one of our own 53459243Sobrien * addresses.) 53559243Sobrien */ 53659243Sobrien if (rt && rt->rt_ifa && rt->rt_ifa->ifa_ifp) 53759243Sobrien *retifp = rt->rt_ifa->ifa_ifp; 53859243Sobrien 53959243Sobrien return (0); 54059243Sobrien} 54159243Sobrien 54259243Sobrienint 54359243Sobrienin6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 54459243Sobrien struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp, 54559243Sobrien struct rtentry **retrt, u_int rtableid) 54659243Sobrien{ 54759243Sobrien 54859243Sobrien return (selectroute(dstsock, opts, mopts, ro, retifp, retrt, 0, 54959243Sobrien rtableid)); 55059243Sobrien} 55159243Sobrien 55259243Sobrien/* 55359243Sobrien * Default hop limit selection. The precedence is as follows: 55459243Sobrien * 1. Hoplimit value specified via ioctl. 55559243Sobrien * 2. (If the outgoing interface is detected) the current 55659243Sobrien * hop limit of the interface specified by router advertisement. 55759243Sobrien * 3. The system default hoplimit. 55859243Sobrien*/ 55959243Sobrienint 56059243Sobrienin6_selecthlim(struct inpcb *in6p, struct ifnet *ifp) 56159243Sobrien{ 56259243Sobrien if (in6p && in6p->inp_hops >= 0) 56359243Sobrien return (in6p->inp_hops); 56459243Sobrien else if (ifp) 56559243Sobrien return (ND_IFINFO(ifp)->chlim); 566145479Smp else 56759243Sobrien return (ip6_defhlim); 56859243Sobrien} 56959243Sobrien 57059243Sobrien/* 57159243Sobrien * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 57259243Sobrien * If the address scope of is link-local, embed the interface index in the 57359243Sobrien * address. The routine determines our precedence 57459243Sobrien * between advanced API scope/interface specification and basic API 57559243Sobrien * specification. 57659243Sobrien * 57759243Sobrien * this function should be nuked in the future, when we get rid of 57859243Sobrien * embedded scopeid thing. 57959243Sobrien * 58059243Sobrien * XXX actually, it is over-specification to return ifp against sin6_scope_id. 58159243Sobrien * there can be multiple interfaces that belong to a particular scope zone 58259243Sobrien * (in specification, we have 1:N mapping between a scope zone and interfaces). 583145479Smp * we may want to change the function to return something other than ifp. 58459243Sobrien */ 58559243Sobrienint 58659243Sobrienin6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6, 58759243Sobrien struct inpcb *in6p, struct ifnet **ifpp) 58859243Sobrien{ 58959243Sobrien struct ifnet *ifp = NULL; 59059243Sobrien u_int32_t scopeid; 59159243Sobrien 59259243Sobrien *in6 = sin6->sin6_addr; 59359243Sobrien scopeid = sin6->sin6_scope_id; 59459243Sobrien if (ifpp) 595 *ifpp = NULL; 596 597 /* 598 * don't try to read sin6->sin6_addr beyond here, since the caller may 599 * ask us to overwrite existing sockaddr_in6 600 */ 601 602 if (IN6_IS_SCOPE_EMBED(in6)) { 603 struct in6_pktinfo *pi; 604 605 /* 606 * KAME assumption: link id == interface id 607 */ 608 609 if (in6p && in6p->inp_outputopts6 && 610 (pi = in6p->inp_outputopts6->ip6po_pktinfo) && 611 pi->ipi6_ifindex) { 612 ifp = if_get(pi->ipi6_ifindex); 613 if (ifp == NULL) 614 return ENXIO; /* XXX EINVAL? */ 615 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 616 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 617 in6p->inp_moptions6 && 618 in6p->inp_moptions6->im6o_multicast_ifp) { 619 ifp = in6p->inp_moptions6->im6o_multicast_ifp; 620 in6->s6_addr16[1] = htons(ifp->if_index); 621 } else if (scopeid) { 622 ifp = if_get(scopeid); 623 if (ifp == NULL) 624 return ENXIO; /* XXX EINVAL? */ 625 /*XXX assignment to 16bit from 32bit variable */ 626 in6->s6_addr16[1] = htons(scopeid & 0xffff); 627 } 628 629 if (ifpp) 630 *ifpp = ifp; 631 } 632 633 return 0; 634} 635 636/* 637 * generate standard sockaddr_in6 from embedded form. 638 * touches sin6_addr and sin6_scope_id only. 639 * 640 * this function should be nuked in the future, when we get rid of 641 * embedded scopeid thing. 642 */ 643int 644in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6, 645 struct ifnet *ifp) 646{ 647 u_int32_t scopeid; 648 649 sin6->sin6_addr = *in6; 650 651 /* 652 * don't try to read *in6 beyond here, since the caller may 653 * ask us to overwrite existing sockaddr_in6 654 */ 655 656 sin6->sin6_scope_id = 0; 657 if (IN6_IS_SCOPE_EMBED(in6)) { 658 /* 659 * KAME assumption: link id == interface id 660 */ 661 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 662 if (scopeid) { 663 /* sanity check */ 664 if (if_get(scopeid) == NULL) 665 return ENXIO; 666 if (ifp && ifp->if_index != scopeid) 667 return ENXIO; 668 sin6->sin6_addr.s6_addr16[1] = 0; 669 sin6->sin6_scope_id = scopeid; 670 } 671 } 672 673 return 0; 674} 675 676/* 677 * just clear the embedded scope identifer. 678 */ 679void 680in6_clearscope(struct in6_addr *addr) 681{ 682 if (IN6_IS_SCOPE_EMBED(addr)) 683 addr->s6_addr16[1] = 0; 684} 685