xref: /freebsd/sys/netinet6/in6_src.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	$KAME: in6_src.c,v 1.132 2003/08/26 04:42:27 keiichi Exp $
32  */
33 
34 /*-
35  * Copyright (c) 1982, 1986, 1991, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 #include "opt_inet.h"
65 #include "opt_inet6.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/priv.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/sysctl.h>
77 #include <sys/errno.h>
78 #include <sys/time.h>
79 #include <sys/jail.h>
80 #include <sys/kernel.h>
81 #include <sys/rmlock.h>
82 #include <sys/sx.h>
83 
84 #include <net/if.h>
85 #include <net/if_var.h>
86 #include <net/if_dl.h>
87 #include <net/if_private.h>
88 #include <net/route.h>
89 #include <net/route/nhop.h>
90 #include <net/if_llatbl.h>
91 
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/udp.h>
99 #include <netinet/udp_var.h>
100 
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet6/in6_fib.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet6/ip6_var.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/nd6.h>
108 
109 static struct mtx addrsel_lock;
110 #define	ADDRSEL_LOCK_INIT()	mtx_init(&addrsel_lock, "addrsel_lock", NULL, MTX_DEF)
111 #define	ADDRSEL_LOCK()		mtx_lock(&addrsel_lock)
112 #define	ADDRSEL_UNLOCK()	mtx_unlock(&addrsel_lock)
113 #define	ADDRSEL_LOCK_ASSERT()	mtx_assert(&addrsel_lock, MA_OWNED)
114 
115 static struct sx addrsel_sxlock;
116 #define	ADDRSEL_SXLOCK_INIT()	sx_init(&addrsel_sxlock, "addrsel_sxlock")
117 #define	ADDRSEL_SLOCK()		sx_slock(&addrsel_sxlock)
118 #define	ADDRSEL_SUNLOCK()	sx_sunlock(&addrsel_sxlock)
119 #define	ADDRSEL_XLOCK()		sx_xlock(&addrsel_sxlock)
120 #define	ADDRSEL_XUNLOCK()	sx_xunlock(&addrsel_sxlock)
121 
122 #define ADDR_LABEL_NOTAPP (-1)
123 VNET_DEFINE_STATIC(struct in6_addrpolicy, defaultaddrpolicy);
124 #define	V_defaultaddrpolicy		VNET(defaultaddrpolicy)
125 
126 VNET_DEFINE(int, ip6_prefer_tempaddr) = 0;
127 
128 static int selectroute(struct sockaddr_in6 *, struct ip6_pktopts *,
129 	struct ip6_moptions *, struct route_in6 *, struct ifnet **,
130 	struct nhop_object **, int, u_int, uint32_t);
131 static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
132 	struct ip6_moptions *, struct ifnet **,
133 	struct ifnet *, u_int);
134 static int in6_selectsrc(uint32_t, struct sockaddr_in6 *,
135 	struct ip6_pktopts *, struct inpcb *, struct ucred *,
136 	struct ifnet **, struct in6_addr *);
137 
138 static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *);
139 
140 static void init_policy_queue(void);
141 static int add_addrsel_policyent(struct in6_addrpolicy *);
142 static int delete_addrsel_policyent(struct in6_addrpolicy *);
143 static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
144 	void *);
145 static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
146 static struct in6_addrpolicy *match_addrsel_policy(struct sockaddr_in6 *);
147 
148 /*
149  * Return an IPv6 address, which is the most appropriate for a given
150  * destination and user specified options.
151  * If necessary, this function lookups the routing table and returns
152  * an entry to the caller for later use.
153  */
154 #define REPLACE(r) do {\
155 	IP6STAT_INC2(ip6s_sources_rule, (r)); \
156 	/* { \
157 	char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \
158 	printf("in6_selectsrc: replace %s with %s by %d\n", ia_best ? ip6_sprintf(ip6buf, &ia_best->ia_addr.sin6_addr) : "none", ip6_sprintf(ip6b, &ia->ia_addr.sin6_addr), (r)); \
159 	} */ \
160 	goto replace; \
161 } while(0)
162 #define NEXT(r) do {\
163 	/* { \
164 	char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \
165 	printf("in6_selectsrc: keep %s against %s by %d\n", ia_best ? ip6_sprintf(ip6buf, &ia_best->ia_addr.sin6_addr) : "none", ip6_sprintf(ip6b, &ia->ia_addr.sin6_addr), (r)); \
166 	} */ \
167 	goto next;		/* XXX: we can't use 'continue' here */ \
168 } while(0)
169 #define BREAK(r) do { \
170 	IP6STAT_INC2(ip6s_sources_rule, (r)); \
171 	goto out;		/* XXX: we can't use 'break' here */ \
172 } while(0)
173 
174 static int
175 in6_selectsrc(uint32_t fibnum, struct sockaddr_in6 *dstsock,
176     struct ip6_pktopts *opts, struct inpcb *inp, struct ucred *cred,
177     struct ifnet **ifpp, struct in6_addr *srcp)
178 {
179 	struct rm_priotracker in6_ifa_tracker;
180 	struct in6_addr dst, tmp;
181 	struct ifnet *ifp = NULL, *oifp = NULL;
182 	struct in6_ifaddr *ia = NULL, *ia_best = NULL;
183 	struct in6_pktinfo *pi = NULL;
184 	int dst_scope = -1, best_scope = -1, best_matchlen = -1;
185 	struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL;
186 	u_int32_t odstzone;
187 	int prefer_tempaddr;
188 	int error;
189 	struct ip6_moptions *mopts;
190 
191 	NET_EPOCH_ASSERT();
192 	KASSERT(srcp != NULL, ("%s: srcp is NULL", __func__));
193 
194 	dst = dstsock->sin6_addr; /* make a copy for local operation */
195 	if (ifpp) {
196 		/*
197 		 * Save a possibly passed in ifp for in6_selectsrc. Only
198 		 * neighbor discovery code should use this feature, where
199 		 * we may know the interface but not the FIB number holding
200 		 * the connected subnet in case someone deleted it from the
201 		 * default FIB and we need to check the interface.
202 		 */
203 		if (*ifpp != NULL)
204 			oifp = *ifpp;
205 		*ifpp = NULL;
206 	}
207 
208 	if (inp != NULL) {
209 		INP_LOCK_ASSERT(inp);
210 		mopts = inp->in6p_moptions;
211 	} else {
212 		mopts = NULL;
213 	}
214 
215 	/*
216 	 * If the source address is explicitly specified by the caller,
217 	 * check if the requested source address is indeed a unicast address
218 	 * assigned to the node, and can be used as the packet's source
219 	 * address.  If everything is okay, use the address as source.
220 	 */
221 	if (opts && (pi = opts->ip6po_pktinfo) &&
222 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
223 		/* get the outgoing interface */
224 		if ((error = in6_selectif(dstsock, opts, mopts, &ifp, oifp,
225 		    fibnum))
226 		    != 0)
227 			return (error);
228 
229 		/*
230 		 * determine the appropriate zone id of the source based on
231 		 * the zone of the destination and the outgoing interface.
232 		 * If the specified address is ambiguous wrt the scope zone,
233 		 * the interface must be specified; otherwise, ifa_ifwithaddr()
234 		 * will fail matching the address.
235 		 */
236 		tmp = pi->ipi6_addr;
237 		if (ifp) {
238 			error = in6_setscope(&tmp, ifp, &odstzone);
239 			if (error)
240 				return (error);
241 		}
242 		if (cred != NULL && (error = prison_local_ip6(cred,
243 		    &tmp, (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0)
244 			return (error);
245 
246 		/*
247 		 * If IPV6_BINDANY socket option is set, we allow to specify
248 		 * non local addresses as source address in IPV6_PKTINFO
249 		 * ancillary data.
250 		 */
251 		if ((inp->inp_flags & INP_BINDANY) == 0) {
252 			ia = in6ifa_ifwithaddr(&tmp, 0 /* XXX */, false);
253 			if (ia == NULL || (ia->ia6_flags & (IN6_IFF_ANYCAST |
254 			    IN6_IFF_NOTREADY)))
255 				return (EADDRNOTAVAIL);
256 			bcopy(&ia->ia_addr.sin6_addr, srcp, sizeof(*srcp));
257 		} else
258 			bcopy(&tmp, srcp, sizeof(*srcp));
259 		pi->ipi6_addr = tmp; /* XXX: this overrides pi */
260 		if (ifpp)
261 			*ifpp = ifp;
262 		return (0);
263 	}
264 
265 	/*
266 	 * Otherwise, if the socket has already bound the source, just use it.
267 	 */
268 	if (inp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
269 		if (cred != NULL &&
270 		    (error = prison_local_ip6(cred, &inp->in6p_laddr,
271 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
272 			return (error);
273 		bcopy(&inp->in6p_laddr, srcp, sizeof(*srcp));
274 		return (0);
275 	}
276 
277 	/*
278 	 * Bypass source address selection and use the primary jail IP
279 	 * if requested.
280 	 */
281 	if (cred != NULL && !prison_saddrsel_ip6(cred, srcp))
282 		return (0);
283 
284 	/*
285 	 * If the address is not specified, choose the best one based on
286 	 * the outgoing interface and the destination address.
287 	 */
288 	/* get the outgoing interface */
289 	if ((error = in6_selectif(dstsock, opts, mopts, &ifp, oifp,
290 	    (inp != NULL) ? inp->inp_inc.inc_fibnum : fibnum)) != 0)
291 		return (error);
292 
293 #ifdef DIAGNOSTIC
294 	if (ifp == NULL)	/* this should not happen */
295 		panic("in6_selectsrc: NULL ifp");
296 #endif
297 	error = in6_setscope(&dst, ifp, &odstzone);
298 	if (error)
299 		return (error);
300 
301 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
302 	CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
303 		int new_scope = -1, new_matchlen = -1;
304 		struct in6_addrpolicy *new_policy = NULL;
305 		u_int32_t srczone, osrczone, dstzone;
306 		struct in6_addr src;
307 		struct ifnet *ifp1 = ia->ia_ifp;
308 
309 		/*
310 		 * We'll never take an address that breaks the scope zone
311 		 * of the destination.  We also skip an address if its zone
312 		 * does not contain the outgoing interface.
313 		 * XXX: we should probably use sin6_scope_id here.
314 		 */
315 		if (in6_setscope(&dst, ifp1, &dstzone) ||
316 		    odstzone != dstzone) {
317 			continue;
318 		}
319 		src = ia->ia_addr.sin6_addr;
320 		if (in6_setscope(&src, ifp, &osrczone) ||
321 		    in6_setscope(&src, ifp1, &srczone) ||
322 		    osrczone != srczone) {
323 			continue;
324 		}
325 
326 		/* avoid unusable addresses */
327 		if ((ia->ia6_flags &
328 		     (IN6_IFF_NOTREADY | IN6_IFF_ANYCAST | IN6_IFF_DETACHED))) {
329 				continue;
330 		}
331 		if (!V_ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
332 			continue;
333 
334 		/* If jailed only take addresses of the jail into account. */
335 		if (cred != NULL &&
336 		    prison_check_ip6(cred, &ia->ia_addr.sin6_addr) != 0)
337 			continue;
338 
339 		/* Rule 1: Prefer same address */
340 		if (IN6_ARE_ADDR_EQUAL(&dst, &ia->ia_addr.sin6_addr)) {
341 			ia_best = ia;
342 			BREAK(1); /* there should be no better candidate */
343 		}
344 
345 		if (ia_best == NULL)
346 			REPLACE(0);
347 
348 		/* Rule 2: Prefer appropriate scope */
349 		if (dst_scope < 0)
350 			dst_scope = in6_addrscope(&dst);
351 		new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
352 		if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
353 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
354 				REPLACE(2);
355 			NEXT(2);
356 		} else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
357 			if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
358 				NEXT(2);
359 			REPLACE(2);
360 		}
361 
362 		/*
363 		 * Rule 3: Avoid deprecated addresses.  Note that the case of
364 		 * !ip6_use_deprecated is already rejected above.
365 		 */
366 		if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
367 			NEXT(3);
368 		if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
369 			REPLACE(3);
370 
371 		/* Rule 4: Prefer home addresses */
372 		/*
373 		 * XXX: This is a TODO.  We should probably merge the MIP6
374 		 * case above.
375 		 */
376 
377 		/* Rule 5: Prefer outgoing interface */
378 		if (!(ND_IFINFO(ifp)->flags & ND6_IFF_NO_PREFER_IFACE)) {
379 			if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
380 				NEXT(5);
381 			if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
382 				REPLACE(5);
383 		}
384 
385 		/*
386 		 * Rule 6: Prefer matching label
387 		 * Note that best_policy should be non-NULL here.
388 		 */
389 		if (dst_policy == NULL)
390 			dst_policy = lookup_addrsel_policy(dstsock);
391 		if (dst_policy->label != ADDR_LABEL_NOTAPP) {
392 			new_policy = lookup_addrsel_policy(&ia->ia_addr);
393 			if (dst_policy->label == best_policy->label &&
394 			    dst_policy->label != new_policy->label)
395 				NEXT(6);
396 			if (dst_policy->label != best_policy->label &&
397 			    dst_policy->label == new_policy->label)
398 				REPLACE(6);
399 		}
400 
401 		/*
402 		 * Rule 7: Prefer public addresses.
403 		 * We allow users to reverse the logic by configuring
404 		 * a sysctl variable, so that privacy conscious users can
405 		 * always prefer temporary addresses.
406 		 */
407 		if (opts == NULL ||
408 		    opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
409 			prefer_tempaddr = V_ip6_prefer_tempaddr;
410 		} else if (opts->ip6po_prefer_tempaddr ==
411 		    IP6PO_TEMPADDR_NOTPREFER) {
412 			prefer_tempaddr = 0;
413 		} else
414 			prefer_tempaddr = 1;
415 		if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
416 		    (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
417 			if (prefer_tempaddr)
418 				REPLACE(7);
419 			else
420 				NEXT(7);
421 		}
422 		if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
423 		    !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
424 			if (prefer_tempaddr)
425 				NEXT(7);
426 			else
427 				REPLACE(7);
428 		}
429 
430 		/*
431 		 * Rule 8: prefer addresses on alive interfaces.
432 		 * This is a KAME specific rule.
433 		 */
434 		if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
435 		    !(ia->ia_ifp->if_flags & IFF_UP))
436 			NEXT(8);
437 		if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
438 		    (ia->ia_ifp->if_flags & IFF_UP))
439 			REPLACE(8);
440 
441 		/*
442 		 * Rule 9: prefer address with better virtual status.
443 		 */
444 		if (ifa_preferred(&ia_best->ia_ifa, &ia->ia_ifa))
445 			REPLACE(9);
446 		if (ifa_preferred(&ia->ia_ifa, &ia_best->ia_ifa))
447 			NEXT(9);
448 
449 		/*
450 		 * Rule 10: prefer address with `prefer_source' flag.
451 		 */
452 		if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0 &&
453 		    (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0)
454 			REPLACE(10);
455 		if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0 &&
456 		    (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0)
457 			NEXT(10);
458 
459 		/*
460 		 * Rule 14: Use longest matching prefix.
461 		 * Note: in the address selection draft, this rule is
462 		 * documented as "Rule 8".  However, since it is also
463 		 * documented that this rule can be overridden, we assign
464 		 * a large number so that it is easy to assign smaller numbers
465 		 * to more preferred rules.
466 		 */
467 		new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, &dst);
468 		if (best_matchlen < new_matchlen)
469 			REPLACE(14);
470 		if (new_matchlen < best_matchlen)
471 			NEXT(14);
472 
473 		/* Rule 15 is reserved. */
474 
475 		/*
476 		 * Last resort: just keep the current candidate.
477 		 * Or, do we need more rules?
478 		 */
479 		continue;
480 
481 	  replace:
482 		ia_best = ia;
483 		best_scope = (new_scope >= 0 ? new_scope :
484 			      in6_addrscope(&ia_best->ia_addr.sin6_addr));
485 		best_policy = (new_policy ? new_policy :
486 			       lookup_addrsel_policy(&ia_best->ia_addr));
487 		best_matchlen = (new_matchlen >= 0 ? new_matchlen :
488 				 in6_matchlen(&ia_best->ia_addr.sin6_addr,
489 					      &dst));
490 
491 	  next:
492 		continue;
493 
494 	  out:
495 		break;
496 	}
497 
498 	if ((ia = ia_best) == NULL) {
499 		IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
500 		IP6STAT_INC(ip6s_sources_none);
501 		return (EADDRNOTAVAIL);
502 	}
503 
504 	/*
505 	 * At this point at least one of the addresses belonged to the jail
506 	 * but it could still be, that we want to further restrict it, e.g.
507 	 * theoratically IN6_IS_ADDR_LOOPBACK.
508 	 * It must not be IN6_IS_ADDR_UNSPECIFIED anymore.
509 	 * prison_local_ip6() will fix an IN6_IS_ADDR_LOOPBACK but should
510 	 * let all others previously selected pass.
511 	 * Use tmp to not change ::1 on lo0 to the primary jail address.
512 	 */
513 	tmp = ia->ia_addr.sin6_addr;
514 	if (cred != NULL && prison_local_ip6(cred, &tmp, (inp != NULL &&
515 	    (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0) {
516 		IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
517 		IP6STAT_INC(ip6s_sources_none);
518 		return (EADDRNOTAVAIL);
519 	}
520 
521 	if (ifpp)
522 		*ifpp = ifp;
523 
524 	bcopy(&tmp, srcp, sizeof(*srcp));
525 	if (ia->ia_ifp == ifp)
526 		IP6STAT_INC2(ip6s_sources_sameif, best_scope);
527 	else
528 		IP6STAT_INC2(ip6s_sources_otherif, best_scope);
529 	if (dst_scope == best_scope)
530 		IP6STAT_INC2(ip6s_sources_samescope, best_scope);
531 	else
532 		IP6STAT_INC2(ip6s_sources_otherscope, best_scope);
533 	if (IFA6_IS_DEPRECATED(ia))
534 		IP6STAT_INC2(ip6s_sources_deprecated, best_scope);
535 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
536 	return (0);
537 }
538 
539 /*
540  * Select source address based on @inp, @dstsock and @opts.
541  * Stores selected address to @srcp. If @scope_ambiguous is set,
542  * embed scope from selected outgoing interface. If @hlim pointer
543  * is provided, stores calculated hop limit there.
544  * Returns 0 on success.
545  */
546 int
547 in6_selectsrc_socket(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
548     struct inpcb *inp, struct ucred *cred, int scope_ambiguous,
549     struct in6_addr *srcp, int *hlim)
550 {
551 	struct ifnet *retifp;
552 	uint32_t fibnum;
553 	int error;
554 
555 	fibnum = inp->inp_inc.inc_fibnum;
556 	retifp = NULL;
557 
558 	error = in6_selectsrc(fibnum, dstsock, opts, inp, cred, &retifp, srcp);
559 	if (error != 0)
560 		return (error);
561 
562 	if (hlim != NULL)
563 		*hlim = in6_selecthlim(inp, retifp);
564 
565 	if (retifp == NULL || scope_ambiguous == 0)
566 		return (0);
567 
568 	/*
569 	 * Application should provide a proper zone ID or the use of
570 	 * default zone IDs should be enabled.  Unfortunately, some
571 	 * applications do not behave as it should, so we need a
572 	 * workaround.  Even if an appropriate ID is not determined
573 	 * (when it's required), if we can determine the outgoing
574 	 * interface. determine the zone ID based on the interface.
575 	 */
576 	error = in6_setscope(&dstsock->sin6_addr, retifp, NULL);
577 
578 	return (error);
579 }
580 
581 /*
582  * Select source address based on @fibnum, @dst and @scopeid.
583  * Stores selected address to @srcp.
584  * Returns 0 on success.
585  *
586  * Used by non-socket based consumers (ND code mostly)
587  */
588 int
589 in6_selectsrc_addr(uint32_t fibnum, const struct in6_addr *dst,
590     uint32_t scopeid, struct ifnet *ifp, struct in6_addr *srcp,
591     int *hlim)
592 {
593 	struct ifnet *retifp;
594 	struct sockaddr_in6 dst_sa;
595 	int error;
596 
597 	retifp = ifp;
598 	bzero(&dst_sa, sizeof(dst_sa));
599 	dst_sa.sin6_family = AF_INET6;
600 	dst_sa.sin6_len = sizeof(dst_sa);
601 	dst_sa.sin6_addr = *dst;
602 	dst_sa.sin6_scope_id = scopeid;
603 	sa6_embedscope(&dst_sa, 0);
604 
605 	error = in6_selectsrc(fibnum, &dst_sa, NULL, NULL, NULL, &retifp, srcp);
606 	if (hlim != NULL)
607 		*hlim = in6_selecthlim(NULL, retifp);
608 
609 	return (error);
610 }
611 
612 static struct nhop_object *
613 cache_route(uint32_t fibnum, const struct sockaddr_in6 *dst, struct route_in6 *ro,
614     uint32_t flowid)
615 {
616 	/*
617 	 * Use a cached route if it exists and is valid, else try to allocate
618 	 * a new one. Note that we should check the address family of the
619 	 * cached destination, in case of sharing the cache with IPv4.
620 	 * Assumes that 'struct route_in6' is exclusively locked.
621 	 */
622 	if (ro->ro_nh != NULL && (
623 	    !NH_IS_VALID(ro->ro_nh) || ro->ro_dst.sin6_family != AF_INET6 ||
624 	    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &dst->sin6_addr)))
625 		RO_NHFREE(ro);
626 
627 	if (ro->ro_nh == NULL) {
628 		ro->ro_dst = *dst;
629 
630 		const struct in6_addr *paddr;
631 		struct in6_addr unscoped_addr;
632 		uint32_t scopeid = 0;
633 		if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr)) {
634 			in6_splitscope(&dst->sin6_addr, &unscoped_addr, &scopeid);
635 			paddr = &unscoped_addr;
636 		} else
637 			paddr = &dst->sin6_addr;
638 		ro->ro_nh = fib6_lookup(fibnum, paddr, scopeid, NHR_REF, flowid);
639 	}
640 	return (ro->ro_nh);
641 }
642 
643 static struct nhop_object *
644 lookup_route(uint32_t fibnum, struct sockaddr_in6 *dst, struct route_in6 *ro,
645     struct ip6_pktopts *opts, uint32_t flowid)
646 {
647 	struct nhop_object *nh = NULL;
648 
649 	/*
650 	 * If the next hop address for the packet is specified by the caller,
651 	 * use it as the gateway.
652 	 */
653 	if (opts && opts->ip6po_nexthop) {
654 		struct route_in6 *ron = &opts->ip6po_nextroute;
655 		struct sockaddr_in6 *sin6_next = satosin6(opts->ip6po_nexthop);
656 
657 		nh = cache_route(fibnum, sin6_next, ron, flowid);
658 
659 		/*
660 		 * The node identified by that address must be a
661 		 * neighbor of the sending host.
662 		 */
663 		if (nh != NULL && (nh->nh_flags & NHF_GATEWAY) != 0)
664 			nh = NULL;
665 	} else if (ro != NULL) {
666 		nh = cache_route(fibnum, dst, ro, flowid);
667 		if (nh == NULL)
668 			return (NULL);
669 
670 		/*
671 		 * Check if the outgoing interface conflicts with
672 		 * the interface specified by ipi6_ifindex (if specified).
673 		 */
674 		struct in6_pktinfo *pi;
675 		if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
676 			if (nh->nh_aifp->if_index != pi->ipi6_ifindex)
677 				nh = NULL;
678 		}
679 	}
680 
681 	return (nh);
682 }
683 
684 /*
685  * Finds outgoing nexthop or the outgoing interface for the
686  * @dstsock.
687  * Return 0 on success and stores the lookup result in @retnh and @retifp
688  */
689 static int
690 selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
691     struct ip6_moptions *mopts, struct route_in6 *ro,
692     struct ifnet **retifp, struct nhop_object **retnh, int norouteok,
693     u_int fibnum, uint32_t flowid)
694 {
695 	int error = 0;
696 	struct ifnet *ifp = NULL;
697 	struct in6_pktinfo *pi = NULL;
698 	struct in6_addr *dst = &dstsock->sin6_addr;
699 
700 	/* If the caller specify the outgoing interface explicitly, use it. */
701 	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
702 		/* XXX boundary check is assumed to be already done. */
703 		ifp = ifnet_byindex(pi->ipi6_ifindex);
704 		if (ifp != NULL && (norouteok || IN6_IS_ADDR_MULTICAST(dst))) {
705 			/*
706 			 * we do not have to check or get the route for
707 			 * multicast.
708 			 */
709 			goto done;
710 		} else
711 			goto getroute;
712 	}
713 	/*
714 	 * If the destination address is a multicast address and the outgoing
715 	 * interface for the address is specified by the caller, use it.
716 	 */
717 	if (IN6_IS_ADDR_MULTICAST(dst) &&
718 	    mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) {
719 		goto done; /* we do not need a route for multicast. */
720 	}
721 	/*
722 	 * If destination address is LLA or link- or node-local multicast,
723 	 * use it's embedded scope zone id to determine outgoing interface.
724 	 */
725 	if (IN6_IS_ADDR_MC_LINKLOCAL(dst) ||
726 	    IN6_IS_ADDR_MC_NODELOCAL(dst)) {
727 		uint32_t zoneid = ntohs(in6_getscope(dst));
728 		if (zoneid > 0) {
729 			ifp = in6_getlinkifnet(zoneid);
730 			goto done;
731 		}
732 	}
733 
734   getroute:;
735 	struct nhop_object *nh = lookup_route(fibnum, dstsock, ro, opts, flowid);
736 	if (nh != NULL) {
737 		*retifp = nh->nh_aifp;
738 		error = 0;
739 	} else {
740 		*retifp = NULL;
741 		IP6STAT_INC(ip6s_noroute);
742 		error = EHOSTUNREACH;
743 	}
744 	*retnh = nh;
745 	return (error);
746 
747   done:
748 	if (ifp == NULL) {
749 		/*
750 		 * This can happen if the caller did not pass a cached route
751 		 * nor any other hints.  We treat this case an error.
752 		 */
753 		error = EHOSTUNREACH;
754 	}
755 	if (error == EHOSTUNREACH)
756 		IP6STAT_INC(ip6s_noroute);
757 
758 	*retifp = ifp;
759 	*retnh = NULL;
760 
761 	return (error);
762 }
763 
764 static int
765 in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
766     struct ip6_moptions *mopts, struct ifnet **retifp,
767     struct ifnet *oifp, u_int fibnum)
768 {
769 	int error;
770 	struct route_in6 sro;
771 	struct nhop_object *nh = NULL;
772 	uint16_t nh_flags;
773 
774 	KASSERT(retifp != NULL, ("%s: retifp is NULL", __func__));
775 
776 	bzero(&sro, sizeof(sro));
777 	nh_flags = 0;
778 
779 	error = selectroute(dstsock, opts, mopts, &sro, retifp, &nh, 1, fibnum, 0);
780 
781 	if (nh != NULL)
782 		nh_flags = nh->nh_flags;
783 	if (nh != NULL && nh == sro.ro_nh)
784 		NH_FREE(nh);
785 
786 	if (error != 0) {
787 		/* Help ND. See oifp comment in in6_selectsrc(). */
788 		if (oifp != NULL && fibnum == RT_DEFAULT_FIB) {
789 			*retifp = oifp;
790 			error = 0;
791 		}
792 		return (error);
793 	}
794 
795 	/*
796 	 * do not use a rejected or black hole route.
797 	 * XXX: this check should be done in the L2 output routine.
798 	 * However, if we skipped this check here, we'd see the following
799 	 * scenario:
800 	 * - install a rejected route for a scoped address prefix
801 	 *   (like fe80::/10)
802 	 * - send a packet to a destination that matches the scoped prefix,
803 	 *   with ambiguity about the scope zone.
804 	 * - pick the outgoing interface from the route, and disambiguate the
805 	 *   scope zone with the interface.
806 	 * - ip6_output() would try to get another route with the "new"
807 	 *   destination, which may be valid.
808 	 * - we'd see no error on output.
809 	 * Although this may not be very harmful, it should still be confusing.
810 	 * We thus reject the case here.
811 	 */
812 
813 	if (nh_flags & (NHF_REJECT | NHF_BLACKHOLE)) {
814 		error = (nh_flags & NHF_HOST ? EHOSTUNREACH : ENETUNREACH);
815 		return (error);
816 	}
817 
818 	return (0);
819 }
820 
821 /* Public wrapper function to selectroute(). */
822 int
823 in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
824     struct ip6_moptions *mopts, struct route_in6 *ro,
825     struct ifnet **retifp, struct nhop_object **retnh, u_int fibnum, uint32_t flowid)
826 {
827 	MPASS(retifp != NULL);
828 	MPASS(retnh != NULL);
829 
830 	return (selectroute(dstsock, opts, mopts, ro, retifp,
831 	    retnh, 0, fibnum, flowid));
832 }
833 
834 /*
835  * Default hop limit selection. The precedence is as follows:
836  * 1. Hoplimit value specified via ioctl.
837  * 2. (If the outgoing interface is detected) the current
838  *     hop limit of the interface specified by router advertisement.
839  * 3. The system default hoplimit.
840  */
841 int
842 in6_selecthlim(struct inpcb *inp, struct ifnet *ifp)
843 {
844 
845 	if (inp && inp->in6p_hops >= 0)
846 		return (inp->in6p_hops);
847 	else if (ifp)
848 		return (ND_IFINFO(ifp)->chlim);
849 	else if (inp && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
850 		struct nhop_object *nh;
851 		struct in6_addr dst;
852 		uint32_t fibnum, scopeid;
853 		int hlim;
854 
855 		fibnum = inp->inp_inc.inc_fibnum;
856 		in6_splitscope(&inp->in6p_faddr, &dst, &scopeid);
857 		nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
858 		if (nh != NULL) {
859 			hlim = ND_IFINFO(nh->nh_ifp)->chlim;
860 			return (hlim);
861 		}
862 	}
863 	return (V_ip6_defhlim);
864 }
865 
866 void
867 addrsel_policy_init(void)
868 {
869 
870 	init_policy_queue();
871 
872 	/* initialize the "last resort" policy */
873 	bzero(&V_defaultaddrpolicy, sizeof(V_defaultaddrpolicy));
874 	V_defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
875 
876 	if (!IS_DEFAULT_VNET(curvnet))
877 		return;
878 
879 	ADDRSEL_LOCK_INIT();
880 	ADDRSEL_SXLOCK_INIT();
881 }
882 
883 static struct in6_addrpolicy *
884 lookup_addrsel_policy(struct sockaddr_in6 *key)
885 {
886 	struct in6_addrpolicy *match = NULL;
887 
888 	ADDRSEL_LOCK();
889 	match = match_addrsel_policy(key);
890 
891 	if (match == NULL)
892 		match = &V_defaultaddrpolicy;
893 	else
894 		match->use++;
895 	ADDRSEL_UNLOCK();
896 
897 	return (match);
898 }
899 
900 /*
901  * Subroutines to manage the address selection policy table via sysctl.
902  */
903 struct walkarg {
904 	struct sysctl_req *w_req;
905 };
906 
907 static int in6_src_sysctl(SYSCTL_HANDLER_ARGS);
908 SYSCTL_DECL(_net_inet6_ip6);
909 static SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy,
910     CTLFLAG_RD | CTLFLAG_MPSAFE, in6_src_sysctl,
911     "");
912 
913 static int
914 in6_src_sysctl(SYSCTL_HANDLER_ARGS)
915 {
916 	struct walkarg w;
917 
918 	if (req->newptr)
919 		return EPERM;
920 
921 	bzero(&w, sizeof(w));
922 	w.w_req = req;
923 
924 	return (walk_addrsel_policy(dump_addrsel_policyent, &w));
925 }
926 
927 int
928 in6_src_ioctl(u_long cmd, caddr_t data)
929 {
930 	struct in6_addrpolicy ent0;
931 
932 	if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
933 		return (EOPNOTSUPP); /* check for safety */
934 
935 	ent0 = *(struct in6_addrpolicy *)data;
936 
937 	if (ent0.label == ADDR_LABEL_NOTAPP)
938 		return (EINVAL);
939 	/* check if the prefix mask is consecutive. */
940 	if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
941 		return (EINVAL);
942 	/* clear trailing garbages (if any) of the prefix address. */
943 	IN6_MASK_ADDR(&ent0.addr.sin6_addr, &ent0.addrmask.sin6_addr);
944 	ent0.use = 0;
945 
946 	switch (cmd) {
947 	case SIOCAADDRCTL_POLICY:
948 		return (add_addrsel_policyent(&ent0));
949 	case SIOCDADDRCTL_POLICY:
950 		return (delete_addrsel_policyent(&ent0));
951 	}
952 
953 	return (0);		/* XXX: compromise compilers */
954 }
955 
956 /*
957  * The followings are implementation of the policy table using a
958  * simple tail queue.
959  * XXX such details should be hidden.
960  * XXX implementation using binary tree should be more efficient.
961  */
962 struct addrsel_policyent {
963 	TAILQ_ENTRY(addrsel_policyent) ape_entry;
964 	struct in6_addrpolicy ape_policy;
965 };
966 
967 TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
968 
969 VNET_DEFINE_STATIC(struct addrsel_policyhead, addrsel_policytab);
970 #define	V_addrsel_policytab		VNET(addrsel_policytab)
971 
972 static void
973 init_policy_queue(void)
974 {
975 
976 	TAILQ_INIT(&V_addrsel_policytab);
977 }
978 
979 static int
980 add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
981 {
982 	struct addrsel_policyent *new, *pol;
983 
984 	new = malloc(sizeof(*new), M_IFADDR,
985 	       M_WAITOK);
986 	ADDRSEL_XLOCK();
987 	ADDRSEL_LOCK();
988 
989 	/* duplication check */
990 	TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
991 		if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
992 				       &pol->ape_policy.addr.sin6_addr) &&
993 		    IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
994 				       &pol->ape_policy.addrmask.sin6_addr)) {
995 			ADDRSEL_UNLOCK();
996 			ADDRSEL_XUNLOCK();
997 			free(new, M_IFADDR);
998 			return (EEXIST);	/* or override it? */
999 		}
1000 	}
1001 
1002 	bzero(new, sizeof(*new));
1003 
1004 	/* XXX: should validate entry */
1005 	new->ape_policy = *newpolicy;
1006 
1007 	TAILQ_INSERT_TAIL(&V_addrsel_policytab, new, ape_entry);
1008 	ADDRSEL_UNLOCK();
1009 	ADDRSEL_XUNLOCK();
1010 
1011 	return (0);
1012 }
1013 
1014 static int
1015 delete_addrsel_policyent(struct in6_addrpolicy *key)
1016 {
1017 	struct addrsel_policyent *pol;
1018 
1019 	ADDRSEL_XLOCK();
1020 	ADDRSEL_LOCK();
1021 
1022 	/* search for the entry in the table */
1023 	TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
1024 		if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
1025 		    &pol->ape_policy.addr.sin6_addr) &&
1026 		    IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
1027 		    &pol->ape_policy.addrmask.sin6_addr)) {
1028 			break;
1029 		}
1030 	}
1031 	if (pol == NULL) {
1032 		ADDRSEL_UNLOCK();
1033 		ADDRSEL_XUNLOCK();
1034 		return (ESRCH);
1035 	}
1036 
1037 	TAILQ_REMOVE(&V_addrsel_policytab, pol, ape_entry);
1038 	ADDRSEL_UNLOCK();
1039 	ADDRSEL_XUNLOCK();
1040 	free(pol, M_IFADDR);
1041 
1042 	return (0);
1043 }
1044 
1045 static int
1046 walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), void *w)
1047 {
1048 	struct addrsel_policyent *pol;
1049 	int error = 0;
1050 
1051 	ADDRSEL_SLOCK();
1052 	TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
1053 		if ((error = (*callback)(&pol->ape_policy, w)) != 0) {
1054 			ADDRSEL_SUNLOCK();
1055 			return (error);
1056 		}
1057 	}
1058 	ADDRSEL_SUNLOCK();
1059 	return (error);
1060 }
1061 
1062 static int
1063 dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
1064 {
1065 	int error = 0;
1066 	struct walkarg *w = arg;
1067 
1068 	error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol));
1069 
1070 	return (error);
1071 }
1072 
1073 static struct in6_addrpolicy *
1074 match_addrsel_policy(struct sockaddr_in6 *key)
1075 {
1076 	struct addrsel_policyent *pent;
1077 	struct in6_addrpolicy *bestpol = NULL, *pol;
1078 	int matchlen, bestmatchlen = -1;
1079 	u_char *mp, *ep, *k, *p, m;
1080 
1081 	TAILQ_FOREACH(pent, &V_addrsel_policytab, ape_entry) {
1082 		matchlen = 0;
1083 
1084 		pol = &pent->ape_policy;
1085 		mp = (u_char *)&pol->addrmask.sin6_addr;
1086 		ep = mp + 16;	/* XXX: scope field? */
1087 		k = (u_char *)&key->sin6_addr;
1088 		p = (u_char *)&pol->addr.sin6_addr;
1089 		for (; mp < ep && *mp; mp++, k++, p++) {
1090 			m = *mp;
1091 			if ((*k & m) != *p)
1092 				goto next; /* not match */
1093 			if (m == 0xff) /* short cut for a typical case */
1094 				matchlen += 8;
1095 			else {
1096 				while (m >= 0x80) {
1097 					matchlen++;
1098 					m <<= 1;
1099 				}
1100 			}
1101 		}
1102 
1103 		/* matched.  check if this is better than the current best. */
1104 		if (bestpol == NULL ||
1105 		    matchlen > bestmatchlen) {
1106 			bestpol = pol;
1107 			bestmatchlen = matchlen;
1108 		}
1109 
1110 	  next:
1111 		continue;
1112 	}
1113 
1114 	return (bestpol);
1115 }
1116