2 * Copyright (c) 2003-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/syslog.h>
69 #include <sys/queue.h>
70 #include <sys/mcache.h>
71 #include <sys/protosw.h>
73 #include <dev/random/randomdev.h>
75 #include <kern/locks.h>
76 #include <kern/zalloc.h>
77 #include <machine/machine_routines.h>
80 #include <net/if_var.h>
81 #include <net/if_types.h>
82 #include <net/if_dl.h>
83 #include <net/route.h>
84 #include <net/radix.h>
86 #include <netinet/in.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet6/in6_ifattach.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/nd6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/scope6_var.h>
95 #include <net/net_osdep.h>
97 static void defrouter_addreq(struct nd_defrouter
*, boolean_t
);
98 static void defrouter_delreq(struct nd_defrouter
*);
99 static struct nd_defrouter
*defrtrlist_update_common(struct nd_defrouter
*,
101 static struct nd_defrouter
*defrtrlist_update(struct nd_defrouter
*);
103 static struct in6_ifaddr
*in6_pfx_newpersistaddr(struct nd_prefix
*, int,
106 static struct nd_pfxrouter
*pfxrtr_lookup(struct nd_prefix
*,
107 struct nd_defrouter
*);
108 static void pfxrtr_add(struct nd_prefix
*, struct nd_defrouter
*);
109 static void pfxrtr_del(struct nd_pfxrouter
*, struct nd_prefix
*);
110 static struct nd_pfxrouter
*find_pfxlist_reachable_router(struct nd_prefix
*);
111 static void nd6_rtmsg(int, struct rtentry
*);
113 static int nd6_prefix_onlink_common(struct nd_prefix
*, boolean_t
,
115 static struct nd_prefix
*nd6_prefix_equal_lookup(struct nd_prefix
*, boolean_t
);
116 static void nd6_prefix_sync(struct ifnet
*);
118 static void in6_init_address_ltimes(struct nd_prefix
*,
119 struct in6_addrlifetime
*);
121 static int rt6_deleteroute(struct radix_node
*, void *);
123 static struct nd_defrouter
*nddr_alloc(int);
124 static void nddr_free(struct nd_defrouter
*);
125 static void nddr_trace(struct nd_defrouter
*, int);
127 static struct nd_prefix
*ndpr_alloc(int);
128 static void ndpr_free(struct nd_prefix
*);
129 static void ndpr_trace(struct nd_prefix
*, int);
131 extern int nd6_recalc_reachtm_interval
;
133 static struct ifnet
*nd6_defifp
= NULL
;
134 int nd6_defifindex
= 0;
135 static unsigned int nd6_defrouter_genid
;
137 int ip6_use_tempaddr
= 1; /* use temp addr by default for testing now */
139 int nd6_accept_6to4
= 1;
141 int ip6_desync_factor
;
142 u_int32_t ip6_temp_preferred_lifetime
= DEF_TEMP_PREFERRED_LIFETIME
;
143 u_int32_t ip6_temp_valid_lifetime
= DEF_TEMP_VALID_LIFETIME
;
145 * shorter lifetimes for debugging purposes.
146 * u_int32_t ip6_temp_preferred_lifetime = 800;
147 * static u_int32_t ip6_temp_valid_lifetime = 1800;
149 int ip6_temp_regen_advance
= TEMPADDR_REGEN_ADVANCE
;
151 extern lck_mtx_t
*nd6_mutex
;
153 /* Serialization variables for single thread access to nd_prefix */
154 static boolean_t nd_prefix_busy
;
155 static void *nd_prefix_waitchan
= &nd_prefix_busy
;
156 static int nd_prefix_waiters
= 0;
158 /* Serialization variables for single thread access to nd_defrouter */
159 static boolean_t nd_defrouter_busy
;
160 static void *nd_defrouter_waitchan
= &nd_defrouter_busy
;
161 static int nd_defrouter_waiters
= 0;
163 /* RTPREF_MEDIUM has to be 0! */
164 #define RTPREF_HIGH 1
165 #define RTPREF_MEDIUM 0
166 #define RTPREF_LOW (-1)
167 #define RTPREF_RESERVED (-2)
168 #define RTPREF_INVALID (-3) /* internal */
170 #define NDPR_TRACE_HIST_SIZE 32 /* size of trace history */
173 __private_extern__
unsigned int ndpr_trace_hist_size
= NDPR_TRACE_HIST_SIZE
;
175 struct nd_prefix_dbg
{
176 struct nd_prefix ndpr_pr
; /* nd_prefix */
177 u_int16_t ndpr_refhold_cnt
; /* # of ref */
178 u_int16_t ndpr_refrele_cnt
; /* # of rele */
180 * Circular lists of ndpr_addref and ndpr_remref callers.
182 ctrace_t ndpr_refhold
[NDPR_TRACE_HIST_SIZE
];
183 ctrace_t ndpr_refrele
[NDPR_TRACE_HIST_SIZE
];
186 static unsigned int ndpr_debug
; /* debug flags */
187 static unsigned int ndpr_size
; /* size of zone element */
188 static struct zone
*ndpr_zone
; /* zone for nd_prefix */
190 #define NDPR_ZONE_MAX 64 /* maximum elements in zone */
191 #define NDPR_ZONE_NAME "nd6_prefix" /* zone name */
193 #define NDDR_TRACE_HIST_SIZE 32 /* size of trace history */
196 __private_extern__
unsigned int nddr_trace_hist_size
= NDDR_TRACE_HIST_SIZE
;
198 struct nd_defrouter_dbg
{
199 struct nd_defrouter nddr_dr
; /* nd_defrouter */
200 uint16_t nddr_refhold_cnt
; /* # of ref */
201 uint16_t nddr_refrele_cnt
; /* # of rele */
203 * Circular lists of ndpr_addref and ndpr_remref callers.
205 ctrace_t nddr_refhold
[NDDR_TRACE_HIST_SIZE
];
206 ctrace_t nddr_refrele
[NDDR_TRACE_HIST_SIZE
];
209 static unsigned int nddr_debug
; /* debug flags */
210 static unsigned int nddr_size
; /* size of zone element */
211 static struct zone
*nddr_zone
; /* zone for nd_defrouter */
213 #define NDDR_ZONE_MAX 64 /* maximum elements in zone */
214 #define NDDR_ZONE_NAME "nd6_defrouter" /* zone name */
216 static unsigned int ndprtr_size
; /* size of zone element */
217 static struct zone
*ndprtr_zone
; /* zone for nd_pfxrouter */
219 #define NDPRTR_ZONE_MAX 64 /* maximum elements in zone */
220 #define NDPRTR_ZONE_NAME "nd6_pfxrouter" /* zone name */
225 PE_parse_boot_argn("ifa_debug", &ndpr_debug
, sizeof (ndpr_debug
));
226 PE_parse_boot_argn("ifa_debug", &nddr_debug
, sizeof (nddr_debug
));
228 ndpr_size
= (ndpr_debug
== 0) ? sizeof (struct nd_prefix
) :
229 sizeof (struct nd_prefix_dbg
);
230 ndpr_zone
= zinit(ndpr_size
, NDPR_ZONE_MAX
* ndpr_size
, 0,
232 if (ndpr_zone
== NULL
) {
233 panic("%s: failed allocating %s", __func__
, NDPR_ZONE_NAME
);
236 zone_change(ndpr_zone
, Z_EXPAND
, TRUE
);
237 zone_change(ndpr_zone
, Z_CALLERACCT
, FALSE
);
239 nddr_size
= (nddr_debug
== 0) ? sizeof (struct nd_defrouter
) :
240 sizeof (struct nd_defrouter_dbg
);
241 nddr_zone
= zinit(nddr_size
, NDDR_ZONE_MAX
* nddr_size
, 0,
243 if (nddr_zone
== NULL
) {
244 panic("%s: failed allocating %s", __func__
, NDDR_ZONE_NAME
);
247 zone_change(nddr_zone
, Z_EXPAND
, TRUE
);
248 zone_change(nddr_zone
, Z_CALLERACCT
, FALSE
);
250 ndprtr_size
= sizeof (struct nd_pfxrouter
);
251 ndprtr_zone
= zinit(ndprtr_size
, NDPRTR_ZONE_MAX
* ndprtr_size
, 0,
253 if (ndprtr_zone
== NULL
) {
254 panic("%s: failed allocating %s", __func__
, NDPRTR_ZONE_NAME
);
257 zone_change(ndprtr_zone
, Z_EXPAND
, TRUE
);
258 zone_change(ndprtr_zone
, Z_CALLERACCT
, FALSE
);
262 * Receive Router Solicitation Message - just for routers.
263 * Router solicitation/advertisement is mostly managed by userland program
264 * (rtadvd) so here we have no function like nd6_ra_output().
274 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
275 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
276 struct nd_router_solicit
*nd_rs
;
277 struct in6_addr saddr6
= ip6
->ip6_src
;
280 union nd_opts ndopts
;
282 /* Expect 32-bit aligned data pointer on strict-align platforms */
283 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
285 /* If I'm not a router, ignore it. */
286 if (!ip6_forwarding
|| !(ifp
->if_eflags
& IFEF_IPV6_ROUTER
))
290 if (ip6
->ip6_hlim
!= 255) {
292 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
293 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
294 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
299 * Don't update the neighbor cache, if src = :: or a non-neighbor.
300 * The former case indicates that the src has no IP address assigned
301 * yet. See nd6_ns_input() for the latter case.
303 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
306 struct sockaddr_in6 src_sa6
;
308 bzero(&src_sa6
, sizeof (src_sa6
));
309 src_sa6
.sin6_family
= AF_INET6
;
310 src_sa6
.sin6_len
= sizeof (src_sa6
);
311 src_sa6
.sin6_addr
= ip6
->ip6_src
;
312 if (!nd6_is_addr_neighbor(&src_sa6
, ifp
, 0)) {
313 nd6log((LOG_INFO
, "nd6_rs_input: "
314 "RS packet from non-neighbor\n"));
319 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
320 nd_rs
= (struct nd_router_solicit
*)((caddr_t
)ip6
+ off
);
321 icmp6len
-= sizeof (*nd_rs
);
322 nd6_option_init(nd_rs
+ 1, icmp6len
, &ndopts
);
323 if (nd6_options(&ndopts
) < 0) {
325 "nd6_rs_input: invalid ND option, ignored\n"));
326 /* nd6_options have incremented stats */
330 if (ndopts
.nd_opts_src_lladdr
) {
331 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
332 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
335 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
337 "nd6_rs_input: lladdrlen mismatch for %s "
338 "(if %d, RS packet %d)\n",
339 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
343 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_ROUTER_SOLICIT
, 0);
350 icmp6stat
.icp6s_badrs
++;
355 * Receive Router Advertisement Message.
358 * TODO: on-link bit on prefix information
359 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
367 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
368 struct nd_ifinfo
*ndi
= NULL
;
369 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
370 struct nd_router_advert
*nd_ra
;
371 struct in6_addr saddr6
= ip6
->ip6_src
;
373 union nd_opts ndopts
;
374 struct nd_defrouter
*dr
= NULL
;
377 u_int32_t lladdrlen
= 0;
378 struct nd_prefix_list
*nd_prefix_list_head
= NULL
;
379 u_int32_t nd_prefix_list_length
= 0;
380 struct in6_ifaddr
*ia6
= NULL
;
381 struct nd_prefix_list
*prfl
;
382 struct nd_defrouter dr0
;
383 u_int32_t advreachable
;
385 #if (DEVELOPMENT || DEBUG)
386 if (ip6_accept_rtadv
== 0)
388 #endif /* (DEVELOPMENT || DEBUG) */
389 /* Expect 32-bit aligned data pointer on strict-align platforms */
390 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
393 * Discard RA unless IFEF_ACCEPT_RTADV is set (as host), or when
394 * IFEF_IPV6_ROUTER is set (as router) but the RA is not locally
395 * generated. For convenience, we allow locally generated (rtadvd)
396 * RAs to be processed on the advertising interface, as a router.
398 * Note that we don't test against ip6_forwarding as we could be
399 * both a host and a router on different interfaces, hence the
400 * check against the per-interface flags.
402 if (!(ifp
->if_eflags
& (IFEF_ACCEPT_RTADV
| IFEF_IPV6_ROUTER
)) ||
403 ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) &&
404 (ia6
= ifa_foraddr6(&saddr6
)) == NULL
))
408 IFA_REMREF(&ia6
->ia_ifa
);
412 if (ip6
->ip6_hlim
!= 255) {
414 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
415 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
416 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
420 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) {
422 "nd6_ra_input: src %s is not link-local\n",
423 ip6_sprintf(&saddr6
)));
427 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
428 nd_ra
= (struct nd_router_advert
*)((caddr_t
)ip6
+ off
);
430 icmp6len
-= sizeof (*nd_ra
);
431 nd6_option_init(nd_ra
+ 1, icmp6len
, &ndopts
);
432 if (nd6_options(&ndopts
) < 0) {
434 "nd6_ra_input: invalid ND option, ignored\n"));
435 /* nd6_options have incremented stats */
439 advreachable
= nd_ra
->nd_ra_reachable
;
441 /* remember if this is a multicasted advertisement */
442 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
445 ndi
= ND_IFINFO(ifp
);
446 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
447 lck_mtx_lock(&ndi
->lock
);
448 bzero(&dr0
, sizeof (dr0
));
450 dr0
.flags
= nd_ra
->nd_ra_flags_reserved
;
451 dr0
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
452 dr0
.expire
= net_uptime() + dr0
.rtlifetime
;
454 /* unspecified or not? (RFC 2461 6.3.4) */
456 advreachable
= ntohl(advreachable
);
457 if (advreachable
<= MAX_REACHABLE_TIME
&&
458 ndi
->basereachable
!= advreachable
) {
459 ndi
->basereachable
= advreachable
;
460 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
461 ndi
->recalctm
= nd6_recalc_reachtm_interval
; /* reset */
464 if (nd_ra
->nd_ra_retransmit
)
465 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
466 if (nd_ra
->nd_ra_curhoplimit
) {
467 if (ndi
->chlim
< nd_ra
->nd_ra_curhoplimit
) {
468 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
469 } else if (ndi
->chlim
!= nd_ra
->nd_ra_curhoplimit
) {
471 "RA with a lower CurHopLimit sent from "
472 "%s on %s (current = %d, received = %d). "
473 "Ignored.\n", ip6_sprintf(&ip6
->ip6_src
),
474 if_name(ifp
), ndi
->chlim
,
475 nd_ra
->nd_ra_curhoplimit
));
478 lck_mtx_unlock(&ndi
->lock
);
479 lck_mtx_lock(nd6_mutex
);
480 dr
= defrtrlist_update(&dr0
);
481 lck_mtx_unlock(nd6_mutex
);
486 if (ndopts
.nd_opts_pi
) {
487 struct nd_opt_hdr
*pt
;
488 struct nd_opt_prefix_info
*pi
= NULL
;
491 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
492 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
493 pt
= (struct nd_opt_hdr
*)((caddr_t
)pt
+
494 (pt
->nd_opt_len
<< 3))) {
495 struct in6_addr pi_mask
;
496 bzero(&pi_mask
, sizeof(pi_mask
));
498 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
)
500 pi
= (struct nd_opt_prefix_info
*)pt
;
502 if (pi
->nd_opt_pi_len
!= 4) {
504 "nd6_ra_input: invalid option "
505 "len %d for prefix information option, "
506 "ignored\n", pi
->nd_opt_pi_len
));
510 if (128 < pi
->nd_opt_pi_prefix_len
) {
512 "nd6_ra_input: invalid prefix "
513 "len %d for prefix information option, "
514 "ignored\n", pi
->nd_opt_pi_prefix_len
));
519 * To ignore ::/64 make sure bits beyond prefixlen
522 in6_prefixlen2mask(&pi_mask
, pi
->nd_opt_pi_prefix_len
);
523 pi
->nd_opt_pi_prefix
.s6_addr32
[0] &= pi_mask
.s6_addr32
[0];
524 pi
->nd_opt_pi_prefix
.s6_addr32
[1] &= pi_mask
.s6_addr32
[1];
525 pi
->nd_opt_pi_prefix
.s6_addr32
[2] &= pi_mask
.s6_addr32
[2];
526 pi
->nd_opt_pi_prefix
.s6_addr32
[3] &= pi_mask
.s6_addr32
[3];
528 if (IN6_IS_ADDR_UNSPECIFIED(&pi
->nd_opt_pi_prefix
) ||
529 IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
) ||
530 IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
532 "%s: invalid prefix %s, ignored\n",
534 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
538 bzero(&pr
, sizeof (pr
));
539 lck_mtx_init(&pr
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
541 pr
.ndpr_prefix
.sin6_family
= AF_INET6
;
542 pr
.ndpr_prefix
.sin6_len
= sizeof (pr
.ndpr_prefix
);
543 pr
.ndpr_prefix
.sin6_addr
= pi
->nd_opt_pi_prefix
;
544 pr
.ndpr_ifp
= m
->m_pkthdr
.rcvif
;
546 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
547 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
548 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
549 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
550 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
551 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
553 ntohl(pi
->nd_opt_pi_preferred_time
);
556 * Exceptions to stateless autoconfiguration processing:
557 * + nd6_accept_6to4 == 0 && address has 6to4 prefix
558 * + ip6_only_allow_rfc4193_prefix != 0 &&
559 * address not RFC 4193
561 if (ip6_only_allow_rfc4193_prefix
&&
562 !IN6_IS_ADDR_UNIQUE_LOCAL(&pi
->nd_opt_pi_prefix
)) {
564 "nd6_ra_input: no SLAAC on prefix %s "
566 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
567 pr
.ndpr_raf_auto
= 0;
568 } else if (!nd6_accept_6to4
&&
569 IN6_IS_ADDR_6TO4(&pi
->nd_opt_pi_prefix
)) {
571 "%s: no SLAAC on prefix %s "
572 "[6to4]\n", __func__
,
573 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
574 pr
.ndpr_raf_auto
= 0;
577 if (in6_init_prefix_ltimes(&pr
)) {
579 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
580 continue; /* prefix lifetime init failed */
584 (void) prelist_update(&pr
, dr
, m
, mcast
);
585 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
588 * We have to copy the values out after the
589 * prelist_update call since some of these values won't
590 * be properly set until after the router advertisement
591 * updating can vet the values.
594 MALLOC(prfl
, struct nd_prefix_list
*, sizeof (*prfl
),
595 M_TEMP
, M_WAITOK
| M_ZERO
);
598 log(LOG_DEBUG
, "%s: unable to MALLOC RA prefix "
599 "structure\n", __func__
);
603 /* this is only for nd6_post_msg(), otherwise unused */
604 bcopy(&pr
.ndpr_prefix
, &prfl
->pr
.ndpr_prefix
,
605 sizeof (prfl
->pr
.ndpr_prefix
));
606 prfl
->pr
.ndpr_raf
= pr
.ndpr_raf
;
607 prfl
->pr
.ndpr_plen
= pr
.ndpr_plen
;
608 prfl
->pr
.ndpr_vltime
= pr
.ndpr_vltime
;
609 prfl
->pr
.ndpr_pltime
= pr
.ndpr_pltime
;
610 prfl
->pr
.ndpr_expire
= pr
.ndpr_expire
;
611 prfl
->pr
.ndpr_base_calendartime
=
612 pr
.ndpr_base_calendartime
;
613 prfl
->pr
.ndpr_base_uptime
= pr
.ndpr_base_uptime
;
614 prfl
->pr
.ndpr_stateflags
= pr
.ndpr_stateflags
;
615 prfl
->pr
.ndpr_addrcnt
= pr
.ndpr_addrcnt
;
616 prfl
->pr
.ndpr_ifp
= pr
.ndpr_ifp
;
618 prfl
->next
= nd_prefix_list_head
;
619 nd_prefix_list_head
= prfl
;
620 nd_prefix_list_length
++;
627 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
628 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
631 if (mtu
< IPV6_MMTU
) {
632 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu option "
633 "mtu=%d sent from %s, ignoring\n",
634 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
638 lck_mtx_lock(&ndi
->lock
);
641 if (mtu
<= ndi
->maxmtu
) {
642 int change
= (ndi
->linkmtu
!= mtu
);
645 lck_mtx_unlock(&ndi
->lock
);
646 if (change
) /* in6_maxmtu may change */
649 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu "
650 "mtu=%d sent from %s; "
651 "exceeds maxmtu %d, ignoring\n",
652 mtu
, ip6_sprintf(&ip6
->ip6_src
),
654 lck_mtx_unlock(&ndi
->lock
);
657 lck_mtx_unlock(&ndi
->lock
);
658 nd6log((LOG_INFO
, "nd6_ra_input: mtu option "
659 "mtu=%d sent from %s; maxmtu unknown, "
661 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
668 * Source link layer address
670 if (ndopts
.nd_opts_src_lladdr
) {
671 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
672 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
675 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
677 "nd6_ra_input: lladdrlen mismatch for %s "
678 "(if %d, RA packet %d)\n",
679 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
683 if (dr
&& dr
->stateflags
& NDDRF_MAPPED
)
684 saddr6
= dr
->rtaddr_mapped
;
686 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, (int)lladdrlen
,
687 ND_ROUTER_ADVERT
, 0);
690 nd6_post_msg(KEV_ND6_RA
, nd_prefix_list_head
, nd_prefix_list_length
,
694 * Installing a link-layer address might change the state of the
695 * router's neighbor cache, which might also affect our on-link
696 * detection of adveritsed prefixes.
698 lck_mtx_lock(nd6_mutex
);
699 pfxlist_onlink_check();
700 lck_mtx_unlock(nd6_mutex
);
708 while ((prfl
= nd_prefix_list_head
) != NULL
) {
709 nd_prefix_list_head
= prfl
->next
;
716 icmp6stat
.icp6s_badra
++;
721 * default router list proccessing sub routines
724 /* tell the change to user processes watching the routing socket. */
726 nd6_rtmsg(int cmd
, struct rtentry
*rt
)
728 struct rt_addrinfo info
;
729 struct ifnet
*ifp
= rt
->rt_ifp
;
731 RT_LOCK_ASSERT_HELD(rt
);
733 bzero((caddr_t
)&info
, sizeof (info
));
734 /* It's not necessary to lock ifp for if_lladdr */
735 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
736 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
737 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
739 * ifa_addr pointers for both should always be valid
740 * in this context; no need to hold locks.
742 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
743 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
745 rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0);
749 defrouter_addreq(struct nd_defrouter
*new, boolean_t scoped
)
751 struct sockaddr_in6 def
, mask
, gate
;
752 struct rtentry
*newrt
= NULL
;
753 unsigned int ifscope
;
755 struct nd_ifinfo
*ndi
= ND_IFINFO(new->ifp
);
757 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
758 NDDR_LOCK_ASSERT_NOTHELD(new);
760 * We're free to lock and unlock NDDR because our callers
761 * are holding an extra reference for us.
765 if (new->stateflags
& NDDRF_INSTALLED
)
768 if (new->ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
769 nd6log2((LOG_INFO
, "%s: ignoring router %s, scoped=%d, "
770 "static=%d on advertising interface\n", if_name(new->ifp
),
771 ip6_sprintf(&new->rtaddr
), scoped
,
772 (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
776 nd6log2((LOG_INFO
, "%s: adding default router %s, scoped=%d, "
777 "static=%d\n", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
),
778 scoped
, (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
780 Bzero(&def
, sizeof (def
));
781 Bzero(&mask
, sizeof (mask
));
782 Bzero(&gate
, sizeof (gate
));
784 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
785 = sizeof (struct sockaddr_in6
);
786 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
788 if (new->stateflags
& NDDRF_MAPPED
)
789 gate
.sin6_addr
= new->rtaddr_mapped
;
791 gate
.sin6_addr
= new->rtaddr
;
793 ifscope
= scoped
? new->ifp
->if_index
: IFSCOPE_NONE
;
797 * Cellular networks may have buggy deployments
798 * with gateway IPv6 link local address with same
799 * interface identifier as the one that has been
800 * assigned for the cellular context.
801 * If gateway is same as locally configured link local
802 * interface on cellular interface, generated a different one
803 * and store it in the nd_defrouter entry and use it to work
806 if (new->ifp
->if_type
== IFT_CELLULAR
&&
807 !(new->stateflags
& NDDRF_STATIC
) &&
808 !(new->stateflags
& NDDRF_MAPPED
) &&
809 IN6_IS_ADDR_LINKLOCAL(&gate
.sin6_addr
) &&
810 ndi
&& !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
811 struct in6_ifaddr
*tmp_ia6
= in6ifa_ifpforlinklocal(new->ifp
, 0);
813 if (tmp_ia6
!= NULL
&&
814 !(tmp_ia6
->ia6_flags
& IN6_IFF_NOTMANUAL
) &&
815 IN6_ARE_ADDR_EQUAL(&tmp_ia6
->ia_addr
.sin6_addr
,
817 gate
.sin6_addr
.s6_addr8
[15] += 1;
818 new->rtaddr_mapped
= gate
.sin6_addr
;
819 new->stateflags
|= NDDRF_MAPPED
;
821 nd6log((LOG_INFO
, "%s: Default router %s mapped "
822 "to ", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
)));
823 nd6log((LOG_INFO
, "%s\n", ip6_sprintf(&new->rtaddr_mapped
)));
827 err
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&def
,
828 (struct sockaddr
*)&gate
, (struct sockaddr
*)&mask
,
829 RTF_GATEWAY
, &newrt
, ifscope
);
833 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */
834 RT_REMREF_LOCKED(newrt
);
837 new->stateflags
|= NDDRF_INSTALLED
;
838 if (ifscope
!= IFSCOPE_NONE
)
839 new->stateflags
|= NDDRF_IFSCOPE
;
841 nd6log((LOG_ERR
, "%s: failed to add default router "
842 "%s on %s scoped %d (errno = %d)\n", __func__
,
843 ip6_sprintf(&gate
.sin6_addr
), if_name(new->ifp
),
844 (ifscope
!= IFSCOPE_NONE
), err
));
853 struct nd_defrouter
*
855 struct in6_addr
*addr
,
858 struct nd_defrouter
*dr
;
860 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
862 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
863 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
865 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) {
866 NDDR_ADDREF_LOCKED(dr
);
873 return (NULL
); /* search failed */
877 * Remove the default route for a given router.
878 * This is just a subroutine function for defrouter_select(), and should
879 * not be called from anywhere else.
882 defrouter_delreq(struct nd_defrouter
*dr
)
884 struct sockaddr_in6 def
, mask
, gate
;
885 struct rtentry
*oldrt
= NULL
;
886 unsigned int ifscope
;
889 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
890 NDDR_LOCK_ASSERT_NOTHELD(dr
);
892 * We're free to lock and unlock NDDR because our callers
893 * are holding an extra reference for us.
896 /* ifp would be NULL for the "drany" case */
897 if (dr
->ifp
!= NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
))
900 nd6log2((LOG_INFO
, "%s: removing default router %s, scoped=%d, "
901 "static=%d\n", dr
->ifp
!= NULL
? if_name(dr
->ifp
) : "ANY",
902 ip6_sprintf(&dr
->rtaddr
), (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
903 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
905 Bzero(&def
, sizeof (def
));
906 Bzero(&mask
, sizeof (mask
));
907 Bzero(&gate
, sizeof (gate
));
909 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
910 = sizeof (struct sockaddr_in6
);
911 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
914 * The router entry may be mapped to a different address.
915 * If that is the case, use the mapped address as gateway
916 * to do operation on the routing table.
917 * To get more context, read the related comment in
920 if (dr
->stateflags
& NDDRF_MAPPED
)
921 gate
.sin6_addr
= dr
->rtaddr_mapped
;
923 gate
.sin6_addr
= dr
->rtaddr
;
925 if (dr
->ifp
!= NULL
) {
926 ifscope
= (dr
->stateflags
& NDDRF_IFSCOPE
) ?
927 dr
->ifp
->if_index
: IFSCOPE_NONE
;
929 ifscope
= IFSCOPE_NONE
;
933 err
= rtrequest_scoped(RTM_DELETE
,
934 (struct sockaddr
*)&def
, (struct sockaddr
*)&gate
,
935 (struct sockaddr
*)&mask
, RTF_GATEWAY
, &oldrt
, ifscope
);
939 nd6_rtmsg(RTM_DELETE
, oldrt
);
942 } else if (err
!= ESRCH
) {
943 nd6log((LOG_ERR
, "%s: failed to delete default router "
944 "%s on %s scoped %d (errno = %d)\n", __func__
,
945 ip6_sprintf(&gate
.sin6_addr
), dr
->ifp
!= NULL
?
946 if_name(dr
->ifp
) : "ANY", (ifscope
!= IFSCOPE_NONE
), err
));
949 /* ESRCH means it's no longer in the routing table; ignore it */
950 if (oldrt
!= NULL
|| err
== ESRCH
) {
951 dr
->stateflags
&= ~NDDRF_INSTALLED
;
952 if (ifscope
!= IFSCOPE_NONE
)
953 dr
->stateflags
&= ~NDDRF_IFSCOPE
;
962 * remove all default routes from default router list
965 defrouter_reset(void)
967 struct nd_defrouter
*dr
, drany
;
969 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
971 dr
= TAILQ_FIRST(&nd_defrouter
);
974 if (dr
->stateflags
& NDDRF_INSTALLED
) {
975 NDDR_ADDREF_LOCKED(dr
);
977 lck_mtx_unlock(nd6_mutex
);
978 defrouter_delreq(dr
);
979 lck_mtx_lock(nd6_mutex
);
981 dr
= TAILQ_FIRST(&nd_defrouter
);
984 dr
= TAILQ_NEXT(dr
, dr_entry
);
988 /* Nuke primary (non-scoped) default router */
989 bzero(&drany
, sizeof (drany
));
990 lck_mtx_init(&drany
.nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
991 lck_mtx_unlock(nd6_mutex
);
992 defrouter_delreq(&drany
);
993 lck_mtx_destroy(&drany
.nddr_lock
, ifa_mtx_grp
);
994 lck_mtx_lock(nd6_mutex
);
998 defrtrlist_ioctl(u_long cmd
, caddr_t data
)
1000 struct nd_defrouter dr0
;
1001 unsigned int ifindex
;
1002 struct ifnet
*dr_ifp
;
1003 int error
= 0, add
= 0;
1005 /* XXX Handle mapped default router entries */
1007 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1008 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1011 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1012 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1013 bzero(&dr0
, sizeof (dr0
));
1014 if (cmd
== SIOCDRADD_IN6_64
|| cmd
== SIOCDRDEL_IN6_64
) {
1015 struct in6_defrouter_64
*r_64
=
1016 (struct in6_defrouter_64
*)(void *)data
;
1019 bcopy(&r_64
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1020 sizeof (dr0
.rtaddr
));
1021 dr0
.flags
= r_64
->flags
;
1022 bcopy(&r_64
->if_index
, &i
, sizeof (i
));
1025 struct in6_defrouter_32
*r_32
=
1026 (struct in6_defrouter_32
*)(void *)data
;
1029 bcopy(&r_32
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
1030 sizeof (dr0
.rtaddr
));
1031 dr0
.flags
= r_32
->flags
;
1032 bcopy(&r_32
->if_index
, &i
, sizeof (i
));
1035 ifnet_head_lock_shared();
1036 /* Don't need to check is ifindex is < 0 since it's unsigned */
1037 if (if_index
< ifindex
||
1038 (dr_ifp
= ifindex2ifnet
[ifindex
]) == NULL
) {
1046 if (ND_IFINFO(dr_ifp
) == NULL
||
1047 !ND_IFINFO(dr_ifp
)->initialized
) {
1052 if (IN6_IS_SCOPE_EMBED(&dr0
.rtaddr
)) {
1053 uint16_t *scope
= &dr0
.rtaddr
.s6_addr16
[1];
1056 *scope
= htons(dr_ifp
->if_index
);
1057 } else if (*scope
!= htons(dr_ifp
->if_index
)) {
1064 error
= defrtrlist_add_static(&dr0
);
1065 if (!add
|| error
!= 0) {
1066 int err
= defrtrlist_del_static(&dr0
);
1073 error
= EOPNOTSUPP
; /* check for safety */
1081 * XXX Please make sure to remove dr from the
1082 * global default router tailq list before this
1084 * Also ensure that you release the list reference
1085 * only after calling this routine.
1088 defrtrlist_del(struct nd_defrouter
*dr
)
1090 #if (DEVELOPMENT || DEBUG)
1091 struct nd_defrouter
*dr_itr
= NULL
;
1093 struct nd_prefix
*pr
;
1094 struct ifnet
*ifp
= dr
->ifp
;
1095 struct nd_ifinfo
*ndi
= NULL
;
1098 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1100 #if (DEVELOPMENT || DEBUG)
1102 * Verify that the router is not in the global default
1104 * Can't use defrouter_lookup here because that just works
1105 * with address and ifp pointer.
1106 * We have to compare the memory here.
1107 * Also we can't use ASSERT here as that is not defined
1108 * for development builds.
1110 TAILQ_FOREACH(dr_itr
, &nd_defrouter
, dr_entry
)
1111 VERIFY(dr
!= dr_itr
);
1113 ++nd6_defrouter_genid
;
1115 * Flush all the routing table entries that use the router
1118 /* above is a good condition? */
1120 lck_mtx_unlock(nd6_mutex
);
1121 if (dr
->stateflags
& NDDRF_MAPPED
)
1122 rt6_flush(&dr
->rtaddr_mapped
, ifp
);
1124 rt6_flush(&dr
->rtaddr
, ifp
);
1126 lck_mtx_lock(nd6_mutex
);
1128 nd6log2((LOG_INFO
, "%s: freeing defrouter %s\n", if_name(dr
->ifp
),
1129 ip6_sprintf(&dr
->rtaddr
)));
1131 * Delete it from the routing table.
1134 lck_mtx_unlock(nd6_mutex
);
1135 defrouter_delreq(dr
);
1136 lck_mtx_lock(nd6_mutex
);
1140 * Also delete all the pointers to the router in each prefix lists.
1142 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1143 struct nd_pfxrouter
*pfxrtr
;
1146 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
1147 pfxrtr_del(pfxrtr
, pr
);
1151 pfxlist_onlink_check();
1154 ndi
= ND_IFINFO(ifp
);
1155 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1156 lck_mtx_lock(&ndi
->lock
);
1157 VERIFY(ndi
->ndefrouters
>= 0);
1158 if (ndi
->ndefrouters
> 0 && --ndi
->ndefrouters
== 0) {
1162 lck_mtx_unlock(&ndi
->lock
);
1165 * If the router is the primary one, choose a new one.
1166 * We always try to pick another eligible router
1167 * on this interface as we do scoped routing
1169 defrouter_select(ifp
);
1176 defrtrlist_add_static(struct nd_defrouter
*new)
1178 struct nd_defrouter
*dr
;
1181 new->rtlifetime
= -1;
1182 new->stateflags
|= NDDRF_STATIC
;
1184 /* we only want the preference level */
1185 new->flags
&= ND_RA_FLAG_RTPREF_MASK
;
1187 lck_mtx_lock(nd6_mutex
);
1188 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1189 if (dr
!= NULL
&& !(dr
->stateflags
& NDDRF_STATIC
)) {
1194 dr
= defrtrlist_update(new);
1202 lck_mtx_unlock(nd6_mutex
);
1208 defrtrlist_del_static(struct nd_defrouter
*new)
1210 struct nd_defrouter
*dr
;
1212 lck_mtx_lock(nd6_mutex
);
1213 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1214 if (dr
== NULL
|| !(dr
->stateflags
& NDDRF_STATIC
)) {
1219 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1221 NDDR_REMREF(dr
); /* remove list reference */
1224 lck_mtx_unlock(nd6_mutex
);
1226 return (dr
!= NULL
? 0 : EINVAL
);
1230 * for default router selection
1231 * regards router-preference field as a 2-bit signed integer
1234 rtpref(struct nd_defrouter
*dr
)
1236 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
1237 case ND_RA_FLAG_RTPREF_HIGH
:
1238 return (RTPREF_HIGH
);
1239 case ND_RA_FLAG_RTPREF_MEDIUM
:
1240 case ND_RA_FLAG_RTPREF_RSV
:
1241 return (RTPREF_MEDIUM
);
1242 case ND_RA_FLAG_RTPREF_LOW
:
1243 return (RTPREF_LOW
);
1246 * This case should never happen. If it did, it would mean a
1247 * serious bug of kernel internal. We thus always bark here.
1248 * Or, can we even panic?
1250 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
1251 return (RTPREF_INVALID
);
1257 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
1259 * 1) Routers that are reachable or probably reachable should be preferred.
1260 * If we have more than one (probably) reachable router, prefer ones
1261 * with the highest router preference.
1262 * 2) When no routers on the list are known to be reachable or
1263 * probably reachable, routers SHOULD be selected in a round-robin
1264 * fashion, regardless of router preference values.
1265 * 3) If the Default Router List is empty, assume that all
1266 * destinations are on-link.
1268 * When Scoped Routing is enabled, the selection logic is amended as follows:
1270 * a) When a default interface is specified, the primary/non-scoped default
1271 * router will be set to the reachable router on that link (if any) with
1272 * the highest router preference.
1273 * b) When there are more than one routers on the same link, the one with
1274 * the highest router preference will be installed, either as scoped or
1275 * non-scoped route entry. If they all share the same preference value,
1276 * the one installed will be the static or the first encountered reachable
1277 * router, i.e. static one wins over dynamic.
1278 * c) When no routers on the list are known to be reachable, or probably
1279 * reachable, no round-robin selection will take place when the default
1282 * We assume nd_defrouter is sorted by router preference value.
1283 * Since the code below covers both with and without router preference cases,
1284 * we do not need to classify the cases by ifdef.
1287 defrouter_select(struct ifnet
*ifp
)
1289 struct nd_defrouter
*dr
= NULL
;
1290 struct nd_defrouter
*selected_dr
= NULL
;
1291 struct nd_defrouter
*installed_dr
= NULL
;
1292 struct llinfo_nd6
*ln
= NULL
;
1293 struct rtentry
*rt
= NULL
;
1294 struct nd_ifinfo
*ndi
= NULL
;
1295 unsigned int genid
= 0;
1296 boolean_t is_installed_reachable
= FALSE
;
1298 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1302 "%s:%d: Return early. NULL interface",
1303 __func__
, __LINE__
));
1307 if (ifp
== lo_ifp
) {
1309 "%s:%d: Return early. "
1310 "Default router select called for loopback.\n",
1311 __func__
, __LINE__
));
1315 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1317 "%s:%d: Return early. "
1318 "Default router select called for interface"
1319 " %s with IFEF_IPV6_ROUTER flag set\n",
1320 __func__
, __LINE__
, if_name(ifp
)));
1325 * Let's handle easy case (3) first:
1326 * If default router list is empty, there's nothing to be done.
1328 if (!TAILQ_FIRST(&nd_defrouter
)) {
1330 "%s:%d: Return early. "
1331 "Default router is empty.\n", __func__
, __LINE__
));
1336 * Take an early exit if number of routers in nd_ifinfo is
1337 * 0 for the interface.
1339 ndi
= ND_IFINFO(ifp
);
1340 if (!ndi
|| !ndi
->initialized
) {
1342 "%s:%d: Return early. "
1343 "Interface %s's nd_ifinfo not initialized.\n",
1344 __func__
, __LINE__
, if_name(ifp
)));
1348 if (ndi
->ndefrouters
== 0) {
1350 "%s:%d: Return early. "
1351 "%s does not have any default routers.\n",
1352 __func__
, __LINE__
, if_name(ifp
)));
1357 * Due to the number of times we drop nd6_mutex, we need to
1358 * serialize this function.
1360 while (nd_defrouter_busy
) {
1361 nd_defrouter_waiters
++;
1362 msleep(nd_defrouter_waitchan
, nd6_mutex
, (PZERO
-1),
1364 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1366 nd_defrouter_busy
= TRUE
;
1369 * Search for a (probably) reachable router from the list.
1370 * We just pick up the first reachable one (if any), assuming that
1371 * the ordering rule of the list described in defrtrlist_update().
1373 * For all intents and purposes of Scoped Routing:
1374 * selected_dr = candidate for primary router
1375 * installed_dr = currently installed primary router
1377 genid
= nd6_defrouter_genid
;
1378 dr
= TAILQ_FIRST(&nd_defrouter
);
1380 while (dr
!= NULL
) {
1381 struct in6_addr rtaddr
;
1382 struct ifnet
*drifp
= NULL
;
1383 struct nd_defrouter
*drrele
= NULL
;
1389 dr
= TAILQ_NEXT(dr
, dr_entry
);
1394 * Optimize for the common case.
1395 * When the interface has only one default router
1396 * there's no point checking for reachability as
1397 * there's nothing else to choose from.
1399 if (ndi
->ndefrouters
== 1) {
1401 "%s:%d: Fast forward default router selection "
1402 "as interface %s has learned only one default "
1403 "router and there's nothing else to choose from.\n",
1404 __func__
, __LINE__
, if_name(ifp
)));
1405 VERIFY(selected_dr
== NULL
&& installed_dr
== NULL
);
1407 if (dr
->stateflags
& NDDRF_INSTALLED
)
1409 NDDR_ADDREF_LOCKED(selected_dr
);
1414 if (dr
->stateflags
& NDDRF_MAPPED
)
1415 rtaddr
= dr
->rtaddr_mapped
;
1417 rtaddr
= dr
->rtaddr
;
1419 NDDR_ADDREF_LOCKED(dr
); /* for this for loop */
1422 /* Callee returns a locked route upon success */
1423 if (selected_dr
== NULL
) {
1424 lck_mtx_unlock(nd6_mutex
);
1425 if ((rt
= nd6_lookup(&rtaddr
, 0, drifp
, 0)) != NULL
&&
1426 (ln
= rt
->rt_llinfo
) != NULL
&&
1427 ND6_IS_LLINFO_PROBREACH(ln
)) {
1428 RT_LOCK_ASSERT_HELD(rt
);
1430 NDDR_ADDREF(selected_dr
);
1432 lck_mtx_lock(nd6_mutex
);
1436 RT_REMREF_LOCKED(rt
);
1443 * When there are more than one routers on the same link, the one with
1444 * the highest router preference will be installed.
1445 * Since the list is in decreasing order of preference:
1446 * 1) If selected_dr is not NULL, only use dr if it is static and has
1447 * equal preference and selected_dr is not static.
1448 * 2) Else if selected_dr is NULL, and dr is static make selected_dr = dr
1451 if (((selected_dr
&& (rtpref(dr
) >= rtpref(selected_dr
)) &&
1452 !(selected_dr
->stateflags
& NDDRF_STATIC
)) ||
1453 (selected_dr
== NULL
)) &&
1454 (dr
->stateflags
& NDDRF_STATIC
)) {
1456 /* Release it later on */
1457 VERIFY(drrele
== NULL
);
1458 drrele
= selected_dr
;
1461 NDDR_ADDREF_LOCKED(selected_dr
);
1464 /* Record the currently installed router */
1465 if (dr
->stateflags
& NDDRF_INSTALLED
) {
1466 if (installed_dr
== NULL
) {
1468 NDDR_ADDREF_LOCKED(installed_dr
);
1469 if (dr
->stateflags
& NDDRF_MAPPED
)
1470 rtaddr
= installed_dr
->rtaddr_mapped
;
1472 rtaddr
= installed_dr
->rtaddr
;
1474 lck_mtx_unlock(nd6_mutex
);
1475 /* Callee returns a locked route upon success */
1476 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
1477 RT_LOCK_ASSERT_HELD(rt
);
1478 if ((ln
= rt
->rt_llinfo
) != NULL
&&
1479 ND6_IS_LLINFO_PROBREACH(ln
))
1480 is_installed_reachable
= TRUE
;
1482 RT_REMREF_LOCKED(rt
);
1486 lck_mtx_lock(nd6_mutex
);
1488 /* this should not happen; warn for diagnosis */
1489 nd6log((LOG_ERR
, "defrouter_select: more than one "
1490 "default router is installed for interface :%s.\n",
1497 NDDR_REMREF(dr
); /* for this for loop */
1499 NDDR_REMREF(drrele
);
1502 * Check if the list changed when we gave up
1503 * the nd6_mutex lock
1505 if(genid
!= nd6_defrouter_genid
) {
1507 NDDR_REMREF(selected_dr
);
1512 NDDR_REMREF(installed_dr
);
1513 installed_dr
= NULL
;
1516 if (ndi
->ndefrouters
== 0) {
1518 "%s:%d: Interface %s no longer "
1519 "has any default routers. Abort.\n",
1520 __func__
, __LINE__
, if_name(ifp
)));
1524 "%s:%d: Iterate default router list again "
1525 "for interface %s, as the list seems to have "
1526 "changed during release-reaquire of global "
1527 "nd6_mutex lock.\n",
1528 __func__
, __LINE__
, if_name(ifp
)));
1530 is_installed_reachable
= FALSE
;
1531 genid
= nd6_defrouter_genid
;
1532 dr
= TAILQ_FIRST(&nd_defrouter
);
1534 dr
= TAILQ_NEXT(dr
, dr_entry
);
1539 * If none of the default routers was found to be reachable,
1540 * round-robin the list regardless of preference.
1541 * Please note selected_dr equal to NULL implies that even
1542 * installed default router is not reachable
1544 if (selected_dr
== NULL
) {
1546 for (dr
= TAILQ_NEXT(installed_dr
, dr_entry
); dr
;
1547 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1548 if (installed_dr
->ifp
!= dr
->ifp
)
1556 * If none was installed or the installed one if the last
1557 * one on the list, select the first one from the list
1559 if ((installed_dr
== NULL
) || (selected_dr
== NULL
)) {
1560 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1561 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1562 if (dr
->ifp
== ifp
) {
1569 if ((selected_dr
== NULL
) && (installed_dr
== NULL
)) {
1571 "%s:%d: Between release and reaquire of global "
1572 "nd6_mutex lock, the list seems to have changed "
1573 "and it does not have any default routers for "
1575 __func__
, __LINE__
, if_name(ifp
)));
1579 if (selected_dr
!= installed_dr
)
1580 NDDR_ADDREF(selected_dr
);
1581 } else if (installed_dr
!= NULL
) {
1582 if (installed_dr
!= selected_dr
) {
1584 * This means that selected default router is reachable
1585 * while installed one may or may not be.
1586 * Static router should always be considered as reachable
1587 * for router selection process.
1589 if ((installed_dr
->stateflags
& NDDRF_STATIC
) &&
1590 rtpref(installed_dr
) >= rtpref(selected_dr
)) {
1591 NDDR_REMREF(selected_dr
);
1592 selected_dr
= installed_dr
;
1593 } else if (is_installed_reachable
) {
1594 if (rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1595 NDDR_REMREF(selected_dr
);
1596 selected_dr
= installed_dr
;
1600 NDDR_REMREF(selected_dr
);
1606 * If the selected router is different than the installed one,
1607 * remove the installed router and install the selected one.
1608 * Note that the selected router is never NULL here.
1609 * Else check if the route entry scope has to be changed.
1611 lck_mtx_unlock(nd6_mutex
);
1612 if (installed_dr
!= selected_dr
) {
1614 "%s:%d: Found a better router for interface "
1615 "%s. Installing new default route.\n",
1616 __func__
, __LINE__
, if_name(ifp
)));
1617 if (installed_dr
!= NULL
) {
1618 defrouter_delreq(installed_dr
);
1621 * Install scoped route if the interface is not
1622 * the default nd6 interface.
1624 defrouter_addreq(selected_dr
,
1625 (selected_dr
->ifp
!= nd6_defifp
));
1626 } else if (((installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1627 (installed_dr
->ifp
== nd6_defifp
)) ||
1628 (!(installed_dr
->stateflags
& NDDRF_IFSCOPE
) &&
1629 (installed_dr
->ifp
!= nd6_defifp
))) {
1631 "%s:%d: Need to reinstall default route for interface "
1632 "%s as its scope has changed.\n",
1633 __func__
, __LINE__
, if_name(ifp
)));
1634 defrouter_delreq(installed_dr
);
1635 defrouter_addreq(installed_dr
,
1636 (installed_dr
->ifp
!= nd6_defifp
));
1639 "%s:%d: No need to change the default "
1640 "route for interface %s.\n",
1641 __func__
, __LINE__
, if_name(ifp
)));
1643 lck_mtx_lock(nd6_mutex
);
1645 if (selected_dr
&& (selected_dr
!= installed_dr
))
1646 NDDR_REMREF(selected_dr
);
1648 NDDR_REMREF(installed_dr
);
1649 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1650 VERIFY(nd_defrouter_busy
);
1651 nd_defrouter_busy
= FALSE
;
1652 if (nd_defrouter_waiters
> 0) {
1653 nd_defrouter_waiters
= 0;
1654 wakeup(nd_defrouter_waitchan
);
1658 static struct nd_defrouter
*
1659 defrtrlist_update_common(struct nd_defrouter
*new, boolean_t scoped
)
1661 struct nd_defrouter
*dr
, *n
;
1662 struct ifnet
*ifp
= new->ifp
;
1663 struct nd_ifinfo
*ndi
= NULL
;
1664 struct timeval caltime
;
1666 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1668 if ((dr
= defrouter_lookup(&new->rtaddr
, ifp
)) != NULL
) {
1670 if (new->rtlifetime
== 0) {
1671 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1673 NDDR_REMREF(dr
); /* remove list reference */
1677 int oldpref
= rtpref(dr
);
1678 struct nd_defrouter
*p
= NULL
;
1680 dr
->flags
= new->flags
; /* xxx flag check */
1681 dr
->rtlifetime
= new->rtlifetime
;
1682 dr
->expire
= new->expire
;
1685 * If the preference does not change, there's no need
1686 * to sort the entries. If Scoped Routing is enabled,
1687 * put the primary/non-scoped router at the top of the
1688 * list of routers in the same preference band, unless
1689 * it's already at that position.
1691 /* same preference and scoped; just return */
1692 if (rtpref(new) == oldpref
&& scoped
)
1695 n
= TAILQ_FIRST(&nd_defrouter
);
1697 /* preference changed; sort it */
1698 if (rtpref(new) != oldpref
)
1701 /* not at the top of band; sort it */
1702 if (n
!= dr
&& rtpref(n
) == oldpref
&&
1703 (!p
|| rtpref(p
) > rtpref(n
)))
1707 n
= TAILQ_NEXT(n
, dr_entry
);
1710 /* nothing has changed, just return */
1711 if (n
== NULL
&& (scoped
||
1712 !(dr
->stateflags
& NDDRF_IFSCOPE
)))
1716 * preferred router may be changed, so relocate
1718 * XXX: calling TAILQ_REMOVE directly is a bad manner.
1719 * However, since defrtrlist_del() has many side
1720 * effects, we intentionally do so here.
1721 * defrouter_select() below will handle routing
1724 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1725 new->stateflags
= dr
->stateflags
;
1735 /* entry does not exist */
1736 if (new->rtlifetime
== 0) {
1740 n
= nddr_alloc(M_WAITOK
);
1745 ndi
= ND_IFINFO(ifp
);
1746 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1747 lck_mtx_lock(&ndi
->lock
);
1748 if (ip6_maxifdefrouters
>= 0 &&
1749 ndi
->ndefrouters
>= ip6_maxifdefrouters
) {
1750 lck_mtx_unlock(&ndi
->lock
);
1755 NDDR_ADDREF(n
); /* for the nd_defrouter list */
1756 NDDR_ADDREF(n
); /* for the caller */
1758 ++nd6_defrouter_genid
;
1760 VERIFY(ndi
->ndefrouters
!= 0);
1761 lck_mtx_unlock(&ndi
->lock
);
1763 nd6log2((LOG_INFO
, "%s: allocating defrouter %s\n", if_name(ifp
),
1764 ip6_sprintf(&new->rtaddr
)));
1766 getmicrotime(&caltime
);
1768 memcpy(&n
->rtaddr
, &new->rtaddr
, sizeof (n
->rtaddr
));
1769 n
->flags
= new->flags
;
1770 n
->stateflags
= new->stateflags
;
1771 n
->rtlifetime
= new->rtlifetime
;
1772 n
->expire
= new->expire
;
1773 n
->base_calendartime
= caltime
.tv_sec
;
1774 n
->base_uptime
= net_uptime();
1779 /* get nd6_service() to be scheduled as soon as it's convenient */
1780 ++nd6_sched_timeout_want
;
1783 * Insert the new router in the Default Router List;
1784 * The Default Router List should be in the descending order
1785 * of router-preferece. When Scoped Routing is disabled, routers
1786 * with the same preference are sorted in the arriving time order;
1787 * otherwise, the first entry in the list of routers having the same
1788 * preference is the primary default router, when the interface used
1789 * by the entry is the default interface.
1792 /* insert at the end of the group */
1793 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1794 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1795 if (rtpref(n
) > rtpref(dr
) ||
1796 (!scoped
&& rtpref(n
) == rtpref(dr
)))
1800 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1802 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
1804 defrouter_select(ifp
);
1809 static struct nd_defrouter
*
1810 defrtrlist_update(struct nd_defrouter
*new)
1812 struct nd_defrouter
*dr
;
1814 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1815 dr
= defrtrlist_update_common(new,
1816 (nd6_defifp
!= NULL
&& new->ifp
!= nd6_defifp
));
1821 static struct nd_pfxrouter
*
1822 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1824 struct nd_pfxrouter
*search
;
1826 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1827 NDPR_LOCK_ASSERT_HELD(pr
);
1829 for (search
= pr
->ndpr_advrtrs
.lh_first
; search
;
1830 search
= search
->pfr_next
) {
1831 if (search
->router
== dr
)
1839 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1841 struct nd_pfxrouter
*new;
1843 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1844 NDPR_LOCK_ASSERT_NOTHELD(pr
);
1846 new = zalloc(ndprtr_zone
);
1849 bzero(new, sizeof (*new));
1853 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
1857 pfxlist_onlink_check();
1861 pfxrtr_del(struct nd_pfxrouter
*pfr
, struct nd_prefix
*pr
)
1863 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1864 NDPR_LOCK_ASSERT_HELD(pr
);
1866 LIST_REMOVE(pfr
, pfr_entry
);
1867 zfree(ndprtr_zone
, pfr
);
1871 * The routine has been modified to atomically refresh expiry
1872 * time for nd6 prefix as the part of lookup.
1873 * There's a corner case where a system going
1874 * in sleep gets rid of manual addresses configured in the system
1875 * and then schedules the prefix for deletion.
1876 * However before the prefix gets deleted, if system comes out
1877 * from sleep and configures same address before prefix deletion
1878 * , the later prefix deletion will remove the prefix route and
1879 * the system will not be able to communicate with other IPv6
1880 * neighbor nodes in the same subnet.
1883 nd6_prefix_lookup(struct nd_prefix
*pr
, int nd6_prefix_expiry
)
1885 struct nd_prefix
*search
;
1887 lck_mtx_lock(nd6_mutex
);
1888 for (search
= nd_prefix
.lh_first
; search
; search
= search
->ndpr_next
) {
1890 if (pr
->ndpr_ifp
== search
->ndpr_ifp
&&
1891 pr
->ndpr_plen
== search
->ndpr_plen
&&
1892 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1893 &search
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1894 if (nd6_prefix_expiry
!= ND6_PREFIX_EXPIRY_UNSPEC
) {
1895 search
->ndpr_expire
= nd6_prefix_expiry
;
1897 NDPR_ADDREF_LOCKED(search
);
1898 NDPR_UNLOCK(search
);
1901 NDPR_UNLOCK(search
);
1903 lck_mtx_unlock(nd6_mutex
);
1909 nd6_prelist_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
,
1910 struct nd_prefix
**newp
, boolean_t force_scoped
)
1912 struct nd_prefix
*new = NULL
;
1913 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1914 struct nd_ifinfo
*ndi
= NULL
;
1917 if (ip6_maxifprefixes
>= 0) {
1918 ndi
= ND_IFINFO(ifp
);
1919 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1920 lck_mtx_lock(&ndi
->lock
);
1921 if (ndi
->nprefixes
>= ip6_maxifprefixes
) {
1922 lck_mtx_unlock(&ndi
->lock
);
1925 lck_mtx_unlock(&ndi
->lock
);
1928 new = ndpr_alloc(M_WAITOK
);
1934 new->ndpr_ifp
= pr
->ndpr_ifp
;
1935 new->ndpr_prefix
= pr
->ndpr_prefix
;
1936 new->ndpr_plen
= pr
->ndpr_plen
;
1937 new->ndpr_vltime
= pr
->ndpr_vltime
;
1938 new->ndpr_pltime
= pr
->ndpr_pltime
;
1939 new->ndpr_flags
= pr
->ndpr_flags
;
1940 if (pr
->ndpr_stateflags
& NDPRF_STATIC
)
1941 new->ndpr_stateflags
|= NDPRF_STATIC
;
1943 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
1948 new->ndpr_lastupdate
= net_uptime();
1951 NDPR_ADDREF_LOCKED(new); /* for caller */
1953 /* initialization */
1954 LIST_INIT(&new->ndpr_advrtrs
);
1955 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
1956 /* make prefix in the canonical form */
1957 for (i
= 0; i
< 4; i
++)
1958 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1959 new->ndpr_mask
.s6_addr32
[i
];
1963 /* get nd6_service() to be scheduled as soon as it's convenient */
1964 ++nd6_sched_timeout_want
;
1966 lck_mtx_lock(nd6_mutex
);
1967 /* link ndpr_entry to nd_prefix list */
1968 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
1969 new->ndpr_debug
|= IFD_ATTACHED
;
1970 NDPR_ADDREF(new); /* for nd_prefix list */
1972 lck_mtx_lock(&ndi
->lock
);
1974 VERIFY(ndi
->nprefixes
!= 0);
1975 lck_mtx_unlock(&ndi
->lock
);
1977 /* ND_OPT_PI_FLAG_ONLINK processing */
1978 if (new->ndpr_raf_onlink
) {
1981 if ((e
= nd6_prefix_onlink_common(new, force_scoped
,
1982 new->ndpr_ifp
->if_index
)) != 0) {
1983 nd6log((LOG_ERR
, "nd6_prelist_add: failed to make "
1984 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
1985 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
1986 new->ndpr_plen
, force_scoped
? "scoped" :
1987 "non-scoped", if_name(ifp
), e
));
1988 /* proceed anyway. XXX: is it correct? */
1993 pfxrtr_add(new, dr
);
1996 lck_mtx_unlock(nd6_mutex
);
2002 * Caller must have held an extra reference on nd_prefix.
2005 prelist_remove(struct nd_prefix
*pr
)
2007 struct nd_pfxrouter
*pfr
, *next
;
2008 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2010 struct nd_ifinfo
*ndi
= NULL
;
2012 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2013 NDPR_LOCK_ASSERT_HELD(pr
);
2015 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
2019 * If there are no more addresses, defunct the prefix. This is needed
2020 * because we don't want multiple threads calling prelist_remove() for
2021 * the same prefix and this might happen because we unlock nd6_mutex
2024 if (pr
->ndpr_addrcnt
== 0)
2025 pr
->ndpr_stateflags
|= NDPRF_DEFUNCT
;
2027 /* make sure to invalidate the prefix until it is really freed. */
2028 pr
->ndpr_vltime
= 0;
2029 pr
->ndpr_pltime
= 0;
2032 * Though these flags are now meaningless, we'd rather keep the value
2033 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2034 * when executing "ndp -p".
2036 if (pr
->ndpr_stateflags
& NDPRF_ONLINK
) {
2037 NDPR_ADDREF_LOCKED(pr
);
2039 lck_mtx_unlock(nd6_mutex
);
2040 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2041 nd6log((LOG_ERR
, "prelist_remove: failed to make "
2042 "%s/%d offlink on %s, errno=%d\n",
2043 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2044 pr
->ndpr_plen
, if_name(ifp
), e
));
2045 /* what should we do? */
2047 lck_mtx_lock(nd6_mutex
);
2049 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2053 if (pr
->ndpr_addrcnt
> 0) {
2055 * The state might have changed if we called
2056 * nd6_prefix_offlink().
2058 pr
->ndpr_stateflags
&= ~NDPRF_DEFUNCT
;
2059 return; /* notice here? */
2062 /* unlink ndpr_entry from nd_prefix list */
2063 LIST_REMOVE(pr
, ndpr_entry
);
2064 pr
->ndpr_debug
&= ~IFD_ATTACHED
;
2066 /* free list of routers that adversed the prefix */
2067 for (pfr
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr
= next
) {
2068 next
= pfr
->pfr_next
;
2069 pfxrtr_del(pfr
, pr
);
2072 ndi
= ND_IFINFO(ifp
);
2073 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2074 lck_mtx_lock(&ndi
->lock
);
2075 VERIFY(ndi
->nprefixes
> 0);
2077 lck_mtx_unlock(&ndi
->lock
);
2079 /* This must not be the last reference to the nd_prefix */
2080 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2081 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__
, pr
);
2086 * Don't call pfxlist_onlink_check() here because we are
2087 * holding the NDPR lock and this could cause a deadlock when
2088 * there are multiple threads executing pfxlist_onlink_check().
2094 struct nd_prefix
*new,
2095 struct nd_defrouter
*dr
, /* may be NULL */
2099 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
2101 struct ifnet
*ifp
= new->ndpr_ifp
;
2102 struct nd_prefix
*pr
;
2106 struct in6_addrlifetime lt6_tmp
;
2107 uint64_t timenow
= net_uptime();
2109 /* no need to lock "new" here, as it is local to the caller */
2110 NDPR_LOCK_ASSERT_NOTHELD(new);
2115 * Authenticity for NA consists authentication for
2116 * both IP header and IP datagrams, doesn't it ?
2118 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
2119 auth
= (m
->m_flags
& M_AUTHIPHDR
) && (m
->m_flags
& M_AUTHIPDGM
);
2123 if ((pr
= nd6_prefix_lookup(new, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
2125 * nd6_prefix_lookup() ensures that pr and new have the same
2126 * prefix on a same interface.
2130 * Update prefix information. Note that the on-link (L) bit
2131 * and the autonomous (A) bit should NOT be changed from 1
2134 lck_mtx_lock(nd6_mutex
);
2136 if (new->ndpr_raf_onlink
== 1)
2137 pr
->ndpr_raf_onlink
= 1;
2138 if (new->ndpr_raf_auto
== 1)
2139 pr
->ndpr_raf_auto
= 1;
2140 if (new->ndpr_raf_onlink
) {
2141 pr
->ndpr_vltime
= new->ndpr_vltime
;
2142 pr
->ndpr_pltime
= new->ndpr_pltime
;
2143 (void) in6_init_prefix_ltimes(pr
); /* XXX error case? */
2144 pr
->ndpr_lastupdate
= net_uptime();
2147 NDPR_ADDREF_LOCKED(pr
);
2148 if (new->ndpr_raf_onlink
&&
2149 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
2153 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2155 "prelist_update: failed to make "
2156 "the prefix %s/%d on-link on %s "
2158 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2159 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
2160 /* proceed anyway. XXX: is it correct? */
2165 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
) {
2172 lck_mtx_unlock(nd6_mutex
);
2176 if (new->ndpr_vltime
== 0)
2178 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
2181 bzero(&new->ndpr_addr
, sizeof (struct in6_addr
));
2183 error
= nd6_prelist_add(new, dr
, &pr
, FALSE
);
2184 if (error
!= 0 || pr
== NULL
) {
2185 nd6log((LOG_NOTICE
, "prelist_update: "
2186 "nd6_prelist_add failed for %s/%d on %s "
2187 "errno=%d, returnpr=0x%llx\n",
2188 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2189 new->ndpr_plen
, if_name(new->ndpr_ifp
),
2190 error
, (uint64_t)VM_KERNEL_ADDRPERM(pr
)));
2191 goto end
; /* we should just give up in this case. */
2196 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
2197 * Note that pr must be non NULL at this point.
2200 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
2201 if (!new->ndpr_raf_auto
)
2205 * 5.5.3 (b). the link-local prefix should have been ignored in
2209 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2210 if (new->ndpr_pltime
> new->ndpr_vltime
) {
2211 error
= EINVAL
; /* XXX: won't be used */
2216 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2217 * an address configured by stateless autoconfiguration already in the
2218 * list of addresses associated with the interface, and the Valid
2219 * Lifetime is not 0, form an address. We first check if we have
2220 * a matching prefix.
2222 ifnet_lock_shared(ifp
);
2223 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2224 struct in6_ifaddr
*ifa6
;
2225 u_int32_t remaininglifetime
;
2228 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2232 ifa6
= (struct in6_ifaddr
*)ifa
;
2235 * We only consider autoconfigured addresses as per RFC 4862.
2237 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
2242 * Spec is not clear here, but I believe we should concentrate
2243 * on unicast (i.e. not anycast) addresses.
2244 * XXX: other ia6_flags? detached or duplicated?
2246 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0) {
2251 * Ignore the address if it is not associated with a prefix
2252 * or is associated with a prefix that is different from this
2253 * one. (pr is never NULL here)
2255 if (ifa6
->ia6_ndpr
!= pr
) {
2260 if (ia6_match
== NULL
) { /* remember the first one */
2262 IFA_ADDREF_LOCKED(ifa
); /* for ia6_match */
2266 * An already autoconfigured address matched. Now that we
2267 * are sure there is at least one matched address, we can
2268 * proceed to 5.5.3. (e): update the lifetimes according to the
2269 * "two hours" rule and the privacy extension.
2271 #define TWOHOUR (120*60)
2273 /* retrieve time as uptime (last arg is 0) */
2274 in6ifa_getlifetime(ifa6
, <6_tmp
, 0);
2276 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
2277 remaininglifetime
= ND6_INFINITE_LIFETIME
;
2278 else if (timenow
- ifa6
->ia6_updatetime
> lt6_tmp
.ia6t_vltime
) {
2280 * The case of "invalid" address. We should usually
2281 * not see this case.
2283 remaininglifetime
= 0;
2285 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
2286 (timenow
- ifa6
->ia6_updatetime
);
2288 /* when not updating, keep the current stored lifetime. */
2289 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
2291 if (TWOHOUR
< new->ndpr_vltime
||
2292 remaininglifetime
< new->ndpr_vltime
) {
2293 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2294 } else if (remaininglifetime
<= TWOHOUR
) {
2296 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2300 * new->ndpr_vltime <= TWOHOUR &&
2301 * TWOHOUR < remaininglifetime
2303 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
2306 /* The 2 hour rule is not imposed for preferred lifetime. */
2307 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
2309 /* Special handling for lifetimes of temporary addresses. */
2310 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
2311 u_int32_t maxvltime
, maxpltime
;
2313 /* Constrain lifetimes to system limits. */
2314 if (lt6_tmp
.ia6t_vltime
> ip6_temp_valid_lifetime
)
2315 lt6_tmp
.ia6t_vltime
= ip6_temp_valid_lifetime
;
2316 if (lt6_tmp
.ia6t_pltime
> ip6_temp_preferred_lifetime
)
2317 lt6_tmp
.ia6t_pltime
=
2318 ip6_temp_preferred_lifetime
-
2322 * According to RFC 4941, section 3.3 (1), we only
2323 * update the lifetimes when they are in the maximum
2326 if (ip6_temp_valid_lifetime
>
2327 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2328 ip6_desync_factor
)) {
2329 maxvltime
= ip6_temp_valid_lifetime
-
2330 (timenow
- ifa6
->ia6_createtime
) -
2334 if (ip6_temp_preferred_lifetime
>
2335 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2336 ip6_desync_factor
)) {
2337 maxpltime
= ip6_temp_preferred_lifetime
-
2338 (timenow
- ifa6
->ia6_createtime
) -
2343 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
2344 lt6_tmp
.ia6t_vltime
> maxvltime
)
2345 lt6_tmp
.ia6t_vltime
= maxvltime
;
2347 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
2348 lt6_tmp
.ia6t_pltime
> maxpltime
)
2349 lt6_tmp
.ia6t_pltime
= maxpltime
;
2352 in6_init_address_ltimes(pr
, <6_tmp
);
2354 in6ifa_setlifetime(ifa6
, <6_tmp
);
2355 ifa6
->ia6_updatetime
= timenow
;
2358 ifnet_lock_done(ifp
);
2359 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
2361 * 5.5.3 (d) (continued)
2362 * No address matched and the valid lifetime is non-zero.
2363 * Create a new address.
2366 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
))
2369 * note that we should use pr (not new) for reference.
2371 IFA_LOCK(&ia6
->ia_ifa
);
2374 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2376 VERIFY(pr
->ndpr_addrcnt
!= 0);
2378 IFA_UNLOCK(&ia6
->ia_ifa
);
2382 * When a new public address is created as described
2383 * in RFC 4862, also create a new temporary address.
2386 * When an interface connects to a new link, a new
2387 * randomized interface identifier should be generated
2388 * immediately together with a new set of temporary
2389 * addresses. Thus, we specifiy 1 as the 2nd arg of
2392 if (ip6_use_tempaddr
) {
2394 if ((e
= in6_tmpifadd(ia6
, 1)) != 0) {
2395 nd6log((LOG_NOTICE
, "prelist_update: "
2396 "failed to create a temporary "
2397 "address, errno=%d\n",
2401 IFA_REMREF(&ia6
->ia_ifa
);
2405 * A newly added address might affect the status
2406 * of other addresses, so we check and update it.
2407 * XXX: what if address duplication happens?
2409 lck_mtx_lock(nd6_mutex
);
2410 pfxlist_onlink_check();
2411 lck_mtx_unlock(nd6_mutex
);
2418 if (ia6_match
!= NULL
)
2419 IFA_REMREF(&ia6_match
->ia_ifa
);
2424 * Neighbor Discover Default Router structure reference counting routines.
2426 static struct nd_defrouter
*
2429 struct nd_defrouter
*dr
;
2431 dr
= (how
== M_WAITOK
) ? zalloc(nddr_zone
) : zalloc_noblock(nddr_zone
);
2433 bzero(dr
, nddr_size
);
2434 lck_mtx_init(&dr
->nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2435 dr
->nddr_debug
|= IFD_ALLOC
;
2436 if (nddr_debug
!= 0) {
2437 dr
->nddr_debug
|= IFD_DEBUG
;
2438 dr
->nddr_trace
= nddr_trace
;
2445 nddr_free(struct nd_defrouter
*dr
)
2448 if (dr
->nddr_debug
& IFD_ATTACHED
) {
2449 panic("%s: attached nddr %p is being freed", __func__
, dr
);
2451 } else if (!(dr
->nddr_debug
& IFD_ALLOC
)) {
2452 panic("%s: nddr %p cannot be freed", __func__
, dr
);
2455 dr
->nddr_debug
&= ~IFD_ALLOC
;
2458 lck_mtx_destroy(&dr
->nddr_lock
, ifa_mtx_grp
);
2459 zfree(nddr_zone
, dr
);
2463 nddr_trace(struct nd_defrouter
*dr
, int refhold
)
2465 struct nd_defrouter_dbg
*dr_dbg
= (struct nd_defrouter_dbg
*)dr
;
2470 if (!(dr
->nddr_debug
& IFD_DEBUG
)) {
2471 panic("%s: nddr %p has no debug structure", __func__
, dr
);
2475 cnt
= &dr_dbg
->nddr_refhold_cnt
;
2476 tr
= dr_dbg
->nddr_refhold
;
2478 cnt
= &dr_dbg
->nddr_refrele_cnt
;
2479 tr
= dr_dbg
->nddr_refrele
;
2482 idx
= atomic_add_16_ov(cnt
, 1) % NDDR_TRACE_HIST_SIZE
;
2483 ctrace_record(&tr
[idx
]);
2487 nddr_addref(struct nd_defrouter
*nddr
, int locked
)
2491 NDDR_LOCK_SPIN(nddr
);
2493 NDDR_LOCK_ASSERT_HELD(nddr
);
2495 if (++nddr
->nddr_refcount
== 0) {
2496 panic("%s: nddr %p wraparound refcnt\n", __func__
, nddr
);
2498 } else if (nddr
->nddr_trace
!= NULL
) {
2499 (*nddr
->nddr_trace
)(nddr
, TRUE
);
2506 struct nd_defrouter
*
2507 nddr_remref(struct nd_defrouter
*nddr
, int locked
)
2511 NDDR_LOCK_SPIN(nddr
);
2513 NDDR_LOCK_ASSERT_HELD(nddr
);
2515 if (nddr
->nddr_refcount
== 0) {
2516 panic("%s: nddr %p negative refcnt\n", __func__
, nddr
);
2518 } else if (nddr
->nddr_trace
!= NULL
) {
2519 (*nddr
->nddr_trace
)(nddr
, FALSE
);
2522 if (--nddr
->nddr_refcount
== 0) {
2528 if (!locked
&& nddr
!= NULL
)
2535 nddr_getexpire(struct nd_defrouter
*dr
)
2537 struct timeval caltime
;
2540 if (dr
->expire
!= 0) {
2541 /* account for system time change */
2542 getmicrotime(&caltime
);
2544 dr
->base_calendartime
+=
2545 NET_CALCULATE_CLOCKSKEW(caltime
,
2546 dr
->base_calendartime
, net_uptime(), dr
->base_uptime
);
2548 expiry
= dr
->base_calendartime
+
2549 dr
->expire
- dr
->base_uptime
;
2557 * Neighbor Discover Prefix structure reference counting routines.
2559 static struct nd_prefix
*
2562 struct nd_prefix
*pr
;
2564 pr
= (how
== M_WAITOK
) ? zalloc(ndpr_zone
) : zalloc_noblock(ndpr_zone
);
2566 bzero(pr
, ndpr_size
);
2567 lck_mtx_init(&pr
->ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2568 RB_INIT(&pr
->ndpr_prproxy_sols
);
2569 pr
->ndpr_debug
|= IFD_ALLOC
;
2570 if (ndpr_debug
!= 0) {
2571 pr
->ndpr_debug
|= IFD_DEBUG
;
2572 pr
->ndpr_trace
= ndpr_trace
;
2579 ndpr_free(struct nd_prefix
*pr
)
2582 if (pr
->ndpr_debug
& IFD_ATTACHED
) {
2583 panic("%s: attached ndpr %p is being freed", __func__
, pr
);
2585 } else if (!(pr
->ndpr_debug
& IFD_ALLOC
)) {
2586 panic("%s: ndpr %p cannot be freed", __func__
, pr
);
2588 } else if (pr
->ndpr_rt
!= NULL
) {
2589 panic("%s: ndpr %p route %p not freed", __func__
, pr
,
2592 } else if (pr
->ndpr_prproxy_sols_cnt
!= 0) {
2593 panic("%s: ndpr %p non-zero solicitors count (%d)",
2594 __func__
, pr
, pr
->ndpr_prproxy_sols_cnt
);
2596 } else if (!RB_EMPTY(&pr
->ndpr_prproxy_sols
)) {
2597 panic("%s: ndpr %p non-empty solicitors tree", __func__
, pr
);
2600 pr
->ndpr_debug
&= ~IFD_ALLOC
;
2603 lck_mtx_destroy(&pr
->ndpr_lock
, ifa_mtx_grp
);
2604 zfree(ndpr_zone
, pr
);
2608 ndpr_trace(struct nd_prefix
*pr
, int refhold
)
2610 struct nd_prefix_dbg
*pr_dbg
= (struct nd_prefix_dbg
*)pr
;
2615 if (!(pr
->ndpr_debug
& IFD_DEBUG
)) {
2616 panic("%s: ndpr %p has no debug structure", __func__
, pr
);
2620 cnt
= &pr_dbg
->ndpr_refhold_cnt
;
2621 tr
= pr_dbg
->ndpr_refhold
;
2623 cnt
= &pr_dbg
->ndpr_refrele_cnt
;
2624 tr
= pr_dbg
->ndpr_refrele
;
2627 idx
= atomic_add_16_ov(cnt
, 1) % NDPR_TRACE_HIST_SIZE
;
2628 ctrace_record(&tr
[idx
]);
2632 ndpr_addref(struct nd_prefix
*ndpr
, int locked
)
2635 NDPR_LOCK_SPIN(ndpr
);
2637 NDPR_LOCK_ASSERT_HELD(ndpr
);
2639 if (++ndpr
->ndpr_refcount
== 0) {
2640 panic("%s: ndpr %p wraparound refcnt\n", __func__
, ndpr
);
2642 } else if (ndpr
->ndpr_trace
!= NULL
) {
2643 (*ndpr
->ndpr_trace
)(ndpr
, TRUE
);
2651 ndpr_remref(struct nd_prefix
*ndpr
, int locked
)
2654 NDPR_LOCK_SPIN(ndpr
);
2656 NDPR_LOCK_ASSERT_HELD(ndpr
);
2658 if (ndpr
->ndpr_refcount
== 0) {
2659 panic("%s: ndpr %p negative refcnt\n", __func__
, ndpr
);
2661 } else if (ndpr
->ndpr_trace
!= NULL
) {
2662 (*ndpr
->ndpr_trace
)(ndpr
, FALSE
);
2665 if (--ndpr
->ndpr_refcount
== 0) {
2666 if (ndpr
->ndpr_addrcnt
!= 0) {
2667 panic("%s: freeing ndpr %p with outstanding address "
2668 "reference (%d)", __func__
, ndpr
,
2669 ndpr
->ndpr_addrcnt
);
2677 if (!locked
&& ndpr
!= NULL
)
2684 ndpr_getexpire(struct nd_prefix
*pr
)
2686 struct timeval caltime
;
2689 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_vltime
!= ND6_INFINITE_LIFETIME
) {
2690 /* account for system time change */
2691 getmicrotime(&caltime
);
2693 pr
->ndpr_base_calendartime
+=
2694 NET_CALCULATE_CLOCKSKEW(caltime
,
2695 pr
->ndpr_base_calendartime
, net_uptime(),
2696 pr
->ndpr_base_uptime
);
2698 expiry
= pr
->ndpr_base_calendartime
+
2699 pr
->ndpr_expire
- pr
->ndpr_base_uptime
;
2707 * A supplement function used in the on-link detection below;
2708 * detect if a given prefix has a (probably) reachable advertising router.
2709 * XXX: lengthy function name...
2711 * Callers *must* increase the reference count of nd_prefix.
2713 static struct nd_pfxrouter
*
2714 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
2716 struct nd_pfxrouter
*pfxrtr
;
2718 struct llinfo_nd6
*ln
;
2720 struct in6_addr rtaddr
;
2723 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2724 NDPR_LOCK_ASSERT_HELD(pr
);
2726 genid
= pr
->ndpr_genid
;
2727 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2729 ifp
= pfxrtr
->router
->ifp
;
2730 if (pfxrtr
->router
->stateflags
& NDDRF_MAPPED
)
2731 rtaddr
= pfxrtr
->router
->rtaddr_mapped
;
2733 rtaddr
= pfxrtr
->router
->rtaddr
;
2736 lck_mtx_unlock(nd6_mutex
);
2737 /* Callee returns a locked route upon success */
2738 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
2739 RT_LOCK_ASSERT_HELD(rt
);
2740 if ((ln
= rt
->rt_llinfo
) != NULL
&&
2741 ND6_IS_LLINFO_PROBREACH(ln
)) {
2742 RT_REMREF_LOCKED(rt
);
2744 lck_mtx_lock(nd6_mutex
);
2748 RT_REMREF_LOCKED(rt
);
2751 lck_mtx_lock(nd6_mutex
);
2753 if (pr
->ndpr_genid
!= genid
) {
2754 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2755 genid
= pr
->ndpr_genid
;
2757 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
);
2759 NDPR_LOCK_ASSERT_HELD(pr
);
2766 * Check if each prefix in the prefix list has at least one available router
2767 * that advertised the prefix (a router is "available" if its neighbor cache
2768 * entry is reachable or probably reachable).
2769 * If the check fails, the prefix may be off-link, because, for example,
2770 * we have moved from the network but the lifetime of the prefix has not
2771 * expired yet. So we should not use the prefix if there is another prefix
2772 * that has an available router.
2773 * But, if there is no prefix that has an available router, we still regards
2774 * all the prefixes as on-link. This is because we can't tell if all the
2775 * routers are simply dead or if we really moved from the network and there
2776 * is no router around us.
2779 pfxlist_onlink_check(void)
2781 struct nd_prefix
*pr
, *prclear
;
2782 struct in6_ifaddr
*ifa
;
2783 struct nd_defrouter
*dr
;
2784 struct nd_pfxrouter
*pfxrtr
= NULL
;
2785 int err
, i
, found
= 0;
2786 struct ifaddr
**ifap
= NULL
;
2787 struct nd_prefix
*ndpr
;
2789 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2791 while (nd_prefix_busy
) {
2792 nd_prefix_waiters
++;
2793 msleep(nd_prefix_waitchan
, nd6_mutex
, (PZERO
-1),
2795 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2797 nd_prefix_busy
= TRUE
;
2800 * Check if there is a prefix that has a reachable advertising
2803 pr
= nd_prefix
.lh_first
;
2806 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
) {
2811 NDPR_ADDREF_LOCKED(pr
);
2812 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
) &&
2813 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
2814 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2820 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2824 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
2825 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
2826 * stop us from checking the same prefix twice.
2828 pr
= nd_prefix
.lh_first
;
2830 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2832 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2833 NDPR_UNLOCK(prclear
);
2836 * If we have no such prefix, check whether we still have a router
2837 * that does not advertise any prefixes.
2840 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
2841 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2842 struct nd_prefix
*pr0
;
2844 for (pr0
= nd_prefix
.lh_first
; pr0
;
2845 pr0
= pr0
->ndpr_next
) {
2847 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
) {
2857 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && pfxrtr
== NULL
)) {
2859 * There is at least one prefix that has a reachable router,
2860 * or at least a router which probably does not advertise
2861 * any prefixes. The latter would be the case when we move
2862 * to a new link where we have a router that does not provide
2863 * prefixes and we configure an address by hand.
2864 * Detach prefixes which have no reachable advertising
2865 * router, and attach other prefixes.
2867 pr
= nd_prefix
.lh_first
;
2871 * We aren't interested prefixes already processed,
2872 * nor in prefixes without the L bit
2873 * set nor in static prefixes
2875 if (pr
->ndpr_raf_onlink
== 0 ||
2876 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2877 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2882 NDPR_ADDREF_LOCKED(pr
);
2883 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2884 find_pfxlist_reachable_router(pr
) == NULL
&&
2885 (pr
->ndpr_debug
& IFD_ATTACHED
))
2886 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
2887 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2888 find_pfxlist_reachable_router(pr
) != NULL
&&
2889 (pr
->ndpr_debug
& IFD_ATTACHED
))
2890 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2891 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2895 * Since find_pfxlist_reachable_router() drops the
2896 * nd6_mutex, we have to start over, but the
2897 * NDPRF_PROCESSED_ONLINK flag will stop us from
2898 * checking the same prefix twice.
2900 pr
= nd_prefix
.lh_first
;
2903 /* there is no prefix that has a reachable router */
2904 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2906 if (pr
->ndpr_raf_onlink
== 0 ||
2907 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2911 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
2912 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2916 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2918 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2919 NDPR_UNLOCK(prclear
);
2922 * Remove each interface route associated with a (just) detached
2923 * prefix, and reinstall the interface route for a (just) attached
2924 * prefix. Note that all attempt of reinstallation does not
2925 * necessarily success, when a same prefix is shared among multiple
2926 * interfaces. Such cases will be handled in nd6_prefix_onlink,
2927 * so we don't have to care about them.
2929 pr
= nd_prefix
.lh_first
;
2934 if (pr
->ndpr_raf_onlink
== 0 ||
2935 pr
->ndpr_stateflags
& NDPRF_STATIC
||
2936 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2937 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
2942 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2943 NDPR_ADDREF_LOCKED(pr
);
2944 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2945 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
2947 lck_mtx_unlock(nd6_mutex
);
2948 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2950 "pfxlist_onlink_check: failed to "
2951 "make %s/%d offlink, errno=%d\n",
2952 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2955 lck_mtx_lock(nd6_mutex
);
2957 pr
= nd_prefix
.lh_first
;
2960 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2961 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
2962 pr
->ndpr_raf_onlink
) {
2964 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2966 "pfxlist_onlink_check: failed to "
2967 "make %s/%d offlink, errno=%d\n",
2968 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2972 pr
= nd_prefix
.lh_first
;
2980 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2982 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2983 NDPR_UNLOCK(prclear
);
2985 VERIFY(nd_prefix_busy
);
2986 nd_prefix_busy
= FALSE
;
2987 if (nd_prefix_waiters
> 0) {
2988 nd_prefix_waiters
= 0;
2989 wakeup(nd_prefix_waitchan
);
2993 * Changes on the prefix status might affect address status as well.
2994 * Make sure that all addresses derived from an attached prefix are
2995 * attached, and that all addresses derived from a detached prefix are
2996 * detached. Note, however, that a manually configured address should
2997 * always be attached.
2998 * The precise detection logic is same as the one for prefixes.
3000 * ifnet_get_address_list_family_internal() may fail due to memory
3001 * pressure, but we will eventually be called again when we receive
3002 * another NA, RA, or when the link status changes.
3004 err
= ifnet_get_address_list_family_internal(NULL
, &ifap
, AF_INET6
, 0,
3006 if (err
!= 0 || ifap
== NULL
) {
3007 nd6log((LOG_ERR
, "%s: ifnet_get_address_list_family_internal "
3008 "failed", __func__
));
3011 for (i
= 0; ifap
[i
]; i
++) {
3012 ifa
= ifatoia6(ifap
[i
]);
3013 IFA_LOCK(&ifa
->ia_ifa
);
3014 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3015 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3016 IFA_UNLOCK(&ifa
->ia_ifa
);
3019 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3021 * This can happen when we first configure the address
3022 * (i.e. the address exists, but the prefix does not).
3023 * XXX: complicated relationships...
3025 IFA_UNLOCK(&ifa
->ia_ifa
);
3028 IFA_UNLOCK(&ifa
->ia_ifa
);
3031 NDPR_ADDREF_LOCKED(ndpr
);
3032 if (find_pfxlist_reachable_router(ndpr
)) {
3033 if (NDPR_REMREF_LOCKED(ndpr
) == NULL
) {
3045 for (i
= 0; ifap
[i
]; i
++) {
3046 ifa
= ifatoia6(ifap
[i
]);
3047 IFA_LOCK(&ifa
->ia_ifa
);
3048 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3049 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3050 IFA_UNLOCK(&ifa
->ia_ifa
);
3053 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3054 /* XXX: see above. */
3055 IFA_UNLOCK(&ifa
->ia_ifa
);
3058 IFA_UNLOCK(&ifa
->ia_ifa
);
3060 NDPR_ADDREF_LOCKED(ndpr
);
3061 if (find_pfxlist_reachable_router(ndpr
)) {
3063 IFA_LOCK(&ifa
->ia_ifa
);
3064 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3065 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3066 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3067 IFA_UNLOCK(&ifa
->ia_ifa
);
3068 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3070 IFA_UNLOCK(&ifa
->ia_ifa
);
3074 IFA_LOCK(&ifa
->ia_ifa
);
3075 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
3076 in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DETACHED
,
3077 ifa
->ia_ifa
.ifa_ifp
, &ifa
->ia_addr
.sin6_addr
,
3079 IFA_UNLOCK(&ifa
->ia_ifa
);
3084 for (i
= 0; ifap
[i
]; i
++) {
3085 ifa
= ifatoia6(ifap
[i
]);
3086 IFA_LOCK(&ifa
->ia_ifa
);
3087 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3088 IFA_UNLOCK(&ifa
->ia_ifa
);
3091 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3092 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3093 in6_ifaddr_set_dadprogress((struct in6_ifaddr
*)ifa
);
3094 IFA_UNLOCK(&ifa
->ia_ifa
);
3095 /* Do we need a delay in this case? */
3096 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3098 IFA_UNLOCK(&ifa
->ia_ifa
);
3102 ifnet_free_address_list(ifap
);
3105 static struct nd_prefix
*
3106 nd6_prefix_equal_lookup(struct nd_prefix
*pr
, boolean_t primary_only
)
3108 struct nd_prefix
*opr
;
3110 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3112 for (opr
= nd_prefix
.lh_first
; opr
; opr
= opr
->ndpr_next
) {
3117 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3121 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
3122 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
3123 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
) &&
3125 !(opr
->ndpr_stateflags
& NDPRF_IFSCOPE
))) {
3126 NDPR_ADDREF_LOCKED(opr
);
3136 * Synchronize the interface routes of similar prefixes on different
3137 * interfaces; the one using the default interface would be (re)installed
3138 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3141 nd6_prefix_sync(struct ifnet
*ifp
)
3143 struct nd_prefix
*pr
, *opr
;
3146 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3151 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3153 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
3157 if (pr
->ndpr_ifp
== ifp
&&
3158 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) &&
3159 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3169 /* Remove conflicting entries */
3170 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3172 lck_mtx_unlock(nd6_mutex
);
3173 err
= nd6_prefix_offlink(opr
);
3174 lck_mtx_lock(nd6_mutex
);
3177 "%s: failed to make %s/%d offlink on %s, "
3178 "errno=%d\n", __func__
,
3179 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3180 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3184 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3185 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3186 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
)));
3189 lck_mtx_unlock(nd6_mutex
);
3190 err
= nd6_prefix_offlink(pr
);
3191 lck_mtx_lock(nd6_mutex
);
3194 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3195 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3196 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3199 /* Add the entries back */
3201 err
= nd6_prefix_onlink_scoped(opr
, opr
->ndpr_ifp
->if_index
);
3204 "%s: failed to make %s/%d scoped onlink on %s, "
3205 "errno=%d\n", __func__
,
3206 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3207 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3211 err
= nd6_prefix_onlink_scoped(pr
, IFSCOPE_NONE
);
3214 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3215 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3216 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3221 "%s: error promoting %s/%d to %s from %s\n",
3222 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3223 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3224 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3227 "%s: %s/%d promoted, previously on %s\n",
3228 if_name(pr
->ndpr_ifp
),
3229 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
,
3230 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3238 nd6_prefix_onlink_common(struct nd_prefix
*pr
, boolean_t force_scoped
,
3239 unsigned int ifscope
)
3242 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3243 struct sockaddr_in6 mask6
, prefix
;
3244 struct nd_prefix
*opr
;
3246 int error
= 0, prproxy
= 0;
3247 struct rtentry
*rt
= NULL
;
3249 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3253 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3255 "%s: %s/%d on %s scoped=%d is already on-link\n",
3256 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3257 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3258 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3265 * Add the interface route associated with the prefix. Before
3266 * installing the route, check if there's the same prefix on another
3267 * interface, and the prefix has already installed the interface route.
3269 opr
= nd6_prefix_equal_lookup(pr
, FALSE
);
3273 if (!force_scoped
) {
3275 * If a primary/non-scoped interface route already exists,
3276 * install the new one as a scoped entry. If the existing
3277 * interface route is scoped, install new as non-scoped.
3279 ifscope
= (opr
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
3280 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3283 else if (ifscope
!= IFSCOPE_NONE
)
3284 ifscope
= IFSCOPE_NONE
;
3288 * We prefer link-local addresses as the associated interface address.
3290 /* search for a link-local addr */
3291 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
3292 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3294 struct in6_ifaddr
*ia6
;
3295 ifnet_lock_shared(ifp
);
3296 IFP_TO_IA6(ifp
, ia6
);
3297 ifnet_lock_done(ifp
);
3300 /* should we care about ia6_flags? */
3305 * This can still happen, when, for example, we receive an RA
3306 * containing a prefix with the L bit set and the A bit clear,
3307 * after removing all IPv6 addresses on the receiving
3308 * interface. This should, of course, be rare though.
3311 "nd6_prefix_onlink: failed to find any ifaddr"
3312 " to add route for a prefix(%s/%d) on %s\n",
3313 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3314 pr
->ndpr_plen
, if_name(ifp
)));
3320 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3321 * ifa->ifa_rtrequest = nd6_rtrequest;
3323 bzero(&mask6
, sizeof (mask6
));
3324 mask6
.sin6_len
= sizeof (mask6
);
3325 mask6
.sin6_addr
= pr
->ndpr_mask
;
3326 prefix
= pr
->ndpr_prefix
;
3327 if ((rt
= pr
->ndpr_rt
) != NULL
)
3329 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3333 rtflags
= ifa
->ifa_flags
| RTF_CLONING
| RTF_UP
;
3335 if (nd6_need_cache(ifp
)) {
3336 /* explicitly set in case ifa_flags does not set the flag. */
3337 rtflags
|= RTF_CLONING
;
3340 * explicitly clear the cloning bit in case ifa_flags sets it.
3342 rtflags
&= ~RTF_CLONING
;
3345 lck_mtx_unlock(nd6_mutex
);
3352 error
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&prefix
,
3353 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
,
3357 * Serialize the setting of NDPRF_PRPROXY.
3359 lck_mtx_lock(&proxy6_lock
);
3363 nd6_rtmsg(RTM_ADD
, rt
);
3368 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a"
3369 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx,"
3370 " scoped=%d, errno = %d\n",
3371 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3372 pr
->ndpr_plen
, if_name(ifp
),
3373 ip6_sprintf(&((struct sockaddr_in6
*)
3374 (void *)ifa
->ifa_addr
)->sin6_addr
),
3375 ip6_sprintf(&mask6
.sin6_addr
), rtflags
,
3376 (ifscope
!= IFSCOPE_NONE
), error
));
3378 NDPR_LOCK_ASSERT_HELD(pr
);
3380 pr
->ndpr_stateflags
&= ~(NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3383 * TODO: If the prefix route exists, we should really find it and
3384 * refer the prefix to it; otherwise ndpr_rt is NULL.
3386 if (!(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) &&
3387 (rt
!= NULL
|| error
== EEXIST
)) {
3388 struct nd_ifinfo
*ndi
= NULL
;
3390 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3391 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3393 ndi
= ND_IFINFO(ifp
);
3394 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3395 lck_mtx_lock(&ndi
->lock
);
3397 pr
->ndpr_rt
= rt
; /* keep reference from rtrequest */
3398 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
3399 if (ifscope
!= IFSCOPE_NONE
) {
3400 pr
->ndpr_stateflags
|= NDPRF_IFSCOPE
;
3401 } else if ((rtflags
& RTF_CLONING
) &&
3402 (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) &&
3403 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3405 * At present, in order for the prefix to be eligible
3406 * as a proxying/proxied prefix, we require that the
3407 * prefix route entry be marked as a cloning route with
3408 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3409 * true for the interface type, hence the test for
3410 * RTF_CLONING above.
3412 pr
->ndpr_stateflags
|= NDPRF_PRPROXY
;
3415 lck_mtx_unlock(&ndi
->lock
);
3416 } else if (rt
!= NULL
&& pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
3419 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3420 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3426 * If this is an upstream prefix, find the downstream ones (if any)
3427 * and re-configure their prefix routes accordingly. Otherwise,
3428 * this could be potentially be a downstream prefix, and so find the
3429 * upstream prefix, if any.
3431 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3433 NDPR_REMREF(pr
); /* release reference for this routine */
3434 lck_mtx_unlock(&proxy6_lock
);
3436 lck_mtx_lock(nd6_mutex
);
3442 nd6_prefix_onlink(struct nd_prefix
*pr
)
3444 return (nd6_prefix_onlink_common(pr
, FALSE
, IFSCOPE_NONE
));
3448 nd6_prefix_onlink_scoped(struct nd_prefix
*pr
, unsigned int ifscope
)
3450 return (nd6_prefix_onlink_common(pr
, TRUE
, ifscope
));
3454 nd6_prefix_offlink(struct nd_prefix
*pr
)
3456 int plen
, error
= 0, prproxy
;
3457 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3458 struct sockaddr_in6 sa6
, mask6
, prefix
;
3459 struct rtentry
*rt
= NULL
, *ndpr_rt
= NULL
;
3460 unsigned int ifscope
;
3462 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3466 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3468 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3469 "off-link\n", ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3470 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3471 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0));
3476 bzero(&sa6
, sizeof (sa6
));
3477 sa6
.sin6_family
= AF_INET6
;
3478 sa6
.sin6_len
= sizeof (sa6
);
3479 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
,
3480 sizeof (struct in6_addr
));
3481 bzero(&mask6
, sizeof (mask6
));
3482 mask6
.sin6_family
= AF_INET6
;
3483 mask6
.sin6_len
= sizeof (sa6
);
3484 bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof (struct in6_addr
));
3485 prefix
= pr
->ndpr_prefix
;
3486 plen
= pr
->ndpr_plen
;
3487 if ((ndpr_rt
= pr
->ndpr_rt
) != NULL
)
3489 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3492 ifscope
= (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ?
3493 ifp
->if_index
: IFSCOPE_NONE
;
3495 error
= rtrequest_scoped(RTM_DELETE
, (struct sockaddr
*)&sa6
,
3496 NULL
, (struct sockaddr
*)&mask6
, 0, &rt
, ifscope
);
3499 /* report the route deletion to the routing socket. */
3501 nd6_rtmsg(RTM_DELETE
, rt
);
3507 "nd6_prefix_offlink: failed to delete route: "
3508 "%s/%d on %s, scoped %d, (errno = %d)\n",
3509 ip6_sprintf(&sa6
.sin6_addr
), plen
, if_name(ifp
),
3510 (ifscope
!= IFSCOPE_NONE
), error
));
3513 if (ndpr_rt
!= NULL
)
3516 lck_mtx_lock(&proxy6_lock
);
3519 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3520 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3521 pr
->ndpr_stateflags
&= ~(NDPRF_ONLINK
| NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3522 if (pr
->ndpr_prproxy_sols_cnt
> 0) {
3524 nd6_prproxy_sols_reap(pr
);
3525 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3526 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3531 * If this was an upstream prefix, find the downstream ones and do
3532 * some cleanups. If this was a downstream prefix, the prefix route
3533 * has been removed from the routing table above, but there may be
3534 * other tasks to perform.
3536 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3538 NDPR_REMREF(pr
); /* release reference for this routine */
3539 lck_mtx_unlock(&proxy6_lock
);
3544 static struct in6_ifaddr
*
3545 in6_pfx_newpersistaddr(struct nd_prefix
*pr
, int mcast
, int *errorp
)
3547 struct in6_ifaddr
*ia6
= NULL
;
3548 struct ifnet
*ifp
= NULL
;
3549 struct nd_ifinfo
*ndi
= NULL
;
3550 struct in6_addr mask
;
3551 struct in6_aliasreq ifra
;
3552 int error
, ifaupdate
, iidlen
, notcga
;
3555 VERIFY(errorp
!= NULL
);
3563 * Prefix Length check:
3564 * If the sum of the prefix length and interface identifier
3565 * length does not equal 128 bits, the Prefix Information
3566 * option MUST be ignored. The length of the interface
3567 * identifier is defined in a separate link-type specific
3570 iidlen
= in6_if2idlen(ifp
);
3572 error
= EADDRNOTAVAIL
;
3573 /* this should not happen, so we always log it. */
3574 log(LOG_ERR
, "%s: IID length undefined (%s)\n",
3575 __func__
, if_name(ifp
));
3577 } else if (iidlen
!= 64) {
3578 error
= EADDRNOTAVAIL
;
3580 * stateless autoconfiguration not yet well-defined for IID
3581 * lengths other than 64 octets. Just give up for now.
3583 nd6log((LOG_INFO
, "%s: IID length not 64 octets (%s)\n",
3584 __func__
, if_name(ifp
)));
3588 if (iidlen
+ pr
->ndpr_plen
!= 128) {
3589 error
= EADDRNOTAVAIL
;
3591 "%s: invalid prefix length %d for %s, ignored\n",
3592 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
3596 bzero(&ifra
, sizeof (ifra
));
3597 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3598 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
3599 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
3602 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
,
3603 sizeof (ifra
.ifra_addr
.sin6_addr
));
3604 in6_len2mask(&mask
, pr
->ndpr_plen
);
3605 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
3606 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
3607 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
3608 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
3610 ndi
= ND_IFINFO(ifp
);
3611 VERIFY(ndi
->initialized
);
3612 lck_mtx_lock(&ndi
->lock
);
3614 notcga
= nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
||
3615 (ndi
->flags
& ND6_IFF_INSECURE
) != 0;
3617 lck_mtx_unlock(&ndi
->lock
);
3621 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
3623 error
= EADDRNOTAVAIL
;
3624 nd6log((LOG_INFO
, "%s: no link-local address (%s)\n",
3625 __func__
, if_name(ifp
)));
3629 IFA_LOCK(&ia6
->ia_ifa
);
3630 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
3631 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
3632 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
3633 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
3634 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3635 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
3636 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3637 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
3638 IFA_UNLOCK(&ia6
->ia_ifa
);
3639 IFA_REMREF(&ia6
->ia_ifa
);
3642 in6_cga_node_lock();
3643 struct in6_cga_prepare local_cga_prepare
;
3645 if (ndi
->cga_initialized
) {
3646 bcopy(&(ndi
->local_cga_modifier
),
3647 &(local_cga_prepare
.cga_modifier
),
3648 sizeof(local_cga_prepare
.cga_modifier
));
3649 error
= in6_cga_generate(&local_cga_prepare
, 0,
3650 &ifra
.ifra_addr
.sin6_addr
);
3652 error
= in6_cga_generate(NULL
, 0,
3653 &ifra
.ifra_addr
.sin6_addr
);
3655 in6_cga_node_unlock();
3657 ifra
.ifra_flags
|= IN6_IFF_SECURED
;
3659 nd6log((LOG_ERR
, "%s: no CGA available (%s)\n",
3660 __func__
, if_name(ifp
)));
3665 VERIFY(ia6
== NULL
);
3667 /* new prefix mask. */
3668 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
3669 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
3670 bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
,
3671 sizeof (ifra
.ifra_prefixmask
.sin6_addr
));
3674 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
3675 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
3678 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
3681 * Make sure that we do not have this address already. This should
3682 * usually not happen, but we can still see this case, e.g., if we
3683 * have manually configured the exact address to be configured.
3685 if ((ia6
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
))
3687 error
= EADDRNOTAVAIL
;
3688 IFA_REMREF(&ia6
->ia_ifa
);
3691 /* this should be rare enough to make an explicit log */
3692 log(LOG_INFO
, "%s: %s is already configured!\n",
3693 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
3698 * Allocate ifaddr structure, link into chain, etc.
3699 * If we are going to create a new address upon receiving a multicasted
3700 * RA, we need to impose a random delay before starting DAD.
3701 * [RFC 4862, Section 5.4.2]
3703 ifaupdate
= IN6_IFAUPDATE_NOWAIT
;
3705 ifaupdate
|= IN6_IFAUPDATE_DADDELAY
;
3706 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &ia6
);
3709 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
3710 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
),
3711 if_name(ifp
), error
));
3712 error
= EADDRNOTAVAIL
;
3716 VERIFY(ia6
!= NULL
);
3717 in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia6
, NULL
);
3728 #define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
3731 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
)
3733 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
3734 struct in6_ifaddr
*ia
, *newia
;
3735 struct in6_aliasreq ifra
;
3736 int i
, error
, ifaupdate
;
3737 int trylimit
= 3; /* XXX: adhoc value */
3738 u_int32_t randid
[2];
3739 time_t vltime0
, pltime0
;
3740 uint64_t timenow
= net_uptime();
3741 struct in6_addr addr
;
3742 struct nd_prefix
*ndpr
;
3744 bzero(&ifra
, sizeof (ifra
));
3745 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3746 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3747 ifra
.ifra_addr
= ia0
->ia_addr
;
3748 /* copy prefix mask */
3749 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
3750 /* clear the old IFID */
3751 for (i
= 0; i
< 4; i
++) {
3752 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
]
3753 &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
3755 addr
= ia0
->ia_addr
.sin6_addr
;
3756 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3759 in6_iid_mktmp(ifp
, (u_int8_t
*)randid
,
3760 (const u_int8_t
*)&addr
.s6_addr
[8], forcegen
);
3762 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3763 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
3764 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3765 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
3768 * in6_iid_mktmp() quite likely provided a unique interface ID.
3769 * However, we may still have a chance to see collision, because
3770 * there may be a time lag between generation of the ID and generation
3771 * of the address. So, we'll do one more sanity check.
3773 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
)) != NULL
) {
3774 IFA_REMREF(&ia
->ia_ifa
);
3775 if (trylimit
-- == 0) {
3776 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find "
3777 "a unique random IFID\n"));
3785 * The Valid Lifetime is the lower of the Valid Lifetime of the
3786 * public address or TEMP_VALID_LIFETIME.
3787 * The Preferred Lifetime is the lower of the Preferred Lifetime
3788 * of the public address or TEMP_PREFERRED_LIFETIME -
3791 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3792 if (ia0
->ia6_lifetime
.ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3793 vltime0
= IFA6_IS_INVALID(ia0
, timenow
) ? 0 :
3794 (ia0
->ia6_lifetime
.ia6ti_vltime
-
3795 (timenow
- ia0
->ia6_updatetime
));
3796 if (vltime0
> ip6_temp_valid_lifetime
)
3797 vltime0
= ip6_temp_valid_lifetime
;
3799 vltime0
= ip6_temp_valid_lifetime
;
3801 if (ia0
->ia6_lifetime
.ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3802 pltime0
= IFA6_IS_DEPRECATED(ia0
, timenow
) ? 0 :
3803 (ia0
->ia6_lifetime
.ia6ti_pltime
-
3804 (timenow
- ia0
->ia6_updatetime
));
3805 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
)
3806 pltime0
= ip6_temp_preferred_lifetime
-
3809 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
3811 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
3812 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
3813 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3815 * A temporary address is created only if this calculated Preferred
3816 * Lifetime is greater than REGEN_ADVANCE time units.
3818 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
)
3821 /* XXX: scope zone ID? */
3823 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
3825 /* allocate ifaddr structure, link into chain, etc. */
3826 ifaupdate
= IN6_IFAUPDATE_NOWAIT
| IN6_IFAUPDATE_DADDELAY
;
3827 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &newia
);
3829 nd6log((LOG_ERR
, "in6_tmpifadd: failed to add address.\n"));
3832 VERIFY(newia
!= NULL
);
3834 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3835 ndpr
= ia0
->ia6_ndpr
;
3838 * We lost the race with another thread that has purged
3839 * ia0 address; in this case, purge the tmp addr as well.
3841 nd6log((LOG_ERR
, "in6_tmpifadd: no public address\n"));
3842 VERIFY(!(ia0
->ia6_flags
& IN6_IFF_AUTOCONF
));
3843 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3844 in6_purgeaddr(&newia
->ia_ifa
);
3845 IFA_REMREF(&newia
->ia_ifa
);
3846 return (EADDRNOTAVAIL
);
3848 NDPR_ADDREF(ndpr
); /* for us */
3849 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3850 IFA_LOCK(&newia
->ia_ifa
);
3851 if (newia
->ia6_ndpr
!= NULL
) {
3852 NDPR_LOCK(newia
->ia6_ndpr
);
3853 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3854 newia
->ia6_ndpr
->ndpr_addrcnt
--;
3855 NDPR_UNLOCK(newia
->ia6_ndpr
);
3856 NDPR_REMREF(newia
->ia6_ndpr
); /* release addr reference */
3858 newia
->ia6_ndpr
= ndpr
;
3859 NDPR_LOCK(newia
->ia6_ndpr
);
3860 newia
->ia6_ndpr
->ndpr_addrcnt
++;
3861 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
3862 NDPR_ADDREF_LOCKED(newia
->ia6_ndpr
); /* for addr reference */
3863 NDPR_UNLOCK(newia
->ia6_ndpr
);
3864 IFA_UNLOCK(&newia
->ia_ifa
);
3866 * A newly added address might affect the status of other addresses.
3867 * XXX: when the temporary address is generated with a new public
3868 * address, the onlink check is redundant. However, it would be safe
3869 * to do the check explicitly everywhere a new address is generated,
3870 * and, in fact, we surely need the check when we create a new
3871 * temporary address due to deprecation of an old temporary address.
3873 lck_mtx_lock(nd6_mutex
);
3874 pfxlist_onlink_check();
3875 lck_mtx_unlock(nd6_mutex
);
3876 IFA_REMREF(&newia
->ia_ifa
);
3878 /* remove our reference */
3886 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
3888 struct timeval caltime
;
3889 u_int64_t timenow
= net_uptime();
3891 NDPR_LOCK_ASSERT_HELD(ndpr
);
3893 getmicrotime(&caltime
);
3894 ndpr
->ndpr_base_calendartime
= caltime
.tv_sec
;
3895 ndpr
->ndpr_base_uptime
= timenow
;
3897 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
3898 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
3899 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime"
3900 "(%d) is greater than valid lifetime(%d)\n",
3901 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
));
3904 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
3905 ndpr
->ndpr_preferred
= 0;
3907 ndpr
->ndpr_preferred
= timenow
+ ndpr
->ndpr_pltime
;
3908 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
3909 ndpr
->ndpr_expire
= 0;
3911 ndpr
->ndpr_expire
= timenow
+ ndpr
->ndpr_vltime
;
3917 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
3920 uint64_t timenow
= net_uptime();
3922 /* Valid lifetime must not be updated unless explicitly specified. */
3923 /* init ia6t_expire */
3924 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
3925 lt6
->ia6t_expire
= 0;
3927 lt6
->ia6t_expire
= timenow
;
3928 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
3931 /* init ia6t_preferred */
3932 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
) {
3933 lt6
->ia6t_preferred
= 0;
3935 lt6
->ia6t_preferred
= timenow
;
3936 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
3941 * Delete all the routing table entries that use the specified gateway.
3942 * XXX: this function causes search through all entries of routing table, so
3943 * it shouldn't be called when acting as a router.
3947 struct in6_addr
*gateway
,
3950 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
3952 /* We'll care only link-local addresses */
3953 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
3956 lck_mtx_lock(rnh_lock
);
3957 /* XXX: hack for KAME's link-local address kludge */
3958 gateway
->s6_addr16
[1] = htons(ifp
->if_index
);
3960 rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
);
3961 lck_mtx_unlock(rnh_lock
);
3966 struct radix_node
*rn
,
3969 struct rtentry
*rt
= (struct rtentry
*)rn
;
3970 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
3972 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
3975 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
) {
3980 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) {
3985 * Do not delete a static route.
3986 * XXX: this seems to be a bit ad-hoc. Should we consider the
3987 * 'cloned' bit instead?
3989 if ((rt
->rt_flags
& RTF_STATIC
) != 0) {
3994 * We delete only host route. This means, in particular, we don't
3995 * delete default route.
3997 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
4003 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
4004 * rnh_lock here prevents another thread from calling rt_setgate()
4008 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4009 rt_mask(rt
), rt
->rt_flags
, 0));
4013 nd6_setdefaultiface(
4017 ifnet_t def_ifp
= NULL
;
4019 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4021 ifnet_head_lock_shared();
4022 if (ifindex
< 0 || if_index
< ifindex
) {
4026 def_ifp
= ifindex2ifnet
[ifindex
];
4029 lck_mtx_lock(nd6_mutex
);
4030 if (nd6_defifindex
!= ifindex
) {
4031 struct ifnet
*odef_ifp
= nd6_defifp
;
4033 nd6_defifindex
= ifindex
;
4034 if (nd6_defifindex
> 0)
4035 nd6_defifp
= def_ifp
;
4039 if (nd6_defifp
!= NULL
)
4040 nd6log((LOG_INFO
, "%s: is now the default "
4041 "interface (was %s)\n", if_name(nd6_defifp
),
4042 odef_ifp
!= NULL
? if_name(odef_ifp
) : "NONE"));
4044 nd6log((LOG_INFO
, "No default interface set\n"));
4047 * If the Default Router List is empty, install a route
4048 * to the specified interface as default or remove the default
4049 * route when the default interface becomes canceled.
4050 * The check for the queue is actually redundant, but
4051 * we do this here to avoid re-install the default route
4052 * if the list is NOT empty.
4054 if (odef_ifp
!= NULL
) {
4055 defrouter_select(odef_ifp
);
4058 if (nd6_defifp
!= NULL
) {
4059 defrouter_select(nd6_defifp
);
4060 nd6_prefix_sync(nd6_defifp
);
4064 * Our current implementation assumes one-to-one mapping between
4065 * interfaces and links, so it would be natural to use the
4066 * default interface as the default link.
4068 scope6_setdefault(nd6_defifp
);
4070 lck_mtx_unlock(nd6_mutex
);