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