2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: in6_src.c,v 1.132 2003/08/26 04:42:27 keiichi Exp $
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
65 #include "opt_inet6.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/malloc.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/sysctl.h>
77 #include <sys/errno.h>
80 #include <sys/kernel.h>
81 #include <sys/rmlock.h>
85 #include <net/if_var.h>
86 #include <net/if_dl.h>
87 #include <net/if_private.h>
88 #include <net/route.h>
89 #include <net/route/nhop.h>
90 #include <net/if_llatbl.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/udp.h>
99 #include <netinet/udp_var.h>
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet6/in6_fib.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet6/ip6_var.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/nd6.h>
109 static struct mtx addrsel_lock;
110 #define ADDRSEL_LOCK_INIT() mtx_init(&addrsel_lock, "addrsel_lock", NULL, MTX_DEF)
111 #define ADDRSEL_LOCK() mtx_lock(&addrsel_lock)
112 #define ADDRSEL_UNLOCK() mtx_unlock(&addrsel_lock)
113 #define ADDRSEL_LOCK_ASSERT() mtx_assert(&addrsel_lock, MA_OWNED)
115 static struct sx addrsel_sxlock;
116 #define ADDRSEL_SXLOCK_INIT() sx_init(&addrsel_sxlock, "addrsel_sxlock")
117 #define ADDRSEL_SLOCK() sx_slock(&addrsel_sxlock)
118 #define ADDRSEL_SUNLOCK() sx_sunlock(&addrsel_sxlock)
119 #define ADDRSEL_XLOCK() sx_xlock(&addrsel_sxlock)
120 #define ADDRSEL_XUNLOCK() sx_xunlock(&addrsel_sxlock)
122 #define ADDR_LABEL_NOTAPP (-1)
123 VNET_DEFINE_STATIC(struct in6_addrpolicy, defaultaddrpolicy);
124 #define V_defaultaddrpolicy VNET(defaultaddrpolicy)
126 VNET_DEFINE(int, ip6_prefer_tempaddr) = 0;
128 static int selectroute(struct sockaddr_in6 *, struct ip6_pktopts *,
129 struct ip6_moptions *, struct route_in6 *, struct ifnet **,
130 struct nhop_object **, int, u_int, uint32_t);
131 static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
132 struct ip6_moptions *, struct ifnet **,
133 struct ifnet *, u_int);
134 static int in6_selectsrc(uint32_t, struct sockaddr_in6 *,
135 struct ip6_pktopts *, struct inpcb *, struct ucred *,
136 struct ifnet **, struct in6_addr *);
138 static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *);
140 static void init_policy_queue(void);
141 static int add_addrsel_policyent(struct in6_addrpolicy *);
142 static int delete_addrsel_policyent(struct in6_addrpolicy *);
143 static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
145 static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
146 static struct in6_addrpolicy *match_addrsel_policy(struct sockaddr_in6 *);
149 * Return an IPv6 address, which is the most appropriate for a given
150 * destination and user specified options.
151 * If necessary, this function lookups the routing table and returns
152 * an entry to the caller for later use.
154 #define REPLACE(r) do {\
155 IP6STAT_INC(ip6s_sources_rule[(r)]); \
157 char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \
158 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)); \
162 #define NEXT(r) do {\
164 char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \
165 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)); \
167 goto next; /* XXX: we can't use 'continue' here */ \
169 #define BREAK(r) do { \
170 IP6STAT_INC(ip6s_sources_rule[(r)]); \
171 goto out; /* XXX: we can't use 'break' here */ \
175 in6_selectsrc(uint32_t fibnum, struct sockaddr_in6 *dstsock,
176 struct ip6_pktopts *opts, struct inpcb *inp, struct ucred *cred,
177 struct ifnet **ifpp, struct in6_addr *srcp)
179 struct rm_priotracker in6_ifa_tracker;
180 struct in6_addr dst, tmp;
181 struct ifnet *ifp = NULL, *oifp = NULL;
182 struct in6_ifaddr *ia = NULL, *ia_best = NULL;
183 struct in6_pktinfo *pi = NULL;
184 int dst_scope = -1, best_scope = -1, best_matchlen = -1;
185 struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL;
189 struct ip6_moptions *mopts;
192 KASSERT(srcp != NULL, ("%s: srcp is NULL", __func__));
194 dst = dstsock->sin6_addr; /* make a copy for local operation */
197 * Save a possibly passed in ifp for in6_selectsrc. Only
198 * neighbor discovery code should use this feature, where
199 * we may know the interface but not the FIB number holding
200 * the connected subnet in case someone deleted it from the
201 * default FIB and we need to check the interface.
209 INP_LOCK_ASSERT(inp);
210 mopts = inp->in6p_moptions;
216 * If the source address is explicitly specified by the caller,
217 * check if the requested source address is indeed a unicast address
218 * assigned to the node, and can be used as the packet's source
219 * address. If everything is okay, use the address as source.
221 if (opts && (pi = opts->ip6po_pktinfo) &&
222 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
223 /* get the outgoing interface */
224 if ((error = in6_selectif(dstsock, opts, mopts, &ifp, oifp,
230 * determine the appropriate zone id of the source based on
231 * the zone of the destination and the outgoing interface.
232 * If the specified address is ambiguous wrt the scope zone,
233 * the interface must be specified; otherwise, ifa_ifwithaddr()
234 * will fail matching the address.
238 error = in6_setscope(&tmp, ifp, &odstzone);
242 if (cred != NULL && (error = prison_local_ip6(cred,
243 &tmp, (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0)
247 * If IPV6_BINDANY socket option is set, we allow to specify
248 * non local addresses as source address in IPV6_PKTINFO
251 if ((inp->inp_flags & INP_BINDANY) == 0) {
252 ia = in6ifa_ifwithaddr(&tmp, 0 /* XXX */, false);
253 if (ia == NULL || (ia->ia6_flags & (IN6_IFF_ANYCAST |
255 return (EADDRNOTAVAIL);
256 bcopy(&ia->ia_addr.sin6_addr, srcp, sizeof(*srcp));
258 bcopy(&tmp, srcp, sizeof(*srcp));
259 pi->ipi6_addr = tmp; /* XXX: this overrides pi */
266 * Otherwise, if the socket has already bound the source, just use it.
268 if (inp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
270 (error = prison_local_ip6(cred, &inp->in6p_laddr,
271 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
273 bcopy(&inp->in6p_laddr, srcp, sizeof(*srcp));
278 * Bypass source address selection and use the primary jail IP
281 if (cred != NULL && !prison_saddrsel_ip6(cred, srcp))
285 * If the address is not specified, choose the best one based on
286 * the outgoing interface and the destination address.
288 /* get the outgoing interface */
289 if ((error = in6_selectif(dstsock, opts, mopts, &ifp, oifp,
290 (inp != NULL) ? inp->inp_inc.inc_fibnum : fibnum)) != 0)
294 if (ifp == NULL) /* this should not happen */
295 panic("in6_selectsrc: NULL ifp");
297 error = in6_setscope(&dst, ifp, &odstzone);
301 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
302 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
303 int new_scope = -1, new_matchlen = -1;
304 struct in6_addrpolicy *new_policy = NULL;
305 u_int32_t srczone, osrczone, dstzone;
307 struct ifnet *ifp1 = ia->ia_ifp;
310 * We'll never take an address that breaks the scope zone
311 * of the destination. We also skip an address if its zone
312 * does not contain the outgoing interface.
313 * XXX: we should probably use sin6_scope_id here.
315 if (in6_setscope(&dst, ifp1, &dstzone) ||
316 odstzone != dstzone) {
319 src = ia->ia_addr.sin6_addr;
320 if (in6_setscope(&src, ifp, &osrczone) ||
321 in6_setscope(&src, ifp1, &srczone) ||
322 osrczone != srczone) {
326 /* avoid unusable addresses */
328 (IN6_IFF_NOTREADY | IN6_IFF_ANYCAST | IN6_IFF_DETACHED))) {
331 if (!V_ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
334 /* If jailed only take addresses of the jail into account. */
336 prison_check_ip6(cred, &ia->ia_addr.sin6_addr) != 0)
339 /* Rule 1: Prefer same address */
340 if (IN6_ARE_ADDR_EQUAL(&dst, &ia->ia_addr.sin6_addr)) {
342 BREAK(1); /* there should be no better candidate */
348 /* Rule 2: Prefer appropriate scope */
350 dst_scope = in6_addrscope(&dst);
351 new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
352 if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
353 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
356 } else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
357 if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
363 * Rule 3: Avoid deprecated addresses. Note that the case of
364 * !ip6_use_deprecated is already rejected above.
366 if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
368 if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
371 /* Rule 4: Prefer home addresses */
373 * XXX: This is a TODO. We should probably merge the MIP6
377 /* Rule 5: Prefer outgoing interface */
378 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_NO_PREFER_IFACE)) {
379 if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
381 if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
386 * Rule 6: Prefer matching label
387 * Note that best_policy should be non-NULL here.
389 if (dst_policy == NULL)
390 dst_policy = lookup_addrsel_policy(dstsock);
391 if (dst_policy->label != ADDR_LABEL_NOTAPP) {
392 new_policy = lookup_addrsel_policy(&ia->ia_addr);
393 if (dst_policy->label == best_policy->label &&
394 dst_policy->label != new_policy->label)
396 if (dst_policy->label != best_policy->label &&
397 dst_policy->label == new_policy->label)
402 * Rule 7: Prefer public addresses.
403 * We allow users to reverse the logic by configuring
404 * a sysctl variable, so that privacy conscious users can
405 * always prefer temporary addresses.
408 opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
409 prefer_tempaddr = V_ip6_prefer_tempaddr;
410 } else if (opts->ip6po_prefer_tempaddr ==
411 IP6PO_TEMPADDR_NOTPREFER) {
415 if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
416 (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
422 if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
423 !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
431 * Rule 8: prefer addresses on alive interfaces.
432 * This is a KAME specific rule.
434 if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
435 !(ia->ia_ifp->if_flags & IFF_UP))
437 if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
438 (ia->ia_ifp->if_flags & IFF_UP))
442 * Rule 9: prefer address with better virtual status.
444 if (ifa_preferred(&ia_best->ia_ifa, &ia->ia_ifa))
446 if (ifa_preferred(&ia->ia_ifa, &ia_best->ia_ifa))
450 * Rule 10: prefer address with `prefer_source' flag.
452 if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0 &&
453 (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0)
455 if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0 &&
456 (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0)
460 * Rule 14: Use longest matching prefix.
461 * Note: in the address selection draft, this rule is
462 * documented as "Rule 8". However, since it is also
463 * documented that this rule can be overridden, we assign
464 * a large number so that it is easy to assign smaller numbers
465 * to more preferred rules.
467 new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, &dst);
468 if (best_matchlen < new_matchlen)
470 if (new_matchlen < best_matchlen)
473 /* Rule 15 is reserved. */
476 * Last resort: just keep the current candidate.
477 * Or, do we need more rules?
483 best_scope = (new_scope >= 0 ? new_scope :
484 in6_addrscope(&ia_best->ia_addr.sin6_addr));
485 best_policy = (new_policy ? new_policy :
486 lookup_addrsel_policy(&ia_best->ia_addr));
487 best_matchlen = (new_matchlen >= 0 ? new_matchlen :
488 in6_matchlen(&ia_best->ia_addr.sin6_addr,
498 if ((ia = ia_best) == NULL) {
499 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
500 IP6STAT_INC(ip6s_sources_none);
501 return (EADDRNOTAVAIL);
505 * At this point at least one of the addresses belonged to the jail
506 * but it could still be, that we want to further restrict it, e.g.
507 * theoratically IN6_IS_ADDR_LOOPBACK.
508 * It must not be IN6_IS_ADDR_UNSPECIFIED anymore.
509 * prison_local_ip6() will fix an IN6_IS_ADDR_LOOPBACK but should
510 * let all others previously selected pass.
511 * Use tmp to not change ::1 on lo0 to the primary jail address.
513 tmp = ia->ia_addr.sin6_addr;
514 if (cred != NULL && prison_local_ip6(cred, &tmp, (inp != NULL &&
515 (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0) {
516 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
517 IP6STAT_INC(ip6s_sources_none);
518 return (EADDRNOTAVAIL);
524 bcopy(&tmp, srcp, sizeof(*srcp));
525 if (ia->ia_ifp == ifp)
526 IP6STAT_INC(ip6s_sources_sameif[best_scope]);
528 IP6STAT_INC(ip6s_sources_otherif[best_scope]);
529 if (dst_scope == best_scope)
530 IP6STAT_INC(ip6s_sources_samescope[best_scope]);
532 IP6STAT_INC(ip6s_sources_otherscope[best_scope]);
533 if (IFA6_IS_DEPRECATED(ia))
534 IP6STAT_INC(ip6s_sources_deprecated[best_scope]);
535 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
540 * Select source address based on @inp, @dstsock and @opts.
541 * Stores selected address to @srcp. If @scope_ambiguous is set,
542 * embed scope from selected outgoing interface. If @hlim pointer
543 * is provided, stores calculated hop limit there.
544 * Returns 0 on success.
547 in6_selectsrc_socket(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
548 struct inpcb *inp, struct ucred *cred, int scope_ambiguous,
549 struct in6_addr *srcp, int *hlim)
551 struct ifnet *retifp;
555 fibnum = inp->inp_inc.inc_fibnum;
558 error = in6_selectsrc(fibnum, dstsock, opts, inp, cred, &retifp, srcp);
563 *hlim = in6_selecthlim(inp, retifp);
565 if (retifp == NULL || scope_ambiguous == 0)
569 * Application should provide a proper zone ID or the use of
570 * default zone IDs should be enabled. Unfortunately, some
571 * applications do not behave as it should, so we need a
572 * workaround. Even if an appropriate ID is not determined
573 * (when it's required), if we can determine the outgoing
574 * interface. determine the zone ID based on the interface.
576 error = in6_setscope(&dstsock->sin6_addr, retifp, NULL);
582 * Select source address based on @fibnum, @dst and @scopeid.
583 * Stores selected address to @srcp.
584 * Returns 0 on success.
586 * Used by non-socket based consumers (ND code mostly)
589 in6_selectsrc_addr(uint32_t fibnum, const struct in6_addr *dst,
590 uint32_t scopeid, struct ifnet *ifp, struct in6_addr *srcp,
593 struct ifnet *retifp;
594 struct sockaddr_in6 dst_sa;
598 bzero(&dst_sa, sizeof(dst_sa));
599 dst_sa.sin6_family = AF_INET6;
600 dst_sa.sin6_len = sizeof(dst_sa);
601 dst_sa.sin6_addr = *dst;
602 dst_sa.sin6_scope_id = scopeid;
603 sa6_embedscope(&dst_sa, 0);
605 error = in6_selectsrc(fibnum, &dst_sa, NULL, NULL, NULL, &retifp, srcp);
607 *hlim = in6_selecthlim(NULL, retifp);
612 static struct nhop_object *
613 cache_route(uint32_t fibnum, const struct sockaddr_in6 *dst, struct route_in6 *ro,
617 * Use a cached route if it exists and is valid, else try to allocate
618 * a new one. Note that we should check the address family of the
619 * cached destination, in case of sharing the cache with IPv4.
620 * Assumes that 'struct route_in6' is exclusively locked.
622 if (ro->ro_nh != NULL && (
623 !NH_IS_VALID(ro->ro_nh) || ro->ro_dst.sin6_family != AF_INET6 ||
624 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &dst->sin6_addr)))
627 if (ro->ro_nh == NULL) {
630 const struct in6_addr *paddr;
631 struct in6_addr unscoped_addr;
632 uint32_t scopeid = 0;
633 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr)) {
634 in6_splitscope(&dst->sin6_addr, &unscoped_addr, &scopeid);
635 paddr = &unscoped_addr;
637 paddr = &dst->sin6_addr;
638 ro->ro_nh = fib6_lookup(fibnum, paddr, scopeid, NHR_REF, flowid);
643 static struct nhop_object *
644 lookup_route(uint32_t fibnum, struct sockaddr_in6 *dst, struct route_in6 *ro,
645 struct ip6_pktopts *opts, uint32_t flowid)
647 struct nhop_object *nh = NULL;
650 * If the next hop address for the packet is specified by the caller,
651 * use it as the gateway.
653 if (opts && opts->ip6po_nexthop) {
654 struct route_in6 *ron = &opts->ip6po_nextroute;
655 struct sockaddr_in6 *sin6_next = satosin6(opts->ip6po_nexthop);
657 nh = cache_route(fibnum, sin6_next, ron, flowid);
660 * The node identified by that address must be a
661 * neighbor of the sending host.
663 if (nh != NULL && (nh->nh_flags & NHF_GATEWAY) != 0)
665 } else if (ro != NULL) {
666 nh = cache_route(fibnum, dst, ro, flowid);
671 * Check if the outgoing interface conflicts with
672 * the interface specified by ipi6_ifindex (if specified).
674 struct in6_pktinfo *pi;
675 if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
676 if (nh->nh_aifp->if_index != pi->ipi6_ifindex)
685 * Finds outgoing nexthop or the outgoing interface for the
687 * Return 0 on success and stores the lookup result in @retnh and @retifp
690 selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
691 struct ip6_moptions *mopts, struct route_in6 *ro,
692 struct ifnet **retifp, struct nhop_object **retnh, int norouteok,
693 u_int fibnum, uint32_t flowid)
696 struct ifnet *ifp = NULL;
697 struct in6_pktinfo *pi = NULL;
698 struct in6_addr *dst = &dstsock->sin6_addr;
700 /* If the caller specify the outgoing interface explicitly, use it. */
701 if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
702 /* XXX boundary check is assumed to be already done. */
703 ifp = ifnet_byindex(pi->ipi6_ifindex);
704 if (ifp != NULL && (norouteok || IN6_IS_ADDR_MULTICAST(dst))) {
706 * we do not have to check or get the route for
714 * If the destination address is a multicast address and the outgoing
715 * interface for the address is specified by the caller, use it.
717 if (IN6_IS_ADDR_MULTICAST(dst) &&
718 mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) {
719 goto done; /* we do not need a route for multicast. */
722 * If destination address is LLA or link- or node-local multicast,
723 * use it's embedded scope zone id to determine outgoing interface.
725 if (IN6_IS_ADDR_MC_LINKLOCAL(dst) ||
726 IN6_IS_ADDR_MC_NODELOCAL(dst)) {
727 uint32_t zoneid = ntohs(in6_getscope(dst));
729 ifp = in6_getlinkifnet(zoneid);
735 struct nhop_object *nh = lookup_route(fibnum, dstsock, ro, opts, flowid);
737 *retifp = nh->nh_aifp;
741 IP6STAT_INC(ip6s_noroute);
742 error = EHOSTUNREACH;
750 * This can happen if the caller did not pass a cached route
751 * nor any other hints. We treat this case an error.
753 error = EHOSTUNREACH;
755 if (error == EHOSTUNREACH)
756 IP6STAT_INC(ip6s_noroute);
765 in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
766 struct ip6_moptions *mopts, struct ifnet **retifp,
767 struct ifnet *oifp, u_int fibnum)
770 struct route_in6 sro;
771 struct nhop_object *nh = NULL;
774 KASSERT(retifp != NULL, ("%s: retifp is NULL", __func__));
776 bzero(&sro, sizeof(sro));
779 error = selectroute(dstsock, opts, mopts, &sro, retifp, &nh, 1, fibnum, 0);
782 nh_flags = nh->nh_flags;
783 if (nh != NULL && nh == sro.ro_nh)
787 /* Help ND. See oifp comment in in6_selectsrc(). */
788 if (oifp != NULL && fibnum == RT_DEFAULT_FIB) {
796 * do not use a rejected or black hole route.
797 * XXX: this check should be done in the L2 output routine.
798 * However, if we skipped this check here, we'd see the following
800 * - install a rejected route for a scoped address prefix
802 * - send a packet to a destination that matches the scoped prefix,
803 * with ambiguity about the scope zone.
804 * - pick the outgoing interface from the route, and disambiguate the
805 * scope zone with the interface.
806 * - ip6_output() would try to get another route with the "new"
807 * destination, which may be valid.
808 * - we'd see no error on output.
809 * Although this may not be very harmful, it should still be confusing.
810 * We thus reject the case here.
813 if (nh_flags & (NHF_REJECT | NHF_BLACKHOLE)) {
814 error = (nh_flags & NHF_HOST ? EHOSTUNREACH : ENETUNREACH);
821 /* Public wrapper function to selectroute(). */
823 in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
824 struct ip6_moptions *mopts, struct route_in6 *ro,
825 struct ifnet **retifp, struct nhop_object **retnh, u_int fibnum, uint32_t flowid)
827 MPASS(retifp != NULL);
828 MPASS(retnh != NULL);
830 return (selectroute(dstsock, opts, mopts, ro, retifp,
831 retnh, 0, fibnum, flowid));
835 * Default hop limit selection. The precedence is as follows:
836 * 1. Hoplimit value specified via ioctl.
837 * 2. (If the outgoing interface is detected) the current
838 * hop limit of the interface specified by router advertisement.
839 * 3. The system default hoplimit.
842 in6_selecthlim(struct inpcb *inp, struct ifnet *ifp)
845 if (inp && inp->in6p_hops >= 0)
846 return (inp->in6p_hops);
848 return (ND_IFINFO(ifp)->chlim);
849 else if (inp && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
850 struct nhop_object *nh;
852 uint32_t fibnum, scopeid;
855 fibnum = inp->inp_inc.inc_fibnum;
856 in6_splitscope(&inp->in6p_faddr, &dst, &scopeid);
857 nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
859 hlim = ND_IFINFO(nh->nh_ifp)->chlim;
863 return (V_ip6_defhlim);
867 addrsel_policy_init(void)
872 /* initialize the "last resort" policy */
873 bzero(&V_defaultaddrpolicy, sizeof(V_defaultaddrpolicy));
874 V_defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
876 if (!IS_DEFAULT_VNET(curvnet))
880 ADDRSEL_SXLOCK_INIT();
883 static struct in6_addrpolicy *
884 lookup_addrsel_policy(struct sockaddr_in6 *key)
886 struct in6_addrpolicy *match = NULL;
889 match = match_addrsel_policy(key);
892 match = &V_defaultaddrpolicy;
901 * Subroutines to manage the address selection policy table via sysctl.
904 struct sysctl_req *w_req;
907 static int in6_src_sysctl(SYSCTL_HANDLER_ARGS);
908 SYSCTL_DECL(_net_inet6_ip6);
909 static SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy,
910 CTLFLAG_RD | CTLFLAG_MPSAFE, in6_src_sysctl,
914 in6_src_sysctl(SYSCTL_HANDLER_ARGS)
921 bzero(&w, sizeof(w));
924 return (walk_addrsel_policy(dump_addrsel_policyent, &w));
928 in6_src_ioctl(u_long cmd, caddr_t data)
930 struct in6_addrpolicy ent0;
932 if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
933 return (EOPNOTSUPP); /* check for safety */
935 ent0 = *(struct in6_addrpolicy *)data;
937 if (ent0.label == ADDR_LABEL_NOTAPP)
939 /* check if the prefix mask is consecutive. */
940 if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
942 /* clear trailing garbages (if any) of the prefix address. */
943 IN6_MASK_ADDR(&ent0.addr.sin6_addr, &ent0.addrmask.sin6_addr);
947 case SIOCAADDRCTL_POLICY:
948 return (add_addrsel_policyent(&ent0));
949 case SIOCDADDRCTL_POLICY:
950 return (delete_addrsel_policyent(&ent0));
953 return (0); /* XXX: compromise compilers */
957 * The followings are implementation of the policy table using a
959 * XXX such details should be hidden.
960 * XXX implementation using binary tree should be more efficient.
962 struct addrsel_policyent {
963 TAILQ_ENTRY(addrsel_policyent) ape_entry;
964 struct in6_addrpolicy ape_policy;
967 TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
969 VNET_DEFINE_STATIC(struct addrsel_policyhead, addrsel_policytab);
970 #define V_addrsel_policytab VNET(addrsel_policytab)
973 init_policy_queue(void)
976 TAILQ_INIT(&V_addrsel_policytab);
980 add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
982 struct addrsel_policyent *new, *pol;
984 new = malloc(sizeof(*new), M_IFADDR,
989 /* duplication check */
990 TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
991 if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
992 &pol->ape_policy.addr.sin6_addr) &&
993 IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
994 &pol->ape_policy.addrmask.sin6_addr)) {
998 return (EEXIST); /* or override it? */
1002 bzero(new, sizeof(*new));
1004 /* XXX: should validate entry */
1005 new->ape_policy = *newpolicy;
1007 TAILQ_INSERT_TAIL(&V_addrsel_policytab, new, ape_entry);
1015 delete_addrsel_policyent(struct in6_addrpolicy *key)
1017 struct addrsel_policyent *pol;
1022 /* search for the entry in the table */
1023 TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
1024 if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
1025 &pol->ape_policy.addr.sin6_addr) &&
1026 IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
1027 &pol->ape_policy.addrmask.sin6_addr)) {
1037 TAILQ_REMOVE(&V_addrsel_policytab, pol, ape_entry);
1040 free(pol, M_IFADDR);
1046 walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), void *w)
1048 struct addrsel_policyent *pol;
1052 TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) {
1053 if ((error = (*callback)(&pol->ape_policy, w)) != 0) {
1063 dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
1066 struct walkarg *w = arg;
1068 error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol));
1073 static struct in6_addrpolicy *
1074 match_addrsel_policy(struct sockaddr_in6 *key)
1076 struct addrsel_policyent *pent;
1077 struct in6_addrpolicy *bestpol = NULL, *pol;
1078 int matchlen, bestmatchlen = -1;
1079 u_char *mp, *ep, *k, *p, m;
1081 TAILQ_FOREACH(pent, &V_addrsel_policytab, ape_entry) {
1084 pol = &pent->ape_policy;
1085 mp = (u_char *)&pol->addrmask.sin6_addr;
1086 ep = mp + 16; /* XXX: scope field? */
1087 k = (u_char *)&key->sin6_addr;
1088 p = (u_char *)&pol->addr.sin6_addr;
1089 for (; mp < ep && *mp; mp++, k++, p++) {
1092 goto next; /* not match */
1093 if (m == 0xff) /* short cut for a typical case */
1103 /* matched. check if this is better than the current best. */
1104 if (bestpol == NULL ||
1105 matchlen > bestmatchlen) {
1107 bestmatchlen = matchlen;