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