xref: /netbsd-src/sys/netinet6/in6_src.c (revision 7dec41f044f3905aa6d3ea2553b711107f0a6cd6)
1*7dec41f0Sozaki-r /*	$NetBSD: in6_src.c,v 1.92 2023/08/03 04:24:55 ozaki-r Exp $	*/
278678b13Srpaulo /*	$KAME: in6_src.c,v 1.159 2005/10/19 01:40:32 t-momose Exp $	*/
39d853e8aSitojun 
49d853e8aSitojun /*
59d853e8aSitojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
69d853e8aSitojun  * All rights reserved.
79d853e8aSitojun  *
89d853e8aSitojun  * Redistribution and use in source and binary forms, with or without
99d853e8aSitojun  * modification, are permitted provided that the following conditions
109d853e8aSitojun  * are met:
119d853e8aSitojun  * 1. Redistributions of source code must retain the above copyright
129d853e8aSitojun  *    notice, this list of conditions and the following disclaimer.
139d853e8aSitojun  * 2. Redistributions in binary form must reproduce the above copyright
149d853e8aSitojun  *    notice, this list of conditions and the following disclaimer in the
159d853e8aSitojun  *    documentation and/or other materials provided with the distribution.
169d853e8aSitojun  * 3. Neither the name of the project nor the names of its contributors
179d853e8aSitojun  *    may be used to endorse or promote products derived from this software
189d853e8aSitojun  *    without specific prior written permission.
199d853e8aSitojun  *
209d853e8aSitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
219d853e8aSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229d853e8aSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239d853e8aSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
249d853e8aSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259d853e8aSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269d853e8aSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279d853e8aSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289d853e8aSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299d853e8aSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309d853e8aSitojun  * SUCH DAMAGE.
319d853e8aSitojun  */
329d853e8aSitojun 
339d853e8aSitojun /*
349d853e8aSitojun  * Copyright (c) 1982, 1986, 1991, 1993
359d853e8aSitojun  *	The Regents of the University of California.  All rights reserved.
369d853e8aSitojun  *
379d853e8aSitojun  * Redistribution and use in source and binary forms, with or without
389d853e8aSitojun  * modification, are permitted provided that the following conditions
399d853e8aSitojun  * are met:
409d853e8aSitojun  * 1. Redistributions of source code must retain the above copyright
419d853e8aSitojun  *    notice, this list of conditions and the following disclaimer.
429d853e8aSitojun  * 2. Redistributions in binary form must reproduce the above copyright
439d853e8aSitojun  *    notice, this list of conditions and the following disclaimer in the
449d853e8aSitojun  *    documentation and/or other materials provided with the distribution.
4578678b13Srpaulo  * 3. All advertising materials mentioning features or use of this software
4678678b13Srpaulo  *    must display the following acknowledgement:
4778678b13Srpaulo  *	This product includes software developed by the University of
4878678b13Srpaulo  *	California, Berkeley and its contributors.
4978678b13Srpaulo  * 4. Neither the name of the University nor the names of its contributors
509d853e8aSitojun  *    may be used to endorse or promote products derived from this software
519d853e8aSitojun  *    without specific prior written permission.
529d853e8aSitojun  *
539d853e8aSitojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
549d853e8aSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
559d853e8aSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
569d853e8aSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
579d853e8aSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
589d853e8aSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
599d853e8aSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
609d853e8aSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
619d853e8aSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
629d853e8aSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
639d853e8aSitojun  * SUCH DAMAGE.
649d853e8aSitojun  *
659d853e8aSitojun  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
669d853e8aSitojun  */
679d853e8aSitojun 
684f2ad952Slukem #include <sys/cdefs.h>
69*7dec41f0Sozaki-r __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.92 2023/08/03 04:24:55 ozaki-r Exp $");
704f2ad952Slukem 
711c4a50f1Spooka #ifdef _KERNEL_OPT
729d853e8aSitojun #include "opt_inet.h"
731c4a50f1Spooka #endif
749d853e8aSitojun 
759d853e8aSitojun #include <sys/param.h>
769d853e8aSitojun #include <sys/systm.h>
779d853e8aSitojun #include <sys/malloc.h>
789d853e8aSitojun #include <sys/mbuf.h>
799d853e8aSitojun #include <sys/protosw.h>
809d853e8aSitojun #include <sys/socket.h>
819d853e8aSitojun #include <sys/socketvar.h>
829d853e8aSitojun #include <sys/ioctl.h>
839d853e8aSitojun #include <sys/errno.h>
849d853e8aSitojun #include <sys/time.h>
8578678b13Srpaulo #include <sys/kernel.h>
869d853e8aSitojun #include <sys/proc.h>
87874fef37Selad #include <sys/kauth.h>
889d853e8aSitojun 
899d853e8aSitojun #include <net/if.h>
9078678b13Srpaulo #include <net/if_types.h>
919d853e8aSitojun #include <net/route.h>
929d853e8aSitojun 
939d853e8aSitojun #include <netinet/in.h>
949d853e8aSitojun #include <netinet/in_var.h>
959d853e8aSitojun #include <netinet/in_systm.h>
969d853e8aSitojun #include <netinet/ip.h>
979d853e8aSitojun #include <netinet/in_pcb.h>
9884f52095Schristos #include <netinet/portalgo.h>
999d853e8aSitojun #include <netinet6/in6_var.h>
1009d853e8aSitojun #include <netinet/ip6.h>
1019d853e8aSitojun #include <netinet6/in6_pcb.h>
1029d853e8aSitojun #include <netinet6/ip6_var.h>
1030dd41b37Sthorpej #include <netinet6/ip6_private.h>
1049d853e8aSitojun #include <netinet6/nd6.h>
105ec67eee5Sitojun #include <netinet6/scope6_var.h>
1069d853e8aSitojun 
10778678b13Srpaulo #ifdef MIP6
10878678b13Srpaulo #include <netinet6/mip6.h>
10978678b13Srpaulo #include <netinet6/mip6_var.h>
11078678b13Srpaulo #include "mip.h"
11178678b13Srpaulo #if NMIP > 0
11278678b13Srpaulo #include <net/if_mip.h>
11378678b13Srpaulo #endif /* NMIP > 0 */
11478678b13Srpaulo #endif /* MIP6 */
11578678b13Srpaulo 
116c2e43be1Sdyoung #include <netinet/tcp_vtw.h>
117c2e43be1Sdyoung 
11878678b13Srpaulo #define ADDR_LABEL_NOTAPP (-1)
11978678b13Srpaulo struct in6_addrpolicy defaultaddrpolicy;
12078678b13Srpaulo 
12178678b13Srpaulo int ip6_prefer_tempaddr = 0;
12278678b13Srpaulo 
1235493f188Sdyoung static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
1244badfc20Sozaki-r 	struct ip6_moptions *, struct route *, struct ifnet **, struct psref *);
12578678b13Srpaulo 
1265493f188Sdyoung static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *);
12778678b13Srpaulo 
1285493f188Sdyoung static void init_policy_queue(void);
1295493f188Sdyoung static int add_addrsel_policyent(struct in6_addrpolicy *);
1305493f188Sdyoung static int delete_addrsel_policyent(struct in6_addrpolicy *);
1315493f188Sdyoung static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
1325493f188Sdyoung 				    void *);
1335493f188Sdyoung static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
1345493f188Sdyoung static struct in6_addrpolicy *match_addrsel_policy(struct sockaddr_in6 *);
13578678b13Srpaulo 
136195d1af8Sroy #define	IFA6_IS_VALIDATED(ia) \
137195d1af8Sroy 	(((ia)->ia6_flags & (IN6_IFF_TENTATIVE | IN6_IFF_DETACHED)) == 0)
138195d1af8Sroy 
1399d853e8aSitojun /*
140574214f1Sitojun  * Return an IPv6 address, which is the most appropriate for a given
1419d853e8aSitojun  * destination and user specified options.
142574214f1Sitojun  * If necessary, this function lookups the routing table and returns
1439d853e8aSitojun  * an entry to the caller for later use.
1449d853e8aSitojun  */
14535561f6bSchristos #if 0				/* disabled ad-hoc */
14678678b13Srpaulo #define REPLACE(r) do {\
14735561f6bSchristos 	char _buf1[INET6_ADDRSTRLEN], _buf2[INET6_ADDRSTRLEN]; \
14878678b13Srpaulo 	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
14978678b13Srpaulo 		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
15078678b13Srpaulo 		ip6stat.ip6s_sources_rule[(r)]++; \
15135561f6bSchristos 	printf("%s: replace %s with %s by %d\n", __func__, ia_best ? \
15235561f6bSchristos 	    IN6_PRINT(_buf1, &ia_best->ia_addr.sin6_addr) : "none", \
15335561f6bSchristos 	    IN6_PRINT(_buf2, &ia->ia_addr.sin6_addr), (r)); \
15478678b13Srpaulo 	goto replace; \
15535561f6bSchristos } while(/*CONSTCOND*/0)
15678678b13Srpaulo #define NEXT(r) do {\
15778678b13Srpaulo 	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
15878678b13Srpaulo 		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
15978678b13Srpaulo 		ip6stat.ip6s_sources_rule[(r)]++; \
16035561f6bSchristos 	printf("%s: keep %s against %s by %d\n", ia_best ? \
16135561f6bSchristos 	    IN6_PRINT(_buf1, &ia_best->ia_addr.sin6_addr) : "none", \
16235561f6bSchristos 	    IN6_PRINT(_buf2, &ia->ia_addr.sin6_addr), (r)); \
16378678b13Srpaulo 	goto next; 		/* XXX: we can't use 'continue' here */ \
16435561f6bSchristos } while(/*CONSTCOND*/0)
16578678b13Srpaulo #define BREAK(r) do { \
16678678b13Srpaulo 	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
16778678b13Srpaulo 		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
16878678b13Srpaulo 		ip6stat.ip6s_sources_rule[(r)]++; \
16978678b13Srpaulo 	goto out; 		/* XXX: we can't use 'break' here */ \
17035561f6bSchristos } while(/*CONSTCOND*/0)
17178678b13Srpaulo #else
17278678b13Srpaulo #define REPLACE(r) goto replace
17378678b13Srpaulo #define NEXT(r) goto next
17478678b13Srpaulo #define BREAK(r) goto out
17578678b13Srpaulo #endif
17678678b13Srpaulo 
177c5224ffdSozaki-r /*
178c5224ffdSozaki-r  * Called inside pserialize critical section. Don't sleep/block.
179c5224ffdSozaki-r  */
180c5224ffdSozaki-r static struct in6_ifaddr *
in6_select_best_ia(struct sockaddr_in6 * dstsock,struct in6_addr * dst,const struct ifnet * ifp,const struct ip6_pktopts * opts,const u_int32_t odstzone)181c5224ffdSozaki-r in6_select_best_ia(struct sockaddr_in6 *dstsock, struct in6_addr *dst,
182c5224ffdSozaki-r     const struct ifnet *ifp, const struct ip6_pktopts *opts,
183c5224ffdSozaki-r     const u_int32_t odstzone)
1849d853e8aSitojun {
185c5224ffdSozaki-r 	struct in6_ifaddr *ia, *ia_best = NULL;
18678678b13Srpaulo 	int dst_scope = -1, best_scope = -1, best_matchlen = -1;
18778678b13Srpaulo 	struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL;
1884badfc20Sozaki-r 
1896cf9fce7Sozaki-r 	IN6_ADDRLIST_READER_FOREACH(ia) {
19078678b13Srpaulo 		int new_scope = -1, new_matchlen = -1;
19178678b13Srpaulo 		struct in6_addrpolicy *new_policy = NULL;
19278678b13Srpaulo 		u_int32_t srczone, osrczone, dstzone;
19378678b13Srpaulo 		struct in6_addr src;
19478678b13Srpaulo 		struct ifnet *ifp1 = ia->ia_ifp;
195c5224ffdSozaki-r 		int prefer_tempaddr;
19678678b13Srpaulo 
19778678b13Srpaulo 		/*
19878678b13Srpaulo 		 * We'll never take an address that breaks the scope zone
19978678b13Srpaulo 		 * of the destination.  We also skip an address if its zone
20078678b13Srpaulo 		 * does not contain the outgoing interface.
20178678b13Srpaulo 		 * XXX: we should probably use sin6_scope_id here.
20278678b13Srpaulo 		 */
203c5224ffdSozaki-r 		if (in6_setscope(dst, ifp1, &dstzone) ||
20478678b13Srpaulo 		    odstzone != dstzone) {
20578678b13Srpaulo 			continue;
20678678b13Srpaulo 		}
20778678b13Srpaulo 		src = ia->ia_addr.sin6_addr;
208548324cbSchristos 
209548324cbSchristos 		/* Skip the scope test in impossible cases */
210548324cbSchristos 		if (!(ifp->if_flags & IFF_LOOPBACK) &&
211548324cbSchristos 		    IN6_IS_ADDR_LOOPBACK(&src))
212548324cbSchristos 			continue;
213548324cbSchristos 
21478678b13Srpaulo 		if (in6_setscope(&src, ifp, &osrczone) ||
21578678b13Srpaulo 		    in6_setscope(&src, ifp1, &srczone) ||
21678678b13Srpaulo 		    osrczone != srczone) {
21778678b13Srpaulo 			continue;
21878678b13Srpaulo 		}
21978678b13Srpaulo 
22078678b13Srpaulo 		/* avoid unusable addresses */
221fbb6c0f8Sroy 		if ((ia->ia6_flags & (IN6_IFF_DUPLICATED | IN6_IFF_ANYCAST)))
22278678b13Srpaulo 			continue;
22378678b13Srpaulo 		if (!ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
22478678b13Srpaulo 			continue;
22578678b13Srpaulo 
22678678b13Srpaulo #if defined(MIP6) && NMIP > 0
22778678b13Srpaulo 		/* avoid unusable home addresses. */
22878678b13Srpaulo 		if ((ia->ia6_flags & IN6_IFF_HOME) &&
22978678b13Srpaulo 		    !mip6_ifa6_is_addr_valid_hoa(ia))
23078678b13Srpaulo 			continue;
23178678b13Srpaulo #endif /* MIP6 && NMIP > 0 */
23278678b13Srpaulo 
23378678b13Srpaulo 		/* Rule 1: Prefer same address */
234c5224ffdSozaki-r 		if (IN6_ARE_ADDR_EQUAL(dst, &ia->ia_addr.sin6_addr)) {
23578678b13Srpaulo 			ia_best = ia;
23678678b13Srpaulo 			BREAK(1); /* there should be no better candidate */
23778678b13Srpaulo 		}
23878678b13Srpaulo 
23978678b13Srpaulo 		if (ia_best == NULL)
240fbb6c0f8Sroy 			REPLACE(1);
24178678b13Srpaulo 
24278678b13Srpaulo 		/* Rule 2: Prefer appropriate scope */
24378678b13Srpaulo 		if (dst_scope < 0)
244c5224ffdSozaki-r 			dst_scope = in6_addrscope(dst);
24578678b13Srpaulo 		new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
24678678b13Srpaulo 		if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
24778678b13Srpaulo 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
24878678b13Srpaulo 				REPLACE(2);
24978678b13Srpaulo 			NEXT(2);
25078678b13Srpaulo 		} else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
25178678b13Srpaulo 			if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
25278678b13Srpaulo 				NEXT(2);
25378678b13Srpaulo 			REPLACE(2);
25478678b13Srpaulo 		}
25578678b13Srpaulo 
25678678b13Srpaulo 		/*
25778678b13Srpaulo 		 * Rule 3: Avoid deprecated addresses.  Note that the case of
25878678b13Srpaulo 		 * !ip6_use_deprecated is already rejected above.
259195d1af8Sroy 		 * Treat unvalidated addresses as deprecated here.
26078678b13Srpaulo 		 */
261195d1af8Sroy 		if (IFA6_IS_VALIDATED(ia_best) && !IFA6_IS_VALIDATED(ia))
262195d1af8Sroy 			NEXT(3);
263195d1af8Sroy 		if (!IFA6_IS_VALIDATED(ia_best) && IFA6_IS_VALIDATED(ia))
264195d1af8Sroy 			REPLACE(3);
26578678b13Srpaulo 		if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
26678678b13Srpaulo 			NEXT(3);
26778678b13Srpaulo 		if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
26878678b13Srpaulo 			REPLACE(3);
26978678b13Srpaulo 
27078678b13Srpaulo 		/* Rule 4: Prefer home addresses */
27178678b13Srpaulo #if defined(MIP6) && NMIP > 0
27278678b13Srpaulo 		if (!MIP6_IS_MN)
27378678b13Srpaulo 			goto skip_rule4;
27478678b13Srpaulo 
27578678b13Srpaulo 		if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
27678678b13Srpaulo 		    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
27778678b13Srpaulo 			/* both address are not home addresses. */
27878678b13Srpaulo 			goto skip_rule4;
27978678b13Srpaulo 		}
28078678b13Srpaulo 
28178678b13Srpaulo 		/*
28278678b13Srpaulo 		 * If SA is simultaneously a home address and care-of
28378678b13Srpaulo 		 * address and SB is not, then prefer SA. Similarly,
28478678b13Srpaulo 		 * if SB is simultaneously a home address and care-of
28578678b13Srpaulo 		 * address and SA is not, then prefer SB.
28678678b13Srpaulo 		 */
28778678b13Srpaulo 		if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
28878678b13Srpaulo 			ia_best->ia_ifp->if_type != IFT_MIP)
28978678b13Srpaulo 		    &&
29078678b13Srpaulo 		    ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
29178678b13Srpaulo 			ia->ia_ifp->if_type == IFT_MIP))
29278678b13Srpaulo 			NEXT(4);
29378678b13Srpaulo 		if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
29478678b13Srpaulo 			ia_best->ia_ifp->if_type == IFT_MIP)
29578678b13Srpaulo 		    &&
29678678b13Srpaulo 		    ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
29778678b13Srpaulo 			ia->ia_ifp->if_type != IFT_MIP))
29878678b13Srpaulo 			REPLACE(4);
29978678b13Srpaulo 		if (ip6po_usecoa == 0) {
30078678b13Srpaulo 			/*
30178678b13Srpaulo 			 * If SA is just a home address and SB is just
30278678b13Srpaulo 			 * a care-of address, then prefer
30378678b13Srpaulo 			 * SA. Similarly, if SB is just a home address
30478678b13Srpaulo 			 * and SA is just a care-of address, then
30578678b13Srpaulo 			 * prefer SB.
30678678b13Srpaulo 			 */
30778678b13Srpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
30878678b13Srpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
30978678b13Srpaulo 				NEXT(4);
31078678b13Srpaulo 			}
31178678b13Srpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
31278678b13Srpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) != 0) {
31378678b13Srpaulo 				REPLACE(4);
31478678b13Srpaulo 			}
31578678b13Srpaulo 		} else {
31678678b13Srpaulo 			/*
31778678b13Srpaulo 			 * a sender don't want to use a home address
31878678b13Srpaulo 			 * because:
31978678b13Srpaulo 			 *
32078678b13Srpaulo 			 * 1) we cannot use.  (ex. NS or NA to global
32178678b13Srpaulo 			 * addresses.)
32278678b13Srpaulo 			 *
32378678b13Srpaulo 			 * 2) a user specified not to use.
32478678b13Srpaulo 			 * (ex. mip6control -u)
32578678b13Srpaulo 			 */
32678678b13Srpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
32778678b13Srpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) != 0) {
32878678b13Srpaulo 				/* XXX breaks stat */
32978678b13Srpaulo 				NEXT(0);
33078678b13Srpaulo 			}
33178678b13Srpaulo 			if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
33278678b13Srpaulo 			    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
33378678b13Srpaulo 				/* XXX breaks stat */
33478678b13Srpaulo 				REPLACE(0);
33578678b13Srpaulo 			}
33678678b13Srpaulo 		}
33778678b13Srpaulo 	skip_rule4:
33878678b13Srpaulo #endif /* MIP6 && NMIP > 0 */
33978678b13Srpaulo 
34078678b13Srpaulo 		/* Rule 5: Prefer outgoing interface */
34178678b13Srpaulo 		if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
34278678b13Srpaulo 			NEXT(5);
34378678b13Srpaulo 		if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
34478678b13Srpaulo 			REPLACE(5);
34578678b13Srpaulo 
34678678b13Srpaulo 		/*
34778678b13Srpaulo 		 * Rule 6: Prefer matching label
34878678b13Srpaulo 		 * Note that best_policy should be non-NULL here.
34978678b13Srpaulo 		 */
35078678b13Srpaulo 		if (dst_policy == NULL)
35178678b13Srpaulo 			dst_policy = lookup_addrsel_policy(dstsock);
35278678b13Srpaulo 		if (dst_policy->label != ADDR_LABEL_NOTAPP) {
35378678b13Srpaulo 			new_policy = lookup_addrsel_policy(&ia->ia_addr);
35478678b13Srpaulo 			if (dst_policy->label == best_policy->label &&
35578678b13Srpaulo 			    dst_policy->label != new_policy->label)
35678678b13Srpaulo 				NEXT(6);
35778678b13Srpaulo 			if (dst_policy->label != best_policy->label &&
35878678b13Srpaulo 			    dst_policy->label == new_policy->label)
35978678b13Srpaulo 				REPLACE(6);
36078678b13Srpaulo 		}
36178678b13Srpaulo 
36278678b13Srpaulo 		/*
36378678b13Srpaulo 		 * Rule 7: Prefer public addresses.
36478678b13Srpaulo 		 * We allow users to reverse the logic by configuring
36578678b13Srpaulo 		 * a sysctl variable, so that privacy conscious users can
36678678b13Srpaulo 		 * always prefer temporary addresses.
36778678b13Srpaulo 		 */
36878678b13Srpaulo 		if (opts == NULL ||
36978678b13Srpaulo 		    opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
37078678b13Srpaulo 			prefer_tempaddr = ip6_prefer_tempaddr;
37178678b13Srpaulo 		} else if (opts->ip6po_prefer_tempaddr ==
37278678b13Srpaulo 		    IP6PO_TEMPADDR_NOTPREFER) {
37378678b13Srpaulo 			prefer_tempaddr = 0;
37478678b13Srpaulo 		} else
37578678b13Srpaulo 			prefer_tempaddr = 1;
37678678b13Srpaulo 		if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
37778678b13Srpaulo 		    (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
37878678b13Srpaulo 			if (prefer_tempaddr)
37978678b13Srpaulo 				REPLACE(7);
38078678b13Srpaulo 			else
38178678b13Srpaulo 				NEXT(7);
38278678b13Srpaulo 		}
38378678b13Srpaulo 		if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
38478678b13Srpaulo 		    !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
38578678b13Srpaulo 			if (prefer_tempaddr)
38678678b13Srpaulo 				NEXT(7);
38778678b13Srpaulo 			else
38878678b13Srpaulo 				REPLACE(7);
38978678b13Srpaulo 		}
39078678b13Srpaulo 
39178678b13Srpaulo 		/*
39278678b13Srpaulo 		 * Rule 8: prefer addresses on alive interfaces.
39378678b13Srpaulo 		 * This is a KAME specific rule.
39478678b13Srpaulo 		 */
39578678b13Srpaulo 		if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
39678678b13Srpaulo 		    !(ia->ia_ifp->if_flags & IFF_UP))
39778678b13Srpaulo 			NEXT(8);
39878678b13Srpaulo 		if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
39978678b13Srpaulo 		    (ia->ia_ifp->if_flags & IFF_UP))
40078678b13Srpaulo 			REPLACE(8);
40178678b13Srpaulo 
40278678b13Srpaulo 		/*
40378678b13Srpaulo 		 * Rule 9: prefer addresses on "preferred" interfaces.
40478678b13Srpaulo 		 * This is a KAME specific rule.
40578678b13Srpaulo 		 */
40678678b13Srpaulo #ifdef notyet			/* until introducing address selection */
40778678b13Srpaulo #define NDI_BEST ND_IFINFO(ia_best->ia_ifp)
40878678b13Srpaulo #define NDI_NEW  ND_IFINFO(ia->ia_ifp)
40978678b13Srpaulo 		if ((NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
41078678b13Srpaulo 		    !(NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
41178678b13Srpaulo 			NEXT(9);
41278678b13Srpaulo 		if (!(NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
41378678b13Srpaulo 		    (NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
41478678b13Srpaulo 			REPLACE(9);
41578678b13Srpaulo #undef NDI_BEST
41678678b13Srpaulo #undef NDI_NEW
41778678b13Srpaulo #endif
41878678b13Srpaulo 
41978678b13Srpaulo 		/*
42078678b13Srpaulo 		 * Rule 14: Use longest matching prefix.
42178678b13Srpaulo 		 * Note: in the address selection draft, this rule is
42278678b13Srpaulo 		 * documented as "Rule 8".  However, since it is also
42378678b13Srpaulo 		 * documented that this rule can be overridden, we assign
42478678b13Srpaulo 		 * a large number so that it is easy to assign smaller numbers
42578678b13Srpaulo 		 * to more preferred rules.
42678678b13Srpaulo 		 */
427c5224ffdSozaki-r 		new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, dst);
42878678b13Srpaulo 		if (best_matchlen < new_matchlen)
42978678b13Srpaulo 			REPLACE(14);
43078678b13Srpaulo 		if (new_matchlen < best_matchlen)
43178678b13Srpaulo 			NEXT(14);
43278678b13Srpaulo 
43378678b13Srpaulo 		/* Rule 15 is reserved. */
43478678b13Srpaulo 
43578678b13Srpaulo 		/*
43678678b13Srpaulo 		 * Last resort: just keep the current candidate.
43778678b13Srpaulo 		 * Or, do we need more rules?
43878678b13Srpaulo 		 */
43978678b13Srpaulo 		continue;
44078678b13Srpaulo 
44178678b13Srpaulo 	  replace:
44278678b13Srpaulo 		ia_best = ia;
44378678b13Srpaulo 		best_scope = (new_scope >= 0 ? new_scope :
44478678b13Srpaulo 			      in6_addrscope(&ia_best->ia_addr.sin6_addr));
44578678b13Srpaulo 		best_policy = (new_policy ? new_policy :
44678678b13Srpaulo 			       lookup_addrsel_policy(&ia_best->ia_addr));
44778678b13Srpaulo 		best_matchlen = (new_matchlen >= 0 ? new_matchlen :
44878678b13Srpaulo 				 in6_matchlen(&ia_best->ia_addr.sin6_addr,
449c5224ffdSozaki-r 					      dst));
45078678b13Srpaulo 
45178678b13Srpaulo 	  next:
45278678b13Srpaulo 		continue;
45378678b13Srpaulo 
45478678b13Srpaulo 	  out:
45578678b13Srpaulo 		break;
45678678b13Srpaulo 	}
45778678b13Srpaulo 
458c5224ffdSozaki-r 	return ia_best;
459c5224ffdSozaki-r }
460c5224ffdSozaki-r #undef REPLACE
461c5224ffdSozaki-r #undef BREAK
462c5224ffdSozaki-r #undef NEXT
463c5224ffdSozaki-r 
464c5224ffdSozaki-r int
in6_selectsrc(struct sockaddr_in6 * dstsock,struct ip6_pktopts * opts,struct ip6_moptions * mopts,struct route * ro,struct in6_addr * laddr,struct ifnet ** ifpp,struct psref * psref,struct in6_addr * ret_ia6)465c5224ffdSozaki-r in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
466c5224ffdSozaki-r 	struct ip6_moptions *mopts, struct route *ro, struct in6_addr *laddr,
467c5224ffdSozaki-r 	struct ifnet **ifpp, struct psref *psref, struct in6_addr *ret_ia6)
468c5224ffdSozaki-r {
469c5224ffdSozaki-r 	struct in6_addr dst;
470c5224ffdSozaki-r 	struct ifnet *ifp = NULL;
471c5224ffdSozaki-r 	struct in6_ifaddr *ia = NULL;
472c5224ffdSozaki-r 	struct in6_pktinfo *pi = NULL;
473c5224ffdSozaki-r 	u_int32_t odstzone;
474b9baaeebSchristos 	int error = 0, iferror;
475c5224ffdSozaki-r #if defined(MIP6) && NMIP > 0
476c5224ffdSozaki-r 	u_int8_t ip6po_usecoa = 0;
477c5224ffdSozaki-r #endif /* MIP6 && NMIP > 0 */
478c5224ffdSozaki-r 	struct psref local_psref;
479c5224ffdSozaki-r 	int bound = curlwp_bind();
480c5224ffdSozaki-r #define PSREF (psref == NULL) ? &local_psref : psref
481c5224ffdSozaki-r 	int s;
482c5224ffdSozaki-r 
483c5224ffdSozaki-r 	KASSERT((ifpp != NULL && psref != NULL) ||
484c5224ffdSozaki-r 	        (ifpp == NULL && psref == NULL));
485c5224ffdSozaki-r 
486c5224ffdSozaki-r 	dst = dstsock->sin6_addr; /* make a copy for local operation */
487c5224ffdSozaki-r 	if (ifpp)
488c5224ffdSozaki-r 		*ifpp = NULL;
489c5224ffdSozaki-r 
490c5224ffdSozaki-r 	/*
491c5224ffdSozaki-r 	 * Try to determine the outgoing interface for the given destination.
492c5224ffdSozaki-r 	 * We do this regardless of whether the socket is bound, since the
493c5224ffdSozaki-r 	 * caller may need this information as a side effect of the call
494c5224ffdSozaki-r 	 * to this function (e.g., for identifying the appropriate scope zone
495c5224ffdSozaki-r 	 * ID).
496c5224ffdSozaki-r 	 */
497b9baaeebSchristos 	iferror = in6_selectif(dstsock, opts, mopts, ro, &ifp, PSREF);
498c5224ffdSozaki-r 	if (ifpp != NULL)
499c5224ffdSozaki-r 		*ifpp = ifp;
500c5224ffdSozaki-r 
501c5224ffdSozaki-r 	/*
502c5224ffdSozaki-r 	 * If the source address is explicitly specified by the caller,
503c5224ffdSozaki-r 	 * check if the requested source address is indeed a unicast address
504c5224ffdSozaki-r 	 * assigned to the node, and can be used as the packet's source
505c5224ffdSozaki-r 	 * address.  If everything is okay, use the address as source.
506c5224ffdSozaki-r 	 */
507c5224ffdSozaki-r 	if (opts && (pi = opts->ip6po_pktinfo) &&
508c5224ffdSozaki-r 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
509c5224ffdSozaki-r 		struct sockaddr_in6 srcsock;
510c5224ffdSozaki-r 		struct in6_ifaddr *ia6;
511c5224ffdSozaki-r 		int _s;
512c5224ffdSozaki-r 		struct ifaddr *ifa;
513c5224ffdSozaki-r 
514c5224ffdSozaki-r 		/*
515c5224ffdSozaki-r 		 * Determine the appropriate zone id of the source based on
516c5224ffdSozaki-r 		 * the zone of the destination and the outgoing interface.
517c5224ffdSozaki-r 		 * If the specified address is ambiguous wrt the scope zone,
518c5224ffdSozaki-r 		 * the interface must be specified; otherwise, ifa_ifwithaddr()
519c5224ffdSozaki-r 		 * will fail matching the address.
520c5224ffdSozaki-r 		 */
521c5224ffdSozaki-r 		memset(&srcsock, 0, sizeof(srcsock));
522c5224ffdSozaki-r 		srcsock.sin6_family = AF_INET6;
523c5224ffdSozaki-r 		srcsock.sin6_len = sizeof(srcsock);
524c5224ffdSozaki-r 		srcsock.sin6_addr = pi->ipi6_addr;
525c5224ffdSozaki-r 		if (ifp) {
526c5224ffdSozaki-r 			error = in6_setscope(&srcsock.sin6_addr, ifp, NULL);
527c5224ffdSozaki-r 			if (error != 0)
528c5224ffdSozaki-r 				goto exit;
529c5224ffdSozaki-r 		}
530c5224ffdSozaki-r 
531c5224ffdSozaki-r 		_s = pserialize_read_enter();
532c5224ffdSozaki-r 		ifa = ifa_ifwithaddr(sin6tosa(&srcsock));
533c5224ffdSozaki-r 		if ((ia6 = ifatoia6(ifa)) == NULL ||
534c5224ffdSozaki-r 		    ia6->ia6_flags &
535c5224ffdSozaki-r 		    (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)) {
536c5224ffdSozaki-r 			pserialize_read_exit(_s);
537c5224ffdSozaki-r 			error = EADDRNOTAVAIL;
538c5224ffdSozaki-r 			goto exit;
539c5224ffdSozaki-r 		}
540c5224ffdSozaki-r 		pi->ipi6_addr = srcsock.sin6_addr; /* XXX: this overrides pi */
541c5224ffdSozaki-r 		if (ifpp)
542c5224ffdSozaki-r 			*ifpp = ifp;
543c5224ffdSozaki-r 		*ret_ia6 = ia6->ia_addr.sin6_addr;
544c5224ffdSozaki-r 		pserialize_read_exit(_s);
545c5224ffdSozaki-r 		goto exit;
546c5224ffdSozaki-r 	}
547c5224ffdSozaki-r 
548c5224ffdSozaki-r 	/*
549c5224ffdSozaki-r 	 * If the socket has already bound the source, just use it.  We don't
550c5224ffdSozaki-r 	 * care at the moment whether in6_selectif() succeeded above, even
551c5224ffdSozaki-r 	 * though it would eventually cause an error.
552c5224ffdSozaki-r 	 */
553c5224ffdSozaki-r 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
554c5224ffdSozaki-r 		*ret_ia6 = *laddr;
555c5224ffdSozaki-r 		goto exit;
556c5224ffdSozaki-r 	}
557c5224ffdSozaki-r 
558c5224ffdSozaki-r 	/*
559c5224ffdSozaki-r 	 * The outgoing interface is crucial in the general selection procedure
560c5224ffdSozaki-r 	 * below.  If it is not known at this point, we fail.
561c5224ffdSozaki-r 	 */
562b9baaeebSchristos 	if (ifp == NULL) {
563b9baaeebSchristos 		error = iferror;
564c5224ffdSozaki-r 		goto exit;
565b9baaeebSchristos 	}
566c5224ffdSozaki-r 
567c5224ffdSozaki-r 	/*
568c5224ffdSozaki-r 	 * If the address is not yet determined, choose the best one based on
569c5224ffdSozaki-r 	 * the outgoing interface and the destination address.
570c5224ffdSozaki-r 	 */
571c5224ffdSozaki-r 
572c5224ffdSozaki-r #if defined(MIP6) && NMIP > 0
573c5224ffdSozaki-r 	/*
574c5224ffdSozaki-r 	 * a caller can specify IP6PO_USECOA to not to use a home
575c5224ffdSozaki-r 	 * address.  for example, the case that the neighbour
576c5224ffdSozaki-r 	 * unreachability detection to the global address.
577c5224ffdSozaki-r 	 */
578c5224ffdSozaki-r 	if (opts != NULL &&
579c5224ffdSozaki-r 	    (opts->ip6po_flags & IP6PO_USECOA) != 0) {
580c5224ffdSozaki-r 		ip6po_usecoa = 1;
581c5224ffdSozaki-r 	}
582c5224ffdSozaki-r #endif /* MIP6 && NMIP > 0 */
583c5224ffdSozaki-r 
584c5224ffdSozaki-r 	error = in6_setscope(&dst, ifp, &odstzone);
585c5224ffdSozaki-r 	if (error != 0)
586c5224ffdSozaki-r 		goto exit;
587c5224ffdSozaki-r 
588c5224ffdSozaki-r 	s = pserialize_read_enter();
589c5224ffdSozaki-r 
590c5224ffdSozaki-r 	ia = in6_select_best_ia(dstsock, &dst, ifp, opts, odstzone);
591c5224ffdSozaki-r 	if (ia == NULL) {
5920f3a4486Sozaki-r 		pserialize_read_exit(s);
5930f3a4486Sozaki-r 		error = EADDRNOTAVAIL;
5944badfc20Sozaki-r 		goto exit;
5959d853e8aSitojun 	}
5960f3a4486Sozaki-r 	*ret_ia6 = ia->ia_addr.sin6_addr;
597c5224ffdSozaki-r 
5980f3a4486Sozaki-r 	pserialize_read_exit(s);
5994badfc20Sozaki-r exit:
6004badfc20Sozaki-r 	if (ifpp == NULL)
6014badfc20Sozaki-r 		if_put(ifp, PSREF);
6024badfc20Sozaki-r 	curlwp_bindx(bound);
6030f3a4486Sozaki-r 	return error;
6044badfc20Sozaki-r #undef PSREF
6059d853e8aSitojun }
6069d853e8aSitojun 
607d0432711Sozaki-r int
in6_selectroute(struct sockaddr_in6 * dstsock,struct ip6_pktopts * opts,struct route ** ro,struct rtentry ** retrt,bool count_discard)608d0432711Sozaki-r in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
609d0432711Sozaki-r     struct route **ro, struct rtentry **retrt, bool count_discard)
6109d853e8aSitojun {
61178678b13Srpaulo 	int error = 0;
61278678b13Srpaulo 	struct rtentry *rt = NULL;
6136e6136eaSozaki-r 	union {
6146e6136eaSozaki-r 		struct sockaddr		dst;
6158d545fd9Skardel 		struct sockaddr_in	dst4;
6166e6136eaSozaki-r 		struct sockaddr_in6	dst6;
6176e6136eaSozaki-r 	} u;
6184badfc20Sozaki-r 
6196e6136eaSozaki-r 	KASSERT(ro != NULL);
6204c25fb2fSozaki-r 	KASSERT(*ro != NULL);
6216e6136eaSozaki-r 	KASSERT(retrt != NULL);
6229d853e8aSitojun 
62378678b13Srpaulo #if 0
62478678b13Srpaulo 	if (dstsock->sin6_addr.s6_addr32[0] == 0 &&
62578678b13Srpaulo 	    dstsock->sin6_addr.s6_addr32[1] == 0 &&
62678678b13Srpaulo 	    !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) {
62728f4c24cSryo 		char ip6buf[INET6_ADDRSTRLEN];
628548324cbSchristos 		printf("%s: strange destination %s\n", __func__,
62935561f6bSchristos 		       IN6_PRINT(ip6buf, &dstsock->sin6_addr));
63078678b13Srpaulo 	} else {
63128f4c24cSryo 		char ip6buf[INET6_ADDRSTRLEN];
632548324cbSchristos 		printf("%s: destination = %s%%%d\n", __func__,
63335561f6bSchristos 		       IN6_PRINT(ip6buf, &dstsock->sin6_addr),
63478678b13Srpaulo 		       dstsock->sin6_scope_id); /* for debug */
6359d853e8aSitojun 	}
63678678b13Srpaulo #endif
63778678b13Srpaulo 
63878678b13Srpaulo 	/*
63978678b13Srpaulo 	 * If the next hop address for the packet is specified by the caller,
64078678b13Srpaulo 	 * use it as the gateway.
64178678b13Srpaulo 	 */
64278678b13Srpaulo 	if (opts && opts->ip6po_nexthop) {
6435493f188Sdyoung 		struct route *ron;
644d0432711Sozaki-r 		struct sockaddr_in6 *sin6_next;
64578678b13Srpaulo 
64678678b13Srpaulo 		sin6_next = satosin6(opts->ip6po_nexthop);
64778678b13Srpaulo 
64878678b13Srpaulo 		/* at this moment, we only support AF_INET6 next hops */
64978678b13Srpaulo 		if (sin6_next->sin6_family != AF_INET6) {
6509e214c7fSozaki-r 			IP6_STATINC(IP6_STAT_ODROPPED);
65178678b13Srpaulo 			error = EAFNOSUPPORT; /* or should we proceed? */
65278678b13Srpaulo 			goto done;
65378678b13Srpaulo 		}
65478678b13Srpaulo 
65578678b13Srpaulo 		/*
65678678b13Srpaulo 		 * If the next hop is an IPv6 address, then the node identified
65778678b13Srpaulo 		 * by that address must be a neighbor of the sending host.
65878678b13Srpaulo 		 */
65972f0a6dfSdyoung 		ron = &opts->ip6po_nextroute;
660d0432711Sozaki-r 		rt = rtcache_lookup(ron, sin6tosa(sin6_next));
661d0432711Sozaki-r 		if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) != 0 ||
66272f0a6dfSdyoung 		    !nd6_is_addr_neighbor(sin6_next, rt->rt_ifp)) {
6634c25fb2fSozaki-r 			if (rt != NULL) {
6644c25fb2fSozaki-r 				if (count_discard)
6654c25fb2fSozaki-r 					in6_ifstat_inc(rt->rt_ifp,
6664c25fb2fSozaki-r 					    ifs6_out_discard);
6674c25fb2fSozaki-r 				rtcache_unref(rt, ron);
668d0432711Sozaki-r 				rt = NULL;
6694c25fb2fSozaki-r 			}
6704c25fb2fSozaki-r 			rtcache_free(ron);
67178678b13Srpaulo 			error = EHOSTUNREACH;
67278678b13Srpaulo 			goto done;
67378678b13Srpaulo 		}
674d0432711Sozaki-r 		*ro = ron;
67578678b13Srpaulo 
67678678b13Srpaulo 		goto done;
67778678b13Srpaulo 	}
67878678b13Srpaulo 
67978678b13Srpaulo 	/*
68078678b13Srpaulo 	 * Use a cached route if it exists and is valid, else try to allocate
68178678b13Srpaulo 	 * a new one.  Note that we should check the address family of the
682ae1b9c29Sitojun 	 * cached destination, in case of sharing the cache with IPv4.
6838d545fd9Skardel 	 *
6848d545fd9Skardel 	 * for V4 mapped addresses we want to pick up the v4 route
6858d545fd9Skardel 	 * see PR kern/56348
6869d853e8aSitojun 	 */
6878d545fd9Skardel 	if (IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
6888d545fd9Skardel 		in6_sin6_2_sin(&u.dst4, dstsock);
6898d545fd9Skardel 	} else {
69072f0a6dfSdyoung 		u.dst6 = *dstsock;
69172f0a6dfSdyoung 		u.dst6.sin6_scope_id = 0;
6928d545fd9Skardel 	}
6938d545fd9Skardel 
694d0432711Sozaki-r 	rt = rtcache_lookup1(*ro, &u.dst, 1);
6959d853e8aSitojun 
69672f0a6dfSdyoung 	if (rt == NULL)
69778678b13Srpaulo 		error = EHOSTUNREACH;
69878678b13Srpaulo 
6999d853e8aSitojun 	/*
70078678b13Srpaulo 	 * Check if the outgoing interface conflicts with
70178678b13Srpaulo 	 * the interface specified by ipi6_ifindex (if specified).
70278678b13Srpaulo 	 * Note that loopback interface is always okay.
70378678b13Srpaulo 	 * (this may happen when we are sending a packet to one of
70478678b13Srpaulo 	 *  our own addresses.)
7059d853e8aSitojun 	 */
7066e6136eaSozaki-r 	if (opts && opts->ip6po_pktinfo && opts->ip6po_pktinfo->ipi6_ifindex) {
7073de81a88Sozaki-r 		if (rt != NULL && !(rt->rt_ifp->if_flags & IFF_LOOPBACK) &&
708d0432711Sozaki-r 		    rt->rt_ifp->if_index != opts->ip6po_pktinfo->ipi6_ifindex) {
7093de81a88Sozaki-r 			if (count_discard)
710d0432711Sozaki-r 				in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
71178678b13Srpaulo 			error = EHOSTUNREACH;
712*7dec41f0Sozaki-r 			rtcache_unref(rt, *ro);
713d0432711Sozaki-r 			rt = NULL;
7149d853e8aSitojun 		}
7159d853e8aSitojun 	}
7169d853e8aSitojun 
71778678b13Srpaulo done:
71878678b13Srpaulo 	if (error == EHOSTUNREACH)
7190dd41b37Sthorpej 		IP6_STATINC(IP6_STAT_NOROUTE);
720d0432711Sozaki-r 	*retrt = rt;
721d0432711Sozaki-r 	return error;
72278678b13Srpaulo }
72378678b13Srpaulo 
72478678b13Srpaulo static int
in6_selectif(struct sockaddr_in6 * dstsock,struct ip6_pktopts * opts,struct ip6_moptions * mopts,struct route * ro,struct ifnet ** retifp,struct psref * psref)72572cfe732Schristos in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
7264badfc20Sozaki-r 	struct ip6_moptions *mopts, struct route *ro, struct ifnet **retifp,
7274badfc20Sozaki-r 	struct psref *psref)
72878678b13Srpaulo {
7294c25fb2fSozaki-r 	int error = 0;
73078678b13Srpaulo 	struct rtentry *rt = NULL;
731d0432711Sozaki-r 	struct in6_addr *dst;
732d0432711Sozaki-r 	struct in6_pktinfo *pi = NULL;
73378678b13Srpaulo 
7344badfc20Sozaki-r 	KASSERT(retifp != NULL);
7354badfc20Sozaki-r 	*retifp = NULL;
736d0432711Sozaki-r 	dst = &dstsock->sin6_addr;
7374badfc20Sozaki-r 
738d0432711Sozaki-r 	/* If the caller specify the outgoing interface explicitly, use it. */
739d0432711Sozaki-r 	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
740d0432711Sozaki-r 		/* XXX boundary check is assumed to be already done. */
741d0432711Sozaki-r 		*retifp = if_get_byindex(pi->ipi6_ifindex, psref);
742d0432711Sozaki-r 		if (*retifp != NULL)
743d0432711Sozaki-r 			return 0;
744d0432711Sozaki-r 		goto getroute;
74578678b13Srpaulo 	}
74678678b13Srpaulo 
74778678b13Srpaulo 	/*
748d0432711Sozaki-r 	 * If the destination address is a multicast address and the outgoing
749d0432711Sozaki-r 	 * interface for the address is specified by the caller, use it.
750d0432711Sozaki-r 	 */
751d0432711Sozaki-r 	if (IN6_IS_ADDR_MULTICAST(dst) && mopts != NULL) {
752d0432711Sozaki-r 		*retifp = if_get_byindex(mopts->im6o_multicast_if_index, psref);
753d0432711Sozaki-r 		if (*retifp != NULL)
754d0432711Sozaki-r 			return 0; /* we do not need a route for multicast. */
755d0432711Sozaki-r 	}
756d0432711Sozaki-r 
757d0432711Sozaki-r getroute:
758d0432711Sozaki-r 	error = in6_selectroute(dstsock, opts, &ro, &rt, false);
759d0432711Sozaki-r 	if (error != 0)
760d0432711Sozaki-r 		return error;
761d0432711Sozaki-r 
762d0432711Sozaki-r 	*retifp = if_get_byindex(rt->rt_ifp->if_index, psref);
763d0432711Sozaki-r 
764d0432711Sozaki-r 	/*
76578678b13Srpaulo 	 * do not use a rejected or black hole route.
76678678b13Srpaulo 	 * XXX: this check should be done in the L2 output routine.
76778678b13Srpaulo 	 * However, if we skipped this check here, we'd see the following
76878678b13Srpaulo 	 * scenario:
76978678b13Srpaulo 	 * - install a rejected route for a scoped address prefix
77078678b13Srpaulo 	 *   (like fe80::/10)
77178678b13Srpaulo 	 * - send a packet to a destination that matches the scoped prefix,
77278678b13Srpaulo 	 *   with ambiguity about the scope zone.
77378678b13Srpaulo 	 * - pick the outgoing interface from the route, and disambiguate the
77478678b13Srpaulo 	 *   scope zone with the interface.
77578678b13Srpaulo 	 * - ip6_output() would try to get another route with the "new"
77678678b13Srpaulo 	 *   destination, which may be valid.
77778678b13Srpaulo 	 * - we'd see no error on output.
77878678b13Srpaulo 	 * Although this may not be very harmful, it should still be confusing.
77978678b13Srpaulo 	 * We thus reject the case here.
78078678b13Srpaulo 	 */
7814c25fb2fSozaki-r 	if ((rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) {
7824c25fb2fSozaki-r 		error = (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
7834c25fb2fSozaki-r 		/* XXX: ifp can be returned with psref even if error */
7844c25fb2fSozaki-r 		goto out;
7854c25fb2fSozaki-r 	}
78678678b13Srpaulo 
78778678b13Srpaulo 	/*
78878678b13Srpaulo 	 * Adjust the "outgoing" interface.  If we're going to loop the packet
78978678b13Srpaulo 	 * back to ourselves, the ifp would be the loopback interface.
79078678b13Srpaulo 	 * However, we'd rather know the interface associated to the
79178678b13Srpaulo 	 * destination address (which should probably be one of our own
79278678b13Srpaulo 	 * addresses.)
79378678b13Srpaulo 	 */
794a8d68489Sozaki-r 	if (rt->rt_ifa->ifa_ifp != *retifp &&
795a403cbd4Sozaki-r 	    !if_is_deactivated(rt->rt_ifa->ifa_ifp)) {
7964badfc20Sozaki-r 		if_put(*retifp, psref);
79778678b13Srpaulo 		*retifp = rt->rt_ifa->ifa_ifp;
798c5696d3cSozaki-r 		if_acquire(*retifp, psref);
7994badfc20Sozaki-r 	}
8004c25fb2fSozaki-r out:
8014c25fb2fSozaki-r 	rtcache_unref(rt, ro);
8024c25fb2fSozaki-r 	return error;
8039d853e8aSitojun }
8049d853e8aSitojun 
80572cfe732Schristos /*
8069d853e8aSitojun  * Default hop limit selection. The precedence is as follows:
8079d853e8aSitojun  * 1. Hoplimit value specified via ioctl.
8089d853e8aSitojun  * 2. (If the outgoing interface is detected) the current
8099d853e8aSitojun  *     hop limit of the interface specified by router advertisement.
8109d853e8aSitojun  * 3. The system default hoplimit.
8119d853e8aSitojun */
8129d853e8aSitojun int
in6pcb_selecthlim(struct inpcb * inp,struct ifnet * ifp)813b000e63fSozaki-r in6pcb_selecthlim(struct inpcb *inp, struct ifnet *ifp)
8149d853e8aSitojun {
815a071c829Sozaki-r 	if (inp && in6p_hops6(inp) >= 0)
816a071c829Sozaki-r 		return in6p_hops6(inp);
8179d853e8aSitojun 	else if (ifp)
8185c1df51dSitojun 		return (ND_IFINFO(ifp)->chlim);
8199d853e8aSitojun 	else
8209d853e8aSitojun 		return (ip6_defhlim);
8219d853e8aSitojun }
8229d853e8aSitojun 
8232373b55aSozaki-r int
in6pcb_selecthlim_rt(struct inpcb * inp)824b000e63fSozaki-r in6pcb_selecthlim_rt(struct inpcb *inp)
8252373b55aSozaki-r {
8262373b55aSozaki-r 	struct rtentry *rt;
8272373b55aSozaki-r 
8280e390eeeSozaki-r 	if (inp == NULL)
829b000e63fSozaki-r 		return in6pcb_selecthlim(inp, NULL);
8302373b55aSozaki-r 
8310e390eeeSozaki-r 	rt = rtcache_validate(&inp->inp_route);
8324c25fb2fSozaki-r 	if (rt != NULL) {
833b000e63fSozaki-r 		int ret = in6pcb_selecthlim(inp, rt->rt_ifp);
8340e390eeeSozaki-r 		rtcache_unref(rt, &inp->inp_route);
8354c25fb2fSozaki-r 		return ret;
8364c25fb2fSozaki-r 	} else
837b000e63fSozaki-r 		return in6pcb_selecthlim(inp, NULL);
8382373b55aSozaki-r }
8392373b55aSozaki-r 
8409d853e8aSitojun /*
8419d853e8aSitojun  * Find an empty port and set it to the specified PCB.
8429d853e8aSitojun  */
8439d853e8aSitojun int
in6pcb_set_port(struct sockaddr_in6 * sin6,struct inpcb * inp,struct lwp * l)844b000e63fSozaki-r in6pcb_set_port(struct sockaddr_in6 *sin6, struct inpcb *inp, struct lwp *l)
8459d853e8aSitojun {
8460e390eeeSozaki-r 	struct socket *so = inp->inp_socket;
8470e390eeeSozaki-r 	struct inpcbtable *table = inp->inp_table;
848495906caSitojun 	u_int16_t lport, *lastport;
8493c1f169aSelad 	enum kauth_network_req req;
8505ec72efbSchristos 	int error = 0;
8519d853e8aSitojun 
8520e390eeeSozaki-r 	if (inp->inp_flags & IN6P_LOWPORT) {
8534d401793Sitojun #ifndef IPNOPRIVPORTS
8543c1f169aSelad 		req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
8553c1f169aSelad #else
8563c1f169aSelad 		req = KAUTH_REQ_NETWORK_BIND_PORT;
8574d401793Sitojun #endif
858495906caSitojun 		lastport = &table->inpt_lastlow;
8599d853e8aSitojun 	} else {
8603c1f169aSelad 		req = KAUTH_REQ_NETWORK_BIND_PORT;
8613c1f169aSelad 
862495906caSitojun 		lastport = &table->inpt_lastport;
863495906caSitojun 	}
86478678b13Srpaulo 
8653c1f169aSelad 	/* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
8663c1f169aSelad 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND, req, so,
8673c1f169aSelad 	    sin6, NULL);
8683c1f169aSelad 	if (error)
869b1520331Selad 		return (EACCES);
8703c1f169aSelad 
8715ec72efbSchristos        /*
8725ec72efbSchristos         * Use RFC6056 randomized port selection
8735ec72efbSchristos         */
8740e390eeeSozaki-r 	error = portalgo_randport(&lport, inp, l->l_cred);
8755ec72efbSchristos 	if (error)
8765ec72efbSchristos 		return error;
877495906caSitojun 
8780e390eeeSozaki-r 	inp->inp_flags |= IN6P_ANONPORT;
879495906caSitojun 	*lastport = lport;
8800e390eeeSozaki-r 	inp->inp_lport = htons(lport);
881b000e63fSozaki-r 	in6pcb_set_state(inp, INP_BOUND);
8829d853e8aSitojun 	return (0);		/* success */
8839d853e8aSitojun }
884ec67eee5Sitojun 
885574214f1Sitojun void
addrsel_policy_init(void)886d072fd0fSdyoung addrsel_policy_init(void)
887574214f1Sitojun {
88878678b13Srpaulo 	init_policy_queue();
88978678b13Srpaulo 
89078678b13Srpaulo 	/* initialize the "last resort" policy */
891c363a9cbScegger 	memset(&defaultaddrpolicy, 0, sizeof(defaultaddrpolicy));
89278678b13Srpaulo 	defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
89378678b13Srpaulo }
89478678b13Srpaulo 
8959faa0319Sozaki-r /*
8969faa0319Sozaki-r  * XXX: NOMPSAFE if a policy is set
8979faa0319Sozaki-r  */
89878678b13Srpaulo static struct in6_addrpolicy *
lookup_addrsel_policy(struct sockaddr_in6 * key)89972cfe732Schristos lookup_addrsel_policy(struct sockaddr_in6 *key)
90078678b13Srpaulo {
90178678b13Srpaulo 	struct in6_addrpolicy *match = NULL;
90278678b13Srpaulo 
90378678b13Srpaulo 	match = match_addrsel_policy(key);
90478678b13Srpaulo 
90578678b13Srpaulo 	if (match == NULL)
90678678b13Srpaulo 		match = &defaultaddrpolicy;
90778678b13Srpaulo 	else
90878678b13Srpaulo 		match->use++;
90978678b13Srpaulo 
91078678b13Srpaulo 	return (match);
91178678b13Srpaulo }
91278678b13Srpaulo 
91378678b13Srpaulo /*
91478678b13Srpaulo  * Subroutines to manage the address selection policy table via sysctl.
91578678b13Srpaulo  */
916461a86f9Schristos struct sel_walkarg {
91778678b13Srpaulo 	size_t	w_total;
91878678b13Srpaulo 	size_t	w_given;
91953524e44Schristos 	void *	w_where;
92053524e44Schristos 	void *w_limit;
92178678b13Srpaulo };
92278678b13Srpaulo 
9235b5956f3Schristos int sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS);
92478678b13Srpaulo int
sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS)9255b5956f3Schristos sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS)
92678678b13Srpaulo {
92778678b13Srpaulo 	int error = 0;
92878678b13Srpaulo 	int s;
92978678b13Srpaulo 
93078678b13Srpaulo 	s = splsoftnet();
93178678b13Srpaulo 
93278678b13Srpaulo 	if (newp) {
93378678b13Srpaulo 		error = EPERM;
93478678b13Srpaulo 		goto end;
93578678b13Srpaulo 	}
93678678b13Srpaulo 	if (oldp && oldlenp == NULL) {
93778678b13Srpaulo 		error = EINVAL;
93878678b13Srpaulo 		goto end;
93978678b13Srpaulo 	}
94078678b13Srpaulo 	if (oldp || oldlenp) {
941461a86f9Schristos 		struct sel_walkarg w;
94255f3f4d6Schristos 		size_t oldlen = *oldlenp;
94378678b13Srpaulo 
94453524e44Schristos 		memset(&w, 0, sizeof(w));
94578678b13Srpaulo 		w.w_given = oldlen;
94678678b13Srpaulo 		w.w_where = oldp;
94778678b13Srpaulo 		if (oldp)
94853524e44Schristos 			w.w_limit = (char *)oldp + oldlen;
94978678b13Srpaulo 
95078678b13Srpaulo 		error = walk_addrsel_policy(dump_addrsel_policyent, &w);
95178678b13Srpaulo 
95278678b13Srpaulo 		*oldlenp = w.w_total;
95378678b13Srpaulo 		if (oldp && w.w_total > oldlen && error == 0)
95478678b13Srpaulo 			error = ENOMEM;
95578678b13Srpaulo 	}
95678678b13Srpaulo 
95778678b13Srpaulo   end:
95878678b13Srpaulo 	splx(s);
95978678b13Srpaulo 
96078678b13Srpaulo 	return (error);
96178678b13Srpaulo }
96278678b13Srpaulo 
96378678b13Srpaulo int
in6_src_ioctl(u_long cmd,void * data)96472cfe732Schristos in6_src_ioctl(u_long cmd, void *data)
96578678b13Srpaulo {
96678678b13Srpaulo 	int i;
96778678b13Srpaulo 	struct in6_addrpolicy ent0;
96878678b13Srpaulo 
96978678b13Srpaulo 	if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
97078678b13Srpaulo 		return (EOPNOTSUPP); /* check for safety */
97178678b13Srpaulo 
97278678b13Srpaulo 	ent0 = *(struct in6_addrpolicy *)data;
97378678b13Srpaulo 
97478678b13Srpaulo 	if (ent0.label == ADDR_LABEL_NOTAPP)
97578678b13Srpaulo 		return (EINVAL);
97678678b13Srpaulo 	/* check if the prefix mask is consecutive. */
97778678b13Srpaulo 	if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
97878678b13Srpaulo 		return (EINVAL);
97978678b13Srpaulo 	/* clear trailing garbages (if any) of the prefix address. */
98078678b13Srpaulo 	for (i = 0; i < 4; i++) {
98178678b13Srpaulo 		ent0.addr.sin6_addr.s6_addr32[i] &=
98278678b13Srpaulo 			ent0.addrmask.sin6_addr.s6_addr32[i];
98378678b13Srpaulo 	}
98478678b13Srpaulo 	ent0.use = 0;
98578678b13Srpaulo 
98678678b13Srpaulo 	switch (cmd) {
98778678b13Srpaulo 	case SIOCAADDRCTL_POLICY:
98878678b13Srpaulo 		return (add_addrsel_policyent(&ent0));
98978678b13Srpaulo 	case SIOCDADDRCTL_POLICY:
99078678b13Srpaulo 		return (delete_addrsel_policyent(&ent0));
99178678b13Srpaulo 	}
99278678b13Srpaulo 
99378678b13Srpaulo 	return (0);		/* XXX: compromise compilers */
99478678b13Srpaulo }
99578678b13Srpaulo 
99678678b13Srpaulo /*
99778678b13Srpaulo  * The followings are implementation of the policy table using a
99878678b13Srpaulo  * simple tail queue.
99978678b13Srpaulo  * XXX such details should be hidden.
100078678b13Srpaulo  * XXX implementation using binary tree should be more efficient.
100178678b13Srpaulo  */
100278678b13Srpaulo struct addrsel_policyent {
100378678b13Srpaulo 	TAILQ_ENTRY(addrsel_policyent) ape_entry;
100478678b13Srpaulo 	struct in6_addrpolicy ape_policy;
100578678b13Srpaulo };
100678678b13Srpaulo 
100778678b13Srpaulo TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
100878678b13Srpaulo 
100978678b13Srpaulo struct addrsel_policyhead addrsel_policytab;
101078678b13Srpaulo 
101178678b13Srpaulo static void
init_policy_queue(void)1012c9395522Smatt init_policy_queue(void)
101378678b13Srpaulo {
101478678b13Srpaulo 	TAILQ_INIT(&addrsel_policytab);
101578678b13Srpaulo }
101678678b13Srpaulo 
101778678b13Srpaulo static int
add_addrsel_policyent(struct in6_addrpolicy * newpolicy)101872cfe732Schristos add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
101978678b13Srpaulo {
10206f1589d5Smatt 	struct addrsel_policyent *newpol, *pol;
102178678b13Srpaulo 
102278678b13Srpaulo 	/* duplication check */
10236f1589d5Smatt 	TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
102478678b13Srpaulo 		if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
102578678b13Srpaulo 		    &pol->ape_policy.addr.sin6_addr) &&
102678678b13Srpaulo 		    IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
102778678b13Srpaulo 		    &pol->ape_policy.addrmask.sin6_addr)) {
102878678b13Srpaulo 			return (EEXIST);	/* or override it? */
102978678b13Srpaulo 		}
103078678b13Srpaulo 	}
103178678b13Srpaulo 
10326f1589d5Smatt 	newpol = malloc(sizeof(*newpol), M_IFADDR, M_WAITOK|M_ZERO);
103378678b13Srpaulo 
103478678b13Srpaulo 	/* XXX: should validate entry */
10356f1589d5Smatt 	newpol->ape_policy = *newpolicy;
103678678b13Srpaulo 
10376f1589d5Smatt 	TAILQ_INSERT_TAIL(&addrsel_policytab, newpol, ape_entry);
103878678b13Srpaulo 
103978678b13Srpaulo 	return (0);
104078678b13Srpaulo }
104178678b13Srpaulo 
104278678b13Srpaulo static int
delete_addrsel_policyent(struct in6_addrpolicy * key)104372cfe732Schristos delete_addrsel_policyent(struct in6_addrpolicy *key)
104478678b13Srpaulo {
104578678b13Srpaulo 	struct addrsel_policyent *pol;
104678678b13Srpaulo 
104778678b13Srpaulo 	/* search for the entry in the table */
104878678b13Srpaulo 	for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
104978678b13Srpaulo 	     pol = TAILQ_NEXT(pol, ape_entry)) {
105078678b13Srpaulo 		if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
105178678b13Srpaulo 		    &pol->ape_policy.addr.sin6_addr) &&
105278678b13Srpaulo 		    IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
105378678b13Srpaulo 		    &pol->ape_policy.addrmask.sin6_addr)) {
105478678b13Srpaulo 			break;
105578678b13Srpaulo 		}
105678678b13Srpaulo 	}
105778678b13Srpaulo 	if (pol == NULL) {
105878678b13Srpaulo 		return (ESRCH);
105978678b13Srpaulo 	}
106078678b13Srpaulo 
106178678b13Srpaulo 	TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
106278678b13Srpaulo 
106378678b13Srpaulo 	return (0);
106478678b13Srpaulo }
106578678b13Srpaulo 
106678678b13Srpaulo static int
walk_addrsel_policy(int (* callback)(struct in6_addrpolicy *,void *),void * w)10675493f188Sdyoung walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), void *w)
106878678b13Srpaulo {
106978678b13Srpaulo 	struct addrsel_policyent *pol;
107078678b13Srpaulo 	int error = 0;
107178678b13Srpaulo 
10725493f188Sdyoung 	TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
10735493f188Sdyoung 		if ((error = (*callback)(&pol->ape_policy, w)) != 0)
10745493f188Sdyoung 			return error;
107578678b13Srpaulo 	}
107678678b13Srpaulo 
10775493f188Sdyoung 	return error;
107878678b13Srpaulo }
107978678b13Srpaulo 
108078678b13Srpaulo static int
dump_addrsel_policyent(struct in6_addrpolicy * pol,void * arg)10815493f188Sdyoung dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
108278678b13Srpaulo {
108378678b13Srpaulo 	int error = 0;
1084461a86f9Schristos 	struct sel_walkarg *w = arg;
108578678b13Srpaulo 
108653524e44Schristos 	if (w->w_where && (char *)w->w_where + sizeof(*pol) <= (char *)w->w_limit) {
108778678b13Srpaulo 		if ((error = copyout(pol, w->w_where, sizeof(*pol))) != 0)
10885493f188Sdyoung 			return error;
108953524e44Schristos 		w->w_where = (char *)w->w_where + sizeof(*pol);
109078678b13Srpaulo 	}
109178678b13Srpaulo 	w->w_total += sizeof(*pol);
109278678b13Srpaulo 
10935493f188Sdyoung 	return error;
109478678b13Srpaulo }
109578678b13Srpaulo 
109678678b13Srpaulo static struct in6_addrpolicy *
match_addrsel_policy(struct sockaddr_in6 * key)109772cfe732Schristos match_addrsel_policy(struct sockaddr_in6 *key)
109878678b13Srpaulo {
109978678b13Srpaulo 	struct addrsel_policyent *pent;
110078678b13Srpaulo 	struct in6_addrpolicy *bestpol = NULL, *pol;
110178678b13Srpaulo 	int matchlen, bestmatchlen = -1;
110278678b13Srpaulo 	u_char *mp, *ep, *k, *p, m;
110378678b13Srpaulo 
110478678b13Srpaulo 	for (pent = TAILQ_FIRST(&addrsel_policytab); pent;
110578678b13Srpaulo 	     pent = TAILQ_NEXT(pent, ape_entry)) {
110678678b13Srpaulo 		matchlen = 0;
110778678b13Srpaulo 
110878678b13Srpaulo 		pol = &pent->ape_policy;
110978678b13Srpaulo 		mp = (u_char *)&pol->addrmask.sin6_addr;
111078678b13Srpaulo 		ep = mp + 16;	/* XXX: scope field? */
111178678b13Srpaulo 		k = (u_char *)&key->sin6_addr;
111278678b13Srpaulo 		p = (u_char *)&pol->addr.sin6_addr;
111378678b13Srpaulo 		for (; mp < ep && *mp; mp++, k++, p++) {
111478678b13Srpaulo 			m = *mp;
111578678b13Srpaulo 			if ((*k & m) != *p)
111678678b13Srpaulo 				goto next; /* not match */
111778678b13Srpaulo 			if (m == 0xff) /* short cut for a typical case */
111878678b13Srpaulo 				matchlen += 8;
111978678b13Srpaulo 			else {
112078678b13Srpaulo 				while (m >= 0x80) {
112178678b13Srpaulo 					matchlen++;
112278678b13Srpaulo 					m <<= 1;
112378678b13Srpaulo 				}
112478678b13Srpaulo 			}
112578678b13Srpaulo 		}
112678678b13Srpaulo 
112778678b13Srpaulo 		/* matched.  check if this is better than the current best. */
112878678b13Srpaulo 		if (bestpol == NULL ||
112978678b13Srpaulo 		    matchlen > bestmatchlen) {
113078678b13Srpaulo 			bestpol = pol;
113178678b13Srpaulo 			bestmatchlen = matchlen;
113278678b13Srpaulo 		}
113378678b13Srpaulo 
113478678b13Srpaulo 	  next:
113578678b13Srpaulo 		continue;
113678678b13Srpaulo 	}
113778678b13Srpaulo 
113878678b13Srpaulo 	return (bestpol);
1139574214f1Sitojun }
1140