2 * Copyright (c) 2000-2016 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>
95 #include <netinet/in.h>
96 #include <netinet/in_arp.h>
97 #include <netinet/if_ether.h>
98 #include <netinet6/in6_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/nd6.h>
102 #include <netinet6/scope6_var.h>
103 #include <netinet/icmp6.h>
107 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
108 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
110 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
113 int nd6_prune
= 1; /* walk list every 1 seconds */
114 int nd6_prune_lazy
= 5; /* lazily walk list every 5 seconds */
115 int nd6_delay
= 5; /* delay first probe time 5 second */
116 int nd6_umaxtries
= 3; /* maximum unicast query */
117 int nd6_mmaxtries
= 3; /* maximum multicast query */
118 int nd6_useloopback
= 1; /* use loopback interface for local traffic */
119 int nd6_gctimer
= (60 * 60 * 24); /* 1 day: garbage collection timer */
121 /* preventing too many loops in ND option parsing */
122 int nd6_maxndopt
= 10; /* max # of ND options allowed */
124 int nd6_maxqueuelen
= 1; /* max # of packets cached in unresolved ND entries */
132 int nd6_optimistic_dad
=
133 (ND6_OPTIMISTIC_DAD_LINKLOCAL
|ND6_OPTIMISTIC_DAD_AUTOCONF
|
134 ND6_OPTIMISTIC_DAD_TEMPORARY
|ND6_OPTIMISTIC_DAD_DYNAMIC
|
135 ND6_OPTIMISTIC_DAD_SECURED
|ND6_OPTIMISTIC_DAD_MANUAL
);
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
;
177 struct nd_prhead nd_prefix
= { 0 };
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(int);
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 int nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
;
223 static int nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
;
226 * Insertion and removal from llinfo_nd6 must be done with rnh_lock held.
228 #define LN_DEQUEUE(_ln) do { \
229 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); \
230 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
231 (_ln)->ln_next->ln_prev = (_ln)->ln_prev; \
232 (_ln)->ln_prev->ln_next = (_ln)->ln_next; \
233 (_ln)->ln_prev = (_ln)->ln_next = NULL; \
234 (_ln)->ln_flags &= ~ND6_LNF_IN_USE; \
237 #define LN_INSERTHEAD(_ln) do { \
238 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); \
239 RT_LOCK_ASSERT_HELD((_ln)->ln_rt); \
240 (_ln)->ln_next = llinfo_nd6.ln_next; \
241 llinfo_nd6.ln_next = (_ln); \
242 (_ln)->ln_prev = &llinfo_nd6; \
243 (_ln)->ln_next->ln_prev = (_ln); \
244 (_ln)->ln_flags |= ND6_LNF_IN_USE; \
247 static struct zone
*llinfo_nd6_zone
;
248 #define LLINFO_ND6_ZONE_MAX 256 /* maximum elements in zone */
249 #define LLINFO_ND6_ZONE_NAME "llinfo_nd6" /* name for zone */
251 extern int tvtohz(struct timeval
*);
253 static int nd6_init_done
;
255 SYSCTL_DECL(_net_inet6_icmp6
);
257 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_DRLIST
, nd6_drlist
,
258 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
259 nd6_sysctl_drlist
, "S,in6_defrouter", "");
261 SYSCTL_PROC(_net_inet6_icmp6
, ICMPV6CTL_ND6_PRLIST
, nd6_prlist
,
262 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
263 nd6_sysctl_prlist
, "S,in6_defrouter", "");
265 SYSCTL_DECL(_net_inet6_ip6
);
267 static int ip6_maxchainsent
= 0;
268 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, maxchainsent
,
269 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_maxchainsent
, 0,
270 "use dlil_output_list");
277 VERIFY(!nd6_init_done
);
279 all1_sa
.sin6_family
= AF_INET6
;
280 all1_sa
.sin6_len
= sizeof (struct sockaddr_in6
);
281 for (i
= 0; i
< sizeof (all1_sa
.sin6_addr
); i
++)
282 all1_sa
.sin6_addr
.s6_addr
[i
] = 0xff;
284 /* initialization of the default router list */
285 TAILQ_INIT(&nd_defrouter
);
287 nd_if_lock_grp_attr
= lck_grp_attr_alloc_init();
288 nd_if_lock_grp
= lck_grp_alloc_init("nd_if_lock", nd_if_lock_grp_attr
);
289 nd_if_lock_attr
= lck_attr_alloc_init();
291 llinfo_nd6_zone
= zinit(sizeof (struct llinfo_nd6
),
292 LLINFO_ND6_ZONE_MAX
* sizeof (struct llinfo_nd6
), 0,
293 LLINFO_ND6_ZONE_NAME
);
294 if (llinfo_nd6_zone
== NULL
)
295 panic("%s: failed allocating llinfo_nd6_zone", __func__
);
297 zone_change(llinfo_nd6_zone
, Z_EXPAND
, TRUE
);
298 zone_change(llinfo_nd6_zone
, Z_CALLERACCT
, FALSE
);
307 timeout(nd6_slowtimo
, NULL
, ND6_SLOWTIMER_INTERVAL
* hz
);
310 static struct llinfo_nd6
*
311 nd6_llinfo_alloc(int how
)
313 struct llinfo_nd6
*ln
;
315 ln
= (how
== M_WAITOK
) ? zalloc(llinfo_nd6_zone
) :
316 zalloc_noblock(llinfo_nd6_zone
);
318 bzero(ln
, sizeof (*ln
));
324 nd6_llinfo_free(void *arg
)
326 struct llinfo_nd6
*ln
= arg
;
328 if (ln
->ln_next
!= NULL
|| ln
->ln_prev
!= NULL
) {
329 panic("%s: trying to free %p when it is in use", __func__
, ln
);
333 /* Just in case there's anything there, free it */
334 if (ln
->ln_hold
!= NULL
) {
335 m_freem_list(ln
->ln_hold
);
339 /* Purge any link-layer info caching */
340 VERIFY(ln
->ln_rt
->rt_llinfo
== ln
);
341 if (ln
->ln_rt
->rt_llinfo_purge
!= NULL
)
342 ln
->ln_rt
->rt_llinfo_purge(ln
->ln_rt
);
344 zfree(llinfo_nd6_zone
, ln
);
348 nd6_llinfo_purge(struct rtentry
*rt
)
350 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
352 RT_LOCK_ASSERT_HELD(rt
);
353 VERIFY(rt
->rt_llinfo_purge
== nd6_llinfo_purge
&& ln
!= NULL
);
355 if (ln
->ln_llreach
!= NULL
) {
357 ifnet_llreach_free(ln
->ln_llreach
);
358 ln
->ln_llreach
= NULL
;
364 nd6_llinfo_get_ri(struct rtentry
*rt
, struct rt_reach_info
*ri
)
366 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
367 struct if_llreach
*lr
= ln
->ln_llreach
;
370 bzero(ri
, sizeof (*ri
));
371 ri
->ri_rssi
= IFNET_RSSI_UNKNOWN
;
372 ri
->ri_lqm
= IFNET_LQM_THRESH_OFF
;
373 ri
->ri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
376 /* Export to rt_reach_info structure */
378 /* Export ND6 send expiration (calendar) time */
380 ifnet_llreach_up2calexp(lr
, ln
->ln_lastused
);
386 nd6_llinfo_get_iflri(struct rtentry
*rt
, struct ifnet_llreach_info
*iflri
)
388 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
389 struct if_llreach
*lr
= ln
->ln_llreach
;
392 bzero(iflri
, sizeof (*iflri
));
393 iflri
->iflri_rssi
= IFNET_RSSI_UNKNOWN
;
394 iflri
->iflri_lqm
= IFNET_LQM_THRESH_OFF
;
395 iflri
->iflri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
398 /* Export to ifnet_llreach_info structure */
399 ifnet_lr2iflri(lr
, iflri
);
400 /* Export ND6 send expiration (uptime) time */
401 iflri
->iflri_snd_expire
=
402 ifnet_llreach_up2upexp(lr
, ln
->ln_lastused
);
408 nd6_llinfo_refresh(struct rtentry
*rt
)
410 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
411 uint64_t timenow
= net_uptime();
413 * Can't refresh permanent, static or entries that are
414 * not direct host entries
416 if (!ln
|| ln
->ln_expire
== 0 ||
417 (rt
->rt_flags
& RTF_STATIC
) ||
418 !(rt
->rt_flags
& RTF_LLINFO
)) {
422 if ((ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) &&
423 (ln
->ln_state
< ND6_LLINFO_PROBE
)) {
424 if (ln
->ln_expire
> timenow
) {
425 ln_setexpire(ln
, timenow
);
426 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PROBE
);
433 ndcache_state2str(short ndp_state
)
435 const char *ndp_state_str
= "UNKNOWN";
437 case ND6_LLINFO_PURGE
:
438 ndp_state_str
= "ND6_LLINFO_PURGE";
440 case ND6_LLINFO_NOSTATE
:
441 ndp_state_str
= "ND6_LLINFO_NOSTATE";
443 case ND6_LLINFO_INCOMPLETE
:
444 ndp_state_str
= "ND6_LLINFO_INCOMPLETE";
446 case ND6_LLINFO_REACHABLE
:
447 ndp_state_str
= "ND6_LLINFO_REACHABLE";
449 case ND6_LLINFO_STALE
:
450 ndp_state_str
= "ND6_LLINFO_STALE";
452 case ND6_LLINFO_DELAY
:
453 ndp_state_str
= "ND6_LLINFO_DELAY";
455 case ND6_LLINFO_PROBE
:
456 ndp_state_str
= "ND6_LLINFO_PROBE";
459 /* Init'd to UNKNOWN */
462 return ndp_state_str
;
466 ln_setexpire(struct llinfo_nd6
*ln
, uint64_t expiry
)
468 ln
->ln_expire
= expiry
;
472 ln_getexpire(struct llinfo_nd6
*ln
)
474 struct timeval caltime
;
477 if (ln
->ln_expire
!= 0) {
478 struct rtentry
*rt
= ln
->ln_rt
;
481 /* account for system time change */
482 getmicrotime(&caltime
);
484 rt
->base_calendartime
+=
485 NET_CALCULATE_CLOCKSKEW(caltime
,
486 rt
->base_calendartime
, net_uptime(), rt
->base_uptime
);
488 expiry
= rt
->base_calendartime
+
489 ln
->ln_expire
- rt
->base_uptime
;
497 nd6_ifreset(struct ifnet
*ifp
)
499 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
501 VERIFY(ndi
->initialized
);
503 lck_mtx_assert(&ndi
->lock
, LCK_MTX_ASSERT_OWNED
);
504 ndi
->linkmtu
= ifp
->if_mtu
;
505 ndi
->chlim
= IPV6_DEFHLIM
;
506 ndi
->basereachable
= REACHABLE_TIME
;
507 ndi
->reachable
= ND_COMPUTE_RTIME(ndi
->basereachable
);
508 ndi
->retrans
= RETRANS_TIMER
;
512 nd6_ifattach(struct ifnet
*ifp
)
514 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
517 if (!ndi
->initialized
) {
518 lck_mtx_init(&ndi
->lock
, nd_if_lock_grp
, nd_if_lock_attr
);
519 ndi
->flags
= ND6_IFF_PERFORMNUD
;
520 ndi
->flags
|= ND6_IFF_DAD
;
521 ndi
->initialized
= TRUE
;
524 lck_mtx_lock(&ndi
->lock
);
526 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
527 ndi
->flags
|= ND6_IFF_IFDISABLED
;
531 lck_mtx_unlock(&ndi
->lock
);
538 * XXX Look more into this. Especially since we recycle ifnets and do delayed
542 nd6_ifdetach(struct nd_ifinfo
*nd
)
544 /* XXX destroy nd's lock? */
550 nd6_setmtu(struct ifnet
*ifp
)
552 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
553 u_int32_t oldmaxmtu
, maxmtu
;
555 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)) {
559 lck_mtx_lock(&ndi
->lock
);
560 oldmaxmtu
= ndi
->maxmtu
;
563 * The ND level maxmtu is somewhat redundant to the interface MTU
564 * and is an implementation artifact of KAME. Instead of hard-
565 * limiting the maxmtu based on the interface type here, we simply
566 * take the if_mtu value since SIOCSIFMTU would have taken care of
567 * the sanity checks related to the maximum MTU allowed for the
568 * interface (a value that is known only by the interface layer),
569 * by sending the request down via ifnet_ioctl(). The use of the
570 * ND level maxmtu and linkmtu are done via IN6_LINKMTU() which
571 * does further checking against if_mtu.
573 maxmtu
= ndi
->maxmtu
= ifp
->if_mtu
;
576 * Decreasing the interface MTU under IPV6 minimum MTU may cause
577 * undesirable situation. We thus notify the operator of the change
578 * explicitly. The check for oldmaxmtu is necessary to restrict the
579 * log to the case of changing the MTU, not initializing it.
581 if (oldmaxmtu
>= IPV6_MMTU
&& ndi
->maxmtu
< IPV6_MMTU
) {
582 log(LOG_NOTICE
, "nd6_setmtu: "
583 "new link MTU on %s (%u) is too small for IPv6\n",
584 if_name(ifp
), (uint32_t)ndi
->maxmtu
);
586 ndi
->linkmtu
= ifp
->if_mtu
;
587 lck_mtx_unlock(&ndi
->lock
);
589 /* also adjust in6_maxmtu if necessary. */
590 if (maxmtu
> in6_maxmtu
) {
596 nd6_option_init(void *opt
, int icmp6len
, union nd_opts
*ndopts
)
598 bzero(ndopts
, sizeof (*ndopts
));
599 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)opt
;
600 ndopts
->nd_opts_last
=
601 (struct nd_opt_hdr
*)(((u_char
*)opt
) + icmp6len
);
604 ndopts
->nd_opts_done
= 1;
605 ndopts
->nd_opts_search
= NULL
;
610 * Take one ND option.
613 nd6_option(union nd_opts
*ndopts
)
615 struct nd_opt_hdr
*nd_opt
;
619 panic("ndopts == NULL in nd6_option\n");
620 if (!ndopts
->nd_opts_last
)
621 panic("uninitialized ndopts in nd6_option\n");
622 if (!ndopts
->nd_opts_search
)
624 if (ndopts
->nd_opts_done
)
627 nd_opt
= ndopts
->nd_opts_search
;
629 /* make sure nd_opt_len is inside the buffer */
630 if ((caddr_t
)&nd_opt
->nd_opt_len
>= (caddr_t
)ndopts
->nd_opts_last
) {
631 bzero(ndopts
, sizeof (*ndopts
));
635 olen
= nd_opt
->nd_opt_len
<< 3;
638 * Message validation requires that all included
639 * options have a length that is greater than zero.
641 bzero(ndopts
, sizeof (*ndopts
));
645 ndopts
->nd_opts_search
= (struct nd_opt_hdr
*)((caddr_t
)nd_opt
+ olen
);
646 if (ndopts
->nd_opts_search
> ndopts
->nd_opts_last
) {
647 /* option overruns the end of buffer, invalid */
648 bzero(ndopts
, sizeof (*ndopts
));
650 } else if (ndopts
->nd_opts_search
== ndopts
->nd_opts_last
) {
651 /* reached the end of options chain */
652 ndopts
->nd_opts_done
= 1;
653 ndopts
->nd_opts_search
= NULL
;
659 * Parse multiple ND options.
660 * This function is much easier to use, for ND routines that do not need
661 * multiple options of the same type.
664 nd6_options(union nd_opts
*ndopts
)
666 struct nd_opt_hdr
*nd_opt
;
670 panic("ndopts == NULL in nd6_options");
671 if (ndopts
->nd_opts_last
== NULL
)
672 panic("uninitialized ndopts in nd6_options");
673 if (ndopts
->nd_opts_search
== NULL
)
677 nd_opt
= nd6_option(ndopts
);
678 if (nd_opt
== NULL
&& ndopts
->nd_opts_last
== NULL
) {
680 * Message validation requires that all included
681 * options have a length that is greater than zero.
683 icmp6stat
.icp6s_nd_badopt
++;
684 bzero(ndopts
, sizeof (*ndopts
));
691 switch (nd_opt
->nd_opt_type
) {
692 case ND_OPT_SOURCE_LINKADDR
:
693 case ND_OPT_TARGET_LINKADDR
:
695 case ND_OPT_REDIRECTED_HEADER
:
697 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
]) {
699 "duplicated ND6 option found (type=%d)\n",
700 nd_opt
->nd_opt_type
));
703 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
707 case ND_OPT_PREFIX_INFORMATION
:
708 if (ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] == 0) {
709 ndopts
->nd_opt_array
[nd_opt
->nd_opt_type
] =
712 ndopts
->nd_opts_pi_end
=
713 (struct nd_opt_prefix_info
*)nd_opt
;
720 * Unknown options must be silently ignored,
721 * to accomodate future extension to the protocol.
724 "nd6_options: unsupported option %d - "
725 "option ignored\n", nd_opt
->nd_opt_type
));
730 if (i
> nd6_maxndopt
) {
731 icmp6stat
.icp6s_nd_toomanyopt
++;
732 nd6log((LOG_INFO
, "too many loop in nd opt\n"));
736 if (ndopts
->nd_opts_done
)
753 * ND6 service routine to expire default route list and prefix list
756 nd6_service(void *arg
)
758 struct nd6svc_arg
*ap
= arg
;
759 struct llinfo_nd6
*ln
;
760 struct nd_defrouter
*dr
= NULL
;
761 struct nd_prefix
*pr
= NULL
;
762 struct ifnet
*ifp
= NULL
;
763 struct in6_ifaddr
*ia6
, *nia6
;
765 bool send_nc_failure_kev
= false;
766 struct nd_drhead nd_defrouter_tmp
;
767 struct nd_defrouter
*ndr
= NULL
;
769 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
771 * Since we may drop rnh_lock and nd6_mutex below, we want
772 * to run this entire operation single threaded.
774 while (nd6_service_busy
) {
775 nd6log2((LOG_DEBUG
, "%s: %s is blocked by %d waiters\n",
776 __func__
, ap
->draining
? "drainer" : "timer",
777 nd6_service_waiters
));
778 nd6_service_waiters
++;
779 (void) msleep(nd6_service_wc
, rnh_lock
, (PZERO
-1),
781 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
784 /* We are busy now; tell everyone else to go away */
785 nd6_service_busy
= TRUE
;
788 timenow
= net_uptime();
791 * send_nc_failure_kev gets set when default router's IPv6 address
793 * That can happen either:
794 * 1. When the entry has resolved once but can't be
795 * resolved later and the neighbor cache entry for gateway is deleted
796 * after max probe attempts.
798 * 2. When the entry is in ND6_LLINFO_INCOMPLETE but can not be resolved
799 * after max neighbor address resolution attempts.
801 * Both set send_nc_failure_kev to true. ifp is also set to the previous
802 * neighbor cache entry's route's ifp.
803 * Once we are done sending the notification, set send_nc_failure_kev
804 * to false to stop sending false notifications for non default router
807 * We may to send more information like Gateway's IP that could not be
808 * resolved, however right now we do not install more than one default
809 * route per interface in the routing table.
811 if (send_nc_failure_kev
&& ifp
->if_addrlen
== IF_LLREACH_MAXLEN
) {
812 struct kev_msg ev_msg
;
813 struct kev_nd6_ndfailure nd6_ndfailure
;
814 bzero(&ev_msg
, sizeof(ev_msg
));
815 bzero(&nd6_ndfailure
, sizeof(nd6_ndfailure
));
816 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
817 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
818 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
819 ev_msg
.event_code
= KEV_ND6_NDFAILURE
;
821 nd6_ndfailure
.link_data
.if_family
= ifp
->if_family
;
822 nd6_ndfailure
.link_data
.if_unit
= ifp
->if_unit
;
823 strlcpy(nd6_ndfailure
.link_data
.if_name
,
825 sizeof(nd6_ndfailure
.link_data
.if_name
));
826 ev_msg
.dv
[0].data_ptr
= &nd6_ndfailure
;
827 ev_msg
.dv
[0].data_length
=
828 sizeof(nd6_ndfailure
);
829 dlil_post_complete_msg(NULL
, &ev_msg
);
832 send_nc_failure_kev
= false;
835 * The global list llinfo_nd6 is modified by nd6_request() and is
836 * therefore protected by rnh_lock. For obvious reasons, we cannot
837 * hold rnh_lock across calls that might lead to code paths which
838 * attempt to acquire rnh_lock, else we deadlock. Hence for such
839 * cases we drop rt_lock and rnh_lock, make the calls, and repeat the
840 * loop. To ensure that we don't process the same entry more than
841 * once in a single timeout, we mark the "already-seen" entries with
842 * ND6_LNF_TIMER_SKIP flag. At the end of the loop, we do a second
843 * pass thru the entries and clear the flag so they can be processed
844 * during the next timeout.
846 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
848 ln
= llinfo_nd6
.ln_next
;
849 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
851 struct sockaddr_in6
*dst
;
852 struct llinfo_nd6
*next
;
853 u_int32_t retrans
, flags
;
854 struct nd_ifinfo
*ndi
= NULL
;
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
, ND6_LLINFO_STALE
);
904 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PURGE
);
905 ln_setexpire(ln
, timenow
);
909 * If the entry has not expired, skip it. Take note on the
910 * state, as entries that are in the STALE state are simply
911 * waiting to be garbage collected, in which case we can
912 * relax the callout scheduling (use nd6_prune_lazy).
914 if (ln
->ln_expire
> timenow
) {
915 switch (ln
->ln_state
) {
916 case ND6_LLINFO_STALE
:
928 ndi
= ND_IFINFO(ifp
);
929 VERIFY(ndi
->initialized
);
930 retrans
= ndi
->retrans
;
933 RT_LOCK_ASSERT_HELD(rt
);
935 switch (ln
->ln_state
) {
936 case ND6_LLINFO_INCOMPLETE
:
937 if (ln
->ln_asked
< nd6_mmaxtries
) {
938 struct ifnet
*exclifp
= ln
->ln_exclifp
;
940 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
941 RT_ADDREF_LOCKED(rt
);
943 lck_mtx_unlock(rnh_lock
);
944 if (ip6_forwarding
) {
945 nd6_prproxy_ns_output(ifp
, exclifp
,
946 NULL
, &dst
->sin6_addr
, ln
);
948 nd6_ns_output(ifp
, NULL
,
949 &dst
->sin6_addr
, ln
, NULL
);
953 lck_mtx_lock(rnh_lock
);
955 struct mbuf
*m
= ln
->ln_hold
;
957 send_nc_failure_kev
= (rt
->rt_flags
& RTF_ROUTER
) ? true : false;
959 RT_ADDREF_LOCKED(rt
);
961 lck_mtx_unlock(rnh_lock
);
965 mnext
= m
->m_nextpkt
;
967 m
->m_pkthdr
.rcvif
= ifp
;
968 icmp6_error_flag(m
, ICMP6_DST_UNREACH
,
969 ICMP6_DST_UNREACH_ADDR
, 0, 0);
973 RT_ADDREF_LOCKED(rt
);
975 lck_mtx_unlock(rnh_lock
);
979 lck_mtx_lock(rnh_lock
);
982 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
985 case ND6_LLINFO_REACHABLE
:
986 if (ln
->ln_expire
!= 0) {
987 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
);
988 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
994 case ND6_LLINFO_STALE
:
995 case ND6_LLINFO_PURGE
:
996 /* Garbage Collection(RFC 4861 5.3) */
997 if (ln
->ln_expire
!= 0) {
998 RT_ADDREF_LOCKED(rt
);
1000 lck_mtx_unlock(rnh_lock
);
1003 lck_mtx_lock(rnh_lock
);
1011 case ND6_LLINFO_DELAY
:
1012 if ((flags
& ND6_IFF_PERFORMNUD
) != 0) {
1015 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_PROBE
);
1016 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
1017 RT_ADDREF_LOCKED(rt
);
1019 lck_mtx_unlock(rnh_lock
);
1020 nd6_ns_output(ifp
, &dst
->sin6_addr
,
1021 &dst
->sin6_addr
, ln
, NULL
);
1024 lck_mtx_lock(rnh_lock
);
1027 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
); /* XXX */
1028 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
1033 case ND6_LLINFO_PROBE
:
1034 if (ln
->ln_asked
< nd6_umaxtries
) {
1036 ln_setexpire(ln
, timenow
+ retrans
/ 1000);
1037 RT_ADDREF_LOCKED(rt
);
1039 lck_mtx_unlock(rnh_lock
);
1040 nd6_ns_output(ifp
, &dst
->sin6_addr
,
1041 &dst
->sin6_addr
, ln
, NULL
);
1044 lck_mtx_lock(rnh_lock
);
1046 send_nc_failure_kev
= (rt
->rt_flags
& RTF_ROUTER
) ? true : false;
1047 RT_ADDREF_LOCKED(rt
);
1049 lck_mtx_unlock(rnh_lock
);
1052 lck_mtx_lock(rnh_lock
);
1055 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1064 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1066 /* Now clear the flag from all entries */
1067 ln
= llinfo_nd6
.ln_next
;
1068 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1069 struct rtentry
*rt
= ln
->ln_rt
;
1070 struct llinfo_nd6
*next
= ln
->ln_next
;
1073 if (ln
->ln_flags
& ND6_LNF_TIMER_SKIP
)
1074 ln
->ln_flags
&= ~ND6_LNF_TIMER_SKIP
;
1078 lck_mtx_unlock(rnh_lock
);
1080 /* expire default router list */
1081 TAILQ_INIT(&nd_defrouter_tmp
);
1083 lck_mtx_lock(nd6_mutex
);
1084 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter
, dr_entry
, ndr
) {
1086 if (dr
->expire
!= 0 && dr
->expire
< timenow
) {
1087 if (dr
->ifp
!= NULL
&&
1088 dr
->ifp
->if_type
== IFT_CELLULAR
) {
1090 * Some buggy cellular gateways may not send
1091 * periodic router advertisements.
1092 * Or they may send it with router lifetime
1093 * value that is less than the configured Max and Min
1094 * Router Advertisement interval.
1095 * To top that an idle device may not wake up
1096 * when periodic RA is received on cellular
1098 * We could send RS on every wake but RFC
1099 * 4861 precludes that.
1100 * The addresses are of infinite lifetimes
1101 * and are tied to the lifetime of the bearer,
1102 * so keeping the addresses and just getting rid of
1103 * the router does not help us anyways.
1104 * If there's network renumbering, a lifetime with
1105 * value 0 would remove the default router.
1106 * Also it will get deleted as part of purge when
1107 * the PDP context is torn down and configured again.
1108 * For that reason, do not expire the default router
1109 * learned on cellular interface. Ever.
1111 dr
->expire
+= dr
->rtlifetime
;
1113 "%s: Refreshing expired default router entry "
1114 "%s for interface %s\n", __func__
,
1115 ip6_sprintf(&dr
->rtaddr
), if_name(dr
->ifp
)));
1119 * Remove the entry from default router list
1120 * and add it to the temp list.
1121 * nd_defrouter_tmp will be a local temporary
1122 * list as no one else can get the same
1123 * removed entry once it is removed from default
1125 * Remove the reference after calling defrtrlist_del
1127 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1128 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
1131 if (dr
->expire
== 0 || (dr
->stateflags
& NDDRF_STATIC
))
1139 * Keep the following separate from the above
1140 * iteration of nd_defrouter because it's not safe
1141 * to call defrtrlist_del while iterating global default
1142 * router list. Global list has to be traversed
1143 * while holding nd6_mutex throughout.
1145 * The following call to defrtrlist_del should be
1146 * safe as we are iterating a local list of
1149 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, ndr
) {
1150 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
1152 NDDR_REMREF(dr
); /* remove list reference */
1154 lck_mtx_unlock(nd6_mutex
);
1157 * expire interface addresses.
1158 * in the past the loop was inside prefix expiry processing.
1159 * However, from a stricter speci-confrmance standpoint, we should
1160 * rather separate address lifetimes and prefix lifetimes.
1163 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1164 for (ia6
= in6_ifaddrs
; ia6
; ia6
= nia6
) {
1166 nia6
= ia6
->ia_next
;
1167 IFA_LOCK(&ia6
->ia_ifa
);
1169 * Extra reference for ourselves; it's no-op if
1170 * we don't have to regenerate temporary address,
1171 * otherwise it protects the address from going
1172 * away since we drop in6_ifaddr_rwlock below.
1174 IFA_ADDREF_LOCKED(&ia6
->ia_ifa
);
1175 /* check address lifetime */
1176 if (IFA6_IS_INVALID(ia6
, timenow
)) {
1178 * If the expiring address is temporary, try
1179 * regenerating a new one. This would be useful when
1180 * we suspended a laptop PC, then turned it on after a
1181 * period that could invalidate all temporary
1182 * addresses. Although we may have to restart the
1183 * loop (see below), it must be after purging the
1184 * address. Otherwise, we'd see an infinite loop of
1187 if (ip6_use_tempaddr
&&
1188 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1190 * NOTE: We have to drop the lock here
1191 * because regen_tmpaddr() eventually calls
1192 * in6_update_ifa(), which must take the lock
1193 * and would otherwise cause a hang. This is
1194 * safe because the goto addrloop leads to a
1195 * re-evaluation of the in6_ifaddrs list
1197 IFA_UNLOCK(&ia6
->ia_ifa
);
1198 lck_rw_done(&in6_ifaddr_rwlock
);
1199 (void) regen_tmpaddr(ia6
);
1201 IFA_UNLOCK(&ia6
->ia_ifa
);
1202 lck_rw_done(&in6_ifaddr_rwlock
);
1206 * Purging the address would have caused
1207 * in6_ifaddr_rwlock to be dropped and reacquired;
1208 * therefore search again from the beginning
1209 * of in6_ifaddrs list.
1211 in6_purgeaddr(&ia6
->ia_ifa
);
1214 /* Release extra reference taken above */
1215 IFA_REMREF(&ia6
->ia_ifa
);
1219 * The lazy timer runs every nd6_prune_lazy seconds with at
1220 * most "2 * nd6_prune_lazy - 1" leeway. We consider the worst
1221 * case here and make sure we schedule the regular timer if an
1222 * interface address is about to expire.
1224 if (IFA6_IS_INVALID(ia6
, timenow
+ 3 * nd6_prune_lazy
))
1228 IFA_LOCK_ASSERT_HELD(&ia6
->ia_ifa
);
1229 if (IFA6_IS_DEPRECATED(ia6
, timenow
)) {
1230 int oldflags
= ia6
->ia6_flags
;
1232 ia6
->ia6_flags
|= IN6_IFF_DEPRECATED
;
1235 * If a temporary address has just become deprecated,
1236 * regenerate a new one if possible.
1238 if (ip6_use_tempaddr
&&
1239 (ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1240 (oldflags
& IN6_IFF_DEPRECATED
) == 0) {
1242 /* see NOTE above */
1243 IFA_UNLOCK(&ia6
->ia_ifa
);
1244 lck_rw_done(&in6_ifaddr_rwlock
);
1245 if (regen_tmpaddr(ia6
) == 0) {
1247 * A new temporary address is
1249 * XXX: this means the address chain
1250 * has changed while we are still in
1251 * the loop. Although the change
1252 * would not cause disaster (because
1253 * it's not a deletion, but an
1254 * addition,) we'd rather restart the
1255 * loop just for safety. Or does this
1256 * significantly reduce performance??
1258 /* Release extra reference */
1259 IFA_REMREF(&ia6
->ia_ifa
);
1262 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1264 IFA_UNLOCK(&ia6
->ia_ifa
);
1268 * A new RA might have made a deprecated address
1271 ia6
->ia6_flags
&= ~IN6_IFF_DEPRECATED
;
1272 IFA_UNLOCK(&ia6
->ia_ifa
);
1274 lck_rw_assert(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1275 /* Release extra reference taken above */
1276 IFA_REMREF(&ia6
->ia_ifa
);
1278 lck_rw_done(&in6_ifaddr_rwlock
);
1280 lck_mtx_lock(nd6_mutex
);
1281 /* expire prefix list */
1282 pr
= nd_prefix
.lh_first
;
1283 while (pr
!= NULL
) {
1286 * check prefix lifetime.
1287 * since pltime is just for autoconf, pltime processing for
1288 * prefix is not necessary.
1291 if (pr
->ndpr_stateflags
& NDPRF_PROCESSED_SERVICE
||
1292 pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
1293 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1298 if (pr
->ndpr_expire
!= 0 && pr
->ndpr_expire
< timenow
) {
1300 * address expiration and prefix expiration are
1301 * separate. NEVER perform in6_purgeaddr here.
1303 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1304 NDPR_ADDREF_LOCKED(pr
);
1308 pfxlist_onlink_check();
1309 pr
= nd_prefix
.lh_first
;
1312 if (pr
->ndpr_expire
== 0 ||
1313 (pr
->ndpr_stateflags
& NDPRF_STATIC
))
1317 pr
->ndpr_stateflags
|= NDPRF_PROCESSED_SERVICE
;
1322 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
1324 pr
->ndpr_stateflags
&= ~NDPRF_PROCESSED_SERVICE
;
1327 lck_mtx_unlock(nd6_mutex
);
1329 lck_mtx_lock(rnh_lock
);
1330 /* We're done; let others enter */
1331 nd6_service_busy
= FALSE
;
1332 if (nd6_service_waiters
> 0) {
1333 nd6_service_waiters
= 0;
1334 wakeup(nd6_service_wc
);
1339 static int nd6_need_draining
= 0;
1342 nd6_drain(void *arg
)
1345 nd6log2((LOG_DEBUG
, "%s: draining ND6 entries\n", __func__
));
1347 lck_mtx_lock(rnh_lock
);
1348 nd6_need_draining
= 1;
1349 nd6_sched_timeout(NULL
, NULL
);
1350 lck_mtx_unlock(rnh_lock
);
1354 * We use the ``arg'' variable to decide whether or not the timer we're
1355 * running is the fast timer. We do this to reset the nd6_fast_timer_on
1356 * variable so that later we don't end up ignoring a ``fast timer''
1357 * request if the 5 second timer is running (see nd6_sched_timeout).
1360 nd6_timeout(void *arg
)
1362 struct nd6svc_arg sarg
;
1365 lck_mtx_lock(rnh_lock
);
1366 bzero(&sarg
, sizeof (sarg
));
1367 if (nd6_need_draining
!= 0) {
1368 nd6_need_draining
= 0;
1372 nd6log2((LOG_DEBUG
, "%s: found %u, aging_lazy %u, aging %u, "
1373 "sticky %u, killed %u\n", __func__
, sarg
.found
, sarg
.aging_lazy
,
1374 sarg
.aging
, sarg
.sticky
, sarg
.killed
));
1375 /* re-arm the timer if there's work to do */
1377 VERIFY(nd6_timeout_run
>= 0 && nd6_timeout_run
< 2);
1378 if (arg
== &nd6_fast_timer_on
)
1379 nd6_fast_timer_on
= FALSE
;
1380 if (sarg
.aging_lazy
> 0 || sarg
.aging
> 0 || nd6_sched_timeout_want
) {
1381 struct timeval atv
, ltv
, *leeway
;
1382 int lazy
= nd6_prune_lazy
;
1384 if (sarg
.aging
> 0 || lazy
< 1) {
1386 atv
.tv_sec
= nd6_prune
;
1391 atv
.tv_sec
= MAX(nd6_prune
, lazy
);
1393 read_frandom(&buf
, sizeof(buf
));
1394 ltv
.tv_sec
= MAX(buf
% lazy
, 1) * 2;
1397 nd6_sched_timeout(&atv
, leeway
);
1398 } else if (nd6_debug
) {
1399 nd6log2((LOG_DEBUG
, "%s: not rescheduling timer\n", __func__
));
1401 lck_mtx_unlock(rnh_lock
);
1405 nd6_sched_timeout(struct timeval
*atv
, struct timeval
*ltv
)
1409 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1412 tv
.tv_sec
= MAX(nd6_prune
, 1);
1414 ltv
= NULL
; /* ignore leeway */
1416 /* see comments on top of this file */
1417 if (nd6_timeout_run
== 0) {
1419 nd6log2((LOG_DEBUG
, "%s: timer scheduled in "
1420 "T+%llus.%lluu (demand %d)\n", __func__
,
1421 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1422 nd6_sched_timeout_want
));
1423 nd6_fast_timer_on
= TRUE
;
1424 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1426 nd6log2((LOG_DEBUG
, "%s: timer scheduled in "
1427 "T+%llus.%lluu with %llus.%lluu leeway "
1428 "(demand %d)\n", __func__
, (uint64_t)atv
->tv_sec
,
1429 (uint64_t)atv
->tv_usec
, (uint64_t)ltv
->tv_sec
,
1430 (uint64_t)ltv
->tv_usec
, nd6_sched_timeout_want
));
1431 nd6_fast_timer_on
= FALSE
;
1432 timeout_with_leeway(nd6_timeout
, NULL
,
1433 tvtohz(atv
), tvtohz(ltv
));
1436 nd6_sched_timeout_want
= 0;
1437 } else if (nd6_timeout_run
== 1 && ltv
== NULL
&&
1438 nd6_fast_timer_on
== FALSE
) {
1439 nd6log2((LOG_DEBUG
, "%s: fast timer scheduled in "
1440 "T+%llus.%lluu (demand %d)\n", __func__
,
1441 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1442 nd6_sched_timeout_want
));
1443 nd6_fast_timer_on
= TRUE
;
1444 nd6_sched_timeout_want
= 0;
1446 timeout(nd6_timeout
, &nd6_fast_timer_on
, tvtohz(atv
));
1449 nd6log2((LOG_DEBUG
, "%s: not scheduling timer: "
1450 "timers %d, fast_timer %d, T+%llus.%lluu\n",
1451 __func__
, nd6_timeout_run
, nd6_fast_timer_on
,
1452 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
));
1454 nd6log2((LOG_DEBUG
, "%s: not scheduling timer: "
1455 "timers %d, fast_timer %d, T+%llus.%lluu "
1456 "with %llus.%lluu leeway\n", __func__
,
1457 nd6_timeout_run
, nd6_fast_timer_on
,
1458 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
,
1459 (uint64_t)ltv
->tv_sec
, (uint64_t)ltv
->tv_usec
));
1465 * ND6 router advertisement kernel notification
1468 nd6_post_msg(u_int32_t code
, struct nd_prefix_list
*prefix_list
,
1469 u_int32_t list_length
, u_int32_t mtu
, char *dl_addr
, u_int32_t dl_addr_len
)
1471 struct kev_msg ev_msg
;
1472 struct kev_nd6_ra_data nd6_ra_msg_data
;
1473 struct nd_prefix_list
*itr
= prefix_list
;
1475 bzero(&ev_msg
, sizeof (struct kev_msg
));
1476 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1477 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1478 ev_msg
.kev_subclass
= KEV_ND6_SUBCLASS
;
1479 ev_msg
.event_code
= code
;
1481 bzero(&nd6_ra_msg_data
, sizeof (nd6_ra_msg_data
));
1482 nd6_ra_msg_data
.lladdrlen
= (dl_addr_len
<= ND6_ROUTER_LL_SIZE
) ?
1483 dl_addr_len
: ND6_ROUTER_LL_SIZE
;
1484 bcopy(dl_addr
, &nd6_ra_msg_data
.lladdr
, nd6_ra_msg_data
.lladdrlen
);
1486 if (mtu
> 0 && mtu
>= IPV6_MMTU
) {
1487 nd6_ra_msg_data
.mtu
= mtu
;
1488 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_MTU
;
1491 if (list_length
> 0 && prefix_list
!= NULL
) {
1492 nd6_ra_msg_data
.list_length
= list_length
;
1493 nd6_ra_msg_data
.flags
|= KEV_ND6_DATA_VALID_PREFIX
;
1496 while (itr
!= NULL
&& nd6_ra_msg_data
.list_index
< list_length
) {
1497 bcopy(&itr
->pr
.ndpr_prefix
, &nd6_ra_msg_data
.prefix
.prefix
,
1498 sizeof (nd6_ra_msg_data
.prefix
.prefix
));
1499 nd6_ra_msg_data
.prefix
.raflags
= itr
->pr
.ndpr_raf
;
1500 nd6_ra_msg_data
.prefix
.prefixlen
= itr
->pr
.ndpr_plen
;
1501 nd6_ra_msg_data
.prefix
.origin
= PR_ORIG_RA
;
1502 nd6_ra_msg_data
.prefix
.vltime
= itr
->pr
.ndpr_vltime
;
1503 nd6_ra_msg_data
.prefix
.pltime
= itr
->pr
.ndpr_pltime
;
1504 nd6_ra_msg_data
.prefix
.expire
= ndpr_getexpire(&itr
->pr
);
1505 nd6_ra_msg_data
.prefix
.flags
= itr
->pr
.ndpr_stateflags
;
1506 nd6_ra_msg_data
.prefix
.refcnt
= itr
->pr
.ndpr_addrcnt
;
1507 nd6_ra_msg_data
.prefix
.if_index
= itr
->pr
.ndpr_ifp
->if_index
;
1509 /* send the message up */
1510 ev_msg
.dv
[0].data_ptr
= &nd6_ra_msg_data
;
1511 ev_msg
.dv
[0].data_length
= sizeof (nd6_ra_msg_data
);
1512 ev_msg
.dv
[1].data_length
= 0;
1513 dlil_post_complete_msg(NULL
, &ev_msg
);
1515 /* clean up for the next prefix */
1516 bzero(&nd6_ra_msg_data
.prefix
, sizeof (nd6_ra_msg_data
.prefix
));
1518 nd6_ra_msg_data
.list_index
++;
1523 * Regenerate deprecated/invalidated temporary address
1526 regen_tmpaddr(struct in6_ifaddr
*ia6
)
1530 struct in6_ifaddr
*public_ifa6
= NULL
;
1531 uint64_t timenow
= net_uptime();
1533 ifp
= ia6
->ia_ifa
.ifa_ifp
;
1534 ifnet_lock_shared(ifp
);
1535 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
1536 struct in6_ifaddr
*it6
;
1539 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1543 it6
= (struct in6_ifaddr
*)ifa
;
1545 /* ignore no autoconf addresses. */
1546 if ((it6
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
1550 /* ignore autoconf addresses with different prefixes. */
1551 if (it6
->ia6_ndpr
== NULL
|| it6
->ia6_ndpr
!= ia6
->ia6_ndpr
) {
1556 * Now we are looking at an autoconf address with the same
1557 * prefix as ours. If the address is temporary and is still
1558 * preferred, do not create another one. It would be rare, but
1559 * could happen, for example, when we resume a laptop PC after
1562 if ((it6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1563 !IFA6_IS_DEPRECATED(it6
, timenow
)) {
1565 if (public_ifa6
!= NULL
)
1566 IFA_REMREF(&public_ifa6
->ia_ifa
);
1572 * This is a public autoconf address that has the same prefix
1573 * as ours. If it is preferred, keep it. We can't break the
1574 * loop here, because there may be a still-preferred temporary
1575 * address with the prefix.
1577 if (!IFA6_IS_DEPRECATED(it6
, timenow
)) {
1578 IFA_ADDREF_LOCKED(ifa
); /* for public_ifa6 */
1580 if (public_ifa6
!= NULL
)
1581 IFA_REMREF(&public_ifa6
->ia_ifa
);
1587 ifnet_lock_done(ifp
);
1589 if (public_ifa6
!= NULL
) {
1592 if ((e
= in6_tmpifadd(public_ifa6
, 0)) != 0) {
1593 log(LOG_NOTICE
, "regen_tmpaddr: failed to create a new"
1594 " tmp addr,errno=%d\n", e
);
1595 IFA_REMREF(&public_ifa6
->ia_ifa
);
1598 IFA_REMREF(&public_ifa6
->ia_ifa
);
1606 * Nuke neighbor cache/prefix/default router management table, right before
1610 nd6_purge(struct ifnet
*ifp
)
1612 struct llinfo_nd6
*ln
;
1613 struct nd_defrouter
*dr
, *ndr
;
1614 struct nd_prefix
*pr
, *npr
;
1616 struct nd_drhead nd_defrouter_tmp
;
1618 TAILQ_INIT(&nd_defrouter_tmp
);
1620 /* Nuke default router list entries toward ifp */
1621 lck_mtx_lock(nd6_mutex
);
1622 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter
, dr_entry
, ndr
) {
1626 * Remove the entry from default router list
1627 * and add it to the temp list.
1628 * nd_defrouter_tmp will be a local temporary
1629 * list as no one else can get the same
1630 * removed entry once it is removed from default
1632 * Remove the reference after calling defrtrlist_del.
1634 * The uninstalled entries have to be iterated first
1635 * when we call defrtrlist_del.
1636 * This is to ensure that we don't end up calling
1637 * default router selection when there are other
1638 * uninstalled candidate default routers on
1640 * If we don't respect that order, we may end
1641 * up missing out on some entries.
1643 * For that reason, installed ones must be inserted
1644 * at the tail and uninstalled ones at the head
1646 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
1648 if (dr
->stateflags
& NDDRF_INSTALLED
)
1649 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
1651 TAILQ_INSERT_HEAD(&nd_defrouter_tmp
, dr
, dr_entry
);
1655 * The following call to defrtrlist_del should be
1656 * safe as we are iterating a local list of
1659 * We don't really need nd6_mutex here but keeping
1660 * it as it is to avoid changing assertios held in
1661 * the functions in the call-path.
1663 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, ndr
) {
1664 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
1666 NDDR_REMREF(dr
); /* remove list reference */
1669 /* Nuke prefix list entries toward ifp */
1671 for (pr
= nd_prefix
.lh_first
; pr
; pr
= npr
) {
1673 npr
= pr
->ndpr_next
;
1674 if (pr
->ndpr_ifp
== ifp
&&
1675 !(pr
->ndpr_stateflags
& NDPRF_DEFUNCT
)) {
1677 * Because if_detach() does *not* release prefixes
1678 * while purging addresses the reference count will
1679 * still be above zero. We therefore reset it to
1680 * make sure that the prefix really gets purged.
1682 pr
->ndpr_addrcnt
= 0;
1685 * Previously, pr->ndpr_addr is removed as well,
1686 * but I strongly believe we don't have to do it.
1687 * nd6_purge() is only called from in6_ifdetach(),
1688 * which removes all the associated interface addresses
1690 * (jinmei@kame.net 20010129)
1692 NDPR_ADDREF_LOCKED(pr
);
1697 npr
= nd_prefix
.lh_first
;
1703 pfxlist_onlink_check();
1704 lck_mtx_unlock(nd6_mutex
);
1706 /* cancel default outgoing interface setting */
1707 if (nd6_defifindex
== ifp
->if_index
) {
1708 nd6_setdefaultiface(0);
1712 * Perform default router selection even when we are a router,
1713 * if Scoped Routing is enabled.
1715 lck_mtx_lock(nd6_mutex
);
1716 /* refresh default router list */
1717 defrouter_select(ifp
);
1718 lck_mtx_unlock(nd6_mutex
);
1721 * Nuke neighbor cache entries for the ifp.
1722 * Note that rt->rt_ifp may not be the same as ifp,
1723 * due to KAME goto ours hack. See RTM_RESOLVE case in
1724 * nd6_rtrequest(), and ip6_input().
1727 lck_mtx_lock(rnh_lock
);
1728 ln
= llinfo_nd6
.ln_next
;
1729 while (ln
!= NULL
&& ln
!= &llinfo_nd6
) {
1731 struct llinfo_nd6
*nln
;
1736 if (rt
->rt_gateway
!= NULL
&&
1737 rt
->rt_gateway
->sa_family
== AF_LINK
&&
1738 SDL(rt
->rt_gateway
)->sdl_index
== ifp
->if_index
) {
1739 RT_ADDREF_LOCKED(rt
);
1741 lck_mtx_unlock(rnh_lock
);
1743 * See comments on nd6_service() for reasons why
1744 * this loop is repeated; we bite the costs of
1745 * going thru the same llinfo_nd6 more than once
1746 * here, since this purge happens during detach,
1747 * and that unlike the timer case, it's possible
1748 * there's more than one purges happening at the
1749 * same time (thus a flag wouldn't buy anything).
1753 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
1760 lck_mtx_unlock(rnh_lock
);
1764 * Upon success, the returned route will be locked and the caller is
1765 * responsible for releasing the reference and doing RT_UNLOCK(rt).
1766 * This routine does not require rnh_lock to be held by the caller,
1767 * although it needs to be indicated of such a case in order to call
1768 * the correct variant of the relevant routing routines.
1771 nd6_lookup(struct in6_addr
*addr6
, int create
, struct ifnet
*ifp
, int rt_locked
)
1774 struct sockaddr_in6 sin6
;
1775 unsigned int ifscope
;
1777 bzero(&sin6
, sizeof (sin6
));
1778 sin6
.sin6_len
= sizeof (struct sockaddr_in6
);
1779 sin6
.sin6_family
= AF_INET6
;
1780 sin6
.sin6_addr
= *addr6
;
1782 ifscope
= (ifp
!= NULL
) ? ifp
->if_index
: IFSCOPE_NONE
;
1784 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1785 rt
= rtalloc1_scoped_locked(SA(&sin6
), create
, 0, ifscope
);
1787 rt
= rtalloc1_scoped(SA(&sin6
), create
, 0, ifscope
);
1792 if ((rt
->rt_flags
& RTF_LLINFO
) == 0) {
1794 * This is the case for the default route.
1795 * If we want to create a neighbor cache for the
1796 * address, we should free the route for the
1797 * destination and allocate an interface route.
1810 if (create
&& ifp
) {
1812 u_int32_t ifa_flags
;
1816 * If no route is available and create is set,
1817 * we allocate a host route for the destination
1818 * and treat it like an interface route.
1819 * This hack is necessary for a neighbor which can't
1820 * be covered by our own prefix.
1822 ifa
= ifaof_ifpforaddr(SA(&sin6
), ifp
);
1827 * Create a new route. RTF_LLINFO is necessary
1828 * to create a Neighbor Cache entry for the
1829 * destination in nd6_rtrequest which will be
1830 * called in rtrequest via ifa->ifa_rtrequest.
1833 lck_mtx_lock(rnh_lock
);
1835 ifa_flags
= ifa
->ifa_flags
;
1837 if ((e
= rtrequest_scoped_locked(RTM_ADD
,
1838 SA(&sin6
), ifa
->ifa_addr
, SA(&all1_sa
),
1839 (ifa_flags
| RTF_HOST
| RTF_LLINFO
) &
1840 ~RTF_CLONING
, &rt
, ifscope
)) != 0) {
1842 log(LOG_ERR
, "%s: failed to add route "
1843 "for a neighbor(%s), errno=%d\n",
1844 __func__
, ip6_sprintf(addr6
), e
);
1847 lck_mtx_unlock(rnh_lock
);
1853 if (rt
->rt_llinfo
) {
1854 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
1855 struct nd_ifinfo
*ndi
= ND_IFINFO(rt
->rt_ifp
);
1857 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
1859 * For interface's that do not perform NUD
1860 * neighbor cache entres must always be marked
1861 * reachable with no expiry
1863 if (ndi
->flags
& ND6_IFF_PERFORMNUD
) {
1864 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_NOSTATE
);
1866 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
1867 ln_setexpire(ln
, 0);
1874 RT_LOCK_ASSERT_HELD(rt
);
1876 * Validation for the entry.
1877 * Note that the check for rt_llinfo is necessary because a cloned
1878 * route from a parent route that has the L flag (e.g. the default
1879 * route to a p2p interface) may have the flag, too, while the
1880 * destination is not actually a neighbor.
1881 * XXX: we can't use rt->rt_ifp to check for the interface, since
1882 * it might be the loopback interface if the entry is for our
1883 * own address on a non-loopback interface. Instead, we should
1884 * use rt->rt_ifa->ifa_ifp, which would specify the REAL
1886 * Note also that ifa_ifp and ifp may differ when we connect two
1887 * interfaces to a same link, install a link prefix to an interface,
1888 * and try to install a neighbor cache on an interface that does not
1889 * have a route to the prefix.
1891 * If the address is from a proxied prefix, the ifa_ifp and ifp might
1892 * not match, because nd6_na_input() could have modified the ifp
1893 * of the route to point to the interface where the NA arrived on,
1894 * hence the test for RTF_PROXY.
1896 if ((rt
->rt_flags
& RTF_GATEWAY
) || (rt
->rt_flags
& RTF_LLINFO
) == 0 ||
1897 rt
->rt_gateway
->sa_family
!= AF_LINK
|| rt
->rt_llinfo
== NULL
||
1898 (ifp
&& rt
->rt_ifa
->ifa_ifp
!= ifp
&&
1899 !(rt
->rt_flags
& RTF_PROXY
))) {
1900 RT_REMREF_LOCKED(rt
);
1903 log(LOG_DEBUG
, "%s: failed to lookup %s "
1904 "(if = %s)\n", __func__
, ip6_sprintf(addr6
),
1905 ifp
? if_name(ifp
) : "unspec");
1906 /* xxx more logs... kazu */
1911 * Caller needs to release reference and call RT_UNLOCK(rt).
1917 * Test whether a given IPv6 address is a neighbor or not, ignoring
1918 * the actual neighbor cache. The neighbor cache is ignored in order
1919 * to not reenter the routing code from within itself.
1922 nd6_is_new_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
)
1924 struct nd_prefix
*pr
;
1925 struct ifaddr
*dstaddr
;
1927 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
1930 * A link-local address is always a neighbor.
1931 * XXX: a link does not necessarily specify a single interface.
1933 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
)) {
1934 struct sockaddr_in6 sin6_copy
;
1938 * We need sin6_copy since sa6_recoverscope() may modify the
1942 if (sa6_recoverscope(&sin6_copy
, FALSE
))
1943 return (0); /* XXX: should be impossible */
1944 if (in6_setscope(&sin6_copy
.sin6_addr
, ifp
, &zone
))
1946 if (sin6_copy
.sin6_scope_id
== zone
)
1953 * If the address matches one of our addresses,
1954 * it should be a neighbor.
1955 * If the address matches one of our on-link prefixes, it should be a
1958 for (pr
= nd_prefix
.lh_first
; pr
; pr
= pr
->ndpr_next
) {
1960 if (pr
->ndpr_ifp
!= ifp
) {
1964 if (!(pr
->ndpr_stateflags
& NDPRF_ONLINK
)) {
1968 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr
->ndpr_prefix
.sin6_addr
,
1969 &addr
->sin6_addr
, &pr
->ndpr_mask
)) {
1977 * If the address is assigned on the node of the other side of
1978 * a p2p interface, the address should be a neighbor.
1980 dstaddr
= ifa_ifwithdstaddr(SA(addr
));
1981 if (dstaddr
!= NULL
) {
1982 if (dstaddr
->ifa_ifp
== ifp
) {
1983 IFA_REMREF(dstaddr
);
1986 IFA_REMREF(dstaddr
);
1995 * Detect if a given IPv6 address identifies a neighbor on a given link.
1996 * XXX: should take care of the destination of a p2p link?
1999 nd6_is_addr_neighbor(struct sockaddr_in6
*addr
, struct ifnet
*ifp
,
2004 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2005 lck_mtx_lock(nd6_mutex
);
2006 if (nd6_is_new_addr_neighbor(addr
, ifp
)) {
2007 lck_mtx_unlock(nd6_mutex
);
2010 lck_mtx_unlock(nd6_mutex
);
2013 * Even if the address matches none of our addresses, it might be
2014 * in the neighbor cache.
2016 if ((rt
= nd6_lookup(&addr
->sin6_addr
, 0, ifp
, rt_locked
)) != NULL
) {
2017 RT_LOCK_ASSERT_HELD(rt
);
2018 RT_REMREF_LOCKED(rt
);
2027 * Free an nd6 llinfo entry.
2028 * Since the function would cause significant changes in the kernel, DO NOT
2029 * make it global, unless you have a strong reason for the change, and are sure
2030 * that the change is safe.
2033 nd6_free(struct rtentry
*rt
)
2035 struct llinfo_nd6
*ln
;
2036 struct in6_addr in6
;
2037 struct nd_defrouter
*dr
;
2039 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2040 RT_LOCK_ASSERT_NOTHELD(rt
);
2041 lck_mtx_lock(nd6_mutex
);
2044 RT_ADDREF_LOCKED(rt
); /* Extra ref */
2046 in6
= SIN6(rt_key(rt
))->sin6_addr
;
2049 * Prevent another thread from modifying rt_key, rt_gateway
2050 * via rt_setgate() after the rt_lock is dropped by marking
2051 * the route as defunct.
2053 rt
->rt_flags
|= RTF_CONDEMNED
;
2056 * We used to have pfctlinput(PRC_HOSTDEAD) here. Even though it is
2057 * not harmful, it was not really necessary. Perform default router
2058 * selection even when we are a router, if Scoped Routing is enabled.
2060 dr
= defrouter_lookup(&SIN6(rt_key(rt
))->sin6_addr
, rt
->rt_ifp
);
2062 if ((ln
&& ln
->ln_router
) || dr
) {
2064 * rt6_flush must be called whether or not the neighbor
2065 * is in the Default Router List.
2066 * See a corresponding comment in nd6_na_input().
2069 lck_mtx_unlock(nd6_mutex
);
2070 rt6_flush(&in6
, rt
->rt_ifp
);
2071 lck_mtx_lock(nd6_mutex
);
2079 * Unreachablity of a router might affect the default
2080 * router selection and on-link detection of advertised
2085 * Temporarily fake the state to choose a new default
2086 * router and to perform on-link determination of
2087 * prefixes correctly.
2088 * Below the state will be set correctly,
2089 * or the entry itself will be deleted.
2092 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_INCOMPLETE
);
2095 * Since defrouter_select() does not affect the
2096 * on-link determination and MIP6 needs the check
2097 * before the default router selection, we perform
2101 pfxlist_onlink_check();
2104 * refresh default router list
2106 defrouter_select(rt
->rt_ifp
);
2108 RT_LOCK_ASSERT_NOTHELD(rt
);
2109 lck_mtx_unlock(nd6_mutex
);
2111 * Detach the route from the routing tree and the list of neighbor
2112 * caches, and disable the route entry not to be used in already
2115 (void) rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
), 0, NULL
);
2117 /* Extra ref held above; now free it */
2122 nd6_rtrequest(int req
, struct rtentry
*rt
, struct sockaddr
*sa
)
2125 struct sockaddr
*gate
= rt
->rt_gateway
;
2126 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
2127 static struct sockaddr_dl null_sdl
=
2128 { .sdl_len
= sizeof (null_sdl
), .sdl_family
= AF_LINK
};
2129 struct ifnet
*ifp
= rt
->rt_ifp
;
2132 char buf
[MAX_IPv6_STR_LEN
];
2133 struct nd_ifinfo
*ndi
= ND_IFINFO(rt
->rt_ifp
);
2135 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
2136 VERIFY(nd6_init_done
);
2137 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
2138 RT_LOCK_ASSERT_HELD(rt
);
2141 * We have rnh_lock held, see if we need to schedule the timer;
2142 * we might do this again below during RTM_RESOLVE, but doing it
2143 * now handles all other cases.
2145 if (nd6_sched_timeout_want
)
2146 nd6_sched_timeout(NULL
, NULL
);
2148 if (rt
->rt_flags
& RTF_GATEWAY
)
2151 if (!nd6_need_cache(ifp
) && !(rt
->rt_flags
& RTF_HOST
)) {
2153 * This is probably an interface direct route for a link
2154 * which does not need neighbor caches (e.g. fe80::%lo0/64).
2155 * We do not need special treatment below for such a route.
2156 * Moreover, the RTF_LLINFO flag which would be set below
2157 * would annoy the ndp(8) command.
2162 if (req
== RTM_RESOLVE
) {
2165 if (!nd6_need_cache(ifp
)) { /* stf case */
2168 struct sockaddr_in6 sin6
;
2170 rtkey_to_sa6(rt
, &sin6
);
2172 * nd6_is_addr_neighbor() may call nd6_lookup(),
2173 * therefore we drop rt_lock to avoid deadlock
2174 * during the lookup.
2176 RT_ADDREF_LOCKED(rt
);
2178 no_nd_cache
= !nd6_is_addr_neighbor(&sin6
, ifp
, 1);
2180 RT_REMREF_LOCKED(rt
);
2184 * FreeBSD and BSD/OS often make a cloned host route based
2185 * on a less-specific route (e.g. the default route).
2186 * If the less specific route does not have a "gateway"
2187 * (this is the case when the route just goes to a p2p or an
2188 * stf interface), we'll mistakenly make a neighbor cache for
2189 * the host route, and will see strange neighbor solicitation
2190 * for the corresponding destination. In order to avoid the
2191 * confusion, we check if the destination of the route is
2192 * a neighbor in terms of neighbor discovery, and stop the
2193 * process if not. Additionally, we remove the LLINFO flag
2194 * so that ndp(8) will not try to get the neighbor information
2195 * of the destination.
2198 rt
->rt_flags
&= ~RTF_LLINFO
;
2203 timenow
= net_uptime();
2208 * There is no backward compatibility :)
2210 * if ((rt->rt_flags & RTF_HOST) == 0 &&
2211 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
2212 * rt->rt_flags |= RTF_CLONING;
2214 if ((rt
->rt_flags
& RTF_CLONING
) ||
2215 ((rt
->rt_flags
& RTF_LLINFO
) && ln
== NULL
)) {
2217 * Case 1: This route should come from a route to
2218 * interface (RTF_CLONING case) or the route should be
2219 * treated as on-link but is currently not
2220 * (RTF_LLINFO && ln == NULL case).
2222 if (rt_setgate(rt
, rt_key(rt
), SA(&null_sdl
)) == 0) {
2223 gate
= rt
->rt_gateway
;
2224 SDL(gate
)->sdl_type
= ifp
->if_type
;
2225 SDL(gate
)->sdl_index
= ifp
->if_index
;
2227 * In case we're called before 1.0 sec.
2232 (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
)
2233 ? 0 : MAX(timenow
, 1));
2236 if (rt
->rt_flags
& RTF_CLONING
)
2240 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
2241 * We don't do that here since llinfo is not ready yet.
2243 * There are also couple of other things to be discussed:
2244 * - unsolicited NA code needs improvement beforehand
2245 * - RFC4861 says we MAY send multicast unsolicited NA
2246 * (7.2.6 paragraph 4), however, it also says that we
2247 * SHOULD provide a mechanism to prevent multicast NA storm.
2248 * we don't have anything like it right now.
2249 * note that the mechanism needs a mutual agreement
2250 * between proxies, which means that we need to implement
2251 * a new protocol, or a new kludge.
2252 * - from RFC4861 6.2.4, host MUST NOT send an unsolicited RA.
2253 * we need to check ip6forwarding before sending it.
2254 * (or should we allow proxy ND configuration only for
2255 * routers? there's no mention about proxy ND from hosts)
2259 if (!(ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
))) {
2261 * Address resolution isn't necessary for a point to
2262 * point link, so we can skip this test for a p2p link.
2264 if (gate
->sa_family
!= AF_LINK
||
2265 gate
->sa_len
< sizeof (null_sdl
)) {
2266 /* Don't complain in case of RTM_ADD */
2267 if (req
== RTM_RESOLVE
) {
2268 log(LOG_ERR
, "%s: route to %s has bad "
2269 "gateway address (sa_family %u "
2270 "sa_len %u) on %s\n", __func__
,
2272 &SIN6(rt_key(rt
))->sin6_addr
, buf
,
2273 sizeof (buf
)), gate
->sa_family
,
2274 gate
->sa_len
, if_name(ifp
));
2278 SDL(gate
)->sdl_type
= ifp
->if_type
;
2279 SDL(gate
)->sdl_index
= ifp
->if_index
;
2282 break; /* This happens on a route change */
2284 * Case 2: This route may come from cloning, or a manual route
2285 * add with a LL address.
2287 rt
->rt_llinfo
= ln
= nd6_llinfo_alloc(M_WAITOK
);
2292 rt
->rt_llinfo_get_ri
= nd6_llinfo_get_ri
;
2293 rt
->rt_llinfo_get_iflri
= nd6_llinfo_get_iflri
;
2294 rt
->rt_llinfo_purge
= nd6_llinfo_purge
;
2295 rt
->rt_llinfo_free
= nd6_llinfo_free
;
2296 rt
->rt_llinfo_refresh
= nd6_llinfo_refresh
;
2297 rt
->rt_flags
|= RTF_LLINFO
;
2299 /* this is required for "ndp" command. - shin */
2301 * For interface's that do not perform NUD
2302 * neighbor cache entries must always be marked
2303 * reachable with no expiry
2305 if ((req
== RTM_ADD
) ||
2306 !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
2308 * gate should have some valid AF_LINK entry,
2309 * and ln->ln_expire should have some lifetime
2310 * which is specified by ndp command.
2312 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2313 ln_setexpire(ln
, 0);
2316 * When req == RTM_RESOLVE, rt is created and
2317 * initialized in rtrequest(), so rt_expire is 0.
2319 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_NOSTATE
);
2320 /* In case we're called before 1.0 sec. has elapsed */
2321 ln_setexpire(ln
, (ifp
->if_eflags
& IFEF_IPV6_ND6ALT
) ?
2322 0 : MAX(timenow
, 1));
2327 /* We have at least one entry; arm the timer if not already */
2328 nd6_sched_timeout(NULL
, NULL
);
2331 * If we have too many cache entries, initiate immediate
2332 * purging for some "less recently used" entries. Note that
2333 * we cannot directly call nd6_free() here because it would
2334 * cause re-entering rtable related routines triggering an LOR
2337 if (ip6_neighborgcthresh
> 0 &&
2338 nd6_inuse
>= ip6_neighborgcthresh
) {
2341 for (i
= 0; i
< 10 && llinfo_nd6
.ln_prev
!= ln
; i
++) {
2342 struct llinfo_nd6
*ln_end
= llinfo_nd6
.ln_prev
;
2343 struct rtentry
*rt_end
= ln_end
->ln_rt
;
2345 /* Move this entry to the head */
2348 LN_INSERTHEAD(ln_end
);
2350 if (ln_end
->ln_expire
== 0) {
2354 if (ln_end
->ln_state
> ND6_LLINFO_INCOMPLETE
)
2355 ND6_CACHE_STATE_TRANSITION(ln_end
, ND6_LLINFO_STALE
);
2357 ND6_CACHE_STATE_TRANSITION(ln_end
, ND6_LLINFO_PURGE
);
2358 ln_setexpire(ln_end
, timenow
);
2364 * check if rt_key(rt) is one of my address assigned
2367 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(rt
->rt_ifp
,
2368 &SIN6(rt_key(rt
))->sin6_addr
);
2370 caddr_t macp
= nd6_ifptomac(ifp
);
2371 ln_setexpire(ln
, 0);
2372 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2374 Bcopy(macp
, LLADDR(SDL(gate
)), ifp
->if_addrlen
);
2375 SDL(gate
)->sdl_alen
= ifp
->if_addrlen
;
2377 if (nd6_useloopback
) {
2378 if (rt
->rt_ifp
!= lo_ifp
) {
2380 * Purge any link-layer info caching.
2382 if (rt
->rt_llinfo_purge
!= NULL
)
2383 rt
->rt_llinfo_purge(rt
);
2386 * Adjust route ref count for the
2389 if (rt
->rt_if_ref_fn
!= NULL
) {
2390 rt
->rt_if_ref_fn(lo_ifp
, 1);
2391 rt
->rt_if_ref_fn(rt
->rt_ifp
,
2395 rt
->rt_ifp
= lo_ifp
;
2397 * If rmx_mtu is not locked, update it
2398 * to the MTU used by the new interface.
2400 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
2401 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
2403 * Make sure rt_ifa be equal to the ifaddr
2404 * corresponding to the address.
2405 * We need this because when we refer
2406 * rt_ifa->ia6_flags in ip6_input, we assume
2407 * that the rt_ifa points to the address instead
2408 * of the loopback address.
2410 if (ifa
!= rt
->rt_ifa
) {
2415 } else if (rt
->rt_flags
& RTF_ANNOUNCE
) {
2416 ln_setexpire(ln
, 0);
2417 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_REACHABLE
);
2419 /* join solicited node multicast for proxy ND */
2420 if (ifp
->if_flags
& IFF_MULTICAST
) {
2421 struct in6_addr llsol
;
2422 struct in6_multi
*in6m
;
2425 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2426 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2427 llsol
.s6_addr32
[1] = 0;
2428 llsol
.s6_addr32
[2] = htonl(1);
2429 llsol
.s6_addr8
[12] = 0xff;
2430 if (in6_setscope(&llsol
, ifp
, NULL
))
2432 error
= in6_mc_join(ifp
, &llsol
,
2435 nd6log((LOG_ERR
, "%s: failed to join "
2436 "%s (errno=%d)\n", if_name(ifp
),
2437 ip6_sprintf(&llsol
), error
));
2448 /* leave from solicited node multicast for proxy ND */
2449 if ((rt
->rt_flags
& RTF_ANNOUNCE
) &&
2450 (ifp
->if_flags
& IFF_MULTICAST
)) {
2451 struct in6_addr llsol
;
2452 struct in6_multi
*in6m
;
2454 llsol
= SIN6(rt_key(rt
))->sin6_addr
;
2455 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2456 llsol
.s6_addr32
[1] = 0;
2457 llsol
.s6_addr32
[2] = htonl(1);
2458 llsol
.s6_addr8
[12] = 0xff;
2459 if (in6_setscope(&llsol
, ifp
, NULL
) == 0) {
2460 in6_multihead_lock_shared();
2461 IN6_LOOKUP_MULTI(&llsol
, ifp
, in6m
);
2462 in6_multihead_lock_done();
2464 in6_mc_leave(in6m
, NULL
);
2471 * Unchain it but defer the actual freeing until the route
2472 * itself is to be freed. rt->rt_llinfo still points to
2473 * llinfo_nd6, and likewise, ln->ln_rt stil points to this
2474 * route entry, except that RTF_LLINFO is now cleared.
2476 if (ln
->ln_flags
& ND6_LNF_IN_USE
)
2480 * Purge any link-layer info caching.
2482 if (rt
->rt_llinfo_purge
!= NULL
)
2483 rt
->rt_llinfo_purge(rt
);
2485 rt
->rt_flags
&= ~RTF_LLINFO
;
2486 if (ln
->ln_hold
!= NULL
) {
2487 m_freem_list(ln
->ln_hold
);
2494 nd6_siocgdrlst(void *data
, int data_is_64
)
2496 struct in6_drlist_32
*drl_32
;
2497 struct nd_defrouter
*dr
;
2500 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2502 dr
= TAILQ_FIRST(&nd_defrouter
);
2504 /* XXX Handle mapped defrouter entries */
2505 /* For 64-bit process */
2507 struct in6_drlist_64
*drl_64
;
2509 drl_64
= _MALLOC(sizeof (*drl_64
), M_TEMP
, M_WAITOK
|M_ZERO
);
2513 /* preserve the interface name */
2514 bcopy(data
, drl_64
, sizeof (drl_64
->ifname
));
2516 while (dr
&& i
< DRLSTSIZ
) {
2517 drl_64
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2518 if (IN6_IS_ADDR_LINKLOCAL(
2519 &drl_64
->defrouter
[i
].rtaddr
)) {
2520 /* XXX: need to this hack for KAME stack */
2521 drl_64
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2524 "default router list contains a "
2525 "non-linklocal address(%s)\n",
2526 ip6_sprintf(&drl_64
->defrouter
[i
].rtaddr
));
2528 drl_64
->defrouter
[i
].flags
= dr
->flags
;
2529 drl_64
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2530 drl_64
->defrouter
[i
].expire
= nddr_getexpire(dr
);
2531 drl_64
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2533 dr
= TAILQ_NEXT(dr
, dr_entry
);
2535 bcopy(drl_64
, data
, sizeof (*drl_64
));
2536 _FREE(drl_64
, M_TEMP
);
2540 /* For 32-bit process */
2541 drl_32
= _MALLOC(sizeof (*drl_32
), M_TEMP
, M_WAITOK
|M_ZERO
);
2545 /* preserve the interface name */
2546 bcopy(data
, drl_32
, sizeof (drl_32
->ifname
));
2548 while (dr
!= NULL
&& i
< DRLSTSIZ
) {
2549 drl_32
->defrouter
[i
].rtaddr
= dr
->rtaddr
;
2550 if (IN6_IS_ADDR_LINKLOCAL(&drl_32
->defrouter
[i
].rtaddr
)) {
2551 /* XXX: need to this hack for KAME stack */
2552 drl_32
->defrouter
[i
].rtaddr
.s6_addr16
[1] = 0;
2555 "default router list contains a "
2556 "non-linklocal address(%s)\n",
2557 ip6_sprintf(&drl_32
->defrouter
[i
].rtaddr
));
2559 drl_32
->defrouter
[i
].flags
= dr
->flags
;
2560 drl_32
->defrouter
[i
].rtlifetime
= dr
->rtlifetime
;
2561 drl_32
->defrouter
[i
].expire
= nddr_getexpire(dr
);
2562 drl_32
->defrouter
[i
].if_index
= dr
->ifp
->if_index
;
2564 dr
= TAILQ_NEXT(dr
, dr_entry
);
2566 bcopy(drl_32
, data
, sizeof (*drl_32
));
2567 _FREE(drl_32
, M_TEMP
);
2572 * XXX meaning of fields, especialy "raflags", is very
2573 * differnet between RA prefix list and RR/static prefix list.
2574 * how about separating ioctls into two?
2577 nd6_siocgprlst(void *data
, int data_is_64
)
2579 struct in6_prlist_32
*prl_32
;
2580 struct nd_prefix
*pr
;
2583 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
2585 pr
= nd_prefix
.lh_first
;
2587 /* XXX Handle mapped defrouter entries */
2588 /* For 64-bit process */
2590 struct in6_prlist_64
*prl_64
;
2592 prl_64
= _MALLOC(sizeof (*prl_64
), M_TEMP
, M_WAITOK
|M_ZERO
);
2596 /* preserve the interface name */
2597 bcopy(data
, prl_64
, sizeof (prl_64
->ifname
));
2599 while (pr
&& i
< PRLSTSIZ
) {
2600 struct nd_pfxrouter
*pfr
;
2604 (void) in6_embedscope(&prl_64
->prefix
[i
].prefix
,
2605 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2606 prl_64
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2607 prl_64
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2608 prl_64
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2609 prl_64
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2610 prl_64
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2611 prl_64
->prefix
[i
].expire
= ndpr_getexpire(pr
);
2613 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2617 #define RTRADDR prl_64->prefix[i].advrtr[j]
2618 RTRADDR
= pfr
->router
->rtaddr
;
2619 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2620 /* XXX: hack for KAME */
2621 RTRADDR
.s6_addr16
[1] = 0;
2624 "a router(%s) advertises "
2626 "non-link local address\n",
2627 ip6_sprintf(&RTRADDR
));
2632 pfr
= pfr
->pfr_next
;
2634 prl_64
->prefix
[i
].advrtrs
= j
;
2635 prl_64
->prefix
[i
].origin
= PR_ORIG_RA
;
2641 bcopy(prl_64
, data
, sizeof (*prl_64
));
2642 _FREE(prl_64
, M_TEMP
);
2646 /* For 32-bit process */
2647 prl_32
= _MALLOC(sizeof (*prl_32
), M_TEMP
, M_WAITOK
|M_ZERO
);
2651 /* preserve the interface name */
2652 bcopy(data
, prl_32
, sizeof (prl_32
->ifname
));
2654 while (pr
&& i
< PRLSTSIZ
) {
2655 struct nd_pfxrouter
*pfr
;
2659 (void) in6_embedscope(&prl_32
->prefix
[i
].prefix
,
2660 &pr
->ndpr_prefix
, NULL
, NULL
, NULL
);
2661 prl_32
->prefix
[i
].raflags
= pr
->ndpr_raf
;
2662 prl_32
->prefix
[i
].prefixlen
= pr
->ndpr_plen
;
2663 prl_32
->prefix
[i
].vltime
= pr
->ndpr_vltime
;
2664 prl_32
->prefix
[i
].pltime
= pr
->ndpr_pltime
;
2665 prl_32
->prefix
[i
].if_index
= pr
->ndpr_ifp
->if_index
;
2666 prl_32
->prefix
[i
].expire
= ndpr_getexpire(pr
);
2668 pfr
= pr
->ndpr_advrtrs
.lh_first
;
2672 #define RTRADDR prl_32->prefix[i].advrtr[j]
2673 RTRADDR
= pfr
->router
->rtaddr
;
2674 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR
)) {
2675 /* XXX: hack for KAME */
2676 RTRADDR
.s6_addr16
[1] = 0;
2679 "a router(%s) advertises "
2681 "non-link local address\n",
2682 ip6_sprintf(&RTRADDR
));
2687 pfr
= pfr
->pfr_next
;
2689 prl_32
->prefix
[i
].advrtrs
= j
;
2690 prl_32
->prefix
[i
].origin
= PR_ORIG_RA
;
2696 bcopy(prl_32
, data
, sizeof (*prl_32
));
2697 _FREE(prl_32
, M_TEMP
);
2702 nd6_ioctl(u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
2704 struct nd_defrouter
*dr
;
2705 struct nd_prefix
*pr
;
2709 VERIFY(ifp
!= NULL
);
2712 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
2713 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
2715 * obsolete API, use sysctl under net.inet6.icmp6
2717 lck_mtx_lock(nd6_mutex
);
2718 error
= nd6_siocgdrlst(data
, cmd
== SIOCGDRLST_IN6_64
);
2719 lck_mtx_unlock(nd6_mutex
);
2722 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
2723 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
2725 * obsolete API, use sysctl under net.inet6.icmp6
2727 lck_mtx_lock(nd6_mutex
);
2728 error
= nd6_siocgprlst(data
, cmd
== SIOCGPRLST_IN6_64
);
2729 lck_mtx_unlock(nd6_mutex
);
2732 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
2733 case SIOCGIFINFO_IN6
: { /* struct in6_ondireq */
2735 struct in6_ondireq
*ondi
= (struct in6_ondireq
*)(void *)data
;
2736 struct nd_ifinfo
*ndi
;
2738 * SIOCGIFINFO_IN6 ioctl is encoded with in6_ondireq
2739 * instead of in6_ndireq, so we treat it as such.
2741 ndi
= ND_IFINFO(ifp
);
2742 if ((NULL
== ndi
) || (FALSE
== ndi
->initialized
)){
2746 lck_mtx_lock(&ndi
->lock
);
2747 linkmtu
= IN6_LINKMTU(ifp
);
2748 bcopy(&linkmtu
, &ondi
->ndi
.linkmtu
, sizeof (linkmtu
));
2749 bcopy(&ndi
->maxmtu
, &ondi
->ndi
.maxmtu
,
2750 sizeof (u_int32_t
));
2751 bcopy(&ndi
->basereachable
, &ondi
->ndi
.basereachable
,
2752 sizeof (u_int32_t
));
2753 bcopy(&ndi
->reachable
, &ondi
->ndi
.reachable
,
2754 sizeof (u_int32_t
));
2755 bcopy(&ndi
->retrans
, &ondi
->ndi
.retrans
,
2756 sizeof (u_int32_t
));
2757 bcopy(&ndi
->flags
, &ondi
->ndi
.flags
,
2758 sizeof (u_int32_t
));
2759 bcopy(&ndi
->recalctm
, &ondi
->ndi
.recalctm
,
2761 ondi
->ndi
.chlim
= ndi
->chlim
;
2762 ondi
->ndi
.receivedra
= 0;
2763 lck_mtx_unlock(&ndi
->lock
);
2767 case SIOCSIFINFO_FLAGS
: { /* struct in6_ndireq */
2769 * XXX BSD has a bunch of checks here to ensure
2770 * that interface disabled flag is not reset if
2771 * link local address has failed DAD.
2772 * Investigate that part.
2774 struct in6_ndireq
*cndi
= (struct in6_ndireq
*)(void *)data
;
2775 u_int32_t oflags
, flags
;
2776 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
2778 /* XXX: almost all other fields of cndi->ndi is unused */
2779 if ((NULL
== ndi
) || !ndi
->initialized
) {
2784 lck_mtx_lock(&ndi
->lock
);
2785 oflags
= ndi
->flags
;
2786 bcopy(&cndi
->ndi
.flags
, &(ndi
->flags
), sizeof (flags
));
2788 lck_mtx_unlock(&ndi
->lock
);
2790 if (oflags
== flags
) {
2794 error
= nd6_setifinfo(ifp
, oflags
, flags
);
2798 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
2799 /* flush default router list */
2801 * xxx sumikawa: should not delete route if default
2802 * route equals to the top of default router list
2804 lck_mtx_lock(nd6_mutex
);
2806 defrouter_select(ifp
);
2807 lck_mtx_unlock(nd6_mutex
);
2808 /* xxx sumikawa: flush prefix list */
2811 case SIOCSPFXFLUSH_IN6
: { /* struct in6_ifreq */
2812 /* flush all the prefix advertised by routers */
2813 struct nd_prefix
*next
= NULL
;
2815 lck_mtx_lock(nd6_mutex
);
2816 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
2817 struct in6_ifaddr
*ia
= NULL
;
2818 bool iterate_pfxlist_again
= false;
2820 next
= pr
->ndpr_next
;
2823 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
2827 if (ifp
!= lo_ifp
&& pr
->ndpr_ifp
!= ifp
) {
2831 /* do we really have to remove addresses as well? */
2832 NDPR_ADDREF_LOCKED(pr
);
2834 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2836 while (ia
!= NULL
) {
2837 IFA_LOCK(&ia
->ia_ifa
);
2838 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0) {
2839 IFA_UNLOCK(&ia
->ia_ifa
);
2844 if (ia
->ia6_ndpr
== pr
) {
2845 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
2846 IFA_UNLOCK(&ia
->ia_ifa
);
2847 lck_rw_done(&in6_ifaddr_rwlock
);
2848 lck_mtx_unlock(nd6_mutex
);
2849 in6_purgeaddr(&ia
->ia_ifa
);
2850 IFA_REMREF(&ia
->ia_ifa
);
2851 lck_mtx_lock(nd6_mutex
);
2852 lck_rw_lock_exclusive(
2853 &in6_ifaddr_rwlock
);
2855 * Purging the address caused
2856 * in6_ifaddr_rwlock to be
2858 * reacquired; therefore search again
2859 * from the beginning of in6_ifaddrs.
2860 * The same applies for the prefix list.
2863 iterate_pfxlist_again
= true;
2866 IFA_UNLOCK(&ia
->ia_ifa
);
2869 lck_rw_done(&in6_ifaddr_rwlock
);
2873 pfxlist_onlink_check();
2875 if (iterate_pfxlist_again
) {
2876 next
= nd_prefix
.lh_first
;
2879 lck_mtx_unlock(nd6_mutex
);
2883 case SIOCSRTRFLUSH_IN6
: { /* struct in6_ifreq */
2884 /* flush all the default routers */
2885 struct nd_defrouter
*next
;
2886 struct nd_drhead nd_defrouter_tmp
;
2888 TAILQ_INIT(&nd_defrouter_tmp
);
2889 lck_mtx_lock(nd6_mutex
);
2890 if ((dr
= TAILQ_FIRST(&nd_defrouter
)) != NULL
) {
2892 * The first entry of the list may be stored in
2893 * the routing table, so we'll delete it later.
2895 for (dr
= TAILQ_NEXT(dr
, dr_entry
); dr
; dr
= next
) {
2896 next
= TAILQ_NEXT(dr
, dr_entry
);
2897 if (ifp
== lo_ifp
|| dr
->ifp
== ifp
) {
2899 * Remove the entry from default router list
2900 * and add it to the temp list.
2901 * nd_defrouter_tmp will be a local temporary
2902 * list as no one else can get the same
2903 * removed entry once it is removed from default
2905 * Remove the reference after calling defrtrlist_de
2907 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
2908 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
2912 dr
= TAILQ_FIRST(&nd_defrouter
);
2913 if (ifp
== lo_ifp
||
2915 TAILQ_REMOVE(&nd_defrouter
, dr
, dr_entry
);
2916 TAILQ_INSERT_TAIL(&nd_defrouter_tmp
, dr
, dr_entry
);
2921 * Keep the following separate from the above iteration of
2922 * nd_defrouter because it's not safe to call
2923 * defrtrlist_del while iterating global default
2924 * router list. Global list has to be traversed
2925 * while holding nd6_mutex throughout.
2927 * The following call to defrtrlist_del should be
2928 * safe as we are iterating a local list of
2931 TAILQ_FOREACH_SAFE(dr
, &nd_defrouter_tmp
, dr_entry
, next
) {
2932 TAILQ_REMOVE(&nd_defrouter_tmp
, dr
, dr_entry
);
2934 NDDR_REMREF(dr
); /* remove list reference */
2936 lck_mtx_unlock(nd6_mutex
);
2940 case SIOCGNBRINFO_IN6_32
: { /* struct in6_nbrinfo_32 */
2941 struct llinfo_nd6
*ln
;
2942 struct in6_nbrinfo_32 nbi_32
;
2943 struct in6_addr nb_addr
; /* make local for safety */
2945 bcopy(data
, &nbi_32
, sizeof (nbi_32
));
2946 nb_addr
= nbi_32
.addr
;
2948 * XXX: KAME specific hack for scoped addresses
2949 * XXXX: for other scopes than link-local?
2951 if (IN6_IS_ADDR_LINKLOCAL(&nbi_32
.addr
) ||
2952 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_32
.addr
)) {
2954 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
2957 *idp
= htons(ifp
->if_index
);
2960 /* Callee returns a locked route upon success */
2961 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
2965 RT_LOCK_ASSERT_HELD(rt
);
2967 nbi_32
.state
= ln
->ln_state
;
2968 nbi_32
.asked
= ln
->ln_asked
;
2969 nbi_32
.isrouter
= ln
->ln_router
;
2970 nbi_32
.expire
= ln_getexpire(ln
);
2971 RT_REMREF_LOCKED(rt
);
2973 bcopy(&nbi_32
, data
, sizeof (nbi_32
));
2977 case SIOCGNBRINFO_IN6_64
: { /* struct in6_nbrinfo_64 */
2978 struct llinfo_nd6
*ln
;
2979 struct in6_nbrinfo_64 nbi_64
;
2980 struct in6_addr nb_addr
; /* make local for safety */
2982 bcopy(data
, &nbi_64
, sizeof (nbi_64
));
2983 nb_addr
= nbi_64
.addr
;
2985 * XXX: KAME specific hack for scoped addresses
2986 * XXXX: for other scopes than link-local?
2988 if (IN6_IS_ADDR_LINKLOCAL(&nbi_64
.addr
) ||
2989 IN6_IS_ADDR_MC_LINKLOCAL(&nbi_64
.addr
)) {
2991 (u_int16_t
*)(void *)&nb_addr
.s6_addr
[2];
2994 *idp
= htons(ifp
->if_index
);
2997 /* Callee returns a locked route upon success */
2998 if ((rt
= nd6_lookup(&nb_addr
, 0, ifp
, 0)) == NULL
) {
3002 RT_LOCK_ASSERT_HELD(rt
);
3004 nbi_64
.state
= ln
->ln_state
;
3005 nbi_64
.asked
= ln
->ln_asked
;
3006 nbi_64
.isrouter
= ln
->ln_router
;
3007 nbi_64
.expire
= ln_getexpire(ln
);
3008 RT_REMREF_LOCKED(rt
);
3010 bcopy(&nbi_64
, data
, sizeof (nbi_64
));
3014 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
3015 case SIOCGDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
3016 struct in6_ndifreq_64
*ndif_64
=
3017 (struct in6_ndifreq_64
*)(void *)data
;
3018 struct in6_ndifreq_32
*ndif_32
=
3019 (struct in6_ndifreq_32
*)(void *)data
;
3021 if (cmd
== SIOCGDEFIFACE_IN6_64
) {
3022 u_int64_t j
= nd6_defifindex
;
3023 bcopy(&j
, &ndif_64
->ifindex
, sizeof (j
));
3025 bcopy(&nd6_defifindex
, &ndif_32
->ifindex
,
3026 sizeof (u_int32_t
));
3031 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
3032 case SIOCSDEFIFACE_IN6_64
: { /* struct in6_ndifreq_64 */
3033 struct in6_ndifreq_64
*ndif_64
=
3034 (struct in6_ndifreq_64
*)(void *)data
;
3035 struct in6_ndifreq_32
*ndif_32
=
3036 (struct in6_ndifreq_32
*)(void *)data
;
3039 if (cmd
== SIOCSDEFIFACE_IN6_64
) {
3041 bcopy(&ndif_64
->ifindex
, &j
, sizeof (j
));
3044 bcopy(&ndif_32
->ifindex
, &idx
, sizeof (idx
));
3047 error
= nd6_setdefaultiface(idx
);
3051 case SIOCGIFCGAPREP_IN6
:
3052 case SIOCSIFCGAPREP_IN6
:
3054 struct in6_cgareq
*p_cgareq
=
3055 (struct in6_cgareq
*)(void *)data
;
3056 struct nd_ifinfo
*ndi
= ND_IFINFO(ifp
);
3058 struct in6_cga_modifier
*req_cga_mod
=
3059 &(p_cgareq
->cgar_cgaprep
.cga_modifier
);
3060 struct in6_cga_modifier
*ndi_cga_mod
= NULL
;
3062 if ((NULL
== ndi
) || !ndi
->initialized
) {
3067 lck_mtx_lock(&ndi
->lock
);
3068 ndi_cga_mod
= &(ndi
->local_cga_modifier
);
3070 if (cmd
== SIOCSIFCGAPREP_IN6
) {
3071 bcopy(req_cga_mod
, ndi_cga_mod
, sizeof(*ndi_cga_mod
));
3072 ndi
->cga_initialized
= TRUE
;
3074 bcopy(ndi_cga_mod
, req_cga_mod
, sizeof(*req_cga_mod
));
3076 lck_mtx_unlock(&ndi
->lock
);
3085 * Create neighbor cache entry and cache link-layer address,
3086 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
3089 nd6_cache_lladdr(struct ifnet
*ifp
, struct in6_addr
*from
, char *lladdr
,
3090 int lladdrlen
, int type
, int code
)
3092 #pragma unused(lladdrlen)
3093 struct rtentry
*rt
= NULL
;
3094 struct llinfo_nd6
*ln
= NULL
;
3096 struct sockaddr_dl
*sdl
= NULL
;
3102 boolean_t sched_timeout
= FALSE
;
3103 struct nd_ifinfo
*ndi
= NULL
;
3106 panic("ifp == NULL in nd6_cache_lladdr");
3108 panic("from == NULL in nd6_cache_lladdr");
3110 /* nothing must be updated for unspecified address */
3111 if (IN6_IS_ADDR_UNSPECIFIED(from
))
3115 * Validation about ifp->if_addrlen and lladdrlen must be done in
3118 timenow
= net_uptime();
3120 rt
= nd6_lookup(from
, 0, ifp
, 0);
3122 if ((rt
= nd6_lookup(from
, 1, ifp
, 0)) == NULL
)
3124 RT_LOCK_ASSERT_HELD(rt
);
3127 RT_LOCK_ASSERT_HELD(rt
);
3128 /* do nothing if static ndp is set */
3129 if (rt
->rt_flags
& RTF_STATIC
) {
3130 RT_REMREF_LOCKED(rt
);
3139 if ((rt
->rt_flags
& (RTF_GATEWAY
| RTF_LLINFO
)) != RTF_LLINFO
) {
3146 ln
= (struct llinfo_nd6
*)rt
->rt_llinfo
;
3149 if (rt
->rt_gateway
== NULL
)
3151 if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
3153 sdl
= SDL(rt
->rt_gateway
);
3155 olladdr
= (sdl
->sdl_alen
) ? 1 : 0;
3156 if (olladdr
&& lladdr
) {
3157 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
3165 * newentry olladdr lladdr llchange (*=record)
3168 * 0 n y -- (3) * STALE
3170 * 0 y y y (5) * STALE
3171 * 1 -- n -- (6) NOSTATE(= PASSIVE)
3172 * 1 -- y -- (7) * STALE
3175 if (lladdr
) { /* (3-5) and (7) */
3177 * Record source link-layer address
3178 * XXX is it dependent to ifp->if_type?
3180 sdl
->sdl_alen
= ifp
->if_addrlen
;
3181 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
3183 /* cache the gateway (sender HW) address */
3184 nd6_llreach_alloc(rt
, ifp
, LLADDR(sdl
), sdl
->sdl_alen
, FALSE
);
3188 if ((!olladdr
&& lladdr
!= NULL
) || /* (3) */
3189 (olladdr
&& lladdr
!= NULL
&& llchange
)) { /* (5) */
3191 newstate
= ND6_LLINFO_STALE
;
3192 } else /* (1-2,4) */
3196 if (lladdr
== NULL
) /* (6) */
3197 newstate
= ND6_LLINFO_NOSTATE
;
3199 newstate
= ND6_LLINFO_STALE
;
3203 * For interface's that do not perform NUD
3204 * neighbor cache entres must always be marked
3205 * reachable with no expiry
3207 ndi
= ND_IFINFO(ifp
);
3208 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3210 if (ndi
&& !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
3211 newstate
= ND6_LLINFO_REACHABLE
;
3212 ln_setexpire(ln
, 0);
3217 * Update the state of the neighbor cache.
3219 ND6_CACHE_STATE_TRANSITION(ln
, newstate
);
3221 if ((ln
->ln_state
== ND6_LLINFO_STALE
) ||
3222 (ln
->ln_state
== ND6_LLINFO_REACHABLE
)) {
3223 struct mbuf
*m
= ln
->ln_hold
;
3225 * XXX: since nd6_output() below will cause
3226 * state tansition to DELAY and reset the timer,
3227 * we must set the timer now, although it is actually
3230 if (ln
->ln_state
== ND6_LLINFO_STALE
)
3231 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
3235 struct sockaddr_in6 sin6
;
3237 rtkey_to_sa6(rt
, &sin6
);
3239 * we assume ifp is not a p2p here, so just
3240 * set the 2nd argument as the 1st one.
3243 nd6_output_list(ifp
, ifp
, m
, &sin6
, rt
, NULL
);
3246 } else if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
3247 /* probe right away */
3248 ln_setexpire(ln
, timenow
);
3249 sched_timeout
= TRUE
;
3254 * ICMP6 type dependent behavior.
3256 * NS: clear IsRouter if new entry
3257 * RS: clear IsRouter
3258 * RA: set IsRouter if there's lladdr
3259 * redir: clear IsRouter if new entry
3262 * The spec says that we must set IsRouter in the following cases:
3263 * - If lladdr exist, set IsRouter. This means (1-5).
3264 * - If it is old entry (!newentry), set IsRouter. This means (7).
3265 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
3266 * A quetion arises for (1) case. (1) case has no lladdr in the
3267 * neighbor cache, this is similar to (6).
3268 * This case is rare but we figured that we MUST NOT set IsRouter.
3270 * newentry olladdr lladdr llchange NS RS RA redir
3272 * 0 n n -- (1) c ? s
3273 * 0 y n -- (2) c s s
3274 * 0 n y -- (3) c s s
3277 * 1 -- n -- (6) c c c s
3278 * 1 -- y -- (7) c c s c s
3282 switch (type
& 0xff) {
3283 case ND_NEIGHBOR_SOLICIT
:
3285 * New entry must have is_router flag cleared.
3287 if (is_newentry
) /* (6-7) */
3292 * If the ICMP message is a Redirect to a better router, always
3293 * set the is_router flag. Otherwise, if the entry is newly
3294 * created, then clear the flag. [RFC 4861, sec 8.3]
3296 if (code
== ND_REDIRECT_ROUTER
)
3298 else if (is_newentry
) /* (6-7) */
3301 case ND_ROUTER_SOLICIT
:
3303 * is_router flag must always be cleared.
3307 case ND_ROUTER_ADVERT
:
3309 * Mark an entry with lladdr as a router.
3311 if ((!is_newentry
&& (olladdr
|| lladdr
)) || /* (2-5) */
3312 (is_newentry
&& lladdr
)) { /* (7) */
3319 * When the link-layer address of a router changes, select the
3320 * best router again. In particular, when the neighbor entry is newly
3321 * created, it might affect the selection policy.
3322 * Question: can we restrict the first condition to the "is_newentry"
3325 * Note: Perform default router selection even when we are a router,
3326 * if Scoped Routing is enabled.
3328 if (do_update
&& ln
->ln_router
) {
3329 RT_REMREF_LOCKED(rt
);
3331 lck_mtx_lock(nd6_mutex
);
3332 defrouter_select(ifp
);
3333 lck_mtx_unlock(nd6_mutex
);
3335 RT_REMREF_LOCKED(rt
);
3338 if (sched_timeout
) {
3339 lck_mtx_lock(rnh_lock
);
3340 nd6_sched_timeout(NULL
, NULL
);
3341 lck_mtx_unlock(rnh_lock
);
3346 nd6_slowtimo(void *arg
)
3349 struct nd_ifinfo
*nd6if
= NULL
;
3350 struct ifnet
*ifp
= NULL
;
3352 ifnet_head_lock_shared();
3353 for (ifp
= ifnet_head
.tqh_first
; ifp
;
3354 ifp
= ifp
->if_link
.tqe_next
) {
3355 nd6if
= ND_IFINFO(ifp
);
3356 if ((NULL
== nd6if
) || (FALSE
== nd6if
->initialized
)) {
3360 lck_mtx_lock(&nd6if
->lock
);
3361 if (nd6if
->basereachable
&& /* already initialized */
3362 (nd6if
->recalctm
-= ND6_SLOWTIMER_INTERVAL
) <= 0) {
3364 * Since reachable time rarely changes by router
3365 * advertisements, we SHOULD insure that a new random
3366 * value gets recomputed at least once every few hours.
3369 nd6if
->recalctm
= nd6_recalc_reachtm_interval
;
3371 ND_COMPUTE_RTIME(nd6if
->basereachable
);
3373 lck_mtx_unlock(&nd6if
->lock
);
3376 timeout(nd6_slowtimo
, NULL
, ND6_SLOWTIMER_INTERVAL
* hz
);
3380 nd6_output(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
3381 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
3383 return nd6_output_list(ifp
, origifp
, m0
, dst
, hint0
, adv
);
3389 * Assumption: route determination for first packet can be correctly applied to
3390 * all packets in the chain.
3392 #define senderr(e) { error = (e); goto bad; }
3394 nd6_output_list(struct ifnet
*ifp
, struct ifnet
*origifp
, struct mbuf
*m0
,
3395 struct sockaddr_in6
*dst
, struct rtentry
*hint0
, struct flowadv
*adv
)
3397 struct rtentry
*rt
= hint0
, *hint
= hint0
;
3398 struct llinfo_nd6
*ln
= NULL
;
3401 struct rtentry
*rtrele
= NULL
;
3402 struct nd_ifinfo
*ndi
= NULL
;
3406 RT_ADDREF_LOCKED(rt
);
3409 if (IN6_IS_ADDR_MULTICAST(&dst
->sin6_addr
) || !nd6_need_cache(ifp
)) {
3416 * Next hop determination. Because we may involve the gateway route
3417 * in addition to the original route, locking is rather complicated.
3418 * The general concept is that regardless of whether the route points
3419 * to the original route or to the gateway route, this routine takes
3420 * an extra reference on such a route. This extra reference will be
3421 * released at the end.
3423 * Care must be taken to ensure that the "hint0" route never gets freed
3424 * via rtfree(), since the caller may have stored it inside a struct
3425 * route with a reference held for that placeholder.
3427 * This logic is similar to, though not exactly the same as the one
3428 * used by route_to_gwroute().
3432 * We have a reference to "rt" by now (or below via rtalloc1),
3433 * which will either be released or freed at the end of this
3436 RT_LOCK_ASSERT_HELD(rt
);
3437 if (!(rt
->rt_flags
& RTF_UP
)) {
3438 RT_REMREF_LOCKED(rt
);
3440 if ((hint
= rt
= rtalloc1_scoped(SA(dst
), 1, 0,
3441 ifp
->if_index
)) != NULL
) {
3443 if (rt
->rt_ifp
!= ifp
) {
3444 /* XXX: loop care? */
3446 error
= nd6_output_list(ifp
, origifp
, m0
,
3452 senderr(EHOSTUNREACH
);
3456 if (rt
->rt_flags
& RTF_GATEWAY
) {
3457 struct rtentry
*gwrt
;
3458 struct in6_ifaddr
*ia6
= NULL
;
3459 struct sockaddr_in6 gw6
;
3461 rtgw_to_sa6(rt
, &gw6
);
3463 * Must drop rt_lock since nd6_is_addr_neighbor()
3464 * calls nd6_lookup() and acquires rnh_lock.
3469 * We skip link-layer address resolution and NUD
3470 * if the gateway is not a neighbor from ND point
3471 * of view, regardless of the value of nd_ifinfo.flags.
3472 * The second condition is a bit tricky; we skip
3473 * if the gateway is our own address, which is
3474 * sometimes used to install a route to a p2p link.
3476 if (!nd6_is_addr_neighbor(&gw6
, ifp
, 0) ||
3477 (ia6
= in6ifa_ifpwithaddr(ifp
, &gw6
.sin6_addr
))) {
3479 * We allow this kind of tricky route only
3480 * when the outgoing interface is p2p.
3481 * XXX: we may need a more generic rule here.
3484 IFA_REMREF(&ia6
->ia_ifa
);
3485 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
3486 senderr(EHOSTUNREACH
);
3491 gw6
= *(SIN6(rt
->rt_gateway
));
3493 /* If hint is now down, give up */
3494 if (!(rt
->rt_flags
& RTF_UP
)) {
3496 senderr(EHOSTUNREACH
);
3499 /* If there's no gateway route, look it up */
3500 if ((gwrt
= rt
->rt_gwroute
) == NULL
) {
3504 /* Become a regular mutex */
3505 RT_CONVERT_LOCK(rt
);
3508 * Take gwrt's lock while holding route's lock;
3509 * this is okay since gwrt never points back
3510 * to rt, so no lock ordering issues.
3513 if (!(gwrt
->rt_flags
& RTF_UP
)) {
3514 rt
->rt_gwroute
= NULL
;
3519 lck_mtx_lock(rnh_lock
);
3520 gwrt
= rtalloc1_scoped_locked(SA(&gw6
), 1, 0,
3525 * Bail out if the route is down, no route
3526 * to gateway, circular route, or if the
3527 * gateway portion of "rt" has changed.
3529 if (!(rt
->rt_flags
& RTF_UP
) ||
3530 gwrt
== NULL
|| gwrt
== rt
||
3531 !equal(SA(&gw6
), rt
->rt_gateway
)) {
3533 RT_REMREF_LOCKED(gwrt
);
3538 rtfree_locked(gwrt
);
3539 lck_mtx_unlock(rnh_lock
);
3540 senderr(EHOSTUNREACH
);
3542 VERIFY(gwrt
!= NULL
);
3544 * Set gateway route; callee adds ref to gwrt;
3545 * gwrt has an extra ref from rtalloc1() for
3548 rt_set_gwroute(rt
, rt_key(rt
), gwrt
);
3550 lck_mtx_unlock(rnh_lock
);
3551 /* Remember to release/free "rt" at the end */
3555 RT_ADDREF_LOCKED(gwrt
);
3558 /* Remember to release/free "rt" at the end */
3565 * This is an opportunity to revalidate the parent
3566 * route's gwroute, in case it now points to a dead
3567 * route entry. Parent route won't go away since the
3568 * clone (hint) holds a reference to it. rt == gwrt.
3571 if ((hint
->rt_flags
& (RTF_WASCLONED
| RTF_UP
)) ==
3572 (RTF_WASCLONED
| RTF_UP
)) {
3573 struct rtentry
*prt
= hint
->rt_parent
;
3574 VERIFY(prt
!= NULL
);
3576 RT_CONVERT_LOCK(hint
);
3579 rt_revalidate_gwroute(prt
, rt
);
3586 /* rt == gwrt; if it is now down, give up */
3587 if (!(rt
->rt_flags
& RTF_UP
)) {
3591 /* "rtrele" == original "rt" */
3592 senderr(EHOSTUNREACH
);
3596 /* Become a regular mutex */
3597 RT_CONVERT_LOCK(rt
);
3601 * Address resolution or Neighbor Unreachability Detection
3603 * At this point, the destination of the packet must be a unicast
3604 * or an anycast address(i.e. not a multicast).
3607 /* Look up the neighbor cache for the nexthop */
3608 if (rt
&& (rt
->rt_flags
& RTF_LLINFO
) != 0) {
3611 struct sockaddr_in6 sin6
;
3613 * Clear out Scope ID field in case it is set.
3616 sin6
.sin6_scope_id
= 0;
3618 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
3619 * the condition below is not very efficient. But we believe
3620 * it is tolerable, because this should be a rare case.
3621 * Must drop rt_lock since nd6_is_addr_neighbor() calls
3622 * nd6_lookup() and acquires rnh_lock.
3626 if (nd6_is_addr_neighbor(&sin6
, ifp
, 0)) {
3627 /* "rtrele" may have been used, so clean up "rt" now */
3629 /* Don't free "hint0" */
3635 /* Callee returns a locked route upon success */
3636 rt
= nd6_lookup(&dst
->sin6_addr
, 1, ifp
, 0);
3638 RT_LOCK_ASSERT_HELD(rt
);
3641 } else if (rt
!= NULL
) {
3650 ndi
= ND_IFINFO(ifp
);
3651 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
3652 lck_mtx_lock(&ndi
->lock
);
3653 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0 &&
3654 !(ndi
->flags
& ND6_IFF_PERFORMNUD
)) {
3655 lck_mtx_unlock(&ndi
->lock
);
3657 "nd6_output: can't allocate llinfo for %s "
3658 "(ln=0x%llx, rt=0x%llx)\n",
3659 ip6_sprintf(&dst
->sin6_addr
),
3660 (uint64_t)VM_KERNEL_ADDRPERM(ln
),
3661 (uint64_t)VM_KERNEL_ADDRPERM(rt
));
3662 senderr(EIO
); /* XXX: good error? */
3664 lck_mtx_unlock(&ndi
->lock
);
3666 goto sendpkt
; /* send anyway */
3669 net_update_uptime();
3670 timenow
= net_uptime();
3672 /* We don't have to do link-layer address resolution on a p2p link. */
3673 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
3674 ln
->ln_state
< ND6_LLINFO_REACHABLE
) {
3675 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_STALE
);
3676 ln_setexpire(ln
, timenow
+ nd6_gctimer
);
3680 * The first time we send a packet to a neighbor whose entry is
3681 * STALE, we have to change the state to DELAY and a sets a timer to
3682 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
3683 * neighbor unreachability detection on expiration.
3686 if (ln
->ln_state
== ND6_LLINFO_STALE
) {
3688 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_DELAY
);
3689 ln_setexpire(ln
, timenow
+ nd6_delay
);
3690 /* N.B.: we will re-arm the timer below. */
3691 _CASSERT(ND6_LLINFO_DELAY
> ND6_LLINFO_INCOMPLETE
);
3695 * If the neighbor cache entry has a state other than INCOMPLETE
3696 * (i.e. its link-layer address is already resolved), just
3699 if (ln
->ln_state
> ND6_LLINFO_INCOMPLETE
) {
3702 * Move this entry to the head of the queue so that it is
3703 * less likely for this entry to be a target of forced
3704 * garbage collection (see nd6_rtrequest()). Do this only
3705 * if the entry is non-permanent (as permanent ones will
3706 * never be purged), and if the number of active entries
3707 * is at least half of the threshold.
3709 if (ln
->ln_state
== ND6_LLINFO_DELAY
||
3710 (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3711 nd6_inuse
>= (ip6_neighborgcthresh
>> 1))) {
3712 lck_mtx_lock(rnh_lock
);
3713 if (ln
->ln_state
== ND6_LLINFO_DELAY
)
3714 nd6_sched_timeout(NULL
, NULL
);
3715 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3716 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
3718 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
3724 lck_mtx_unlock(rnh_lock
);
3730 * If this is a prefix proxy route, record the inbound interface
3731 * so that it can be excluded from the list of interfaces eligible
3732 * for forwarding the proxied NS in nd6_prproxy_ns_output().
3734 if (rt
->rt_flags
& RTF_PROXY
)
3735 ln
->ln_exclifp
= ((origifp
== ifp
) ? NULL
: origifp
);
3738 * There is a neighbor cache entry, but no ethernet address
3739 * response yet. Replace the held mbuf (if any) with this
3742 * This code conforms to the rate-limiting rule described in Section
3743 * 7.2.2 of RFC 4861, because the timer is set correctly after sending
3746 if (ln
->ln_state
== ND6_LLINFO_NOSTATE
)
3747 ND6_CACHE_STATE_TRANSITION(ln
, ND6_LLINFO_INCOMPLETE
);
3749 m_freem_list(ln
->ln_hold
);
3751 if (ln
->ln_expire
!= 0 && ln
->ln_asked
< nd6_mmaxtries
&&
3752 ln
->ln_expire
<= timenow
) {
3754 ndi
= ND_IFINFO(ifp
);
3755 VERIFY(ndi
!= NULL
&& ndi
->initialized
);
3756 lck_mtx_lock(&ndi
->lock
);
3757 ln_setexpire(ln
, timenow
+ ndi
->retrans
/ 1000);
3758 lck_mtx_unlock(&ndi
->lock
);
3760 /* We still have a reference on rt (for ln) */
3762 nd6_prproxy_ns_output(ifp
, origifp
, NULL
,
3763 &dst
->sin6_addr
, ln
);
3765 nd6_ns_output(ifp
, NULL
, &dst
->sin6_addr
, ln
, NULL
);
3766 lck_mtx_lock(rnh_lock
);
3767 nd6_sched_timeout(NULL
, NULL
);
3768 lck_mtx_unlock(rnh_lock
);
3770 if(ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
3771 ln_setexpire(ln
, timenow
);
3776 * Move this entry to the head of the queue so that it is
3777 * less likely for this entry to be a target of forced
3778 * garbage collection (see nd6_rtrequest()). Do this only
3779 * if the entry is non-permanent (as permanent ones will
3780 * never be purged), and if the number of active entries
3781 * is at least half of the threshold.
3783 if (ln
->ln_expire
!= 0 && ip6_neighborgcthresh
> 0 &&
3784 nd6_inuse
>= (ip6_neighborgcthresh
>> 1)) {
3785 lck_mtx_lock(rnh_lock
);
3787 if (ln
->ln_flags
& ND6_LNF_IN_USE
) {
3791 /* Clean up "rt" now while we can */
3793 RT_REMREF_LOCKED(rt
);
3799 rt
= NULL
; /* "rt" has been taken care of */
3800 lck_mtx_unlock(rnh_lock
);
3807 RT_LOCK_ASSERT_NOTHELD(rt
);
3809 /* discard the packet if IPv6 operation is disabled on the interface */
3810 if (ifp
->if_eflags
& IFEF_IPV6_DISABLED
) {
3811 error
= ENETDOWN
; /* better error? */
3815 if (ifp
->if_flags
& IFF_LOOPBACK
) {
3816 /* forwarding rules require the original scope_id */
3817 m0
->m_pkthdr
.rcvif
= origifp
;
3818 error
= dlil_output(origifp
, PF_INET6
, m0
, (caddr_t
)rt
,
3822 /* Do not allow loopback address to wind up on a wire */
3823 struct ip6_hdr
*ip6
= mtod(m0
, struct ip6_hdr
*);
3825 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
3826 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
))) {
3827 ip6stat
.ip6s_badscope
++;
3828 error
= EADDRNOTAVAIL
;
3835 /* Mark use timestamp */
3836 if (rt
->rt_llinfo
!= NULL
)
3837 nd6_llreach_use(rt
->rt_llinfo
);
3841 struct mbuf
*mcur
= m0
;
3842 uint32_t pktcnt
= 0;
3845 if (hint
!= NULL
&& nstat_collect
) {
3848 if ((mcur
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
) &&
3849 (mcur
->m_pkthdr
.tso_segsz
> 0))
3850 scnt
= mcur
->m_pkthdr
.len
/ mcur
->m_pkthdr
.tso_segsz
;
3854 nstat_route_tx(hint
, scnt
, mcur
->m_pkthdr
.len
, 0);
3858 mcur
->m_pkthdr
.rcvif
= NULL
;
3859 mcur
= mcur
->m_nextpkt
;
3861 if (pktcnt
> ip6_maxchainsent
)
3862 ip6_maxchainsent
= pktcnt
;
3863 error
= dlil_output(ifp
, PF_INET6
, m0
, (caddr_t
)rt
, SA(dst
), 0, adv
);
3871 /* Clean up "rt" unless it's already been done */
3875 RT_REMREF_LOCKED(rt
);
3882 /* And now clean up "rtrele" if there is any */
3883 if (rtrele
!= NULL
) {
3884 RT_LOCK_SPIN(rtrele
);
3885 if (rtrele
== hint0
) {
3886 RT_REMREF_LOCKED(rtrele
);
3898 nd6_need_cache(struct ifnet
*ifp
)
3901 * XXX: we currently do not make neighbor cache on any interface
3902 * other than ARCnet, Ethernet, FDDI and GIF.
3905 * - unidirectional tunnels needs no ND
3907 switch (ifp
->if_type
) {
3913 case IFT_IEEE8023ADLAG
:
3917 case IFT_GIF
: /* XXX need more cases? */
3931 nd6_storelladdr(struct ifnet
*ifp
, struct rtentry
*rt
, struct mbuf
*m
,
3932 struct sockaddr
*dst
, u_char
*desten
)
3935 struct sockaddr_dl
*sdl
;
3937 if (m
->m_flags
& M_MCAST
) {
3938 switch (ifp
->if_type
) {
3942 case IFT_IEEE8023ADLAG
:
3947 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst
)->sin6_addr
, desten
);
3950 for (i
= 0; i
< ifp
->if_addrlen
; i
++)
3957 return (0); /* caller will free mbuf */
3962 /* this could happen, if we could not allocate memory */
3963 return (0); /* caller will free mbuf */
3966 if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
3967 printf("nd6_storelladdr: something odd happens\n");
3969 return (0); /* caller will free mbuf */
3971 sdl
= SDL(rt
->rt_gateway
);
3972 if (sdl
->sdl_alen
== 0) {
3973 /* this should be impossible, but we bark here for debugging */
3974 printf("nd6_storelladdr: sdl_alen == 0\n");
3976 return (0); /* caller will free mbuf */
3979 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
3985 * This is the ND pre-output routine; care must be taken to ensure that
3986 * the "hint" route never gets freed via rtfree(), since the caller may
3987 * have stored it inside a struct route with a reference held for that
3991 nd6_lookup_ipv6(ifnet_t ifp
, const struct sockaddr_in6
*ip6_dest
,
3992 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
3995 route_t route
= hint
;
3997 struct sockaddr_dl
*sdl
= NULL
;
4000 if (ifp
== NULL
|| ip6_dest
== NULL
)
4003 if (ip6_dest
->sin6_family
!= AF_INET6
)
4004 return (EAFNOSUPPORT
);
4006 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
4011 * Callee holds a reference on the route and returns
4012 * with the route entry locked, upon success.
4014 result
= route_to_gwroute((const struct sockaddr
*)ip6_dest
,
4019 RT_LOCK_ASSERT_HELD(route
);
4022 if ((packet
!= NULL
&& (packet
->m_flags
& M_MCAST
) != 0) ||
4023 ((ifp
->if_flags
& IFF_MULTICAST
) &&
4024 IN6_IS_ADDR_MULTICAST(&ip6_dest
->sin6_addr
))) {
4027 result
= dlil_resolve_multi(ifp
,
4028 (const struct sockaddr
*)ip6_dest
,
4029 SA(ll_dest
), ll_dest_len
);
4033 } else if (route
== NULL
) {
4036 * For unicast, lookup existing ND6 entries but
4037 * do not trigger a resolution
4039 lck_mtx_lock(rnh_lock
);
4040 route
= rt_lookup(TRUE
,
4041 __DECONST(struct sockaddr
*, ip6_dest
), NULL
,
4042 rt_tables
[AF_INET6
], ifp
->if_index
);
4043 lck_mtx_unlock(rnh_lock
);
4045 if (route
!= NULL
) {
4050 if (route
== NULL
) {
4052 * This could happen, if we could not allocate memory or
4053 * if route_to_gwroute() didn't return a route.
4059 if (route
->rt_gateway
->sa_family
!= AF_LINK
) {
4060 printf("%s: route %s on %s%d gateway address not AF_LINK\n",
4061 __func__
, ip6_sprintf(&ip6_dest
->sin6_addr
),
4062 route
->rt_ifp
->if_name
, route
->rt_ifp
->if_unit
);
4063 result
= EADDRNOTAVAIL
;
4067 sdl
= SDL(route
->rt_gateway
);
4068 if (sdl
->sdl_alen
== 0) {
4069 /* this should be impossible, but we bark here for debugging */
4070 printf("%s: route %s on %s%d sdl_alen == 0\n", __func__
,
4071 ip6_sprintf(&ip6_dest
->sin6_addr
), route
->rt_ifp
->if_name
,
4072 route
->rt_ifp
->if_unit
);
4073 result
= EHOSTUNREACH
;
4077 copy_len
= sdl
->sdl_len
<= ll_dest_len
? sdl
->sdl_len
: ll_dest_len
;
4078 bcopy(sdl
, ll_dest
, copy_len
);
4081 if (route
!= NULL
) {
4082 if (route
== hint
) {
4083 RT_REMREF_LOCKED(route
);
4093 #if (DEVELOPMENT || DEBUG)
4095 static int sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
;
4096 SYSCTL_PROC(_net_inet6_icmp6
, OID_AUTO
, nd6_lookup_ipv6
,
4097 CTLTYPE_STRUCT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0,
4098 sysctl_nd6_lookup_ipv6
, "S", "");
4101 sysctl_nd6_lookup_ipv6 SYSCTL_HANDLER_ARGS
4103 #pragma unused(oidp, arg1, arg2)
4105 struct nd6_lookup_ipv6_args nd6_lookup_ipv6_args
;
4109 * Only root can lookup MAC addresses
4111 error
= proc_suser(current_proc());
4113 printf("%s: proc_suser() error %d\n",
4117 if (req
->oldptr
== USER_ADDR_NULL
) {
4118 req
->oldidx
= sizeof(struct nd6_lookup_ipv6_args
);
4120 if (req
->newptr
== USER_ADDR_NULL
) {
4123 if (req
->oldlen
!= sizeof(struct nd6_lookup_ipv6_args
) ||
4124 req
->newlen
!= sizeof(struct nd6_lookup_ipv6_args
)) {
4126 printf("%s: bad req, error %d\n",
4130 error
= SYSCTL_IN(req
, &nd6_lookup_ipv6_args
,
4131 sizeof(struct nd6_lookup_ipv6_args
));
4133 printf("%s: SYSCTL_IN() error %d\n",
4137 /* Make sure to terminate the string */
4138 nd6_lookup_ipv6_args
.ifname
[IFNAMSIZ
- 1] = 0;
4140 error
= ifnet_find_by_name(nd6_lookup_ipv6_args
.ifname
, &ifp
);
4142 printf("%s: ifnet_find_by_name() error %d\n",
4147 error
= nd6_lookup_ipv6(ifp
, &nd6_lookup_ipv6_args
.ip6_dest
,
4148 &nd6_lookup_ipv6_args
.ll_dest_
._sdl
,
4149 nd6_lookup_ipv6_args
.ll_dest_len
, NULL
, NULL
);
4151 printf("%s: nd6_lookup_ipv6() error %d\n",
4156 error
= SYSCTL_OUT(req
, &nd6_lookup_ipv6_args
,
4157 sizeof(struct nd6_lookup_ipv6_args
));
4159 printf("%s: SYSCTL_OUT() error %d\n",
4167 #endif /* (DEVELOPEMENT || DEBUG) */
4170 nd6_setifinfo(struct ifnet
*ifp
, u_int32_t before
, u_int32_t after
)
4176 * Handle ND6_IFF_IFDISABLED
4178 if ((before
& ND6_IFF_IFDISABLED
) ||
4179 (after
& ND6_IFF_IFDISABLED
)) {
4180 b
= (before
& ND6_IFF_IFDISABLED
);
4181 a
= (after
& ND6_IFF_IFDISABLED
);
4183 if (b
!= a
&& (err
= nd6_if_disable(ifp
,
4184 ((int32_t)(a
- b
) > 0))) != 0)
4189 * Handle ND6_IFF_PROXY_PREFIXES
4191 if ((before
& ND6_IFF_PROXY_PREFIXES
) ||
4192 (after
& ND6_IFF_PROXY_PREFIXES
)) {
4193 b
= (before
& ND6_IFF_PROXY_PREFIXES
);
4194 a
= (after
& ND6_IFF_PROXY_PREFIXES
);
4196 if (b
!= a
&& (err
= nd6_if_prproxy(ifp
,
4197 ((int32_t)(a
- b
) > 0))) != 0)
4205 * Enable/disable IPv6 on an interface, called as part of
4206 * setting/clearing ND6_IFF_IFDISABLED, or during DAD failure.
4209 nd6_if_disable(struct ifnet
*ifp
, boolean_t enable
)
4211 ifnet_lock_shared(ifp
);
4213 ifp
->if_eflags
|= IFEF_IPV6_DISABLED
;
4215 ifp
->if_eflags
&= ~IFEF_IPV6_DISABLED
;
4216 ifnet_lock_done(ifp
);
4222 nd6_sysctl_drlist SYSCTL_HANDLER_ARGS
4224 #pragma unused(oidp, arg1, arg2)
4225 char pbuf
[MAX_IPv6_STR_LEN
];
4226 struct nd_defrouter
*dr
;
4229 if (req
->newptr
!= USER_ADDR_NULL
)
4232 /* XXX Handle mapped defrouter entries */
4233 lck_mtx_lock(nd6_mutex
);
4234 if (proc_is64bit(req
->p
)) {
4235 struct in6_defrouter_64 d
;
4237 bzero(&d
, sizeof (d
));
4238 d
.rtaddr
.sin6_family
= AF_INET6
;
4239 d
.rtaddr
.sin6_len
= sizeof (d
.rtaddr
);
4241 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
4242 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
4243 if (in6_recoverscope(&d
.rtaddr
,
4244 &dr
->rtaddr
, dr
->ifp
) != 0)
4245 log(LOG_ERR
, "scope error in default router "
4246 "list (%s)\n", inet_ntop(AF_INET6
,
4247 &dr
->rtaddr
, pbuf
, sizeof (pbuf
)));
4248 d
.flags
= dr
->flags
;
4249 d
.stateflags
= dr
->stateflags
;
4250 d
.rtlifetime
= dr
->rtlifetime
;
4251 d
.expire
= nddr_getexpire(dr
);
4252 d
.if_index
= dr
->ifp
->if_index
;
4253 error
= SYSCTL_OUT(req
, &d
, sizeof (d
));
4258 struct in6_defrouter_32 d
;
4260 bzero(&d
, sizeof (d
));
4261 d
.rtaddr
.sin6_family
= AF_INET6
;
4262 d
.rtaddr
.sin6_len
= sizeof (d
.rtaddr
);
4264 TAILQ_FOREACH(dr
, &nd_defrouter
, dr_entry
) {
4265 d
.rtaddr
.sin6_addr
= dr
->rtaddr
;
4266 if (in6_recoverscope(&d
.rtaddr
,
4267 &dr
->rtaddr
, dr
->ifp
) != 0)
4268 log(LOG_ERR
, "scope error in default router "
4269 "list (%s)\n", inet_ntop(AF_INET6
,
4270 &dr
->rtaddr
, pbuf
, sizeof (pbuf
)));
4271 d
.flags
= dr
->flags
;
4272 d
.stateflags
= dr
->stateflags
;
4273 d
.rtlifetime
= dr
->rtlifetime
;
4274 d
.expire
= nddr_getexpire(dr
);
4275 d
.if_index
= dr
->ifp
->if_index
;
4276 error
= SYSCTL_OUT(req
, &d
, sizeof (d
));
4281 lck_mtx_unlock(nd6_mutex
);
4286 nd6_sysctl_prlist SYSCTL_HANDLER_ARGS
4288 #pragma unused(oidp, arg1, arg2)
4289 char pbuf
[MAX_IPv6_STR_LEN
];
4290 struct nd_pfxrouter
*pfr
;
4291 struct sockaddr_in6 s6
;
4292 struct nd_prefix
*pr
;
4295 if (req
->newptr
!= USER_ADDR_NULL
)
4298 bzero(&s6
, sizeof (s6
));
4299 s6
.sin6_family
= AF_INET6
;
4300 s6
.sin6_len
= sizeof (s6
);
4302 /* XXX Handle mapped defrouter entries */
4303 lck_mtx_lock(nd6_mutex
);
4304 if (proc_is64bit(req
->p
)) {
4305 struct in6_prefix_64 p
;
4307 bzero(&p
, sizeof (p
));
4308 p
.origin
= PR_ORIG_RA
;
4310 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
4312 p
.prefix
= pr
->ndpr_prefix
;
4313 if (in6_recoverscope(&p
.prefix
,
4314 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
4315 log(LOG_ERR
, "scope error in "
4316 "prefix list (%s)\n", inet_ntop(AF_INET6
,
4317 &p
.prefix
.sin6_addr
, pbuf
, sizeof (pbuf
)));
4318 p
.raflags
= pr
->ndpr_raf
;
4319 p
.prefixlen
= pr
->ndpr_plen
;
4320 p
.vltime
= pr
->ndpr_vltime
;
4321 p
.pltime
= pr
->ndpr_pltime
;
4322 p
.if_index
= pr
->ndpr_ifp
->if_index
;
4323 p
.expire
= ndpr_getexpire(pr
);
4324 p
.refcnt
= pr
->ndpr_addrcnt
;
4325 p
.flags
= pr
->ndpr_stateflags
;
4327 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
4329 error
= SYSCTL_OUT(req
, &p
, sizeof (p
));
4334 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
4335 s6
.sin6_addr
= pfr
->router
->rtaddr
;
4336 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
4337 pfr
->router
->ifp
) != 0)
4339 "scope error in prefix list (%s)\n",
4340 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
4341 pbuf
, sizeof (pbuf
)));
4342 error
= SYSCTL_OUT(req
, &s6
, sizeof (s6
));
4351 struct in6_prefix_32 p
;
4353 bzero(&p
, sizeof (p
));
4354 p
.origin
= PR_ORIG_RA
;
4356 LIST_FOREACH(pr
, &nd_prefix
, ndpr_entry
) {
4358 p
.prefix
= pr
->ndpr_prefix
;
4359 if (in6_recoverscope(&p
.prefix
,
4360 &pr
->ndpr_prefix
.sin6_addr
, pr
->ndpr_ifp
) != 0)
4362 "scope error in prefix list (%s)\n",
4363 inet_ntop(AF_INET6
, &p
.prefix
.sin6_addr
,
4364 pbuf
, sizeof (pbuf
)));
4365 p
.raflags
= pr
->ndpr_raf
;
4366 p
.prefixlen
= pr
->ndpr_plen
;
4367 p
.vltime
= pr
->ndpr_vltime
;
4368 p
.pltime
= pr
->ndpr_pltime
;
4369 p
.if_index
= pr
->ndpr_ifp
->if_index
;
4370 p
.expire
= ndpr_getexpire(pr
);
4371 p
.refcnt
= pr
->ndpr_addrcnt
;
4372 p
.flags
= pr
->ndpr_stateflags
;
4374 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
)
4376 error
= SYSCTL_OUT(req
, &p
, sizeof (p
));
4381 LIST_FOREACH(pfr
, &pr
->ndpr_advrtrs
, pfr_entry
) {
4382 s6
.sin6_addr
= pfr
->router
->rtaddr
;
4383 if (in6_recoverscope(&s6
, &pfr
->router
->rtaddr
,
4384 pfr
->router
->ifp
) != 0)
4386 "scope error in prefix list (%s)\n",
4387 inet_ntop(AF_INET6
, &s6
.sin6_addr
,
4388 pbuf
, sizeof (pbuf
)));
4389 error
= SYSCTL_OUT(req
, &s6
, sizeof (s6
));
4398 lck_mtx_unlock(nd6_mutex
);
4404 in6_ifaddr_set_dadprogress(struct in6_ifaddr
*ia
)
4406 struct ifnet
* ifp
= ia
->ia_ifp
;
4407 uint32_t flags
= IN6_IFF_TENTATIVE
;
4408 uint32_t optdad
= nd6_optimistic_dad
;
4409 struct nd_ifinfo
*ndi
= NULL
;
4411 ndi
= ND_IFINFO(ifp
);
4412 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
4413 if (!(ndi
->flags
& ND6_IFF_DAD
))
4417 if ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) != 0) {
4420 lck_mtx_lock(&ndi
->lock
);
4421 if ((ndi
->flags
& ND6_IFF_REPLICATED
) != 0) {
4424 lck_mtx_unlock(&ndi
->lock
);
4429 if ((optdad
& ND6_OPTIMISTIC_DAD_LINKLOCAL
) &&
4430 IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
))
4431 flags
= IN6_IFF_OPTIMISTIC
;
4432 else if ((optdad
& ND6_OPTIMISTIC_DAD_AUTOCONF
) &&
4433 (ia
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
4434 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
4435 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
)
4436 flags
= IN6_IFF_OPTIMISTIC
;
4437 } else if (ia
->ia6_flags
& IN6_IFF_SECURED
) {
4438 if (optdad
& ND6_OPTIMISTIC_DAD_SECURED
)
4439 flags
= IN6_IFF_OPTIMISTIC
;
4442 * Keeping the behavior for temp and CGA
4443 * SLAAC addresses to have a knob for optimistic
4445 * Other than that if ND6_OPTIMISTIC_DAD_AUTOCONF
4446 * is set, we should default to optimistic
4448 * For now this means SLAAC addresses with interface
4449 * identifier derived from modified EUI-64 bit
4452 flags
= IN6_IFF_OPTIMISTIC
;
4454 } else if ((optdad
& ND6_OPTIMISTIC_DAD_DYNAMIC
) &&
4455 (ia
->ia6_flags
& IN6_IFF_DYNAMIC
)) {
4456 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
4457 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
)
4458 flags
= IN6_IFF_OPTIMISTIC
;
4460 flags
= IN6_IFF_OPTIMISTIC
;
4462 } else if ((optdad
& ND6_OPTIMISTIC_DAD_MANUAL
) &&
4463 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)) {
4466 * Bypass tentative for address assignments
4467 * not covered above (e.g. manual) upon request
4469 if (!IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
) &&
4470 !(ia
->ia6_flags
& IN6_IFF_AUTOCONF
) &&
4471 !(ia
->ia6_flags
& IN6_IFF_DYNAMIC
))
4472 flags
= IN6_IFF_OPTIMISTIC
;
4476 ia
->ia6_flags
&= ~(IN6_IFF_DUPLICATED
| IN6_IFF_DADPROGRESS
);
4477 ia
->ia6_flags
|= flags
;
4479 nd6log2((LOG_DEBUG
, "%s - %s ifp %s ia6_flags 0x%x\n",
4481 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
4482 if_name(ia
->ia_ifp
),