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