xref: /freebsd/sys/netinet6/in6_src.c (revision 60d8dbbef075d3b39891ed35e124d0f7ef8e5fb9)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4686cdd19SJun-ichiro itojun Hagino  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5686cdd19SJun-ichiro itojun Hagino  * All rights reserved.
6686cdd19SJun-ichiro itojun Hagino  *
7686cdd19SJun-ichiro itojun Hagino  * Redistribution and use in source and binary forms, with or without
8686cdd19SJun-ichiro itojun Hagino  * modification, are permitted provided that the following conditions
9686cdd19SJun-ichiro itojun Hagino  * are met:
10686cdd19SJun-ichiro itojun Hagino  * 1. Redistributions of source code must retain the above copyright
11686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer.
12686cdd19SJun-ichiro itojun Hagino  * 2. Redistributions in binary form must reproduce the above copyright
13686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer in the
14686cdd19SJun-ichiro itojun Hagino  *    documentation and/or other materials provided with the distribution.
15686cdd19SJun-ichiro itojun Hagino  * 3. Neither the name of the project nor the names of its contributors
16686cdd19SJun-ichiro itojun Hagino  *    may be used to endorse or promote products derived from this software
17686cdd19SJun-ichiro itojun Hagino  *    without specific prior written permission.
18686cdd19SJun-ichiro itojun Hagino  *
19686cdd19SJun-ichiro itojun Hagino  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20686cdd19SJun-ichiro itojun Hagino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21686cdd19SJun-ichiro itojun Hagino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22686cdd19SJun-ichiro itojun Hagino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23686cdd19SJun-ichiro itojun Hagino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24686cdd19SJun-ichiro itojun Hagino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25686cdd19SJun-ichiro itojun Hagino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26686cdd19SJun-ichiro itojun Hagino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27686cdd19SJun-ichiro itojun Hagino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28686cdd19SJun-ichiro itojun Hagino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29686cdd19SJun-ichiro itojun Hagino  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: in6_src.c,v 1.132 2003/08/26 04:42:27 keiichi Exp $
32686cdd19SJun-ichiro itojun Hagino  */
33686cdd19SJun-ichiro itojun Hagino 
34caf43b02SWarner Losh /*-
35686cdd19SJun-ichiro itojun Hagino  * Copyright (c) 1982, 1986, 1991, 1993
36686cdd19SJun-ichiro itojun Hagino  *	The Regents of the University of California.  All rights reserved.
37686cdd19SJun-ichiro itojun Hagino  *
38686cdd19SJun-ichiro itojun Hagino  * Redistribution and use in source and binary forms, with or without
39686cdd19SJun-ichiro itojun Hagino  * modification, are permitted provided that the following conditions
40686cdd19SJun-ichiro itojun Hagino  * are met:
41686cdd19SJun-ichiro itojun Hagino  * 1. Redistributions of source code must retain the above copyright
42686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer.
43686cdd19SJun-ichiro itojun Hagino  * 2. Redistributions in binary form must reproduce the above copyright
44686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer in the
45686cdd19SJun-ichiro itojun Hagino  *    documentation and/or other materials provided with the distribution.
46fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
47686cdd19SJun-ichiro itojun Hagino  *    may be used to endorse or promote products derived from this software
48686cdd19SJun-ichiro itojun Hagino  *    without specific prior written permission.
49686cdd19SJun-ichiro itojun Hagino  *
50686cdd19SJun-ichiro itojun Hagino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51686cdd19SJun-ichiro itojun Hagino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52686cdd19SJun-ichiro itojun Hagino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53686cdd19SJun-ichiro itojun Hagino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54686cdd19SJun-ichiro itojun Hagino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55686cdd19SJun-ichiro itojun Hagino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56686cdd19SJun-ichiro itojun Hagino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57686cdd19SJun-ichiro itojun Hagino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58686cdd19SJun-ichiro itojun Hagino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59686cdd19SJun-ichiro itojun Hagino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60686cdd19SJun-ichiro itojun Hagino  * SUCH DAMAGE.
61686cdd19SJun-ichiro itojun Hagino  */
62686cdd19SJun-ichiro itojun Hagino 
63b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
64686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
65686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
66686cdd19SJun-ichiro itojun Hagino #include <sys/param.h>
67686cdd19SJun-ichiro itojun Hagino #include <sys/systm.h>
684e7f640dSJohn Baldwin #include <sys/lock.h>
6933841545SHajimu UMEMOTO #include <sys/malloc.h>
70686cdd19SJun-ichiro itojun Hagino #include <sys/mbuf.h>
71acd3428bSRobert Watson #include <sys/priv.h>
72686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
73686cdd19SJun-ichiro itojun Hagino #include <sys/socket.h>
74686cdd19SJun-ichiro itojun Hagino #include <sys/socketvar.h>
757fc91b3fSHajimu UMEMOTO #include <sys/sockio.h>
767fc91b3fSHajimu UMEMOTO #include <sys/sysctl.h>
77686cdd19SJun-ichiro itojun Hagino #include <sys/errno.h>
78686cdd19SJun-ichiro itojun Hagino #include <sys/time.h>
79413628a7SBjoern A. Zeeb #include <sys/jail.h>
804a201dfcSHajimu UMEMOTO #include <sys/kernel.h>
81cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
825d525653SHajimu UMEMOTO #include <sys/sx.h>
83686cdd19SJun-ichiro itojun Hagino 
84686cdd19SJun-ichiro itojun Hagino #include <net/if.h>
8576039bc8SGleb Smirnoff #include <net/if_var.h>
869452b0d2SQing Li #include <net/if_dl.h>
873d0d5b21SJustin Hibbits #include <net/if_private.h>
88686cdd19SJun-ichiro itojun Hagino #include <net/route.h>
89983066f0SAlexander V. Chernikov #include <net/route/nhop.h>
906e6b3f7cSQing Li #include <net/if_llatbl.h>
91686cdd19SJun-ichiro itojun Hagino 
92686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h>
93686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h>
94686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h>
95686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h>
96686cdd19SJun-ichiro itojun Hagino #include <netinet/in_pcb.h>
97dc3c09c8SBjoern A. Zeeb #include <netinet/ip_var.h>
98dc3c09c8SBjoern A. Zeeb #include <netinet/udp.h>
99dc3c09c8SBjoern A. Zeeb #include <netinet/udp_var.h>
1004b79449eSBjoern A. Zeeb 
101686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
1039977be4aSAlexander V. Chernikov #include <netinet6/in6_fib.h>
104686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
105686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h>
106a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
107686cdd19SJun-ichiro itojun Hagino #include <netinet6/nd6.h>
108686cdd19SJun-ichiro itojun Hagino 
1097fc91b3fSHajimu UMEMOTO static struct mtx addrsel_lock;
1107fc91b3fSHajimu UMEMOTO #define	ADDRSEL_LOCK_INIT()	mtx_init(&addrsel_lock, "addrsel_lock", NULL, MTX_DEF)
1117fc91b3fSHajimu UMEMOTO #define	ADDRSEL_LOCK()		mtx_lock(&addrsel_lock)
1127fc91b3fSHajimu UMEMOTO #define	ADDRSEL_UNLOCK()	mtx_unlock(&addrsel_lock)
1137fc91b3fSHajimu UMEMOTO #define	ADDRSEL_LOCK_ASSERT()	mtx_assert(&addrsel_lock, MA_OWNED)
1147fc91b3fSHajimu UMEMOTO 
1155d525653SHajimu UMEMOTO static struct sx addrsel_sxlock;
1165d525653SHajimu UMEMOTO #define	ADDRSEL_SXLOCK_INIT()	sx_init(&addrsel_sxlock, "addrsel_sxlock")
1175d525653SHajimu UMEMOTO #define	ADDRSEL_SLOCK()		sx_slock(&addrsel_sxlock)
1185d525653SHajimu UMEMOTO #define	ADDRSEL_SUNLOCK()	sx_sunlock(&addrsel_sxlock)
1195d525653SHajimu UMEMOTO #define	ADDRSEL_XLOCK()		sx_xlock(&addrsel_sxlock)
1205d525653SHajimu UMEMOTO #define	ADDRSEL_XUNLOCK()	sx_xunlock(&addrsel_sxlock)
1215d525653SHajimu UMEMOTO 
1227fc91b3fSHajimu UMEMOTO #define ADDR_LABEL_NOTAPP (-1)
1235f901c92SAndrew Turner VNET_DEFINE_STATIC(struct in6_addrpolicy, defaultaddrpolicy);
1241e77c105SRobert Watson #define	V_defaultaddrpolicy		VNET(defaultaddrpolicy)
125d6385b1cSHajimu UMEMOTO 
12682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_prefer_tempaddr) = 0;
12782cea7e6SBjoern A. Zeeb 
1286f56329aSXin LI static int selectroute(struct sockaddr_in6 *, struct ip6_pktopts *,
129a1f7e5f8SHajimu UMEMOTO 	struct ip6_moptions *, struct route_in6 *, struct ifnet **,
130983066f0SAlexander V. Chernikov 	struct nhop_object **, int, u_int, uint32_t);
1316f56329aSXin LI static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
132357ce739SAlexander V. Chernikov 	struct ip6_moptions *, struct ifnet **,
1336f56329aSXin LI 	struct ifnet *, u_int);
134601c0b8bSAlexander V. Chernikov static int in6_selectsrc(uint32_t, struct sockaddr_in6 *,
135601c0b8bSAlexander V. Chernikov 	struct ip6_pktopts *, struct inpcb *, struct ucred *,
136601c0b8bSAlexander V. Chernikov 	struct ifnet **, struct in6_addr *);
137d6385b1cSHajimu UMEMOTO 
1389233d8f3SDavid E. O'Brien static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *);
139d6385b1cSHajimu UMEMOTO 
1409233d8f3SDavid E. O'Brien static void init_policy_queue(void);
1419233d8f3SDavid E. O'Brien static int add_addrsel_policyent(struct in6_addrpolicy *);
1429233d8f3SDavid E. O'Brien static int delete_addrsel_policyent(struct in6_addrpolicy *);
1436f56329aSXin LI static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
1446f56329aSXin LI 	void *);
1459233d8f3SDavid E. O'Brien static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
1469233d8f3SDavid E. O'Brien static struct in6_addrpolicy *match_addrsel_policy(struct sockaddr_in6 *);
1477fc91b3fSHajimu UMEMOTO 
148686cdd19SJun-ichiro itojun Hagino /*
14933841545SHajimu UMEMOTO  * Return an IPv6 address, which is the most appropriate for a given
150686cdd19SJun-ichiro itojun Hagino  * destination and user specified options.
15133841545SHajimu UMEMOTO  * If necessary, this function lookups the routing table and returns
152686cdd19SJun-ichiro itojun Hagino  * an entry to the caller for later use.
153686cdd19SJun-ichiro itojun Hagino  */
154d6385b1cSHajimu UMEMOTO #define REPLACE(r) do {\
155*60d8dbbeSKristof Provost 	IP6STAT_INC2(ip6s_sources_rule, (r)); \
156ab569b9cSBjoern A. Zeeb 	/* { \
157ab569b9cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \
158ab569b9cSBjoern A. Zeeb 	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)); \
159ab569b9cSBjoern A. Zeeb 	} */ \
160d6385b1cSHajimu UMEMOTO 	goto replace; \
161d6385b1cSHajimu UMEMOTO } while(0)
162d6385b1cSHajimu UMEMOTO #define NEXT(r) do {\
163ab569b9cSBjoern A. Zeeb 	/* { \
164ab569b9cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \
165ab569b9cSBjoern A. Zeeb 	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)); \
166ab569b9cSBjoern A. Zeeb 	} */ \
167d6385b1cSHajimu UMEMOTO 	goto next;		/* XXX: we can't use 'continue' here */ \
168d6385b1cSHajimu UMEMOTO } while(0)
169d6385b1cSHajimu UMEMOTO #define BREAK(r) do { \
170*60d8dbbeSKristof Provost 	IP6STAT_INC2(ip6s_sources_rule, (r)); \
171d6385b1cSHajimu UMEMOTO 	goto out;		/* XXX: we can't use 'break' here */ \
172d6385b1cSHajimu UMEMOTO } while(0)
173d6385b1cSHajimu UMEMOTO 
174601c0b8bSAlexander V. Chernikov static int
in6_selectsrc(uint32_t fibnum,struct sockaddr_in6 * dstsock,struct ip6_pktopts * opts,struct inpcb * inp,struct ucred * cred,struct ifnet ** ifpp,struct in6_addr * srcp)175601c0b8bSAlexander V. Chernikov in6_selectsrc(uint32_t fibnum, struct sockaddr_in6 *dstsock,
176601c0b8bSAlexander V. Chernikov     struct ip6_pktopts *opts, struct inpcb *inp, struct ucred *cred,
17788d166bfSBjoern A. Zeeb     struct ifnet **ifpp, struct in6_addr *srcp)
178686cdd19SJun-ichiro itojun Hagino {
179cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in6_ifa_tracker;
180877fc3b6SBjoern A. Zeeb 	struct in6_addr dst, tmp;
1811b46c7f8SBjoern A. Zeeb 	struct ifnet *ifp = NULL, *oifp = NULL;
182d6385b1cSHajimu UMEMOTO 	struct in6_ifaddr *ia = NULL, *ia_best = NULL;
183686cdd19SJun-ichiro itojun Hagino 	struct in6_pktinfo *pi = NULL;
184d6385b1cSHajimu UMEMOTO 	int dst_scope = -1, best_scope = -1, best_matchlen = -1;
185d6385b1cSHajimu UMEMOTO 	struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL;
186d6385b1cSHajimu UMEMOTO 	u_int32_t odstzone;
187d6385b1cSHajimu UMEMOTO 	int prefer_tempaddr;
188151ba793SAlexander Kabaev 	int error;
189f2f877d3SBjoern A. Zeeb 	struct ip6_moptions *mopts;
190e6a27350SHajimu UMEMOTO 
191605284b8SAlexander V. Chernikov 	NET_EPOCH_ASSERT();
19288d166bfSBjoern A. Zeeb 	KASSERT(srcp != NULL, ("%s: srcp is NULL", __func__));
19388d166bfSBjoern A. Zeeb 
194a1f7e5f8SHajimu UMEMOTO 	dst = dstsock->sin6_addr; /* make a copy for local operation */
1951b46c7f8SBjoern A. Zeeb 	if (ifpp) {
1961b46c7f8SBjoern A. Zeeb 		/*
1971b46c7f8SBjoern A. Zeeb 		 * Save a possibly passed in ifp for in6_selectsrc. Only
1981b46c7f8SBjoern A. Zeeb 		 * neighbor discovery code should use this feature, where
1991b46c7f8SBjoern A. Zeeb 		 * we may know the interface but not the FIB number holding
2001b46c7f8SBjoern A. Zeeb 		 * the connected subnet in case someone deleted it from the
2011b46c7f8SBjoern A. Zeeb 		 * default FIB and we need to check the interface.
2021b46c7f8SBjoern A. Zeeb 		 */
2031b46c7f8SBjoern A. Zeeb 		if (*ifpp != NULL)
2041b46c7f8SBjoern A. Zeeb 			oifp = *ifpp;
205a1f7e5f8SHajimu UMEMOTO 		*ifpp = NULL;
2061b46c7f8SBjoern A. Zeeb 	}
207686cdd19SJun-ichiro itojun Hagino 
208a55b8b20SBjoern A. Zeeb 	if (inp != NULL) {
209a55b8b20SBjoern A. Zeeb 		INP_LOCK_ASSERT(inp);
210f2f877d3SBjoern A. Zeeb 		mopts = inp->in6p_moptions;
211a55b8b20SBjoern A. Zeeb 	} else {
212f2f877d3SBjoern A. Zeeb 		mopts = NULL;
213a55b8b20SBjoern A. Zeeb 	}
214f2f877d3SBjoern A. Zeeb 
215686cdd19SJun-ichiro itojun Hagino 	/*
216686cdd19SJun-ichiro itojun Hagino 	 * If the source address is explicitly specified by the caller,
217d6385b1cSHajimu UMEMOTO 	 * check if the requested source address is indeed a unicast address
218d6385b1cSHajimu UMEMOTO 	 * assigned to the node, and can be used as the packet's source
219d6385b1cSHajimu UMEMOTO 	 * address.  If everything is okay, use the address as source.
220686cdd19SJun-ichiro itojun Hagino 	 */
221686cdd19SJun-ichiro itojun Hagino 	if (opts && (pi = opts->ip6po_pktinfo) &&
222d6385b1cSHajimu UMEMOTO 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
223d6385b1cSHajimu UMEMOTO 		/* get the outgoing interface */
224357ce739SAlexander V. Chernikov 		if ((error = in6_selectif(dstsock, opts, mopts, &ifp, oifp,
225601c0b8bSAlexander V. Chernikov 		    fibnum))
22681d5d46bSBjoern A. Zeeb 		    != 0)
22788d166bfSBjoern A. Zeeb 			return (error);
22868efda09SHajimu UMEMOTO 
229686cdd19SJun-ichiro itojun Hagino 		/*
230d6385b1cSHajimu UMEMOTO 		 * determine the appropriate zone id of the source based on
231d6385b1cSHajimu UMEMOTO 		 * the zone of the destination and the outgoing interface.
232a1f7e5f8SHajimu UMEMOTO 		 * If the specified address is ambiguous wrt the scope zone,
233a1f7e5f8SHajimu UMEMOTO 		 * the interface must be specified; otherwise, ifa_ifwithaddr()
234a1f7e5f8SHajimu UMEMOTO 		 * will fail matching the address.
235d6385b1cSHajimu UMEMOTO 		 */
2363e16fab3SAndrey V. Elsukov 		tmp = pi->ipi6_addr;
237d6385b1cSHajimu UMEMOTO 		if (ifp) {
2383e16fab3SAndrey V. Elsukov 			error = in6_setscope(&tmp, ifp, &odstzone);
23988d166bfSBjoern A. Zeeb 			if (error)
24088d166bfSBjoern A. Zeeb 				return (error);
241d6385b1cSHajimu UMEMOTO 		}
24288d166bfSBjoern A. Zeeb 		if (cred != NULL && (error = prison_local_ip6(cred,
2433e16fab3SAndrey V. Elsukov 		    &tmp, (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0)
24488d166bfSBjoern A. Zeeb 			return (error);
245a1f7e5f8SHajimu UMEMOTO 
24604894769SAndrey V. Elsukov 		/*
24704894769SAndrey V. Elsukov 		 * If IPV6_BINDANY socket option is set, we allow to specify
24804894769SAndrey V. Elsukov 		 * non local addresses as source address in IPV6_PKTINFO
24904894769SAndrey V. Elsukov 		 * ancillary data.
25004894769SAndrey V. Elsukov 		 */
25104894769SAndrey V. Elsukov 		if ((inp->inp_flags & INP_BINDANY) == 0) {
2528268d82cSAlexander V. Chernikov 			ia = in6ifa_ifwithaddr(&tmp, 0 /* XXX */, false);
2533e16fab3SAndrey V. Elsukov 			if (ia == NULL || (ia->ia6_flags & (IN6_IFF_ANYCAST |
2548268d82cSAlexander V. Chernikov 			    IN6_IFF_NOTREADY)))
25588d166bfSBjoern A. Zeeb 				return (EADDRNOTAVAIL);
2563e16fab3SAndrey V. Elsukov 			bcopy(&ia->ia_addr.sin6_addr, srcp, sizeof(*srcp));
25704894769SAndrey V. Elsukov 		} else
2583e16fab3SAndrey V. Elsukov 			bcopy(&tmp, srcp, sizeof(*srcp));
2593e16fab3SAndrey V. Elsukov 		pi->ipi6_addr = tmp; /* XXX: this overrides pi */
260a1f7e5f8SHajimu UMEMOTO 		if (ifpp)
261a1f7e5f8SHajimu UMEMOTO 			*ifpp = ifp;
26288d166bfSBjoern A. Zeeb 		return (0);
263d6385b1cSHajimu UMEMOTO 	}
264d6385b1cSHajimu UMEMOTO 
265d6385b1cSHajimu UMEMOTO 	/*
266d6385b1cSHajimu UMEMOTO 	 * Otherwise, if the socket has already bound the source, just use it.
267686cdd19SJun-ichiro itojun Hagino 	 */
268f2f877d3SBjoern A. Zeeb 	if (inp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
269b89e82ddSJamie Gritton 		if (cred != NULL &&
27088d166bfSBjoern A. Zeeb 		    (error = prison_local_ip6(cred, &inp->in6p_laddr,
271b89e82ddSJamie Gritton 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
27288d166bfSBjoern A. Zeeb 			return (error);
27388d166bfSBjoern A. Zeeb 		bcopy(&inp->in6p_laddr, srcp, sizeof(*srcp));
27488d166bfSBjoern A. Zeeb 		return (0);
275f2f877d3SBjoern A. Zeeb 	}
276686cdd19SJun-ichiro itojun Hagino 
277686cdd19SJun-ichiro itojun Hagino 	/*
278592bcae8SBjoern A. Zeeb 	 * Bypass source address selection and use the primary jail IP
279592bcae8SBjoern A. Zeeb 	 * if requested.
280592bcae8SBjoern A. Zeeb 	 */
281592bcae8SBjoern A. Zeeb 	if (cred != NULL && !prison_saddrsel_ip6(cred, srcp))
282592bcae8SBjoern A. Zeeb 		return (0);
283592bcae8SBjoern A. Zeeb 
284592bcae8SBjoern A. Zeeb 	/*
285d6385b1cSHajimu UMEMOTO 	 * If the address is not specified, choose the best one based on
286d6385b1cSHajimu UMEMOTO 	 * the outgoing interface and the destination address.
287686cdd19SJun-ichiro itojun Hagino 	 */
288d6385b1cSHajimu UMEMOTO 	/* get the outgoing interface */
289357ce739SAlexander V. Chernikov 	if ((error = in6_selectif(dstsock, opts, mopts, &ifp, oifp,
290559b4296SAlan Somers 	    (inp != NULL) ? inp->inp_inc.inc_fibnum : fibnum)) != 0)
29188d166bfSBjoern A. Zeeb 		return (error);
292686cdd19SJun-ichiro itojun Hagino 
293d6385b1cSHajimu UMEMOTO #ifdef DIAGNOSTIC
294d6385b1cSHajimu UMEMOTO 	if (ifp == NULL)	/* this should not happen */
295d6385b1cSHajimu UMEMOTO 		panic("in6_selectsrc: NULL ifp");
296d6385b1cSHajimu UMEMOTO #endif
29788d166bfSBjoern A. Zeeb 	error = in6_setscope(&dst, ifp, &odstzone);
29888d166bfSBjoern A. Zeeb 	if (error)
29988d166bfSBjoern A. Zeeb 		return (error);
300a1f7e5f8SHajimu UMEMOTO 
301cc0a3c8cSAndrey V. Elsukov 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
302d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
303d6385b1cSHajimu UMEMOTO 		int new_scope = -1, new_matchlen = -1;
304d6385b1cSHajimu UMEMOTO 		struct in6_addrpolicy *new_policy = NULL;
305d6385b1cSHajimu UMEMOTO 		u_int32_t srczone, osrczone, dstzone;
306a1f7e5f8SHajimu UMEMOTO 		struct in6_addr src;
307d6385b1cSHajimu UMEMOTO 		struct ifnet *ifp1 = ia->ia_ifp;
308d6385b1cSHajimu UMEMOTO 
309d6385b1cSHajimu UMEMOTO 		/*
310d6385b1cSHajimu UMEMOTO 		 * We'll never take an address that breaks the scope zone
311d6385b1cSHajimu UMEMOTO 		 * of the destination.  We also skip an address if its zone
312d6385b1cSHajimu UMEMOTO 		 * does not contain the outgoing interface.
313d6385b1cSHajimu UMEMOTO 		 * XXX: we should probably use sin6_scope_id here.
314d6385b1cSHajimu UMEMOTO 		 */
315a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&dst, ifp1, &dstzone) ||
316d6385b1cSHajimu UMEMOTO 		    odstzone != dstzone) {
317d6385b1cSHajimu UMEMOTO 			continue;
318d6385b1cSHajimu UMEMOTO 		}
319a1f7e5f8SHajimu UMEMOTO 		src = ia->ia_addr.sin6_addr;
320a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&src, ifp, &osrczone) ||
321a1f7e5f8SHajimu UMEMOTO 		    in6_setscope(&src, ifp1, &srczone) ||
322d6385b1cSHajimu UMEMOTO 		    osrczone != srczone) {
323d6385b1cSHajimu UMEMOTO 			continue;
324686cdd19SJun-ichiro itojun Hagino 		}
325686cdd19SJun-ichiro itojun Hagino 
326d6385b1cSHajimu UMEMOTO 		/* avoid unusable addresses */
327d6385b1cSHajimu UMEMOTO 		if ((ia->ia6_flags &
328d6385b1cSHajimu UMEMOTO 		     (IN6_IFF_NOTREADY | IN6_IFF_ANYCAST | IN6_IFF_DETACHED))) {
329d6385b1cSHajimu UMEMOTO 				continue;
330d6385b1cSHajimu UMEMOTO 		}
331603724d3SBjoern A. Zeeb 		if (!V_ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
332d6385b1cSHajimu UMEMOTO 			continue;
333d6385b1cSHajimu UMEMOTO 
334877fc3b6SBjoern A. Zeeb 		/* If jailed only take addresses of the jail into account. */
335413628a7SBjoern A. Zeeb 		if (cred != NULL &&
336877fc3b6SBjoern A. Zeeb 		    prison_check_ip6(cred, &ia->ia_addr.sin6_addr) != 0)
337413628a7SBjoern A. Zeeb 			continue;
338413628a7SBjoern A. Zeeb 
339d6385b1cSHajimu UMEMOTO 		/* Rule 1: Prefer same address */
340a1f7e5f8SHajimu UMEMOTO 		if (IN6_ARE_ADDR_EQUAL(&dst, &ia->ia_addr.sin6_addr)) {
341d6385b1cSHajimu UMEMOTO 			ia_best = ia;
342d6385b1cSHajimu UMEMOTO 			BREAK(1); /* there should be no better candidate */
343d6385b1cSHajimu UMEMOTO 		}
344d6385b1cSHajimu UMEMOTO 
345d6385b1cSHajimu UMEMOTO 		if (ia_best == NULL)
346d6385b1cSHajimu UMEMOTO 			REPLACE(0);
347d6385b1cSHajimu UMEMOTO 
348d6385b1cSHajimu UMEMOTO 		/* Rule 2: Prefer appropriate scope */
349d6385b1cSHajimu UMEMOTO 		if (dst_scope < 0)
350a1f7e5f8SHajimu UMEMOTO 			dst_scope = in6_addrscope(&dst);
351d6385b1cSHajimu UMEMOTO 		new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
352d6385b1cSHajimu UMEMOTO 		if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
353d6385b1cSHajimu UMEMOTO 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
354d6385b1cSHajimu UMEMOTO 				REPLACE(2);
355d6385b1cSHajimu UMEMOTO 			NEXT(2);
356d6385b1cSHajimu UMEMOTO 		} else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
357d6385b1cSHajimu UMEMOTO 			if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
358d6385b1cSHajimu UMEMOTO 				NEXT(2);
359d6385b1cSHajimu UMEMOTO 			REPLACE(2);
360d6385b1cSHajimu UMEMOTO 		}
361d6385b1cSHajimu UMEMOTO 
362d6385b1cSHajimu UMEMOTO 		/*
363d6385b1cSHajimu UMEMOTO 		 * Rule 3: Avoid deprecated addresses.  Note that the case of
364d6385b1cSHajimu UMEMOTO 		 * !ip6_use_deprecated is already rejected above.
365d6385b1cSHajimu UMEMOTO 		 */
366d6385b1cSHajimu UMEMOTO 		if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
367d6385b1cSHajimu UMEMOTO 			NEXT(3);
368d6385b1cSHajimu UMEMOTO 		if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
369d6385b1cSHajimu UMEMOTO 			REPLACE(3);
370d6385b1cSHajimu UMEMOTO 
371d6385b1cSHajimu UMEMOTO 		/* Rule 4: Prefer home addresses */
372d6385b1cSHajimu UMEMOTO 		/*
373d6385b1cSHajimu UMEMOTO 		 * XXX: This is a TODO.  We should probably merge the MIP6
374d6385b1cSHajimu UMEMOTO 		 * case above.
375d6385b1cSHajimu UMEMOTO 		 */
376d6385b1cSHajimu UMEMOTO 
377d6385b1cSHajimu UMEMOTO 		/* Rule 5: Prefer outgoing interface */
378164051ceSHajimu UMEMOTO 		if (!(ND_IFINFO(ifp)->flags & ND6_IFF_NO_PREFER_IFACE)) {
379d6385b1cSHajimu UMEMOTO 			if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
380d6385b1cSHajimu UMEMOTO 				NEXT(5);
381d6385b1cSHajimu UMEMOTO 			if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
382d6385b1cSHajimu UMEMOTO 				REPLACE(5);
383164051ceSHajimu UMEMOTO 		}
384d6385b1cSHajimu UMEMOTO 
385d6385b1cSHajimu UMEMOTO 		/*
386d6385b1cSHajimu UMEMOTO 		 * Rule 6: Prefer matching label
387d6385b1cSHajimu UMEMOTO 		 * Note that best_policy should be non-NULL here.
388d6385b1cSHajimu UMEMOTO 		 */
389d6385b1cSHajimu UMEMOTO 		if (dst_policy == NULL)
390d6385b1cSHajimu UMEMOTO 			dst_policy = lookup_addrsel_policy(dstsock);
391d6385b1cSHajimu UMEMOTO 		if (dst_policy->label != ADDR_LABEL_NOTAPP) {
392d6385b1cSHajimu UMEMOTO 			new_policy = lookup_addrsel_policy(&ia->ia_addr);
393d6385b1cSHajimu UMEMOTO 			if (dst_policy->label == best_policy->label &&
394d6385b1cSHajimu UMEMOTO 			    dst_policy->label != new_policy->label)
395d6385b1cSHajimu UMEMOTO 				NEXT(6);
396d6385b1cSHajimu UMEMOTO 			if (dst_policy->label != best_policy->label &&
397d6385b1cSHajimu UMEMOTO 			    dst_policy->label == new_policy->label)
398d6385b1cSHajimu UMEMOTO 				REPLACE(6);
399d6385b1cSHajimu UMEMOTO 		}
400d6385b1cSHajimu UMEMOTO 
401d6385b1cSHajimu UMEMOTO 		/*
402d6385b1cSHajimu UMEMOTO 		 * Rule 7: Prefer public addresses.
403d6385b1cSHajimu UMEMOTO 		 * We allow users to reverse the logic by configuring
404d6385b1cSHajimu UMEMOTO 		 * a sysctl variable, so that privacy conscious users can
405d6385b1cSHajimu UMEMOTO 		 * always prefer temporary addresses.
406d6385b1cSHajimu UMEMOTO 		 */
407d6385b1cSHajimu UMEMOTO 		if (opts == NULL ||
408d6385b1cSHajimu UMEMOTO 		    opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
409603724d3SBjoern A. Zeeb 			prefer_tempaddr = V_ip6_prefer_tempaddr;
410d6385b1cSHajimu UMEMOTO 		} else if (opts->ip6po_prefer_tempaddr ==
411d6385b1cSHajimu UMEMOTO 		    IP6PO_TEMPADDR_NOTPREFER) {
412d6385b1cSHajimu UMEMOTO 			prefer_tempaddr = 0;
413d6385b1cSHajimu UMEMOTO 		} else
414d6385b1cSHajimu UMEMOTO 			prefer_tempaddr = 1;
415d6385b1cSHajimu UMEMOTO 		if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
416d6385b1cSHajimu UMEMOTO 		    (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
417d6385b1cSHajimu UMEMOTO 			if (prefer_tempaddr)
418d6385b1cSHajimu UMEMOTO 				REPLACE(7);
419d6385b1cSHajimu UMEMOTO 			else
420d6385b1cSHajimu UMEMOTO 				NEXT(7);
421d6385b1cSHajimu UMEMOTO 		}
422d6385b1cSHajimu UMEMOTO 		if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
423d6385b1cSHajimu UMEMOTO 		    !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
424d6385b1cSHajimu UMEMOTO 			if (prefer_tempaddr)
425d6385b1cSHajimu UMEMOTO 				NEXT(7);
426d6385b1cSHajimu UMEMOTO 			else
427d6385b1cSHajimu UMEMOTO 				REPLACE(7);
428d6385b1cSHajimu UMEMOTO 		}
429d6385b1cSHajimu UMEMOTO 
430d6385b1cSHajimu UMEMOTO 		/*
431d6385b1cSHajimu UMEMOTO 		 * Rule 8: prefer addresses on alive interfaces.
432d6385b1cSHajimu UMEMOTO 		 * This is a KAME specific rule.
433d6385b1cSHajimu UMEMOTO 		 */
434d6385b1cSHajimu UMEMOTO 		if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
435d6385b1cSHajimu UMEMOTO 		    !(ia->ia_ifp->if_flags & IFF_UP))
436d6385b1cSHajimu UMEMOTO 			NEXT(8);
437d6385b1cSHajimu UMEMOTO 		if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
438d6385b1cSHajimu UMEMOTO 		    (ia->ia_ifp->if_flags & IFF_UP))
439d6385b1cSHajimu UMEMOTO 			REPLACE(8);
440d6385b1cSHajimu UMEMOTO 
441d6385b1cSHajimu UMEMOTO 		/*
442d6e6b994SAndrey V. Elsukov 		 * Rule 9: prefer address with better virtual status.
443d6e6b994SAndrey V. Elsukov 		 */
444d6e6b994SAndrey V. Elsukov 		if (ifa_preferred(&ia_best->ia_ifa, &ia->ia_ifa))
445d6e6b994SAndrey V. Elsukov 			REPLACE(9);
446ccc53de9SAndrey V. Elsukov 		if (ifa_preferred(&ia->ia_ifa, &ia_best->ia_ifa))
447ccc53de9SAndrey V. Elsukov 			NEXT(9);
448d6e6b994SAndrey V. Elsukov 
449d6e6b994SAndrey V. Elsukov 		/*
4505dbfa43fSAndrey V. Elsukov 		 * Rule 10: prefer address with `prefer_source' flag.
4515dbfa43fSAndrey V. Elsukov 		 */
4525dbfa43fSAndrey V. Elsukov 		if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0 &&
4535dbfa43fSAndrey V. Elsukov 		    (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0)
4545dbfa43fSAndrey V. Elsukov 			REPLACE(10);
4555dbfa43fSAndrey V. Elsukov 		if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0 &&
4565dbfa43fSAndrey V. Elsukov 		    (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0)
4575dbfa43fSAndrey V. Elsukov 			NEXT(10);
4585dbfa43fSAndrey V. Elsukov 
4595dbfa43fSAndrey V. Elsukov 		/*
460d6385b1cSHajimu UMEMOTO 		 * Rule 14: Use longest matching prefix.
461d6385b1cSHajimu UMEMOTO 		 * Note: in the address selection draft, this rule is
462d6385b1cSHajimu UMEMOTO 		 * documented as "Rule 8".  However, since it is also
463d6385b1cSHajimu UMEMOTO 		 * documented that this rule can be overridden, we assign
464d6385b1cSHajimu UMEMOTO 		 * a large number so that it is easy to assign smaller numbers
465d6385b1cSHajimu UMEMOTO 		 * to more preferred rules.
466d6385b1cSHajimu UMEMOTO 		 */
467a1f7e5f8SHajimu UMEMOTO 		new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, &dst);
468d6385b1cSHajimu UMEMOTO 		if (best_matchlen < new_matchlen)
469d6385b1cSHajimu UMEMOTO 			REPLACE(14);
470d6385b1cSHajimu UMEMOTO 		if (new_matchlen < best_matchlen)
471d6385b1cSHajimu UMEMOTO 			NEXT(14);
472d6385b1cSHajimu UMEMOTO 
473d6385b1cSHajimu UMEMOTO 		/* Rule 15 is reserved. */
474d6385b1cSHajimu UMEMOTO 
475d6385b1cSHajimu UMEMOTO 		/*
476d6385b1cSHajimu UMEMOTO 		 * Last resort: just keep the current candidate.
477d6385b1cSHajimu UMEMOTO 		 * Or, do we need more rules?
478d6385b1cSHajimu UMEMOTO 		 */
479d6385b1cSHajimu UMEMOTO 		continue;
480d6385b1cSHajimu UMEMOTO 
481d6385b1cSHajimu UMEMOTO 	  replace:
482d6385b1cSHajimu UMEMOTO 		ia_best = ia;
483d6385b1cSHajimu UMEMOTO 		best_scope = (new_scope >= 0 ? new_scope :
484d6385b1cSHajimu UMEMOTO 			      in6_addrscope(&ia_best->ia_addr.sin6_addr));
485d6385b1cSHajimu UMEMOTO 		best_policy = (new_policy ? new_policy :
486d6385b1cSHajimu UMEMOTO 			       lookup_addrsel_policy(&ia_best->ia_addr));
487d6385b1cSHajimu UMEMOTO 		best_matchlen = (new_matchlen >= 0 ? new_matchlen :
488d6385b1cSHajimu UMEMOTO 				 in6_matchlen(&ia_best->ia_addr.sin6_addr,
489a1f7e5f8SHajimu UMEMOTO 					      &dst));
490d6385b1cSHajimu UMEMOTO 
491d6385b1cSHajimu UMEMOTO 	  next:
492d6385b1cSHajimu UMEMOTO 		continue;
493d6385b1cSHajimu UMEMOTO 
494d6385b1cSHajimu UMEMOTO 	  out:
495d6385b1cSHajimu UMEMOTO 		break;
496d6385b1cSHajimu UMEMOTO 	}
497d6385b1cSHajimu UMEMOTO 
498d1da0a06SRobert Watson 	if ((ia = ia_best) == NULL) {
499cc0a3c8cSAndrey V. Elsukov 		IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
500e7a87117SAndrey V. Elsukov 		IP6STAT_INC(ip6s_sources_none);
50188d166bfSBjoern A. Zeeb 		return (EADDRNOTAVAIL);
502d1da0a06SRobert Watson 	}
503686cdd19SJun-ichiro itojun Hagino 
504877fc3b6SBjoern A. Zeeb 	/*
505877fc3b6SBjoern A. Zeeb 	 * At this point at least one of the addresses belonged to the jail
506877fc3b6SBjoern A. Zeeb 	 * but it could still be, that we want to further restrict it, e.g.
507877fc3b6SBjoern A. Zeeb 	 * theoratically IN6_IS_ADDR_LOOPBACK.
508877fc3b6SBjoern A. Zeeb 	 * It must not be IN6_IS_ADDR_UNSPECIFIED anymore.
509877fc3b6SBjoern A. Zeeb 	 * prison_local_ip6() will fix an IN6_IS_ADDR_LOOPBACK but should
510877fc3b6SBjoern A. Zeeb 	 * let all others previously selected pass.
511877fc3b6SBjoern A. Zeeb 	 * Use tmp to not change ::1 on lo0 to the primary jail address.
512877fc3b6SBjoern A. Zeeb 	 */
513877fc3b6SBjoern A. Zeeb 	tmp = ia->ia_addr.sin6_addr;
514877fc3b6SBjoern A. Zeeb 	if (cred != NULL && prison_local_ip6(cred, &tmp, (inp != NULL &&
515877fc3b6SBjoern A. Zeeb 	    (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0) {
516cc0a3c8cSAndrey V. Elsukov 		IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
517e7a87117SAndrey V. Elsukov 		IP6STAT_INC(ip6s_sources_none);
518877fc3b6SBjoern A. Zeeb 		return (EADDRNOTAVAIL);
519877fc3b6SBjoern A. Zeeb 	}
520877fc3b6SBjoern A. Zeeb 
521a1f7e5f8SHajimu UMEMOTO 	if (ifpp)
522a1f7e5f8SHajimu UMEMOTO 		*ifpp = ifp;
523a1f7e5f8SHajimu UMEMOTO 
524877fc3b6SBjoern A. Zeeb 	bcopy(&tmp, srcp, sizeof(*srcp));
5254ff7c740SAndrey V. Elsukov 	if (ia->ia_ifp == ifp)
526*60d8dbbeSKristof Provost 		IP6STAT_INC2(ip6s_sources_sameif, best_scope);
5274ff7c740SAndrey V. Elsukov 	else
528*60d8dbbeSKristof Provost 		IP6STAT_INC2(ip6s_sources_otherif, best_scope);
5294ff7c740SAndrey V. Elsukov 	if (dst_scope == best_scope)
530*60d8dbbeSKristof Provost 		IP6STAT_INC2(ip6s_sources_samescope, best_scope);
5314ff7c740SAndrey V. Elsukov 	else
532*60d8dbbeSKristof Provost 		IP6STAT_INC2(ip6s_sources_otherscope, best_scope);
5334ff7c740SAndrey V. Elsukov 	if (IFA6_IS_DEPRECATED(ia))
534*60d8dbbeSKristof Provost 		IP6STAT_INC2(ip6s_sources_deprecated, best_scope);
535cc0a3c8cSAndrey V. Elsukov 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
53688d166bfSBjoern A. Zeeb 	return (0);
537d6385b1cSHajimu UMEMOTO }
538d6385b1cSHajimu UMEMOTO 
5391272577eSXin LI /*
540601c0b8bSAlexander V. Chernikov  * Select source address based on @inp, @dstsock and @opts.
541601c0b8bSAlexander V. Chernikov  * Stores selected address to @srcp. If @scope_ambiguous is set,
542601c0b8bSAlexander V. Chernikov  * embed scope from selected outgoing interface. If @hlim pointer
543601c0b8bSAlexander V. Chernikov  * is provided, stores calculated hop limit there.
544601c0b8bSAlexander V. Chernikov  * Returns 0 on success.
545601c0b8bSAlexander V. Chernikov  */
546601c0b8bSAlexander V. Chernikov 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)547601c0b8bSAlexander V. Chernikov in6_selectsrc_socket(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
548601c0b8bSAlexander V. Chernikov     struct inpcb *inp, struct ucred *cred, int scope_ambiguous,
549601c0b8bSAlexander V. Chernikov     struct in6_addr *srcp, int *hlim)
550601c0b8bSAlexander V. Chernikov {
551601c0b8bSAlexander V. Chernikov 	struct ifnet *retifp;
552601c0b8bSAlexander V. Chernikov 	uint32_t fibnum;
553601c0b8bSAlexander V. Chernikov 	int error;
554601c0b8bSAlexander V. Chernikov 
555559b4296SAlan Somers 	fibnum = inp->inp_inc.inc_fibnum;
556601c0b8bSAlexander V. Chernikov 	retifp = NULL;
557601c0b8bSAlexander V. Chernikov 
558601c0b8bSAlexander V. Chernikov 	error = in6_selectsrc(fibnum, dstsock, opts, inp, cred, &retifp, srcp);
559601c0b8bSAlexander V. Chernikov 	if (error != 0)
560601c0b8bSAlexander V. Chernikov 		return (error);
561601c0b8bSAlexander V. Chernikov 
562601c0b8bSAlexander V. Chernikov 	if (hlim != NULL)
563601c0b8bSAlexander V. Chernikov 		*hlim = in6_selecthlim(inp, retifp);
564601c0b8bSAlexander V. Chernikov 
565601c0b8bSAlexander V. Chernikov 	if (retifp == NULL || scope_ambiguous == 0)
566601c0b8bSAlexander V. Chernikov 		return (0);
567601c0b8bSAlexander V. Chernikov 
568601c0b8bSAlexander V. Chernikov 	/*
569601c0b8bSAlexander V. Chernikov 	 * Application should provide a proper zone ID or the use of
570601c0b8bSAlexander V. Chernikov 	 * default zone IDs should be enabled.  Unfortunately, some
571601c0b8bSAlexander V. Chernikov 	 * applications do not behave as it should, so we need a
572601c0b8bSAlexander V. Chernikov 	 * workaround.  Even if an appropriate ID is not determined
573601c0b8bSAlexander V. Chernikov 	 * (when it's required), if we can determine the outgoing
574601c0b8bSAlexander V. Chernikov 	 * interface. determine the zone ID based on the interface.
575601c0b8bSAlexander V. Chernikov 	 */
576601c0b8bSAlexander V. Chernikov 	error = in6_setscope(&dstsock->sin6_addr, retifp, NULL);
577601c0b8bSAlexander V. Chernikov 
578601c0b8bSAlexander V. Chernikov 	return (error);
579601c0b8bSAlexander V. Chernikov }
580601c0b8bSAlexander V. Chernikov 
581601c0b8bSAlexander V. Chernikov /*
582601c0b8bSAlexander V. Chernikov  * Select source address based on @fibnum, @dst and @scopeid.
583601c0b8bSAlexander V. Chernikov  * Stores selected address to @srcp.
584601c0b8bSAlexander V. Chernikov  * Returns 0 on success.
585601c0b8bSAlexander V. Chernikov  *
586601c0b8bSAlexander V. Chernikov  * Used by non-socket based consumers (ND code mostly)
587601c0b8bSAlexander V. Chernikov  */
588601c0b8bSAlexander V. Chernikov int
in6_selectsrc_addr(uint32_t fibnum,const struct in6_addr * dst,uint32_t scopeid,struct ifnet * ifp,struct in6_addr * srcp,int * hlim)589601c0b8bSAlexander V. Chernikov in6_selectsrc_addr(uint32_t fibnum, const struct in6_addr *dst,
590601c0b8bSAlexander V. Chernikov     uint32_t scopeid, struct ifnet *ifp, struct in6_addr *srcp,
591601c0b8bSAlexander V. Chernikov     int *hlim)
592601c0b8bSAlexander V. Chernikov {
593601c0b8bSAlexander V. Chernikov 	struct ifnet *retifp;
594601c0b8bSAlexander V. Chernikov 	struct sockaddr_in6 dst_sa;
595601c0b8bSAlexander V. Chernikov 	int error;
596601c0b8bSAlexander V. Chernikov 
597601c0b8bSAlexander V. Chernikov 	retifp = ifp;
598601c0b8bSAlexander V. Chernikov 	bzero(&dst_sa, sizeof(dst_sa));
599601c0b8bSAlexander V. Chernikov 	dst_sa.sin6_family = AF_INET6;
600601c0b8bSAlexander V. Chernikov 	dst_sa.sin6_len = sizeof(dst_sa);
601601c0b8bSAlexander V. Chernikov 	dst_sa.sin6_addr = *dst;
602601c0b8bSAlexander V. Chernikov 	dst_sa.sin6_scope_id = scopeid;
603601c0b8bSAlexander V. Chernikov 	sa6_embedscope(&dst_sa, 0);
604601c0b8bSAlexander V. Chernikov 
605601c0b8bSAlexander V. Chernikov 	error = in6_selectsrc(fibnum, &dst_sa, NULL, NULL, NULL, &retifp, srcp);
606601c0b8bSAlexander V. Chernikov 	if (hlim != NULL)
607601c0b8bSAlexander V. Chernikov 		*hlim = in6_selecthlim(NULL, retifp);
608601c0b8bSAlexander V. Chernikov 
609601c0b8bSAlexander V. Chernikov 	return (error);
610601c0b8bSAlexander V. Chernikov }
611601c0b8bSAlexander V. Chernikov 
61281a235ecSAlexander V. Chernikov static struct nhop_object *
cache_route(uint32_t fibnum,const struct sockaddr_in6 * dst,struct route_in6 * ro,uint32_t flowid)61381a235ecSAlexander V. Chernikov cache_route(uint32_t fibnum, const struct sockaddr_in6 *dst, struct route_in6 *ro,
61481a235ecSAlexander V. Chernikov     uint32_t flowid)
61581a235ecSAlexander V. Chernikov {
616601c0b8bSAlexander V. Chernikov 	/*
61781a235ecSAlexander V. Chernikov 	 * Use a cached route if it exists and is valid, else try to allocate
61881a235ecSAlexander V. Chernikov 	 * a new one. Note that we should check the address family of the
61981a235ecSAlexander V. Chernikov 	 * cached destination, in case of sharing the cache with IPv4.
62081a235ecSAlexander V. Chernikov 	 * Assumes that 'struct route_in6' is exclusively locked.
62181a235ecSAlexander V. Chernikov 	 */
62281a235ecSAlexander V. Chernikov 	if (ro->ro_nh != NULL && (
62381a235ecSAlexander V. Chernikov 	    !NH_IS_VALID(ro->ro_nh) || ro->ro_dst.sin6_family != AF_INET6 ||
62481a235ecSAlexander V. Chernikov 	    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &dst->sin6_addr)))
62581a235ecSAlexander V. Chernikov 		RO_NHFREE(ro);
62681a235ecSAlexander V. Chernikov 
62781a235ecSAlexander V. Chernikov 	if (ro->ro_nh == NULL) {
62881a235ecSAlexander V. Chernikov 		ro->ro_dst = *dst;
62981a235ecSAlexander V. Chernikov 
63081a235ecSAlexander V. Chernikov 		const struct in6_addr *paddr;
63181a235ecSAlexander V. Chernikov 		struct in6_addr unscoped_addr;
63281a235ecSAlexander V. Chernikov 		uint32_t scopeid = 0;
63381a235ecSAlexander V. Chernikov 		if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr)) {
63481a235ecSAlexander V. Chernikov 			in6_splitscope(&dst->sin6_addr, &unscoped_addr, &scopeid);
63581a235ecSAlexander V. Chernikov 			paddr = &unscoped_addr;
63681a235ecSAlexander V. Chernikov 		} else
63781a235ecSAlexander V. Chernikov 			paddr = &dst->sin6_addr;
63881a235ecSAlexander V. Chernikov 		ro->ro_nh = fib6_lookup(fibnum, paddr, scopeid, NHR_REF, flowid);
63981a235ecSAlexander V. Chernikov 	}
64081a235ecSAlexander V. Chernikov 	return (ro->ro_nh);
64181a235ecSAlexander V. Chernikov }
64281a235ecSAlexander V. Chernikov 
6432756774cSAlexander V. Chernikov static struct nhop_object *
lookup_route(uint32_t fibnum,struct sockaddr_in6 * dst,struct route_in6 * ro,struct ip6_pktopts * opts,uint32_t flowid)6442756774cSAlexander V. Chernikov lookup_route(uint32_t fibnum, struct sockaddr_in6 *dst, struct route_in6 *ro,
6452756774cSAlexander V. Chernikov     struct ip6_pktopts *opts, uint32_t flowid)
6462756774cSAlexander V. Chernikov {
6472756774cSAlexander V. Chernikov 	struct nhop_object *nh = NULL;
6482756774cSAlexander V. Chernikov 
6492756774cSAlexander V. Chernikov 	/*
6502756774cSAlexander V. Chernikov 	 * If the next hop address for the packet is specified by the caller,
6512756774cSAlexander V. Chernikov 	 * use it as the gateway.
6522756774cSAlexander V. Chernikov 	 */
6532756774cSAlexander V. Chernikov 	if (opts && opts->ip6po_nexthop) {
6542756774cSAlexander V. Chernikov 		struct route_in6 *ron = &opts->ip6po_nextroute;
6552756774cSAlexander V. Chernikov 		struct sockaddr_in6 *sin6_next = satosin6(opts->ip6po_nexthop);
6562756774cSAlexander V. Chernikov 
6572756774cSAlexander V. Chernikov 		nh = cache_route(fibnum, sin6_next, ron, flowid);
6582756774cSAlexander V. Chernikov 
6592756774cSAlexander V. Chernikov 		/*
6602756774cSAlexander V. Chernikov 		 * The node identified by that address must be a
6612756774cSAlexander V. Chernikov 		 * neighbor of the sending host.
6622756774cSAlexander V. Chernikov 		 */
6632756774cSAlexander V. Chernikov 		if (nh != NULL && (nh->nh_flags & NHF_GATEWAY) != 0)
6642756774cSAlexander V. Chernikov 			nh = NULL;
6652756774cSAlexander V. Chernikov 	} else if (ro != NULL) {
6662756774cSAlexander V. Chernikov 		nh = cache_route(fibnum, dst, ro, flowid);
6672756774cSAlexander V. Chernikov 		if (nh == NULL)
6682756774cSAlexander V. Chernikov 			return (NULL);
6692756774cSAlexander V. Chernikov 
6702756774cSAlexander V. Chernikov 		/*
6712756774cSAlexander V. Chernikov 		 * Check if the outgoing interface conflicts with
6722756774cSAlexander V. Chernikov 		 * the interface specified by ipi6_ifindex (if specified).
6732756774cSAlexander V. Chernikov 		 */
6742756774cSAlexander V. Chernikov 		struct in6_pktinfo *pi;
6752756774cSAlexander V. Chernikov 		if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
6762756774cSAlexander V. Chernikov 			if (nh->nh_aifp->if_index != pi->ipi6_ifindex)
6772756774cSAlexander V. Chernikov 				nh = NULL;
6782756774cSAlexander V. Chernikov 		}
6792756774cSAlexander V. Chernikov 	}
6802756774cSAlexander V. Chernikov 
6812756774cSAlexander V. Chernikov 	return (nh);
6822756774cSAlexander V. Chernikov }
6832756774cSAlexander V. Chernikov 
68481a235ecSAlexander V. Chernikov /*
68581a235ecSAlexander V. Chernikov  * Finds outgoing nexthop or the outgoing interface for the
68681a235ecSAlexander V. Chernikov  * @dstsock.
68781a235ecSAlexander V. Chernikov  * Return 0 on success and stores the lookup result in @retnh and @retifp
6881272577eSXin LI  */
689d6385b1cSHajimu UMEMOTO 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)6901272577eSXin LI selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
6911272577eSXin LI     struct ip6_moptions *mopts, struct route_in6 *ro,
692983066f0SAlexander V. Chernikov     struct ifnet **retifp, struct nhop_object **retnh, int norouteok,
693983066f0SAlexander V. Chernikov     u_int fibnum, uint32_t flowid)
694d6385b1cSHajimu UMEMOTO {
695d6385b1cSHajimu UMEMOTO 	int error = 0;
696d6385b1cSHajimu UMEMOTO 	struct ifnet *ifp = NULL;
697d6385b1cSHajimu UMEMOTO 	struct in6_pktinfo *pi = NULL;
698d6385b1cSHajimu UMEMOTO 	struct in6_addr *dst = &dstsock->sin6_addr;
699d6385b1cSHajimu UMEMOTO 
700d6385b1cSHajimu UMEMOTO 	/* If the caller specify the outgoing interface explicitly, use it. */
701d6385b1cSHajimu UMEMOTO 	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
702d6385b1cSHajimu UMEMOTO 		/* XXX boundary check is assumed to be already done. */
703d6385b1cSHajimu UMEMOTO 		ifp = ifnet_byindex(pi->ipi6_ifindex);
70481a235ecSAlexander V. Chernikov 		if (ifp != NULL && (norouteok || IN6_IS_ADDR_MULTICAST(dst))) {
705d6385b1cSHajimu UMEMOTO 			/*
706cd0fdcf7SHajimu UMEMOTO 			 * we do not have to check or get the route for
707d6385b1cSHajimu UMEMOTO 			 * multicast.
708d6385b1cSHajimu UMEMOTO 			 */
709d6385b1cSHajimu UMEMOTO 			goto done;
710d6385b1cSHajimu UMEMOTO 		} else
711d6385b1cSHajimu UMEMOTO 			goto getroute;
712d6385b1cSHajimu UMEMOTO 	}
713ea455de9SAndrey V. Elsukov 	/*
714d6385b1cSHajimu UMEMOTO 	 * If the destination address is a multicast address and the outgoing
715d6385b1cSHajimu UMEMOTO 	 * interface for the address is specified by the caller, use it.
716d6385b1cSHajimu UMEMOTO 	 */
717d6385b1cSHajimu UMEMOTO 	if (IN6_IS_ADDR_MULTICAST(dst) &&
718d6385b1cSHajimu UMEMOTO 	    mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) {
719d6385b1cSHajimu UMEMOTO 		goto done; /* we do not need a route for multicast. */
720d6385b1cSHajimu UMEMOTO 	}
72145d1880aSAndrey V. Elsukov 	/*
72245d1880aSAndrey V. Elsukov 	 * If destination address is LLA or link- or node-local multicast,
72345d1880aSAndrey V. Elsukov 	 * use it's embedded scope zone id to determine outgoing interface.
72445d1880aSAndrey V. Elsukov 	 */
72545d1880aSAndrey V. Elsukov 	if (IN6_IS_ADDR_MC_LINKLOCAL(dst) ||
72645d1880aSAndrey V. Elsukov 	    IN6_IS_ADDR_MC_NODELOCAL(dst)) {
7272756774cSAlexander V. Chernikov 		uint32_t zoneid = ntohs(in6_getscope(dst));
72845d1880aSAndrey V. Elsukov 		if (zoneid > 0) {
72945d1880aSAndrey V. Elsukov 			ifp = in6_getlinkifnet(zoneid);
73045d1880aSAndrey V. Elsukov 			goto done;
73145d1880aSAndrey V. Elsukov 		}
73245d1880aSAndrey V. Elsukov 	}
733d6385b1cSHajimu UMEMOTO 
7342756774cSAlexander V. Chernikov   getroute:;
7352756774cSAlexander V. Chernikov 	struct nhop_object *nh = lookup_route(fibnum, dstsock, ro, opts, flowid);
7362756774cSAlexander V. Chernikov 	if (nh != NULL) {
7372756774cSAlexander V. Chernikov 		*retifp = nh->nh_aifp;
7382756774cSAlexander V. Chernikov 		error = 0;
7392756774cSAlexander V. Chernikov 	} else {
7402756774cSAlexander V. Chernikov 		*retifp = NULL;
7412756774cSAlexander V. Chernikov 		IP6STAT_INC(ip6s_noroute);
74281a235ecSAlexander V. Chernikov 		error = EHOSTUNREACH;
743b1536a81SAndrey V. Elsukov 	}
7442756774cSAlexander V. Chernikov 	*retnh = nh;
7452756774cSAlexander V. Chernikov 	return (error);
746686cdd19SJun-ichiro itojun Hagino 
747d6385b1cSHajimu UMEMOTO   done:
7482756774cSAlexander V. Chernikov 	if (ifp == NULL) {
749d6385b1cSHajimu UMEMOTO 		/*
750d6385b1cSHajimu UMEMOTO 		 * This can happen if the caller did not pass a cached route
751d6385b1cSHajimu UMEMOTO 		 * nor any other hints.  We treat this case an error.
752d6385b1cSHajimu UMEMOTO 		 */
753d6385b1cSHajimu UMEMOTO 		error = EHOSTUNREACH;
754d6385b1cSHajimu UMEMOTO 	}
755d6385b1cSHajimu UMEMOTO 	if (error == EHOSTUNREACH)
7569cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_noroute);
757d6385b1cSHajimu UMEMOTO 
758d6385b1cSHajimu UMEMOTO 	*retifp = ifp;
7592756774cSAlexander V. Chernikov 	*retnh = NULL;
760d6385b1cSHajimu UMEMOTO 
761d6385b1cSHajimu UMEMOTO 	return (error);
762686cdd19SJun-ichiro itojun Hagino }
763686cdd19SJun-ichiro itojun Hagino 
764a1f7e5f8SHajimu UMEMOTO 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)7651272577eSXin LI in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
766357ce739SAlexander V. Chernikov     struct ip6_moptions *mopts, struct ifnet **retifp,
7675aa7e8edSBjoern A. Zeeb     struct ifnet *oifp, u_int fibnum)
768a1f7e5f8SHajimu UMEMOTO {
769a1f7e5f8SHajimu UMEMOTO 	int error;
770a1f7e5f8SHajimu UMEMOTO 	struct route_in6 sro;
771983066f0SAlexander V. Chernikov 	struct nhop_object *nh = NULL;
772983066f0SAlexander V. Chernikov 	uint16_t nh_flags;
773a1f7e5f8SHajimu UMEMOTO 
7741b46c7f8SBjoern A. Zeeb 	KASSERT(retifp != NULL, ("%s: retifp is NULL", __func__));
7751b46c7f8SBjoern A. Zeeb 
776a1f7e5f8SHajimu UMEMOTO 	bzero(&sro, sizeof(sro));
777983066f0SAlexander V. Chernikov 	nh_flags = 0;
778a1f7e5f8SHajimu UMEMOTO 
779983066f0SAlexander V. Chernikov 	error = selectroute(dstsock, opts, mopts, &sro, retifp, &nh, 1, fibnum, 0);
780357ce739SAlexander V. Chernikov 
781983066f0SAlexander V. Chernikov 	if (nh != NULL)
782983066f0SAlexander V. Chernikov 		nh_flags = nh->nh_flags;
783983066f0SAlexander V. Chernikov 	if (nh != NULL && nh == sro.ro_nh)
784983066f0SAlexander V. Chernikov 		NH_FREE(nh);
785357ce739SAlexander V. Chernikov 
786357ce739SAlexander V. Chernikov 	if (error != 0) {
7871b46c7f8SBjoern A. Zeeb 		/* Help ND. See oifp comment in in6_selectsrc(). */
7881b46c7f8SBjoern A. Zeeb 		if (oifp != NULL && fibnum == RT_DEFAULT_FIB) {
7891b46c7f8SBjoern A. Zeeb 			*retifp = oifp;
7901b46c7f8SBjoern A. Zeeb 			error = 0;
7911b46c7f8SBjoern A. Zeeb 		}
792a1f7e5f8SHajimu UMEMOTO 		return (error);
793a1f7e5f8SHajimu UMEMOTO 	}
794a1f7e5f8SHajimu UMEMOTO 
795a1f7e5f8SHajimu UMEMOTO 	/*
796a1f7e5f8SHajimu UMEMOTO 	 * do not use a rejected or black hole route.
797a1f7e5f8SHajimu UMEMOTO 	 * XXX: this check should be done in the L2 output routine.
798a1f7e5f8SHajimu UMEMOTO 	 * However, if we skipped this check here, we'd see the following
799a1f7e5f8SHajimu UMEMOTO 	 * scenario:
800a1f7e5f8SHajimu UMEMOTO 	 * - install a rejected route for a scoped address prefix
801a1f7e5f8SHajimu UMEMOTO 	 *   (like fe80::/10)
802a1f7e5f8SHajimu UMEMOTO 	 * - send a packet to a destination that matches the scoped prefix,
803a1f7e5f8SHajimu UMEMOTO 	 *   with ambiguity about the scope zone.
804a1f7e5f8SHajimu UMEMOTO 	 * - pick the outgoing interface from the route, and disambiguate the
805a1f7e5f8SHajimu UMEMOTO 	 *   scope zone with the interface.
806a1f7e5f8SHajimu UMEMOTO 	 * - ip6_output() would try to get another route with the "new"
807a1f7e5f8SHajimu UMEMOTO 	 *   destination, which may be valid.
808a1f7e5f8SHajimu UMEMOTO 	 * - we'd see no error on output.
809a1f7e5f8SHajimu UMEMOTO 	 * Although this may not be very harmful, it should still be confusing.
810a1f7e5f8SHajimu UMEMOTO 	 * We thus reject the case here.
811a1f7e5f8SHajimu UMEMOTO 	 */
812a1f7e5f8SHajimu UMEMOTO 
813983066f0SAlexander V. Chernikov 	if (nh_flags & (NHF_REJECT | NHF_BLACKHOLE)) {
814983066f0SAlexander V. Chernikov 		error = (nh_flags & NHF_HOST ? EHOSTUNREACH : ENETUNREACH);
815357ce739SAlexander V. Chernikov 		return (error);
816a1f7e5f8SHajimu UMEMOTO 	}
817a1f7e5f8SHajimu UMEMOTO 
818a1f7e5f8SHajimu UMEMOTO 	return (0);
819a1f7e5f8SHajimu UMEMOTO }
820a1f7e5f8SHajimu UMEMOTO 
8218483fce6SBjoern A. Zeeb /* Public wrapper function to selectroute(). */
822a1f7e5f8SHajimu UMEMOTO 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)8231272577eSXin LI in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
8241272577eSXin LI     struct ip6_moptions *mopts, struct route_in6 *ro,
825983066f0SAlexander V. Chernikov     struct ifnet **retifp, struct nhop_object **retnh, u_int fibnum, uint32_t flowid)
82681d5d46bSBjoern A. Zeeb {
82781a235ecSAlexander V. Chernikov 	MPASS(retifp != NULL);
82881a235ecSAlexander V. Chernikov 	MPASS(retnh != NULL);
82981d5d46bSBjoern A. Zeeb 
83081d5d46bSBjoern A. Zeeb 	return (selectroute(dstsock, opts, mopts, ro, retifp,
831983066f0SAlexander V. Chernikov 	    retnh, 0, fibnum, flowid));
83281d5d46bSBjoern A. Zeeb }
83381d5d46bSBjoern A. Zeeb 
834686cdd19SJun-ichiro itojun Hagino /*
835686cdd19SJun-ichiro itojun Hagino  * Default hop limit selection. The precedence is as follows:
836686cdd19SJun-ichiro itojun Hagino  * 1. Hoplimit value specified via ioctl.
837686cdd19SJun-ichiro itojun Hagino  * 2. (If the outgoing interface is detected) the current
838686cdd19SJun-ichiro itojun Hagino  *     hop limit of the interface specified by router advertisement.
839686cdd19SJun-ichiro itojun Hagino  * 3. The system default hoplimit.
840686cdd19SJun-ichiro itojun Hagino  */
841686cdd19SJun-ichiro itojun Hagino int
in6_selecthlim(struct inpcb * inp,struct ifnet * ifp)8420ecd976eSBjoern A. Zeeb in6_selecthlim(struct inpcb *inp, struct ifnet *ifp)
843686cdd19SJun-ichiro itojun Hagino {
8441272577eSXin LI 
8450ecd976eSBjoern A. Zeeb 	if (inp && inp->in6p_hops >= 0)
8460ecd976eSBjoern A. Zeeb 		return (inp->in6p_hops);
847686cdd19SJun-ichiro itojun Hagino 	else if (ifp)
84831b1bfe1SHajimu UMEMOTO 		return (ND_IFINFO(ifp)->chlim);
8490ecd976eSBjoern A. Zeeb 	else if (inp && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
8506ad7446cSAlexander V. Chernikov 		struct nhop_object *nh;
8519977be4aSAlexander V. Chernikov 		struct in6_addr dst;
8529977be4aSAlexander V. Chernikov 		uint32_t fibnum, scopeid;
8539977be4aSAlexander V. Chernikov 		int hlim;
85497d8d152SAndre Oppermann 
8550ecd976eSBjoern A. Zeeb 		fibnum = inp->inp_inc.inc_fibnum;
8560ecd976eSBjoern A. Zeeb 		in6_splitscope(&inp->in6p_faddr, &dst, &scopeid);
8576ad7446cSAlexander V. Chernikov 		nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
8586ad7446cSAlexander V. Chernikov 		if (nh != NULL) {
8596ad7446cSAlexander V. Chernikov 			hlim = ND_IFINFO(nh->nh_ifp)->chlim;
8609977be4aSAlexander V. Chernikov 			return (hlim);
861f8315b5fSBjoern A. Zeeb 		}
86297d8d152SAndre Oppermann 	}
863603724d3SBjoern A. Zeeb 	return (V_ip6_defhlim);
864686cdd19SJun-ichiro itojun Hagino }
865686cdd19SJun-ichiro itojun Hagino 
8667fc91b3fSHajimu UMEMOTO void
addrsel_policy_init(void)8671272577eSXin LI addrsel_policy_init(void)
8687fc91b3fSHajimu UMEMOTO {
8697fc91b3fSHajimu UMEMOTO 
8707fc91b3fSHajimu UMEMOTO 	init_policy_queue();
8717fc91b3fSHajimu UMEMOTO 
8727fc91b3fSHajimu UMEMOTO 	/* initialize the "last resort" policy */
873603724d3SBjoern A. Zeeb 	bzero(&V_defaultaddrpolicy, sizeof(V_defaultaddrpolicy));
874603724d3SBjoern A. Zeeb 	V_defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
8751ed81b73SMarko Zec 
8761ed81b73SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
8771ed81b73SMarko Zec 		return;
8781ed81b73SMarko Zec 
8791ed81b73SMarko Zec 	ADDRSEL_LOCK_INIT();
8801ed81b73SMarko Zec 	ADDRSEL_SXLOCK_INIT();
8817fc91b3fSHajimu UMEMOTO }
8827fc91b3fSHajimu UMEMOTO 
883d6385b1cSHajimu UMEMOTO static struct in6_addrpolicy *
lookup_addrsel_policy(struct sockaddr_in6 * key)8841272577eSXin LI lookup_addrsel_policy(struct sockaddr_in6 *key)
885d6385b1cSHajimu UMEMOTO {
886d6385b1cSHajimu UMEMOTO 	struct in6_addrpolicy *match = NULL;
887d6385b1cSHajimu UMEMOTO 
888d6385b1cSHajimu UMEMOTO 	ADDRSEL_LOCK();
889d6385b1cSHajimu UMEMOTO 	match = match_addrsel_policy(key);
890d6385b1cSHajimu UMEMOTO 
891d6385b1cSHajimu UMEMOTO 	if (match == NULL)
892603724d3SBjoern A. Zeeb 		match = &V_defaultaddrpolicy;
893d6385b1cSHajimu UMEMOTO 	else
894d6385b1cSHajimu UMEMOTO 		match->use++;
895d6385b1cSHajimu UMEMOTO 	ADDRSEL_UNLOCK();
896d6385b1cSHajimu UMEMOTO 
897d6385b1cSHajimu UMEMOTO 	return (match);
898d6385b1cSHajimu UMEMOTO }
899d6385b1cSHajimu UMEMOTO 
9007fc91b3fSHajimu UMEMOTO /*
9017fc91b3fSHajimu UMEMOTO  * Subroutines to manage the address selection policy table via sysctl.
9027fc91b3fSHajimu UMEMOTO  */
9037fc91b3fSHajimu UMEMOTO struct walkarg {
9047fc91b3fSHajimu UMEMOTO 	struct sysctl_req *w_req;
9057fc91b3fSHajimu UMEMOTO };
9067fc91b3fSHajimu UMEMOTO 
9077fc91b3fSHajimu UMEMOTO static int in6_src_sysctl(SYSCTL_HANDLER_ARGS);
9087fc91b3fSHajimu UMEMOTO SYSCTL_DECL(_net_inet6_ip6);
9096472ac3dSEd Schouten static SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy,
9107029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, in6_src_sysctl,
9117029da5cSPawel Biernacki     "");
9127fc91b3fSHajimu UMEMOTO 
9137fc91b3fSHajimu UMEMOTO static int
in6_src_sysctl(SYSCTL_HANDLER_ARGS)9147fc91b3fSHajimu UMEMOTO in6_src_sysctl(SYSCTL_HANDLER_ARGS)
9157fc91b3fSHajimu UMEMOTO {
9167fc91b3fSHajimu UMEMOTO 	struct walkarg w;
9177fc91b3fSHajimu UMEMOTO 
9187fc91b3fSHajimu UMEMOTO 	if (req->newptr)
9197fc91b3fSHajimu UMEMOTO 		return EPERM;
9207fc91b3fSHajimu UMEMOTO 
9217fc91b3fSHajimu UMEMOTO 	bzero(&w, sizeof(w));
9227fc91b3fSHajimu UMEMOTO 	w.w_req = req;
9237fc91b3fSHajimu UMEMOTO 
9247fc91b3fSHajimu UMEMOTO 	return (walk_addrsel_policy(dump_addrsel_policyent, &w));
9257fc91b3fSHajimu UMEMOTO }
9267fc91b3fSHajimu UMEMOTO 
9277fc91b3fSHajimu UMEMOTO int
in6_src_ioctl(u_long cmd,caddr_t data)9281272577eSXin LI in6_src_ioctl(u_long cmd, caddr_t data)
9297fc91b3fSHajimu UMEMOTO {
9307fc91b3fSHajimu UMEMOTO 	struct in6_addrpolicy ent0;
9317fc91b3fSHajimu UMEMOTO 
9327fc91b3fSHajimu UMEMOTO 	if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
9337fc91b3fSHajimu UMEMOTO 		return (EOPNOTSUPP); /* check for safety */
9347fc91b3fSHajimu UMEMOTO 
9357fc91b3fSHajimu UMEMOTO 	ent0 = *(struct in6_addrpolicy *)data;
9367fc91b3fSHajimu UMEMOTO 
9377fc91b3fSHajimu UMEMOTO 	if (ent0.label == ADDR_LABEL_NOTAPP)
9387fc91b3fSHajimu UMEMOTO 		return (EINVAL);
9397fc91b3fSHajimu UMEMOTO 	/* check if the prefix mask is consecutive. */
9407fc91b3fSHajimu UMEMOTO 	if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
9417fc91b3fSHajimu UMEMOTO 		return (EINVAL);
9427fc91b3fSHajimu UMEMOTO 	/* clear trailing garbages (if any) of the prefix address. */
9431dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&ent0.addr.sin6_addr, &ent0.addrmask.sin6_addr);
9447fc91b3fSHajimu UMEMOTO 	ent0.use = 0;
9457fc91b3fSHajimu UMEMOTO 
9467fc91b3fSHajimu UMEMOTO 	switch (cmd) {
9477fc91b3fSHajimu UMEMOTO 	case SIOCAADDRCTL_POLICY:
9487fc91b3fSHajimu UMEMOTO 		return (add_addrsel_policyent(&ent0));
9497fc91b3fSHajimu UMEMOTO 	case SIOCDADDRCTL_POLICY:
9507fc91b3fSHajimu UMEMOTO 		return (delete_addrsel_policyent(&ent0));
9517fc91b3fSHajimu UMEMOTO 	}
9527fc91b3fSHajimu UMEMOTO 
9537fc91b3fSHajimu UMEMOTO 	return (0);		/* XXX: compromise compilers */
9547fc91b3fSHajimu UMEMOTO }
9557fc91b3fSHajimu UMEMOTO 
9567fc91b3fSHajimu UMEMOTO /*
9577fc91b3fSHajimu UMEMOTO  * The followings are implementation of the policy table using a
9587fc91b3fSHajimu UMEMOTO  * simple tail queue.
9597fc91b3fSHajimu UMEMOTO  * XXX such details should be hidden.
9607fc91b3fSHajimu UMEMOTO  * XXX implementation using binary tree should be more efficient.
9617fc91b3fSHajimu UMEMOTO  */
9627fc91b3fSHajimu UMEMOTO struct addrsel_policyent {
9637fc91b3fSHajimu UMEMOTO 	TAILQ_ENTRY(addrsel_policyent) ape_entry;
9647fc91b3fSHajimu UMEMOTO 	struct in6_addrpolicy ape_policy;
9657fc91b3fSHajimu UMEMOTO };
9667fc91b3fSHajimu UMEMOTO 
9677fc91b3fSHajimu UMEMOTO TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
9687fc91b3fSHajimu UMEMOTO 
9695f901c92SAndrew Turner VNET_DEFINE_STATIC(struct addrsel_policyhead, addrsel_policytab);
9701e77c105SRobert Watson #define	V_addrsel_policytab		VNET(addrsel_policytab)
9717fc91b3fSHajimu UMEMOTO 
9727fc91b3fSHajimu UMEMOTO static void
init_policy_queue(void)9731272577eSXin LI init_policy_queue(void)
9747fc91b3fSHajimu UMEMOTO {
9751272577eSXin LI 
976603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_addrsel_policytab);
9777fc91b3fSHajimu UMEMOTO }
9787fc91b3fSHajimu UMEMOTO 
9797fc91b3fSHajimu UMEMOTO static int
add_addrsel_policyent(struct in6_addrpolicy * newpolicy)9801272577eSXin LI add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
9817fc91b3fSHajimu UMEMOTO {
9827fc91b3fSHajimu UMEMOTO 	struct addrsel_policyent *new, *pol;
9837fc91b3fSHajimu UMEMOTO 
9841ede983cSDag-Erling Smørgrav 	new = malloc(sizeof(*new), M_IFADDR,
985349b668aSHajimu UMEMOTO 	       M_WAITOK);
9865d525653SHajimu UMEMOTO 	ADDRSEL_XLOCK();
9877fc91b3fSHajimu UMEMOTO 	ADDRSEL_LOCK();
9887fc91b3fSHajimu UMEMOTO 
9897fc91b3fSHajimu UMEMOTO 	/* duplication check */
990603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
991c1a049acSSUZUKI Shinsuke 		if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
992c1a049acSSUZUKI Shinsuke 				       &pol->ape_policy.addr.sin6_addr) &&
993c1a049acSSUZUKI Shinsuke 		    IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
994c1a049acSSUZUKI Shinsuke 				       &pol->ape_policy.addrmask.sin6_addr)) {
995349b668aSHajimu UMEMOTO 			ADDRSEL_UNLOCK();
9965d525653SHajimu UMEMOTO 			ADDRSEL_XUNLOCK();
9971ede983cSDag-Erling Smørgrav 			free(new, M_IFADDR);
9987fc91b3fSHajimu UMEMOTO 			return (EEXIST);	/* or override it? */
9997fc91b3fSHajimu UMEMOTO 		}
10007fc91b3fSHajimu UMEMOTO 	}
10017fc91b3fSHajimu UMEMOTO 
10027fc91b3fSHajimu UMEMOTO 	bzero(new, sizeof(*new));
10037fc91b3fSHajimu UMEMOTO 
10047fc91b3fSHajimu UMEMOTO 	/* XXX: should validate entry */
10057fc91b3fSHajimu UMEMOTO 	new->ape_policy = *newpolicy;
10067fc91b3fSHajimu UMEMOTO 
1007603724d3SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&V_addrsel_policytab, new, ape_entry);
10087fc91b3fSHajimu UMEMOTO 	ADDRSEL_UNLOCK();
10095d525653SHajimu UMEMOTO 	ADDRSEL_XUNLOCK();
10107fc91b3fSHajimu UMEMOTO 
10117fc91b3fSHajimu UMEMOTO 	return (0);
10127fc91b3fSHajimu UMEMOTO }
10137fc91b3fSHajimu UMEMOTO 
10147fc91b3fSHajimu UMEMOTO static int
delete_addrsel_policyent(struct in6_addrpolicy * key)10151272577eSXin LI delete_addrsel_policyent(struct in6_addrpolicy *key)
10167fc91b3fSHajimu UMEMOTO {
10177fc91b3fSHajimu UMEMOTO 	struct addrsel_policyent *pol;
10187fc91b3fSHajimu UMEMOTO 
10195d525653SHajimu UMEMOTO 	ADDRSEL_XLOCK();
10207fc91b3fSHajimu UMEMOTO 	ADDRSEL_LOCK();
10217fc91b3fSHajimu UMEMOTO 
10227fc91b3fSHajimu UMEMOTO 	/* search for the entry in the table */
1023603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
1024c1a049acSSUZUKI Shinsuke 		if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
1025c1a049acSSUZUKI Shinsuke 		    &pol->ape_policy.addr.sin6_addr) &&
1026c1a049acSSUZUKI Shinsuke 		    IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
1027c1a049acSSUZUKI Shinsuke 		    &pol->ape_policy.addrmask.sin6_addr)) {
10287fc91b3fSHajimu UMEMOTO 			break;
10297fc91b3fSHajimu UMEMOTO 		}
10307fc91b3fSHajimu UMEMOTO 	}
1031349b668aSHajimu UMEMOTO 	if (pol == NULL) {
1032349b668aSHajimu UMEMOTO 		ADDRSEL_UNLOCK();
10335d525653SHajimu UMEMOTO 		ADDRSEL_XUNLOCK();
10347fc91b3fSHajimu UMEMOTO 		return (ESRCH);
1035349b668aSHajimu UMEMOTO 	}
10367fc91b3fSHajimu UMEMOTO 
1037603724d3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_addrsel_policytab, pol, ape_entry);
10387fc91b3fSHajimu UMEMOTO 	ADDRSEL_UNLOCK();
10395d525653SHajimu UMEMOTO 	ADDRSEL_XUNLOCK();
1040ecc5c738SAndrey V. Elsukov 	free(pol, M_IFADDR);
10417fc91b3fSHajimu UMEMOTO 
10427fc91b3fSHajimu UMEMOTO 	return (0);
10437fc91b3fSHajimu UMEMOTO }
10447fc91b3fSHajimu UMEMOTO 
10457fc91b3fSHajimu UMEMOTO static int
walk_addrsel_policy(int (* callback)(struct in6_addrpolicy *,void *),void * w)1046f1e0de69SDavid E. O'Brien walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), void *w)
10477fc91b3fSHajimu UMEMOTO {
10487fc91b3fSHajimu UMEMOTO 	struct addrsel_policyent *pol;
10497fc91b3fSHajimu UMEMOTO 	int error = 0;
10507fc91b3fSHajimu UMEMOTO 
10515d525653SHajimu UMEMOTO 	ADDRSEL_SLOCK();
1052603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
10535d525653SHajimu UMEMOTO 		if ((error = (*callback)(&pol->ape_policy, w)) != 0) {
10545d525653SHajimu UMEMOTO 			ADDRSEL_SUNLOCK();
10557fc91b3fSHajimu UMEMOTO 			return (error);
1056349b668aSHajimu UMEMOTO 		}
10575d525653SHajimu UMEMOTO 	}
10585d525653SHajimu UMEMOTO 	ADDRSEL_SUNLOCK();
10597fc91b3fSHajimu UMEMOTO 	return (error);
10607fc91b3fSHajimu UMEMOTO }
10617fc91b3fSHajimu UMEMOTO 
10627fc91b3fSHajimu UMEMOTO static int
dump_addrsel_policyent(struct in6_addrpolicy * pol,void * arg)10631272577eSXin LI dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
10647fc91b3fSHajimu UMEMOTO {
10657fc91b3fSHajimu UMEMOTO 	int error = 0;
10667fc91b3fSHajimu UMEMOTO 	struct walkarg *w = arg;
10677fc91b3fSHajimu UMEMOTO 
10687fc91b3fSHajimu UMEMOTO 	error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol));
10697fc91b3fSHajimu UMEMOTO 
10707fc91b3fSHajimu UMEMOTO 	return (error);
10717fc91b3fSHajimu UMEMOTO }
1072d6385b1cSHajimu UMEMOTO 
1073d6385b1cSHajimu UMEMOTO static struct in6_addrpolicy *
match_addrsel_policy(struct sockaddr_in6 * key)10741272577eSXin LI match_addrsel_policy(struct sockaddr_in6 *key)
1075d6385b1cSHajimu UMEMOTO {
1076d6385b1cSHajimu UMEMOTO 	struct addrsel_policyent *pent;
1077d6385b1cSHajimu UMEMOTO 	struct in6_addrpolicy *bestpol = NULL, *pol;
1078d6385b1cSHajimu UMEMOTO 	int matchlen, bestmatchlen = -1;
1079d6385b1cSHajimu UMEMOTO 	u_char *mp, *ep, *k, *p, m;
1080d6385b1cSHajimu UMEMOTO 
1081603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(pent, &V_addrsel_policytab, ape_entry) {
1082d6385b1cSHajimu UMEMOTO 		matchlen = 0;
1083d6385b1cSHajimu UMEMOTO 
1084d6385b1cSHajimu UMEMOTO 		pol = &pent->ape_policy;
1085d6385b1cSHajimu UMEMOTO 		mp = (u_char *)&pol->addrmask.sin6_addr;
1086d6385b1cSHajimu UMEMOTO 		ep = mp + 16;	/* XXX: scope field? */
1087d6385b1cSHajimu UMEMOTO 		k = (u_char *)&key->sin6_addr;
1088d6385b1cSHajimu UMEMOTO 		p = (u_char *)&pol->addr.sin6_addr;
1089d6385b1cSHajimu UMEMOTO 		for (; mp < ep && *mp; mp++, k++, p++) {
1090d6385b1cSHajimu UMEMOTO 			m = *mp;
1091d6385b1cSHajimu UMEMOTO 			if ((*k & m) != *p)
1092d6385b1cSHajimu UMEMOTO 				goto next; /* not match */
1093d6385b1cSHajimu UMEMOTO 			if (m == 0xff) /* short cut for a typical case */
1094d6385b1cSHajimu UMEMOTO 				matchlen += 8;
1095d6385b1cSHajimu UMEMOTO 			else {
1096d6385b1cSHajimu UMEMOTO 				while (m >= 0x80) {
1097d6385b1cSHajimu UMEMOTO 					matchlen++;
1098d6385b1cSHajimu UMEMOTO 					m <<= 1;
1099d6385b1cSHajimu UMEMOTO 				}
1100d6385b1cSHajimu UMEMOTO 			}
1101d6385b1cSHajimu UMEMOTO 		}
1102d6385b1cSHajimu UMEMOTO 
1103d6385b1cSHajimu UMEMOTO 		/* matched.  check if this is better than the current best. */
1104d6385b1cSHajimu UMEMOTO 		if (bestpol == NULL ||
1105d6385b1cSHajimu UMEMOTO 		    matchlen > bestmatchlen) {
1106d6385b1cSHajimu UMEMOTO 			bestpol = pol;
1107d6385b1cSHajimu UMEMOTO 			bestmatchlen = matchlen;
1108d6385b1cSHajimu UMEMOTO 		}
1109d6385b1cSHajimu UMEMOTO 
1110d6385b1cSHajimu UMEMOTO 	  next:
1111d6385b1cSHajimu UMEMOTO 		continue;
1112d6385b1cSHajimu UMEMOTO 	}
1113d6385b1cSHajimu UMEMOTO 
1114d6385b1cSHajimu UMEMOTO 	return (bestpol);
1115d6385b1cSHajimu UMEMOTO }
1116