Home | History | Annotate | Line # | Download | only in netinet6
in6_src.c revision 1.26
      1  1.23    rpaulo /*	$KAME: in6_src.c,v 1.159 2005/10/19 01:40:32 t-momose Exp $	*/
      2   1.1    itojun 
      3   1.1    itojun /*
      4   1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.1    itojun  * All rights reserved.
      6   1.1    itojun  *
      7   1.1    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.1    itojun  * modification, are permitted provided that the following conditions
      9   1.1    itojun  * are met:
     10   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.1    itojun  *    may be used to endorse or promote products derived from this software
     17   1.1    itojun  *    without specific prior written permission.
     18   1.1    itojun  *
     19   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1    itojun  * SUCH DAMAGE.
     30   1.1    itojun  */
     31   1.1    itojun 
     32   1.1    itojun /*
     33   1.1    itojun  * Copyright (c) 1982, 1986, 1991, 1993
     34   1.1    itojun  *	The Regents of the University of California.  All rights reserved.
     35   1.1    itojun  *
     36   1.1    itojun  * Redistribution and use in source and binary forms, with or without
     37   1.1    itojun  * modification, are permitted provided that the following conditions
     38   1.1    itojun  * are met:
     39   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     40   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     41   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     43   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     44  1.23    rpaulo  * 3. All advertising materials mentioning features or use of this software
     45  1.23    rpaulo  *    must display the following acknowledgement:
     46  1.23    rpaulo  *	This product includes software developed by the University of
     47  1.23    rpaulo  *	California, Berkeley and its contributors.
     48  1.23    rpaulo  * 4. Neither the name of the University nor the names of its contributors
     49   1.1    itojun  *    may be used to endorse or promote products derived from this software
     50   1.1    itojun  *    without specific prior written permission.
     51   1.1    itojun  *
     52   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.1    itojun  * SUCH DAMAGE.
     63   1.1    itojun  *
     64   1.1    itojun  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
     65   1.1    itojun  */
     66   1.9     lukem 
     67   1.9     lukem #include <sys/cdefs.h>
     68  1.26      elad __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.26 2006/05/14 21:19:34 elad Exp $");
     69   1.1    itojun 
     70   1.1    itojun #include "opt_inet.h"
     71   1.1    itojun 
     72   1.1    itojun #include <sys/param.h>
     73   1.1    itojun #include <sys/systm.h>
     74   1.1    itojun #include <sys/malloc.h>
     75   1.1    itojun #include <sys/mbuf.h>
     76   1.1    itojun #include <sys/protosw.h>
     77   1.1    itojun #include <sys/socket.h>
     78   1.1    itojun #include <sys/socketvar.h>
     79  1.23    rpaulo #ifndef __FreeBSD__
     80   1.1    itojun #include <sys/ioctl.h>
     81  1.23    rpaulo #else
     82  1.23    rpaulo #include <sys/sockio.h>
     83  1.23    rpaulo #endif
     84  1.23    rpaulo #ifdef __FreeBSD__
     85  1.23    rpaulo #include <sys/sysctl.h>
     86  1.23    rpaulo #endif
     87   1.1    itojun #include <sys/errno.h>
     88   1.1    itojun #include <sys/time.h>
     89  1.23    rpaulo #include <sys/kernel.h>
     90   1.1    itojun #include <sys/proc.h>
     91  1.26      elad #include <sys/kauth.h>
     92   1.1    itojun 
     93   1.1    itojun #include <net/if.h>
     94  1.23    rpaulo #include <net/if_types.h>
     95   1.1    itojun #include <net/route.h>
     96  1.23    rpaulo #ifdef RADIX_MPATH
     97  1.23    rpaulo #include <net/radix_mpath.h>
     98  1.23    rpaulo #endif
     99   1.1    itojun 
    100   1.1    itojun #include <netinet/in.h>
    101   1.1    itojun #include <netinet/in_var.h>
    102   1.1    itojun #include <netinet/in_systm.h>
    103   1.1    itojun #include <netinet/ip.h>
    104   1.1    itojun #include <netinet/in_pcb.h>
    105   1.1    itojun #include <netinet6/in6_var.h>
    106   1.1    itojun #include <netinet/ip6.h>
    107  1.23    rpaulo #ifndef __OpenBSD__
    108   1.1    itojun #include <netinet6/in6_pcb.h>
    109  1.23    rpaulo #endif
    110   1.1    itojun #include <netinet6/ip6_var.h>
    111   1.1    itojun #include <netinet6/nd6.h>
    112  1.13    itojun #include <netinet6/scope6_var.h>
    113  1.23    rpaulo 
    114  1.23    rpaulo #include <net/net_osdep.h>
    115  1.23    rpaulo 
    116  1.23    rpaulo #ifdef MIP6
    117  1.23    rpaulo #include <netinet6/mip6.h>
    118  1.23    rpaulo #include <netinet6/mip6_var.h>
    119  1.23    rpaulo #include "mip.h"
    120  1.23    rpaulo #if NMIP > 0
    121  1.23    rpaulo #include <net/if_mip.h>
    122  1.23    rpaulo #endif /* NMIP > 0 */
    123  1.23    rpaulo #endif /* MIP6 */
    124  1.23    rpaulo 
    125  1.23    rpaulo #ifndef __OpenBSD__
    126  1.23    rpaulo #include "loop.h"
    127  1.23    rpaulo #endif
    128  1.23    rpaulo #ifdef __NetBSD__
    129  1.23    rpaulo extern struct ifnet loif[NLOOP];
    130   1.2    itojun #endif
    131   1.1    itojun 
    132  1.23    rpaulo #define ADDR_LABEL_NOTAPP (-1)
    133  1.23    rpaulo struct in6_addrpolicy defaultaddrpolicy;
    134  1.23    rpaulo 
    135  1.23    rpaulo #ifdef notyet /* until introducing ND extensions and address selection */
    136  1.23    rpaulo int ip6_prefer_tempaddr = 0;
    137  1.23    rpaulo #endif
    138  1.23    rpaulo 
    139  1.23    rpaulo static int selectroute __P((struct sockaddr_in6 *, struct ip6_pktopts *,
    140  1.23    rpaulo 	struct ip6_moptions *, struct route_in6 *, struct ifnet **,
    141  1.23    rpaulo 	struct rtentry **, int, int));
    142  1.23    rpaulo static int in6_selectif __P((struct sockaddr_in6 *, struct ip6_pktopts *,
    143  1.23    rpaulo 	struct ip6_moptions *, struct route_in6 *, struct ifnet **));
    144  1.23    rpaulo 
    145  1.23    rpaulo static struct in6_addrpolicy *lookup_addrsel_policy __P((struct sockaddr_in6 *));
    146  1.23    rpaulo 
    147  1.23    rpaulo static void init_policy_queue __P((void));
    148  1.23    rpaulo static int add_addrsel_policyent __P((struct in6_addrpolicy *));
    149  1.23    rpaulo static int delete_addrsel_policyent __P((struct in6_addrpolicy *));
    150  1.23    rpaulo static int walk_addrsel_policy __P((int (*)(struct in6_addrpolicy *, void *),
    151  1.23    rpaulo 				    void *));
    152  1.23    rpaulo static int dump_addrsel_policyent __P((struct in6_addrpolicy *, void *));
    153  1.23    rpaulo static struct in6_addrpolicy *match_addrsel_policy __P((struct sockaddr_in6 *));
    154   1.1    itojun 
    155   1.1    itojun /*
    156   1.5    itojun  * Return an IPv6 address, which is the most appropriate for a given
    157   1.1    itojun  * destination and user specified options.
    158   1.5    itojun  * If necessary, this function lookups the routing table and returns
    159   1.1    itojun  * an entry to the caller for later use.
    160   1.1    itojun  */
    161  1.23    rpaulo #if 0				/* diabled ad-hoc */
    162  1.23    rpaulo #define REPLACE(r) do {\
    163  1.23    rpaulo 	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
    164  1.23    rpaulo 		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
    165  1.23    rpaulo 		ip6stat.ip6s_sources_rule[(r)]++; \
    166  1.23    rpaulo 	/* printf("in6_selectsrc: replace %s with %s by %d\n", ia_best ? ip6_sprintf(&ia_best->ia_addr.sin6_addr) : "none", ip6_sprintf(&ia->ia_addr.sin6_addr), (r)); */ \
    167  1.23    rpaulo 	goto replace; \
    168  1.23    rpaulo } while(0)
    169  1.23    rpaulo #define NEXT(r) do {\
    170  1.23    rpaulo 	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
    171  1.23    rpaulo 		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
    172  1.23    rpaulo 		ip6stat.ip6s_sources_rule[(r)]++; \
    173  1.23    rpaulo 	/* printf("in6_selectsrc: keep %s against %s by %d\n", ia_best ? ip6_sprintf(&ia_best->ia_addr.sin6_addr) : "none", ip6_sprintf(&ia->ia_addr.sin6_addr), (r)); */ \
    174  1.23    rpaulo 	goto next; 		/* XXX: we can't use 'continue' here */ \
    175  1.23    rpaulo } while(0)
    176  1.23    rpaulo #define BREAK(r) do { \
    177  1.23    rpaulo 	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
    178  1.23    rpaulo 		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
    179  1.23    rpaulo 		ip6stat.ip6s_sources_rule[(r)]++; \
    180  1.23    rpaulo 	goto out; 		/* XXX: we can't use 'break' here */ \
    181  1.23    rpaulo } while(0)
    182  1.23    rpaulo #else
    183  1.23    rpaulo #define REPLACE(r) goto replace
    184  1.23    rpaulo #define NEXT(r) goto next
    185  1.23    rpaulo #define BREAK(r) goto out
    186  1.23    rpaulo #endif
    187  1.23    rpaulo 
    188   1.1    itojun struct in6_addr *
    189  1.23    rpaulo in6_selectsrc(dstsock, opts, mopts, ro, laddr, ifpp, errorp)
    190   1.1    itojun 	struct sockaddr_in6 *dstsock;
    191   1.1    itojun 	struct ip6_pktopts *opts;
    192   1.1    itojun 	struct ip6_moptions *mopts;
    193   1.1    itojun 	struct route_in6 *ro;
    194   1.1    itojun 	struct in6_addr *laddr;
    195  1.23    rpaulo 	struct ifnet **ifpp;
    196   1.1    itojun 	int *errorp;
    197   1.1    itojun {
    198  1.23    rpaulo 	struct in6_addr dst;
    199  1.23    rpaulo 	struct ifnet *ifp = NULL;
    200  1.23    rpaulo 	struct in6_ifaddr *ia = NULL, *ia_best = NULL;
    201   1.1    itojun 	struct in6_pktinfo *pi = NULL;
    202  1.23    rpaulo 	int dst_scope = -1, best_scope = -1, best_matchlen = -1;
    203  1.23    rpaulo 	struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL;
    204  1.23    rpaulo 	u_int32_t odstzone;
    205  1.23    rpaulo #ifdef notyet /* until introducing ND extensions and address selection */
    206  1.23    rpaulo 	int prefer_tempaddr;
    207  1.23    rpaulo #endif
    208  1.23    rpaulo #if defined(MIP6) && NMIP > 0
    209  1.23    rpaulo 	u_int8_t ip6po_usecoa = 0;
    210  1.23    rpaulo #endif /* MIP6 && NMIP > 0 */
    211   1.1    itojun 
    212  1.23    rpaulo 	dst = dstsock->sin6_addr; /* make a copy for local operation */
    213   1.1    itojun 	*errorp = 0;
    214  1.23    rpaulo 	if (ifpp)
    215  1.23    rpaulo 		*ifpp = NULL;
    216   1.1    itojun 
    217   1.1    itojun 	/*
    218   1.1    itojun 	 * If the source address is explicitly specified by the caller,
    219  1.23    rpaulo 	 * check if the requested source address is indeed a unicast address
    220  1.23    rpaulo 	 * assigned to the node, and can be used as the packet's source
    221  1.23    rpaulo 	 * address.  If everything is okay, use the address as source.
    222   1.1    itojun 	 */
    223   1.1    itojun 	if (opts && (pi = opts->ip6po_pktinfo) &&
    224  1.23    rpaulo 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
    225  1.23    rpaulo 		struct sockaddr_in6 srcsock;
    226  1.23    rpaulo 		struct in6_ifaddr *ia6;
    227  1.23    rpaulo 
    228  1.23    rpaulo 		/* get the outgoing interface */
    229  1.23    rpaulo 		if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ifp))
    230  1.23    rpaulo 		    != 0) {
    231  1.23    rpaulo 			return (NULL);
    232  1.23    rpaulo 		}
    233  1.23    rpaulo 
    234  1.23    rpaulo 		/*
    235  1.23    rpaulo 		 * Determine the appropriate zone id of the source based on
    236  1.23    rpaulo 		 * the zone of the destination and the outgoing interface.
    237  1.23    rpaulo 		 * If the specified address is ambiguous wrt the scope zone,
    238  1.23    rpaulo 		 * the interface must be specified; otherwise, ifa_ifwithaddr()
    239  1.23    rpaulo 		 * will fail matching the address.
    240  1.23    rpaulo 		 */
    241  1.23    rpaulo 		bzero(&srcsock, sizeof(srcsock));
    242  1.23    rpaulo 		srcsock.sin6_family = AF_INET6;
    243  1.23    rpaulo 		srcsock.sin6_len = sizeof(srcsock);
    244  1.23    rpaulo 		srcsock.sin6_addr = pi->ipi6_addr;
    245  1.23    rpaulo 		if (ifp) {
    246  1.23    rpaulo 			*errorp = in6_setscope(&srcsock.sin6_addr, ifp, NULL);
    247  1.23    rpaulo 			if (*errorp != 0)
    248  1.23    rpaulo 				return (NULL);
    249  1.23    rpaulo 		}
    250  1.23    rpaulo 
    251  1.23    rpaulo 		ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)(&srcsock));
    252  1.23    rpaulo 		if (ia6 == NULL ||
    253  1.23    rpaulo 		    (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) {
    254  1.23    rpaulo 			*errorp = EADDRNOTAVAIL;
    255  1.23    rpaulo 			return (NULL);
    256  1.23    rpaulo 		}
    257  1.23    rpaulo 		pi->ipi6_addr = srcsock.sin6_addr; /* XXX: this overrides pi */
    258  1.23    rpaulo 		if (ifpp)
    259  1.23    rpaulo 			*ifpp = ifp;
    260  1.23    rpaulo 		return (&ia6->ia_addr.sin6_addr);
    261  1.23    rpaulo 	}
    262   1.1    itojun 
    263   1.1    itojun 	/*
    264  1.23    rpaulo 	 * Otherwise, if the socket has already bound the source, just use it.
    265   1.1    itojun 	 */
    266   1.1    itojun 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
    267  1.15    itojun 		return (laddr);
    268   1.1    itojun 
    269   1.1    itojun 	/*
    270  1.23    rpaulo 	 * If the address is not specified, choose the best one based on
    271  1.23    rpaulo 	 * the outgoing interface and the destination address.
    272   1.1    itojun 	 */
    273  1.23    rpaulo 	/* get the outgoing interface */
    274  1.23    rpaulo 	if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, &ifp)) != 0)
    275  1.23    rpaulo 		return (NULL);
    276   1.1    itojun 
    277  1.23    rpaulo #if defined(MIP6) && NMIP > 0
    278   1.1    itojun 	/*
    279  1.23    rpaulo 	 * a caller can specify IP6PO_USECOA to not to use a home
    280  1.23    rpaulo 	 * address.  for example, the case that the neighbour
    281  1.23    rpaulo 	 * unreachability detection to the global address.
    282   1.1    itojun 	 */
    283  1.23    rpaulo 	if (opts != NULL &&
    284  1.23    rpaulo 	    (opts->ip6po_flags & IP6PO_USECOA) != 0) {
    285  1.23    rpaulo 		ip6po_usecoa = 1;
    286  1.23    rpaulo 	}
    287  1.23    rpaulo #endif /* MIP6 && NMIP > 0 */
    288  1.23    rpaulo 
    289  1.23    rpaulo #ifdef DIAGNOSTIC
    290  1.23    rpaulo 	if (ifp == NULL)	/* this should not happen */
    291  1.23    rpaulo 		panic("in6_selectsrc: NULL ifp");
    292  1.23    rpaulo #endif
    293  1.23    rpaulo 	*errorp = in6_setscope(&dst, ifp, &odstzone);
    294  1.23    rpaulo 	if (*errorp != 0)
    295  1.23    rpaulo 		return (NULL);
    296  1.23    rpaulo 
    297  1.23    rpaulo 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
    298  1.23    rpaulo 		int new_scope = -1, new_matchlen = -1;
    299  1.23    rpaulo 		struct in6_addrpolicy *new_policy = NULL;
    300  1.23    rpaulo 		u_int32_t srczone, osrczone, dstzone;
    301  1.23    rpaulo 		struct in6_addr src;
    302  1.23    rpaulo 		struct ifnet *ifp1 = ia->ia_ifp;
    303  1.23    rpaulo 
    304   1.1    itojun 		/*
    305  1.23    rpaulo 		 * We'll never take an address that breaks the scope zone
    306  1.23    rpaulo 		 * of the destination.  We also skip an address if its zone
    307  1.23    rpaulo 		 * does not contain the outgoing interface.
    308  1.23    rpaulo 		 * XXX: we should probably use sin6_scope_id here.
    309   1.1    itojun 		 */
    310  1.23    rpaulo 		if (in6_setscope(&dst, ifp1, &dstzone) ||
    311  1.23    rpaulo 		    odstzone != dstzone) {
    312  1.23    rpaulo 			continue;
    313  1.23    rpaulo 		}
    314  1.23    rpaulo 		src = ia->ia_addr.sin6_addr;
    315  1.23    rpaulo 		if (in6_setscope(&src, ifp, &osrczone) ||
    316  1.23    rpaulo 		    in6_setscope(&src, ifp1, &srczone) ||
    317  1.23    rpaulo 		    osrczone != srczone) {
    318  1.23    rpaulo 			continue;
    319  1.23    rpaulo 		}
    320  1.23    rpaulo 
    321  1.23    rpaulo 		/* avoid unusable addresses */
    322  1.23    rpaulo 		if ((ia->ia6_flags &
    323  1.23    rpaulo 		     (IN6_IFF_NOTREADY | IN6_IFF_ANYCAST | IN6_IFF_DETACHED))) {
    324  1.23    rpaulo 				continue;
    325  1.23    rpaulo 		}
    326  1.23    rpaulo 		if (!ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
    327  1.23    rpaulo 			continue;
    328  1.23    rpaulo 
    329  1.23    rpaulo #if defined(MIP6) && NMIP > 0
    330  1.23    rpaulo 		/* avoid unusable home addresses. */
    331  1.23    rpaulo 		if ((ia->ia6_flags & IN6_IFF_HOME) &&
    332  1.23    rpaulo 		    !mip6_ifa6_is_addr_valid_hoa(ia))
    333  1.23    rpaulo 			continue;
    334  1.23    rpaulo #endif /* MIP6 && NMIP > 0 */
    335  1.23    rpaulo 
    336  1.23    rpaulo 		/* Rule 1: Prefer same address */
    337  1.23    rpaulo 		if (IN6_ARE_ADDR_EQUAL(&dst, &ia->ia_addr.sin6_addr)) {
    338  1.23    rpaulo 			ia_best = ia;
    339  1.23    rpaulo 			BREAK(1); /* there should be no better candidate */
    340   1.1    itojun 		}
    341   1.1    itojun 
    342  1.23    rpaulo 		if (ia_best == NULL)
    343  1.23    rpaulo 			REPLACE(0);
    344  1.23    rpaulo 
    345  1.23    rpaulo 		/* Rule 2: Prefer appropriate scope */
    346  1.23    rpaulo 		if (dst_scope < 0)
    347  1.23    rpaulo 			dst_scope = in6_addrscope(&dst);
    348  1.23    rpaulo 		new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
    349  1.23    rpaulo 		if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
    350  1.23    rpaulo 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
    351  1.23    rpaulo 				REPLACE(2);
    352  1.23    rpaulo 			NEXT(2);
    353  1.23    rpaulo 		} else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
    354  1.23    rpaulo 			if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
    355  1.23    rpaulo 				NEXT(2);
    356  1.23    rpaulo 			REPLACE(2);
    357  1.23    rpaulo 		}
    358   1.1    itojun 
    359  1.23    rpaulo 		/*
    360  1.23    rpaulo 		 * Rule 3: Avoid deprecated addresses.  Note that the case of
    361  1.23    rpaulo 		 * !ip6_use_deprecated is already rejected above.
    362  1.23    rpaulo 		 */
    363  1.23    rpaulo 		if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
    364  1.23    rpaulo 			NEXT(3);
    365  1.23    rpaulo 		if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
    366  1.23    rpaulo 			REPLACE(3);
    367  1.23    rpaulo 
    368  1.23    rpaulo 		/* Rule 4: Prefer home addresses */
    369  1.23    rpaulo #if defined(MIP6) && NMIP > 0
    370  1.23    rpaulo 		if (!MIP6_IS_MN)
    371  1.23    rpaulo 			goto skip_rule4;
    372  1.23    rpaulo 
    373  1.23    rpaulo 		if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
    374  1.23    rpaulo 		    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
    375  1.23    rpaulo 			/* both address are not home addresses. */
    376  1.23    rpaulo 			goto skip_rule4;
    377  1.23    rpaulo 		}
    378   1.1    itojun 
    379  1.23    rpaulo 		/*
    380  1.23    rpaulo 		 * If SA is simultaneously a home address and care-of
    381  1.23    rpaulo 		 * address and SB is not, then prefer SA. Similarly,
    382  1.23    rpaulo 		 * if SB is simultaneously a home address and care-of
    383  1.23    rpaulo 		 * address and SA is not, then prefer SB.
    384  1.23    rpaulo 		 */
    385  1.23    rpaulo 		if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
    386  1.23    rpaulo 			ia_best->ia_ifp->if_type != IFT_MIP)
    387  1.23    rpaulo 		    &&
    388  1.23    rpaulo 		    ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
    389  1.23    rpaulo 			ia->ia_ifp->if_type == IFT_MIP))
    390  1.23    rpaulo 			NEXT(4);
    391  1.23    rpaulo 		if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
    392  1.23    rpaulo 			ia_best->ia_ifp->if_type == IFT_MIP)
    393  1.23    rpaulo 		    &&
    394  1.23    rpaulo 		    ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
    395  1.23    rpaulo 			ia->ia_ifp->if_type != IFT_MIP))
    396  1.23    rpaulo 			REPLACE(4);
    397  1.23    rpaulo 		if (ip6po_usecoa == 0) {
    398  1.23    rpaulo 			/*
    399  1.23    rpaulo 			 * If SA is just a home address and SB is just
    400  1.23    rpaulo 			 * a care-of address, then prefer
    401  1.23    rpaulo 			 * SA. Similarly, if SB is just a home address
    402  1.23    rpaulo 			 * and SA is just a care-of address, then
    403  1.23    rpaulo 			 * prefer SB.
    404  1.23    rpaulo 			 */
    405  1.23    rpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
    406  1.23    rpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
    407  1.23    rpaulo 				NEXT(4);
    408  1.23    rpaulo 			}
    409  1.23    rpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
    410  1.23    rpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) != 0) {
    411  1.23    rpaulo 				REPLACE(4);
    412  1.23    rpaulo 			}
    413  1.23    rpaulo 		} else {
    414  1.23    rpaulo 			/*
    415  1.23    rpaulo 			 * a sender don't want to use a home address
    416  1.23    rpaulo 			 * because:
    417  1.23    rpaulo 			 *
    418  1.23    rpaulo 			 * 1) we cannot use.  (ex. NS or NA to global
    419  1.23    rpaulo 			 * addresses.)
    420  1.23    rpaulo 			 *
    421  1.23    rpaulo 			 * 2) a user specified not to use.
    422  1.23    rpaulo 			 * (ex. mip6control -u)
    423  1.23    rpaulo 			 */
    424  1.23    rpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
    425  1.23    rpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) != 0) {
    426  1.23    rpaulo 				/* XXX breaks stat */
    427  1.23    rpaulo 				NEXT(0);
    428  1.23    rpaulo 			}
    429  1.23    rpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
    430  1.23    rpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
    431  1.23    rpaulo 				/* XXX breaks stat */
    432  1.23    rpaulo 				REPLACE(0);
    433   1.1    itojun 			}
    434  1.23    rpaulo 		}
    435  1.23    rpaulo 	skip_rule4:
    436  1.23    rpaulo #endif /* MIP6 && NMIP > 0 */
    437  1.23    rpaulo 
    438  1.23    rpaulo 		/* Rule 5: Prefer outgoing interface */
    439  1.23    rpaulo 		if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
    440  1.23    rpaulo 			NEXT(5);
    441  1.23    rpaulo 		if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
    442  1.23    rpaulo 			REPLACE(5);
    443  1.23    rpaulo 
    444  1.23    rpaulo 		/*
    445  1.23    rpaulo 		 * Rule 6: Prefer matching label
    446  1.23    rpaulo 		 * Note that best_policy should be non-NULL here.
    447  1.23    rpaulo 		 */
    448  1.23    rpaulo 		if (dst_policy == NULL)
    449  1.23    rpaulo 			dst_policy = lookup_addrsel_policy(dstsock);
    450  1.23    rpaulo 		if (dst_policy->label != ADDR_LABEL_NOTAPP) {
    451  1.23    rpaulo 			new_policy = lookup_addrsel_policy(&ia->ia_addr);
    452  1.23    rpaulo 			if (dst_policy->label == best_policy->label &&
    453  1.23    rpaulo 			    dst_policy->label != new_policy->label)
    454  1.23    rpaulo 				NEXT(6);
    455  1.23    rpaulo 			if (dst_policy->label != best_policy->label &&
    456  1.23    rpaulo 			    dst_policy->label == new_policy->label)
    457  1.23    rpaulo 				REPLACE(6);
    458  1.23    rpaulo 		}
    459  1.23    rpaulo 
    460  1.23    rpaulo 		/*
    461  1.23    rpaulo 		 * Rule 7: Prefer public addresses.
    462  1.23    rpaulo 		 * We allow users to reverse the logic by configuring
    463  1.23    rpaulo 		 * a sysctl variable, so that privacy conscious users can
    464  1.23    rpaulo 		 * always prefer temporary addresses.
    465  1.23    rpaulo 		 */
    466  1.23    rpaulo #ifdef notyet /* until introducing ND extensions and address selection */
    467  1.23    rpaulo 		if (opts == NULL ||
    468  1.23    rpaulo 		    opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
    469  1.23    rpaulo 			prefer_tempaddr = ip6_prefer_tempaddr;
    470  1.23    rpaulo 		} else if (opts->ip6po_prefer_tempaddr ==
    471  1.23    rpaulo 		    IP6PO_TEMPADDR_NOTPREFER) {
    472  1.23    rpaulo 			prefer_tempaddr = 0;
    473  1.23    rpaulo 		} else
    474  1.23    rpaulo 			prefer_tempaddr = 1;
    475  1.23    rpaulo 		if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
    476  1.23    rpaulo 		    (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
    477  1.23    rpaulo 			if (prefer_tempaddr)
    478  1.23    rpaulo 				REPLACE(7);
    479  1.23    rpaulo 			else
    480  1.23    rpaulo 				NEXT(7);
    481  1.23    rpaulo 		}
    482  1.23    rpaulo 		if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
    483  1.23    rpaulo 		    !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
    484  1.23    rpaulo 			if (prefer_tempaddr)
    485  1.23    rpaulo 				NEXT(7);
    486  1.23    rpaulo 			else
    487  1.23    rpaulo 				REPLACE(7);
    488   1.1    itojun 		}
    489  1.23    rpaulo #endif
    490  1.23    rpaulo 
    491  1.23    rpaulo 		/*
    492  1.23    rpaulo 		 * Rule 8: prefer addresses on alive interfaces.
    493  1.23    rpaulo 		 * This is a KAME specific rule.
    494  1.23    rpaulo 		 */
    495  1.23    rpaulo 		if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
    496  1.23    rpaulo 		    !(ia->ia_ifp->if_flags & IFF_UP))
    497  1.23    rpaulo 			NEXT(8);
    498  1.23    rpaulo 		if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
    499  1.23    rpaulo 		    (ia->ia_ifp->if_flags & IFF_UP))
    500  1.23    rpaulo 			REPLACE(8);
    501  1.23    rpaulo 
    502  1.23    rpaulo 		/*
    503  1.23    rpaulo 		 * Rule 9: prefer addresses on "preferred" interfaces.
    504  1.23    rpaulo 		 * This is a KAME specific rule.
    505  1.23    rpaulo 		 */
    506  1.23    rpaulo #ifdef notyet			/* until introducing address selection */
    507  1.23    rpaulo #define NDI_BEST ND_IFINFO(ia_best->ia_ifp)
    508  1.23    rpaulo #define NDI_NEW  ND_IFINFO(ia->ia_ifp)
    509  1.23    rpaulo 		if ((NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
    510  1.23    rpaulo 		    !(NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
    511  1.23    rpaulo 			NEXT(9);
    512  1.23    rpaulo 		if (!(NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
    513  1.23    rpaulo 		    (NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
    514  1.23    rpaulo 			REPLACE(9);
    515  1.23    rpaulo #undef NDI_BEST
    516  1.23    rpaulo #undef NDI_NEW
    517  1.23    rpaulo #endif
    518  1.23    rpaulo 
    519  1.23    rpaulo 		/*
    520  1.23    rpaulo 		 * Rule 14: Use longest matching prefix.
    521  1.23    rpaulo 		 * Note: in the address selection draft, this rule is
    522  1.23    rpaulo 		 * documented as "Rule 8".  However, since it is also
    523  1.23    rpaulo 		 * documented that this rule can be overridden, we assign
    524  1.23    rpaulo 		 * a large number so that it is easy to assign smaller numbers
    525  1.23    rpaulo 		 * to more preferred rules.
    526  1.23    rpaulo 		 */
    527  1.23    rpaulo 		new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, &dst);
    528  1.23    rpaulo 		if (best_matchlen < new_matchlen)
    529  1.23    rpaulo 			REPLACE(14);
    530  1.23    rpaulo 		if (new_matchlen < best_matchlen)
    531  1.23    rpaulo 			NEXT(14);
    532  1.23    rpaulo 
    533  1.23    rpaulo 		/* Rule 15 is reserved. */
    534  1.23    rpaulo 
    535  1.23    rpaulo 		/*
    536  1.23    rpaulo 		 * Last resort: just keep the current candidate.
    537  1.23    rpaulo 		 * Or, do we need more rules?
    538  1.23    rpaulo 		 */
    539  1.23    rpaulo 		continue;
    540  1.23    rpaulo 
    541  1.23    rpaulo 	  replace:
    542  1.23    rpaulo 		ia_best = ia;
    543  1.23    rpaulo 		best_scope = (new_scope >= 0 ? new_scope :
    544  1.23    rpaulo 			      in6_addrscope(&ia_best->ia_addr.sin6_addr));
    545  1.23    rpaulo 		best_policy = (new_policy ? new_policy :
    546  1.23    rpaulo 			       lookup_addrsel_policy(&ia_best->ia_addr));
    547  1.23    rpaulo 		best_matchlen = (new_matchlen >= 0 ? new_matchlen :
    548  1.23    rpaulo 				 in6_matchlen(&ia_best->ia_addr.sin6_addr,
    549  1.23    rpaulo 					      &dst));
    550  1.23    rpaulo 
    551  1.23    rpaulo 	  next:
    552  1.23    rpaulo 		continue;
    553  1.23    rpaulo 
    554  1.23    rpaulo 	  out:
    555  1.23    rpaulo 		break;
    556  1.23    rpaulo 	}
    557  1.23    rpaulo 
    558  1.23    rpaulo 	if ((ia = ia_best) == NULL) {
    559  1.23    rpaulo 		*errorp = EADDRNOTAVAIL;
    560  1.23    rpaulo 		return (NULL);
    561  1.23    rpaulo 	}
    562  1.23    rpaulo 
    563  1.23    rpaulo 	if (ifpp)
    564  1.23    rpaulo 		*ifpp = ifp;
    565  1.23    rpaulo 	return (&ia->ia_addr.sin6_addr);
    566  1.23    rpaulo }
    567  1.23    rpaulo #undef REPLACE
    568  1.23    rpaulo #undef BREAK
    569  1.23    rpaulo #undef NEXT
    570  1.23    rpaulo 
    571  1.23    rpaulo static int
    572  1.23    rpaulo selectroute(dstsock, opts, mopts, ro, retifp, retrt, clone, norouteok)
    573  1.23    rpaulo 	struct sockaddr_in6 *dstsock;
    574  1.23    rpaulo 	struct ip6_pktopts *opts;
    575  1.23    rpaulo 	struct ip6_moptions *mopts;
    576  1.23    rpaulo #ifdef NEW_STRUCT_ROUTE
    577  1.23    rpaulo 	struct route *ro;
    578  1.23    rpaulo #else
    579  1.23    rpaulo 	struct route_in6 *ro;
    580  1.23    rpaulo #endif
    581  1.23    rpaulo 	struct ifnet **retifp;
    582  1.23    rpaulo 	struct rtentry **retrt;
    583  1.23    rpaulo 	int clone;
    584  1.23    rpaulo 	int norouteok;
    585  1.23    rpaulo {
    586  1.23    rpaulo 	int error = 0;
    587  1.23    rpaulo 	struct ifnet *ifp = NULL;
    588  1.23    rpaulo 	struct rtentry *rt = NULL;
    589  1.23    rpaulo 	struct sockaddr_in6 *sin6_next;
    590  1.23    rpaulo 	struct in6_pktinfo *pi = NULL;
    591  1.23    rpaulo 	struct in6_addr *dst;
    592  1.23    rpaulo 
    593  1.23    rpaulo 	dst = &dstsock->sin6_addr;
    594  1.23    rpaulo 
    595  1.23    rpaulo #if 0
    596  1.23    rpaulo 	if (dstsock->sin6_addr.s6_addr32[0] == 0 &&
    597  1.23    rpaulo 	    dstsock->sin6_addr.s6_addr32[1] == 0 &&
    598  1.23    rpaulo 	    !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) {
    599  1.23    rpaulo 		printf("in6_selectroute: strange destination %s\n",
    600  1.23    rpaulo 		       ip6_sprintf(&dstsock->sin6_addr));
    601  1.23    rpaulo 	} else {
    602  1.23    rpaulo 		printf("in6_selectroute: destination = %s%%%d\n",
    603  1.23    rpaulo 		       ip6_sprintf(&dstsock->sin6_addr),
    604  1.23    rpaulo 		       dstsock->sin6_scope_id); /* for debug */
    605  1.23    rpaulo 	}
    606  1.23    rpaulo #endif
    607  1.23    rpaulo 
    608  1.23    rpaulo 	/* If the caller specify the outgoing interface explicitly, use it. */
    609  1.23    rpaulo 	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
    610  1.23    rpaulo 		/* XXX boundary check is assumed to be already done. */
    611  1.23    rpaulo #ifdef __FreeBSD__
    612  1.23    rpaulo 		ifp = ifnet_byindex(pi->ipi6_ifindex);
    613  1.23    rpaulo #else
    614  1.23    rpaulo 		ifp = ifindex2ifnet[pi->ipi6_ifindex];
    615  1.23    rpaulo #endif
    616  1.23    rpaulo 		if (ifp != NULL &&
    617  1.23    rpaulo 		    (norouteok || retrt == NULL ||
    618  1.23    rpaulo 		    IN6_IS_ADDR_MULTICAST(dst))) {
    619  1.23    rpaulo 			/*
    620  1.23    rpaulo 			 * we do not have to check or get the route for
    621  1.23    rpaulo 			 * multicast.
    622  1.23    rpaulo 			 */
    623  1.23    rpaulo 			goto done;
    624  1.23    rpaulo 		} else
    625  1.23    rpaulo 			goto getroute;
    626   1.1    itojun 	}
    627   1.1    itojun 
    628   1.1    itojun 	/*
    629  1.23    rpaulo 	 * If the destination address is a multicast address and the outgoing
    630  1.23    rpaulo 	 * interface for the address is specified by the caller, use it.
    631   1.1    itojun 	 */
    632  1.23    rpaulo 	if (IN6_IS_ADDR_MULTICAST(dst) &&
    633  1.23    rpaulo 	    mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) {
    634  1.23    rpaulo 		goto done; /* we do not need a route for multicast. */
    635  1.23    rpaulo 	}
    636  1.23    rpaulo 
    637  1.23    rpaulo   getroute:
    638  1.23    rpaulo 	/*
    639  1.23    rpaulo 	 * If the next hop address for the packet is specified by the caller,
    640  1.23    rpaulo 	 * use it as the gateway.
    641  1.23    rpaulo 	 */
    642  1.23    rpaulo 	if (opts && opts->ip6po_nexthop) {
    643  1.23    rpaulo 		struct route_in6 *ron;
    644  1.23    rpaulo 
    645  1.23    rpaulo 		sin6_next = satosin6(opts->ip6po_nexthop);
    646  1.23    rpaulo 
    647  1.23    rpaulo 		/* at this moment, we only support AF_INET6 next hops */
    648  1.23    rpaulo 		if (sin6_next->sin6_family != AF_INET6) {
    649  1.23    rpaulo 			error = EAFNOSUPPORT; /* or should we proceed? */
    650  1.23    rpaulo 			goto done;
    651  1.23    rpaulo 		}
    652  1.23    rpaulo 
    653  1.23    rpaulo 		/*
    654  1.23    rpaulo 		 * If the next hop is an IPv6 address, then the node identified
    655  1.23    rpaulo 		 * by that address must be a neighbor of the sending host.
    656  1.23    rpaulo 		 */
    657  1.23    rpaulo 		ron = &opts->ip6po_nextroute;
    658  1.23    rpaulo 		if ((ron->ro_rt &&
    659  1.23    rpaulo 		    (ron->ro_rt->rt_flags & (RTF_UP | RTF_GATEWAY)) !=
    660  1.23    rpaulo 		    RTF_UP) ||
    661  1.23    rpaulo 		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ron->ro_dst)->sin6_addr,
    662  1.23    rpaulo 		    &sin6_next->sin6_addr)) {
    663  1.23    rpaulo 			if (ron->ro_rt) {
    664  1.23    rpaulo 				RTFREE(ron->ro_rt);
    665  1.23    rpaulo 				ron->ro_rt = NULL;
    666   1.1    itojun 			}
    667  1.23    rpaulo 			*satosin6(&ron->ro_dst) = *sin6_next;
    668  1.23    rpaulo 		}
    669  1.23    rpaulo 		if (ron->ro_rt == NULL) {
    670  1.23    rpaulo 			rtalloc((struct route *)ron); /* multi path case? */
    671  1.23    rpaulo 			if (ron->ro_rt == NULL ||
    672  1.23    rpaulo 			    (ron->ro_rt->rt_flags & RTF_GATEWAY)) {
    673  1.23    rpaulo 				if (ron->ro_rt) {
    674  1.23    rpaulo 					RTFREE(ron->ro_rt);
    675  1.23    rpaulo 					ron->ro_rt = NULL;
    676  1.23    rpaulo 				}
    677  1.23    rpaulo 				error = EHOSTUNREACH;
    678  1.23    rpaulo 				goto done;
    679   1.1    itojun 			}
    680   1.1    itojun 		}
    681  1.23    rpaulo 		if (!nd6_is_addr_neighbor(sin6_next, ron->ro_rt->rt_ifp)) {
    682  1.23    rpaulo 			RTFREE(ron->ro_rt);
    683  1.23    rpaulo 			ron->ro_rt = NULL;
    684  1.23    rpaulo 			error = EHOSTUNREACH;
    685  1.23    rpaulo 			goto done;
    686  1.23    rpaulo 		}
    687  1.23    rpaulo 		rt = ron->ro_rt;
    688  1.23    rpaulo 		ifp = rt->rt_ifp;
    689  1.23    rpaulo 
    690  1.23    rpaulo 		/*
    691  1.23    rpaulo 		 * When cloning is required, try to allocate a route to the
    692  1.23    rpaulo 		 * destination so that the caller can store path MTU
    693  1.23    rpaulo 		 * information.
    694  1.23    rpaulo 		 */
    695  1.23    rpaulo 		if (!clone)
    696  1.23    rpaulo 			goto done;
    697   1.1    itojun 	}
    698   1.1    itojun 
    699   1.1    itojun 	/*
    700  1.23    rpaulo 	 * Use a cached route if it exists and is valid, else try to allocate
    701  1.23    rpaulo 	 * a new one.  Note that we should check the address family of the
    702  1.13    itojun 	 * cached destination, in case of sharing the cache with IPv4.
    703   1.1    itojun 	 */
    704   1.1    itojun 	if (ro) {
    705   1.1    itojun 		if (ro->ro_rt &&
    706  1.23    rpaulo 		    (!(ro->ro_rt->rt_flags & RTF_UP) ||
    707  1.23    rpaulo 		     ((struct sockaddr *)(&ro->ro_dst))->sa_family != AF_INET6 ||
    708  1.23    rpaulo 		     !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr,
    709  1.23    rpaulo 		     dst))) {
    710   1.1    itojun 			RTFREE(ro->ro_rt);
    711  1.23    rpaulo 			ro->ro_rt = (struct rtentry *)NULL;
    712   1.1    itojun 		}
    713  1.23    rpaulo 		if (ro->ro_rt == (struct rtentry *)NULL) {
    714   1.5    itojun 			struct sockaddr_in6 *sa6;
    715   1.5    itojun 
    716   1.1    itojun 			/* No route yet, so try to acquire one */
    717   1.1    itojun 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
    718   1.5    itojun 			sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
    719  1.23    rpaulo 			*sa6 = *dstsock;
    720  1.23    rpaulo 			sa6->sin6_scope_id = 0;
    721  1.23    rpaulo 			if (clone) {
    722  1.23    rpaulo #ifdef RADIX_MPATH
    723  1.23    rpaulo 				rtalloc_mpath((struct route *)ro,
    724  1.23    rpaulo 				    ntohl(sa6->sin6_addr.s6_addr32[3]));
    725  1.23    rpaulo #else
    726  1.23    rpaulo 				rtalloc((struct route *)ro);
    727  1.23    rpaulo #endif /* RADIX_MPATH */
    728  1.23    rpaulo 			} else {
    729  1.23    rpaulo #ifdef RADIX_MPATH
    730  1.23    rpaulo 				rtalloc_mpath((struct route *)ro,
    731  1.23    rpaulo 				    ntohl(sa6->sin6_addr.s6_addr32[3]));
    732  1.23    rpaulo #else
    733   1.1    itojun 				ro->ro_rt = rtalloc1(&((struct route *)ro)
    734   1.1    itojun 						     ->ro_dst, 0);
    735  1.23    rpaulo #endif /* RADIX_MPATH */
    736   1.1    itojun 			}
    737   1.1    itojun 		}
    738   1.1    itojun 
    739   1.1    itojun 		/*
    740  1.23    rpaulo 		 * do not care about the result if we have the nexthop
    741  1.23    rpaulo 		 * explicitly specified.
    742   1.1    itojun 		 */
    743  1.23    rpaulo 		if (opts && opts->ip6po_nexthop)
    744  1.23    rpaulo 			goto done;
    745   1.1    itojun 
    746   1.1    itojun 		if (ro->ro_rt) {
    747  1.23    rpaulo 			ifp = ro->ro_rt->rt_ifp;
    748  1.23    rpaulo 
    749  1.23    rpaulo 			if (ifp == NULL) { /* can this really happen? */
    750  1.23    rpaulo 				RTFREE(ro->ro_rt);
    751  1.23    rpaulo 				ro->ro_rt = NULL;
    752  1.23    rpaulo 			}
    753   1.1    itojun 		}
    754  1.23    rpaulo 		if (ro->ro_rt == NULL)
    755  1.23    rpaulo 			error = EHOSTUNREACH;
    756  1.23    rpaulo 		rt = ro->ro_rt;
    757  1.23    rpaulo 
    758  1.23    rpaulo 		/*
    759  1.23    rpaulo 		 * Check if the outgoing interface conflicts with
    760  1.23    rpaulo 		 * the interface specified by ipi6_ifindex (if specified).
    761  1.23    rpaulo 		 * Note that loopback interface is always okay.
    762  1.23    rpaulo 		 * (this may happen when we are sending a packet to one of
    763  1.23    rpaulo 		 *  our own addresses.)
    764  1.23    rpaulo 		 */
    765  1.23    rpaulo 		if (opts && opts->ip6po_pktinfo &&
    766  1.23    rpaulo 		    opts->ip6po_pktinfo->ipi6_ifindex) {
    767  1.23    rpaulo 			if (!(ifp->if_flags & IFF_LOOPBACK) &&
    768  1.23    rpaulo 			    ifp->if_index !=
    769  1.23    rpaulo 			    opts->ip6po_pktinfo->ipi6_ifindex) {
    770  1.23    rpaulo 				error = EHOSTUNREACH;
    771  1.23    rpaulo 				goto done;
    772  1.23    rpaulo 			}
    773  1.23    rpaulo 		}
    774  1.23    rpaulo 	}
    775  1.23    rpaulo 
    776  1.23    rpaulo   done:
    777  1.23    rpaulo 	if (ifp == NULL && rt == NULL) {
    778   1.1    itojun 		/*
    779  1.23    rpaulo 		 * This can happen if the caller did not pass a cached route
    780  1.23    rpaulo 		 * nor any other hints.  We treat this case an error.
    781   1.1    itojun 		 */
    782  1.23    rpaulo 		error = EHOSTUNREACH;
    783  1.23    rpaulo 	}
    784  1.23    rpaulo 	if (error == EHOSTUNREACH)
    785  1.23    rpaulo 		ip6stat.ip6s_noroute++;
    786   1.1    itojun 
    787  1.23    rpaulo 	if (retifp != NULL)
    788  1.23    rpaulo 		*retifp = ifp;
    789  1.23    rpaulo 	if (retrt != NULL)
    790  1.23    rpaulo 		*retrt = rt;	/* rt may be NULL */
    791   1.1    itojun 
    792  1.23    rpaulo 	return (error);
    793  1.23    rpaulo }
    794  1.23    rpaulo 
    795  1.23    rpaulo static int
    796  1.23    rpaulo in6_selectif(dstsock, opts, mopts, ro, retifp)
    797  1.23    rpaulo 	struct sockaddr_in6 *dstsock;
    798  1.23    rpaulo 	struct ip6_pktopts *opts;
    799  1.23    rpaulo 	struct ip6_moptions *mopts;
    800  1.23    rpaulo 	struct route_in6 *ro;
    801  1.23    rpaulo 	struct ifnet **retifp;
    802  1.23    rpaulo {
    803  1.23    rpaulo 	int error, clone;
    804  1.23    rpaulo 	struct rtentry *rt = NULL;
    805  1.23    rpaulo 
    806  1.23    rpaulo 	clone = IN6_IS_ADDR_MULTICAST(&dstsock->sin6_addr) ? 0 : 1;
    807  1.23    rpaulo 	if ((error = selectroute(dstsock, opts, mopts, ro, retifp,
    808  1.23    rpaulo 	    &rt, clone, 1)) != 0) {
    809  1.23    rpaulo 		return (error);
    810   1.1    itojun 	}
    811   1.1    itojun 
    812  1.23    rpaulo 	/*
    813  1.23    rpaulo 	 * do not use a rejected or black hole route.
    814  1.23    rpaulo 	 * XXX: this check should be done in the L2 output routine.
    815  1.23    rpaulo 	 * However, if we skipped this check here, we'd see the following
    816  1.23    rpaulo 	 * scenario:
    817  1.23    rpaulo 	 * - install a rejected route for a scoped address prefix
    818  1.23    rpaulo 	 *   (like fe80::/10)
    819  1.23    rpaulo 	 * - send a packet to a destination that matches the scoped prefix,
    820  1.23    rpaulo 	 *   with ambiguity about the scope zone.
    821  1.23    rpaulo 	 * - pick the outgoing interface from the route, and disambiguate the
    822  1.23    rpaulo 	 *   scope zone with the interface.
    823  1.23    rpaulo 	 * - ip6_output() would try to get another route with the "new"
    824  1.23    rpaulo 	 *   destination, which may be valid.
    825  1.23    rpaulo 	 * - we'd see no error on output.
    826  1.23    rpaulo 	 * Although this may not be very harmful, it should still be confusing.
    827  1.23    rpaulo 	 * We thus reject the case here.
    828  1.23    rpaulo 	 */
    829  1.23    rpaulo 	if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE)))
    830  1.23    rpaulo 		return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    831  1.23    rpaulo 
    832  1.23    rpaulo 	/*
    833  1.23    rpaulo 	 * Adjust the "outgoing" interface.  If we're going to loop the packet
    834  1.23    rpaulo 	 * back to ourselves, the ifp would be the loopback interface.
    835  1.23    rpaulo 	 * However, we'd rather know the interface associated to the
    836  1.23    rpaulo 	 * destination address (which should probably be one of our own
    837  1.23    rpaulo 	 * addresses.)
    838  1.23    rpaulo 	 */
    839  1.23    rpaulo 	if (rt && rt->rt_ifa && rt->rt_ifa->ifa_ifp)
    840  1.23    rpaulo 		*retifp = rt->rt_ifa->ifa_ifp;
    841  1.23    rpaulo 
    842  1.15    itojun 	return (0);
    843   1.1    itojun }
    844   1.1    itojun 
    845  1.23    rpaulo int
    846  1.23    rpaulo in6_selectroute(dstsock, opts, mopts, ro, retifp, retrt, clone)
    847  1.23    rpaulo 	struct sockaddr_in6 *dstsock;
    848  1.23    rpaulo 	struct ip6_pktopts *opts;
    849  1.23    rpaulo 	struct ip6_moptions *mopts;
    850  1.23    rpaulo 	struct route_in6 *ro;
    851  1.23    rpaulo 	struct ifnet **retifp;
    852  1.23    rpaulo 	struct rtentry **retrt;
    853  1.23    rpaulo 	int clone;		/* meaningful only for bsdi and freebsd. */
    854  1.23    rpaulo {
    855  1.23    rpaulo 	return (selectroute(dstsock, opts, mopts, ro, retifp,
    856  1.23    rpaulo 	    retrt, clone, 0));
    857  1.23    rpaulo }
    858  1.23    rpaulo 
    859   1.1    itojun /*
    860   1.1    itojun  * Default hop limit selection. The precedence is as follows:
    861   1.1    itojun  * 1. Hoplimit value specified via ioctl.
    862   1.1    itojun  * 2. (If the outgoing interface is detected) the current
    863   1.1    itojun  *     hop limit of the interface specified by router advertisement.
    864   1.1    itojun  * 3. The system default hoplimit.
    865   1.1    itojun */
    866   1.1    itojun int
    867   1.1    itojun in6_selecthlim(in6p, ifp)
    868   1.1    itojun 	struct in6pcb *in6p;
    869   1.1    itojun 	struct ifnet *ifp;
    870   1.1    itojun {
    871   1.1    itojun 	if (in6p && in6p->in6p_hops >= 0)
    872  1.15    itojun 		return (in6p->in6p_hops);
    873   1.1    itojun 	else if (ifp)
    874  1.15    itojun 		return (ND_IFINFO(ifp)->chlim);
    875   1.1    itojun 	else
    876  1.15    itojun 		return (ip6_defhlim);
    877   1.1    itojun }
    878   1.1    itojun 
    879   1.1    itojun /*
    880   1.1    itojun  * Find an empty port and set it to the specified PCB.
    881   1.1    itojun  */
    882   1.1    itojun int
    883  1.14    itojun in6_pcbsetport(laddr, in6p, p)
    884   1.1    itojun 	struct in6_addr *laddr;
    885   1.1    itojun 	struct in6pcb *in6p;
    886  1.14    itojun 	struct proc *p;
    887   1.1    itojun {
    888   1.1    itojun 	struct socket *so = in6p->in6p_socket;
    889  1.17    itojun 	struct inpcbtable *table = in6p->in6p_table;
    890  1.17    itojun 	int cnt;
    891  1.23    rpaulo 	u_int16_t minport, maxport;
    892  1.17    itojun 	u_int16_t lport, *lastport;
    893   1.1    itojun 	int wild = 0;
    894   1.1    itojun 	void *t;
    895  1.17    itojun 
    896   1.1    itojun 	/* XXX: this is redundant when called from in6_pcbbind */
    897   1.1    itojun 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    898   1.1    itojun 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    899   1.1    itojun 	    (so->so_options & SO_ACCEPTCONN) == 0))
    900  1.17    itojun 		wild = 1;
    901   1.1    itojun 
    902   1.1    itojun 	if (in6p->in6p_flags & IN6P_LOWPORT) {
    903   1.3    itojun #ifndef IPNOPRIVPORTS
    904  1.26      elad 		if (p == 0 || (kauth_authorize_generic(p->p_cred,
    905  1.26      elad 					KAUTH_GENERIC_ISSUSER, &p->p_acflag) != 0))
    906   1.1    itojun 			return (EACCES);
    907   1.3    itojun #endif
    908  1.23    rpaulo 		minport = ip6_lowportmin;
    909  1.23    rpaulo 		maxport = ip6_lowportmax;
    910  1.17    itojun 		lastport = &table->inpt_lastlow;
    911   1.1    itojun 	} else {
    912  1.23    rpaulo 		minport = ip6_anonportmin;
    913  1.23    rpaulo 		maxport = ip6_anonportmax;
    914  1.17    itojun 		lastport = &table->inpt_lastport;
    915   1.1    itojun 	}
    916  1.23    rpaulo 
    917  1.23    rpaulo 	if (minport > maxport) {	/* sanity check */
    918  1.17    itojun 		u_int16_t swp;
    919  1.23    rpaulo 
    920  1.23    rpaulo 		swp = minport;
    921  1.23    rpaulo 		minport = maxport;
    922  1.23    rpaulo 		maxport = swp;
    923  1.17    itojun 	}
    924  1.17    itojun 
    925  1.17    itojun 	lport = *lastport - 1;
    926  1.23    rpaulo 	for (cnt = maxport - minport + 1; cnt; cnt--, lport--) {
    927  1.23    rpaulo 		if (lport < minport || lport > maxport)
    928  1.23    rpaulo 			lport = maxport;
    929  1.17    itojun #ifdef INET
    930   1.1    itojun 		if (IN6_IS_ADDR_V4MAPPED(laddr)) {
    931  1.17    itojun 			t = in_pcblookup_port(table,
    932  1.23    rpaulo 			    *(struct in_addr *)&laddr->s6_addr32[3],
    933  1.17    itojun 			    lport, wild);
    934  1.17    itojun 		} else
    935   1.1    itojun #endif
    936  1.17    itojun 		{
    937  1.17    itojun 			t = in6_pcblookup_port(table, laddr, lport, wild);
    938   1.1    itojun 		}
    939   1.1    itojun 		if (t == 0)
    940  1.17    itojun 			goto found;
    941   1.1    itojun 	}
    942   1.1    itojun 
    943  1.17    itojun 	return (EAGAIN);
    944  1.17    itojun 
    945  1.17    itojun found:
    946  1.17    itojun 	in6p->in6p_flags |= IN6P_ANONPORT;
    947  1.17    itojun 	*lastport = lport;
    948  1.17    itojun 	in6p->in6p_lport = htons(lport);
    949  1.17    itojun 	in6_pcbstate(in6p, IN6P_BOUND);
    950  1.15    itojun 	return (0);		/* success */
    951   1.2    itojun }
    952   1.2    itojun 
    953  1.23    rpaulo void
    954  1.23    rpaulo addrsel_policy_init()
    955  1.23    rpaulo {
    956  1.23    rpaulo 	init_policy_queue();
    957  1.23    rpaulo 
    958  1.23    rpaulo 	/* initialize the "last resort" policy */
    959  1.23    rpaulo 	bzero(&defaultaddrpolicy, sizeof(defaultaddrpolicy));
    960  1.23    rpaulo 	defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
    961  1.23    rpaulo }
    962  1.23    rpaulo 
    963  1.23    rpaulo static struct in6_addrpolicy *
    964  1.23    rpaulo lookup_addrsel_policy(key)
    965  1.23    rpaulo 	struct sockaddr_in6 *key;
    966  1.23    rpaulo {
    967  1.23    rpaulo 	struct in6_addrpolicy *match = NULL;
    968  1.23    rpaulo 
    969  1.23    rpaulo 	match = match_addrsel_policy(key);
    970  1.23    rpaulo 
    971  1.23    rpaulo 	if (match == NULL)
    972  1.23    rpaulo 		match = &defaultaddrpolicy;
    973  1.23    rpaulo 	else
    974  1.23    rpaulo 		match->use++;
    975  1.23    rpaulo 
    976  1.23    rpaulo 	return (match);
    977  1.23    rpaulo }
    978  1.23    rpaulo 
    979   1.2    itojun /*
    980  1.23    rpaulo  * Subroutines to manage the address selection policy table via sysctl.
    981   1.2    itojun  */
    982  1.23    rpaulo struct walkarg {
    983  1.23    rpaulo 	size_t	w_total;
    984  1.23    rpaulo 	size_t	w_given;
    985  1.23    rpaulo 	caddr_t	w_where;
    986  1.23    rpaulo 	caddr_t w_limit;
    987  1.23    rpaulo };
    988  1.23    rpaulo 
    989  1.23    rpaulo int
    990  1.23    rpaulo in6_src_sysctl(oldp, oldlenp, newp, newlen)
    991  1.23    rpaulo 	void *oldp;
    992  1.23    rpaulo 	size_t *oldlenp;
    993  1.23    rpaulo 	void *newp;
    994  1.23    rpaulo 	size_t newlen;
    995  1.23    rpaulo {
    996  1.23    rpaulo 	int error = 0;
    997  1.23    rpaulo 	int s;
    998  1.23    rpaulo 
    999  1.23    rpaulo 	s = splsoftnet();
   1000  1.23    rpaulo 
   1001  1.23    rpaulo 	if (newp) {
   1002  1.23    rpaulo 		error = EPERM;
   1003  1.23    rpaulo 		goto end;
   1004  1.23    rpaulo 	}
   1005  1.23    rpaulo 	if (oldp && oldlenp == NULL) {
   1006  1.23    rpaulo 		error = EINVAL;
   1007  1.23    rpaulo 		goto end;
   1008  1.23    rpaulo 	}
   1009  1.23    rpaulo 	if (oldp || oldlenp) {
   1010  1.23    rpaulo 		struct walkarg w;
   1011  1.24  christos 		size_t oldlen = *oldlenp;
   1012  1.23    rpaulo 
   1013  1.23    rpaulo 		bzero(&w, sizeof(w));
   1014  1.23    rpaulo 		w.w_given = oldlen;
   1015  1.23    rpaulo 		w.w_where = oldp;
   1016  1.23    rpaulo 		if (oldp)
   1017  1.23    rpaulo 			w.w_limit = (caddr_t)oldp + oldlen;
   1018  1.23    rpaulo 
   1019  1.23    rpaulo 		error = walk_addrsel_policy(dump_addrsel_policyent, &w);
   1020  1.23    rpaulo 
   1021  1.23    rpaulo 		*oldlenp = w.w_total;
   1022  1.23    rpaulo 		if (oldp && w.w_total > oldlen && error == 0)
   1023  1.23    rpaulo 			error = ENOMEM;
   1024  1.23    rpaulo 	}
   1025  1.23    rpaulo 
   1026  1.23    rpaulo   end:
   1027  1.23    rpaulo 	splx(s);
   1028  1.23    rpaulo 
   1029  1.23    rpaulo 	return (error);
   1030  1.23    rpaulo }
   1031  1.23    rpaulo 
   1032   1.2    itojun int
   1033  1.23    rpaulo in6_src_ioctl(cmd, data)
   1034  1.23    rpaulo 	u_long cmd;
   1035  1.23    rpaulo 	caddr_t data;
   1036   1.2    itojun {
   1037  1.23    rpaulo 	int i;
   1038  1.23    rpaulo 	struct in6_addrpolicy ent0;
   1039   1.2    itojun 
   1040  1.23    rpaulo 	if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
   1041  1.23    rpaulo 		return (EOPNOTSUPP); /* check for safety */
   1042   1.2    itojun 
   1043  1.23    rpaulo 	ent0 = *(struct in6_addrpolicy *)data;
   1044   1.2    itojun 
   1045  1.23    rpaulo 	if (ent0.label == ADDR_LABEL_NOTAPP)
   1046  1.23    rpaulo 		return (EINVAL);
   1047  1.23    rpaulo 	/* check if the prefix mask is consecutive. */
   1048  1.23    rpaulo 	if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
   1049  1.23    rpaulo 		return (EINVAL);
   1050  1.23    rpaulo 	/* clear trailing garbages (if any) of the prefix address. */
   1051  1.23    rpaulo 	for (i = 0; i < 4; i++) {
   1052  1.23    rpaulo 		ent0.addr.sin6_addr.s6_addr32[i] &=
   1053  1.23    rpaulo 			ent0.addrmask.sin6_addr.s6_addr32[i];
   1054  1.23    rpaulo 	}
   1055  1.23    rpaulo 	ent0.use = 0;
   1056   1.2    itojun 
   1057  1.23    rpaulo 	switch (cmd) {
   1058  1.23    rpaulo 	case SIOCAADDRCTL_POLICY:
   1059  1.23    rpaulo 		return (add_addrsel_policyent(&ent0));
   1060  1.23    rpaulo 	case SIOCDADDRCTL_POLICY:
   1061  1.23    rpaulo 		return (delete_addrsel_policyent(&ent0));
   1062   1.2    itojun 	}
   1063   1.2    itojun 
   1064  1.23    rpaulo 	return (0);		/* XXX: compromise compilers */
   1065   1.2    itojun }
   1066   1.2    itojun 
   1067   1.2    itojun /*
   1068  1.23    rpaulo  * The followings are implementation of the policy table using a
   1069  1.23    rpaulo  * simple tail queue.
   1070  1.23    rpaulo  * XXX such details should be hidden.
   1071  1.23    rpaulo  * XXX implementation using binary tree should be more efficient.
   1072   1.2    itojun  */
   1073  1.23    rpaulo struct addrsel_policyent {
   1074  1.23    rpaulo 	TAILQ_ENTRY(addrsel_policyent) ape_entry;
   1075  1.23    rpaulo 	struct in6_addrpolicy ape_policy;
   1076  1.23    rpaulo };
   1077  1.23    rpaulo 
   1078  1.23    rpaulo TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
   1079  1.23    rpaulo 
   1080  1.23    rpaulo struct addrsel_policyhead addrsel_policytab;
   1081  1.23    rpaulo 
   1082  1.23    rpaulo static void
   1083  1.23    rpaulo init_policy_queue()
   1084  1.23    rpaulo {
   1085  1.23    rpaulo 	TAILQ_INIT(&addrsel_policytab);
   1086  1.23    rpaulo }
   1087  1.23    rpaulo 
   1088  1.23    rpaulo static int
   1089  1.23    rpaulo add_addrsel_policyent(newpolicy)
   1090  1.23    rpaulo 	struct in6_addrpolicy *newpolicy;
   1091  1.23    rpaulo {
   1092  1.23    rpaulo 	struct addrsel_policyent *new, *pol;
   1093  1.23    rpaulo 
   1094  1.23    rpaulo 	/* duplication check */
   1095  1.23    rpaulo 	for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
   1096  1.23    rpaulo 	     pol = TAILQ_NEXT(pol, ape_entry)) {
   1097  1.23    rpaulo 		if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
   1098  1.23    rpaulo 		    &pol->ape_policy.addr.sin6_addr) &&
   1099  1.23    rpaulo 		    IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
   1100  1.23    rpaulo 		    &pol->ape_policy.addrmask.sin6_addr)) {
   1101  1.23    rpaulo 			return (EEXIST);	/* or override it? */
   1102  1.23    rpaulo 		}
   1103  1.23    rpaulo 	}
   1104  1.23    rpaulo 
   1105  1.23    rpaulo 	MALLOC(new, struct addrsel_policyent *, sizeof(*new), M_IFADDR,
   1106  1.23    rpaulo 	       M_WAITOK);
   1107  1.23    rpaulo 	bzero(new, sizeof(*new));
   1108  1.23    rpaulo 
   1109  1.23    rpaulo 	/* XXX: should validate entry */
   1110  1.23    rpaulo 	new->ape_policy = *newpolicy;
   1111  1.23    rpaulo 
   1112  1.23    rpaulo 	TAILQ_INSERT_TAIL(&addrsel_policytab, new, ape_entry);
   1113  1.23    rpaulo 
   1114  1.23    rpaulo 	return (0);
   1115  1.23    rpaulo }
   1116  1.23    rpaulo 
   1117  1.23    rpaulo static int
   1118  1.23    rpaulo delete_addrsel_policyent(key)
   1119  1.23    rpaulo 	struct in6_addrpolicy *key;
   1120   1.2    itojun {
   1121  1.23    rpaulo 	struct addrsel_policyent *pol;
   1122  1.23    rpaulo 
   1123  1.23    rpaulo 	/* search for the entry in the table */
   1124  1.23    rpaulo 	for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
   1125  1.23    rpaulo 	     pol = TAILQ_NEXT(pol, ape_entry)) {
   1126  1.23    rpaulo 		if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
   1127  1.23    rpaulo 		    &pol->ape_policy.addr.sin6_addr) &&
   1128  1.23    rpaulo 		    IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
   1129  1.23    rpaulo 		    &pol->ape_policy.addrmask.sin6_addr)) {
   1130  1.23    rpaulo 			break;
   1131  1.23    rpaulo 		}
   1132  1.23    rpaulo 	}
   1133  1.23    rpaulo 	if (pol == NULL) {
   1134  1.23    rpaulo 		return (ESRCH);
   1135  1.23    rpaulo 	}
   1136   1.2    itojun 
   1137  1.23    rpaulo 	TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
   1138   1.2    itojun 
   1139  1.23    rpaulo 	return (0);
   1140  1.23    rpaulo }
   1141  1.23    rpaulo 
   1142  1.23    rpaulo static int
   1143  1.23    rpaulo walk_addrsel_policy(callback, w)
   1144  1.23    rpaulo 	int (*callback) __P((struct in6_addrpolicy *, void *));
   1145  1.23    rpaulo 	void *w;
   1146  1.23    rpaulo {
   1147  1.23    rpaulo 	struct addrsel_policyent *pol;
   1148  1.23    rpaulo 	int error = 0;
   1149   1.2    itojun 
   1150  1.23    rpaulo 	for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
   1151  1.23    rpaulo 	     pol = TAILQ_NEXT(pol, ape_entry)) {
   1152  1.23    rpaulo 		if ((error = (*callback)(&pol->ape_policy, w)) != 0) {
   1153  1.23    rpaulo 			return (error);
   1154   1.2    itojun 		}
   1155   1.2    itojun 	}
   1156   1.2    itojun 
   1157  1.23    rpaulo 	return (error);
   1158   1.5    itojun }
   1159   1.5    itojun 
   1160  1.23    rpaulo static int
   1161  1.23    rpaulo dump_addrsel_policyent(pol, arg)
   1162  1.23    rpaulo 	struct in6_addrpolicy *pol;
   1163  1.23    rpaulo 	void *arg;
   1164  1.23    rpaulo {
   1165  1.23    rpaulo 	int error = 0;
   1166  1.23    rpaulo 	struct walkarg *w = arg;
   1167  1.23    rpaulo 
   1168  1.23    rpaulo 	if (w->w_where && w->w_where + sizeof(*pol) <= w->w_limit) {
   1169  1.23    rpaulo 		if ((error = copyout(pol, w->w_where, sizeof(*pol))) != 0)
   1170  1.23    rpaulo 			return (error);
   1171  1.23    rpaulo 		w->w_where += sizeof(*pol);
   1172  1.23    rpaulo 	}
   1173  1.23    rpaulo 	w->w_total += sizeof(*pol);
   1174  1.23    rpaulo 
   1175  1.23    rpaulo 	return (error);
   1176  1.23    rpaulo }
   1177  1.23    rpaulo 
   1178  1.23    rpaulo static struct in6_addrpolicy *
   1179  1.23    rpaulo match_addrsel_policy(key)
   1180  1.23    rpaulo 	struct sockaddr_in6 *key;
   1181   1.5    itojun {
   1182  1.23    rpaulo 	struct addrsel_policyent *pent;
   1183  1.23    rpaulo 	struct in6_addrpolicy *bestpol = NULL, *pol;
   1184  1.23    rpaulo 	int matchlen, bestmatchlen = -1;
   1185  1.23    rpaulo 	u_char *mp, *ep, *k, *p, m;
   1186  1.23    rpaulo 
   1187  1.23    rpaulo 	for (pent = TAILQ_FIRST(&addrsel_policytab); pent;
   1188  1.23    rpaulo 	     pent = TAILQ_NEXT(pent, ape_entry)) {
   1189  1.23    rpaulo 		matchlen = 0;
   1190  1.23    rpaulo 
   1191  1.23    rpaulo 		pol = &pent->ape_policy;
   1192  1.23    rpaulo 		mp = (u_char *)&pol->addrmask.sin6_addr;
   1193  1.23    rpaulo 		ep = mp + 16;	/* XXX: scope field? */
   1194  1.23    rpaulo 		k = (u_char *)&key->sin6_addr;
   1195  1.23    rpaulo 		p = (u_char *)&pol->addr.sin6_addr;
   1196  1.23    rpaulo 		for (; mp < ep && *mp; mp++, k++, p++) {
   1197  1.23    rpaulo 			m = *mp;
   1198  1.23    rpaulo 			if ((*k & m) != *p)
   1199  1.23    rpaulo 				goto next; /* not match */
   1200  1.23    rpaulo 			if (m == 0xff) /* short cut for a typical case */
   1201  1.23    rpaulo 				matchlen += 8;
   1202  1.23    rpaulo 			else {
   1203  1.23    rpaulo 				while (m >= 0x80) {
   1204  1.23    rpaulo 					matchlen++;
   1205  1.23    rpaulo 					m <<= 1;
   1206  1.23    rpaulo 				}
   1207  1.23    rpaulo 			}
   1208  1.23    rpaulo 		}
   1209  1.23    rpaulo 
   1210  1.23    rpaulo 		/* matched.  check if this is better than the current best. */
   1211  1.23    rpaulo 		if (bestpol == NULL ||
   1212  1.23    rpaulo 		    matchlen > bestmatchlen) {
   1213  1.23    rpaulo 			bestpol = pol;
   1214  1.23    rpaulo 			bestmatchlen = matchlen;
   1215  1.23    rpaulo 		}
   1216  1.23    rpaulo 
   1217  1.23    rpaulo 	  next:
   1218  1.23    rpaulo 		continue;
   1219  1.23    rpaulo 	}
   1220  1.23    rpaulo 
   1221  1.23    rpaulo 	return (bestpol);
   1222   1.1    itojun }
   1223