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