1/*	$NetBSD: in6_src.c,v 1.92 2023/08/03 04:24:55 ozaki-r Exp $	*/
2/*	$KAME: in6_src.c,v 1.159 2005/10/19 01:40:32 t-momose Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 *	The Regents of the University of California.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *	This product includes software developed by the University of
48 *	California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 *    may be used to endorse or promote products derived from this software
51 *    without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
66 */
67
68#include <sys/cdefs.h>
69__KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.92 2023/08/03 04:24:55 ozaki-r Exp $");
70
71#ifdef _KERNEL_OPT
72#include "opt_inet.h"
73#endif
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/malloc.h>
78#include <sys/mbuf.h>
79#include <sys/protosw.h>
80#include <sys/socket.h>
81#include <sys/socketvar.h>
82#include <sys/ioctl.h>
83#include <sys/errno.h>
84#include <sys/time.h>
85#include <sys/kernel.h>
86#include <sys/proc.h>
87#include <sys/kauth.h>
88
89#include <net/if.h>
90#include <net/if_types.h>
91#include <net/route.h>
92
93#include <netinet/in.h>
94#include <netinet/in_var.h>
95#include <netinet/in_systm.h>
96#include <netinet/ip.h>
97#include <netinet/in_pcb.h>
98#include <netinet/portalgo.h>
99#include <netinet6/in6_var.h>
100#include <netinet/ip6.h>
101#include <netinet6/in6_pcb.h>
102#include <netinet6/ip6_var.h>
103#include <netinet6/ip6_private.h>
104#include <netinet6/nd6.h>
105#include <netinet6/scope6_var.h>
106
107#ifdef MIP6
108#include <netinet6/mip6.h>
109#include <netinet6/mip6_var.h>
110#include "mip.h"
111#if NMIP > 0
112#include <net/if_mip.h>
113#endif /* NMIP > 0 */
114#endif /* MIP6 */
115
116#include <netinet/tcp_vtw.h>
117
118#define ADDR_LABEL_NOTAPP (-1)
119struct in6_addrpolicy defaultaddrpolicy;
120
121int ip6_prefer_tempaddr = 0;
122
123static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
124	struct ip6_moptions *, struct route *, struct ifnet **, struct psref *);
125
126static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *);
127
128static void init_policy_queue(void);
129static int add_addrsel_policyent(struct in6_addrpolicy *);
130static int delete_addrsel_policyent(struct in6_addrpolicy *);
131static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
132				    void *);
133static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
134static struct in6_addrpolicy *match_addrsel_policy(struct sockaddr_in6 *);
135
136#define	IFA6_IS_VALIDATED(ia) \
137	(((ia)->ia6_flags & (IN6_IFF_TENTATIVE | IN6_IFF_DETACHED)) == 0)
138
139/*
140 * Return an IPv6 address, which is the most appropriate for a given
141 * destination and user specified options.
142 * If necessary, this function lookups the routing table and returns
143 * an entry to the caller for later use.
144 */
145#if 0				/* disabled ad-hoc */
146#define REPLACE(r) do {\
147	char _buf1[INET6_ADDRSTRLEN], _buf2[INET6_ADDRSTRLEN]; \
148	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
149		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
150		ip6stat.ip6s_sources_rule[(r)]++; \
151	printf("%s: replace %s with %s by %d\n", __func__, ia_best ? \
152	    IN6_PRINT(_buf1, &ia_best->ia_addr.sin6_addr) : "none", \
153	    IN6_PRINT(_buf2, &ia->ia_addr.sin6_addr), (r)); \
154	goto replace; \
155} while(/*CONSTCOND*/0)
156#define NEXT(r) do {\
157	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
158		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
159		ip6stat.ip6s_sources_rule[(r)]++; \
160	printf("%s: keep %s against %s by %d\n", ia_best ? \
161	    IN6_PRINT(_buf1, &ia_best->ia_addr.sin6_addr) : "none", \
162	    IN6_PRINT(_buf2, &ia->ia_addr.sin6_addr), (r)); \
163	goto next; 		/* XXX: we can't use 'continue' here */ \
164} while(/*CONSTCOND*/0)
165#define BREAK(r) do { \
166	if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
167		sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
168		ip6stat.ip6s_sources_rule[(r)]++; \
169	goto out; 		/* XXX: we can't use 'break' here */ \
170} while(/*CONSTCOND*/0)
171#else
172#define REPLACE(r) goto replace
173#define NEXT(r) goto next
174#define BREAK(r) goto out
175#endif
176
177/*
178 * Called inside pserialize critical section. Don't sleep/block.
179 */
180static struct in6_ifaddr *
181in6_select_best_ia(struct sockaddr_in6 *dstsock, struct in6_addr *dst,
182    const struct ifnet *ifp, const struct ip6_pktopts *opts,
183    const u_int32_t odstzone)
184{
185	struct in6_ifaddr *ia, *ia_best = NULL;
186	int dst_scope = -1, best_scope = -1, best_matchlen = -1;
187	struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL;
188
189	IN6_ADDRLIST_READER_FOREACH(ia) {
190		int new_scope = -1, new_matchlen = -1;
191		struct in6_addrpolicy *new_policy = NULL;
192		u_int32_t srczone, osrczone, dstzone;
193		struct in6_addr src;
194		struct ifnet *ifp1 = ia->ia_ifp;
195		int prefer_tempaddr;
196
197		/*
198		 * We'll never take an address that breaks the scope zone
199		 * of the destination.  We also skip an address if its zone
200		 * does not contain the outgoing interface.
201		 * XXX: we should probably use sin6_scope_id here.
202		 */
203		if (in6_setscope(dst, ifp1, &dstzone) ||
204		    odstzone != dstzone) {
205			continue;
206		}
207		src = ia->ia_addr.sin6_addr;
208
209		/* Skip the scope test in impossible cases */
210		if (!(ifp->if_flags & IFF_LOOPBACK) &&
211		    IN6_IS_ADDR_LOOPBACK(&src))
212			continue;
213
214		if (in6_setscope(&src, ifp, &osrczone) ||
215		    in6_setscope(&src, ifp1, &srczone) ||
216		    osrczone != srczone) {
217			continue;
218		}
219
220		/* avoid unusable addresses */
221		if ((ia->ia6_flags & (IN6_IFF_DUPLICATED | IN6_IFF_ANYCAST)))
222			continue;
223		if (!ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
224			continue;
225
226#if defined(MIP6) && NMIP > 0
227		/* avoid unusable home addresses. */
228		if ((ia->ia6_flags & IN6_IFF_HOME) &&
229		    !mip6_ifa6_is_addr_valid_hoa(ia))
230			continue;
231#endif /* MIP6 && NMIP > 0 */
232
233		/* Rule 1: Prefer same address */
234		if (IN6_ARE_ADDR_EQUAL(dst, &ia->ia_addr.sin6_addr)) {
235			ia_best = ia;
236			BREAK(1); /* there should be no better candidate */
237		}
238
239		if (ia_best == NULL)
240			REPLACE(1);
241
242		/* Rule 2: Prefer appropriate scope */
243		if (dst_scope < 0)
244			dst_scope = in6_addrscope(dst);
245		new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
246		if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
247			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
248				REPLACE(2);
249			NEXT(2);
250		} else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
251			if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
252				NEXT(2);
253			REPLACE(2);
254		}
255
256		/*
257		 * Rule 3: Avoid deprecated addresses.  Note that the case of
258		 * !ip6_use_deprecated is already rejected above.
259		 * Treat unvalidated addresses as deprecated here.
260		 */
261		if (IFA6_IS_VALIDATED(ia_best) && !IFA6_IS_VALIDATED(ia))
262			NEXT(3);
263		if (!IFA6_IS_VALIDATED(ia_best) && IFA6_IS_VALIDATED(ia))
264			REPLACE(3);
265		if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
266			NEXT(3);
267		if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
268			REPLACE(3);
269
270		/* Rule 4: Prefer home addresses */
271#if defined(MIP6) && NMIP > 0
272		if (!MIP6_IS_MN)
273			goto skip_rule4;
274
275		if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
276		    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
277			/* both address are not home addresses. */
278			goto skip_rule4;
279		}
280
281		/*
282		 * If SA is simultaneously a home address and care-of
283		 * address and SB is not, then prefer SA. Similarly,
284		 * if SB is simultaneously a home address and care-of
285		 * address and SA is not, then prefer SB.
286		 */
287		if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
288			ia_best->ia_ifp->if_type != IFT_MIP)
289		    &&
290		    ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
291			ia->ia_ifp->if_type == IFT_MIP))
292			NEXT(4);
293		if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
294			ia_best->ia_ifp->if_type == IFT_MIP)
295		    &&
296		    ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
297			ia->ia_ifp->if_type != IFT_MIP))
298			REPLACE(4);
299		if (ip6po_usecoa == 0) {
300			/*
301			 * If SA is just a home address and SB is just
302			 * a care-of address, then prefer
303			 * SA. Similarly, if SB is just a home address
304			 * and SA is just a care-of address, then
305			 * prefer SB.
306			 */
307			if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
308			    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
309				NEXT(4);
310			}
311			if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
312			    (ia->ia6_flags & IN6_IFF_HOME) != 0) {
313				REPLACE(4);
314			}
315		} else {
316			/*
317			 * a sender don't want to use a home address
318			 * because:
319			 *
320			 * 1) we cannot use.  (ex. NS or NA to global
321			 * addresses.)
322			 *
323			 * 2) a user specified not to use.
324			 * (ex. mip6control -u)
325			 */
326			if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
327			    (ia->ia6_flags & IN6_IFF_HOME) != 0) {
328				/* XXX breaks stat */
329				NEXT(0);
330			}
331			if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
332			    (ia->ia6_flags & IN6_IFF_HOME) == 0) {
333				/* XXX breaks stat */
334				REPLACE(0);
335			}
336		}
337	skip_rule4:
338#endif /* MIP6 && NMIP > 0 */
339
340		/* Rule 5: Prefer outgoing interface */
341		if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
342			NEXT(5);
343		if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
344			REPLACE(5);
345
346		/*
347		 * Rule 6: Prefer matching label
348		 * Note that best_policy should be non-NULL here.
349		 */
350		if (dst_policy == NULL)
351			dst_policy = lookup_addrsel_policy(dstsock);
352		if (dst_policy->label != ADDR_LABEL_NOTAPP) {
353			new_policy = lookup_addrsel_policy(&ia->ia_addr);
354			if (dst_policy->label == best_policy->label &&
355			    dst_policy->label != new_policy->label)
356				NEXT(6);
357			if (dst_policy->label != best_policy->label &&
358			    dst_policy->label == new_policy->label)
359				REPLACE(6);
360		}
361
362		/*
363		 * Rule 7: Prefer public addresses.
364		 * We allow users to reverse the logic by configuring
365		 * a sysctl variable, so that privacy conscious users can
366		 * always prefer temporary addresses.
367		 */
368		if (opts == NULL ||
369		    opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
370			prefer_tempaddr = ip6_prefer_tempaddr;
371		} else if (opts->ip6po_prefer_tempaddr ==
372		    IP6PO_TEMPADDR_NOTPREFER) {
373			prefer_tempaddr = 0;
374		} else
375			prefer_tempaddr = 1;
376		if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
377		    (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
378			if (prefer_tempaddr)
379				REPLACE(7);
380			else
381				NEXT(7);
382		}
383		if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
384		    !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
385			if (prefer_tempaddr)
386				NEXT(7);
387			else
388				REPLACE(7);
389		}
390
391		/*
392		 * Rule 8: prefer addresses on alive interfaces.
393		 * This is a KAME specific rule.
394		 */
395		if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
396		    !(ia->ia_ifp->if_flags & IFF_UP))
397			NEXT(8);
398		if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
399		    (ia->ia_ifp->if_flags & IFF_UP))
400			REPLACE(8);
401
402		/*
403		 * Rule 9: prefer addresses on "preferred" interfaces.
404		 * This is a KAME specific rule.
405		 */
406#ifdef notyet			/* until introducing address selection */
407#define NDI_BEST ND_IFINFO(ia_best->ia_ifp)
408#define NDI_NEW  ND_IFINFO(ia->ia_ifp)
409		if ((NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
410		    !(NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
411			NEXT(9);
412		if (!(NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
413		    (NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
414			REPLACE(9);
415#undef NDI_BEST
416#undef NDI_NEW
417#endif
418
419		/*
420		 * Rule 14: Use longest matching prefix.
421		 * Note: in the address selection draft, this rule is
422		 * documented as "Rule 8".  However, since it is also
423		 * documented that this rule can be overridden, we assign
424		 * a large number so that it is easy to assign smaller numbers
425		 * to more preferred rules.
426		 */
427		new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, dst);
428		if (best_matchlen < new_matchlen)
429			REPLACE(14);
430		if (new_matchlen < best_matchlen)
431			NEXT(14);
432
433		/* Rule 15 is reserved. */
434
435		/*
436		 * Last resort: just keep the current candidate.
437		 * Or, do we need more rules?
438		 */
439		continue;
440
441	  replace:
442		ia_best = ia;
443		best_scope = (new_scope >= 0 ? new_scope :
444			      in6_addrscope(&ia_best->ia_addr.sin6_addr));
445		best_policy = (new_policy ? new_policy :
446			       lookup_addrsel_policy(&ia_best->ia_addr));
447		best_matchlen = (new_matchlen >= 0 ? new_matchlen :
448				 in6_matchlen(&ia_best->ia_addr.sin6_addr,
449					      dst));
450
451	  next:
452		continue;
453
454	  out:
455		break;
456	}
457
458	return ia_best;
459}
460#undef REPLACE
461#undef BREAK
462#undef NEXT
463
464int
465in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
466	struct ip6_moptions *mopts, struct route *ro, struct in6_addr *laddr,
467	struct ifnet **ifpp, struct psref *psref, struct in6_addr *ret_ia6)
468{
469	struct in6_addr dst;
470	struct ifnet *ifp = NULL;
471	struct in6_ifaddr *ia = NULL;
472	struct in6_pktinfo *pi = NULL;
473	u_int32_t odstzone;
474	int error = 0, iferror;
475#if defined(MIP6) && NMIP > 0
476	u_int8_t ip6po_usecoa = 0;
477#endif /* MIP6 && NMIP > 0 */
478	struct psref local_psref;
479	int bound = curlwp_bind();
480#define PSREF (psref == NULL) ? &local_psref : psref
481	int s;
482
483	KASSERT((ifpp != NULL && psref != NULL) ||
484	        (ifpp == NULL && psref == NULL));
485
486	dst = dstsock->sin6_addr; /* make a copy for local operation */
487	if (ifpp)
488		*ifpp = NULL;
489
490	/*
491	 * Try to determine the outgoing interface for the given destination.
492	 * We do this regardless of whether the socket is bound, since the
493	 * caller may need this information as a side effect of the call
494	 * to this function (e.g., for identifying the appropriate scope zone
495	 * ID).
496	 */
497	iferror = in6_selectif(dstsock, opts, mopts, ro, &ifp, PSREF);
498	if (ifpp != NULL)
499		*ifpp = ifp;
500
501	/*
502	 * If the source address is explicitly specified by the caller,
503	 * check if the requested source address is indeed a unicast address
504	 * assigned to the node, and can be used as the packet's source
505	 * address.  If everything is okay, use the address as source.
506	 */
507	if (opts && (pi = opts->ip6po_pktinfo) &&
508	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
509		struct sockaddr_in6 srcsock;
510		struct in6_ifaddr *ia6;
511		int _s;
512		struct ifaddr *ifa;
513
514		/*
515		 * Determine the appropriate zone id of the source based on
516		 * the zone of the destination and the outgoing interface.
517		 * If the specified address is ambiguous wrt the scope zone,
518		 * the interface must be specified; otherwise, ifa_ifwithaddr()
519		 * will fail matching the address.
520		 */
521		memset(&srcsock, 0, sizeof(srcsock));
522		srcsock.sin6_family = AF_INET6;
523		srcsock.sin6_len = sizeof(srcsock);
524		srcsock.sin6_addr = pi->ipi6_addr;
525		if (ifp) {
526			error = in6_setscope(&srcsock.sin6_addr, ifp, NULL);
527			if (error != 0)
528				goto exit;
529		}
530
531		_s = pserialize_read_enter();
532		ifa = ifa_ifwithaddr(sin6tosa(&srcsock));
533		if ((ia6 = ifatoia6(ifa)) == NULL ||
534		    ia6->ia6_flags &
535		    (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)) {
536			pserialize_read_exit(_s);
537			error = EADDRNOTAVAIL;
538			goto exit;
539		}
540		pi->ipi6_addr = srcsock.sin6_addr; /* XXX: this overrides pi */
541		if (ifpp)
542			*ifpp = ifp;
543		*ret_ia6 = ia6->ia_addr.sin6_addr;
544		pserialize_read_exit(_s);
545		goto exit;
546	}
547
548	/*
549	 * If the socket has already bound the source, just use it.  We don't
550	 * care at the moment whether in6_selectif() succeeded above, even
551	 * though it would eventually cause an error.
552	 */
553	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
554		*ret_ia6 = *laddr;
555		goto exit;
556	}
557
558	/*
559	 * The outgoing interface is crucial in the general selection procedure
560	 * below.  If it is not known at this point, we fail.
561	 */
562	if (ifp == NULL) {
563		error = iferror;
564		goto exit;
565	}
566
567	/*
568	 * If the address is not yet determined, choose the best one based on
569	 * the outgoing interface and the destination address.
570	 */
571
572#if defined(MIP6) && NMIP > 0
573	/*
574	 * a caller can specify IP6PO_USECOA to not to use a home
575	 * address.  for example, the case that the neighbour
576	 * unreachability detection to the global address.
577	 */
578	if (opts != NULL &&
579	    (opts->ip6po_flags & IP6PO_USECOA) != 0) {
580		ip6po_usecoa = 1;
581	}
582#endif /* MIP6 && NMIP > 0 */
583
584	error = in6_setscope(&dst, ifp, &odstzone);
585	if (error != 0)
586		goto exit;
587
588	s = pserialize_read_enter();
589
590	ia = in6_select_best_ia(dstsock, &dst, ifp, opts, odstzone);
591	if (ia == NULL) {
592		pserialize_read_exit(s);
593		error = EADDRNOTAVAIL;
594		goto exit;
595	}
596	*ret_ia6 = ia->ia_addr.sin6_addr;
597
598	pserialize_read_exit(s);
599exit:
600	if (ifpp == NULL)
601		if_put(ifp, PSREF);
602	curlwp_bindx(bound);
603	return error;
604#undef PSREF
605}
606
607int
608in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
609    struct route **ro, struct rtentry **retrt, bool count_discard)
610{
611	int error = 0;
612	struct rtentry *rt = NULL;
613	union {
614		struct sockaddr		dst;
615		struct sockaddr_in	dst4;
616		struct sockaddr_in6	dst6;
617	} u;
618
619	KASSERT(ro != NULL);
620	KASSERT(*ro != NULL);
621	KASSERT(retrt != NULL);
622
623#if 0
624	if (dstsock->sin6_addr.s6_addr32[0] == 0 &&
625	    dstsock->sin6_addr.s6_addr32[1] == 0 &&
626	    !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) {
627		char ip6buf[INET6_ADDRSTRLEN];
628		printf("%s: strange destination %s\n", __func__,
629		       IN6_PRINT(ip6buf, &dstsock->sin6_addr));
630	} else {
631		char ip6buf[INET6_ADDRSTRLEN];
632		printf("%s: destination = %s%%%d\n", __func__,
633		       IN6_PRINT(ip6buf, &dstsock->sin6_addr),
634		       dstsock->sin6_scope_id); /* for debug */
635	}
636#endif
637
638	/*
639	 * If the next hop address for the packet is specified by the caller,
640	 * use it as the gateway.
641	 */
642	if (opts && opts->ip6po_nexthop) {
643		struct route *ron;
644		struct sockaddr_in6 *sin6_next;
645
646		sin6_next = satosin6(opts->ip6po_nexthop);
647
648		/* at this moment, we only support AF_INET6 next hops */
649		if (sin6_next->sin6_family != AF_INET6) {
650			IP6_STATINC(IP6_STAT_ODROPPED);
651			error = EAFNOSUPPORT; /* or should we proceed? */
652			goto done;
653		}
654
655		/*
656		 * If the next hop is an IPv6 address, then the node identified
657		 * by that address must be a neighbor of the sending host.
658		 */
659		ron = &opts->ip6po_nextroute;
660		rt = rtcache_lookup(ron, sin6tosa(sin6_next));
661		if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) != 0 ||
662		    !nd6_is_addr_neighbor(sin6_next, rt->rt_ifp)) {
663			if (rt != NULL) {
664				if (count_discard)
665					in6_ifstat_inc(rt->rt_ifp,
666					    ifs6_out_discard);
667				rtcache_unref(rt, ron);
668				rt = NULL;
669			}
670			rtcache_free(ron);
671			error = EHOSTUNREACH;
672			goto done;
673		}
674		*ro = ron;
675
676		goto done;
677	}
678
679	/*
680	 * Use a cached route if it exists and is valid, else try to allocate
681	 * a new one.  Note that we should check the address family of the
682	 * cached destination, in case of sharing the cache with IPv4.
683	 *
684	 * for V4 mapped addresses we want to pick up the v4 route
685	 * see PR kern/56348
686	 */
687	if (IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
688		in6_sin6_2_sin(&u.dst4, dstsock);
689	} else {
690		u.dst6 = *dstsock;
691		u.dst6.sin6_scope_id = 0;
692	}
693
694	rt = rtcache_lookup1(*ro, &u.dst, 1);
695
696	if (rt == NULL)
697		error = EHOSTUNREACH;
698
699	/*
700	 * Check if the outgoing interface conflicts with
701	 * the interface specified by ipi6_ifindex (if specified).
702	 * Note that loopback interface is always okay.
703	 * (this may happen when we are sending a packet to one of
704	 *  our own addresses.)
705	 */
706	if (opts && opts->ip6po_pktinfo && opts->ip6po_pktinfo->ipi6_ifindex) {
707		if (rt != NULL && !(rt->rt_ifp->if_flags & IFF_LOOPBACK) &&
708		    rt->rt_ifp->if_index != opts->ip6po_pktinfo->ipi6_ifindex) {
709			if (count_discard)
710				in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
711			error = EHOSTUNREACH;
712			rtcache_unref(rt, *ro);
713			rt = NULL;
714		}
715	}
716
717done:
718	if (error == EHOSTUNREACH)
719		IP6_STATINC(IP6_STAT_NOROUTE);
720	*retrt = rt;
721	return error;
722}
723
724static int
725in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
726	struct ip6_moptions *mopts, struct route *ro, struct ifnet **retifp,
727	struct psref *psref)
728{
729	int error = 0;
730	struct rtentry *rt = NULL;
731	struct in6_addr *dst;
732	struct in6_pktinfo *pi = NULL;
733
734	KASSERT(retifp != NULL);
735	*retifp = NULL;
736	dst = &dstsock->sin6_addr;
737
738	/* If the caller specify the outgoing interface explicitly, use it. */
739	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
740		/* XXX boundary check is assumed to be already done. */
741		*retifp = if_get_byindex(pi->ipi6_ifindex, psref);
742		if (*retifp != NULL)
743			return 0;
744		goto getroute;
745	}
746
747	/*
748	 * If the destination address is a multicast address and the outgoing
749	 * interface for the address is specified by the caller, use it.
750	 */
751	if (IN6_IS_ADDR_MULTICAST(dst) && mopts != NULL) {
752		*retifp = if_get_byindex(mopts->im6o_multicast_if_index, psref);
753		if (*retifp != NULL)
754			return 0; /* we do not need a route for multicast. */
755	}
756
757getroute:
758	error = in6_selectroute(dstsock, opts, &ro, &rt, false);
759	if (error != 0)
760		return error;
761
762	*retifp = if_get_byindex(rt->rt_ifp->if_index, psref);
763
764	/*
765	 * do not use a rejected or black hole route.
766	 * XXX: this check should be done in the L2 output routine.
767	 * However, if we skipped this check here, we'd see the following
768	 * scenario:
769	 * - install a rejected route for a scoped address prefix
770	 *   (like fe80::/10)
771	 * - send a packet to a destination that matches the scoped prefix,
772	 *   with ambiguity about the scope zone.
773	 * - pick the outgoing interface from the route, and disambiguate the
774	 *   scope zone with the interface.
775	 * - ip6_output() would try to get another route with the "new"
776	 *   destination, which may be valid.
777	 * - we'd see no error on output.
778	 * Although this may not be very harmful, it should still be confusing.
779	 * We thus reject the case here.
780	 */
781	if ((rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) {
782		error = (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
783		/* XXX: ifp can be returned with psref even if error */
784		goto out;
785	}
786
787	/*
788	 * Adjust the "outgoing" interface.  If we're going to loop the packet
789	 * back to ourselves, the ifp would be the loopback interface.
790	 * However, we'd rather know the interface associated to the
791	 * destination address (which should probably be one of our own
792	 * addresses.)
793	 */
794	if (rt->rt_ifa->ifa_ifp != *retifp &&
795	    !if_is_deactivated(rt->rt_ifa->ifa_ifp)) {
796		if_put(*retifp, psref);
797		*retifp = rt->rt_ifa->ifa_ifp;
798		if_acquire(*retifp, psref);
799	}
800out:
801	rtcache_unref(rt, ro);
802	return error;
803}
804
805/*
806 * Default hop limit selection. The precedence is as follows:
807 * 1. Hoplimit value specified via ioctl.
808 * 2. (If the outgoing interface is detected) the current
809 *     hop limit of the interface specified by router advertisement.
810 * 3. The system default hoplimit.
811*/
812int
813in6pcb_selecthlim(struct inpcb *inp, struct ifnet *ifp)
814{
815	if (inp && in6p_hops6(inp) >= 0)
816		return in6p_hops6(inp);
817	else if (ifp)
818		return (ND_IFINFO(ifp)->chlim);
819	else
820		return (ip6_defhlim);
821}
822
823int
824in6pcb_selecthlim_rt(struct inpcb *inp)
825{
826	struct rtentry *rt;
827
828	if (inp == NULL)
829		return in6pcb_selecthlim(inp, NULL);
830
831	rt = rtcache_validate(&inp->inp_route);
832	if (rt != NULL) {
833		int ret = in6pcb_selecthlim(inp, rt->rt_ifp);
834		rtcache_unref(rt, &inp->inp_route);
835		return ret;
836	} else
837		return in6pcb_selecthlim(inp, NULL);
838}
839
840/*
841 * Find an empty port and set it to the specified PCB.
842 */
843int
844in6pcb_set_port(struct sockaddr_in6 *sin6, struct inpcb *inp, struct lwp *l)
845{
846	struct socket *so = inp->inp_socket;
847	struct inpcbtable *table = inp->inp_table;
848	u_int16_t lport, *lastport;
849	enum kauth_network_req req;
850	int error = 0;
851
852	if (inp->inp_flags & IN6P_LOWPORT) {
853#ifndef IPNOPRIVPORTS
854		req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
855#else
856		req = KAUTH_REQ_NETWORK_BIND_PORT;
857#endif
858		lastport = &table->inpt_lastlow;
859	} else {
860		req = KAUTH_REQ_NETWORK_BIND_PORT;
861
862		lastport = &table->inpt_lastport;
863	}
864
865	/* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
866	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND, req, so,
867	    sin6, NULL);
868	if (error)
869		return (EACCES);
870
871       /*
872        * Use RFC6056 randomized port selection
873        */
874	error = portalgo_randport(&lport, inp, l->l_cred);
875	if (error)
876		return error;
877
878	inp->inp_flags |= IN6P_ANONPORT;
879	*lastport = lport;
880	inp->inp_lport = htons(lport);
881	in6pcb_set_state(inp, INP_BOUND);
882	return (0);		/* success */
883}
884
885void
886addrsel_policy_init(void)
887{
888	init_policy_queue();
889
890	/* initialize the "last resort" policy */
891	memset(&defaultaddrpolicy, 0, sizeof(defaultaddrpolicy));
892	defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
893}
894
895/*
896 * XXX: NOMPSAFE if a policy is set
897 */
898static struct in6_addrpolicy *
899lookup_addrsel_policy(struct sockaddr_in6 *key)
900{
901	struct in6_addrpolicy *match = NULL;
902
903	match = match_addrsel_policy(key);
904
905	if (match == NULL)
906		match = &defaultaddrpolicy;
907	else
908		match->use++;
909
910	return (match);
911}
912
913/*
914 * Subroutines to manage the address selection policy table via sysctl.
915 */
916struct sel_walkarg {
917	size_t	w_total;
918	size_t	w_given;
919	void *	w_where;
920	void *w_limit;
921};
922
923int sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS);
924int
925sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS)
926{
927	int error = 0;
928	int s;
929
930	s = splsoftnet();
931
932	if (newp) {
933		error = EPERM;
934		goto end;
935	}
936	if (oldp && oldlenp == NULL) {
937		error = EINVAL;
938		goto end;
939	}
940	if (oldp || oldlenp) {
941		struct sel_walkarg w;
942		size_t oldlen = *oldlenp;
943
944		memset(&w, 0, sizeof(w));
945		w.w_given = oldlen;
946		w.w_where = oldp;
947		if (oldp)
948			w.w_limit = (char *)oldp + oldlen;
949
950		error = walk_addrsel_policy(dump_addrsel_policyent, &w);
951
952		*oldlenp = w.w_total;
953		if (oldp && w.w_total > oldlen && error == 0)
954			error = ENOMEM;
955	}
956
957  end:
958	splx(s);
959
960	return (error);
961}
962
963int
964in6_src_ioctl(u_long cmd, void *data)
965{
966	int i;
967	struct in6_addrpolicy ent0;
968
969	if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
970		return (EOPNOTSUPP); /* check for safety */
971
972	ent0 = *(struct in6_addrpolicy *)data;
973
974	if (ent0.label == ADDR_LABEL_NOTAPP)
975		return (EINVAL);
976	/* check if the prefix mask is consecutive. */
977	if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
978		return (EINVAL);
979	/* clear trailing garbages (if any) of the prefix address. */
980	for (i = 0; i < 4; i++) {
981		ent0.addr.sin6_addr.s6_addr32[i] &=
982			ent0.addrmask.sin6_addr.s6_addr32[i];
983	}
984	ent0.use = 0;
985
986	switch (cmd) {
987	case SIOCAADDRCTL_POLICY:
988		return (add_addrsel_policyent(&ent0));
989	case SIOCDADDRCTL_POLICY:
990		return (delete_addrsel_policyent(&ent0));
991	}
992
993	return (0);		/* XXX: compromise compilers */
994}
995
996/*
997 * The followings are implementation of the policy table using a
998 * simple tail queue.
999 * XXX such details should be hidden.
1000 * XXX implementation using binary tree should be more efficient.
1001 */
1002struct addrsel_policyent {
1003	TAILQ_ENTRY(addrsel_policyent) ape_entry;
1004	struct in6_addrpolicy ape_policy;
1005};
1006
1007TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
1008
1009struct addrsel_policyhead addrsel_policytab;
1010
1011static void
1012init_policy_queue(void)
1013{
1014	TAILQ_INIT(&addrsel_policytab);
1015}
1016
1017static int
1018add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
1019{
1020	struct addrsel_policyent *newpol, *pol;
1021
1022	/* duplication check */
1023	TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
1024		if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
1025		    &pol->ape_policy.addr.sin6_addr) &&
1026		    IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
1027		    &pol->ape_policy.addrmask.sin6_addr)) {
1028			return (EEXIST);	/* or override it? */
1029		}
1030	}
1031
1032	newpol = malloc(sizeof(*newpol), M_IFADDR, M_WAITOK|M_ZERO);
1033
1034	/* XXX: should validate entry */
1035	newpol->ape_policy = *newpolicy;
1036
1037	TAILQ_INSERT_TAIL(&addrsel_policytab, newpol, ape_entry);
1038
1039	return (0);
1040}
1041
1042static int
1043delete_addrsel_policyent(struct in6_addrpolicy *key)
1044{
1045	struct addrsel_policyent *pol;
1046
1047	/* search for the entry in the table */
1048	for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
1049	     pol = TAILQ_NEXT(pol, ape_entry)) {
1050		if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
1051		    &pol->ape_policy.addr.sin6_addr) &&
1052		    IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
1053		    &pol->ape_policy.addrmask.sin6_addr)) {
1054			break;
1055		}
1056	}
1057	if (pol == NULL) {
1058		return (ESRCH);
1059	}
1060
1061	TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
1062
1063	return (0);
1064}
1065
1066static int
1067walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), void *w)
1068{
1069	struct addrsel_policyent *pol;
1070	int error = 0;
1071
1072	TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
1073		if ((error = (*callback)(&pol->ape_policy, w)) != 0)
1074			return error;
1075	}
1076
1077	return error;
1078}
1079
1080static int
1081dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
1082{
1083	int error = 0;
1084	struct sel_walkarg *w = arg;
1085
1086	if (w->w_where && (char *)w->w_where + sizeof(*pol) <= (char *)w->w_limit) {
1087		if ((error = copyout(pol, w->w_where, sizeof(*pol))) != 0)
1088			return error;
1089		w->w_where = (char *)w->w_where + sizeof(*pol);
1090	}
1091	w->w_total += sizeof(*pol);
1092
1093	return error;
1094}
1095
1096static struct in6_addrpolicy *
1097match_addrsel_policy(struct sockaddr_in6 *key)
1098{
1099	struct addrsel_policyent *pent;
1100	struct in6_addrpolicy *bestpol = NULL, *pol;
1101	int matchlen, bestmatchlen = -1;
1102	u_char *mp, *ep, *k, *p, m;
1103
1104	for (pent = TAILQ_FIRST(&addrsel_policytab); pent;
1105	     pent = TAILQ_NEXT(pent, ape_entry)) {
1106		matchlen = 0;
1107
1108		pol = &pent->ape_policy;
1109		mp = (u_char *)&pol->addrmask.sin6_addr;
1110		ep = mp + 16;	/* XXX: scope field? */
1111		k = (u_char *)&key->sin6_addr;
1112		p = (u_char *)&pol->addr.sin6_addr;
1113		for (; mp < ep && *mp; mp++, k++, p++) {
1114			m = *mp;
1115			if ((*k & m) != *p)
1116				goto next; /* not match */
1117			if (m == 0xff) /* short cut for a typical case */
1118				matchlen += 8;
1119			else {
1120				while (m >= 0x80) {
1121					matchlen++;
1122					m <<= 1;
1123				}
1124			}
1125		}
1126
1127		/* matched.  check if this is better than the current best. */
1128		if (bestpol == NULL ||
1129		    matchlen > bestmatchlen) {
1130			bestpol = pol;
1131			bestmatchlen = matchlen;
1132		}
1133
1134	  next:
1135		continue;
1136	}
1137
1138	return (bestpol);
1139}
1140