2 * Copyright (c) 2000-2020 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
61 * BSD/OS version heavily modifies this code, related to llinfo.
62 * Since we don't have BSD/OS version of net/route.c in our hand,
63 * I left the code mostly as it was in 970310. -- itojun
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/sockio.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/errno.h>
76 #include <sys/syslog.h>
77 #include <sys/protosw.h>
79 #include <sys/mcache.h>
81 #include <dev/random/randomdev.h>
83 #include <kern/queue.h>
84 #include <kern/zalloc.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
89 #include <net/if_llreach.h>
90 #include <net/route.h>
92 #include <net/ntstat.h>
93 #include <net/net_osdep.h>
94 #include <net/nwk_wq.h>
96 #include <netinet/in.h>
97 #include <netinet/in_arp.h>
98 #include <netinet/if_ether.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet/icmp6.h>
110 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
111 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
113 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
116 int nd6_prune
= 1; /* walk list every 1 seconds */
117 int nd6_prune_lazy
= 5; /* lazily walk list every 5 seconds */
118 int nd6_delay
= 5; /* delay first probe time 5 second */
119 int nd6_umaxtries
= 3; /* maximum unicast query */
120 int nd6_mmaxtries
= 3; /* maximum multicast query */
121 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
122 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
124 /* preventing too many loops in ND option parsing */
125 int nd6_maxndopt
= 10; /* max # of ND options allowed */
127 int nd6_maxqueuelen
= 1; /* max # of packets cached in unresolved ND entries */
135 int nd6_optimistic_dad
= ND6_OPTIMISTIC_DAD_DEFAULT
;
138 static int nd6_inuse
, nd6_allocated
;
141 * Synchronization notes:
143 * The global list of ND entries are stored in llinfo_nd6; an entry
144 * gets inserted into the list when the route is created and gets
145 * removed from the list when it is deleted; this is done as part
146 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in nd6_rtrequest().
148 * Because rnh_lock and rt_lock for the entry are held during those
149 * operations, the same locks (and thus lock ordering) must be used
150 * elsewhere to access the relevant data structure fields:
152 * ln_next, ln_prev, ln_rt
154 * - Routing lock (rnh_lock)
156 * ln_hold, ln_asked, ln_expire, ln_state, ln_router, ln_flags,
157 * ln_llreach, ln_lastused
159 * - Routing entry lock (rt_lock)
161 * Due to the dependency on rt_lock, llinfo_nd6 has the same lifetime
162 * as the route entry itself. When a route is deleted (RTM_DELETE),
163 * it is simply removed from the global list but the memory is not
164 * freed until the route itself is freed.
166 struct llinfo_nd6 llinfo_nd6
= {
167 .ln_next
= &llinfo_nd6
,
168 .ln_prev
= &llinfo_nd6
,
171 static lck_grp_attr_t
*nd_if_lock_grp_attr
= NULL
;
172 static lck_grp_t
*nd_if_lock_grp
= NULL
;
173 static lck_attr_t
*nd_if_lock_attr
= NULL
;
175 /* Protected by nd6_mutex */
176 struct nd_drhead nd_defrouter_list
;
177 struct nd_prhead nd_prefix
= { .lh_first
= 0 };
178 struct nd_rtihead nd_rti_list
;
180 * nd6_timeout() is scheduled on a demand basis. nd6_timeout_run is used
181 * to indicate whether or not a timeout has been scheduled. The rnh_lock
182 * mutex is used to protect this scheduling; it is a natural choice given
183 * the work done in the timer callback. Unfortunately, there are cases
184 * when nd6_timeout() needs to be scheduled while rnh_lock cannot be easily
185 * held, due to lock ordering. In those cases, we utilize a "demand" counter
186 * nd6_sched_timeout_want which can be atomically incremented without
187 * having to hold rnh_lock. On places where we acquire rnh_lock, such as
188 * nd6_rtrequest(), we check this counter and schedule the timer if it is
189 * non-zero. The increment happens on various places when we allocate
190 * new ND entries, default routers, prefixes and addresses.
192 static int nd6_timeout_run
; /* nd6_timeout is scheduled to run */
193 static void nd6_timeout(void *);
194 int nd6_sched_timeout_want
; /* demand count for timer to be sched */
195 static boolean_t nd6_fast_timer_on
= FALSE
;
197 /* Serialization variables for nd6_service(), protected by rnh_lock */
198 static boolean_t nd6_service_busy
;
199 static void *nd6_service_wc
= &nd6_service_busy
;
200 static int nd6_service_waiters
= 0;
202 int nd6_recalc_reachtm_interval
= ND6_RECALC_REACHTM_INTERVAL
;
203 static struct sockaddr_in6 all1_sa
;
205 static int regen_tmpaddr(struct in6_ifaddr
*);
206 extern lck_mtx_t
*nd6_mutex
;
208 static struct llinfo_nd6
*nd6_llinfo_alloc(zalloc_flags_t
);
209 static void nd6_llinfo_free(void *);
210 static void nd6_llinfo_purge(struct rtentry
*);
211 static void nd6_llinfo_get_ri(struct rtentry
*, struct rt_reach_info
*);
212 static void nd6_llinfo_get_iflri(struct rtentry
*, struct ifnet_llreach_info
*);
213 static void nd6_llinfo_refresh(struct rtentry
*);
214 static uint64_t ln_getexpire(struct llinfo_nd6
*);
216 static void nd6_service(void *);
217 static void nd6_slowtimo(void *);
218 static int nd6_is_new_addr_neighbor(struct sockaddr_in6
*, struct ifnet
*);
219 static int nd6_siocgdrlst(void *, int);
220 static int nd6_siocgprlst(void *, int);
222 static void nd6_router_select_rti_entries(struct ifnet
*);
223 static void nd6_purge_interface_default_routers(struct ifnet
*);
224 static void nd6_purge_interface_rti_entries(struct ifnet
*);
225 static void nd6_purge_interface_prefixes(struct ifnet
*);
226 static void nd6_purge_interface_llinfo(struct ifnet
*);
228 static int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
;
229 static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
;
232 * Insertion and removal from llinfo_nd6 must be done with rnh_lock held.
234 #define LN_DEQUEUE(_ln) do { \
235 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); \
236 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
237 (_ln)->ln_next->ln_prev = (_ln)->ln_prev; \
238 (_ln)->ln_prev->ln_next = (_ln)->ln_next; \
239 (_ln)->ln_prev = (_ln)->ln_next = NULL; \
240 (_ln)->ln_flags &= ~ND6_LNF_IN_USE; \
243 #define LN_INSERTHEAD(_ln) do { \
244 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); \
245 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
246 (_ln)->ln_next = llinfo_nd6.ln_next; \
247 llinfo_nd6.ln_next = (_ln); \
248 (_ln)->ln_prev = &llinfo_nd6; \
249 (_ln)->ln_next->ln_prev = (_ln); \
250 (_ln)->ln_flags |= ND6_LNF_IN_USE; \
253 static ZONE_DECLARE(llinfo_nd6_zone
, "llinfo_nd6",
254 sizeof(struct llinfo_nd6
), ZC_ZFREE_CLEARMEM
);
256 extern int tvtohz(struct timeval
*);
258 static int nd6_init_done
;
260 SYSCTL_DECL(_net_inet6_icmp6
);
262 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
263 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
264 nd6_sysctl_drlist
, "S,in6_defrouter", "");
266 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
267 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
268 nd6_sysctl_prlist
, "S,in6_defrouter", "");
270 SYSCTL_DECL(_net_inet6_ip6
);
272 static int ip6_maxchainsent
= 0;
273 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, maxchainsent
,
274 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_maxchainsent
, 0,
275 "use dlil_output_list");
277 SYSCTL_DECL(_net_inet6_icmp6
);
278 int nd6_process_rti
= ND6_PROCESS_RTI_DEFAULT
;
280 SYSCTL_INT(_net_inet6_icmp6
, OID_AUTO
, nd6_process_rti
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
282 "Enable/disable processing of Route Information Option in the "
283 "IPv6 Router Advertisement.");
290 VERIFY(!nd6_init_done
);
292 all1_sa
.sin6_family
= AF_INET6
;
293 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
294 for (i
= 0; i
< sizeof(all1_sa
.sin6_addr
); i
++) {
295 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
298 /* initialization of the default router list */
299 TAILQ_INIT(&nd_defrouter_list
);
300 TAILQ_INIT(&nd_rti_list
);
302 nd_if_lock_grp_attr
= lck_grp_attr_alloc_init();
303 nd_if_lock_grp
= lck_grp_alloc_init("nd_if_lock", nd_if_lock_grp_attr
);
304 nd_if_lock_attr
= lck_attr_alloc_init();
312 timeout(nd6_slowtimo
, NULL
, ND6_SLOWTIMER_INTERVAL
* hz
);
315 static struct llinfo_nd6
*
316 nd6_llinfo_alloc(zalloc_flags_t how
)
318 return zalloc_flags(llinfo_nd6_zone
, how
| Z_ZERO
);
322 nd6_llinfo_free(void *arg
)
324 struct llinfo_nd6
*ln
= arg
;
326 if (ln
->ln_next
!= NULL
|| ln
->ln_prev
!= NULL
) {
327 panic("%s: trying to free %p when it is in use", __func__
, ln
);
331 /* Just in case there's anything there, free it */
332 if (ln
->ln_hold
!= NULL
) {
333 m_freem_list(ln
->ln_hold
);
337 /* Purge any link-layer info caching */
338 VERIFY(ln
->ln_rt
->rt_llinfo
== ln
);
339 if (ln
->ln_rt
->rt_llinfo_purge
!= NULL
) {
340 ln
->ln_rt
->rt_llinfo_purge(ln
->ln_rt
);
343 zfree(llinfo_nd6_zone
, ln
);
347 nd6_llinfo_purge(struct rtentry
*rt
)
349 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
351 RT_LOCK_ASSERT_HELD(rt
);
352 VERIFY(rt
->rt_llinfo_purge
== nd6_llinfo_purge
&& ln
!= NULL
);
354 if (ln
->ln_llreach
!= NULL
) {
356 ifnet_llreach_free(ln
->ln_llreach
);
357 ln
->ln_llreach
= NULL
;
363 nd6_llinfo_get_ri(struct rtentry
*rt
, struct rt_reach_info
*ri
)
365 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
366 struct if_llreach
*lr
= ln
->ln_llreach
;
369 bzero(ri
, sizeof(*ri
));
370 ri
->ri_rssi
= IFNET_RSSI_UNKNOWN
;
371 ri
->ri_lqm
= IFNET_LQM_THRESH_OFF
;
372 ri
->ri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
375 /* Export to rt_reach_info structure */
377 /* Export ND6 send expiration (calendar) time */
379 ifnet_llreach_up2calexp(lr
, ln
->ln_lastused
);
385 nd6_llinfo_get_iflri(struct rtentry
*rt
, struct ifnet_llreach_info
*iflri
)
387 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
388 struct if_llreach
*lr
= ln
->ln_llreach
;
391 bzero(iflri
, sizeof(*iflri
));
392 iflri
->iflri_rssi
= IFNET_RSSI_UNKNOWN
;
393 iflri
->iflri_lqm
= IFNET_LQM_THRESH_OFF
;
394 iflri
->iflri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
397 /* Export to ifnet_llreach_info structure */
398 ifnet_lr2iflri(lr
, iflri
);
399 /* Export ND6 send expiration (uptime) time */
400 iflri
->iflri_snd_expire
=
401 ifnet_llreach_up2upexp(lr
, ln
->ln_lastused
);
407 nd6_llinfo_refresh(struct rtentry
*rt
)
409 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
410 uint64_t timenow
= net_uptime();
412 * Can't refresh permanent, static or entries that are
413 * not direct host entries
415 if (!ln
|| ln
->ln_expire
== 0 ||
416 (rt
->rt_flags
& RTF_STATIC
) ||
417 !(rt
->rt_flags
& RTF_LLINFO
)) {
421 if ((ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) &&
422 (ln
->ln_state
< ND6_LLINFO_PROBE
)) {
423 if (ln
->ln_expire
> timenow
) {
424 ln_setexpire(ln
, timenow
);
425 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PROBE
);
432 ndcache_state2str(short ndp_state
)
434 const char *ndp_state_str
= "UNKNOWN";
436 case ND6_LLINFO_PURGE
:
437 ndp_state_str
= "ND6_LLINFO_PURGE";
439 case ND6_LLINFO_NOSTATE
:
440 ndp_state_str
= "ND6_LLINFO_NOSTATE";
442 case ND6_LLINFO_INCOMPLETE
:
443 ndp_state_str
= "ND6_LLINFO_INCOMPLETE";
445 case ND6_LLINFO_REACHABLE
:
446 ndp_state_str
= "ND6_LLINFO_REACHABLE";
448 case ND6_LLINFO_STALE
:
449 ndp_state_str
= "ND6_LLINFO_STALE";
451 case ND6_LLINFO_DELAY
:
452 ndp_state_str
= "ND6_LLINFO_DELAY";
454 case ND6_LLINFO_PROBE
:
455 ndp_state_str
= "ND6_LLINFO_PROBE";
458 /* Init'd to UNKNOWN */
461 return ndp_state_str
;
465 ln_setexpire(struct llinfo_nd6
*ln
, uint64_t expiry
)
467 ln
->ln_expire
= expiry
;
471 ln_getexpire(struct llinfo_nd6
*ln
)
473 struct timeval caltime
;
476 if (ln
->ln_expire
!= 0) {
477 struct rtentry
*rt
= ln
->ln_rt
;
480 /* account for system time change */
481 getmicrotime(&caltime
);
483 rt
->base_calendartime
+=
484 NET_CALCULATE_CLOCKSKEW(caltime
,
485 rt
->base_calendartime
, net_uptime(), rt
->base_uptime
);
487 expiry
= rt
->base_calendartime
+
488 ln
->ln_expire
- rt
->base_uptime
;
496 nd6_ifreset(struct ifnet
*ifp
)
498 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
500 VERIFY(ndi
->initialized
);
502 LCK_MTX_ASSERT(&ndi
->lock
, LCK_MTX_ASSERT_OWNED
);
503 ndi
->linkmtu
= ifp
->if_mtu
;
504 ndi
->chlim
= IPV6_DEFHLIM
;
505 ndi
->basereachable
= REACHABLE_TIME
;
506 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
507 ndi
->retrans
= RETRANS_TIMER
;
511 nd6_ifattach(struct ifnet
*ifp
)
513 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
516 if (!ndi
->initialized
) {
517 lck_mtx_init(&ndi
->lock
, nd_if_lock_grp
, nd_if_lock_attr
);
518 ndi
->flags
= ND6_IFF_PERFORMNUD
;
519 ndi
->flags
|= ND6_IFF_DAD
;
520 ndi
->initialized
= TRUE
;
523 lck_mtx_lock(&ndi
->lock
);
525 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
526 ndi
->flags
|= ND6_IFF_IFDISABLED
;
530 lck_mtx_unlock(&ndi
->lock
);
534 "Reinit'd ND information for interface %s\n",
541 * XXX Look more into this. Especially since we recycle ifnets and do delayed
545 nd6_ifdetach(struct nd_ifinfo
*nd
)
547 /* XXX destroy nd's lock? */
553 nd6_setmtu(struct ifnet
*ifp
)
555 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
556 u_int32_t oldmaxmtu
, maxmtu
;
558 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)) {
562 lck_mtx_lock(&ndi
->lock
);
563 oldmaxmtu
= ndi
->maxmtu
;
566 * The ND level maxmtu is somewhat redundant to the interface MTU
567 * and is an implementation artifact of KAME. Instead of hard-
568 * limiting the maxmtu based on the interface type here, we simply
569 * take the if_mtu value since SIOCSIFMTU would have taken care of
570 * the sanity checks related to the maximum MTU allowed for the
571 * interface (a value that is known only by the interface layer),
572 * by sending the request down via ifnet_ioctl(). The use of the
573 * ND level maxmtu and linkmtu are done via IN6_LINKMTU() which
574 * does further checking against if_mtu.
576 maxmtu
= ndi
->maxmtu
= ifp
->if_mtu
;
579 * Decreasing the interface MTU under IPV6 minimum MTU may cause
580 * undesirable situation. We thus notify the operator of the change
581 * explicitly. The check for oldmaxmtu is necessary to restrict the
582 * log to the case of changing the MTU, not initializing it.
584 if (oldmaxmtu
>= IPV6_MMTU
&& ndi
->maxmtu
< IPV6_MMTU
) {
585 log(LOG_NOTICE
, "nd6_setmtu: "
586 "new link MTU on %s (%u) is too small for IPv6\n",
587 if_name(ifp
), (uint32_t)ndi
->maxmtu
);
589 ndi
->linkmtu
= ifp
->if_mtu
;
590 lck_mtx_unlock(&ndi
->lock
);
592 /* also adjust in6_maxmtu if necessary. */
593 if (maxmtu
> in6_maxmtu
) {
599 nd6_option_init(void *opt
, int icmp6len
, union nd_opts
*ndopts
)
601 bzero(ndopts
, sizeof(*ndopts
));
602 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
603 ndopts
->nd_opts_last
=
604 (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
607 ndopts
->nd_opts_done
= 1;
608 ndopts
->nd_opts_search
= NULL
;
613 * Take one ND option.
616 nd6_option(union nd_opts
*ndopts
)
618 struct nd_opt_hdr
*nd_opt
;
622 panic("ndopts == NULL in nd6_option\n");
624 if (!ndopts
->nd_opts_last
) {
625 panic("uninitialized ndopts in nd6_option\n");
627 if (!ndopts
->nd_opts_search
) {
630 if (ndopts
->nd_opts_done
) {
634 nd_opt
= ndopts
->nd_opts_search
;
636 /* make sure nd_opt_len is inside the buffer */
637 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
638 bzero(ndopts
, sizeof(*ndopts
));
642 olen
= nd_opt
->nd_opt_len
<< 3;
645 * Message validation requires that all included
646 * options have a length that is greater than zero.
648 bzero(ndopts
, sizeof(*ndopts
));
652 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
653 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
654 /* option overruns the end of buffer, invalid */
655 bzero(ndopts
, sizeof(*ndopts
));
657 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
658 /* reached the end of options chain */
659 ndopts
->nd_opts_done
= 1;
660 ndopts
->nd_opts_search
= NULL
;
666 * Parse multiple ND options.
667 * This function is much easier to use, for ND routines that do not need
668 * multiple options of the same type.
671 nd6_options(union nd_opts
*ndopts
)
673 struct nd_opt_hdr
*nd_opt
;
676 if (ndopts
== NULL
) {
677 panic("ndopts == NULL in nd6_options");
679 if (ndopts
->nd_opts_last
== NULL
) {
680 panic("uninitialized ndopts in nd6_options");
682 if (ndopts
->nd_opts_search
== NULL
) {
687 nd_opt
= nd6_option(ndopts
);
688 if (nd_opt
== NULL
&& ndopts
->nd_opts_last
== NULL
) {
690 * Message validation requires that all included
691 * options have a length that is greater than zero.
693 icmp6stat
.icp6s_nd_badopt
++;
694 bzero(ndopts
, sizeof(*ndopts
));
698 if (nd_opt
== NULL
) {
702 switch (nd_opt
->nd_opt_type
) {
703 case ND_OPT_SOURCE_LINKADDR
:
704 case ND_OPT_TARGET_LINKADDR
:
706 case ND_OPT_REDIRECTED_HEADER
:
708 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
710 "duplicated ND6 option found (type=%d)\n",
711 nd_opt
->nd_opt_type
);
714 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
718 case ND_OPT_PREFIX_INFORMATION
:
719 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
720 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
723 ndopts
->nd_opts_pi_end
=
724 (struct nd_opt_prefix_info
*)nd_opt
;
728 case ND_OPT_CAPTIVE_PORTAL
:
731 case ND_OPT_ROUTE_INFO
:
732 if (nd6_process_rti
) {
733 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
734 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]
737 ndopts
->nd_opts_rti_end
=
738 (struct nd_opt_route_info
*)nd_opt
;
744 * Unknown options must be silently ignored,
745 * to accomodate future extension to the protocol.
748 "nd6_options: unsupported option %d - "
749 "option ignored\n", nd_opt
->nd_opt_type
);
754 if (i
> nd6_maxndopt
) {
755 icmp6stat
.icp6s_nd_toomanyopt
++;
756 nd6log(info
, "too many loop in nd opt\n");
760 if (ndopts
->nd_opts_done
) {
779 nd6_service_neighbor_cache(struct nd6svc_arg
*ap
, uint64_t timenow
)
781 struct llinfo_nd6
*ln
;
782 struct ifnet
*ifp
= NULL
;
783 boolean_t send_nc_failure_kev
= FALSE
;
784 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
786 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
789 * send_nc_failure_kev gets set when default router's IPv6 address
791 * That can happen either:
792 * 1. When the entry has resolved once but can't be
793 * resolved later and the neighbor cache entry for gateway is deleted
794 * after max probe attempts.
796 * 2. When the entry is in ND6_LLINFO_INCOMPLETE but can not be resolved
797 * after max neighbor address resolution attempts.
799 * Both set send_nc_failure_kev to true. ifp is also set to the previous
800 * neighbor cache entry's route's ifp.
801 * Once we are done sending the notification, set send_nc_failure_kev
802 * to false to stop sending false notifications for non default router
805 * We may to send more information like Gateway's IP that could not be
806 * resolved, however right now we do not install more than one default
807 * route per interface in the routing table.
809 if (send_nc_failure_kev
&& ifp
!= NULL
&&
810 ifp
->if_addrlen
== IF_LLREACH_MAXLEN
) {
811 struct kev_msg ev_msg
;
812 struct kev_nd6_ndfailure nd6_ndfailure
;
813 bzero(&ev_msg
, sizeof(ev_msg
));
814 bzero(&nd6_ndfailure
, sizeof(nd6_ndfailure
));
815 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
816 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
817 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
818 ev_msg
.event_code
= KEV_ND6_NDFAILURE
;
820 nd6_ndfailure
.link_data
.if_family
= ifp
->if_family
;
821 nd6_ndfailure
.link_data
.if_unit
= ifp
->if_unit
;
822 strlcpy(nd6_ndfailure
.link_data
.if_name
,
824 sizeof(nd6_ndfailure
.link_data
.if_name
));
825 ev_msg
.dv
[0].data_ptr
= &nd6_ndfailure
;
826 ev_msg
.dv
[0].data_length
=
827 sizeof(nd6_ndfailure
);
828 dlil_post_complete_msg(NULL
, &ev_msg
);
831 send_nc_failure_kev
= FALSE
;
834 * The global list llinfo_nd6 is modified by nd6_request() and is
835 * therefore protected by rnh_lock. For obvious reasons, we cannot
836 * hold rnh_lock across calls that might lead to code paths which
837 * attempt to acquire rnh_lock, else we deadlock. Hence for such
838 * cases we drop rt_lock and rnh_lock, make the calls, and repeat the
839 * loop. To ensure that we don't process the same entry more than
840 * once in a single timeout, we mark the "already-seen" entries with
841 * ND6_LNF_TIMER_SKIP flag. At the end of the loop, we do a second
842 * pass thru the entries and clear the flag so they can be processed
843 * during the next timeout.
845 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
847 ln
= llinfo_nd6
.ln_next
;
848 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
850 struct sockaddr_in6
*dst
;
851 struct llinfo_nd6
*next
;
852 u_int32_t retrans
, flags
;
853 struct nd_ifinfo
*ndi
= NULL
;
854 boolean_t is_router
= FALSE
;
856 /* ln_next/prev/rt is protected by rnh_lock */
861 /* We've seen this already; skip it */
862 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
) {
869 /* rt->rt_ifp should never be NULL */
870 if ((ifp
= rt
->rt_ifp
) == NULL
) {
871 panic("%s: ln(%p) rt(%p) rt_ifp == NULL", __func__
,
876 /* rt_llinfo must always be equal to ln */
877 if ((struct llinfo_nd6
*)rt
->rt_llinfo
!= ln
) {
878 panic("%s: rt_llinfo(%p) is not equal to ln(%p)",
879 __func__
, rt
->rt_llinfo
, ln
);
883 /* rt_key should never be NULL */
884 dst
= SIN6(rt_key(rt
));
886 panic("%s: rt(%p) key is NULL ln(%p)", __func__
,
891 /* Set the flag in case we jump to "again" */
892 ln
->ln_flags
|= ND6_LNF_TIMER_SKIP
;
894 if (ln
->ln_expire
== 0 || (rt
->rt_flags
& RTF_STATIC
)) {
896 } else if (ap
->draining
&& (rt
->rt_refcnt
== 0)) {
898 * If we are draining, immediately purge non-static
899 * entries without oustanding route refcnt.
901 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
902 ND6_CACHE_STATE_TRANSITION(ln
, (short)ND6_LLINFO_STALE
);
904 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PURGE
);
906 ln_setexpire(ln
, timenow
);
910 * If the entry has not expired, skip it. Take note on the
911 * state, as entries that are in the STALE state are simply
912 * waiting to be garbage collected, in which case we can
913 * relax the callout scheduling (use nd6_prune_lazy).
915 if (ln
->ln_expire
> timenow
) {
916 switch (ln
->ln_state
) {
917 case ND6_LLINFO_STALE
:
929 ndi
= ND_IFINFO(ifp
);
930 VERIFY(ndi
->initialized
);
931 retrans
= ndi
->retrans
;
934 RT_LOCK_ASSERT_HELD(rt
);
935 is_router
= (rt
->rt_flags
& RTF_ROUTER
) ? TRUE
: FALSE
;
937 switch (ln
->ln_state
) {
938 case ND6_LLINFO_INCOMPLETE
:
939 if (ln
->ln_asked
< nd6_mmaxtries
) {
940 struct ifnet
*exclifp
= ln
->ln_exclifp
;
942 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
943 RT_ADDREF_LOCKED(rt
);
945 lck_mtx_unlock(rnh_lock
);
946 if (ip6_forwarding
) {
947 nd6_prproxy_ns_output(ifp
, exclifp
,
948 NULL
, &dst
->sin6_addr
, ln
);
950 nd6_ns_output(ifp
, NULL
,
951 &dst
->sin6_addr
, ln
, NULL
);
955 lck_mtx_lock(rnh_lock
);
957 struct mbuf
*m
= ln
->ln_hold
;
959 send_nc_failure_kev
= is_router
;
961 RT_ADDREF_LOCKED(rt
);
963 lck_mtx_unlock(rnh_lock
);
967 mnext
= m
->m_nextpkt
;
969 m
->m_pkthdr
.rcvif
= ifp
;
970 icmp6_error_flag(m
, ICMP6_DST_UNREACH
,
971 ICMP6_DST_UNREACH_ADDR
, 0, 0);
975 RT_ADDREF_LOCKED(rt
);
977 lck_mtx_unlock(rnh_lock
);
981 * Enqueue work item to invoke callback for
984 route_event_enqueue_nwk_wq_entry(rt
, NULL
,
985 ROUTE_LLENTRY_UNREACH
, NULL
, FALSE
);
988 lck_mtx_lock(rnh_lock
);
990 * nd6_free above would flush out the routing table of
991 * any cloned routes with same next-hop.
992 * Walk the tree anyways as there could be static routes
995 * We also already have a reference to rt that gets freed right
996 * after the block below executes. Don't need an extra reference
1000 struct route_event rt_ev
;
1001 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_LLENTRY_UNREACH
);
1002 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
, (void *)&rt_ev
);
1006 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1009 case ND6_LLINFO_REACHABLE
:
1010 if (ln
->ln_expire
!= 0) {
1011 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
);
1012 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
1015 * Enqueue work item to invoke callback for
1018 route_event_enqueue_nwk_wq_entry(rt
, NULL
,
1019 ROUTE_LLENTRY_STALE
, NULL
, TRUE
);
1021 RT_ADDREF_LOCKED(rt
);
1024 struct route_event rt_ev
;
1025 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_LLENTRY_STALE
);
1026 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
, (void *)&rt_ev
);
1034 case ND6_LLINFO_STALE
:
1035 case ND6_LLINFO_PURGE
:
1036 /* Garbage Collection(RFC 4861 5.3) */
1037 if (ln
->ln_expire
!= 0) {
1038 RT_ADDREF_LOCKED(rt
);
1040 lck_mtx_unlock(rnh_lock
);
1043 lck_mtx_lock(rnh_lock
);
1051 case ND6_LLINFO_DELAY
:
1052 if ((flags
& ND6_IFF_PERFORMNUD
) != 0) {
1055 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PROBE
);
1056 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
1057 RT_ADDREF_LOCKED(rt
);
1059 lck_mtx_unlock(rnh_lock
);
1060 nd6_ns_output(ifp
, &dst
->sin6_addr
,
1061 &dst
->sin6_addr
, ln
, NULL
);
1064 lck_mtx_lock(rnh_lock
);
1067 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
); /* XXX */
1068 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
1073 case ND6_LLINFO_PROBE
:
1074 if (ln
->ln_asked
< nd6_umaxtries
) {
1076 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
1077 RT_ADDREF_LOCKED(rt
);
1079 lck_mtx_unlock(rnh_lock
);
1080 nd6_ns_output(ifp
, &dst
->sin6_addr
,
1081 &dst
->sin6_addr
, ln
, NULL
);
1084 lck_mtx_lock(rnh_lock
);
1086 is_router
= (rt
->rt_flags
& RTF_ROUTER
) ? TRUE
: FALSE
;
1087 send_nc_failure_kev
= is_router
;
1088 RT_ADDREF_LOCKED(rt
);
1090 lck_mtx_unlock(rnh_lock
);
1095 * Enqueue work item to invoke callback for
1098 route_event_enqueue_nwk_wq_entry(rt
, NULL
,
1099 ROUTE_LLENTRY_UNREACH
, NULL
, FALSE
);
1101 lck_mtx_lock(rnh_lock
);
1103 * nd6_free above would flush out the routing table of
1104 * any cloned routes with same next-hop.
1105 * Walk the tree anyways as there could be static routes
1108 * We also already have a reference to rt that gets freed right
1109 * after the block below executes. Don't need an extra reference
1113 struct route_event rt_ev
;
1114 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_LLENTRY_UNREACH
);
1115 (void) rnh
->rnh_walktree(rnh
,
1116 route_event_walktree
, (void *)&rt_ev
);
1120 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1129 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1131 /* Now clear the flag from all entries */
1132 ln
= llinfo_nd6
.ln_next
;
1133 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1134 struct rtentry
*rt
= ln
->ln_rt
;
1135 struct llinfo_nd6
*next
= ln
->ln_next
;
1138 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
) {
1139 ln
->ln_flags
&= ~ND6_LNF_TIMER_SKIP
;
1147 nd6_service_expired_default_router(struct nd6svc_arg
*ap
, uint64_t timenow
)
1149 struct nd_defrouter
*dr
= NULL
;
1150 struct nd_defrouter
*ndr
= NULL
;
1151 struct nd_drhead nd_defrouter_tmp
;
1152 /* expire default router list */
1153 TAILQ_INIT(&nd_defrouter_tmp
);
1155 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1156 lck_mtx_lock(nd6_mutex
);
1158 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_list
, dr_entry
, ndr
) {
1160 if (dr
->expire
!= 0 && dr
->expire
< timenow
) {
1161 VERIFY(dr
->ifp
!= NULL
);
1162 in6_ifstat_inc(dr
->ifp
, ifs6_defrtr_expiry_cnt
);
1163 if ((dr
->stateflags
& NDDRF_INELIGIBLE
) == 0) {
1164 in6_event_enqueue_nwk_wq_entry(IN6_NDP_RTR_EXPIRY
, dr
->ifp
,
1165 &dr
->rtaddr
, dr
->rtlifetime
);
1167 if (dr
->ifp
!= NULL
&&
1168 dr
->ifp
->if_type
== IFT_CELLULAR
) {
1170 * Some buggy cellular gateways may not send
1171 * periodic router advertisements.
1172 * Or they may send it with router lifetime
1173 * value that is less than the configured Max and Min
1174 * Router Advertisement interval.
1175 * To top that an idle device may not wake up
1176 * when periodic RA is received on cellular
1178 * We could send RS on every wake but RFC
1179 * 4861 precludes that.
1180 * The addresses are of infinite lifetimes
1181 * and are tied to the lifetime of the bearer,
1182 * so keeping the addresses and just getting rid of
1183 * the router does not help us anyways.
1184 * If there's network renumbering, a lifetime with
1185 * value 0 would remove the default router.
1186 * Also it will get deleted as part of purge when
1187 * the PDP context is torn down and configured again.
1188 * For that reason, do not expire the default router
1189 * learned on cellular interface. Ever.
1191 dr
->expire
+= dr
->rtlifetime
;
1193 "%s: Refreshing expired default router entry "
1194 "%s for interface %s\n", __func__
,
1195 ip6_sprintf(&dr
->rtaddr
), if_name(dr
->ifp
));
1199 * Remove the entry from default router list
1200 * and add it to the temp list.
1201 * nd_defrouter_tmp will be a local temporary
1202 * list as no one else can get the same
1203 * removed entry once it is removed from default
1205 * Remove the reference after calling defrtrlist_del
1207 TAILQ_REMOVE(&nd_defrouter_list
, dr
, dr_entry
);
1208 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
1211 if (dr
->expire
== 0 || (dr
->stateflags
& NDDRF_STATIC
)) {
1220 * Keep the following separate from the above
1221 * iteration of nd_defrouter because it's not safe
1222 * to call defrtrlist_del while iterating global default
1223 * router list. Global list has to be traversed
1224 * while holding nd6_mutex throughout.
1226 * The following call to defrtrlist_del should be
1227 * safe as we are iterating a local list of
1230 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, ndr
) {
1231 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
1232 defrtrlist_del(dr
, NULL
);
1233 NDDR_REMREF(dr
); /* remove list reference */
1236 /* XXX TBD: Also iterate through RTI router lists */
1238 * Also check if default router selection needs to be triggered
1239 * for default interface, to avoid an issue with co-existence of
1240 * static un-scoped default route configuration and default router
1241 * discovery/selection.
1243 if (trigger_v6_defrtr_select
) {
1244 defrouter_select(NULL
, NULL
);
1245 trigger_v6_defrtr_select
= FALSE
;
1247 lck_mtx_unlock(nd6_mutex
);
1251 nd6_service_expired_route_info(struct nd6svc_arg
*ap
, uint64_t timenow
)
1253 struct nd_route_info
*rti
= NULL
;
1254 struct nd_route_info
*rti_next
= NULL
;
1256 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1257 lck_mtx_lock(nd6_mutex
);
1258 nd6_rti_list_wait(__func__
);
1260 TAILQ_FOREACH_SAFE(rti
, &nd_rti_list
, nd_rti_entry
, rti_next
) {
1261 struct nd_defrouter
*dr
= NULL
;
1262 struct nd_defrouter
*ndr
= NULL
;
1263 struct nd_route_info rti_tmp
= {};
1265 rti_tmp
.nd_rti_prefix
= rti
->nd_rti_prefix
;
1266 rti_tmp
.nd_rti_prefixlen
= rti
->nd_rti_prefixlen
;
1267 TAILQ_INIT(&rti_tmp
.nd_rti_router_list
);
1269 TAILQ_FOREACH_SAFE(dr
, &rti
->nd_rti_router_list
, dr_entry
, ndr
) {
1271 if (dr
->expire
!= 0 && dr
->expire
< timenow
) {
1272 VERIFY(dr
->ifp
!= NULL
);
1273 if (dr
->ifp
!= NULL
&&
1274 dr
->ifp
->if_type
== IFT_CELLULAR
) {
1276 * Don't expire these routes over cellular.
1277 * XXX Should we change this for non default routes?
1279 dr
->expire
+= dr
->rtlifetime
;
1281 "%s: Refreshing expired default router entry "
1282 "%s for interface %s\n", __func__
,
1283 ip6_sprintf(&dr
->rtaddr
), if_name(dr
->ifp
));
1287 * Remove the entry from rti entry's router list
1288 * and add it to the temp list.
1289 * Remove the reference after calling defrtrlist_del
1291 TAILQ_REMOVE(&rti
->nd_rti_router_list
, dr
, dr_entry
);
1292 TAILQ_INSERT_TAIL(&rti_tmp
.nd_rti_router_list
, dr
, dr_entry
);
1295 if (dr
->expire
== 0 || (dr
->stateflags
& NDDRF_STATIC
)) {
1304 * Keep the following separate from the above
1305 * iteration of nd_defrouter because it's not safe
1306 * to call defrtrlist_del while iterating global default
1307 * router list. Global list has to be traversed
1308 * while holding nd6_mutex throughout.
1310 * The following call to defrtrlist_del should be
1311 * safe as we are iterating a local list of
1314 TAILQ_FOREACH_SAFE(dr
, &rti_tmp
.nd_rti_router_list
, dr_entry
, ndr
) {
1315 TAILQ_REMOVE(&rti_tmp
.nd_rti_router_list
, dr
, dr_entry
);
1316 defrtrlist_del(dr
, &rti
->nd_rti_router_list
);
1317 NDDR_REMREF(dr
); /* remove list reference */
1321 * The above may have removed an entry from default router list.
1322 * If it did and the list is now empty, remove the rti as well.
1324 if (TAILQ_EMPTY(&rti
->nd_rti_router_list
)) {
1325 TAILQ_REMOVE(&nd_rti_list
, rti
, nd_rti_entry
);
1330 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1331 nd6_rti_list_signal_done();
1332 lck_mtx_unlock(nd6_mutex
);
1337 * @function nd6_handle_duplicated_ip6_addr
1340 * Handle a duplicated IPv6 secured non-termporary address
1343 * If the collision count hasn't been exceeded, removes the old
1344 * conflicting IPv6 address, increments the collision count,
1345 * and allocates a new address.
1347 * Returns TRUE if the old address was removed, and the locks
1348 * (in6_ifaddr_rwlock, ia6->ia_ifa) were unlocked.
1351 nd6_handle_duplicated_ip6_addr(struct in6_ifaddr
*ia6
)
1353 uint8_t collision_count
;
1355 struct in6_ifaddr
*new_ia6
;
1356 struct nd_prefix
*pr
;
1359 LCK_RW_ASSERT(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1360 IFA_LOCK_ASSERT_HELD(&ia6
->ia_ifa
);
1362 /* don't retry too many times */
1363 collision_count
= ia6
->ia6_cga_collision_count
;
1364 if (collision_count
>= ip6_cga_conflict_retries
) {
1368 /* need the prefix to allocate a new address */
1376 "%s: %s duplicated (collision count %d)\n",
1377 ifp
->if_xname
, ip6_sprintf(&ia6
->ia_addr
.sin6_addr
),
1380 /* remove the old address */
1381 IFA_UNLOCK(&ia6
->ia_ifa
);
1382 lck_rw_done(&in6_ifaddr_rwlock
);
1383 in6_purgeaddr(&ia6
->ia_ifa
);
1385 /* allocate a new address with new collision count */
1387 new_ia6
= in6_pfx_newpersistaddr(pr
, 1, &error
, FALSE
, collision_count
);
1388 if (new_ia6
!= NULL
) {
1390 "%s: %s new (collision count %d)\n",
1391 ifp
->if_xname
, ip6_sprintf(&new_ia6
->ia_addr
.sin6_addr
),
1393 IFA_LOCK(&new_ia6
->ia_ifa
);
1395 new_ia6
->ia6_ndpr
= pr
;
1396 NDPR_ADDREF(pr
); /* for addr reference */
1398 VERIFY(pr
->ndpr_addrcnt
!= 0);
1400 IFA_UNLOCK(&new_ia6
->ia_ifa
);
1401 IFA_REMREF(&new_ia6
->ia_ifa
);
1403 log(LOG_ERR
, "%s: in6_pfx_newpersistaddr failed %d\n",
1407 /* release extra prefix reference */
1413 secured_address_is_duplicated(int flags
)
1415 #define _IN6_IFF_DUPLICATED_AUTOCONF_SECURED \
1416 (IN6_IFF_DUPLICATED | IN6_IFF_AUTOCONF | IN6_IFF_SECURED)
1417 return (flags
& _IN6_IFF_DUPLICATED_AUTOCONF_SECURED
) ==
1418 _IN6_IFF_DUPLICATED_AUTOCONF_SECURED
;
1422 nd6_service_ip6_addr(struct nd6svc_arg
*ap
, uint64_t timenow
)
1424 struct in6_ifaddr
*ia6
= NULL
;
1425 struct in6_ifaddr
*nia6
= NULL
;
1427 * expire interface addresses.
1428 * in the past the loop was inside prefix expiry processing.
1429 * However, from a stricter spec-conformance standpoint, we should
1430 * rather separate address lifetimes and prefix lifetimes.
1434 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1436 TAILQ_FOREACH_SAFE(ia6
, &in6_ifaddrhead
, ia6_link
, nia6
) {
1437 int oldflags
= ia6
->ia6_flags
;
1439 IFA_LOCK(&ia6
->ia_ifa
);
1441 * Extra reference for ourselves; it's no-op if
1442 * we don't have to regenerate temporary address,
1443 * otherwise it protects the address from going
1444 * away since we drop in6_ifaddr_rwlock below.
1446 IFA_ADDREF_LOCKED(&ia6
->ia_ifa
);
1448 /* check for duplicated secured address */
1449 if (secured_address_is_duplicated(ia6
->ia6_flags
) &&
1450 nd6_handle_duplicated_ip6_addr(ia6
)) {
1452 * nd6_handle_duplicated_ip6_addr() unlocked
1453 * (in6_ifaddr_rwlock, ia6->ia_ifa) already.
1454 * Still need to release extra reference on
1455 * ia6->ia_ifa taken above.
1457 IFA_REMREF(&ia6
->ia_ifa
);
1461 /* check address lifetime */
1462 if (IFA6_IS_INVALID(ia6
, timenow
)) {
1464 * If the expiring address is temporary, try
1465 * regenerating a new one. This would be useful when
1466 * we suspended a laptop PC, then turned it on after a
1467 * period that could invalidate all temporary
1468 * addresses. Although we may have to restart the
1469 * loop (see below), it must be after purging the
1470 * address. Otherwise, we'd see an infinite loop of
1473 if (ip6_use_tempaddr
&&
1474 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1476 * NOTE: We have to drop the lock here
1477 * because regen_tmpaddr() eventually calls
1478 * in6_update_ifa(), which must take the lock
1479 * and would otherwise cause a hang. This is
1480 * safe because the goto addrloop leads to a
1481 * re-evaluation of the in6_ifaddrs list
1483 IFA_UNLOCK(&ia6
->ia_ifa
);
1484 lck_rw_done(&in6_ifaddr_rwlock
);
1485 (void) regen_tmpaddr(ia6
);
1487 IFA_UNLOCK(&ia6
->ia_ifa
);
1488 lck_rw_done(&in6_ifaddr_rwlock
);
1492 * Purging the address would have caused
1493 * in6_ifaddr_rwlock to be dropped and reacquired;
1494 * therefore search again from the beginning
1495 * of in6_ifaddrs list.
1497 in6_purgeaddr(&ia6
->ia_ifa
);
1500 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) == 0) {
1501 in6_ifstat_inc(ia6
->ia_ifa
.ifa_ifp
, ifs6_addr_expiry_cnt
);
1502 in6_event_enqueue_nwk_wq_entry(IN6_NDP_ADDR_EXPIRY
,
1503 ia6
->ia_ifa
.ifa_ifp
, &ia6
->ia_addr
.sin6_addr
,
1506 /* Release extra reference taken above */
1507 IFA_REMREF(&ia6
->ia_ifa
);
1511 * The lazy timer runs every nd6_prune_lazy seconds with at
1512 * most "2 * nd6_prune_lazy - 1" leeway. We consider the worst
1513 * case here and make sure we schedule the regular timer if an
1514 * interface address is about to expire.
1516 if (IFA6_IS_INVALID(ia6
, timenow
+ 3 * nd6_prune_lazy
)) {
1521 IFA_LOCK_ASSERT_HELD(&ia6
->ia_ifa
);
1522 if (IFA6_IS_DEPRECATED(ia6
, timenow
)) {
1523 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
1525 if ((oldflags
& IN6_IFF_DEPRECATED
) == 0) {
1527 * Only enqueue the Deprecated event when the address just
1528 * becomes deprecated.
1529 * Keep it limited to the stable address as it is common for
1530 * older temporary addresses to get deprecated while we generate
1533 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) == 0) {
1534 in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DEPRECATED
,
1535 ia6
->ia_ifa
.ifa_ifp
, &ia6
->ia_addr
.sin6_addr
,
1540 * If a temporary address has just become deprecated,
1541 * regenerate a new one if possible.
1543 if (ip6_use_tempaddr
&&
1544 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1545 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
1546 /* see NOTE above */
1547 IFA_UNLOCK(&ia6
->ia_ifa
);
1548 lck_rw_done(&in6_ifaddr_rwlock
);
1549 if (regen_tmpaddr(ia6
) == 0) {
1551 * A new temporary address is
1553 * XXX: this means the address chain
1554 * has changed while we are still in
1555 * the loop. Although the change
1556 * would not cause disaster (because
1557 * it's not a deletion, but an
1558 * addition,) we'd rather restart the
1559 * loop just for safety. Or does this
1560 * significantly reduce performance??
1562 /* Release extra reference */
1563 IFA_REMREF(&ia6
->ia_ifa
);
1566 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1568 IFA_UNLOCK(&ia6
->ia_ifa
);
1572 * A new RA might have made a deprecated address
1575 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
1576 IFA_UNLOCK(&ia6
->ia_ifa
);
1578 LCK_RW_ASSERT(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1579 /* Release extra reference taken above */
1580 IFA_REMREF(&ia6
->ia_ifa
);
1582 lck_rw_done(&in6_ifaddr_rwlock
);
1586 nd6_service_expired_prefix(struct nd6svc_arg
*ap
, uint64_t timenow
)
1588 struct nd_prefix
*pr
= NULL
;
1590 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1591 lck_mtx_lock(nd6_mutex
);
1592 /* expire prefix list */
1593 pr
= nd_prefix
.lh_first
;
1594 while (pr
!= NULL
) {
1597 * check prefix lifetime.
1598 * since pltime is just for autoconf, pltime processing for
1599 * prefix is not necessary.
1602 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_SERVICE
||
1603 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
1604 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1609 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_expire
< timenow
) {
1611 * address expiration and prefix expiration are
1612 * separate. NEVER perform in6_purgeaddr here.
1614 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1619 in6_ifstat_inc(pr
->ndpr_ifp
, ifs6_pfx_expiry_cnt
);
1620 in6_event_enqueue_nwk_wq_entry(IN6_NDP_PFX_EXPIRY
,
1621 pr
->ndpr_ifp
, &pr
->ndpr_prefix
.sin6_addr
,
1624 pfxlist_onlink_check();
1625 pr
= nd_prefix
.lh_first
;
1628 if (pr
->ndpr_expire
== 0 ||
1629 (pr
->ndpr_stateflags
& NDPRF_STATIC
)) {
1634 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1639 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1641 pr
->ndpr_stateflags
&= ~NDPRF_PROCESSED_SERVICE
;
1644 lck_mtx_unlock(nd6_mutex
);
1649 * ND6 service routine to expire default route list and prefix list
1652 nd6_service(void *arg
)
1654 struct nd6svc_arg
*ap
= arg
;
1657 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1659 * Since we may drop rnh_lock and nd6_mutex below, we want
1660 * to run this entire operation single threaded.
1662 while (nd6_service_busy
) {
1663 nd6log2(debug
, "%s: %s is blocked by %d waiters\n",
1664 __func__
, ap
->draining
? "drainer" : "timer",
1665 nd6_service_waiters
);
1666 nd6_service_waiters
++;
1667 (void) msleep(nd6_service_wc
, rnh_lock
, (PZERO
- 1),
1669 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1672 /* We are busy now; tell everyone else to go away */
1673 nd6_service_busy
= TRUE
;
1674 net_update_uptime();
1675 timenow
= net_uptime();
1677 /* Iterate and service neighbor cache entries */
1678 nd6_service_neighbor_cache(ap
, timenow
);
1681 * There is lock ordering requirement and rnh_lock
1682 * has to be released before acquiring nd6_mutex.
1684 lck_mtx_unlock(rnh_lock
);
1686 /* Iterate and service expired default router */
1687 nd6_service_expired_default_router(ap
, timenow
);
1688 /* Iterate and service expired route information entries */
1689 nd6_service_expired_route_info(ap
, timenow
);
1691 /* Iterate and service expired/duplicated IPv6 address */
1692 nd6_service_ip6_addr(ap
, timenow
);
1694 /* Iterate and service expired IPv6 prefixes */
1695 nd6_service_expired_prefix(ap
, timenow
);
1697 lck_mtx_lock(rnh_lock
);
1698 /* We're done; let others enter */
1699 nd6_service_busy
= FALSE
;
1700 if (nd6_service_waiters
> 0) {
1701 nd6_service_waiters
= 0;
1702 wakeup(nd6_service_wc
);
1706 static int nd6_need_draining
= 0;
1709 nd6_drain(void *arg
)
1712 nd6log2(debug
, "%s: draining ND6 entries\n", __func__
);
1714 lck_mtx_lock(rnh_lock
);
1715 nd6_need_draining
= 1;
1716 nd6_sched_timeout(NULL
, NULL
);
1717 lck_mtx_unlock(rnh_lock
);
1721 * We use the ``arg'' variable to decide whether or not the timer we're
1722 * running is the fast timer. We do this to reset the nd6_fast_timer_on
1723 * variable so that later we don't end up ignoring a ``fast timer''
1724 * request if the 5 second timer is running (see nd6_sched_timeout).
1727 nd6_timeout(void *arg
)
1729 struct nd6svc_arg sarg
;
1732 lck_mtx_lock(rnh_lock
);
1733 bzero(&sarg
, sizeof(sarg
));
1734 if (nd6_need_draining
!= 0) {
1735 nd6_need_draining
= 0;
1739 nd6log2(debug
, "%s: found %u, aging_lazy %u, aging %u, "
1740 "sticky %u, killed %u\n", __func__
, sarg
.found
, sarg
.aging_lazy
,
1741 sarg
.aging
, sarg
.sticky
, sarg
.killed
);
1742 /* re-arm the timer if there's work to do */
1744 VERIFY(nd6_timeout_run
>= 0 && nd6_timeout_run
< 2);
1745 if (arg
== &nd6_fast_timer_on
) {
1746 nd6_fast_timer_on
= FALSE
;
1748 if (sarg
.aging_lazy
> 0 || sarg
.aging
> 0 || nd6_sched_timeout_want
) {
1749 struct timeval atv
, ltv
, *leeway
;
1750 int lazy
= nd6_prune_lazy
;
1752 if (sarg
.aging
> 0 || lazy
< 1) {
1754 atv
.tv_sec
= nd6_prune
;
1759 atv
.tv_sec
= MAX(nd6_prune
, lazy
);
1761 read_frandom(&buf
, sizeof(buf
));
1762 ltv
.tv_sec
= MAX(buf
% lazy
, 1) * 2;
1765 nd6_sched_timeout(&atv
, leeway
);
1766 } else if (nd6_debug
) {
1767 nd6log2(debug
, "%s: not rescheduling timer\n", __func__
);
1769 lck_mtx_unlock(rnh_lock
);
1773 nd6_sched_timeout(struct timeval
*atv
, struct timeval
*ltv
)
1777 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1780 tv
.tv_sec
= MAX(nd6_prune
, 1);
1782 ltv
= NULL
; /* ignore leeway */
1784 /* see comments on top of this file */
1785 if (nd6_timeout_run
== 0) {
1787 nd6log2(debug
, "%s: timer scheduled in "
1788 "T+%llus.%lluu (demand %d)\n", __func__
,
1789 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1790 nd6_sched_timeout_want
);
1791 nd6_fast_timer_on
= TRUE
;
1792 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1794 nd6log2(debug
, "%s: timer scheduled in "
1795 "T+%llus.%lluu with %llus.%lluu leeway "
1796 "(demand %d)\n", __func__
, (uint64_t)atv
->tv_sec
,
1797 (uint64_t)atv
->tv_usec
, (uint64_t)ltv
->tv_sec
,
1798 (uint64_t)ltv
->tv_usec
, nd6_sched_timeout_want
);
1799 nd6_fast_timer_on
= FALSE
;
1800 timeout_with_leeway(nd6_timeout
, NULL
,
1801 tvtohz(atv
), tvtohz(ltv
));
1804 nd6_sched_timeout_want
= 0;
1805 } else if (nd6_timeout_run
== 1 && ltv
== NULL
&&
1806 nd6_fast_timer_on
== FALSE
) {
1807 nd6log2(debug
, "%s: fast timer scheduled in "
1808 "T+%llus.%lluu (demand %d)\n", __func__
,
1809 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1810 nd6_sched_timeout_want
);
1811 nd6_fast_timer_on
= TRUE
;
1812 nd6_sched_timeout_want
= 0;
1814 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1817 nd6log2(debug
, "%s: not scheduling timer: "
1818 "timers %d, fast_timer %d, T+%llus.%lluu\n",
1819 __func__
, nd6_timeout_run
, nd6_fast_timer_on
,
1820 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
);
1822 nd6log2(debug
, "%s: not scheduling timer: "
1823 "timers %d, fast_timer %d, T+%llus.%lluu "
1824 "with %llus.%lluu leeway\n", __func__
,
1825 nd6_timeout_run
, nd6_fast_timer_on
,
1826 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1827 (uint64_t)ltv
->tv_sec
, (uint64_t)ltv
->tv_usec
);
1833 * ND6 router advertisement kernel notification
1836 nd6_post_msg(u_int32_t code
, struct nd_prefix_list
*prefix_list
,
1837 u_int32_t list_length
, u_int32_t mtu
)
1839 struct kev_msg ev_msg
;
1840 struct kev_nd6_ra_data nd6_ra_msg_data
;
1841 struct nd_prefix_list
*itr
= prefix_list
;
1843 bzero(&ev_msg
, sizeof(struct kev_msg
));
1844 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1845 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1846 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
1847 ev_msg
.event_code
= code
;
1849 bzero(&nd6_ra_msg_data
, sizeof(nd6_ra_msg_data
));
1851 if (mtu
> 0 && mtu
>= IPV6_MMTU
) {
1852 nd6_ra_msg_data
.mtu
= mtu
;
1853 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_MTU
;
1856 if (list_length
> 0 && prefix_list
!= NULL
) {
1857 nd6_ra_msg_data
.list_length
= list_length
;
1858 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_PREFIX
;
1861 while (itr
!= NULL
&& nd6_ra_msg_data
.list_index
< list_length
) {
1862 bcopy(&itr
->pr
.ndpr_prefix
, &nd6_ra_msg_data
.prefix
.prefix
,
1863 sizeof(nd6_ra_msg_data
.prefix
.prefix
));
1864 nd6_ra_msg_data
.prefix
.raflags
= itr
->pr
.ndpr_raf
;
1865 nd6_ra_msg_data
.prefix
.prefixlen
= itr
->pr
.ndpr_plen
;
1866 nd6_ra_msg_data
.prefix
.origin
= PR_ORIG_RA
;
1867 nd6_ra_msg_data
.prefix
.vltime
= itr
->pr
.ndpr_vltime
;
1868 nd6_ra_msg_data
.prefix
.pltime
= itr
->pr
.ndpr_pltime
;
1869 nd6_ra_msg_data
.prefix
.expire
= ndpr_getexpire(&itr
->pr
);
1870 nd6_ra_msg_data
.prefix
.flags
= itr
->pr
.ndpr_stateflags
;
1871 nd6_ra_msg_data
.prefix
.refcnt
= itr
->pr
.ndpr_addrcnt
;
1872 nd6_ra_msg_data
.prefix
.if_index
= itr
->pr
.ndpr_ifp
->if_index
;
1874 /* send the message up */
1875 ev_msg
.dv
[0].data_ptr
= &nd6_ra_msg_data
;
1876 ev_msg
.dv
[0].data_length
= sizeof(nd6_ra_msg_data
);
1877 ev_msg
.dv
[1].data_length
= 0;
1878 dlil_post_complete_msg(NULL
, &ev_msg
);
1880 /* clean up for the next prefix */
1881 bzero(&nd6_ra_msg_data
.prefix
, sizeof(nd6_ra_msg_data
.prefix
));
1883 nd6_ra_msg_data
.list_index
++;
1888 * Regenerate deprecated/invalidated temporary address
1891 regen_tmpaddr(struct in6_ifaddr
*ia6
)
1895 struct in6_ifaddr
*public_ifa6
= NULL
;
1896 uint64_t timenow
= net_uptime();
1898 ifp
= ia6
->ia_ifa
.ifa_ifp
;
1899 ifnet_lock_shared(ifp
);
1900 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
1901 struct in6_ifaddr
*it6
;
1904 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1908 it6
= (struct in6_ifaddr
*)ifa
;
1910 /* ignore no autoconf addresses. */
1911 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
1915 /* ignore autoconf addresses with different prefixes. */
1916 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
) {
1921 * Now we are looking at an autoconf address with the same
1922 * prefix as ours. If the address is temporary and is still
1923 * preferred, do not create another one. It would be rare, but
1924 * could happen, for example, when we resume a laptop PC after
1927 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1928 !IFA6_IS_DEPRECATED(it6
, timenow
)) {
1930 if (public_ifa6
!= NULL
) {
1931 IFA_REMREF(&public_ifa6
->ia_ifa
);
1938 * This is a public autoconf address that has the same prefix
1939 * as ours. If it is preferred, keep it. We can't break the
1940 * loop here, because there may be a still-preferred temporary
1941 * address with the prefix.
1943 if (!IFA6_IS_DEPRECATED(it6
, timenow
)) {
1944 IFA_ADDREF_LOCKED(ifa
); /* for public_ifa6 */
1946 if (public_ifa6
!= NULL
) {
1947 IFA_REMREF(&public_ifa6
->ia_ifa
);
1954 ifnet_lock_done(ifp
);
1956 if (public_ifa6
!= NULL
) {
1959 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
1960 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
1961 " tmp addr,errno=%d\n", e
);
1962 IFA_REMREF(&public_ifa6
->ia_ifa
);
1965 IFA_REMREF(&public_ifa6
->ia_ifa
);
1973 nd6_purge_interface_default_routers(struct ifnet
*ifp
)
1975 struct nd_defrouter
*dr
= NULL
;
1976 struct nd_defrouter
*ndr
= NULL
;
1977 struct nd_drhead nd_defrouter_tmp
= {};
1979 TAILQ_INIT(&nd_defrouter_tmp
);
1981 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1983 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_list
, dr_entry
, ndr
) {
1984 if (dr
->ifp
!= ifp
) {
1988 * Remove the entry from default router list
1989 * and add it to the temp list.
1990 * nd_defrouter_tmp will be a local temporary
1991 * list as no one else can get the same
1992 * removed entry once it is removed from default
1994 * Remove the reference after calling defrtrlist_del.
1996 * The uninstalled entries have to be iterated first
1997 * when we call defrtrlist_del.
1998 * This is to ensure that we don't end up calling
1999 * default router selection when there are other
2000 * uninstalled candidate default routers on
2002 * If we don't respect that order, we may end
2003 * up missing out on some entries.
2005 * For that reason, installed ones must be inserted
2006 * at the tail and uninstalled ones at the head
2008 TAILQ_REMOVE(&nd_defrouter_list
, dr
, dr_entry
);
2010 if (dr
->stateflags
& NDDRF_INSTALLED
) {
2011 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
2013 TAILQ_INSERT_HEAD(&nd_defrouter_tmp
, dr
, dr_entry
);
2018 * The following call to defrtrlist_del should be
2019 * safe as we are iterating a local list of
2022 * We don't really need nd6_mutex here but keeping
2023 * it as it is to avoid changing assertios held in
2024 * the functions in the call-path.
2026 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, ndr
) {
2027 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
2028 defrtrlist_del(dr
, NULL
);
2029 NDDR_REMREF(dr
); /* remove list reference */
2034 nd6_purge_interface_prefixes(struct ifnet
*ifp
)
2036 boolean_t removed
= FALSE
;
2037 struct nd_prefix
*pr
= NULL
;
2038 struct nd_prefix
*npr
= NULL
;
2040 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2042 /* Nuke prefix list entries toward ifp */
2043 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
2045 npr
= pr
->ndpr_next
;
2046 if (pr
->ndpr_ifp
== ifp
&&
2047 !(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)) {
2049 * Because if_detach() does *not* release prefixes
2050 * while purging addresses the reference count will
2051 * still be above zero. We therefore reset it to
2052 * make sure that the prefix really gets purged.
2054 pr
->ndpr_addrcnt
= 0;
2057 * Previously, pr->ndpr_addr is removed as well,
2058 * but I strongly believe we don't have to do it.
2059 * nd6_purge() is only called from in6_ifdetach(),
2060 * which removes all the associated interface addresses
2062 * (jinmei@kame.net 20010129)
2069 npr
= nd_prefix
.lh_first
;
2075 pfxlist_onlink_check();
2080 nd6_router_select_rti_entries(struct ifnet
*ifp
)
2082 struct nd_route_info
*rti
= NULL
;
2083 struct nd_route_info
*rti_next
= NULL
;
2085 nd6_rti_list_wait(__func__
);
2087 TAILQ_FOREACH_SAFE(rti
, &nd_rti_list
, nd_rti_entry
, rti_next
) {
2088 defrouter_select(ifp
, &rti
->nd_rti_router_list
);
2091 nd6_rti_list_signal_done();
2095 nd6_purge_interface_rti_entries(struct ifnet
*ifp
)
2097 struct nd_route_info
*rti
= NULL
;
2098 struct nd_route_info
*rti_next
= NULL
;
2100 nd6_rti_list_wait(__func__
);
2102 TAILQ_FOREACH_SAFE(rti
, &nd_rti_list
, nd_rti_entry
, rti_next
) {
2103 struct nd_route_info rti_tmp
= {};
2104 struct nd_defrouter
*dr
= NULL
;
2105 struct nd_defrouter
*ndr
= NULL
;
2107 rti_tmp
.nd_rti_prefix
= rti
->nd_rti_prefix
;
2108 rti_tmp
.nd_rti_prefixlen
= rti
->nd_rti_prefixlen
;
2109 TAILQ_INIT(&rti_tmp
.nd_rti_router_list
);
2111 TAILQ_FOREACH_SAFE(dr
, &rti
->nd_rti_router_list
, dr_entry
, ndr
) {
2113 * If ifp is provided, skip the entries that don't match.
2114 * Else it is treated as a purge.
2116 if (ifp
!= NULL
&& dr
->ifp
!= ifp
) {
2121 * Remove the entry from rti's router list
2122 * and add it to the temp list.
2123 * Remove the reference after calling defrtrlist_del.
2125 * The uninstalled entries have to be iterated first
2126 * when we call defrtrlist_del.
2127 * This is to ensure that we don't end up calling
2128 * router selection when there are other
2129 * uninstalled candidate default routers on
2131 * If we don't respect that order, we may end
2132 * up missing out on some entries.
2134 * For that reason, installed ones must be inserted
2135 * at the tail and uninstalled ones at the head
2138 TAILQ_REMOVE(&rti
->nd_rti_router_list
, dr
, dr_entry
);
2139 if (dr
->stateflags
& NDDRF_INSTALLED
) {
2140 TAILQ_INSERT_TAIL(&rti_tmp
.nd_rti_router_list
, dr
, dr_entry
);
2142 TAILQ_INSERT_HEAD(&rti_tmp
.nd_rti_router_list
, dr
, dr_entry
);
2147 * The following call to defrtrlist_del should be
2148 * safe as we are iterating a local list of
2151 * We don't really need nd6_mutex here but keeping
2152 * it as it is to avoid changing assertios held in
2153 * the functions in the call-path.
2155 TAILQ_FOREACH_SAFE(dr
, &rti_tmp
.nd_rti_router_list
, dr_entry
, ndr
) {
2156 TAILQ_REMOVE(&rti_tmp
.nd_rti_router_list
, dr
, dr_entry
);
2157 defrtrlist_del(dr
, &rti
->nd_rti_router_list
);
2158 NDDR_REMREF(dr
); /* remove list reference */
2161 * The above may have removed an entry from default router list.
2162 * If it did and the list is now empty, remove the rti as well.
2164 if (TAILQ_EMPTY(&rti
->nd_rti_router_list
)) {
2165 TAILQ_REMOVE(&nd_rti_list
, rti
, nd_rti_entry
);
2170 nd6_rti_list_signal_done();
2174 nd6_purge_interface_llinfo(struct ifnet
*ifp
)
2176 struct llinfo_nd6
*ln
= NULL
;
2177 /* Note that rt->rt_ifp may not be the same as ifp,
2178 * due to KAME goto ours hack. See RTM_RESOLVE case in
2179 * nd6_rtrequest(), and ip6_input().
2182 lck_mtx_lock(rnh_lock
);
2183 ln
= llinfo_nd6
.ln_next
;
2184 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
2186 struct llinfo_nd6
*nln
;
2191 if (rt
->rt_gateway
!= NULL
&&
2192 rt
->rt_gateway
->sa_family
== AF_LINK
&&
2193 SDL(rt
->rt_gateway
)->sdl_index
== ifp
->if_index
) {
2194 RT_ADDREF_LOCKED(rt
);
2196 lck_mtx_unlock(rnh_lock
);
2198 * See comments on nd6_service() for reasons why
2199 * this loop is repeated; we bite the costs of
2200 * going thru the same llinfo_nd6 more than once
2201 * here, since this purge happens during detach,
2202 * and that unlike the timer case, it's possible
2203 * there's more than one purges happening at the
2204 * same time (thus a flag wouldn't buy anything).
2208 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2215 lck_mtx_unlock(rnh_lock
);
2219 * Nuke neighbor cache/prefix/default router management table, right before
2223 nd6_purge(struct ifnet
*ifp
)
2225 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2226 lck_mtx_lock(nd6_mutex
);
2228 /* Nuke default router list entries toward ifp */
2229 nd6_purge_interface_default_routers(ifp
);
2231 /* Nuke prefix list entries toward ifp */
2232 nd6_purge_interface_prefixes(ifp
);
2234 /* Nuke route info option entries toward ifp */
2235 nd6_purge_interface_rti_entries(ifp
);
2237 lck_mtx_unlock(nd6_mutex
);
2239 /* cancel default outgoing interface setting */
2240 if (nd6_defifindex
== ifp
->if_index
) {
2241 nd6_setdefaultiface(0);
2245 * Perform default router selection even when we are a router,
2246 * if Scoped Routing is enabled.
2247 * XXX ?Should really not be needed since when defrouter_select
2248 * was changed to work on interface.
2250 lck_mtx_lock(nd6_mutex
);
2251 /* refresh default router list */
2252 defrouter_select(ifp
, NULL
);
2253 lck_mtx_unlock(nd6_mutex
);
2255 /* Nuke neighbor cache entries for the ifp. */
2256 nd6_purge_interface_llinfo(ifp
);
2260 * Upon success, the returned route will be locked and the caller is
2261 * responsible for releasing the reference and doing RT_UNLOCK(rt).
2262 * This routine does not require rnh_lock to be held by the caller,
2263 * although it needs to be indicated of such a case in order to call
2264 * the correct variant of the relevant routing routines.
2267 nd6_lookup(struct in6_addr
*addr6
, int create
, struct ifnet
*ifp
, int rt_locked
)
2270 struct sockaddr_in6 sin6
;
2271 unsigned int ifscope
;
2273 bzero(&sin6
, sizeof(sin6
));
2274 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2275 sin6
.sin6_family
= AF_INET6
;
2276 sin6
.sin6_addr
= *addr6
;
2278 ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
2280 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2281 rt
= rtalloc1_scoped_locked(SA(&sin6
), create
, 0, ifscope
);
2283 rt
= rtalloc1_scoped(SA(&sin6
), create
, 0, ifscope
);
2288 if ((rt
->rt_flags
& RTF_LLINFO
) == 0) {
2290 * This is the case for the default route.
2291 * If we want to create a neighbor cache for the
2292 * address, we should free the route for the
2293 * destination and allocate an interface route.
2307 if (create
&& ifp
) {
2309 u_int32_t ifa_flags
;
2313 * If no route is available and create is set,
2314 * we allocate a host route for the destination
2315 * and treat it like an interface route.
2316 * This hack is necessary for a neighbor which can't
2317 * be covered by our own prefix.
2319 ifa
= ifaof_ifpforaddr(SA(&sin6
), ifp
);
2325 * Create a new route. RTF_LLINFO is necessary
2326 * to create a Neighbor Cache entry for the
2327 * destination in nd6_rtrequest which will be
2328 * called in rtrequest via ifa->ifa_rtrequest.
2331 lck_mtx_lock(rnh_lock
);
2334 ifa_flags
= ifa
->ifa_flags
;
2336 if ((e
= rtrequest_scoped_locked(RTM_ADD
,
2337 SA(&sin6
), ifa
->ifa_addr
, SA(&all1_sa
),
2338 (ifa_flags
| RTF_HOST
| RTF_LLINFO
) &
2339 ~RTF_CLONING
, &rt
, ifscope
)) != 0) {
2341 log(LOG_ERR
, "%s: failed to add route "
2342 "for a neighbor(%s), errno=%d\n",
2343 __func__
, ip6_sprintf(addr6
), e
);
2347 lck_mtx_unlock(rnh_lock
);
2355 if (rt
->rt_llinfo
) {
2356 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
2357 struct nd_ifinfo
*ndi
= ND_IFINFO(rt
->rt_ifp
);
2359 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2361 * For interface's that do not perform NUD
2362 * neighbor cache entres must always be marked
2363 * reachable with no expiry
2365 if (ndi
->flags
& ND6_IFF_PERFORMNUD
) {
2366 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_NOSTATE
);
2368 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2369 ln_setexpire(ln
, 0);
2376 RT_LOCK_ASSERT_HELD(rt
);
2378 * Validation for the entry.
2379 * Note that the check for rt_llinfo is necessary because a cloned
2380 * route from a parent route that has the L flag (e.g. the default
2381 * route to a p2p interface) may have the flag, too, while the
2382 * destination is not actually a neighbor.
2383 * XXX: we can't use rt->rt_ifp to check for the interface, since
2384 * it might be the loopback interface if the entry is for our
2385 * own address on a non-loopback interface. Instead, we should
2386 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
2388 * Note also that ifa_ifp and ifp may differ when we connect two
2389 * interfaces to a same link, install a link prefix to an interface,
2390 * and try to install a neighbor cache on an interface that does not
2391 * have a route to the prefix.
2393 * If the address is from a proxied prefix, the ifa_ifp and ifp might
2394 * not match, because nd6_na_input() could have modified the ifp
2395 * of the route to point to the interface where the NA arrived on,
2396 * hence the test for RTF_PROXY.
2398 if ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
2399 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
2400 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
&&
2401 !(rt
->rt_flags
& RTF_PROXY
))) {
2402 RT_REMREF_LOCKED(rt
);
2405 log(LOG_DEBUG
, "%s: failed to lookup %s "
2406 "(if = %s)\n", __func__
, ip6_sprintf(addr6
),
2407 ifp
? if_name(ifp
) : "unspec");
2408 /* xxx more logs... kazu */
2413 * Caller needs to release reference and call RT_UNLOCK(rt).
2419 * Test whether a given IPv6 address is a neighbor or not, ignoring
2420 * the actual neighbor cache. The neighbor cache is ignored in order
2421 * to not reenter the routing code from within itself.
2424 nd6_is_new_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
)
2426 struct nd_prefix
*pr
;
2427 struct ifaddr
*dstaddr
;
2429 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2432 * A link-local address is always a neighbor.
2433 * XXX: a link does not necessarily specify a single interface.
2435 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
)) {
2436 struct sockaddr_in6 sin6_copy
;
2440 * We need sin6_copy since sa6_recoverscope() may modify the
2444 if (sa6_recoverscope(&sin6_copy
, FALSE
)) {
2445 return 0; /* XXX: should be impossible */
2447 if (in6_setscope(&sin6_copy
.sin6_addr
, ifp
, &zone
)) {
2450 if (sin6_copy
.sin6_scope_id
== zone
) {
2458 * If the address matches one of our addresses,
2459 * it should be a neighbor.
2460 * If the address matches one of our on-link prefixes, it should be a
2463 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
2465 if (pr
->ndpr_ifp
!= ifp
) {
2469 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
2473 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr
->ndpr_prefix
.sin6_addr
,
2474 &addr
->sin6_addr
, &pr
->ndpr_mask
)) {
2482 * If the address is assigned on the node of the other side of
2483 * a p2p interface, the address should be a neighbor.
2485 dstaddr
= ifa_ifwithdstaddr(SA(addr
));
2486 if (dstaddr
!= NULL
) {
2487 if (dstaddr
->ifa_ifp
== ifp
) {
2488 IFA_REMREF(dstaddr
);
2491 IFA_REMREF(dstaddr
);
2500 * Detect if a given IPv6 address identifies a neighbor on a given link.
2501 * XXX: should take care of the destination of a p2p link?
2504 nd6_is_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
,
2509 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2510 lck_mtx_lock(nd6_mutex
);
2511 if (nd6_is_new_addr_neighbor(addr
, ifp
)) {
2512 lck_mtx_unlock(nd6_mutex
);
2515 lck_mtx_unlock(nd6_mutex
);
2518 * Even if the address matches none of our addresses, it might be
2519 * in the neighbor cache.
2521 if ((rt
= nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
)) != NULL
) {
2522 RT_LOCK_ASSERT_HELD(rt
);
2523 RT_REMREF_LOCKED(rt
);
2532 * Free an nd6 llinfo entry.
2533 * Since the function would cause significant changes in the kernel, DO NOT
2534 * make it global, unless you have a strong reason for the change, and are sure
2535 * that the change is safe.
2538 nd6_free(struct rtentry
*rt
)
2540 struct llinfo_nd6
*ln
= NULL
;
2541 struct in6_addr in6
= {};
2542 struct nd_defrouter
*dr
= NULL
;
2544 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2545 RT_LOCK_ASSERT_NOTHELD(rt
);
2546 lck_mtx_lock(nd6_mutex
);
2549 RT_ADDREF_LOCKED(rt
); /* Extra ref */
2551 in6
= SIN6(rt_key(rt
))->sin6_addr
;
2554 * Prevent another thread from modifying rt_key, rt_gateway
2555 * via rt_setgate() after the rt_lock is dropped by marking
2556 * the route as defunct.
2558 rt
->rt_flags
|= RTF_CONDEMNED
;
2561 * We used to have pfctlinput(PRC_HOSTDEAD) here. Even though it is
2562 * not harmful, it was not really necessary. Perform default router
2563 * selection even when we are a router, if Scoped Routing is enabled.
2565 /* XXX TDB Handle lists in route information option as well */
2566 dr
= defrouter_lookup(NULL
, &SIN6(rt_key(rt
))->sin6_addr
, rt
->rt_ifp
);
2568 if ((ln
&& ln
->ln_router
) || dr
) {
2570 * rt6_flush must be called whether or not the neighbor
2571 * is in the Default Router List.
2572 * See a corresponding comment in nd6_na_input().
2575 lck_mtx_unlock(nd6_mutex
);
2576 rt6_flush(&in6
, rt
->rt_ifp
);
2577 lck_mtx_lock(nd6_mutex
);
2585 * Unreachablity of a router might affect the default
2586 * router selection and on-link detection of advertised
2591 * Temporarily fake the state to choose a new default
2592 * router and to perform on-link determination of
2593 * prefixes correctly.
2594 * Below the state will be set correctly,
2595 * or the entry itself will be deleted.
2598 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_INCOMPLETE
);
2601 * Since defrouter_select() does not affect the
2602 * on-link determination and MIP6 needs the check
2603 * before the default router selection, we perform
2607 pfxlist_onlink_check();
2610 * refresh default router list
2612 defrouter_select(rt
->rt_ifp
, NULL
);
2614 /* Loop through all RTI's as well and trigger router selection. */
2615 nd6_router_select_rti_entries(rt
->rt_ifp
);
2617 RT_LOCK_ASSERT_NOTHELD(rt
);
2618 lck_mtx_unlock(nd6_mutex
);
2620 * Detach the route from the routing tree and the list of neighbor
2621 * caches, and disable the route entry not to be used in already
2624 (void) rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
), 0, NULL
);
2626 /* Extra ref held above; now free it */
2631 nd6_rtrequest(int req
, struct rtentry
*rt
, struct sockaddr
*sa
)
2634 struct sockaddr
*gate
= rt
->rt_gateway
;
2635 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
2636 static struct sockaddr_dl null_sdl
=
2637 { .sdl_len
= sizeof(null_sdl
), .sdl_family
= AF_LINK
};
2638 struct ifnet
*ifp
= rt
->rt_ifp
;
2641 char buf
[MAX_IPv6_STR_LEN
];
2642 struct nd_ifinfo
*ndi
= ND_IFINFO(rt
->rt_ifp
);
2644 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2645 VERIFY(nd6_init_done
);
2646 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2647 RT_LOCK_ASSERT_HELD(rt
);
2650 * We have rnh_lock held, see if we need to schedule the timer;
2651 * we might do this again below during RTM_RESOLVE, but doing it
2652 * now handles all other cases.
2654 if (nd6_sched_timeout_want
) {
2655 nd6_sched_timeout(NULL
, NULL
);
2658 if (rt
->rt_flags
& RTF_GATEWAY
) {
2662 if (!nd6_need_cache(ifp
) && !(rt
->rt_flags
& RTF_HOST
)) {
2664 * This is probably an interface direct route for a link
2665 * which does not need neighbor caches (e.g. fe80::%lo0/64).
2666 * We do not need special treatment below for such a route.
2667 * Moreover, the RTF_LLINFO flag which would be set below
2668 * would annoy the ndp(8) command.
2673 if (req
== RTM_RESOLVE
) {
2676 if (!nd6_need_cache(ifp
)) { /* stf case */
2679 struct sockaddr_in6 sin6
;
2681 rtkey_to_sa6(rt
, &sin6
);
2683 * nd6_is_addr_neighbor() may call nd6_lookup(),
2684 * therefore we drop rt_lock to avoid deadlock
2685 * during the lookup.
2687 RT_ADDREF_LOCKED(rt
);
2689 no_nd_cache
= !nd6_is_addr_neighbor(&sin6
, ifp
, 1);
2691 RT_REMREF_LOCKED(rt
);
2695 * FreeBSD and BSD/OS often make a cloned host route based
2696 * on a less-specific route (e.g. the default route).
2697 * If the less specific route does not have a "gateway"
2698 * (this is the case when the route just goes to a p2p or an
2699 * stf interface), we'll mistakenly make a neighbor cache for
2700 * the host route, and will see strange neighbor solicitation
2701 * for the corresponding destination. In order to avoid the
2702 * confusion, we check if the destination of the route is
2703 * a neighbor in terms of neighbor discovery, and stop the
2704 * process if not. Additionally, we remove the LLINFO flag
2705 * so that ndp(8) will not try to get the neighbor information
2706 * of the destination.
2709 rt
->rt_flags
&= ~RTF_LLINFO
;
2714 timenow
= net_uptime();
2719 * There is no backward compatibility :)
2721 * if ((rt->rt_flags & RTF_HOST) == 0 &&
2722 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
2723 * rt->rt_flags |= RTF_CLONING;
2725 if ((rt
->rt_flags
& RTF_CLONING
) ||
2726 ((rt
->rt_flags
& RTF_LLINFO
) && ln
== NULL
)) {
2728 * Case 1: This route should come from a route to
2729 * interface (RTF_CLONING case) or the route should be
2730 * treated as on-link but is currently not
2731 * (RTF_LLINFO && ln == NULL case).
2733 if (rt_setgate(rt
, rt_key(rt
), SA(&null_sdl
)) == 0) {
2734 gate
= rt
->rt_gateway
;
2735 SDL(gate
)->sdl_type
= ifp
->if_type
;
2736 SDL(gate
)->sdl_index
= ifp
->if_index
;
2738 * In case we're called before 1.0 sec.
2743 (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
)
2744 ? 0 : MAX(timenow
, 1));
2747 if (rt
->rt_flags
& RTF_CLONING
) {
2752 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
2753 * We don't do that here since llinfo is not ready yet.
2755 * There are also couple of other things to be discussed:
2756 * - unsolicited NA code needs improvement beforehand
2757 * - RFC4861 says we MAY send multicast unsolicited NA
2758 * (7.2.6 paragraph 4), however, it also says that we
2759 * SHOULD provide a mechanism to prevent multicast NA storm.
2760 * we don't have anything like it right now.
2761 * note that the mechanism needs a mutual agreement
2762 * between proxies, which means that we need to implement
2763 * a new protocol, or a new kludge.
2764 * - from RFC4861 6.2.4, host MUST NOT send an unsolicited RA.
2765 * we need to check ip6forwarding before sending it.
2766 * (or should we allow proxy ND configuration only for
2767 * routers? there's no mention about proxy ND from hosts)
2771 if (!(ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))) {
2773 * Address resolution isn't necessary for a point to
2774 * point link, so we can skip this test for a p2p link.
2776 if (gate
->sa_family
!= AF_LINK
||
2777 gate
->sa_len
< sizeof(null_sdl
)) {
2778 /* Don't complain in case of RTM_ADD */
2779 if (req
== RTM_RESOLVE
) {
2780 log(LOG_ERR
, "%s: route to %s has bad "
2781 "gateway address (sa_family %u "
2782 "sa_len %u) on %s\n", __func__
,
2784 &SIN6(rt_key(rt
))->sin6_addr
, buf
,
2785 sizeof(buf
)), gate
->sa_family
,
2786 gate
->sa_len
, if_name(ifp
));
2790 SDL(gate
)->sdl_type
= ifp
->if_type
;
2791 SDL(gate
)->sdl_index
= ifp
->if_index
;
2794 break; /* This happens on a route change */
2797 * Case 2: This route may come from cloning, or a manual route
2798 * add with a LL address.
2800 rt
->rt_llinfo
= ln
= nd6_llinfo_alloc(Z_WAITOK
);
2803 rt
->rt_llinfo_get_ri
= nd6_llinfo_get_ri
;
2804 rt
->rt_llinfo_get_iflri
= nd6_llinfo_get_iflri
;
2805 rt
->rt_llinfo_purge
= nd6_llinfo_purge
;
2806 rt
->rt_llinfo_free
= nd6_llinfo_free
;
2807 rt
->rt_llinfo_refresh
= nd6_llinfo_refresh
;
2808 rt
->rt_flags
|= RTF_LLINFO
;
2810 /* this is required for "ndp" command. - shin */
2812 * For interface's that do not perform NUD
2813 * neighbor cache entries must always be marked
2814 * reachable with no expiry
2816 if ((req
== RTM_ADD
) ||
2817 !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
2819 * gate should have some valid AF_LINK entry,
2820 * and ln->ln_expire should have some lifetime
2821 * which is specified by ndp command.
2823 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2824 ln_setexpire(ln
, 0);
2827 * When req == RTM_RESOLVE, rt is created and
2828 * initialized in rtrequest(), so rt_expire is 0.
2830 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_NOSTATE
);
2831 /* In case we're called before 1.0 sec. has elapsed */
2832 ln_setexpire(ln
, (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
) ?
2833 0 : MAX(timenow
, 1));
2838 /* We have at least one entry; arm the timer if not already */
2839 nd6_sched_timeout(NULL
, NULL
);
2842 * If we have too many cache entries, initiate immediate
2843 * purging for some "less recently used" entries. Note that
2844 * we cannot directly call nd6_free() here because it would
2845 * cause re-entering rtable related routines triggering an LOR
2848 if (ip6_neighborgcthresh
> 0 &&
2849 nd6_inuse
>= ip6_neighborgcthresh
) {
2852 for (i
= 0; i
< 10 && llinfo_nd6
.ln_prev
!= ln
; i
++) {
2853 struct llinfo_nd6
*ln_end
= llinfo_nd6
.ln_prev
;
2854 struct rtentry
*rt_end
= ln_end
->ln_rt
;
2856 /* Move this entry to the head */
2859 LN_INSERTHEAD(ln_end
);
2861 if (ln_end
->ln_expire
== 0) {
2865 if (ln_end
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
2866 ND6_CACHE_STATE_TRANSITION(ln_end
, ND6_LLINFO_STALE
);
2868 ND6_CACHE_STATE_TRANSITION(ln_end
, ND6_LLINFO_PURGE
);
2870 ln_setexpire(ln_end
, timenow
);
2876 * check if rt_key(rt) is one of my address assigned
2879 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
2880 &SIN6(rt_key(rt
))->sin6_addr
);
2882 caddr_t macp
= nd6_ifptomac(ifp
);
2883 ln_setexpire(ln
, 0);
2884 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2886 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
2887 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
2889 if (nd6_useloopback
) {
2890 if (rt
->rt_ifp
!= lo_ifp
) {
2892 * Purge any link-layer info caching.
2894 if (rt
->rt_llinfo_purge
!= NULL
) {
2895 rt
->rt_llinfo_purge(rt
);
2899 * Adjust route ref count for the
2902 if (rt
->rt_if_ref_fn
!= NULL
) {
2903 rt
->rt_if_ref_fn(lo_ifp
, 1);
2904 rt
->rt_if_ref_fn(rt
->rt_ifp
,
2908 rt
->rt_ifp
= lo_ifp
;
2910 * If rmx_mtu is not locked, update it
2911 * to the MTU used by the new interface.
2913 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
2914 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
2917 * Make sure rt_ifa be equal to the ifaddr
2918 * corresponding to the address.
2919 * We need this because when we refer
2920 * rt_ifa->ia6_flags in ip6_input, we assume
2921 * that the rt_ifa points to the address instead
2922 * of the loopback address.
2924 if (ifa
!= rt
->rt_ifa
) {
2929 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
2930 ln_setexpire(ln
, 0);
2931 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2933 /* join solicited node multicast for proxy ND */
2934 if (ifp
->if_flags
& IFF_MULTICAST
) {
2935 struct in6_addr llsol
;
2936 struct in6_multi
*in6m
;
2939 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2940 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2941 llsol
.s6_addr32
[1] = 0;
2942 llsol
.s6_addr32
[2] = htonl(1);
2943 llsol
.s6_addr8
[12] = 0xff;
2944 if (in6_setscope(&llsol
, ifp
, NULL
)) {
2947 error
= in6_mc_join(ifp
, &llsol
,
2950 nd6log(error
, "%s: failed to join "
2951 "%s (errno=%d)\n", if_name(ifp
),
2952 ip6_sprintf(&llsol
), error
);
2964 /* leave from solicited node multicast for proxy ND */
2965 if ((rt
->rt_flags
& RTF_ANNOUNCE
) &&
2966 (ifp
->if_flags
& IFF_MULTICAST
)) {
2967 struct in6_addr llsol
;
2968 struct in6_multi
*in6m
;
2970 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2971 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2972 llsol
.s6_addr32
[1] = 0;
2973 llsol
.s6_addr32
[2] = htonl(1);
2974 llsol
.s6_addr8
[12] = 0xff;
2975 if (in6_setscope(&llsol
, ifp
, NULL
) == 0) {
2976 in6_multihead_lock_shared();
2977 IN6_LOOKUP_MULTI(&llsol
, ifp
, in6m
);
2978 in6_multihead_lock_done();
2980 in6_mc_leave(in6m
, NULL
);
2987 * Unchain it but defer the actual freeing until the route
2988 * itself is to be freed. rt->rt_llinfo still points to
2989 * llinfo_nd6, and likewise, ln->ln_rt stil points to this
2990 * route entry, except that RTF_LLINFO is now cleared.
2992 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
2997 * Purge any link-layer info caching.
2999 if (rt
->rt_llinfo_purge
!= NULL
) {
3000 rt
->rt_llinfo_purge(rt
);
3003 rt
->rt_flags
&= ~RTF_LLINFO
;
3004 if (ln
->ln_hold
!= NULL
) {
3005 m_freem_list(ln
->ln_hold
);
3012 nd6_siocgdrlst(void *data
, int data_is_64
)
3014 struct in6_drlist_32
*drl_32
;
3015 struct nd_defrouter
*dr
;
3018 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3020 dr
= TAILQ_FIRST(&nd_defrouter_list
);
3022 /* XXX Handle mapped defrouter entries */
3023 /* For 64-bit process */
3025 struct in6_drlist_64
*drl_64
;
3027 drl_64
= _MALLOC(sizeof(*drl_64
), M_TEMP
, M_WAITOK
| M_ZERO
);
3028 if (drl_64
== NULL
) {
3032 /* preserve the interface name */
3033 bcopy(data
, drl_64
, sizeof(drl_64
->ifname
));
3035 while (dr
&& i
< DRLSTSIZ
) {
3036 drl_64
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
3037 if (IN6_IS_ADDR_LINKLOCAL(
3038 &drl_64
->defrouter
[i
].rtaddr
)) {
3039 /* XXX: need to this hack for KAME stack */
3040 drl_64
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
3043 "default router list contains a "
3044 "non-linklocal address(%s)\n",
3045 ip6_sprintf(&drl_64
->defrouter
[i
].rtaddr
));
3047 drl_64
->defrouter
[i
].flags
= dr
->flags
;
3048 drl_64
->defrouter
[i
].rtlifetime
= (u_short
)dr
->rtlifetime
;
3049 drl_64
->defrouter
[i
].expire
= (u_long
)nddr_getexpire(dr
);
3050 drl_64
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
3052 dr
= TAILQ_NEXT(dr
, dr_entry
);
3054 bcopy(drl_64
, data
, sizeof(*drl_64
));
3055 _FREE(drl_64
, M_TEMP
);
3059 /* For 32-bit process */
3060 drl_32
= _MALLOC(sizeof(*drl_32
), M_TEMP
, M_WAITOK
| M_ZERO
);
3061 if (drl_32
== NULL
) {
3065 /* preserve the interface name */
3066 bcopy(data
, drl_32
, sizeof(drl_32
->ifname
));
3068 while (dr
!= NULL
&& i
< DRLSTSIZ
) {
3069 drl_32
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
3070 if (IN6_IS_ADDR_LINKLOCAL(&drl_32
->defrouter
[i
].rtaddr
)) {
3071 /* XXX: need to this hack for KAME stack */
3072 drl_32
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
3075 "default router list contains a "
3076 "non-linklocal address(%s)\n",
3077 ip6_sprintf(&drl_32
->defrouter
[i
].rtaddr
));
3079 drl_32
->defrouter
[i
].flags
= dr
->flags
;
3080 drl_32
->defrouter
[i
].rtlifetime
= (u_short
)dr
->rtlifetime
;
3081 drl_32
->defrouter
[i
].expire
= (u_int32_t
)nddr_getexpire(dr
);
3082 drl_32
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
3084 dr
= TAILQ_NEXT(dr
, dr_entry
);
3086 bcopy(drl_32
, data
, sizeof(*drl_32
));
3087 _FREE(drl_32
, M_TEMP
);
3092 * XXX meaning of fields, especialy "raflags", is very
3093 * differnet between RA prefix list and RR/static prefix list.
3094 * how about separating ioctls into two?
3097 nd6_siocgprlst(void *data
, int data_is_64
)
3099 struct in6_prlist_32
*prl_32
;
3100 struct nd_prefix
*pr
;
3103 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
3105 pr
= nd_prefix
.lh_first
;
3107 /* XXX Handle mapped defrouter entries */
3108 /* For 64-bit process */
3110 struct in6_prlist_64
*prl_64
;
3112 prl_64
= _MALLOC(sizeof(*prl_64
), M_TEMP
, M_WAITOK
| M_ZERO
);
3113 if (prl_64
== NULL
) {
3117 /* preserve the interface name */
3118 bcopy(data
, prl_64
, sizeof(prl_64
->ifname
));
3120 while (pr
&& i
< PRLSTSIZ
) {
3121 struct nd_pfxrouter
*pfr
;
3125 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
3126 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
3127 prl_64
->prefix
[i
].raflags
= pr
->ndpr_raf
;
3128 prl_64
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
3129 prl_64
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
3130 prl_64
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
3131 prl_64
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
3132 prl_64
->prefix
[i
].expire
= (u_long
)ndpr_getexpire(pr
);
3134 pfr
= pr
->ndpr_advrtrs
.lh_first
;
3138 #define RTRADDR prl_64->prefix[i].advrtr[j]
3139 RTRADDR
= pfr
->router
->rtaddr
;
3140 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
3141 /* XXX: hack for KAME */
3142 RTRADDR
.s6_addr16
[1] = 0;
3145 "a router(%s) advertises "
3147 "non-link local address\n",
3148 ip6_sprintf(&RTRADDR
));
3153 pfr
= pfr
->pfr_next
;
3155 ASSERT(j
<= USHRT_MAX
);
3156 prl_64
->prefix
[i
].advrtrs
= (u_short
)j
;
3157 prl_64
->prefix
[i
].origin
= PR_ORIG_RA
;
3163 bcopy(prl_64
, data
, sizeof(*prl_64
));
3164 _FREE(prl_64
, M_TEMP
);
3168 /* For 32-bit process */
3169 prl_32
= _MALLOC(sizeof(*prl_32
), M_TEMP
, M_WAITOK
| M_ZERO
);
3170 if (prl_32
== NULL
) {
3174 /* preserve the interface name */
3175 bcopy(data
, prl_32
, sizeof(prl_32
->ifname
));
3177 while (pr
&& i
< PRLSTSIZ
) {
3178 struct nd_pfxrouter
*pfr
;
3182 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
3183 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
3184 prl_32
->prefix
[i
].raflags
= pr
->ndpr_raf
;
3185 prl_32
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
3186 prl_32
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
3187 prl_32
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
3188 prl_32
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
3189 prl_32
->prefix
[i
].expire
= (u_int32_t
)ndpr_getexpire(pr
);
3191 pfr
= pr
->ndpr_advrtrs
.lh_first
;
3195 #define RTRADDR prl_32->prefix[i].advrtr[j]
3196 RTRADDR
= pfr
->router
->rtaddr
;
3197 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
3198 /* XXX: hack for KAME */
3199 RTRADDR
.s6_addr16
[1] = 0;
3202 "a router(%s) advertises "
3204 "non-link local address\n",
3205 ip6_sprintf(&RTRADDR
));
3210 pfr
= pfr
->pfr_next
;
3212 ASSERT(j
<= USHRT_MAX
);
3213 prl_32
->prefix
[i
].advrtrs
= (u_short
)j
;
3214 prl_32
->prefix
[i
].origin
= PR_ORIG_RA
;
3220 bcopy(prl_32
, data
, sizeof(*prl_32
));
3221 _FREE(prl_32
, M_TEMP
);
3226 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
3228 struct nd_defrouter
*dr
;
3229 struct nd_prefix
*pr
;
3233 VERIFY(ifp
!= NULL
);
3236 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
3237 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
3239 * obsolete API, use sysctl under net.inet6.icmp6
3241 lck_mtx_lock(nd6_mutex
);
3242 error
= nd6_siocgdrlst(data
, cmd
== SIOCGDRLST_IN6_64
);
3243 lck_mtx_unlock(nd6_mutex
);
3246 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
3247 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
3249 * obsolete API, use sysctl under net.inet6.icmp6
3251 lck_mtx_lock(nd6_mutex
);
3252 error
= nd6_siocgprlst(data
, cmd
== SIOCGPRLST_IN6_64
);
3253 lck_mtx_unlock(nd6_mutex
);
3256 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
3257 case SIOCGIFINFO_IN6
: { /* struct in6_ondireq */
3259 struct in6_ondireq
*ondi
= (struct in6_ondireq
*)(void *)data
;
3260 struct nd_ifinfo
*ndi
;
3262 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
3263 * instead of in6_ndireq, so we treat it as such.
3265 ndi
= ND_IFINFO(ifp
);
3266 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)) {
3270 lck_mtx_lock(&ndi
->lock
);
3271 linkmtu
= IN6_LINKMTU(ifp
);
3272 bcopy(&linkmtu
, &ondi
->ndi
.linkmtu
, sizeof(linkmtu
));
3273 bcopy(&ndi
->maxmtu
, &ondi
->ndi
.maxmtu
,
3275 bcopy(&ndi
->basereachable
, &ondi
->ndi
.basereachable
,
3277 bcopy(&ndi
->reachable
, &ondi
->ndi
.reachable
,
3279 bcopy(&ndi
->retrans
, &ondi
->ndi
.retrans
,
3281 bcopy(&ndi
->flags
, &ondi
->ndi
.flags
,
3283 bcopy(&ndi
->recalctm
, &ondi
->ndi
.recalctm
,
3285 ondi
->ndi
.chlim
= ndi
->chlim
;
3287 * The below truncation is fine as we mostly use it for
3288 * debugging purpose.
3290 ondi
->ndi
.receivedra
= (uint8_t)ndi
->ndefrouters
;
3291 ondi
->ndi
.collision_count
= (uint8_t)ndi
->cga_collision_count
;
3292 lck_mtx_unlock(&ndi
->lock
);
3296 case SIOCSIFINFO_FLAGS
: { /* struct in6_ndireq */
3298 * XXX BSD has a bunch of checks here to ensure
3299 * that interface disabled flag is not reset if
3300 * link local address has failed DAD.
3301 * Investigate that part.
3303 struct in6_ndireq
*cndi
= (struct in6_ndireq
*)(void *)data
;
3304 u_int32_t oflags
, flags
;
3305 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
3307 /* XXX: almost all other fields of cndi->ndi is unused */
3308 if ((NULL
== ndi
) || !ndi
->initialized
) {
3313 lck_mtx_lock(&ndi
->lock
);
3314 oflags
= ndi
->flags
;
3315 bcopy(&cndi
->ndi
.flags
, &(ndi
->flags
), sizeof(flags
));
3317 lck_mtx_unlock(&ndi
->lock
);
3319 if (oflags
== flags
) {
3323 error
= nd6_setifinfo(ifp
, oflags
, flags
);
3327 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
3328 /* flush default router list */
3330 * xxx sumikawa: should not delete route if default
3331 * route equals to the top of default router list
3333 * XXX TODO: Needs to be done for RTI as well
3334 * Is very specific flush command with ndp for default routers.
3336 lck_mtx_lock(nd6_mutex
);
3338 defrouter_select(ifp
, NULL
);
3339 lck_mtx_unlock(nd6_mutex
);
3340 /* xxx sumikawa: flush prefix list */
3343 case SIOCSPFXFLUSH_IN6
: { /* struct in6_ifreq */
3344 /* flush all the prefix advertised by routers */
3345 struct nd_prefix
*next
= NULL
;
3347 lck_mtx_lock(nd6_mutex
);
3348 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
3349 struct in6_ifaddr
*ia
= NULL
;
3350 bool iterate_pfxlist_again
= false;
3352 next
= pr
->ndpr_next
;
3355 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
3359 if (ifp
!= lo_ifp
&& pr
->ndpr_ifp
!= ifp
) {
3363 /* do we really have to remove addresses as well? */
3366 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
3367 bool from_begining
= true;
3368 while (from_begining
) {
3369 from_begining
= false;
3370 TAILQ_FOREACH(ia
, &in6_ifaddrhead
, ia6_link
) {
3371 IFA_LOCK(&ia
->ia_ifa
);
3372 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
3373 IFA_UNLOCK(&ia
->ia_ifa
);
3377 if (ia
->ia6_ndpr
== pr
) {
3378 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
3379 IFA_UNLOCK(&ia
->ia_ifa
);
3380 lck_rw_done(&in6_ifaddr_rwlock
);
3381 lck_mtx_unlock(nd6_mutex
);
3382 in6_purgeaddr(&ia
->ia_ifa
);
3383 IFA_REMREF(&ia
->ia_ifa
);
3384 lck_mtx_lock(nd6_mutex
);
3385 lck_rw_lock_exclusive(
3386 &in6_ifaddr_rwlock
);
3388 * Purging the address caused
3389 * in6_ifaddr_rwlock to be
3391 * reacquired; therefore search again
3392 * from the beginning of in6_ifaddrs.
3393 * The same applies for the prefix list.
3395 iterate_pfxlist_again
= true;
3396 from_begining
= true;
3399 IFA_UNLOCK(&ia
->ia_ifa
);
3402 lck_rw_done(&in6_ifaddr_rwlock
);
3406 pfxlist_onlink_check();
3408 if (iterate_pfxlist_again
) {
3409 next
= nd_prefix
.lh_first
;
3412 lck_mtx_unlock(nd6_mutex
);
3416 case SIOCSRTRFLUSH_IN6
: { /* struct in6_ifreq */
3417 /* flush all the default routers */
3418 struct nd_defrouter
*next
;
3419 struct nd_drhead nd_defrouter_tmp
;
3421 TAILQ_INIT(&nd_defrouter_tmp
);
3422 lck_mtx_lock(nd6_mutex
);
3423 if ((dr
= TAILQ_FIRST(&nd_defrouter_list
)) != NULL
) {
3425 * The first entry of the list may be stored in
3426 * the routing table, so we'll delete it later.
3428 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
3429 next
= TAILQ_NEXT(dr
, dr_entry
);
3430 if (ifp
== lo_ifp
|| dr
->ifp
== ifp
) {
3432 * Remove the entry from default router list
3433 * and add it to the temp list.
3434 * nd_defrouter_tmp will be a local temporary
3435 * list as no one else can get the same
3436 * removed entry once it is removed from default
3438 * Remove the reference after calling defrtrlist_de
3440 TAILQ_REMOVE(&nd_defrouter_list
, dr
, dr_entry
);
3441 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
3445 dr
= TAILQ_FIRST(&nd_defrouter_list
);
3446 if (ifp
== lo_ifp
||
3448 TAILQ_REMOVE(&nd_defrouter_list
, dr
, dr_entry
);
3449 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
3454 * Keep the following separate from the above iteration of
3455 * nd_defrouter because it's not safe to call
3456 * defrtrlist_del while iterating global default
3457 * router list. Global list has to be traversed
3458 * while holding nd6_mutex throughout.
3460 * The following call to defrtrlist_del should be
3461 * safe as we are iterating a local list of
3464 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, next
) {
3465 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
3466 defrtrlist_del(dr
, NULL
);
3467 NDDR_REMREF(dr
); /* remove list reference */
3470 /* For now flush RTI routes here as well to avoid any regressions */
3471 nd6_purge_interface_rti_entries((ifp
== lo_ifp
) ? NULL
: ifp
);
3473 lck_mtx_unlock(nd6_mutex
);
3477 case SIOCGNBRINFO_IN6_32
: { /* struct in6_nbrinfo_32 */
3478 struct llinfo_nd6
*ln
;
3479 struct in6_nbrinfo_32 nbi_32
;
3480 struct in6_addr nb_addr
; /* make local for safety */
3482 bcopy(data
, &nbi_32
, sizeof(nbi_32
));
3483 nb_addr
= nbi_32
.addr
;
3485 * XXX: KAME specific hack for scoped addresses
3486 * XXXX: for other scopes than link-local?
3488 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32
.addr
) ||
3489 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32
.addr
)) {
3491 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
3494 *idp
= htons(ifp
->if_index
);
3498 /* Callee returns a locked route upon success */
3499 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
3503 RT_LOCK_ASSERT_HELD(rt
);
3505 nbi_32
.state
= ln
->ln_state
;
3506 nbi_32
.asked
= ln
->ln_asked
;
3507 nbi_32
.isrouter
= ln
->ln_router
;
3508 nbi_32
.expire
= (int)ln_getexpire(ln
);
3509 RT_REMREF_LOCKED(rt
);
3511 bcopy(&nbi_32
, data
, sizeof(nbi_32
));
3515 case SIOCGNBRINFO_IN6_64
: { /* struct in6_nbrinfo_64 */
3516 struct llinfo_nd6
*ln
;
3517 struct in6_nbrinfo_64 nbi_64
;
3518 struct in6_addr nb_addr
; /* make local for safety */
3520 bcopy(data
, &nbi_64
, sizeof(nbi_64
));
3521 nb_addr
= nbi_64
.addr
;
3523 * XXX: KAME specific hack for scoped addresses
3524 * XXXX: for other scopes than link-local?
3526 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64
.addr
) ||
3527 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64
.addr
)) {
3529 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
3532 *idp
= htons(ifp
->if_index
);
3536 /* Callee returns a locked route upon success */
3537 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
3541 RT_LOCK_ASSERT_HELD(rt
);
3543 nbi_64
.state
= ln
->ln_state
;
3544 nbi_64
.asked
= ln
->ln_asked
;
3545 nbi_64
.isrouter
= ln
->ln_router
;
3546 nbi_64
.expire
= (int)ln_getexpire(ln
);
3547 RT_REMREF_LOCKED(rt
);
3549 bcopy(&nbi_64
, data
, sizeof(nbi_64
));
3553 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
3554 case SIOCGDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
3555 struct in6_ndifreq_64
*ndif_64
=
3556 (struct in6_ndifreq_64
*)(void *)data
;
3557 struct in6_ndifreq_32
*ndif_32
=
3558 (struct in6_ndifreq_32
*)(void *)data
;
3560 if (cmd
== SIOCGDEFIFACE_IN6_64
) {
3561 u_int64_t j
= nd6_defifindex
;
3562 __nochk_bcopy(&j
, &ndif_64
->ifindex
, sizeof(j
));
3564 bcopy(&nd6_defifindex
, &ndif_32
->ifindex
,
3570 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
3571 case SIOCSDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
3572 struct in6_ndifreq_64
*ndif_64
=
3573 (struct in6_ndifreq_64
*)(void *)data
;
3574 struct in6_ndifreq_32
*ndif_32
=
3575 (struct in6_ndifreq_32
*)(void *)data
;
3578 if (cmd
== SIOCSDEFIFACE_IN6_64
) {
3580 __nochk_bcopy(&ndif_64
->ifindex
, &j
, sizeof(j
));
3583 bcopy(&ndif_32
->ifindex
, &idx
, sizeof(idx
));
3586 error
= nd6_setdefaultiface(idx
);
3590 case SIOCGIFCGAPREP_IN6_32
:
3591 case SIOCGIFCGAPREP_IN6_64
: {
3592 /* get CGA parameters */
3594 struct in6_cgareq_32
*cga32
;
3595 struct in6_cgareq_64
*cga64
;
3598 struct nd_ifinfo
*ndi
;
3599 struct in6_cga_modifier
*ndi_cga_mod
;
3600 struct in6_cga_modifier
*req_cga_mod
;
3602 ndi
= ND_IFINFO(ifp
);
3603 if ((NULL
== ndi
) || !ndi
->initialized
) {
3607 cgareq_u
.data
= data
;
3608 req_cga_mod
= (cmd
== SIOCGIFCGAPREP_IN6_64
)
3609 ? &(cgareq_u
.cga64
->cgar_cgaprep
.cga_modifier
)
3610 : &(cgareq_u
.cga32
->cgar_cgaprep
.cga_modifier
);
3611 lck_mtx_lock(&ndi
->lock
);
3612 ndi_cga_mod
= &(ndi
->local_cga_modifier
);
3613 bcopy(ndi_cga_mod
, req_cga_mod
, sizeof(*req_cga_mod
));
3614 lck_mtx_unlock(&ndi
->lock
);
3617 case SIOCSIFCGAPREP_IN6_32
:
3618 case SIOCSIFCGAPREP_IN6_64
:
3620 /* set CGA parameters */
3621 struct in6_cgareq cgareq
;
3623 struct nd_ifinfo
*ndi
;
3624 struct in6_cga_modifier
*ndi_cga_mod
;
3625 struct in6_cga_modifier
*req_cga_mod
;
3627 ndi
= ND_IFINFO(ifp
);
3628 if ((NULL
== ndi
) || !ndi
->initialized
) {
3632 is64
= (cmd
== SIOCSIFCGAPREP_IN6_64
);
3633 in6_cgareq_copy_from_user(data
, is64
, &cgareq
);
3634 req_cga_mod
= &cgareq
.cgar_cgaprep
.cga_modifier
;
3635 lck_mtx_lock(&ndi
->lock
);
3636 ndi_cga_mod
= &(ndi
->local_cga_modifier
);
3637 bcopy(req_cga_mod
, ndi_cga_mod
, sizeof(*ndi_cga_mod
));
3638 ndi
->cga_initialized
= TRUE
;
3639 ndi
->cga_collision_count
= 0;
3640 lck_mtx_unlock(&ndi
->lock
);
3650 * Create neighbor cache entry and cache link-layer address,
3651 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
3654 nd6_cache_lladdr(struct ifnet
*ifp
, struct in6_addr
*from
, char *lladdr
,
3655 int lladdrlen
, int type
, int code
)
3657 #pragma unused(lladdrlen)
3658 struct rtentry
*rt
= NULL
;
3659 struct llinfo_nd6
*ln
= NULL
;
3661 struct sockaddr_dl
*sdl
= NULL
;
3667 boolean_t sched_timeout
= FALSE
;
3668 struct nd_ifinfo
*ndi
= NULL
;
3671 panic("ifp == NULL in nd6_cache_lladdr");
3674 panic("from == NULL in nd6_cache_lladdr");
3677 /* nothing must be updated for unspecified address */
3678 if (IN6_IS_ADDR_UNSPECIFIED(from
)) {
3683 * Validation about ifp->if_addrlen and lladdrlen must be done in
3686 timenow
= net_uptime();
3688 rt
= nd6_lookup(from
, 0, ifp
, 0);
3690 if ((rt
= nd6_lookup(from
, 1, ifp
, 0)) == NULL
) {
3693 RT_LOCK_ASSERT_HELD(rt
);
3696 RT_LOCK_ASSERT_HELD(rt
);
3697 /* do nothing if static ndp is set */
3698 if (rt
->rt_flags
& RTF_STATIC
) {
3699 RT_REMREF_LOCKED(rt
);
3706 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
3713 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
3717 if (rt
->rt_gateway
== NULL
) {
3720 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
3723 sdl
= SDL(rt
->rt_gateway
);
3725 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
3726 if (olladdr
&& lladdr
) {
3727 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
)) {
3737 * newentry olladdr lladdr llchange (*=record)
3740 * 0 n y -- (3) * STALE
3742 * 0 y y y (5) * STALE
3743 * 1 -- n -- (6) NOSTATE(= PASSIVE)
3744 * 1 -- y -- (7) * STALE
3747 if (lladdr
!= NULL
) { /* (3-5) and (7) */
3749 * Record source link-layer address
3750 * XXX is it dependent to ifp->if_type?
3752 sdl
->sdl_alen
= ifp
->if_addrlen
;
3753 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
3755 /* cache the gateway (sender HW) address */
3756 nd6_llreach_alloc(rt
, ifp
, LLADDR(sdl
), sdl
->sdl_alen
, FALSE
);
3759 if (is_newentry
== 0) {
3760 if ((!olladdr
&& lladdr
!= NULL
) || /* (3) */
3761 (olladdr
&& lladdr
!= NULL
&& llchange
)) { /* (5) */
3763 newstate
= ND6_LLINFO_STALE
;
3764 } else { /* (1-2,4) */
3769 if (lladdr
== NULL
) { /* (6) */
3770 newstate
= ND6_LLINFO_NOSTATE
;
3772 newstate
= ND6_LLINFO_STALE
;
3777 * For interface's that do not perform NUD
3778 * neighbor cache entres must always be marked
3779 * reachable with no expiry
3781 ndi
= ND_IFINFO(ifp
);
3782 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3784 if (ndi
&& !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
3785 newstate
= ND6_LLINFO_REACHABLE
;
3786 ln_setexpire(ln
, 0);
3791 * Update the state of the neighbor cache.
3793 ND6_CACHE_STATE_TRANSITION(ln
, newstate
);
3795 if ((ln
->ln_state
== ND6_LLINFO_STALE
) ||
3796 (ln
->ln_state
== ND6_LLINFO_REACHABLE
)) {
3797 struct mbuf
*m
= ln
->ln_hold
;
3799 * XXX: since nd6_output() below will cause
3800 * state tansition to DELAY and reset the timer,
3801 * we must set the timer now, although it is actually
3804 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
3805 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
3810 struct sockaddr_in6 sin6
;
3812 rtkey_to_sa6(rt
, &sin6
);
3814 * we assume ifp is not a p2p here, so just
3815 * set the 2nd argument as the 1st one.
3818 nd6_output_list(ifp
, ifp
, m
, &sin6
, rt
, NULL
);
3821 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
3822 /* probe right away */
3823 ln_setexpire(ln
, timenow
);
3824 sched_timeout
= TRUE
;
3829 * ICMP6 type dependent behavior.
3831 * NS: clear IsRouter if new entry
3832 * RS: clear IsRouter
3833 * RA: set IsRouter if there's lladdr
3834 * redir: clear IsRouter if new entry
3837 * The spec says that we must set IsRouter in the following cases:
3838 * - If lladdr exist, set IsRouter. This means (1-5).
3839 * - If it is old entry (!newentry), set IsRouter. This means (7).
3840 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
3841 * A quetion arises for (1) case. (1) case has no lladdr in the
3842 * neighbor cache, this is similar to (6).
3843 * This case is rare but we figured that we MUST NOT set IsRouter.
3845 * newentry olladdr lladdr llchange NS RS RA redir
3847 * 0 n n -- (1) c ? s
3848 * 0 y n -- (2) c s s
3849 * 0 n y -- (3) c s s
3852 * 1 -- n -- (6) c c c s
3853 * 1 -- y -- (7) c c s c s
3857 switch (type
& 0xff) {
3858 case ND_NEIGHBOR_SOLICIT
:
3860 * New entry must have is_router flag cleared.
3862 if (is_newentry
) { /* (6-7) */
3868 * If the ICMP message is a Redirect to a better router, always
3869 * set the is_router flag. Otherwise, if the entry is newly
3870 * created, then clear the flag. [RFC 4861, sec 8.3]
3872 if (code
== ND_REDIRECT_ROUTER
) {
3874 } else if (is_newentry
) { /* (6-7) */
3878 case ND_ROUTER_SOLICIT
:
3880 * is_router flag must always be cleared.
3884 case ND_ROUTER_ADVERT
:
3886 * Mark an entry with lladdr as a router.
3888 if ((!is_newentry
&& (olladdr
|| lladdr
)) || /* (2-5) */
3889 (is_newentry
&& lladdr
)) { /* (7) */
3896 int route_ev_code
= 0;
3899 route_ev_code
= ROUTE_LLENTRY_CHANGED
;
3901 route_ev_code
= ROUTE_LLENTRY_RESOLVED
;
3904 /* Enqueue work item to invoke callback for this route entry */
3905 route_event_enqueue_nwk_wq_entry(rt
, NULL
, route_ev_code
, NULL
, TRUE
);
3907 if (ln
->ln_router
|| (rt
->rt_flags
& RTF_ROUTER
)) {
3908 struct radix_node_head
*rnh
= NULL
;
3909 struct route_event rt_ev
;
3910 route_event_init(&rt_ev
, rt
, NULL
, llchange
? ROUTE_LLENTRY_CHANGED
:
3911 ROUTE_LLENTRY_RESOLVED
);
3913 * We already have a valid reference on rt.
3914 * The function frees that before returning.
3915 * We therefore don't need an extra reference here
3918 lck_mtx_lock(rnh_lock
);
3920 rnh
= rt_tables
[AF_INET6
];
3922 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
,
3925 lck_mtx_unlock(rnh_lock
);
3931 * When the link-layer address of a router changes, select the
3932 * best router again. In particular, when the neighbor entry is newly
3933 * created, it might affect the selection policy.
3934 * Question: can we restrict the first condition to the "is_newentry"
3937 * Note: Perform default router selection even when we are a router,
3938 * if Scoped Routing is enabled.
3940 if (do_update
&& ln
->ln_router
) {
3942 * XXX TODO: This should also be iterated over router list
3943 * for route information option's router lists as well.
3945 RT_REMREF_LOCKED(rt
);
3947 lck_mtx_lock(nd6_mutex
);
3948 defrouter_select(ifp
, NULL
);
3949 nd6_router_select_rti_entries(ifp
);
3950 lck_mtx_unlock(nd6_mutex
);
3952 RT_REMREF_LOCKED(rt
);
3955 if (sched_timeout
) {
3956 lck_mtx_lock(rnh_lock
);
3957 nd6_sched_timeout(NULL
, NULL
);
3958 lck_mtx_unlock(rnh_lock
);
3963 nd6_slowtimo(void *arg
)
3966 struct nd_ifinfo
*nd6if
= NULL
;
3967 struct ifnet
*ifp
= NULL
;
3969 ifnet_head_lock_shared();
3970 for (ifp
= ifnet_head
.tqh_first
; ifp
;
3971 ifp
= ifp
->if_link
.tqe_next
) {
3972 nd6if
= ND_IFINFO(ifp
);
3973 if ((NULL
== nd6if
) || (FALSE
== nd6if
->initialized
)) {
3977 lck_mtx_lock(&nd6if
->lock
);
3978 if (nd6if
->basereachable
&& /* already initialized */
3979 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
3981 * Since reachable time rarely changes by router
3982 * advertisements, we SHOULD insure that a new random
3983 * value gets recomputed at least once every few hours.
3986 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
3988 ND_COMPUTE_RTIME(nd6if
->basereachable
);
3990 lck_mtx_unlock(&nd6if
->lock
);
3993 timeout(nd6_slowtimo
, NULL
, ND6_SLOWTIMER_INTERVAL
* hz
);
3997 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
3998 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
4000 return nd6_output_list(ifp
, origifp
, m0
, dst
, hint0
, adv
);
4006 * Assumption: route determination for first packet can be correctly applied to
4007 * all packets in the chain.
4009 #define senderr(e) { error = (e); goto bad; }
4011 nd6_output_list(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
4012 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
4014 struct rtentry
*rt
= hint0
, *hint
= hint0
;
4015 struct llinfo_nd6
*ln
= NULL
;
4018 struct rtentry
*rtrele
= NULL
;
4019 struct nd_ifinfo
*ndi
= NULL
;
4023 RT_ADDREF_LOCKED(rt
);
4026 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
) || !nd6_need_cache(ifp
)) {
4034 * Next hop determination. Because we may involve the gateway route
4035 * in addition to the original route, locking is rather complicated.
4036 * The general concept is that regardless of whether the route points
4037 * to the original route or to the gateway route, this routine takes
4038 * an extra reference on such a route. This extra reference will be
4039 * released at the end.
4041 * Care must be taken to ensure that the "hint0" route never gets freed
4042 * via rtfree(), since the caller may have stored it inside a struct
4043 * route with a reference held for that placeholder.
4045 * This logic is similar to, though not exactly the same as the one
4046 * used by route_to_gwroute().
4050 * We have a reference to "rt" by now (or below via rtalloc1),
4051 * which will either be released or freed at the end of this
4054 RT_LOCK_ASSERT_HELD(rt
);
4055 if (!(rt
->rt_flags
& RTF_UP
)) {
4056 RT_REMREF_LOCKED(rt
);
4058 if ((hint
= rt
= rtalloc1_scoped(SA(dst
), 1, 0,
4059 ifp
->if_index
)) != NULL
) {
4061 if (rt
->rt_ifp
!= ifp
) {
4062 /* XXX: loop care? */
4064 error
= nd6_output_list(ifp
, origifp
, m0
,
4070 senderr(EHOSTUNREACH
);
4074 if (rt
->rt_flags
& RTF_GATEWAY
) {
4075 struct rtentry
*gwrt
;
4076 struct in6_ifaddr
*ia6
= NULL
;
4077 struct sockaddr_in6 gw6
;
4079 rtgw_to_sa6(rt
, &gw6
);
4081 * Must drop rt_lock since nd6_is_addr_neighbor()
4082 * calls nd6_lookup() and acquires rnh_lock.
4087 * We skip link-layer address resolution and NUD
4088 * if the gateway is not a neighbor from ND point
4089 * of view, regardless of the value of nd_ifinfo.flags.
4090 * The second condition is a bit tricky; we skip
4091 * if the gateway is our own address, which is
4092 * sometimes used to install a route to a p2p link.
4094 if (!nd6_is_addr_neighbor(&gw6
, ifp
, 0) ||
4095 (ia6
= in6ifa_ifpwithaddr(ifp
, &gw6
.sin6_addr
))) {
4097 * We allow this kind of tricky route only
4098 * when the outgoing interface is p2p.
4099 * XXX: we may need a more generic rule here.
4102 IFA_REMREF(&ia6
->ia_ifa
);
4104 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
4105 senderr(EHOSTUNREACH
);
4111 gw6
= *(SIN6(rt
->rt_gateway
));
4113 /* If hint is now down, give up */
4114 if (!(rt
->rt_flags
& RTF_UP
)) {
4116 senderr(EHOSTUNREACH
);
4119 /* If there's no gateway route, look it up */
4120 if ((gwrt
= rt
->rt_gwroute
) == NULL
) {
4124 /* Become a regular mutex */
4125 RT_CONVERT_LOCK(rt
);
4128 * Take gwrt's lock while holding route's lock;
4129 * this is okay since gwrt never points back
4130 * to rt, so no lock ordering issues.
4133 if (!(gwrt
->rt_flags
& RTF_UP
)) {
4134 rt
->rt_gwroute
= NULL
;
4139 lck_mtx_lock(rnh_lock
);
4140 gwrt
= rtalloc1_scoped_locked(SA(&gw6
), 1, 0,
4145 * Bail out if the route is down, no route
4146 * to gateway, circular route, or if the
4147 * gateway portion of "rt" has changed.
4149 if (!(rt
->rt_flags
& RTF_UP
) ||
4150 gwrt
== NULL
|| gwrt
== rt
||
4151 !equal(SA(&gw6
), rt
->rt_gateway
)) {
4153 RT_REMREF_LOCKED(gwrt
);
4158 rtfree_locked(gwrt
);
4160 lck_mtx_unlock(rnh_lock
);
4161 senderr(EHOSTUNREACH
);
4163 VERIFY(gwrt
!= NULL
);
4165 * Set gateway route; callee adds ref to gwrt;
4166 * gwrt has an extra ref from rtalloc1() for
4169 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
4171 lck_mtx_unlock(rnh_lock
);
4172 /* Remember to release/free "rt" at the end */
4176 RT_ADDREF_LOCKED(gwrt
);
4179 /* Remember to release/free "rt" at the end */
4186 * This is an opportunity to revalidate the parent
4187 * route's gwroute, in case it now points to a dead
4188 * route entry. Parent route won't go away since the
4189 * clone (hint) holds a reference to it. rt == gwrt.
4192 if ((hint
->rt_flags
& (RTF_WASCLONED
| RTF_UP
)) ==
4193 (RTF_WASCLONED
| RTF_UP
)) {
4194 struct rtentry
*prt
= hint
->rt_parent
;
4195 VERIFY(prt
!= NULL
);
4197 RT_CONVERT_LOCK(hint
);
4200 rt_revalidate_gwroute(prt
, rt
);
4207 /* rt == gwrt; if it is now down, give up */
4208 if (!(rt
->rt_flags
& RTF_UP
)) {
4212 /* "rtrele" == original "rt" */
4213 senderr(EHOSTUNREACH
);
4217 /* Become a regular mutex */
4218 RT_CONVERT_LOCK(rt
);
4222 * Address resolution or Neighbor Unreachability Detection
4224 * At this point, the destination of the packet must be a unicast
4225 * or an anycast address(i.e. not a multicast).
4228 /* Look up the neighbor cache for the nexthop */
4229 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0) {
4232 struct sockaddr_in6 sin6
;
4234 * Clear out Scope ID field in case it is set.
4237 sin6
.sin6_scope_id
= 0;
4239 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
4240 * the condition below is not very efficient. But we believe
4241 * it is tolerable, because this should be a rare case.
4242 * Must drop rt_lock since nd6_is_addr_neighbor() calls
4243 * nd6_lookup() and acquires rnh_lock.
4248 if (nd6_is_addr_neighbor(&sin6
, ifp
, 0)) {
4249 /* "rtrele" may have been used, so clean up "rt" now */
4251 /* Don't free "hint0" */
4258 /* Callee returns a locked route upon success */
4259 rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 0);
4261 RT_LOCK_ASSERT_HELD(rt
);
4264 } else if (rt
!= NULL
) {
4273 ndi
= ND_IFINFO(ifp
);
4274 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
4275 lck_mtx_lock(&ndi
->lock
);
4276 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
4277 !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
4278 lck_mtx_unlock(&ndi
->lock
);
4280 "nd6_output: can't allocate llinfo for %s "
4281 "(ln=0x%llx, rt=0x%llx)\n",
4282 ip6_sprintf(&dst
->sin6_addr
),
4283 (uint64_t)VM_KERNEL_ADDRPERM(ln
),
4284 (uint64_t)VM_KERNEL_ADDRPERM(rt
));
4285 senderr(EIO
); /* XXX: good error? */
4287 lck_mtx_unlock(&ndi
->lock
);
4289 goto sendpkt
; /* send anyway */
4292 net_update_uptime();
4293 timenow
= net_uptime();
4295 /* We don't have to do link-layer address resolution on a p2p link. */
4296 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
4297 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
4298 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
);
4299 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
4303 * The first time we send a packet to a neighbor whose entry is
4304 * STALE, we have to change the state to DELAY and a sets a timer to
4305 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
4306 * neighbor unreachability detection on expiration.
4309 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
4311 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_DELAY
);
4312 ln_setexpire(ln
, timenow
+ nd6_delay
);
4313 /* N.B.: we will re-arm the timer below. */
4314 _CASSERT(ND6_LLINFO_DELAY
> ND6_LLINFO_INCOMPLETE
);
4318 * If the neighbor cache entry has a state other than INCOMPLETE
4319 * (i.e. its link-layer address is already resolved), just
4322 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
4325 * Move this entry to the head of the queue so that it is
4326 * less likely for this entry to be a target of forced
4327 * garbage collection (see nd6_rtrequest()). Do this only
4328 * if the entry is non-permanent (as permanent ones will
4329 * never be purged), and if the number of active entries
4330 * is at least half of the threshold.
4332 if (ln
->ln_state
== ND6_LLINFO_DELAY
||
4333 (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
4334 nd6_inuse
>= (ip6_neighborgcthresh
>> 1))) {
4335 lck_mtx_lock(rnh_lock
);
4336 if (ln
->ln_state
== ND6_LLINFO_DELAY
) {
4337 nd6_sched_timeout(NULL
, NULL
);
4339 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
4340 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
4342 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
4348 lck_mtx_unlock(rnh_lock
);
4354 * If this is a prefix proxy route, record the inbound interface
4355 * so that it can be excluded from the list of interfaces eligible
4356 * for forwarding the proxied NS in nd6_prproxy_ns_output().
4358 if (rt
->rt_flags
& RTF_PROXY
) {
4359 ln
->ln_exclifp
= ((origifp
== ifp
) ? NULL
: origifp
);
4363 * There is a neighbor cache entry, but no ethernet address
4364 * response yet. Replace the held mbuf (if any) with this
4367 * This code conforms to the rate-limiting rule described in Section
4368 * 7.2.2 of RFC 4861, because the timer is set correctly after sending
4371 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
) {
4372 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_INCOMPLETE
);
4375 m_freem_list(ln
->ln_hold
);
4378 if (!ND6_LLINFO_PERMANENT(ln
) && ln
->ln_asked
== 0) {
4380 ndi
= ND_IFINFO(ifp
);
4381 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
4382 lck_mtx_lock(&ndi
->lock
);
4383 ln_setexpire(ln
, timenow
+ ndi
->retrans
/ 1000);
4384 lck_mtx_unlock(&ndi
->lock
);
4386 /* We still have a reference on rt (for ln) */
4387 if (ip6_forwarding
) {
4388 nd6_prproxy_ns_output(ifp
, origifp
, NULL
,
4389 &dst
->sin6_addr
, ln
);
4391 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, NULL
);
4393 lck_mtx_lock(rnh_lock
);
4394 nd6_sched_timeout(NULL
, NULL
);
4395 lck_mtx_unlock(rnh_lock
);
4400 * Move this entry to the head of the queue so that it is
4401 * less likely for this entry to be a target of forced
4402 * garbage collection (see nd6_rtrequest()). Do this only
4403 * if the entry is non-permanent (as permanent ones will
4404 * never be purged), and if the number of active entries
4405 * is at least half of the threshold.
4407 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
4408 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
4409 lck_mtx_lock(rnh_lock
);
4411 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
4415 /* Clean up "rt" now while we can */
4417 RT_REMREF_LOCKED(rt
);
4423 rt
= NULL
; /* "rt" has been taken care of */
4424 lck_mtx_unlock(rnh_lock
);
4431 RT_LOCK_ASSERT_NOTHELD(rt
);
4434 /* discard the packet if IPv6 operation is disabled on the interface */
4435 if (ifp
->if_eflags
& IFEF_IPV6_DISABLED
) {
4436 error
= ENETDOWN
; /* better error? */
4440 if (ifp
->if_flags
& IFF_LOOPBACK
) {
4441 /* forwarding rules require the original scope_id */
4442 m0
->m_pkthdr
.rcvif
= origifp
;
4443 error
= dlil_output(origifp
, PF_INET6
, m0
, (caddr_t
)rt
,
4447 /* Do not allow loopback address to wind up on a wire */
4448 struct ip6_hdr
*ip6
= mtod(m0
, struct ip6_hdr
*);
4450 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
4451 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
4452 ip6stat
.ip6s_badscope
++;
4453 error
= EADDRNOTAVAIL
;
4460 /* Mark use timestamp */
4461 if (rt
->rt_llinfo
!= NULL
) {
4462 nd6_llreach_use(rt
->rt_llinfo
);
4467 struct mbuf
*mcur
= m0
;
4468 uint32_t pktcnt
= 0;
4471 if (hint
!= NULL
&& nstat_collect
) {
4474 if ((mcur
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
) &&
4475 (mcur
->m_pkthdr
.tso_segsz
> 0)) {
4476 scnt
= mcur
->m_pkthdr
.len
/ mcur
->m_pkthdr
.tso_segsz
;
4481 nstat_route_tx(hint
, scnt
, mcur
->m_pkthdr
.len
, 0);
4485 mcur
->m_pkthdr
.rcvif
= NULL
;
4486 mcur
= mcur
->m_nextpkt
;
4488 if (pktcnt
> ip6_maxchainsent
) {
4489 ip6_maxchainsent
= pktcnt
;
4491 error
= dlil_output(ifp
, PF_INET6
, m0
, (caddr_t
)rt
, SA(dst
), 0, adv
);
4500 /* Clean up "rt" unless it's already been done */
4504 RT_REMREF_LOCKED(rt
);
4511 /* And now clean up "rtrele" if there is any */
4512 if (rtrele
!= NULL
) {
4513 RT_LOCK_SPIN(rtrele
);
4514 if (rtrele
== hint0
) {
4515 RT_REMREF_LOCKED(rtrele
);
4527 nd6_need_cache(struct ifnet
*ifp
)
4530 * XXX: we currently do not make neighbor cache on any interface
4531 * other than ARCnet, Ethernet, FDDI and GIF.
4534 * - unidirectional tunnels needs no ND
4536 switch (ifp
->if_type
) {
4542 case IFT_IEEE8023ADLAG
:
4546 case IFT_GIF
: /* XXX need more cases? */
4561 nd6_storelladdr(struct ifnet
*ifp
, struct rtentry
*rt
, struct mbuf
*m
,
4562 struct sockaddr
*dst
, u_char
*desten
)
4565 struct sockaddr_dl
*sdl
;
4567 if (m
->m_flags
& M_MCAST
) {
4568 switch (ifp
->if_type
) {
4572 case IFT_IEEE8023ADLAG
:
4577 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
, desten
);
4580 for (i
= 0; i
< ifp
->if_addrlen
; i
++) {
4588 return 0; /* caller will free mbuf */
4593 /* this could happen, if we could not allocate memory */
4594 return 0; /* caller will free mbuf */
4597 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
4598 printf("nd6_storelladdr: something odd happens\n");
4600 return 0; /* caller will free mbuf */
4602 sdl
= SDL(rt
->rt_gateway
);
4603 if (sdl
->sdl_alen
== 0) {
4604 /* this should be impossible, but we bark here for debugging */
4605 printf("nd6_storelladdr: sdl_alen == 0\n");
4607 return 0; /* caller will free mbuf */
4610 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
4616 * This is the ND pre-output routine; care must be taken to ensure that
4617 * the "hint" route never gets freed via rtfree(), since the caller may
4618 * have stored it inside a struct route with a reference held for that
4622 nd6_lookup_ipv6(ifnet_t ifp
, const struct sockaddr_in6
*ip6_dest
,
4623 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
4626 route_t route
= hint
;
4628 struct sockaddr_dl
*sdl
= NULL
;
4631 if (ifp
== NULL
|| ip6_dest
== NULL
) {
4635 if (ip6_dest
->sin6_family
!= AF_INET6
) {
4636 return EAFNOSUPPORT
;
4639 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
4645 * Callee holds a reference on the route and returns
4646 * with the route entry locked, upon success.
4648 result
= route_to_gwroute((const struct sockaddr
*)ip6_dest
,
4653 if (route
!= NULL
) {
4654 RT_LOCK_ASSERT_HELD(route
);
4658 if ((packet
!= NULL
&& (packet
->m_flags
& M_MCAST
) != 0) ||
4659 ((ifp
->if_flags
& IFF_MULTICAST
) &&
4660 IN6_IS_ADDR_MULTICAST(&ip6_dest
->sin6_addr
))) {
4661 if (route
!= NULL
) {
4664 result
= dlil_resolve_multi(ifp
,
4665 (const struct sockaddr
*)ip6_dest
,
4666 SA(ll_dest
), ll_dest_len
);
4667 if (route
!= NULL
) {
4671 } else if (route
== NULL
) {
4674 * For unicast, lookup existing ND6 entries but
4675 * do not trigger a resolution
4677 lck_mtx_lock(rnh_lock
);
4678 route
= rt_lookup(TRUE
,
4679 __DECONST(struct sockaddr
*, ip6_dest
), NULL
,
4680 rt_tables
[AF_INET6
], ifp
->if_index
);
4681 lck_mtx_unlock(rnh_lock
);
4683 if (route
!= NULL
) {
4688 if (route
== NULL
) {
4690 * This could happen, if we could not allocate memory or
4691 * if route_to_gwroute() didn't return a route.
4697 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
4698 nd6log0(error
, "%s: route %s on %s%d gateway address not AF_LINK\n",
4699 __func__
, ip6_sprintf(&ip6_dest
->sin6_addr
),
4700 route
->rt_ifp
->if_name
, route
->rt_ifp
->if_unit
);
4701 result
= EADDRNOTAVAIL
;
4705 sdl
= SDL(route
->rt_gateway
);
4706 if (sdl
->sdl_alen
== 0) {
4707 /* this should be impossible, but we bark here for debugging */
4708 nd6log(error
, "%s: route %s on %s%d sdl_alen == 0\n", __func__
,
4709 ip6_sprintf(&ip6_dest
->sin6_addr
), route
->rt_ifp
->if_name
,
4710 route
->rt_ifp
->if_unit
);
4711 result
= EHOSTUNREACH
;
4715 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
4716 bcopy(sdl
, ll_dest
, copy_len
);
4719 if (route
!= NULL
) {
4720 if (route
== hint
) {
4721 RT_REMREF_LOCKED(route
);
4731 #if (DEVELOPMENT || DEBUG)
4733 static int sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
;
4734 SYSCTL_PROC(_net_inet6_icmp6
, OID_AUTO
, nd6_lookup_ipv6
,
4735 CTLTYPE_STRUCT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0,
4736 sysctl_nd6_lookup_ipv6
, "S", "");
4739 sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
4741 #pragma unused(oidp, arg1, arg2)
4743 struct nd6_lookup_ipv6_args nd6_lookup_ipv6_args
;
4747 * Only root can lookup MAC addresses
4749 error
= proc_suser(current_proc());
4751 nd6log0(error
, "%s: proc_suser() error %d\n",
4755 if (req
->oldptr
== USER_ADDR_NULL
) {
4756 req
->oldidx
= sizeof(struct nd6_lookup_ipv6_args
);
4758 if (req
->newptr
== USER_ADDR_NULL
) {
4761 if (req
->oldlen
!= sizeof(struct nd6_lookup_ipv6_args
) ||
4762 req
->newlen
!= sizeof(struct nd6_lookup_ipv6_args
)) {
4764 nd6log0(error
, "%s: bad req, error %d\n",
4768 error
= SYSCTL_IN(req
, &nd6_lookup_ipv6_args
,
4769 sizeof(struct nd6_lookup_ipv6_args
));
4771 nd6log0(error
, "%s: SYSCTL_IN() error %d\n",
4776 if (nd6_lookup_ipv6_args
.ll_dest_len
> sizeof(nd6_lookup_ipv6_args
.ll_dest_
)) {
4778 nd6log0(error
, "%s: bad ll_dest_len, error %d\n",
4783 /* Make sure to terminate the string */
4784 nd6_lookup_ipv6_args
.ifname
[IFNAMSIZ
- 1] = 0;
4786 error
= ifnet_find_by_name(nd6_lookup_ipv6_args
.ifname
, &ifp
);
4788 nd6log0(error
, "%s: ifnet_find_by_name() error %d\n",
4793 error
= nd6_lookup_ipv6(ifp
, &nd6_lookup_ipv6_args
.ip6_dest
,
4794 &nd6_lookup_ipv6_args
.ll_dest_
._sdl
,
4795 nd6_lookup_ipv6_args
.ll_dest_len
, NULL
, NULL
);
4797 nd6log0(error
, "%s: nd6_lookup_ipv6() error %d\n",
4802 error
= SYSCTL_OUT(req
, &nd6_lookup_ipv6_args
,
4803 sizeof(struct nd6_lookup_ipv6_args
));
4805 nd6log0(error
, "%s: SYSCTL_OUT() error %d\n",
4813 #endif /* (DEVELOPEMENT || DEBUG) */
4816 nd6_setifinfo(struct ifnet
*ifp
, u_int32_t before
, u_int32_t after
)
4822 * Handle ND6_IFF_IFDISABLED
4824 if ((before
& ND6_IFF_IFDISABLED
) ||
4825 (after
& ND6_IFF_IFDISABLED
)) {
4826 b
= (before
& ND6_IFF_IFDISABLED
);
4827 a
= (after
& ND6_IFF_IFDISABLED
);
4829 if (b
!= a
&& (err
= nd6_if_disable(ifp
,
4830 ((int32_t)(a
- b
) > 0))) != 0) {
4836 * Handle ND6_IFF_PROXY_PREFIXES
4838 if ((before
& ND6_IFF_PROXY_PREFIXES
) ||
4839 (after
& ND6_IFF_PROXY_PREFIXES
)) {
4840 b
= (before
& ND6_IFF_PROXY_PREFIXES
);
4841 a
= (after
& ND6_IFF_PROXY_PREFIXES
);
4843 if (b
!= a
&& (err
= nd6_if_prproxy(ifp
,
4844 ((int32_t)(a
- b
) > 0))) != 0) {
4853 * Enable/disable IPv6 on an interface, called as part of
4854 * setting/clearing ND6_IFF_IFDISABLED, or during DAD failure.
4857 nd6_if_disable(struct ifnet
*ifp
, boolean_t enable
)
4860 if_set_eflags(ifp
, IFEF_IPV6_DISABLED
);
4862 if_clear_eflags(ifp
, IFEF_IPV6_DISABLED
);
4869 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
4871 #pragma unused(oidp, arg1, arg2)
4872 char pbuf
[MAX_IPv6_STR_LEN
];
4873 struct nd_defrouter
*dr
;
4876 if (req
->newptr
!= USER_ADDR_NULL
) {
4880 /* XXX Handle mapped defrouter entries */
4881 lck_mtx_lock(nd6_mutex
);
4882 if (proc_is64bit(req
->p
)) {
4883 struct in6_defrouter_64 d
;
4885 bzero(&d
, sizeof(d
));
4886 d
.rtaddr
.sin6_family
= AF_INET6
;
4887 d
.rtaddr
.sin6_len
= sizeof(d
.rtaddr
);
4889 TAILQ_FOREACH(dr
, &nd_defrouter_list
, dr_entry
) {
4890 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
4891 if (in6_recoverscope(&d
.rtaddr
,
4892 &dr
->rtaddr
, dr
->ifp
) != 0) {
4893 log(LOG_ERR
, "scope error in default router "
4894 "list (%s)\n", inet_ntop(AF_INET6
,
4895 &dr
->rtaddr
, pbuf
, sizeof(pbuf
)));
4897 d
.flags
= dr
->flags
;
4898 d
.stateflags
= dr
->stateflags
;
4899 d
.rtlifetime
= (u_short
)dr
->rtlifetime
;
4900 d
.expire
= (int)nddr_getexpire(dr
);
4901 d
.if_index
= dr
->ifp
->if_index
;
4902 error
= SYSCTL_OUT(req
, &d
, sizeof(d
));
4908 struct in6_defrouter_32 d
;
4910 bzero(&d
, sizeof(d
));
4911 d
.rtaddr
.sin6_family
= AF_INET6
;
4912 d
.rtaddr
.sin6_len
= sizeof(d
.rtaddr
);
4914 TAILQ_FOREACH(dr
, &nd_defrouter_list
, dr_entry
) {
4915 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
4916 if (in6_recoverscope(&d
.rtaddr
,
4917 &dr
->rtaddr
, dr
->ifp
) != 0) {
4918 log(LOG_ERR
, "scope error in default router "
4919 "list (%s)\n", inet_ntop(AF_INET6
,
4920 &dr
->rtaddr
, pbuf
, sizeof(pbuf
)));
4922 d
.flags
= dr
->flags
;
4923 d
.stateflags
= dr
->stateflags
;
4924 d
.rtlifetime
= (u_short
)dr
->rtlifetime
;
4925 d
.expire
= (int)nddr_getexpire(dr
);
4926 d
.if_index
= dr
->ifp
->if_index
;
4927 error
= SYSCTL_OUT(req
, &d
, sizeof(d
));
4933 lck_mtx_unlock(nd6_mutex
);
4938 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
4940 #pragma unused(oidp, arg1, arg2)
4941 char pbuf
[MAX_IPv6_STR_LEN
];
4942 struct nd_pfxrouter
*pfr
;
4943 struct sockaddr_in6 s6
;
4944 struct nd_prefix
*pr
;
4947 if (req
->newptr
!= USER_ADDR_NULL
) {
4951 bzero(&s6
, sizeof(s6
));
4952 s6
.sin6_family
= AF_INET6
;
4953 s6
.sin6_len
= sizeof(s6
);
4955 /* XXX Handle mapped defrouter entries */
4956 lck_mtx_lock(nd6_mutex
);
4957 if (proc_is64bit(req
->p
)) {
4958 struct in6_prefix_64 p
;
4960 bzero(&p
, sizeof(p
));
4961 p
.origin
= PR_ORIG_RA
;
4963 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
4965 p
.prefix
= pr
->ndpr_prefix
;
4966 if (in6_recoverscope(&p
.prefix
,
4967 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0) {
4968 log(LOG_ERR
, "scope error in "
4969 "prefix list (%s)\n", inet_ntop(AF_INET6
,
4970 &p
.prefix
.sin6_addr
, pbuf
, sizeof(pbuf
)));
4972 p
.raflags
= pr
->ndpr_raf
;
4973 p
.prefixlen
= pr
->ndpr_plen
;
4974 p
.vltime
= pr
->ndpr_vltime
;
4975 p
.pltime
= pr
->ndpr_pltime
;
4976 p
.if_index
= pr
->ndpr_ifp
->if_index
;
4977 p
.expire
= (u_long
)ndpr_getexpire(pr
);
4978 p
.refcnt
= pr
->ndpr_addrcnt
;
4979 p
.flags
= pr
->ndpr_stateflags
;
4981 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
4983 error
= SYSCTL_OUT(req
, &p
, sizeof(p
));
4988 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
4989 s6
.sin6_addr
= pfr
->router
->rtaddr
;
4990 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
4991 pfr
->router
->ifp
) != 0) {
4993 "scope error in prefix list (%s)\n",
4994 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
4995 pbuf
, sizeof(pbuf
)));
4997 error
= SYSCTL_OUT(req
, &s6
, sizeof(s6
));
5008 struct in6_prefix_32 p
;
5010 bzero(&p
, sizeof(p
));
5011 p
.origin
= PR_ORIG_RA
;
5013 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
5015 p
.prefix
= pr
->ndpr_prefix
;
5016 if (in6_recoverscope(&p
.prefix
,
5017 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0) {
5019 "scope error in prefix list (%s)\n",
5020 inet_ntop(AF_INET6
, &p
.prefix
.sin6_addr
,
5021 pbuf
, sizeof(pbuf
)));
5023 p
.raflags
= pr
->ndpr_raf
;
5024 p
.prefixlen
= pr
->ndpr_plen
;
5025 p
.vltime
= pr
->ndpr_vltime
;
5026 p
.pltime
= pr
->ndpr_pltime
;
5027 p
.if_index
= pr
->ndpr_ifp
->if_index
;
5028 p
.expire
= (u_int32_t
)ndpr_getexpire(pr
);
5029 p
.refcnt
= pr
->ndpr_addrcnt
;
5030 p
.flags
= pr
->ndpr_stateflags
;
5032 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
5034 error
= SYSCTL_OUT(req
, &p
, sizeof(p
));
5039 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
5040 s6
.sin6_addr
= pfr
->router
->rtaddr
;
5041 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
5042 pfr
->router
->ifp
) != 0) {
5044 "scope error in prefix list (%s)\n",
5045 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
5046 pbuf
, sizeof(pbuf
)));
5048 error
= SYSCTL_OUT(req
, &s6
, sizeof(s6
));
5059 lck_mtx_unlock(nd6_mutex
);
5065 in6_ifaddr_set_dadprogress(struct in6_ifaddr
*ia
)
5067 struct ifnet
* ifp
= ia
->ia_ifp
;
5068 uint32_t flags
= IN6_IFF_TENTATIVE
;
5069 uint32_t optdad
= nd6_optimistic_dad
;
5070 struct nd_ifinfo
*ndi
= NULL
;
5072 ndi
= ND_IFINFO(ifp
);
5073 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
5074 if (!(ndi
->flags
& ND6_IFF_DAD
)) {
5079 if (ifp
->if_ipv6_router_mode
== IPV6_ROUTER_MODE_EXCLUSIVE
) {
5082 lck_mtx_lock(&ndi
->lock
);
5083 if ((ndi
->flags
& ND6_IFF_REPLICATED
) != 0) {
5086 lck_mtx_unlock(&ndi
->lock
);
5091 if ((optdad
& ND6_OPTIMISTIC_DAD_LINKLOCAL
) &&
5092 IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
5093 flags
= IN6_IFF_OPTIMISTIC
;
5094 } else if ((optdad
& ND6_OPTIMISTIC_DAD_AUTOCONF
) &&
5095 (ia
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
5096 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
5097 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
) {
5098 flags
= IN6_IFF_OPTIMISTIC
;
5100 } else if (ia
->ia6_flags
& IN6_IFF_SECURED
) {
5101 if (optdad
& ND6_OPTIMISTIC_DAD_SECURED
) {
5102 flags
= IN6_IFF_OPTIMISTIC
;
5106 * Keeping the behavior for temp and CGA
5107 * SLAAC addresses to have a knob for optimistic
5109 * Other than that if ND6_OPTIMISTIC_DAD_AUTOCONF
5110 * is set, we should default to optimistic
5112 * For now this means SLAAC addresses with interface
5113 * identifier derived from modified EUI-64 bit
5116 flags
= IN6_IFF_OPTIMISTIC
;
5118 } else if ((optdad
& ND6_OPTIMISTIC_DAD_DYNAMIC
) &&
5119 (ia
->ia6_flags
& IN6_IFF_DYNAMIC
)) {
5120 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
5121 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
) {
5122 flags
= IN6_IFF_OPTIMISTIC
;
5125 flags
= IN6_IFF_OPTIMISTIC
;
5127 } else if ((optdad
& ND6_OPTIMISTIC_DAD_MANUAL
) &&
5128 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)) {
5131 * Bypass tentative for address assignments
5132 * not covered above (e.g. manual) upon request
5134 if (!IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
) &&
5135 !(ia
->ia6_flags
& IN6_IFF_AUTOCONF
) &&
5136 !(ia
->ia6_flags
& IN6_IFF_DYNAMIC
)) {
5137 flags
= IN6_IFF_OPTIMISTIC
;
5142 ia
->ia6_flags
&= ~(IN6_IFF_DUPLICATED
| IN6_IFF_DADPROGRESS
);
5143 ia
->ia6_flags
|= flags
;
5145 nd6log2(debug
, "%s - %s ifp %s ia6_flags 0x%x\n",
5147 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
5148 if_name(ia
->ia_ifp
),