2 * Copyright (c) 2003-2015 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_types.h>
81 #include <net/if_dl.h>
82 #include <net/route.h>
83 #include <net/radix.h>
85 #include <netinet/in.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/nd6.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/scope6_var.h>
94 #include <net/net_osdep.h>
96 static void defrouter_addreq(struct nd_defrouter
*, boolean_t
);
97 static void defrouter_delreq(struct nd_defrouter
*);
98 static struct nd_defrouter
*defrtrlist_update_common(struct nd_defrouter
*,
100 static struct nd_defrouter
*defrtrlist_update(struct nd_defrouter
*);
102 static struct in6_ifaddr
*in6_pfx_newpersistaddr(struct nd_prefix
*, int,
104 static void defrtrlist_sync(struct ifnet
*);
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
;
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
;
386 /* Expect 32-bit aligned data pointer on strict-align platforms */
387 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
390 * Discard RA unless IFEF_ACCEPT_RTADV is set (as host), or when
391 * IFEF_IPV6_ROUTER is set (as router) but the RA is not locally
392 * generated. For convenience, we allow locally generated (rtadvd)
393 * RAs to be processed on the advertising interface, as a router.
395 * Note that we don't test against ip6_forwarding as we could be
396 * both a host and a router on different interfaces, hence the
397 * check against the per-interface flags.
399 if (!(ifp
->if_eflags
& (IFEF_ACCEPT_RTADV
| IFEF_IPV6_ROUTER
)) ||
400 ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) &&
401 (ia6
= ifa_foraddr6(&saddr6
)) == NULL
))
405 IFA_REMREF(&ia6
->ia_ifa
);
409 if (ip6
->ip6_hlim
!= 255) {
411 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
412 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
413 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
417 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6
)) {
419 "nd6_ra_input: src %s is not link-local\n",
420 ip6_sprintf(&saddr6
)));
424 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
425 nd_ra
= (struct nd_router_advert
*)((caddr_t
)ip6
+ off
);
427 icmp6len
-= sizeof (*nd_ra
);
428 nd6_option_init(nd_ra
+ 1, icmp6len
, &ndopts
);
429 if (nd6_options(&ndopts
) < 0) {
431 "nd6_ra_input: invalid ND option, ignored\n"));
432 /* nd6_options have incremented stats */
436 advreachable
= nd_ra
->nd_ra_reachable
;
438 /* remember if this is a multicasted advertisement */
439 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
))
442 ndi
= ND_IFINFO(ifp
);
443 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
444 lck_mtx_lock(&ndi
->lock
);
445 bzero(&dr0
, sizeof (dr0
));
447 dr0
.flags
= nd_ra
->nd_ra_flags_reserved
;
448 dr0
.rtlifetime
= ntohs(nd_ra
->nd_ra_router_lifetime
);
449 dr0
.expire
= net_uptime() + dr0
.rtlifetime
;
451 /* unspecified or not? (RFC 2461 6.3.4) */
453 advreachable
= ntohl(advreachable
);
454 if (advreachable
<= MAX_REACHABLE_TIME
&&
455 ndi
->basereachable
!= advreachable
) {
456 ndi
->basereachable
= advreachable
;
457 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
458 ndi
->recalctm
= nd6_recalc_reachtm_interval
; /* reset */
461 if (nd_ra
->nd_ra_retransmit
)
462 ndi
->retrans
= ntohl(nd_ra
->nd_ra_retransmit
);
463 if (nd_ra
->nd_ra_curhoplimit
) {
464 if (ndi
->chlim
< nd_ra
->nd_ra_curhoplimit
) {
465 ndi
->chlim
= nd_ra
->nd_ra_curhoplimit
;
466 } else if (ndi
->chlim
!= nd_ra
->nd_ra_curhoplimit
) {
468 "RA with a lower CurHopLimit sent from "
469 "%s on %s (current = %d, received = %d). "
470 "Ignored.\n", ip6_sprintf(&ip6
->ip6_src
),
471 if_name(ifp
), ndi
->chlim
,
472 nd_ra
->nd_ra_curhoplimit
));
475 lck_mtx_unlock(&ndi
->lock
);
476 lck_mtx_lock(nd6_mutex
);
477 dr
= defrtrlist_update(&dr0
);
478 lck_mtx_unlock(nd6_mutex
);
483 if (ndopts
.nd_opts_pi
) {
484 struct nd_opt_hdr
*pt
;
485 struct nd_opt_prefix_info
*pi
= NULL
;
488 for (pt
= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi
;
489 pt
<= (struct nd_opt_hdr
*)ndopts
.nd_opts_pi_end
;
490 pt
= (struct nd_opt_hdr
*)((caddr_t
)pt
+
491 (pt
->nd_opt_len
<< 3))) {
492 if (pt
->nd_opt_type
!= ND_OPT_PREFIX_INFORMATION
)
494 pi
= (struct nd_opt_prefix_info
*)pt
;
496 if (pi
->nd_opt_pi_len
!= 4) {
498 "nd6_ra_input: invalid option "
499 "len %d for prefix information option, "
500 "ignored\n", pi
->nd_opt_pi_len
));
504 if (128 < pi
->nd_opt_pi_prefix_len
) {
506 "nd6_ra_input: invalid prefix "
507 "len %d for prefix information option, "
508 "ignored\n", pi
->nd_opt_pi_prefix_len
));
512 if (IN6_IS_ADDR_MULTICAST(&pi
->nd_opt_pi_prefix
) ||
513 IN6_IS_ADDR_LINKLOCAL(&pi
->nd_opt_pi_prefix
)) {
515 "%s: invalid prefix %s, ignored\n",
517 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
521 bzero(&pr
, sizeof (pr
));
522 lck_mtx_init(&pr
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
524 pr
.ndpr_prefix
.sin6_family
= AF_INET6
;
525 pr
.ndpr_prefix
.sin6_len
= sizeof (pr
.ndpr_prefix
);
526 pr
.ndpr_prefix
.sin6_addr
= pi
->nd_opt_pi_prefix
;
527 pr
.ndpr_ifp
= m
->m_pkthdr
.rcvif
;
529 pr
.ndpr_raf_onlink
= (pi
->nd_opt_pi_flags_reserved
&
530 ND_OPT_PI_FLAG_ONLINK
) ? 1 : 0;
531 pr
.ndpr_raf_auto
= (pi
->nd_opt_pi_flags_reserved
&
532 ND_OPT_PI_FLAG_AUTO
) ? 1 : 0;
533 pr
.ndpr_plen
= pi
->nd_opt_pi_prefix_len
;
534 pr
.ndpr_vltime
= ntohl(pi
->nd_opt_pi_valid_time
);
536 ntohl(pi
->nd_opt_pi_preferred_time
);
539 * Exceptions to stateless autoconfiguration processing:
540 * + nd6_accept_6to4 == 0 && address has 6to4 prefix
541 * + ip6_only_allow_rfc4193_prefix != 0 &&
542 * address not RFC 4193
544 if (ip6_only_allow_rfc4193_prefix
&&
545 !IN6_IS_ADDR_UNIQUE_LOCAL(&pi
->nd_opt_pi_prefix
)) {
547 "nd6_ra_input: no SLAAC on prefix %s "
549 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
550 pr
.ndpr_raf_auto
= 0;
551 } else if (!nd6_accept_6to4
&&
552 IN6_IS_ADDR_6TO4(&pi
->nd_opt_pi_prefix
)) {
554 "%s: no SLAAC on prefix %s "
555 "[6to4]\n", __func__
,
556 ip6_sprintf(&pi
->nd_opt_pi_prefix
)));
557 pr
.ndpr_raf_auto
= 0;
560 if (in6_init_prefix_ltimes(&pr
)) {
562 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
563 continue; /* prefix lifetime init failed */
567 (void) prelist_update(&pr
, dr
, m
, mcast
);
568 lck_mtx_destroy(&pr
.ndpr_lock
, ifa_mtx_grp
);
571 * We have to copy the values out after the
572 * prelist_update call since some of these values won't
573 * be properly set until after the router advertisement
574 * updating can vet the values.
577 MALLOC(prfl
, struct nd_prefix_list
*, sizeof (*prfl
),
578 M_TEMP
, M_WAITOK
| M_ZERO
);
581 log(LOG_DEBUG
, "%s: unable to MALLOC RA prefix "
582 "structure\n", __func__
);
586 /* this is only for nd6_post_msg(), otherwise unused */
587 bcopy(&pr
.ndpr_prefix
, &prfl
->pr
.ndpr_prefix
,
588 sizeof (prfl
->pr
.ndpr_prefix
));
589 prfl
->pr
.ndpr_raf
= pr
.ndpr_raf
;
590 prfl
->pr
.ndpr_plen
= pr
.ndpr_plen
;
591 prfl
->pr
.ndpr_vltime
= pr
.ndpr_vltime
;
592 prfl
->pr
.ndpr_pltime
= pr
.ndpr_pltime
;
593 prfl
->pr
.ndpr_expire
= pr
.ndpr_expire
;
594 prfl
->pr
.ndpr_base_calendartime
=
595 pr
.ndpr_base_calendartime
;
596 prfl
->pr
.ndpr_base_uptime
= pr
.ndpr_base_uptime
;
597 prfl
->pr
.ndpr_stateflags
= pr
.ndpr_stateflags
;
598 prfl
->pr
.ndpr_addrcnt
= pr
.ndpr_addrcnt
;
599 prfl
->pr
.ndpr_ifp
= pr
.ndpr_ifp
;
601 prfl
->next
= nd_prefix_list_head
;
602 nd_prefix_list_head
= prfl
;
603 nd_prefix_list_length
++;
610 if (ndopts
.nd_opts_mtu
&& ndopts
.nd_opts_mtu
->nd_opt_mtu_len
== 1) {
611 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
614 if (mtu
< IPV6_MMTU
) {
615 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu option "
616 "mtu=%d sent from %s, ignoring\n",
617 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
621 lck_mtx_lock(&ndi
->lock
);
624 if (mtu
<= ndi
->maxmtu
) {
625 int change
= (ndi
->linkmtu
!= mtu
);
628 lck_mtx_unlock(&ndi
->lock
);
629 if (change
) /* in6_maxmtu may change */
632 nd6log((LOG_INFO
, "nd6_ra_input: bogus mtu "
633 "mtu=%d sent from %s; "
634 "exceeds maxmtu %d, ignoring\n",
635 mtu
, ip6_sprintf(&ip6
->ip6_src
),
637 lck_mtx_unlock(&ndi
->lock
);
640 lck_mtx_unlock(&ndi
->lock
);
641 nd6log((LOG_INFO
, "nd6_ra_input: mtu option "
642 "mtu=%d sent from %s; maxmtu unknown, "
644 mtu
, ip6_sprintf(&ip6
->ip6_src
)));
651 * Source link layer address
653 if (ndopts
.nd_opts_src_lladdr
) {
654 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
655 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
658 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
660 "nd6_ra_input: lladdrlen mismatch for %s "
661 "(if %d, RA packet %d)\n",
662 ip6_sprintf(&saddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
666 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, (int)lladdrlen
,
667 ND_ROUTER_ADVERT
, 0);
670 nd6_post_msg(KEV_ND6_RA
, nd_prefix_list_head
, nd_prefix_list_length
,
671 mtu
, lladdr
, lladdrlen
);
674 * Installing a link-layer address might change the state of the
675 * router's neighbor cache, which might also affect our on-link
676 * detection of adveritsed prefixes.
678 lck_mtx_lock(nd6_mutex
);
679 pfxlist_onlink_check();
680 lck_mtx_unlock(nd6_mutex
);
688 while ((prfl
= nd_prefix_list_head
) != NULL
) {
689 nd_prefix_list_head
= prfl
->next
;
696 icmp6stat
.icp6s_badra
++;
701 * default router list proccessing sub routines
704 /* tell the change to user processes watching the routing socket. */
710 struct rt_addrinfo info
;
711 struct ifnet
*ifp
= rt
->rt_ifp
;
713 RT_LOCK_ASSERT_HELD(rt
);
715 bzero((caddr_t
)&info
, sizeof (info
));
716 /* It's not necessary to lock ifp for if_lladdr */
717 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
718 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
719 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
721 * ifa_addr pointers for both should always be valid
722 * in this context; no need to hold locks.
724 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
725 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
727 rt_missmsg(cmd
, &info
, rt
->rt_flags
, 0);
731 defrouter_addreq(struct nd_defrouter
*new, boolean_t scoped
)
733 struct sockaddr_in6 def
, mask
, gate
;
734 struct rtentry
*newrt
= NULL
;
735 unsigned int ifscope
;
738 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
739 NDDR_LOCK_ASSERT_NOTHELD(new);
741 * We're free to lock and unlock NDDR because our callers
742 * are holding an extra reference for us.
746 if (new->stateflags
& NDDRF_INSTALLED
)
749 if (new->ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
750 nd6log2((LOG_INFO
, "%s: ignoring router %s, scoped=%d, "
751 "static=%d on advertising interface\n", if_name(new->ifp
),
752 ip6_sprintf(&new->rtaddr
), scoped
,
753 (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
757 nd6log2((LOG_INFO
, "%s: adding default router %s, scoped=%d, "
758 "static=%d\n", if_name(new->ifp
), ip6_sprintf(&new->rtaddr
),
759 scoped
, (new->stateflags
& NDDRF_STATIC
) ? 1 : 0));
761 Bzero(&def
, sizeof (def
));
762 Bzero(&mask
, sizeof (mask
));
763 Bzero(&gate
, sizeof (gate
));
765 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
766 = sizeof (struct sockaddr_in6
);
767 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
768 gate
.sin6_addr
= new->rtaddr
;
770 ifscope
= scoped
? new->ifp
->if_index
: IFSCOPE_NONE
;
773 err
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&def
,
774 (struct sockaddr
*)&gate
, (struct sockaddr
*)&mask
,
775 RTF_GATEWAY
, &newrt
, ifscope
);
779 nd6_rtmsg(RTM_ADD
, newrt
); /* tell user process */
780 RT_REMREF_LOCKED(newrt
);
783 new->stateflags
|= NDDRF_INSTALLED
;
784 if (ifscope
!= IFSCOPE_NONE
)
785 new->stateflags
|= NDDRF_IFSCOPE
;
786 new->genid
= nd6_defrouter_genid
;
788 nd6log((LOG_ERR
, "%s: failed to add default router "
789 "%s on %s scoped %d (errno = %d)\n", __func__
,
790 ip6_sprintf(&gate
.sin6_addr
), if_name(new->ifp
),
791 (ifscope
!= IFSCOPE_NONE
), err
));
800 struct nd_defrouter
*
802 struct in6_addr
*addr
,
805 struct nd_defrouter
*dr
;
807 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
809 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
810 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
812 if (dr
->ifp
== ifp
&& IN6_ARE_ADDR_EQUAL(addr
, &dr
->rtaddr
)) {
813 NDDR_ADDREF_LOCKED(dr
);
820 return (NULL
); /* search failed */
824 * Remove the default route for a given router.
825 * This is just a subroutine function for defrouter_select(), and should
826 * not be called from anywhere else.
829 defrouter_delreq(struct nd_defrouter
*dr
)
831 struct sockaddr_in6 def
, mask
, gate
;
832 struct rtentry
*oldrt
= NULL
;
833 unsigned int ifscope
;
836 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
837 NDDR_LOCK_ASSERT_NOTHELD(dr
);
839 * We're free to lock and unlock NDDR because our callers
840 * are holding an extra reference for us.
843 /* ifp would be NULL for the "drany" case */
844 if (dr
->ifp
!= NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
))
847 nd6log2((LOG_INFO
, "%s: removing default router %s, scoped=%d, "
848 "static=%d\n", dr
->ifp
!= NULL
? if_name(dr
->ifp
) : "ANY",
849 ip6_sprintf(&dr
->rtaddr
), (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
850 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
852 Bzero(&def
, sizeof (def
));
853 Bzero(&mask
, sizeof (mask
));
854 Bzero(&gate
, sizeof (gate
));
856 def
.sin6_len
= mask
.sin6_len
= gate
.sin6_len
857 = sizeof (struct sockaddr_in6
);
858 def
.sin6_family
= mask
.sin6_family
= gate
.sin6_family
= AF_INET6
;
859 gate
.sin6_addr
= dr
->rtaddr
;
861 if (dr
->ifp
!= NULL
) {
862 ifscope
= (dr
->stateflags
& NDDRF_IFSCOPE
) ?
863 dr
->ifp
->if_index
: IFSCOPE_NONE
;
865 ifscope
= IFSCOPE_NONE
;
868 err
= rtrequest_scoped(RTM_DELETE
,
869 (struct sockaddr
*)&def
, (struct sockaddr
*)&gate
,
870 (struct sockaddr
*)&mask
, RTF_GATEWAY
, &oldrt
, ifscope
);
874 nd6_rtmsg(RTM_DELETE
, oldrt
);
877 } else if (err
!= ESRCH
) {
878 nd6log((LOG_ERR
, "%s: failed to delete default router "
879 "%s on %s scoped %d (errno = %d)\n", __func__
,
880 ip6_sprintf(&gate
.sin6_addr
), dr
->ifp
!= NULL
?
881 if_name(dr
->ifp
) : "ANY", (ifscope
!= IFSCOPE_NONE
), err
));
884 /* ESRCH means it's no longer in the routing table; ignore it */
885 if (oldrt
!= NULL
|| err
== ESRCH
) {
886 dr
->stateflags
&= ~NDDRF_INSTALLED
;
887 if (ifscope
!= IFSCOPE_NONE
)
888 dr
->stateflags
&= ~NDDRF_IFSCOPE
;
897 * remove all default routes from default router list
900 defrouter_reset(void)
902 struct nd_defrouter
*dr
, drany
;
904 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
906 dr
= TAILQ_FIRST(&nd_defrouter
);
909 if (dr
->stateflags
& NDDRF_INSTALLED
) {
910 NDDR_ADDREF_LOCKED(dr
);
912 lck_mtx_unlock(nd6_mutex
);
913 defrouter_delreq(dr
);
914 lck_mtx_lock(nd6_mutex
);
916 dr
= TAILQ_FIRST(&nd_defrouter
);
919 dr
= TAILQ_NEXT(dr
, dr_entry
);
923 /* Nuke primary (non-scoped) default router */
924 if (ip6_doscopedroute
) {
925 bzero(&drany
, sizeof (drany
));
926 lck_mtx_init(&drany
.nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
927 lck_mtx_unlock(nd6_mutex
);
928 defrouter_delreq(&drany
);
929 lck_mtx_destroy(&drany
.nddr_lock
, ifa_mtx_grp
);
930 lck_mtx_lock(nd6_mutex
);
936 defrtrlist_ioctl(u_long cmd
, caddr_t data
)
938 struct nd_defrouter dr0
;
939 unsigned int ifindex
;
940 struct ifnet
*dr_ifp
;
941 int error
= 0, add
= 0;
944 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
945 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
948 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
949 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
950 bzero(&dr0
, sizeof (dr0
));
951 if (cmd
== SIOCDRADD_IN6_64
|| cmd
== SIOCDRDEL_IN6_64
) {
952 struct in6_defrouter_64
*r_64
=
953 (struct in6_defrouter_64
*)(void *)data
;
956 bcopy(&r_64
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
957 sizeof (dr0
.rtaddr
));
958 dr0
.flags
= r_64
->flags
;
959 bcopy(&r_64
->if_index
, &i
, sizeof (i
));
962 struct in6_defrouter_32
*r_32
=
963 (struct in6_defrouter_32
*)(void *)data
;
966 bcopy(&r_32
->rtaddr
.sin6_addr
, &dr0
.rtaddr
,
967 sizeof (dr0
.rtaddr
));
968 dr0
.flags
= r_32
->flags
;
969 bcopy(&r_32
->if_index
, &i
, sizeof (i
));
972 ifnet_head_lock_shared();
973 /* Don't need to check is ifindex is < 0 since it's unsigned */
974 if (if_index
< ifindex
||
975 (dr_ifp
= ifindex2ifnet
[ifindex
]) == NULL
) {
983 if (IN6_IS_SCOPE_EMBED(&dr0
.rtaddr
)) {
984 uint16_t *scope
= &dr0
.rtaddr
.s6_addr16
[1];
987 *scope
= htons(dr_ifp
->if_index
);
988 } else if (*scope
!= htons(dr_ifp
->if_index
)) {
995 error
= defrtrlist_add_static(&dr0
);
996 if (!add
|| error
!= 0) {
997 int err
= defrtrlist_del_static(&dr0
);
1004 error
= EOPNOTSUPP
; /* check for safety */
1012 defrtrlist_del(struct nd_defrouter
*dr
)
1014 struct nd_defrouter
*deldr
= NULL
;
1015 struct nd_prefix
*pr
;
1016 struct ifnet
*ifp
= dr
->ifp
;
1017 struct nd_ifinfo
*ndi
= NULL
;
1020 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1022 if (!ip6_doscopedroute
&& dr
== TAILQ_FIRST(&nd_defrouter
))
1023 deldr
= dr
; /* The router is primary. */
1025 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1026 ++nd6_defrouter_genid
;
1028 * Flush all the routing table entries that use the router
1031 if (ip6_doscopedroute
|| !ip6_forwarding
) {
1032 /* above is a good condition? */
1034 lck_mtx_unlock(nd6_mutex
);
1035 rt6_flush(&dr
->rtaddr
, ifp
);
1036 lck_mtx_lock(nd6_mutex
);
1039 nd6log2((LOG_INFO
, "%s: freeing defrouter %s\n", if_name(dr
->ifp
),
1040 ip6_sprintf(&dr
->rtaddr
)));
1042 * Delete it from the routing table.
1045 lck_mtx_unlock(nd6_mutex
);
1046 defrouter_delreq(dr
);
1047 lck_mtx_lock(nd6_mutex
);
1051 * Also delete all the pointers to the router in each prefix lists.
1053 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1054 struct nd_pfxrouter
*pfxrtr
;
1057 if ((pfxrtr
= pfxrtr_lookup(pr
, dr
)) != NULL
)
1058 pfxrtr_del(pfxrtr
, pr
);
1062 pfxlist_onlink_check();
1065 * If the router is the primary one, choose a new one. If Scoped
1066 * Routing is enabled, always try to pick another eligible router
1067 * on this interface.
1069 if (deldr
|| ip6_doscopedroute
)
1070 defrouter_select(ifp
);
1073 ndi
= ND_IFINFO(ifp
);
1074 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1075 lck_mtx_lock(&ndi
->lock
);
1076 VERIFY(ndi
->ndefrouters
>= 0);
1077 if (ndi
->ndefrouters
> 0 && --ndi
->ndefrouters
== 0) {
1081 lck_mtx_unlock(&ndi
->lock
);
1086 NDDR_REMREF(dr
); /* remove list reference */
1090 defrtrlist_add_static(struct nd_defrouter
*new)
1092 struct nd_defrouter
*dr
;
1095 new->rtlifetime
= -1;
1096 new->stateflags
|= NDDRF_STATIC
;
1098 /* we only want the preference level */
1099 new->flags
&= ND_RA_FLAG_RTPREF_MASK
;
1101 lck_mtx_lock(nd6_mutex
);
1102 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1103 if (dr
!= NULL
&& !(dr
->stateflags
& NDDRF_STATIC
)) {
1108 dr
= defrtrlist_update(new);
1116 lck_mtx_unlock(nd6_mutex
);
1122 defrtrlist_del_static(struct nd_defrouter
*new)
1124 struct nd_defrouter
*dr
;
1126 lck_mtx_lock(nd6_mutex
);
1127 dr
= defrouter_lookup(&new->rtaddr
, new->ifp
);
1128 if (dr
== NULL
|| !(dr
->stateflags
& NDDRF_STATIC
)) {
1136 lck_mtx_unlock(nd6_mutex
);
1138 return (dr
!= NULL
? 0 : EINVAL
);
1142 * for default router selection
1143 * regards router-preference field as a 2-bit signed integer
1146 rtpref(struct nd_defrouter
*dr
)
1148 switch (dr
->flags
& ND_RA_FLAG_RTPREF_MASK
) {
1149 case ND_RA_FLAG_RTPREF_HIGH
:
1150 return (RTPREF_HIGH
);
1151 case ND_RA_FLAG_RTPREF_MEDIUM
:
1152 case ND_RA_FLAG_RTPREF_RSV
:
1153 return (RTPREF_MEDIUM
);
1154 case ND_RA_FLAG_RTPREF_LOW
:
1155 return (RTPREF_LOW
);
1158 * This case should never happen. If it did, it would mean a
1159 * serious bug of kernel internal. We thus always bark here.
1160 * Or, can we even panic?
1162 log(LOG_ERR
, "rtpref: impossible RA flag %x\n", dr
->flags
);
1163 return (RTPREF_INVALID
);
1169 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
1171 * 1) Routers that are reachable or probably reachable should be preferred.
1172 * If we have more than one (probably) reachable router, prefer ones
1173 * with the highest router preference.
1174 * 2) When no routers on the list are known to be reachable or
1175 * probably reachable, routers SHOULD be selected in a round-robin
1176 * fashion, regardless of router preference values.
1177 * 3) If the Default Router List is empty, assume that all
1178 * destinations are on-link.
1180 * When Scoped Routing is enabled, the selection logic is amended as follows:
1182 * a) When a default interface is specified, the primary/non-scoped default
1183 * router will be set to the reachable router on that link (if any) with
1184 * the highest router preference.
1185 * b) When there are more than one routers on the same link, the one with
1186 * the highest router preference will be installed, either as scoped or
1187 * non-scoped route entry. If they all share the same preference value,
1188 * the one installed will be the static or the first encountered reachable
1189 * router, i.e. static one wins over dynamic.
1190 * c) When no routers on the list are known to be reachable, or probably
1191 * reachable, no round-robin selection will take place when the default
1194 * We assume nd_defrouter is sorted by router preference value.
1195 * Since the code below covers both with and without router preference cases,
1196 * we do not need to classify the cases by ifdef.
1199 defrouter_select(struct ifnet
*ifp
)
1202 struct nd_defrouter
*dr
, *selected_dr
= NULL
, *installed_dr
= NULL
;
1203 struct nd_defrouter
*installed_dr0
= NULL
;
1204 struct rtentry
*rt
= NULL
;
1205 struct llinfo_nd6
*ln
= NULL
;
1207 boolean_t found_installedrt
= FALSE
;
1209 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1212 * We no longer install (default) interface route; only prefix routes
1213 * are installed as interface routes. Therefore, there is no harm in
1214 * going through this routine even if a default interface is specified,
1215 * which happens when Scoped Routing is enabled. But for efficiency,
1216 * we fall back to the original KAME logic when Scoped Routing is
1219 if (ip6_forwarding
&& !ip6_doscopedroute
) {
1220 nd6log((LOG_WARNING
,
1221 "defrouter_select: called unexpectedly (forwarding=%d)\n",
1227 * Let's handle easy case (3) first:
1228 * If default router list is empty, there's nothing to be done.
1230 if (!TAILQ_FIRST(&nd_defrouter
))
1234 * Due to the number of times we drop nd6_mutex, we need to
1235 * serialize this function.
1237 while (nd_defrouter_busy
) {
1238 nd_defrouter_waiters
++;
1239 msleep(nd_defrouter_waitchan
, nd6_mutex
, (PZERO
-1),
1241 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1243 nd_defrouter_busy
= TRUE
;
1246 * Search for a (probably) reachable router from the list.
1247 * We just pick up the first reachable one (if any), assuming that
1248 * the ordering rule of the list described in defrtrlist_update().
1250 * For all intents and purposes of Scoped Routing:
1251 * selected_dr = candidate for primary router
1252 * installed_dr = currently installed primary router
1254 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1255 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1256 boolean_t reachable
, advrouter
;
1257 struct in6_addr rtaddr
;
1258 struct ifnet
*drifp
;
1259 struct nd_defrouter
*drrele
;
1264 rtaddr
= *(&dr
->rtaddr
);
1266 advrouter
= (drifp
!= NULL
&&
1267 (drifp
->if_eflags
& IFEF_IPV6_ROUTER
));
1268 NDDR_ADDREF_LOCKED(dr
); /* for this for loop */
1271 lck_mtx_unlock(nd6_mutex
);
1272 /* Callee returns a locked route upon success */
1273 if ((rt
= nd6_lookup(&rtaddr
, 0, drifp
, 0)) != NULL
) {
1274 RT_LOCK_ASSERT_HELD(rt
);
1275 if ((ln
= rt
->rt_llinfo
) != NULL
&&
1276 ND6_IS_LLINFO_PROBREACH(ln
)) {
1278 if (selected_dr
== NULL
&&
1279 (!ip6_doscopedroute
||
1280 (drifp
== nd6_defifp
&& !advrouter
))) {
1282 NDDR_ADDREF(selected_dr
);
1285 RT_REMREF_LOCKED(rt
);
1289 lck_mtx_lock(nd6_mutex
);
1291 /* Handle case (b) */
1293 if (ip6_doscopedroute
&& drifp
== nd6_defifp
&& !advrouter
&&
1294 (selected_dr
== NULL
|| rtpref(dr
) > rtpref(selected_dr
) ||
1295 (rtpref(dr
) == rtpref(selected_dr
) &&
1296 (dr
->stateflags
& NDDRF_STATIC
) &&
1297 !(selected_dr
->stateflags
& NDDRF_STATIC
)))) {
1299 /* Release it later on */
1300 VERIFY(drrele
== NULL
);
1301 drrele
= selected_dr
;
1304 NDDR_ADDREF_LOCKED(selected_dr
);
1307 if (!(dr
->stateflags
& NDDRF_INSTALLED
)) {
1309 * If the router hasn't been installed and it is
1310 * reachable, try to install it later on below.
1311 * If it's static, try to install it anyway.
1313 if (!advrouter
&& (reachable
||
1314 (dr
->stateflags
& NDDRF_STATIC
))) {
1317 nd6log2((LOG_INFO
, "%s: possible router %s, "
1318 "scoped=%d, static=%d\n", if_name(drifp
),
1319 ip6_sprintf(&rtaddr
),
1320 (dr
->stateflags
& NDDRF_IFSCOPE
) ? 1 : 0,
1321 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1324 NDDR_REMREF(dr
); /* for this for loop */
1326 NDDR_REMREF(drrele
);
1330 /* Record the currently installed primary/non-scoped router */
1331 if (!ip6_doscopedroute
|| !(dr
->stateflags
& NDDRF_IFSCOPE
)) {
1332 if (installed_dr
== NULL
) {
1334 NDDR_ADDREF_LOCKED(installed_dr
);
1336 /* this should not happen; warn for diagnosis */
1337 log(LOG_ERR
, "defrouter_select: more than one "
1338 "%s default router is installed\n",
1339 ip6_doscopedroute
? "non-scoped" : "");
1343 NDDR_REMREF(dr
); /* for this for loop */
1345 NDDR_REMREF(drrele
);
1348 /* If none was selected, use the currently installed one */
1349 if (ip6_doscopedroute
&& selected_dr
== NULL
&& installed_dr
!= NULL
) {
1350 selected_dr
= installed_dr
;
1351 NDDR_ADDREF(selected_dr
);
1355 * Install the unreachable one(s) if necesssary.
1357 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1358 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1359 struct nd_defrouter
*_dr
;
1361 if (!ip6_doscopedroute
)
1366 /* If already (or will be) installed, skip */
1367 if ((dr
->stateflags
& NDDRF_INSTALLED
) || dr
->genid
== -1) {
1372 /* See if there is already a default router for the link */
1373 for (_dr
= TAILQ_FIRST(&nd_defrouter
); _dr
;
1374 _dr
= TAILQ_NEXT(_dr
, dr_entry
)) {
1377 if (_dr
== dr
|| _dr
->ifp
!= dr
->ifp
) {
1383 if ((_dr
->stateflags
& NDDRF_INSTALLED
) ||
1393 /* If none so far, schedule it to be installed below */
1394 if (_dr
== NULL
&& dr
->ifp
!= NULL
&&
1395 !(dr
->ifp
->if_eflags
& IFEF_IPV6_ROUTER
)) {
1398 nd6log2((LOG_INFO
, "%s: possible router %s, "
1399 "static=%d (unreachable)\n", if_name(dr
->ifp
),
1400 ip6_sprintf(&dr
->rtaddr
),
1401 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1408 nd6log2((LOG_INFO
, "%s: considering primary default router %s, "
1409 "static=%d [round 1]\n", if_name(dr
->ifp
),
1410 ip6_sprintf(&dr
->rtaddr
),
1411 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1415 * If none of the default routers was found to be reachable,
1416 * round-robin the list regardless of preference, except when
1417 * Scoped Routing is enabled per case (c).
1419 * Otherwise, if we have an installed router, check if the selected
1420 * (reachable) router should really be preferred to the installed one.
1421 * We only prefer the new router when the old one is not reachable
1422 * or when the new one has a really higher preference value.
1424 if (!ip6_doscopedroute
&& selected_dr
== NULL
) {
1425 if (installed_dr
== NULL
||
1426 !TAILQ_NEXT(installed_dr
, dr_entry
)) {
1427 selected_dr
= TAILQ_FIRST(&nd_defrouter
);
1429 NDDR_ADDREF(selected_dr
);
1431 selected_dr
= TAILQ_NEXT(installed_dr
, dr_entry
);
1433 NDDR_ADDREF(selected_dr
);
1435 } else if (selected_dr
!= NULL
&& installed_dr
!= NULL
) {
1436 lck_mtx_unlock(nd6_mutex
);
1437 rt
= nd6_lookup(&installed_dr
->rtaddr
, 0, installed_dr
->ifp
, 0);
1439 RT_LOCK_ASSERT_HELD(rt
);
1440 if ((ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
) &&
1441 ND6_IS_LLINFO_PROBREACH(ln
) &&
1442 (!ip6_doscopedroute
||
1443 installed_dr
->ifp
== nd6_defifp
) &&
1444 rtpref(selected_dr
) <= rtpref(installed_dr
)) {
1445 NDDR_REMREF(selected_dr
);
1446 selected_dr
= installed_dr
;
1447 NDDR_ADDREF(selected_dr
);
1449 RT_REMREF_LOCKED(rt
);
1452 found_installedrt
= TRUE
;
1454 lck_mtx_lock(nd6_mutex
);
1457 if (ip6_doscopedroute
) {
1459 * If the installed primary router is not on the current
1460 * IPv6 default interface, demote it to a scoped entry.
1462 if (installed_dr
!= NULL
&& installed_dr
->ifp
!= nd6_defifp
&&
1463 !(installed_dr
->stateflags
& NDDRF_IFSCOPE
)) {
1464 if (selected_dr
!= NULL
&&
1465 selected_dr
->ifp
!= nd6_defifp
) {
1466 NDDR_REMREF(selected_dr
);
1473 * If the selected router is currently scoped, make sure
1474 * we update (it needs to be promoted to primary.)
1476 if (selected_dr
!= NULL
&&
1477 (selected_dr
->stateflags
& NDDRF_IFSCOPE
))
1481 * If the installed router is no longer reachable, remove
1482 * it and install the selected router instead.
1484 if (installed_dr
!= NULL
1485 && selected_dr
!= NULL
1486 && installed_dr
!= selected_dr
1487 && found_installedrt
== FALSE
1488 && installed_dr
->ifp
== selected_dr
->ifp
) {
1490 installed_dr0
= installed_dr
;
1491 /* NB: we previousled referenced installed_dr */
1492 installed_dr
= NULL
;
1493 selected_dr
->genid
= -1;
1499 * If Scoped Routing is enabled and there's nothing to update,
1500 * just return. Otherwise, if Scoped Routing is disabled and if
1501 * the selected router is different than the installed one,
1502 * remove the installed router and install the selected one.
1505 VERIFY(dr
!= NULL
|| ip6_doscopedroute
);
1506 if (!ip6_doscopedroute
|| !update
) {
1510 if (dr
!= installed_dr
) {
1511 nd6log2((LOG_INFO
, "%s: no update, selected router %s, "
1512 "installed router %s\n", if_name(dr
->ifp
),
1513 ip6_sprintf(&dr
->rtaddr
), installed_dr
!= NULL
?
1514 ip6_sprintf(&installed_dr
->rtaddr
) : "NONE"));
1516 nd6log2((LOG_INFO
, "%s: no update, router is %s\n",
1517 if_name(dr
->ifp
), ip6_sprintf(&dr
->rtaddr
)));
1519 if (!ip6_doscopedroute
&& installed_dr
!= dr
) {
1521 * No need to ADDREF dr because at this point
1522 * dr points to selected_dr, which already holds
1525 lck_mtx_unlock(nd6_mutex
);
1527 defrouter_delreq(installed_dr
);
1529 defrouter_addreq(dr
, FALSE
);
1530 lck_mtx_lock(nd6_mutex
);
1536 * Scoped Routing is enabled and we need to update. The selected
1537 * router needs to be installed as primary/non-scoped entry. If
1538 * there is any existing entry that is non-scoped, remove it from
1539 * the routing table and reinstall it as scoped entry.
1542 nd6log2((LOG_INFO
, "%s: considering primary default router %s, "
1543 "static=%d [round 2]\n", if_name(dr
->ifp
),
1544 ip6_sprintf(&dr
->rtaddr
),
1545 (dr
->stateflags
& NDDRF_STATIC
) ? 1 : 0));
1549 * On the following while loops we use two flags:
1553 * genid is used to skip entries that are not to be added/removed on the
1554 * second while loop.
1555 * NDDRF_PROCESSED is used to skip entries that were already
1557 * This is necessary because we drop the nd6_mutex and start the while
1560 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
1562 VERIFY((dr
->stateflags
& NDDRF_PROCESSED
) == 0);
1565 /* Remove conflicting entries */
1566 dr
= TAILQ_FIRST(&nd_defrouter
);
1569 if (!(dr
->stateflags
& NDDRF_INSTALLED
) ||
1570 dr
->stateflags
& NDDRF_PROCESSED
) {
1572 dr
= TAILQ_NEXT(dr
, dr_entry
);
1575 dr
->stateflags
|= NDDRF_PROCESSED
;
1577 /* A NULL selected_dr will remove primary default route */
1578 if ((dr
== selected_dr
&& (dr
->stateflags
& NDDRF_IFSCOPE
)) ||
1579 (dr
!= selected_dr
&& !(dr
->stateflags
& NDDRF_IFSCOPE
))) {
1580 NDDR_ADDREF_LOCKED(dr
);
1582 lck_mtx_unlock(nd6_mutex
);
1583 defrouter_delreq(dr
);
1584 lck_mtx_lock(nd6_mutex
);
1586 if (dr
&& dr
!= installed_dr0
)
1591 * Since we lost nd6_mutex, we have to start over.
1593 dr
= TAILQ_FIRST(&nd_defrouter
);
1597 dr
= TAILQ_NEXT(dr
, dr_entry
);
1600 /* -1 is a special number, make sure we don't use it for genid */
1601 if (++nd6_defrouter_genid
== -1)
1602 nd6_defrouter_genid
= 1;
1604 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
1606 dr
->stateflags
&= ~NDDRF_PROCESSED
;
1609 /* Add the entries back */
1610 dr
= TAILQ_FIRST(&nd_defrouter
);
1612 struct nd_defrouter
*_dr
;
1615 if (dr
->stateflags
& NDDRF_PROCESSED
||
1618 dr
= TAILQ_NEXT(dr
, dr_entry
);
1621 dr
->stateflags
|= NDDRF_PROCESSED
;
1623 /* Handle case (b) */
1624 for (_dr
= TAILQ_FIRST(&nd_defrouter
); _dr
;
1625 _dr
= TAILQ_NEXT(_dr
, dr_entry
)) {
1629 * This is safe because we previously checked if
1633 if (_dr
->ifp
== dr
->ifp
&& rtpref(_dr
) >= rtpref(dr
) &&
1634 (_dr
->stateflags
& NDDRF_INSTALLED
)) {
1635 NDDR_ADDREF_LOCKED(_dr
);
1642 /* If same preference and i/f, static entry takes precedence */
1643 if (_dr
!= NULL
&& rtpref(_dr
) == rtpref(dr
) &&
1644 !(_dr
->stateflags
& NDDRF_STATIC
) &&
1645 (dr
->stateflags
& NDDRF_STATIC
)) {
1646 lck_mtx_unlock(nd6_mutex
);
1647 defrouter_delreq(_dr
);
1648 lck_mtx_lock(nd6_mutex
);
1653 if (_dr
== NULL
&& !(dr
->stateflags
& NDDRF_INSTALLED
)) {
1654 NDDR_ADDREF_LOCKED(dr
);
1656 lck_mtx_unlock(nd6_mutex
);
1657 defrouter_addreq(dr
, (selected_dr
== NULL
||
1658 dr
->ifp
!= selected_dr
->ifp
));
1659 dr
->genid
= nd6_defrouter_genid
;
1660 lck_mtx_lock(nd6_mutex
);
1663 * Since we lost nd6_mutex, we have to start over.
1665 dr
= TAILQ_FIRST(&nd_defrouter
);
1669 dr
= TAILQ_NEXT(dr
, dr_entry
);
1672 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
1674 dr
->stateflags
&= ~NDDRF_PROCESSED
;
1678 NDDR_REMREF(selected_dr
);
1680 NDDR_REMREF(installed_dr
);
1682 NDDR_REMREF(installed_dr0
);
1683 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1684 VERIFY(nd_defrouter_busy
);
1685 nd_defrouter_busy
= FALSE
;
1686 if (nd_defrouter_waiters
> 0) {
1687 nd_defrouter_waiters
= 0;
1688 wakeup(nd_defrouter_waitchan
);
1692 static struct nd_defrouter
*
1693 defrtrlist_update_common(struct nd_defrouter
*new, boolean_t scoped
)
1695 struct nd_defrouter
*dr
, *n
;
1696 struct ifnet
*ifp
= new->ifp
;
1697 struct nd_ifinfo
*ndi
= NULL
;
1698 struct timeval caltime
;
1700 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1702 if ((dr
= defrouter_lookup(&new->rtaddr
, ifp
)) != NULL
) {
1704 if (new->rtlifetime
== 0) {
1709 int oldpref
= rtpref(dr
);
1712 dr
->flags
= new->flags
; /* xxx flag check */
1713 dr
->rtlifetime
= new->rtlifetime
;
1714 dr
->expire
= new->expire
;
1717 * If the preference does not change, there's no need
1718 * to sort the entries. If Scoped Routing is enabled,
1719 * put the primary/non-scoped router at the top of the
1720 * list of routers in the same preference band, unless
1721 * it's already at that position.
1723 if (ip6_doscopedroute
) {
1724 struct nd_defrouter
*p
= NULL
;
1726 /* same preference and scoped; just return */
1727 if (rtpref(new) == oldpref
&& scoped
)
1730 n
= TAILQ_FIRST(&nd_defrouter
);
1732 /* preference changed; sort it */
1733 if (rtpref(new) != oldpref
)
1736 /* not at the top of band; sort it */
1737 if (n
!= dr
&& rtpref(n
) == oldpref
&&
1738 (!p
|| rtpref(p
) > rtpref(n
)))
1742 n
= TAILQ_NEXT(n
, dr_entry
);
1745 /* nothing has changed, just return */
1746 if (n
== NULL
&& (scoped
||
1747 !(dr
->stateflags
& NDDRF_IFSCOPE
)))
1749 } else if (rtpref(new) == oldpref
) {
1754 * preferred router may be changed, so relocate
1756 * XXX: calling TAILQ_REMOVE directly is a bad manner.
1757 * However, since defrtrlist_del() has many side
1758 * effects, we intentionally do so here.
1759 * defrouter_select() below will handle routing
1762 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1763 new->stateflags
= dr
->stateflags
;
1764 new->stateflags
&= ~NDDRF_PROCESSED
;
1774 /* entry does not exist */
1775 if (new->rtlifetime
== 0) {
1779 n
= nddr_alloc(M_WAITOK
);
1784 ndi
= ND_IFINFO(ifp
);
1785 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1786 lck_mtx_lock(&ndi
->lock
);
1787 if (ip6_maxifdefrouters
>= 0 &&
1788 ndi
->ndefrouters
>= ip6_maxifdefrouters
) {
1789 lck_mtx_unlock(&ndi
->lock
);
1794 NDDR_ADDREF(n
); /* for the nd_defrouter list */
1795 NDDR_ADDREF(n
); /* for the caller */
1797 ++nd6_defrouter_genid
;
1799 VERIFY(ndi
->ndefrouters
!= 0);
1800 lck_mtx_unlock(&ndi
->lock
);
1802 nd6log2((LOG_INFO
, "%s: allocating defrouter %s\n", if_name(ifp
),
1803 ip6_sprintf(&new->rtaddr
)));
1805 getmicrotime(&caltime
);
1807 memcpy(&n
->rtaddr
, &new->rtaddr
, sizeof (n
->rtaddr
));
1808 n
->flags
= new->flags
;
1809 n
->stateflags
= new->stateflags
;
1810 n
->stateflags
&= ~NDDRF_PROCESSED
;
1811 n
->rtlifetime
= new->rtlifetime
;
1812 n
->expire
= new->expire
;
1813 n
->base_calendartime
= caltime
.tv_sec
;
1814 n
->base_uptime
= net_uptime();
1816 n
->genid
= new->genid
;
1820 /* get nd6_service() to be scheduled as soon as it's convenient */
1821 ++nd6_sched_timeout_want
;
1824 * Insert the new router in the Default Router List;
1825 * The Default Router List should be in the descending order
1826 * of router-preferece. When Scoped Routing is disabled, routers
1827 * with the same preference are sorted in the arriving time order;
1828 * otherwise, the first entry in the list of routers having the same
1829 * preference is the primary default router, when the interface used
1830 * by the entry is the default interface.
1833 /* insert at the end of the group */
1834 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1835 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1836 if (rtpref(n
) > rtpref(dr
) ||
1837 (ip6_doscopedroute
&& !scoped
&& rtpref(n
) == rtpref(dr
)))
1841 TAILQ_INSERT_BEFORE(dr
, n
, dr_entry
);
1843 TAILQ_INSERT_TAIL(&nd_defrouter
, n
, dr_entry
);
1845 defrouter_select(ifp
);
1850 static struct nd_defrouter
*
1851 defrtrlist_update(struct nd_defrouter
*new)
1853 struct nd_defrouter
*dr
;
1855 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1856 dr
= defrtrlist_update_common(new,
1857 (nd6_defifp
!= NULL
&& new->ifp
!= nd6_defifp
));
1863 defrtrlist_sync(struct ifnet
*ifp
)
1865 struct nd_defrouter
*dr
, new;
1867 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1869 if (!ip6_doscopedroute
) {
1870 defrouter_select(ifp
);
1874 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
1875 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
1877 if (dr
->ifp
== ifp
&& (dr
->stateflags
& NDDRF_INSTALLED
))
1883 defrouter_select(ifp
);
1885 memcpy(&new.rtaddr
, &dr
->rtaddr
, sizeof (new.rtaddr
));
1886 new.flags
= dr
->flags
;
1887 new.stateflags
= dr
->stateflags
;
1888 new.stateflags
&= ~NDDRF_PROCESSED
;
1889 new.rtlifetime
= dr
->rtlifetime
;
1890 new.expire
= dr
->expire
;
1892 new.genid
= dr
->genid
;
1895 dr
= defrtrlist_update_common(&new, FALSE
);
1901 static struct nd_pfxrouter
*
1902 pfxrtr_lookup(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1904 struct nd_pfxrouter
*search
;
1906 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1907 NDPR_LOCK_ASSERT_HELD(pr
);
1909 for (search
= pr
->ndpr_advrtrs
.lh_first
; search
;
1910 search
= search
->pfr_next
) {
1911 if (search
->router
== dr
)
1919 pfxrtr_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
)
1921 struct nd_pfxrouter
*new;
1923 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1924 NDPR_LOCK_ASSERT_NOTHELD(pr
);
1926 new = zalloc(ndprtr_zone
);
1929 bzero(new, sizeof (*new));
1933 LIST_INSERT_HEAD(&pr
->ndpr_advrtrs
, new, pfr_entry
);
1937 pfxlist_onlink_check();
1941 pfxrtr_del(struct nd_pfxrouter
*pfr
, struct nd_prefix
*pr
)
1943 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1944 NDPR_LOCK_ASSERT_HELD(pr
);
1946 LIST_REMOVE(pfr
, pfr_entry
);
1947 zfree(ndprtr_zone
, pfr
);
1951 * The routine has been modified to atomically refresh expiry
1952 * time for nd6 prefix as the part of lookup.
1953 * rdar://20339655 explains the corner case where a system going
1954 * in sleep gets rid of manual addresses configured in the system
1955 * and then schedules the prefix for deletion.
1956 * However before the prefix gets deleted, if system comes out
1957 * from sleep and configures same address before prefix deletion
1958 * , the later prefix deletion will remove the prefix route and
1959 * the system will not be able to communicate with other IPv6
1960 * neighbor nodes in the same subnet.
1963 nd6_prefix_lookup(struct nd_prefix
*pr
, int nd6_prefix_expiry
)
1965 struct nd_prefix
*search
;
1967 lck_mtx_lock(nd6_mutex
);
1968 for (search
= nd_prefix
.lh_first
; search
; search
= search
->ndpr_next
) {
1970 if (pr
->ndpr_ifp
== search
->ndpr_ifp
&&
1971 pr
->ndpr_plen
== search
->ndpr_plen
&&
1972 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
1973 &search
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
)) {
1974 if (nd6_prefix_expiry
!= ND6_PREFIX_EXPIRY_UNSPEC
) {
1975 search
->ndpr_expire
= nd6_prefix_expiry
;
1977 NDPR_ADDREF_LOCKED(search
);
1978 NDPR_UNLOCK(search
);
1981 NDPR_UNLOCK(search
);
1983 lck_mtx_unlock(nd6_mutex
);
1989 nd6_prelist_add(struct nd_prefix
*pr
, struct nd_defrouter
*dr
,
1990 struct nd_prefix
**newp
, boolean_t force_scoped
)
1992 struct nd_prefix
*new = NULL
;
1993 struct ifnet
*ifp
= pr
->ndpr_ifp
;
1994 struct nd_ifinfo
*ndi
= NULL
;
1997 if (ip6_maxifprefixes
>= 0) {
1998 ndi
= ND_IFINFO(ifp
);
1999 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2000 lck_mtx_lock(&ndi
->lock
);
2001 if (ndi
->nprefixes
>= ip6_maxifprefixes
) {
2002 lck_mtx_unlock(&ndi
->lock
);
2005 lck_mtx_unlock(&ndi
->lock
);
2008 new = ndpr_alloc(M_WAITOK
);
2014 new->ndpr_ifp
= pr
->ndpr_ifp
;
2015 new->ndpr_prefix
= pr
->ndpr_prefix
;
2016 new->ndpr_plen
= pr
->ndpr_plen
;
2017 new->ndpr_vltime
= pr
->ndpr_vltime
;
2018 new->ndpr_pltime
= pr
->ndpr_pltime
;
2019 new->ndpr_flags
= pr
->ndpr_flags
;
2020 if (pr
->ndpr_stateflags
& NDPRF_STATIC
)
2021 new->ndpr_stateflags
|= NDPRF_STATIC
;
2023 if ((error
= in6_init_prefix_ltimes(new)) != 0) {
2028 new->ndpr_lastupdate
= net_uptime();
2031 NDPR_ADDREF_LOCKED(new); /* for caller */
2033 /* initialization */
2034 LIST_INIT(&new->ndpr_advrtrs
);
2035 in6_prefixlen2mask(&new->ndpr_mask
, new->ndpr_plen
);
2036 /* make prefix in the canonical form */
2037 for (i
= 0; i
< 4; i
++)
2038 new->ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
2039 new->ndpr_mask
.s6_addr32
[i
];
2043 /* get nd6_service() to be scheduled as soon as it's convenient */
2044 ++nd6_sched_timeout_want
;
2046 lck_mtx_lock(nd6_mutex
);
2047 /* link ndpr_entry to nd_prefix list */
2048 LIST_INSERT_HEAD(&nd_prefix
, new, ndpr_entry
);
2049 new->ndpr_debug
|= IFD_ATTACHED
;
2050 NDPR_ADDREF(new); /* for nd_prefix list */
2052 /* ND_OPT_PI_FLAG_ONLINK processing */
2053 if (new->ndpr_raf_onlink
) {
2056 if ((e
= nd6_prefix_onlink_common(new, force_scoped
,
2057 new->ndpr_ifp
->if_index
)) != 0) {
2058 nd6log((LOG_ERR
, "nd6_prelist_add: failed to make "
2059 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
2060 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2061 new->ndpr_plen
, force_scoped
? "scoped" :
2062 "non-scoped", if_name(ifp
), e
));
2063 /* proceed anyway. XXX: is it correct? */
2068 pfxrtr_add(new, dr
);
2071 lck_mtx_lock(&ndi
->lock
);
2073 VERIFY(ndi
->nprefixes
!= 0);
2074 lck_mtx_unlock(&ndi
->lock
);
2076 lck_mtx_unlock(nd6_mutex
);
2082 * Caller must have held an extra reference on nd_prefix.
2085 prelist_remove(struct nd_prefix
*pr
)
2087 struct nd_pfxrouter
*pfr
, *next
;
2088 struct ifnet
*ifp
= pr
->ndpr_ifp
;
2090 struct nd_ifinfo
*ndi
= NULL
;
2092 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2093 NDPR_LOCK_ASSERT_HELD(pr
);
2095 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
2099 * If there are no more addresses, defunct the prefix. This is needed
2100 * because we don't want multiple threads calling prelist_remove() for
2101 * the same prefix and this might happen because we unlock nd6_mutex
2104 if (pr
->ndpr_addrcnt
== 0)
2105 pr
->ndpr_stateflags
|= NDPRF_DEFUNCT
;
2107 /* make sure to invalidate the prefix until it is really freed. */
2108 pr
->ndpr_vltime
= 0;
2109 pr
->ndpr_pltime
= 0;
2112 * Though these flags are now meaningless, we'd rather keep the value
2113 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2114 * when executing "ndp -p".
2116 if (pr
->ndpr_stateflags
& NDPRF_ONLINK
) {
2117 NDPR_ADDREF_LOCKED(pr
);
2119 lck_mtx_unlock(nd6_mutex
);
2120 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
2121 nd6log((LOG_ERR
, "prelist_remove: failed to make "
2122 "%s/%d offlink on %s, errno=%d\n",
2123 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2124 pr
->ndpr_plen
, if_name(ifp
), e
));
2125 /* what should we do? */
2127 lck_mtx_lock(nd6_mutex
);
2129 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2133 if (pr
->ndpr_addrcnt
> 0) {
2135 * The state might have changed if we called
2136 * nd6_prefix_offlink().
2138 pr
->ndpr_stateflags
&= ~NDPRF_DEFUNCT
;
2139 return; /* notice here? */
2142 /* unlink ndpr_entry from nd_prefix list */
2143 LIST_REMOVE(pr
, ndpr_entry
);
2144 pr
->ndpr_debug
&= ~IFD_ATTACHED
;
2146 /* free list of routers that adversed the prefix */
2147 for (pfr
= pr
->ndpr_advrtrs
.lh_first
; pfr
; pfr
= next
) {
2148 next
= pfr
->pfr_next
;
2149 pfxrtr_del(pfr
, pr
);
2152 ndi
= ND_IFINFO(ifp
);
2153 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2154 lck_mtx_lock(&ndi
->lock
);
2155 VERIFY(ndi
->nprefixes
> 0);
2157 lck_mtx_unlock(&ndi
->lock
);
2159 /* This must not be the last reference to the nd_prefix */
2160 if (NDPR_REMREF_LOCKED(pr
) == NULL
) {
2161 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__
, pr
);
2166 * Don't call pfxlist_onlink_check() here because we are
2167 * holding the NDPR lock and this could cause a deadlock when
2168 * there are multiple threads executing pfxlist_onlink_check().
2174 struct nd_prefix
*new,
2175 struct nd_defrouter
*dr
, /* may be NULL */
2179 struct in6_ifaddr
*ia6
= NULL
, *ia6_match
= NULL
;
2181 struct ifnet
*ifp
= new->ndpr_ifp
;
2182 struct nd_prefix
*pr
;
2186 struct in6_addrlifetime lt6_tmp
;
2187 uint64_t timenow
= net_uptime();
2189 /* no need to lock "new" here, as it is local to the caller */
2190 NDPR_LOCK_ASSERT_NOTHELD(new);
2195 * Authenticity for NA consists authentication for
2196 * both IP header and IP datagrams, doesn't it ?
2198 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
2199 auth
= (m
->m_flags
& M_AUTHIPHDR
) && (m
->m_flags
& M_AUTHIPDGM
);
2203 if ((pr
= nd6_prefix_lookup(new, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
2205 * nd6_prefix_lookup() ensures that pr and new have the same
2206 * prefix on a same interface.
2210 * Update prefix information. Note that the on-link (L) bit
2211 * and the autonomous (A) bit should NOT be changed from 1
2214 lck_mtx_lock(nd6_mutex
);
2216 if (new->ndpr_raf_onlink
== 1)
2217 pr
->ndpr_raf_onlink
= 1;
2218 if (new->ndpr_raf_auto
== 1)
2219 pr
->ndpr_raf_auto
= 1;
2220 if (new->ndpr_raf_onlink
) {
2221 pr
->ndpr_vltime
= new->ndpr_vltime
;
2222 pr
->ndpr_pltime
= new->ndpr_pltime
;
2223 (void) in6_init_prefix_ltimes(pr
); /* XXX error case? */
2224 pr
->ndpr_lastupdate
= net_uptime();
2227 NDPR_ADDREF_LOCKED(pr
);
2228 if (new->ndpr_raf_onlink
&&
2229 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
2233 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
2235 "prelist_update: failed to make "
2236 "the prefix %s/%d on-link on %s "
2238 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
2239 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), e
));
2240 /* proceed anyway. XXX: is it correct? */
2245 if (dr
&& pfxrtr_lookup(pr
, dr
) == NULL
) {
2252 lck_mtx_unlock(nd6_mutex
);
2254 struct nd_prefix
*newpr
= NULL
;
2258 if (new->ndpr_vltime
== 0)
2260 if (new->ndpr_raf_onlink
== 0 && new->ndpr_raf_auto
== 0)
2263 bzero(&new->ndpr_addr
, sizeof (struct in6_addr
));
2265 error
= nd6_prelist_add(new, dr
, &newpr
, FALSE
);
2266 if (error
!= 0 || newpr
== NULL
) {
2267 nd6log((LOG_NOTICE
, "prelist_update: "
2268 "nd6_prelist_add failed for %s/%d on %s "
2269 "errno=%d, returnpr=0x%llx\n",
2270 ip6_sprintf(&new->ndpr_prefix
.sin6_addr
),
2271 new->ndpr_plen
, if_name(new->ndpr_ifp
),
2272 error
, (uint64_t)VM_KERNEL_ADDRPERM(newpr
)));
2273 goto end
; /* we should just give up in this case. */
2277 * XXX: from the ND point of view, we can ignore a prefix
2278 * with the on-link bit being zero. However, we need a
2279 * prefix structure for references from autoconfigured
2280 * addresses. Thus, we explicitly make sure that the prefix
2281 * itself expires now.
2284 if (newpr
->ndpr_raf_onlink
== 0) {
2285 newpr
->ndpr_vltime
= 0;
2286 newpr
->ndpr_pltime
= 0;
2287 in6_init_prefix_ltimes(newpr
);
2295 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
2296 * Note that pr must be non NULL at this point.
2299 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
2300 if (!new->ndpr_raf_auto
)
2304 * 5.5.3 (b). the link-local prefix should have been ignored in
2308 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2309 if (new->ndpr_pltime
> new->ndpr_vltime
) {
2310 error
= EINVAL
; /* XXX: won't be used */
2315 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2316 * an address configured by stateless autoconfiguration already in the
2317 * list of addresses associated with the interface, and the Valid
2318 * Lifetime is not 0, form an address. We first check if we have
2319 * a matching prefix.
2321 ifnet_lock_shared(ifp
);
2322 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2323 struct in6_ifaddr
*ifa6
;
2324 u_int32_t remaininglifetime
;
2327 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2331 ifa6
= (struct in6_ifaddr
*)ifa
;
2334 * We only consider autoconfigured addresses as per RFC 4862.
2336 if (!(ifa6
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
2341 * Spec is not clear here, but I believe we should concentrate
2342 * on unicast (i.e. not anycast) addresses.
2343 * XXX: other ia6_flags? detached or duplicated?
2345 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0) {
2350 * Ignore the address if it is not associated with a prefix
2351 * or is associated with a prefix that is different from this
2352 * one. (pr is never NULL here)
2354 if (ifa6
->ia6_ndpr
!= pr
) {
2359 if (ia6_match
== NULL
) { /* remember the first one */
2361 IFA_ADDREF_LOCKED(ifa
); /* for ia6_match */
2365 * An already autoconfigured address matched. Now that we
2366 * are sure there is at least one matched address, we can
2367 * proceed to 5.5.3. (e): update the lifetimes according to the
2368 * "two hours" rule and the privacy extension.
2370 #define TWOHOUR (120*60)
2372 /* retrieve time as uptime (last arg is 0) */
2373 in6ifa_getlifetime(ifa6
, <6_tmp
, 0);
2375 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
)
2376 remaininglifetime
= ND6_INFINITE_LIFETIME
;
2377 else if (timenow
- ifa6
->ia6_updatetime
> lt6_tmp
.ia6t_vltime
) {
2379 * The case of "invalid" address. We should usually
2380 * not see this case.
2382 remaininglifetime
= 0;
2384 remaininglifetime
= lt6_tmp
.ia6t_vltime
-
2385 (timenow
- ifa6
->ia6_updatetime
);
2387 /* when not updating, keep the current stored lifetime. */
2388 lt6_tmp
.ia6t_vltime
= remaininglifetime
;
2390 if (TWOHOUR
< new->ndpr_vltime
||
2391 remaininglifetime
< new->ndpr_vltime
) {
2392 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2393 } else if (remaininglifetime
<= TWOHOUR
) {
2395 lt6_tmp
.ia6t_vltime
= new->ndpr_vltime
;
2399 * new->ndpr_vltime <= TWOHOUR &&
2400 * TWOHOUR < remaininglifetime
2402 lt6_tmp
.ia6t_vltime
= TWOHOUR
;
2405 /* The 2 hour rule is not imposed for preferred lifetime. */
2406 lt6_tmp
.ia6t_pltime
= new->ndpr_pltime
;
2408 /* Special handling for lifetimes of temporary addresses. */
2409 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
2410 u_int32_t maxvltime
, maxpltime
;
2412 /* Constrain lifetimes to system limits. */
2413 if (lt6_tmp
.ia6t_vltime
> ip6_temp_valid_lifetime
)
2414 lt6_tmp
.ia6t_vltime
= ip6_temp_valid_lifetime
;
2415 if (lt6_tmp
.ia6t_pltime
> ip6_temp_preferred_lifetime
)
2416 lt6_tmp
.ia6t_pltime
=
2417 ip6_temp_preferred_lifetime
-
2421 * According to RFC 4941, section 3.3 (1), we only
2422 * update the lifetimes when they are in the maximum
2425 if (ip6_temp_valid_lifetime
>
2426 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2427 ip6_desync_factor
)) {
2428 maxvltime
= ip6_temp_valid_lifetime
-
2429 (timenow
- ifa6
->ia6_createtime
) -
2433 if (ip6_temp_preferred_lifetime
>
2434 (u_int32_t
)((timenow
- ifa6
->ia6_createtime
) +
2435 ip6_desync_factor
)) {
2436 maxpltime
= ip6_temp_preferred_lifetime
-
2437 (timenow
- ifa6
->ia6_createtime
) -
2442 if (lt6_tmp
.ia6t_vltime
== ND6_INFINITE_LIFETIME
||
2443 lt6_tmp
.ia6t_vltime
> maxvltime
)
2444 lt6_tmp
.ia6t_vltime
= maxvltime
;
2446 if (lt6_tmp
.ia6t_pltime
== ND6_INFINITE_LIFETIME
||
2447 lt6_tmp
.ia6t_pltime
> maxpltime
)
2448 lt6_tmp
.ia6t_pltime
= maxpltime
;
2451 in6_init_address_ltimes(pr
, <6_tmp
);
2453 in6ifa_setlifetime(ifa6
, <6_tmp
);
2454 ifa6
->ia6_updatetime
= timenow
;
2457 ifnet_lock_done(ifp
);
2458 if (ia6_match
== NULL
&& new->ndpr_vltime
) {
2460 * 5.5.3 (d) (continued)
2461 * No address matched and the valid lifetime is non-zero.
2462 * Create a new address.
2465 if ((ia6
= in6_pfx_newpersistaddr(new, mcast
, &error
))
2468 * note that we should use pr (not new) for reference.
2470 IFA_LOCK(&ia6
->ia_ifa
);
2473 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
2475 VERIFY(pr
->ndpr_addrcnt
!= 0);
2477 IFA_UNLOCK(&ia6
->ia_ifa
);
2481 * When a new public address is created as described
2482 * in RFC 4862, also create a new temporary address.
2485 * When an interface connects to a new link, a new
2486 * randomized interface identifier should be generated
2487 * immediately together with a new set of temporary
2488 * addresses. Thus, we specifiy 1 as the 2nd arg of
2491 if (ip6_use_tempaddr
) {
2493 if ((e
= in6_tmpifadd(ia6
, 1)) != 0) {
2494 nd6log((LOG_NOTICE
, "prelist_update: "
2495 "failed to create a temporary "
2496 "address, errno=%d\n",
2500 IFA_REMREF(&ia6
->ia_ifa
);
2504 * A newly added address might affect the status
2505 * of other addresses, so we check and update it.
2506 * XXX: what if address duplication happens?
2508 lck_mtx_lock(nd6_mutex
);
2509 pfxlist_onlink_check();
2510 lck_mtx_unlock(nd6_mutex
);
2519 if (ia6_match
!= NULL
)
2520 IFA_REMREF(&ia6_match
->ia_ifa
);
2525 * Neighbor Discover Default Router structure reference counting routines.
2527 static struct nd_defrouter
*
2530 struct nd_defrouter
*dr
;
2532 dr
= (how
== M_WAITOK
) ? zalloc(nddr_zone
) : zalloc_noblock(nddr_zone
);
2534 bzero(dr
, nddr_size
);
2535 lck_mtx_init(&dr
->nddr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2536 dr
->nddr_debug
|= IFD_ALLOC
;
2537 if (nddr_debug
!= 0) {
2538 dr
->nddr_debug
|= IFD_DEBUG
;
2539 dr
->nddr_trace
= nddr_trace
;
2546 nddr_free(struct nd_defrouter
*dr
)
2549 if (dr
->nddr_debug
& IFD_ATTACHED
) {
2550 panic("%s: attached nddr %p is being freed", __func__
, dr
);
2552 } else if (!(dr
->nddr_debug
& IFD_ALLOC
)) {
2553 panic("%s: nddr %p cannot be freed", __func__
, dr
);
2556 dr
->nddr_debug
&= ~IFD_ALLOC
;
2559 lck_mtx_destroy(&dr
->nddr_lock
, ifa_mtx_grp
);
2560 zfree(nddr_zone
, dr
);
2564 nddr_trace(struct nd_defrouter
*dr
, int refhold
)
2566 struct nd_defrouter_dbg
*dr_dbg
= (struct nd_defrouter_dbg
*)dr
;
2571 if (!(dr
->nddr_debug
& IFD_DEBUG
)) {
2572 panic("%s: nddr %p has no debug structure", __func__
, dr
);
2576 cnt
= &dr_dbg
->nddr_refhold_cnt
;
2577 tr
= dr_dbg
->nddr_refhold
;
2579 cnt
= &dr_dbg
->nddr_refrele_cnt
;
2580 tr
= dr_dbg
->nddr_refrele
;
2583 idx
= atomic_add_16_ov(cnt
, 1) % NDDR_TRACE_HIST_SIZE
;
2584 ctrace_record(&tr
[idx
]);
2588 nddr_addref(struct nd_defrouter
*nddr
, int locked
)
2592 NDDR_LOCK_SPIN(nddr
);
2594 NDDR_LOCK_ASSERT_HELD(nddr
);
2596 if (++nddr
->nddr_refcount
== 0) {
2597 panic("%s: nddr %p wraparound refcnt\n", __func__
, nddr
);
2599 } else if (nddr
->nddr_trace
!= NULL
) {
2600 (*nddr
->nddr_trace
)(nddr
, TRUE
);
2607 struct nd_defrouter
*
2608 nddr_remref(struct nd_defrouter
*nddr
, int locked
)
2612 NDDR_LOCK_SPIN(nddr
);
2614 NDDR_LOCK_ASSERT_HELD(nddr
);
2616 if (nddr
->nddr_refcount
== 0) {
2617 panic("%s: nddr %p negative refcnt\n", __func__
, nddr
);
2619 } else if (nddr
->nddr_trace
!= NULL
) {
2620 (*nddr
->nddr_trace
)(nddr
, FALSE
);
2623 if (--nddr
->nddr_refcount
== 0) {
2629 if (!locked
&& nddr
!= NULL
)
2636 nddr_getexpire(struct nd_defrouter
*dr
)
2638 struct timeval caltime
;
2641 if (dr
->expire
!= 0) {
2642 /* account for system time change */
2643 getmicrotime(&caltime
);
2645 dr
->base_calendartime
+=
2646 NET_CALCULATE_CLOCKSKEW(caltime
,
2647 dr
->base_calendartime
, net_uptime(), dr
->base_uptime
);
2649 expiry
= dr
->base_calendartime
+
2650 dr
->expire
- dr
->base_uptime
;
2658 * Neighbor Discover Prefix structure reference counting routines.
2660 static struct nd_prefix
*
2663 struct nd_prefix
*pr
;
2665 pr
= (how
== M_WAITOK
) ? zalloc(ndpr_zone
) : zalloc_noblock(ndpr_zone
);
2667 bzero(pr
, ndpr_size
);
2668 lck_mtx_init(&pr
->ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2669 RB_INIT(&pr
->ndpr_prproxy_sols
);
2670 pr
->ndpr_debug
|= IFD_ALLOC
;
2671 if (ndpr_debug
!= 0) {
2672 pr
->ndpr_debug
|= IFD_DEBUG
;
2673 pr
->ndpr_trace
= ndpr_trace
;
2680 ndpr_free(struct nd_prefix
*pr
)
2683 if (pr
->ndpr_debug
& IFD_ATTACHED
) {
2684 panic("%s: attached ndpr %p is being freed", __func__
, pr
);
2686 } else if (!(pr
->ndpr_debug
& IFD_ALLOC
)) {
2687 panic("%s: ndpr %p cannot be freed", __func__
, pr
);
2689 } else if (pr
->ndpr_rt
!= NULL
) {
2690 panic("%s: ndpr %p route %p not freed", __func__
, pr
,
2693 } else if (pr
->ndpr_prproxy_sols_cnt
!= 0) {
2694 panic("%s: ndpr %p non-zero solicitors count (%d)",
2695 __func__
, pr
, pr
->ndpr_prproxy_sols_cnt
);
2697 } else if (!RB_EMPTY(&pr
->ndpr_prproxy_sols
)) {
2698 panic("%s: ndpr %p non-empty solicitors tree", __func__
, pr
);
2701 pr
->ndpr_debug
&= ~IFD_ALLOC
;
2704 lck_mtx_destroy(&pr
->ndpr_lock
, ifa_mtx_grp
);
2705 zfree(ndpr_zone
, pr
);
2709 ndpr_trace(struct nd_prefix
*pr
, int refhold
)
2711 struct nd_prefix_dbg
*pr_dbg
= (struct nd_prefix_dbg
*)pr
;
2716 if (!(pr
->ndpr_debug
& IFD_DEBUG
)) {
2717 panic("%s: ndpr %p has no debug structure", __func__
, pr
);
2721 cnt
= &pr_dbg
->ndpr_refhold_cnt
;
2722 tr
= pr_dbg
->ndpr_refhold
;
2724 cnt
= &pr_dbg
->ndpr_refrele_cnt
;
2725 tr
= pr_dbg
->ndpr_refrele
;
2728 idx
= atomic_add_16_ov(cnt
, 1) % NDPR_TRACE_HIST_SIZE
;
2729 ctrace_record(&tr
[idx
]);
2733 ndpr_addref(struct nd_prefix
*ndpr
, int locked
)
2736 NDPR_LOCK_SPIN(ndpr
);
2738 NDPR_LOCK_ASSERT_HELD(ndpr
);
2740 if (++ndpr
->ndpr_refcount
== 0) {
2741 panic("%s: ndpr %p wraparound refcnt\n", __func__
, ndpr
);
2743 } else if (ndpr
->ndpr_trace
!= NULL
) {
2744 (*ndpr
->ndpr_trace
)(ndpr
, TRUE
);
2752 ndpr_remref(struct nd_prefix
*ndpr
, int locked
)
2755 NDPR_LOCK_SPIN(ndpr
);
2757 NDPR_LOCK_ASSERT_HELD(ndpr
);
2759 if (ndpr
->ndpr_refcount
== 0) {
2760 panic("%s: ndpr %p negative refcnt\n", __func__
, ndpr
);
2762 } else if (ndpr
->ndpr_trace
!= NULL
) {
2763 (*ndpr
->ndpr_trace
)(ndpr
, FALSE
);
2766 if (--ndpr
->ndpr_refcount
== 0) {
2767 if (ndpr
->ndpr_addrcnt
!= 0) {
2768 panic("%s: freeing ndpr %p with outstanding address "
2769 "reference (%d)", __func__
, ndpr
,
2770 ndpr
->ndpr_addrcnt
);
2778 if (!locked
&& ndpr
!= NULL
)
2785 ndpr_getexpire(struct nd_prefix
*pr
)
2787 struct timeval caltime
;
2790 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_vltime
!= ND6_INFINITE_LIFETIME
) {
2791 /* account for system time change */
2792 getmicrotime(&caltime
);
2794 pr
->ndpr_base_calendartime
+=
2795 NET_CALCULATE_CLOCKSKEW(caltime
,
2796 pr
->ndpr_base_calendartime
, net_uptime(),
2797 pr
->ndpr_base_uptime
);
2799 expiry
= pr
->ndpr_base_calendartime
+
2800 pr
->ndpr_expire
- pr
->ndpr_base_uptime
;
2808 * A supplement function used in the on-link detection below;
2809 * detect if a given prefix has a (probably) reachable advertising router.
2810 * XXX: lengthy function name...
2812 * Callers *must* increase the reference count of nd_prefix.
2814 static struct nd_pfxrouter
*
2815 find_pfxlist_reachable_router(struct nd_prefix
*pr
)
2817 struct nd_pfxrouter
*pfxrtr
;
2819 struct llinfo_nd6
*ln
;
2821 struct in6_addr rtaddr
;
2824 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2825 NDPR_LOCK_ASSERT_HELD(pr
);
2827 genid
= pr
->ndpr_genid
;
2828 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2830 ifp
= pfxrtr
->router
->ifp
;
2831 rtaddr
= pfxrtr
->router
->rtaddr
;
2833 lck_mtx_unlock(nd6_mutex
);
2834 /* Callee returns a locked route upon success */
2835 if ((rt
= nd6_lookup(&rtaddr
, 0, ifp
, 0)) != NULL
) {
2836 RT_LOCK_ASSERT_HELD(rt
);
2837 if ((ln
= rt
->rt_llinfo
) != NULL
&&
2838 ND6_IS_LLINFO_PROBREACH(ln
)) {
2839 RT_REMREF_LOCKED(rt
);
2841 lck_mtx_lock(nd6_mutex
);
2845 RT_REMREF_LOCKED(rt
);
2848 lck_mtx_lock(nd6_mutex
);
2850 if (pr
->ndpr_genid
!= genid
) {
2851 pfxrtr
= LIST_FIRST(&pr
->ndpr_advrtrs
);
2852 genid
= pr
->ndpr_genid
;
2854 pfxrtr
= LIST_NEXT(pfxrtr
, pfr_entry
);
2856 NDPR_LOCK_ASSERT_HELD(pr
);
2863 * Check if each prefix in the prefix list has at least one available router
2864 * that advertised the prefix (a router is "available" if its neighbor cache
2865 * entry is reachable or probably reachable).
2866 * If the check fails, the prefix may be off-link, because, for example,
2867 * we have moved from the network but the lifetime of the prefix has not
2868 * expired yet. So we should not use the prefix if there is another prefix
2869 * that has an available router.
2870 * But, if there is no prefix that has an available router, we still regards
2871 * all the prefixes as on-link. This is because we can't tell if all the
2872 * routers are simply dead or if we really moved from the network and there
2873 * is no router around us.
2876 pfxlist_onlink_check(void)
2878 struct nd_prefix
*pr
, *prclear
;
2879 struct in6_ifaddr
*ifa
;
2880 struct nd_defrouter
*dr
;
2881 struct nd_pfxrouter
*pfxrtr
= NULL
;
2882 int err
, i
, found
= 0;
2883 struct ifaddr
**ifap
= NULL
;
2884 struct nd_prefix
*ndpr
;
2886 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2888 while (nd_prefix_busy
) {
2889 nd_prefix_waiters
++;
2890 msleep(nd_prefix_waitchan
, nd6_mutex
, (PZERO
-1),
2892 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2894 nd_prefix_busy
= TRUE
;
2897 * Check if there is a prefix that has a reachable advertising
2900 pr
= nd_prefix
.lh_first
;
2903 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
) {
2908 NDPR_ADDREF_LOCKED(pr
);
2909 if (pr
->ndpr_raf_onlink
&& find_pfxlist_reachable_router(pr
) &&
2910 (pr
->ndpr_debug
& IFD_ATTACHED
)) {
2911 if (NDPR_REMREF_LOCKED(pr
) == NULL
)
2917 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2921 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
2922 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
2923 * stop us from checking the same prefix twice.
2925 pr
= nd_prefix
.lh_first
;
2927 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
2929 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
2930 NDPR_UNLOCK(prclear
);
2933 * If we have no such prefix, check whether we still have a router
2934 * that does not advertise any prefixes.
2937 for (dr
= TAILQ_FIRST(&nd_defrouter
); dr
;
2938 dr
= TAILQ_NEXT(dr
, dr_entry
)) {
2939 struct nd_prefix
*pr0
;
2941 for (pr0
= nd_prefix
.lh_first
; pr0
;
2942 pr0
= pr0
->ndpr_next
) {
2944 if ((pfxrtr
= pfxrtr_lookup(pr0
, dr
)) != NULL
) {
2954 if (pr
!= NULL
|| (TAILQ_FIRST(&nd_defrouter
) && pfxrtr
== NULL
)) {
2956 * There is at least one prefix that has a reachable router,
2957 * or at least a router which probably does not advertise
2958 * any prefixes. The latter would be the case when we move
2959 * to a new link where we have a router that does not provide
2960 * prefixes and we configure an address by hand.
2961 * Detach prefixes which have no reachable advertising
2962 * router, and attach other prefixes.
2964 pr
= nd_prefix
.lh_first
;
2968 * We aren't interested prefixes already processed,
2969 * nor in prefixes without the L bit
2970 * set nor in static prefixes
2972 if (pr
->ndpr_raf_onlink
== 0 ||
2973 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
2974 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
2979 NDPR_ADDREF_LOCKED(pr
);
2980 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
2981 find_pfxlist_reachable_router(pr
) == NULL
&&
2982 (pr
->ndpr_debug
& IFD_ATTACHED
))
2983 pr
->ndpr_stateflags
|= NDPRF_DETACHED
;
2984 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
2985 find_pfxlist_reachable_router(pr
) != NULL
&&
2986 (pr
->ndpr_debug
& IFD_ATTACHED
))
2987 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
2988 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
2992 * Since find_pfxlist_reachable_router() drops the
2993 * nd6_mutex, we have to start over, but the
2994 * NDPRF_PROCESSED_ONLINK flag will stop us from
2995 * checking the same prefix twice.
2997 pr
= nd_prefix
.lh_first
;
3000 /* there is no prefix that has a reachable router */
3001 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3003 if (pr
->ndpr_raf_onlink
== 0 ||
3004 pr
->ndpr_stateflags
& NDPRF_STATIC
) {
3008 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0)
3009 pr
->ndpr_stateflags
&= ~NDPRF_DETACHED
;
3013 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
3015 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
3016 NDPR_UNLOCK(prclear
);
3019 * Remove each interface route associated with a (just) detached
3020 * prefix, and reinstall the interface route for a (just) attached
3021 * prefix. Note that all attempt of reinstallation does not
3022 * necessarily success, when a same prefix is shared among multiple
3023 * interfaces. Such cases will be handled in nd6_prefix_onlink,
3024 * so we don't have to care about them.
3026 pr
= nd_prefix
.lh_first
;
3031 if (pr
->ndpr_raf_onlink
== 0 ||
3032 pr
->ndpr_stateflags
& NDPRF_STATIC
||
3033 pr
->ndpr_stateflags
& NDPRF_PROCESSED_ONLINK
||
3034 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
3039 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_ONLINK
;
3040 NDPR_ADDREF_LOCKED(pr
);
3041 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0 &&
3042 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3044 lck_mtx_unlock(nd6_mutex
);
3045 if ((e
= nd6_prefix_offlink(pr
)) != 0) {
3047 "pfxlist_onlink_check: failed to "
3048 "make %s/%d offlink, errno=%d\n",
3049 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3052 lck_mtx_lock(nd6_mutex
);
3054 pr
= nd_prefix
.lh_first
;
3057 if ((pr
->ndpr_stateflags
& NDPRF_DETACHED
) == 0 &&
3058 (pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0 &&
3059 pr
->ndpr_raf_onlink
) {
3061 if ((e
= nd6_prefix_onlink(pr
)) != 0) {
3063 "pfxlist_onlink_check: failed to "
3064 "make %s/%d offlink, errno=%d\n",
3065 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3069 pr
= nd_prefix
.lh_first
;
3077 LIST_FOREACH(prclear
, &nd_prefix
, ndpr_entry
) {
3079 prclear
->ndpr_stateflags
&= ~NDPRF_PROCESSED_ONLINK
;
3080 NDPR_UNLOCK(prclear
);
3082 VERIFY(nd_prefix_busy
);
3083 nd_prefix_busy
= FALSE
;
3084 if (nd_prefix_waiters
> 0) {
3085 nd_prefix_waiters
= 0;
3086 wakeup(nd_prefix_waitchan
);
3090 * Changes on the prefix status might affect address status as well.
3091 * Make sure that all addresses derived from an attached prefix are
3092 * attached, and that all addresses derived from a detached prefix are
3093 * detached. Note, however, that a manually configured address should
3094 * always be attached.
3095 * The precise detection logic is same as the one for prefixes.
3097 * ifnet_get_address_list_family_internal() may fail due to memory
3098 * pressure, but we will eventually be called again when we receive
3099 * another NA, RA, or when the link status changes.
3101 err
= ifnet_get_address_list_family_internal(NULL
, &ifap
, AF_INET6
, 0,
3103 if (err
!= 0 || ifap
== NULL
) {
3104 nd6log((LOG_ERR
, "%s: ifnet_get_address_list_family_internal "
3105 "failed", __func__
));
3108 for (i
= 0; ifap
[i
]; i
++) {
3109 ifa
= ifatoia6(ifap
[i
]);
3110 IFA_LOCK(&ifa
->ia_ifa
);
3111 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3112 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3113 IFA_UNLOCK(&ifa
->ia_ifa
);
3116 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3118 * This can happen when we first configure the address
3119 * (i.e. the address exists, but the prefix does not).
3120 * XXX: complicated relationships...
3122 IFA_UNLOCK(&ifa
->ia_ifa
);
3125 IFA_UNLOCK(&ifa
->ia_ifa
);
3128 NDPR_ADDREF_LOCKED(ndpr
);
3129 if (find_pfxlist_reachable_router(ndpr
)) {
3130 if (NDPR_REMREF_LOCKED(ndpr
) == NULL
) {
3142 for (i
= 0; ifap
[i
]; i
++) {
3143 ifa
= ifatoia6(ifap
[i
]);
3144 IFA_LOCK(&ifa
->ia_ifa
);
3145 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 ||
3146 (ifap
[i
]->ifa_debug
& IFD_ATTACHED
) == 0) {
3147 IFA_UNLOCK(&ifa
->ia_ifa
);
3150 if ((ndpr
= ifa
->ia6_ndpr
) == NULL
) {
3151 /* XXX: see above. */
3152 IFA_UNLOCK(&ifa
->ia_ifa
);
3155 IFA_UNLOCK(&ifa
->ia_ifa
);
3157 NDPR_ADDREF_LOCKED(ndpr
);
3158 if (find_pfxlist_reachable_router(ndpr
)) {
3160 IFA_LOCK(&ifa
->ia_ifa
);
3161 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3162 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3163 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
3164 IFA_UNLOCK(&ifa
->ia_ifa
);
3165 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3167 IFA_UNLOCK(&ifa
->ia_ifa
);
3171 IFA_LOCK(&ifa
->ia_ifa
);
3172 ifa
->ia6_flags
|= IN6_IFF_DETACHED
;
3173 IFA_UNLOCK(&ifa
->ia_ifa
);
3178 for (i
= 0; ifap
[i
]; i
++) {
3179 ifa
= ifatoia6(ifap
[i
]);
3180 IFA_LOCK(&ifa
->ia_ifa
);
3181 if ((ifa
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3182 IFA_UNLOCK(&ifa
->ia_ifa
);
3185 if (ifa
->ia6_flags
& IN6_IFF_DETACHED
) {
3186 ifa
->ia6_flags
&= ~IN6_IFF_DETACHED
;
3187 ifa
->ia6_flags
|= IN6_IFF_TENTATIVE
;
3188 IFA_UNLOCK(&ifa
->ia_ifa
);
3189 /* Do we need a delay in this case? */
3190 nd6_dad_start((struct ifaddr
*)ifa
, 0);
3192 IFA_UNLOCK(&ifa
->ia_ifa
);
3196 ifnet_free_address_list(ifap
);
3199 static struct nd_prefix
*
3200 nd6_prefix_equal_lookup(struct nd_prefix
*pr
, boolean_t primary_only
)
3202 struct nd_prefix
*opr
;
3204 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3206 for (opr
= nd_prefix
.lh_first
; opr
; opr
= opr
->ndpr_next
) {
3211 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3215 if (opr
->ndpr_plen
== pr
->ndpr_plen
&&
3216 in6_are_prefix_equal(&pr
->ndpr_prefix
.sin6_addr
,
3217 &opr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_plen
) &&
3219 !(opr
->ndpr_stateflags
& NDPRF_IFSCOPE
))) {
3220 NDPR_ADDREF_LOCKED(opr
);
3230 * Synchronize the interface routes of similar prefixes on different
3231 * interfaces; the one using the default interface would be (re)installed
3232 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3235 nd6_prefix_sync(struct ifnet
*ifp
)
3237 struct nd_prefix
*pr
, *opr
;
3240 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3242 if (!ip6_doscopedroute
|| ifp
== NULL
)
3245 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
3247 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
3251 if (pr
->ndpr_ifp
== ifp
&&
3252 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) &&
3253 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3263 /* Remove conflicting entries */
3264 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3266 lck_mtx_unlock(nd6_mutex
);
3267 err
= nd6_prefix_offlink(opr
);
3268 lck_mtx_lock(nd6_mutex
);
3271 "%s: failed to make %s/%d offlink on %s, "
3272 "errno=%d\n", __func__
,
3273 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3274 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3278 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3279 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3280 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
)));
3283 lck_mtx_unlock(nd6_mutex
);
3284 err
= nd6_prefix_offlink(pr
);
3285 lck_mtx_lock(nd6_mutex
);
3288 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3289 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3290 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3293 /* Add the entries back */
3295 err
= nd6_prefix_onlink_scoped(opr
, opr
->ndpr_ifp
->if_index
);
3298 "%s: failed to make %s/%d scoped onlink on %s, "
3299 "errno=%d\n", __func__
,
3300 ip6_sprintf(&opr
->ndpr_prefix
.sin6_addr
),
3301 opr
->ndpr_plen
, if_name(opr
->ndpr_ifp
), err
));
3305 err
= nd6_prefix_onlink_scoped(pr
, IFSCOPE_NONE
);
3308 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3309 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3310 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
), err
));
3315 "%s: error promoting %s/%d to %s from %s\n",
3316 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3317 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3318 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3321 "%s: %s/%d promoted, previously on %s\n",
3322 if_name(pr
->ndpr_ifp
),
3323 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
), pr
->ndpr_plen
,
3324 (opr
!= NULL
) ? if_name(opr
->ndpr_ifp
) : "NONE"));
3332 nd6_prefix_onlink_common(struct nd_prefix
*pr
, boolean_t force_scoped
,
3333 unsigned int ifscope
)
3336 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3337 struct sockaddr_in6 mask6
, prefix
;
3338 struct nd_prefix
*opr
;
3340 int error
= 0, prproxy
= 0;
3341 struct rtentry
*rt
= NULL
;
3343 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3347 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3349 "%s: %s/%d on %s scoped=%d is already on-link\n",
3350 __func__
, ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3351 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3352 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0);
3359 * Add the interface route associated with the prefix. Before
3360 * installing the route, check if there's the same prefix on another
3361 * interface, and the prefix has already installed the interface route.
3363 opr
= nd6_prefix_equal_lookup(pr
, FALSE
);
3367 if (!ip6_doscopedroute
) {
3368 /* if an interface route already exists, just return */
3371 ifscope
= IFSCOPE_NONE
;
3372 } else if (!force_scoped
) {
3374 * If a primary/non-scoped interface route already exists,
3375 * install the new one as a scoped entry. If the existing
3376 * interface route is scoped, install new as non-scoped.
3378 ifscope
= (opr
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
3379 opr
= nd6_prefix_equal_lookup(pr
, TRUE
);
3382 else if (ifscope
!= IFSCOPE_NONE
)
3383 ifscope
= IFSCOPE_NONE
;
3387 * We prefer link-local addresses as the associated interface address.
3389 /* search for a link-local addr */
3390 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
,
3391 IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3393 struct in6_ifaddr
*ia6
;
3394 ifnet_lock_shared(ifp
);
3395 IFP_TO_IA6(ifp
, ia6
);
3396 ifnet_lock_done(ifp
);
3399 /* should we care about ia6_flags? */
3404 * This can still happen, when, for example, we receive an RA
3405 * containing a prefix with the L bit set and the A bit clear,
3406 * after removing all IPv6 addresses on the receiving
3407 * interface. This should, of course, be rare though.
3410 "nd6_prefix_onlink: failed to find any ifaddr"
3411 " to add route for a prefix(%s/%d) on %s\n",
3412 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3413 pr
->ndpr_plen
, if_name(ifp
)));
3419 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3420 * ifa->ifa_rtrequest = nd6_rtrequest;
3422 bzero(&mask6
, sizeof (mask6
));
3423 mask6
.sin6_len
= sizeof (mask6
);
3424 mask6
.sin6_addr
= pr
->ndpr_mask
;
3425 prefix
= pr
->ndpr_prefix
;
3426 if ((rt
= pr
->ndpr_rt
) != NULL
)
3428 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3432 rtflags
= ifa
->ifa_flags
| RTF_CLONING
| RTF_UP
;
3434 if (nd6_need_cache(ifp
)) {
3435 /* explicitly set in case ifa_flags does not set the flag. */
3436 rtflags
|= RTF_CLONING
;
3439 * explicitly clear the cloning bit in case ifa_flags sets it.
3441 rtflags
&= ~RTF_CLONING
;
3444 lck_mtx_unlock(nd6_mutex
);
3451 error
= rtrequest_scoped(RTM_ADD
, (struct sockaddr
*)&prefix
,
3452 ifa
->ifa_addr
, (struct sockaddr
*)&mask6
, rtflags
, &rt
,
3456 * Serialize the setting of NDPRF_PRPROXY.
3458 lck_mtx_lock(&proxy6_lock
);
3462 nd6_rtmsg(RTM_ADD
, rt
);
3467 nd6log((LOG_ERR
, "nd6_prefix_onlink: failed to add route for a"
3468 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx,"
3469 " scoped=%d, errno = %d\n",
3470 ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3471 pr
->ndpr_plen
, if_name(ifp
),
3472 ip6_sprintf(&((struct sockaddr_in6
*)
3473 (void *)ifa
->ifa_addr
)->sin6_addr
),
3474 ip6_sprintf(&mask6
.sin6_addr
), rtflags
,
3475 (ifscope
!= IFSCOPE_NONE
), error
));
3477 NDPR_LOCK_ASSERT_HELD(pr
);
3479 pr
->ndpr_stateflags
&= ~(NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3482 * TODO: If the prefix route exists, we should really find it and
3483 * refer the prefix to it; otherwise ndpr_rt is NULL.
3485 if (!(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) &&
3486 (rt
!= NULL
|| error
== EEXIST
)) {
3487 struct nd_ifinfo
*ndi
= NULL
;
3489 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3490 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3492 ndi
= ND_IFINFO(ifp
);
3493 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3494 lck_mtx_lock(&ndi
->lock
);
3496 pr
->ndpr_rt
= rt
; /* keep reference from rtrequest */
3497 pr
->ndpr_stateflags
|= NDPRF_ONLINK
;
3498 if (ifscope
!= IFSCOPE_NONE
) {
3499 pr
->ndpr_stateflags
|= NDPRF_IFSCOPE
;
3500 } else if ((rtflags
& RTF_CLONING
) &&
3501 (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) &&
3502 !IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3504 * At present, in order for the prefix to be eligible
3505 * as a proxying/proxied prefix, we require that the
3506 * prefix route entry be marked as a cloning route with
3507 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3508 * true for the interface type, hence the test for
3509 * RTF_CLONING above.
3511 pr
->ndpr_stateflags
|= NDPRF_PRPROXY
;
3514 lck_mtx_unlock(&ndi
->lock
);
3515 } else if (rt
!= NULL
&& pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)
3518 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3519 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3525 * If this is an upstream prefix, find the downstream ones (if any)
3526 * and re-configure their prefix routes accordingly. Otherwise,
3527 * this could be potentially be a downstream prefix, and so find the
3528 * upstream prefix, if any.
3530 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3532 NDPR_REMREF(pr
); /* release reference for this routine */
3533 lck_mtx_unlock(&proxy6_lock
);
3535 lck_mtx_lock(nd6_mutex
);
3541 nd6_prefix_onlink(struct nd_prefix
*pr
)
3543 return (nd6_prefix_onlink_common(pr
, FALSE
, IFSCOPE_NONE
));
3547 nd6_prefix_onlink_scoped(struct nd_prefix
*pr
, unsigned int ifscope
)
3549 return (nd6_prefix_onlink_common(pr
, TRUE
, ifscope
));
3553 nd6_prefix_offlink(struct nd_prefix
*pr
)
3555 int plen
, error
= 0, prproxy
;
3556 struct ifnet
*ifp
= pr
->ndpr_ifp
;
3557 struct nd_prefix
*opr
;
3558 struct sockaddr_in6 sa6
, mask6
, prefix
;
3559 struct rtentry
*rt
= NULL
, *ndpr_rt
= NULL
;
3560 unsigned int ifscope
;
3562 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3566 if ((pr
->ndpr_stateflags
& NDPRF_ONLINK
) == 0) {
3568 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3569 "off-link\n", ip6_sprintf(&pr
->ndpr_prefix
.sin6_addr
),
3570 pr
->ndpr_plen
, if_name(pr
->ndpr_ifp
),
3571 (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ? 1 : 0));
3576 bzero(&sa6
, sizeof (sa6
));
3577 sa6
.sin6_family
= AF_INET6
;
3578 sa6
.sin6_len
= sizeof (sa6
);
3579 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &sa6
.sin6_addr
,
3580 sizeof (struct in6_addr
));
3581 bzero(&mask6
, sizeof (mask6
));
3582 mask6
.sin6_family
= AF_INET6
;
3583 mask6
.sin6_len
= sizeof (sa6
);
3584 bcopy(&pr
->ndpr_mask
, &mask6
.sin6_addr
, sizeof (struct in6_addr
));
3585 prefix
= pr
->ndpr_prefix
;
3586 plen
= pr
->ndpr_plen
;
3587 if ((ndpr_rt
= pr
->ndpr_rt
) != NULL
)
3589 NDPR_ADDREF_LOCKED(pr
); /* keep reference for this routine */
3592 ifscope
= (pr
->ndpr_stateflags
& NDPRF_IFSCOPE
) ?
3593 ifp
->if_index
: IFSCOPE_NONE
;
3595 error
= rtrequest_scoped(RTM_DELETE
, (struct sockaddr
*)&sa6
,
3596 NULL
, (struct sockaddr
*)&mask6
, 0, &rt
, ifscope
);
3599 /* report the route deletion to the routing socket. */
3601 nd6_rtmsg(RTM_DELETE
, rt
);
3606 * The following check takes place only when Scoped Routing
3607 * is not enabled. There might be the same prefix on another
3608 * interface, the prefix which could not be on-link just
3609 * because we have the interface route (see comments in
3610 * nd6_prefix_onlink). If there's one, try to make the prefix
3611 * on-link on the interface.
3613 lck_mtx_lock(nd6_mutex
);
3614 opr
= nd_prefix
.lh_first
;
3616 /* does not apply in the Scoped Routing case */
3617 if (ip6_doscopedroute
)
3621 opr
= opr
->ndpr_next
;
3626 if ((opr
->ndpr_stateflags
& NDPRF_ONLINK
) != 0) {
3628 opr
= opr
->ndpr_next
;
3632 * KAME specific: detached prefixes should not be
3635 if ((opr
->ndpr_stateflags
& NDPRF_DETACHED
) != 0) {
3637 opr
= opr
->ndpr_next
;
3640 if (opr
->ndpr_plen
== plen
&&
3641 in6_are_prefix_equal(&prefix
.sin6_addr
,
3642 &opr
->ndpr_prefix
.sin6_addr
, plen
)) {
3645 NDPR_ADDREF_LOCKED(opr
);
3647 if ((e
= nd6_prefix_onlink(opr
)) != 0) {
3649 "nd6_prefix_offlink: failed to "
3650 "recover a prefix %s/%d from %s "
3651 "to %s (errno = %d)\n",
3653 &opr
->ndpr_prefix
.sin6_addr
),
3654 opr
->ndpr_plen
, if_name(ifp
),
3655 if_name(opr
->ndpr_ifp
), e
));
3658 opr
= nd_prefix
.lh_first
;
3661 opr
= opr
->ndpr_next
;
3664 lck_mtx_unlock(nd6_mutex
);
3667 "nd6_prefix_offlink: failed to delete route: "
3668 "%s/%d on %s, scoped %d, (errno = %d)\n",
3669 ip6_sprintf(&sa6
.sin6_addr
), plen
, if_name(ifp
),
3670 (ifscope
!= IFSCOPE_NONE
), error
));
3673 if (ndpr_rt
!= NULL
)
3676 lck_mtx_lock(&proxy6_lock
);
3679 prproxy
= (pr
->ndpr_stateflags
& NDPRF_PRPROXY
);
3680 VERIFY(!prproxy
|| !(pr
->ndpr_stateflags
& NDPRF_IFSCOPE
));
3681 pr
->ndpr_stateflags
&= ~(NDPRF_ONLINK
| NDPRF_IFSCOPE
| NDPRF_PRPROXY
);
3682 if (pr
->ndpr_prproxy_sols_cnt
> 0) {
3684 nd6_prproxy_sols_reap(pr
);
3685 VERIFY(pr
->ndpr_prproxy_sols_cnt
== 0);
3686 VERIFY(RB_EMPTY(&pr
->ndpr_prproxy_sols
));
3691 * If this was an upstream prefix, find the downstream ones and do
3692 * some cleanups. If this was a downstream prefix, the prefix route
3693 * has been removed from the routing table above, but there may be
3694 * other tasks to perform.
3696 nd6_prproxy_prelist_update(pr
, prproxy
? pr
: NULL
);
3698 NDPR_REMREF(pr
); /* release reference for this routine */
3699 lck_mtx_unlock(&proxy6_lock
);
3704 static struct in6_ifaddr
*
3705 in6_pfx_newpersistaddr(struct nd_prefix
*pr
, int mcast
, int *errorp
)
3707 struct in6_ifaddr
*ia6
= NULL
;
3708 struct ifnet
*ifp
= NULL
;
3709 struct nd_ifinfo
*ndi
= NULL
;
3710 struct in6_addr mask
;
3711 struct in6_aliasreq ifra
;
3712 int error
, ifaupdate
, iidlen
, notcga
;
3715 VERIFY(errorp
!= NULL
);
3723 * Prefix Length check:
3724 * If the sum of the prefix length and interface identifier
3725 * length does not equal 128 bits, the Prefix Information
3726 * option MUST be ignored. The length of the interface
3727 * identifier is defined in a separate link-type specific
3730 iidlen
= in6_if2idlen(ifp
);
3732 error
= EADDRNOTAVAIL
;
3733 /* this should not happen, so we always log it. */
3734 log(LOG_ERR
, "%s: IID length undefined (%s)\n",
3735 __func__
, if_name(ifp
));
3737 } else if (iidlen
!= 64) {
3738 error
= EADDRNOTAVAIL
;
3740 * stateless autoconfiguration not yet well-defined for IID
3741 * lengths other than 64 octets. Just give up for now.
3743 nd6log((LOG_INFO
, "%s: IID length not 64 octets (%s)\n",
3744 __func__
, if_name(ifp
)));
3748 if (iidlen
+ pr
->ndpr_plen
!= 128) {
3749 error
= EADDRNOTAVAIL
;
3751 "%s: invalid prefix length %d for %s, ignored\n",
3752 __func__
, pr
->ndpr_plen
, if_name(ifp
)));
3756 bzero(&ifra
, sizeof (ifra
));
3757 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3758 ifra
.ifra_addr
.sin6_family
= AF_INET6
;
3759 ifra
.ifra_addr
.sin6_len
= sizeof (struct sockaddr_in6
);
3762 bcopy(&pr
->ndpr_prefix
.sin6_addr
, &ifra
.ifra_addr
.sin6_addr
,
3763 sizeof (ifra
.ifra_addr
.sin6_addr
));
3764 in6_len2mask(&mask
, pr
->ndpr_plen
);
3765 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] &= mask
.s6_addr32
[0];
3766 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] &= mask
.s6_addr32
[1];
3767 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] &= mask
.s6_addr32
[2];
3768 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] &= mask
.s6_addr32
[3];
3770 ndi
= ND_IFINFO(ifp
);
3771 VERIFY(ndi
->initialized
);
3772 lck_mtx_lock(&ndi
->lock
);
3774 notcga
= nd6_send_opstate
== ND6_SEND_OPMODE_DISABLED
||
3775 (ndi
->flags
& ND6_IFF_INSECURE
) != 0;
3777 lck_mtx_unlock(&ndi
->lock
);
3781 ia6
= in6ifa_ifpforlinklocal(ifp
, 0);
3783 error
= EADDRNOTAVAIL
;
3784 nd6log((LOG_INFO
, "%s: no link-local address (%s)\n",
3785 __func__
, if_name(ifp
)));
3789 IFA_LOCK(&ia6
->ia_ifa
);
3790 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[0] |=
3791 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[0] & ~mask
.s6_addr32
[0]);
3792 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[1] |=
3793 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[1] & ~mask
.s6_addr32
[1]);
3794 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3795 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[2] & ~mask
.s6_addr32
[2]);
3796 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3797 (ia6
->ia_addr
.sin6_addr
.s6_addr32
[3] & ~mask
.s6_addr32
[3]);
3798 IFA_UNLOCK(&ia6
->ia_ifa
);
3799 IFA_REMREF(&ia6
->ia_ifa
);
3802 in6_cga_node_lock();
3803 error
= in6_cga_generate(NULL
, 0, &ifra
.ifra_addr
.sin6_addr
);
3804 in6_cga_node_unlock();
3806 ifra
.ifra_flags
|= IN6_IFF_SECURED
;
3808 nd6log((LOG_ERR
, "%s: no CGA available (%s)\n",
3809 __func__
, if_name(ifp
)));
3814 VERIFY(ia6
== NULL
);
3816 /* new prefix mask. */
3817 ifra
.ifra_prefixmask
.sin6_len
= sizeof (struct sockaddr_in6
);
3818 ifra
.ifra_prefixmask
.sin6_family
= AF_INET6
;
3819 bcopy(&mask
, &ifra
.ifra_prefixmask
.sin6_addr
,
3820 sizeof (ifra
.ifra_prefixmask
.sin6_addr
));
3823 ifra
.ifra_lifetime
.ia6t_vltime
= pr
->ndpr_vltime
;
3824 ifra
.ifra_lifetime
.ia6t_pltime
= pr
->ndpr_pltime
;
3827 ifra
.ifra_flags
|= IN6_IFF_AUTOCONF
; /* obey autoconf */
3830 * Make sure that we do not have this address already. This should
3831 * usually not happen, but we can still see this case, e.g., if we
3832 * have manually configured the exact address to be configured.
3834 if ((ia6
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
))
3836 error
= EADDRNOTAVAIL
;
3837 IFA_REMREF(&ia6
->ia_ifa
);
3840 /* this should be rare enough to make an explicit log */
3841 log(LOG_INFO
, "%s: %s is already configured!\n",
3842 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
));
3847 * Allocate ifaddr structure, link into chain, etc.
3848 * If we are going to create a new address upon receiving a multicasted
3849 * RA, we need to impose a random delay before starting DAD.
3850 * [RFC 4862, Section 5.4.2]
3852 ifaupdate
= IN6_IFAUPDATE_NOWAIT
;
3854 ifaupdate
|= IN6_IFAUPDATE_DADDELAY
;
3855 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &ia6
);
3858 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
3859 __func__
, ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
),
3860 if_name(ifp
), error
));
3861 error
= EADDRNOTAVAIL
;
3865 VERIFY(ia6
!= NULL
);
3866 in6_post_msg(ifp
, KEV_INET6_NEW_RTADV_ADDR
, ia6
, NULL
);
3877 #define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
3880 in6_tmpifadd(const struct in6_ifaddr
*ia0
, int forcegen
)
3882 struct ifnet
*ifp
= ia0
->ia_ifa
.ifa_ifp
;
3883 struct in6_ifaddr
*ia
, *newia
;
3884 struct in6_aliasreq ifra
;
3885 int i
, error
, ifaupdate
;
3886 int trylimit
= 3; /* XXX: adhoc value */
3887 u_int32_t randid
[2];
3888 time_t vltime0
, pltime0
;
3889 uint64_t timenow
= net_uptime();
3890 struct in6_addr addr
;
3891 struct nd_prefix
*ndpr
;
3893 bzero(&ifra
, sizeof (ifra
));
3894 strlcpy(ifra
.ifra_name
, if_name(ifp
), sizeof (ifra
.ifra_name
));
3895 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3896 ifra
.ifra_addr
= ia0
->ia_addr
;
3897 /* copy prefix mask */
3898 ifra
.ifra_prefixmask
= ia0
->ia_prefixmask
;
3899 /* clear the old IFID */
3900 for (i
= 0; i
< 4; i
++) {
3901 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[i
]
3902 &= ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
3904 addr
= ia0
->ia_addr
.sin6_addr
;
3905 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3908 in6_iid_mktmp(ifp
, (u_int8_t
*)randid
,
3909 (const u_int8_t
*)&addr
.s6_addr
[8], forcegen
);
3911 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] |=
3912 (randid
[0] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[2]));
3913 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] |=
3914 (randid
[1] & ~(ifra
.ifra_prefixmask
.sin6_addr
.s6_addr32
[3]));
3917 * in6_iid_mktmp() quite likely provided a unique interface ID.
3918 * However, we may still have a chance to see collision, because
3919 * there may be a time lag between generation of the ID and generation
3920 * of the address. So, we'll do one more sanity check.
3922 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
.ifra_addr
.sin6_addr
)) != NULL
) {
3923 IFA_REMREF(&ia
->ia_ifa
);
3924 if (trylimit
-- == 0) {
3925 nd6log((LOG_NOTICE
, "in6_tmpifadd: failed to find "
3926 "a unique random IFID\n"));
3934 * The Valid Lifetime is the lower of the Valid Lifetime of the
3935 * public address or TEMP_VALID_LIFETIME.
3936 * The Preferred Lifetime is the lower of the Preferred Lifetime
3937 * of the public address or TEMP_PREFERRED_LIFETIME -
3940 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3941 if (ia0
->ia6_lifetime
.ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3942 vltime0
= IFA6_IS_INVALID(ia0
, timenow
) ? 0 :
3943 (ia0
->ia6_lifetime
.ia6ti_vltime
-
3944 (timenow
- ia0
->ia6_updatetime
));
3945 if (vltime0
> ip6_temp_valid_lifetime
)
3946 vltime0
= ip6_temp_valid_lifetime
;
3948 vltime0
= ip6_temp_valid_lifetime
;
3950 if (ia0
->ia6_lifetime
.ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3951 pltime0
= IFA6_IS_DEPRECATED(ia0
, timenow
) ? 0 :
3952 (ia0
->ia6_lifetime
.ia6ti_pltime
-
3953 (timenow
- ia0
->ia6_updatetime
));
3954 if (pltime0
> ip6_temp_preferred_lifetime
- ip6_desync_factor
)
3955 pltime0
= ip6_temp_preferred_lifetime
-
3958 pltime0
= ip6_temp_preferred_lifetime
- ip6_desync_factor
;
3960 ifra
.ifra_lifetime
.ia6t_vltime
= vltime0
;
3961 ifra
.ifra_lifetime
.ia6t_pltime
= pltime0
;
3962 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3964 * A temporary address is created only if this calculated Preferred
3965 * Lifetime is greater than REGEN_ADVANCE time units.
3967 if (ifra
.ifra_lifetime
.ia6t_pltime
<= ip6_temp_regen_advance
)
3970 /* XXX: scope zone ID? */
3972 ifra
.ifra_flags
|= (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
);
3974 /* allocate ifaddr structure, link into chain, etc. */
3975 ifaupdate
= IN6_IFAUPDATE_NOWAIT
| IN6_IFAUPDATE_DADDELAY
;
3976 error
= in6_update_ifa(ifp
, &ifra
, ifaupdate
, &newia
);
3978 nd6log((LOG_ERR
, "in6_tmpifadd: failed to add address.\n"));
3981 VERIFY(newia
!= NULL
);
3983 IFA_LOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3984 ndpr
= ia0
->ia6_ndpr
;
3987 * We lost the race with another thread that has purged
3988 * ia0 address; in this case, purge the tmp addr as well.
3990 nd6log((LOG_ERR
, "in6_tmpifadd: no public address\n"));
3991 VERIFY(!(ia0
->ia6_flags
& IN6_IFF_AUTOCONF
));
3992 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3993 in6_purgeaddr(&newia
->ia_ifa
);
3994 IFA_REMREF(&newia
->ia_ifa
);
3995 return (EADDRNOTAVAIL
);
3997 NDPR_ADDREF(ndpr
); /* for us */
3998 IFA_UNLOCK(&IA6_NONCONST(ia0
)->ia_ifa
);
3999 IFA_LOCK(&newia
->ia_ifa
);
4000 if (newia
->ia6_ndpr
!= NULL
) {
4001 NDPR_LOCK(newia
->ia6_ndpr
);
4002 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
4003 newia
->ia6_ndpr
->ndpr_addrcnt
--;
4004 NDPR_UNLOCK(newia
->ia6_ndpr
);
4005 NDPR_REMREF(newia
->ia6_ndpr
); /* release addr reference */
4007 newia
->ia6_ndpr
= ndpr
;
4008 NDPR_LOCK(newia
->ia6_ndpr
);
4009 newia
->ia6_ndpr
->ndpr_addrcnt
++;
4010 VERIFY(newia
->ia6_ndpr
->ndpr_addrcnt
!= 0);
4011 NDPR_ADDREF_LOCKED(newia
->ia6_ndpr
); /* for addr reference */
4012 NDPR_UNLOCK(newia
->ia6_ndpr
);
4013 IFA_UNLOCK(&newia
->ia_ifa
);
4015 * A newly added address might affect the status of other addresses.
4016 * XXX: when the temporary address is generated with a new public
4017 * address, the onlink check is redundant. However, it would be safe
4018 * to do the check explicitly everywhere a new address is generated,
4019 * and, in fact, we surely need the check when we create a new
4020 * temporary address due to deprecation of an old temporary address.
4022 lck_mtx_lock(nd6_mutex
);
4023 pfxlist_onlink_check();
4024 lck_mtx_unlock(nd6_mutex
);
4025 IFA_REMREF(&newia
->ia_ifa
);
4027 /* remove our reference */
4035 in6_init_prefix_ltimes(struct nd_prefix
*ndpr
)
4037 struct timeval caltime
;
4038 u_int64_t timenow
= net_uptime();
4040 NDPR_LOCK_ASSERT_HELD(ndpr
);
4042 getmicrotime(&caltime
);
4043 ndpr
->ndpr_base_calendartime
= caltime
.tv_sec
;
4044 ndpr
->ndpr_base_uptime
= timenow
;
4046 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
4047 if (ndpr
->ndpr_pltime
> ndpr
->ndpr_vltime
) {
4048 nd6log((LOG_INFO
, "in6_init_prefix_ltimes: preferred lifetime"
4049 "(%d) is greater than valid lifetime(%d)\n",
4050 (u_int
)ndpr
->ndpr_pltime
, (u_int
)ndpr
->ndpr_vltime
));
4053 if (ndpr
->ndpr_pltime
== ND6_INFINITE_LIFETIME
)
4054 ndpr
->ndpr_preferred
= 0;
4056 ndpr
->ndpr_preferred
= timenow
+ ndpr
->ndpr_pltime
;
4057 if (ndpr
->ndpr_vltime
== ND6_INFINITE_LIFETIME
)
4058 ndpr
->ndpr_expire
= 0;
4060 ndpr
->ndpr_expire
= timenow
+ ndpr
->ndpr_vltime
;
4066 in6_init_address_ltimes(struct nd_prefix
*new, struct in6_addrlifetime
*lt6
)
4069 uint64_t timenow
= net_uptime();
4071 /* Valid lifetime must not be updated unless explicitly specified. */
4072 /* init ia6t_expire */
4073 if (lt6
->ia6t_vltime
== ND6_INFINITE_LIFETIME
) {
4074 lt6
->ia6t_expire
= 0;
4076 lt6
->ia6t_expire
= timenow
;
4077 lt6
->ia6t_expire
+= lt6
->ia6t_vltime
;
4080 /* init ia6t_preferred */
4081 if (lt6
->ia6t_pltime
== ND6_INFINITE_LIFETIME
) {
4082 lt6
->ia6t_preferred
= 0;
4084 lt6
->ia6t_preferred
= timenow
;
4085 lt6
->ia6t_preferred
+= lt6
->ia6t_pltime
;
4090 * Delete all the routing table entries that use the specified gateway.
4091 * XXX: this function causes search through all entries of routing table, so
4092 * it shouldn't be called when acting as a router.
4096 struct in6_addr
*gateway
,
4099 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
4101 /* We'll care only link-local addresses */
4102 if (!IN6_IS_ADDR_LINKLOCAL(gateway
)) {
4105 lck_mtx_lock(rnh_lock
);
4106 /* XXX: hack for KAME's link-local address kludge */
4107 gateway
->s6_addr16
[1] = htons(ifp
->if_index
);
4109 rnh
->rnh_walktree(rnh
, rt6_deleteroute
, (void *)gateway
);
4110 lck_mtx_unlock(rnh_lock
);
4115 struct radix_node
*rn
,
4118 struct rtentry
*rt
= (struct rtentry
*)rn
;
4119 struct in6_addr
*gate
= (struct in6_addr
*)arg
;
4121 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
4124 if (rt
->rt_gateway
== NULL
|| rt
->rt_gateway
->sa_family
!= AF_INET6
) {
4129 if (!IN6_ARE_ADDR_EQUAL(gate
, &SIN6(rt
->rt_gateway
)->sin6_addr
)) {
4134 * Do not delete a static route.
4135 * XXX: this seems to be a bit ad-hoc. Should we consider the
4136 * 'cloned' bit instead?
4138 if ((rt
->rt_flags
& RTF_STATIC
) != 0) {
4143 * We delete only host route. This means, in particular, we don't
4144 * delete default route.
4146 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
4152 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
4153 * rnh_lock here prevents another thread from calling rt_setgate()
4157 return (rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4158 rt_mask(rt
), rt
->rt_flags
, 0));
4162 nd6_setdefaultiface(
4166 ifnet_t def_ifp
= NULL
;
4168 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4170 ifnet_head_lock_shared();
4171 if (ifindex
< 0 || if_index
< ifindex
) {
4175 def_ifp
= ifindex2ifnet
[ifindex
];
4178 lck_mtx_lock(nd6_mutex
);
4179 if (nd6_defifindex
!= ifindex
) {
4180 struct ifnet
*odef_ifp
= nd6_defifp
;
4182 nd6_defifindex
= ifindex
;
4183 if (nd6_defifindex
> 0)
4184 nd6_defifp
= def_ifp
;
4188 if (nd6_defifp
!= NULL
)
4189 nd6log((LOG_INFO
, "%s: is now the default "
4190 "interface (was %s)\n", if_name(nd6_defifp
),
4191 odef_ifp
!= NULL
? if_name(odef_ifp
) : "NONE"));
4193 nd6log((LOG_INFO
, "No default interface set\n"));
4196 * If the Default Router List is empty, install a route
4197 * to the specified interface as default or remove the default
4198 * route when the default interface becomes canceled.
4199 * The check for the queue is actually redundant, but
4200 * we do this here to avoid re-install the default route
4201 * if the list is NOT empty.
4203 if (ip6_doscopedroute
|| TAILQ_FIRST(&nd_defrouter
) == NULL
) {
4204 defrtrlist_sync(nd6_defifp
);
4205 nd6_prefix_sync(nd6_defifp
);
4209 * Our current implementation assumes one-to-one mapping between
4210 * interfaces and links, so it would be natural to use the
4211 * default interface as the default link.
4213 scope6_setdefault(nd6_defifp
);
4215 lck_mtx_unlock(nd6_mutex
);